-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserialize.js
97 lines (90 loc) · 2.57 KB
/
serialize.js
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
const alphabet = "!$&'()*+-.0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz=";
export function url_btoa(data)
{
let result = "";
for(let i = 0; i < data.length; i += 3)
{
const a = data[i];
const b = data[i + 1];
const c = data[i + 2];
const aIndex = a >> 2;
const bIndex = ((a & 3) << 4) | (b >> 4);
const cIndex = ((b & 15) << 2) | (c >> 6);
const dIndex = c & 63;
result +=
alphabet.charAt(aIndex) +
alphabet.charAt(bIndex) +
(i + 1 < data.length ? alphabet.charAt(cIndex) : "") +
(i + 2 < data.length ? alphabet.charAt(dIndex) : "");
}
return result;
}
export function url_atob(str)
{
let result = new Uint8Array(Math.floor((str.length * 3) / 4));
let resultIndex = 0;
let paddingCount = 0;
for(let i = 0; i < str.length; i += 4)
{
const aIndex = alphabet.indexOf(str.charAt(i));
const bIndex = alphabet.indexOf(str.charAt(i + 1));
const cIndex = i + 2 < str.length ? alphabet.indexOf(str.charAt(i + 2)) : -1;
const dIndex = i + 3 < str.length ? alphabet.indexOf(str.charAt(i + 3)) : -1;
const a = (aIndex << 2) | (bIndex >> 4);
const b = ((bIndex & 15) << 4) | (cIndex >> 2);
const c = ((cIndex & 3) << 6) | dIndex;
result[resultIndex++] = a;
if(cIndex !== -1)
result[resultIndex++] = b;
if(dIndex !== -1)
result[resultIndex++] = c;
if(str.charAt(i + 2) === "=")
paddingCount++;
if(str.charAt(i + 3) === "=")
paddingCount++;
}
return result.slice(0, resultIndex - paddingCount);
}
const compressionMethod = "deflate";
export async function deflate(str)
{
const stream = new TextEncoderStream();
const writer = stream.writable.getWriter();
const writePromise = writer.write(str).then(_ => writer.close());
let res = [];
const reader = stream
.readable
.pipeThrough(new CompressionStream(compressionMethod))
.getReader();
const readPromise = new Promise(async resolve => {
while(true) {
const {done, value} = await reader.read();
if(done) break;
res.push(...value);
}
resolve();
});
await Promise.all([writePromise, readPromise]);
return res;
}
export async function inflate(bytes)
{
const stream = new DecompressionStream(compressionMethod);
const writer = stream.writable.getWriter();
const writePromise = writer.write(bytes).then(_ => writer.close());
let res = "";
const reader = stream
.readable
.pipeThrough(new TextDecoderStream())
.getReader();
const readPromise = new Promise(async resolve => {
while(true) {
const {done, value} = await reader.read();
if(done) break;
res += value;
}
resolve();
});
await Promise.all([writePromise, readPromise]);
return res;
}