header.js 685 B

12345678910111213141516171819202122
  1. var splicer = require('../');
  2. var through = require('through2');
  3. var JSONStream = require('JSONStream');
  4. var split = require('split');
  5. var headerData = {};
  6. var headers = through.obj(function (buf, enc, next) {
  7. var line = buf.toString('utf8');
  8. if (line === '') {
  9. this.push(headerData);
  10. pipeline.splice(1, 1, JSONStream.parse([ 'rows', true ]));
  11. }
  12. else {
  13. var m = /^(\S+):(.+)/.exec(line);
  14. var key = m && m[1].trim();
  15. var value = m && m[2].trim();
  16. if (m) headerData[key] = value;
  17. }
  18. next();
  19. });
  20. var pipeline = splicer([ split(), headers, JSONStream.stringify() ]);
  21. process.stdin.pipe(pipeline).pipe(process.stdout);