mirror of
https://github.com/zebrajr/node.git
synced 2026-01-15 12:15:26 +00:00
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:
@@ -150,8 +150,9 @@ class AbortSignal extends EventTarget {
|
||||
}
|
||||
|
||||
throwIfAborted() {
|
||||
if (this.aborted) {
|
||||
throw this.reason;
|
||||
validateThisAbortSignal(this);
|
||||
if (this[kAborted]) {
|
||||
throw this[kReason];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user