fix(nodewebhelpers): Fix import and use correct module structure for Node.js streams in smartstream.nodewebhelpers.ts
This commit is contained in:
parent
673f5c86fb
commit
9e3fd28c4a
@ -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
|
||||
|
||||
|
@ -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.'
|
||||
}
|
||||
|
@ -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<Uint8Array> {
|
||||
const fileStream = createReadStream(filePath);
|
||||
const fileStream = plugins.fs.createReadStream(filePath);
|
||||
|
||||
return new ReadableStream({
|
||||
start(controller) {
|
||||
@ -32,3 +32,120 @@ export function createWebReadableStreamFromFile(filePath: string): ReadableStrea
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 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<Uint8Array>): 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<Uint8Array> {
|
||||
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<Uint8Array>): 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<Uint8Array> {
|
||||
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);
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
@ -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';
|
||||
|
@ -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.'
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user