Compare commits

...

6 Commits

7 changed files with 55 additions and 12 deletions

View File

@ -1,5 +1,23 @@
# Changelog # Changelog
## 2024-10-16 - 3.2.1 - fix(core)
Fix the order of operations in SmartDuplex _read method to ensure proper waiting for items.
- Adjusted the order of reading function execution and waiting for items in the SmartDuplex _read method.
- Fixed potential issues with stream data processing timing.
## 2024-10-16 - 3.2.0 - feat(SmartDuplex)
Added method to create SmartDuplex from a WebReadableStream.
- Implemented a static method in SmartDuplex to allow creating an instance from a WebReadableStream.
- This addition enhances the capability of SmartDuplex to integrate with web streams, facilitating seamless stream manipulation across environments.
## 2024-10-14 - 3.1.2 - fix(WebDuplexStream)
Fix variable naming inconsistency in WebDuplexStream test
- Changed variable names from 'transformStream' to 'webDuplexStream' for consistency.
- Renamed 'writableStream' and 'readableStream' to 'writer' and 'reader' respectively.
## 2024-10-13 - 3.1.1 - fix(WebDuplexStream) ## 2024-10-13 - 3.1.1 - fix(WebDuplexStream)
Improved read/write interface and error handling in WebDuplexStream Improved read/write interface and error handling in WebDuplexStream

View File

@ -1,6 +1,6 @@
{ {
"name": "@push.rocks/smartstream", "name": "@push.rocks/smartstream",
"version": "3.1.1", "version": "3.2.1",
"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",

View File

@ -28,22 +28,22 @@ tap.test('WebDuplexStream should handle transform with a write function', async
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 webDuplexStream = new webstream.WebDuplexStream<number, number>({
writeFunction: async (chunk, { push }) => { writeFunction: async (chunk, { push }) => {
// Push the doubled number into the stream // Push the doubled number into the stream
push(chunk * 2); push(chunk * 2);
}, },
}); });
const writableStream = transformStream.writable.getWriter(); const writer = webDuplexStream.writable.getWriter();
const readableStream = transformStream.readable.getReader(); const reader = webDuplexStream.readable.getReader();
const output: number[] = []; const output: number[] = [];
// Read from the stream asynchronously // Read from the stream asynchronously
const readPromise = (async () => { const readPromise = (async () => {
while (true) { while (true) {
const { value, done } = await readableStream.read(); const { value, done } = await reader.read();
if (done) break; if (done) break;
if (value !== undefined) { if (value !== undefined) {
output.push(value); output.push(value);
@ -53,9 +53,9 @@ tap.test('WebDuplexStream should handle transform with a write function', async
// Write to the stream // Write to the stream
for (const num of input) { for (const num of input) {
await writableStream.write(num); await writer.write(num);
} }
await writableStream.close(); await writer.close();
// Wait for the reading to complete // Wait for the reading to complete
await readPromise; await readPromise;

View File

@ -3,6 +3,6 @@
*/ */
export const commitinfo = { export const commitinfo = {
name: '@push.rocks/smartstream', name: '@push.rocks/smartstream',
version: '3.1.1', version: '3.2.1',
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.'
} }

View File

@ -52,6 +52,31 @@ export class SmartDuplex<TInput = any, TOutput = any> extends Duplex {
return smartDuplex; return smartDuplex;
} }
public static fromWebReadableStream<T = any>(
readableStream: ReadableStream<T>
): SmartDuplex<T, T> {
const smartDuplex = new SmartDuplex<T, T>({
readFunction: async () => {
const reader = readableStream.getReader();
try {
while (true) {
const { value, done } = await reader.read();
if (value !== undefined) {
smartDuplex.push(value);
}
if (done) {
smartDuplex.end();
break;
}
}
} finally {
reader.releaseLock();
}
},
});
return smartDuplex;
}
// INSTANCE // INSTANCE
private backpressuredArray: plugins.lik.BackpressuredArray<TOutput>; // an array that only takes a defined amount of items 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>;
@ -80,11 +105,11 @@ export class SmartDuplex<TInput = any, TOutput = any> extends Duplex {
public async _read(size: number): Promise<void> { public async _read(size: number): Promise<void> {
this.debugLog(`${this.options.name}: read was called`); this.debugLog(`${this.options.name}: read was called`);
await this.backpressuredArray.waitForItems();
this.debugLog(`${this.options.name}: successfully waited for items.`);
if (this.options.readFunction) { if (this.options.readFunction) {
await this.options.readFunction(); await this.options.readFunction();
} }
await this.backpressuredArray.waitForItems();
this.debugLog(`${this.options.name}: successfully waited for items.`);
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();

View File

@ -1,4 +1,4 @@
import { Transform, type TransformCallback, type TransformOptions } from 'stream'; import { type TransformOptions } from 'stream';
import { SmartDuplex } from './smartstream.classes.smartduplex.js'; import { SmartDuplex } from './smartstream.classes.smartduplex.js';
export interface AsyncTransformFunction<TInput, TOutput> { export interface AsyncTransformFunction<TInput, TOutput> {

View File

@ -3,6 +3,6 @@
*/ */
export const commitinfo = { export const commitinfo = {
name: '@push.rocks/smartstream', name: '@push.rocks/smartstream',
version: '3.1.1', version: '3.2.1',
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.'
} }