From 1550073dbc2f908bd9f4bed198e3fa2f30893ea0 Mon Sep 17 00:00:00 2001 From: raisinten Date: Thu, 12 Nov 2020 19:56:06 +0530 Subject: [PATCH] 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 Reviewed-By: Benjamin Gruenbaum Reviewed-By: Luigi Pinca Reviewed-By: Zeyu Yang Reviewed-By: Andrey Pechkurov Reviewed-By: Rich Trott --- lib/internal/abort_controller.js | 16 +++++++++++++++- test/parallel/test-abortcontroller.js | 11 ++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/lib/internal/abort_controller.js b/lib/internal/abort_controller.js index b387e5f4e2cea0..37001dc733982b 100644 --- a/lib/internal/abort_controller.js +++ b/lib/internal/abort_controller.js @@ -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'); } diff --git a/test/parallel/test-abortcontroller.js b/test/parallel/test-abortcontroller.js index 8910ac7155762b..673a774ef148c3 100644 --- a/test/parallel/test-abortcontroller.js +++ b/test/parallel/test-abortcontroller.js @@ -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$/ + ); +}