Last active
August 29, 2015 14:02
-
-
Save juliangruber/78cf90b6d9c097b41291 to your computer and use it in GitHub Desktop.
This file contains 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 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