fix(core): update

This commit is contained in:
Philipp Kunz 2023-11-11 20:30:42 +01:00
parent 788f2665c2
commit a9230ca790
4 changed files with 30 additions and 20 deletions

View File

@ -5,7 +5,9 @@ import * as fs from 'fs';
tap.test('should create a SmartStream from a Buffer', async () => { tap.test('should create a SmartStream from a Buffer', async () => {
const bufferData = Buffer.from('This is a test buffer'); const bufferData = Buffer.from('This is a test buffer');
const smartStream = SmartDuplex.fromBuffer(bufferData); const smartStream = SmartDuplex.fromBuffer(bufferData, {
handleBackpressure: false,
});
let receivedData = Buffer.alloc(0); let receivedData = Buffer.alloc(0);
@ -25,7 +27,9 @@ tap.test('should create a SmartStream from an Observable', async () => {
const observableData = 'Observable test data'; const observableData = 'Observable test data';
const testObservable = smartrx.rxjs.of(Buffer.from(observableData)); const testObservable = smartrx.rxjs.of(Buffer.from(observableData));
const smartStream = SmartDuplex.fromObservable(testObservable); const smartStream = SmartDuplex.fromObservable(testObservable, {
handleBackpressure: false,
});
let receivedData = Buffer.alloc(0); let receivedData = Buffer.alloc(0);

View File

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

View File

@ -14,7 +14,7 @@ export interface IStreamFinalFunction<rT> {
(toolsArg: IStreamTools): Promise<rT>; (toolsArg: IStreamTools): Promise<rT>;
} }
export interface SmartStreamOptions<TInput, TOutput> extends DuplexOptions { export interface ISmartDuplexOptions<TInput, TOutput> extends DuplexOptions {
handleBackpressure?: boolean; handleBackpressure?: boolean;
readFunction?: () => Promise<void>; readFunction?: () => Promise<void>;
writeFunction?: IStreamWriteFunction<TInput, TOutput>; writeFunction?: IStreamWriteFunction<TInput, TOutput>;
@ -24,23 +24,23 @@ export interface SmartStreamOptions<TInput, TOutput> extends DuplexOptions {
export class SmartDuplex<TInput = any, TOutput = any> extends Duplex { export class SmartDuplex<TInput = any, TOutput = any> extends Duplex {
// STATIC // STATIC
static fromBuffer(buffer: Buffer, options?: DuplexOptions): SmartDuplex { static fromBuffer(buffer: Buffer, options?: ISmartDuplexOptions<any, any>): SmartDuplex {
const smartStream = new SmartDuplex(options); const smartDuplex = new SmartDuplex(options);
process.nextTick(() => { process.nextTick(() => {
smartStream.push(buffer); smartDuplex.push(buffer);
smartStream.push(null); // Signal the end of the data smartDuplex.push(null); // Signal the end of the data
}); });
return smartStream; return smartDuplex;
} }
static fromObservable( static fromObservable(
observable: plugins.smartrx.rxjs.Observable<any>, observable: plugins.smartrx.rxjs.Observable<any>,
options?: DuplexOptions options?: ISmartDuplexOptions<any, any>
): SmartDuplex { ): SmartDuplex {
const smartStream = new SmartDuplex(options); const smartStream = new SmartDuplex(options);
smartStream.observableSubscription = observable.subscribe({ smartStream.observableSubscription = observable.subscribe({
next: (data) => { next: (data) => {
if (!smartStream.push(data)) { if (!smartStream.push(data) && smartStream.handleBackpressure) {
// Pause the observable if the stream buffer is full // Pause the observable if the stream buffer is full
smartStream.observableSubscription?.unsubscribe(); smartStream.observableSubscription?.unsubscribe();
smartStream.once('drain', () => { smartStream.once('drain', () => {
@ -115,7 +115,7 @@ export class SmartDuplex<TInput = any, TOutput = any> extends Duplex {
private finalFunction?: IStreamFinalFunction<TOutput>; private finalFunction?: IStreamFinalFunction<TOutput>;
private observableSubscription?: plugins.smartrx.rxjs.Subscription; private observableSubscription?: plugins.smartrx.rxjs.Subscription;
constructor(optionsArg?: SmartStreamOptions<TInput, TOutput>) { constructor(optionsArg?: ISmartDuplexOptions<TInput, TOutput>) {
super(optionsArg); super(optionsArg);
this.readFunction = optionsArg?.readFunction; this.readFunction = optionsArg?.readFunction;
this.writeFunction = optionsArg?.writeFunction; this.writeFunction = optionsArg?.writeFunction;
@ -129,6 +129,18 @@ export class SmartDuplex<TInput = any, TOutput = any> extends Duplex {
} }
} }
public push(chunkArg?: TOutput | null): boolean {
const result = super.push(chunkArg);
if (!result && this.handleBackpressure) {
this.pause();
// Listen for 'drain' event to resume
this.once('drain', () => {
this.resume(); // Resume the source of data
});
}
return result;
}
// Ensure the _write method types the chunk as TInput and encodes TOutput // Ensure the _write method types the chunk as TInput and encodes TOutput
public async _write(chunk: TInput, encoding: string, callback: (error?: Error | null) => void) { public async _write(chunk: TInput, encoding: string, callback: (error?: Error | null) => void) {
if (!this.writeFunction) { if (!this.writeFunction) {
@ -146,13 +158,7 @@ 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) {
if (!this.push(modifiedChunk) && this.handleBackpressure) { this.push(modifiedChunk);
this.pause();
// Listen for 'drain' event to resume
this.once('drain', () => {
this.resume(); // Resume the source of data
});
}
} }
callback(); callback();
} catch (err) { } catch (err) {

View File

@ -15,7 +15,7 @@ export function createTransformFunction<TInput, TOutput>(
const result = await asyncFunction(chunkArg); const result = await asyncFunction(chunkArg);
return result; return result;
} }
}) });
return smartDuplexStream; return smartDuplexStream;
} }