This repository was archived by the owner on Dec 4, 2018. It is now read-only.

Description
JSONStream 1.3.2 silently ignores JSON syntax errors sometimes, for example it does so for {"], but rightfully emits an error for a{"].
Minimal working example:
const JSONStream = require('JSONStream');
const fs = require('fs');
const FILE = 'json-stream-bug.json';
fs.writeFileSync(FILE, '{"]', { encoding: 'utf8' });
fs.createReadStream(FILE, {
encoding: 'utf8'
})
.on('error', err => {
console.error(err);
})
.pipe(JSONStream.parse())
.on('error', err => {
console.error(err);
})
.on('data', data => {
console.log(data);
});
Workaround
Check if the end event gets emitted without data having been emitted before.
const JSONStream = require('JSONStream');
const fs = require('fs');
const FILE = 'json-stream-bug.json';
fs.writeFileSync(FILE, '{"]', { encoding: 'utf8' });
let dataReceivedYet = false;
fs.createReadStream(FILE, {
encoding: 'utf8'
})
.on('error', err => {
console.error(err);
})
.pipe(JSONStream.parse())
.on('error', err => {
console.error(err);
})
.on('data', data => {
dataReceivedYet = true;
console.log(data);
})
.on('end', () => {
if (!dataReceivedYet) {
// Error situation
console.error('Unknown JSON parse error');
}
});