Skip to content

Commit 1550073

Browse files
RaisinTencodebytere
authored andcommitted
events: disabled manual construction AbortSignal
Fixes: #36064 PR-URL: #36094 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 f7b2fce commit 1550073

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
@@ -6,7 +6,9 @@
66
const {
77
ObjectAssign,
88
ObjectDefineProperties,
9+
ObjectSetPrototypeOf,
910
Symbol,
11+
TypeError,
1012
} = primordials;
1113

1214
const {
@@ -35,6 +37,11 @@ function customInspect(self, obj, depth, options) {
3537
}
3638

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

4047
[customInspectSymbol](depth, options) {
@@ -50,6 +57,13 @@ ObjectDefineProperties(AbortSignal.prototype, {
5057

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

60+
function createAbortSignal() {
61+
const signal = new EventTarget();
62+
ObjectSetPrototypeOf(signal, AbortSignal.prototype);
63+
signal[kAborted] = false;
64+
return signal;
65+
}
66+
5367
function abortSignal(signal) {
5468
if (signal[kAborted]) return;
5569
signal[kAborted] = true;
@@ -65,7 +79,7 @@ function abortSignal(signal) {
6579
const kSignal = Symbol('signal');
6680
class AbortController {
6781
constructor() {
68-
this[kSignal] = new AbortSignal();
82+
this[kSignal] = createAbortSignal();
6983
emitExperimentalWarning('AbortController');
7084
}
7185

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

88
{
99
// Tests that abort is fired with the correct event type on AbortControllers
@@ -51,3 +51,12 @@ const { ok, strictEqual } = require('assert');
5151
strictEqual(firstTrusted, secondTrusted);
5252
strictEqual(untrusted, firstTrusted);
5353
}
54+
55+
{
56+
// Tests that AbortSignal is impossible to construct manually
57+
const ac = new AbortController();
58+
throws(
59+
() => new ac.signal.constructor(),
60+
/^TypeError: Illegal constructor$/
61+
);
62+
}

0 commit comments

Comments
 (0)