I have a file in which I have serialized array of objects. I need to stream the file object by object.
My question regarding nodejs stream is that, do I have to take care of my data boundaries? That is, nodejs readable stream emits line by line. So after every line, should I be checking if it is a valid JSON object?
My current implementation is something like this
f = fs.createReadStream('file.txt');
let buff = '';
f.on('data', (data) => {
buff += data;
try {
process(JSON.parse(buff));
buff = '';
} catch (e) {
return;
}
}
Is there a better alternative? Also do I need to define my own data boundaries? Note that the objects I need to serialize are continuous. That is, they will be received over time and not once.