Skip to content

Commit

Permalink
events: disabled manual construction AbortSignal
Browse files Browse the repository at this point in the history
Fixes: #36064

PR-URL: #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>
  • Loading branch information
RaisinTen authored and nodejs-github-bot committed Nov 14, 2020
1 parent 1d02a35 commit 22293ea
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
16 changes: 15 additions & 1 deletion lib/internal/abort_controller.js
Expand Up @@ -6,7 +6,9 @@
const {
ObjectAssign,
ObjectDefineProperties,
ObjectSetPrototypeOf,
Symbol,
TypeError,
} = primordials;

const {
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
Expand All @@ -65,7 +79,7 @@ function abortSignal(signal) {
const kSignal = Symbol('signal');
class AbortController {
constructor() {
this[kSignal] = new AbortSignal();
this[kSignal] = createAbortSignal();
emitExperimentalWarning('AbortController');
}

Expand Down
11 changes: 10 additions & 1 deletion test/parallel/test-abortcontroller.js
Expand Up @@ -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
Expand Down Expand Up @@ -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$/
);
}

0 comments on commit 22293ea

Please sign in to comment.