Compare commits

...

8 Commits

Author SHA1 Message Date
ea66d1b2fb 3.0.0 2023-11-03 13:55:56 +01:00
c37f62abec BREAKING CHANGE(core): update 2023-11-03 13:55:56 +01:00
2c904cc1ec 2.0.8 2023-11-02 00:30:16 +01:00
d1561ad1b7 fix(core): update 2023-11-02 00:30:15 +01:00
0ae3fee987 2.0.7 2023-11-01 14:18:35 +01:00
047c2bd402 fix(core): update 2023-11-01 14:18:34 +01:00
9ed3de718f 2.0.6 2023-11-01 14:17:39 +01:00
14530f393c fix(core): update 2023-11-01 14:17:39 +01:00
7 changed files with 118 additions and 57 deletions

View File

@ -1,6 +1,6 @@
{
"name": "@push.rocks/smartstream",
"version": "2.0.5",
"version": "3.0.0",
"private": false,
"description": "simplifies access to node streams",
"main": "dist_ts/index.js",

View File

@ -33,9 +33,7 @@ tap.test('should handle a read stream', async (tools) => {
tap.test('should create a valid Intake', async (tools) => {
testIntake = new smartstream.StreamIntake<string>();
testIntake
.getReadable()
.pipe(
testIntake.pipe(
smartstream.createDuplexStream<string, string>(
async (chunkString) => {
await tools.delayFor(100);

View File

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

View File

@ -1,3 +1,4 @@
export * from './smartstream.classes.passthrough.js';
export * from './smartstream.classes.smartstream.js';
export * from './smartstream.classes.streamwrapper.js';
export * from './smartstream.classes.streamintake.js';

View File

@ -0,0 +1,19 @@
import * as plugins from './smartstream.plugins.js';
export class PassThrough extends plugins.stream.Duplex {
constructor(options?: plugins.stream.DuplexOptions) {
super(options);
}
_read(size: number): void {
// No-op: Data written will be automatically available for reading.
}
_write(chunk: any, encoding: BufferEncoding, callback: (error?: Error | null) => void): void {
if (this.push(chunk, encoding)) {
callback();
} else {
this.once('drain', callback);
}
}
}

View File

@ -1,20 +1,35 @@
import * as plugins from './smartstream.plugins.js';
import { Duplex, type DuplexOptions } from 'stream';
export interface SmartStreamOptions<TInput, TOutput> extends DuplexOptions {
// You can add more custom options relevant to TInput and TOutput if necessary
}
export class SmartStream extends Duplex {
export class SmartStream<TInput = any, TOutput = any> extends Duplex {
private observableSubscription?: plugins.smartrx.rxjs.Subscription;
private asyncChunkModifier?: (chunk: TInput) => Promise<TOutput>;
constructor(options?: DuplexOptions) {
constructor(options?: SmartStreamOptions<TInput, TOutput>, asyncChunkModifierArg?: (chunk: TInput) => Promise<TOutput>) {
super(options);
this.asyncChunkModifier = asyncChunkModifierArg;
}
_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();
// Ensure the _write method types the chunk as TInput and encodes TOutput
public async _write(chunk: TInput, encoding: string, callback: (error?: Error | null) => void) {
try {
if (this.asyncChunkModifier) {
const modifiedChunk = await this.asyncChunkModifier(chunk);
if (!this.push(modifiedChunk)) {
// Handle backpressure here if necessary
}
} else {
if (!this.push(chunk as unknown as TOutput)) {
// Handle backpressure here if necessary
}
}
callback();
} catch (err) {
callback(err);
}
}
static fromBuffer(buffer: Buffer, options?: DuplexOptions): SmartStream {
@ -52,4 +67,46 @@ export class SmartStream extends Duplex {
return smartStream;
}
static fromReplaySubject(replaySubject: plugins.smartrx.rxjs.ReplaySubject<any>, options?: DuplexOptions): SmartStream {
const smartStream = new SmartStream(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;
}
}

View File

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