当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Node.js Stream.pipeline()用法及代码示例


stream.pipeline()方法是一种模块方法,通过链接传递错误的流并精确清理并在完成管道后提供回调函数,将其用于管道。

用法:

stream.pipeline(...streams, callback)

参数:该方法接受上面提到和下面描述的两个参数。



  • …streams:这是两个或多个要通过管道传输的流。
  • callback:管道完全完成后会调用此函数,如果管道未完成,则会显示“错误”。

返回值:它返回清除函数。

以下示例说明了Node.js中stream.pipeline()方法的用法:

范例1:

// Node.js program to demonstrate the      
// stream.pipeline() method 
  
// Includng fs and zlib module 
var fs = require('fs'); 
const zlib = require('zlib'); 
  
// Constructing finished from stream 
const { pipeline } = require('stream'); 
  
// Constructing promisify from 
// util 
const { promisify } = require('util'); 
  
// Defining pipelineAsync method 
const pipelineAsync = promisify(pipeline); 
  
// Constructing readable stream 
const readable = fs.createReadStream("input.text"); 
  
// Contructing writable stream 
var writable = fs.createWriteStream("output.text"); 
  
// Creating transform stream 
const transform = zlib.createGzip(); 
  
// Async function 
(async function run() { 
  try{  
  
    // pipelining three streams 
    await pipelineAsync( 
      readable, 
      transform, 
      writable 
    ); 
    console.log("pipeline accomplished."); 
    } 
  
  // Shows error 
  catch(err) { 
    console.error('pipeline failed with error:', err); 
  } 
  })();

输出:

Promise {  }
pipeline accomplished.

范例2:

// Node.js program to demonstrate the      
// stream.pipeline() method 
  
// Includng fs and zlib module 
var fs = require('fs'); 
const zlib = require('zlib'); 
  
// Constructing finished from stream 
const { pipeline } = require('stream'); 
  
// Constructing promisify from 
// util 
const { promisify } = require('util'); 
  
// Defining pipelineAsync method 
const pipelineAsync = promisify(pipeline); 
  
// Constructing readable stream 
const readable = fs.createReadStream("input.text"); 
  
// Contructing writable stream 
var writable = fs.createWriteStream("output.text"); 
  
// Creating transform stream 
const transform = zlib.createGzip(); 
  
// Async function 
(async function run() { 
  try{  
  
    // pipelining three streams 
    await pipelineAsync( 
      readable, 
      writable, 
      transform 
    ); 
    console.log("pipeline accomplished."); 
    } 
  
  // Shows error 
  catch(err) { 
    console.error('pipeline failed with error:', err); 
  } 
  })();

输出:在此,配管时流的顺序不正确,因此会发生错误。

Promise {  }
pipeline failed with error:Error [ERR_STREAM_CANNOT_PIPE]:Cannot pipe, not readable
    at WriteStream.Writable.pipe (_stream_writable.js:243:24)
    at pipe (internal/streams/pipeline.js:57:15)
    at Array.reduce ()
    at pipeline (internal/streams/pipeline.js:88:18)
    at Promise (internal/util.js:274:30)
    at new Promise ()
    at pipeline (internal/util.js:273:12)
    at run (/home/runner/ThirstyTimelyKey/index.js:33:11)
    at /home/runner/ThirstyTimelyKey/index.js:45:5
    at Script.runInContext (vm.js:133:20)

参考: https://nodejs.org/api/stream.html#stream_stream_pipeline_streams_callback




相关用法


注:本文由纯净天空筛选整理自nidhi1352singh大神的英文原创作品 Node.js | Stream.pipeline() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。