title: how to use node transform to transform one object into another
body: <p>I'm new to Node streams and trying to figure out why things aren't working as expected. I'm just practicing and want to do a silly transform.</p>
  
  <pre><code>interface MyTransformStreamOptions {
    [key: string]: any
  }
  
  class MyTransformStream extends Transform {
    constructor(options) {
      options.readableObjectMode = true;
      options.writableObjectMode = true;
  
      super(options);
    }
  
    _transform(chunk, encoding, callback) {
      console.log('encoding: ' + encoding);
  
      // chunk should be an object here, due to
      // readableObjectMode = true right?
  
      const newObj = {};
  
      for (const [key, value] of Object.entries(chunk)) {
        console.log(`[key, value]: [${key}, ${value}]`);
        newObj['aa' + key] = 'bb' + value;
      }
  
      callback(null, newObj);
    }
  }
  
  const myTransformStream = new MyTransformStream({} as MyTransformStreamOptions);
  
  process.stdin
    .pipe(myTransformStream)
    .pipe(process.stdout);
  </code></pre>
  
  <p>Finally I run the script and input this via stdin:</p>
  
  <pre><code>{ car: 'honda', truck: 'mazda' }
  </code></pre>
  
  <p>and also I've tried:</p>
  
  <pre><code>"{ car: 'honda', truck: 'mazda' }"
  </code></pre>
  
  <p>But the loop treats the chunk as a string and not an object. So I see output like:</p>
  
  <pre><code>[key, value]: [0, 123]
  [key, value]: [1, 32]
  ...etc...
  </code></pre>
  
  <p>I've tried various combinations of:</p>
  
  <pre><code>options.readableObjectMode = true/false;
  options.writableObjectMode = true/false;
  </code></pre>
  
  <p>And also in <code>_transform()</code> using:</p>
  
  <pre><code>const obj = JSON.parse(chunk.toString());
  </code></pre>
  
  <p>but <code>obj</code> is never an object and is always a string.</p>
  
  <p>My goal is imply to receive an object into my transform stream, alter it, and output it. But I get stuck dealing with strings only that I cannot convert to objects even with <code>JSON.parse()</code>.</p>
  
  <p>Where am I going wrong here?</p>
  
  <p><strong>Update - Partial Solution</strong></p>
  
  <p>The problem was with my use of <code>JSON.parse()</code>. I was trying to parse:</p>
  
  <pre><code>{ car: 'honda', truck: 'mazda' }
  </code></pre>
  
  <p>Which is valid JS, but not valid JSON, duh!</p>
  
  <p>However, the bigger issue still remains. I've set the Transform stream to: <code>writableObjectMode = true</code>, but when I do:</p>
  
  <pre><code>callback(null, newObj);
  </code></pre>
  
  <p>I get the error:</p>
  
  <p><code>TypeError: Invalid data, chunk must be a string or buffer, not object</code></p>
  
  <p>If I do:</p>
  
  <pre><code>callback(null, JSON.stringify(newObj));
  </code></pre>
  
  <p>things works as expected.</p>
  
  <p>However, shouldn't this work without stringifying given that I've set things up in object mode?</p>