smartstream/ts/smartstream.functions.ts

31 lines
823 B
TypeScript
Raw Normal View History

2023-11-03 22:25:00 +00:00
import { Transform, type TransformCallback, type TransformOptions } from 'stream';
2023-11-11 18:47:20 +00:00
import { SmartDuplex } from './smartstream.classes.smartduplex.js';
2023-11-03 22:25:00 +00:00
export interface AsyncTransformFunction<TInput, TOutput> {
(chunkArg: TInput): Promise<TOutput>;
}
export function createTransformFunction<TInput, TOutput>(
asyncFunction: AsyncTransformFunction<TInput, TOutput>,
options?: TransformOptions
2023-11-11 18:47:20 +00:00
): SmartDuplex {
const smartDuplexStream = new SmartDuplex({
2023-11-03 22:25:00 +00:00
...options,
2023-11-11 18:47:20 +00:00
writeFunction: async (chunkArg, toolsArg) => {
const result = await asyncFunction(chunkArg);
return result;
2023-11-03 22:25:00 +00:00
}
2023-11-11 19:30:42 +00:00
});
2023-11-03 22:25:00 +00:00
2023-11-11 18:47:20 +00:00
return smartDuplexStream;
2023-11-13 20:38:12 +00:00
}
export const createPassThrough = () => {
return new SmartDuplex({
objectMode: true,
writeFunction: async (chunkArg, toolsArg) => {
return chunkArg;
}
})
2024-03-16 17:29:44 +00:00
}