fix(core): update

This commit is contained in:
Philipp Kunz 2023-11-03 23:25:00 +01:00
parent 91d01f3689
commit b135e6023a
3 changed files with 29 additions and 1 deletions

View File

@ -3,6 +3,6 @@
*/
export const commitinfo = {
name: '@push.rocks/smartstream',
version: '3.0.5',
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

@ -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;
}