-
Notifications
You must be signed in to change notification settings - Fork 30.5k
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: prevent undefined as an event name #21007
Conversation
This commit prevents undefined as an event name in addListener(), removeListener(), and emit().
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Big 💙 for keeping this change in behaviour minimal. :)
If according to the message:
Why not restrict to that first thing in the affected methods instead of keying in on |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be good to also validate null
and objects. Both of them will clearly not behave as expected.
function validateEventName(name, paramName) { | ||
if (name === undefined) { | ||
const { ERR_INVALID_ARG_TYPE } = lazyErrors(); | ||
const err = new ERR_INVALID_ARG_TYPE(paramName, ['string', 'symbol'], name); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
are there other possible paramNames? Otherwise I think it would be best to inline that string.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the guard is to ensure the "eventName" argument must be one of type string or symbol then it should be applied to more than just undefined
. Blocking just because there are a couple approvals and I think it needs a bit more work.
@cjihrig I’m sorry you closed this. Would you mind if I picked it up again? I think this is strictly an improvement, so I’d like to see it make its way into Node and blocking it seems inappropriate to me. |
This commit prevents undefined as an event name in
addListener()
,removeListener()
, andemit()
.Refs: #20923
A few things to note:
undefined
as an event name is almost certainly a mistake. I considered givingnull
the same treatment, but decided not to as a first draft.'eventName'
input must be a string or symbol. Our documentation calls the parameter'eventName'
, even though the code doesn't. The docs also call for a string or symbol, which simply isn't true.emit()
. I considered adding validation to the methods that retrieve listeners, but I'm not sure that's necessary since they are only querying state, not modifying it.Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passes