Commit Graph

1740 Commits

Author SHA1 Message Date
Damien Arrachequesne
46213a647e fix: prevent duplicate connections when multiplexing
This bug was introduced in [1]: a multiplexed socket could in some
cases send multiple CONNECT packets, resulting in duplicate connections
on the server side.

A cached socket will now be reopened only if it was inactive, that is,
if one had explicitly called socket.disconnect() before.

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

[1]: b7dd891e89
2023-02-20 17:09:50 +01:00
Damien Arrachequesne
4996f9ee71 fix: do not drain the queue while the socket is offline
In the previous implementation added in [1], the socket would try to
send the packet even if it was disconnected, which would needlessly
exhaust the number of retries.

[1]: 655dce9755
2023-02-20 17:01:22 +01:00
Damien Arrachequesne
5980918b89 chore(release): 4.6.0
Diff: https://github.com/socketio/socket.io-client/compare/4.5.4...4.6.0
2023-02-07 00:26:46 +01:00
Damien Arrachequesne
dff34a3f17 chore: bump engine.io-client to version 6.4.0
The minor bump is due to changes on the server side, but we'll keep the
versions in sync.

Diff: https://github.com/socketio/engine.io-client/compare/6.3.1...6.4.0
Release notes: https://github.com/socketio/engine.io-client/releases/tag/6.4.0
2023-02-06 17:23:45 +01:00
Damien Arrachequesne
4d6d95e079 fix(typings): do not expose browser-specific types
Related:

- https://github.com/socketio/socket.io-client/issues/1561
- b862924b7f
- 37d7a0aa79
2023-02-04 08:21:59 +01:00
Damien Arrachequesne
c54e09d092 test: add more tests for the retry mechanism 2023-02-04 08:04:38 +01:00
Damien Arrachequesne
0110e46842 chore: bump engine.io-client to version 6.3.1
Diff: https://github.com/socketio/engine.io-client/compare/6.3.0...6.3.1
Release notes: https://github.com/socketio/engine.io-client/releases/tag/6.3.1
2023-02-04 07:29:54 +01:00
Damien Arrachequesne
5ba0d498dc refactor: minor edit
Following 655dce9755
2023-02-03 08:27:15 +01:00
Damien Arrachequesne
b1eed5fd75 chore: bump engine.io-client to version 6.3.0
Diff: https://github.com/socketio/engine.io-client/compare/6.2.3...6.3.0
Release notes: https://github.com/socketio/engine.io-client/releases/tag/6.3.0
2023-02-03 08:20:45 +01:00
Damien Arrachequesne
b7dd891e89 fix: ensure manager.socket() returns an active socket
Related: https://github.com/socketio/socket.io-client/issues/1460
2023-02-03 08:16:38 +01:00
Damien Arrachequesne
655dce9755 feat: implement retry mechanism
Syntax:

```js
const socket = io({
  retries: 3,
  ackTimeout: 10000
});

// "my-event" will be sent up to 4 times (1 + 3), until the server sends an acknowledgement
socket.emit("my-event", (err) => {});
```

Notes:

- the order of the packets is guaranteed, as we send packets one by one
- the same packet id is reused for consecutive retries, in order to
allow deduplication on the server side
2023-02-01 08:19:34 +01:00
Damien Arrachequesne
9f3292525b test: add test with onAnyOutgoing() and binary attachments 2023-01-30 08:26:56 +01:00
Damien Arrachequesne
f27cba5b33 refactor: add recovered flag after a successful recovery
Following b4e20c5c70
2023-01-30 08:14:45 +01:00
Damien Arrachequesne
47b979d573 feat: add promise-based acknowledgements
This commit adds some syntactic sugar around acknowledgements:

```js
// without timeout
const response = await socket.emitWithAck("hello", "world");

// with a specific timeout
try {
  const response = await socket.timeout(1000).emitWithAck("hello", "world");
} catch (err) {
  // the server did not acknowledge the event in the given delay
}
```

Note: enviroments that do not support Promises ([1]) will need to add a
polyfill in order to use this feature

See also: 184f3cf7af

[1]: https://caniuse.com/promises
2023-01-30 08:08:06 +01:00
Damien Arrachequesne
b4e20c5c70 feat: implement connection state recovery
Connection state recovery allows a client to reconnect after a
temporary disconnection and restore its state:

- id
- rooms
- data
- missed packets

See also: 54d5ee05a6
2023-01-25 09:21:08 +01:00
Damien Arrachequesne
a1c528b089 fix(typings): properly type emits with timeout (2)
This follows [1], in order to keep the label of each argument.

[1]: 33e417258c

Related:

- https://github.com/socketio/socket.io-client/pull/1570#issuecomment-1384075633
- https://github.com/microsoft/TypeScript/issues/39941
- https://github.com/microsoft/TypeScript/issues/48049
2023-01-17 15:56:37 +01:00
Engin Aydogan
33e417258c fix(typings): properly type emits with timeout (#1570)
When emitting with a timeout (added in version 4.4.0), the "err"
argument was not properly typed and would require to split the client
and server typings. It will now be automatically inferred as an Error
object.

Workaround for previous versions:

```ts
type WithTimeoutAck<isEmitter extends boolean, args extends any[]> = isEmitter extends true ? [Error, ...args] : args;

interface ClientToServerEvents<isEmitter extends boolean = false> {
    withAck: (data: { argName: boolean }, callback: (...args: WithTimeoutAck<isEmitter, [string]>) => void) => void;
}

interface ServerToClientEvents<isEmitter extends boolean = false> {

}

const io = new Server<ClientToServerEvents, ServerToClientEvents<true>>(3000);

io.on("connection", (socket) => {
    socket.on("withAck", (val, cb) => {
        cb("123");
    });
});

const socket: Socket<ServerToClientEvents, ClientToServerEvents<true>> = ioc("http://localhost:3000");

socket.timeout(100).emit("withAck", { argName: true }, (err, val) => {
  // ...
});
```

Related: https://github.com/socketio/socket.io-client/issues/1555
2023-01-16 12:31:01 +01:00
Damien Arrachequesne
18c6e0a6da chore: add security policy 2022-12-14 07:46:55 +01:00
Damien Arrachequesne
91ef8395d5 chore(release): 4.5.4
Diff: https://github.com/socketio/socket.io-client/compare/4.5.3...4.5.4
2022-11-22 22:28:31 +01:00
Damien Arrachequesne
d882822908 ci: migrate from zuul to webdriver.io
zuul is now archived [1] and does not support the new W3C WebDriver
protocol, since it relies on the wd package [2] under the hood, which
uses the (now deprecated) JSON Wire Protocol.

We will now use the webdriver.io test framework, which allows to run
our tests in local and on Sauce Labs (cross-browser and mobile tests).
This allows us to run our tests on latest versions of Android and iOS,
since Sauce Labs only supports the W3C WebDriver protocol for these
platforms ([3]).

[1]: https://github.com/defunctzombie/zuul
[2]: https://github.com/admc/wd
[3]: https://docs.saucelabs.com/dev/w3c-webdriver-capabilities/
2022-11-17 09:51:12 +01:00
Dirk Stolle
e8912891b2 ci: update actions in GitHub Actions workflows (#1564) 2022-11-15 11:34:22 +01:00
Annniee1
cedd3117cf chore: bump socket.io-parser to version 4.2.1 (#1559) 2022-11-05 23:05:44 +01:00
Damien Arrachequesne
9ec85f7da1 docs: add missing versions in the changelog (2) 2022-11-02 08:15:35 +01:00
Damien Arrachequesne
f9b773ffa0 docs: add missing versions in the changelog 2022-11-02 07:48:35 +01:00
Damien Arrachequesne
2eca8dad28 chore(release): 4.5.3
Diff: https://github.com/socketio/socket.io-client/compare/4.5.2...4.5.3
2022-10-15 07:04:22 +02:00
Damien Arrachequesne
7c056889ee docs: add jsdoc for each public method 2022-10-15 06:54:56 +02:00
Damien Arrachequesne
2403b88057 fix: do not swallow user exceptions
Following [1], any exception in a user-provided event listener would
get caught in the try...catch of the decoder and result in the
reconnection of the socket.

[1]: c597023169

Related:

- https://github.com/socketio/socket.io-client/issues/1551
- https://github.com/socketio/socket.io-client/issues/1554
- https://github.com/socketio/socket.io-client/issues/1557
2022-10-13 16:26:35 +02:00
Damien Arrachequesne
1098618a8c ci: temporarily remove iOS 16 from the test matrix
Tests iPhone and iPad 16 are currently failing.

Saucelabs error message:

> An unknown server-side error occurred while processing the command. Original error: W3C capabilities should be provided

It might be time to migrate from zuul (https://github.com/defunctzombie/zuul).
2022-10-13 10:19:49 +02:00
Damien Arrachequesne
2d70813729 chore(release): 4.5.2
Diff: https://github.com/socketio/socket.io-client/compare/4.5.1...4.5.2
2022-09-02 23:36:55 +01:00
Damien Arrachequesne
c597023169 fix: handle ill-formatted packet from server
The decoder can throw an error when trying to decode an invalid payload
sent by the server, so the manager will now catch it, close the
connection and then reconnect instead of crashing.

Related:

- https://github.com/socketio/socket.io/issues/4392
- https://github.com/socketio/socket.io-client/issues/1551
2022-09-02 23:28:35 +01:00
Damien Arrachequesne
8c659bcccf chore: regenerate lockfile
For some reason, the lockfile was not in sync anymore with the
package.json file:

> `npm ci` can only install packages when your package.json and package-lock.json or npm-shrinkwrap.json are in sync.

That may be linked to a new version of Node.js (v16.15.1).
2022-06-26 08:19:46 +02:00
Damien Arrachequesne
f0350a0100 chore(release): 4.5.1
Diff: https://github.com/socketio/socket.io-client/compare/4.5.0...4.5.1
2022-05-17 23:34:19 +02:00
Damien Arrachequesne
abdba07030 chore(release): 4.5.0
Diff: https://github.com/socketio/socket.io-client/compare/4.4.1...4.5.0
2022-04-24 00:37:51 +02:00
Damien Arrachequesne
faf68a5de1 chore: update default label for bug reports 2022-04-24 00:18:42 +02:00
Damien Arrachequesne
c0ba7344e2 chore: add Node.js 16 in the test matrix
See also: https://github.com/nodejs/Release
2022-04-24 00:17:23 +02:00
Damien Arrachequesne
e8590188ec refactor: replace the disconnected attribute by a getter 2022-04-24 00:13:56 +02:00
Damien Arrachequesne
74e3e601a4 feat: add support for catch-all listeners for outgoing packets
This is similar to `onAny()`, but for outgoing packets.

Syntax:

```js
socket.onAnyOutgoing((event, ...args) => {
  console.log(event);
});
```

Related: 531104d332
2022-04-23 23:57:03 +02:00
Damien Arrachequesne
692d54e9fd chore: point the CI badge towards the main branch 2022-04-23 22:59:48 +02:00
Damien Arrachequesne
6fdf3c9bfa 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]: socketio/socket.io-client#1536

Related: df32277c3f
2022-04-23 22:55:39 +02:00
Damien Arrachequesne
b862924b7f feat: add details to the disconnect event
The "disconnect" 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("disconnect", (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); // ""
});
```

Related: b9252e2074
2022-04-23 00:57:23 +02:00
Damien Arrachequesne
eaf782c41b docs: remove broken badges
Related: https://github.com/socketio/socket.io/issues/4242
2022-01-10 08:05:04 +01:00
Damien Arrachequesne
359d1e25e7 chore(release): 4.4.1
Diff: https://github.com/socketio/socket.io-client/compare/4.4.0...4.4.1
2022-01-06 07:24:53 +01:00
Damien Arrachequesne
f56fdd0f09 chore: remove duplicate package.json file
The additional package.json file, which was copied to
build/cjs/package.json, did hide the parent one, leading to several
issues and providing no real feature. The other one, copied to
build/esm/package.json, is needed though, to enforce the module type.

Related:

- https://github.com/socketio/socket.io/issues/4194
- https://github.com/socketio/socket.io-client/issues/1513
2022-01-04 09:57:43 +01:00
Gray Zhang
19836d9abf chore: add types to exports field to be compatible with nodenext module resolution (#1522)
See [1] for detail, in `nodenext` module resolution it requires a
`types` field in `exports` with full filename including extension.

[1]: https://github.com/microsoft/TypeScript/issues/46770#issuecomment-966612103

Reference: https://www.typescriptlang.org/tsconfig/#module
2021-12-28 10:22:45 +01:00
Damien Arrachequesne
71e34a3826 chore(release): 4.4.0
Diff: https://github.com/socketio/socket.io-client/compare/4.3.2...4.4.0
2021-11-18 13:51:36 +01:00
Damien Arrachequesne
1e1952b817 chore: bump engine.io-client version
Diff: https://github.com/socketio/engine.io-client/compare/6.0.1...6.1.1
2021-11-18 13:47:41 +01:00
Damien Arrachequesne
522ffbe7a8 fix: prevent double ack with timeout
The ack was not properly removed upon timeout, and could be called
twice.

Related: ccf7998cc5
2021-11-18 13:40:46 +01:00
Damien Arrachequesne
99c2cb8421 fix: fix socket.disconnect().connect() usage
Previously, calling `socket.disconnect().connect()` could, if the
connection was upgraded to WebSocket, result in "disconnect" being
emitted twice, and an engine being leaked.

Here's what happened:

> socket.disconnect()

- calls `socket.destroy()` so the socket doesn't listen to the manager events anymore
- then calls `manager._close()` which closes the underlying engine but not the manager itself (it waits for the "close" event of the engine)

> socket.connect()

- calls `socket.subEvents()` so the socket does listen to the manager events
- calls `manager.open()` which creates a new engine

And then the first engine emits a "close" event, which is forwarded to
the socket, hence the second "disconnect" event.

Related: https://github.com/socketio/socket.io-client/issues/1014
2021-11-18 13:39:40 +01:00
Damien Arrachequesne
53d8fcafab fix: add package name in nested package.json
Note: the version must be kept in sync when publishing a new release

Related: socketio/socket.io-client#1513
2021-11-16 19:59:59 +01:00
Damien Arrachequesne
d54d12ce63 fix: prevent socket from reconnecting after middleware failure
Related: https://github.com/socketio/socket.io/discussions/4150
2021-11-16 19:57:47 +01:00