
Security News
Feross on the 10 Minutes or Less Podcast: Nobody Reads the Code
Socket CEO Feross Aboukhadijeh joins 10 Minutes or Less, a podcast by Ali Rohde, to discuss the recent surge in open source supply chain attacks.
Hyjack.js lets you tie into event emitter objects and do things without changing your code.
Hyjack.js lets you tie into existing EventEmitter based objects and hyjack their events for things like logging, monitoring, or whatever. It does this by overriding the objects default .emit event and introducing its own emitter function that does something and then calls back to the original.
Have you ever needed to do something like start logging request response times in your running application? Need to make sure that you are actually cleaning up your connections that you are creating? Want to do it all without restarting your application?
That is why I wrote Hyjack.js. It's goal is to provide you a method of tying into existing event emitter based objects on the fly to change the way your application logs or details information. Combine it with something like statsd and you have a really powerful reporting integration system.
One other think that Hyjack lets you do is intercept (but not repleace) the default methods and prototype methods on existing libraries, and Objects.
Yeah, I know, but there is already a Node module named Hijack (https://www.npmjs.org/package/hijack) but it seems to be dead. In fact it did almost the same thing that Hyjack does.
npm install hyjack
Tests are developed using Mocha. If you want to test make sure you have development dependencies installed and that you have Mocha installed globally.
mocha test
or
npm test
Below are some examples of how you could use Hyjack within your projects. This is not the only things you can do with Hyjack, but they are a good starting point.
Also, make sure and check out the Cookbook at https://github.com/jdarling/hyjack/wiki for more Recipes on how to make use of Hyjack. The Cookbook is still quite young, but I'm open to Pull Requests to add new Recipes and will be adding new Recipes as I create them for my own projects.
Make sure that you require('hyjack') in your code some place then create a new instance of Hyjack. Ideally this would be the first piece of code within your project. Then start using hyjack. Yep, its that easy.
var Hyjack = require('./index');
new Hyjack();
Setup your hyjack.config file as follows:
{
'Find what events are available': {
type: 'event',
method: 'trigger',
unit: 'http',
object: 'ClientRequest',
callback: function(event){
hyjack.emit('console::log', [event, arguments.length]);
}
}
}
Then watch the messages start appearing.
Setup your hyjack.config file as follows:
{
'Log when a socket is connected': {
type: 'event',
method: 'trigger',
unit: 'net',
object: 'Socket',
event: 'connect',
callback: function(){
hyjack.emit('console::log', 'Socket connected');
}
},
'Log when a socket is disconnected': {
type: 'event',
method: 'trigger',
unit: 'net',
object: 'Socket',
event: 'close',
callback: function(){
hyjack.emit('console::log', 'Socket disconnected');
}
}
}
Then watch the messages start appearing.
Setup your hyjack.config file as follows:
{
'Capture all traffic going to the outside world': {
type: 'event',
method: 'timer',
start: {
unit: 'http',
object: 'ClientRequest',
event: 'socket',
callback: function(event, socket){
this.map.set(socket._httpMessage, {
start: this.counter,
started: new Date()
});
}
},
complete: {
unit: 'http',
object: 'Agent',
event: 'free',
callback: function(event, socket){
var conversation = this.map.get(socket._httpMessage);
if(!conversation){
return;
}
// Make sure you cleanup after yourself
this.map.delete(socket._httpMessage);
conversation.complete = this.counter;
conversation.duration = conversation.complete - conversation.start;
conversation.completed = new Date();
try{ // capture the uri component of the req object if it exists
// This same statement could be written as
// conversation.uri =
// (socket._httpMessage.res && socket._httpMessage.res.request)?
// socket._httpMessage.res.request.uri:false;
// this would require two boolean evaluations that should not be
// required since the socket._httpMessage.res.request.uri component
// should exist. If the case were that it was unknown if the component
// would exist then a if or ternary operator would be faster than the
// try/catch implementation.
conversation.uri = socket._httpMessage.res.request.uri;
}catch(e){
}
hyjack.emit('hyjack::log', ['HTTP Response: ', conversation]);
}
}
}
}
Then watch the messages start appearing.
When used properly the impact of Hyjack on a project is minimal. Taking the load test example and running it on an i5 2nd Generation machine with 8GB ram and a 256GB Sata 6 SSD the output is consistently something similar to the following:
No hyjack (min, max, avg, number): 92ms 192ms 139ms 10,000 iterations
With hyjack (min, max, avg, number): 89ms 200ms 143ms 10,000 iterations
That is 10,000 iterations of grabbing the same index page through a local proxy and logging it. This is done using Async eachLimit with a top limit of 10.
Really the important numbers above are the max response time. Without Hyjack this stays around 140ms and with Hyjack it stays around 140ms. This isn't quite correct though, because while we don't impact the caller in version 0.1.x like we did in 0.0.x we still have overhead that is getting processed at some point. The overhead is moved out of cycle using process.nextTick to allow the system to complete any processing that it needs to.
This results in an additional overhead of two new method calls, an Array join, and a closure creation for 0.1.x. On the test machine this adds at worst case an overhead to the call stack of 25 nanoseconds to queue up the handlers for later processing.
v0.1.0
v0.0.x
FAQs
Hyjack.js lets you tie into event emitter objects and do things without changing your code.
The npm package hyjack receives a total of 5 weekly downloads. As such, hyjack popularity was classified as not popular.
We found that hyjack demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Socket CEO Feross Aboukhadijeh joins 10 Minutes or Less, a podcast by Ali Rohde, to discuss the recent surge in open source supply chain attacks.

Research
/Security News
Campaign of 108 extensions harvests identities, steals sessions, and adds backdoors to browsers, all tied to the same C2 infrastructure.

Security News
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.