Node.js | Stream transform.destroy() Method

The transform.destroy() method in a Readable Stream is used to destroy the transform stream, and also emits an ‘error’ event optionally. Moreover, the transform stream releases any internal resources after this call is being made.

Syntax:

transform.destroy( error )

Parameters: This method accepts single parameters error which emits an error event optionally.

Return Value: It emits error event if any error is made in creating stream else it only destroys the transform streams.

Below examples illustrate the use of transform.destroy() method in Node.js:



Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Node.js program to demonstrate the     
// transform.destroy() method
  
// Accessing zlib module
var zlib = require("zlib");
  
// Create a transform stream
var transform = zlib.createGzip();
  
// Calling destroy method
transform.destroy();
transform.destroyed;

chevron_right


Output:

true

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Node.js program to demonstrate the     
// transform.destroy() method
   
// Accessing fs module
var fs = require("fs");
  
// Accessing zlib module
var zlib = require("zlib");
  
// Create a readable stream
var readable = fs.createReadStream('input.text');
  
// Create a writable stream
var writable = fs.createWriteStream('output.text');
  
// Create a transform stream
var transform = zlib.createGzip();
  
// Calling pipe method
readable.pipe(transform).pipe(writable);
  
// Calling destroy method
transform.destroy();
console.log("done...");

chevron_right


Output:

done...

Here, the transform stream is destroyed so the piping made is also removed.

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




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.