Fabrício Matté
Web developer, open source projects contributor and Stack Overflow citizen.
Fabrício Matté
twitter.com/Ult_Combo
github.com/UltCombo
A sequence of data elements made available over time.
An array of asynchronous values.
$ curl --data domain=test http://api.otsus.vm/install | format-json-stream > test.json
Process data chunks as soon as they are made available.
Better perf, smaller memory footprint.
Connect streams through pipes.
Separation of concerns, reusable code.
http://blog.chromium.org/2015/03/new-javascript-techniques-for-rapid.html
Has a single resolution (fulfillment/rejection) value
Consumes/processes/emits streaming chunks of data
Produces data
Consumes data
Is both readable and writable
Is a duplex stream, where the output is in some way calculated from the input.
Written data goes through the stream, being transformed into new readable data.
Is a simple transform stream implementation which passes the input to the output without any modification.
Connects a readable stream to a writable stream.
Handles backpressure and pause/resume auto-magically.
HLS
Video feed
Codec
.pipe()
.pipe()
RTP
File system
fs.createReadStream('path')
fs.createWriteStream('path')
.pipe()
HTTP req stream
FTP GET stream
Composability!
A platform to run JavaScript everywhere.
Control file system, network and everything in the host machine.
Develop CLI programs using JavaScript.
A fork of Node.js.
Updated JavaScript engine (V8) and other base libraries.
Open governance model.
Improved streams.
var Readable = require('stream').Readable;
var randomStream = new Readable({
read: function() {
var n = Math.floor(Math.random() * 100);
this.push(n < 95 ? n + '\n' : null);
}
});
var Readable = require('stream').Readable;
var util = require('util');
util.inherits(RandomNumbers, Readable);
function RandomNumbers(opt) {
Readable.call(this, opt);
}
RandomNumbers.prototype._read = function() {
var n = Math.floor(Math.random() * 100);
this.push(n < 95 ? n + '\n' : null);
};
var randomStream = new RandomNumbers();
The package manager for JavaScript.
Over 140,000 public packages.
*npm is always lowercase
*gulp is always lowercase
Returns a readable stream which produces vinyl files.
Examples:
Are transform streams that operate on vinyl file streams.
Examples:
gulp.src('js/*.js')
.pipe(gulp.dest('dist/js'));
Is equivalent to:
var readableStream = gulp.src('js/*.js');
var writableStream = gulp.dest('dest/js');
readableStream.pipe(writableStream);
gulp.src('js/*.js') // readable stream
.pipe(babel()) // transform stream
.pipe(uglify()) // transform stream
.pipe(concat('all.js')) // transform stream
.pipe(gulp.dest('dist/js')); // writable stream
No intermediary disk writes!
gulp.src('js/*.js') // readable stream
.pipe(babel()) // transform stream
.pipe(through2.obj(function(file, enc, cb) {
file.contents = Buffer.concat([new Buffer('// Gulping Streams\n'), file.contents]);
cb(null, file);
}))
.pipe(gulp.dest('dest/js')); // writable stream
Fabrício Matté
twitter.com/Ult_Combo
github.com/UltCombo
https://slides.com/fabriciomatte/gulping-streams
By Fabrício Matté
#streams #gulpjs
Web developer, open source projects contributor and Stack Overflow citizen.