1.0.0 • Published 5 years ago

whatwg-streams-impl v1.0.0

Weekly downloads
4
License
MIT
Repository
github
Last release
5 years ago

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
}