From 9e3fd28c4a93479f55b2d94c3814fe1d8541be60 Mon Sep 17 00:00:00 2001 From: Philipp Kunz Date: Tue, 19 Nov 2024 09:51:05 +0100 Subject: [PATCH] fix(nodewebhelpers): Fix import and use correct module structure for Node.js streams in smartstream.nodewebhelpers.ts --- changelog.md | 6 ++ ts/00_commitinfo_data.ts | 2 +- ts/smartstream.nodewebhelpers.ts | 121 ++++++++++++++++++++++++++++++- ts/smartstream.plugins.ts | 3 +- ts_web/00_commitinfo_data.ts | 2 +- 5 files changed, 129 insertions(+), 5 deletions(-) diff --git a/changelog.md b/changelog.md index dc9d296..3471add 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,11 @@ # Changelog +## 2024-11-19 - 3.2.5 - fix(nodewebhelpers) +Fix import and use correct module structure for Node.js streams in smartstream.nodewebhelpers.ts + +- Corrected the import statement for the fs module. +- Updated the use of the fs.createReadStream method. + ## 2024-10-16 - 3.2.4 - fix(SmartDuplex) Fix stream termination when reading from a web readable stream diff --git a/ts/00_commitinfo_data.ts b/ts/00_commitinfo_data.ts index 68ef49f..47af58a 100644 --- a/ts/00_commitinfo_data.ts +++ b/ts/00_commitinfo_data.ts @@ -3,6 +3,6 @@ */ export const commitinfo = { name: '@push.rocks/smartstream', - version: '3.2.4', + version: '3.2.5', description: 'A library to simplify the creation and manipulation of Node.js streams, providing utilities for handling transform, duplex, and readable/writable streams effectively in TypeScript.' } diff --git a/ts/smartstream.nodewebhelpers.ts b/ts/smartstream.nodewebhelpers.ts index ff60737..046c494 100644 --- a/ts/smartstream.nodewebhelpers.ts +++ b/ts/smartstream.nodewebhelpers.ts @@ -1,4 +1,4 @@ -import { createReadStream } from 'fs'; +import * as plugins from './smartstream.plugins.js'; /** * Creates a Web ReadableStream from a file. @@ -7,7 +7,7 @@ import { createReadStream } from 'fs'; * @returns A Web ReadableStream that reads the file in chunks */ export function createWebReadableStreamFromFile(filePath: string): ReadableStream { - const fileStream = createReadStream(filePath); + const fileStream = plugins.fs.createReadStream(filePath); return new ReadableStream({ start(controller) { @@ -31,4 +31,121 @@ export function createWebReadableStreamFromFile(filePath: string): ReadableStrea fileStream.destroy(); } }); +} + +/** + * Converts a Web ReadableStream to a Node.js Readable stream. + * + * @param webStream - The Web ReadableStream to convert + * @returns A Node.js Readable stream that reads data from the Web ReadableStream + */ +export function convertWebReadableToNodeReadable(webStream: ReadableStream): plugins.stream.Readable { + const reader = webStream.getReader(); + + return new plugins.stream.Readable({ + async read() { + try { + const { value, done } = await reader.read(); + if (done) { + this.push(null); // Signal end of stream + } else { + this.push(Buffer.from(value)); // Convert Uint8Array to Buffer for Node.js Readable + } + } catch (err) { + this.destroy(err); // Handle errors by destroying the stream + } + } + }); +} + +/** + * Converts a Node.js Readable stream to a Web ReadableStream. + * + * @param nodeStream - The Node.js Readable stream to convert + * @returns A Web ReadableStream that reads data from the Node.js Readable stream + */ +export function convertNodeReadableToWebReadable(nodeStream: plugins.stream.Readable): ReadableStream { + return new ReadableStream({ + start(controller) { + nodeStream.on('data', (chunk) => { + controller.enqueue(new Uint8Array(chunk)); + }); + + nodeStream.on('end', () => { + controller.close(); + }); + + nodeStream.on('error', (err) => { + controller.error(err); + }); + }, + cancel() { + nodeStream.destroy(); + } + }); +} + +/** + * Converts a Web WritableStream to a Node.js Writable stream. + * + * @param webWritable - The Web WritableStream to convert + * @returns A Node.js Writable stream that writes data to the Web WritableStream + */ +export function convertWebWritableToNodeWritable(webWritable: WritableStream): plugins.stream.Writable { + const writer = webWritable.getWriter(); + + return new plugins.stream.Writable({ + async write(chunk, encoding, callback) { + try { + await writer.write(new Uint8Array(chunk)); + callback(); + } catch (err) { + callback(err); + } + }, + final(callback) { + writer.close().then(() => callback()).catch(callback); + }, + destroy(err, callback) { + writer.abort(err).then(() => callback(err)).catch(callback); + } + }); +} + +/** + * Converts a Node.js Writable stream to a Web WritableStream. + * + * @param nodeWritable - The Node.js Writable stream to convert + * @returns A Web WritableStream that writes data to the Node.js Writable stream + */ +export function convertNodeWritableToWebWritable(nodeWritable: plugins.stream.Writable): WritableStream { + return new WritableStream({ + write(chunk) { + return new Promise((resolve, reject) => { + nodeWritable.write(Buffer.from(chunk), (err) => { + if (err) { + reject(err); + } else { + resolve(); + } + }); + }); + }, + close() { + return new Promise((resolve, reject) => { + nodeWritable.end((err) => { + if (err) { + reject(err); + } else { + resolve(); + } + }); + }); + }, + abort(reason) { + return new Promise((resolve, reject) => { + nodeWritable.destroy(reason); + }); + } + }); } \ No newline at end of file diff --git a/ts/smartstream.plugins.ts b/ts/smartstream.plugins.ts index e210f10..a77ed92 100644 --- a/ts/smartstream.plugins.ts +++ b/ts/smartstream.plugins.ts @@ -1,7 +1,8 @@ // node native +import * as fs from 'fs'; import * as stream from 'stream'; -export { stream }; +export { fs, stream }; // pushrocks scope import * as lik from '@push.rocks/lik'; diff --git a/ts_web/00_commitinfo_data.ts b/ts_web/00_commitinfo_data.ts index 68ef49f..47af58a 100644 --- a/ts_web/00_commitinfo_data.ts +++ b/ts_web/00_commitinfo_data.ts @@ -3,6 +3,6 @@ */ export const commitinfo = { name: '@push.rocks/smartstream', - version: '3.2.4', + version: '3.2.5', description: 'A library to simplify the creation and manipulation of Node.js streams, providing utilities for handling transform, duplex, and readable/writable streams effectively in TypeScript.' }