feat: add node shim

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
MiaoWoo 2020-05-11 22:13:03 +08:00
parent b323d16123
commit 287585d8f5
3 changed files with 26 additions and 13 deletions

View File

@ -6,6 +6,7 @@ i18n.initialize();
console.i18n("ms.ployfill.initialize"); console.i18n("ms.ployfill.initialize");
require('./es5-ext'); require('./es5-ext');
require('core-js'); require('core-js');
require('./node-shim');
global.setGlobal('Proxy', require('./proxy').Proxy) global.setGlobal('Proxy', require('./proxy').Proxy)
global.setGlobal('XMLHttpRequest', require('./xml-http-request').XMLHttpRequest) global.setGlobal('XMLHttpRequest', require('./xml-http-request').XMLHttpRequest)
global.setGlobal('Blob', require('blob-polyfill').Blob) global.setGlobal('Blob', require('blob-polyfill').Blob)

View File

@ -0,0 +1,8 @@
global.setGlobal('process', {
env: {
__noSuchProperty__: (prop) => {
return Packages.java.lang.System.getenv(prop)
}
},
platform: Packages.java.lang.System.getProperty("os.name")
}, {})

View File

@ -82,6 +82,7 @@ export class XMLHttpRequest {
private _url: string; private _url: string;
private _async: boolean; private _async: boolean;
private _mimeType: string; private _mimeType: string;
private _requestHeaders: HttpHeader = {};
private _status: number = 0; private _status: number = 0;
private _statusText: string = null; private _statusText: string = null;
@ -126,17 +127,17 @@ export class XMLHttpRequest {
return this._responseURL; return this._responseURL;
} }
onload() { } public onload: () => void;
onerror(ex: Error) { } public onerror: (ex: Error) => void;
onabort() { } public onabort: () => void;
onprogress() { } public onprogress: () => void;
ontimeout(ex: Error) { } public ontimeout: (ex: Error) => void;
onloadend() { } public onloadend: () => void;
onloadstart() { } public onloadstart: () => void;
onreadystatechange() { } public onreadystatechange: () => void;
setRequestHeader(key: string, val: string) { setRequestHeader(key: string, val: string) {
this._connection.setRequestProperty(key, val); this._requestHeaders[key] = val;
} }
getResponseHeader(key: string): string { getResponseHeader(key: string): string {
return this._responseHeaders[key]; return this._responseHeaders[key];
@ -171,6 +172,9 @@ export class XMLHttpRequest {
this.setReadyState(ReadyState.OPENED); this.setReadyState(ReadyState.OPENED);
} }
send(body?: string | object): Future<string> { send(body?: string | object): Future<string> {
for (const header in this._requestHeaders) {
this._connection.setRequestProperty(header, this._requestHeaders[header]);
}
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() }
@ -188,13 +192,13 @@ export class XMLHttpRequest {
} }
abort() { abort() {
this._connection.disconnect(); this._connection.disconnect();
this.onabort(); this.onabort && this.onabort();
} }
private _send(body?: string | object) { private _send(body?: string | object) {
try { try {
this._connection.connect(); this._connection.connect();
this.onloadstart(); this.onloadstart && this.onloadstart();
if (body) { if (body) {
let bodyType = Object.prototype.toString.call(body); let bodyType = Object.prototype.toString.call(body);
if (typeof body !== "string") { throw new Error(`body(${bodyType}) must be string!`) } if (typeof body !== "string") { throw new Error(`body(${bodyType}) must be string!`) }
@ -214,7 +218,7 @@ export class XMLHttpRequest {
this._response = this.readOutput(this._connection.getErrorStream()); this._response = this.readOutput(this._connection.getErrorStream());
} }
this.setResponseHeaders(this._connection.getHeaderFields()); this.setResponseHeaders(this._connection.getHeaderFields());
this.onloadend(); this.onloadend && this.onloadend();
} catch (ex) { } catch (ex) {
if (ex instanceof SocketTimeoutException && this.ontimeout) { if (ex instanceof SocketTimeoutException && this.ontimeout) {
return this.ontimeout(ex) return this.ontimeout(ex)
@ -236,7 +240,7 @@ export class XMLHttpRequest {
private setReadyState(state: ReadyState) { private setReadyState(state: ReadyState) {
this._readyState = state; this._readyState = state;
this.onreadystatechange(); this.onreadystatechange && this.onreadystatechange();
} }
private readOutput(input: any) { private readOutput(input: any) {