Cumulative downloads
Total Downloads
Last day
4.7%
920,109
Compared to previous day
Last week
6.6%
4,561,539
Compared to previous week
Last month
4.8%
19,026,897
Compared to previous month
Last year
59.5%
201,975,900
Compared to previous year
A type-safe implementation of EventEmitter
for browser and Node.js.
Despite event emitters potentially accepting any runtime value, defining a strict event contract is crucial when developing complex event-driven architectures. Unfortunately, the native type definitions for Node's EventEmitter
annotate event names as string
, which forbids any stricter type validation.
// index.js const emitter = new EventEmitter() // Let's say our application expects a "ping" // event with the number payload. emitter.on('ping', (n: number) => {}) // We can, however, emit a different event by mistake. emitter.emit('pong', 1) // Or even the correct event with the wrong data. emitter.emit('ping', 'wait, not a number')
The purpose of this library is to provide an EventEmitter
instance that can accept a generic describing the expected events contract.
import { Emitter } from 'strict-event-emitter' // Define a strict events contract where keys // represent event names and values represent // the list of arguments expected in ".emit()". type Events = { ping: [number] } const emitter = new Emitter<Events>() emitter.addListener('ping', (n) => { // "n" argument type is inferred as "number'. }) emitter.emit('ping', 10) // OK emitter.emit('unknown', 10) // TypeError (invalid event name) emitter.emit('ping', 'wait, not a number') // TypeError (invalid data)
This library is also a custom EventEmitter
implementation, which makes it compatible with other environments, like browsers or React Native.
npm install strict-event-emitter
import { Emitter } from 'strict-event-emitter' // 1. Define a type that describes your events. // Set event names as the keys, and their expected payloads as values. type Events = { connect: [id: string] disconnect: [id: string] } // 2. Create a strict emitter and pass the previously defined "Events" // as its first generic argument. const emitter = new Emitter<Events>() // 3. Use the "emitter" the same way you'd use the regular "EventEmitter" instance. emitter.addListener('connect', (id) => {}) emitter.emit('connect', 'abc-123')
MIT
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
4 existing vulnerabilities detected
Details
Reason
Found 5/30 approved changesets -- score normalized to 1
Reason
1 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
license file not detected
Details
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-09-02
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn Moretiny-node-eventemitter
Tiny portable node:events compliant strictly-typed EventEmitter implementation
strict-event-emitter-types
Strictly and safely type any EventEmitter-like interface on any object
@mkellsy/event-emitter
Strictly typed event emitter
rettime
Type-safe dependency-free EventTarget-inspired event emitter for browser and Node.js