feat(SmartDuplex): Added method to create SmartDuplex from a WebReadableStream.

This commit is contained in:
Philipp Kunz 2024-10-16 01:02:46 +02:00
parent 22d93b4c07
commit ddb7d4af03
5 changed files with 34 additions and 3 deletions

View File

@ -1,5 +1,11 @@
# Changelog # Changelog
## 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) ## 2024-10-14 - 3.1.2 - fix(WebDuplexStream)
Fix variable naming inconsistency in WebDuplexStream test Fix variable naming inconsistency in WebDuplexStream test

View File

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

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.2', version: '3.2.0',
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.'
} }