Skip to content

Instantly share code, notes, and snippets.

@jgerardsimcock
Last active August 29, 2015 14:06
Show Gist options
  • Save jgerardsimcock/9b5367724d20733a247d to your computer and use it in GitHub Desktop.
Save jgerardsimcock/9b5367724d20733a247d to your computer and use it in GitHub Desktop.
var combine = require('stream-combiner');
var split = require('split');
var through = require('through');
var zlib = require('zlib');
module.exports = function () {
var genreInfo;
function write(buf) {
if (buf.length === 0) {
return;
}
buf = JSON.parse(buf);
if (buf.type === 'genre') {
if (genreInfo) {
this.queue(JSON.stringify(genreInfo) + '\n');
}
genreInfo = {
name: buf.name,
books: []
};
}
else {
genreInfo.books.push(buf.name);
}
}
function end() {
this.queue(JSON.stringify(genreInfo) + '\n');
this.queue(null);
}
return combine(
split(),
through(write, end),
zlib.createGzip()
);
};
//concat stream takes the whole content as a buffer
var concat = require('concat-stream');
//pipes it into concat with a callback function that you can then do stuff to.
//the function takes an object then you can call methods on that object
process.stdin.pipe(concat(function(src){
var s = src.toString().split('').reverse().join('');
console.log(s);
}));
var spawn = require('child_process').spawn;
var duplexer = require('duplexer');
module.exports = function (cmd, args) {
var proc = spawn(cmd, args);
return duplexer(proc.stdin, proc.stdout);
};
var trumpet = require('trumpet');
var through = require('through');
var tr = trumpet();
var loud = tr.select('.loud').createStream();
loud.pipe(through(function (buf) {
this.queue(buf.toString().toUpperCase());
})).pipe(loud);
process.stdin.pipe(tr).pipe(process.stdout);
var http = require('http');
var through = require('through');
var server = http.createServer(function (req, res) {
if (req.method === 'POST') {
req.pipe(through(function (buf) {
this.queue(buf.toString().toUpperCase());
})).pipe(res);
}
else res.end('send me a POST\n');
});
server.listen(parseInt(process.argv[2]));
var tar = require('tar');
var parser = tar.Parse();
var crypto = require('crypto');
var through = require('through');
var gunzipStream = require('zlib').createGunzip();
var cypherStream = crypto.createDecipher(process.argv[2], process.argv[3]);
parser.on('entry', function (e) {
var md5Stream = crypto.createHash('md5', { encoding: 'hex' });
if (e.type !== 'File') {
return;
}
e.pipe(md5Stream).pipe(through(function (buf) {
this.queue(buf.toString());
}, function () {
this.queue(" " + e.path + "\n");
this.queue(null);
})).pipe(process.stdout);
});
process.stdin.pipe(cypherStream).pipe(gunzipStream).pipe(parser);
//this will pipe data from one thing to another and then you can transform it to a string and then do stuff to it.
var through = require('through');
var tr = through(
function write(data){
this.queue(data.toString().toUpperCase());
}
);
process.stdin.pipe(tr).pipe(process.stdout);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment