Commit Graph

7693 Commits

Author SHA1 Message Date
Marc Jansing
a21ad88828 docs(changelog): add note about maxHttpBufferSize default value (#4596)
Reference: https://github.com/socketio/socket.io/releases/tag/2.5.0
2023-01-18 08:16:27 +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
214182b4f0 chore(release): 5.0.6
Diff: https://github.com/socketio/engine.io-parser/compare/5.0.5...5.0.6
2023-01-16 08:35:56 +01:00
Damien Arrachequesne
54d5ee05a6 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

Usage:

```js
import { Server } from "socket.io";

const io = new Server({
  connectionStateRecovery: {
    // default values
    maxDisconnectionDuration: 2 * 60 * 1000,
    skipMiddlewares: true,
  },
});

io.on("connection", (socket) => {
  console.log(socket.recovered); // whether the state was recovered or not
});
```

Here's how it works:

- the server sends a session ID during the handshake (which is
different from the current `id` attribute, which is public and can be
freely shared)

- the server also includes an offset in each packet (added at the end
of the data array, for backward compatibility)

- upon temporary disconnection, the server stores the client state for
a given delay (implemented at the adapter level)

- upon reconnection, the client sends both the session ID and the last
offset it has processed, and the server tries to restore the state

A few notes:

- the base adapter exposes two additional methods, persistSession() and
restoreSession(), that must be implemented by the other adapters in
order to allow the feature to work within a cluster

See: f5294126a8

- acknowledgements are not affected, because it won't work if the
client reconnects on another server (as the ack id is local)

- any disconnection that lasts longer than the
`maxDisconnectionDuration` value will result in a new session, so users
will still need to care for the state reconciliation between the server
and the client

Related: https://github.com/socketio/socket.io/discussions/4510
2023-01-12 12:21:56 +01:00
Damien Arrachequesne
da2b542797 perf: precompute the WebSocket frames when broadcasting
Note:

- only packets without binary attachments are affected
- the permessage-deflate extension must be disabled (which is the default)

Related:

- 5f7b47d40f
- 5e34722b0b
2023-01-12 08:50:07 +01:00
Damien Arrachequesne
4d6f4541c3 chore(release): 6.3.1
Diff: https://github.com/socketio/engine.io/compare/6.3.0...6.3.1
2023-01-12 08:23:07 +01:00
Damien Arrachequesne
69603b955a refactor: make the compress option optional
The compress option was inadvertently made mandatory in [1].

[1]: 6d87a4065a
2023-01-12 08:15:21 +01:00
Damien Arrachequesne
28f60b880a chore(release): 2.5.2
Diff: https://github.com/socketio/socket.io-adapter/compare/2.5.1...2.5.2
2023-01-12 08:09:40 +01:00
Damien Arrachequesne
ca39942ce0 chore: move ws from peerDependencies to dependencies
In order to prevent issues like [1].

Note: the version matches the one imported by the `engine.io` package

See: https://github.com/socketio/engine.io/blob/6.3.0/package.json#L43

[1]: https://github.com/socketio/socket.io-redis-adapter/issues/478
2023-01-12 08:02:52 +01:00
Tristan F
b7d54dbe8d docs: add Rust client implementation (#4592)
client-only implementation -- it *may* add server-side support in the future.
2023-01-12 06:26:22 +01:00
Tristan F
d4a9b2cdcb refactor(typings): add types for io.engine (#4591)
This adds typings for the socket.io engine field, which offers better
IntelliSense when retrieving the server, as well as more confidence on
the developer-side of what types of fields are entering the server.

Related: https://github.com/socketio/socket.io/issues/4590
2023-01-11 10:45:57 +01:00
Damien Arrachequesne
ae1ea77991 chore(release): 6.3.0
Diff: https://github.com/socketio/engine.io/compare/6.2.1...6.3.0
2023-01-10 17:28:24 +01:00
Damien Arrachequesne
e2b39b63f1 chore(release): 6.3.0
Diff: https://github.com/socketio/engine.io-client/compare/6.2.3...6.3.0
2023-01-10 17:05:58 +01:00
Damien Arrachequesne
a65a047526 fix: wait for all packets to be sent before closing the WebSocket connection
This reverts commit [1], which was included in `engine.io@5.1.0` and
`socket.io@4.1.0`.

The WebSocket connection was closed before all packets were written
out, so for example when calling `socket.disconnect(true)` on the
Socket.IO server (which disconnect from all namespaces and close the
connection), the client would receive only the first disconnect packet
and kept trying to reconnect to the other namespaces.

The only difference with the previous implementation (pre 5.1.0) is
that the "drain" event gets only called once at the end, and not after
each packet.

[1]: ad5306aeae

Related: https://github.com/socketio/engine.io/issues/648
2023-01-10 16:42:12 +01:00
Damien Arrachequesne
ed87609baf fix: fix the ES module wrapper
The package does not have a default export, so importing it from a
project using ES modules would break in some cases.

> Cannot destructure property 'Server' of '_engineIo.default'

Related: https://github.com/socketio/engine.io/issues/657
2023-01-10 15:28:43 +01:00
Damien Arrachequesne
bc98bf1232 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() {} }`
2023-01-10 15:22:57 +01:00
Damien Arrachequesne
33dc073172 docs: add some TODOs for the next major release 2023-01-10 14:57:58 +01:00
iifawzi
d0fd4746af feat: add the "addTrailingSlash" option (#655)
The "addTrailingSlash" option allows to control whether a trailing
slash is added to the path of the HTTP requests:

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

Related: 21a6e1219a

Signed-off-by: iifawzi <iifawzie@gmail.com>
2023-01-10 14:51:31 +01:00
Zong
047f420b86 fix: use explicit context for setTimeout function (#699)
In order to fix "Illegal invocation" errors that happen on some
platforms.
2023-01-10 14:01:21 +01:00
Damien Arrachequesne
5e34722b0b perf: add the wsPreEncodedFrame option
This optimization is only applied if:

- the permessage-deflate extension is disabled (which is the default)
- the "ws" package is used (which is the default)

In that case, the WebSocket frame will only be computed once, when
broadcasting to multiple clients.

Related: 5f7b47d40f
2023-01-09 10:34:25 +01:00
Damien Arrachequesne
12b7d7817e fix: properly parse relative URL with a "@" character
A query parameter with a "@" character could be incorrectly parsed.

Example: "/foo?bar=@example.com" => host: example.com

The parse() method is also used in the `socket.io-client` package, to
extract the namespace and the query parameters.

Notes:

- this bug does not seem exploitable, as an attacker would need to
inject the query parameter in the code executed by the client.

- we might use the URL object in the next major version, but that
means dropping support for some platforms such as IE

Reference: https://caniuse.com/url

Thanks to Li Jiantao of STAR Labs (@starlabs_sg) for the responsible
disclosure.
2023-01-09 09:47:34 +01:00
Damien Arrachequesne
4e3991f740 chore(release): 2.5.1
Diff: https://github.com/socketio/socket.io-adapter/compare/2.5.0...2.5.1
2023-01-06 12:02:23 +01:00
Damien Arrachequesne
99b0f18819 fix: properly precompute the WebSocket frames
The named import is not supported in some cases:

> node_modules/socket.io-adapter/dist/index.js:170
>            packetOpts.wsPreEncodedFrame = ws_1.WebSocket.Sender.frame(data, {
>                                                          ^
>
> TypeError: Cannot read properties of undefined (reading 'Sender')
>     at RedisAdapter._encode (/.../node_modules/socket.io-adapter/dist/index.js:170:59)
>     at RedisAdapter.broadcast (/.../node_modules/socket.io-adapter/dist/index.js:117:37)

Related: https://github.com/socketio/socket.io-redis-adapter/issues/478
2023-01-06 11:58:04 +01:00
Damien Arrachequesne
05ebbad996 ci: temporarily disable browser tests
Until we migrate to webdriver.io.

Related: d882822908
2023-01-06 11:39:30 +01:00
Damien Arrachequesne
ed2b0cbfa3 chore(release): 5.0.5
Diff: https://github.com/socketio/engine.io-parser/compare/5.0.4...5.0.5
2023-01-06 11:38:26 +01:00
Mauricio Narvaez
351ba8245b fix: properly encode empty buffer in base64 encoding (#131)
An empty buffer was encoded into `bundefined` instead of `b`
(reproduced on Chrome v108 / Ubuntu).

Co-authored-by: Mauricio Narvaez <nvz@fb.com>
2023-01-06 11:26:29 +01:00
Damien Arrachequesne
00a8e7589e chore(release): 2.5.0
Diff: https://github.com/socketio/socket.io-adapter/compare/2.4.0...2.5.0
2023-01-06 09:26:10 +01:00
Damien Arrachequesne
5f7b47d40f perf: precompute the WebSocket frames when broadcasting
Note:

- only packets without binary attachments are affected
- the permessage-deflate extension must be disabled (which is the default)

Previous attempt:

- wsPreEncoded option: 5579d40c24
- fix for binary packets: a33e42bb7b
- revert: 88eee5948a
2023-01-06 08:42:51 +01:00
Damien Arrachequesne
6fffc2c3d7 ci: upgrade to actions/checkout@3 and actions/setup-node@3
Reference: https://github.blog/changelog/2022-09-22-github-actions-all-actions-will-begin-running-on-node16-instead-of-node12/
2023-01-05 08:40:16 +01:00
Damien Arrachequesne
f5294126a8 feat: implement connection state recovery
More information about how this feature is supposed to work will be
provided in the main repository.
2023-01-05 08:39:19 +01:00
Damien Arrachequesne
d5c56d4094 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.
2023-01-05 08:21:31 +01:00
Damien Arrachequesne
060745ada3 test: migrate to TypeScript 2023-01-02 11:28:45 +01:00
Damien Arrachequesne
5924e987b6 docs: improve the documentation of the protocol 2022-12-21 00:46:56 +01:00
Damien Arrachequesne
de247df875 docs: improve the documentation of the protocol 2022-12-19 11:00:06 +01:00
dependabot[bot]
8a937a4006 chore(deps): bump qs and express in /examples/latency (#665)
Bumps [qs](https://github.com/ljharb/qs) to 6.11.0 and updates ancestor dependency [express](https://github.com/expressjs/express). These dependencies need to be updated together.


Updates `qs` from 6.7.0 to 6.11.0
- [Release notes](https://github.com/ljharb/qs/releases)
- [Changelog](https://github.com/ljharb/qs/blob/main/CHANGELOG.md)
- [Commits](https://github.com/ljharb/qs/compare/v6.7.0...v6.11.0)

Updates `express` from 4.17.1 to 4.18.2
- [Release notes](https://github.com/expressjs/express/releases)
- [Changelog](https://github.com/expressjs/express/blob/master/History.md)
- [Commits](https://github.com/expressjs/express/compare/4.17.1...4.18.2)

---
updated-dependencies:
- dependency-name: qs
  dependency-type: indirect
- dependency-name: express
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2022-12-14 10:15:52 +01:00
dependabot[bot]
719e275ff7 chore(deps): bump qs from 6.9.4 to 6.11.0 (#664)
Bumps [qs](https://github.com/ljharb/qs) from 6.9.4 to 6.11.0.
- [Release notes](https://github.com/ljharb/qs/releases)
- [Changelog](https://github.com/ljharb/qs/blob/main/CHANGELOG.md)
- [Commits](https://github.com/ljharb/qs/compare/v6.9.4...v6.11.0)

---
updated-dependencies:
- dependency-name: qs
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <support@github.com>

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2022-12-14 10:15:26 +01:00
dependabot[bot]
8f8b217f1c chore(deps): bump engine.io from 4.1.2 to 6.2.1 in /examples/latency (#663)
Bumps [engine.io](https://github.com/socketio/engine.io) from 4.1.2 to 6.2.1.
- [Release notes](https://github.com/socketio/engine.io/releases)
- [Changelog](https://github.com/socketio/engine.io/blob/main/CHANGELOG.md)
- [Commits](https://github.com/socketio/engine.io/compare/4.1.2...6.2.1)

---
updated-dependencies:
- dependency-name: engine.io
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2022-12-14 10:15:12 +01:00
Damien Arrachequesne
da45d84c09 chore: bump ws to version 8.11.0
Diff: https://github.com/websockets/ws/compare/8.2.3...8.11.0
2022-12-14 07:58:30 +01:00
Damien Arrachequesne
547c541fb9 chore: add security policy 2022-12-14 07:47:51 +01:00
Damien Arrachequesne
18c6e0a6da chore: add security policy 2022-12-14 07:46:55 +01:00
Damien Arrachequesne
bd74e7c988 chore: add security policy 2022-12-14 07:42:42 +01:00
Damien Arrachequesne
ed6d016a52 chore: add security policy 2022-12-14 07:28:15 +01:00
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
2ddcb825ae chore: bump caniuse-lite version 2022-12-13 15:40:56 +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
Damien Arrachequesne
721837c2c9 ci: update the versions of actions/checkout and actions/setup-node
Reference: https://github.blog/changelog/2022-09-22-github-actions-all-actions-will-begin-running-on-node16-instead-of-node12/
2022-12-13 15:33:39 +01:00
Damien Arrachequesne
6565c8e956 ci: temporarily disable browser tests
The tests are in failure since the 13th November, without any logs from
Saucelabs, so we temporarily disable them until further investigation.

See: https://github.com/socketio/engine.io-client/actions/runs/3453198221
2022-12-13 15:29:20 +01:00
Damien Arrachequesne
824ab4cdd8 chore: bump ws to version 8.11.0
Diff: https://github.com/websockets/ws/compare/8.2.3...8.11.0
2022-12-09 09:36:46 +01:00