-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
89 lines (82 loc) · 1.93 KB
/
index.ts
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
const TypedArrays = [
'Int8Array',
'Uint8Array',
'Uint8ClampedArray',
'Int16Array',
'Uint16Array',
'Int32Array',
'Uint32Array',
'BigInt64Array',
'BigUint64Array',
'Float32Array',
'Float64Array',
] as const;
function toBase64(buffer: ArrayBuffer) {
let binary = '';
let bytes = new Uint8Array(buffer);
for (let i = 0; i < bytes.byteLength; i++) {
binary += String.fromCharCode(bytes[i]);
}
return btoa(binary);
}
function fromBase64(base64: string) {
let binary = atob(base64);
let bytes = new Uint8Array(binary.length);
for (let i = 0; i < binary.length; i++) {
bytes[i] = binary.charCodeAt(i);
}
return bytes.buffer;
}
export function createParser(
arrayBufferToBase64 = toBase64,
base64ToArrayBuffer = fromBase64
) {
return {
parse(
text: string,
reviver?: ((this: any, key: string, value: any) => any) | undefined
) {
return JSON.parse(text, function (key, value) {
if (typeof value === 'string') {
let [type, data] = value.split(':');
if (TypedArrays.includes(type as any)) {
value = new globalThis[
type as typeof TypedArrays[number]
](base64ToArrayBuffer(data));
}
}
if (typeof reviver === 'function') {
value = reviver.call(this, key, value);
}
return value;
});
},
stringify(
value: any,
replacer?:
| (number | string)[]
| null
| ((this: any, key: string, value: any) => any),
space?: string | number
) {
return JSON.stringify(
value,
function (key, value) {
if (Array.isArray(replacer))
if (!Array.isArray(this) && key && !(key in replacer))
return undefined;
if (typeof replacer === 'function')
value = replacer.call(this, key, value);
if (TypedArrays.includes(value?.constructor.name)) {
value =
value.constructor.name +
':' +
arrayBufferToBase64(value.buffer);
}
return value;
},
space
);
},
} as Pick<JSON, 'parse' | 'stringify'>;
}