From 6451e93c12d6a95caf1f66cadc82cf6924cd6349 Mon Sep 17 00:00:00 2001 From: Philipp Kunz Date: Sun, 2 Jun 2024 23:40:52 +0200 Subject: [PATCH] fix(smartduplex): now has a .getWebStreams method, that exposes a web streams compatible API --- ts/00_commitinfo_data.ts | 2 +- ts/smartstream.classes.smartduplex.ts | 49 ++++++++++++++++++++ ts_web/00_commitinfo_data.ts | 2 +- ts_web/convert.ts | 64 --------------------------- ts_web/index.ts | 3 -- 5 files changed, 51 insertions(+), 69 deletions(-) delete mode 100644 ts_web/convert.ts diff --git a/ts/00_commitinfo_data.ts b/ts/00_commitinfo_data.ts index 8c25f90..76649a8 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.0.39', + version: '3.0.40', 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 f329744..ae9b629 100644 --- a/ts/smartstream.classes.smartduplex.ts +++ b/ts/smartstream.classes.smartduplex.ts @@ -157,4 +157,53 @@ export class SmartDuplex extends Duplex { this.backpressuredArray.push(null); callback(); } + + public getWebStreams(): { readable: ReadableStream, writable: WritableStream } { + const duplex = this; + const readable = new ReadableStream({ + start(controller) { + duplex.on('readable', () => { + let chunk; + while (null !== (chunk = duplex.read())) { + controller.enqueue(chunk); + } + }); + + duplex.on('end', () => { + controller.close(); + }); + }, + cancel(reason) { + duplex.destroy(new Error(reason)); + } + }); + + const writable = new WritableStream({ + write(chunk) { + return new Promise((resolve, reject) => { + const isBackpressured = !duplex.write(chunk, (error) => { + if (error) { + reject(error); + } else { + resolve(); + } + }); + + if (isBackpressured) { + duplex.once('drain', resolve); + } + }); + }, + close() { + return new Promise((resolve, reject) => { + duplex.end(resolve); + }); + }, + abort(reason) { + duplex.destroy(new Error(reason)); + } + }); + + return { readable, writable }; + } } diff --git a/ts_web/00_commitinfo_data.ts b/ts_web/00_commitinfo_data.ts index 8c25f90..76649a8 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.0.39', + version: '3.0.40', 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_web/convert.ts b/ts_web/convert.ts deleted file mode 100644 index a88e297..0000000 --- a/ts_web/convert.ts +++ /dev/null @@ -1,64 +0,0 @@ -export interface IDuplexStream { - read(): any; - write(chunk: any, callback?: (error?: Error | null) => void): boolean; - on(event: string, listener: (...args: any[]) => void): this; - once(event: string, listener: (...args: any[]) => void): this; - end(callback?: () => void): void; - destroy(error?: Error): void; -} - -export interface IReadableStreamOptions { - highWaterMark?: number; -} - -export interface IWritableStreamOptions { - highWaterMark?: number; -} - -export function convertDuplexToWebStream(duplex: IDuplexStream): { readable: ReadableStream, writable: WritableStream } { - const readable = new ReadableStream({ - start(controller) { - duplex.on('readable', () => { - let chunk; - while (null !== (chunk = duplex.read())) { - controller.enqueue(chunk); - } - }); - - duplex.on('end', () => { - controller.close(); - }); - }, - cancel(reason) { - duplex.destroy(new Error(reason)); - } - }); - - const writable = new WritableStream({ - write(chunk) { - return new Promise((resolve, reject) => { - const isBackpressured = !duplex.write(chunk, (error) => { - if (error) { - reject(error); - } else { - resolve(); - } - }); - - if (isBackpressured) { - duplex.once('drain', resolve); - } - }); - }, - close() { - return new Promise((resolve, reject) => { - duplex.end(resolve); - }); - }, - abort(reason) { - duplex.destroy(new Error(reason)); - } - }); - - return { readable, writable }; -} diff --git a/ts_web/index.ts b/ts_web/index.ts index b49b5d7..7a71611 100644 --- a/ts_web/index.ts +++ b/ts_web/index.ts @@ -1,5 +1,2 @@ import './plugins.js'; export * from './classes.webduplexstream.js'; -export { - convertDuplexToWebStream, -} from './convert.js'; \ No newline at end of file