-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathstream.ts
32 lines (28 loc) · 891 Bytes
/
stream.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
import zlib from 'node:zlib'
/** NOTE: can be removed once Bun has implemented this class
*
* https://github.com/oven-sh/bun/issues/1723
* https://github.com/oven-sh/bun/issues/1723#issuecomment-1774174194
* */
export class CompressionStream {
readable: ReadableStream
writable: WritableStream
constructor(format: 'gzip' | 'deflate') {
const handle =
format === 'deflate'
? zlib.createDeflate()
: format === 'gzip'
? zlib.createGzip()
: zlib.createDeflateRaw()
this.readable = new ReadableStream({
start(controller) {
handle.on('data', (chunk: Uint8Array) => controller.enqueue(chunk))
handle.once('end', () => controller.close())
},
})
this.writable = new WritableStream({
write: (chunk: Uint8Array) => handle.write(chunk) as any,
close: () => handle.end() as any,
})
}
}