From ddb7d4af033a1473d426faef0824d035a407b5f2 Mon Sep 17 00:00:00 2001 From: Philipp Kunz Date: Wed, 16 Oct 2024 01:02:46 +0200 Subject: [PATCH] feat(SmartDuplex): Added method to create SmartDuplex from a WebReadableStream. --- changelog.md | 6 ++++++ ts/00_commitinfo_data.ts | 2 +- ts/smartstream.classes.smartduplex.ts | 25 +++++++++++++++++++++++++ ts/smartstream.functions.ts | 2 +- ts_web/00_commitinfo_data.ts | 2 +- 5 files changed, 34 insertions(+), 3 deletions(-) diff --git a/changelog.md b/changelog.md index 57543d0..4537fdd 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,11 @@ # 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) Fix variable naming inconsistency in WebDuplexStream test diff --git a/ts/00_commitinfo_data.ts b/ts/00_commitinfo_data.ts index 70ba700..ad47881 100644 --- a/ts/00_commitinfo_data.ts +++ b/ts/00_commitinfo_data.ts @@ -3,6 +3,6 @@ */ export const commitinfo = { 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.' } diff --git a/ts/smartstream.classes.smartduplex.ts b/ts/smartstream.classes.smartduplex.ts index a029734..e344234 100644 --- a/ts/smartstream.classes.smartduplex.ts +++ b/ts/smartstream.classes.smartduplex.ts @@ -52,6 +52,31 @@ export class SmartDuplex extends Duplex { return smartDuplex; } + public static fromWebReadableStream( + readableStream: ReadableStream + ): SmartDuplex { + const smartDuplex = new SmartDuplex({ + 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 private backpressuredArray: plugins.lik.BackpressuredArray; // an array that only takes a defined amount of items public options: ISmartDuplexOptions; diff --git a/ts/smartstream.functions.ts b/ts/smartstream.functions.ts index 935a1dd..11d7203 100644 --- a/ts/smartstream.functions.ts +++ b/ts/smartstream.functions.ts @@ -1,4 +1,4 @@ -import { Transform, type TransformCallback, type TransformOptions } from 'stream'; +import { type TransformOptions } from 'stream'; import { SmartDuplex } from './smartstream.classes.smartduplex.js'; export interface AsyncTransformFunction { diff --git a/ts_web/00_commitinfo_data.ts b/ts_web/00_commitinfo_data.ts index 70ba700..ad47881 100644 --- a/ts_web/00_commitinfo_data.ts +++ b/ts_web/00_commitinfo_data.ts @@ -3,6 +3,6 @@ */ export const commitinfo = { 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.' }