2017-09-23 09:42:16 +00:00
|
|
|
/**
|
2017-10-30 12:47:10 +00:00
|
|
|
* 符合 CommonJS 规范的 类似 Node 的模块化加载
|
|
|
|
* 一. 注: MiaoScript 中 require.main 不存在
|
|
|
|
* 二. 加载 require 流程 例如 在 dir 目录下 调用 require('xx');
|
|
|
|
* a) 加载流程
|
|
|
|
* 1. 如果xx模块是一个內建模块
|
|
|
|
* a. 编译并返回该模块
|
|
|
|
* b. 停止执行
|
|
|
|
* 2. 如果模块以 `./` `../` 开头
|
|
|
|
* a. 尝试使用 resolveAsFile(dir/xx) 加载文件
|
|
|
|
* b. 尝试使用 resolveAsDirectory(dir/xx) 加载目录
|
2019-04-24 01:33:00 +00:00
|
|
|
* 3. 尝试去 root root/node_modules => xx 加载模块
|
2017-11-30 05:22:44 +00:00
|
|
|
* a. 尝试使用 resolveAsFile(xx/xx) 加载文件
|
|
|
|
* b. 尝试使用 resolveAsDirectory(xx/xx) 加载目录
|
2017-10-30 12:47:10 +00:00
|
|
|
* 4. 抛出 not found 异常
|
|
|
|
* b) resolveAsFile 解析流程
|
|
|
|
* 1. 如果 xx 是一个文件 则作为 `javascript` 文本加载 并停止执行
|
|
|
|
* 2. 如果 xx.js 是一个文件 则作为 `javascript` 文本加载 并停止执行
|
2017-11-30 05:22:44 +00:00
|
|
|
* 3. 如果 xx.json 是一个文件 则使用 `JSON.parse(xx.json)` 解析为对象加载 并停止执行
|
|
|
|
* 暂不支持 4. 如果 xx.msm 是一个文件 则使用MScript解析器解析 并停止执行
|
2017-10-30 12:47:10 +00:00
|
|
|
* c) resolveAsDirectory 解析流程
|
|
|
|
* 1. 如果 xx/package.json 存在 则使用 `JSON.parse(xx/package.json)` 解析并取得 main 字段使用 resolveAsFile(main) 加载
|
|
|
|
* 2. 如果 xx/index.js 存在 则使用 resolveAsFile(xx/index.js) 加载
|
2017-11-30 05:22:44 +00:00
|
|
|
* 3. 如果 xx/index.json 存在 则使用 `xx/index.json` 解析为对象加载 并停止执行
|
|
|
|
* 暂不支持 4. 如果 xx/index.msm 是一个文件 则使用MScript解析器解析 并停止执行
|
2017-09-23 09:42:16 +00:00
|
|
|
*/
|
2020-01-31 18:41:24 +00:00
|
|
|
// @ts-check
|
|
|
|
/// <reference types="@ms/nashorn" />
|
|
|
|
(
|
|
|
|
/**
|
|
|
|
* @param {any} parent
|
|
|
|
*/
|
|
|
|
function(parent) {
|
|
|
|
'use strict';
|
|
|
|
var File = Java.type('java.io.File');
|
|
|
|
var Paths = Java.type('java.nio.file.Paths');
|
|
|
|
var Files = Java.type('java.nio.file.Files');
|
|
|
|
var StandardCopyOption = Java.type('java.nio.file.StandardCopyOption');
|
|
|
|
var FileNotFoundException = Java.type('java.io.FileNotFoundException');
|
2019-09-23 10:46:32 +00:00
|
|
|
|
2020-01-31 18:41:24 +00:00
|
|
|
var TarInputStream = Java.type('org.kamranzafar.jtar.TarInputStream');
|
|
|
|
var GZIPInputStream = Java.type('java.util.zip.GZIPInputStream');
|
|
|
|
var BufferedInputStream = Java.type('java.io.BufferedInputStream');
|
2017-09-23 09:42:16 +00:00
|
|
|
|
2020-01-31 18:41:24 +00:00
|
|
|
var URL = Java.type('java.net.URL')
|
|
|
|
var JavaString = Java.type('java.lang.String')
|
|
|
|
var separatorChar = File.separatorChar;
|
2020-01-17 10:39:23 +00:00
|
|
|
|
2020-01-31 18:41:24 +00:00
|
|
|
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']
|
2019-09-05 07:58:24 +00:00
|
|
|
|
2020-01-31 18:41:24 +00:00
|
|
|
/**
|
|
|
|
* @param {...object} t
|
|
|
|
*/
|
|
|
|
function __assign(t) {
|
|
|
|
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
|
|
s = arguments[i];
|
|
|
|
if (s === undefined) { continue; };
|
|
|
|
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
|
|
t[p] = s[p];
|
|
|
|
}
|
|
|
|
return t;
|
|
|
|
};
|
2017-11-30 05:22:44 +00:00
|
|
|
|
2020-01-31 18:41:24 +00:00
|
|
|
/**
|
|
|
|
* 判断是否为一个文件
|
|
|
|
* @param {any} file
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
|
|
|
function _isFile(file) {
|
|
|
|
return file.isFile && file.isFile();
|
|
|
|
}
|
2017-11-30 05:22:44 +00:00
|
|
|
|
2020-01-31 18:41:24 +00:00
|
|
|
/**
|
|
|
|
* 获得文件规范路径
|
|
|
|
* @param {any} file
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
|
|
|
function _canonical(file) {
|
|
|
|
return file.canonicalPath;
|
|
|
|
}
|
2019-09-01 15:58:07 +00:00
|
|
|
|
2020-01-31 18:41:24 +00:00
|
|
|
/**
|
|
|
|
* 获得文件绝对路径
|
|
|
|
* @param {any} file
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
|
|
|
function _absolute(file) {
|
|
|
|
return file.absolutePath;
|
2017-10-11 17:39:51 +00:00
|
|
|
}
|
2017-10-10 13:01:43 +00:00
|
|
|
|
2020-01-31 18:41:24 +00:00
|
|
|
/**
|
|
|
|
* 解析模块名称为文件
|
|
|
|
* 按照下列顺序查找
|
|
|
|
* 当前目录 ./
|
|
|
|
* 父目录 ../
|
|
|
|
* 模块目录 /node_modules
|
|
|
|
* @param {string} name 模块名称
|
|
|
|
* @param {string} parent 父目录
|
|
|
|
*/
|
|
|
|
function resolve(name, parent) {
|
|
|
|
name = _canonical(name) || name;
|
|
|
|
// 解析本地目录
|
|
|
|
if (name.startsWith('./') || name.startsWith('../')) {
|
|
|
|
return resolveAsFile(name, parent) || resolveAsDirectory(name, parent) || undefined;
|
|
|
|
} else {
|
|
|
|
// 解析Node目录
|
|
|
|
var dir = [parent, 'node_modules'].join(separatorChar);
|
|
|
|
if (cacheModuleIds[name]) return cacheModuleIds[name]
|
|
|
|
cacheModuleIds[name] = resolveAsFile(name, dir) || resolveAsDirectory(name, dir) ||
|
|
|
|
// @ts-ignore
|
|
|
|
(parent && parent.toString().startsWith(root) ? resolve(name, new File(parent).getParent()) : undefined);
|
|
|
|
return cacheModuleIds[name];
|
|
|
|
}
|
2017-09-23 09:42:16 +00:00
|
|
|
}
|
2020-01-31 18:41:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 解析文件
|
|
|
|
* @param {string} 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;
|
|
|
|
}
|
|
|
|
// JS文件
|
|
|
|
var js = new File(normalizeName(_absolute(file), '.js'));
|
|
|
|
if (js.isFile()) {
|
|
|
|
return js;
|
|
|
|
}
|
|
|
|
// JSON文件
|
|
|
|
var json = new File(normalizeName(_absolute(file), '.json'));
|
|
|
|
if (json.isFile()) {
|
|
|
|
return json;
|
|
|
|
}
|
2017-11-30 05:22:44 +00:00
|
|
|
}
|
2020-01-31 18:41:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 解析目录
|
|
|
|
* @param {string} file 文件
|
|
|
|
* @param {string | undefined} dir 目录
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
|
|
|
function resolveAsDirectory(file, dir) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// if no package or package.main exists, look for index.js
|
|
|
|
return resolveAsFile('index.js', dir);
|
2017-10-11 17:39:51 +00:00
|
|
|
}
|
2017-10-26 13:01:24 +00:00
|
|
|
|
2020-01-31 18:41:24 +00:00
|
|
|
/**
|
|
|
|
* 后缀检测和添加
|
|
|
|
* @param {string} fileName 文件名称
|
|
|
|
* @param {string} ext 后缀
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
|
|
|
function normalizeName(fileName, ext) {
|
|
|
|
var extension = ext || '.js';
|
|
|
|
if (fileName.endsWith(extension)) {
|
|
|
|
return fileName;
|
2017-10-11 17:39:51 +00:00
|
|
|
}
|
2020-01-31 18:41:24 +00:00
|
|
|
return fileName + extension;
|
2017-10-11 17:39:51 +00:00
|
|
|
}
|
2017-10-10 13:01:43 +00:00
|
|
|
|
2020-01-31 18:41:24 +00:00
|
|
|
/**
|
|
|
|
* 检查模块缓存
|
|
|
|
* @param {string} id 模块ID
|
|
|
|
* @param {string} name 模块名称
|
|
|
|
* @param {any} file 模块文件
|
|
|
|
* @param {any} optional 附加选项
|
|
|
|
* @returns {Object}
|
|
|
|
*/
|
|
|
|
function getCacheModule(id, name, file, optional) {
|
|
|
|
var module = cacheModules[id];
|
|
|
|
if (optional.cache && module) {
|
|
|
|
return module;
|
|
|
|
}
|
|
|
|
return createModule(id, name, file, optional)
|
2017-09-23 09:42:16 +00:00
|
|
|
}
|
|
|
|
|
2020-01-31 18:41:24 +00:00
|
|
|
/**
|
|
|
|
* 编译模块
|
|
|
|
* @param {string} id 模块ID
|
|
|
|
* @param {string} name 模块名称
|
|
|
|
* @param {any} file 模块文件
|
|
|
|
* @param {any} optional 附加选项
|
|
|
|
* @returns {Object}
|
|
|
|
*/
|
|
|
|
function createModule(id, name, file, optional) {
|
|
|
|
console.trace('Loading module', name + '(' + id + ')', 'Optional', JSON.stringify(optional));
|
|
|
|
var module = {
|
|
|
|
id: id,
|
|
|
|
exports: {},
|
|
|
|
loaded: false,
|
|
|
|
require: getRequire(file.parentFile, id)
|
|
|
|
};
|
|
|
|
cacheModules[id] = module;
|
|
|
|
var cfile = _canonical(file);
|
|
|
|
if (cfile.endsWith('.js')) {
|
2020-03-31 08:55:18 +00:00
|
|
|
compileJs(module, file, __assign(optional, { id: id }));
|
2020-01-31 18:41:24 +00:00
|
|
|
} 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);
|
|
|
|
}
|
2017-11-30 05:22:44 +00:00
|
|
|
return module;
|
|
|
|
}
|
2017-10-10 13:01:43 +00:00
|
|
|
|
2020-01-31 18:41:24 +00:00
|
|
|
/**
|
|
|
|
* 预编译JS
|
|
|
|
* @param {any} module JS模块
|
|
|
|
* @param {any} file JS文件
|
|
|
|
* @param {any} optional 附加选项
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
function compileJs(module, file, optional) {
|
|
|
|
// @ts-ignore
|
|
|
|
var origin = base.read(file);
|
|
|
|
if (optional.hook) {
|
|
|
|
origin = optional.hook(origin);
|
|
|
|
}
|
|
|
|
// 2019-09-19 使用 扩展函数直接 load 无需保存/删除文件
|
2020-02-16 06:48:54 +00:00
|
|
|
// 2020-02-16 结尾新增换行 防止有注释导致加载失败
|
2020-01-31 18:41:24 +00:00
|
|
|
// @ts-ignore
|
2020-03-31 08:55:18 +00:00
|
|
|
var compiledWrapper = engineLoad({ script: '(function $(module, exports, require, __dirname, __filename) {' + origin + '\n});', name: optional.id });
|
2020-01-31 18:41:24 +00:00
|
|
|
compiledWrapper.apply(module.exports, [
|
|
|
|
module, module.exports, module.require, file.parentFile, file
|
|
|
|
]);
|
|
|
|
module.loaded = true;
|
2017-11-30 05:22:44 +00:00
|
|
|
}
|
2017-09-23 09:42:16 +00:00
|
|
|
|
2020-01-31 18:41:24 +00:00
|
|
|
/**
|
|
|
|
* 预编译Json
|
|
|
|
* @param {{ id?: string | null; exports?: {}; loaded: any; require?: any; }} module Json模块
|
|
|
|
* @param {any} file Json 文件
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
function compileJson(module, file) {
|
|
|
|
// @ts-ignore
|
|
|
|
module.exports = JSON.parse(base.read(file));
|
|
|
|
module.loaded = true;
|
|
|
|
}
|
2017-09-27 12:40:57 +00:00
|
|
|
|
2020-01-31 18:41:24 +00:00
|
|
|
/**
|
|
|
|
* 尝试从网络下载依赖包
|
|
|
|
* @param {string} name 包名称
|
|
|
|
*/
|
|
|
|
function download(name) {
|
|
|
|
// handle name es6-map/implement => es6-map @ms/common/dist/reflect => @ms/common
|
|
|
|
var name_arr = name.split('/');
|
|
|
|
var module_name = name.startsWith('@') ? name_arr[0] + '/' + name_arr[1] : name_arr[0];
|
2020-03-31 08:55:18 +00:00
|
|
|
var target = root + separatorChar + 'node_modules' + separatorChar + module_name;
|
|
|
|
var _package = new File(target, 'package.json');
|
|
|
|
if (_package.exists()) { return }
|
2020-01-31 18:41:24 +00:00
|
|
|
// at windows need replace file name java.lang.IllegalArgumentException: Invalid prefix or suffix
|
2020-03-31 08:55:18 +00:00
|
|
|
var info = fetchPackageInfo(module_name);
|
2020-01-31 18:41:24 +00:00
|
|
|
var url = info.versions[info['dist-tags']['latest']].dist.tarball;
|
2020-03-02 16:57:19 +00:00
|
|
|
console.log('fetch node_module ' + module_name + ' from ' + url + ' waiting...')
|
2020-01-31 18:41:24 +00:00
|
|
|
var tis = new TarInputStream(new BufferedInputStream(new GZIPInputStream(new URL(url).openStream())));
|
|
|
|
// @ts-ignore
|
2020-03-31 08:55:18 +00:00
|
|
|
var entry;
|
2020-01-31 18:41:24 +00:00
|
|
|
while ((entry = tis.getNextEntry()) != null) {
|
2020-03-02 02:34:30 +00:00
|
|
|
var targetPath = Paths.get(target + separatorChar + entry.getName().substring(8));
|
2020-01-31 18:41:24 +00:00
|
|
|
targetPath.toFile().getParentFile().mkdirs();
|
|
|
|
Files.copy(tis, targetPath, StandardCopyOption.REPLACE_EXISTING);
|
|
|
|
}
|
|
|
|
return name;
|
2019-09-23 10:46:32 +00:00
|
|
|
}
|
|
|
|
|
2020-03-31 08:55:18 +00:00
|
|
|
function fetchPackageInfo(module_name) {
|
|
|
|
var tempFile = Files.createTempFile(module_name.replace('/', '_'), '.json');
|
2020-03-02 16:57:19 +00:00
|
|
|
try {
|
|
|
|
Files.copy(new URL('https://registry.npm.taobao.org/' + module_name).openStream(), tempFile, StandardCopyOption.REPLACE_EXISTING);
|
|
|
|
} catch (ex) {
|
|
|
|
console.debug('can\'t fetch package ' + module_name + ' from taobao registry. try fetch from yumc registry...')
|
|
|
|
Files.copy(new URL('https://repo.yumc.pw/repository/npm/' + module_name).openStream(), tempFile, StandardCopyOption.REPLACE_EXISTING);
|
|
|
|
}
|
|
|
|
tempFile.toFile().deleteOnExit();
|
|
|
|
return JSON.parse(new JavaString(Files.readAllBytes(tempFile), 'UTF-8'));
|
|
|
|
}
|
|
|
|
|
2020-01-31 18:41:24 +00:00
|
|
|
/**
|
|
|
|
* 检查核心模块
|
|
|
|
* @param {string} name
|
|
|
|
*/
|
2020-03-31 08:55:18 +00:00
|
|
|
function checkCoreModule(name, path) {
|
2020-01-31 18:41:24 +00:00
|
|
|
if (CoreModules.indexOf(name) != -1) {
|
2020-03-31 08:55:18 +00:00
|
|
|
var newName = '@ms/nodejs/dist/' + name
|
|
|
|
if (resolve(newName, path) !== undefined) {
|
|
|
|
return newName;
|
|
|
|
}
|
|
|
|
throw new Error("Can't load nodejs core module " + name + " . maybe later will auto replace to @ms/nodejs/" + name + ' to compatible...')
|
2020-01-31 18:41:24 +00:00
|
|
|
}
|
2020-03-31 08:55:18 +00:00
|
|
|
return name;
|
2020-01-17 10:39:23 +00:00
|
|
|
}
|
|
|
|
|
2020-01-31 18:41:24 +00:00
|
|
|
/**
|
|
|
|
* 加载模块
|
|
|
|
* @param {string} name 模块名称
|
|
|
|
* @param {string} path 路径
|
|
|
|
* @param {any} optional 附加选项
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
|
|
|
function _require(name, path, optional) {
|
2020-03-31 08:55:18 +00:00
|
|
|
name = checkCoreModule(name, path);
|
2020-01-31 18:41:24 +00:00
|
|
|
var file = new File(name);
|
|
|
|
file = _isFile(file) ? file : resolve(name, path);
|
|
|
|
optional = __assign({ cache: true }, optional);
|
|
|
|
if (file === undefined) {
|
|
|
|
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;
|
|
|
|
return _require(download(name), path, optional);
|
|
|
|
} catch (ex) {
|
|
|
|
notFoundModules[name] = true;
|
|
|
|
throw new FileNotFoundException("Can't found module " + name + ' in directory ' + path + ' ERROR: ' + ex)
|
2019-09-23 16:19:39 +00:00
|
|
|
}
|
2019-09-23 10:46:32 +00:00
|
|
|
}
|
2020-01-31 18:41:24 +00:00
|
|
|
// 重定向文件名称和类型
|
|
|
|
return getCacheModule(_canonical(file), file.name.split('.')[0], file, optional);
|
2017-10-10 13:01:43 +00:00
|
|
|
}
|
2017-09-23 09:42:16 +00:00
|
|
|
|
2020-01-31 18:41:24 +00:00
|
|
|
/**
|
|
|
|
* 闭包方法
|
|
|
|
* @param {string} parent 父目录
|
|
|
|
* @param {string} parentId
|
|
|
|
* @returns {Function}
|
|
|
|
*/
|
|
|
|
function exports(parent, parentId) {
|
|
|
|
var __DynamicRequire__ =
|
|
|
|
/**
|
|
|
|
* @param {string} path
|
|
|
|
* @param {any} optional
|
|
|
|
*/
|
|
|
|
function __DynamicRequire__(path, optional) {
|
|
|
|
return _require(path, parent, __assign({ parentId: parentId }, optional)).exports;
|
|
|
|
}
|
|
|
|
return __DynamicRequire__
|
|
|
|
}
|
2017-09-23 09:42:16 +00:00
|
|
|
|
2020-01-31 18:41:24 +00:00
|
|
|
/**
|
|
|
|
* @param {string} parent
|
|
|
|
* @param {string} parentId
|
|
|
|
*/
|
|
|
|
function getRequire(parent, parentId) {
|
|
|
|
/**
|
|
|
|
* @type {any} require
|
|
|
|
*/
|
|
|
|
var require = exports(parent, parentId)
|
|
|
|
require.resolve =
|
|
|
|
/**
|
|
|
|
* @param {string} name
|
|
|
|
*/
|
|
|
|
function __DynamicResolve__(name) {
|
|
|
|
return _canonical(new File(resolve(name, parent)))
|
|
|
|
}
|
|
|
|
require.clear =
|
|
|
|
/**
|
|
|
|
* @param {string} name
|
|
|
|
*/
|
|
|
|
function __DynamicClear__(name) {
|
2020-03-31 08:55:18 +00:00
|
|
|
for (var cacheModule in cacheModules) {
|
|
|
|
if (cacheModule.indexOf(name) != -1) {
|
|
|
|
console.trace('Clear module ' + cacheModule + ' ...')
|
|
|
|
delete cacheModules[cacheModule]
|
|
|
|
}
|
|
|
|
}
|
2020-01-31 18:41:24 +00:00
|
|
|
}
|
|
|
|
return require;
|
2020-01-17 10:39:23 +00:00
|
|
|
}
|
|
|
|
|
2020-01-31 18:41:24 +00:00
|
|
|
if (typeof parent === 'string') {
|
|
|
|
parent = new File(parent);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @type {{[key:string]:any}} cacheModules
|
|
|
|
*/
|
|
|
|
var cacheModules = {};
|
|
|
|
/**
|
2020-03-31 08:55:18 +00:00
|
|
|
* @type {{[key:string]:string}} cacheModuleIds
|
2020-01-31 18:41:24 +00:00
|
|
|
*/
|
|
|
|
var cacheModuleIds = {};
|
|
|
|
/**
|
2020-03-31 08:55:18 +00:00
|
|
|
* @type {{[key:string]:boolean}} notFoundModules
|
2020-01-31 18:41:24 +00:00
|
|
|
*/
|
|
|
|
var notFoundModules = {};
|
|
|
|
console.info('Initialization require module. ParentDir:', _canonical(parent));
|
|
|
|
return getRequire(parent, "null");
|
|
|
|
});
|