From 4540f14305faaaa2eaf585bf9376cbdef8b8e8f1 Mon Sep 17 00:00:00 2001 From: RafaelGSS Date: Tue, 3 Jan 2023 19:35:09 -0300 Subject: [PATCH] lib: reuse invalid state errors on webstreams Signed-off-by: RafaelGSS --- lib/internal/webstreams/readablestream.js | 27 ++++++++++++++++++----- lib/internal/webstreams/writablestream.js | 16 ++++++++++---- 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/lib/internal/webstreams/readablestream.js b/lib/internal/webstreams/readablestream.js index 5344785b90cd3e..7a2c8bccd43396 100644 --- a/lib/internal/webstreams/readablestream.js +++ b/lib/internal/webstreams/readablestream.js @@ -37,6 +37,7 @@ const { ERR_INVALID_STATE, ERR_INVALID_THIS, }, + hideStackFrames, } = require('internal/errors'); const { @@ -140,6 +141,21 @@ const kError = Symbol('kError'); const kPull = Symbol('kPull'); const kRelease = Symbol('kRelease'); +let releasedError; +let releasingError; + +const lazyReadableReleasedError = hideStackFrames(() => { + releasedError ??= new ERR_INVALID_STATE.TypeError('Reader released'); + releasedError.stack; + return releasedError; +}); + +const lazyReadableReleasingError = hideStackFrames(() => { + releasingError ??= new ERR_INVALID_STATE.TypeError('Releasing reader'); + releasingError.stack; + return releasingError; +}); + const getNonWritablePropertyDescriptor = (value) => { return { __proto__: null, @@ -2029,7 +2045,7 @@ function readableStreamDefaultReaderRelease(reader) { readableStreamReaderGenericRelease(reader); readableStreamDefaultReaderErrorReadRequests( reader, - new ERR_INVALID_STATE.TypeError('Releasing reader') + lazyReadableReleasingError(), ); } @@ -2044,7 +2060,7 @@ function readableStreamBYOBReaderRelease(reader) { readableStreamReaderGenericRelease(reader); readableStreamBYOBReaderErrorReadIntoRequests( reader, - new ERR_INVALID_STATE.TypeError('Releasing reader') + lazyReadableReleasingError(), ); } @@ -2062,13 +2078,12 @@ function readableStreamReaderGenericRelease(reader) { assert(stream !== undefined); assert(stream[kState].reader === reader); + const releasedStateError = lazyReadableReleasedError(); if (stream[kState].state === 'readable') { - reader[kState].close.reject?.( - new ERR_INVALID_STATE.TypeError('Reader released')); + reader[kState].close.reject?.(releasedStateError); } else { reader[kState].close = { - promise: PromiseReject( - new ERR_INVALID_STATE.TypeError('Reader released')), + promise: PromiseReject(releasedStateError), resolve: undefined, reject: undefined, }; diff --git a/lib/internal/webstreams/writablestream.js b/lib/internal/webstreams/writablestream.js index ba66cea7a4850d..157cb55aadd8de 100644 --- a/lib/internal/webstreams/writablestream.js +++ b/lib/internal/webstreams/writablestream.js @@ -24,6 +24,7 @@ const { ERR_INVALID_STATE, ERR_INVALID_THIS, }, + hideStackFrames, } = require('internal/errors'); const { @@ -77,6 +78,14 @@ const kAbort = Symbol('kAbort'); const kCloseSentinel = Symbol('kCloseSentinel'); const kError = Symbol('kError'); +let releasedError; + +const lazyWritableReleasedError = hideStackFrames(() => { + releasedError ??= new ERR_INVALID_STATE.TypeError('Writer has been released'); + releasedError.stack; + return releasedError; +}); + const getNonWritablePropertyDescriptor = (value) => { return { __proto__: null, @@ -970,10 +979,9 @@ function writableStreamDefaultWriterRelease(writer) { } = writer[kState]; assert(stream !== undefined); assert(stream[kState].writer === writer); - const releasedError = - new ERR_INVALID_STATE.TypeError('Writer has been released'); - writableStreamDefaultWriterEnsureReadyPromiseRejected(writer, releasedError); - writableStreamDefaultWriterEnsureClosedPromiseRejected(writer, releasedError); + const releasedStateError = lazyWritableReleasedError(); + writableStreamDefaultWriterEnsureReadyPromiseRejected(writer, releasedStateError); + writableStreamDefaultWriterEnsureClosedPromiseRejected(writer, releasedStateError); stream[kState].writer = undefined; writer[kState].stream = undefined; }