Commit Graph

676 Commits

Author SHA1 Message Date
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
Damien Arrachequesne
5f47a50ee5 refactor: refactor the handling of the options 2020-02-11 23:33:34 +01:00
Damien Arrachequesne
81d7171c6b feat: reverse the ping-pong mechanism
The ping packets will now be sent by the server, because the timers set
in the browsers are not reliable enough. We suspect that a lot of
timeout problems came from timers being delayed on the client-side.

Breaking change: v3.x clients will not be able to connect anymore (they
will send a ping packet and timeout while waiting for a pong packet).

Related: https://github.com/socketio/engine.io/issues/312
2020-02-04 12:29:37 +01:00
Damien Arrachequesne
08aff9487f refactor: use prettier to format code 2020-01-14 22:58:35 +01:00
Damien Arrachequesne
0fcc5417ac refactor: migrate to ES6 syntax 2020-01-10 07:29:26 +01:00
Damien Arrachequesne
11dc4f3a56 chore: migrate to webpack 4
Breaking change: the output bundle will now be found in the dist/ folder.
2020-01-10 07:27:02 +01:00
Christopher
fd7fff252d [fix] Ensure valid status is passed to error handler (#622)
Closes https://github.com/socketio/engine.io-client/issues/621
2019-09-13 11:59:51 +02:00
Nubami SQReder
226626921d [feat] Support extra charset info in Content-Type header (#619)
XHR Polling failed to make handshake with server that adds charset info into Content-Type header.

Closes https://github.com/socketio/engine.io-client/issues/618
2019-09-13 11:58:24 +02:00
Jake Barnes
2847411dd0 [feat] Add withCredentials option (#614)
withCredentials was always set to true, despite the browser default being false, and can now be overridden.

Closes https://github.com/socketio/engine.io-client/issues/495
2019-09-13 11:56:36 +02:00
Szymon Lesisz
0eeaa7a927 [fix] fix NodeWebSocket declaration (#613)
NodeWebSocket was be required ONLY if global variables self and WebSocket were not defined.

Closes https://github.com/socketio/engine.io-client/issues/609
2019-09-13 11:53:59 +02:00
Ben James
1ec53d424d [fix] Fix Websocket implementation in React Native (#607)
As `self` is undefined in React Native.
2019-01-20 22:34:30 +01:00
Damien Arrachequesne
66b9e4a186 [fix] fix JSONP transport in Node.js environment
The commit 99bcc62 resulted in `window is not defined` errors.
2018-11-19 22:12:20 +01:00
Nubami SQReder
f62fca4b05 [fix] Trigger xhr polling transport unload handler on 'pagehide' or 'unload' instead of 'beforeunload' (#604)
...to prevent broke connection on cancelled beforeunload event

Fixes #603
2018-11-19 13:11:45 +01:00
Damien Arrachequesne
99bcc622cb [fix] Remove any reference to the global variable
Related: https://github.com/socketio/socket.io-client/issues/1166
2018-11-07 22:40:39 +01:00
Craig Thayer
26e9329050 [feat] Detect React-Native environment and use all websocket features (#591)
React-Native provides a Websocket object that is more functionally aligned with the Node.js websocket than the browser websocket.

It has the same constructor signature as the Node.js websocket and can support extraHeaders and protocols.

This PR will detect when the engine.io-client is running in React-Native, call the proper Websocket constructor, and enable support for extraHeaders.
2018-11-02 08:09:02 +01:00
Thomas Hunkapiller
4349b648d4 [fix] Fix UTF-8 encoding in Firefox WebWorker (#596) 2018-03-09 13:01:02 +01:00
Damien Arrachequesne
8cde767f94 [test] Update travis configuration (#594) 2018-02-28 01:02:49 +01:00