@ungap/compression-stream
Advanced tools
+30
| import { Readable, Writable } from 'node:stream'; | ||
| import zlib from 'node:zlib'; | ||
| const compression = { | ||
| 'deflate': zlib.createDeflate, | ||
| 'deflate-raw': zlib.createDeflateRaw, | ||
| 'gzip': zlib.createGzip, | ||
| }; | ||
| export class CompressionStream { | ||
| constructor(format) { | ||
| const handle = compression[format](); | ||
| this.readable = Readable.toWeb(handle); | ||
| this.writable = Writable.toWeb(handle); | ||
| } | ||
| } | ||
| const decompression = { | ||
| 'deflate': zlib.createInflate, | ||
| 'deflate-raw': zlib.createInflateRaw, | ||
| 'gzip': zlib.createGunzip, | ||
| }; | ||
| export class DecompressionStream { | ||
| constructor(format) { | ||
| const handle = decompression[format](); | ||
| this.readable = Readable.toWeb(handle); | ||
| this.writable = Writable.toWeb(handle); | ||
| } | ||
| } |
| import { CompressionStream, DecompressionStream } from './facade.js'; | ||
| globalThis.CompressionStream ??= CompressionStream; | ||
| globalThis.DecompressionStream ??= DecompressionStream; |
+3
-54
@@ -1,56 +0,5 @@ | ||
| let { CompressionStream, DecompressionStream } = globalThis; | ||
| import * as localThis from './facade.js'; | ||
| if (!CompressionStream) { | ||
| // original idea: MIT License. Jimmy Wärting <https://jimmy.warting.se/opensource> | ||
| // @see https://github.com/oven-sh/bun/issues/1723#issuecomment-1774174194 | ||
| const { | ||
| createGzip, createGunzip, | ||
| createDeflate, createInflate, | ||
| createDeflateRaw, createInflateRaw, | ||
| } = await import('node:zlib'); | ||
| export const CompressionStream = globalThis.CompressionStream ?? localThis.CompressionStream; | ||
| class Stream { | ||
| constructor(compress, format) { | ||
| 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({ | ||
| type: 'bytes', | ||
| start: controller => { | ||
| handler.on('data', chunk => controller.enqueue(chunk)); | ||
| handler.once('end', () => controller.close()); | ||
| }, | ||
| }); | ||
| this.writable = new WritableStream({ | ||
| write: chunk => handler.write(chunk), | ||
| close: () => handler.end(), | ||
| }); | ||
| } | ||
| } | ||
| CompressionStream = class CompressionStream extends Stream { | ||
| constructor(format) { | ||
| super(true, format); | ||
| } | ||
| } | ||
| DecompressionStream = class DecompressionStream extends Stream { | ||
| constructor(format) { | ||
| super(false, format); | ||
| } | ||
| } | ||
| } | ||
| export { CompressionStream, DecompressionStream }; | ||
| export const DecompressionStream = globalThis.DecompressionStream ?? localThis.DecompressionStream; |
+7
-1
| { | ||
| "name": "@ungap/compression-stream", | ||
| "version": "0.1.0", | ||
| "version": "0.2.0", | ||
| "main": "index.js", | ||
@@ -15,4 +15,10 @@ "module": "index.js", | ||
| "description": "A De/CompressionStream for Bun", | ||
| "exports": { | ||
| ".": "./index.js", | ||
| "./poly": "./polyfill.js" | ||
| }, | ||
| "files": [ | ||
| "index.js", | ||
| "polyfill.js", | ||
| "facade.js", | ||
| "README.md" | ||
@@ -19,0 +25,0 @@ ], |
+7
-5
| # compression-stream | ||
| A De/CompressionStream *ponyfill* for Bun, fully based on [Jimmy Wärting](https://github.com/oven-sh/bun/issues/1723#issuecomment-1774174194)'s idea. | ||
| A De/CompressionStream *poly* / *ponyfill* for Bun, based on [this issue](https://github.com/oven-sh/bun/issues/1723)'s ideas. | ||
| This modules does not patch the *global* environment, it just provides native or polyfilled classes. | ||
| ```js | ||
| // polyfill (globally patched) | ||
| import '@ungap/compression-stream/poly'; | ||
| // CompressionStream & DecompressionStream now globally available | ||
| ```js | ||
| // ponyfill (no global patch) | ||
| import { CompressionStream, DecompressionStream } from '@ungap/compression-stream'; | ||
| // that's literally it 🥳 | ||
| // CompressionStream & DecompressionStream native if available | ||
| ``` |
5
66.67%14
16.67%2389
-11.02%32
-34.69%