Type-safe EventEmitter for browser and Node.js.
Installations
npm install strict-event-emitter
Developer Guide
Typescript
No
Module System
CommonJS, ESM
Node Version
18.17.1
NPM Version
9.6.7
Releases
Contributors
Unable to fetch Contributors
Languages
TypeScript (99.11%)
JavaScript (0.89%)
Developer
Download Statistics
Total Downloads
467,364,336
Last Day
973,323
Last Week
4,281,546
Last Month
18,618,478
Last Year
220,229,256
GitHub Statistics
63 Stars
66 Commits
7 Forks
3 Watching
2 Branches
7 Contributors
Bundle Size
2.36 kB
Minified
898.00 B
Minified + Gzipped
Package Meta Information
Latest Version
0.5.1
Package Id
strict-event-emitter@0.5.1
Unpacked Size
44.67 kB
Size
7.02 kB
File Count
7
NPM Version
9.6.7
Node Version
18.17.1
Publised On
21 Sept 2023
Total Downloads
Cumulative downloads
Total Downloads
467,364,336
Last day
-1.4%
973,323
Compared to previous day
Last week
-14.2%
4,281,546
Compared to previous week
Last month
6.7%
18,618,478
Compared to previous month
Last year
31.2%
220,229,256
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Strict Event Emitter
A type-safe implementation of EventEmitter
for browser and Node.js.
Motivation
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.
1// index.js 2const emitter = new EventEmitter() 3 4// Let's say our application expects a "ping" 5// event with the number payload. 6emitter.on('ping', (n: number) => {}) 7 8// We can, however, emit a different event by mistake. 9emitter.emit('pong', 1) 10 11// Or even the correct event with the wrong data. 12emitter.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.
1import { Emitter } from 'strict-event-emitter' 2 3// Define a strict events contract where keys 4// represent event names and values represent 5// the list of arguments expected in ".emit()". 6type Events = { 7 ping: [number] 8} 9 10const emitter = new Emitter<Events>() 11emitter.addListener('ping', (n) => { 12 // "n" argument type is inferred as "number'. 13}) 14 15emitter.emit('ping', 10) // OK 16emitter.emit('unknown', 10) // TypeError (invalid event name) 17emitter.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.
Getting started
Install
1npm install strict-event-emitter
Use
1import { Emitter } from 'strict-event-emitter' 2 3// 1. Define an interface that describes your events. 4// Set event names as the keys, and their expected payloads as values. 5interface Events { 6 connect: [id: string] 7 disconnect: [id: string] 8} 9 10// 2. Create a strict emitter and pass the previously defined "Events" 11// as its first generic argument. 12const emitter = new Emitter<Events>() 13 14// 3. Use the "emitter" the same way you'd use the regular "EventEmitter" instance. 15emitter.addListener('connect', (id) => {}) 16emitter.emit('connect', 'abc-123')
License
MIT
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
6 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-gcx4-mw62-g8wm
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
Reason
Found 5/30 approved changesets -- score normalized to 1
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/main.yml:1
- Info: no jobLevel write permissions found
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:13: update your workflow using https://app.stepsecurity.io/secureworkflow/open-draft/strict-event-emitter/main.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:18: update your workflow using https://app.stepsecurity.io/secureworkflow/open-draft/strict-event-emitter/main.yml/main?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/main.yml:24: update your workflow using https://app.stepsecurity.io/secureworkflow/open-draft/strict-event-emitter/main.yml/main?enable=pin
- Info: 0 out of 2 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 1 third-party GitHubAction dependencies pinned
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
license file not detected
Details
- Warn: project does not have a license file
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 12 are checked with a SAST tool
Score
2.9
/10
Last Scanned on 2025-01-27
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 MoreOther packages similar to strict-event-emitter
strict-event-emitter-types
Strictly and safely type any EventEmitter-like interface on any object
eventemitter-strict
A eventemitter with typescript full support
strict-event-emitter-once
Promise based `once` for strict-event-emitter
strict-event-emitter-fork
Type-safe implementation of EventEmitter for browser and Node.js