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
Backport-PR-URL: #38386
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Zeyu Yang <[email protected]>
Reviewed-By: Andrey Pechkurov <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
  • Loading branch information
RaisinTen authored and targos committed Apr 30, 2021
1 parent daad521 commit 64cd54b
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@

const {
Object,
ObjectSetPrototypeOf,
Symbol,
TypeError,
} = primordials;

const {
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
Expand All @@ -64,7 +78,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
Original file line number Diff line number Diff line change
Expand Up @@ -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');

{
Expand Down Expand Up @@ -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$/
);
}

0 comments on commit 64cd54b

Please sign in to comment.