Skip to content
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

Add CompressionStream #1723

Open
Symbitic opened this issue Jan 4, 2023 · 18 comments
Open

Add CompressionStream #1723

Symbitic opened this issue Jan 4, 2023 · 18 comments
Labels
enhancement New feature or request web-api Something that relates to a standard Web API wintercg Web-interoperable Runtimes Community Group compatiblity

Comments

@Symbitic
Copy link

Symbitic commented Jan 4, 2023

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.

@Symbitic Symbitic added the enhancement New feature or request label Jan 4, 2023
@Jarred-Sumner
Copy link
Collaborator

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.

@Electroid Electroid added the web-api Something that relates to a standard Web API label Jan 5, 2023
@Jarred-Sumner
Copy link
Collaborator

Prerequisite 3b25921

@vesamet
Copy link

vesamet commented Feb 26, 2023

Out of curiosity, what is the state of this issue?
(My app really depends on Hono's compression middleware 😅)

@cloudspeech
Copy link

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.

@marc-barry
Copy link

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?

@niklasgrewe
Copy link

any news about this issue? I also use Hono and want to use there compress middleware

@venkatd
Copy link

venkatd commented Aug 28, 2023

Our backend is unable to run without DecompressionStream, putting in my vote for this!

@tomek-f
Copy link

tomek-f commented Sep 10, 2023

Any news about this issue?

@ewrogers
Copy link

Out of curiosity, what is the state of this issue? (My app really depends on Hono's compression middleware 😅)

+1 for the same reason, though I can disable it for now it would be great to have this soon.

@cloudspeech
Copy link

One could try https://github.com/101arrowz/compression-streams-polyfill in the meantime.

@jimmywarting
Copy link

jimmywarting commented Oct 22, 2023

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... )

@javalsai
Copy link

javalsai commented Jan 4, 2024

I think this might be relevant in here, I was doing stuff with gzip streams in bun and I found a weird bug with following code:
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 echo <some data> | bun/node <path> > /dev/null and they should behave the same way, but in my case I was doing random data and big streams and found out from dd with bs=1K and count=16 up, the program hangs forever, so:

  • dd if=/dev/urandom bs=1K count=15 | bun std-gzip-pipe.js > /dev/null: works in under 0.002 secs on my machine
  • dd if=/dev/urandom bs=1K count=16 | bun std-gzip-pipe.js > /dev/null: hangs forever on bun, node works however

It turns out to be a stdio issue

@sbenzemame
Copy link

Any news about this issue?

@danthegoodman1
Copy link

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.

@sunneydev
Copy link

sunneydev commented Feb 3, 2024

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.

@codedread
Copy link

Does this bug also cover the DecompressionStream implementation? If so, can someone update the bug title?

@Mehuge
Copy link

Mehuge commented Mar 8, 2024

My deduplication backup [ddb] software uses streams with compression, specifically it .pipe()s to and from a compressor (gzip/gunzip). It operates over http as well as over the file system. Loading an entire file into memory is ofc not an option, so has to use streams.

@danielniccoli
Copy link

It's on the roadmap #159

I, too, hope for a soon implementation. 🙏☺️

@nektro nektro added the wintercg Web-interoperable Runtimes Community Group compatiblity label Jun 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request web-api Something that relates to a standard Web API wintercg Web-interoperable Runtimes Community Group compatiblity
Projects
None yet
Development

No branches or pull requests