|
|
|
@@ -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) {
|
|
|
|
|