fix(VirtualStream): Fix stream closing behavior to correctly handle closing bits

This commit is contained in:
2024-10-16 09:56:32 +02:00
parent 57e51543e7
commit 74e6205ac3
3 changed files with 23 additions and 6 deletions
+15 -5
View File
@@ -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<T = Uint8Array> 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<T = Uint8Array> implements plugins.typedRequestInterf
streamIsDone = done;
}
if (closeAfterReading) {
await this.close();
await this.close(true);
}
}
@@ -400,7 +403,7 @@ export class VirtualStream<T = Uint8Array> 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<T = Uint8Array> 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;
}
}