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

Ability to finalize a stream without an input chunk #21

Closed
BitLooter opened this issue Dec 30, 2020 · 4 comments
Closed

Ability to finalize a stream without an input chunk #21

BitLooter opened this issue Dec 30, 2020 · 4 comments
Assignees

Comments

@BitLooter
Copy link

What can't you do right now?
I'm writing a wrapper around Inflate to use it as a sink for a WritableStream. However, unlike fflate's streaming implementation, finalizing a WritableStream is done by calling close(), which takes no parameters (no final chunk), and there's no way for a sink to know when a stream is finished until close() is called. I tried calling Inflate.push([], true) to signal to fflate to flush the stream but it throws an error (from the slc function at line 145):

RangeError: Invalid typed array length: -32768

Example script: https://gist.github.com/BitLooter/d507cf7c5b320e806fec06b22d2ccaa4
Tested with fflate 0.4.6 on Node 15.5.0. Note that while this is an implementation of a UnderlyingSink for a WritableStream (which Node doesn't support) it still runs in Node as it's not actually creating any stream objects, it's just calling methods on an object.

An optimal solution
I need a way to finalize an fflate stream without having any input data, to call from an UnderlyingSink's close() method.

(How) is this done by other libraries?
I have written a sink wrapper for Pako and solved this problem by pushing an empty chunk with the Z_FINISH flag, similar to what I attempted with fflate. I don't know if this is the best solution but that or a new method to finalize the stream without data would work.

@101arrowz
Copy link
Owner

No, your solution should be correct, and there's a bug that is causing it to fail. Honestly, I'm surprised that there's an error with pushing a final empty chunk; I extensively tested the ability to do exactly that for exactly this reason. I'll push an update when the bug is fixed. Thanks for the report.

@101arrowz 101arrowz self-assigned this Dec 30, 2020
@101arrowz
Copy link
Owner

101arrowz commented Dec 30, 2020

@BitLooter Check v0.4.7 on NPM and let me know if it solves the issue. I'm pretty sure the problem's been resolved on the specific example you gave, but just to make sure, I'd suggest you test on your full code as well. Sorry for the cryptic error, and thanks for taking the time to make a reproducible example!

By the way, you may want to take a look at this file in the demo, which uses the Web Streams API to conform to the WHATWG streaming spec. It looks to be in line with what you're doing.

@101arrowz
Copy link
Owner

Did the update end up working for you?

@BitLooter
Copy link
Author

Just tested it, working perfectly now. Thanks for the quick fix!

I also took a look at stream-adapter. Looks useful if I need a TransformStream but what I'm doing is a little different - mine is just a WritableStream that collects the output chunks to a buffer. I'm using it to decode a stream from a fetch request directly into a Uint8Array without an intermediate buffer. I'll keep it in mind if I need an output stream though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants