Skip to content
This repository has been archived by the owner on Jul 5, 2019. It is now read-only.

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

Open
kevzettler opened this issue Jul 18, 2014 · 1 comment
Open

Comments

@kevzettler
Copy link

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
Copy link
Owner

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants