Created
April 10, 2022 01:36
-
-
Save yskszk63/460760bbefa4ef24c9589dcf132a8232 to your computer and use it in GitHub Desktop.
Close at pull - Web Stream
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* ``` | |
* $ 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