-
Notifications
You must be signed in to change notification settings - Fork 28.2k
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 options to stream consumers #40111
Comments
it would also be useful if ...don't have any other suggestions for json or text |
I think these are thought to mirror the body mixin from the fetch standard. |
Yea, but what if you want to turn any iterable into a blob then you have no other way to set the correct type other than creating 2 blobs. chunk = new Blob([await blob(iterable)], {type: 'text/html'}) i where looking into how we could use this user-land version of stream-consumers in node-fetch This these 2 missing optimisation makes it a tiny bit unappealing for us to use stream-consumer at all. I also wished that i mean |
I'm rewriting my framework to use /** @type {import('stream/consumers')} */
let streamConsumers;
try {
streamConsumers = await import(new URL('node:stream/consumers').toString());
} catch {}
let BlobClass = (typeof Blob === 'undefined' ? undefined : Blob);
export default class HttpRequest {
/** snip **/
async blob() {
if (streamConsumers) {
const contentType = this.headers['content-type'];
// SEE HERE: Adding the option in case it's added in the future.
const result = await streamConsumers.blob(this.stream, {
type: contentType,
});
this.#bodyUsed = true;
if (!contentType || result.type === contentType) {
return result;
}
// SEE HERE: Proxy needed to set type.
return new Proxy(result, {
get: (target, p, receiver) => {
if (p === 'type') return this.headers['content-type'];
return Reflect.get(target, p, receiver);
},
});
}
if (BlobClass === undefined) {
try {
const module = await import('node:buffer');
if ('Blob' in module === false) throw new Error('NOT_SUPPORTED');
BlobClass = module.Blob;
} catch {
BlobClass = null;
}
}
if (BlobClass === null) {
throw new Error('NOT_SUPPORTED');
}
try {
const chunks = [];
for await (const chunk of this.stream) { chunks.push(chunk); }
return new BlobClass(chunks, {
type: this.headers['content-type'],
});
} finally {
this.#bodyUsed = true;
}
} I'm not entirely clear on spec, but I think if somebody calls one function, like |
There has been no activity on this feature request for 5 months and it is unlikely to be implemented. It will be closed 6 months after the last non-automated comment. For more information on how the project manages feature requests, please consult the feature request management document. |
There has been no activity on this feature request and it is being closed. If you feel closing this issue is not the right thing to do, please leave a comment. For more information on how the project manages feature requests, please consult the feature request management document. |
@jimmywarting:
Yeah, let's take that to a separate issue. I think adding an
options
argument to each of the convenience consumers makes sense.Originally posted by @jasnell in #40089 (comment)
The text was updated successfully, but these errors were encountered: