mirror of
https://github.com/cool-team-official/cool-admin-midway-packages.git
synced 2025-12-13 23:02:49 +00:00
更高效的自动路由方式
This commit is contained in:
parent
2afd61606b
commit
e94ee25d8d
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@cool-midway/core",
|
"name": "@cool-midway/core",
|
||||||
"version": "7.1.21",
|
"version": "7.1.22",
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"typings": "index.d.ts",
|
"typings": "index.d.ts",
|
||||||
@ -61,4 +61,4 @@
|
|||||||
"uuid": "^9.0.1",
|
"uuid": "^9.0.1",
|
||||||
"ws": "^8.16.0"
|
"ws": "^8.16.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -12,7 +12,6 @@ import { Configuration } from "@midwayjs/decorator";
|
|||||||
import * as DefaultConfig from "./config/config.default";
|
import * as DefaultConfig from "./config/config.default";
|
||||||
import { CoolExceptionFilter } from "./exception/filter";
|
import { CoolExceptionFilter } from "./exception/filter";
|
||||||
import { FuncUtil } from "./util/func";
|
import { FuncUtil } from "./util/func";
|
||||||
import location from "./util/location";
|
|
||||||
import * as koa from "@midwayjs/koa";
|
import * as koa from "@midwayjs/koa";
|
||||||
import { CoolModuleConfig } from "./module/config";
|
import { CoolModuleConfig } from "./module/config";
|
||||||
import { CoolModuleImport } from "./module/import";
|
import { CoolModuleImport } from "./module/import";
|
||||||
@ -54,11 +53,11 @@ export class CoolConfiguration implements ILifeCycle {
|
|||||||
|
|
||||||
// 缓存设置为全局
|
// 缓存设置为全局
|
||||||
// global["COOL-CACHE"] = await container.getAsync(CacheManager);
|
// global["COOL-CACHE"] = await container.getAsync(CacheManager);
|
||||||
// 清除 location
|
// // 清除 location
|
||||||
setTimeout(() => {
|
// setTimeout(() => {
|
||||||
location.clean();
|
// location.clean();
|
||||||
this.coreLogger.info("\x1B[36m [cool:core] location clean \x1B[0m");
|
// this.coreLogger.info("\x1B[36m [cool:core] location clean \x1B[0m");
|
||||||
}, 10000);
|
// }, 10000);
|
||||||
}
|
}
|
||||||
|
|
||||||
async onConfigLoad(
|
async onConfigLoad(
|
||||||
@ -75,6 +74,6 @@ export class CoolConfiguration implements ILifeCycle {
|
|||||||
const eps: CoolEps = await container.getAsync(CoolEps);
|
const eps: CoolEps = await container.getAsync(CoolEps);
|
||||||
eps.init();
|
eps.init();
|
||||||
this.coolEventManager.emit("onServerReady");
|
this.coolEventManager.emit("onServerReady");
|
||||||
location.clean();
|
// location.clean();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -156,7 +156,7 @@ export function CoolController(
|
|||||||
res.path.split(`modules/${module}`)[0]
|
res.path.split(`modules/${module}`)[0]
|
||||||
}modules/${module}/config.${_.endsWith(res.path, "ts") ? "ts" : "js"}`;
|
}modules/${module}/config.${_.endsWith(res.path, "ts") ? "ts" : "js"}`;
|
||||||
if (os.type() == "Windows_NT") {
|
if (os.type() == "Windows_NT") {
|
||||||
path = path.substr(1);
|
path = path.substring(1);
|
||||||
}
|
}
|
||||||
if (fs.existsSync(path)) {
|
if (fs.existsSync(path)) {
|
||||||
const config: ModuleConfig = require(path).default();
|
const config: ModuleConfig = require(path).default();
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@cool-midway/core",
|
"name": "@cool-midway/core",
|
||||||
"version": "7.1.21",
|
"version": "7.1.22",
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"typings": "index.d.ts",
|
"typings": "index.d.ts",
|
||||||
@ -59,4 +59,4 @@
|
|||||||
"ws": "^8.16.0",
|
"ws": "^8.16.0",
|
||||||
"pm2": "^5.3.1"
|
"pm2": "^5.3.1"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1,95 +1,52 @@
|
|||||||
import { Session } from 'inspector';
|
|
||||||
import { v1 as uuid } from 'uuid';
|
|
||||||
import * as util from 'util';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Location 工具类
|
* Location 工具类
|
||||||
*/
|
*/
|
||||||
class LocationUtil {
|
class LocationUtil {
|
||||||
static instance = null;
|
private locationCache = new Map<string, any>();
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获得脚本位置
|
* 获取目标类的定义位置
|
||||||
* @param target
|
* @param target 目标类
|
||||||
|
* @returns 目标类的定义位置
|
||||||
*/
|
*/
|
||||||
async scriptPath(target: any) {
|
async scriptPath(target: any) {
|
||||||
const id = uuid();
|
const targetName = target.name;
|
||||||
global[this.PREFIX][id] = target;
|
|
||||||
const evaluated = await this.post$('Runtime.evaluate', {
|
// 检查缓存
|
||||||
expression: `global['${this.PREFIX}']['${id}']`,
|
if (this.locationCache.has(targetName)) {
|
||||||
objectGroup: this.PREFIX,
|
return this.locationCache.get(targetName);
|
||||||
});
|
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this.session = null;
|
const originalPrepareStackTrace = Error.prepareStackTrace;
|
||||||
this.post$ = null;
|
let targetFile;
|
||||||
this.scripts = null;
|
|
||||||
delete global[this.PREFIX];
|
try {
|
||||||
LocationUtil.instance = null;
|
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();
|
export default new LocationUtil();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user