feat: 优化路径寻找逻辑

Signed-off-by: MiaoWoo <admin@yumc.pw>
master
MiaoWoo 2020-12-17 16:17:16 +08:00
parent 824c440f63
commit 51fc4549da
4 changed files with 69 additions and 28 deletions

View File

@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>pw.yumc</groupId> <groupId>pw.yumc</groupId>
<artifactId>MiaoScript</artifactId> <artifactId>MiaoScript</artifactId>
<version>0.9.7</version> <version>0.10.0</version>
<developers> <developers>
<developer> <developer>
<id>502647092</id> <id>502647092</id>
@ -54,7 +54,10 @@
<properties> <properties>
<env.GIT_COMMIT>DEV</env.GIT_COMMIT> <env.GIT_COMMIT>DEV</env.GIT_COMMIT>
<update.changes> <update.changes>
§620-12-07 §afix: 修复 Windows 环境 重载异常; §620-12-17 §afeat: JavaScriptTask 新增任务ID 并通过ID比较优先级;
§620-12-16 §afeat: 优化 require 路径寻找速度;
§620-12-15 §cfix: 修复 非主线程重载时发生的异常;
§620-12-07 §cfix: 修复 Windows 环境 重载异常;
§620-11-19 §afeat: 新增 JavaScriptTask 类; §620-11-19 §afeat: 新增 JavaScriptTask 类;
§620-11-11 §afeat: 新增 package 版本锁定逻辑; §620-11-11 §afeat: 新增 package 版本锁定逻辑;
§620-09-21 §afeat: 完善 upgrade 逻辑; §620-09-21 §afeat: 完善 upgrade 逻辑;

View File

@ -4,11 +4,17 @@ import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
public class JavaScriptTask implements Delayed { public class JavaScriptTask implements Delayed {
private final long id;
private final Object task; private final Object task;
private final long startTime; private final long startTime;
private final long executeTime; private final long executeTime;
public JavaScriptTask(Object task, long ms) { public JavaScriptTask(Object task, long ms) {
this(0, task, ms);
}
public JavaScriptTask(long id, Object task, long ms) {
this.id = id;
this.task = task; this.task = task;
this.startTime = System.currentTimeMillis(); this.startTime = System.currentTimeMillis();
this.executeTime = ms; this.executeTime = ms;
@ -22,7 +28,16 @@ public class JavaScriptTask implements Delayed {
@Override @Override
public int compareTo(Delayed delayed) { public int compareTo(Delayed delayed) {
JavaScriptTask task = (JavaScriptTask) delayed; JavaScriptTask task = (JavaScriptTask) delayed;
return (int) ((this.startTime + this.executeTime) - (task.getStartTime() + task.getExecuteTime())); int delay = (int) ((this.startTime + this.executeTime) - (task.getStartTime() + task.getExecuteTime()));
if (delay != 0) {
return delay;
} else {
return (int) (this.id - task.getId());
}
}
public long getId() {
return this.id;
} }
public Object getTask() { public Object getTask() {

View File

@ -39,9 +39,9 @@ public class ScriptEngine {
@SneakyThrows @SneakyThrows
public void enableEngine() { public void enableEngine() {
createEngine();
ClassLoader originLoader = Thread.currentThread().getContextClassLoader(); ClassLoader originLoader = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(this.loader); Thread.currentThread().setContextClassLoader(this.loader);
createEngine();
Path bios = Paths.get(root, "bios.js"); Path bios = Paths.get(root, "bios.js");
// 如果存在自定义bios就加载自定义的 // 如果存在自定义bios就加载自定义的
if (Files.exists(bios)) { if (Files.exists(bios)) {

View File

@ -125,22 +125,20 @@
* 寻找 ${NODE_PATH} * 寻找 ${NODE_PATH}
* @param {string} name 模块名称 * @param {string} name 模块名称
* @param {string} parent 父目录 * @param {string} parent 父目录
* @param {any} optional 附加参数
*/ */
function resolve(name, parent) { function resolve(name, parent, optional) {
name = _canonical(name) || name name = _canonical(name) || name
if (cacheModuleIds[name]) return cacheModuleIds[name]
// 解析本地目录 // 解析本地目录
if (name.startsWith('./') || name.startsWith('../')) { if (optional.local) {
var moduleId = parent + '/' + name return resolveAsFile(name, parent) || resolveAsDirectory(name, parent) || undefined
if (cacheModuleIds[moduleId]) return cacheModuleIds[moduleId]
return cacheModuleIds[moduleId] = resolveAsFile(name, parent) || resolveAsDirectory(name, parent) || undefined
} else { } else {
// 解析Node目录 // 解析Node目录
var dir = [parent, 'node_modules'].join(separatorChar) var dir = [parent, 'node_modules'].join(separatorChar)
return cacheModuleIds[name] = resolveAsFile(name, dir) || resolveAsDirectory(name, dir) || return resolveAsFile(name, dir) || resolveAsDirectory(name, dir) ||
// @ts-ignore // @ts-ignore
(parent && parent.toString().startsWith(root) ? (parent && parent.toString().startsWith(root) ?
resolve(name, new File(parent).getParent()) : resolveAsDirectory(name, NODE_PATH) || undefined) resolve(name, new File(parent).getParent(), optional) : resolveAsDirectory(name, NODE_PATH) || undefined)
} }
} }
@ -341,7 +339,7 @@
* @param {string} name * @param {string} name
* @param {string} path * @param {string} path
*/ */
function checkCoreModule(name, path) { function checkCoreModule(name, path, optional) {
if (name.startsWith('@ms') && lastModule.endsWith('.js')) { if (name.startsWith('@ms') && lastModule.endsWith('.js')) {
// @ts-ignore // @ts-ignore
console.warn(lastModule + ' load deprecated module ' + name + ' auto replace to ' + (name = name.replace('@ms', global.scope)) + '...') console.warn(lastModule + ' load deprecated module ' + name + ' auto replace to ' + (name = name.replace('@ms', global.scope)) + '...')
@ -352,7 +350,7 @@
if (CoreModules.indexOf(name) !== -1) { if (CoreModules.indexOf(name) !== -1) {
// @ts-ignore // @ts-ignore
var newName = global.scope + '/nodejs/dist/' + name var newName = global.scope + '/nodejs/dist/' + name
if (resolve(newName, path) !== undefined) { if (resolve(newName, path, optional) !== undefined) {
return newName return newName
} }
// @ts-ignore // @ts-ignore
@ -360,7 +358,12 @@
} }
return name return name
} }
/**
* 检查缓存模块
*/
function checkCacheModule(optional) {
return !optional.path.startsWith('/') && cacheModuleIds[optional.parentId] && cacheModuleIds[optional.parentId][optional.path]
}
/** /**
* 加载模块 * 加载模块
* @param {string} name 模块名称 * @param {string} name 模块名称
@ -369,17 +372,18 @@
* @returns {*} * @returns {*}
*/ */
function _require(name, path, optional) { function _require(name, path, optional) {
name = checkCoreModule(name, path) var cachePath = checkCacheModule(optional)
if (cachePath) { return _requireFile(new File(cachePath), optional) }
name = checkCoreModule(name, path, optional)
var file = new File(name) var file = new File(name)
file = _isFile(file) ? file : resolve(name, path) file = _isFile(file) ? file : resolve(name, path, optional)
optional = __assign({ cache: true }, optional)
if (file === undefined) { if (file === undefined) {
// excloud local dir, prevent too many recursive call and cache not found module
if (optional.local || optional.recursive || notFoundModules[name]) {
console.log(name, path, optional, notFoundModules[name])
throw new Error("Can't found module " + name + '(' + JSON.stringify(optional) + ') at local ' + path + ' or network!')
}
try { try {
// excloud local dir, prevent too many recursive call and cache not found module
if (name.startsWith('.') || name.startsWith('/') || optional.recursive || notFoundModules[name]) {
console.log(name, path, optional, notFoundModules[name])
throw new Error("Can't found module " + name + '(' + JSON.stringify(optional) + ') at local ' + path + ' or network!')
}
optional.recursive = true optional.recursive = true
return _require(download(name), path, optional) return _require(download(name), path, optional)
} catch (ex) { } catch (ex) {
@ -387,6 +391,13 @@
throw new FileNotFoundException("Can't found module " + name + ' in directory ' + path + ' ERROR: ' + ex) throw new FileNotFoundException("Can't found module " + name + ' in directory ' + path + ' ERROR: ' + ex)
} }
} }
var parent = cacheModuleIds[optional.parentId]
if (!parent) { cacheModuleIds[optional.parentId] = {} }
cacheModuleIds[optional.parentId][optional.path] = _canonical(file)
return _requireFile(file, optional)
}
function _requireFile(file, optional) {
// 重定向文件名称和类型 // 重定向文件名称和类型
return getCacheModule(_canonical(file), file.name.split('.')[0], file, optional) return getCacheModule(_canonical(file), file.name.split('.')[0], file, optional)
} }
@ -403,15 +414,16 @@
* @param {any} optional * @param {any} optional
*/ */
return function __DynamicRequire__(path, optional) { return function __DynamicRequire__(path, optional) {
return _require(path, parent, __assign({ parentId: parentId }, optional)).exports return _require(path, parent, __assign({ cache: true, parentId: parentId, parent: parent, path: path, local: path.startsWith('.') || path.startsWith('/') }, optional)).exports
} }
} }
/** /**
* @param {string} name * @param {string} name
* @param {any} optional 附加选项
*/ */
function __DynamicResolve__(name) { function __DynamicResolve__(name, optional) {
return _canonical(new File(resolve(name, parent))) return _canonical(new File(resolve(name, parent, __assign({ cache: true, parent: parent, local: name.startsWith('.') || name.startsWith('/') }, optional))))
} }
/** /**
@ -427,6 +439,8 @@
} }
function __DynamicDisable__() { function __DynamicDisable__() {
// @ts-ignore
base.save(cacheModuleIdsFile, JSON.stringify(cacheModuleIds))
for (var cacheModule in cacheModules) { for (var cacheModule in cacheModules) {
delete cacheModules[cacheModule] delete cacheModules[cacheModule]
} }
@ -461,8 +475,9 @@
* @type {{[key:string]:any}} cacheModules * @type {{[key:string]:any}} cacheModules
*/ */
var cacheModules = {} var cacheModules = {}
var cacheModuleIdsFile = _canonical(new File(NODE_PATH, 'cacheModuleIds.json'))
/** /**
* @type {{[key:string]:string}} cacheModuleIds * @type {{[key:string]:{[key:string]:string}}} cacheModuleIds
*/ */
var cacheModuleIds = {} var cacheModuleIds = {}
/** /**
@ -474,6 +489,14 @@
console.info('- NODE_PATH:', NODE_PATH) console.info('- NODE_PATH:', NODE_PATH)
console.info('- NODE_REGISTRY:', NODE_REGISTRY) console.info('- NODE_REGISTRY:', NODE_REGISTRY)
console.info('- MS_NODE_REGISTRY:', MS_NODE_REGISTRY) console.info('- MS_NODE_REGISTRY:', MS_NODE_REGISTRY)
try {
// @ts-ignore
cacheModuleIds = JSON.parse(base.read(cacheModuleIdsFile))
console.log('Read cacheModuleIds from file ' + cacheModuleIdsFile)
} catch (error) {
cacheModuleIds = {}
console.log('Initialization new cacheModuleIds')
}
try { try {
ModulesVersionLock = JSON.parse(fetchContent('http://ms.yumc.pw/api/plugin/download/name/version_lock', 'version_lock')) ModulesVersionLock = JSON.parse(fetchContent('http://ms.yumc.pw/api/plugin/download/name/version_lock', 'version_lock'))
console.info('Lock module version List:') console.info('Lock module version List:')
@ -484,5 +507,5 @@
console.debug(error) console.debug(error)
ModulesVersionLock = {} ModulesVersionLock = {}
} }
return getRequire(parent, "null") return getRequire(parent, "")
}) })