Note there are many transform streams that do this:
JSON -> JS
but I am looking to create a Node.js transform stream that can do:
JS -> JSON
I have a readable stream:
const readable = getReadableStream({objectMode:true});
the readable stream outputs Objects, not strings.
I need to create a transform stream which can filter some of those objects and convert the objects to JSON, like so:
const t = new Transform({
objectMode: true,
transform(chunk, encoding, cb) {
if(chunk && chunk.marker === true){
this.push(JSON.stringify(chunk));
}
cb();
},
flush(cb) {
cb();
}
});
however, for some reason my transform stream cannot accepts objects to the transform method, only string and buffer, what can I do?
I tried adding this two options:
const t = new Transform({
objectMode: true,
readableObjectMode: true, // added this
writableObjectMode: true, // added this too
transform(chunk, encoding, cb) {
this.push(chunk);
cb();
},
flush(cb) {
cb();
}
});
unfortunately my transform stream still cannot accept objects, only string/buffer.