-
Notifications
You must be signed in to change notification settings - Fork 0
/
compression.ts
76 lines (72 loc) · 2.38 KB
/
compression.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
import {Model, ModelDeclaration, newModel} from "./model";
import {compress, decompress} from "brotli-wasm";
/*
*/
/**
* Compress a string using brotli
*
* NOTE: does not work in the browser use brotliPromise instead
* ```
* import brotliPromise from 'brotli-wasm';
*
* async function compressBrotliEncode(data: string): Promise<string> {
* const brotli = await brotliPromise;
* const encoder = new TextEncoder();
* const encodedData = encoder.encode(data);
* const compressedData = brotli.compress(encodedData);
* const byteNumbers = new Array(compressedData.length);
* for (let i = 0; i < compressedData.length; i++) {
* byteNumbers[i] = compressedData[i];
* }
* const byteArray = new Uint8Array(byteNumbers);
* // @ts-ignore
* const byteCharacters = String.fromCharCode.apply(null, byteArray);
* return btoa(byteCharacters);
* }
* ```
*/
export function compressBrotliEncode(data: string): string {
return Buffer.from(
compress(Buffer.from(data))
).toString("base64");
}
/**
* Decompress a string using brotli
*
* NOTE: does not work in the browser use brotliPromise instead
* ```
* import brotliPromise from 'brotli-wasm';
*
* async function decompressBrotliDecode(data: string): Promise<string> {
* const brotli = await brotliPromise;
* const byteCharacters = atob(data);
* const byteNumbers = new Array(byteCharacters.length);
* for (let i = 0; i < byteCharacters.length; i++) {
* byteNumbers[i] = byteCharacters.charCodeAt(i);
* }
* const byteArray = new Uint8Array(byteNumbers);
* const decompressedData = brotli.decompress(byteArray);
* const textDecoder = new TextDecoder();
* return textDecoder.decode(decompressedData);
* }
* ```
*
* @param data
*/
export async function decompressBrotliDecode(data: string): Promise<string> {
return Buffer.from(
decompress(Buffer.from(data, "base64"))
).toString();
}
export async function decompressModel(data: string, version: string = "v0"): Promise<Model> {
return decompressBrotliDecode(data).then((json) => {
const m = JSON.parse(json) as ModelDeclaration;
if (m.version !== version) {
throw new Error(`Invalid model version: ${m.version} expected: ${version}`);
}
return newModel({
declaration: m,
type: m.modelType,
});
});
}