events: type check listeners

Make sure the argument passed is a string. Also use typeof === function
check instead of isArray().
This commit is contained in:
Trevor Norris 2013-02-20 15:33:24 -08:00 committed by isaacs
parent 8ab346c98f
commit aba03ebb5a

View File

@ -288,11 +288,14 @@ EventEmitter.prototype.removeAllListeners = function(type) {
}; };
EventEmitter.prototype.listeners = function(type) { EventEmitter.prototype.listeners = function(type) {
if (!this._events || !this._events[type]) return []; if (typeof type !== 'string')
if (!isArray(this._events[type])) { throw TypeError('event type must be a string');
if (!this._events || !this._events[type])
return [];
if (typeof this._events[type] === 'function')
return [this._events[type]]; return [this._events[type]];
} return this._events[type].slice();
return this._events[type].slice(0);
}; };
EventEmitter.listenerCount = function(emitter, type) { EventEmitter.listenerCount = function(emitter, type) {