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 126 127 | 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 6x 6x 6x 6x 6x 6x 3x 3379x 3316x 924x 924x 808x 808x 337x 337x 436x 8x 4x 4x 2x 2x 924x 2392x 2359x 4014x 4014x 1964x 1964x 1689x 1039x 9x 9x 1030x 2x 1028x 3x 2359x 33x 8x 5x 3x 2x 1x 25x 3x 1078x 1078x 3x 19x 3x 2348x 2344x 2325x 925x 925x 925x 812x 812x 925x 1400x 1394x 1388x 1360x 1360x 3015x 3015x 1360x 28x 3x 82x 82x | import {JsonPackExtension} from '../JsonPackExtension';
import {JsonPackValue} from '../JsonPackValue';
import {fromBase64} from '@jsonjoy.com/base64/lib/fromBase64';
import {toBase64} from '@jsonjoy.com/base64/lib/toBase64';
import {isUint8Array} from '@jsonjoy.com/buffers/lib/isUint8Array';
import {binUriStart, msgPackExtStart, msgPackUriStart} from './constants';
import type {binary_string} from './types';
const binUriStartLength = binUriStart.length;
const msgPackUriStartLength = msgPackUriStart.length;
const msgPackExtStartLength = msgPackExtStart.length;
const minDataUri = Math.min(binUriStartLength, msgPackUriStartLength);
const parseExtDataUri = (uri: string): JsonPackExtension => {
uri = uri.substring(msgPackExtStartLength);
const commaIndex = uri.indexOf(',');
Iif (commaIndex === -1) throw new Error('INVALID_EXT_DATA_URI');
const typeString = uri.substring(0, commaIndex);
const buf = fromBase64(uri.substring(commaIndex + 1));
return new JsonPackExtension(Number(typeString), buf);
};
/**
* Replaces strings with Uint8Arrays in-place.
*/
export const unwrapBinary = (value: unknown): unknown => {
if (!value) return value;
if (value instanceof Array) {
const len = value.length;
for (let i = 0; i < len; i++) {
const item = value[i];
switch (typeof item) {
case 'object': {
unwrapBinary(item);
continue;
}
case 'string': {
if (item.length < minDataUri) continue;
if (item.substring(0, binUriStartLength) === binUriStart)
value[i] = fromBase64(item.substring(binUriStartLength));
else if (item.substring(0, msgPackUriStartLength) === msgPackUriStart)
value[i] = new JsonPackValue(fromBase64(item.substring(msgPackUriStartLength)));
else if (item.substring(0, msgPackExtStartLength) === msgPackExtStart) value[i] = parseExtDataUri(item);
}
}
}
return value;
}
if (typeof value === 'object') {
for (const key in value) {
const item = (value as any)[key];
switch (typeof item) {
case 'object': {
unwrapBinary(item);
continue;
}
case 'string': {
if (item.length < minDataUri) continue;
if (item.substring(0, binUriStartLength) === binUriStart) {
const buf = fromBase64(item.substring(binUriStartLength));
(value as any)[key] = buf;
} else if (item.substring(0, msgPackUriStartLength) === msgPackUriStart) {
(value as any)[key] = new JsonPackValue(fromBase64(item.substring(msgPackUriStartLength)));
} else if (item.substring(0, msgPackExtStartLength) === msgPackExtStart)
(value as any)[key] = parseExtDataUri(item);
}
}
}
return value;
}
if (typeof value === 'string') {
if (value.length < minDataUri) return value;
if (value.substring(0, binUriStartLength) === binUriStart) return fromBase64(value.substring(binUriStartLength));
if (value.substring(0, msgPackUriStartLength) === msgPackUriStart)
return new JsonPackValue(fromBase64(value.substring(msgPackUriStartLength)));
if (value.substring(0, msgPackExtStartLength) === msgPackExtStart) return parseExtDataUri(value);
else Ereturn value;
}
return value;
};
export const parse = (json: string): unknown => {
const parsed = JSON.parse(json);
return unwrapBinary(parsed);
};
export const stringifyBinary = <T extends Uint8Array>(value: T): binary_string<T> =>
<binary_string<T>>(binUriStart + toBase64(value));
/**
* Replaces Uint8Arrays with strings, returns a new structure,
* without mutating the original.
*/
export const wrapBinary = (value: unknown): unknown => {
if (!value) return value;
if (isUint8Array(value)) return stringifyBinary(value);
if (value instanceof Array) {
const out: unknown[] = [];
const len = value.length;
for (let i = 0; i < len; i++) {
const item = value[i];
out.push(!item || typeof item !== 'object' ? item : wrapBinary(item));
}
return out;
}
if (value instanceof JsonPackValue) return msgPackUriStart + toBase64(value.val);
if (value instanceof JsonPackExtension) return msgPackExtStart + value.tag + ',' + toBase64(value.val);
if (typeof value === 'object') {
const out: {[key: string]: unknown} = {};
for (const key in value) {
const item = (value as any)[key];
out[key] = !item || typeof item !== 'object' ? item : wrapBinary(item);
}
return out;
}
return value;
};
type Stringify =
| ((value: any, replacer?: (this: any, key: string, value: any) => any, space?: string | number) => string)
| ((value: any, replacer?: (number | string)[] | null, space?: string | number) => string);
export const stringify: Stringify = (value: unknown, replacer: any, space: any) => {
const wrapped = wrapBinary(value);
return JSON.stringify(wrapped, replacer, space);
};
|