Open In App

Node.js Stream transform.destroy() Method

Improve
Improve
Like Article
Like
Save
Share
Report

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 made. 

Syntax:

transform.destroy( error )

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

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

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

Example 1: 

javascript




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


Output:

true

Example 2: 

javascript




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


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



Last Updated : 03 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads