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
- HTTP responses, on the client
- HTTP requests, on the server
- fs read streams
- zlib streams
- crypto streams
- TCP sockets
- child process stdout and stderr
- process.stdin
Writable
- HTTP requests, on the client
- HTTP responses, on the server
- fs write streams
- zlib streams
- crypto streams
- TCP sockets
- child process stdin
- process.stdout, process.stderr
Transform
- zlib streams
- crypto streams
Duplex
- TCP sockets
- zlib streams
- crypto streams
Async json parse
- JSONStream
- stream-json
- json-mask
Streams
By Vladimir
Streams
- 72