21 lines
644 B
TypeScript
21 lines
644 B
TypeScript
import { Transform, type TransformCallback, type TransformOptions } from 'stream';
|
|
import { SmartDuplex } from './smartstream.classes.smartduplex.js';
|
|
|
|
export interface AsyncTransformFunction<TInput, TOutput> {
|
|
(chunkArg: TInput): Promise<TOutput>;
|
|
}
|
|
|
|
export function createTransformFunction<TInput, TOutput>(
|
|
asyncFunction: AsyncTransformFunction<TInput, TOutput>,
|
|
options?: TransformOptions
|
|
): SmartDuplex {
|
|
const smartDuplexStream = new SmartDuplex({
|
|
...options,
|
|
writeFunction: async (chunkArg, toolsArg) => {
|
|
const result = await asyncFunction(chunkArg);
|
|
return result;
|
|
}
|
|
});
|
|
|
|
return smartDuplexStream;
|
|
} |