mirror of
https://github.com/nodejs/node.git
synced 2026-11-30 11:55:44 -05:00
events: disabled manual construction AbortSignal
Fixes: https://github.com/nodejs/node/issues/36064 PR-URL: https://github.com/nodejs/node/pull/36094 Backport-PR-URL: https://github.com/nodejs/node/pull/38386 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Zeyu Yang <himself65@outlook.com> Reviewed-By: Andrey Pechkurov <apechkurov@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
This commit is contained in:
1 parent
daad5214c4
commit
64cd54be57
2 files changed
+25
-2
No files matched your search
@@ -5,7 +5,9 @@
|
||||
|
||||
const {
|
||||
Object,
|
||||
ObjectSetPrototypeOf,
|
||||
Symbol,
|
||||
TypeError,
|
||||
} = primordials;
|
||||
|
||||
const {
|
||||
@@ -34,6 +36,11 @@ function customInspect(self, obj, depth, options) {
|
||||
}
|
||||
|
||||
class AbortSignal extends EventTarget {
|
||||
constructor() {
|
||||
// eslint-disable-next-line no-restricted-syntax
|
||||
throw new TypeError('Illegal constructor');
|
||||
}
|
||||
|
||||
get aborted() { return !!this[kAborted]; }
|
||||
|
||||
[customInspectSymbol](depth, options) {
|
||||
@@ -49,6 +56,13 @@ Object.defineProperties(AbortSignal.prototype, {
|
||||
|
||||
defineEventHandler(AbortSignal.prototype, 'abort');
|
||||
|
||||
function createAbortSignal() {
|
||||
const signal = new EventTarget();
|
||||
ObjectSetPrototypeOf(signal, AbortSignal.prototype);
|
||||
signal[kAborted] = false;
|
||||
return signal;
|
||||
}
|
||||
|
||||
function abortSignal(signal) {
|
||||
if (signal[kAborted]) return;
|
||||
signal[kAborted] = true;
|
||||
@@ -64,7 +78,7 @@ function abortSignal(signal) {
|
||||
const kSignal = Symbol('signal');
|
||||
class AbortController {
|
||||
constructor() {
|
||||
this[kSignal] = new AbortSignal();
|
||||
this[kSignal] = createAbortSignal();
|
||||
emitExperimentalWarning('AbortController');
|
||||
}
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
const common = require('../common');
|
||||
|
||||
const { ok, strictEqual } = require('assert');
|
||||
const { ok, strictEqual, throws } = require('assert');
|
||||
const { Event } = require('internal/event_target');
|
||||
|
||||
{
|
||||
@@ -52,3 +52,12 @@ const { Event } = require('internal/event_target');
|
||||
strictEqual(firstTrusted, secondTrusted);
|
||||
strictEqual(untrusted, firstTrusted);
|
||||
}
|
||||
|
||||
{
|
||||
// Tests that AbortSignal is impossible to construct manually
|
||||
const ac = new AbortController();
|
||||
throws(
|
||||
() => new ac.signal.constructor(),
|
||||
/^TypeError: Illegal constructor$/
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user