From 1e32deaf2bd4ff6a6ba4211d39ecfb61bb6c5f49 Mon Sep 17 00:00:00 2001 From: Vyacheslav Skakun Date: Wed, 24 Jan 2024 16:01:51 +0300 Subject: [PATCH 1/2] fix readAsPromised - stuck if stream alredy "end" #36 fix setPromiseItem - no wait input promise !! #37 --- 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(); - }); + } }, }; } From 7bd131078a342783dbd52f082a202a63c502cc61 Mon Sep 17 00:00:00 2001 From: Vyacheslav Skakun Date: Mon, 18 Mar 2024 17:57:44 +0300 Subject: [PATCH 2/2] Fix: Readable Stream read 16,384 objects in ObjectMode #41 --- src/JsonStreamStringify.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/JsonStreamStringify.ts b/src/JsonStreamStringify.ts index ea97f06..224565f 100644 --- a/src/JsonStreamStringify.ts +++ b/src/JsonStreamStringify.ts @@ -126,7 +126,7 @@ function quoteString(string: string) { return str; } -function readAsPromised(stream, size) { +function readAsPromised(stream, size?) { const value = stream.read(size); if (value === null) { return new Promise((resolve, reject) => { @@ -327,10 +327,10 @@ export class JsonStreamStringify extends Readable { let i = 0; const item = { type: 'readable object', - async read(size: number) { + async read() { try { let out = ''; - const data = await readAsPromised(input, size); + const data = await readAsPromised(input); if (data === null) { if (i && that.indent) { out += `\n${parent.indent}`;