Skip to content

Commit 3f0b623

Browse files
committed
stream: convert premature close to AbortError
AbortError is a more "web" align alternative to ERR_STREAM_PREMATURE_CLOSE. PR-URL: #39524 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent b14fb01 commit 3f0b623

4 files changed

+19
-6
lines changed

lib/internal/webstreams/adapters.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ const {
5050
ERR_INVALID_STATE,
5151
ERR_STREAM_PREMATURE_CLOSE,
5252
},
53+
AbortError,
5354
} = require('internal/errors');
5455

5556
const {
@@ -120,6 +121,12 @@ function newWritableStreamFromStreamWritable(streamWritable) {
120121
}
121122

122123
const cleanup = finished(streamWritable, (error) => {
124+
if (error?.code === 'ERR_STREAM_PREMATURE_CLOSE') {
125+
const err = new AbortError();
126+
err.cause = error;
127+
error = err;
128+
}
129+
123130
cleanup();
124131
// This is a protection against non-standard, legacy streams
125132
// that happen to emit an error event again after finished is called.
@@ -144,7 +151,7 @@ function newWritableStreamFromStreamWritable(streamWritable) {
144151
closed = undefined;
145152
return;
146153
}
147-
controller.error(new ERR_STREAM_PREMATURE_CLOSE());
154+
controller.error(new AbortError());
148155
controller = undefined;
149156
});
150157

@@ -395,6 +402,12 @@ function newReadableStreamFromStreamReadable(streamReadable) {
395402
streamReadable.pause();
396403

397404
const cleanup = finished(streamReadable, (error) => {
405+
if (error?.code === 'ERR_STREAM_PREMATURE_CLOSE') {
406+
const err = new AbortError();
407+
err.cause = error;
408+
error = err;
409+
}
410+
398411
cleanup();
399412
// This is a protection against non-standard, legacy streams
400413
// that happen to emit an error event again after finished is called.

test/parallel/test-whatwg-webstreams-adapters-to-readablestream.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ const {
5959
const reader = readableStream.getReader();
6060

6161
assert.rejects(reader.closed, {
62-
code: 'ERR_STREAM_PREMATURE_CLOSE',
62+
code: 'ABORT_ERR',
6363
});
6464

6565
readable.on('end', common.mustNotCall());

test/parallel/test-whatwg-webstreams-adapters-to-readablewritablepair.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ const {
2727
const writer = writable.getWriter();
2828

2929
assert.rejects(reader.closed, {
30-
code: 'ERR_STREAM_PREMATURE_CLOSE',
30+
code: 'ABORT_ERR',
3131
});
3232

3333
assert.rejects(writer.closed, {
34-
code: 'ERR_STREAM_PREMATURE_CLOSE',
34+
code: 'ABORT_ERR',
3535
});
3636

3737
duplex.destroy();
@@ -165,7 +165,7 @@ const {
165165

166166
reader.closed.then(common.mustCall());
167167
assert.rejects(writer.closed, {
168-
code: 'ERR_STREAM_PREMATURE_CLOSE',
168+
code: 'ABORT_ERR',
169169
});
170170

171171
duplex.end();

test/parallel/test-whatwg-webstreams-adapters-to-writablestream.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ class TestWritable extends Writable {
9292
const writableStream = newWritableStreamFromStreamWritable(writable);
9393

9494
assert.rejects(writableStream.close(), {
95-
code: 'ERR_STREAM_PREMATURE_CLOSE'
95+
code: 'ABORT_ERR'
9696
});
9797

9898
writable.end();

0 commit comments

Comments
 (0)