Skip to content

Commit

Permalink
fix: fix usage with vite
Browse files Browse the repository at this point in the history
It seems vite has issues with absolute dependencies in the "browser"
field, so we'll provide a quick workaround.

Related:

- socketio/socket.io-client#1494
- socketio/socket.io-client#1495
  • Loading branch information
darrachequesne committed Oct 14, 2021
1 parent b3bb73a commit 4971914
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 5 deletions.
4 changes: 1 addition & 3 deletions lib/transports/polling-xhr.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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
*/
Expand Down
23 changes: 23 additions & 0 deletions lib/transports/xmlhttprequest.browser.ts
Original file line number Diff line number Diff line change
@@ -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) {}
}
}
5 changes: 5 additions & 0 deletions lib/transports/xmlhttprequest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import * as XMLHttpRequestModule from "xmlhttprequest-ssl";

const XMLHttpRequest = XMLHttpRequestModule.default || XMLHttpRequestModule;

export default XMLHttpRequest;
2 changes: 1 addition & 1 deletion support/package.cjs.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
2 changes: 1 addition & 1 deletion support/package.esm.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down

0 comments on commit 4971914

Please sign in to comment.