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 authored and GitHub committed 2023-02-08 13:29:09 +00:00
1 parent da89ab8545
commit 8844cc42e7
2 files changed
+20 -2

No files matched your search

+3 -2
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];
}
}
+17
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);
}