Compare commits
22 Commits
Author | SHA1 | Date | |
---|---|---|---|
12c9d8cc9d | |||
3a2dc1c37e | |||
1f67bc0e1e | |||
b15ddd987c | |||
cc43080513 | |||
49d235411f | |||
d238662bea | |||
8efb2b1093 | |||
4926f57d83 | |||
86552f2b1b | |||
353a8ecde6 | |||
3e03b81a43 | |||
5e4ec5b837 | |||
62796f7151 | |||
2c1d9f05ce | |||
34cbf28972 | |||
1b6e38c040 | |||
b135e6023a | |||
91d01f3689 | |||
e8e067ea77 | |||
2cb490cd2a | |||
98397bb85e |
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@push.rocks/smartstream",
|
||||
"version": "3.0.3",
|
||||
"version": "3.0.14",
|
||||
"private": false,
|
||||
"description": "simplifies access to node streams",
|
||||
"main": "dist_ts/index.js",
|
||||
@ -24,9 +24,10 @@
|
||||
"devDependencies": {
|
||||
"@git.zone/tsbuild": "^2.1.66",
|
||||
"@git.zone/tsrun": "^1.2.44",
|
||||
"@git.zone/tstest": "^1.0.77",
|
||||
"@push.rocks/smartfile": "^10.0.33",
|
||||
"@push.rocks/tapbundle": "^5.0.15"
|
||||
"@git.zone/tstest": "^1.0.84",
|
||||
"@push.rocks/smartfile": "^11.0.0",
|
||||
"@push.rocks/tapbundle": "^5.0.15",
|
||||
"@types/node": "^20.9.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"@push.rocks/smartpromise": "^4.0.3",
|
||||
|
540
pnpm-lock.yaml
generated
540
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
0
readme_instructions.md
Normal file
0
readme_instructions.md
Normal file
@ -10,21 +10,21 @@ tap.test('should handle a read stream', async (tools) => {
|
||||
const streamWrapper = new smartstream.StreamWrapper([
|
||||
smartfile.fsStream.createReadStream('./test/assets/readabletext.txt'),
|
||||
new smartstream.SmartDuplex({
|
||||
writeAndTransformFunction: async (chunkStringArg: Buffer, streamTools) => {
|
||||
writeFunction: async (chunkStringArg: Buffer, streamTools) => {
|
||||
// do something with the stream here
|
||||
const result = chunkStringArg.toString().substr(0, 100);
|
||||
streamTools.push('wow =========== \n');
|
||||
return Buffer.from(result);
|
||||
},
|
||||
streamEndFunction: async (tools) => {
|
||||
finalFunction: async (tools) => {
|
||||
return Buffer.from('this is the end');
|
||||
},
|
||||
}),
|
||||
new smartstream.SmartDuplex({
|
||||
writeAndTransformFunction: async (chunkStringArg) => {
|
||||
writeFunction: async (chunkStringArg) => {
|
||||
console.log(chunkStringArg.toString());
|
||||
},
|
||||
streamEndFunction: async (tools) => {
|
||||
finalFunction: async (tools) => {
|
||||
tools.push(null);
|
||||
},
|
||||
})
|
||||
@ -37,7 +37,7 @@ tap.test('should create a valid Intake', async (tools) => {
|
||||
testIntake.pipe(
|
||||
new smartstream.SmartDuplex({
|
||||
objectMode: true,
|
||||
writeAndTransformFunction: async (chunkStringArg: string, streamTools) => {
|
||||
writeFunction: async (chunkStringArg: string, streamTools) => {
|
||||
await tools.delayFor(100);
|
||||
console.log(chunkStringArg);
|
||||
return chunkStringArg;
|
||||
|
@ -3,6 +3,6 @@
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@push.rocks/smartstream',
|
||||
version: '3.0.3',
|
||||
version: '3.0.14',
|
||||
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.streamwrapper.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)) {
|
||||
callback();
|
||||
} else {
|
||||
this.once('drain', callback);
|
||||
this.once('drain', () => {
|
||||
callback();
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
@ -6,18 +6,19 @@ export interface IStreamTools {
|
||||
push: (pipeObject: any) => void;
|
||||
}
|
||||
|
||||
export interface IWriteAndTransformFunction<T, rT> {
|
||||
export interface IStreamWriteFunction<T, rT> {
|
||||
(chunkArg: T, toolsArg: IStreamTools): Promise<rT>;
|
||||
}
|
||||
|
||||
export interface IStreamEndFunction<rT> {
|
||||
export interface IStreamFinalFunction<rT> {
|
||||
(toolsArg: IStreamTools): Promise<rT>;
|
||||
}
|
||||
|
||||
export interface SmartStreamOptions<TInput, TOutput> extends DuplexOptions {
|
||||
handleBackpressure?: boolean;
|
||||
readFunction?: () => Promise<void>;
|
||||
writeAndTransformFunction?: IWriteAndTransformFunction<TInput, TOutput>;
|
||||
streamEndFunction?: IStreamEndFunction<TOutput>;
|
||||
writeFunction?: IStreamWriteFunction<TInput, TOutput>;
|
||||
finalFunction?: IStreamFinalFunction<TOutput>;
|
||||
// Add other custom options if necessary
|
||||
}
|
||||
|
||||
@ -109,15 +110,17 @@ export class SmartDuplex<TInput = any, TOutput = any> extends Duplex {
|
||||
|
||||
// INSTANCE
|
||||
private readFunction?: () => Promise<void>;
|
||||
private writeAndTransformFunction?: IWriteAndTransformFunction<TInput, TOutput>;
|
||||
private streamEndFunction?: IStreamEndFunction<TOutput>;
|
||||
private handleBackpressure: boolean;
|
||||
private writeFunction?: IStreamWriteFunction<TInput, TOutput>;
|
||||
private finalFunction?: IStreamFinalFunction<TOutput>;
|
||||
private observableSubscription?: plugins.smartrx.rxjs.Subscription;
|
||||
|
||||
constructor(optionsArg?: SmartStreamOptions<TInput, TOutput>) {
|
||||
super(optionsArg);
|
||||
this.readFunction = optionsArg?.readFunction;
|
||||
this.writeAndTransformFunction = optionsArg?.writeAndTransformFunction;
|
||||
this.streamEndFunction = optionsArg?.streamEndFunction;
|
||||
this.writeFunction = optionsArg?.writeFunction;
|
||||
this.finalFunction = optionsArg?.finalFunction;
|
||||
this.handleBackpressure = optionsArg?.handleBackpressure ?? true;
|
||||
}
|
||||
|
||||
public async _read(size: number): Promise<void> {
|
||||
@ -128,7 +131,7 @@ export class SmartDuplex<TInput = any, TOutput = any> extends Duplex {
|
||||
|
||||
// Ensure the _write method types the chunk as TInput and encodes TOutput
|
||||
public async _write(chunk: TInput, encoding: string, callback: (error?: Error | null) => void) {
|
||||
if (!this.writeAndTransformFunction) {
|
||||
if (!this.writeFunction) {
|
||||
return callback(new Error('No stream function provided'));
|
||||
}
|
||||
|
||||
@ -141,10 +144,14 @@ export class SmartDuplex<TInput = any, TOutput = any> extends Duplex {
|
||||
};
|
||||
|
||||
try {
|
||||
const modifiedChunk = await this.writeAndTransformFunction(chunk, tools);
|
||||
const modifiedChunk = await this.writeFunction(chunk, tools);
|
||||
if (modifiedChunk) {
|
||||
if (!this.push(modifiedChunk)) {
|
||||
// Handle backpressure if necessary
|
||||
if (!this.push(modifiedChunk) && this.handleBackpressure) {
|
||||
this.pause();
|
||||
// Listen for 'drain' event to resume
|
||||
this.once('drain', () => {
|
||||
this.resume(); // Resume the source of data
|
||||
});
|
||||
}
|
||||
}
|
||||
callback();
|
||||
@ -154,24 +161,24 @@ export class SmartDuplex<TInput = any, TOutput = any> extends Duplex {
|
||||
}
|
||||
|
||||
public async _final(callback: (error?: Error | null) => void) {
|
||||
if (this.streamEndFunction) {
|
||||
if (this.finalFunction) {
|
||||
const tools: IStreamTools = {
|
||||
truncate: () => callback(),
|
||||
push: (pipeObject) => this.push(pipeObject),
|
||||
};
|
||||
|
||||
try {
|
||||
const finalChunk = await this.streamEndFunction(tools);
|
||||
const finalChunk = await this.finalFunction(tools);
|
||||
if (finalChunk) {
|
||||
this.push(finalChunk);
|
||||
}
|
||||
callback();
|
||||
} catch (err) {
|
||||
callback(err);
|
||||
}
|
||||
} else {
|
||||
this.push(null),
|
||||
callback();
|
||||
// nothing here
|
||||
}
|
||||
this.push(null);
|
||||
callback();
|
||||
}
|
||||
}
|
||||
|
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