From 35dfefe4f2bf90d2a7d69a8a60c7f90b18aa49e6 Mon Sep 17 00:00:00 2001 From: Viacheslav <31972288+SlavikPr@users.noreply.github.com> Date: Sat, 27 Jan 2024 12:55:38 +0300 Subject: [PATCH] Resolve issue #38 & #37 fix readAsPromised - stuck if stream alredy "end" #36 (#38) fix setPromiseItem - no wait input promise !! #37 Co-authored-by: Vyacheslav Skakun --- src/JsonStreamStringify.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/JsonStreamStringify.ts b/src/JsonStreamStringify.ts index 9ae8c52..fbac647 100644 --- a/src/JsonStreamStringify.ts +++ b/src/JsonStreamStringify.ts @@ -130,6 +130,10 @@ function readAsPromised(stream, size) { const value = stream.read(size); if (value === null) { return new Promise((resolve, reject) => { + if (stream.readableEnded) { + resolve(null); + return; + } const endListener = () => resolve(null); stream.once('end', endListener); stream.once('error', reject); @@ -358,11 +362,13 @@ export class JsonStreamStringify extends Readable { this.item = { async read() { if (read) return; - read = true; - input.then((v) => that.setItem(v, parent, key), (err) => { + try { + read = true; + that.setItem(await input, parent, key); + } catch (err) { that.emit('error', err); that.destroy(); - }); + } }, }; }