Security News
Node.js EOL Versions CVE Dubbed the "Worst CVE of the Year" by Security Experts
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Born from a desire to have an API / hooks system that:
Credit to the kareem & grappling-hook projects for their insight and the inspiration they provided (also some code ;).)
Before hooks are called before a given function executes. They receve in their method sig all the params the hooked method receves and can augment them.
// Here, false marks the hook as synchronous
hooks.before('cook', false, function(food) {
// food == 'burger'
assert.equal('burger', food);
// your code here
// synchronous hooks can NOT augment params passed to hooked methods
});
var cook = hooks.createHook('cook', function(food) {
// This is the method you want to hook
done();
});
// When you call the now-hooked method, the hooks will be called
cook('burger');
Asynchronous is the default
hooks.before('cook', function(food, next) {
assert.equal('burger', food);
// food = burger
food = 'hotdog';
// your code here
next(food);
});
var cook = hooks.createHook('cook', function(food) {
// food = hotdog
assert.equal('hotdog', food);
done();
// This is the method you want to hook
});
// When you call the now-hooked method, the hooks will be called
cook('burger');
var count1 = 0;
var count2 = 0;
hooks.before('cook', function(next) {
++count1;
next();
});
hooks.before('cook', function(next) {
++count2;
next();
});
var cook = hooks.createHook('cook', function() {
assert.equal(1, count1);
assert.equal(1, count2);
done();
});
cook();
// Here, false marks the hook as synchronous
hooks.after('cook', false, function(feelings) {
// feelings == 'I LOVE BURGERS'
assert.equal('I LOVE BURGERS', feelings);
done();
});
var cook = hooks.createHook('cook', function(food) {
return 'I LOVE BURGERS';
});
// When you call the now-hooked method, the hooks will be called
cook('burger');
Asynchronous is the default
hooks.after('cook', function(feelings, feelings2, next) {
// feelings == 'I LOVE BURGERS'
// feelings2 == 'I hate fries'
assert.equal('I LOVE BURGERS', feelings);
assert.equal('I hate fries', feelings2);
next('Burgers are OK');
});
var cook = hooks.createHook('cook', function(food, callback) {
callback('I LOVE BURGERS', 'I hate fries');
});
// When you call the now-hooked method, the hooks will be called
cook('burger', function(feelings, feelings2) {
// feelings == 'Burgers are OK'
// feelings2 == 'I hate fries'
assert.equal('Burgers are OK', feelings);
assert.equal('I hate fries', feelings2);
done();
});
var count1 = 0;
var count2 = 0;
hooks.after('cook', function(next) {
++count1;
next();
});
hooks.after('cook', function(next) {
++count2;
next();
});
var cook = hooks.createHook('cook', function(cb) {
cb();
});
cook(function() {
assert.equal(1, count1);
assert.equal(1, count2);
done();
});
// Here, false marks the hook as synchronous
hooks.after('cook', false, function(feelings) {
// feelings == 'I LOVE BURGERS'
assert.equal('I LOVE BURGERS', feelings);
done();
});
// first param is the hook name, next is an array of arguments, next is a flag for is the call immutable and lastly is your hooked method/code
hooks.callHook('cook', ['burger'], true, function(food) {
return 'I LOVE BURGERS';
});
FAQs
Simple, stable, easy to use system for pre/post hooks in NodeJS
We found that mooring 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
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.