dominictarr / split

MIT License
347 stars 39 forks source link

split dies when using a transform function that returns an object #8

Open kevzettler opened 10 years ago

kevzettler commented 10 years ago

per the readme I should beable to pass a function to split which returns an object ( like JSON.parse ) and then get a stream of objects.

Doing this I get an error

function splitHandler(){
  return {test: true};
}

  var stream = fs.createReadStream(file_path)
                 .pipe(zlib.createGunzip())
                 .pipe(split(splitHandler))
                 .pipe(process.stdout)
net.js:614
    throw new TypeError('invalid data');
          ^
TypeError: invalid data
    at WriteStream.Socket.write (net.js:614:11)
    at Stream.ondata (stream.js:51:26)
    at Stream.emit (events.js:95:17)
    at drain (/home/app/aquirer_shuffle/node_modules/split/node_modules/through/index.js:36:16)
    at Stream.stream.queue.stream.push (/home/app/aquirer_shuffle/node_modules/split/node_modules/through/index.js:45:5)
    at emit (/home/app/aquirer_shuffle/node_modules/split/index.js:33:16)
    at next (/home/app/aquirer_shuffle/node_modules/split/index.js:48:7)
    at Stream.<anonymous> (/home/app/aquirer_shuffle/node_modules/split/index.js:53:5)
    at Stream.stream.write (/home/app/aquirer_shuffle/node_modules/split/node_modules/through/index.js:26:11)
    at write (_stream_readable.js:585:24)

if I change the splitHandler to

function splitHandler(){
  return "derp";
}

and return a string it works

dominictarr commented 10 years ago

the problem is that process.stdout can only accept buffers or strings, and you are passing it objects. if you do .on('data', console.log) that will work, or else add a stream (like require('JSONStream').stringify() that turns a stream of objects into a stream of bytes.