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
Allow ReadableStream passthrough #1096
Comments
We don't just have to let that slip, we also need to support it so that we can read it. the easiest solution would be to simply convert it to a node stream if it's a whatwg stream using Both node streams and whatwg streams are async iterable, but the browsers haven't implemented it yet As much as we would like to work with whatwg streams, I guess that in the moment it's internally best if we have a node stream as it works best with piping & passing the data to node's http request Many node developers depends on response body being a node streams as that is also the way it write stuff back to the fs So it would be hard to change the response body to something else at this point. It's much easier to accept both types of stream then it is to return the "correct" things in the response/request the whatwg streams have a spec proposal to also adding Another thought i had right now is if we could just embraced iterators and looped over them instead Something potentially better could be to replace Lines 361 to 383 in 6ee9d31
with something like: if (isBlob(body)) {
// Body is Blob
body = body.stream();
}
if (body === null) {
// Body is null
dest.end();
} else if (Buffer.isBuffer(body)) {
// Body is buffer
dest.write(body);
dest.end();
} else {
// Body is either a async iterator that yields uint8array, whatwg stream or node stream
for await (let chunk of body ) {
if (!dest.write(chunk)) {
await new Promise(rs => dest.once('drain', rs));
}
}
dest.end()
} I like this more then what i just did with #1092, but we would have to change some more code throughout the rest of the pipeline |
I'm trying to use this library together with "fetch-mock", for testing some Cloudflare workers. In the workers, I'm using
TransformStream
andresponse.body.pipeThrough
which work like they would in a Web Worker. (Spec: https://streams.spec.whatwg.org). I'm using https://github.com/MattiasBuelens/web-streams-polyfill to create aReadableStream
which I then pass as the body. However, it's not possible to return aReadableStream
innode-fetch
, it always gets transformed to aBuffer
. That's becausebody instanceof Stream
returns false here:node-fetch/src/body.js
Line 51 in 6ee9d31
This is because this polyfill doesn't use the existing Node streams.
Is it possible to test for streams using
body instanceof Stream || typeof body.pipeThrough === 'function'
? If necessary I'll create a PR.Another possibility is to use https://github.com/MattiasBuelens/web-streams-polyfill in order to create
response.body
on every response in the next version ofnode-fetch
, or to add an additional option to select the stream implementation (web-streams-polyfill or Node streams).Another option is to add an option
sendAsRaw
to allow passthrough of any body.The text was updated successfully, but these errors were encountered: