From 1c217925723d46c1760759f167d55ccf8e87a185 Mon Sep 17 00:00:00 2001 From: MiaoWoo Date: Fri, 17 Jan 2020 18:39:23 +0800 Subject: [PATCH] feat: add global.trace and check node module Signed-off-by: MiaoWoo --- src/main/resources/bios.js | 1 + src/main/resources/core/console.js | 1 + src/main/resources/core/require.js | 31 ++++++++++++++++++++++++------ 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/src/main/resources/bios.js b/src/main/resources/bios.js index b87bab0..7fadf61 100644 --- a/src/main/resources/bios.js +++ b/src/main/resources/bios.js @@ -19,6 +19,7 @@ var global = this; if (java.nio.file.Files.exists(java.nio.file.Paths.get(root, "debug"))) { logger.info('Running debugging mode...'); global.debug = true; + global.trace = true; } // Check Class Loader, Sometimes Server will can't find plugin.yml file loader = checkClassLoader(); diff --git a/src/main/resources/core/console.js b/src/main/resources/core/console.js index 83a6f2f..d41d13b 100644 --- a/src/main/resources/core/console.js +++ b/src/main/resources/core/console.js @@ -11,6 +11,7 @@ log: log, info: log, ex: log, + trace: global.trace ? _proxy('TRACE') : global.noop, debug: global.debug ? _proxy('DEBUG') : global.noop, warn: _proxy('WARN'), error: _proxy('ERROR') diff --git a/src/main/resources/core/require.js b/src/main/resources/core/require.js index 253179d..fac3cba 100644 --- a/src/main/resources/core/require.js +++ b/src/main/resources/core/require.js @@ -39,6 +39,8 @@ var URL = Java.type('java.net.URL') var separatorChar = File.separatorChar; + var CoreModules = ['assert', 'async_hooks', 'child_process', 'cluster', 'crypto', 'dns', 'domain', 'events', 'fs', 'http', 'http2', 'https', 'inspector', 'net', 'os', 'path', 'vm', 'url', 'util', 'zlib', 'worker_threads'] + function __assign(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; @@ -93,9 +95,10 @@ } else { // 解析Node目录 var dir = [parent, 'node_modules'].join(separatorChar); - return resolveAsFile(name, dir) || - resolveAsDirectory(name, dir) || + if (cacheModuleIds[name]) return cacheModuleIds[name] + cacheModuleIds[name] = resolveAsFile(name, dir) || resolveAsDirectory(name, dir) || (parent && parent.toString().startsWith(root) ? resolve(name, new File(parent).getParent()) : undefined); + return cacheModuleIds[name]; } } @@ -169,12 +172,12 @@ if (optional.cache && module) { return module; } - console.debug('Loading module', name + '(' + id + ')', 'Optional', JSON.stringify(optional)); + console.trace('Loading module', name + '(' + id + ')', 'Optional', JSON.stringify(optional)); module = { id: id, exports: {}, loaded: false, - require: exports(file.parentFile) + require: getRequire(file.parentFile) }; cacheModules[id] = module; var cfile = _canonical(file); @@ -245,6 +248,12 @@ return name; } + function checkCoreModule(name) { + if (CoreModules.indexOf(name) != -1) { + throw new Error("Can't load nodejs core module " + name + " . maybe later will auto replace to @ms/" + name + ' to compatible...') + } + } + /** * 加载模块 * @param name 模块名称 @@ -253,6 +262,7 @@ * @returns {*} */ function _require(name, path, optional) { + checkCoreModule(name); var file = new File(name); file = _isFile(file) ? file : resolve(name, path); optional = __assign({ cache: true }, optional); @@ -285,11 +295,20 @@ }; } + function getRequire(parent) { + var require = exports(parent) + require.resolve = function __DynamicResolve__(name) { + return _canonical(new File(resolve(name, parent))) + } + return require; + } + if (typeof parent === 'string') { parent = new File(parent); } var cacheModules = []; + var cacheModuleIds = []; var notFoundModules = []; - console.debug('Initialization require module... ParentDir:', _canonical(parent)); - return exports(parent); + console.info('Initialization require module... ParentDir:', _canonical(parent)); + return getRequire(parent); });