npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

aspects-js

v1.0.2

Published

use aop in nodejs

Downloads

12

Readme

aspects-js

aspects-js

中文

Use aspect in node js

1.Install

Just install by npm

$ npm install --save aspects-js

2.Usage

You need require aspects-js at first of entry js file

require('aspects-js');

3.Add aspect

Add a js file to write an aspect. First, you should require class Aspect from aspects-js.

//file: testAspect.js
const { Aspect } = require('aspects-js');

Secondly, you should declare a class extends Aspect and implements property pointcut and functions for join point.

//file: testAspect.js
class TestAspect extends Aspect {
    get pointcut() { return '*.do*()' },
    before() { console.log('this is for before join point') },
    after() { console.log('this is for after join point') }
}

Then, you should exports an instance of your class which is extends Aspect

//file: testAspect.js
module.exports = new TestAspect();

At last, require your aspects at entry.js file

//file: entry.js
require('./testAspect.js');

Now, all classes when you required will be cut by all your aspects.

4.Classes and Interfaces

Interface Aspect

interface Aspect {
    readonly pointcut: Pointcut | string | ((joinPoint: JoinPoint) => boolean);
    readonly order: number;

    after(joinPoint: JoinPoint, result: any, error: Error);
    afterReturn(joinPoint: JoinPoint, result: any): any;
    afterThrow(joinPoint: JoinPoint, error: Error): void;
    before(joinPoint: JoinPoint):void;
    around(joinPoint: JoinPoint): any;
}

Class JoinPoint

class JoinPoint {
    readonly type: Class;
    readonly fun: Function;
    readonly thisArg: any;
    readonly target: any;
    readonly args: any[];

    proceed(...args: any[]): any;
}

Class Pointcut

class Pointcut {
    constructor(pointcut: string);

    matches(joinPoint: JoinPoint): boolean;
}

5.Pointcut expression

1.Normal

"ClassName.FunctionName()"

2.Keyword execution

"execution(ClassName.FunctionName())"

3.Keyword within

"within(ClassName)"

4.Arguments

"FunctionName(..)"
"FunctionName(Type1,Type2)"
"FunctionName(Type1,..,Type2)"

4.Operators and wildcards

> * Match all word wildcards

"*Service.do*()"

Match all methods which's a name is start with do and in classes which's a name is end with Service

> ? Match one word wildcards

"you?.do?()"

> + Or operate for name

"within(Test1+Test2)"

Just match all methods in classes which's a name is Test1 or Test2

> |,|| Or operate for condition

"within(Test1)|within(Test2)"

Just match all methods in classes which's a name is Test1 or Test2

> &,&& And operator for condition

"within(Test1)&abc"

Just match method abc in class Test1

> ! Not operate for condition

"!within(Test)"

match all methods except the methods in class Test

> () Brackets operator for condition

Increase the priority of expressions

> () Call operator for function

"abc()"
"abc(..)"

Match all methods which's a name is abc

"abc(Type1)"

Match all methods which's a name is abc and has one argument that instance of class Type1

> , Split operator for arguments

"*(Type1,Type2)"

Match all methods which has two arguments that then first one is instance of class Type1 and the second one is instance of class Type2

> . Property operator for search methods of classes

"Test.abc()"

Match the method abc of class Test

> .. Multiple arguments operator for arguments

Match none or multiple arguments

5.Others

order property of interface Aspects

The Higher precedence when the value is lower

6.Updates and releases

1.0.2.pre

support function for pointcut add property order for class Aspects

1.0.1

use AST for pointcut