A fast way to split concatenated JSON data into individual chunks.
Install:
npm install json-split-stream
const JSONSplitStream = require('json-split-stream');
const chunker = new JSONSplitStream(); // is a Duplex stream
chunker.write('[1,2,3]{"a":12}');
chunker.read(); // => '[1,2,3]'
chunker.read(); // => '{"a":12}'
chunker.read(); // => null
Also supports not storing the incoming data and just emitting events for JSON boundaries (counted in JS string length, not bytes):
const chunker = new JSONSplitStream({ storeData: false });
chunker.on('finishedJSON', ({ jsonEnd }) => {
console.log('ended at', jsonEnd);
});
chunker.write('[1,2,3]{"a":');
chunker.write('12}');
// Prints:
ended at 7
ended at 15
MIT