-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Add CompressionStream #1723
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Yeah we need to do this WebKit has a CompressionStream/DecompressionStream we could use and uWebSockets (our HTTP server lib) also has builtin support for compression. Neither are wired up yet. |
Prerequisite 3b25921 |
Out of curiosity, what is the state of this issue? |
My node app uses DecompressionStream to unpack npm packages 'manually', i.e. without official npm tooling - I would very much like to switch to bun, but this is blocking me ATM. |
We just tried to port our proxy from Deno to Bun as sadly Bun doesn't support compression on its own. So we have to use https://hono.dev/middleware/builtin/compress with Bun. Is there another way to handle HTTP compression in Bun? |
any news about this issue? I also use Hono and want to use there compress middleware |
Our backend is unable to run without |
Any news about this issue? |
+1 for the same reason, though I can disable it for now it would be great to have this soon. |
One could try https://github.com/101arrowz/compression-streams-polyfill in the meantime. |
I wired up my own compression stream polyfill that is based upon zlip instead of using fflate // @bun
/*! MIT License. Jimmy Wärting <https://jimmy.warting.se/opensource> */
import zlib from 'node:zlib'
// fyi, Byte streams aren't really implemented anywhere yet
// It only exist as a issue: https://github.com/WICG/compression/issues/31
const make = (ctx, handle) => Object.assign(ctx, {
writable: new WritableStream({
write: chunk => handle.write(chunk),
close: () => handle.end()
}),
readable: new ReadableStream({
type: 'bytes',
start (ctrl) {
handle.on('data', chunk => ctrl.enqueue(chunk))
handle.once('end', () => ctrl.close())
}
})
})
globalThis.CompressionStream ??= class CompressionStream {
constructor(format) {
make(this, format === 'deflate' ? zlib.createDeflate() :
format === 'gzip' ? zlib.createGzip() : zlib.createDeflateRaw())
}
}
globalThis.DecompressionStream ??= class DecompressionStream {
constructor(format) {
make(this, format === 'deflate' ? zlib.createInflate() :
format === 'gzip' ? zlib.createGunzip() :
zlib.createInflateRaw())
}
} ( this is only needed in Bun - all other env have compression stream right now... ) |
import { createGzip } from 'node:zlib'
import { Readable, pipeline } from 'node:stream'
const gzip = createGzip()
pipeline(
Readable.toWeb(process.stdin),
gzip,
process.stdout,
() => {},
) Now, you should be able to run the program with
It turns out to be a stdio issue |
Any news about this issue? |
This is a bit of a blocker for a use case I have for bun. In the meantime I can use third party packages but I hope this is supported soon. |
Before this issue is resolved you can use https://www.npmjs.com/package/bun-compression. I just ported the compression middleware that Elysia was using for hono. |
Does this bug also cover the DecompressionStream implementation? If so, can someone update the bug title? |
My deduplication backup [ddb] software uses streams with compression, specifically it |
It's on the roadmap #159 I, too, hope for a soon implementation. 🙏 |
it looks like Safari has it too https://developer.mozilla.org/en-US/docs/Web/API/CompressionStream/CompressionStream#browser_compatibility ... and so does WebKit https://github.com/WebKit/WebKit/blob/f40dca81c492428dec3f1c977b27ed2e906bbf30/Source/WebCore/Modules/compression/CompressionStream.idl#L32 ... what is stopping this feature to land? 🤔 |
This comment has been minimized.
This comment has been minimized.
I think @tiagorangel1 meant to say bump to which I agree ... is there at least any ETA to share about this, or what are the main blockers? 🤔 |
To whom it might concern, I've published a ponyfill that bsically patches only Bun https://github.com/ungap/compression-stream. It's mostly based on this code which works indeed pretty well (at least for my use-case) so thank you @jimmywarting and happy to improve any credits you like in there too, if needed. I hope this won't be needed in the near future, it just exports back native classes if found in the global context. |
maybe unnecessary but (imho) worth asking to @Jarred-Sumner: if a polyfill is "that trivial", wouldn't it make sense for Bun to just ship a non-optimized, JS based version, of such polyfill to enable all people dependent on such feature and then, eventually, put in the pipe possible native optimizations to be released? Me, among others, really need this feature which also feels super natural to use in an engine that promotes |
If anyone needs zstd/brotli/gzip compression in hono I made a plugin that also work with bun: https://github.com/vuolter/hono-compress |
Any news on this? I don't see it on the roadmap #159 anymore. |
This comment has been minimized.
This comment has been minimized.
it looks like both CompressionStream and DecompressionStream are now part of the TC55 (recently landed) standard API ... https://min-common-api.proposal.wintercg.org/#api-index Is there any reason this has not already landed in bun in a way or another? |
it's awkward nobody is answering around this issue ... and there's no progress whatsoever ... can anyone please provide an ETA, a statement, or something around this issue? Thank you 🙏 |
We will implement CompressionStream support. Probably not this month.
Potentially next month.
…On Sun, Mar 16, 2025 at 10:36 AM Andrea Giammarchi ***@***.***> wrote:
it's awkward nobody is answering around this issue ... and there's no
progress whatsoever ... can anyone please provide an ETA, a statement, or
something around this issue? Thank you 🙏
—
Reply to this email directly, view it on GitHub
<#1723 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAFNGS4WIBXV4NR2UKRDTQT2UWZADAVCNFSM6AAAAAATRK56HSVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDOMRXGU2TMOJTGA>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
[image: WebReflection]*WebReflection* left a comment (oven-sh/bun#1723)
<#1723 (comment)>
it's awkward nobody is answering around this issue ... and there's no
progress whatsoever ... can anyone please provide an ETA, a statement, or
something around this issue? Thank you 🙏
—
Reply to this email directly, view it on GitHub
<#1723 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAFNGS4WIBXV4NR2UKRDTQT2UWZADAVCNFSM6AAAAAATRK56HSVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDOMRXGU2TMOJTGA>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
For now, I'm using this polyfill // compression-polyfill.ts
/**
* This is a polyfill for the CompressionStream and DecompressionStream.
* This solves the issue where Drizzle Studio Can't find variable: CompressionStream
*
* ![BUG]: ReferenceError: Can't find variable: CompressionStream
* @see https://github.com/drizzle-team/drizzle-orm/issues/3880
*
* Bun doesn't have CompressionStream and DecompressionStream yet
* @see https://github.com/oven-sh/bun/issues/1723
*/
import zlib from "node:zlib";
class CompressionStream {
readable;
writable;
constructor(format: "deflate" | "deflate-raw" | "gzip") {
const handle =
format === "deflate"
? zlib.createDeflate()
: format === "gzip"
? zlib.createGzip()
: zlib.createDeflateRaw();
this.readable = new ReadableStream({
start(controller) {
handle.on("data", (chunk) => controller.enqueue(chunk));
handle.once("end", () => controller.close());
},
});
this.writable = new WritableStream({
write: (chunk) => {
handle.write(chunk);
},
close: () => {
handle.end();
},
});
}
}
globalThis.CompressionStream ??= CompressionStream;
globalThis.DecompressionStream ??= CompressionStream; import "./compression-polyfill"; |
Why not implement slow polyfill and then fix it? For now it's hard to inject it for cli's like drizzle studio |
@SohelIslamImran I wrote and published one already #1723 (comment) edit and your decompression feels like it won't work at all ... and ... @kravetsone I've proposed that too but no answer #1723 (comment) ... still ... @Jarred-Sumner thanks for that ETA, looking forward to make my poly/ponyfill deprecated ASAP 🙏 |
👀 |
@SohelIslamImran #1723 (comment) globalThis.DecompressionStream ??= CompressionStream; |
But working! 👀.. I'm just using this hack for Drizale Studio for just development, not as a production solution. @jimmywarting |
maybe a better version of my previous code: // @bun
/*! MIT License. Jimmy Wärting <https://jimmy.warting.se/opensource> */
import zlib from 'node:zlib'
import stream from 'node:stream'
// fyi, Byte streams aren't really implemented anywhere yet
// It only exist as a issue: https://github.com/WICG/compression/issues/31
const make = (ctx, handle) => Object.assign(ctx, {
readable: new stream.Readable.toWeb(handle),
writable: new stream.Writable.toWeb(handle),
})
globalThis.CompressionStream ??= class CompressionStream {
constructor(format) {
make(this, format === 'deflate' ? zlib.createDeflate() :
format === 'gzip' ? zlib.createGzip() : zlib.createDeflateRaw())
}
}
globalThis.DecompressionStream ??= class DecompressionStream {
constructor(format) {
make(this, format === 'deflate' ? zlib.createInflate() :
format === 'gzip' ? zlib.createGunzip() :
zlib.createInflateRaw())
}
} |
++ the long awaited |
What is the problem this feature would solve?
The compress middleware in Hono depends on CompressStream, which isn't implemented by WebKit. By adding it, it would increase the performance of transferring static assets such as CSS and SVG files.
What is the feature you are proposing to solve the problem?
Implement CompressionStream in Bun. That would automatically allow the compress middleware in Hono to work, and it would enable more Bun-optimized frameworks like Elysia to design their own middleware.
What alternatives have you considered?
It might be possible to implement CompressStream in WebKit and use that.
The text was updated successfully, but these errors were encountered: