-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgzip.mjs
34 lines (34 loc) · 1 KB
/
gzip.mjs
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
const url=new URL('./gzip.wasm',import.meta.url);
let wasm;
const wbg={
__wbindgen_init_externref_table: function(){
const table=wasm.__wbindgen_export_0;
const offset=table.grow(4);
table.set(0,undefined);
table.set(offset);
table.set(offset+1,null);
table.set(offset+2,true);
table.set(offset+3,false);
}
};
const {instance}=await WebAssembly.instantiateStreaming(await fetch(url,{cache: 'force-cache'}),{wbg});
wasm=instance.exports;
const malloc=wasm.__wbindgen_malloc;
const free=wasm.__wbindgen_free;
/**
* Compresses an array of bytes with Gzip (with a minimal header).
* @param {Uint8Array} bytes
* @param {0|1|2|3|4|5|6|7|8|9|10} [level=10]
* @return {Uint8Array}
*/
const gzip=(bytes,level=10)=>{
const n1=bytes.length;
const p1=malloc(n1,1);
new Uint8Array(wasm.memory.buffer).set(bytes,p1);
const [p2,n2]=wasm.gzip(p1,n1,level);
const res=new Uint8Array(wasm.memory.buffer).subarray(p2,p2+n2).slice();
free(p2,n2);
return res;
};
export {gzip};
export default gzip;