{"library":"node-readable-to-web-readable-stream","title":"Node.js Readable to Web ReadableStream Conversion Utility","description":"node-readable-to-web-readable-stream is a focused utility library designed to bridge the gap between Node.js `stream.Readable` and the Web API `ReadableStream`. It provides functions to convert Node.js readable streams into either byte-mode (BYOB-compliant) or default-mode WHATWG ReadableStreams, enabling seamless integration with web-native streaming APIs. The current stable version is `0.4.2`, with releases occurring relatively frequently to address bug fixes and introduce enhancements, such as improved backpressure mechanisms and default stream support. Key differentiators include its explicit support for BYOB readers, robust backpressure handling, and cross-platform compatibility across Node.js (>=18), Bun (>=1.2), and modern web browsers. It is distributed as an ECMAScript Module (ESM).","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install node-readable-to-web-readable-stream"],"cli":null},"imports":["import { makeByteReadableStreamFromNodeReadable } from 'node-readable-to-web-readable-stream';","import { makeDefaultReadableStreamFromNodeReadable } from 'node-readable-to-web-readable-stream';","import { toWebReadableStream } from 'node-readable-to-web-readable-stream';","import type { Readable } from 'node:stream';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { createReadStream } from 'node:fs';\nimport { makeByteReadableStreamFromNodeReadable } from 'node-readable-to-web-readable-stream';\n\n// Imagine 'large-file.bin' is a large binary file\n// Create a Node.js Readable stream\nconst nodeReadable = createReadStream('large-file.bin');\n\n// Convert to a web ReadableStream, specifically a byte stream for BYOB support\nconst webReadable = makeByteReadableStreamFromNodeReadable(nodeReadable);\n\n// Example: Consume the webReadable using a BYOB reader\nasync function readStream(stream: ReadableStream<Uint8Array>) {\n  const reader = stream.getReader({ mode: 'byob' });\n  try {\n    while (true) {\n      const { value, done } = await reader.read(new Uint8Array(1024)); // Read into a pre-allocated buffer\n      if (done) {\n        console.log('Stream finished.');\n        break;\n      }\n      if (value) {\n        console.log(`Read ${value.byteLength} bytes.`);\n        // Process the chunk here\n      }\n    }\n  } catch (error) {\n    console.error('Error reading stream:', error);\n  } finally {\n    reader.releaseLock();\n  }\n}\n\n// In a real application, you'd ensure 'large-file.bin' exists or create it.\n// For demonstration, we'll create a dummy file if it doesn't exist.\nimport { promises as fsPromises } from 'node:fs';\nasync function setupAndRun() {\n  try {\n    await fsPromises.access('large-file.bin');\n  } catch (error) {\n    await fsPromises.writeFile('large-file.bin', Buffer.alloc(1024 * 1024, 'A')); // 1MB dummy file\n    console.log('Created dummy large-file.bin');\n  }\n  readStream(webReadable);\n}\nsetupAndRun();","lang":"typescript","description":"Demonstrates converting a Node.js `fs.createReadStream` into a Web API `ReadableStream` (byte mode) and consuming it with a BYOB reader.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}