Un flux de données directionnel et ordonné
node.js - v0.8 | node.js - v0.10 |
Streams 1 | Streams 2 |
push-stream | pull-stream |
Omniprésent dans la philosophie UNIX
FILE*
en C sous linux|
gzip |
tee "file.txt.gz"http://blog.izs.me/post/48281998870/unix-philosophy-and-node-js
Appartient au super-pattern de Message-Passing
"message passing sends a message to a process and relies on the process to select and invoke the actual code to run"
"The justifications for using an intermediate layer essentially falls into two categories: encapsulation and distribution"
... 4 en réalité
var Readable = require('stream').Readable; // ... readable.on('readable', function () { var chunk = readable.read(); }); readable.on('end', onEnd); readable.on('data', function (data) { // don't really use this please... });
var Writable = require('stream').Writable; // ... writable.write(chunk); writable.end(); var written = writable.write(chunk); // written is true or false
var Duplex = require('stream').Duplex; // ... var socket = net.createConnection(80, 'google.com'); socket.write("GET /\r\n\r\n"); socket.on('readable', function () { console.log(socket.read()); })
var Transform = require('stream').Transform; // ... var gzip = zlib.createGzip(); process.stdin .pipe(gzip) .pipe(process.stdout);
Avec un flux en écriture et en lecture, on peut chaîner les redirections de flux
http.createServer(function (req, res) { var busboy = new Busboy({ headers: req.headers }); var pack = tar.pack(); var gzip = zlib.createGzip(); busboy .on('file', function (field, stream, filename) { var entry = tar.entry({name: filename}); filename.pipe(entry); }) .on('finish', function () { pack.finalize(); }); req.pipe(busboy); pack.pipe(gzip).pipe(res); }).listen(80);
En tant qu'utilisateur de la classe Stream :
pipe()
Les streams peuvent en réalité aussi transporter des objets serialisés en JSON
Introduction du pattern A.pipe(B).pipe(A)
Utilisation du pattern A.pipe(B).pipe(A)
@dominictarr
)@substack
)@raynos
)
Florent Jaby
@Floby