whatwg-streams-impl

1.0.0 • Public • Published

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:

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
}

Package Sidebar

Install

npm i whatwg-streams-impl

Weekly Downloads

3

Version

1.0.0

License

MIT

Unpacked Size

84 kB

Total Files

35

Last publish

Collaborators

  • mpotra