Web Stream Tools

0.0.1 · active · verified Tue Apr 21

Web Stream Tools (available as `@openpgp/web-stream-tools` on npm) is a JavaScript utility library designed to simplify working with WhatWG Streams. It provides a comprehensive set of functions for common stream operations, including reading to completion (`readToEnd`), concatenating streams (`concat`), slicing parts of streams (`slice`), cloning streams (`clone`), and facilitating conversion between Node.js streams and Web Streams (`webToNode`, `nodeToWeb`). The library also features advanced capabilities for transforming stream data, either directly (`transform`) or in chunks with backpressure handling (`transformPair`), and for parsing structured data from streams (`parse`) with functions like `readByte`, `readBytes`, and `readLine`. The current stable version is 0.3.0. Developed by the OpenPGP.js team, it maintains an active development status, with updates generally released as needed. A key differentiator is its explicit focus on the WhatWG Streams API, providing a consistent interface across browser and modern Node.js environments, streamlining complex stream manipulations that often involve manual reader/writer management.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to use the `stream.transform` function to encrypt data flowing through a WhatWG ReadableStream, including setup for an imaginary encryptor and reading the output.

import * as stream from '@openpgp/web-stream-tools';

// Imagine an encryptor API with process and finish methods
class Encryptor {
  process(chunk) {
    console.log('Processing chunk:', chunk.byteLength, 'bytes');
    // Simulate encryption, return a new Uint8Array
    return new Uint8Array(chunk.map(b => b ^ 0xFF));
  }
  finish() {
    console.log('Finishing encryption.');
    // Return any final encrypted bytes, or an empty Uint8Array
    return new Uint8Array([0xDE, 0xAD, 0xBE, 0xEF]);
  }
}

async function encryptDataStream() {
  const encryptor = new new Encryptor();
  const inputData = new TextEncoder().encode("This is a test string to be encrypted using web-stream-tools.");
  
  // Create a ReadableStream from the input data
  const input = new ReadableStream({
    start(controller) {
      controller.enqueue(inputData);
      controller.close();
    }
  });

  const encryptedStream = stream.transform(input, function process(chunk) {
    return encryptor.process(chunk);
  }, function finish() {
    return encryptor.finish();
  });

  // Read the encrypted stream to the end and collect all chunks
  const reader = encryptedStream.getReader();
  let allChunks = [];
  while (true) {
    const { done, value } = await reader.read();
    if (done) {
      break;
    }
    allChunks.push(value);
  }
  const finalEncryptedData = new Uint8Array(allChunks.reduce((acc, chunk) => acc.concat(Array.from(chunk)), []));
  console.log('Total encrypted data length:', finalEncryptedData.byteLength, 'bytes');
  console.log('Encryption complete.');
}

encryptDataStream().catch(console.error);

view raw JSON →