{"_id":"events-intercept","_rev":"2991341","name":"events-intercept","description":"event interceptors - like middleware for EventEmitter","dist-tags":{"latest":"2.0.0"},"maintainers":[{"name":"brandonhorst","email":"brandonhorst@gmail.com"}],"time":{"modified":"2023-08-28T09:17:53.000Z","created":"2014-06-25T20:36:48.122Z","2.0.0":"2015-01-02T22:17:20.152Z","1.1.1":"2014-09-15T01:41:47.720Z","1.1.0":"2014-06-27T10:08:50.777Z","1.0.0":"2014-06-27T10:06:26.679Z","0.2.1":"2014-06-26T16:24:39.509Z","0.2.0":"2014-06-26T15:29:41.798Z","0.1.0":"2014-06-25T20:36:48.122Z"},"users":{},"author":{"name":"@brandonhorst"},"repository":{"type":"git","url":"https://github.com/brandonhorst/events-intercept.git"},"versions":{"2.0.0":{"name":"events-intercept","version":"2.0.0","description":"event interceptors - like middleware for EventEmitter","main":"lib/events-intercept.js","scripts":{"test":"mocha test","posttest":"istanbul cover _mocha -- -R spec","coveralls":"istanbul cover _mocha --report lcovonly -- -R spec && cat ./coverage/lcov.info | coveralls && rm -rf ./coverage"},"repository":{"type":"git","url":"https://github.com/brandonhorst/events-intercept.git"},"keywords":["event","events","emit","intercept","tap","hook","report","mutate","EventEmitter"],"author":{"name":"@brandonhorst"},"license":"MIT","bugs":{"url":"https://github.com/brandonhorst/events-intercept/issues"},"homepage":"https://github.com/brandonhorst/events-intercept","devDependencies":{"chai":"^1.10.0","coveralls":"^2.11.2","istanbul":"^0.3.5","mocha":"^2.1.0","sinon":"^1.12.2","sinon-chai":"^2.6.0"},"gitHead":"9e83c2d178c267980c2e58ef0c7e930c2cd55043","_id":"events-intercept@2.0.0","_shasum":"adbf38681c5a4b2011c41ee41f61a34cba448897","_from":".","_npmVersion":"2.1.11","_nodeVersion":"0.10.33","_npmUser":{"name":"brandonhorst","email":"brandonhorst@gmail.com"},"maintainers":[{"name":"brandonhorst","email":"brandonhorst@gmail.com"}],"dist":{"shasum":"adbf38681c5a4b2011c41ee41f61a34cba448897","size":6884,"noattachment":false,"key":"/events-intercept/-/events-intercept-2.0.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/events-intercept/download/events-intercept-2.0.0.tgz"},"directories":{},"publish_time":1420237040152,"_hasShrinkwrap":false,"_cnpm_publish_time":1420237040152,"_cnpmcore_publish_time":"2021-12-16T20:57:25.076Z"},"1.1.1":{"name":"events-intercept","version":"1.1.1","description":"event interceptors - like middleware for EventEmitter","main":"lib/events-intercept.js","scripts":{"test":"node_modules/mocha/bin/mocha test","posttest":"istanbul cover ./node_modules/mocha/bin/_mocha -- -R spec","coveralls":"istanbul cover ./node_modules/mocha/bin/_mocha --report lcovonly -- -R spec && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage"},"repository":{"type":"git","url":"https://github.com/brandonhorst/events-intercept.git"},"keywords":["event","events","emit","intercept","tap","hook","report","mutate","EventEmitter"],"author":{"name":"@brandonhorst"},"license":"MIT","bugs":{"url":"https://github.com/brandonhorst/events-intercept/issues"},"homepage":"https://github.com/brandonhorst/events-intercept","devDependencies":{"chai":"^1.9.1","chai-spies":"^0.5.1","coveralls":"^2.10.1","gulp":"^3.8.2","istanbul":"^0.2.12","mocha":"^1.20.1","sinon":"^1.10.2","sinon-chai":"^2.5.0"},"gitHead":"a61505bf00f4d17a8dd1e7a27c42076a9df798b5","_id":"events-intercept@1.1.1","_shasum":"e20d3c37fc1f56f2e3a278588e015d9c3ca8897b","_from":".","_npmVersion":"1.4.23","_npmUser":{"name":"brandonhorst","email":"brandonhorst@gmail.com"},"maintainers":[{"name":"brandonhorst","email":"brandonhorst@gmail.com"}],"dist":{"shasum":"e20d3c37fc1f56f2e3a278588e015d9c3ca8897b","size":6760,"noattachment":false,"key":"/events-intercept/-/events-intercept-1.1.1.tgz","tarball":"http://registry.cnpm.dingdandao.com/events-intercept/download/events-intercept-1.1.1.tgz"},"directories":{},"publish_time":1410745307720,"_hasShrinkwrap":false,"_cnpm_publish_time":1410745307720,"_cnpmcore_publish_time":"2021-12-16T20:57:25.274Z"},"1.1.0":{"name":"events-intercept","version":"1.1.0","description":"event interceptors - like middleware for EventEmitter","main":"lib/events-intercept.js","scripts":{"test":"node_modules/mocha/bin/mocha test","posttest":"istanbul cover ./node_modules/mocha/bin/_mocha -- -R spec","coveralls":"istanbul cover ./node_modules/mocha/bin/_mocha --report lcovonly -- -R spec && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage"},"repository":{"type":"git","url":"https://github.com/brandonhorst/events-intercept.git"},"keywords":["event","events","emit","intercept","tap","hook","report","mutate","EventEmitter"],"author":{"name":"@brandonhorst"},"license":"MIT","bugs":{"url":"https://github.com/brandonhorst/events-intercept/issues"},"homepage":"https://github.com/brandonhorst/events-intercept","dependencies":{"lodash":"^2.4.1"},"devDependencies":{"chai":"^1.9.1","chai-spies":"^0.5.1","coveralls":"^2.10.1","gulp":"^3.8.2","istanbul":"^0.2.12","mocha":"^1.20.1","sinon":"^1.10.2","sinon-chai":"^2.5.0"},"gitHead":"5ecad77438c1fc0c6986afc10e658690a23aa4c3","_id":"events-intercept@1.1.0","_shasum":"7b3a10d62c84178cf0a58ba61d1ceff9e14a6a0f","_from":".","_npmVersion":"1.4.16","_npmUser":{"name":"brandonhorst","email":"brandonhorst@gmail.com"},"maintainers":[{"name":"brandonhorst","email":"brandonhorst@gmail.com"}],"dist":{"shasum":"7b3a10d62c84178cf0a58ba61d1ceff9e14a6a0f","size":6845,"noattachment":false,"key":"/events-intercept/-/events-intercept-1.1.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/events-intercept/download/events-intercept-1.1.0.tgz"},"directories":{},"publish_time":1403863730777,"_hasShrinkwrap":false,"_cnpm_publish_time":1403863730777,"_cnpmcore_publish_time":"2021-12-16T20:57:25.458Z"},"1.0.0":{"name":"events-intercept","version":"1.0.0","description":"event interceptors - like middleware for EventEmitter","main":"lib/events-intercept.js","scripts":{"test":"node_modules/mocha/bin/mocha test","posttest":"istanbul cover ./node_modules/mocha/bin/_mocha -- -R spec","coveralls":"istanbul cover ./node_modules/mocha/bin/_mocha --report lcovonly -- -R spec && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage"},"repository":{"type":"git","url":"https://github.com/brandonhorst/events-intercept.git"},"keywords":["event","events","emit","intercept","tap","hook","report","mutate","EventEmitter"],"author":{"name":"@brandonhorst"},"license":"MIT","bugs":{"url":"https://github.com/brandonhorst/events-intercept/issues"},"homepage":"https://github.com/brandonhorst/events-intercept","dependencies":{"lodash":"^2.4.1"},"devDependencies":{"chai":"^1.9.1","chai-spies":"^0.5.1","coveralls":"^2.10.1","gulp":"^3.8.2","istanbul":"^0.2.12","mocha":"^1.20.1","sinon":"^1.10.2","sinon-chai":"^2.5.0"},"gitHead":"42cc29c6aefc826a481cf7f9795f4823a8f6f527","_id":"events-intercept@1.0.0","_shasum":"ca85b1664f15814670ebe70c1cf6b65355611905","_from":".","_npmVersion":"1.4.16","_npmUser":{"name":"brandonhorst","email":"brandonhorst@gmail.com"},"maintainers":[{"name":"brandonhorst","email":"brandonhorst@gmail.com"}],"dist":{"shasum":"ca85b1664f15814670ebe70c1cf6b65355611905","size":6853,"noattachment":false,"key":"/events-intercept/-/events-intercept-1.0.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/events-intercept/download/events-intercept-1.0.0.tgz"},"directories":{},"publish_time":1403863586679,"_hasShrinkwrap":false,"_cnpm_publish_time":1403863586679,"_cnpmcore_publish_time":"2021-12-16T20:57:25.641Z"},"0.2.1":{"name":"events-intercept","version":"0.2.1","description":"event interceptors - like middleware for EventEmitter","main":"lib/events-intercept.js","scripts":{"test":"node_modules/mocha/bin/mocha test","posttest":"istanbul cover ./node_modules/mocha/bin/_mocha --report lcovonly -- -R spec","coverage":"cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage"},"repository":{"type":"git","url":"https://github.com/brandonhorst/events-intercept.git"},"keywords":["event","events","emit","intercept","tap","hook","report","mutate","EventEmitter"],"author":{"name":"@brandonhorst"},"license":"MIT","bugs":{"url":"https://github.com/brandonhorst/events-intercept/issues"},"homepage":"https://github.com/brandonhorst/events-intercept","dependencies":{"async":"^0.9.0","lodash":"^2.4.1"},"devDependencies":{"chai":"^1.9.1","chai-spies":"^0.5.1","coveralls":"^2.10.1","gulp":"^3.8.2","istanbul":"^0.2.12","mocha":"^1.20.1"},"gitHead":"943be089a623d4ae9a541d3b19b3647e34275692","_id":"events-intercept@0.2.1","_shasum":"1bc8d6f340e8360cf7193c0881553a2a2ef29d9c","_from":".","_npmVersion":"1.4.15","_npmUser":{"name":"brandonhorst","email":"brandonhorst@gmail.com"},"maintainers":[{"name":"brandonhorst","email":"brandonhorst@gmail.com"}],"dist":{"shasum":"1bc8d6f340e8360cf7193c0881553a2a2ef29d9c","size":4946,"noattachment":false,"key":"/events-intercept/-/events-intercept-0.2.1.tgz","tarball":"http://registry.cnpm.dingdandao.com/events-intercept/download/events-intercept-0.2.1.tgz"},"directories":{},"publish_time":1403799879509,"_hasShrinkwrap":false,"_cnpm_publish_time":1403799879509,"_cnpmcore_publish_time":"2021-12-16T20:57:25.837Z"},"0.2.0":{"name":"events-intercept","version":"0.2.0","description":"event interceptors - like middleware for EventEmitter","main":"lib/events-intercept.js","scripts":{"test":"node_modules/mocha/bin/mocha test","posttest":"istanbul cover ./node_modules/mocha/bin/_mocha --report lcovonly -- -R spec","coverage":"cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage"},"repository":{"type":"git","url":"https://github.com/brandonhorst/events-intercept.git"},"keywords":["event","events","emit","intercept","tap","hook","report","mutate","EventEmitter"],"author":{"name":"@brandonhorst"},"license":"MIT","bugs":{"url":"https://github.com/brandonhorst/events-intercept/issues"},"homepage":"https://github.com/brandonhorst/events-intercept","dependencies":{"async":"^0.9.0","istanbul":"^0.2.12","lodash":"^2.4.1"},"devDependencies":{"chai":"^1.9.1","chai-spies":"^0.5.1","coveralls":"^2.10.1","gulp":"^3.8.2","istanbul":"^0.2.12","mocha":"^1.20.1"},"gitHead":"0bccdcfdf302c895376e37e0ad6a485f4b7dc226","_id":"events-intercept@0.2.0","_shasum":"aadf798cadbd6474d5888b0caeedfe3f00b68092","_from":".","_npmVersion":"1.4.15","_npmUser":{"name":"brandonhorst","email":"brandonhorst@gmail.com"},"maintainers":[{"name":"brandonhorst","email":"brandonhorst@gmail.com"}],"dist":{"shasum":"aadf798cadbd6474d5888b0caeedfe3f00b68092","size":4942,"noattachment":false,"key":"/events-intercept/-/events-intercept-0.2.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/events-intercept/download/events-intercept-0.2.0.tgz"},"directories":{},"publish_time":1403796581798,"_hasShrinkwrap":false,"_cnpm_publish_time":1403796581798,"_cnpmcore_publish_time":"2021-12-16T20:57:26.015Z"},"0.1.0":{"name":"events-intercept","version":"0.1.0","description":"event interceptors - like middleware for EventEmitter","main":"lib/events-intercept.js","scripts":{"test":"mocha test"},"repository":{"type":"git","url":"https://github.com/brandonhorst/events-intercept.git"},"keywords":["event","events","emit","intercept","tap","hook","report","mutate","EventEmitter"],"author":{"name":"@brandonhorst"},"license":"MIT","bugs":{"url":"https://github.com/brandonhorst/events-intercept/issues"},"homepage":"https://github.com/brandonhorst/events-intercept","dependencies":{"async":"^0.9.0","lodash":"^2.4.1"},"devDependencies":{"chai":"^1.9.1","chai-spies":"^0.5.1","gulp":"^3.8.2","mocha":"^1.20.1"},"gitHead":"c6a44d082fdc15074c6c4381c3cf60203ebe51c8","_id":"events-intercept@0.1.0","_shasum":"1d9a61739e7f825a357e543c7201eaf658d03b23","_from":".","_npmVersion":"1.4.15","_npmUser":{"name":"brandonhorst","email":"brandonhorst@gmail.com"},"maintainers":[{"name":"brandonhorst","email":"brandonhorst@gmail.com"}],"dist":{"shasum":"1d9a61739e7f825a357e543c7201eaf658d03b23","size":4217,"noattachment":false,"key":"/events-intercept/-/events-intercept-0.1.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/events-intercept/download/events-intercept-0.1.0.tgz"},"directories":{},"publish_time":1403728608122,"_hasShrinkwrap":false,"_cnpm_publish_time":1403728608122,"_cnpmcore_publish_time":"2021-12-16T20:57:26.229Z"}},"readme":"#`events-intercept`\n\n[![Build Status](https://travis-ci.org/brandonhorst/events-intercept.svg?branch=master)](https://travis-ci.org/brandonhorst/events-intercept)\n[![Coverage Status](https://coveralls.io/repos/brandonhorst/events-intercept/badge.png?branch=master)](https://coveralls.io/r/brandonhorst/events-intercept?branch=master)\n\nThe node [EventEmitter](http://nodejs.org/api/events.html) is very powerful. However, at times it could be valuable to intercept events before they reach their handlers, to modify the data, or emit other events. That's a job for `event-intercept`.\n\n##Installation\n\n```sh\nnpm install events-intercept\n```\n\n##Standalone Usage\n\nThe module contains a constructor, `EventEmitter`, which inherits from the standard node `events.EventEmitter`.\n\n\tvar EventEmitter = require('events-intercept').EventEmitter;\n\tvar emitter = new EventEmitter();\n\nIn our application, we have an object that will emit a `data` event, and pass it a single argument.\n\n\temitter.emit('data', 'myData')\n\nIt is very easy to listen for this event and handle it\n\n\temitter.on('data', function(arg) {\n\t\tconsole.log(arg);\n\t}); //logs 'myData'\n\nHowever, we want to intercept that event and modify the data. We can do that by setting an `interceptor` with `intercept(event, interceptor)`. It is passed all arguments that would be passed to the emitter, as well as a standard node callback. In this case, let's just add a prefix on to the data.\n\n\temitter.intercept('data', function(arg, done) {\n\t\treturn done(null, 'intercepted ' + arg);\n\t});\n\nThis code will be executed before the handler, and the new argument will be passed on to the handler appropriately.\n\n\temitter.emit('data', 'some other data');\n\t//logs 'intercepted some other data'\n\nIf multiple interceptors are added to a single event, they will be called in the order that they are added, like [async.waterfall](https://github.com/caolan/async#waterfall).\n\nHere's that sample code all together. Of course, `intercept` supports proper function chaining.\n\n\tvar eventsIntercept = require('events-intercept');\n\tvar emitter = new eventsIntercept.EventEmitter();\n\n\temitter\n\t.on('data', function(arg) {\n\t\tconsole.log(arg);\n\t}).intercept('data', function(arg, done) {\n\t\treturn done(null, 'intercepted ' + arg);\n\t}).emit('data', 'myData');\n\t//logs 'intercepted myData'\n\nPlease see `test/intercept.js` for more complete samples.\n\n##Calling Separate Events\n\nThere may be times when you want to intercept one event and call another. Luckily, all `intercept` handlers are called with the `EventEmitter` as the `this` context, so you can `emit` events yourself.\n\n\temitter.intercept('data', function(done) {\n\t\tthis\n\t\t.emit('otherData')\n\t\t.emit('thirdData');\n\t\treturn done(null);\n\t});\n\t//emits 'data', 'otherData', and 'thirdData'\n\nRemember, `emit`ting an event that you are `intercept`ing will cause a loop, so be careful.\n\nIn fact, an `intercept`or do not need to call the callback at all, which means that the event that was `intercept`ed will never be called at all.\n\n\n\temitter.intercept('data', function(done) {\n\t\tthis\n\t\t.emit('otherData')\n\t\t.emit('thirdData');\n\t});\n\t//emits 'otherData' and 'thirdData' but not 'data'\n\n##Utilities\n\n`events-intercept` supports all of the useful utilities that the standard `EventEmitter` supports:\n\n* `interceptors(type)` returns an array of all interceptors (functions) for the given type.\n* `removeInterceptor(type, interceptor)` removes an interceptor of a given type. You must pass in the interceptor function.\n* `removeAllInterceptors(type)` removes all interceptors for a given type.\n* `removeAllInterceptors()` removes all interceptors. Will remove the `removeInterceptor` event last, so they will all get triggered.\n* the EventEmitter will throw a warning if more than 10 interceptors are added to a single event, as this could represent a memory leak. `setMaxInterceptors(n)` allows you to change that. Set it to 0 for no limit.\n\nAll of these are demonstrated in the tests.\n\n##Patching\n\nOf course, many EventEmitters that you have the pleasure of using will not have the foresight to use `event-intercept`. Thankfully, Javascript is awesome, it's possible to monkey patch the interception capabilities onto an existing object. Just call\n\n\tvar events = require('events');\n\tvar eventsIntercept = require('events-intercept');\n\n\tvar emitter = new events.EventEmitter();\n\n\teventsIntercept.patch(emitter)\n\n\temitter\n\t.on('data', function(arg) {\n\t\tconsole.log(arg);\n\t}).intercept('data', function(arg, done) {\n\t\treturn done(null, 'intercepted ' + arg);\n\t}).emit('data', 'myData');\n\t//logs 'intercepted myData'\n\nNow, you should be able to call `intercept` on the standard `EventEmitter`.\n\nThis is also shown in `test/intercept.js`.\n","_attachments":{},"homepage":"https://github.com/brandonhorst/events-intercept","bugs":{"url":"https://github.com/brandonhorst/events-intercept/issues"},"license":"MIT"}