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 <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>
1 parent f7b2fce commit 1550073

2 files changed

Lines changed: 25 additions & 2 deletions

File tree

‎lib/internal/abort_controller.js‎

Lines changed: 15 additions & 1 deletion
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
classAbortSignalextendsEventTarget{
40+
constructor(){
41+
// eslint-disable-next-line no-restricted-syntax
42+
thrownewTypeError('Illegal constructor');
43+
}
44+
3845
getaborted(){return!!this[kAborted];}
3946

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

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

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

‎test/parallel/test-abortcontroller.js‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
constcommon=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+
constac=newAbortController();
58+
throws(
59+
()=>newac.signal.constructor(),
60+
/^TypeError:Illegalconstructor$/
61+
);
62+
}

0 commit comments

Comments
 (0)