Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "stream: add abort signal for ReadableStream and WritableStream" #46756

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
66 changes: 2 additions & 64 deletions doc/api/stream.md
Original file line number Diff line number Diff line change
Expand Up @@ -3228,24 +3228,17 @@ readable.getReader().read().then((result) => {

<!-- YAML
added: v15.4.0
changes:
- version: v19.7.0
pr-url: https://github.com/nodejs/node/pull/46273
description: Added support for `ReadableStream` and
`WritableStream`.
-->

* `signal` {AbortSignal} A signal representing possible cancellation
* `stream` {Stream|ReadableStream|WritableStream}

A stream to attach a signal to.
* `stream` {Stream} a stream to attach a signal to

Attaches an AbortSignal to a readable or writeable stream. This lets code
control stream destruction using an `AbortController`.

Calling `abort` on the `AbortController` corresponding to the passed
`AbortSignal` will behave the same way as calling `.destroy(new AbortError())`
on the stream, and `controller.error(new AbortError())` for webstreams.
on the stream.

```js
const fs = require('node:fs');
Expand All @@ -3259,61 +3252,6 @@ const read = addAbortSignal(
controller.abort();
```

Or using an `AbortSignal` with a readable stream as an async iterable:

```js
const controller = new AbortController();
setTimeout(() => controller.abort(), 10_000); // set a timeout
const stream = addAbortSignal(
controller.signal,
fs.createReadStream(('object.json')),
);
(async () => {
try {
for await (const chunk of stream) {
await process(chunk);
}
} catch (e) {
if (e.name === 'AbortError') {
// The operation was cancelled
} else {
throw e;
}
}
})();
```

Or using an `AbortSignal` with a ReadableStream:

```js
const controller = new AbortController();
const rs = new ReadableStream({
start(controller) {
controller.enqueue('hello');
controller.enqueue('world');
controller.close();
},
});

addAbortSignal(controller.signal, rs);

finished(rs, (err) => {
if (err) {
if (err.name === 'AbortError') {
// The operation was cancelled
}
}
});

const reader = rs.getReader();

reader.read().then(({ value, done }) => {
console.log(value); // hello
console.log(done); // false
controller.abort();
});
```

## API for stream implementers

<!--type=misc-->
Expand Down
25 changes: 9 additions & 16 deletions lib/internal/streams/add-abort-signal.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,6 @@ const {
codes,
} = require('internal/errors');

const {
isNodeStream,
isWebStream,
kControllerErrorFunction,
} = require('internal/streams/utils');

const eos = require('internal/streams/end-of-stream');
const { ERR_INVALID_ARG_TYPE } = codes;

Expand All @@ -24,25 +18,24 @@ const validateAbortSignal = (signal, name) => {
}
};

function isNodeStream(obj) {
return !!(obj && typeof obj.pipe === 'function');
}

module.exports.addAbortSignal = function addAbortSignal(signal, stream) {
validateAbortSignal(signal, 'signal');
if (!isNodeStream(stream) && !isWebStream(stream)) {
throw new ERR_INVALID_ARG_TYPE('stream', ['ReadableStream', 'WritableStream', 'Stream'], stream);
if (!isNodeStream(stream)) {
throw new ERR_INVALID_ARG_TYPE('stream', 'stream.Stream', stream);
}
return module.exports.addAbortSignalNoValidate(signal, stream);
};

module.exports.addAbortSignalNoValidate = function(signal, stream) {
if (typeof signal !== 'object' || !('aborted' in signal)) {
return stream;
}
const onAbort = isNodeStream(stream) ?
() => {
stream.destroy(new AbortError(undefined, { cause: signal.reason }));
} :
() => {
stream[kControllerErrorFunction](new AbortError(undefined, { cause: signal.reason }));
};
const onAbort = () => {
stream.destroy(new AbortError(undefined, { cause: signal.reason }));
};
if (signal.aborted) {
onAbort();
} else {
Expand Down
2 changes: 0 additions & 2 deletions lib/internal/streams/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ const kIsReadable = Symbol('kIsReadable');
const kIsDisturbed = Symbol('kIsDisturbed');

const kIsClosedPromise = SymbolFor('nodejs.webstream.isClosedPromise');
const kControllerErrorFunction = SymbolFor('nodejs.webstream.controllerErrorFunction');

function isReadableNodeStream(obj, strict = false) {
return !!(
Expand Down Expand Up @@ -306,7 +305,6 @@ module.exports = {
isReadable,
kIsReadable,
kIsClosedPromise,
kControllerErrorFunction,
isClosed,
isDestroyed,
isDuplexNodeStream,
Expand Down
4 changes: 1 addition & 3 deletions lib/internal/webstreams/readablestream.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ const {
kIsErrored,
kIsReadable,
kIsClosedPromise,
kControllerErrorFunction,
} = require('internal/streams/utils');

const {
Expand Down Expand Up @@ -261,7 +260,6 @@ class ReadableStream {
};

this[kIsClosedPromise] = createDeferredPromise();
this[kControllerErrorFunction] = () => {};

// The spec requires handling of the strategy first
// here. Specifically, if getting the size and
Expand Down Expand Up @@ -1893,6 +1891,7 @@ function readableStreamClose(stream) {
assert(stream[kState].state === 'readable');
stream[kState].state = 'closed';
stream[kIsClosedPromise].resolve();

const {
reader,
} = stream[kState];
Expand Down Expand Up @@ -2331,7 +2330,6 @@ function setupReadableStreamDefaultController(
stream,
};
stream[kState].controller = controller;
stream[kControllerErrorFunction] = FunctionPrototypeBind(controller.error, controller);

const startResult = startAlgorithm();

Expand Down
4 changes: 0 additions & 4 deletions lib/internal/webstreams/writablestream.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ const {

const {
kIsClosedPromise,
kControllerErrorFunction,
} = require('internal/streams/utils');

const {
Expand Down Expand Up @@ -200,7 +199,6 @@ class WritableStream {
};

this[kIsClosedPromise] = createDeferredPromise();
this[kControllerErrorFunction] = () => {};

const size = extractSizeAlgorithm(strategy?.size);
const highWaterMark = extractHighWaterMark(strategy?.highWaterMark, 1);
Expand Down Expand Up @@ -372,7 +370,6 @@ function TransferredWritableStream() {
},
};
this[kIsClosedPromise] = createDeferredPromise();
this[kControllerErrorFunction] = () => {};
},
[], WritableStream));
}
Expand Down Expand Up @@ -1285,7 +1282,6 @@ function setupWritableStreamDefaultController(
writeAlgorithm,
};
stream[kState].controller = controller;
stream[kControllerErrorFunction] = FunctionPrototypeBind(controller.error, controller);

writableStreamUpdateBackpressure(
stream,
Expand Down
168 changes: 0 additions & 168 deletions test/parallel/test-webstreams-abort-controller.js

This file was deleted.