@zenweb/core
Version:
ZenWeb Core Module - Module loader and Server
87 lines (86 loc) • 2.37 kB
JavaScript
import Debug from 'debug';
;
/**
* zenweb debug
*/
export const debug = createDebug('zenweb');
/**
* 扩展 zenweb debug
* @param namespace 命名空间
*/
export function zenwebDebug(namespace) {
return namespace ? debug.extend(namespace) : debug;
}
/**
* 创建一个新的 debug 对象
* @param namespace 命名空间
* @returns
*/
export function createDebug(namespace) {
return Debug(namespace);
}
/**
* 取得调用栈中位置信息,例如文件位置
* @param stackIndex 第几层
*/
export function getStackLocation(stackIndex = 3) {
const stack = new Error().stack?.split('\n')[stackIndex];
if (stack) {
// Stack trace format :
// https://v8.dev/docs/stack-trace-api
return stack.slice(stack.indexOf('(') + 1, stack.lastIndexOf(')'));
}
}
/**
* 调用代理
* - 当对象中属性或方法被调用时通过 `call` 取得对象并返回被调用方法
* @param call 返回对象实例
*/
export function callProxy(call) {
return new Proxy({}, {
getPrototypeOf() {
return Object.getPrototypeOf(call());
},
setPrototypeOf(target, v) {
return Object.setPrototypeOf(call(), v);
},
isExtensible() {
return Object.isExtensible(call());
},
preventExtensions() {
return Object.preventExtensions(call());
},
getOwnPropertyDescriptor(target, p) {
return Object.getOwnPropertyDescriptor(call(), p);
},
defineProperty(target, property, attributes) {
return Object.defineProperty(call(), property, attributes);
},
has(target, p) {
const ins = call();
return p in ins;
},
get(target, p, receiver) {
const ins = call();
if (p in ins) {
const _p = ins[p];
if (typeof _p === 'function') {
_p.bind(ins);
}
return _p;
}
},
set(target, p, newValue, receiver) {
const ins = call();
ins[p] = newValue;
return true;
},
deleteProperty(target, p) {
const ins = call();
return delete ins[p];
},
ownKeys() {
return Reflect.ownKeys(call());
},
});
}