Compare commits
12 Commits
Author | SHA1 | Date | |
---|---|---|---|
9c30e5bab1 | |||
5f2c5f9380 | |||
f9b8bf33b0 | |||
a55b2548d7 | |||
c8465b82be | |||
b593e3a32c | |||
a490f521ab | |||
59027782dc | |||
8c7dd7970c | |||
22d18dc21f | |||
1cb6f727af | |||
824c44d165 |
41
changelog.md
Normal file
41
changelog.md
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
# Changelog
|
||||||
|
|
||||||
|
## 2024-10-13 - 3.0.46 - fix(WebDuplexStream)
|
||||||
|
Fix errors in WebDuplexStream transformation and test logic
|
||||||
|
|
||||||
|
- Corrected async handling in WebDuplexStream write function
|
||||||
|
- Fixed `WebDuplexStream` tests to properly handle asynchronous reading and writing
|
||||||
|
|
||||||
|
## 2024-10-13 - 3.0.45 - fix(ts)
|
||||||
|
Fixed formatting issues in SmartDuplex class
|
||||||
|
|
||||||
|
- Resolved inconsistent spacing in SmartDuplex class methods and constructor.
|
||||||
|
- Ensured consistent formatting in the getWebStreams method.
|
||||||
|
|
||||||
|
## 2024-06-02 - 3.0.39 - smartduplex
|
||||||
|
Add .getWebStreams method
|
||||||
|
|
||||||
|
- Introduced a new `.getWebStreams` method in the smartduplex module, providing compatibility with the web streams API.
|
||||||
|
|
||||||
|
## 2024-03-16 - 3.0.34 - configuration
|
||||||
|
Update project configuration files
|
||||||
|
|
||||||
|
- Updated `tsconfig` for optimization.
|
||||||
|
- Modified `npmextra.json` to set the `githost` attribute.
|
||||||
|
|
||||||
|
## 2023-11-03 - 3.0.0 to 3.0.8 - core
|
||||||
|
Transition to major version 3.x
|
||||||
|
|
||||||
|
- Implemented breaking changes in the core system for better performance and feature set.
|
||||||
|
- Continuous core updates to improve stability and performance across minor version increments.
|
||||||
|
|
||||||
|
## 2023-11-02 - 2.0.4 to 2.0.8 - core
|
||||||
|
Core updates and a major fix
|
||||||
|
|
||||||
|
- Implemented core updates addressing minor bugs and enhancements.
|
||||||
|
- A significant breaking change update transitioning from 2.0.x to 3.0.0.
|
||||||
|
|
||||||
|
## 2022-03-31 - 2.0.0 - core
|
||||||
|
Major esm transition
|
||||||
|
|
||||||
|
- Implemented a breaking change by switching the core to ESM (ECMAScript Module) format for modernized module handling.
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@push.rocks/smartstream",
|
"name": "@push.rocks/smartstream",
|
||||||
"version": "3.0.40",
|
"version": "3.0.46",
|
||||||
"private": false,
|
"private": false,
|
||||||
"description": "A library to simplify the creation and manipulation of Node.js streams, providing utilities for handling transform, duplex, and readable/writable streams effectively in TypeScript.",
|
"description": "A library to simplify the creation and manipulation of Node.js streams, providing utilities for handling transform, duplex, and readable/writable streams effectively in TypeScript.",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
|
@ -1,37 +1,37 @@
|
|||||||
import { expect, expectAsync, tap } from '@push.rocks/tapbundle';
|
import { expect, tap } from '@push.rocks/tapbundle';
|
||||||
import * as webstream from '../ts_web/index.js';
|
import * as webstream from '../ts_web/index.js';
|
||||||
|
|
||||||
tap.test('WebDuplexStream', async (toolsArg) => {
|
tap.test('WebDuplexStream fromUInt8Array should read back the same Uint8Array', async () => {
|
||||||
const testDone = toolsArg.defer(); // Create a deferred object to control test completion.
|
|
||||||
const inputUint8Array = new Uint8Array([1, 2, 3, 4, 5]);
|
const inputUint8Array = new Uint8Array([1, 2, 3, 4, 5]);
|
||||||
const stream = webstream.WebDuplexStream.fromUInt8Array(inputUint8Array);
|
const stream = webstream.WebDuplexStream.fromUInt8Array(inputUint8Array);
|
||||||
|
|
||||||
const reader = stream.readable.getReader();
|
const reader = stream.readable.getReader();
|
||||||
let readUint8Array = new Uint8Array();
|
let readUint8Array = new Uint8Array();
|
||||||
|
|
||||||
reader.read().then(function processText({ done, value }) {
|
// Read from the stream
|
||||||
if (done) {
|
while (true) {
|
||||||
expect(readUint8Array).toEqual(inputUint8Array);
|
const { value, done } = await reader.read();
|
||||||
testDone.resolve(); // Correctly signal that the test is done.
|
if (done) break;
|
||||||
return;
|
if (value) {
|
||||||
|
// Concatenate value to readUint8Array
|
||||||
|
const tempArray = new Uint8Array(readUint8Array.length + value.length);
|
||||||
|
tempArray.set(readUint8Array, 0);
|
||||||
|
tempArray.set(value, readUint8Array.length);
|
||||||
|
readUint8Array = tempArray;
|
||||||
}
|
}
|
||||||
readUint8Array = new Uint8Array([...readUint8Array, ...value]);
|
}
|
||||||
return reader.read().then(processText);
|
|
||||||
});
|
|
||||||
|
|
||||||
return testDone.promise; // Return the promise to properly wait for the test to complete.
|
expect(readUint8Array).toEqual(inputUint8Array);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
tap.test('WebDuplexStream should handle transform with a write function', async () => {
|
||||||
tap.test('should handle transform with a write function', async (toolsArg) => {
|
|
||||||
const testDone = toolsArg.defer();
|
|
||||||
const input = [1, 2, 3, 4, 5];
|
const input = [1, 2, 3, 4, 5];
|
||||||
const expectedOutput = [2, 4, 6, 8, 10];
|
const expectedOutput = [2, 4, 6, 8, 10];
|
||||||
|
|
||||||
const transformStream = new webstream.WebDuplexStream<number, number>({
|
const transformStream = new webstream.WebDuplexStream<number, number>({
|
||||||
writeFunction: (chunk, { push }) => {
|
writeFunction: async (chunk, { push }) => {
|
||||||
push(chunk * 2); // Push the doubled number into the stream
|
// Push the doubled number into the stream
|
||||||
return Promise.resolve(); // Resolve the promise immediately
|
push(chunk * 2);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -40,31 +40,28 @@ tap.test('should handle transform with a write function', async (toolsArg) => {
|
|||||||
|
|
||||||
const output: number[] = [];
|
const output: number[] = [];
|
||||||
|
|
||||||
// Process the text and resolve the test once done.
|
// Read from the stream asynchronously
|
||||||
const processText = async ({ done, value }) => {
|
const readPromise = (async () => {
|
||||||
if (done) {
|
while (true) {
|
||||||
expect(output).toEqual(expectedOutput);
|
const { value, done } = await readableStream.read();
|
||||||
testDone.resolve(); // Resolve the deferred test once all values have been read.
|
if (done) break;
|
||||||
return;
|
if (value !== undefined) {
|
||||||
|
output.push(value);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (value !== undefined) {
|
})();
|
||||||
output.push(value);
|
|
||||||
}
|
|
||||||
// Continue reading and processing.
|
|
||||||
await readableStream.read().then(processText);
|
|
||||||
};
|
|
||||||
|
|
||||||
// Start the read process before writing to the stream.
|
// Write to the stream
|
||||||
readableStream.read().then(processText);
|
|
||||||
|
|
||||||
// Sequentially write to the stream and close when done.
|
|
||||||
for (const num of input) {
|
for (const num of input) {
|
||||||
await writableStream.write(num);
|
await writableStream.write(num);
|
||||||
}
|
}
|
||||||
await writableStream.close();
|
await writableStream.close();
|
||||||
|
|
||||||
return testDone.promise; // This will wait until the testDone is resolved before completing the test.
|
// Wait for the reading to complete
|
||||||
|
await readPromise;
|
||||||
|
|
||||||
|
// Assert that the output matches the expected transformed data
|
||||||
|
expect(output).toEqual(expectedOutput);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
tap.start();
|
tap.start();
|
@ -1,8 +1,8 @@
|
|||||||
/**
|
/**
|
||||||
* autocreated commitinfo by @pushrocks/commitinfo
|
* autocreated commitinfo by @push.rocks/commitinfo
|
||||||
*/
|
*/
|
||||||
export const commitinfo = {
|
export const commitinfo = {
|
||||||
name: '@push.rocks/smartstream',
|
name: '@push.rocks/smartstream',
|
||||||
version: '3.0.40',
|
version: '3.0.46',
|
||||||
description: 'A library to simplify the creation and manipulation of Node.js streams, providing utilities for handling transform, duplex, and readable/writable streams effectively in TypeScript.'
|
description: 'A library to simplify the creation and manipulation of Node.js streams, providing utilities for handling transform, duplex, and readable/writable streams effectively in TypeScript.'
|
||||||
}
|
}
|
||||||
|
@ -1,3 +1,8 @@
|
|||||||
|
import { stream } from './smartstream.plugins.js';
|
||||||
|
export {
|
||||||
|
stream,
|
||||||
|
}
|
||||||
|
|
||||||
export * from './smartstream.classes.smartduplex.js';
|
export * from './smartstream.classes.smartduplex.js';
|
||||||
export * from './smartstream.classes.streamwrapper.js';
|
export * from './smartstream.classes.streamwrapper.js';
|
||||||
export * from './smartstream.classes.streamintake.js';
|
export * from './smartstream.classes.streamintake.js';
|
||||||
|
@ -53,21 +53,29 @@ export class SmartDuplex<TInput = any, TOutput = any> extends Duplex {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// INSTANCE
|
// INSTANCE
|
||||||
private backpressuredArray: plugins.lik.BackpressuredArray<TOutput>;
|
private backpressuredArray: plugins.lik.BackpressuredArray<TOutput>; // an array that only takes a defined amount of items
|
||||||
public options: ISmartDuplexOptions<TInput, TOutput>;
|
public options: ISmartDuplexOptions<TInput, TOutput>;
|
||||||
private observableSubscription?: plugins.smartrx.rxjs.Subscription;
|
private observableSubscription?: plugins.smartrx.rxjs.Subscription;
|
||||||
private debugLog(messageArg: string) {
|
private debugLog(messageArg: string) {
|
||||||
|
// optional debug log
|
||||||
if (this.options.debug) {
|
if (this.options.debug) {
|
||||||
console.log(messageArg);
|
console.log(messageArg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
constructor(optionsArg?: ISmartDuplexOptions<TInput, TOutput>) {
|
constructor(optionsArg?: ISmartDuplexOptions<TInput, TOutput>) {
|
||||||
super(Object.assign({
|
super(
|
||||||
highWaterMark: 1,
|
Object.assign(
|
||||||
}, optionsArg));
|
{
|
||||||
|
highWaterMark: 1,
|
||||||
|
},
|
||||||
|
optionsArg
|
||||||
|
)
|
||||||
|
);
|
||||||
this.options = optionsArg;
|
this.options = optionsArg;
|
||||||
this.backpressuredArray = new plugins.lik.BackpressuredArray<TOutput>(this.options.highWaterMark || 1)
|
this.backpressuredArray = new plugins.lik.BackpressuredArray<TOutput>(
|
||||||
|
this.options.highWaterMark || 1
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async _read(size: number): Promise<void> {
|
public async _read(size: number): Promise<void> {
|
||||||
@ -78,13 +86,13 @@ export class SmartDuplex<TInput = any, TOutput = any> extends Duplex {
|
|||||||
await this.options.readFunction();
|
await this.options.readFunction();
|
||||||
}
|
}
|
||||||
let canPushMore = true;
|
let canPushMore = true;
|
||||||
while(this.backpressuredArray.data.length > 0 && canPushMore) {
|
while (this.backpressuredArray.data.length > 0 && canPushMore) {
|
||||||
const nextChunk = this.backpressuredArray.shift();
|
const nextChunk = this.backpressuredArray.shift();
|
||||||
canPushMore = this.push(nextChunk);
|
canPushMore = this.push(nextChunk);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async backpressuredPush (pushArg: TOutput) {
|
public async backpressuredPush(pushArg: TOutput) {
|
||||||
const canPushMore = this.backpressuredArray.push(pushArg);
|
const canPushMore = this.backpressuredArray.push(pushArg);
|
||||||
if (!canPushMore) {
|
if (!canPushMore) {
|
||||||
this.debugLog(`${this.options.name}: cannot push more`);
|
this.debugLog(`${this.options.name}: cannot push more`);
|
||||||
@ -92,7 +100,7 @@ export class SmartDuplex<TInput = any, TOutput = any> extends Duplex {
|
|||||||
this.debugLog(`${this.options.name}: can push more again`);
|
this.debugLog(`${this.options.name}: can push more again`);
|
||||||
}
|
}
|
||||||
return canPushMore;
|
return canPushMore;
|
||||||
};
|
}
|
||||||
|
|
||||||
private asyncWritePromiseObjectmap = new plugins.lik.ObjectMap<Promise<any>>();
|
private asyncWritePromiseObjectmap = new plugins.lik.ObjectMap<Promise<any>>();
|
||||||
// Ensure the _write method types the chunk as TInput and encodes TOutput
|
// Ensure the _write method types the chunk as TInput and encodes TOutput
|
||||||
@ -110,7 +118,7 @@ export class SmartDuplex<TInput = any, TOutput = any> extends Duplex {
|
|||||||
},
|
},
|
||||||
push: async (pushArg: TOutput) => {
|
push: async (pushArg: TOutput) => {
|
||||||
return await this.backpressuredPush(pushArg);
|
return await this.backpressuredPush(pushArg);
|
||||||
}
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -158,7 +166,7 @@ export class SmartDuplex<TInput = any, TOutput = any> extends Duplex {
|
|||||||
callback();
|
callback();
|
||||||
}
|
}
|
||||||
|
|
||||||
public getWebStreams(): { readable: ReadableStream, writable: WritableStream } {
|
public async getWebStreams(): Promise<{ readable: ReadableStream; writable: WritableStream }> {
|
||||||
const duplex = this;
|
const duplex = this;
|
||||||
const readable = new ReadableStream({
|
const readable = new ReadableStream({
|
||||||
start(controller) {
|
start(controller) {
|
||||||
@ -175,7 +183,7 @@ export class SmartDuplex<TInput = any, TOutput = any> extends Duplex {
|
|||||||
},
|
},
|
||||||
cancel(reason) {
|
cancel(reason) {
|
||||||
duplex.destroy(new Error(reason));
|
duplex.destroy(new Error(reason));
|
||||||
}
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const writable = new WritableStream({
|
const writable = new WritableStream({
|
||||||
@ -201,7 +209,7 @@ export class SmartDuplex<TInput = any, TOutput = any> extends Duplex {
|
|||||||
},
|
},
|
||||||
abort(reason) {
|
abort(reason) {
|
||||||
duplex.destroy(new Error(reason));
|
duplex.destroy(new Error(reason));
|
||||||
}
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
return { readable, writable };
|
return { readable, writable };
|
||||||
|
@ -1,6 +1,45 @@
|
|||||||
import * as plugins from './smartstream.plugins.js';
|
import * as plugins from './smartstream.plugins.js';
|
||||||
|
|
||||||
export class StreamIntake<T> extends plugins.stream.Readable {
|
export class StreamIntake<T> extends plugins.stream.Readable {
|
||||||
|
// STATIC
|
||||||
|
public static async fromStream<U>(inputStream: plugins.stream.Readable | ReadableStream, options?: plugins.stream.ReadableOptions): Promise<StreamIntake<U>> {
|
||||||
|
const intakeStream = new StreamIntake<U>(options);
|
||||||
|
|
||||||
|
if (inputStream instanceof plugins.stream.Readable) {
|
||||||
|
inputStream.on('data', (chunk: U) => {
|
||||||
|
intakeStream.pushData(chunk);
|
||||||
|
});
|
||||||
|
|
||||||
|
inputStream.on('end', () => {
|
||||||
|
intakeStream.signalEnd();
|
||||||
|
});
|
||||||
|
|
||||||
|
inputStream.on('error', (err: Error) => {
|
||||||
|
intakeStream.destroy(err);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
const reader = (inputStream as ReadableStream).getReader();
|
||||||
|
|
||||||
|
const readChunk = () => {
|
||||||
|
reader.read().then(({ done, value }) => {
|
||||||
|
if (done) {
|
||||||
|
intakeStream.signalEnd();
|
||||||
|
} else {
|
||||||
|
intakeStream.pushData(value);
|
||||||
|
readChunk();
|
||||||
|
}
|
||||||
|
}).catch((err) => {
|
||||||
|
intakeStream.destroy(err);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
readChunk();
|
||||||
|
}
|
||||||
|
|
||||||
|
return intakeStream;
|
||||||
|
}
|
||||||
|
|
||||||
|
// INSTANCE
|
||||||
private signalEndBoolean = false;
|
private signalEndBoolean = false;
|
||||||
private chunkStore: T[] = [];
|
private chunkStore: T[] = [];
|
||||||
public pushNextObservable = new plugins.smartrx.ObservableIntake<any>();
|
public pushNextObservable = new plugins.smartrx.ObservableIntake<any>();
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
/**
|
/**
|
||||||
* autocreated commitinfo by @pushrocks/commitinfo
|
* autocreated commitinfo by @push.rocks/commitinfo
|
||||||
*/
|
*/
|
||||||
export const commitinfo = {
|
export const commitinfo = {
|
||||||
name: '@push.rocks/smartstream',
|
name: '@push.rocks/smartstream',
|
||||||
version: '3.0.40',
|
version: '3.0.46',
|
||||||
description: 'A library to simplify the creation and manipulation of Node.js streams, providing utilities for handling transform, duplex, and readable/writable streams effectively in TypeScript.'
|
description: 'A library to simplify the creation and manipulation of Node.js streams, providing utilities for handling transform, duplex, and readable/writable streams effectively in TypeScript.'
|
||||||
}
|
}
|
||||||
|
@ -63,6 +63,7 @@ export class WebDuplexStream<TInput = any, TOutput = any> extends TransformStrea
|
|||||||
options: WebDuplexStreamOptions<TInput, TOutput>;
|
options: WebDuplexStreamOptions<TInput, TOutput>;
|
||||||
|
|
||||||
constructor(optionsArg: WebDuplexStreamOptions<TInput, TOutput>) {
|
constructor(optionsArg: WebDuplexStreamOptions<TInput, TOutput>) {
|
||||||
|
// here we call into the official web stream api
|
||||||
super({
|
super({
|
||||||
async transform(chunk, controller) {
|
async transform(chunk, controller) {
|
||||||
// Transformation logic remains unchanged
|
// Transformation logic remains unchanged
|
||||||
@ -72,15 +73,14 @@ export class WebDuplexStream<TInput = any, TOutput = any> extends TransformStrea
|
|||||||
push: (pushArg: TOutput) => controller.enqueue(pushArg),
|
push: (pushArg: TOutput) => controller.enqueue(pushArg),
|
||||||
};
|
};
|
||||||
|
|
||||||
optionsArg.writeFunction(chunk, tools)
|
try {
|
||||||
.then(writeReturnChunk => {
|
const writeReturnChunk = await optionsArg.writeFunction(chunk, tools);
|
||||||
// the write return chunk is optional
|
if (writeReturnChunk) { // return chunk is optional
|
||||||
// just in case the write function returns something other than void.
|
controller.enqueue(writeReturnChunk);
|
||||||
if (writeReturnChunk) {
|
}
|
||||||
controller.enqueue(writeReturnChunk);
|
} catch (err) {
|
||||||
}
|
controller.error(err);
|
||||||
})
|
}
|
||||||
.catch(err => controller.error(err));
|
|
||||||
} else {
|
} else {
|
||||||
controller.error(new Error('No write function provided'));
|
controller.error(new Error('No write function provided'));
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user