feat: 优化XHR相关功能

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
MiaoWoo 2021-07-10 18:14:55 +08:00
parent 51fd13e734
commit 157782b43e
3 changed files with 21 additions and 10 deletions

View File

@ -5,7 +5,7 @@ import * as base64 from 'base64-js'
const Arrays = Java.type('java.util.Arrays')
const Level = Java.type('java.util.logging.Level')
const Paths = Java.type('java.nio.file.Paths')
const ignoreLogPrefix = ['java.', 'javax.', 'sun.', 'net.minecraft.', 'org.bukkit.', 'jdk.nashorn.', 'io.netty.', 'org.spongepowered.', 'org.apache', 'org.springframework']
const ignoreLogPrefix = ['java.', 'javax.', 'sun.', 'net.minecraft.', 'org.bukkit.', 'jdk.nashorn.', 'org.openjdk.nashorn', 'io.netty.', 'org.spongepowered.', 'org.apache', 'org.springframework']
enum LogLevel {
ALL,
@ -156,7 +156,7 @@ export class MiaoScriptConsole implements Console {
let className = trace.className
var fileName = trace.fileName as string
var lineNumber = trace.lineNumber
if (className.startsWith('jdk.nashorn.internal.scripts')) {
if (className.startsWith('jdk.nashorn.internal.scripts') || className.startsWith('org.openjdk.nashorn.internal.scripts')) {
className = className.substr(className.lastIndexOf('$') + 1)
var { fileName, lineNumber } = this.readSourceMap(fileName, lineNumber)
if (fileName.startsWith(root)) { fileName = fileName.split(root)[1] }

View File

@ -1,7 +1,7 @@
let containerStartTime = Date.now()
console.i18n("ms.core.ioc.initialize", { scope: global.scope })
import { plugin, server, task, constants } from '@ccms/api'
import { DefaultContainer as container, inject, provideSingleton, ContainerInstance, buildProviderModule, Autowired } from '@ccms/container'
import { DefaultContainer as container, provideSingleton, ContainerInstance, buildProviderModule, Autowired } from '@ccms/container'
console.i18n("ms.core.ioc.completed", { scope: global.scope, time: (Date.now() - containerStartTime) / 1000 })
import http from '@ccms/common/dist/http'

View File

@ -1,8 +1,6 @@
import '@ccms/nashorn'
const URL = Java.type("java.net.URL")
const Files = Java.type("java.nio.file.Files")
const StandardCopyOption = Java.type("java.nio.file.StandardCopyOption")
const JavaString = Java.type("java.lang.String")
const SecureRandom = Java.type("java.security.SecureRandom")
const SSLContext = Java.type("javax.net.ssl.SSLContext")
@ -13,8 +11,12 @@ const X509TrustManager = Java.type("javax.net.ssl.X509TrustManager")
const SocketTimeoutException = Java.type('java.net.SocketTimeoutException')
const Callable = Java.type('java.util.concurrent.Callable')
const TimeUnit = Java.type('java.util.concurrent.TimeUnit')
const Executors = Java.type('java.util.concurrent.Executors')
const ByteArrayOutputStream = Java.type("java.io.ByteArrayOutputStream")
const ByteArray = Java.type("byte[]")
const UTF_8 = "UTF-8"
const TrustAnyHostnameVerifier = new HostnameVerifier({ verify: () => true })
@ -72,7 +74,7 @@ type HttpHeader = { [key: string]: string }
const executor = Executors.newCachedThreadPool()
export class XMLHttpRequest {
private _timeout: number
private _timeout: number = 5000;
private _responseType: ResponseType = 'text';
private _withCredentials: boolean
@ -170,6 +172,7 @@ export class XMLHttpRequest {
this._connection.setConnectTimeout(this._timeout)
this._connection.setReadTimeout(this._timeout)
this.setRequestHeader('X-Requested-With', 'XMLHttpRequest')
this.setReadyState(ReadyState.OPENED)
}
send(body?: string | object): Future<string> {
@ -178,7 +181,7 @@ export class XMLHttpRequest {
}
if (this._readyState !== ReadyState.OPENED) { throw new Error(`Error Status ${this._readyState}!`) }
let future = executor.submit(new Callable({ call: () => this._send(body) }))
if (!this._async) { future.get() }
if (!this._async) { future.get(this._timeout, TimeUnit.MILLISECONDS) }
return future
}
get() {
@ -248,8 +251,16 @@ export class XMLHttpRequest {
}
private readOutput(input: any) {
var tempFile = Files.createTempFile('xhr', '.response')
Files.copy(input, tempFile, StandardCopyOption['REPLACE_EXISTING']); tempFile.toFile().deleteOnExit()
return new JavaString(Files.readAllBytes(tempFile), 'UTF-8')
let output = new ByteArrayOutputStream()
let buffer = new ByteArray(1024)
try {
let n: number
while ((n = input.read(buffer)) != -1) {
output.write(buffer, 0, n)
}
return output.toString(UTF_8)
} finally {
output.close()
}
}
}