From 3f0b62375b3a4edc8365b803749e8dd5abc706b0 Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Mon, 26 Jul 2021 09:31:10 +0200 Subject: [PATCH] stream: convert premature close to AbortError AbortError is a more "web" align alternative to ERR_STREAM_PREMATURE_CLOSE. PR-URL: /~https://github.com/nodejs/node/pull/39524 Reviewed-By: Antoine du Hamel Reviewed-By: James M Snell --- lib/internal/webstreams/adapters.js | 15 ++++++++++++++- ...hatwg-webstreams-adapters-to-readablestream.js | 2 +- ...webstreams-adapters-to-readablewritablepair.js | 6 +++--- ...hatwg-webstreams-adapters-to-writablestream.js | 2 +- 4 files changed, 19 insertions(+), 6 deletions(-) diff --git a/lib/internal/webstreams/adapters.js b/lib/internal/webstreams/adapters.js index 1c251a830726ba..7718fcc2c6b198 100644 --- a/lib/internal/webstreams/adapters.js +++ b/lib/internal/webstreams/adapters.js @@ -50,6 +50,7 @@ const { ERR_INVALID_STATE, ERR_STREAM_PREMATURE_CLOSE, }, + AbortError, } = require('internal/errors'); const { @@ -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. @@ -144,7 +151,7 @@ function newWritableStreamFromStreamWritable(streamWritable) { closed = undefined; return; } - controller.error(new ERR_STREAM_PREMATURE_CLOSE()); + controller.error(new AbortError()); controller = undefined; }); @@ -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. diff --git a/test/parallel/test-whatwg-webstreams-adapters-to-readablestream.js b/test/parallel/test-whatwg-webstreams-adapters-to-readablestream.js index 05710c2c0c36f2..433552388ac7a0 100644 --- a/test/parallel/test-whatwg-webstreams-adapters-to-readablestream.js +++ b/test/parallel/test-whatwg-webstreams-adapters-to-readablestream.js @@ -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()); diff --git a/test/parallel/test-whatwg-webstreams-adapters-to-readablewritablepair.js b/test/parallel/test-whatwg-webstreams-adapters-to-readablewritablepair.js index c425a8c1a23e80..a4ce9d163214cd 100644 --- a/test/parallel/test-whatwg-webstreams-adapters-to-readablewritablepair.js +++ b/test/parallel/test-whatwg-webstreams-adapters-to-readablewritablepair.js @@ -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(); @@ -165,7 +165,7 @@ const { reader.closed.then(common.mustCall()); assert.rejects(writer.closed, { - code: 'ERR_STREAM_PREMATURE_CLOSE', + code: 'ABORT_ERR', }); duplex.end(); diff --git a/test/parallel/test-whatwg-webstreams-adapters-to-writablestream.js b/test/parallel/test-whatwg-webstreams-adapters-to-writablestream.js index e23318b7707213..4457ad39f8cb6d 100644 --- a/test/parallel/test-whatwg-webstreams-adapters-to-writablestream.js +++ b/test/parallel/test-whatwg-webstreams-adapters-to-writablestream.js @@ -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();