Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/JsonStreamStringify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -454,12 +454,14 @@ export class JsonStreamStringify extends Readable {
/** if set, this string will be prepended to the next _push call, if the call output is not empty, and set to undefined */
prePush?: string;
private _push(data) {
const out = (this.objectItem ? this.objectItem.write() : '') + data;
if (this.prePush && out.length) {
this.buffer += this.prePush;
const prefix = this.objectItem ? this.objectItem.write() : '';
if (this.prePush && (prefix.length || data.length)) {
this.buffer += prefix + this.prePush;
this.prePush = undefined;
} else {
this.buffer += prefix;
}
this.buffer += out;
this.buffer += data;
if (this.buffer.length >= this.bufferSize) {
this.pushCalled = !this.push(this.buffer);
this.buffer = '';
Expand Down
11 changes: 11 additions & 0 deletions test-src/JsonStreamStringify.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,17 @@ describe('JsonStreamStringify', function () {
a: readableStream(1, 2, 3),
}, '{"a":[1,2,3]}'));

it('{key:"value",data:readableStream("hello")} should be {"key":"value","data":"hello"}', createTest({
key: 'value',
data: readableStream('hello'),
}, '{"key":"value","data":"hello"}'));

it('{a:"b",c:readableStream("d"),e:"f"} should be {"a":"b","c":"d","e":"f"}', createTest({
a: 'b',
c: readableStream('d'),
e: 'f',
}, '{"a":"b","c":"d","e":"f"}'));

it('readableStream(\'a\', \'b\', \'c\') should be "abc"', createTest(readableStream('a', 'b', 'c'), '"abc"'));

it('readableStream(\'a\', \'b\', \'c\') should be "abc"', () => {
Expand Down