feat(virtualstream): Enhance VirtualStream with optional closure when reading from webstream

This commit is contained in:
Philipp Kunz 2024-10-11 02:15:45 +02:00
parent 04d60e6a95
commit 83cd25d5a2
4 changed files with 11 additions and 3 deletions

View File

@ -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.

View File

@ -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) => {

View File

@ -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.'
}

View File

@ -363,7 +363,7 @@ export class VirtualStream<T = Uint8Array> implements plugins.typedRequestInterf
* reads from a Readable and sends it to the other side
* @param readableStreamArg
*/
public async readFromWebstream(readableStreamArg: ReadableStream<T>) {
public async readFromWebstream(readableStreamArg: ReadableStream<T>, closeAfterReading = true) {
const reader = readableStreamArg.getReader();
let streamIsDone = false;
while(!streamIsDone) {
@ -373,6 +373,9 @@ export class VirtualStream<T = Uint8Array> implements plugins.typedRequestInterf
}
streamIsDone = done;
}
if (closeAfterReading) {
await this.close();
}
}
public async writeToWebstream(writableStreamArg: WritableStream<T>) {