From e2f9bbf587458ba007e2958d747b800064853c4d Mon Sep 17 00:00:00 2001 From: MiaoWoo Date: Tue, 19 Apr 2022 17:20:43 +0800 Subject: [PATCH] feat: update to 0.20.0 Signed-off-by: MiaoWoo --- pom.xml | 9 +- src/main/java/pw/yumc/MiaoScript/Base.java | 33 ++- .../pw/yumc/MiaoScript/MiaoScriptEngine.java | 4 +- .../pw/yumc/MiaoScript/api/MiaoScriptAPI.java | 2 +- src/main/resources/bios.js | 3 +- src/main/resources/core/console.js | 5 +- src/main/resources/core/index.d.ts | 25 ++ src/main/resources/core/polyfill.js | 21 +- src/main/resources/core/require.js | 276 +++++++++++------- 9 files changed, 249 insertions(+), 129 deletions(-) create mode 100644 src/main/resources/core/index.d.ts diff --git a/pom.xml b/pom.xml index 8e5c96a..664e414 100644 --- a/pom.xml +++ b/pom.xml @@ -2,7 +2,7 @@ 4.0.0 pw.yumc MiaoScript - 0.19.5 + 0.20.0 502647092 @@ -53,6 +53,13 @@ DEV + §622-04-09 §afeat: 优化 引擎初始化逻辑; +       §afeat: 优化 require 网络加载; +       §afeat: 新增 JS 类型定义文件; +       §afeat: 新增 自定义类型加载逻辑; + §622-02-16 §afeat: 新增 MiaoScriptAPI; +       §afeat: 新增 ScriptEvent; +       §afeat: 新增 .mjs.json 类型加载; §622-02-16 §afeat: 优化 初始化逻辑 加快引擎加载速度;       §afeat: 新增 root 目录变更检测 变更后重新生成缓存;       §afeat: 添加常用库的软依赖; diff --git a/src/main/java/pw/yumc/MiaoScript/Base.java b/src/main/java/pw/yumc/MiaoScript/Base.java index 1fa10c7..bd7d2e8 100644 --- a/src/main/java/pw/yumc/MiaoScript/Base.java +++ b/src/main/java/pw/yumc/MiaoScript/Base.java @@ -7,6 +7,7 @@ import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.Paths; /** * Created with IntelliJ IDEA @@ -42,18 +43,36 @@ public class Base { } public String read(String path) throws IOException { - return new String(Files.readAllBytes(new File(path).toPath()), StandardCharsets.UTF_8); + return read(Paths.get(path)); + } + + public String read(File file) throws IOException { + return read(file.toPath()); + } + + public String read(Path path) throws IOException { + return new String(Files.readAllBytes(path), StandardCharsets.UTF_8); } public Path save(String path, String content) throws IOException { - File file = new File(path); - file.getParentFile().mkdirs(); - return Files.write(file.toPath(), content.getBytes(StandardCharsets.UTF_8)); + return save(Paths.get(path), content); + } + + public Path save(File file, String content) throws IOException { + return save(file.toPath(), content); + } + + public Path save(Path path, String content) throws IOException { + path.getParent().toFile().mkdirs(); + return Files.write(path, content.getBytes(StandardCharsets.UTF_8)); } public boolean move(String source, String target) { - File file = new File(source); - return file.renameTo(new File(target)); + return move(new File(source), new File(target)); + } + + public boolean move(File source, File target) { + return source.renameTo(target); } public boolean delete(String path) throws IOException { @@ -79,7 +98,7 @@ public class Base { f.deleteOnExit(); } } else { - this.delete(f.getAbsolutePath()); + this.delete(f); } } } diff --git a/src/main/java/pw/yumc/MiaoScript/MiaoScriptEngine.java b/src/main/java/pw/yumc/MiaoScript/MiaoScriptEngine.java index 561a4f8..ed2e61c 100644 --- a/src/main/java/pw/yumc/MiaoScript/MiaoScriptEngine.java +++ b/src/main/java/pw/yumc/MiaoScript/MiaoScriptEngine.java @@ -129,9 +129,9 @@ public class MiaoScriptEngine implements ScriptEngine, Invocable { downloadJar(libRoot, "org.ow2.asm", "asm-tree", "9.2"); downloadJar(libRoot, "org.ow2.asm", "asm-util", "9.2"); Class NashornScriptEngineFactory = Class.forName("org.openjdk.nashorn.api.scripting.NashornScriptEngineFactory"); - Method getScriptEngine = NashornScriptEngineFactory.getMethod("getScriptEngine"); + Method getScriptEngine = NashornScriptEngineFactory.getMethod("getScriptEngine", ClassLoader.class); Object factory = NashornScriptEngineFactory.newInstance(); - engine = (ScriptEngine) getScriptEngine.invoke(factory); + engine = (ScriptEngine) getScriptEngine.invoke(factory, ClassLoader.getSystemClassLoader()); } @SneakyThrows diff --git a/src/main/java/pw/yumc/MiaoScript/api/MiaoScriptAPI.java b/src/main/java/pw/yumc/MiaoScript/api/MiaoScriptAPI.java index cafce44..ca20137 100644 --- a/src/main/java/pw/yumc/MiaoScript/api/MiaoScriptAPI.java +++ b/src/main/java/pw/yumc/MiaoScript/api/MiaoScriptAPI.java @@ -4,7 +4,7 @@ import pw.yumc.MiaoScript.MiaoScriptEngine; import pw.yumc.MiaoScript.ScriptEngine; public class MiaoScriptAPI { - public static final String VERSION = "0.19.5"; + public static final String VERSION = "0.20.0"; private static ScriptEngine scriptEngine; public static void setEngine(ScriptEngine scriptEngine) { diff --git a/src/main/resources/bios.js b/src/main/resources/bios.js index 04fe00e..f34d566 100644 --- a/src/main/resources/bios.js +++ b/src/main/resources/bios.js @@ -37,8 +37,9 @@ var global = this; global.enable = function (future) { if (!future.isDone()) { logger.info("Waiting MiaoScript booted...") - future.get() } + // await polyfill loading + future.get() logger.info("MiaoScript booted starting...") global.engineDisableImpl = require(System.getenv("MS_NODE_CORE_MODULE") || (global.scope + '/core')).default || function () { logger.info('Error: abnormal Initialization MiaoScript Engine. Skip disable step...') diff --git a/src/main/resources/core/console.js b/src/main/resources/core/console.js index 63fd6a7..17d2a21 100644 --- a/src/main/resources/core/console.js +++ b/src/main/resources/core/console.js @@ -1,3 +1,4 @@ +/// // @ts-check ( /** @@ -19,11 +20,9 @@ log: log, info: log, ex: log, - // @ts-ignore trace: global.level === "trace" ? _proxy('TRACE') : global.noop, - // @ts-ignore debug: global.debug ? _proxy('DEBUG') : global.noop, warn: _proxy('WARN'), error: _proxy('ERROR') - }; + } }) diff --git a/src/main/resources/core/index.d.ts b/src/main/resources/core/index.d.ts new file mode 100644 index 0000000..cdaf7e4 --- /dev/null +++ b/src/main/resources/core/index.d.ts @@ -0,0 +1,25 @@ +declare const global: any +declare const root: string +declare const base: Core +declare function engineLoad(str: string | { script: string, name: string }): any +interface Core { + version: string + getClass(name: String): any + getProxyClass(): any + getJavaScriptTaskClass(): any + getInstance(): any + read(path: string): string + save(path: string, content: string): void + delete(path: string): void +} +namespace Java { + function type(clazz: string): T + function from(javaObj: T[]): T[] + function to(array: T[], type?: T): T[] + function extend(...parentTypes: any[]): any + function synchronized(func: () => void, lock: any): Function + function isJavaObject(obj: any): boolean + function asJSONCompatible(obj: T): T + //@ts-ignore + // function super(type: any); +} diff --git a/src/main/resources/core/polyfill.js b/src/main/resources/core/polyfill.js index d3426c3..56c6b01 100644 --- a/src/main/resources/core/polyfill.js +++ b/src/main/resources/core/polyfill.js @@ -1,3 +1,4 @@ +/// ( /** * @param {string} root @@ -5,24 +6,24 @@ */ function (root, logger) { // Init Global Value - global.root = root; - global.logger = logger; + global.root = root + global.logger = logger global.ScriptEngineStartTime = new Date().getTime() - global.engineLoad = load; - global.noop = function () { }; - global.load = load = function __PreventGlobalLoadFunction__() { throw new Error('Internal engine system not allow use `load` function!'); } + global.engineLoad = load + global.noop = function () { } + global.load = load = function __PreventGlobalLoadFunction__() { throw new Error('Internal engine system not allow use `load` function!') } global.setGlobal = function (key, value, config) { if (config) { - config.value = value; + config.value = value Object.defineProperty(global, key, config) } else { - global[key] = value; + global[key] = value } - }; + } // Init console and require - global.console = engineLoad(java.lang.System.getenv("MS_NODE_CORE_CONSOLE") || 'classpath:core/console.js')(logger); + global.console = engineLoad(java.lang.System.getenv("MS_NODE_CORE_CONSOLE") || 'classpath:core/console.js')(logger) console.log("Loading Engine at Thread", java.lang.Thread.currentThread().name) - global.require = engineLoad(java.lang.System.getenv("MS_NODE_CORE_REQUIRE") || 'classpath:core/require.js')(root); + global.require = engineLoad(java.lang.System.getenv("MS_NODE_CORE_REQUIRE") || 'classpath:core/require.js')(root) require(global.scope + '/polyfill') } ) diff --git a/src/main/resources/core/require.js b/src/main/resources/core/require.js index 52f81f7..21e34ef 100644 --- a/src/main/resources/core/require.js +++ b/src/main/resources/core/require.js @@ -24,6 +24,7 @@ * 3. 如果 xx/index.json 存在 则使用 `xx/index.json` 解析为对象加载 并停止执行 * 暂不支持 4. 如果 xx/index.msm 是一个文件 则使用MScript解析器解析 并停止执行 */ +/// // @ts-check ( /** @@ -31,43 +32,29 @@ */ function (parent) { 'use strict' - // @ts-ignore + var System = Java.type('java.lang.System') + var File = Java.type('java.io.File') - // @ts-ignore var Paths = Java.type('java.nio.file.Paths') - // @ts-ignore var Files = Java.type('java.nio.file.Files') - // @ts-ignore var StandardCopyOption = Java.type('java.nio.file.StandardCopyOption') - // @ts-ignore var TarInputStream = Java.type('org.kamranzafar.jtar.TarInputStream') - // @ts-ignore var GZIPInputStream = Java.type('java.util.zip.GZIPInputStream') - // @ts-ignore var BufferedInputStream = Java.type('java.io.BufferedInputStream') - // @ts-ignore var URL = Java.type('java.net.URL') - // @ts-ignore var ByteArrayOutputStream = Java.type("java.io.ByteArrayOutputStream") - // @ts-ignore var ByteArray = Java.type("byte[]") - // @ts-ignore var Thread = Java.type('java.lang.Thread') - // @ts-ignore var Callable = Java.type('java.util.concurrent.Callable') - // @ts-ignore var Executors = Java.type('java.util.concurrent.Executors') + var TimeUnit = Java.type('java.util.concurrent.TimeUnit') var separatorChar = File.separatorChar - // @ts-ignore - var NODE_PATH = java.lang.System.getenv("NODE_PATH") || root + separatorChar + 'node_modules' - // @ts-ignore - var NODE_REGISTRY = java.lang.System.getenv("NODE_REGISTRY") || 'https://registry.npm.taobao.org' - // @ts-ignore - var MS_NODE_REGISTRY = java.lang.System.getenv("MS_NODE_REGISTRY") || 'https://repo.yumc.pw/repository/npm' - + var MS_NODE_PATH = System.getenv("MS_NODE_PATH") || root + separatorChar + 'node_modules' + var MS_NODE_REGISTRY = System.getenv("MS_NODE_REGISTRY") || 'https://registry.npmmirror.com' + var FALLBACK_NODE_REGISTRY = System.getenv("FALLBACK_NODE_REGISTRY") || 'https://repo.yumc.pw/repository/npm' var CoreModules = [ "assert", "async_hooks", "Buffer", "child_process", "cluster", "crypto", "dgram", "dns", "domain", "events", "fs", "http", "http2", "https", @@ -142,22 +129,20 @@ // 解析Node目录 var dir = [parent, 'node_modules'].join(separatorChar) return resolveAsFile(name, dir) || resolveAsDirectory(name, dir) || - // @ts-ignore (parent && parent.toString().startsWith(root) ? - resolve(name, new File(parent).getParent(), optional) : resolveAsDirectory(name, NODE_PATH) || undefined) + resolve(name, new File(parent).getParent(), optional) : resolveAsDirectory(name, MS_NODE_PATH) || undefined) } } /** * 解析文件 - * @param {string} file 文件 + * @param {any} file 文件 * @param {string | undefined} dir 目录 * @returns {*} */ function resolveAsFile(file, dir) { file = dir !== undefined ? new File(dir, file) : new File(file) // 直接文件 - // @ts-ignore if (file.isFile()) { return file } @@ -183,7 +168,6 @@ dir = dir !== undefined ? new File(dir, file) : new File(file) var _package = new File(dir, 'package.json') if (_package.exists()) { - // @ts-ignore var json = JSON.parse(base.read(_package)) if (json.main) { return resolveAsFile(json.main, dir) @@ -210,47 +194,51 @@ /** * 检查模块缓存 * @param {string} id 模块ID - * @param {string} name 模块名称 * @param {any} file 模块文件 * @param {any} optional 附加选项 * @returns {Object} */ - function getCacheModule(id, name, file, optional) { + function getCacheModule(id, file, optional) { var module = cacheModules[id] if (optional.cache && module) { return module } - return createModule(id, name, file, optional) + return createModule(id, file, optional) } /** * 编译模块 * @param {string} id 模块ID - * @param {string} name 模块名称 * @param {any} file 模块文件 * @param {any} optional 附加选项 * @returns {Object} */ - function createModule(id, name, file, optional) { + function createModule(id, file, optional) { + var filename = file.name + var lastDotIndexOf = filename.lastIndexOf('.') + if (lastDotIndexOf == -1) { + throw Error('require module must include file ext.') + } + var name = filename.substring(0, lastDotIndexOf) + var ext = filename.substring(lastDotIndexOf + 1) + var loader = requireLoaders[ext] + if (!loader) { + throw Error('Unsupported module ' + filename + '. require loader not found.') + } console.trace('Loading module', name + '(' + id + ')', 'Optional', JSON.stringify(optional)) var module = { id: id, + name: name, + ext: ext, exports: {}, loaded: false, - require: getRequire(file.parentFile, id) + loader: loader, + require: getRequire(file.parentFile, id), + __dirname: file.parentFile, + __filename: file } cacheModules[id] = module - var cfile = _canonical(file) - if (cfile.endsWith('.js') || cfile.endsWith('.mjs.json')) { - compileJs(module, file, __assign(optional, { id: id })) - } else if (cfile.endsWith('.json')) { - compileJson(module, file) - } else if (cfile.endsWith('.msm')) { - throw Error('Unsupported MiaoScript module!') - } else { - throw Error('Unknown file type ' + cfile) - } - return module + return loader(module, file, __assign(optional, { id: id })) } /** @@ -258,37 +246,52 @@ * @param {any} module JS模块 * @param {any} file JS文件 * @param {any} optional 附加选项 - * @returns {void} + * @returns {any} */ - function compileJs(module, file, optional) { - // @ts-ignore - var origin = base.read(file) + function compileJsFile(module, file, optional) { + return compileJs(module, base.read(file), optional) + } + + /** + * 预编译JS + * @param {any} module JS模块 + * @param {any} script JS脚本 + * @param {any} optional 附加选项 + * @returns {any} + */ + function compileJs(module, script, optional) { if (optional.hook) { - origin = optional.hook(origin) + script = optional.hook(script) + } + if (optional.beforeCompile) { + script = optional.beforeCompile(script) } // 2019-09-19 使用 扩展函数直接 load 无需保存/删除文件 // 2020-02-16 结尾新增换行 防止有注释导致加载失败 - // @ts-ignore var compiledWrapper = engineLoad({ - script: '(function $(module, exports, require, __dirname, __filename) {' + origin + '\n});', + script: '(function (module, exports, require, __dirname, __filename) {' + script + '\n});', name: optional.id }) compiledWrapper.apply(module.exports, [ - module, module.exports, module.require, file.parentFile, file + module, module.exports, module.require, module.__dirname, module.__filename ]) module.loaded = true + if (optional.afterCompile) { + module = optional.afterCompile(module) || module + } + return module } /** * 预编译Json * @param {{ id?: string | null; exports?: {}; loaded: any; require?: any; }} module Json模块 * @param {any} file Json 文件 - * @returns {void} + * @returns {any} */ function compileJson(module, file) { - // @ts-ignore module.exports = JSON.parse(base.read(file)) module.loaded = true + return module } /** @@ -296,11 +299,12 @@ * @param {string} name 包名称 */ function download(name) { - // handle name es6-map/implement => es6-map @ccms/common/dist/reflect => @ccms/common + // process package name + // es6-map/implement => es6-map + // @ccms/common/dist/reflect => @ccms/common var name_arr = name.split('/') var module_name = name.startsWith('@') ? name_arr[0] + '/' + name_arr[1] : name_arr[0] - // @ts-ignore - var target = NODE_PATH + separatorChar + module_name + var target = MS_NODE_PATH + separatorChar + module_name var _package = new File(target, 'package.json') if (_package.exists()) { return @@ -311,7 +315,6 @@ console.log('fetch node_module ' + module_name + ' from ' + url + ' waiting...') return executor.submit(new Callable(function () { var tis = new TarInputStream(new BufferedInputStream(new GZIPInputStream(new URL(url).openStream()))) - // @ts-ignore var entry while ((entry = tis.getNextEntry()) != null) { var targetPath = Paths.get(target + separatorChar + entry.getName().substring(8)) @@ -319,7 +322,9 @@ Files.copy(tis, targetPath, StandardCopyOption.REPLACE_EXISTING) } return name - })).get() + })) + // default wait 45 seconds + .get(45, TimeUnit.SECONDS) } /** @@ -328,16 +333,17 @@ function fetchPackageInfo(module_name) { var content = '' try { - content = fetchContent(NODE_REGISTRY + '/' + module_name) - } catch (ex) { - console.debug('can\'t fetch package ' + module_name + ' from ' + NODE_REGISTRY + ' registry. try fetch from ' + MS_NODE_REGISTRY + ' registry...') content = fetchContent(MS_NODE_REGISTRY + '/' + module_name) + } catch (ex) { + console.warn('can\'t fetch package ' + module_name + ' from ' + MS_NODE_REGISTRY + ' registry. try fetch from ' + FALLBACK_NODE_REGISTRY + ' registry...') + content = fetchContent(FALLBACK_NODE_REGISTRY + '/' + module_name) } return JSON.parse(content) } - function fetchContent(url) { - return executor.submit(new Callable(function () { + function fetchContent(url, timeout) { + timeout = timeout || 10 + return executor.submit(new Callable(function fetchContent() { var input = new URL(url).openStream() var output = new ByteArrayOutputStream() var buffer = new ByteArray(1024) @@ -348,9 +354,10 @@ } return output.toString("UTF-8") } finally { + input.close() output.close() } - })).get() + })).get(timeout, TimeUnit.SECONDS) } var lastModule = '' @@ -362,19 +369,16 @@ */ function checkCoreModule(name, path, optional) { if (name.startsWith('@ms') && lastModule.endsWith('.js')) { - // @ts-ignore console.warn(lastModule + ' load deprecated module ' + name + ' auto replace to ' + (name = name.replace('@ms', global.scope)) + '...') return name } else { lastModule = name } if (CoreModules.indexOf(name) !== -1) { - // @ts-ignore var newName = global.scope + '/nodejs/dist/' + name if (resolve(newName, path, optional) !== undefined) { return newName } - // @ts-ignore throw new Error("Can't load nodejs core module " + name + " . maybe later will auto replace to " + global.scope + "/nodejs/" + name + ' to compatible...') } return name @@ -406,8 +410,9 @@ if (cachePath && cacheFile.exists()) { return _requireFile(cacheFile, optional) } - // search module + // check core module name = checkCoreModule(name, path, optional) + // search module if ((file = resolve(name, path, optional)) === undefined) { // excloud local dir, prevent too many recursive call and cache not found module if (optional.local || optional.recursive || notFoundModules[name]) { @@ -443,7 +448,7 @@ function _requireFile(file, optional) { // 重定向文件名称和类型 - return getCacheModule(_canonical(file), file.name.split('.')[0], file, optional) + return getCacheModule(_canonical(file), file, optional) } /** @@ -496,7 +501,6 @@ } function __DynamicDisable__() { - // @ts-ignore base.save(cacheModuleIdsFile, JSON.stringify(upgradeMode ? {} : cacheModuleIds)) for (var cacheModule in cacheModules) { delete cacheModules[cacheModule] @@ -526,11 +530,98 @@ require.clear = __DynamicClear__ require.disable = __DynamicDisable__ require.setUpgradeMode = __setUpgradeMode__ + require.loader = { + register: registerLoader, + get: getLoader, + unregister: unregisterLoader, + } require.internal = { coreModules: CoreModules, cacheModules: cacheModules, cacheModuleIds: cacheModuleIds, notFoundModules: notFoundModules, + requireLoaders: requireLoaders + } + return require + } + + /** + * @param {string} ext + * @param {any} loader + */ + function registerLoader(ext, loader) { + requireLoaders[ext] = loader + console.info('Register Require Loader ' + ext + ' => ' + (loader.name || '') + '.') + } + /** + * @param {*} ext + */ + function getLoader(ext) { + return requireLoaders[ext] + } + /** + * @param {*} ext + */ + function unregisterLoader(ext) { + delete requireLoaders[ext] + console.info('unregister Require Loader ' + ext + '.') + } + + function printRequireInfo() { + console.info('Initialization require module.') + console.info('ParentDir:', _canonical(parent)) + console.info('Require module env list:') + console.info('- MS_NODE_PATH:', MS_NODE_PATH.startsWith(root) ? MS_NODE_PATH.split(root)[1] : MS_NODE_PATH) + console.info('- MS_NODE_REGISTRY:', MS_NODE_REGISTRY) + console.info('- FALLBACK_NODE_REGISTRY:', FALLBACK_NODE_REGISTRY) + } + + function initCacheModuleIds() { + try { + cacheModuleIds = JSON.parse(base.read(cacheModuleIdsFile)) + if (cacheModuleIds['@ccms-cache-module-root'] != MS_NODE_PATH) { + throw new Error('canonicalRoot Change ' + cacheModuleIds['@ccms-cache-module-root'] + ' to ' + MS_NODE_PATH + ' Clear Cache!') + } + console.log('Read cacheModuleIds from file', cacheModuleIdsFile.startsWith(root) ? cacheModuleIdsFile.split(root)[1] : cacheModuleIdsFile) + } catch (error) { + cacheModuleIds = {} + cacheModuleIds['@ccms-cache-module-root'] = MS_NODE_PATH + console.log('Initialization new cacheModuleIds: ' + error) + } + } + + function initVersionLock() { + try { + ModulesVersionLock = JSON.parse(fetchContent('https://ms.yumc.pw/api/plugin/download/name/version_lock', 5)) + try { + ModulesVersionLock = __assign(ModulesVersionLock, JSON.parse(base.read(localVersionLockFile))) + } catch (e) { + } + } catch (error) { + console.warn("无法获取到最新的版本锁定信息 使用默认配置.") + console.warn("InitVersionLock Error:", error) + console.debug(error) + ModulesVersionLock = { "@babel/standalone": "7.12.18", "crypto-js": "3.3.0" } + } + console.info('Lock module version List:') + for (var key in ModulesVersionLock) { + console.info('- ' + key + ': ' + ModulesVersionLock[key]) + } + } + + function initRequireLoader(require) { + registerLoader('js', compileJsFile) + registerLoader('json', compileJson) + try { + engineLoad({ + script: fetchContent('https://ms.yumc.pw/api/plugin/download/name/require_loader', 5), + name: 'core/require_loader.js' + })(require) + } catch (error) { + console.warn("无法获取到最新的加载器信息 使用默认配置.") + console.warn("InitRequireLoader Error:", error) + console.debug(error) + registerLoader('ms', compileJsFile) } return require } @@ -538,12 +629,16 @@ if (typeof parent === 'string') { parent = new File(parent) } + /** + * @type {{[key:string]:(module:any, file:string, optional?:any)=>any}} requireLoader + */ + var requireLoaders = {} /** * @type {{[key:string]:any}} cacheModules */ var cacheModules = {} - var cacheModuleIdsFile = _canonical(new File(NODE_PATH, 'cacheModuleIds.json')) - var localVersionLockFile = _canonical(new File(NODE_PATH, 'moduleVersionLock.json')) + var cacheModuleIdsFile = _canonical(new File(MS_NODE_PATH, 'cacheModuleIds.json')) + var localVersionLockFile = _canonical(new File(MS_NODE_PATH, 'moduleVersionLock.json')) /** * @type {{[key:string]:{[key:string]:string}|string}} cacheModuleIds */ @@ -556,37 +651,10 @@ var executor = Executors.newSingleThreadExecutor(function (r) { return new Thread(r, "MiaoScript require thread") }) - console.info('Initialization require module. ParentDir:', _canonical(parent)) - console.info('Require module env list:') - console.info('- NODE_PATH:', NODE_PATH) - console.info('- NODE_REGISTRY:', NODE_REGISTRY) - console.info('- MS_NODE_REGISTRY:', MS_NODE_REGISTRY) - try { - // @ts-ignore - cacheModuleIds = JSON.parse(base.read(cacheModuleIdsFile)) - if (cacheModuleIds['@ccms-cache-module-root'] != NODE_PATH) { - throw new Error('canonicalRoot Change ' + cacheModuleIds['@ccms-cache-module-root'] + ' to ' + NODE_PATH + ' Clear Cache!') - } - console.log('Read cacheModuleIds from file ' + cacheModuleIdsFile) - } catch (error) { - cacheModuleIds = {} - cacheModuleIds['@ccms-cache-module-root'] = NODE_PATH - console.log('Initialization new cacheModuleIds: ' + error) - } - try { - ModulesVersionLock = JSON.parse(fetchContent('http://ms.yumc.pw/api/plugin/download/name/version_lock')) - try { - // @ts-ignore - ModulesVersionLock = __assign(ModulesVersionLock, JSON.parse(base.read(localVersionLockFile))) - } catch (e) { - } - console.info('Lock module version List:') - for (var key in ModulesVersionLock) { - console.info('- ' + key + ': ' + ModulesVersionLock[key]) - } - } catch (error) { - console.debug(error) - ModulesVersionLock = {} - } - return getRequire(parent, "") + + printRequireInfo() + initCacheModuleIds() + initVersionLock() + + return initRequireLoader(getRequire(parent, "")) })