Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

events: set EventEmitterAsyncResource fields private #54889

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 12 additions & 24 deletions lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ const {
AbortError,
codes: {
ERR_INVALID_ARG_TYPE,
ERR_INVALID_THIS,
ERR_UNHANDLED_ERROR,
},
genericNodeError,
Expand Down Expand Up @@ -106,9 +105,9 @@ function lazyEventEmitterAsyncResource() {
AsyncResource,
} = require('async_hooks');

const kEventEmitter = Symbol('kEventEmitter');
const kAsyncResource = Symbol('kAsyncResource');
class EventEmitterReferencingAsyncResource extends AsyncResource {
#eventEmitter;

/**
* @param {EventEmitter} ee
* @param {string} [type]
Expand All @@ -119,21 +118,21 @@ function lazyEventEmitterAsyncResource() {
*/
constructor(ee, type, options) {
super(type, options);
this[kEventEmitter] = ee;
this.#eventEmitter = ee;
}

/**
* @type {EventEmitter}
*/
get eventEmitter() {
if (this[kEventEmitter] === undefined)
throw new ERR_INVALID_THIS('EventEmitterReferencingAsyncResource');
return this[kEventEmitter];
return this.#eventEmitter;
}
}

EventEmitterAsyncResource =
class EventEmitterAsyncResource extends EventEmitter {
#asyncResource;

/**
* @param {{
* name?: string,
Expand All @@ -154,8 +153,7 @@ function lazyEventEmitterAsyncResource() {
}
super(options);

this[kAsyncResource] =
new EventEmitterReferencingAsyncResource(this, name, options);
this.#asyncResource = new EventEmitterReferencingAsyncResource(this, name, options);
}

/**
Expand All @@ -164,9 +162,7 @@ function lazyEventEmitterAsyncResource() {
* @returns {boolean}
*/
emit(event, ...args) {
if (this[kAsyncResource] === undefined)
throw new ERR_INVALID_THIS('EventEmitterAsyncResource');
const { asyncResource } = this;
const asyncResource = this.#asyncResource;
ArrayPrototypeUnshift(args, super.emit, this, event);
return ReflectApply(asyncResource.runInAsyncScope, asyncResource,
args);
Expand All @@ -176,36 +172,28 @@ function lazyEventEmitterAsyncResource() {
* @returns {void}
*/
emitDestroy() {
if (this[kAsyncResource] === undefined)
throw new ERR_INVALID_THIS('EventEmitterAsyncResource');
this.asyncResource.emitDestroy();
this.#asyncResource.emitDestroy();
}

/**
* @type {number}
*/
get asyncId() {
if (this[kAsyncResource] === undefined)
throw new ERR_INVALID_THIS('EventEmitterAsyncResource');
return this.asyncResource.asyncId();
return this.#asyncResource.asyncId();
}

/**
* @type {number}
*/
get triggerAsyncId() {
if (this[kAsyncResource] === undefined)
throw new ERR_INVALID_THIS('EventEmitterAsyncResource');
return this.asyncResource.triggerAsyncId();
return this.#asyncResource.triggerAsyncId();
}

/**
* @type {EventEmitterReferencingAsyncResource}
*/
get asyncResource() {
if (this[kAsyncResource] === undefined)
throw new ERR_INVALID_THIS('EventEmitterAsyncResource');
return this[kAsyncResource];
return this.#asyncResource;
}
};
}
Expand Down
10 changes: 4 additions & 6 deletions test/parallel/test-eventemitter-asyncresource.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,24 +132,22 @@ function makeHook(trackedTypes) {
]));
})().then(common.mustCall());

// Member methods ERR_INVALID_THIS
throws(
() => EventEmitterAsyncResource.prototype.emit(),
{ code: 'ERR_INVALID_THIS' }
{ name: 'TypeError', message: /Cannot read private member/ }
);

throws(
() => EventEmitterAsyncResource.prototype.emitDestroy(),
{ code: 'ERR_INVALID_THIS' }
{ name: 'TypeError', message: /Cannot read private member/ }
);

['asyncId', 'triggerAsyncId', 'asyncResource'].forEach((getter) => {
throws(
() => Reflect.get(EventEmitterAsyncResource.prototype, getter, {}),
{
code: 'ERR_INVALID_THIS',
name: /TypeError/,
message: 'Value of "this" must be of type EventEmitterAsyncResource',
name: 'TypeError',
message: /Cannot read private member/,
stack: new RegExp(`at get ${getter}`),
}
);
Expand Down
Loading