Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Transform stream in node js read strings, but write in object mode?

Tags:

stream

node.js

I am trying to make transform stream to write in object mode, but keep reading strings. Is it possible? Documentation says, that for Duplex stream I can set readableObjectMode and writableObjectMode separately, but somehow it is not working for me. When I use callback with my object in _flush, I get error: Invalid non-string/buffer chunk

Am I doing something wrong or it doesn't work in Transform streams?

Here is my code:

class stream extends Transform {
  private logs: { name: string, errors: any[] };

  constructor() {
    super({ writableObjectMode: true });
    this.logs = { name: this.tableName, errors: [] };
  }

  _transform(chunk, encoding, callback) {
    // stuff here
    callback();
  }

  _flush(callback) {
    //here I get error
    callback(undefined, this.logs);
  }
}
like image 387
klnkt Avatar asked Sep 10 '25 21:09

klnkt


1 Answers

I found the answer. I need to set { readableObjectMode: true } instead, because this is actually a readable side of my transform stream that I am using, not writable.

like image 180
klnkt Avatar answered Sep 13 '25 10:09

klnkt