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

JSON syntax error is sometimes silently ignored #145

@ComFreek

Description

@ComFreek

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');
	}
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions