|
1 |
| -import $request from "sync-request"; |
2 |
| - |
| 1 | +// @ts-ignore |
| 2 | +import $xmlhttprequest from "xmlhttprequest-ssl"; |
3 | 3 |
|
4 | 4 | if (globalThis.XMLHttpRequest == null) {
|
5 |
| - globalThis.XMLHttpRequest = class extends EventTarget implements XMLHttpRequest { |
6 |
| - public static readonly UNSENT = 0; |
7 |
| - public static readonly OPENED = 1; |
8 |
| - public static readonly HEADERS_RECEIVED = 2; |
9 |
| - public static readonly LOADING = 3; |
10 |
| - public static readonly DONE = 4; |
11 |
| - |
12 |
| - public readonly UNSENT = XMLHttpRequest.UNSENT; |
13 |
| - public readonly OPENED = XMLHttpRequest.OPENED; |
14 |
| - public readonly HEADERS_RECEIVED = XMLHttpRequest.HEADERS_RECEIVED; |
15 |
| - public readonly LOADING = XMLHttpRequest.LOADING; |
16 |
| - public readonly DONE = XMLHttpRequest.DONE; |
17 |
| - |
18 |
| - public responseType!: XMLHttpRequestResponseType; |
19 |
| - public withCredentials!: boolean; |
20 |
| - public timeout!: number; |
21 |
| - |
22 |
| - public readonly readyState!: number; |
23 |
| - public readonly response!: ArrayBuffer | Blob | Document | string | null; |
24 |
| - public readonly responseText!: string; |
25 |
| - public readonly responseURL!: string; |
26 |
| - public readonly responseXML!: Document | null; |
27 |
| - public readonly status!: number; |
28 |
| - public readonly statusText!: string; |
29 |
| - public readonly upload!: XMLHttpRequestUpload; |
30 |
| - |
31 |
| - private _url!: string | URL | null; |
32 |
| - private _mime!: string; |
33 |
| - |
34 |
| - constructor() { |
35 |
| - super(); |
36 |
| - |
37 |
| - this._reset(); |
38 |
| - |
39 |
| - this._mime = "text/xml"; |
40 |
| - } |
41 |
| - |
42 |
| - private _reset() { |
43 |
| - (this as any).readyState = XMLHttpRequest.UNSENT; |
44 |
| - (this as any).response = null; |
45 |
| - (this as any).responseText = ""; |
46 |
| - (this as any).responseType = ""; |
47 |
| - (this as any).responseURL = ""; |
48 |
| - (this as any).responseXML = null; |
49 |
| - (this as any).status = 0; |
50 |
| - (this as any).statusText = ""; |
51 |
| - (this as any).timeout = 0; |
52 |
| - (this as any).upload = null; |
53 |
| - (this as any).withCredentials = false; |
54 |
| - |
55 |
| - this._url = null; |
56 |
| - } |
57 |
| - |
58 |
| - private _success() { |
59 |
| - (this as any).readyState = XMLHttpRequest.DONE; |
60 |
| - (this as any).status = 200; |
61 |
| - (this as any).statusText = "OK"; |
62 |
| - } |
63 |
| - |
64 |
| - public set onabort(value: () => void) { |
65 |
| - throw new Error("Not implemented"); |
66 |
| - } |
67 |
| - |
68 |
| - public set onerror(value: () => void) { |
69 |
| - throw new Error("Not implemented"); |
70 |
| - } |
71 |
| - |
72 |
| - public set onreadystatechange(value: () => void) { |
73 |
| - throw new Error("Not implemented"); |
74 |
| - } |
75 |
| - |
76 |
| - public set onloadstart(value: () => void) { |
77 |
| - throw new Error("Not implemented"); |
78 |
| - } |
79 |
| - |
80 |
| - public set onload(value: () => void) { |
81 |
| - throw new Error("Not implemented"); |
82 |
| - } |
83 |
| - |
84 |
| - public set onloadend(value: () => void) { |
85 |
| - throw new Error("Not implemented"); |
86 |
| - } |
87 |
| - |
88 |
| - public set onprogress(value: () => void) { |
89 |
| - throw new Error("Not implemented"); |
90 |
| - } |
91 |
| - |
92 |
| - public set ontimeout(value: () => void) { |
93 |
| - throw new Error("Not implemented"); |
94 |
| - } |
95 |
| - |
96 |
| - public abort() { |
97 |
| - throw new Error("Not implemented"); |
98 |
| - } |
99 |
| - |
100 |
| - public overrideMimeType(mime: string) { |
101 |
| - this._mime = mime; |
102 |
| - } |
103 |
| - |
104 |
| - public getResponseHeader(): string | null { |
105 |
| - throw new Error("Not implemented"); |
106 |
| - } |
107 |
| - |
108 |
| - public getAllResponseHeaders(): string { |
109 |
| - throw new Error("Not implemented"); |
110 |
| - } |
111 |
| - |
112 |
| - public setRequestHeader() { |
113 |
| - throw new Error("Not implemented"); |
114 |
| - } |
115 |
| - |
116 |
| - public open(method: string, url: string | URL, async: boolean = true, username?: string | null | undefined, password?: string | null | undefined): void { |
117 |
| - if (async) { |
118 |
| - throw new Error("Async XMLHttpRequest is not implemented yet"); |
119 |
| - } |
120 |
| - |
121 |
| - if (method !== "GET") { |
122 |
| - throw new Error("Non-GET requests are not implemented yet"); |
123 |
| - } |
124 |
| - |
125 |
| - this._reset(); |
126 |
| - |
127 |
| - this._url = url; |
128 |
| - } |
129 |
| - |
130 |
| - public send(body: null = null) { |
131 |
| - if (body !== null) { |
132 |
| - throw new Error("XMLHttpRequest send body is not implemented yet"); |
133 |
| - } |
134 |
| - |
135 |
| - if (!this._url) { |
136 |
| - throw new Error("You must call open before you call send"); |
137 |
| - } |
138 |
| - |
139 |
| - const response = $request("GET", this._url, { |
140 |
| - headers: { |
141 |
| - "Content-Type": this._mime, |
142 |
| - } |
143 |
| - }); |
144 |
| - |
145 |
| - const buffer = (response.body as Buffer).buffer as any; |
146 |
| - |
147 |
| - const responseText = new TextDecoder("iso-8859-5", { fatal: true }).decode(buffer); |
148 |
| - |
149 |
| - (this as any).response = (this as any).responseText = responseText; |
150 |
| - |
151 |
| - this._url = null; |
152 |
| - |
153 |
| - this._success(); |
154 |
| - } |
155 |
| - }; |
| 5 | + globalThis.XMLHttpRequest = $xmlhttprequest.XMLHttpRequest; |
156 | 6 | }
|
0 commit comments