-
Notifications
You must be signed in to change notification settings - Fork 470
Expand file tree
/
Copy pathgz.worker.js
More file actions
38 lines (32 loc) · 1.35 KB
/
gz.worker.js
File metadata and controls
38 lines (32 loc) · 1.35 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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
async function readableStreamToBuffer(stream) {
return new Uint8Array(await new Response(stream).arrayBuffer());
}
onmessage = async (e) => {
const data = e.data;
if (data.kind === 'compress') {
// Create a gzip compression stream
const compressionStream = new CompressionStream('gzip');
// Write the data to the compression stream
const writer = compressionStream.writable.getWriter();
writer.write(data.arrayData);
writer.close();
// Read the compressed data back into a buffer
const result = await readableStreamToBuffer(compressionStream.readable);
postMessage(result, [result.buffer]);
} else if (data.kind === 'decompress') {
// Create a gzip compression stream
const decompressionStream = new DecompressionStream('gzip');
// Write the data to the compression stream
const writer = decompressionStream.writable.getWriter();
writer.write(data.arrayData);
writer.close();
// Read the compressed data back into a buffer
const result = await readableStreamToBuffer(decompressionStream.readable);
postMessage(result, [result.buffer]);
} else {
throw new Error('unknown message');
}
};