Skip to content

Instantly share code, notes, and snippets.

@max-mapper
Forked from janl/log
Last active August 29, 2015 14:12
Show Gist options
  • Save max-mapper/b0bafdc6b9d8510124dd to your computer and use it in GitHub Desktop.
Save max-mapper/b0bafdc6b9d8510124dd to your computer and use it in GitHub Desktop.
> 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)
var ndj = require('ndjson');
var through = require('through2')
process.stdin.setEncoding('utf8');
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}
var send = through.obj(function(data, enc, done) {
// console.error('data: ', data);
setTimeout(function() {
send.write(data);
done()
}, getRandomInt(1, 100));
};
process
.stdin
.pipe(ndj.parse())
.pipe(send)
.pipe(ndj.serialize())
.pipe(process.stdout)
@janl
Copy link

janl commented Dec 28, 2014

NEAT!

Doesn’t seem quite right, though:

> cat f
{"a":1}
{"a":2}
{"a":3}
jan@rose ~/Work/qs2/node_query_server 
> cat f | ../node_query_server/bin/qs2 

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: write after end
    at writeAfterEnd (/Users/jan/Work/qs2/node_query_server/node_modules/through2/node_modules/readable-stream/lib/_stream_writable.js:144:12)
    at DestroyableTransform.Writable.write (/Users/jan/Work/qs2/node_query_server/node_modules/through2/node_modules/readable-stream/lib/_stream_writable.js:192:5)
    at null._onTimeout (/Users/jan/Work/qs2/node_query_server/index.js:13:10)
    at Timer.listOnTimeout [as ontimeout] (timers.js:112:15)


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment