Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
aspect.js
Advanced tools
Readme
Library for aspect-oriented programming with JavaScript, which takes advantage of ECMAScript 2016 decorators syntax.
NOTE: if you are using
aspect.js
in a plain JavaScript project that uses@babel/plugin-proposal-decorators
, you must set itslegacy
property totrue
until #72 is fixed. See this migration note for more information. A sample project using JavaScript with Node >=10.12.0 (or >=8.12.0) & Babel 7.x can be found at https://github.com/matthewadams/aspect.js-babel7-poc. It's a good way to get going quickly withaspect.js
in that environment.
For further reading on decorators, take a look at the spec.
Blog post, introduction to the AOP and the library could be found here.
Talk from AngularConnect.
import {beforeMethod, Advised, Metadata} from 'aspect.js';
class LoggerAspect {
@beforeMethod({
classNamePattern: /^Article/,
methodNamePattern: /^(get|set)/
})
invokeBeforeMethod(meta: Metadata) {
// meta.advisedMetadata == { bar: 42 }
console.log(`Inside of the logger. Called ${meta.className}.${meta.method.name} with args: ${meta.method.args.join(', ')}.`);
}
}
class Article {
id: number;
title: string;
content: string;
}
@Advised({ bar: 42 })
class ArticleCollection {
articles: Article[] = [];
getArticle(id: number) {
console.log(`Getting article with id: ${id}.`);
return this.articles.filter(a => {
return a.id === id;
}).pop();
}
setArticle(article: Article) {
console.log(`Setting article with id: ${article.id}.`);
this.articles.push(article);
}
}
new ArticleCollection().getArticle(1);
// Result:
// Inside of the logger. Called ArticleCollection.getArticle with args: 1.
// Getting article with id: 1.
In case you're using aspect.js in a browser environment the minifier may break the annotated code because of the performed mangling. In order to handle this problem you can use:
class LoggerAspect {
@beforeMethod({
classes: [ArticleCollection],
methods: [ArticleCollection.prototype.getArticle, ArticleCollection.prototype.setArticle]
})
invokeBeforeMethod(meta: Metadata) {
// meta.advisedMetadata == { bar: 42 }
console.log(`Inside of the logger. Called ${meta.className}.${meta.method.name} with args: ${meta.method.args.join(', ')}.`);
}
}
class ArticleCollection {
getArticle(id: number) {...}
setArticle(article: Article) {...}
}
In this case you can omit the @Advised
decorator.
This way, by explicitly listing the classes and methods which should be woven, you can prevent the unwanted effect of mangling.
git clone https://github.com/mgechev/aop.js --depth 1
npm install -g ts-node
ts-node demo/index.ts
The library offers the following combinations of advices and join points:
beforeMethod(MethodSelector)
- invoked before method callafterMethod(MethodSelector)
- invoked after method callaroundMethod(MethodSelector)
- invoked around method callonThrowOfMethod(MethodSelector)
- invoked on throw of method callasyncOnThrowOfMethod(MethodSelector)
- invoked on throw of async method callbeforeStaticMethod(MethodSelector)
- invoked before static method callafterStaticMethod(MethodSelector)
- invoked after static method callaroundStaticMethod(MethodSelector)
- invoked around static method callonThrowOfStaticMethod(MethodSelector)
- invoked on throw of static method callasyncOnThrowOfStaticMethod(MethodSelector)
- invoked on throw of async static method callbeforeSetter(PropertySelector)
- invoked before setter callafterSetter(PropertySelector)
- invoked after setter callaroundSetter(PropertySelector)
- invoked around setter callonThrowOfSetter(PropertySelector)
- invoked on throw of setter callasyncOnThrowOfSetter(PropertySelector)
- invoked on throw of async setter callbeforeGetter(PropertySelector)
- invoked before getter callafterGetter(PropertySelector)
- invoked after getter callaroundGetter(PropertySelector)
- invoked around getter callonThrowOfGetter(PropertySelector)
- invoked on throw of getter callasyncOnThrowOfGetter(PropertySelector)
- invoked on throw of async getter callMethodSelector
export interface MethodSelector {
classNamePattern?: RegExp;
methodNamePattern?: RegExp;
classes?: Function[];
methods?: Function[];
}
PropertySelector
export interface PropertySelector {
classNamePattern?: RegExp;
propertyNamePattern?: RegExp;
classes?: Function[];
properties?: PropertyDescriptor[];
}
Metadata
export class Metadata {
public method: MethodMetadata;
public className: string;
public advisedMetadata: any;
}
MethodMetadata
export class MethodMetadata {
public proceed: boolean;
public name: string;
public args: any[];
public context: any;
public result: any;
public exception: any;
public invoke: (...args: any[]) => any;
}
Here's a UML class diagram which shows the relations between the individual abstractions:
MIT
FAQs
Library for aspect-oriented programming with JavaScript
The npm package aspect.js receives a total of 1,572 weekly downloads. As such, aspect.js popularity was classified as popular.
We found that aspect.js 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
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.