Compare commits
12 Commits
Author | SHA1 | Date | |
---|---|---|---|
4926f57d83 | |||
86552f2b1b | |||
353a8ecde6 | |||
3e03b81a43 | |||
5e4ec5b837 | |||
62796f7151 | |||
2c1d9f05ce | |||
34cbf28972 | |||
1b6e38c040 | |||
b135e6023a | |||
91d01f3689 | |||
e8e067ea77 |
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@push.rocks/smartstream",
|
"name": "@push.rocks/smartstream",
|
||||||
"version": "3.0.4",
|
"version": "3.0.10",
|
||||||
"private": false,
|
"private": false,
|
||||||
"description": "simplifies access to node streams",
|
"description": "simplifies access to node streams",
|
||||||
"main": "dist_ts/index.js",
|
"main": "dist_ts/index.js",
|
||||||
|
@ -16,7 +16,7 @@ tap.test('should handle a read stream', async (tools) => {
|
|||||||
streamTools.push('wow =========== \n');
|
streamTools.push('wow =========== \n');
|
||||||
return Buffer.from(result);
|
return Buffer.from(result);
|
||||||
},
|
},
|
||||||
streamEndFunction: async (tools) => {
|
finalFunction: async (tools) => {
|
||||||
return Buffer.from('this is the end');
|
return Buffer.from('this is the end');
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
@ -24,7 +24,7 @@ tap.test('should handle a read stream', async (tools) => {
|
|||||||
writeAndTransformFunction: async (chunkStringArg) => {
|
writeAndTransformFunction: async (chunkStringArg) => {
|
||||||
console.log(chunkStringArg.toString());
|
console.log(chunkStringArg.toString());
|
||||||
},
|
},
|
||||||
streamEndFunction: async (tools) => {
|
finalFunction: async (tools) => {
|
||||||
tools.push(null);
|
tools.push(null);
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
@ -3,6 +3,6 @@
|
|||||||
*/
|
*/
|
||||||
export const commitinfo = {
|
export const commitinfo = {
|
||||||
name: '@push.rocks/smartstream',
|
name: '@push.rocks/smartstream',
|
||||||
version: '3.0.4',
|
version: '3.0.10',
|
||||||
description: 'simplifies access to node streams'
|
description: 'simplifies access to node streams'
|
||||||
}
|
}
|
||||||
|
@ -2,3 +2,5 @@ export * from './smartstream.classes.passthrough.js';
|
|||||||
export * from './smartstream.classes.smartduplex.js';
|
export * from './smartstream.classes.smartduplex.js';
|
||||||
export * from './smartstream.classes.streamwrapper.js';
|
export * from './smartstream.classes.streamwrapper.js';
|
||||||
export * from './smartstream.classes.streamintake.js';
|
export * from './smartstream.classes.streamintake.js';
|
||||||
|
|
||||||
|
export * from './smartstream.functions.js'
|
||||||
|
@ -13,7 +13,9 @@ export class PassThrough extends plugins.stream.Duplex {
|
|||||||
if (this.push(chunk, encoding)) {
|
if (this.push(chunk, encoding)) {
|
||||||
callback();
|
callback();
|
||||||
} else {
|
} else {
|
||||||
this.once('drain', callback);
|
this.once('drain', () => {
|
||||||
|
callback();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -17,7 +17,7 @@ export interface IStreamEndFunction<rT> {
|
|||||||
export interface SmartStreamOptions<TInput, TOutput> extends DuplexOptions {
|
export interface SmartStreamOptions<TInput, TOutput> extends DuplexOptions {
|
||||||
readFunction?: () => Promise<void>;
|
readFunction?: () => Promise<void>;
|
||||||
writeAndTransformFunction?: IWriteAndTransformFunction<TInput, TOutput>;
|
writeAndTransformFunction?: IWriteAndTransformFunction<TInput, TOutput>;
|
||||||
streamEndFunction?: IStreamEndFunction<TOutput>;
|
finalFunction?: IStreamEndFunction<TOutput>;
|
||||||
// Add other custom options if necessary
|
// Add other custom options if necessary
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -117,7 +117,7 @@ export class SmartDuplex<TInput = any, TOutput = any> extends Duplex {
|
|||||||
super(optionsArg);
|
super(optionsArg);
|
||||||
this.readFunction = optionsArg?.readFunction;
|
this.readFunction = optionsArg?.readFunction;
|
||||||
this.writeAndTransformFunction = optionsArg?.writeAndTransformFunction;
|
this.writeAndTransformFunction = optionsArg?.writeAndTransformFunction;
|
||||||
this.streamEndFunction = optionsArg?.streamEndFunction;
|
this.streamEndFunction = optionsArg?.finalFunction;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async _read(size: number): Promise<void> {
|
public async _read(size: number): Promise<void> {
|
||||||
@ -170,8 +170,8 @@ export class SmartDuplex<TInput = any, TOutput = any> extends Duplex {
|
|||||||
callback(err);
|
callback(err);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
this.push(null),
|
|
||||||
callback();
|
callback();
|
||||||
}
|
}
|
||||||
|
this.push(null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
26
ts/smartstream.functions.ts
Normal file
26
ts/smartstream.functions.ts
Normal 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;
|
||||||
|
}
|
Reference in New Issue
Block a user