-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchunk.ts
More file actions
57 lines (45 loc) · 1.3 KB
/
chunk.ts
File metadata and controls
57 lines (45 loc) · 1.3 KB
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
// buffer -> b64 -> msgpack encoding
function serializeData(obj: any): any {
if (obj === null || obj === undefined) return obj;
if (Buffer.isBuffer(obj)) {
return {
__type: "Buffer",
__data: obj.toString("base64")
};
}
if (Array.isArray(obj)) {
return obj.map(serializeData);
}
if (typeof obj === "object") {
const result: any = {};
for (const key in obj) {
if (obj.hasOwnProperty(key)) {
result[key] = serializeData(obj[key]);
}
}
return result;
}
return obj;
}
// msgpack decoding -> b64 -> buffer
function deserializeData(obj: any): any {
if (obj === null || obj === undefined) return obj;
// restore marked buffers
if (typeof obj === "object" && obj.__type === "Buffer" && typeof obj.__data === "string") {
return Buffer.from(obj.__data, "base64");
}
if (Array.isArray(obj)) {
return obj.map(deserializeData);
}
if (typeof obj === "object") {
const result: any = {};
for (const key in obj) {
if (obj.hasOwnProperty(key)) {
result[key] = deserializeData(obj[key]);
}
}
return result;
}
return obj;
}
export { serializeData, deserializeData };