whatwg-streams-impl
v1.0.0
Published
WHATWG Streams Implementation for Node.JS
Downloads
2
Maintainers
Readme
WHATWG Streams Implementation
This repository provides a stand-alone implementation in Node.js of the WHATWG Streams Living Standard Specification
Current Status
Current status is WIP (Work In Progress) and implements the Standard as follows:
- Readable streams - partial (missing support for byte streams)
- ReadableStream - partial
- ReadableStreamDefaultReader - complete
- ReadableStreamBYOBReader - missing (WIP)
- ReadableStreamDefaultController - complete
- ReadableByteStreamController - missing (WIP)
- ReadableStreamBYOBRequest - missing
- Writable streams - missing
- WritableStream - missing
- WritableStreamDefaultWriter - missing
- WritableStreamDefaultController - missing
- Transform streams - missing
- TransformStream - missing
- TransformStreamDefaultController - missing
- Other API and operations - partial
- Queuing strategies - missing
- ByteLengthQueuingStrategy - missing
- CountQueuingStrategy - missing
- Queue-with-sizes - complete
- Miscellaneous operations - almost complete
- TransferArrayBuffer(O) - missing
- Queuing strategies - missing
Usage example (ReadableStream)
const nodeReadableStream = getReadableStream(); // Get a Node.js Readable stream somehow
// Create a ReadableStream instance, based on the nodeReadableStream underlying source.
const stream = new ReadableStream({
start(controller) {
nodeReadableStream.pause();
nodeReadableStream.on('data', (chunk) => {
controller.enqueue(chunk);
nodeReadableStream.pause();
});
nodeReadableStream.once('end', () => controller.close());
nodeReadableStream.once('error', (e) => controller.error(e));
},
pull(controller) {
nodeReadableStream.resume();
},
cancel() {
nodeReadableStream.destroy();
}
});
// Get a Reader for the stream.
const reader = stream.getReader();
// Start reading from the reader.
doReadEverything();
// Function that reads from the reader, consuming all data.
async function doReadEverything() {
let value;
let done = false;
while (!done) {
({value, done} = await reader.read());
// Do something with value, if `done` is still not `true`
}
// Finished reading. Do something
}