Skip to content

Commit

Permalink
Fix wrong parse error when chunks are splitted on whitespaces inside …
Browse files Browse the repository at this point in the history
…objects or arrays (#6)
  • Loading branch information
alvedder committed May 12, 2021
1 parent 7da34ca commit 293fbc2
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 15 deletions.
28 changes: 20 additions & 8 deletions src/parse-chunked.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,15 @@ class ChunkParser {
lastFlushPoint = flushPoint;
}

break;
case 0x09: /* \t */
case 0x0A: /* \n */
case 0x0D: /* \r */
case 0x20: /* space */
// prevent passing trailing whitespaces to the next flush(..) call
if (lastFlushPoint === i) {
++lastFlushPoint;
}
break;
}
}
Expand All @@ -317,20 +326,23 @@ class ChunkParser {

// Produce pendingChunk if any
if (flushPoint < chunkLength) {
const newPending = chunk.slice(flushPoint, chunkLength);
let newPending = chunk.slice(flushPoint, chunkLength);

if (this.pendingChunk === null && !this.stateString) {
newPending = newPending.trimStart();
}

this.pendingChunk = this.pendingChunk !== null
? this.pendingChunk + newPending
: newPending;
if (newPending.length) {
this.pendingChunk = this.pendingChunk !== null
? this.pendingChunk + newPending
: newPending;
}
}
}

finish() {
if (this.pendingChunk !== null) {
if (/[^ \t\r\n]/.test(this.pendingChunk)) {
this.flush('', 0, 0);
}

this.flush('', 0, 0);
this.pendingChunk = null;
}

Expand Down
27 changes: 20 additions & 7 deletions test/parse-chunked.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,27 @@ describe('parseChunked()', () => {
}

describe('trailing whitespaces', () => {
const expected = {};
const json = '{} \r\n\t';
describe('at the end', () => {
const expected = {};
const json = '{} \r\n\t';

for (let len = 0; len <= json.length; len++) {
it(len ? len + ' char(s) length chunks' : 'parse full', async () =>
assert.deepStrictEqual(await parse(len ? split(json, len) : [json]), expected)
);
}
});

for (let len = 0; len <= json.length; len++) {
it(len ? len + ' char(s) length chunks' : 'parse full', async () =>
assert.deepStrictEqual(await parse(len ? split(json, len) : [json]), expected)
);
}
describe('between objects and arrays', () => {
const expected = [{}, {}, {}, [], [], [], {}];
const json = '[{} \r\n\t, {}, \r\n\t {} \r\n\t, [], \r\n\t [] \r\n\t, [] \r\n\t, {} \r\n\t]';

for (let len = 0; len <= json.length; len++) {
it(len ? len + ' char(s) length chunks' : 'parse full', async () =>
assert.deepStrictEqual(await parse(len ? split(json, len) : [json]), expected)
);
}
});
});

describe('errors', () => {
Expand Down

0 comments on commit 293fbc2

Please sign in to comment.