Registry / serialization / web-stream-tools

web-stream-tools

JSON →
library0.0.1jsnpmunverified

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.

npm install web-stream-tools
INSTALL
IMPORT
SIG · WEB-STREAM-TOOLS
W
web-stream-tools
serializationjavascriptv0.0.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

* as stream
import * as stream from '@openpgp/web-stream-tools';
import stream from 'web-stream-tools';
The library's official package name is `@openpgp/web-stream-tools`. Older references or custom build configurations might use `web-stream-tools` which will result in import errors or `undefined` imports. The primary export pattern is a namespace import (`* as stream`).
transform
import { transform } from '@openpgp/web-stream-tools';
import { transform } from 'web-stream-tools';
Individual functions can be tree-shaken and imported directly. Ensure the correct package name `@openpgp/web-stream-tools` is used.
readToEnd
import { readToEnd } from '@openpgp/web-stream-tools';
const { readToEnd } = require('@openpgp/web-stream-tools');
While Node.js may support CommonJS `require` for ESM packages via interoperability layers, the library is designed for modern ESM usage. Prefer `import` statements for better compatibility and tooling support.

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);
Debug
Known issues
breakingThe official package name for Web Stream Tools has been changed from `web-stream-tools` to `@openpgp/web-stream-tools`. Direct imports using the old package name will result in `Module not found` errors or `undefined` imports.
fix
Update your `package.json` dependencies and all import statements in your code from `web-stream-tools` to `@openpgp/web-stream-tools`.
affects: >=0.1.0
breakingAs of v0.1.0, the library no longer supports native Node.js `Readable` or `Writable` streams directly as input or output for many functions. It now exclusively expects and operates on WhatWG `ReadableStream` and `WritableStream` instances (often referred to as Node's WebStreams in modern Node.js).
fix
For Node.js environments, ensure you are using Node.js v17+ which provides built-in utilities like `Readable.toWeb()` for converting native Node streams to Web Streams, or `Readable.fromWeb()` for the reverse. Alternatively, transform your data into `Uint8Array` or `String` before passing to functions like `transform` if stream-to-stream conversion is not feasible.
affects: >=0.1.0
gotchaAttempting to use CommonJS `require()` syntax with `@openpgp/web-stream-tools` might not work as expected or could lead to bundling issues. The library is primarily designed for ES Module (`import`) usage with Web Streams, especially given its browser-compatibility focus.
fix
Always prefer `import * as stream from '@openpgp/web-stream-tools';` or named imports in ES Modules. If CommonJS is strictly required, ensure your build tool (e.g., Webpack, Rollup) or Node.js configuration (e.g., `"type": "module"` in `package.json`) handles ESM to CJS interoperability correctly.
affects: *
Errors
Common errors & fixes
Error: Cannot find module 'web-stream-tools'
The package name has changed from 'web-stream-tools' to '@openpgp/web-stream-tools'.
fix
Change the dependency in `package.json` and all import paths in your code to `@openpgp/web-stream-tools`.
TypeError: stream.transform is not a function
Incorrect import statement or module not found, leading to 'stream' being undefined or an empty object.
fix
Ensure you are using `import * as stream from '@openpgp/web-stream-tools';` (for namespace import) or `import { transform } from '@openpgp/web-stream-tools';` (for named import).
Error: The provided stream is not a WhatWG ReadableStream.
You are passing a native Node.js `stream.Readable` object to a function that expects a WhatWG `ReadableStream`.
fix
If in Node.js v17+, convert your Node stream using `myNodeStream.toWeb()` before passing it to `web-stream-tools` functions. For older Node.js versions, you may need to manually wrap the Node stream or use a polyfill.
Upgrade
Version history
0.0.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
24 hits · last 30 days
node
20
OpenAI (training)
1
Resources
web-stream-tools — npm install web-stream-tools · libregistry