更高效的自动路由方式

This commit is contained in:
COOL 2024-12-27 19:06:53 +08:00
parent 2afd61606b
commit e94ee25d8d
5 changed files with 49 additions and 93 deletions

View File

@ -1,6 +1,6 @@
{
"name": "@cool-midway/core",
"version": "7.1.21",
"version": "7.1.22",
"description": "",
"main": "dist/index.js",
"typings": "index.d.ts",
@ -61,4 +61,4 @@
"uuid": "^9.0.1",
"ws": "^8.16.0"
}
}
}

View File

@ -12,7 +12,6 @@ import { Configuration } from "@midwayjs/decorator";
import * as DefaultConfig from "./config/config.default";
import { CoolExceptionFilter } from "./exception/filter";
import { FuncUtil } from "./util/func";
import location from "./util/location";
import * as koa from "@midwayjs/koa";
import { CoolModuleConfig } from "./module/config";
import { CoolModuleImport } from "./module/import";
@ -54,11 +53,11 @@ export class CoolConfiguration implements ILifeCycle {
// 缓存设置为全局
// global["COOL-CACHE"] = await container.getAsync(CacheManager);
// 清除 location
setTimeout(() => {
location.clean();
this.coreLogger.info("\x1B[36m [cool:core] location clean \x1B[0m");
}, 10000);
// // 清除 location
// setTimeout(() => {
// location.clean();
// this.coreLogger.info("\x1B[36m [cool:core] location clean \x1B[0m");
// }, 10000);
}
async onConfigLoad(
@ -75,6 +74,6 @@ export class CoolConfiguration implements ILifeCycle {
const eps: CoolEps = await container.getAsync(CoolEps);
eps.init();
this.coolEventManager.emit("onServerReady");
location.clean();
// location.clean();
}
}

View File

@ -156,7 +156,7 @@ export function CoolController(
res.path.split(`modules/${module}`)[0]
}modules/${module}/config.${_.endsWith(res.path, "ts") ? "ts" : "js"}`;
if (os.type() == "Windows_NT") {
path = path.substr(1);
path = path.substring(1);
}
if (fs.existsSync(path)) {
const config: ModuleConfig = require(path).default();

View File

@ -1,6 +1,6 @@
{
"name": "@cool-midway/core",
"version": "7.1.21",
"version": "7.1.22",
"description": "",
"main": "index.js",
"typings": "index.d.ts",
@ -59,4 +59,4 @@
"ws": "^8.16.0",
"pm2": "^5.3.1"
}
}
}

View File

@ -1,95 +1,52 @@
import { Session } from 'inspector';
import { v1 as uuid } from 'uuid';
import * as util from 'util';
/**
* Location
*/
class LocationUtil {
static instance = null;
session: Session;
PREFIX = '__functionLocation__';
scripts = {};
post$ = null;
constructor() {
if (!LocationUtil.instance) {
this.init();
LocationUtil.instance = this;
}
return LocationUtil.instance;
}
init() {
if (!global[this.PREFIX]) {
global[this.PREFIX] = {};
}
if (this.session) {
return;
}
this.session = new Session();
this.session.connect();
this.post$ = util.promisify(this.session.post).bind(this.session);
this.session.on('Debugger.scriptParsed', res => {
this.scripts[res.params.scriptId] = res.params;
LocationUtil.instance = this;
});
this.post$('Debugger.enable');
LocationUtil.instance = this;
}
private locationCache = new Map<string, any>();
/**
*
* @param target
*
* @param target
* @returns
*/
async scriptPath(target: any) {
const id = uuid();
global[this.PREFIX][id] = target;
const evaluated = await this.post$('Runtime.evaluate', {
expression: `global['${this.PREFIX}']['${id}']`,
objectGroup: this.PREFIX,
});
const properties = await this.post$('Runtime.getProperties', {
objectId: evaluated.result.objectId,
});
const location = properties.internalProperties.find(
prop => prop.name === '[[FunctionLocation]]'
);
const script = this.scripts[location.value.value.scriptId];
delete global[this.PREFIX][id];
let source = decodeURI(script.url);
if (!source.startsWith('file://')) {
source = `file://${source}`;
}
return {
column: location.value.value.columnNumber + 1,
line: location.value.value.lineNumber + 1,
path: source.substr(7),
source,
};
}
/**
*
*/
async clean() {
if (this.session) {
await this.post$('Runtime.releaseObjectGroup', {
objectGroup: this.PREFIX,
});
this.session.disconnect();
const targetName = target.name;
// 检查缓存
if (this.locationCache.has(targetName)) {
return this.locationCache.get(targetName);
}
this.session = null;
this.post$ = null;
this.scripts = null;
delete global[this.PREFIX];
LocationUtil.instance = null;
const originalPrepareStackTrace = Error.prepareStackTrace;
let targetFile;
try {
Error.prepareStackTrace = (error, stack) => stack;
const stack = new Error().stack as any;
for (const site of stack) {
const fileName = site.getFileName();
if (!fileName) continue;
const content = require('fs').readFileSync(fileName, 'utf-8');
if (content.includes(`class ${targetName}`)) {
targetFile = {
path: fileName,
line: site.getLineNumber(),
column: site.getColumnNumber(),
source: `file://${fileName}`
};
this.locationCache.set(targetName, targetFile);
break;
}
}
} finally {
Error.prepareStackTrace = originalPrepareStackTrace;
}
return targetFile;
}
}
// 不再需要单例模式,直接导出实例即可
export default new LocationUtil();