Skip to content

Commit

Permalink
lib: reuse invalid state errors on webstreams
Browse files Browse the repository at this point in the history
Signed-off-by: RafaelGSS <rafael.nunu@hotmail.com>
PR-URL: #46086
Reviewed-By: Robert Nagy <ronagy@icloud.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
  • Loading branch information
RafaelGSS authored and juanarbol committed Jan 25, 2023
1 parent b3802b2 commit 2a7179e
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 10 deletions.
38 changes: 32 additions & 6 deletions lib/internal/webstreams/readablestream.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const {
isArrayBufferDetached,
kEmptyObject,
kEnumerableProperty,
SideEffectFreeRegExpPrototypeSymbolReplace,
} = require('internal/util');

const {
Expand Down Expand Up @@ -140,6 +141,32 @@ const kError = Symbol('kError');
const kPull = Symbol('kPull');
const kRelease = Symbol('kRelease');

let releasedError;
let releasingError;

const userModuleRegExp = /^ {4}at (?:[^/\\(]+ \()(?!node:(.+):\d+:\d+\)$).*/gm;

function lazyReadableReleasedError() {
if (releasedError) {
return releasedError;
}

releasedError = new ERR_INVALID_STATE.TypeError('Reader released');
// Avoid V8 leak and remove userland stackstrace
releasedError.stack = SideEffectFreeRegExpPrototypeSymbolReplace(userModuleRegExp, releasedError.stack, '');
return releasedError;
}

function lazyReadableReleasingError() {
if (releasingError) {
return releasingError;
}
releasingError = new ERR_INVALID_STATE.TypeError('Releasing reader');
// Avoid V8 leak and remove userland stackstrace
releasingError.stack = SideEffectFreeRegExpPrototypeSymbolReplace(userModuleRegExp, releasingError.stack, '');
return releasingError;
}

const getNonWritablePropertyDescriptor = (value) => {
return {
__proto__: null,
Expand Down Expand Up @@ -2029,7 +2056,7 @@ function readableStreamDefaultReaderRelease(reader) {
readableStreamReaderGenericRelease(reader);
readableStreamDefaultReaderErrorReadRequests(
reader,
new ERR_INVALID_STATE.TypeError('Releasing reader')
lazyReadableReleasingError(),
);
}

Expand All @@ -2044,7 +2071,7 @@ function readableStreamBYOBReaderRelease(reader) {
readableStreamReaderGenericRelease(reader);
readableStreamBYOBReaderErrorReadIntoRequests(
reader,
new ERR_INVALID_STATE.TypeError('Releasing reader')
lazyReadableReleasingError(),
);
}

Expand All @@ -2062,13 +2089,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,
};
Expand Down
22 changes: 18 additions & 4 deletions lib/internal/webstreams/writablestream.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const {
createDeferredPromise,
customInspectSymbol: kInspect,
kEnumerableProperty,
SideEffectFreeRegExpPrototypeSymbolReplace,
} = require('internal/util');

const {
Expand Down Expand Up @@ -77,6 +78,20 @@ const kAbort = Symbol('kAbort');
const kCloseSentinel = Symbol('kCloseSentinel');
const kError = Symbol('kError');

let releasedError;

function lazyWritableReleasedError() {
if (releasedError) {
return releasedError;
}
const userModuleRegExp = /^ {4}at (?:[^/\\(]+ \()(?!node:(.+):\d+:\d+\)$).*/gm;

releasedError = new ERR_INVALID_STATE.TypeError('Writer has been released');
// Avoid V8 leak and remove userland stackstrace
releasedError.stack = SideEffectFreeRegExpPrototypeSymbolReplace(userModuleRegExp, releasedError.stack, '');
return releasedError;
}

const getNonWritablePropertyDescriptor = (value) => {
return {
__proto__: null,
Expand Down Expand Up @@ -970,10 +985,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;
}
Expand Down

0 comments on commit 2a7179e

Please sign in to comment.