feat: update to 0.20.0

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
MiaoWoo 2022-04-19 17:20:43 +08:00
parent a9003025ee
commit e2f9bbf587
9 changed files with 249 additions and 129 deletions

View File

@ -2,7 +2,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.19.5</version> <version>0.20.0</version>
<developers> <developers>
<developer> <developer>
<id>502647092</id> <id>502647092</id>
@ -53,6 +53,13 @@
<properties> <properties>
<env.GIT_COMMIT>DEV</env.GIT_COMMIT> <env.GIT_COMMIT>DEV</env.GIT_COMMIT>
<update.changes> <update.changes>
§622-04-09 §afeat: 优化 引擎初始化逻辑;
      §afeat: 优化 require 网络加载;
      §afeat: 新增 JS 类型定义文件;
      §afeat: 新增 自定义类型加载逻辑;
§622-02-16 §afeat: 新增 MiaoScriptAPI;
      §afeat: 新增 ScriptEvent;
      §afeat: 新增 .mjs.json 类型加载;
§622-02-16 §afeat: 优化 初始化逻辑 加快引擎加载速度; §622-02-16 §afeat: 优化 初始化逻辑 加快引擎加载速度;
      §afeat: 新增 root 目录变更检测 变更后重新生成缓存;       §afeat: 新增 root 目录变更检测 变更后重新生成缓存;
      §afeat: 添加常用库的软依赖;       §afeat: 添加常用库的软依赖;

View File

@ -7,6 +7,7 @@ import java.io.IOException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths;
/** /**
* Created with IntelliJ IDEA * Created with IntelliJ IDEA
@ -42,18 +43,36 @@ public class Base {
} }
public String read(String path) throws IOException { 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 { public Path save(String path, String content) throws IOException {
File file = new File(path); return save(Paths.get(path), content);
file.getParentFile().mkdirs(); }
return Files.write(file.toPath(), content.getBytes(StandardCharsets.UTF_8));
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) { public boolean move(String source, String target) {
File file = new File(source); return move(new File(source), new File(target));
return file.renameTo(new File(target)); }
public boolean move(File source, File target) {
return source.renameTo(target);
} }
public boolean delete(String path) throws IOException { public boolean delete(String path) throws IOException {
@ -79,7 +98,7 @@ public class Base {
f.deleteOnExit(); f.deleteOnExit();
} }
} else { } else {
this.delete(f.getAbsolutePath()); this.delete(f);
} }
} }
} }

View File

@ -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-tree", "9.2");
downloadJar(libRoot, "org.ow2.asm", "asm-util", "9.2"); downloadJar(libRoot, "org.ow2.asm", "asm-util", "9.2");
Class<?> NashornScriptEngineFactory = Class.forName("org.openjdk.nashorn.api.scripting.NashornScriptEngineFactory"); 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(); Object factory = NashornScriptEngineFactory.newInstance();
engine = (ScriptEngine) getScriptEngine.invoke(factory); engine = (ScriptEngine) getScriptEngine.invoke(factory, ClassLoader.getSystemClassLoader());
} }
@SneakyThrows @SneakyThrows

View File

@ -4,7 +4,7 @@ import pw.yumc.MiaoScript.MiaoScriptEngine;
import pw.yumc.MiaoScript.ScriptEngine; import pw.yumc.MiaoScript.ScriptEngine;
public class MiaoScriptAPI { public class MiaoScriptAPI {
public static final String VERSION = "0.19.5"; public static final String VERSION = "0.20.0";
private static ScriptEngine scriptEngine; private static ScriptEngine scriptEngine;
public static void setEngine(ScriptEngine scriptEngine) { public static void setEngine(ScriptEngine scriptEngine) {

View File

@ -37,8 +37,9 @@ var global = this;
global.enable = function (future) { global.enable = function (future) {
if (!future.isDone()) { if (!future.isDone()) {
logger.info("Waiting MiaoScript booted...") logger.info("Waiting MiaoScript booted...")
future.get()
} }
// await polyfill loading
future.get()
logger.info("MiaoScript booted starting...") logger.info("MiaoScript booted starting...")
global.engineDisableImpl = require(System.getenv("MS_NODE_CORE_MODULE") || (global.scope + '/core')).default || function () { global.engineDisableImpl = require(System.getenv("MS_NODE_CORE_MODULE") || (global.scope + '/core')).default || function () {
logger.info('Error: abnormal Initialization MiaoScript Engine. Skip disable step...') logger.info('Error: abnormal Initialization MiaoScript Engine. Skip disable step...')

View File

@ -1,3 +1,4 @@
/// <reference path="./index.d.ts" />
// @ts-check // @ts-check
( (
/** /**
@ -19,11 +20,9 @@
log: log, log: log,
info: log, info: log,
ex: log, ex: log,
// @ts-ignore
trace: global.level === "trace" ? _proxy('TRACE') : global.noop, trace: global.level === "trace" ? _proxy('TRACE') : global.noop,
// @ts-ignore
debug: global.debug ? _proxy('DEBUG') : global.noop, debug: global.debug ? _proxy('DEBUG') : global.noop,
warn: _proxy('WARN'), warn: _proxy('WARN'),
error: _proxy('ERROR') error: _proxy('ERROR')
}; }
}) })

25
src/main/resources/core/index.d.ts vendored Normal file
View File

@ -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<T = any>(clazz: string): T
function from<T = any>(javaObj: T[]): T[]
function to<T = any>(array: T[], type?: T): T[]
function extend(...parentTypes: any[]): any
function synchronized(func: () => void, lock: any): Function
function isJavaObject(obj: any): boolean
function asJSONCompatible<T = any>(obj: T): T
//@ts-ignore
// function super(type: any);
}

View File

@ -1,3 +1,4 @@
/// <reference path="./index.d.ts" />
( (
/** /**
* @param {string} root * @param {string} root
@ -5,24 +6,24 @@
*/ */
function (root, logger) { function (root, logger) {
// Init Global Value // Init Global Value
global.root = root; global.root = root
global.logger = logger; global.logger = logger
global.ScriptEngineStartTime = new Date().getTime() global.ScriptEngineStartTime = new Date().getTime()
global.engineLoad = load; global.engineLoad = load
global.noop = function () { }; global.noop = function () { }
global.load = load = function __PreventGlobalLoadFunction__() { throw new Error('Internal engine system not allow use `load` function!'); } global.load = load = function __PreventGlobalLoadFunction__() { throw new Error('Internal engine system not allow use `load` function!') }
global.setGlobal = function (key, value, config) { global.setGlobal = function (key, value, config) {
if (config) { if (config) {
config.value = value; config.value = value
Object.defineProperty(global, key, config) Object.defineProperty(global, key, config)
} else { } else {
global[key] = value; global[key] = value
}
} }
};
// Init console and require // 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) 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') require(global.scope + '/polyfill')
} }
) )

View File

@ -24,6 +24,7 @@
* 3. 如果 xx/index.json 存在 则使用 `xx/index.json` 解析为对象加载 并停止执行 * 3. 如果 xx/index.json 存在 则使用 `xx/index.json` 解析为对象加载 并停止执行
* 暂不支持 4. 如果 xx/index.msm 是一个文件 则使用MScript解析器解析 并停止执行 * 暂不支持 4. 如果 xx/index.msm 是一个文件 则使用MScript解析器解析 并停止执行
*/ */
/// <reference path="./index.d.ts" />
// @ts-check // @ts-check
( (
/** /**
@ -31,43 +32,29 @@
*/ */
function (parent) { function (parent) {
'use strict' 'use strict'
// @ts-ignore var System = Java.type('java.lang.System')
var File = Java.type('java.io.File') var File = Java.type('java.io.File')
// @ts-ignore
var Paths = Java.type('java.nio.file.Paths') var Paths = Java.type('java.nio.file.Paths')
// @ts-ignore
var Files = Java.type('java.nio.file.Files') var Files = Java.type('java.nio.file.Files')
// @ts-ignore
var StandardCopyOption = Java.type('java.nio.file.StandardCopyOption') var StandardCopyOption = Java.type('java.nio.file.StandardCopyOption')
// @ts-ignore
var TarInputStream = Java.type('org.kamranzafar.jtar.TarInputStream') var TarInputStream = Java.type('org.kamranzafar.jtar.TarInputStream')
// @ts-ignore
var GZIPInputStream = Java.type('java.util.zip.GZIPInputStream') var GZIPInputStream = Java.type('java.util.zip.GZIPInputStream')
// @ts-ignore
var BufferedInputStream = Java.type('java.io.BufferedInputStream') var BufferedInputStream = Java.type('java.io.BufferedInputStream')
// @ts-ignore
var URL = Java.type('java.net.URL') var URL = Java.type('java.net.URL')
// @ts-ignore
var ByteArrayOutputStream = Java.type("java.io.ByteArrayOutputStream") var ByteArrayOutputStream = Java.type("java.io.ByteArrayOutputStream")
// @ts-ignore
var ByteArray = Java.type("byte[]") var ByteArray = Java.type("byte[]")
// @ts-ignore
var Thread = Java.type('java.lang.Thread') var Thread = Java.type('java.lang.Thread')
// @ts-ignore
var Callable = Java.type('java.util.concurrent.Callable') var Callable = Java.type('java.util.concurrent.Callable')
// @ts-ignore
var Executors = Java.type('java.util.concurrent.Executors') var Executors = Java.type('java.util.concurrent.Executors')
var TimeUnit = Java.type('java.util.concurrent.TimeUnit')
var separatorChar = File.separatorChar var separatorChar = File.separatorChar
// @ts-ignore var MS_NODE_PATH = System.getenv("MS_NODE_PATH") || root + separatorChar + 'node_modules'
var NODE_PATH = java.lang.System.getenv("NODE_PATH") || root + separatorChar + 'node_modules' var MS_NODE_REGISTRY = System.getenv("MS_NODE_REGISTRY") || 'https://registry.npmmirror.com'
// @ts-ignore var FALLBACK_NODE_REGISTRY = System.getenv("FALLBACK_NODE_REGISTRY") || 'https://repo.yumc.pw/repository/npm'
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 CoreModules = [ var CoreModules = [
"assert", "async_hooks", "Buffer", "child_process", "cluster", "crypto", "assert", "async_hooks", "Buffer", "child_process", "cluster", "crypto",
"dgram", "dns", "domain", "events", "fs", "http", "http2", "https", "dgram", "dns", "domain", "events", "fs", "http", "http2", "https",
@ -142,22 +129,20 @@
// 解析Node目录 // 解析Node目录
var dir = [parent, 'node_modules'].join(separatorChar) var dir = [parent, 'node_modules'].join(separatorChar)
return resolveAsFile(name, dir) || resolveAsDirectory(name, dir) || return resolveAsFile(name, dir) || resolveAsDirectory(name, dir) ||
// @ts-ignore
(parent && parent.toString().startsWith(root) ? (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 目录 * @param {string | undefined} dir 目录
* @returns {*} * @returns {*}
*/ */
function resolveAsFile(file, dir) { function resolveAsFile(file, dir) {
file = dir !== undefined ? new File(dir, file) : new File(file) file = dir !== undefined ? new File(dir, file) : new File(file)
// 直接文件 // 直接文件
// @ts-ignore
if (file.isFile()) { if (file.isFile()) {
return file return file
} }
@ -183,7 +168,6 @@
dir = dir !== undefined ? new File(dir, file) : new File(file) dir = dir !== undefined ? new File(dir, file) : new File(file)
var _package = new File(dir, 'package.json') var _package = new File(dir, 'package.json')
if (_package.exists()) { if (_package.exists()) {
// @ts-ignore
var json = JSON.parse(base.read(_package)) var json = JSON.parse(base.read(_package))
if (json.main) { if (json.main) {
return resolveAsFile(json.main, dir) return resolveAsFile(json.main, dir)
@ -210,47 +194,51 @@
/** /**
* 检查模块缓存 * 检查模块缓存
* @param {string} id 模块ID * @param {string} id 模块ID
* @param {string} name 模块名称
* @param {any} file 模块文件 * @param {any} file 模块文件
* @param {any} optional 附加选项 * @param {any} optional 附加选项
* @returns {Object} * @returns {Object}
*/ */
function getCacheModule(id, name, file, optional) { function getCacheModule(id, file, optional) {
var module = cacheModules[id] var module = cacheModules[id]
if (optional.cache && module) { if (optional.cache && module) {
return module return module
} }
return createModule(id, name, file, optional) return createModule(id, file, optional)
} }
/** /**
* 编译模块 * 编译模块
* @param {string} id 模块ID * @param {string} id 模块ID
* @param {string} name 模块名称
* @param {any} file 模块文件 * @param {any} file 模块文件
* @param {any} optional 附加选项 * @param {any} optional 附加选项
* @returns {Object} * @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)) console.trace('Loading module', name + '(' + id + ')', 'Optional', JSON.stringify(optional))
var module = { var module = {
id: id, id: id,
name: name,
ext: ext,
exports: {}, exports: {},
loaded: false, loaded: false,
require: getRequire(file.parentFile, id) loader: loader,
require: getRequire(file.parentFile, id),
__dirname: file.parentFile,
__filename: file
} }
cacheModules[id] = module cacheModules[id] = module
var cfile = _canonical(file) return loader(module, file, __assign(optional, { id: id }))
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
} }
/** /**
@ -258,37 +246,52 @@
* @param {any} module JS模块 * @param {any} module JS模块
* @param {any} file JS文件 * @param {any} file JS文件
* @param {any} optional 附加选项 * @param {any} optional 附加选项
* @returns {void} * @returns {any}
*/ */
function compileJs(module, file, optional) { function compileJsFile(module, file, optional) {
// @ts-ignore return compileJs(module, base.read(file), optional)
var origin = base.read(file) }
/**
* 预编译JS
* @param {any} module JS模块
* @param {any} script JS脚本
* @param {any} optional 附加选项
* @returns {any}
*/
function compileJs(module, script, optional) {
if (optional.hook) { if (optional.hook) {
origin = optional.hook(origin) script = optional.hook(script)
}
if (optional.beforeCompile) {
script = optional.beforeCompile(script)
} }
// 2019-09-19 使用 扩展函数直接 load 无需保存/删除文件 // 2019-09-19 使用 扩展函数直接 load 无需保存/删除文件
// 2020-02-16 结尾新增换行 防止有注释导致加载失败 // 2020-02-16 结尾新增换行 防止有注释导致加载失败
// @ts-ignore
var compiledWrapper = engineLoad({ var compiledWrapper = engineLoad({
script: '(function $(module, exports, require, __dirname, __filename) {' + origin + '\n});', script: '(function (module, exports, require, __dirname, __filename) {' + script + '\n});',
name: optional.id name: optional.id
}) })
compiledWrapper.apply(module.exports, [ compiledWrapper.apply(module.exports, [
module, module.exports, module.require, file.parentFile, file module, module.exports, module.require, module.__dirname, module.__filename
]) ])
module.loaded = true module.loaded = true
if (optional.afterCompile) {
module = optional.afterCompile(module) || module
}
return module
} }
/** /**
* 预编译Json * 预编译Json
* @param {{ id?: string | null; exports?: {}; loaded: any; require?: any; }} module Json模块 * @param {{ id?: string | null; exports?: {}; loaded: any; require?: any; }} module Json模块
* @param {any} file Json 文件 * @param {any} file Json 文件
* @returns {void} * @returns {any}
*/ */
function compileJson(module, file) { function compileJson(module, file) {
// @ts-ignore
module.exports = JSON.parse(base.read(file)) module.exports = JSON.parse(base.read(file))
module.loaded = true module.loaded = true
return module
} }
/** /**
@ -296,11 +299,12 @@
* @param {string} name 包名称 * @param {string} name 包名称
*/ */
function download(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 name_arr = name.split('/')
var module_name = name.startsWith('@') ? name_arr[0] + '/' + name_arr[1] : name_arr[0] var module_name = name.startsWith('@') ? name_arr[0] + '/' + name_arr[1] : name_arr[0]
// @ts-ignore var target = MS_NODE_PATH + separatorChar + module_name
var target = NODE_PATH + separatorChar + module_name
var _package = new File(target, 'package.json') var _package = new File(target, 'package.json')
if (_package.exists()) { if (_package.exists()) {
return return
@ -311,7 +315,6 @@
console.log('fetch node_module ' + module_name + ' from ' + url + ' waiting...') console.log('fetch node_module ' + module_name + ' from ' + url + ' waiting...')
return executor.submit(new Callable(function () { return executor.submit(new Callable(function () {
var tis = new TarInputStream(new BufferedInputStream(new GZIPInputStream(new URL(url).openStream()))) var tis = new TarInputStream(new BufferedInputStream(new GZIPInputStream(new URL(url).openStream())))
// @ts-ignore
var entry var entry
while ((entry = tis.getNextEntry()) != null) { while ((entry = tis.getNextEntry()) != null) {
var targetPath = Paths.get(target + separatorChar + entry.getName().substring(8)) var targetPath = Paths.get(target + separatorChar + entry.getName().substring(8))
@ -319,7 +322,9 @@
Files.copy(tis, targetPath, StandardCopyOption.REPLACE_EXISTING) Files.copy(tis, targetPath, StandardCopyOption.REPLACE_EXISTING)
} }
return name return name
})).get() }))
// default wait 45 seconds
.get(45, TimeUnit.SECONDS)
} }
/** /**
@ -328,16 +333,17 @@
function fetchPackageInfo(module_name) { function fetchPackageInfo(module_name) {
var content = '' var content = ''
try { 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) 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) return JSON.parse(content)
} }
function fetchContent(url) { function fetchContent(url, timeout) {
return executor.submit(new Callable(function () { timeout = timeout || 10
return executor.submit(new Callable(function fetchContent() {
var input = new URL(url).openStream() var input = new URL(url).openStream()
var output = new ByteArrayOutputStream() var output = new ByteArrayOutputStream()
var buffer = new ByteArray(1024) var buffer = new ByteArray(1024)
@ -348,9 +354,10 @@
} }
return output.toString("UTF-8") return output.toString("UTF-8")
} finally { } finally {
input.close()
output.close() output.close()
} }
})).get() })).get(timeout, TimeUnit.SECONDS)
} }
var lastModule = '' var lastModule = ''
@ -362,19 +369,16 @@
*/ */
function checkCoreModule(name, path, optional) { function checkCoreModule(name, path, optional) {
if (name.startsWith('@ms') && lastModule.endsWith('.js')) { 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)) + '...') console.warn(lastModule + ' load deprecated module ' + name + ' auto replace to ' + (name = name.replace('@ms', global.scope)) + '...')
return name return name
} else { } else {
lastModule = name lastModule = name
} }
if (CoreModules.indexOf(name) !== -1) { if (CoreModules.indexOf(name) !== -1) {
// @ts-ignore
var newName = global.scope + '/nodejs/dist/' + name var newName = global.scope + '/nodejs/dist/' + name
if (resolve(newName, path, optional) !== undefined) { if (resolve(newName, path, optional) !== undefined) {
return newName 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...') throw new Error("Can't load nodejs core module " + name + " . maybe later will auto replace to " + global.scope + "/nodejs/" + name + ' to compatible...')
} }
return name return name
@ -406,8 +410,9 @@
if (cachePath && cacheFile.exists()) { if (cachePath && cacheFile.exists()) {
return _requireFile(cacheFile, optional) return _requireFile(cacheFile, optional)
} }
// search module // check core module
name = checkCoreModule(name, path, optional) name = checkCoreModule(name, path, optional)
// search module
if ((file = resolve(name, path, optional)) === undefined) { if ((file = resolve(name, path, optional)) === undefined) {
// excloud local dir, prevent too many recursive call and cache not found module // excloud local dir, prevent too many recursive call and cache not found module
if (optional.local || optional.recursive || notFoundModules[name]) { if (optional.local || optional.recursive || notFoundModules[name]) {
@ -443,7 +448,7 @@
function _requireFile(file, optional) { 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__() { function __DynamicDisable__() {
// @ts-ignore
base.save(cacheModuleIdsFile, JSON.stringify(upgradeMode ? {} : cacheModuleIds)) base.save(cacheModuleIdsFile, JSON.stringify(upgradeMode ? {} : cacheModuleIds))
for (var cacheModule in cacheModules) { for (var cacheModule in cacheModules) {
delete cacheModules[cacheModule] delete cacheModules[cacheModule]
@ -526,11 +530,98 @@
require.clear = __DynamicClear__ require.clear = __DynamicClear__
require.disable = __DynamicDisable__ require.disable = __DynamicDisable__
require.setUpgradeMode = __setUpgradeMode__ require.setUpgradeMode = __setUpgradeMode__
require.loader = {
register: registerLoader,
get: getLoader,
unregister: unregisterLoader,
}
require.internal = { require.internal = {
coreModules: CoreModules, coreModules: CoreModules,
cacheModules: cacheModules, cacheModules: cacheModules,
cacheModuleIds: cacheModuleIds, cacheModuleIds: cacheModuleIds,
notFoundModules: notFoundModules, 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 || '<anonymous>') + '.')
}
/**
* @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 return require
} }
@ -538,12 +629,16 @@
if (typeof parent === 'string') { if (typeof parent === 'string') {
parent = new File(parent) parent = new File(parent)
} }
/**
* @type {{[key:string]:(module:any, file:string, optional?:any)=>any}} requireLoader
*/
var requireLoaders = {}
/** /**
* @type {{[key:string]:any}} cacheModules * @type {{[key:string]:any}} cacheModules
*/ */
var cacheModules = {} var cacheModules = {}
var cacheModuleIdsFile = _canonical(new File(NODE_PATH, 'cacheModuleIds.json')) var cacheModuleIdsFile = _canonical(new File(MS_NODE_PATH, 'cacheModuleIds.json'))
var localVersionLockFile = _canonical(new File(NODE_PATH, 'moduleVersionLock.json')) var localVersionLockFile = _canonical(new File(MS_NODE_PATH, 'moduleVersionLock.json'))
/** /**
* @type {{[key:string]:{[key:string]:string}|string}} cacheModuleIds * @type {{[key:string]:{[key:string]:string}|string}} cacheModuleIds
*/ */
@ -556,37 +651,10 @@
var executor = Executors.newSingleThreadExecutor(function (r) { var executor = Executors.newSingleThreadExecutor(function (r) {
return new Thread(r, "MiaoScript require thread") return new Thread(r, "MiaoScript require thread")
}) })
console.info('Initialization require module. ParentDir:', _canonical(parent))
console.info('Require module env list:') printRequireInfo()
console.info('- NODE_PATH:', NODE_PATH) initCacheModuleIds()
console.info('- NODE_REGISTRY:', NODE_REGISTRY) initVersionLock()
console.info('- MS_NODE_REGISTRY:', MS_NODE_REGISTRY)
try { return initRequireLoader(getRequire(parent, ""))
// @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, "")
}) })