Custom Transform — modify data as it flows through a stream.
1const { Transform } = require("stream");23// JSON lines parser4class JSONLinesParser extends Transform {5 constructor(options) {6 super({ ...options, objectMode: true });7 this.buffer = "";8 }910 _transform(chunk, encoding, callback) {11 this.buffer += chunk.toString();12 const lines = this.buffer.split("\n");13 this.buffer = lines.pop();1415 for (const line of lines) {16 if (line.trim()) {17 try {18 this.push(JSON.parse(line));19 } catch (err) {20 this.emit("error", err);21 }22 }23 }24 callback();25 }2627 _flush(callback) {28 if (this.buffer.trim()) {29 try {30 this.push(JSON.parse(this.buffer));31 } catch (err) {32 this.emit("error", err);33 }34 }35 callback();36 }37}3839// Usage40const { createReadStream } = require("fs");41const { pipeline } = require("stream/promises");4243await pipeline(44 createReadStream("data.jsonl"),45 new JSONLinesParser(),46 new Writable({47 write(obj, encoding, callback) {48 console.log("Parsed:", obj);49 callback();50 },51 })52);