From 74e6205ac3fe8c2614b1d098b42c493414383268 Mon Sep 17 00:00:00 2001 From: Philipp Kunz Date: Wed, 16 Oct 2024 09:56:32 +0200 Subject: [PATCH] fix(VirtualStream): Fix stream closing behavior to correctly handle closing bits --- changelog.md | 7 +++++++ ts/00_commitinfo_data.ts | 2 +- ts/classes.virtualstream.ts | 20 +++++++++++++++----- 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/changelog.md b/changelog.md index e2be463..a4ce420 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,12 @@ # Changelog +## 2024-10-16 - 3.1.8 - fix(VirtualStream) +Fix stream closing behavior to correctly handle closing bits + +- Introduced a 'closingBit' constant to properly signal the end of stream data. +- Updated the 'readFromWebstream' function to send a closing bit upon completion if 'closeAfterReading' is true. +- Modified the 'close' method to optionally send a closing bit when terminating the stream. + ## 2024-10-16 - 3.1.7 - fix(VirtualStream) Fix issue in VirtualStream to handle null values during data writing. diff --git a/ts/00_commitinfo_data.ts b/ts/00_commitinfo_data.ts index e6d696a..590aeba 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.1.7', + version: '3.1.8', 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 4a73a2c..f50e4e1 100644 --- a/ts/classes.virtualstream.ts +++ b/ts/classes.virtualstream.ts @@ -1,6 +1,9 @@ import * as plugins from './plugins.js'; import { TypedRouter } from './classes.typedrouter.js'; + +const closingBit: any = '#############CLOSING BIT#############'; + export interface ICommFunctions { sendMethod?: ( sendPayload: plugins.typedRequestInterfaces.IStreamRequest @@ -278,7 +281,7 @@ export class VirtualStream implements plugins.typedRequestInterf cycleId: streamTrArg.request.cycleId, cycle: 'response', mainPurpose: 'chunk', - next: this.sendBackpressuredArray.data.length > 1, + next: this.sendBackpressuredArray.data.length > 1, // 1 and not 0 because we call shift a few lines down backpressure: !this.receiveBackpressuredArray.checkSpaceAvailable(), chunkData: this.sendBackpressuredArray.shift(), }; @@ -392,7 +395,7 @@ export class VirtualStream implements plugins.typedRequestInterf streamIsDone = done; } if (closeAfterReading) { - await this.close(); + await this.close(true); } } @@ -400,7 +403,7 @@ export class VirtualStream implements plugins.typedRequestInterf const writer = writableStreamArg.getWriter(); while(this.keepAlive || this.receiveBackpressuredArray.checkHasItems()) { const value = await this.fetchData(); - if (value === null) { + if (value === closingBit) { writableStreamArg.close(); break; } @@ -408,8 +411,15 @@ export class VirtualStream implements plugins.typedRequestInterf } } - public async close() { - this.sendData(null); + /** + * closes the stream + * if sendClosingBitArg is true, the stream will send a closing bit + * @param sendClosingBitArg + */ + public async close(sendClosingBitArg = false) { + if (sendClosingBitArg) { + this.sendData(closingBit); + } this.keepAlive = false; } }