Skip to content

Commit 64cd54b

Browse files
RaisinTentargos
authored andcommitted
events: disabled manual construction AbortSignal
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]>
1 parent daad521 commit 64cd54b

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

lib/internal/abort_controller.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55

66
const {
77
Object,
8+
ObjectSetPrototypeOf,
89
Symbol,
10+
TypeError,
911
} = primordials;
1012

1113
const {
@@ -34,6 +36,11 @@ function customInspect(self, obj, depth, options) {
3436
}
3537

3638
class AbortSignal extends EventTarget {
39+
constructor() {
40+
// eslint-disable-next-line no-restricted-syntax
41+
throw new TypeError('Illegal constructor');
42+
}
43+
3744
get aborted() { return !!this[kAborted]; }
3845

3946
[customInspectSymbol](depth, options) {
@@ -49,6 +56,13 @@ Object.defineProperties(AbortSignal.prototype, {
4956

5057
defineEventHandler(AbortSignal.prototype, 'abort');
5158

59+
function createAbortSignal() {
60+
const signal = new EventTarget();
61+
ObjectSetPrototypeOf(signal, AbortSignal.prototype);
62+
signal[kAborted] = false;
63+
return signal;
64+
}
65+
5266
function abortSignal(signal) {
5367
if (signal[kAborted]) return;
5468
signal[kAborted] = true;
@@ -64,7 +78,7 @@ function abortSignal(signal) {
6478
const kSignal = Symbol('signal');
6579
class AbortController {
6680
constructor() {
67-
this[kSignal] = new AbortSignal();
81+
this[kSignal] = createAbortSignal();
6882
emitExperimentalWarning('AbortController');
6983
}
7084

test/parallel/test-abortcontroller.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
const common = require('../common');
55

6-
const { ok, strictEqual } = require('assert');
6+
const { ok, strictEqual, throws } = require('assert');
77
const { Event } = require('internal/event_target');
88

99
{
@@ -52,3 +52,12 @@ const { Event } = require('internal/event_target');
5252
strictEqual(firstTrusted, secondTrusted);
5353
strictEqual(untrusted, firstTrusted);
5454
}
55+
56+
{
57+
// Tests that AbortSignal is impossible to construct manually
58+
const ac = new AbortController();
59+
throws(
60+
() => new ac.signal.constructor(),
61+
/^TypeError: Illegal constructor$/
62+
);
63+
}

0 commit comments

Comments
 (0)