Compare commits

...

4 Commits

Author SHA1 Message Date
91392e8bd5 3.0.18 2023-11-11 20:56:46 +01:00
d161d6613a fix(core): update 2023-11-11 20:56:46 +01:00
7a14e67f4f 3.0.17 2023-11-11 20:44:01 +01:00
465ccfec40 fix(core): update 2023-11-11 20:44:00 +01:00
4 changed files with 20 additions and 5 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "@push.rocks/smartstream", "name": "@push.rocks/smartstream",
"version": "3.0.16", "version": "3.0.18",
"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",

View File

@ -29,7 +29,7 @@ tap.test('should handle a read stream', async (tools) => {
}, },
}) })
]); ]);
// await streamWrapper.run(); await streamWrapper.run();
}); });
tap.test('should create a valid Intake', async (tools) => { tap.test('should create a valid Intake', async (tools) => {

View File

@ -3,6 +3,6 @@
*/ */
export const commitinfo = { export const commitinfo = {
name: '@push.rocks/smartstream', name: '@push.rocks/smartstream',
version: '3.0.16', version: '3.0.18',
description: 'simplifies access to node streams' description: 'simplifies access to node streams'
} }

View File

@ -129,12 +129,18 @@ export class SmartDuplex<TInput = any, TOutput = any> extends Duplex {
} }
} }
public notBackpressured = true;
public get backpressured(): boolean {
return !this.notBackpressured;
}
public push(chunkArg?: TOutput | null): boolean { public push(chunkArg?: TOutput | null): boolean {
const result = super.push(chunkArg); const result = super.push(chunkArg);
if (!result && this.handleBackpressure) { if (!result && this.handleBackpressure) {
this.notBackpressured = false;
this.pause(); this.pause();
// Listen for 'drain' event to resume // Listen for 'drain' event to resume
this.once('drain', () => { this.once('drain', () => {
this.notBackpressured = true;
this.resume(); // Resume the source of data this.resume(); // Resume the source of data
}); });
} }
@ -158,12 +164,21 @@ export class SmartDuplex<TInput = any, TOutput = any> extends Duplex {
try { try {
const modifiedChunk = await this.writeFunction(chunk, tools); const modifiedChunk = await this.writeFunction(chunk, tools);
if (modifiedChunk) { if (modifiedChunk) {
this.push(modifiedChunk); this.push(modifiedChunk)
if (this.backpressured && this.handleBackpressure) {
this.once('drain', () => {
callback();
});
} else {
callback();
}
} else {
callback();
} }
callback();
} catch (err) { } catch (err) {
callback(err); callback(err);
} }
return this.notBackpressured;
} }
public async _final(callback: (error?: Error | null) => void) { public async _final(callback: (error?: Error | null) => void) {