Skip to content
Related Articles

Related Articles

Node.js | Stream.pipeline() Method
  • Last Updated : 01 Jul, 2020
GeeksforGeeks - Summer Carnival Banner

The stream.pipeline() method is a module method that is used to the pipe by linking the streams passing on errors and accurately cleaning up and providing a callback function when the pipeline is done.

Syntax:

stream.pipeline(...streams, callback)

Parameters: This method accepts two parameters as mentioned above and described below.

  • …streams: These are two or more streams which are to be piped together.
  • callback: This function is called when the pipeline is fully done and it shows an ‘error’ if the pipeline is not accomplished.

Return Value: It returns a cleanup function.

Below examples illustrate the use of stream.pipeline() method in Node.js:



Example 1:




// Node.js program to demonstrate the     
// stream.pipeline() method
  
// Including 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");
  
// Constructing 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);
  }
  })();

Output:

Promise {  }
pipeline accomplished.

Example 2:




// Node.js program to demonstrate the     
// stream.pipeline() method
  
// Including 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");
  
// Constructing 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);
  }
  })();

Output: Here, the order of streams is not proper while piping so an error occurs.

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)

Reference: https://nodejs.org/api/stream.html#stream_stream_pipeline_streams_callback.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :