diff --git a/lib/transports/polling-xhr.ts b/lib/transports/polling-xhr.ts index 50c2b7a99..92eba06f7 100755 --- a/lib/transports/polling-xhr.ts +++ b/lib/transports/polling-xhr.ts @@ -1,6 +1,6 @@ /* global attachEvent */ -import * as XMLHttpRequestModule from "xmlhttprequest-ssl"; +import XMLHttpRequest from "./xmlhttprequest.js"; import debugModule from "debug"; // debug() import globalThis from "../globalThis.js"; import { installTimerFunctions, pick } from "../util.js"; @@ -10,8 +10,6 @@ import { SocketOptions } from "../socket.js"; const debug = debugModule("engine.io-client:polling-xhr"); // debug() -const XMLHttpRequest = XMLHttpRequestModule.default || XMLHttpRequestModule; - /** * Empty function */ diff --git a/lib/transports/xmlhttprequest.browser.ts b/lib/transports/xmlhttprequest.browser.ts new file mode 100644 index 000000000..6da802c72 --- /dev/null +++ b/lib/transports/xmlhttprequest.browser.ts @@ -0,0 +1,23 @@ +// browser shim for xmlhttprequest module + +import hasCORS from "has-cors"; +import globalThis from "../globalThis.js"; + +export default function(opts) { + const xdomain = opts.xdomain; + + // XMLHttpRequest can be disabled on IE + try { + if ("undefined" !== typeof XMLHttpRequest && (!xdomain || hasCORS)) { + return new XMLHttpRequest(); + } + } catch (e) {} + + if (!xdomain) { + try { + return new globalThis[["Active"].concat("Object").join("X")]( + "Microsoft.XMLHTTP" + ); + } catch (e) {} + } +} diff --git a/lib/transports/xmlhttprequest.ts b/lib/transports/xmlhttprequest.ts new file mode 100644 index 000000000..d26dee659 --- /dev/null +++ b/lib/transports/xmlhttprequest.ts @@ -0,0 +1,5 @@ +import * as XMLHttpRequestModule from "xmlhttprequest-ssl"; + +const XMLHttpRequest = XMLHttpRequestModule.default || XMLHttpRequestModule; + +export default XMLHttpRequest; diff --git a/support/package.cjs.json b/support/package.cjs.json index 50a4d3547..65b52aa11 100644 --- a/support/package.cjs.json +++ b/support/package.cjs.json @@ -2,7 +2,7 @@ "type": "commonjs", "browser": { "ws": false, - "xmlhttprequest-ssl": "./xmlhttprequest.js", + "./transports/xmlhttprequest.js": "./transports/xmlhttprequest.browser.js", "./transports/websocket-constructor.js": "./transports/websocket-constructor.browser.js", "./globalThis.js": "./globalThis.browser.js" } diff --git a/support/package.esm.json b/support/package.esm.json index 30a1e8999..5c5493883 100644 --- a/support/package.esm.json +++ b/support/package.esm.json @@ -2,7 +2,7 @@ "type": "module", "browser": { "ws": false, - "xmlhttprequest-ssl": "./xmlhttprequest.js", + "./transports/xmlhttprequest.js": "./transports/xmlhttprequest.browser.js", "./transports/websocket-constructor.js": "./transports/websocket-constructor.browser.js", "./globalThis.js": "./globalThis.browser.js" }