-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompressionstream_patch.ts
More file actions
63 lines (55 loc) · 1.81 KB
/
Copy pathcompressionstream_patch.ts
File metadata and controls
63 lines (55 loc) · 1.81 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
54
55
56
57
58
59
60
61
62
63
export default `import {
createDeflate,
createDeflateRaw,
createGunzip,
createGzip,
createInflate,
createInflateRaw,
} from "node:zlib";
// From https://github.com/ungap/compression-stream/blob/main/index.js with slight modifications.
if (!("CompressionStream" in globalThis)) {
// original idea: MIT License. Jimmy Wärting <https://jimmy.warting.se/opensource>
// @see https://github.com/oven-sh/bun/issues/1723#issuecomment-1774174194
class Stream {
readable: ReadableStream;
writable: WritableStream;
constructor(compress: boolean, format: string) {
let handler;
if (format === "gzip") {
handler = compress ? createGzip() : createGunzip();
} else if (format === "deflate") {
handler = compress ? createDeflate() : createInflate();
} else if (format === "deflate-raw") {
handler = compress ? createDeflateRaw() : createInflateRaw();
} else {
throw new TypeError([
\`Failed to construct '\${this.constructor.name}'\`,
\`Unsupported compression format: '\${format}'\`,
].join(": "));
}
this.readable = new ReadableStream({
// @ts-ignore: why?
type: "bytes",
start: (controller) => {
handler.on("data", (chunk) => controller.enqueue(chunk));
handler.once("end", () => controller.close());
},
});
this.writable = new WritableStream({
write: (chunk) => void handler.write(chunk),
close: () => void handler.end(),
});
}
}
globalThis.CompressionStream = class CompressionStream extends Stream {
constructor(format: string) {
super(true, format);
}
};
globalThis.DecompressionStream = class DecompressionStream extends Stream {
constructor(format: string) {
super(false, format);
}
};
}
`