Skip to content

Commit d5c29ba

Browse files
authoredSep 17, 2024··
events: set EventEmitterAsyncResource fields private
PR-URL: #54889 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
1 parent 8191e1f commit d5c29ba

File tree

2 files changed

+16
-30
lines changed

2 files changed

+16
-30
lines changed
 

Diff for: ‎lib/events.js

+12-24
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ const {
6767
AbortError,
6868
codes: {
6969
ERR_INVALID_ARG_TYPE,
70-
ERR_INVALID_THIS,
7170
ERR_UNHANDLED_ERROR,
7271
},
7372
genericNodeError,
@@ -106,9 +105,9 @@ function lazyEventEmitterAsyncResource() {
106105
AsyncResource,
107106
} = require('async_hooks');
108107

109-
const kEventEmitter = Symbol('kEventEmitter');
110-
const kAsyncResource = Symbol('kAsyncResource');
111108
class EventEmitterReferencingAsyncResource extends AsyncResource {
109+
#eventEmitter;
110+
112111
/**
113112
* @param {EventEmitter} ee
114113
* @param {string} [type]
@@ -119,21 +118,21 @@ function lazyEventEmitterAsyncResource() {
119118
*/
120119
constructor(ee, type, options) {
121120
super(type, options);
122-
this[kEventEmitter] = ee;
121+
this.#eventEmitter = ee;
123122
}
124123

125124
/**
126125
* @type {EventEmitter}
127126
*/
128127
get eventEmitter() {
129-
if (this[kEventEmitter] === undefined)
130-
throw new ERR_INVALID_THIS('EventEmitterReferencingAsyncResource');
131-
return this[kEventEmitter];
128+
return this.#eventEmitter;
132129
}
133130
}
134131

135132
EventEmitterAsyncResource =
136133
class EventEmitterAsyncResource extends EventEmitter {
134+
#asyncResource;
135+
137136
/**
138137
* @param {{
139138
* name?: string,
@@ -154,8 +153,7 @@ function lazyEventEmitterAsyncResource() {
154153
}
155154
super(options);
156155

157-
this[kAsyncResource] =
158-
new EventEmitterReferencingAsyncResource(this, name, options);
156+
this.#asyncResource = new EventEmitterReferencingAsyncResource(this, name, options);
159157
}
160158

161159
/**
@@ -164,9 +162,7 @@ function lazyEventEmitterAsyncResource() {
164162
* @returns {boolean}
165163
*/
166164
emit(event, ...args) {
167-
if (this[kAsyncResource] === undefined)
168-
throw new ERR_INVALID_THIS('EventEmitterAsyncResource');
169-
const { asyncResource } = this;
165+
const asyncResource = this.#asyncResource;
170166
ArrayPrototypeUnshift(args, super.emit, this, event);
171167
return ReflectApply(asyncResource.runInAsyncScope, asyncResource,
172168
args);
@@ -176,36 +172,28 @@ function lazyEventEmitterAsyncResource() {
176172
* @returns {void}
177173
*/
178174
emitDestroy() {
179-
if (this[kAsyncResource] === undefined)
180-
throw new ERR_INVALID_THIS('EventEmitterAsyncResource');
181-
this.asyncResource.emitDestroy();
175+
this.#asyncResource.emitDestroy();
182176
}
183177

184178
/**
185179
* @type {number}
186180
*/
187181
get asyncId() {
188-
if (this[kAsyncResource] === undefined)
189-
throw new ERR_INVALID_THIS('EventEmitterAsyncResource');
190-
return this.asyncResource.asyncId();
182+
return this.#asyncResource.asyncId();
191183
}
192184

193185
/**
194186
* @type {number}
195187
*/
196188
get triggerAsyncId() {
197-
if (this[kAsyncResource] === undefined)
198-
throw new ERR_INVALID_THIS('EventEmitterAsyncResource');
199-
return this.asyncResource.triggerAsyncId();
189+
return this.#asyncResource.triggerAsyncId();
200190
}
201191

202192
/**
203193
* @type {EventEmitterReferencingAsyncResource}
204194
*/
205195
get asyncResource() {
206-
if (this[kAsyncResource] === undefined)
207-
throw new ERR_INVALID_THIS('EventEmitterAsyncResource');
208-
return this[kAsyncResource];
196+
return this.#asyncResource;
209197
}
210198
};
211199
}

Diff for: ‎test/parallel/test-eventemitter-asyncresource.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -132,24 +132,22 @@ function makeHook(trackedTypes) {
132132
]));
133133
})().then(common.mustCall());
134134

135-
// Member methods ERR_INVALID_THIS
136135
throws(
137136
() => EventEmitterAsyncResource.prototype.emit(),
138-
{ code: 'ERR_INVALID_THIS' }
137+
{ name: 'TypeError', message: /Cannot read private member/ }
139138
);
140139

141140
throws(
142141
() => EventEmitterAsyncResource.prototype.emitDestroy(),
143-
{ code: 'ERR_INVALID_THIS' }
142+
{ name: 'TypeError', message: /Cannot read private member/ }
144143
);
145144

146145
['asyncId', 'triggerAsyncId', 'asyncResource'].forEach((getter) => {
147146
throws(
148147
() => Reflect.get(EventEmitterAsyncResource.prototype, getter, {}),
149148
{
150-
code: 'ERR_INVALID_THIS',
151-
name: /TypeError/,
152-
message: 'Value of "this" must be of type EventEmitterAsyncResource',
149+
name: 'TypeError',
150+
message: /Cannot read private member/,
153151
stack: new RegExp(`at get ${getter}`),
154152
}
155153
);

0 commit comments

Comments
 (0)
Please sign in to comment.