Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
5277083097 | |||
8618ac55ef | |||
ea66d1b2fb | |||
c37f62abec |
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@push.rocks/smartstream",
|
||||
"version": "2.0.8",
|
||||
"version": "3.0.1",
|
||||
"private": false,
|
||||
"description": "simplifies access to node streams",
|
||||
"main": "dist_ts/index.js",
|
||||
|
@ -48,4 +48,3 @@ hi+wow
|
||||
hi+wow
|
||||
hi+wow
|
||||
hi+wow
|
||||
noice
|
@ -1,11 +1,11 @@
|
||||
import { expect, tap } from '@push.rocks/tapbundle';
|
||||
import { SmartStream } from '../ts/smartstream.classes.smartstream.js'; // Adjust the import to your file structure
|
||||
import { SmartDuplex } from '../ts/smartstream.classes.smartduplex.js'; // Adjust the import to your file structure
|
||||
import * as smartrx from '@push.rocks/smartrx';
|
||||
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 = SmartStream.fromBuffer(bufferData);
|
||||
const smartStream = SmartDuplex.fromBuffer(bufferData);
|
||||
|
||||
let receivedData = Buffer.alloc(0);
|
||||
|
||||
@ -25,7 +25,7 @@ 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 = SmartStream.fromObservable(testObservable);
|
||||
const smartStream = SmartDuplex.fromObservable(testObservable);
|
||||
|
||||
let receivedData = Buffer.alloc(0);
|
||||
|
||||
|
@ -7,45 +7,43 @@ let testIntake: smartstream.StreamIntake<string>;
|
||||
|
||||
tap.test('should handle a read stream', async (tools) => {
|
||||
const counter = 0;
|
||||
const testSmartstream = new smartstream.StreamWrapper([
|
||||
const streamWrapper = new smartstream.StreamWrapper([
|
||||
smartfile.fsStream.createReadStream('./test/assets/readabletext.txt'),
|
||||
smartstream.createDuplexStream<Buffer, Buffer>(
|
||||
async (chunkStringArg: Buffer, streamTools) => {
|
||||
new smartstream.SmartDuplex({
|
||||
writeAndTransformFunction: async (chunkStringArg: Buffer, streamTools) => {
|
||||
// do something with the stream here
|
||||
const result = chunkStringArg.toString().substr(0, 100);
|
||||
streamTools.pipeMore('wow =========== \n');
|
||||
streamTools.push('wow =========== \n');
|
||||
return Buffer.from(result);
|
||||
},
|
||||
async (tools) => {
|
||||
// tools.pipeMore('hey, this is the end')
|
||||
streamEndFunction: async (tools) => {
|
||||
return Buffer.from('this is the end');
|
||||
},
|
||||
{ objectMode: false }
|
||||
),
|
||||
smartstream.createDuplexStream<Buffer, string>(async (chunkStringArg) => {
|
||||
console.log(chunkStringArg.toString());
|
||||
return null;
|
||||
}),
|
||||
new smartstream.SmartDuplex({
|
||||
writeAndTransformFunction: async (chunkStringArg) => {
|
||||
console.log(chunkStringArg.toString());
|
||||
},
|
||||
streamEndFunction: async (tools) => {
|
||||
tools.push(null);
|
||||
},
|
||||
}),
|
||||
smartstream.cleanPipe(),
|
||||
]);
|
||||
await testSmartstream.run();
|
||||
// await streamWrapper.run();
|
||||
});
|
||||
|
||||
tap.test('should create a valid Intake', async (tools) => {
|
||||
testIntake = new smartstream.StreamIntake<string>();
|
||||
testIntake
|
||||
.getReadable()
|
||||
.pipe(
|
||||
smartstream.createDuplexStream<string, string>(
|
||||
async (chunkString) => {
|
||||
testIntake.pipe(
|
||||
new smartstream.SmartDuplex({
|
||||
objectMode: true,
|
||||
writeAndTransformFunction: async (chunkStringArg: string, streamTools) => {
|
||||
await tools.delayFor(100);
|
||||
console.log(chunkString);
|
||||
return chunkString;
|
||||
},
|
||||
async () => {
|
||||
return 'noice';
|
||||
console.log(chunkStringArg);
|
||||
return chunkStringArg;
|
||||
}
|
||||
)
|
||||
})
|
||||
)
|
||||
.pipe(smartfile.fsStream.createWriteStream('./test/assets/writabletext.txt'));
|
||||
const testFinished = tools.defer();
|
||||
|
@ -3,6 +3,6 @@
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@push.rocks/smartstream',
|
||||
version: '2.0.8',
|
||||
version: '3.0.1',
|
||||
description: 'simplifies access to node streams'
|
||||
}
|
||||
|
@ -1,5 +1,4 @@
|
||||
export * from './smartstream.classes.passthrough.js';
|
||||
export * from './smartstream.classes.smartstream.js';
|
||||
export * from './smartstream.classes.smartduplex.js';
|
||||
export * from './smartstream.classes.streamwrapper.js';
|
||||
export * from './smartstream.classes.streamintake.js';
|
||||
export * from './smartstream.duplex.js';
|
177
ts/smartstream.classes.smartduplex.ts
Normal file
177
ts/smartstream.classes.smartduplex.ts
Normal file
@ -0,0 +1,177 @@
|
||||
import * as plugins from './smartstream.plugins.js';
|
||||
import { Duplex, type DuplexOptions } from 'stream';
|
||||
|
||||
export interface IStreamTools {
|
||||
truncate: () => void;
|
||||
push: (pipeObject: any) => void;
|
||||
}
|
||||
|
||||
export interface IWriteAndTransformFunction<T, rT> {
|
||||
(chunkArg: T, toolsArg: IStreamTools): Promise<rT>;
|
||||
}
|
||||
|
||||
export interface IStreamEndFunction<rT> {
|
||||
(toolsArg: IStreamTools): Promise<rT>;
|
||||
}
|
||||
|
||||
export interface SmartStreamOptions<TInput, TOutput> extends DuplexOptions {
|
||||
readFunction?: () => Promise<void>;
|
||||
writeAndTransformFunction?: IWriteAndTransformFunction<TInput, TOutput>;
|
||||
streamEndFunction?: IStreamEndFunction<TOutput>;
|
||||
// Add other custom options if necessary
|
||||
}
|
||||
|
||||
export class SmartDuplex<TInput = any, TOutput = any> extends Duplex {
|
||||
// STATIC
|
||||
static fromBuffer(buffer: Buffer, options?: DuplexOptions): SmartDuplex {
|
||||
const smartStream = new SmartDuplex(options);
|
||||
process.nextTick(() => {
|
||||
smartStream.push(buffer);
|
||||
smartStream.push(null); // Signal the end of the data
|
||||
});
|
||||
return smartStream;
|
||||
}
|
||||
|
||||
static fromObservable(
|
||||
observable: plugins.smartrx.rxjs.Observable<any>,
|
||||
options?: DuplexOptions
|
||||
): SmartDuplex {
|
||||
const smartStream = new SmartDuplex(options);
|
||||
smartStream.observableSubscription = observable.subscribe({
|
||||
next: (data) => {
|
||||
if (!smartStream.push(data)) {
|
||||
// Pause the observable if the stream buffer is full
|
||||
smartStream.observableSubscription?.unsubscribe();
|
||||
smartStream.once('drain', () => {
|
||||
// Resume the observable when the stream buffer is drained
|
||||
smartStream.observableSubscription?.unsubscribe();
|
||||
smartStream.observableSubscription = observable.subscribe((data) => {
|
||||
smartStream.push(data);
|
||||
});
|
||||
});
|
||||
}
|
||||
},
|
||||
error: (err) => {
|
||||
smartStream.emit('error', err);
|
||||
},
|
||||
complete: () => {
|
||||
smartStream.push(null); // Signal the end of the data
|
||||
},
|
||||
});
|
||||
|
||||
return smartStream;
|
||||
}
|
||||
|
||||
static fromReplaySubject(
|
||||
replaySubject: plugins.smartrx.rxjs.ReplaySubject<any>,
|
||||
options?: DuplexOptions
|
||||
): SmartDuplex {
|
||||
const smartStream = new SmartDuplex(options);
|
||||
let isBackpressured = false;
|
||||
|
||||
// Subscribe to the ReplaySubject
|
||||
const subscription = replaySubject.subscribe({
|
||||
next: (data) => {
|
||||
const canPush = smartStream.push(data);
|
||||
if (!canPush) {
|
||||
// If push returns false, pause the subscription because of backpressure
|
||||
isBackpressured = true;
|
||||
subscription.unsubscribe();
|
||||
}
|
||||
},
|
||||
error: (err) => {
|
||||
smartStream.emit('error', err);
|
||||
},
|
||||
complete: () => {
|
||||
smartStream.push(null); // End the stream when the ReplaySubject completes
|
||||
},
|
||||
});
|
||||
|
||||
// Listen for 'drain' event to resume the subscription if it was paused
|
||||
smartStream.on('drain', () => {
|
||||
if (isBackpressured) {
|
||||
isBackpressured = false;
|
||||
// Resubscribe to the ReplaySubject since we previously paused
|
||||
smartStream.observableSubscription = replaySubject.subscribe({
|
||||
next: (data) => {
|
||||
if (!smartStream.push(data)) {
|
||||
smartStream.observableSubscription?.unsubscribe();
|
||||
isBackpressured = true;
|
||||
}
|
||||
},
|
||||
// No need to repeat error and complete handling here because it's already set up above
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
return smartStream;
|
||||
}
|
||||
|
||||
// INSTANCE
|
||||
private readFunction?: () => Promise<void>;
|
||||
private writeAndTransformFunction?: IWriteAndTransformFunction<TInput, TOutput>;
|
||||
private streamEndFunction?: IStreamEndFunction<TOutput>;
|
||||
private observableSubscription?: plugins.smartrx.rxjs.Subscription;
|
||||
|
||||
constructor(optionsArg?: SmartStreamOptions<TInput, TOutput>) {
|
||||
super(optionsArg);
|
||||
this.readFunction = optionsArg?.readFunction;
|
||||
this.writeAndTransformFunction = optionsArg?.writeAndTransformFunction;
|
||||
this.streamEndFunction = optionsArg?.streamEndFunction;
|
||||
}
|
||||
|
||||
public async _read(size: number): Promise<void> {
|
||||
if (this.readFunction) {
|
||||
await this.readFunction();
|
||||
}
|
||||
}
|
||||
|
||||
// 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.writeAndTransformFunction) {
|
||||
return callback(new Error('No stream function provided'));
|
||||
}
|
||||
|
||||
const tools: IStreamTools = {
|
||||
truncate: () => {
|
||||
this.push(null);
|
||||
callback();
|
||||
},
|
||||
push: (pushArg: TOutput) => this.push(pushArg),
|
||||
};
|
||||
|
||||
try {
|
||||
const modifiedChunk = await this.writeAndTransformFunction(chunk, tools);
|
||||
if (modifiedChunk) {
|
||||
if (!this.push(modifiedChunk)) {
|
||||
// Handle backpressure if necessary
|
||||
}
|
||||
}
|
||||
callback();
|
||||
} catch (err) {
|
||||
callback(err);
|
||||
}
|
||||
}
|
||||
|
||||
public async _final(callback: (error?: Error | null) => void) {
|
||||
if (this.streamEndFunction) {
|
||||
const tools: IStreamTools = {
|
||||
truncate: () => callback(),
|
||||
push: (pipeObject) => this.push(pipeObject),
|
||||
};
|
||||
|
||||
try {
|
||||
const finalChunk = await this.streamEndFunction(tools);
|
||||
if (finalChunk) {
|
||||
this.push(finalChunk);
|
||||
}
|
||||
callback();
|
||||
} catch (err) {
|
||||
callback(err);
|
||||
}
|
||||
} else {
|
||||
this.push(null),
|
||||
callback();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,55 +0,0 @@
|
||||
import * as plugins from './smartstream.plugins.js';
|
||||
import { Duplex, type DuplexOptions } from 'stream';
|
||||
|
||||
export class SmartStream extends Duplex {
|
||||
private observableSubscription?: plugins.smartrx.rxjs.Subscription;
|
||||
|
||||
constructor(options?: DuplexOptions) {
|
||||
super(options);
|
||||
}
|
||||
|
||||
_read(size: number) {
|
||||
// Implement if you need custom behavior, otherwise leave it empty
|
||||
}
|
||||
|
||||
_write(chunk: any, encoding: string, callback: (error?: Error | null) => void) {
|
||||
// Implement if you need custom behavior
|
||||
callback();
|
||||
}
|
||||
|
||||
static fromBuffer(buffer: Buffer, options?: DuplexOptions): SmartStream {
|
||||
const smartStream = new SmartStream(options);
|
||||
process.nextTick(() => {
|
||||
smartStream.push(buffer);
|
||||
smartStream.push(null); // Signal the end of the data
|
||||
});
|
||||
return smartStream;
|
||||
}
|
||||
|
||||
static fromObservable(observable: plugins.smartrx.rxjs.Observable<any>, options?: DuplexOptions): SmartStream {
|
||||
const smartStream = new SmartStream(options);
|
||||
smartStream.observableSubscription = observable.subscribe({
|
||||
next: (data) => {
|
||||
if (!smartStream.push(data)) {
|
||||
// Pause the observable if the stream buffer is full
|
||||
smartStream.observableSubscription?.unsubscribe();
|
||||
smartStream.once('drain', () => {
|
||||
// Resume the observable when the stream buffer is drained
|
||||
smartStream.observableSubscription?.unsubscribe();
|
||||
smartStream.observableSubscription = observable.subscribe(data => {
|
||||
smartStream.push(data);
|
||||
});
|
||||
});
|
||||
}
|
||||
},
|
||||
error: (err) => {
|
||||
smartStream.emit('error', err);
|
||||
},
|
||||
complete: () => {
|
||||
smartStream.push(null); // Signal the end of the data
|
||||
}
|
||||
});
|
||||
|
||||
return smartStream;
|
||||
}
|
||||
}
|
@ -1,56 +1,42 @@
|
||||
import * as plugins from './smartstream.plugins.js';
|
||||
|
||||
export class StreamIntake<T> {
|
||||
export class StreamIntake<T> extends plugins.stream.Readable {
|
||||
private signalEndBoolean = false;
|
||||
private chunkStore: T[] = [];
|
||||
|
||||
public pushNextObservable = new plugins.smartrx.ObservableIntake<any>();
|
||||
|
||||
private pushedNextDeferred = plugins.smartpromise.defer();
|
||||
|
||||
private readableStream = plugins.from2.obj(async (size, next) => {
|
||||
constructor(options?: plugins.stream.ReadableOptions) {
|
||||
super({ ...options, objectMode: true }); // Ensure that we are in object mode.
|
||||
this.pushNextObservable.push('please push next');
|
||||
}
|
||||
|
||||
_read(size: number): void {
|
||||
// console.log('get next');
|
||||
// execute without backpressure
|
||||
while (this.chunkStore.length > 0) {
|
||||
next(null, this.chunkStore.shift());
|
||||
}
|
||||
if (this.signalEndBoolean) {
|
||||
next(null, null);
|
||||
}
|
||||
const pushChunk = (): void => {
|
||||
if (this.chunkStore.length > 0) {
|
||||
// If push returns false, then we should stop reading
|
||||
if (!this.push(this.chunkStore.shift())) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// lets trigger backpressure handling
|
||||
this.pushNextObservable.push('please push next');
|
||||
await this.pushedNextDeferred.promise;
|
||||
this.pushedNextDeferred = plugins.smartpromise.defer();
|
||||
if (this.chunkStore.length === 0) {
|
||||
if (this.signalEndBoolean) {
|
||||
// If we're done, push null to signal the end of the stream
|
||||
this.push(null);
|
||||
} else {
|
||||
// Ask for more data and wait
|
||||
this.pushNextObservable.push('please push next');
|
||||
this.pushedNextDeferred.promise.then(() => {
|
||||
this.pushedNextDeferred = plugins.smartpromise.defer(); // Reset the deferred
|
||||
pushChunk(); // Try pushing the next chunk
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// execute with backpressure
|
||||
while (this.chunkStore.length > 0) {
|
||||
next(null, this.chunkStore.shift());
|
||||
}
|
||||
if (this.signalEndBoolean) {
|
||||
next(null, null);
|
||||
}
|
||||
});
|
||||
|
||||
constructor() {
|
||||
this.pushNextObservable.push('please push next');
|
||||
}
|
||||
|
||||
/**
|
||||
* returns a new style readble stream
|
||||
*/
|
||||
public getReadable() {
|
||||
const readable = new plugins.stream.Readable({
|
||||
objectMode: true,
|
||||
});
|
||||
return readable.wrap(this.readableStream);
|
||||
}
|
||||
|
||||
/**
|
||||
* returns an oldstyle readble stream
|
||||
*/
|
||||
public getReadableStream() {
|
||||
return this.readableStream;
|
||||
pushChunk();
|
||||
}
|
||||
|
||||
public pushData(chunkData: T) {
|
||||
|
@ -1,83 +0,0 @@
|
||||
import * as plugins from './smartstream.plugins.js';
|
||||
|
||||
export interface ITruncateFunc {
|
||||
(): void;
|
||||
}
|
||||
|
||||
export interface IPipeMoreFunc {
|
||||
(pipeObject: any): void;
|
||||
}
|
||||
|
||||
export interface IStreamTools {
|
||||
truncate: ITruncateFunc;
|
||||
pipeMore: IPipeMoreFunc;
|
||||
}
|
||||
|
||||
export interface IStreamFunction<T, rT> {
|
||||
(chunkArg: T, toolsArg: IStreamTools): Promise<rT>;
|
||||
}
|
||||
|
||||
export interface IStreamEndFunction<rT> {
|
||||
(toolsArg: IStreamTools): Promise<rT>;
|
||||
}
|
||||
|
||||
export interface IStreamOptions {
|
||||
objectMode?: boolean;
|
||||
readableObjectMode?: boolean;
|
||||
writableObjectMode?: boolean;
|
||||
}
|
||||
|
||||
export let createDuplexStream = <T, rT>(
|
||||
funcArg: IStreamFunction<T, rT>,
|
||||
endFuncArg?: IStreamEndFunction<rT>,
|
||||
optionsArg: IStreamOptions = {
|
||||
objectMode: false,
|
||||
readableObjectMode: true,
|
||||
writableObjectMode: true,
|
||||
}
|
||||
) => {
|
||||
return plugins.through2(
|
||||
optionsArg,
|
||||
function (chunk, enc, cb) {
|
||||
let truncated = false;
|
||||
const tools: IStreamTools = {
|
||||
truncate: () => {
|
||||
truncated = true;
|
||||
cb(null, null);
|
||||
},
|
||||
pipeMore: (pipeObject) => {
|
||||
this.push(pipeObject);
|
||||
},
|
||||
};
|
||||
const asyncWrapper = async () => {
|
||||
const resultChunk: rT = await funcArg(chunk, tools);
|
||||
if (!truncated) {
|
||||
cb(null, resultChunk);
|
||||
}
|
||||
};
|
||||
asyncWrapper().catch((err) => {
|
||||
console.log(err);
|
||||
});
|
||||
},
|
||||
function (cb) {
|
||||
const tools: IStreamTools = {
|
||||
truncate: () => {
|
||||
cb();
|
||||
},
|
||||
pipeMore: (pushArg) => {
|
||||
this.push(pushArg);
|
||||
},
|
||||
};
|
||||
const asyncWrapper = async () => {
|
||||
if (endFuncArg) {
|
||||
const result = await endFuncArg(tools);
|
||||
this.push(result);
|
||||
}
|
||||
cb();
|
||||
};
|
||||
asyncWrapper().catch((err) => {
|
||||
console.log(err);
|
||||
});
|
||||
}
|
||||
);
|
||||
};
|
Reference in New Issue
Block a user