From 9a839e0d9f8a843daf97790b2984bf38cf159ff5 Mon Sep 17 00:00:00 2001 From: Borewit Date: Sun, 13 Oct 2024 15:53:34 +0200 Subject: [PATCH] Release stream-lock when closing the WebStreamReader --- lib/WebStreamReader.ts | 5 +++-- test/test.ts | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/WebStreamReader.ts b/lib/WebStreamReader.ts index edd2fd74..a9779a74 100644 --- a/lib/WebStreamReader.ts +++ b/lib/WebStreamReader.ts @@ -38,7 +38,8 @@ export class WebStreamReader extends AbstractStreamReader { return 0; } - public abort(): Promise { - return this.reader.cancel(); + public async abort(): Promise { + await this.reader.cancel(); // Signals a loss of interest in the stream by a consumer + this.reader.releaseLock(); } } diff --git a/test/test.ts b/test/test.ts index 27b96c48..6334a5cb 100644 --- a/test/test.ts +++ b/test/test.ts @@ -389,5 +389,19 @@ describe('Node.js StreamReader', () => { }); + describe('WebStreamReader', () => { + + it('abort() should release stream-lock', async () => { + + const readableStream = stringToReadableStream('abc'); + assert.isFalse(readableStream.locked, 'stream is unlocked before initializing tokenizer'); + + const webStreamReader = new WebStreamReader(readableStream); + assert.isTrue(readableStream.locked, 'stream is locked after initializing tokenizer'); + + await webStreamReader.abort(); + assert.isFalse(readableStream.locked, 'stream is unlocked after closing tokenizer'); + }); + }); });