From 83cd25d5a212f750300bd72b5819999901b7b7c1 Mon Sep 17 00:00:00 2001 From: Philipp Kunz Date: Fri, 11 Oct 2024 02:15:45 +0200 Subject: [PATCH] feat(virtualstream): Enhance VirtualStream with optional closure when reading from webstream --- changelog.md | 6 ++++++ test/test.ts | 1 - ts/00_commitinfo_data.ts | 2 +- ts/classes.virtualstream.ts | 5 ++++- 4 files changed, 11 insertions(+), 3 deletions(-) diff --git a/changelog.md b/changelog.md index a10d157..76107d3 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,11 @@ # Changelog +## 2024-10-11 - 3.1.0 - feat(virtualstream) +Enhance VirtualStream with optional closure when reading from webstream + +- Added an optional parameter `closeAfterReading` to the `readFromWebstream` method. +- The stream will close automatically after reading if `closeAfterReading` is set to true. + ## 2024-10-11 - 3.0.33 - fix(test) Increase delay duration before stopping the server in test suite. diff --git a/test/test.ts b/test/test.ts index bfb6c77..e0c1151 100644 --- a/test/test.ts +++ b/test/test.ts @@ -98,7 +98,6 @@ tap.test('should allow VirtualStreams', async () => { const data = await generatedRequestingVS.fetchData(); const decodedData = new TextDecoder().decode(data); expect(decodedData).toEqual('hello'); - // await newRequestingVS.close(); }); tap.test('should end the server', async (toolsArg) => { diff --git a/ts/00_commitinfo_data.ts b/ts/00_commitinfo_data.ts index 7a69e6e..c6e2df9 100644 --- a/ts/00_commitinfo_data.ts +++ b/ts/00_commitinfo_data.ts @@ -3,6 +3,6 @@ */ export const commitinfo = { name: '@api.global/typedrequest', - version: '3.0.33', + version: '3.1.0', description: 'A TypeScript library for making typed requests towards APIs, including facilities for handling requests, routing, and virtual stream handling.' } diff --git a/ts/classes.virtualstream.ts b/ts/classes.virtualstream.ts index 658b13d..bf2f481 100644 --- a/ts/classes.virtualstream.ts +++ b/ts/classes.virtualstream.ts @@ -363,7 +363,7 @@ export class VirtualStream implements plugins.typedRequestInterf * reads from a Readable and sends it to the other side * @param readableStreamArg */ - public async readFromWebstream(readableStreamArg: ReadableStream) { + public async readFromWebstream(readableStreamArg: ReadableStream, closeAfterReading = true) { const reader = readableStreamArg.getReader(); let streamIsDone = false; while(!streamIsDone) { @@ -373,6 +373,9 @@ export class VirtualStream implements plugins.typedRequestInterf } streamIsDone = done; } + if (closeAfterReading) { + await this.close(); + } } public async writeToWebstream(writableStreamArg: WritableStream) {