{"library":"readable-web-to-node-stream","title":"Web ReadableStream to Node.js Readable Stream Converter","description":"`readable-web-to-node-stream` is a utility package designed to seamlessly convert a Web-API `ReadableStream` (typically obtained from browser `fetch` responses or other Web Streams API sources) into a Node.js `Readable` stream. This conversion allows developers to process data originating from web contexts using familiar Node.js stream APIs and utilities, integrating browser-side streaming capabilities with Node.js backend processing. The current stable version is 5.0.0. The package maintains an active release cadence, frequently incorporating improvements and addressing breaking changes, notably the migration to a pure ECMAScript Module (ESM) in version 4.0.0. Key differentiators include its singular focus on this specific conversion direction, first-class TypeScript support, and a clear distinction from its complementary package for the reverse conversion. It targets modern JavaScript environments, requiring Node.js version 18 or newer.","language":"javascript","status":"active","last_verified":"Wed Apr 22","install":{"commands":["npm install readable-web-to-node-stream"],"cli":null},"imports":["import { ReadableWebToNodeStream } from 'readable-web-to-node-stream';","import type { ReadableWebToNodeStream } from 'readable-web-to-node-stream';","import * as WebToNodeStream from 'readable-web-to-node-stream';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { ReadableWebToNodeStream } from 'readable-web-to-node-stream';\nimport { createWriteStream } from 'fs';\nimport { pipeline } from 'stream/promises';\n\nasync function downloadAndSave(url: string, filePath: string) {\n    console.log(`Downloading ${url} and saving to ${filePath}`);\n    // Ensure 'fetch' is available (global in browsers, or polyfilled/imported in Node.js < 18 or for specific use cases)\n    const response = await fetch(url);\n    if (!response.body) {\n        throw new Error('Response body is null. Cannot convert an empty body.');\n    }\n    const webReadableStream = response.body;\n    \n    // Convert the Web-API ReadableStream to a Node.js Readable stream\n    const nodeStream = new ReadableWebToNodeStream(webReadableStream, { propagateDestroy: true });\n\n    // Pipe the Node.js stream to a file using stream.pipeline for robust error handling and cleanup\n    await pipeline(\n        nodeStream,\n        createWriteStream(filePath)\n    );\n    console.log(`Download complete: ${filePath}`);\n}\n\n// Example usage: Downloads a small dummy file from a public URL\n// Replace with a suitable URL for actual testing. For local testing, a simple text file is good.\nconst exampleUrl = 'https://raw.githubusercontent.com/Borewit/readable-web-to-node-stream/master/package.json';\nconst outputFilePath = './downloaded-package.json';\n\ndownloadAndSave(exampleUrl, outputFilePath)\n    .catch(error => console.error('An error occurred during download:', error));\n","lang":"typescript","description":"Demonstrates converting a `fetch` response's Web-API ReadableStream into a Node.js Readable stream and piping it to a file, including error handling.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}