On Github unkillbob / akjs-streams-presentation
James Bunt @unkillbob
"We should have some ways of connecting programs like garden hose--screw in another segment when it becomes necessary to massage data in another way. This is the way of IO also."
var http = require('http'), fs = require('fs'); var pirateBay = http.createServer(function(req, res) { fs.readFile('photoshop.dmg', function(err, data) { res.end(data); }); }); pirateBay.listen(8080);
var http = require('http'), fs = require('fs'); var pirateBay = http.createServer(function(req, res) { fs.createReadStream('photoshop.dmg').pipe(res); }); pirateBay.listen(8080);
process.stdin
fs.createReadStream('/path/to/file.txt', { encoding: 'utf8' })
process.stdout
fs.createWriteStream('/path/to/file.txt', { encoding: 'utf8' })
var zlib = require('zlib'); var gzip = zlib.createGzip(); // readable & writable
var net = require('net'); var client = net.connect({ port: 8124 }, function() { // ... });
process.stdin.on('data', function(buf) { console.log(buf); });
readableStream.on('readable', function() { var data = readable.read(); });
writableStream.write(someData); writableStream.write(someMoreData);
writableStream.end(/*data*/);
readable.pipe(writable) // returns writable
readable.pipe(transform1) .pipe(transform2) // ... .pipe(writable);
var from = require('from'); var readData = from(function getChunk(count, next) { this.emit('data', someData); if (finished) { this.emit('end'); } next(); // or just `return true;` if sync }); readData.pipe(writable);
var through = require('through'); var transform = through(function write(data) { this.queue(data); }, function end() { this.queue(null); }); readable.pipe(transform).pipe(writable);
var map = require('map-stream'); var transform = map(function(data, callback) { // transform callback(null, newData); // emit 'error' event callback(err); // drop data (filter) callback(); }); readable.pipe(transform).pipe(writable);
var concat = require('concat-stream'); var gatherData = concat(function(allTheDatas) { // concatenated Buffer/String or an array }); readable.pipe(gatherData);
var http = require('http'); http.createServer(function(req, res) { // req: Readable stream // res: Writable stream });
var net = require('net'); var client = net.connect({ port: 8124 }, function() { // ... });
fs.createReadStream('log.txt') .pipe(split()) .pipe(processEachLineOfLog());
$ npm install -g gulp
$ npm install --save-dev gulp gulp-util
$ atom gulpfile.js
var gulp = require('gulp'); gulp.task('default', function() { // build all the things });
$ gulp
var gulp = require('gulp'), jshint = require('gulp-jshint'), uglify = require('gulp-uglify'), concat = require('gulp-concat'),
through = require('through'), map = require('map-stream'),
growl = require('growl'), // ...
gulp.task('scripts', ['lint'], function(done) { // return a stream, promise or call done
return gulp.src('./scripts/**/*.js') // readable
.pipe(uglify()) .pipe(concat('all.js'))
.pipe(gulp.dest('./dist')); // through stream });
gulp.task('default', ['lint', 'test'], function() { gulp.watch('{scripts,tests}/**', ['lint', 'test']); });
by @substack