|
2 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
3 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
4 | 4 |
|
5 | | -import zeeWorkerPath from 'firefox-profiler-res/zee-worker.js'; |
| 5 | +async function readableStreamToBuffer( |
| 6 | + stream: ReadableStream<Uint8Array<ArrayBuffer>> |
| 7 | +): Promise<Uint8Array<ArrayBuffer>> { |
| 8 | + const reader = stream.getReader(); |
| 9 | + const chunks: Uint8Array[] = []; |
6 | 10 |
|
7 | | -const zeeCallbacks: Array<{ |
8 | | - success: (data: any) => void; |
9 | | - error: (error: any) => void; |
10 | | -} | null> = []; |
| 11 | + try { |
| 12 | + while (true) { |
| 13 | + const { done, value } = await reader.read(); |
| 14 | + if (done) break; |
| 15 | + if (value) { |
| 16 | + chunks.push(value); |
| 17 | + } |
| 18 | + } |
| 19 | + } finally { |
| 20 | + reader.releaseLock(); |
| 21 | + } |
11 | 22 |
|
12 | | -type ZeeWorkerData = { |
13 | | - callbackID: number; |
14 | | - type: 'success' | 'error'; |
15 | | - data: any; |
16 | | -}; |
| 23 | + // Calculate total length and combine chunks |
| 24 | + const totalLength = chunks.reduce((sum, chunk) => sum + chunk.length, 0); |
| 25 | + const result = new Uint8Array(totalLength); |
| 26 | + let offset = 0; |
| 27 | + for (const chunk of chunks) { |
| 28 | + result.set(chunk, offset); |
| 29 | + offset += chunk.length; |
| 30 | + } |
17 | 31 |
|
18 | | -function workerOnMessage(zeeWorker: Worker) { |
19 | | - zeeWorker.onmessage = function (msg: MessageEvent) { |
20 | | - const data = msg.data as ZeeWorkerData; |
21 | | - const callbacks = zeeCallbacks[data.callbackID]; |
22 | | - if (callbacks) { |
23 | | - callbacks[data.type](data.data); |
24 | | - zeeCallbacks[data.callbackID] = null; |
25 | | - } |
26 | | - }; |
| 32 | + return result; |
27 | 33 | } |
28 | 34 |
|
29 | | -// Neuters data's buffer, if data is a typed array. |
30 | | -export async function compress( |
31 | | - data: string | Uint8Array, |
32 | | - compressionLevel?: number |
33 | | -): Promise<Uint8Array<ArrayBuffer>> { |
34 | | - if (!(typeof window === 'object' && 'Worker' in window)) { |
35 | | - // Try to fall back to Node's zlib library. |
36 | | - const zlib = await import('zlib'); |
37 | | - return new Promise((resolve, reject) => { |
38 | | - zlib.gzip(data, (errorOrNull, result) => { |
39 | | - if (errorOrNull) { |
40 | | - reject(errorOrNull); |
41 | | - } else { |
42 | | - resolve(new Uint8Array(result.buffer as ArrayBuffer)); |
43 | | - } |
44 | | - }); |
45 | | - }); |
| 35 | +// The Streams API doesn't support SAB, and so we need to copy from these to |
| 36 | +// use these API's. |
| 37 | +function copyBufferIfShared(buffer: Uint8Array): Uint8Array<ArrayBuffer> { |
| 38 | + // Check if the buffer is a view into a larger ArrayBuffer (shared) |
| 39 | + if (buffer.buffer instanceof SharedArrayBuffer) { |
| 40 | + // Create a new buffer with its own ArrayBuffer |
| 41 | + return new Uint8Array(buffer); |
46 | 42 | } |
| 43 | + // Return the original buffer if it's not shared |
| 44 | + return buffer as Uint8Array<ArrayBuffer>; |
| 45 | +} |
47 | 46 |
|
48 | | - const zeeWorker = new Worker(zeeWorkerPath); |
49 | | - workerOnMessage(zeeWorker); |
50 | | - |
| 47 | +export async function compress( |
| 48 | + data: string | Uint8Array |
| 49 | +): Promise<Uint8Array<ArrayBuffer>> { |
| 50 | + // Encode the data if it's a string |
51 | 51 | const arrayData = |
52 | 52 | typeof data === 'string' ? new TextEncoder().encode(data) : data; |
53 | | - return new Promise(function (resolve, reject) { |
54 | | - zeeWorker.postMessage( |
55 | | - { |
56 | | - request: 'compress', |
57 | | - data: arrayData, |
58 | | - compressionLevel: compressionLevel, |
59 | | - callbackID: zeeCallbacks.length, |
60 | | - }, |
61 | | - [arrayData.buffer] |
62 | | - ); |
63 | | - zeeCallbacks.push({ |
64 | | - success: resolve, |
65 | | - error: reject, |
66 | | - }); |
67 | | - }); |
| 53 | + |
| 54 | + // Create a gzip compression stream |
| 55 | + const compressionStream = new CompressionStream('gzip'); |
| 56 | + |
| 57 | + // Write the data to the compression stream |
| 58 | + const writer = compressionStream.writable.getWriter(); |
| 59 | + writer.write(copyBufferIfShared(arrayData)); |
| 60 | + writer.close(); |
| 61 | + |
| 62 | + // Read the compressed data back into a buffer |
| 63 | + return readableStreamToBuffer(compressionStream.readable); |
68 | 64 | } |
69 | 65 |
|
70 | | -// Neuters data's buffer, if data is a typed array. |
71 | | -export async function decompress(data: Uint8Array): Promise<Uint8Array> { |
72 | | - if (!(typeof window === 'object' && 'Worker' in window)) { |
73 | | - // Handle the case where we're not running in the browser, e.g. when |
74 | | - // this code is used as part of a library in a Node project. |
75 | | - // We don't get here when running Firefox profiler tests, because our |
76 | | - // tests create a mock window with a mock Worker class. |
77 | | - // Try to fall back to Node's zlib library. |
78 | | - const zlib = await import('zlib'); |
79 | | - return new Promise((resolve, reject) => { |
80 | | - zlib.gunzip(data, (errorOrNull, result) => { |
81 | | - if (errorOrNull) { |
82 | | - reject(errorOrNull); |
83 | | - } else { |
84 | | - resolve(new Uint8Array(result.buffer as ArrayBuffer)); |
85 | | - } |
86 | | - }); |
87 | | - }); |
88 | | - } |
| 66 | +export async function decompress( |
| 67 | + data: Uint8Array |
| 68 | +): Promise<Uint8Array<ArrayBuffer>> { |
| 69 | + // Create a gzip compression stream |
| 70 | + const decompressionStream = new DecompressionStream('gzip'); |
| 71 | + |
| 72 | + // Write the data to the compression stream |
| 73 | + const writer = decompressionStream.writable.getWriter(); |
| 74 | + writer.write(copyBufferIfShared(data)); |
| 75 | + writer.close(); |
89 | 76 |
|
90 | | - const zeeWorker = new Worker(zeeWorkerPath); |
91 | | - return new Promise(function (resolve, reject) { |
92 | | - workerOnMessage(zeeWorker); |
93 | | - zeeWorker.postMessage( |
94 | | - { |
95 | | - request: 'decompress', |
96 | | - data: data, |
97 | | - callbackID: zeeCallbacks.length, |
98 | | - }, |
99 | | - [data.buffer] |
100 | | - ); |
101 | | - zeeCallbacks.push({ |
102 | | - success: resolve, |
103 | | - error: reject, |
104 | | - }); |
105 | | - }); |
| 77 | + // Read the compressed data back into a buffer |
| 78 | + return readableStreamToBuffer(decompressionStream.readable); |
106 | 79 | } |
107 | 80 |
|
108 | 81 | export function isGzip(data: Uint8Array): boolean { |
|
0 commit comments