如何在 Node.js 中对 JavaScript 对象进行管道/流式处理?
本文简要介绍了 node.js 中的管道/流实现,并解释了各个部分作为缓冲区、流的工作以及如何在 node.js 中实现管道
缓冲区:这些是从一个地方传输到另一个地方的数据块的临时存储点,缓冲区充满数据然后传递。缓冲区一次传输小块数据。缓冲区非常有用,因为我们不必等待完整的传输过程,因为我们填满了缓冲区,缓冲区是数据的临时存储点,然后可以完全传递。
流:它们是用于传输数据的数据处理方法。它提高了性能,因为甚至在数据完全传输之前就处理了数据。它将缓冲区的小块收集在一起,当缓冲区被填满时,它将数据向下传递到流中以进行处理并发送给客户端。它们是一种数据处理方法,用于按照适当的顺序将输入读取或写入输出。在播放在线视频时将缓冲区和流一起播放,您无需等待完整的视频被处理,而是在缓冲区和流的帮助下逐位传输的小部分。
可读流:可读流用于从任何来源提取信息,它们用于读取任何来源上存在的数据,然后我们可以使用该数据传输到任何来源或存储在我们的系统中。可读流以块(从缓冲区接收)的形式发出事件,这是需要处理的数据的一小部分。
示例:在当前项目目录中创建一个带有一些示例文本的read.txt文件,在这种情况下,我们有以下文本。
THIS IS READ.TXT FILE
文件名:index.js
Javascript
// Requiring file system module
var fs = require('fs');
// Creating a read stream
var myReadStream = fs.createReadStream('read.txt', 'utf8');
myReadStream.on('data', function (chunk) {
console.log("new chunk received");
// Printing the chunk
console.log(chunk);
});
Javascript
// Requiring file system module
var fs = require('fs');
// Creating a write stream
// It will create a txt file in provided location
var myWriteStream =
fs.createWriteStream(__dirname + '/write.txt');
// Writing on stream
myWriteStream.write("Greetings from GeeksforGeeks");
Javascript
var fs = require('fs');
const zlib = require('zlib');
const { pipeline } = require('stream');
// Constructing promisify
const { promisify } = require('util');
const pipelineAsync = promisify(pipeline);
// Creating a read stream
var myReadStream = fs.createReadStream(
__dirname + '/read.txt', 'utf8');
// Creating a write stream
var myWriteStream = fs.createWriteStream(
__dirname + '/write.txt');
// Creating transform stream
const transform = zlib.createGzip();
(async function run() {
try {
// Pipelining three streams
await pipelineAsync(
// Reading from read stream
myReadStream,
// Transforming the file
transform,
// Writing on write stream
myWriteStream
);
console.log("Pipeline accomplished");
}
catch (err) {
console.error('pipeline failed with error:', err);
}
})();
Javascript
// Requiring file system module
var fs = require('fs');
// Creating a read stream
var myReadStream = fs.createReadStream(
__dirname + '/read.txt','utf8');
// Creating a write stream
var myWriteStream = fs.createWriteStream(
__dirname + '/write.txt');
// Piping using pipe
myReadStream.pipe(myWriteStream);
console.log("Pipeing accomplished");
输出:
new chunk received
THIS IS READ.TXT FILE
可写流:可写流允许您将数据发送到任何目标源,可以是任何文件或在线流式传输。这些流还发出事件以确保定期正常运行。
示例:使用以下代码创建一个index.js文件。
文件名:index.js
Javascript
// Requiring file system module
var fs = require('fs');
// Creating a write stream
// It will create a txt file in provided location
var myWriteStream =
fs.createWriteStream(__dirname + '/write.txt');
// Writing on stream
myWriteStream.write("Greetings from GeeksforGeeks");
输出:将在当前目录中生成一个名为write.txt的新文件,其中包含以下数据。
Greetings from GeeksforGeeks
使用管道()管道:
管道是一种在缓冲区的帮助下直接将数据从一个流传输到另一个流的方法,这样数据传输过程就可以在它填满缓冲区时立即开始。换句话说,管道用于在各个步骤中处理流数据。这是一种模块方法,用于在流转发错误之间进行管道传输,并在管道完成时正确清理并提供回调。
示例:在当前项目目录中创建一个带有一些示例文本的read.txt文件,在这种情况下,我们有以下文本。
THIS IS READ.TXT FILE
文件名:index.js
Javascript
var fs = require('fs');
const zlib = require('zlib');
const { pipeline } = require('stream');
// Constructing promisify
const { promisify } = require('util');
const pipelineAsync = promisify(pipeline);
// Creating a read stream
var myReadStream = fs.createReadStream(
__dirname + '/read.txt', 'utf8');
// Creating a write stream
var myWriteStream = fs.createWriteStream(
__dirname + '/write.txt');
// Creating transform stream
const transform = zlib.createGzip();
(async function run() {
try {
// Pipelining three streams
await pipelineAsync(
// Reading from read stream
myReadStream,
// Transforming the file
transform,
// Writing on write stream
myWriteStream
);
console.log("Pipeline accomplished");
}
catch (err) {
console.error('pipeline failed with error:', err);
}
})();
输出:我们将看到以下输出和 write.txt文件将在当前目录中生成。
Pipeline accomplished
注意:应该使用管道而不是管道,因为管道是不安全的,但在管道上的实现如下:
文件名:index.js
Javascript
// Requiring file system module
var fs = require('fs');
// Creating a read stream
var myReadStream = fs.createReadStream(
__dirname + '/read.txt','utf8');
// Creating a write stream
var myWriteStream = fs.createWriteStream(
__dirname + '/write.txt');
// Piping using pipe
myReadStream.pipe(myWriteStream);
console.log("Pipeing accomplished");
输出:我们将看到以下输出和 write.txt文件将在当前目录中生成。
Pipeing accomplished