
Security News
AGENTS.md Gains Traction as an Open Format for AI Coding Agents
AGENTS.md is a fast-growing open format giving AI coding agents a shared, predictable way to understand project setup, style, and workflows.
Middleware like hooks for node.js which is useful for building some relations between entities (ODM, ORM, etc).
npm install mhook
Define your actions, bind hooks on them, trigger actions. When you trigger an action all binded hooks will be sequentially executed. Be careful, hook function should return promise or call provided callback, otherwise trigger will hang.
var Hook = require('mhook').Hook;
var hook = new Hook(['beforeUpdate', 'afterUpdate', 'afterRemove']);
// add hook with callback
hook.on('beforeUpdate', function(done) {
// do something, and then say that you done,
// pass error as first argument to done
done();
});
// add another hook with promise
hook.on('beforeUpdate', function() {
return Promise.resolve();
});
// trigger `action` - executes all two hooks
hook.trigger('beforeUpdate', [], function(err) {
// this function will be called after all
// hooks done or one of them fail
});
// trigger also return promise
hook.trigger('beforeUpdate', []).then(function(err) {
// all hooks successfully done
}).catch(function(err) {
// one of them fail
});
for getting on
and trigger
methods to your object you can inherits from Hook
var Hook = require('mhook').Hook,
inherits = require('util').inherits;
function Model() {
// apply parent cunstructor
Hook.call(this, ['beforeUpdate', 'afterUpdate', 'afterRemove']);
}
// inherits from Hook
inherits(Model, Hook);
// now we can use `on` and `trigger` as own methods
Model.prototype.update = function(obj, callback) {
this.trigger('beforeUpdate', [obj], function(err) {
if (err) {callback(err); return}
// update
// trigger afterUpdate, etc
});
};
var model = new Model();
model.on('beforeUpdate', function(obj, done) {
console.log('before update object: ', obj);
done();
});
Hook constructor
accepts array of string actions
- possible actions which could be used
at on
and trigger
Bind hook
on action
Trigger some action
with hookArgs
(arguments which will be passed to
every hook function).
If callback
(accepts error as first argument) function presents it will be
called after hooks execution.
Return promise which will be resolved after hooks execution.
Into cloned repository run npm run test
FAQs
Middleware like hooks for node.js
The npm package mhook receives a total of 4,126 weekly downloads. As such, mhook popularity was classified as popular.
We found that mhook demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers 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
AGENTS.md is a fast-growing open format giving AI coding agents a shared, predictable way to understand project setup, style, and workflows.
Security News
/Research
Malicious npm package impersonates Nodemailer and drains wallets by hijacking crypto transactions across multiple blockchains.
Security News
This episode explores the hard problem of reachability analysis, from static analysis limits to handling dynamic languages and massive dependency trees.