Skip to content

Commit a8330f7

Browse files
Daveevanlucas
authored andcommitted
events: make sure console functions exist
If there's no global console cached, initialize it. Fixes: #4467 PR-URL: #4479 Reviewed-By: Roman Reiss <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 056b078 commit a8330f7

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

‎lib/events.js‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,20 @@ EventEmitter.prototype._maxListeners = undefined;
1818

1919
// By default EventEmitters will print a warning if more than 10 listeners are
2020
// added to it. This is a useful default which helps finding memory leaks.
21-
EventEmitter.defaultMaxListeners=10;
21+
vardefaultMaxListeners=10;
22+
23+
Object.defineProperty(EventEmitter,'defaultMaxListeners',{
24+
enumerable: true,
25+
get: function(){
26+
returndefaultMaxListeners;
27+
},
28+
set: function(arg){
29+
// force global console to be compiled.
30+
// see https://github.com/nodejs/node/issues/4467
31+
console;
32+
defaultMaxListeners=arg;
33+
}
34+
});
2235

2336
EventEmitter.init=function(){
2437
this.domain=null;
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/* eslint-disable required-modules */
2+
// ordinarily test files must require('common') but that action causes
3+
// the global console to be compiled, defeating the purpose of this test
4+
5+
'use strict';
6+
7+
constassert=require('assert');
8+
constEventEmitter=require('events');
9+
constleak_warning=/EventEmittermemoryleakdetected\.2hellolisteners/;
10+
11+
varwrite_calls=0;
12+
process.stderr.write=function(data){
13+
if(write_calls===0)
14+
assert.ok(data.match(leak_warning));
15+
elseif(write_calls===1)
16+
assert.ok(data.match(/Trace/));
17+
else
18+
assert.ok(false,'stderr.write should be called only twice');
19+
20+
write_calls++;
21+
};
22+
23+
constold_default=EventEmitter.defaultMaxListeners;
24+
EventEmitter.defaultMaxListeners=1;
25+
26+
conste=newEventEmitter();
27+
e.on('hello',function(){});
28+
e.on('hello',function(){});
29+
30+
// TODO: figure out how to validate console. Currently,
31+
// there is no obvious way of validating that console
32+
// exists here exactly when it should.
33+
34+
assert.equal(write_calls,2);
35+
36+
EventEmitter.defaultMaxListeners=old_default;

0 commit comments

Comments
(0)