Skip to content

Commit

Permalink
stream: convert premature close to AbortError
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
ronag committed Jul 28, 2021
1 parent b14fb01 commit 3f0b623
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 6 deletions.
15 changes: 14 additions & 1 deletion lib/internal/webstreams/adapters.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const {
ERR_INVALID_STATE,
ERR_STREAM_PREMATURE_CLOSE,
},
AbortError,
} = require('internal/errors');

const {
Expand Down Expand Up @@ -120,6 +121,12 @@ function newWritableStreamFromStreamWritable(streamWritable) {
}

const cleanup = finished(streamWritable, (error) => {
if (error?.code === 'ERR_STREAM_PREMATURE_CLOSE') {
const err = new AbortError();
err.cause = error;
error = err;
}

cleanup();
// This is a protection against non-standard, legacy streams
// that happen to emit an error event again after finished is called.
Expand All @@ -144,7 +151,7 @@ function newWritableStreamFromStreamWritable(streamWritable) {
closed = undefined;
return;
}
controller.error(new ERR_STREAM_PREMATURE_CLOSE());
controller.error(new AbortError());
controller = undefined;
});

Expand Down Expand Up @@ -395,6 +402,12 @@ function newReadableStreamFromStreamReadable(streamReadable) {
streamReadable.pause();

const cleanup = finished(streamReadable, (error) => {
if (error?.code === 'ERR_STREAM_PREMATURE_CLOSE') {
const err = new AbortError();
err.cause = error;
error = err;
}

cleanup();
// This is a protection against non-standard, legacy streams
// that happen to emit an error event again after finished is called.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const {
const reader = readableStream.getReader();

assert.rejects(reader.closed, {
code: 'ERR_STREAM_PREMATURE_CLOSE',
code: 'ABORT_ERR',
});

readable.on('end', common.mustNotCall());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ const {
const writer = writable.getWriter();

assert.rejects(reader.closed, {
code: 'ERR_STREAM_PREMATURE_CLOSE',
code: 'ABORT_ERR',
});

assert.rejects(writer.closed, {
code: 'ERR_STREAM_PREMATURE_CLOSE',
code: 'ABORT_ERR',
});

duplex.destroy();
Expand Down Expand Up @@ -165,7 +165,7 @@ const {

reader.closed.then(common.mustCall());
assert.rejects(writer.closed, {
code: 'ERR_STREAM_PREMATURE_CLOSE',
code: 'ABORT_ERR',
});

duplex.end();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class TestWritable extends Writable {
const writableStream = newWritableStreamFromStreamWritable(writable);

assert.rejects(writableStream.close(), {
code: 'ERR_STREAM_PREMATURE_CLOSE'
code: 'ABORT_ERR'
});

writable.end();
Expand Down

0 comments on commit 3f0b623

Please sign in to comment.