Skip to content

Commit f4d898e

Browse files
feat: allow to provide a list of transport implementations
This commit adds the ability to provide a list of transport implementations to use when connecting to an Engine.IO server. This can be used to use HTTP long-polling based on `fetch()`, instead of the default implementation based on the `XMLHttpRequest` object. ``` import { Socket, Fetch, WebSocket } from "engine.io-client"; const socket = new Socket({ transports: [Fetch, WebSocket] }); ``` This is useful in some environments that do not provide a `XMLHttpRequest` object, like Chrome extension background scripts. > XMLHttpRequest() can't be called from a service worker, extension or otherwise. Replace calls from your background script to XMLHttpRequest() with calls to global fetch(). Source: https://developer.chrome.com/docs/extensions/develop/migrate/to-service-workers#replace-xmlhttprequest Related: - #716 - socketio/socket.io#4980 This is also useful when running the client with Deno or Bun, as it allows to use the built-in `fetch()` method and `WebSocket` object, instead of using the `xmlhttprequest-ssl` and `ws` Node.js packages. Related: socketio/socket.io-deno#12 This feature also comes with the ability to exclude the code related to unused transports (a.k.a. "tree-shaking"): ```js import { SocketWithoutUpgrade, WebSocket } from "engine.io-client"; const socket = new SocketWithoutUpgrade({ transports: [WebSocket] }); ``` In that case, the code related to HTTP long-polling and WebTransport will be excluded from the final bundle. Related: socketio/socket.io#4393
1 parent 579b243 commit f4d898e

23 files changed

+538
-335
lines changed

lib/globalThis.browser.ts

-9
This file was deleted.

lib/globalThis.ts

-1
This file was deleted.

lib/transports/xmlhttprequest.ts lib/globals.node.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import * as XMLHttpRequestModule from "xmlhttprequest-ssl";
2-
3-
export const XHR = XMLHttpRequestModule.default || XMLHttpRequestModule;
1+
export const nextTick = process.nextTick;
2+
export const globalThisShim = global;
3+
export const defaultBinaryType = "nodebuffer";
44

55
export function createCookieJar() {
66
return new CookieJar();
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { globalThisShim as globalThis } from "../globalThis.js";
2-
31
export const nextTick = (() => {
42
const isPromiseAvailable =
53
typeof Promise === "function" && typeof Promise.resolve === "function";
@@ -10,6 +8,16 @@ export const nextTick = (() => {
108
}
119
})();
1210

13-
export const WebSocket = globalThis.WebSocket || globalThis.MozWebSocket;
14-
export const usingBrowserWebSocket = true;
11+
export const globalThisShim = (() => {
12+
if (typeof self !== "undefined") {
13+
return self;
14+
} else if (typeof window !== "undefined") {
15+
return window;
16+
} else {
17+
return Function("return this")();
18+
}
19+
})();
20+
1521
export const defaultBinaryType = "arraybuffer";
22+
23+
export function createCookieJar() {}

lib/index.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
import { Socket } from "./socket.js";
22

33
export { Socket };
4-
export { SocketOptions } from "./socket.js";
4+
export {
5+
SocketOptions,
6+
SocketWithoutUpgrade,
7+
SocketWithUpgrade,
8+
} from "./socket.js";
59
export const protocol = Socket.protocol;
610
export { Transport, TransportError } from "./transport.js";
711
export { transports } from "./transports/index.js";
812
export { installTimerFunctions } from "./util.js";
913
export { parse } from "./contrib/parseuri.js";
10-
export { nextTick } from "./transports/websocket-constructor.js";
14+
export { nextTick } from "./globals.node.js";
1115

1216
export { Fetch } from "./transports/polling-fetch.js";
17+
export { XHR as NodeXHR } from "./transports/polling-xhr.node.js";
1318
export { XHR } from "./transports/polling-xhr.js";
19+
export { WS as NodeWebSocket } from "./transports/websocket.node.js";
20+
export { WS as WebSocket } from "./transports/websocket.js";
21+
export { WT as WebTransport } from "./transports/webtransport.js";

0 commit comments

Comments
 (0)