Skip to content

Instantly share code, notes, and snippets.

@ratbeard
Created February 23, 2014 22:15
Show Gist options
  • Select an option

  • Save ratbeard/9178145 to your computer and use it in GitHub Desktop.

Select an option

Save ratbeard/9178145 to your computer and use it in GitHub Desktop.
qrequest = require("request")
http = require('http')
fs = require('fs')
through = require('through')
split = require('split')
concat = require('concat-stream')
ws = require("websocket-stream")
trumpet = require('trumpet')()
spawn = require('child_process').spawn
duplexer = require('duplexer')
combine = require('stream-combiner')
zlib = require('zlib')
crypto = require('crypto')
tar = require('tar')
stdin = process.stdin
stdout = process.stdout
stderr = process.stderr
// SECRETZ
decrypt = crypto.createDecipher(process.argv[2], process.argv[3]);
unzip = zlib.createGunzip();
parser = tar.Parse();
parser.on("entry", function (entry) {
if (entry.type != "File") return;
var encrypt = crypto.createHash("md5", { encoding: 'hex' })
entry
.pipe(encrypt)
.pipe(through(process))
.pipe(stdout);
function process(s) {
ss = s + " " + entry.path + "\n";
this.queue(ss)
}
});
stdin
.pipe(decrypt)
.pipe(unzip)
.pipe(parser)
return
// CRYPT
stream = crypto.createDecipher("aes256", process.argv[2]);
stdin.pipe(stream).pipe(stdout)
return
////
module.exports = function() {
return combine(
split(),
through(processBooks),
zlib.createGzip()
)
}
currentGenre = null
function processBooks(line) {
if (!line) {
this.queue(JSON.stringify(currentGenre) + '\n')
return
}
line = JSON.parse(line)
if (line.type == 'genre') {
if(currentGenre) {
this.queue(JSON.stringify(currentGenre) + '\n')
}
currentGenre = {name: line.name, books: []}
}
else {
currentGenre.books.push(line.name)
}
}
return
// i cheated on this one
module.exports = function (counter) {
counts = {}
input = through(function(c) {
counts[c.country] = (counts[c.country] || 0) + 1
}, end)
function end() {
counter.setCounts(counts)
}
return duplexer(input, counter)
}
function countries(i) {
console.log('countries', i)
}
return
module.exports = function(cmd, args) {
child = spawn(cmd, args)
return duplexer(child.stdin, child.stdout)
}
return
stdin
.pipe(trumpet)
.pipe(stdout)
trumpet.select(".loud").createStream()
.pipe(through(uppercase))
.pipe(stdout)
return
stream = ws("ws://localhost:8100")
stream.end("hello\n")
return
r = request.post("http://localhost:8000")
stdin.pipe(r).pipe(stdout)
return
function uppercase(s) {
//stderr.write(s)
this.queue(String(s).toUpperCase())
}
server = http.createServer(function (request, response) {
if (request.method === 'POST') {
request
.pipe(through(uppercase))
.pipe(response)
}
else throw ":("
//response.end()
})
server.listen(process.argv[2])
@ratbeard
Copy link
Author

Solutions to stream-adventure

!!! 🐀

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