Skip to content

Commit

Permalink
feat: allow to provide a list of transport implementations
Browse files Browse the repository at this point in the history
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
  • Loading branch information
darrachequesne committed May 31, 2024
1 parent 579b243 commit f4d898e
Show file tree
Hide file tree
Showing 23 changed files with 538 additions and 335 deletions.
9 changes: 0 additions & 9 deletions lib/globalThis.browser.ts

This file was deleted.

1 change: 0 additions & 1 deletion lib/globalThis.ts

This file was deleted.

6 changes: 3 additions & 3 deletions lib/transports/xmlhttprequest.ts → lib/globals.node.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as XMLHttpRequestModule from "xmlhttprequest-ssl";

export const XHR = XMLHttpRequestModule.default || XMLHttpRequestModule;
export const nextTick = process.nextTick;
export const globalThisShim = global;
export const defaultBinaryType = "nodebuffer";

export function createCookieJar() {
return new CookieJar();
Expand Down
16 changes: 12 additions & 4 deletions ...ansports/websocket-constructor.browser.ts → lib/globals.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { globalThisShim as globalThis } from "../globalThis.js";

export const nextTick = (() => {
const isPromiseAvailable =
typeof Promise === "function" && typeof Promise.resolve === "function";
Expand All @@ -10,6 +8,16 @@ export const nextTick = (() => {
}
})();

export const WebSocket = globalThis.WebSocket || globalThis.MozWebSocket;
export const usingBrowserWebSocket = true;
export const globalThisShim = (() => {
if (typeof self !== "undefined") {
return self;
} else if (typeof window !== "undefined") {
return window;
} else {
return Function("return this")();
}
})();

export const defaultBinaryType = "arraybuffer";

export function createCookieJar() {}
12 changes: 10 additions & 2 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
import { Socket } from "./socket.js";

export { Socket };
export { SocketOptions } from "./socket.js";
export {
SocketOptions,
SocketWithoutUpgrade,
SocketWithUpgrade,
} from "./socket.js";
export const protocol = Socket.protocol;
export { Transport, TransportError } from "./transport.js";
export { transports } from "./transports/index.js";
export { installTimerFunctions } from "./util.js";
export { parse } from "./contrib/parseuri.js";
export { nextTick } from "./transports/websocket-constructor.js";
export { nextTick } from "./globals.node.js";

export { Fetch } from "./transports/polling-fetch.js";
export { XHR as NodeXHR } from "./transports/polling-xhr.node.js";
export { XHR } from "./transports/polling-xhr.js";
export { WS as NodeWebSocket } from "./transports/websocket.node.js";
export { WS as WebSocket } from "./transports/websocket.js";
export { WT as WebTransport } from "./transports/webtransport.js";
Loading

0 comments on commit f4d898e

Please sign in to comment.