Skip to content

Commit 79fd85b

Browse files
committed
Fixing XMLHttpRequest polyfill
1 parent f6b0f39 commit 79fd85b

File tree

5 files changed

+233
-6
lines changed

5 files changed

+233
-6
lines changed

sdk/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
"comlink": "^4.4.2",
5252
"core-js": "^3.40.0",
5353
"mime": "^4.0.6",
54+
"sync-request": "^6.1.0",
5455
"xmlhttprequest-ssl": "^3.1.0"
5556
},
5657
"devDependencies": {

sdk/rollup.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export default networks.map((network) => {
2727
"node:fs",
2828
"node:crypto",
2929
"mime/lite",
30+
"sync-request",
3031
"xmlhttprequest-ssl",
3132

3233
// Used by the SDK

sdk/rollup.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export default networks.map((network) => {
3434
"node:fs",
3535
"node:crypto",
3636
"mime/lite",
37+
"sync-request",
3738
"xmlhttprequest-ssl",
3839

3940
// Used by the SDK

sdk/src/polyfill/xmlhttprequest.ts

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,67 @@
11
// @ts-ignore
22
import $xmlhttprequest from "xmlhttprequest-ssl";
3+
import $request from "sync-request";
34

45
if (globalThis.XMLHttpRequest == null) {
5-
globalThis.XMLHttpRequest = $xmlhttprequest.XMLHttpRequest;
6+
globalThis.XMLHttpRequest = class extends $xmlhttprequest.XMLHttpRequest {
7+
// We have to override the methods inside of the `constructor`
8+
// because `xmlhttprequest-ssl` doesn't use a regular class,
9+
// instead it defines all of the methods inside of the constructor.
10+
constructor(...args: Array<any>) {
11+
super(...args);
12+
13+
const open = (this as any).open;
14+
const send = (this as any).send;
15+
16+
let _async: boolean = true;
17+
let _url: null | string = null;
18+
let _mime: string = "text/xml";
19+
20+
function reset() {
21+
_async = true;
22+
_url = null;
23+
_mime = "text/xml";
24+
}
25+
26+
(this as any).open = function (method: string, url: string, async: boolean, user?: string, password?: string) {
27+
// Special behavior for synchronous requests
28+
if (method === "GET" && !async && !user && !password) {
29+
_async = false;
30+
_url = url;
31+
32+
// Default to the normal polyfill for async requests
33+
} else {
34+
reset();
35+
return open.call(this, method, url, async, user, password);
36+
}
37+
};
38+
39+
(this as any).send = function (data: any) {
40+
if (_async) {
41+
return send.call(this, data);
42+
43+
// Use `sync-request` for synchronous requests.
44+
} else {
45+
const response = $request("GET", _url!, {
46+
headers: {
47+
"Content-Type": _mime,
48+
}
49+
});
50+
51+
const buffer = (response.body as Buffer).buffer as any;
52+
53+
const responseText = new TextDecoder("iso-8859-5", { fatal: true }).decode(buffer);
54+
55+
(this as any).status = 200;
56+
(this as any).response = (this as any).responseText = responseText;
57+
58+
reset();
59+
}
60+
};
61+
62+
(this as any).overrideMimeType = function (mime: string) {
63+
_mime = mime;
64+
};
65+
}
66+
} as any;
667
}

0 commit comments

Comments
 (0)