Repository navigation
Expand file tree
/
Copy pathshare.js
More file actions
53 lines (42 loc) · 1.57 KB
/
Copy pathshare.js
File metadata and controls
53 lines (42 loc) · 1.57 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
// Offline (file://) share-link encoding: gzip the board JSON and stuff it in the URL.
export async function encodeSharedPayload(payload) {
const json = JSON.stringify(payload);
const compressed = await gzipText(json);
return bytesToBase64Url(compressed);
}
export async function decodeSharedPayload(value) {
const bytes = base64UrlToBytes(value);
const json = await gunzipBytes(bytes);
return JSON.parse(json);
}
export async function gzipText(value) {
if (typeof CompressionStream === "undefined") {
return new TextEncoder().encode(value);
}
const stream = new Blob([value]).stream().pipeThrough(new CompressionStream("gzip"));
const buffer = await new Response(stream).arrayBuffer();
return new Uint8Array(buffer);
}
export async function gunzipBytes(bytes) {
if (typeof DecompressionStream === "undefined") {
return new TextDecoder().decode(bytes);
}
const stream = new Blob([bytes]).stream().pipeThrough(new DecompressionStream("gzip"));
return await new Response(stream).text();
}
export function bytesToBase64Url(bytes) {
let binary = "";
bytes.forEach((byte) => {
binary += String.fromCharCode(byte);
});
return btoa(binary).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/g, "");
}
export function base64UrlToBytes(value) {
const padded = value.replace(/-/g, "+").replace(/_/g, "/").padEnd(Math.ceil(value.length / 4) * 4, "=");
const binary = atob(padded);
const bytes = new Uint8Array(binary.length);
for (let index = 0; index < binary.length; index += 1) {
bytes[index] = binary.charCodeAt(index);
}
return bytes;
}