當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。