-
-
Save janl/41d97adb43f0430c094e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> echo '{"a":1}' | ../node_query_server/bin/qs2 | |
net.js:614 | |
throw new TypeError('invalid data'); | |
^ | |
TypeError: invalid data | |
at WriteStream.Socket.write (net.js:614:11) | |
at null._onTimeout (/Users/jan/Work/qs2/node_query_server/index.js:17:20) | |
at Timer.listOnTimeout [as ontimeout] (timers.js:112:15) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var ndj = require('ndjson'); | |
process.stdin.setEncoding('utf8'); | |
function getRandomInt(min, max) { | |
return Math.floor(Math.random() * (max - min)) + min; | |
} | |
// var nd_out = process | |
// .stdout | |
// .pipe(ndj.serialize()); | |
var send = function(data) { | |
// console.error('data: ', data); | |
setTimeout(function() { | |
// nd_out.write(data); | |
process.stdout.write(data); | |
}, getRandomInt(1, 100)); | |
}; | |
process | |
.stdin | |
.pipe(ndj.parse()) | |
.on('data', send); |
@janl process.stdout.write(data)
fails because data
is a object (ndjson.parse()
returns an object stream). since process.stdout
is a binary stream it only accepts strings or buffers.
simply doing process.stdout.write(JSON.stringify(data))
would fix this 😄
the problem here is you are attempting to write a live javascript object to stdout.
this: stdout.write({a: 1})
you need to stringify it first.
one way would be to pipe it to ndjson.stringify()
before piping to stdout.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated the gist and added a new call log.