Commit Graph

692 Commits

Author SHA1 Message Date
Damien Arrachequesne
1eaeeece35 refactor: remove unused option
This option was added by error during the TypeScript migration.

Related: 587ccf3380
2022-12-14 06:56:05 +01:00
Damien Arrachequesne
ec5a596680 refactor: improve typings
Note: `readyState` and `writeBuffer` are set to public, as they might
be useful for the end user.
2022-12-14 06:49:23 +01:00
Damien Arrachequesne
528a61f86f refactor: bump prettier to version 2.8.1
This major bump creates a lot of noise, but it is necessary for
prettier to be able to parse new syntax such as:

- typed imports: `import { type xxx } from ...`
- private attributes: `class A { #b; #c() {} }`
2022-12-13 15:36:28 +01:00
iifawzi
21a6e1219a feat: add the "addTrailingSlash" option (#694)
The "addTrailingSlash" option allows to control whether a trailing
slash is added to the path of the HTTP requests created by the library:

- true (default): "/engine.io/"
- false: "/engine.io"

Related: https://github.com/socketio/socket.io-client/issues/1550

Signed-off-by: iifawzi <iifawzie@gmail.com>
2022-12-09 08:50:03 +01:00
Damien Arrachequesne
99925a4775 fix: properly clear "beforeunload" event listener
Related: https://github.com/socketio/engine.io-client/issues/693
2022-10-13 12:20:03 +02:00
Damien Arrachequesne
d4d6463ded refactor: export the nextTick helper method
Related: https://github.com/socketio/socket.io-client/issues/1551
2022-10-13 11:13:44 +02:00
Lam Wei Li
dfee8ded72 refactor: replace deprecated String.prototype.substr() (#691)
`.substr()` is deprecated so we replace it with `.slice()` which works
similarly but isn't deprecated.

See also: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substr

Signed-off-by: Lam Wei Li <peteriman@mail.com>
2022-06-15 01:49:18 +02:00
Damien Arrachequesne
ef1c28295e refactor: use named export for XMLHttpRequest shim 2022-05-02 10:00:27 +02:00
Damien Arrachequesne
f158c8e255 fix: simplify the check for WebSocket availability
This check was added for the flashsocket transport, which has been
deprecated for a while now ([1]). But it fails with latest webpack
versions, as the expression `"__initialize" in WebSocket` gets
evaluated to `true`.

Related:

- https://github.com/socketio/engine.io-client/issues/689
- https://github.com/socketio/socket.io-client/issues/1537

[1]: dbc65f3b5a
2022-05-02 09:48:38 +02:00
yujiosaka
32878ea047 fix: use named export for globalThis shim (#688)
Default export of globalThis seems to have a problem in the "browser"
field when the library is loaded asynchronously with webpack.
2022-05-02 09:47:16 +02:00
Damien Arrachequesne
e2ab447d63 refactor: add missing .js extension for ESM usage 2022-04-18 00:43:40 +02:00
Damien Arrachequesne
bc3cefb57b refactor: replace custom clone method by Object.assign() 2022-04-17 23:35:52 +02:00
Damien Arrachequesne
df32277c3f refactor: import single-file 3rd party modules
This commit allows to:

- provide an ESM version of those modules ([1])
- reduce the attack surface in case of supply chain attacks
- reduce the size of the bundle with tree-shaking

As a downside, we won't receive security updates for those modules
anymore.

[1]: https://github.com/socketio/socket.io-client/issues/1536
2022-04-13 08:31:02 +02:00
Damien Arrachequesne
b4b3ed5c68 refactor: merge the polling.ts and polling-xhr.ts files
In the past, there were two implementations of the HTTP long-polling
feature, one with XMLHttpRequest and one based on JSONP for ancient
browsers (IE7/IE8). The JSONP implementation has been removed in [1].

[1]: b2c73812e9
2022-04-11 18:57:43 +02:00
Damien Arrachequesne
b9252e2074 feat: add details to the "close" event
The close event will now include additional details to help debugging
if anything has gone wrong.

Example when a payload is over the maxHttpBufferSize value in HTTP
long-polling mode:

```js
socket.on("close", (reason, details) => {
  console.log(reason); // "transport error"

  // in that case, details is an error object
  console.log(details.message); "xhr post error"
  console.log(details.description); // 413 (the HTTP status of the response)

  // details.context refers to the XMLHttpRequest object
  console.log(details.context.status); // 413
  console.log(details.context.responseText); // ""
});
```

Note: the error object was already included before this commit and is
kept for backward compatibility

Example when a payload is over the maxHttpBufferSize value with
WebSockets:

```js
socket.on("close", (reason, details) => {
  console.log(reason); // "transport close"

  // in that case, details is a plain object
  console.log(details.description); // "websocket connection closed"

  // details.context is a CloseEvent object
  console.log(details.context.code); // 1009 (which means "Message Too Big")
  console.log(details.context.reason); // ""
});
```

Example within a cluster without sticky sessions:

```js
socket.on("close", (reason, details) => {
  console.log(details.context.status); // 400
  console.log(details.context.responseText); // '{"code":1,"message":"Session ID unknown"}'
});
```

Note: we could also print some warnings in development for the "usual"
errors:

- CORS error
- HTTP 400 with multiple nodes
- HTTP 413 with maxHttpBufferSize

but that would require an additional step when going to production
(i.e. setting NODE_ENV variable to "production"). This is open to
discussion!

Related:

- https://github.com/socketio/socket.io/issues/3946
- https://github.com/socketio/socket.io/issues/1979
- https://github.com/socketio/socket.io-client/issues/1518
2022-04-11 17:15:08 +02:00
Damien Arrachequesne
46fdc2f0ed feat: slice write buffer according to the maxPayload value
The server will now include a "maxPayload" field in the handshake
details, allowing the clients to decide how many packets they have to
send to stay under the maxHttpBufferSize value.

Related:

- https://github.com/socketio/socket.io-client/issues/1531
- 088dcb4dff
2022-03-12 11:11:17 +01:00
Damien Arrachequesne
018e1afcc5 fix(typings): allow any value in the query option
Related: https://github.com/socketio/engine.io-client/issues/679
2021-11-05 07:26:32 +01:00
Kaan Gökdemir
8f68f77825 fix(typings): allow port to be a number (#680)
`string` is kept for backward compatibility (and `location.port` is a
string).

Reference: https://developer.mozilla.org/en-US/docs/Web/API/Location/port

Related: https://github.com/socketio/socket.io-client/issues/1444
2021-11-05 07:22:58 +01:00
Damien Arrachequesne
49719142f6 fix: fix usage with vite
It seems vite has issues with absolute dependencies in the "browser"
field, so we'll provide a quick workaround.

Related:

- https://github.com/socketio/socket.io-client/issues/1494
- https://github.com/socketio/socket.io-client/issues/1495
2021-10-14 13:47:29 +02:00
Damien Arrachequesne
b3bb73aac7 chore: bump @socket.io/component-emitter to version 3.0.0
The typed events have been moved from [1] to [2] in order to remove
the intermediary class and reduce the bundle size.

Diff: https://github.com/socketio/emitter/compare/2.0.0...3.0.0

[1]: https://github.com/socketio/socket.io-client
[2]: https://github.com/socketio/emitter/
2021-10-14 13:46:08 +02:00
Damien Arrachequesne
c6561928be refactor: remove XDomainRequest support
This was used in IE8 (but behind a flag).

BREAKING CHANGE: the enableXDR option is removed

Related: https://github.com/socketio/engine.io-client/issues/674
2021-10-08 13:11:19 +02:00
Damien Arrachequesne
b2c73812e9 refactor: remove JSONP polling
JSONP polling was only used in IE7/8, which are not supported anymore.

BREAKING CHANGE: the jsonp and forceJSONP options are removed.
2021-10-08 11:49:15 +02:00
Damien Arrachequesne
27de300de4 chore: migrate to rollup
This change allows us to:

- reduce the size of the bundle
- provide an ESM bundle (for usage in <script type="module">)

BREAKING CHANGE: due to how default export works with ES modules, we
have removed the default export of the library, which means the
following code:

```js
require("engine.io-client")(...);
```

will not work anymore. The named export must be used instead:

```js
const { Socket } = require("engine.io-client);
// or import { Socket } from "engine.io-client";

const socket = new Socket(...);
```

Note: the UMD build still exposes a function though:

```html
<script src="/path/to/engine.io.js"></script>
<script>
  const socket = eio(...);
</script>
```

Note: webpack is still used with zuul because of the custom builder
(zuul-builder-webpack)
2021-10-08 11:46:11 +02:00
Damien Arrachequesne
7245b803e0 chore: migrate to TypeScript
This change introduces an ESM build which will allow tree shaking. A
CJS build is also provided for backward compatibility.
2021-10-04 23:12:09 +02:00
Michael Vartan
5d1d5bea11 feat: add an option to use native timer functions (#672)
This allows to control the behavior of mocked timers (@sinonjs/fake-timers),
depending on the value of the "useNativeTimers" option:

- true: use native setTimeout function
- false (default): use classic timers, that may be mocked

The "installTimerFunctions" method will also be used in the
`socket.io-client` package:

```
import { installTimerFunctions } from "engine.io-client/lib/util";
```

Note: we could also have put the method in its own library, but that
sounded a bit overkill

Related: https://github.com/socketio/socket.io-client/pull/1479
2021-07-30 09:11:49 +02:00
Damien Arrachequesne
2de8b4ef97 refactor: remove forked xmlhttprequest-ssl
Our fork of the xmlhttprequest-ssl package, which was created for the
autoUnref feature (see [1]), is not needed anymore as our changes have
been merged upstream (see [2]).

[1]: 65516836b2
[2]: fd05315b41

Related: https://github.com/socketio/engine.io-client/issues/673
2021-07-13 09:10:27 +02:00
Damien Arrachequesne
589d3ad638 fix: emit ping when receiving a ping from the server
The "ping" event was forgotten when reversing the ping-pong mechanism
in [1]. It will now be properly emitted when receiving a ping from the
server.

[1]: 81d7171c6b

Related: https://github.com/socketio/socket.io-client/issues/1475
2021-06-23 00:44:06 +02:00
Xiaoxin Lu
f30a10b7f4 fix(websocket): fix timer blocking writes (#670)
An immediate setTimeout was used to unblock the WebSocket write.
Unfortunately, this setTimeout can be throttled by browsers when the
tab is backgrounded.

This can lead to missed pong responses to server pings, which
eventually leads to disconnection.

Related: https://github.com/socketio/engine.io-client/issues/649
2021-06-22 23:58:27 +02:00
Damien Arrachequesne
bddd9928fc fix: fix JSONP transport on IE9
This fixes the following error:

> Unable to get value of the property 'readyState': object is null or undefined

Which was introduced in c46611ce44
2021-05-11 08:22:36 +02:00
Damien Arrachequesne
c46611ce44 refactor: remove "self" references 2021-05-04 09:28:21 +02:00
Damien Arrachequesne
dcb85e902d feat: add the "closeOnBeforeunload" option
Since [1], the socket is now closed when receiving the "beforeunload"
event in the browser.

This change was meant to fix a discrepancy between Chrome and Firefox
when the user reloads/closes a browser tab: Firefox would close the
connection (and emit a "disconnect" event, at the Socket.IO level), but
not Chrome (see [2]).

But it also closes the connection when there is another "beforeunload"
handler, for example when the user is prompted "are you sure you want
to leave this page?".

Note: calling "stopImmediatePropagation()" was a possible workaround:

```js
window.addEventListener('beforeunload', (event) => {
  event.preventDefault();
  event.stopImmediatePropagation();
  event.returnValue = 'are you sure you want to leave this page?';
});
```

This commit adds a "closeOnBeforeunload" option, which controls whether
a handler is registered for the "beforeunload" event.

Syntax:

```js
const socket = require('engine.io-client')('ws://localhost', {
  closeOnBeforeunload: false // defaults to true
});
```

[1]: ed48b5dc34
[2]: https://github.com/socketio/socket.io/issues/3639

Related:

- https://github.com/socketio/engine.io-client/issues/661
- https://github.com/socketio/engine.io-client/issues/658
- https://github.com/socketio/socket.io-client/issues/1451

Reference: https://developer.mozilla.org/en-US/docs/Web/API/Window/beforeunload_event
2021-05-04 08:45:09 +02:00
Damien Arrachequesne
d291a4c9f6 fix: ignore packets when the transport is silently closed
In some cases, a "Transport not open" error could be thrown when the
transport was silently closed in the onbeforeunload event (added in
[1]).

To reproduce:

```js
window.addEventListener("unload", () => {
  socket.write("...");
});
```

[1]: ed48b5dc34

Related: https://github.com/socketio/socket.io/issues/3838
2021-03-31 23:53:09 +02:00
Damien Arrachequesne
65516836b2 feat: add autoUnref option
With autoUnref set to true (default: false), the Engine.IO client will
allow the program to exit if there is no other active timer/socket in
the event system.

Note: the 'xmlhttprequest-ssl' package has been copied in the contrib/
directory, until the change is merged upstream

Related: https://github.com/socketio/engine.io-client/issues/653
2021-03-03 10:12:40 +01:00
Damien Arrachequesne
c361bc691f feat: listen to the "offline" event
The connection will be closed once the "offline" event is emitted by
the browser, in order not to wait for the heartbeat mechanism to detect
the disconnection.

Reference: https://developer.mozilla.org/en-US/docs/Web/API/Window/offline_event

Related: https://github.com/socketio/socket.io-client/issues/1433
2021-03-02 09:24:21 +01:00
Damien Arrachequesne
ed48b5dc34 fix: silently close the transport in the beforeunload hook
Related:

- https://github.com/socketio/socket.io/issues/3639
- https://github.com/socketio/socket.io/issues/3069
2021-02-25 00:32:18 +01:00
Damien Arrachequesne
d134feeaa6 feat: add missing ws options
Reference: https://github.com/websockets/ws/blob/master/doc/ws.md#new-websocketaddress-protocols-options

Related:

- https://github.com/socketio/engine.io-client/issues/574
- https://github.com/socketio/engine.io-client/issues/615
2021-01-14 01:19:25 +01:00
Damien Arrachequesne
587ccf3380 refactor: remove the policyPort option
This option was used by the flash-websocket transport.
2020-12-07 10:59:43 +01:00
Camilo Rodríguez
c22681542c refactor: avoid redeclaring const in xhr polling transport (#645) 2020-12-03 16:26:41 +01:00
Damien Arrachequesne
1c8cba8818 fix: check the type of the initial packet
Before this fix, the client could mark the polling transport as open
even though the handshake packet was not received properly (for
example, after a parsing error).

This could lead to writes (POST requests) without "sid" query param,
which failed with:

```
{"code":2,"message":"Bad handshake method"}
```

Related:

- https://github.com/socketio/engine.io-client/issues/636
- https://github.com/socketio/socket.io-client/issues/1390
2020-11-17 11:19:07 +01:00
Damien Arrachequesne
4873a237f1 fix: restore the cherry-picking of the WebSocket options
The previous commit ([1]) makes the Engine.IO tests with Node.js SSL
options fail.

[1]: 177b95fe46
2020-11-17 11:15:53 +01:00
Damien Arrachequesne
89cb771cf0 refactor: remove binary handling for the polling transport (bis)
The `supportsBinary` attribute and the `forceBase64` option are kept
untouched, though they are not used by the polling transport, which
now always converts the payloads to base64.

Related: https://github.com/socketio/socket.io-client/issues/1391
2020-11-17 09:19:28 +01:00
Damien Arrachequesne
177b95fe46 fix(react-native): exclude the localAddress option
React Native only supports the "headers" option in the WebSocket constructor.

Related: https://github.com/socketio/socket.io-client/issues/1397
2020-11-16 23:17:21 +01:00
Damien Arrachequesne
ccb99e3718 fix(react-native): add a default value for the withCredentials option
The undefined value breaks React Native on Android with a rather cryptic error message:

```
Attempt to invoke virtual method 'boolean java.lang.Boolean.booleanValue() on a null object reference
```

This bug was introduced by [1].

[1]: 5f47a50ee5

Related: https://github.com/socketio/socket.io-client/issues/1403
2020-11-16 23:17:21 +01:00
Damien Arrachequesne
ec3f677e92 refactor: remove binary handling for the polling transport
Since Engine.IO v4, the binary payloads are always encoded as base64
with the polling transport.

See https://github.com/socketio/engine.io-protocol#difference-between-v3-and-v4

Possibly related: https://github.com/socketio/socket.io-client/issues/1391
2020-11-08 02:12:11 +01:00
Damien Arrachequesne
bc9d6c1669 refactor: fix typo
Closes https://github.com/socketio/socket.io-client/pull/1354
2020-09-30 01:46:50 +02:00
Damien Arrachequesne
8440a57f76 chore: bump engine.io-parser version
See also: https://github.com/socketio/engine.io-protocol#difference-between-v3-and-v4

Release: https://github.com/socketio/engine.io-parser/releases/tag/4.0.0
Diff: https://github.com/socketio/engine.io-parser/compare/2.2.0...4.0.0
2020-09-09 00:02:24 +02:00
Damien Arrachequesne
2f5c948abe fix(react-native): restrict the list of options for the WebSocket object
Only 'headers' and 'localAddress' options are supported by the
WebSocket implementation in React Native.

The following message was printed to the console:

> Unrecognized WebSocket connection option(s) `agent`, `perMessageDeflate`, `pfx`, `key`, `passphrase`, `cert`, `ca`, `ciphers`, `rejectUnauthorized`. Did you mean to put these under `headers`?

Reference: https://reactnative.dev/docs/network.html#websocket-support
2020-05-25 07:34:17 +02:00
Damien Arrachequesne
3f3a6f9914 fix: use globalThis polyfill instead of self/global (#634)
In order to fix the "self is not defined" issues in Android 8 and React
Native.

Fixes https://github.com/socketio/engine.io-client/issues/611, https://github.com/socketio/engine.io-client/issues/626
Closes https://github.com/socketio/engine.io-client/pull/627
2020-04-16 16:08:37 +02:00
Chris West (Faux)
27fa6949f3 refactor: remove indexof dependency
This is a polyfill for indexOf(), which we use literally everywhere else.
2020-04-16 10:06:44 +02:00
Damien Arrachequesne
7c7f1a9fe2 fix: properly assign options when creating the transport
The query attribute was overwritten, due to the order of arguments in
the assignment. The bug was introduced in the refactor (5f47a50).
2020-02-12 08:14:51 +01:00