{"id":17090,"library":"events-intercept","title":"Event Interceptors for EventEmitter","description":"`events-intercept` is a Node.js library designed to introduce middleware-like interception capabilities to the standard `EventEmitter`. It allows developers to define functions that execute before an event's registered handlers, providing opportunities to modify event arguments, prevent event propagation, or trigger additional events based on intercepted data. The library extends `events.EventEmitter`, adding methods such as `intercept`, `interceptors`, `removeInterceptor`, and `removeAllInterceptors` to manage these pre-handler hooks. The current and last stable version is 2.0.0, released in 2015. This package is no longer actively maintained, making it unsuitable for new projects requiring ongoing support or modern JavaScript features like ES Modules. Its primary differentiator, historically, was providing a structured, waterfall-like approach to event processing, akin to request middleware, directly within the `EventEmitter` paradigm.","status":"abandoned","version":"2.0.0","language":"javascript","source_language":"en","source_url":"https://github.com/brandonhorst/events-intercept","tags":["javascript","event","events","emit","intercept","tap","hook","report","mutate"],"install":[{"cmd":"npm install events-intercept","lang":"bash","label":"npm"},{"cmd":"yarn add events-intercept","lang":"bash","label":"yarn"},{"cmd":"pnpm add events-intercept","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"This package is CommonJS-only and does not support ES Module `import` syntax. Attempting to use `import` will result in a syntax error or a runtime error.","wrong":"import { EventEmitter } from 'events-intercept';","symbol":"EventEmitter","correct":"const EventEmitter = require('events-intercept').EventEmitter;"},{"note":"The entire module can be required, exposing the `EventEmitter` constructor as a property of the resulting object. This is a CommonJS module only.","wrong":"import eventsIntercept from 'events-intercept';","symbol":"eventsIntercept","correct":"const eventsIntercept = require('events-intercept');"}],"quickstart":{"code":"const EventEmitter = require('events-intercept').EventEmitter;\nconst emitter = new EventEmitter();\n\nemitter\n  .on('data', function(arg) {\n    console.log(`Handler received: ${arg}`);\n  })\n  .intercept('data', function(arg, done) {\n    // Interceptor 1: Modify data\n    console.log(`Interceptor 1 received: ${arg}`);\n    return done(null, 'intercepted_1_' + arg);\n  })\n  .intercept('data', function(arg, done) {\n    // Interceptor 2: Further modify data\n    console.log(`Interceptor 2 received: ${arg}`);\n    return done(null, 'intercepted_2_' + arg);\n  });\n\nconsole.log('Emitting event with initial data...');\nemitter.emit('data', 'initial data');\n// Expected output:\n// Emitting event with initial data...\n// Interceptor 1 received: initial data\n// Interceptor 2 received: intercepted_1_initial data\n// Handler received: intercepted_2_initial data\n\nconsole.log('\\nEmitting another event, but intercepting to prevent original handler...');\nemitter.intercept('no-callback-event', function(arg, done) {\n    console.log(`Interceptor for 'no-callback-event' received: ${arg}. Not calling done().`);\n    // Do not call done(), so the original 'no-callback-event' handler is never invoked.\n    this.emit('alternative-event', 'transformed ' + arg);\n});\nemitter.on('no-callback-event', () => console.log('This should not log!'));\nemitter.on('alternative-event', (arg) => console.log(`Alternative event handler received: ${arg}`));\nemitter.emit('no-callback-event', 'original data');","lang":"javascript","description":"This quickstart demonstrates how to set up `EventEmitter` with multiple interceptors for an event, showing how data can be transformed in a pipeline. It also illustrates how an interceptor can prevent the original event's handlers from being called while emitting new events."},"warnings":[{"fix":"Consider alternatives like custom event systems, state management libraries, or modern middleware patterns for stream/message processing that are actively maintained and support modern JavaScript features.","message":"The `events-intercept` package has been abandoned since 2015 and is no longer maintained. It does not receive security updates, bug fixes, or new features. Using it in new projects is strongly discouraged, and existing projects should consider migration.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"For CommonJS-only environments, use `require()`. For ES Module projects, evaluate if the functionality is critical enough to warrant using a legacy CJS module, or seek out an ESM-compatible alternative.","message":"This package is CommonJS-only and lacks native ES Module (ESM) support. It cannot be directly `import`ed in ESM contexts without relying on Node.js's CJS interoperability layers, which can lead to issues or suboptimal performance in modern build environments.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Avoid `this.emit(event)` for the currently intercepted `event` within an interceptor. If you need to trigger subsequent events, emit a *different* event or ensure there's a conditional break in the interception chain.","message":"Re-emitting the *same* event within its own interceptor can lead to an infinite loop, causing stack overflow errors or exhausting system resources.","severity":"gotcha","affected_versions":">=2.0.0"},{"fix":"Monitor for 'MaxInterceptorsExceededWarning'. If legitimate, increase the limit using `emitter.setMaxInterceptors(n)`. Otherwise, refactor your event handling to reduce the number of interceptors or consolidate related logic into fewer, more comprehensive interceptors.","message":"By default, a warning is emitted if more than 10 interceptors are added to a single event type, indicating a potential memory leak. While `setMaxInterceptors(n)` can change this limit (0 for no limit), a large number of interceptors can still impact performance and maintainability.","severity":"gotcha","affected_versions":">=2.0.0"}],"env_vars":null,"last_verified":"2026-04-22T00:00:00.000Z","next_check":"2026-07-21T00:00:00.000Z","problems":[{"fix":"If this is intentional, increase the limit: `emitter.setMaxInterceptors(N)` where `N` is your desired limit (0 for no limit). If unintentional, review your code to identify and remove redundant interceptors.","cause":"More than 10 interceptors (default limit) were added to a single event type, which the library flags as a potential memory leak.","error":"Error: Maximum number of interceptors exceeded"},{"fix":"Inspect interceptors for the affected event. Ensure that `this.emit('eventName', ...)` inside an interceptor does not re-emit the `eventName` that triggered the interceptor itself. Emit a *different* event, or implement logic to prevent re-emission.","cause":"An interceptor for an event is re-emitting the *same* event, leading to an uncontrolled recursive loop of interception and emission.","error":"RangeError: Maximum call stack size exceeded"}],"ecosystem":"npm","meta_description":null}