Skip to content

Instantly share code, notes, and snippets.

@juliangruber
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juliangruber/78cf90b6d9c097b41291 to your computer and use it in GitHub Desktop.
Save juliangruber/78cf90b6d9c097b41291 to your computer and use it in GitHub Desktop.
var co = require('co');
var split = require('split');
var thunk = require('thunkify');
var pipe = thunk(require('multipipe'));
var read = require('co-read');
var write = require('co-write');
var JSONStream = require('JSONStream');
co(function*(){
var headerData = {};
var splitter = split();
yield [
pipe(process.stdin, splitter),
function*(){
var line;
while (line = (yield read(splitter)).toString('utf8')) {
if (line === '') {
yield write(process.stdout, JSON.stringify(headerData));
yield pipe(
splitter,
JSONStream.parse(['rows', true]),
JSONStream.stringify(),
process.stdout
);
return;
}
var m = /^(\S+):(.+)/.exec(line);
var key = m && m[1].trim();
var value = m && m[2].trim();
if (m) headerData[key] = value;
}
}
];
})(function(err){
if (err) throw err;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment