events: disabled manual construction AbortSignal

Fixes: https://github.com/nodejs/node/issues/36064

PR-URL: https://github.com/nodejs/node/pull/36094
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:
raisinten 2020-11-12 19:56:06 +05:30 committed by Node.js GitHub Bot
parent 1d02a35869
commit 22293eab48
2 changed files with 25 additions and 2 deletions

View File

@ -6,7 +6,9 @@
const {
ObjectAssign,
ObjectDefineProperties,
ObjectSetPrototypeOf,
Symbol,
TypeError,
} = primordials;
const {
@ -35,6 +37,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) {
@ -50,6 +57,13 @@ ObjectDefineProperties(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;
@ -65,7 +79,7 @@ function abortSignal(signal) {
const kSignal = Symbol('signal');
class AbortController {
constructor() {
this[kSignal] = new AbortSignal();
this[kSignal] = createAbortSignal();
emitExperimentalWarning('AbortController');
}

View File

@ -3,7 +3,7 @@
const common = require('../common');
const { ok, strictEqual } = require('assert');
const { ok, strictEqual, throws } = require('assert');
{
// Tests that abort is fired with the correct event type on AbortControllers
@ -51,3 +51,12 @@ const { ok, strictEqual } = require('assert');
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$/
);
}