lib: tighten AbortSignal.prototype.throwIfAborted implementation

PR-URL: https://github.com/nodejs/node/pull/46521
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
This commit is contained in:
Antoine du Hamel
2023-02-08 14:29:09 +01:00
committed by GitHub
parent da89ab8545
commit 8844cc42e7
2 changed files with 20 additions and 2 deletions

View File

@@ -150,8 +150,9 @@ class AbortSignal extends EventTarget {
}
throwIfAborted() {
if (this.aborted) {
throw this.reason;
validateThisAbortSignal(this);
if (this[kAborted]) {
throw this[kReason];
}
}

View File

@@ -254,3 +254,20 @@ const { setTimeout: sleep } = require('timers/promises');
const ac = new AbortController();
ac.signal.throwIfAborted();
}
{
const originalDesc = Reflect.getOwnPropertyDescriptor(AbortSignal.prototype, 'aborted');
const actualReason = new Error();
Reflect.defineProperty(AbortSignal.prototype, 'aborted', { value: false });
throws(() => AbortSignal.abort(actualReason).throwIfAborted(), actualReason);
Reflect.defineProperty(AbortSignal.prototype, 'aborted', originalDesc);
}
{
const originalDesc = Reflect.getOwnPropertyDescriptor(AbortSignal.prototype, 'reason');
const actualReason = new Error();
const fakeExcuse = new Error();
Reflect.defineProperty(AbortSignal.prototype, 'reason', { value: fakeExcuse });
throws(() => AbortSignal.abort(actualReason).throwIfAborted(), actualReason);
Reflect.defineProperty(AbortSignal.prototype, 'reason', originalDesc);
}