Compare commits
8 Commits
Author | SHA1 | Date | |
---|---|---|---|
703dc11c6c | |||
28725d1723 | |||
c77e0f2ba6 | |||
196fb6d396 | |||
df0ddf04b3 | |||
2e1aa4a8ff | |||
bc09033af0 | |||
22df9dfd94 |
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@push.rocks/smartstream",
|
||||
"version": "3.0.22",
|
||||
"version": "3.0.26",
|
||||
"private": false,
|
||||
"description": "simplifies access to node streams",
|
||||
"main": "dist_ts/index.js",
|
||||
|
@ -7,7 +7,6 @@ tap.test('should run backpressure test', async (toolsArg) => {
|
||||
const stream1 = new SmartDuplex({
|
||||
name: 'stream1',
|
||||
objectMode: true,
|
||||
handleBackpressure: true,
|
||||
writeFunction: async (chunk, tools) => {
|
||||
await new Promise((resolve) => setTimeout(resolve, 10)); // Slow processing
|
||||
console.log(`processed chunk ${chunk} in stream 1`);
|
||||
@ -17,17 +16,16 @@ tap.test('should run backpressure test', async (toolsArg) => {
|
||||
const stream2 = new SmartDuplex({
|
||||
name: 'stream2',
|
||||
objectMode: true,
|
||||
handleBackpressure: true,
|
||||
writeFunction: async (chunk, tools) => {
|
||||
await new Promise((resolve) => setTimeout(resolve, 20)); // Slow processing
|
||||
console.log(`processed chunk ${chunk} in stream 2`);
|
||||
// await tools.push(chunk);
|
||||
return chunk;
|
||||
},
|
||||
}); // This stream processes data more slowly
|
||||
const stream3 = new SmartDuplex({
|
||||
objectMode: true,
|
||||
name: 'stream3',
|
||||
handleBackpressure: true,
|
||||
writeFunction: async (chunk, tools) => {
|
||||
await new Promise((resolve) => setTimeout(resolve, 100)); // Slow processing
|
||||
console.log(`processed chunk ${chunk} in stream 3`);
|
||||
|
@ -5,9 +5,7 @@ import * as fs from 'fs';
|
||||
|
||||
tap.test('should create a SmartStream from a Buffer', async () => {
|
||||
const bufferData = Buffer.from('This is a test buffer');
|
||||
const smartStream = SmartDuplex.fromBuffer(bufferData, {
|
||||
handleBackpressure: false,
|
||||
});
|
||||
const smartStream = SmartDuplex.fromBuffer(bufferData, {});
|
||||
|
||||
let receivedData = Buffer.alloc(0);
|
||||
|
||||
|
@ -3,6 +3,6 @@
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@push.rocks/smartstream',
|
||||
version: '3.0.22',
|
||||
version: '3.0.26',
|
||||
description: 'simplifies access to node streams'
|
||||
}
|
||||
|
@ -17,7 +17,6 @@ export interface IStreamFinalFunction<rT> {
|
||||
export interface ISmartDuplexOptions<TInput, TOutput> extends DuplexOptions {
|
||||
debug?: boolean;
|
||||
name?: string;
|
||||
handleBackpressure?: boolean;
|
||||
readFunction?: () => Promise<void>;
|
||||
writeFunction?: IStreamWriteFunction<TInput, TOutput>;
|
||||
finalFunction?: IStreamFinalFunction<TOutput>;
|
||||
@ -36,7 +35,7 @@ export class SmartDuplex<TInput = any, TOutput = any> extends Duplex {
|
||||
}
|
||||
|
||||
// INSTANCE
|
||||
private backpressuredArray = new plugins.lik.BackpressuredArray<TOutput>();
|
||||
private backpressuredArray: plugins.lik.BackpressuredArray<TOutput>;
|
||||
public options: ISmartDuplexOptions<TInput, TOutput>;
|
||||
private observableSubscription?: plugins.smartrx.rxjs.Subscription;
|
||||
private debugLog(messageArg: string) {
|
||||
@ -46,8 +45,11 @@ export class SmartDuplex<TInput = any, TOutput = any> extends Duplex {
|
||||
}
|
||||
|
||||
constructor(optionsArg?: ISmartDuplexOptions<TInput, TOutput>) {
|
||||
super(optionsArg);
|
||||
super(Object.assign({
|
||||
highWaterMark: 1,
|
||||
}, optionsArg));
|
||||
this.options = optionsArg;
|
||||
this.backpressuredArray = new plugins.lik.BackpressuredArray<TOutput>(this.options.highWaterMark || 1)
|
||||
}
|
||||
|
||||
public async _read(size: number): Promise<void> {
|
||||
@ -60,9 +62,7 @@ export class SmartDuplex<TInput = any, TOutput = any> extends Duplex {
|
||||
let canPushMore = true;
|
||||
while(this.backpressuredArray.data.length > 0 && canPushMore) {
|
||||
const nextChunk = this.backpressuredArray.shift();
|
||||
if (nextChunk) {
|
||||
canPushMore = this.push(nextChunk);
|
||||
}
|
||||
canPushMore = this.push(nextChunk);
|
||||
}
|
||||
}
|
||||
|
||||
@ -116,22 +116,22 @@ export class SmartDuplex<TInput = any, TOutput = any> extends Duplex {
|
||||
const tools: IStreamTools = {
|
||||
truncate: () => callback(),
|
||||
push: async (pipeObject) => {
|
||||
this.push(pipeObject);
|
||||
this.backpressuredArray.push(pipeObject);
|
||||
},
|
||||
};
|
||||
|
||||
try {
|
||||
const finalChunk = await this.options.finalFunction(tools);
|
||||
if (finalChunk) {
|
||||
this.push(finalChunk);
|
||||
this.backpressuredArray.push(finalChunk);
|
||||
}
|
||||
} catch (err) {
|
||||
this.push(null);
|
||||
this.backpressuredArray.push(null);
|
||||
callback(err);
|
||||
return;
|
||||
}
|
||||
}
|
||||
this.push(null);
|
||||
this.backpressuredArray.push(null);
|
||||
callback();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user