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, doc: add type checking in defaultMaxListeners getter #11938

Closed
wants to merge 1 commit into
base: master
from

Conversation

Projects
None yet
8 participants
@DavidCai1993
Member

DavidCai1993 commented Mar 20, 2017

Since EventEmitter.defaultMaxListeners and emitter.setMaxListeners(n) are both public APIs listed in the document, so it is reasonable to check EventEmitter.defaultMaxListeners getter's input like what emitter.setMaxListeners(n) does to avoid potential confusing results or error messages caused by some userland mistaken input, like:

'use strict'
const E = require('events')

E.defaultMaxListeners = new Proxy({}, { get: () => {} })
let e = new E()
e.addListener('1', function () {})
e.addListener('1', function () {})
// output:
// events.js:260
//      if (m && m > 0 && existing.length > m) {
//                 ^
//
// TypeError: Cannot convert object to primitive value
//     at _addListener (events.js:260:18)
//     at EventEmitter.addListener (events.js:279:10)
//     at Object.<anonymous> (/Users/caiwei/workspace/benchmarks/index.js:7:3)
//     at Module._compile (module.js:571:32)
//     at Object.Module._extensions..js (module.js:580:10)
//     at Module.load (module.js:488:32)
//     at tryModuleLoad (module.js:447:12)
//     at Function.Module._load (module.js:439:3)
//     at Module.runMain (module.js:605:10)
//     at run (bootstrap_node.js:425:7)
Checklist
  • [-] make -j4 test (UNIX), or vcbuild test (Windows) passes
  • [-] tests and/or benchmarks are included
  • [-] documentation is changed or added
  • [-] commit message follows commit guidelines
Affected core subsystem(s)

events, doc

Show outdated Hide outdated lib/events.js
// force global console to be compiled.
// see https://github.com/nodejs/node/issues/4467
console;
defaultMaxListeners = arg;
if (typeof n !== 'number' || n < 0 || isNaN(n))

This comment has been minimized.

@mscdex

mscdex Mar 20, 2017

Contributor

Could we replace isNaN(n) with n !== n, possibly with a comment with a short explanation?

@mscdex

mscdex Mar 20, 2017

Contributor

Could we replace isNaN(n) with n !== n, possibly with a comment with a short explanation?

This comment has been minimized.

@vsemozhetbyt

vsemozhetbyt Mar 20, 2017

Member

@mscdex Is this similar place also worth replacing then?

@vsemozhetbyt

vsemozhetbyt Mar 20, 2017

Member

@mscdex Is this similar place also worth replacing then?

This comment has been minimized.

@mscdex

mscdex Mar 20, 2017

Contributor

Probably, but as a separate commit I'd say.

@mscdex

mscdex Mar 20, 2017

Contributor

Probably, but as a separate commit I'd say.

This comment has been minimized.

@DavidCai1993
@DavidCai1993
@DavidCai1993

This comment has been minimized.

Show comment
Hide comment
@DavidCai1993

DavidCai1993 Mar 20, 2017

Member

@vsemozhetbyt Thanks for reminding, changed it to a shorter one :)

Member

DavidCai1993 commented Mar 20, 2017

@vsemozhetbyt Thanks for reminding, changed it to a shorter one :)

@vsemozhetbyt

This comment has been minimized.

Show comment
Hide comment
@vsemozhetbyt

This comment has been minimized.

Show comment
Hide comment
Show outdated Hide outdated lib/events.js
// force global console to be compiled.
// see https://github.com/nodejs/node/issues/4467
console;
defaultMaxListeners = arg;
// check whether the input is a positive number (which value is
// larger than zero and not a NaN).

This comment has been minimized.

@mscdex

mscdex Mar 20, 2017

Contributor

'which value is larger than zero' should probably instead be something like 'whose value is zero or greater'

@mscdex

mscdex Mar 20, 2017

Contributor

'which value is larger than zero' should probably instead be something like 'whose value is zero or greater'

@cjihrig

LGTM with a couple nits.

Show outdated Hide outdated doc/api/events.md
@@ -262,7 +262,8 @@ By default, a maximum of `10` listeners can be registered for any single
event. This limit can be changed for individual `EventEmitter` instances
using the [`emitter.setMaxListeners(n)`][] method. To change the default
for *all* `EventEmitter` instances, the `EventEmitter.defaultMaxListeners`
property can be used.
property can be used. If trying to set it to a value which is not a positive

This comment has been minimized.

@cjihrig

cjihrig Mar 20, 2017

Contributor

Can I suggest rewording this as:

If this value is not a positive number, a TypeError will be thrown.

@cjihrig

cjihrig Mar 20, 2017

Contributor

Can I suggest rewording this as:

If this value is not a positive number, a TypeError will be thrown.

Show outdated Hide outdated lib/events.js
@@ -52,11 +52,15 @@ Object.defineProperty(EventEmitter, 'defaultMaxListeners', {
get: function() {
return defaultMaxListeners;
},
set: function(arg) {
set: function(n) {

This comment has been minimized.

@cjihrig

cjihrig Mar 20, 2017

Contributor

Changing this variable name seems unnecessary.

@cjihrig

cjihrig Mar 20, 2017

Contributor

Changing this variable name seems unnecessary.

@jasnell

LGTM with nits addressed

@lpinca

lpinca approved these changes Mar 20, 2017

@DavidCai1993

This comment has been minimized.

Show comment
Hide comment
@DavidCai1993

DavidCai1993 Mar 21, 2017

Member

@mscdex @cjihrig Thanks for reviewing. Just updated, PTAL. 😃

Member

DavidCai1993 commented Mar 21, 2017

@mscdex @cjihrig Thanks for reviewing. Just updated, PTAL. 😃

@mscdex

This comment has been minimized.

Show comment
Hide comment
@jasnell

This comment has been minimized.

Show comment
Hide comment
@jasnell

jasnell Mar 22, 2017

Member

@mscdex ... does this LGTY?

Member

jasnell commented Mar 22, 2017

@mscdex ... does this LGTY?

// check whether the input is a positive number (whose value is zero or
// greater and not a NaN).
if (typeof arg !== 'number' || arg < 0 || arg !== arg)
throw new TypeError('defaultMaxListeners must be a positive number');

This comment has been minimized.

@mscdex

mscdex Mar 22, 2017

Contributor

'defaultMaxListeners' should be enclosed in double quotes to match the style when referencing property names in error messages.

@mscdex

mscdex Mar 22, 2017

Contributor

'defaultMaxListeners' should be enclosed in double quotes to match the style when referencing property names in error messages.

@mscdex

This comment has been minimized.

Show comment
Hide comment
@mscdex

mscdex Mar 22, 2017

Contributor

One minor nit, but otherwise LGTM.

Contributor

mscdex commented Mar 22, 2017

One minor nit, but otherwise LGTM.

@jasnell

This comment has been minimized.

Show comment
Hide comment
@jasnell

jasnell Mar 22, 2017

Member

I can take care of that nit while landing

Member

jasnell commented Mar 22, 2017

I can take care of that nit while landing

jasnell added a commit that referenced this pull request Mar 22, 2017

events, doc: check input in defaultMaxListeners
PR-URL: #11938
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Yuta Hiroto <hello@about-hiroppy.com>
Reviewed-By: Brian White <mscdex@mscdex.net>
@jasnell

This comment has been minimized.

Show comment
Hide comment
@jasnell

jasnell Mar 22, 2017

Member

Landed in 221b03a

Member

jasnell commented Mar 22, 2017

Landed in 221b03a

@jasnell jasnell closed this Mar 22, 2017

@jasnell jasnell referenced this pull request Apr 4, 2017

Closed

8.0.0 Release Proposal #12220

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment