-
Notifications
You must be signed in to change notification settings - Fork 29
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
Please consider web compatible stream()
method.
#51
Comments
I would love to converge https://www.npmjs.com/package/web-blob with fetch-blob, however incompatibility of stream method prevents me from doing it. If I were to do the work to replace current node |
As long as the stream is async iterable |
Made a decision to degrade the Guess It can stay like that until node implements whatwg stream ...And for those who do not care about either node or whatwg stream can use for await (const chunk of stream) { ... } All doe async iterable ReadableStream is not implemented anywhere in any browser AFAIK |
In practice it is not the case:
I think it would be a lot better to just not have |
how would you go about subclassing this if it can't provide a stream method? slicing the blob, read each chunk as arraybuffer or something like that? similar to this pr/file? both fyi.
I can see some few solution to this...
if (globalThis.ReadableStream) {
const orig = Blob.prototype.stream
Blob.prototype.stream = function() {
const iterator = orig.call(this)
return new ReadableStream({
pull: ctrl => iterator.next().then(({value, done}) done ? ctrl.close() : ctrl.enqueue(value))
})
}
} This could be subclassed in user-land also. it is easy to check if stream is not spec compatible and see if it's a generator function or regular function with this detection script if (Blob.prototype.stream.constructor.name === 'AsyncGeneratorFunction') {
// not spec compatible, subclass it...
} another solution could be to polyfill // Here is a very basic one
if (!ReadableStream.prototype[Symbol.asyncIterator]) {
ReadableStream.prototype[Symbol.asyncIterator] = async function* () {
const reader = this.getReader()
while (1) {
const chunk = await reader.read()
if (chunk.done) return chunk.value
yield chunk.value
}
}
} The benefit of this is that your There are few requirements on this blob implementation.
this was built for nodejs after all so the polyfilled version of whatwg stream and node stream are async iterator so i think it's best to use for-await-of in most cases to avoid any environmental differences other than those two/three requirements i'm open to suggestions |
First of all thanks you for willingness to carry on this conversation, I really appreciate it. I'll try to respond inline below
class Blob extends FetchBlob {
stream() {
return asyncIterableToReadableStream(FetchBlob.asyncIterate(this))
}
} Where I assume static
I'm not opposed to async iteration API in blob, on the contrary (In fact I wish
I think there is a bit of misunderstanding here. I do not have a case where fetch-blob would be preferred over
More recently I've also run into the same incompatibily problem with a code which targets both node and cloudfare workers (that has native fetch and blob impls).
This is not a great option because while global polyfills seem fine when building an app, they don't seem ok in libraries. Having to tell library users to also bring global polyfill does not seem like a great option.
You are right that one could subclass fetch-blob today like this: class MyBlob extends FetchBlob {
stream() {
return asyncIterableToReadableStream(super.stream())
}
} And maybe it would not break any assumptions anywhere and will work.
You are right about benefits but:
It is true but again node-fetch could just as well depend on any other method. In fact I have filled the issue to that end there as well node-fetch/node-fetch#1120 and wrote POC https://github.com/web-std/node-fetch/pull/2/files
That is very reasonable position. But again you could choose to expose same functionality with a different name. It would work in node just the same and it would make it straight forward for someone to just combine two libs to get a whatwg compliant version. P.S.: I do not think this alone provides much without node-fetch |
For bit more context, I found incompatibilities so problematic that I end up:
But unfortunately it is nearly impossible to backport fixes and I would much rather contribute to the community instead of forking it. |
ping @bitinn, @Richienb, @tinovyatkin |
Good news! WHATWG streams have landed in NodeJS (as experimental) Think we should start using it whenever possible, might be worth looking into adding whatwg stream polyfill now after all! |
Currently implementation deliberately diverges from the Blob spec by using node Readable stream instead web ReadableStream. This is problematic because code that runs both in browser and node can not use
blob.stream()
without having to check what the result is.How about either implementing web compatible stream method, or not implementing it at all.
The text was updated successfully, but these errors were encountered: