feat: optimize network request

Signed-off-by: MiaoWoo <admin@yumc.pw>
master
MiaoWoo 2021-07-10 18:29:44 +08:00
parent fc7fb67023
commit cfd44a6289
2 changed files with 38 additions and 27 deletions

16
pom.xml
View File

@ -1,9 +1,8 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<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.15.0</version> <version>0.16.0</version>
<developers> <developers>
<developer> <developer>
<id>502647092</id> <id>502647092</id>
@ -54,14 +53,15 @@
<properties> <properties>
<env.GIT_COMMIT>DEV</env.GIT_COMMIT> <env.GIT_COMMIT>DEV</env.GIT_COMMIT>
<update.changes> <update.changes>
§621-07-10 §afeat: 优化网络相关功能;
§621-06-25 §afeat: 调整启动逻辑 兼容 Arclight; §621-06-25 §afeat: 调整启动逻辑 兼容 Arclight;
§621-06-22 §afeat: 新增本地版本锁定功能; §621-06-22 §afeat: 新增本地版本锁定功能;
§621-06-19 §afeat: 兼容JDK16 反射异常; §621-06-19 §afeat: 兼容JDK16 反射异常;
§621-05-15 §afeat: 兼容JDK15+ 自动下载Nashorn类库; §621-05-15 §afeat: 兼容JDK15+ 自动下载Nashorn类库
§621-03-25 §afeat: 异步加载 polyfill 并且同步加载 @ccms/core;
§621-03-25 §cfix: 修改 ployfill 为 polyfill
</update.changes> </update.changes>
<update.changelog> <update.changelog>
§621-03-25 §afeat: 异步加载 polyfill 并且同步加载 @ccms/core;
§621-03-25 §cfix: 修改 ployfill 为 polyfill;
§620-12-22 §cfix: 增加 require 效验; §620-12-22 §cfix: 增加 require 效验;
§620-12-17 §afeat: JavaScriptTask 新增任务ID 并通过 ID 比较优先级; §620-12-17 §afeat: JavaScriptTask 新增任务ID 并通过 ID 比较优先级;
§620-12-16 §afeat: 新增 require 缓存 优化路径寻找速度; §620-12-16 §afeat: 新增 require 缓存 优化路径寻找速度;
@ -199,13 +199,13 @@
<dependency> <dependency>
<groupId>org.spigotmc</groupId> <groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId> <artifactId>spigot-api</artifactId>
<version>1.16.2-R0.1-SNAPSHOT</version> <version>1.17.1-R0.1-SNAPSHOT</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.spongepowered</groupId> <groupId>org.spongepowered</groupId>
<artifactId>spongeapi</artifactId> <artifactId>spongeapi</artifactId>
<version>7.2.0</version> <version>7.3.0</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<dependency> <dependency>

View File

@ -50,11 +50,15 @@
// @ts-ignore // @ts-ignore
var URL = Java.type('java.net.URL') var URL = Java.type('java.net.URL')
// @ts-ignore // @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') var Thread = Java.type('java.lang.Thread')
// @ts-ignore // @ts-ignore
var FutureTask = Java.type('java.util.concurrent.FutureTask') var Callable = Java.type('java.util.concurrent.Callable')
// @ts-ignore // @ts-ignore
var JavaString = Java.type('java.lang.String') var Executors = Java.type('java.util.concurrent.Executors')
var separatorChar = File.separatorChar var separatorChar = File.separatorChar
// @ts-ignore // @ts-ignore
@ -238,7 +242,7 @@
cacheModules[id] = module cacheModules[id] = module
var cfile = _canonical(file) var cfile = _canonical(file)
if (cfile.endsWith('.js')) { if (cfile.endsWith('.js')) {
compileJs(module, file, __assign(optional, {id: id})) compileJs(module, file, __assign(optional, { id: id }))
} else if (cfile.endsWith('.json')) { } else if (cfile.endsWith('.json')) {
compileJson(module, file) compileJson(module, file)
} else if (cfile.endsWith('.msm')) { } else if (cfile.endsWith('.msm')) {
@ -305,7 +309,7 @@
var info = fetchPackageInfo(module_name) var info = fetchPackageInfo(module_name)
var url = info.versions[ModulesVersionLock[module_name] || info['dist-tags']['latest']].dist.tarball var url = info.versions[ModulesVersionLock[module_name] || info['dist-tags']['latest']].dist.tarball
console.log('fetch node_module ' + module_name + ' from ' + url + ' waiting...') console.log('fetch node_module ' + module_name + ' from ' + url + ' waiting...')
var future = new FutureTask(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 // @ts-ignore
var entry var entry
@ -315,9 +319,7 @@
Files.copy(tis, targetPath, StandardCopyOption.REPLACE_EXISTING) Files.copy(tis, targetPath, StandardCopyOption.REPLACE_EXISTING)
} }
return name return name
}) })).get()
new Thread(future, "MiaoScript download thread").start()
return future.get()
} }
/** /**
@ -326,23 +328,29 @@
function fetchPackageInfo(module_name) { function fetchPackageInfo(module_name) {
var content = '' var content = ''
try { try {
content = fetchContent(NODE_REGISTRY + '/' + module_name, module_name) content = fetchContent(NODE_REGISTRY + '/' + module_name)
} catch (ex) { } catch (ex) {
console.debug('can\'t fetch package ' + module_name + ' from ' + NODE_REGISTRY + ' registry. try fetch from ' + MS_NODE_REGISTRY + ' registry...') 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, module_name) content = fetchContent(MS_NODE_REGISTRY + '/' + module_name)
} }
return JSON.parse(content) return JSON.parse(content)
} }
function fetchContent(url, name) { function fetchContent(url) {
var future = new FutureTask(function () { return executor.submit(new Callable(function () {
var tempFile = Files.createTempFile(name.replace('/', '_'), '.json') var input = new URL(url).openStream()
Files.copy(new URL(url).openStream(), tempFile, StandardCopyOption.REPLACE_EXISTING) var output = new ByteArrayOutputStream()
tempFile.toFile().deleteOnExit() var buffer = new ByteArray(1024)
return new JavaString(Files.readAllBytes(tempFile), 'UTF-8') try {
}) var n
new Thread(future, "MiaoScript require thread").start() while ((n = input.read(buffer)) !== -1) {
return future.get() output.write(buffer, 0, n)
}
return output.toString("UTF-8")
} finally {
output.close()
}
})).get()
} }
var lastModule = '' var lastModule = ''
@ -540,6 +548,9 @@
*/ */
var notFoundModules = {} var notFoundModules = {}
var upgradeMode = false var upgradeMode = false
var executor = Executors.newSingleThreadExecutor(function (r) {
return new Thread(r, "MiaoScript require thread")
})
console.info('Initialization require module. ParentDir:', _canonical(parent)) console.info('Initialization require module. ParentDir:', _canonical(parent))
console.info('Require module env list:') console.info('Require module env list:')
console.info('- NODE_PATH:', NODE_PATH) console.info('- NODE_PATH:', NODE_PATH)
@ -554,7 +565,7 @@
console.log('Initialization new cacheModuleIds') console.log('Initialization new cacheModuleIds')
} }
try { try {
ModulesVersionLock = JSON.parse(fetchContent('http://ms.yumc.pw/api/plugin/download/name/version_lock', 'version_lock')) ModulesVersionLock = JSON.parse(fetchContent('http://ms.yumc.pw/api/plugin/download/name/version_lock'))
try { try {
// @ts-ignore // @ts-ignore
ModulesVersionLock = __assign(ModulesVersionLock, JSON.parse(base.read(localVersionLockFile))) ModulesVersionLock = __assign(ModulesVersionLock, JSON.parse(base.read(localVersionLockFile)))