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 18:47:20 +00:00
|
|
|
})
|
2023-11-03 22:25:00 +00:00
|
|
|
|
2023-11-11 18:47:20 +00:00
|
|
|
return smartDuplexStream;
|
2023-11-03 22:25:00 +00:00
|
|
|
}
|