Compare commits

...

4 Commits

Author SHA1 Message Date
1b6e38c040 3.0.6 2023-11-03 23:25:01 +01:00
b135e6023a fix(core): update 2023-11-03 23:25:00 +01:00
91d01f3689 3.0.5 2023-11-03 22:26:16 +01:00
e8e067ea77 fix(core): update 2023-11-03 22:26:15 +01:00
6 changed files with 34 additions and 6 deletions

View File

@ -1,6 +1,6 @@
{
"name": "@push.rocks/smartstream",
"version": "3.0.4",
"version": "3.0.6",
"private": false,
"description": "simplifies access to node streams",
"main": "dist_ts/index.js",

View File

@ -16,7 +16,7 @@ tap.test('should handle a read stream', async (tools) => {
streamTools.push('wow =========== \n');
return Buffer.from(result);
},
streamEndFunction: async (tools) => {
finalFunction: async (tools) => {
return Buffer.from('this is the end');
},
}),
@ -24,7 +24,7 @@ tap.test('should handle a read stream', async (tools) => {
writeAndTransformFunction: async (chunkStringArg) => {
console.log(chunkStringArg.toString());
},
streamEndFunction: async (tools) => {
finalFunction: async (tools) => {
tools.push(null);
},
})

View File

@ -3,6 +3,6 @@
*/
export const commitinfo = {
name: '@push.rocks/smartstream',
version: '3.0.4',
version: '3.0.6',
description: 'simplifies access to node streams'
}

View File

@ -2,3 +2,5 @@ export * from './smartstream.classes.passthrough.js';
export * from './smartstream.classes.smartduplex.js';
export * from './smartstream.classes.streamwrapper.js';
export * from './smartstream.classes.streamintake.js';
export * from './smartstream.functions.js'

View File

@ -17,7 +17,7 @@ export interface IStreamEndFunction<rT> {
export interface SmartStreamOptions<TInput, TOutput> extends DuplexOptions {
readFunction?: () => Promise<void>;
writeAndTransformFunction?: IWriteAndTransformFunction<TInput, TOutput>;
streamEndFunction?: IStreamEndFunction<TOutput>;
finalFunction?: IStreamEndFunction<TOutput>;
// Add other custom options if necessary
}
@ -117,7 +117,7 @@ export class SmartDuplex<TInput = any, TOutput = any> extends Duplex {
super(optionsArg);
this.readFunction = optionsArg?.readFunction;
this.writeAndTransformFunction = optionsArg?.writeAndTransformFunction;
this.streamEndFunction = optionsArg?.streamEndFunction;
this.streamEndFunction = optionsArg?.finalFunction;
}
public async _read(size: number): Promise<void> {

View File

@ -0,0 +1,26 @@
import { Transform, type TransformCallback, type TransformOptions } from 'stream';
export interface AsyncTransformFunction<TInput, TOutput> {
(chunkArg: TInput): Promise<TOutput>;
}
export function createTransformFunction<TInput, TOutput>(
asyncFunction: AsyncTransformFunction<TInput, TOutput>,
options?: TransformOptions
): Transform {
const transformStream = new Transform({
...options,
objectMode: true, // Ensure we operate in object mode
async transform(chunk: TInput, encoding: string, callback: TransformCallback) {
try {
const transformed = await asyncFunction(chunk);
this.push(transformed);
callback();
} catch (error) {
callback(error instanceof Error ? error : new Error(String(error)));
}
}
});
return transformStream;
}