Skip to content

Instantly share code, notes, and snippets.

@yskszk63
Created April 10, 2022 01:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yskszk63/460760bbefa4ef24c9589dcf132a8232 to your computer and use it in GitHub Desktop.
Save yskszk63/460760bbefa4ef24c9589dcf132a8232 to your computer and use it in GitHub Desktop.
Close at pull - Web Stream
/*
* ```
* $ node ./run.mjs && deno run ./run.mjs && echo ok
* ok
* ```
*/
if (typeof process !== "undefined") {
const { ReadableStream, WritableStream } = await import("stream/web");
Object.assign(globalThis, {
ReadableStream,
WritableStream,
});
}
const stream = new ReadableStream({
pull(controller) {
const request = controller.byobRequest;
if (!request) {
throw new Error();
}
controller.close();
request.respond(0);
},
type: "bytes",
});
const reader = stream.getReader({ mode: "byob" });
const buf = new Uint8Array(16);
const { done, value } = await reader.read(buf);
if (!done || value.byteLength !== 0) {
throw new Error();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment