Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

event-hook

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

event-hook - npm Package Compare versions

Comparing version 0.0.2 to 1.0.0

48

index.js

@@ -17,3 +17,3 @@ var EventEmitter = require('events').EventEmitter;

self.EventShim = function(obj, events){
self.EventShim = function(obj){
var ret = new EventEmitter();

@@ -24,2 +24,3 @@

var origOn = obj.on;
var origRemoveAll = obj.removeAllListeners;

@@ -48,3 +49,5 @@ ret.getObject = function(){ return obj; };

ret.forwardEvent = function(evt){
var forwardEvent = function(evt){
//Crude but necessary
origRemoveAll.apply(obj, [evt]);
origOn.apply(obj, [evt, function(){

@@ -56,2 +59,8 @@ executeProcessor(evt, arguments, 0);

var oldOn = ret.on;
ret.on = ret.addEventListener = function(){
forwardEvent(arguments[0]);
oldOn.apply(ret, arguments);
};
ret.addEventProcessor = function(evt, processor){

@@ -64,6 +73,2 @@ if(!(evt in eventProcessors))

events.forEach(function(evt){
ret.forwardEvent(evt);
});
return ret;

@@ -79,12 +84,17 @@ };

self.EventHook = function(obj, events){
var ret = self.EventShim(obj, events);
obj.on = obj.addEventListener = function(){
for(var i=0;i<arguments.length;i++){
if(arguments[i] instanceof Function){
arguments[i] = arguments[i].bind(obj);
//TODO: apply this to all eventEmitter methods in target object
self.EventHook = function(obj){
var ret = self.EventShim(obj);
Object.keys(EventEmitter.prototype).forEach(function(funcName){
if(funcName.toLowerCase() == "emit") //Don't hook emit otherwise what the hell are we doing here...
return;
obj[funcName] = function(){
for(var i=0;i<arguments.length;i++){
if(arguments[i] instanceof Function){
arguments[i] = arguments[i].bind(obj);
}
}
ret[funcName].apply(ret, arguments);
}
ret.on.apply(ret, arguments);
};
});
obj.__eventHookShim = ret;

@@ -98,3 +108,11 @@ return ret;

self.IsHooked = function(obj){
return ("__eventHookShim" in obj);
return typeof(self.GetShim(obj)) != "undefined";
};
/*
* Returns the shim object, null otherwise
*/
self.GetShim = function(obj){
if("__eventHookShim" in obj)
return obj.__eventHookShim;
};
{
"name": "event-hook",
"version": "0.0.2",
"version": "1.0.0",
"description": "",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -10,3 +10,3 @@ var evtTools = require('../');

var testObj = new events.EventEmitter();
var shim = evtTools.EventShim(testObj, ["testEvent1"]);
var shim = evtTools.EventShim(testObj);
shim.on("testEvent1", function(magicNo){

@@ -21,2 +21,4 @@ test.equal(magicNo, 42, "Arguments were not propagated correctly");

//As the previous test, but verifies behaviour when forwardEvent is called instead of passing the event name in the pseudo-constructor
//Removed since forwarding doesn't need to be exposed anymore, and therefore isn't
/*
exports.testShimForwardingCall = function(test){

@@ -34,5 +36,7 @@ test.expect(1);

};
*/
//Verifies that an event which is not registered will not be forwarded
exports.testShimNonForwarding = function(test){
//Removed since non-forwarding isn't part of the contract anymore
/*exports.testShimNonForwarding = function(test){
test.expect(0);

@@ -47,13 +51,13 @@

test.done();
};
};*/
//Verifies that testEvent2 will be dispatched, and with the right argument and to the right handler, but testEvent1 won't
exports.testShimMultiEvent = function(test){
test.expect(2);
test.expect(3);
var testObj = new events.EventEmitter();
var shim = evtTools.EventShim(testObj, ["testEvent2"]);
shim.on("testEvent1", function(magicNo){
var shim = evtTools.EventShim(testObj);
/*shim.on("testEvent1", function(magicNo){
test.ok(false, "Unregistered event was forwarded");
});
});*/
shim.on("testEvent2", function(magicNo){

@@ -65,2 +69,6 @@ test.notEqual(magicNo, 42, "Incorrect event was dispatched to registered handler");

testObj.emit("testEvent2",43);
shim.on("testEvent1", function(magicNo){
test.equal(magicNo, 42, "Incorrect argument");
});
testObj.emit("testEvent1",42);
test.done();

@@ -74,3 +82,3 @@ };

var testObj = new events.EventEmitter();
var shim = evtTools.EventShim(testObj, ["testEvent2"]);
var shim = evtTools.EventShim(testObj);
shim.addEventProcessor("testEvent1", function(cb, magicNo){

@@ -96,3 +104,3 @@ test.ok(false, "Incorrect processor was invoked");

var testObj = new events.EventEmitter();
var shim = evtTools.EventShim(testObj, ["testEvent2"]);
var shim = evtTools.EventShim(testObj);
shim.addEventProcessor("testEvent2", function(cb, magicNo){

@@ -119,3 +127,3 @@ test.ok(true, "Processor 1 was invoked");

var testObj = new events.EventEmitter();
var shim = evtTools.EventShim(testObj, ["testEvent2"]);
var shim = evtTools.EventShim(testObj);
shim.addEventProcessor("testEvent2", function(cb, magicNo){

@@ -146,3 +154,3 @@ test.ok(true, "Processor 1 was invoked");

var testObj = new events.EventEmitter();
var shim = evtTools.EventShim(testObj, ["testEvent2"]);
var shim = evtTools.EventShim(testObj);
var firstProcessorCalled = false;

@@ -180,3 +188,3 @@ shim.addEventProcessor("testEvent2", function(cb, magicNo){

var testObj = new events.EventEmitter();
var shim = evtTools.EventHook(testObj, ["testEvent2"]);
var shim = evtTools.EventHook(testObj);
shim.addEventProcessor("testEvent2", function(cb, magicNo){

@@ -199,5 +207,14 @@ cb(magicNo + 10);

test.ok(!evtTools.IsHooked(testObj), "Hook check returning true incorrectly");
var shim = evtTools.EventHook(testObj, []);
var shim = evtTools.EventHook(testObj);
test.ok(evtTools.IsHooked(testObj), "Hook check returning false incorrectly");
test.done();
}
};
exports.testShimRetrievabl = function(test){
test.expect(1);
var testObj = new events.EventEmitter();
var shim = evtTools.EventHook(testObj);
test.equal(evtTools.GetShim(testObj), shim, "Hook retrieval returned incorrect value");
test.done();
};
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc