How to use the abort function from readable-stream

Find comprehensive JavaScript readable-stream.abort code examples handpicked from public code repositorys.

readable-stream.abort is a method used to abort a stream, triggering the "abort" event.

7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
  return function (err) {
    if (closed) return;
    if (destroyed) return;
    destroyed = true; // request.destroy just do .end - .abort is what we want


    if (isRequest(stream)) return stream.abort();
    if (typeof stream.destroy === 'function') return stream.destroy();
    callback(err || new ERR_STREAM_DESTROYED('pipe'));
  };
}
fork icon2
star icon3
watch icon1

How does readable-stream.abort work?

The readable-stream.abort method is used to abort a stream. This method is typically used to signal that a stream should stop processing data and emit the "abort" event. When the readable-stream.abort method is called, the stream implementation should perform any necessary cleanup and then emit the "abort" event. The "abort" event indicates that the stream has been aborted, and is typically used to inform any consumers of the stream that the stream is no longer processing data. In Node.js, the readable-stream.abort method is part of the Readable and Writable stream classes. Both of these classes inherit from the Duplex class, which provides a default implementation of the readable-stream.abort method. Here is an example of how readable-stream.abort can be used in a Node.js application: javascript Copy code {{{{{{{ const { Readable } = require('stream'); const myStream = new Readable({ read(size) { // Implementation of the read() method } }); // Abort the stream myStream.abort(); In this example, we create a new Readable stream using the stream.Readable class. We then call the readable-stream.abort method on the stream, which will trigger the "abort" event. Overall, the readable-stream.abort method is a useful method for aborting a stream and informing any consumers of the stream that the stream has been aborted.

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
const { Readable } = require("stream");

const myStream = new Readable({
  read(size) {
    // Implementation of the read() method
  },
});

// Listen for the "abort" event
myStream.on("abort", () => {
  console.log("The stream has been aborted");
});

// Abort the stream after 5 seconds
setTimeout(() => {
  myStream.abort();
}, 5000);

In this example, we create a new Readable stream using the stream.Readable class. We then listen for the "abort" event on the stream, and log a message to the console when the event is emitted. We then use setTimeout to call the readable-stream.abort method on the stream after 5 seconds. When this method is called, the stream will stop processing data and emit the "abort" event. Overall, this example demonstrates how to use readable-stream.abort to abort a stream and trigger the "abort" event.