- Mastering Node.js(Second Edition)
- Sandro Pasquali Kevin Faaborg
- 178字
- 2021-07-02 19:28:49
Using PassThrough streams
This sort of stream is a trivial implementation of a Transform stream, which simply passes received input bytes through to an output stream. This is useful if one doesn't require any transformation of the input data, and simply wants to easily pipe a Readable stream to a Writable stream.
PassThrough streams have benefits similar to JavaScript's anonymous functions, making it easy to assert minimal functionality without too much fuss. For example, it is not necessary to implement an abstract base class, as one does with for the _read method of a Readable stream. Consider the following use of a PassThrough stream as an event spy:
const fs = require('fs');
const stream = require('stream');
const spy = new stream.PassThrough();
spy
.on('error', (err) => console.error(err))
.on('data', function(chunk) {
console.log(`spied data -> ${chunk}`);
})
.on('end', () => console.log('\nfinished'));
fs.createReadStream('./passthrough.txt').pipe(spy).pipe(process.stdout);
Normally a Transform or Duplex stream is what you want (where you can set up a proper implementation of _read and _write), but in certain scenarios, such as tests, it can be useful to place "watchers" on a stream.
- Building Django 2.0 Web Applications
- TCP/IP入門經典(第5版)
- 數字通信同步技術的MATLAB與FPGA實現:Altera/Verilog版(第2版)
- Mastering TypeScript 3
- 數字調制解調技術的MATLAB與FPGA實現:Altera/Verilog版(第2版)
- 網絡安全應急響應技術實戰
- Unity Artificial Intelligence Programming
- Building Web Applications with ArcGIS
- Working with Legacy Systems
- SAE原理與網絡規劃
- 工業互聯網創新實踐
- 6G無線網絡空口關鍵技術
- 高級網絡技術
- Selenium WebDriver 3 Practical Guide
- Web用戶查詢日志挖掘與應用