Metal.js AOP Utility
metal-aop is a utility package that provides Aspect-Oriented Programming (AOP) capabilities specifically for applications built with the Metal.js framework. AOP allows developers to modularize cross-cutting concerns such as logging, security, and transaction management, by defining advice (code to be executed) and applying it at specific join points (execution points) in the application's flow. The package's latest stable version is 3.0.0, released approximately eight years ago. However, the underlying Metal.js framework has been officially deprecated by Liferay, its primary maintainer, with no active staffing for open-source support and no future plans to use the framework. Consequently, metal-aop is effectively an abandoned project, meaning it receives no new features, bug fixes, or security patches. Its differentiators were primarily its tight integration with Metal.js's component model, which is no longer actively developed.
Common errors
-
TypeError: Aop.before is not a function
cause The Aop object was not correctly imported or is not available in the scope, or the `before` method does not exist on the imported object.fixEnsure `const Aop = require('metal-aop');` is at the top of your file and that `metal-aop` is correctly installed. Verify the AOP object provides a `before` method as expected. -
Cannot read properties of undefined (reading 'doSomething')
cause The target object passed to an AOP method (e.g., `Aop.before`) is `undefined` or `null`, meaning the aspect is trying to attach to a non-existent object.fixEnsure the object you are trying to apply AOP to (e.g., `myServiceInstance`) is properly instantiated and not `null` or `undefined` before calling any `Aop` methods on it.
Warnings
- breaking The Metal.js framework, which metal-aop is built upon, has been officially deprecated. This means metal-aop is effectively abandoned and will not receive updates, bug fixes, or security patches. Running this library in modern environments without careful consideration may lead to compatibility issues or unpatched vulnerabilities.
- gotcha Compatibility with modern JavaScript (ESM, async/await, newer Node.js versions) is not guaranteed due to the library's age and abandoned status. Its design predates many modern language features and module systems.
- gotcha Integrating `metal-aop` with other modern frontend libraries or frameworks (e.g., React, Vue, Angular) is highly discouraged. It was designed for the Metal.js ecosystem and may cause unexpected conflicts or performance issues outside of it.
Install
-
npm install metal-aop -
yarn add metal-aop -
pnpm add metal-aop
Imports
- Aop
import { Aop } from 'metal-aop';const Aop = require('metal-aop'); - Aspect
import Aspect from 'metal-aop';
const { Aspect } = require('metal-aop'); - before
import { before } from 'metal-aop';const { before } = require('metal-aop/src/aop'); // Example for a specific advice type
Quickstart
const Aop = require('metal-aop');
class MyService {
doSomething(data) {
console.log(`Processing data: ${data}`);
return `Result for ${data}`;
}
anotherMethod() {
console.log('Another method called.');
}
}
const myServiceInstance = new MyService();
// Define a 'before' advice
Aop.before(myServiceInstance, 'doSomething', function(originalArgs) {
console.log(`[AOP-Before] Preparing to call doSomething with: ${originalArgs[0]}`);
});
// Define an 'after' advice
Aop.after(myServiceInstance, 'doSomething', function(originalArgs, result) {
console.log(`[AOP-After] doSomething finished. Original args: ${originalArgs[0]}, Result: ${result}`);
});
// Define an 'around' advice
Aop.around(myServiceInstance, 'anotherMethod', function(originalFn) {
console.log('[AOP-Around] Before calling anotherMethod');
const result = originalFn(); // Call the original method
console.log('[AOP-Around] After calling anotherMethod');
return result;
});
console.log('--- Calling doSomething ---');
myServiceInstance.doSomething('input123');
console.log('\n--- Calling anotherMethod ---');
myServiceInstance.anotherMethod();