All files / global/helpers/api unenvelope.js

97.67% Statements 42/43
94.73% Branches 18/19
100% Functions 7/7
97.61% Lines 41/42

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125                                      28x 20x     8x 7x     1x       69x   1x     1x     30x     2x     10x     2x     5x     17x     1x             32x 30x 2x 2x         32x 3x     29x         5x   5x 9x     5x         17x   17x 33x     17x         2x   2x 24x     2x       3x 3x 3x   3x 4384x     3x 3x    
/*
  Copyright (C) 2026 Sber
 
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at
 
          http://www.apache.org/licenses/LICENSE-2.0
 
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/
 
import { strFromU8, inflateSync } from 'fflate';
 
export function unenvelopeDocument(document) {
    if (document.docType === 'application/json') {
        return decodeValue(document.root);
    }
 
    if (document.docType === 'text/plain') {
        return decodeValue(document.root);
    }
 
    throw new Error(`Unsupported docType: ${document.docType}`);
}
 
function decodeValue(value) {
    switch (value.t) {
        case 'null':
            return null;
 
        case 'bool':
            return value.v;
 
        case 'str':
            return decodeStringValue(value);
 
        case 'txt':
            return decodeStringValue(value);
 
        case 'i32':
            return value.v;
 
        case 'dec':
            return Number(value.v);
 
        case 'arr':
            return decodeArrayValue(value);
 
        case 'obj':
            return decodeObjectValue(value);
 
        default:
            throw new Error(`Unsupported value type: ${value.t}`);
    }
}
 
function decodeStringValue(value) {
    let str;
 
    if (value.t === 'str') {
        str = value.v;
    } else if (value.t === 'txt') {
        str = decodeTxtChunks(value.v);
    } else E{
        throw new Error(`Invalid string container: ${value.t}`);
    }
 
    if (value.compressed) {
        return decompressString(str);
    }
 
    return str;
}
 
// Без сортировки. С допущением, что последовательность сохранена
function decodeArrayValue(value) {
    const result = [];
 
    for (const entry of Object.values(value.v)) {
        result.push(decodeValue(entry));
    }
 
    return result;
}
 
// Без сортировки. С допущением, что последовательность сохранена
function decodeObjectValue(value) {
    const result = {};
 
    for (const entry of Object.values(value.v)) {
        result[entry.k] = decodeValue(entry.v);
    }
 
    return result;
}
 
// Без сортировки. С допущением, что последовательность сохранена
function decodeTxtChunks(chunks) {
    let result = '';
 
    for (const chunk of Object.values(chunks)) {
        result += chunk;
    }
 
    return result;
}
 
function decompressString(value) {
    const binary = atob(value);
    const len = binary.length;
    const bytes = new Uint8Array(len);
 
    for (let i = 0; i < len; i++) {
        bytes[i] = binary.charCodeAt(i);
    }
 
    const decompressed = inflateSync(bytes);
    return strFromU8(decompressed);
}