feat: support custom NODE env
Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
parent
eba0e18285
commit
9bb67410ac
4
pom.xml
4
pom.xml
@ -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.7.0</version>
|
<version>0.7.3</version>
|
||||||
<developers>
|
<developers>
|
||||||
<developer>
|
<developer>
|
||||||
<id>502647092</id>
|
<id>502647092</id>
|
||||||
@ -53,6 +53,8 @@
|
|||||||
<properties>
|
<properties>
|
||||||
<env.GIT_COMMIT>DEV</env.GIT_COMMIT>
|
<env.GIT_COMMIT>DEV</env.GIT_COMMIT>
|
||||||
<update.changes>
|
<update.changes>
|
||||||
|
§620-06-23 §afeat: 支持自定义参数;
|
||||||
|
§620-06-22 §afeat: 优化 require 加载逻辑;
|
||||||
§620-05-28 §afeat: 新增 Spring 的支持;
|
§620-05-28 §afeat: 新增 Spring 的支持;
|
||||||
§620-05-02 §afeat: 调整 scope 为 @ccms;
|
§620-05-02 §afeat: 调整 scope 为 @ccms;
|
||||||
§620-04-10 §afeat: 默认从 classpath 加载内建的js模块;
|
§620-04-10 §afeat: 默认从 classpath 加载内建的js模块;
|
||||||
|
@ -6,7 +6,7 @@ var global = this;
|
|||||||
(function () {
|
(function () {
|
||||||
var loader;
|
var loader;
|
||||||
global.boot = function (root, logger) {
|
global.boot = function (root, logger) {
|
||||||
global.scope = "@ccms";
|
global.scope = java.lang.System.getenv("MS_NODE_CORE_SCOPE") || "@ccms";
|
||||||
global.log = logger;
|
global.log = logger;
|
||||||
// Development Env Detect
|
// Development Env Detect
|
||||||
global.root = root || "src/main/resources";
|
global.root = root || "src/main/resources";
|
||||||
@ -27,8 +27,10 @@ var global = this;
|
|||||||
// Async Loading MiaoScript Engine
|
// Async Loading MiaoScript Engine
|
||||||
new java.lang.Thread(function () {
|
new java.lang.Thread(function () {
|
||||||
java.lang.Thread.currentThread().contextClassLoader = loader;
|
java.lang.Thread.currentThread().contextClassLoader = loader;
|
||||||
load('classpath:core/ployfill.js')(root, logger);
|
load(java.lang.System.getenv("MS_NODE_CORE_PLOYFILL") || 'classpath:core/ployfill.js')(root, logger);
|
||||||
global.engineDisable = require(global.scope + '/core').default || function () { logger.info('Error: abnormal Initialization MiaoScript Engine. Skip disable step...') };
|
global.engineDisable = require(java.lang.System.getenv("MS_NODE_CORE_MODULE") || global.scope + '/core').default || function () {
|
||||||
|
logger.info('Error: abnormal Initialization MiaoScript Engine. Skip disable step...')
|
||||||
|
};
|
||||||
}, "MiaoScript thread").start()
|
}, "MiaoScript thread").start()
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -20,10 +20,9 @@
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
// Init console and require
|
// Init console and require
|
||||||
global.console = engineLoad('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('classpath:core/require.js')(root);
|
global.require = engineLoad(java.lang.System.getenv("MS_NODE_CORE_REQUIRE") || 'classpath:core/require.js')(root);
|
||||||
require(global.scope + '/ployfill')
|
require(global.scope + '/ployfill')
|
||||||
require(global.scope + '/nodejs')
|
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
@ -27,7 +27,7 @@
|
|||||||
// @ts-check
|
// @ts-check
|
||||||
(
|
(
|
||||||
/**
|
/**
|
||||||
* @param {any} parent
|
* @param {string} parent
|
||||||
*/
|
*/
|
||||||
function (parent) {
|
function (parent) {
|
||||||
'use strict';
|
'use strict';
|
||||||
@ -55,6 +55,13 @@
|
|||||||
var JavaString = Java.type('java.lang.String')
|
var JavaString = Java.type('java.lang.String')
|
||||||
var separatorChar = File.separatorChar;
|
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 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",
|
||||||
@ -70,13 +77,16 @@
|
|||||||
function __assign(t) {
|
function __assign(t) {
|
||||||
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
||||||
s = arguments[i];
|
s = arguments[i];
|
||||||
if (s === undefined) { continue; };
|
if (s === undefined) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
||||||
t[p] = s[p];
|
t[p] = s[p];
|
||||||
}
|
}
|
||||||
return t;
|
return t;
|
||||||
};
|
}
|
||||||
|
|
||||||
|
// noinspection JSValidateJSDoc
|
||||||
/**
|
/**
|
||||||
* 判断是否为一个文件
|
* 判断是否为一个文件
|
||||||
* @param {any} file
|
* @param {any} file
|
||||||
@ -109,23 +119,26 @@
|
|||||||
* 按照下列顺序查找
|
* 按照下列顺序查找
|
||||||
* 当前目录 ./
|
* 当前目录 ./
|
||||||
* 父目录 ../
|
* 父目录 ../
|
||||||
* 模块目录 /node_modules
|
* 递归模块目录 ../node_modules 到root
|
||||||
|
* 寻找 ${NODE_PATH}
|
||||||
* @param {string} name 模块名称
|
* @param {string} name 模块名称
|
||||||
* @param {string} parent 父目录
|
* @param {string} parent 父目录
|
||||||
*/
|
*/
|
||||||
function resolve(name, parent) {
|
function resolve(name, parent) {
|
||||||
name = _canonical(name) || name;
|
name = _canonical(name) || name;
|
||||||
|
if (cacheModuleIds[name]) return cacheModuleIds[name]
|
||||||
// 解析本地目录
|
// 解析本地目录
|
||||||
if (name.startsWith('./') || name.startsWith('../')) {
|
if (name.startsWith('./') || name.startsWith('../')) {
|
||||||
return resolveAsFile(name, parent) || resolveAsDirectory(name, parent) || undefined;
|
var moduleId = parent + '||' + name
|
||||||
|
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);
|
||||||
if (cacheModuleIds[name]) return cacheModuleIds[name]
|
return cacheModuleIds[name] = resolveAsFile(name, dir) || resolveAsDirectory(name, dir) ||
|
||||||
cacheModuleIds[name] = resolveAsFile(name, dir) || resolveAsDirectory(name, dir) ||
|
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
(parent && parent.toString().startsWith(root) ? resolve(name, new File(parent).getParent()) : undefined);
|
(parent && parent.toString().startsWith(root) ?
|
||||||
return cacheModuleIds[name];
|
resolve(name, new File(parent).getParent()) : resolveAsDirectory(name, NODE_PATH) || undefined);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -136,7 +149,7 @@
|
|||||||
* @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
|
// @ts-ignore
|
||||||
if (file.isFile()) {
|
if (file.isFile()) {
|
||||||
@ -161,7 +174,7 @@
|
|||||||
* @returns {*}
|
* @returns {*}
|
||||||
*/
|
*/
|
||||||
function resolveAsDirectory(file, dir) {
|
function resolveAsDirectory(file, dir) {
|
||||||
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
|
// @ts-ignore
|
||||||
@ -250,7 +263,10 @@
|
|||||||
// 2019-09-19 使用 扩展函数直接 load 无需保存/删除文件
|
// 2019-09-19 使用 扩展函数直接 load 无需保存/删除文件
|
||||||
// 2020-02-16 结尾新增换行 防止有注释导致加载失败
|
// 2020-02-16 结尾新增换行 防止有注释导致加载失败
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
var compiledWrapper = engineLoad({ script: '(function $(module, exports, require, __dirname, __filename) {' + origin + '\n});', name: optional.id });
|
var compiledWrapper = engineLoad({
|
||||||
|
script: '(function $(module, exports, require, __dirname, __filename) {' + origin + '\n});',
|
||||||
|
name: optional.id
|
||||||
|
});
|
||||||
compiledWrapper.apply(module.exports, [
|
compiledWrapper.apply(module.exports, [
|
||||||
module, module.exports, module.require, file.parentFile, file
|
module, module.exports, module.require, file.parentFile, file
|
||||||
]);
|
]);
|
||||||
@ -278,7 +294,7 @@
|
|||||||
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
|
// @ts-ignore
|
||||||
var target = root + separatorChar + 'node_modules' + 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()) { return }
|
if (_package.exists()) { return }
|
||||||
// at windows need replace file name java.lang.IllegalArgumentException: Invalid prefix or suffix
|
// at windows need replace file name java.lang.IllegalArgumentException: Invalid prefix or suffix
|
||||||
@ -302,10 +318,10 @@
|
|||||||
function fetchPackageInfo(module_name) {
|
function fetchPackageInfo(module_name) {
|
||||||
var tempFile = Files.createTempFile(module_name.replace('/', '_'), '.json');
|
var tempFile = Files.createTempFile(module_name.replace('/', '_'), '.json');
|
||||||
try {
|
try {
|
||||||
Files.copy(new URL('https://registry.npm.taobao.org/' + module_name).openStream(), tempFile, StandardCopyOption.REPLACE_EXISTING);
|
Files.copy(new URL(NODE_REGISTRY + '/' + module_name).openStream(), tempFile, StandardCopyOption.REPLACE_EXISTING);
|
||||||
} catch (ex) {
|
} catch (ex) {
|
||||||
console.debug('can\'t fetch package ' + module_name + ' from taobao registry. try fetch from yumc registry...')
|
console.debug('can\'t fetch package ' + module_name + ' from ' + NODE_REGISTRY + ' registry. try fetch from ' + MS_NODE_REGISTRY + ' registry...')
|
||||||
Files.copy(new URL('https://repo.yumc.pw/repository/npm/' + module_name).openStream(), tempFile, StandardCopyOption.REPLACE_EXISTING);
|
Files.copy(new URL(MS_NODE_REGISTRY + '/' + module_name).openStream(), tempFile, StandardCopyOption.REPLACE_EXISTING);
|
||||||
}
|
}
|
||||||
tempFile.toFile().deleteOnExit();
|
tempFile.toFile().deleteOnExit();
|
||||||
return JSON.parse(new JavaString(Files.readAllBytes(tempFile), 'UTF-8'));
|
return JSON.parse(new JavaString(Files.readAllBytes(tempFile), 'UTF-8'));
|
||||||
@ -325,7 +341,7 @@
|
|||||||
} else {
|
} else {
|
||||||
lastModule = name
|
lastModule = name
|
||||||
}
|
}
|
||||||
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) !== undefined) {
|
||||||
@ -374,44 +390,46 @@
|
|||||||
* @returns {Function}
|
* @returns {Function}
|
||||||
*/
|
*/
|
||||||
function exports(parent, parentId) {
|
function exports(parent, parentId) {
|
||||||
var __DynamicRequire__ =
|
/**
|
||||||
/**
|
* @param {string} path
|
||||||
* @param {string} path
|
* @param {any} optional
|
||||||
* @param {any} optional
|
*/
|
||||||
*/
|
return function __DynamicRequire__(path, optional) {
|
||||||
function __DynamicRequire__(path, optional) {
|
return _require(path, parent, __assign({ parentId: parentId }, optional)).exports;
|
||||||
return _require(path, parent, __assign({ parentId: parentId }, optional)).exports;
|
}
|
||||||
}
|
|
||||||
return __DynamicRequire__
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {string} name
|
* @param {string} name
|
||||||
*/
|
*/
|
||||||
function __DynamicResolve__(name) {
|
function __DynamicResolve__(name) {
|
||||||
return _canonical(new File(resolve(name, parent)))
|
return _canonical(new File(resolve(name, parent)))
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {string} name
|
* @param {string} name
|
||||||
*/
|
*/
|
||||||
function __DynamicClear__(name) {
|
function __DynamicClear__(name) {
|
||||||
for (var cacheModule in cacheModules) {
|
for (var cacheModule in cacheModules) {
|
||||||
if (cacheModule.indexOf(name) != -1) {
|
if (cacheModule.indexOf(name) !== -1) {
|
||||||
console.trace('Clear module ' + cacheModule + ' ...')
|
console.trace('Clear module ' + cacheModule + ' ...')
|
||||||
delete cacheModules[cacheModule]
|
delete cacheModules[cacheModule]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function __DynamicDisable__() {
|
function __DynamicDisable__() {
|
||||||
for (var cacheModule in cacheModules) {
|
for (var cacheModule in cacheModules) {
|
||||||
delete cacheModules[cacheModule]
|
delete cacheModules[cacheModule]
|
||||||
}
|
}
|
||||||
cacheModules = undefined;
|
cacheModules = undefined;
|
||||||
for (var cacheModule in cacheModuleIds) {
|
for (var cacheModuleId in cacheModuleIds) {
|
||||||
delete cacheModuleIds[cacheModule]
|
delete cacheModuleIds[cacheModuleId]
|
||||||
}
|
}
|
||||||
cacheModuleIds = undefined;
|
cacheModuleIds = undefined;
|
||||||
notFoundModules = undefined;
|
notFoundModules = undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {string} parent
|
* @param {string} parent
|
||||||
* @param {string} parentId
|
* @param {string} parentId
|
||||||
@ -444,5 +462,9 @@
|
|||||||
*/
|
*/
|
||||||
var notFoundModules = {};
|
var notFoundModules = {};
|
||||||
console.info('Initialization require module. ParentDir:', _canonical(parent));
|
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);
|
||||||
return getRequire(parent, "null");
|
return getRequire(parent, "null");
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user