Profiles·Public

eventemitter2

semver>=6.0.0postconditions12functions6last verified2026-06-24coverage score100%

Postconditions: what we check

  • EventEmitter2 · eventemitter2-001
    error
    Whenerror event emitted without listener
    ThrowsUncaught exception unless ignoreErrors configured
    Required handlingCaller MUST attach error event listener
    costmediumin prodimmediate exceptionusers seeservice unavailablevisibilityvisible
    Sources[1]
  • emit · eventemitter2-emit-unhandled-error
    error
    Whenerror event emitted with no listener and ignoreErrors: false (default)
    ThrowsError — either re-throws the emitted Error instance or throws new Error("Uncaught, unspecified 'error' event.")
    Required handlingCaller MUST attach .on('error', handler) before emitting error events, or pass ignoreErrors:true to the constructor
    costmediumin prodimmediate exceptionusers seeservice unavailablevisibilityvisible
    Sources[2]
  • emitAsync · eventemitter2-emit-async-unhandled-error
    error
    WhenemitAsync('error', err) called with no 'error' listener registered and ignoreErrors: false
    ThrowsReturns Promise.reject(err) — a rejected Promise with the error or a string message
    Required handlingCaller MUST attach .on('error', handler) OR wrap emitAsync() in try/catch or .catch()
    costmediumin proddelayed failureusers seedegraded performancevisibilitysilent
    Sources[2]
  • emitAsync · eventemitter2-emit-async-listener-rejection
    warning
    Whenany registered listener throws or returns a rejected Promise
    ThrowsReturns rejected Promise with the first listener's rejection reason (Promise.all semantics)
    Required handlingCaller MUST await emitAsync() and wrap in try/catch, or chain .catch()
    costlowin proddelayed failureusers seedegraded performancevisibilitysilent
    Sources[2][3]
  • waitFor · eventemitter2-wait-for-timeout
    warning
    Whentimeout option is > 0 and the event is not emitted within that duration
    ThrowsRejects with Error('timeout') — the rejection message is literally 'timeout'
    Required handlingCaller MUST wrap waitFor() in try/catch or .catch() when using timeout option
    costlowin proddelayed failureusers seedegraded performancevisibilitysilent
    Sources[2]
  • waitFor · eventemitter2-wait-for-cancel
    warning
    Whenpromise.cancel() is called before the event fires
    ThrowsRejects with Error('canceled')
    Required handlingCaller MUST handle rejection if cancel() can be called on in-flight promises
    costlowin proddelayed failureusers seedegraded performancevisibilitysilent
    Sources[2]
  • waitFor · eventemitter2-wait-for-handle-error
    warning
    WhenhandleError: true in options AND the event fires with a truthy first argument
    ThrowsRejects with the first argument as the error reason
    Required handlingCaller MUST wrap in try/catch when handleError option is enabled
    costlowin proddelayed failureusers seedegraded performancevisibilitysilent
    Sources[2][4]
  • EventEmitter2.once · eventemitter2-static-once-error-rejection
    error
    Whenthe emitter emits 'error' before the target event fires
    ThrowsRejects with the error emitted — the rejection reason is the Error object from the error event
    Required handlingCaller MUST wrap EventEmitter2.once() in try/catch or chain .catch()
    costmediumin proddelayed failureusers seedegraded performancevisibilitysilent
    Sources[2]
  • EventEmitter2.once · eventemitter2-static-once-timeout
    warning
    Whentimeout option is > 0 and the event does not fire within that duration
    ThrowsRejects with Error('timeout')
    Required handlingCaller MUST wrap in try/catch or .catch() when using timeout option
    costlowin proddelayed failureusers seedegraded performancevisibilitysilent
    Sources[2]
  • EventEmitter2.once · eventemitter2-static-once-cancel
    warning
    Whenpromise.cancel() is called before the event fires
    ThrowsRejects with Error('canceled')
    Required handlingCaller MUST handle rejection if cancel() can be called on in-flight promises
    costlowin proddelayed failureusers seedegraded performancevisibilitysilent
    Sources[2]
  • listenTo · eventemitter2-listen-to-invalid-target
    error
    Whentarget parameter is not an object, or target does not implement addEventListener/on/addListener
    ThrowsTypeError('target musts be an object') or Error('target does not implement any known event API')
    Required handlingCaller MUST validate target implements an event API before calling listenTo()
    costlowin prodimmediate exceptionusers seeservice unavailablevisibilityvisible
    Sources[2]
  • listenTo · eventemitter2-listen-to-invalid-options
    warning
    Whenoptions.on or options.off are provided but are not functions
    ThrowsTypeError('on method must be a function') or TypeError('off method must be a function')
    Required handlingCaller MUST ensure on/off hooks in options are valid functions when using custom subscription API
    costlowin prodimmediate exceptionusers seeservice unavailablevisibilityvisible
    Sources[2]

Sources

Every postcondition cites at least one of these. Grouped by source type; numbered to match the footnotes above.

Official documentation
  • [3]
    developer.mozilla.org/en-US/docs/Web
    All
Source code

Research notes

Curator notes from SOURCES.md captured when the profile was written so you can verify the reasoning, not just the rules.

Sources: eventemitter2

Official Documentation

Primary Sources

  • GitHub Repository: EventEmitter2/EventEmitter2

    • Main repository with comprehensive README
    • Issue #215 documents error event throwing behavior
    • TypeScript definitions: eventemitter2.d.ts
    • Active maintenance and issue triage
  • npm Package: eventemitter2

    • Package metadata and version history
    • Weekly downloads: 13.6M+
    • Latest stable: 6.4.9 (2020-12-14)
    • Zero dependencies (reduced supply chain risk)

API Documentation

  • TypeScript Definitions: eventemitter2.d.ts

    • Complete type definitions for all methods
    • Constructor options interface
    • WaitForOptions and ListenToOptions types
  • jsDocs.io: eventemitter2@6.4.9

    • Auto-generated API documentation
    • Method signatures and descriptions

Tutorials and Guides

Error Handling Patterns

Primary Error Behavior (Contract Basis)

Source: GitHub Issue #215 - Uncaught, unspecified 'error' event

When an 'error' event is emitted WITHOUT listeners attached:

  • Throws: Error: Uncaught, unspecified 'error' event
  • Process exits with stack trace
  • This is EXPECTED behavior per Node.js EventEmitter specification

Mitigation Options:

  1. Attach error listener: emitter.on('error', handler) (RECOMMENDED)
  2. Configure ignoreErrors: new EventEmitter2({ ignoreErrors: true })

Advanced Error Patterns

emitAsync Promise Rejection:

  • Source: README - emitAsync Method
  • Returns Promise.all() of listener results
  • Rejects if any listener throws or returns rejected promise
  • Requires try-catch or .catch() for proper handling

waitFor Promise Rejection:

  • Source: README - waitFor Method
  • Waits for event as a promise
  • With handleError: true - rejects on error events
  • With timeout option - rejects on timeout
  • Requires try-catch for proper handling

Contract Rationale

Postcondition eventemitter2-001: Error Event Listener Required

Behavior: EventEmitter2 instances that emit 'error' events will throw uncaught exceptions if no error listener is attached (unless ignoreErrors: true is configured).

Impact: Can crash Node.js applications in production

Detection: EventListenerAnalyzer tracks instances and verifies error listeners are attached

Severity: ERROR - Process crash without proper handling

Why This Matters:

  1. Production Stability: Missing error listeners are a top cause of Node.js crashes
  2. Common Pattern: 60% of eventemitter2 usage doesn't attach error listeners
  3. High Impact: Crashes can cause data loss, service downtime
  4. Easy to Fix: Simply add emitter.on('error', handler) before use

Security Analysis

CVE Status: ✅ CLEAN - No CVEs found

Supply Chain Risk: NONE - Zero dependencies

Maintenance: Active - Issues triaged, PRs reviewed

Real-World Usage

Code Examples:

Production Users:

  • BitMEX (cryptocurrency trading platform)
  • NsSocket (network socket management)
  • Grunt (build automation)
  • Socket.IO ecosystem

Detection Capabilities

EventListenerAnalyzer (verify-cli/src/analyzers/event-listener-analyzer.ts):

  • ✅ Detects missing error listeners on new instances
  • ✅ Detects missing listeners on class properties
  • ✅ Supports both constructor and factory patterns
  • ✅ Tracks listener attachments via .on(), .once(), .addEventListener()

Expected Detection Rate: 85-95%

  • Covers 80% of common usage patterns
  • Catches main error source (missing error listeners)
  • Some edge cases not detectable (dynamic events, cross-module)

Research Dates

  • Initial Research: 2026-02-26
  • Onboarding Completion: 2026-02-27
  • Phase 1-8 Documentation: Complete
Need a different package?
Request a profile