From f0247654017b2efc0ac5eb5e7acd847247f5589a Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Mon, 26 Jul 2021 09:31:10 +0200 Subject: [PATCH 1/5] webstream: convert premature close to AbortError AbortError is a more "web" align alternative to ERR_STREAM_PREMATURE_CLOSE. --- lib/internal/webstreams/adapters.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/internal/webstreams/adapters.js b/lib/internal/webstreams/adapters.js index 1c251a830726ba..2d63498a14a474 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,10 @@ function newWritableStreamFromStreamWritable(streamWritable) { } const cleanup = finished(streamWritable, (error) => { + if (error?.code === 'ERR_STREAM_PREMATURE_CLOSE') { + error = new AbortError(); + } + cleanup(); // This is a protection against non-standard, legacy streams // that happen to emit an error event again after finished is called. From 18ba68a9a08ae22c0561f4fd16e614f6f948247d Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Mon, 26 Jul 2021 16:14:52 +0200 Subject: [PATCH 2/5] Update lib/internal/webstreams/adapters.js Co-authored-by: Antoine du Hamel --- lib/internal/webstreams/adapters.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/internal/webstreams/adapters.js b/lib/internal/webstreams/adapters.js index 2d63498a14a474..45c9ab3e356cc3 100644 --- a/lib/internal/webstreams/adapters.js +++ b/lib/internal/webstreams/adapters.js @@ -122,7 +122,9 @@ function newWritableStreamFromStreamWritable(streamWritable) { const cleanup = finished(streamWritable, (error) => { if (error?.code === 'ERR_STREAM_PREMATURE_CLOSE') { - error = new AbortError(); + const err = new AbortError(); + err.cause = error; + error = err; } cleanup(); From 2ebc7ff255161de3d1f4d64a78a19c5c51500d51 Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Mon, 26 Jul 2021 21:41:35 +0200 Subject: [PATCH 3/5] fixup --- ...st-whatwg-webstreams-adapters-to-readablewritablepair.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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(); From 1c18eb99f05fd1822166b28af89c6710593ed6a2 Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Tue, 27 Jul 2021 07:47:38 +0200 Subject: [PATCH 4/5] fixup --- lib/internal/webstreams/adapters.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/internal/webstreams/adapters.js b/lib/internal/webstreams/adapters.js index 45c9ab3e356cc3..7718fcc2c6b198 100644 --- a/lib/internal/webstreams/adapters.js +++ b/lib/internal/webstreams/adapters.js @@ -151,7 +151,7 @@ function newWritableStreamFromStreamWritable(streamWritable) { closed = undefined; return; } - controller.error(new ERR_STREAM_PREMATURE_CLOSE()); + controller.error(new AbortError()); controller = undefined; }); @@ -402,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. From 6b90ae917d1671f42cc6b31c8580a2f265379aa4 Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Wed, 28 Jul 2021 08:28:11 +0200 Subject: [PATCH 5/5] fiuxp --- .../test-whatwg-webstreams-adapters-to-readablestream.js | 2 +- .../test-whatwg-webstreams-adapters-to-writablestream.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) 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-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();