0

I have a readable stream that is giving me objects:

const readable = getReadableStream();

I can pipe that readable into a transform stream:

  const t = new Transform({
    objectMode: true,
    transform(chunk, encoding, cb) {
      this.push(chunk);
      cb();
    },
    flush(cb) {
       cb();
    }
  });

  readable.pipe(t);
  readable.push({some:'object'});

the problem is that I get this error:

TypeError [ERR_INVALID_ARG_TYPE]: The "chunk" argument must be one of type string or Buffer. Received type object at WriteStream.Socket.write (net.js:705:11) at Transform.ondata (_stream_readable.js:642:20) at Transform.emit (events.js:159:13)

I think what's happening is that in the transform stream, when transform() is called, that first argument (chunk) must be a string or buffer.

However, I know that I can call

this.push({foo:'bar'}) in the transform method...it's only the incoming data that seems to be restricted to being a string or buffer...

My question is two part:

  1. Is there a way to create a transform stream that can pass along objects, instead of just string or buffer?

  2. If the answer to 1 is "no", what is a reliable way to convert JS objects to JSON in the transform stream?

There might be some call that I am missing in order for it to receive objects instead of strings.

Alexander Mills
  • 90,741
  • 139
  • 482
  • 817

0 Answers0