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

function-interceptor

v1.0.5

Published

nodejs 拦截器

Downloads

9

Readme

function-interceptor

Javascript 函数拦截器

Installation

$ npm install function-interceptor

创建拦截器对象

object为对应的监听对象 const interceptor = new Interceptor(object,type,key);

object: 必填 监听的对象
type: 必填 类型,包含下面三种类型
    class: 通过class或原型+构造函数创建的对象
    factory: 通过工厂模式创建的一级嵌套对象
    story: 通过工厂模式创建的二级嵌套对象
key: 可选 当type=story时候对应的方法的key 默认值为resolve

添加完监视器后把添加的原型方法删除

interceptor.release();

example

监听Array

const Interceptor = require('function-interceptor');

const interceptor = new Interceptor(Array);
interceptor.monitorPrototypeName('push',function (data) {
	console.log(data.name + ' before');
},function (data) {
	console.log(data.name + ' after');
});
interceptor.release();

const list = [1];

list.push('2');
console.log(list.toString())

监听自定义对象

const Interceptor = require('function-interceptor');
class Test {
	constructor() {

	}
	func1(params){
		console.log('func1 start');
		return params + 1;
	}
	static func2(params) {
		console.log('func2 start');
		return new Promise(resolve => {
			setTimeout(() => {
				resolve(params + 1);
			},1000)
		})
	}
}
const interceptor = new Interceptor(Test);
interceptor.monitorPrototypeName('func1',function (data) {
	console.log(data.name + ' before');
});
interceptor.monitorStaticAll(function (data) {
	console.log(data.name + ' before');
},function (data) {
	console.log(data.name + ' after,result=', data.result);
},true);
interceptor.release();
const test = new Test();
console.log('===============', test.func1(1));
(async function () {
    console.log(await Test.func2(2));
})();
// output -----
// func1 before
// func2 before
// func1 start
// func2 start
// func1 after,result= 2
// func2 after,result= 3

监听通过工厂模式创建的一级嵌套对象的函数

const Interceptor = require('function-interceptor');
const jsonObject = {
    query() {
        return 'yes';
    },
    move: function () {
        return new Promise(resolve => {
            resolve('yes');
        })
    }
};

const interceptor = new Interceptor(jsonObject, 'json');
interceptor.monitorStaticName('query', function ({name, args}) {
    console.log(name, args);
}, function ({name, args, result}) {
    console.log(name, args, result);
}, false);

interceptor.monitorStaticName('move', function ({name, args}) {
    console.log(name, args);
}, function ({name, args, result}) {
    console.log(name, args, result);
}, true);

interceptor.release();


jsonObject.query(123);
jsonObject.move(123, 124);
// output -----
// query { '0': 123 }
// query { '0': 123 } yes
// move { '0': 123, '1': 124 }
// move { '0': 123, '1': 124 } yes

监听通过工厂模式创建的二级嵌套对象的函数 如graphql

const Interceptor = require('../index');
const jsonObject = {
    queryData: {
        name: 'query',
        description: '查询某个数据',
        resolve(){
            return 'yes';
        }
    }

};

const interceptor = new Interceptor(jsonObject, 'story');
interceptor.monitorStaticName('queryData', function ({name, args}) {
    console.log(name, args);
}, function ({name, args, result}) {
    console.log(name, args, result);
}, false);


interceptor.release();


jsonObject.queryData.resolve(123);
// output -----
// queryData { '0': 123 }
// queryData { '0': 123 } yes

method

name: 方法名称 beforeCallback: 调用方法前执行的的回调函数 支持基于promise异步操作 afterCallback: 调用方法后执行的的回调函数 支持基于promise异步操作 isAsync: 函数为异步执行 执行时候需要 await 或者then进行执行 返回值: 设置成功 返回true 失败false

monitorPrototypeName(name, beforeCallback, afterCallback, isAsync)

按指定名称对原型方法进行监听

monitorPrototypeRe(re, beforeCallback, afterCallback, isAsync)

按正则表达式对原型方法名称进行监听

monitorPrototypeAll(name, beforeCallback, afterCallback, isAsync)

所有原型方法进行监听

monitorStaticName(name, beforeCallback, afterCallback, isAsync)

按指定名称对静态方法进行监听

monitorStaticRe(re, beforeCallback, afterCallback, isAsync)

按正则表达式对静态方法名称进行监听

monitorStaticAll(name, beforeCallback, afterCallback, isAsync)

所有静态方法进行监听

monitorAll(beforeCallback, afterCallback, isAsync)

匹配所有的方法

release()

删除monitor原型方法