feat: 优化XHR相关功能
Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
parent
51fd13e734
commit
157782b43e
@ -5,7 +5,7 @@ import * as base64 from 'base64-js'
|
|||||||
const Arrays = Java.type('java.util.Arrays')
|
const Arrays = Java.type('java.util.Arrays')
|
||||||
const Level = Java.type('java.util.logging.Level')
|
const Level = Java.type('java.util.logging.Level')
|
||||||
const Paths = Java.type('java.nio.file.Paths')
|
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 {
|
enum LogLevel {
|
||||||
ALL,
|
ALL,
|
||||||
@ -156,7 +156,7 @@ export class MiaoScriptConsole implements Console {
|
|||||||
let className = trace.className
|
let className = trace.className
|
||||||
var fileName = trace.fileName as string
|
var fileName = trace.fileName as string
|
||||||
var lineNumber = trace.lineNumber
|
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)
|
className = className.substr(className.lastIndexOf('$') + 1)
|
||||||
var { fileName, lineNumber } = this.readSourceMap(fileName, lineNumber)
|
var { fileName, lineNumber } = this.readSourceMap(fileName, lineNumber)
|
||||||
if (fileName.startsWith(root)) { fileName = fileName.split(root)[1] }
|
if (fileName.startsWith(root)) { fileName = fileName.split(root)[1] }
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
let containerStartTime = Date.now()
|
let containerStartTime = Date.now()
|
||||||
console.i18n("ms.core.ioc.initialize", { scope: global.scope })
|
console.i18n("ms.core.ioc.initialize", { scope: global.scope })
|
||||||
import { plugin, server, task, constants } from '@ccms/api'
|
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 })
|
console.i18n("ms.core.ioc.completed", { scope: global.scope, time: (Date.now() - containerStartTime) / 1000 })
|
||||||
import http from '@ccms/common/dist/http'
|
import http from '@ccms/common/dist/http'
|
||||||
|
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
import '@ccms/nashorn'
|
import '@ccms/nashorn'
|
||||||
|
|
||||||
const URL = Java.type("java.net.URL")
|
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 JavaString = Java.type("java.lang.String")
|
||||||
const SecureRandom = Java.type("java.security.SecureRandom")
|
const SecureRandom = Java.type("java.security.SecureRandom")
|
||||||
const SSLContext = Java.type("javax.net.ssl.SSLContext")
|
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 SocketTimeoutException = Java.type('java.net.SocketTimeoutException')
|
||||||
|
|
||||||
const Callable = Java.type('java.util.concurrent.Callable')
|
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 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 UTF_8 = "UTF-8"
|
||||||
|
|
||||||
const TrustAnyHostnameVerifier = new HostnameVerifier({ verify: () => true })
|
const TrustAnyHostnameVerifier = new HostnameVerifier({ verify: () => true })
|
||||||
@ -72,7 +74,7 @@ type HttpHeader = { [key: string]: string }
|
|||||||
const executor = Executors.newCachedThreadPool()
|
const executor = Executors.newCachedThreadPool()
|
||||||
|
|
||||||
export class XMLHttpRequest {
|
export class XMLHttpRequest {
|
||||||
private _timeout: number
|
private _timeout: number = 5000;
|
||||||
private _responseType: ResponseType = 'text';
|
private _responseType: ResponseType = 'text';
|
||||||
private _withCredentials: boolean
|
private _withCredentials: boolean
|
||||||
|
|
||||||
@ -170,6 +172,7 @@ export class XMLHttpRequest {
|
|||||||
this._connection.setConnectTimeout(this._timeout)
|
this._connection.setConnectTimeout(this._timeout)
|
||||||
this._connection.setReadTimeout(this._timeout)
|
this._connection.setReadTimeout(this._timeout)
|
||||||
|
|
||||||
|
this.setRequestHeader('X-Requested-With', 'XMLHttpRequest')
|
||||||
this.setReadyState(ReadyState.OPENED)
|
this.setReadyState(ReadyState.OPENED)
|
||||||
}
|
}
|
||||||
send(body?: string | object): Future<string> {
|
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}!`) }
|
if (this._readyState !== ReadyState.OPENED) { throw new Error(`Error Status ${this._readyState}!`) }
|
||||||
let future = executor.submit(new Callable({ call: () => this._send(body) }))
|
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
|
return future
|
||||||
}
|
}
|
||||||
get() {
|
get() {
|
||||||
@ -248,8 +251,16 @@ export class XMLHttpRequest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private readOutput(input: any) {
|
private readOutput(input: any) {
|
||||||
var tempFile = Files.createTempFile('xhr', '.response')
|
let output = new ByteArrayOutputStream()
|
||||||
Files.copy(input, tempFile, StandardCopyOption['REPLACE_EXISTING']); tempFile.toFile().deleteOnExit()
|
let buffer = new ByteArray(1024)
|
||||||
return new JavaString(Files.readAllBytes(tempFile), 'UTF-8')
|
try {
|
||||||
|
let n: number
|
||||||
|
while ((n = input.read(buffer)) != -1) {
|
||||||
|
output.write(buffer, 0, n)
|
||||||
|
}
|
||||||
|
return output.toString(UTF_8)
|
||||||
|
} finally {
|
||||||
|
output.close()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user