express-hooked-native
v1.0.0
Published
Express-Hooked as a native N-API module with full feature parity
Maintainers
Keywords
Readme
Express-Hooked-Native
Express-Hooked-Native is a native (N-API) version of the hooks system for Express.js. It provides a high-performance implementation of the WordPress-inspired hooks and filters system, completely written in C++ as a Node.js native extension.
Author
Benjamin Sanchez Cardenas
Features
- High Performance: Native C++ implementation for fast operations
- Full Compatibility: Same API as express-hooked
- Complete Hook System: Actions and filters with namespace support
- Priorities: Control execution order with priority levels
- Identifiers: Hook management by unique identifier
- Full Support: Complete Express.js integration
Installation
npm install express-hooked-nativeBasic Usage
const ExpressHookedNative = require('express-hooked-native');
// Create hook system instance
const hookSystem = new ExpressHookedNative();
// Register an action
hookSystem.addAction('request_started', (req, res) => {
console.log(`Request received: ${req.method} ${req.url}`);
});
// Register a filter
hookSystem.addFilter('modify_response_data', (data) => {
return {
...data,
processedBy: 'ExpressHookedNative',
timestamp: new Date().toISOString()
};
});
// Execute an action
hookSystem.doAction('request_started', req, res);
// Apply a filter
let responseData = { message: 'Hello World!' };
responseData = hookSystem.applyFilters('modify_response_data', responseData);API
addAction(hookName, callback, priority, acceptedArgs, identifier)
Registers an action that executes at a specific point without modifying data.
addFilter(hookName, callback, priority, acceptedArgs, identifier)
Registers a filter that modifies data flowing through the application.
doAction(hookName, ...args)
Executes all registered actions for a specific hook.
applyFilters(hookName, value, ...additionalArgs)
Applies all registered filters for a specific hook to the provided value.
removeAction(hookName, identifier)
Removes a specific action by its identifier.
removeFilter(hookName, identifier)
Removes a specific filter by its identifier.
hasAction(hookName)
Checks if there are registered actions for a hook.
hasFilter(hookName)
Checks if there are registered filters for a hook.
actionsCount(hookName)
Gets the number of registered actions for a hook.
filtersCount(hookName)
Gets the number of registered filters for a hook.
Namespaces
You can organize your hooks using namespaces with the namespace::hookName format:
hookSystem.addAction('mymodule::before_request', (req, res) => {
// Module-specific logic
});
hookSystem.doAction('mymodule::before_request', req, res);Priorities
Control execution order with priority numbers (lower numbers execute first):
// Executes first (high priority)
hookSystem.addAction('request_started', callback, 5);
// Executes later (low priority)
hookSystem.addAction('request_started', callback, 15);Express Integration
To use with Express:
const express = require('express');
const ExpressHookedNative = require('express-hooked-native');
const app = express();
const hookSystem = new ExpressHookedNative();
// Middleware to trigger hooks
app.use((req, res, next) => {
hookSystem.doAction('request_started', req, res);
next();
});
// Example route
app.get('/', (req, res) => {
let data = { message: 'Hello World!' };
data = hookSystem.applyFilters('modify_response', data);
res.json(data);
});
app.listen(3000);License
GPL 3.0
