Security News
GitHub Removes Malicious Pull Requests Targeting Open Source Repositories
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
eventemitter2
Advanced tools
A Node.js event emitter implementation with namespaces, wildcards, TTL and browser support.
The eventemitter2 npm package is an implementation of the EventEmitter module found in Node.js. It provides an interface for implementing event-driven architecture. It allows you to create objects that can emit named events that cause function objects ('listeners') to be called. It offers several enhancements over the native EventEmitter, such as namespaces, wildcards, and the ability to listen to all events.
Event Emitting
This feature allows you to emit events and register listeners that get called when those events are emitted.
const EventEmitter2 = require('eventemitter2').EventEmitter2;
const emitter = new EventEmitter2();
emitter.on('event', function() {
console.log('an event occurred!');
});
emitter.emit('event');
Namespaces/Wildcards
This feature enables the use of namespaces and wildcards for event names, allowing for more flexible event handling.
const EventEmitter2 = require('eventemitter2').EventEmitter2;
const emitter = new EventEmitter2({
wildcard: true
});
emitter.on('foo.*', function() {
console.log('foo event occurred!');
});
emitter.emit('foo.bar');
Listening to All Events
This feature allows you to listen to all events that are emitted from an EventEmitter2 instance.
const EventEmitter2 = require('eventemitter2').EventEmitter2;
const emitter = new EventEmitter2();
emitter.onAny(function(event, value) {
console.log('All event handler:', event, value);
});
emitter.emit('randomEvent', 'with some value');
The 'events' package is the original EventEmitter that comes with Node.js. It provides the basic functionality for event emitting and listening but lacks some of the advanced features of eventemitter2, such as namespaces and wildcards.
Mitt is a tiny functional event emitter / pubsub. It provides a similar event-driven approach but with a smaller footprint and without namespaces or wildcard features, focusing on simplicity and performance.
This is another EventEmitter implementation with a similar API to eventemitter2. It offers additional features like context binding and once listeners, but it does not support namespaces or wildcards.
EventEmitter2 is an implementation of the EventEmitter module found in Node.js. In addition to having a better benchmark performance than EventEmitter and being browser-compatible, it also extends the interface of EventEmitter with additional non-breaking features.
once
concept with many
.EventEmitterHeatUp x 3,728,965 ops/sec \302\2610.68% (60 runs sampled)
EventEmitter x 2,822,904 ops/sec \302\2610.74% (63 runs sampled)
EventEmitter2 x 7,251,227 ops/sec \302\2610.55% (58 runs sampled)
EventEmitter2 (wild) x 3,220,268 ops/sec \302\2610.44% (65 runs sampled)
Fastest is EventEmitter2
var EventEmitter2 = require('eventemitter2').EventEmitter2;
var server = new EventEmitter2({
//
// set this to `true` to use wildcards. It defaults to `false`.
//
wildcard: true,
//
// the delimiter used to segment namespaces, defaults to `.`.
//
delimiter: '::',
//
// set this to `true` if you want to emit the newListener event. The default value is `true`.
//
newListener: false,
//
// the maximum amount of listeners that can be assigned to an event, default 10.
//
maxListeners: 20,
//
// show event name in memory leak message when more than maximum amount of listeners is assigned, default false
//
verboseMemoryLeak: false
});
server.on('foo.*', function(value1, value2) {
console.log(this.event, value1, value2);
});
once
concept. server.many('foo', 4, function() {
console.log('hello');
});
server.many(['foo', 'bar', 'bazz'], 4, function() {
console.log('hello');
});
npm install --save eventemitter2
.
When an EventEmitter
instance experiences an error, the typical action is
to emit an error
event. Error events are treated as a special case.
If there is no listener for it, then the default action is to print a stack
trace and exit the program.
All EventEmitters emit the event newListener
when new listeners are
added. EventEmitters also emit the event removeListener
when listeners are
removed, and removeListenerAny
when listeners added through onAny
are
removed.
Namespaces with Wildcards
To use namespaces/wildcards, pass the wildcard
option into the EventEmitter
constructor. When namespaces/wildcards are enabled, events can either be
strings (foo.bar
) separated by a delimiter or arrays (['foo', 'bar']
). The
delimiter is also configurable as a constructor option.
An event name passed to any event emitter method can contain a wild card (the
*
character). If the event name is a string, a wildcard may appear as foo.*
.
If the event name is an array, the wildcard may appear as ['foo', '*']
.
If either of the above described events were passed to the on
method,
subsequent emits such as the following would be observed...
emitter.emit('foo.bazz');
emitter.emit(['foo', 'bar']);
A double wildcard (the string **
) matches any number of levels (zero or more) of events. So if for example 'foo.**'
is passed to the on
method, the following events would be observed:
emitter.emit('foo');
emitter.emit('foo.bar');
emitter.emit('foo.bar.baz');
On the other hand, if the single-wildcard event name was passed to the on method, the callback would only observe the second of these events.
Adds a listener to the end of the listeners array for the specified event.
server.on('data', function(value1, value2, value3, ...) {
console.log('The event was raised!');
});
server.on('data', function(value) {
console.log('The event was raised!');
});
Adds a listener to the beginning of the listeners array for the specified event.
server.prependListener('data', function(value1, value2, value3, ...) {
console.log('The event was raised!');
});
Adds a listener that will be fired when any event is emitted. The event name is passed as the first argument to the callback.
server.onAny(function(event, value) {
console.log('All events trigger this.');
});
Adds a listener that will be fired when any event is emitted. The event name is passed as the first argument to the callback. The listener is added to the beginning of the listeners array
server.prependAny(function(event, value) {
console.log('All events trigger this.');
});
Removes the listener that will be fired when any event is emitted.
server.offAny(function(value) {
console.log('The event was raised!');
});
Adds a one time listener for the event. The listener is invoked only the first time the event is fired, after which it is removed.
server.once('get', function (value) {
console.log('Ah, we have our first value!');
});
Adds a one time listener for the event. The listener is invoked only the first time the event is fired, after which it is removed. The listener is added to the beginning of the listeners array
server.prependOnceListener('get', function (value) {
console.log('Ah, we have our first value!');
});
Adds a listener that will execute n times for the event before being removed. The listener is invoked only the first n times the event is fired, after which it is removed.
server.many('get', 4, function (value) {
console.log('This event will be listened to exactly four times.');
});
Adds a listener that will execute n times for the event before being removed. The listener is invoked only the first n times the event is fired, after which it is removed. The listener is added to the beginning of the listeners array.
server.many('get', 4, function (value) {
console.log('This event will be listened to exactly four times.');
});
Remove a listener from the listener array for the specified event. Caution: Calling this method changes the array indices in the listener array behind the listener.
var callback = function(value) {
console.log('someone connected!');
};
server.on('get', callback);
// ...
server.removeListener('get', callback);
Removes all listeners, or those of the specified event.
By default EventEmitters will print a warning if more than 10 listeners are added to it. This is a useful default which helps finding memory leaks. Obviously not all Emitters should be limited to 10. This function allows that to be increased. Set to zero for unlimited.
Returns an array of listeners for the specified event. This array can be manipulated, e.g. to remove listeners.
server.on('get', function(value) {
console.log('someone connected!');
});
console.log(server.listeners('get')); // [ [Function] ]
Returns an array of listeners that are listening for any event that is specified. This array can be manipulated, e.g. to remove listeners.
server.onAny(function(value) {
console.log('someone connected!');
});
console.log(server.listenersAny()[0]); // [ [Function] ]
Execute each of the listeners that may be listening for the specified event name in order with the list of arguments.
Return the results of the listeners via Promise.all. Only this method doesn't work IE.
emitter.on('get',function(i) {
return new Promise(function(resolve){
setTimeout(function(){
resolve(i+3);
},50);
});
});
emitter.on('get',function(i) {
return new Promise(function(resolve){
resolve(i+2)
});
});
emitter.on('get',function(i) {
return Promise.resolve(i+1);
});
emitter.on('get',function(i) {
return i+0;
});
emitter.on('get',function(i) {
// noop
});
emitter.emitAsync('get',0)
.then(function(results){
console.log(results); // [3,2,1,0,undefined]
});
Returns an array listing the events for which the emitter has registered listeners. The values in the array will be strings.
emitter.on('foo', () => {});
emitter.on('bar', () => {});
console.log(emitter.eventNames());
// Prints: [ 'foo', 'bar' ]
FAQs
A feature-rich Node.js event emitter implementation with namespaces, wildcards, TTL, async listeners and browser/worker support.
The npm package eventemitter2 receives a total of 5,110,962 weekly downloads. As such, eventemitter2 popularity was classified as popular.
We found that eventemitter2 demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.