Streams

Gulp

var fs = require('fs');

fs.readFile('/path/to/file.json', 
    'utf8', 
    function (err, data) {
        if (err) throw err; 
        var obj = JSON.parse(data);
    });

Streams in node

  • Readable
  • Writable
  • Duplex
  • Transform

Stream instanceof EventEmitter

StreamClass.emit('eventName', data)

StreamClass.on('eventName', (data)=>{})

Method pipe

//например, по "схеме" DataBase -> File
Readable.pipe(Writable);

//DataBase -> преобразовать в JSON формат -> сохранить JSON в File
Readable.pipe(Transform).pipe(Writable);

//прочитать из DataBase -> обработать -> записать обратно в DataBase результат
Duplex.pipe(Transform).pipe(Duplex);

bottlenecks

Buffering

new StreamObject({objectMode: false, highWaterMark: кол_во_байт}); 
//по умолчанию 16384 (16kb)

new StreamObject({objectMode: true, highWaterMark: кол_во_объектов});
//по умолчанию  16

Readable

  1. HTTP responses, on the client
  2. HTTP requests, on the server
  3. fs read streams
  4. zlib streams
  5. crypto streams
  6. TCP sockets
  7. child process stdout and stderr
  8. process.stdin

Writable

  1. HTTP requests, on the client
  2. HTTP responses, on the server
  3. fs write streams
  4. zlib streams
  5. crypto streams
  6. TCP sockets
  7. child process stdin
  8. process.stdout, process.stderr

Transform 

  1. zlib streams
  2. crypto streams​

Duplex

  1. TCP sockets
  2. zlib streams
  3. crypto streams

Async json parse

Streams

By Vladimir

Streams

  • 72