fix(core): update
This commit is contained in:
parent
788f2665c2
commit
a9230ca790
@ -5,7 +5,9 @@ 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);
|
||||
const smartStream = SmartDuplex.fromBuffer(bufferData, {
|
||||
handleBackpressure: false,
|
||||
});
|
||||
|
||||
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 testObservable = smartrx.rxjs.of(Buffer.from(observableData));
|
||||
|
||||
const smartStream = SmartDuplex.fromObservable(testObservable);
|
||||
const smartStream = SmartDuplex.fromObservable(testObservable, {
|
||||
handleBackpressure: false,
|
||||
});
|
||||
|
||||
let receivedData = Buffer.alloc(0);
|
||||
|
||||
|
@ -3,6 +3,6 @@
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@push.rocks/smartstream',
|
||||
version: '3.0.15',
|
||||
version: '3.0.16',
|
||||
description: 'simplifies access to node streams'
|
||||
}
|
||||
|
@ -14,7 +14,7 @@ export interface IStreamFinalFunction<rT> {
|
||||
(toolsArg: IStreamTools): Promise<rT>;
|
||||
}
|
||||
|
||||
export interface SmartStreamOptions<TInput, TOutput> extends DuplexOptions {
|
||||
export interface ISmartDuplexOptions<TInput, TOutput> extends DuplexOptions {
|
||||
handleBackpressure?: boolean;
|
||||
readFunction?: () => Promise<void>;
|
||||
writeFunction?: IStreamWriteFunction<TInput, TOutput>;
|
||||
@ -24,23 +24,23 @@ export interface SmartStreamOptions<TInput, TOutput> extends DuplexOptions {
|
||||
|
||||
export class SmartDuplex<TInput = any, TOutput = any> extends Duplex {
|
||||
// STATIC
|
||||
static fromBuffer(buffer: Buffer, options?: DuplexOptions): SmartDuplex {
|
||||
const smartStream = new SmartDuplex(options);
|
||||
static fromBuffer(buffer: Buffer, options?: ISmartDuplexOptions<any, any>): SmartDuplex {
|
||||
const smartDuplex = new SmartDuplex(options);
|
||||
process.nextTick(() => {
|
||||
smartStream.push(buffer);
|
||||
smartStream.push(null); // Signal the end of the data
|
||||
smartDuplex.push(buffer);
|
||||
smartDuplex.push(null); // Signal the end of the data
|
||||
});
|
||||
return smartStream;
|
||||
return smartDuplex;
|
||||
}
|
||||
|
||||
static fromObservable(
|
||||
observable: plugins.smartrx.rxjs.Observable<any>,
|
||||
options?: DuplexOptions
|
||||
options?: ISmartDuplexOptions<any, any>
|
||||
): SmartDuplex {
|
||||
const smartStream = new SmartDuplex(options);
|
||||
smartStream.observableSubscription = observable.subscribe({
|
||||
next: (data) => {
|
||||
if (!smartStream.push(data)) {
|
||||
if (!smartStream.push(data) && smartStream.handleBackpressure) {
|
||||
// Pause the observable if the stream buffer is full
|
||||
smartStream.observableSubscription?.unsubscribe();
|
||||
smartStream.once('drain', () => {
|
||||
@ -115,7 +115,7 @@ export class SmartDuplex<TInput = any, TOutput = any> extends Duplex {
|
||||
private finalFunction?: IStreamFinalFunction<TOutput>;
|
||||
private observableSubscription?: plugins.smartrx.rxjs.Subscription;
|
||||
|
||||
constructor(optionsArg?: SmartStreamOptions<TInput, TOutput>) {
|
||||
constructor(optionsArg?: ISmartDuplexOptions<TInput, TOutput>) {
|
||||
super(optionsArg);
|
||||
this.readFunction = optionsArg?.readFunction;
|
||||
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
|
||||
public async _write(chunk: TInput, encoding: string, callback: (error?: Error | null) => void) {
|
||||
if (!this.writeFunction) {
|
||||
@ -146,13 +158,7 @@ export class SmartDuplex<TInput = any, TOutput = any> extends Duplex {
|
||||
try {
|
||||
const modifiedChunk = await this.writeFunction(chunk, tools);
|
||||
if (modifiedChunk) {
|
||||
if (!this.push(modifiedChunk) && this.handleBackpressure) {
|
||||
this.pause();
|
||||
// Listen for 'drain' event to resume
|
||||
this.once('drain', () => {
|
||||
this.resume(); // Resume the source of data
|
||||
});
|
||||
}
|
||||
this.push(modifiedChunk);
|
||||
}
|
||||
callback();
|
||||
} catch (err) {
|
||||
|
@ -15,7 +15,7 @@ export function createTransformFunction<TInput, TOutput>(
|
||||
const result = await asyncFunction(chunkArg);
|
||||
return result;
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
return smartDuplexStream;
|
||||
}
|
Loading…
Reference in New Issue
Block a user