From 6ecbc1dcb3fd19f502609fcf8b0a8c32f9002b36 Mon Sep 17 00:00:00 2001 From: Benjamin Gruenbaum Date: Mon, 7 Dec 2020 18:42:46 +0200 Subject: [PATCH] stream: support abortsignal in constructor PR-URL: https://github.com/nodejs/node/pull/36431 Reviewed-By: Robert Nagy Reviewed-By: Matteo Collina --- doc/api/stream.md | 46 +++++++++++++++++++ lib/internal/streams/add-abort-signal.js | 15 ++++-- lib/internal/streams/readable.js | 6 +++ lib/internal/streams/writable.js | 7 +++ lib/stream.js | 3 +- test/parallel/test-stream-duplex-destroy.js | 17 +++++++ test/parallel/test-stream-readable-destroy.js | 16 +++++++ test/parallel/test-stream-writable-destroy.js | 15 ++++++ 8 files changed, 119 insertions(+), 6 deletions(-) diff --git a/doc/api/stream.md b/doc/api/stream.md index c8ccd06bec79bb..936a81cc6e4219 100644 --- a/doc/api/stream.md +++ b/doc/api/stream.md @@ -1938,6 +1938,9 @@ method. #### `new stream.Writable([options])` ```js @@ -2028,6 +2032,27 @@ const myWritable = new Writable({ }); ``` +Calling `abort` on the `AbortController` corresponding to the passed +`AbortSignal` will behave the same way as calling `.destroy(new AbortError())` +on the writeable stream. + +```js +const { Writable } = require('stream'); + +const controller = new AbortController(); +const myWritable = new Writable({ + write(chunk, encoding, callback) { + // ... + }, + writev(chunks, callback) { + // ... + }, + signal: controller.signal +}); +// Later, abort the operation closing the stream +controller.abort(); + +``` #### `writable._construct(callback)` ```js @@ -2346,6 +2375,23 @@ const myReadable = new Readable({ }); ``` +Calling `abort` on the `AbortController` corresponding to the passed +`AbortSignal` will behave the same way as calling `.destroy(new AbortError())` +on the readable created. + +```js +const fs = require('fs'); +const controller = new AbortController(); +const read = new Readable({ + read(size) { + // ... + }, + signal: controller.signal +}); +// Later, abort the operation closing the stream +controller.abort(); +``` + #### `readable._construct(callback)`