mirror of
https://github.com/socketio/socket.io.git
synced 2026-01-11 16:08:24 -05:00
Compare commits
18 Commits
4.5.3
...
4.6.0-alph
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0c0eb00163 | ||
|
|
f8640d9451 | ||
|
|
93d446a545 | ||
|
|
184f3cf7af | ||
|
|
5d9220b69a | ||
|
|
129883958a | ||
|
|
6c27b8b0a6 | ||
|
|
f3ada7d8cc | ||
|
|
a21ad88828 | ||
|
|
54d5ee05a6 | ||
|
|
da2b542797 | ||
|
|
b7d54dbe8d | ||
|
|
d4a9b2cdcb | ||
|
|
547c541fb9 | ||
|
|
3b7ced7af7 | ||
|
|
c00bb9564c | ||
|
|
57e5f25e26 | ||
|
|
f4b698418a |
203
CHANGELOG.md
203
CHANGELOG.md
@@ -1,34 +1,71 @@
|
||||
# History
|
||||
|
||||
- [4.5.3](#453-2022-10-15) (2022-10-15)
|
||||
- [4.5.2](#452-2022-09-02) (2022-09-02)
|
||||
- [4.5.1](#451-2022-05-17) (2022-05-17)
|
||||
- [4.5.0](#450-2022-04-23) (2022-04-23)
|
||||
- [4.4.1](#441-2022-01-06) (2022-01-06)
|
||||
- [4.4.0](#440-2021-11-18) (2021-11-18)
|
||||
- [4.3.2](#432-2021-11-08) (2021-11-08)
|
||||
- [4.3.1](#431-2021-10-16) (2021-10-16)
|
||||
- [4.3.0](#430-2021-10-14) (2021-10-14)
|
||||
- [4.2.0](#420-2021-08-30) (2021-08-30)
|
||||
- [4.1.3](#413-2021-07-10) (2021-07-10)
|
||||
- [4.1.2](#412-2021-05-17) (2021-05-17)
|
||||
- [4.1.1](#411-2021-05-11) (2021-05-11)
|
||||
- [4.1.0](#410-2021-05-11) (2021-05-11)
|
||||
- [4.0.2](#402-2021-05-06) (2021-05-06)
|
||||
- [4.0.1](#401-2021-03-31) (2021-03-31)
|
||||
- [4.0.0](#400-2021-03-10) (2021-03-10)
|
||||
- [3.1.2](#312-2021-02-26) (2021-02-26)
|
||||
- [3.1.1](#311-2021-02-03) (2021-02-03)
|
||||
- [3.1.0](#310-2021-01-15) (2021-01-15)
|
||||
- [3.0.5](#305-2021-01-05) (2021-01-05)
|
||||
- [3.0.4](#304-2020-12-07) (2020-12-07)
|
||||
- [3.0.3](#303-2020-11-19) (2020-11-19)
|
||||
- [3.0.2](#302-2020-11-17) (2020-11-17)
|
||||
- [3.0.1](#301-2020-11-09) (2020-11-09)
|
||||
- [3.0.0](#300-2020-11-05) (2020-11-05)
|
||||
## 2022
|
||||
|
||||
- [4.5.4](#454-2022-11-22) (Nov 2022)
|
||||
- [4.5.3](#453-2022-10-15) (Oct 2022)
|
||||
- [4.5.2](#452-2022-09-02) (Sep 2022)
|
||||
- [2.5.0](#250-2022-06-26) (Jun 2022) (from the [2.x](https://github.com/socketio/socket.io/tree/2.x) branch)
|
||||
- [4.5.1](#451-2022-05-17) (May 2022)
|
||||
- [4.5.0](#450-2022-04-23) (Apr 2022)
|
||||
- [4.4.1](#441-2022-01-06) (Jan 2022)
|
||||
|
||||
## 2021
|
||||
|
||||
- [4.4.0](#440-2021-11-18) (Nov 2021)
|
||||
- [4.3.2](#432-2021-11-08) (Nov 2021)
|
||||
- [4.3.1](#431-2021-10-16) (Oct 2021)
|
||||
- [4.3.0](#430-2021-10-14) (Oct 2021)
|
||||
- [4.2.0](#420-2021-08-30) (Aug 2021)
|
||||
- [4.1.3](#413-2021-07-10) (Jul 2021)
|
||||
- [4.1.2](#412-2021-05-17) (May 2021)
|
||||
- [4.1.1](#411-2021-05-11) (May 2021)
|
||||
- [4.1.0](#410-2021-05-11) (May 2021)
|
||||
- [4.0.2](#402-2021-05-06) (May 2021)
|
||||
- [4.0.1](#401-2021-03-31) (Mar 2021)
|
||||
- [**4.0.0**](#400-2021-03-10) (Mar 2021)
|
||||
- [3.1.2](#312-2021-02-26) (Feb 2021)
|
||||
- [3.1.1](#311-2021-02-03) (Feb 2021)
|
||||
- [3.1.0](#310-2021-01-15) (Jan 2021)
|
||||
- [2.4.1](#241-2021-01-07) (Jan 2021) (from the [2.x](https://github.com/socketio/socket.io/tree/2.x) branch)
|
||||
- [3.0.5](#305-2021-01-05) (Jan 2021)
|
||||
- [2.4.0](#240-2021-01-04) (Jan 2021) (from the [2.x](https://github.com/socketio/socket.io/tree/2.x) branch)
|
||||
|
||||
## 2020
|
||||
|
||||
- [3.0.4](#304-2020-12-07) (Dec 2020)
|
||||
- [3.0.3](#303-2020-11-19) (Nov 2020)
|
||||
- [3.0.2](#302-2020-11-17) (Nov 2020)
|
||||
- [3.0.1](#301-2020-11-09) (Nov 2020)
|
||||
- [**3.0.0**](#300-2020-11-05) (Nov 2020)
|
||||
|
||||
## 2019
|
||||
|
||||
- [2.3.0](#230-2019-09-20) (Sep 2019)
|
||||
|
||||
## 2018
|
||||
|
||||
- [2.2.0](#220-2018-11-29) (Nov 2018)
|
||||
- [2.1.1](#211-2018-05-17) (May 2018)
|
||||
- [2.1.0](#210-2018-03-29) (Mar 2018)
|
||||
|
||||
|
||||
# Release notes
|
||||
|
||||
## [4.5.4](https://github.com/socketio/socket.io/compare/4.5.3...4.5.4) (2022-11-22)
|
||||
|
||||
This release contains a bump of:
|
||||
|
||||
- `engine.io` in order to fix [CVE-2022-41940](https://github.com/socketio/engine.io/security/advisories/GHSA-r7qp-cfhv-p84w)
|
||||
- `socket.io-parser` in order to fix [CVE-2022-2421](https://github.com/advisories/GHSA-qm95-pgcg-qqfq).
|
||||
|
||||
### Dependencies
|
||||
|
||||
- [`engine.io@~6.2.1`](https://github.com/socketio/engine.io-client/tree/6.2.1) ([diff](https://github.com/socketio/engine.io/compare/6.2.0...6.2.1))
|
||||
- [`ws@~8.2.3`](https://github.com/websockets/ws/releases/tag/8.2.3)
|
||||
|
||||
|
||||
|
||||
## [4.5.3](https://github.com/socketio/socket.io/compare/4.5.2...4.5.3) (2022-10-15)
|
||||
|
||||
|
||||
@@ -49,6 +86,24 @@
|
||||
|
||||
|
||||
|
||||
# [2.5.0](https://github.com/socketio/socket.io/compare/2.4.1...2.5.0) (2022-06-26)
|
||||
|
||||
⚠️ WARNING ⚠️
|
||||
|
||||
The default value of the maxHttpBufferSize option has been decreased from 100 MB to 1 MB, in order to prevent attacks by denial of service.
|
||||
|
||||
Security advisory: [GHSA-j4f2-536g-r55m](https://github.com/advisories/GHSA-j4f2-536g-r55m)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* fix race condition in dynamic namespaces ([05e1278](https://github.com/socketio/socket.io/commit/05e1278cfa99f3ecf3f8f0531ffe57d850e9a05b))
|
||||
* ignore packet received after disconnection ([22d4bdf](https://github.com/socketio/socket.io/commit/22d4bdf00d1a03885dc0171125faddfaef730066))
|
||||
* only set 'connected' to true after middleware execution ([226cc16](https://github.com/socketio/socket.io/commit/226cc16165f9fe60f16ff4d295fb91c8971cde35))
|
||||
* prevent the socket from joining a room after disconnection ([f223178](https://github.com/socketio/socket.io/commit/f223178eb655a7713303b21a78f9ef9e161d6458))
|
||||
|
||||
|
||||
|
||||
## [4.5.1](https://github.com/socketio/socket.io/compare/4.5.0...4.5.1) (2022-05-17)
|
||||
|
||||
|
||||
@@ -285,6 +340,16 @@ we only add a field in the JSON-encoded handshake data:
|
||||
* allow integers as event names ([1c220dd](https://github.com/socketio/socket.io-parser/commit/1c220ddbf45ea4b44bc8dbf6f9ae245f672ba1b9))
|
||||
|
||||
|
||||
|
||||
## [2.4.1](https://github.com/socketio/socket.io/compare/2.4.0...2.4.1) (2021-01-07)
|
||||
|
||||
|
||||
### Reverts
|
||||
|
||||
* fix(security): do not allow all origins by default ([a169050](https://github.com/socketio/socket.io/commit/a1690509470e9dd5559cec4e60908ca6c23e9ba0))
|
||||
|
||||
|
||||
|
||||
## [3.0.5](https://github.com/socketio/socket.io/compare/3.0.4...3.0.5) (2021-01-05)
|
||||
|
||||
|
||||
@@ -298,6 +363,17 @@ we only add a field in the JSON-encoded handshake data:
|
||||
* restore the socket middleware functionality ([bf54327](https://github.com/socketio/socket.io/commit/bf5432742158e4d5ba2722cff4a614967dffa5b9))
|
||||
|
||||
|
||||
|
||||
# [2.4.0](https://github.com/socketio/socket.io/compare/2.3.0...2.4.0) (2021-01-04)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* **security:** do not allow all origins by default ([f78a575](https://github.com/socketio/socket.io/commit/f78a575f66ab693c3ea96ea88429ddb1a44c86c7))
|
||||
* properly overwrite the query sent in the handshake ([d33a619](https://github.com/socketio/socket.io/commit/d33a619905a4905c153d4fec337c74da5b533a9e))
|
||||
|
||||
|
||||
|
||||
## [3.0.4](https://github.com/socketio/socket.io/compare/3.0.3...3.0.4) (2020-12-07)
|
||||
|
||||
|
||||
@@ -583,3 +659,78 @@ io.of("/admin").use((socket, next) => {
|
||||
|
||||
This method was kept for backward-compatibility with pre-1.0 versions.
|
||||
|
||||
|
||||
|
||||
# [2.3.0](https://github.com/socketio/socket.io/compare/2.2.0...2.3.0) (2019-09-20)
|
||||
|
||||
This release mainly contains a bump of the `engine.io` and `ws` packages, but no additional features.
|
||||
|
||||
|
||||
|
||||
# [2.2.0](https://github.com/socketio/socket.io/compare/2.1.1...2.2.0) (2018-11-29)
|
||||
|
||||
### Features
|
||||
|
||||
- add cache-control header when serving the client source ([#2907](https://github.com/socketio/socket.io/pull/2907)) ([b00ae50](https://github.com/socketio/socket.io/commit/b00ae50be65d1bc88fa95145f1c486a6886a6b76))
|
||||
|
||||
### Bug fixes
|
||||
|
||||
- throw an error when trying to access the clients of a dynamic namespace ([#3355](https://github.com/socketio/socket.io/pull/3355)) ([a7fbd1a](https://github.com/socketio/socket.io/commit/a7fbd1ac4a47cafd832fc62e371754df924c5903))
|
||||
|
||||
|
||||
|
||||
# [2.1.1](https://github.com/socketio/socket.io/compare/2.1.0...2.1.1) (2018-05-17)
|
||||
|
||||
### Features
|
||||
|
||||
- add local flag to the socket object ([#3129](https://github.com/socketio/socket.io/pull/3219)) ([1decae3](https://github.com/socketio/socket.io/commit/1decae341c80c0417b32d3124ca30c005240b48a))
|
||||
|
||||
```js
|
||||
socket.local.to('room101').emit(/* */);
|
||||
```
|
||||
|
||||
|
||||
# [2.1.0](https://github.com/socketio/socket.io/compare/2.1.1...2.2.0) (2018-03-29)
|
||||
|
||||
### Features
|
||||
|
||||
- add a 'binary' flag ([#3185](https://github.com/socketio/socket.io/pull/3185)) ([f48a06c](https://github.com/socketio/socket.io/commit/f48a06c040280b44f90fd225c888910544fd63b5))
|
||||
|
||||
```js
|
||||
// by default, the object is recursively scanned to check whether it contains some binary data
|
||||
// in the following example, the check is skipped in order to improve performance
|
||||
socket.binary(false).emit('plain-object', object);
|
||||
|
||||
// it also works at the namespace level
|
||||
io.binary(false).emit('plain-object', object);
|
||||
```
|
||||
|
||||
- add support for dynamic namespaces ([#3195](https://github.com/socketio/socket.io/pull/3195)) ([c0c79f0](https://github.com/socketio/socket.io/commit/c0c79f019e7138194e438339f8192705957c8ec3))
|
||||
|
||||
```js
|
||||
io.of(/^\/dynamic-\d+$/).on('connect', (socket) => {
|
||||
// socket.nsp.name = '/dynamic-101'
|
||||
});
|
||||
|
||||
// client-side
|
||||
const client = require('socket.io-client')('/dynamic-101');
|
||||
```
|
||||
|
||||
### Bug fixes
|
||||
|
||||
- properly emit 'connect' when using a custom namespace ([#3197](https://github.com/socketio/socket.io/pull/3197)) ([f4fc517](https://github.com/socketio/socket.io/commit/f4fc517e0fe25866c95b584291487b8cbdff889d))
|
||||
- include the protocol in the origins check ([#3198](https://github.com/socketio/socket.io/pull/3198)) ([1f1d64b](https://github.com/socketio/socket.io/commit/1f1d64bab61a273712a199591a3f76210d8c0959))
|
||||
|
||||
### Important note :warning: from Engine.IO [3.2.0 release](https://github.com/socketio/engine.io/releases/tag/3.2.0)
|
||||
|
||||
There are two non-breaking changes that are somehow quite important:
|
||||
|
||||
- `ws` was reverted as the default wsEngine (https://github.com/socketio/engine.io/pull/550), as there was several blocking issues with `uws`. You can still use `uws` by running `npm install uws --save` in your project and using the `wsEngine` option:
|
||||
```js
|
||||
var engine = require('engine.io');
|
||||
var server = engine.listen(3000, {
|
||||
wsEngine: 'uws'
|
||||
});
|
||||
```
|
||||
|
||||
- `pingTimeout` now defaults to 5 seconds (instead of 60 seconds): https://github.com/socketio/engine.io/pull/551
|
||||
|
||||
@@ -21,6 +21,7 @@ Some implementations in other languages are also available:
|
||||
- [Dart](https://github.com/rikulo/socket.io-client-dart)
|
||||
- [Python](https://github.com/miguelgrinberg/python-socketio)
|
||||
- [.NET](https://github.com/doghappy/socket.io-client-csharp)
|
||||
- [Rust](https://github.com/1c3t3a/rust-socketio)
|
||||
|
||||
Its main features are:
|
||||
|
||||
|
||||
22
SECURITY.md
Normal file
22
SECURITY.md
Normal file
@@ -0,0 +1,22 @@
|
||||
# Security Policy
|
||||
|
||||
## Supported Versions
|
||||
|
||||
| Version | Supported |
|
||||
|---------|--------------------|
|
||||
| 4.x | :white_check_mark: |
|
||||
| 3.x | :white_check_mark: |
|
||||
| 2.4.x | :white_check_mark: |
|
||||
| < 2.4.0 | :x: |
|
||||
|
||||
## Reporting a Vulnerability
|
||||
|
||||
To report a security vulnerability in this package, please send an email to [@darrachequesne](https://github.com/darrachequesne) (see address in profile) describing the vulnerability and how to reproduce it.
|
||||
|
||||
We will get back to you as soon as possible and publish a fix if necessary.
|
||||
|
||||
:warning: IMPORTANT :warning: please do not create an issue in this repository, as attackers might take advantage of it. Thank you in advance for your responsible disclosure.
|
||||
|
||||
## History
|
||||
|
||||
No security vulnerability were reported yet.
|
||||
4
client-dist/socket.io.esm.min.js
vendored
4
client-dist/socket.io.esm.min.js
vendored
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -1,5 +1,5 @@
|
||||
/*!
|
||||
* Socket.IO v4.5.3
|
||||
* Socket.IO v4.5.4
|
||||
* (c) 2014-2022 Guillermo Rauch
|
||||
* Released under the MIT License.
|
||||
*/
|
||||
@@ -2679,8 +2679,14 @@
|
||||
function _reconstructPacket(data, buffers) {
|
||||
if (!data) return data;
|
||||
|
||||
if (data && data._placeholder) {
|
||||
return buffers[data.num]; // appropriate buffer (should be natural order anyway)
|
||||
if (data && data._placeholder === true) {
|
||||
var isIndexValid = typeof data.num === "number" && data.num >= 0 && data.num < buffers.length;
|
||||
|
||||
if (isIndexValid) {
|
||||
return buffers[data.num]; // appropriate buffer (should be natural order anyway)
|
||||
} else {
|
||||
throw new Error("illegal attachments");
|
||||
}
|
||||
} else if (Array.isArray(data)) {
|
||||
for (var i = 0; i < data.length; i++) {
|
||||
data[i] = _reconstructPacket(data[i], buffers);
|
||||
@@ -2840,6 +2846,10 @@
|
||||
var packet;
|
||||
|
||||
if (typeof obj === "string") {
|
||||
if (this.reconstructor) {
|
||||
throw new Error("got plaintext data when reconstructing a packet");
|
||||
}
|
||||
|
||||
packet = this.decodeString(obj);
|
||||
|
||||
if (packet.type === PacketType.BINARY_EVENT || packet.type === PacketType.BINARY_ACK) {
|
||||
|
||||
File diff suppressed because one or more lines are too long
4
client-dist/socket.io.min.js
vendored
4
client-dist/socket.io.min.js
vendored
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
2
client-dist/socket.io.msgpack.min.js
vendored
2
client-dist/socket.io.msgpack.min.js
vendored
@@ -1,5 +1,5 @@
|
||||
/*!
|
||||
* Socket.IO v4.5.3
|
||||
* Socket.IO v4.5.4
|
||||
* (c) 2014-2022 Guillermo Rauch
|
||||
* Released under the MIT License.
|
||||
*/
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -7,6 +7,11 @@ import type {
|
||||
EventNames,
|
||||
EventsMap,
|
||||
TypedEventBroadcaster,
|
||||
DecorateAcknowledgements,
|
||||
DecorateAcknowledgementsWithTimeoutAndMultipleResponses,
|
||||
AllButLast,
|
||||
Last,
|
||||
SecondArg,
|
||||
} from "./typed-events";
|
||||
|
||||
export class BroadcastOperator<EmitEvents extends EventsMap, SocketData>
|
||||
@@ -16,7 +21,9 @@ export class BroadcastOperator<EmitEvents extends EventsMap, SocketData>
|
||||
private readonly adapter: Adapter,
|
||||
private readonly rooms: Set<Room> = new Set<Room>(),
|
||||
private readonly exceptRooms: Set<Room> = new Set<Room>(),
|
||||
private readonly flags: BroadcastFlags = {}
|
||||
private readonly flags: BroadcastFlags & {
|
||||
expectSingleResponse?: boolean;
|
||||
} = {}
|
||||
) {}
|
||||
|
||||
/**
|
||||
@@ -169,12 +176,10 @@ export class BroadcastOperator<EmitEvents extends EventsMap, SocketData>
|
||||
*/
|
||||
public timeout(timeout: number) {
|
||||
const flags = Object.assign({}, this.flags, { timeout });
|
||||
return new BroadcastOperator<EmitEvents, SocketData>(
|
||||
this.adapter,
|
||||
this.rooms,
|
||||
this.exceptRooms,
|
||||
flags
|
||||
);
|
||||
return new BroadcastOperator<
|
||||
DecorateAcknowledgementsWithTimeoutAndMultipleResponses<EmitEvents>,
|
||||
SocketData
|
||||
>(this.adapter, this.rooms, this.exceptRooms, flags);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -230,7 +235,10 @@ export class BroadcastOperator<EmitEvents extends EventsMap, SocketData>
|
||||
|
||||
const timer = setTimeout(() => {
|
||||
timedOut = true;
|
||||
ack.apply(this, [new Error("operation has timed out"), responses]);
|
||||
ack.apply(this, [
|
||||
new Error("operation has timed out"),
|
||||
this.flags.expectSingleResponse ? null : responses,
|
||||
]);
|
||||
}, this.flags.timeout);
|
||||
|
||||
let expectedServerCount = -1;
|
||||
@@ -244,7 +252,10 @@ export class BroadcastOperator<EmitEvents extends EventsMap, SocketData>
|
||||
responses.length === expectedClientCount
|
||||
) {
|
||||
clearTimeout(timer);
|
||||
ack.apply(this, [null, responses]);
|
||||
ack.apply(this, [
|
||||
null,
|
||||
this.flags.expectSingleResponse ? null : responses,
|
||||
]);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -276,6 +287,36 @@ export class BroadcastOperator<EmitEvents extends EventsMap, SocketData>
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Emits an event and waits for an acknowledgement from all clients.
|
||||
*
|
||||
* @example
|
||||
* try {
|
||||
* const responses = await io.timeout(1000).emitWithAck("some-event");
|
||||
* console.log(responses); // one response per client
|
||||
* } catch (e) {
|
||||
* // some clients did not acknowledge the event in the given delay
|
||||
* }
|
||||
*
|
||||
* @return a Promise that will be fulfilled when all clients have acknowledged the event
|
||||
*/
|
||||
public emitWithAck<Ev extends EventNames<EmitEvents>>(
|
||||
ev: Ev,
|
||||
...args: AllButLast<EventParams<EmitEvents, Ev>>
|
||||
): Promise<SecondArg<Last<EventParams<EmitEvents, Ev>>>> {
|
||||
return new Promise((resolve, reject) => {
|
||||
args.push((err, responses) => {
|
||||
if (err) {
|
||||
err.responses = responses;
|
||||
return reject(err);
|
||||
} else {
|
||||
return resolve(responses);
|
||||
}
|
||||
});
|
||||
this.emit(ev, ...(args as any[] as EventParams<EmitEvents, Ev>));
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a list of clients.
|
||||
*
|
||||
@@ -444,10 +485,44 @@ export class RemoteSocket<EmitEvents extends EventsMap, SocketData>
|
||||
this.data = details.data;
|
||||
this.operator = new BroadcastOperator<EmitEvents, SocketData>(
|
||||
adapter,
|
||||
new Set([this.id])
|
||||
new Set([this.id]),
|
||||
new Set(),
|
||||
{
|
||||
expectSingleResponse: true, // so that remoteSocket.emit() with acknowledgement behaves like socket.emit()
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a timeout in milliseconds for the next operation.
|
||||
*
|
||||
* @example
|
||||
* const sockets = await io.fetchSockets();
|
||||
*
|
||||
* for (const socket of sockets) {
|
||||
* if (someCondition) {
|
||||
* socket.timeout(1000).emit("some-event", (err) => {
|
||||
* if (err) {
|
||||
* // the client did not acknowledge the event in the given delay
|
||||
* }
|
||||
* });
|
||||
* }
|
||||
* }
|
||||
*
|
||||
* // note: if possible, using a room instead of looping over all sockets is preferable
|
||||
* io.timeout(1000).to(someConditionRoom).emit("some-event", (err, responses) => {
|
||||
* // ...
|
||||
* });
|
||||
*
|
||||
* @param timeout
|
||||
*/
|
||||
public timeout(timeout: number) {
|
||||
return this.operator.timeout(timeout) as BroadcastOperator<
|
||||
DecorateAcknowledgements<EmitEvents>,
|
||||
SocketData
|
||||
>;
|
||||
}
|
||||
|
||||
public emit<Ev extends EventNames<EmitEvents>>(
|
||||
ev: Ev,
|
||||
...args: EventParams<EmitEvents, Ev>
|
||||
|
||||
@@ -114,7 +114,7 @@ export class Client<
|
||||
* @param {Object} auth - the auth parameters
|
||||
* @private
|
||||
*/
|
||||
private connect(name: string, auth: object = {}): void {
|
||||
private connect(name: string, auth: Record<string, unknown> = {}): void {
|
||||
if (this.server._nsps.has(name)) {
|
||||
debug("connecting to namespace %s", name);
|
||||
return this.doConnect(name, auth);
|
||||
@@ -152,10 +152,10 @@ export class Client<
|
||||
*
|
||||
* @private
|
||||
*/
|
||||
private doConnect(name: string, auth: object): void {
|
||||
private doConnect(name: string, auth: Record<string, unknown>): void {
|
||||
const nsp = this.server.of(name);
|
||||
|
||||
const socket = nsp._add(this, auth, () => {
|
||||
nsp._add(this, auth, (socket) => {
|
||||
this.sockets.set(socket.id, socket);
|
||||
this.nsps.set(nsp.name, socket);
|
||||
|
||||
@@ -228,7 +228,7 @@ export class Client<
|
||||
}
|
||||
|
||||
private writeToEngine(
|
||||
encodedPackets: Array<String | Buffer>,
|
||||
encodedPackets: Array<string | Buffer>,
|
||||
opts: WriteOptions
|
||||
): void {
|
||||
if (opts.volatile && !this.conn.transport.writable) {
|
||||
@@ -267,7 +267,7 @@ export class Client<
|
||||
*/
|
||||
private ondecoded(packet: Packet): void {
|
||||
let namespace: string;
|
||||
let authPayload;
|
||||
let authPayload: Record<string, unknown>;
|
||||
if (this.conn.protocol === 3) {
|
||||
const parsed = url.parse(packet.nsp, true);
|
||||
namespace = parsed.pathname!;
|
||||
|
||||
132
lib/index.ts
132
lib/index.ts
@@ -17,11 +17,16 @@ import { Client } from "./client";
|
||||
import { EventEmitter } from "events";
|
||||
import { ExtendedError, Namespace, ServerReservedEventsMap } from "./namespace";
|
||||
import { ParentNamespace } from "./parent-namespace";
|
||||
import { Adapter, Room, SocketId } from "socket.io-adapter";
|
||||
import {
|
||||
Adapter,
|
||||
SessionAwareAdapter,
|
||||
Room,
|
||||
SocketId,
|
||||
} from "socket.io-adapter";
|
||||
import * as parser from "socket.io-parser";
|
||||
import type { Encoder } from "socket.io-parser";
|
||||
import debugModule from "debug";
|
||||
import { Socket } from "./socket";
|
||||
import { Socket, DisconnectReason } from "./socket";
|
||||
import type { BroadcastOperator, RemoteSocket } from "./broadcast-operator";
|
||||
import {
|
||||
EventsMap,
|
||||
@@ -29,8 +34,14 @@ import {
|
||||
EventParams,
|
||||
StrictEventEmitter,
|
||||
EventNames,
|
||||
DecorateAcknowledgementsWithTimeoutAndMultipleResponses,
|
||||
AllButLast,
|
||||
Last,
|
||||
FirstArg,
|
||||
SecondArg,
|
||||
} from "./typed-events";
|
||||
import { patchAdapter, restoreAdapter, serveFile } from "./uws";
|
||||
import type { BaseServer } from "engine.io/build/server";
|
||||
|
||||
const debug = debugModule("socket.io:server");
|
||||
|
||||
@@ -71,6 +82,30 @@ interface ServerOptions extends EngineOptions, AttachOptions {
|
||||
* @default 45000
|
||||
*/
|
||||
connectTimeout: number;
|
||||
/**
|
||||
* Whether to enable the recovery of connection state when a client temporarily disconnects.
|
||||
*
|
||||
* The connection state includes the missed packets, the rooms the socket was in and the `data` attribute.
|
||||
*/
|
||||
connectionStateRecovery: {
|
||||
/**
|
||||
* The backup duration of the sessions and the packets.
|
||||
*
|
||||
* @default 120000 (2 minutes)
|
||||
*/
|
||||
maxDisconnectionDuration?: number;
|
||||
/**
|
||||
* Whether to skip middlewares upon successful connection state recovery.
|
||||
*
|
||||
* @default true
|
||||
*/
|
||||
skipMiddlewares?: boolean;
|
||||
};
|
||||
/**
|
||||
* Whether to remove child namespaces that have no sockets connected to them
|
||||
* @default false
|
||||
*/
|
||||
cleanupEmptyChildNamespaces: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -127,7 +162,7 @@ export class Server<
|
||||
* const clientsCount = io.engine.clientsCount;
|
||||
*
|
||||
*/
|
||||
public engine: any;
|
||||
public engine: BaseServer;
|
||||
|
||||
/** @private */
|
||||
readonly _parser: typeof parser;
|
||||
@@ -147,7 +182,7 @@ export class Server<
|
||||
> = new Map();
|
||||
private _adapter?: AdapterConstructor;
|
||||
private _serveClient: boolean;
|
||||
private opts: Partial<EngineOptions>;
|
||||
private readonly opts: Partial<ServerOptions>;
|
||||
private eio: Engine;
|
||||
private _path: string;
|
||||
private clientPathRegex: RegExp;
|
||||
@@ -203,15 +238,31 @@ export class Server<
|
||||
this.serveClient(false !== opts.serveClient);
|
||||
this._parser = opts.parser || parser;
|
||||
this.encoder = new this._parser.Encoder();
|
||||
this.adapter(opts.adapter || Adapter);
|
||||
this.sockets = this.of("/");
|
||||
this.opts = opts;
|
||||
if (opts.connectionStateRecovery) {
|
||||
opts.connectionStateRecovery = Object.assign(
|
||||
{
|
||||
maxDisconnectionDuration: 2 * 60 * 1000,
|
||||
skipMiddlewares: true,
|
||||
},
|
||||
opts.connectionStateRecovery
|
||||
);
|
||||
this.adapter(opts.adapter || SessionAwareAdapter);
|
||||
} else {
|
||||
this.adapter(opts.adapter || Adapter);
|
||||
}
|
||||
opts.cleanupEmptyChildNamespaces = !!opts.cleanupEmptyChildNamespaces;
|
||||
this.sockets = this.of("/");
|
||||
if (srv || typeof srv == "number")
|
||||
this.attach(
|
||||
srv as http.Server | HTTPSServer | Http2SecureServer | number
|
||||
);
|
||||
}
|
||||
|
||||
get _opts() {
|
||||
return this.opts;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets/gets whether client code is being served.
|
||||
*
|
||||
@@ -437,7 +488,7 @@ export class Server<
|
||||
res.writeHeader("cache-control", "public, max-age=0");
|
||||
res.writeHeader(
|
||||
"content-type",
|
||||
"application/" + (isMap ? "json" : "javascript")
|
||||
"application/" + (isMap ? "json" : "javascript") + "; charset=utf-8"
|
||||
);
|
||||
res.writeHeader("etag", expectedEtag);
|
||||
|
||||
@@ -530,7 +581,7 @@ export class Server<
|
||||
res.setHeader("Cache-Control", "public, max-age=0");
|
||||
res.setHeader(
|
||||
"Content-Type",
|
||||
"application/" + (isMap ? "json" : "javascript")
|
||||
"application/" + (isMap ? "json" : "javascript") + "; charset=utf-8"
|
||||
);
|
||||
res.setHeader("ETag", expectedEtag);
|
||||
|
||||
@@ -582,10 +633,10 @@ export class Server<
|
||||
/**
|
||||
* Binds socket.io to an engine.io instance.
|
||||
*
|
||||
* @param {engine.Server} engine engine.io (or compatible) server
|
||||
* @param engine engine.io (or compatible) server
|
||||
* @return self
|
||||
*/
|
||||
public bind(engine): this {
|
||||
public bind(engine: BaseServer): this {
|
||||
this.engine = engine;
|
||||
this.engine.on("connection", this.onconnection.bind(this));
|
||||
return this;
|
||||
@@ -763,6 +814,26 @@ export class Server<
|
||||
return this.sockets.except(room);
|
||||
}
|
||||
|
||||
/**
|
||||
* Emits an event and waits for an acknowledgement from all clients.
|
||||
*
|
||||
* @example
|
||||
* try {
|
||||
* const responses = await io.timeout(1000).emitWithAck("some-event");
|
||||
* console.log(responses); // one response per client
|
||||
* } catch (e) {
|
||||
* // some clients did not acknowledge the event in the given delay
|
||||
* }
|
||||
*
|
||||
* @return a Promise that will be fulfilled when all clients have acknowledged the event
|
||||
*/
|
||||
public emitWithAck<Ev extends EventNames<EmitEvents>>(
|
||||
ev: Ev,
|
||||
...args: AllButLast<EventParams<EmitEvents, Ev>>
|
||||
): Promise<SecondArg<Last<EventParams<EmitEvents, Ev>>>> {
|
||||
return this.sockets.emitWithAck(ev, ...args);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a `message` event to all clients.
|
||||
*
|
||||
@@ -806,9 +877,9 @@ export class Server<
|
||||
* // acknowledgements (without binary content) are supported too:
|
||||
* io.serverSideEmit("ping", (err, responses) => {
|
||||
* if (err) {
|
||||
* // some clients did not acknowledge the event in the given delay
|
||||
* // some servers did not acknowledge the event in the given delay
|
||||
* } else {
|
||||
* console.log(responses); // one response per client
|
||||
* console.log(responses); // one response per server (except the current one)
|
||||
* }
|
||||
* });
|
||||
*
|
||||
@@ -821,11 +892,37 @@ export class Server<
|
||||
*/
|
||||
public serverSideEmit<Ev extends EventNames<ServerSideEvents>>(
|
||||
ev: Ev,
|
||||
...args: EventParams<ServerSideEvents, Ev>
|
||||
...args: EventParams<
|
||||
DecorateAcknowledgementsWithTimeoutAndMultipleResponses<ServerSideEvents>,
|
||||
Ev
|
||||
>
|
||||
): boolean {
|
||||
return this.sockets.serverSideEmit(ev, ...args);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a message and expect an acknowledgement from the other Socket.IO servers of the cluster.
|
||||
*
|
||||
* @example
|
||||
* try {
|
||||
* const responses = await io.serverSideEmitWithAck("ping");
|
||||
* console.log(responses); // one response per server (except the current one)
|
||||
* } catch (e) {
|
||||
* // some servers did not acknowledge the event in the given delay
|
||||
* }
|
||||
*
|
||||
* @param ev - the event name
|
||||
* @param args - an array of arguments
|
||||
*
|
||||
* @return a Promise that will be fulfilled when all servers have acknowledged the event
|
||||
*/
|
||||
public serverSideEmitWithAck<Ev extends EventNames<ServerSideEvents>>(
|
||||
ev: Ev,
|
||||
...args: AllButLast<EventParams<ServerSideEvents, Ev>>
|
||||
): Promise<FirstArg<Last<EventParams<ServerSideEvents, Ev>>>[]> {
|
||||
return this.sockets.serverSideEmitWithAck(ev, ...args);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a list of socket ids.
|
||||
*
|
||||
@@ -999,5 +1096,12 @@ module.exports.Server = Server;
|
||||
module.exports.Namespace = Namespace;
|
||||
module.exports.Socket = Socket;
|
||||
|
||||
export { Socket, ServerOptions, Namespace, BroadcastOperator, RemoteSocket };
|
||||
export {
|
||||
Socket,
|
||||
DisconnectReason,
|
||||
ServerOptions,
|
||||
Namespace,
|
||||
BroadcastOperator,
|
||||
RemoteSocket,
|
||||
};
|
||||
export { Event } from "./socket";
|
||||
|
||||
159
lib/namespace.ts
159
lib/namespace.ts
@@ -6,11 +6,16 @@ import {
|
||||
EventsMap,
|
||||
StrictEventEmitter,
|
||||
DefaultEventsMap,
|
||||
DecorateAcknowledgementsWithTimeoutAndMultipleResponses,
|
||||
AllButLast,
|
||||
Last,
|
||||
FirstArg,
|
||||
SecondArg,
|
||||
} from "./typed-events";
|
||||
import type { Client } from "./client";
|
||||
import debugModule from "debug";
|
||||
import type { Adapter, Room, SocketId } from "socket.io-adapter";
|
||||
import { BroadcastOperator, RemoteSocket } from "./broadcast-operator";
|
||||
import { BroadcastOperator } from "./broadcast-operator";
|
||||
|
||||
const debug = debugModule("socket.io:namespace");
|
||||
|
||||
@@ -296,13 +301,25 @@ export class Namespace<
|
||||
* @return {Socket}
|
||||
* @private
|
||||
*/
|
||||
_add(
|
||||
async _add(
|
||||
client: Client<ListenEvents, EmitEvents, ServerSideEvents>,
|
||||
query,
|
||||
fn?: () => void
|
||||
): Socket<ListenEvents, EmitEvents, ServerSideEvents, SocketData> {
|
||||
auth: Record<string, unknown>,
|
||||
fn: (
|
||||
socket: Socket<ListenEvents, EmitEvents, ServerSideEvents, SocketData>
|
||||
) => void
|
||||
) {
|
||||
debug("adding socket to nsp %s", this.name);
|
||||
const socket = new Socket(this, client, query);
|
||||
const socket = await this._createSocket(client, auth);
|
||||
|
||||
if (
|
||||
// @ts-ignore
|
||||
this.server.opts.connectionStateRecovery?.skipMiddlewares &&
|
||||
socket.recovered &&
|
||||
client.conn.readyState === "open"
|
||||
) {
|
||||
return this._doConnect(socket, fn);
|
||||
}
|
||||
|
||||
this.run(socket, (err) => {
|
||||
process.nextTick(() => {
|
||||
if ("open" !== client.conn.readyState) {
|
||||
@@ -324,22 +341,53 @@ export class Namespace<
|
||||
}
|
||||
}
|
||||
|
||||
// track socket
|
||||
this.sockets.set(socket.id, socket);
|
||||
|
||||
// it's paramount that the internal `onconnect` logic
|
||||
// fires before user-set events to prevent state order
|
||||
// violations (such as a disconnection before the connection
|
||||
// logic is complete)
|
||||
socket._onconnect();
|
||||
if (fn) fn();
|
||||
|
||||
// fire user-set events
|
||||
this.emitReserved("connect", socket);
|
||||
this.emitReserved("connection", socket);
|
||||
this._doConnect(socket, fn);
|
||||
});
|
||||
});
|
||||
return socket;
|
||||
}
|
||||
|
||||
private async _createSocket(
|
||||
client: Client<ListenEvents, EmitEvents, ServerSideEvents>,
|
||||
auth: Record<string, unknown>
|
||||
) {
|
||||
const sessionId = auth.pid;
|
||||
const offset = auth.offset;
|
||||
if (
|
||||
// @ts-ignore
|
||||
this.server.opts.connectionStateRecovery &&
|
||||
typeof sessionId === "string" &&
|
||||
typeof offset === "string"
|
||||
) {
|
||||
const session = await this.adapter.restoreSession(sessionId, offset);
|
||||
if (session) {
|
||||
debug("connection state recovered for sid %s", session.sid);
|
||||
return new Socket(this, client, auth, session);
|
||||
} else {
|
||||
debug("unable to restore session state");
|
||||
}
|
||||
}
|
||||
return new Socket(this, client, auth);
|
||||
}
|
||||
|
||||
private _doConnect(
|
||||
socket: Socket<ListenEvents, EmitEvents, ServerSideEvents, SocketData>,
|
||||
fn: (
|
||||
socket: Socket<ListenEvents, EmitEvents, ServerSideEvents, SocketData>
|
||||
) => void
|
||||
) {
|
||||
// track socket
|
||||
this.sockets.set(socket.id, socket);
|
||||
|
||||
// it's paramount that the internal `onconnect` logic
|
||||
// fires before user-set events to prevent state order
|
||||
// violations (such as a disconnection before the connection
|
||||
// logic is complete)
|
||||
socket._onconnect();
|
||||
if (fn) fn(socket);
|
||||
|
||||
// fire user-set events
|
||||
this.emitReserved("connect", socket);
|
||||
this.emitReserved("connection", socket);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -389,6 +437,30 @@ export class Namespace<
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Emits an event and waits for an acknowledgement from all clients.
|
||||
*
|
||||
* @example
|
||||
* const myNamespace = io.of("/my-namespace");
|
||||
*
|
||||
* try {
|
||||
* const responses = await myNamespace.timeout(1000).emitWithAck("some-event");
|
||||
* console.log(responses); // one response per client
|
||||
* } catch (e) {
|
||||
* // some clients did not acknowledge the event in the given delay
|
||||
* }
|
||||
*
|
||||
* @return a Promise that will be fulfilled when all clients have acknowledged the event
|
||||
*/
|
||||
public emitWithAck<Ev extends EventNames<EmitEvents>>(
|
||||
ev: Ev,
|
||||
...args: AllButLast<EventParams<EmitEvents, Ev>>
|
||||
): Promise<SecondArg<Last<EventParams<EmitEvents, Ev>>>> {
|
||||
return new BroadcastOperator<EmitEvents, SocketData>(
|
||||
this.adapter
|
||||
).emitWithAck(ev, ...args);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a `message` event to all clients.
|
||||
*
|
||||
@@ -436,9 +508,9 @@ export class Namespace<
|
||||
* // acknowledgements (without binary content) are supported too:
|
||||
* myNamespace.serverSideEmit("ping", (err, responses) => {
|
||||
* if (err) {
|
||||
* // some clients did not acknowledge the event in the given delay
|
||||
* // some servers did not acknowledge the event in the given delay
|
||||
* } else {
|
||||
* console.log(responses); // one response per client
|
||||
* console.log(responses); // one response per server (except the current one)
|
||||
* }
|
||||
* });
|
||||
*
|
||||
@@ -451,7 +523,10 @@ export class Namespace<
|
||||
*/
|
||||
public serverSideEmit<Ev extends EventNames<ServerSideEvents>>(
|
||||
ev: Ev,
|
||||
...args: EventParams<ServerSideEvents, Ev>
|
||||
...args: EventParams<
|
||||
DecorateAcknowledgementsWithTimeoutAndMultipleResponses<ServerSideEvents>,
|
||||
Ev
|
||||
>
|
||||
): boolean {
|
||||
if (RESERVED_EVENTS.has(ev)) {
|
||||
throw new Error(`"${String(ev)}" is a reserved event name`);
|
||||
@@ -461,6 +536,44 @@ export class Namespace<
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a message and expect an acknowledgement from the other Socket.IO servers of the cluster.
|
||||
*
|
||||
* @example
|
||||
* const myNamespace = io.of("/my-namespace");
|
||||
*
|
||||
* try {
|
||||
* const responses = await myNamespace.serverSideEmitWithAck("ping");
|
||||
* console.log(responses); // one response per server (except the current one)
|
||||
* } catch (e) {
|
||||
* // some servers did not acknowledge the event in the given delay
|
||||
* }
|
||||
*
|
||||
* @param ev - the event name
|
||||
* @param args - an array of arguments
|
||||
*
|
||||
* @return a Promise that will be fulfilled when all servers have acknowledged the event
|
||||
*/
|
||||
public serverSideEmitWithAck<Ev extends EventNames<ServerSideEvents>>(
|
||||
ev: Ev,
|
||||
...args: AllButLast<EventParams<ServerSideEvents, Ev>>
|
||||
): Promise<FirstArg<Last<EventParams<ServerSideEvents, Ev>>>[]> {
|
||||
return new Promise((resolve, reject) => {
|
||||
args.push((err, responses) => {
|
||||
if (err) {
|
||||
err.responses = responses;
|
||||
return reject(err);
|
||||
} else {
|
||||
return resolve(responses);
|
||||
}
|
||||
});
|
||||
this.serverSideEmit(
|
||||
ev,
|
||||
...(args as any[] as EventParams<ServerSideEvents, Ev>)
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when a packet is received from another Socket.IO server
|
||||
*
|
||||
|
||||
@@ -7,6 +7,9 @@ import type {
|
||||
DefaultEventsMap,
|
||||
} from "./typed-events";
|
||||
import type { BroadcastOptions } from "socket.io-adapter";
|
||||
import debugModule from "debug";
|
||||
|
||||
const debug = debugModule("socket.io:parent-namespace");
|
||||
|
||||
export class ParentNamespace<
|
||||
ListenEvents extends EventsMap = DefaultEventsMap,
|
||||
@@ -52,6 +55,7 @@ export class ParentNamespace<
|
||||
createChild(
|
||||
name: string
|
||||
): Namespace<ListenEvents, EmitEvents, ServerSideEvents, SocketData> {
|
||||
debug("creating child namespace %s", name);
|
||||
const namespace = new Namespace(this.server, name);
|
||||
namespace._fns = this._fns.slice(0);
|
||||
this.listeners("connect").forEach((listener) =>
|
||||
@@ -61,6 +65,21 @@ export class ParentNamespace<
|
||||
namespace.on("connection", listener)
|
||||
);
|
||||
this.children.add(namespace);
|
||||
|
||||
if (this.server._opts.cleanupEmptyChildNamespaces) {
|
||||
const remove = namespace._remove;
|
||||
|
||||
namespace._remove = (socket) => {
|
||||
remove.call(namespace, socket);
|
||||
if (namespace.sockets.size === 0) {
|
||||
debug("closing child namespace %s", name);
|
||||
namespace.adapter.close();
|
||||
this.server._nsps.delete(namespace.name);
|
||||
this.children.delete(namespace);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
this.server._nsps.set(name, namespace);
|
||||
return namespace;
|
||||
}
|
||||
|
||||
150
lib/socket.ts
150
lib/socket.ts
@@ -2,19 +2,26 @@ import { Packet, PacketType } from "socket.io-parser";
|
||||
import debugModule from "debug";
|
||||
import type { Server } from "./index";
|
||||
import {
|
||||
EventParams,
|
||||
EventNames,
|
||||
EventsMap,
|
||||
StrictEventEmitter,
|
||||
AllButLast,
|
||||
DecorateAcknowledgements,
|
||||
DecorateAcknowledgementsWithMultipleResponses,
|
||||
DefaultEventsMap,
|
||||
EventNames,
|
||||
EventParams,
|
||||
EventsMap,
|
||||
FirstArg,
|
||||
Last,
|
||||
StrictEventEmitter,
|
||||
} from "./typed-events";
|
||||
import type { Client } from "./client";
|
||||
import type { Namespace, NamespaceReservedEventsMap } from "./namespace";
|
||||
import type { IncomingMessage, IncomingHttpHeaders } from "http";
|
||||
import type { IncomingHttpHeaders, IncomingMessage } from "http";
|
||||
import type {
|
||||
Adapter,
|
||||
BroadcastFlags,
|
||||
PrivateSessionId,
|
||||
Room,
|
||||
Session,
|
||||
SocketId,
|
||||
} from "socket.io-adapter";
|
||||
import base64id from "base64id";
|
||||
@@ -39,6 +46,15 @@ export type DisconnectReason =
|
||||
| "client namespace disconnect"
|
||||
| "server namespace disconnect";
|
||||
|
||||
const RECOVERABLE_DISCONNECT_REASONS: ReadonlySet<DisconnectReason> = new Set([
|
||||
"transport error",
|
||||
"transport close",
|
||||
"forced close",
|
||||
"ping timeout",
|
||||
"server shutting down",
|
||||
"forced server close",
|
||||
]);
|
||||
|
||||
export interface SocketReservedEventsMap {
|
||||
disconnect: (reason: DisconnectReason) => void;
|
||||
disconnecting: (reason: DisconnectReason) => void;
|
||||
@@ -173,6 +189,11 @@ export class Socket<
|
||||
* An unique identifier for the session.
|
||||
*/
|
||||
public readonly id: SocketId;
|
||||
/**
|
||||
* Whether the connection state was recovered after a temporary disconnection. In that case, any missed packets will
|
||||
* be transmitted to the client, the data attribute and the rooms will be restored.
|
||||
*/
|
||||
public readonly recovered: boolean = false;
|
||||
/**
|
||||
* The handshake details.
|
||||
*/
|
||||
@@ -197,6 +218,14 @@ export class Socket<
|
||||
*/
|
||||
public connected: boolean = false;
|
||||
|
||||
/**
|
||||
* The session ID, which must not be shared (unlike {@link id}).
|
||||
*
|
||||
* @private
|
||||
*/
|
||||
private readonly pid: PrivateSessionId;
|
||||
|
||||
// TODO: remove this unused reference
|
||||
private readonly server: Server<
|
||||
ListenEvents,
|
||||
EmitEvents,
|
||||
@@ -221,16 +250,32 @@ export class Socket<
|
||||
constructor(
|
||||
readonly nsp: Namespace<ListenEvents, EmitEvents, ServerSideEvents>,
|
||||
readonly client: Client<ListenEvents, EmitEvents, ServerSideEvents>,
|
||||
auth: object
|
||||
auth: Record<string, unknown>,
|
||||
previousSession?: Session
|
||||
) {
|
||||
super();
|
||||
this.server = nsp.server;
|
||||
this.adapter = this.nsp.adapter;
|
||||
if (client.conn.protocol === 3) {
|
||||
// @ts-ignore
|
||||
this.id = nsp.name !== "/" ? nsp.name + "#" + client.id : client.id;
|
||||
if (previousSession) {
|
||||
this.id = previousSession.sid;
|
||||
this.pid = previousSession.pid;
|
||||
previousSession.rooms.forEach((room) => this.join(room));
|
||||
this.data = previousSession.data as Partial<SocketData>;
|
||||
previousSession.missedPackets.forEach((packet) => {
|
||||
this.packet({
|
||||
type: PacketType.EVENT,
|
||||
data: packet,
|
||||
});
|
||||
});
|
||||
this.recovered = true;
|
||||
} else {
|
||||
this.id = base64id.generateId(); // don't reuse the Engine.IO id because it's sensitive information
|
||||
if (client.conn.protocol === 3) {
|
||||
// @ts-ignore
|
||||
this.id = nsp.name !== "/" ? nsp.name + "#" + client.id : client.id;
|
||||
} else {
|
||||
this.id = base64id.generateId(); // don't reuse the Engine.IO id because it's sensitive information
|
||||
}
|
||||
this.pid = base64id.generateId();
|
||||
}
|
||||
this.handshake = this.buildHandshake(auth);
|
||||
}
|
||||
@@ -299,12 +344,58 @@ export class Socket<
|
||||
const flags = Object.assign({}, this.flags);
|
||||
this.flags = {};
|
||||
|
||||
this.notifyOutgoingListeners(packet);
|
||||
this.packet(packet, flags);
|
||||
// @ts-ignore
|
||||
if (this.nsp.server.opts.connectionStateRecovery) {
|
||||
// this ensures the packet is stored and can be transmitted upon reconnection
|
||||
this.adapter.broadcast(packet, {
|
||||
rooms: new Set([this.id]),
|
||||
except: new Set(),
|
||||
flags,
|
||||
});
|
||||
} else {
|
||||
this.notifyOutgoingListeners(packet);
|
||||
this.packet(packet, flags);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Emits an event and waits for an acknowledgement
|
||||
*
|
||||
* @example
|
||||
* io.on("connection", async (socket) => {
|
||||
* // 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 client did not acknowledge the event in the given delay
|
||||
* }
|
||||
* });
|
||||
*
|
||||
* @return a Promise that will be fulfilled when the client acknowledges the event
|
||||
*/
|
||||
public emitWithAck<Ev extends EventNames<EmitEvents>>(
|
||||
ev: Ev,
|
||||
...args: AllButLast<EventParams<EmitEvents, Ev>>
|
||||
): Promise<FirstArg<Last<EventParams<EmitEvents, Ev>>>> {
|
||||
// the timeout flag is optional
|
||||
const withErr = this.flags.timeout !== undefined;
|
||||
return new Promise((resolve, reject) => {
|
||||
args.push((arg1, arg2) => {
|
||||
if (withErr) {
|
||||
return arg1 ? reject(arg1) : resolve(arg2);
|
||||
} else {
|
||||
return resolve(arg1);
|
||||
}
|
||||
});
|
||||
this.emit(ev, ...(args as any[] as EventParams<EmitEvents, Ev>));
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @private
|
||||
*/
|
||||
@@ -508,7 +599,10 @@ export class Socket<
|
||||
if (this.conn.protocol === 3) {
|
||||
this.packet({ type: PacketType.CONNECT });
|
||||
} else {
|
||||
this.packet({ type: PacketType.CONNECT, data: { sid: this.id } });
|
||||
this.packet({
|
||||
type: PacketType.CONNECT,
|
||||
data: { sid: this.id, pid: this.pid },
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -644,6 +738,17 @@ export class Socket<
|
||||
if (!this.connected) return this;
|
||||
debug("closing socket - reason %s", reason);
|
||||
this.emitReserved("disconnecting", reason);
|
||||
|
||||
if (RECOVERABLE_DISCONNECT_REASONS.has(reason)) {
|
||||
debug("connection state recovery is enabled for sid %s", this.id);
|
||||
this.adapter.persistSession({
|
||||
sid: this.id,
|
||||
pid: this.pid,
|
||||
rooms: [...this.rooms],
|
||||
data: this.data,
|
||||
});
|
||||
}
|
||||
|
||||
this._cleanup();
|
||||
this.nsp._remove(this);
|
||||
this.client._remove(this);
|
||||
@@ -778,7 +883,14 @@ export class Socket<
|
||||
*
|
||||
* @returns self
|
||||
*/
|
||||
public timeout(timeout: number): this {
|
||||
public timeout(
|
||||
timeout: number
|
||||
): Socket<
|
||||
ListenEvents,
|
||||
DecorateAcknowledgements<EmitEvents>,
|
||||
ServerSideEvents,
|
||||
SocketData
|
||||
> {
|
||||
this.flags.timeout = timeout;
|
||||
return this;
|
||||
}
|
||||
@@ -1088,11 +1200,9 @@ export class Socket<
|
||||
private newBroadcastOperator() {
|
||||
const flags = Object.assign({}, this.flags);
|
||||
this.flags = {};
|
||||
return new BroadcastOperator<EmitEvents, SocketData>(
|
||||
this.adapter,
|
||||
new Set<Room>(),
|
||||
new Set<Room>([this.id]),
|
||||
flags
|
||||
);
|
||||
return new BroadcastOperator<
|
||||
DecorateAcknowledgementsWithMultipleResponses<EmitEvents>,
|
||||
SocketData
|
||||
>(this.adapter, new Set<Room>(), new Set<Room>([this.id]), flags);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -178,3 +178,66 @@ export abstract class StrictEventEmitter<
|
||||
>[];
|
||||
}
|
||||
}
|
||||
|
||||
export type Last<T extends any[]> = T extends [...infer H, infer L] ? L : any;
|
||||
export type AllButLast<T extends any[]> = T extends [...infer H, infer L]
|
||||
? H
|
||||
: any[];
|
||||
export type FirstArg<T> = T extends (arg: infer Param) => infer Result
|
||||
? Param
|
||||
: any;
|
||||
export type SecondArg<T> = T extends (
|
||||
err: Error,
|
||||
arg: infer Param
|
||||
) => infer Result
|
||||
? Param
|
||||
: any;
|
||||
|
||||
type PrependTimeoutError<T extends any[]> = {
|
||||
[K in keyof T]: T[K] extends (...args: infer Params) => infer Result
|
||||
? (err: Error, ...args: Params) => Result
|
||||
: T[K];
|
||||
};
|
||||
|
||||
type ExpectMultipleResponses<T extends any[]> = {
|
||||
[K in keyof T]: T[K] extends (err: Error, arg: infer Param) => infer Result
|
||||
? (err: Error, arg: Param[]) => Result
|
||||
: T[K];
|
||||
};
|
||||
|
||||
/**
|
||||
* Utility type to decorate the acknowledgement callbacks with a timeout error.
|
||||
*
|
||||
* This is needed because the timeout() flag breaks the symmetry between the sender and the receiver:
|
||||
*
|
||||
* @example
|
||||
* interface Events {
|
||||
* "my-event": (val: string) => void;
|
||||
* }
|
||||
*
|
||||
* socket.on("my-event", (cb) => {
|
||||
* cb("123"); // one single argument here
|
||||
* });
|
||||
*
|
||||
* socket.timeout(1000).emit("my-event", (err, val) => {
|
||||
* // two arguments there (the "err" argument is not properly typed)
|
||||
* });
|
||||
*
|
||||
*/
|
||||
export type DecorateAcknowledgements<E> = {
|
||||
[K in keyof E]: E[K] extends (...args: infer Params) => infer Result
|
||||
? (...args: PrependTimeoutError<Params>) => Result
|
||||
: E[K];
|
||||
};
|
||||
|
||||
export type DecorateAcknowledgementsWithTimeoutAndMultipleResponses<E> = {
|
||||
[K in keyof E]: E[K] extends (...args: infer Params) => infer Result
|
||||
? (...args: ExpectMultipleResponses<PrependTimeoutError<Params>>) => Result
|
||||
: E[K];
|
||||
};
|
||||
|
||||
export type DecorateAcknowledgementsWithMultipleResponses<E> = {
|
||||
[K in keyof E]: E[K] extends (...args: infer Params) => infer Result
|
||||
? (...args: ExpectMultipleResponses<Params>) => Result
|
||||
: E[K];
|
||||
};
|
||||
|
||||
216
package-lock.json
generated
216
package-lock.json
generated
@@ -1,20 +1,20 @@
|
||||
{
|
||||
"name": "socket.io",
|
||||
"version": "4.5.2",
|
||||
"version": "4.5.4",
|
||||
"lockfileVersion": 2,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "socket.io",
|
||||
"version": "4.5.2",
|
||||
"version": "4.5.4",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"accepts": "~1.3.4",
|
||||
"base64id": "~2.0.0",
|
||||
"debug": "~4.3.2",
|
||||
"engine.io": "~6.2.0",
|
||||
"socket.io-adapter": "~2.4.0",
|
||||
"socket.io-parser": "~4.2.0"
|
||||
"engine.io": "~6.3.1",
|
||||
"socket.io-adapter": "~2.5.2",
|
||||
"socket.io-parser": "~4.2.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/mocha": "^9.0.0",
|
||||
@@ -23,7 +23,7 @@
|
||||
"nyc": "^15.1.0",
|
||||
"prettier": "^2.3.2",
|
||||
"rimraf": "^3.0.2",
|
||||
"socket.io-client": "4.5.3",
|
||||
"socket.io-client": "4.5.4",
|
||||
"socket.io-client-v2": "npm:socket.io-client@^2.4.0",
|
||||
"superagent": "^8.0.0",
|
||||
"supertest": "^6.1.6",
|
||||
@@ -1334,9 +1334,9 @@
|
||||
}
|
||||
},
|
||||
"node_modules/dezalgo": {
|
||||
"version": "1.0.3",
|
||||
"resolved": "https://registry.npmjs.org/dezalgo/-/dezalgo-1.0.3.tgz",
|
||||
"integrity": "sha512-K7i4zNfT2kgQz3GylDw40ot9GAE47sFZ9EXHFSPP6zONLgH6kWXE0KWJchkbQJLBkRazq4APwZ4OwiFFlT95OQ==",
|
||||
"version": "1.0.4",
|
||||
"resolved": "https://registry.npmjs.org/dezalgo/-/dezalgo-1.0.4.tgz",
|
||||
"integrity": "sha512-rXSP0bf+5n0Qonsb+SVVfNfIsimO4HEtmnIpPHY8Q1UCzKlQrDMfdobr8nJOOsRgWCyMRqeSBQzmWUMq7zvVig==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"asap": "^2.0.0",
|
||||
@@ -1377,9 +1377,9 @@
|
||||
"dev": true
|
||||
},
|
||||
"node_modules/engine.io": {
|
||||
"version": "6.2.0",
|
||||
"resolved": "https://registry.npmjs.org/engine.io/-/engine.io-6.2.0.tgz",
|
||||
"integrity": "sha512-4KzwW3F3bk+KlzSOY57fj/Jx6LyRQ1nbcyIadehl+AnXjKT7gDO0ORdRi/84ixvMKTym6ZKuxvbzN62HDDU1Lg==",
|
||||
"version": "6.3.1",
|
||||
"resolved": "https://registry.npmjs.org/engine.io/-/engine.io-6.3.1.tgz",
|
||||
"integrity": "sha512-VhEArSKyCC8dv223fltbMOqaJInFZqIqLABLnD3VLhclriF9sxnAJu6ZvnIMI+p7+ByZBxXd4otTrLAeeMTImg==",
|
||||
"dependencies": {
|
||||
"@types/cookie": "^0.4.1",
|
||||
"@types/cors": "^2.8.12",
|
||||
@@ -1390,7 +1390,7 @@
|
||||
"cors": "~2.8.5",
|
||||
"debug": "~4.3.1",
|
||||
"engine.io-parser": "~5.0.3",
|
||||
"ws": "~8.2.3"
|
||||
"ws": "~8.11.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=10.0.0"
|
||||
@@ -1417,6 +1417,26 @@
|
||||
"node": ">=10.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/engine.io/node_modules/ws": {
|
||||
"version": "8.11.0",
|
||||
"resolved": "https://registry.npmjs.org/ws/-/ws-8.11.0.tgz",
|
||||
"integrity": "sha512-HPG3wQd9sNQoT9xHyNCXoDUa+Xw/VevmY9FoHyQ+g+rrMn4j6FB4np7Z0OhdTgjx6MgQLK7jwSy1YecU1+4Asg==",
|
||||
"engines": {
|
||||
"node": ">=10.0.0"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"bufferutil": "^4.0.1",
|
||||
"utf-8-validate": "^5.0.2"
|
||||
},
|
||||
"peerDependenciesMeta": {
|
||||
"bufferutil": {
|
||||
"optional": true
|
||||
},
|
||||
"utf-8-validate": {
|
||||
"optional": true
|
||||
}
|
||||
}
|
||||
},
|
||||
"node_modules/error-ex": {
|
||||
"version": "1.3.2",
|
||||
"resolved": "https://registry.npmjs.org/error-ex/-/error-ex-1.3.2.tgz",
|
||||
@@ -1613,32 +1633,20 @@
|
||||
}
|
||||
},
|
||||
"node_modules/formidable": {
|
||||
"version": "2.0.1",
|
||||
"resolved": "https://registry.npmjs.org/formidable/-/formidable-2.0.1.tgz",
|
||||
"integrity": "sha512-rjTMNbp2BpfQShhFbR3Ruk3qk2y9jKpvMW78nJgx8QKtxjDVrwbZG+wvDOmVbifHyOUOQJXxqEy6r0faRrPzTQ==",
|
||||
"version": "2.1.1",
|
||||
"resolved": "https://registry.npmjs.org/formidable/-/formidable-2.1.1.tgz",
|
||||
"integrity": "sha512-0EcS9wCFEzLvfiks7omJ+SiYJAiD+TzK4Pcw1UlUoGnhUxDcMKjt0P7x8wEb0u6OHu8Nb98WG3nxtlF5C7bvUQ==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"dezalgo": "1.0.3",
|
||||
"hexoid": "1.0.0",
|
||||
"once": "1.4.0",
|
||||
"qs": "6.9.3"
|
||||
"dezalgo": "^1.0.4",
|
||||
"hexoid": "^1.0.0",
|
||||
"once": "^1.4.0",
|
||||
"qs": "^6.11.0"
|
||||
},
|
||||
"funding": {
|
||||
"url": "https://ko-fi.com/tunnckoCore/commissions"
|
||||
}
|
||||
},
|
||||
"node_modules/formidable/node_modules/qs": {
|
||||
"version": "6.9.3",
|
||||
"resolved": "https://registry.npmjs.org/qs/-/qs-6.9.3.tgz",
|
||||
"integrity": "sha512-EbZYNarm6138UKKq46tdx08Yo/q9ZhFoAXAI1meAFd2GtbRDhbZY2WQSICskT0c5q99aFzLG1D4nvTk9tqfXIw==",
|
||||
"dev": true,
|
||||
"engines": {
|
||||
"node": ">=0.6"
|
||||
},
|
||||
"funding": {
|
||||
"url": "https://github.com/sponsors/ljharb"
|
||||
}
|
||||
},
|
||||
"node_modules/fromentries": {
|
||||
"version": "1.3.2",
|
||||
"resolved": "https://registry.npmjs.org/fromentries/-/fromentries-1.3.2.tgz",
|
||||
@@ -2254,9 +2262,9 @@
|
||||
"dev": true
|
||||
},
|
||||
"node_modules/json5": {
|
||||
"version": "2.2.1",
|
||||
"resolved": "https://registry.npmjs.org/json5/-/json5-2.2.1.tgz",
|
||||
"integrity": "sha512-1hqLFMSrGHRHxav9q9gNjJ5EXznIxGVO09xQRrwplcS8qs28pZ8s8hupZAmqDwZUmVZ2Qb2jnyPOWcDH8m8dlA==",
|
||||
"version": "2.2.3",
|
||||
"resolved": "https://registry.npmjs.org/json5/-/json5-2.2.3.tgz",
|
||||
"integrity": "sha512-XmOWe7eyHYH14cLdVPoyg+GOH3rYX++KpzrylJwSW98t3Nk+U8XOl8FWKOgwtzdb8lXGf6zYwDUzeHMWfxasyg==",
|
||||
"dev": true,
|
||||
"bin": {
|
||||
"json5": "lib/cli.js"
|
||||
@@ -3467,20 +3475,43 @@
|
||||
}
|
||||
},
|
||||
"node_modules/socket.io-adapter": {
|
||||
"version": "2.4.0",
|
||||
"resolved": "https://registry.npmjs.org/socket.io-adapter/-/socket.io-adapter-2.4.0.tgz",
|
||||
"integrity": "sha512-W4N+o69rkMEGVuk2D/cvca3uYsvGlMwsySWV447y99gUPghxq42BxqLNMndb+a1mm/5/7NeXVQS7RLa2XyXvYg=="
|
||||
"version": "2.5.2",
|
||||
"resolved": "https://registry.npmjs.org/socket.io-adapter/-/socket.io-adapter-2.5.2.tgz",
|
||||
"integrity": "sha512-87C3LO/NOMc+eMcpcxUBebGjkpMDkNBS9tf7KJqcDsmL936EChtVva71Dw2q4tQcuVC+hAUy4an2NO/sYXmwRA==",
|
||||
"dependencies": {
|
||||
"ws": "~8.11.0"
|
||||
}
|
||||
},
|
||||
"node_modules/socket.io-adapter/node_modules/ws": {
|
||||
"version": "8.11.0",
|
||||
"resolved": "https://registry.npmjs.org/ws/-/ws-8.11.0.tgz",
|
||||
"integrity": "sha512-HPG3wQd9sNQoT9xHyNCXoDUa+Xw/VevmY9FoHyQ+g+rrMn4j6FB4np7Z0OhdTgjx6MgQLK7jwSy1YecU1+4Asg==",
|
||||
"engines": {
|
||||
"node": ">=10.0.0"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"bufferutil": "^4.0.1",
|
||||
"utf-8-validate": "^5.0.2"
|
||||
},
|
||||
"peerDependenciesMeta": {
|
||||
"bufferutil": {
|
||||
"optional": true
|
||||
},
|
||||
"utf-8-validate": {
|
||||
"optional": true
|
||||
}
|
||||
}
|
||||
},
|
||||
"node_modules/socket.io-client": {
|
||||
"version": "4.5.3",
|
||||
"resolved": "https://registry.npmjs.org/socket.io-client/-/socket.io-client-4.5.3.tgz",
|
||||
"integrity": "sha512-I/hqDYpQ6JKwtJOf5ikM+Qz+YujZPMEl6qBLhxiP0nX+TfXKhW4KZZG8lamrD6Y5ngjmYHreESVasVCgi5Kl3A==",
|
||||
"version": "4.5.4",
|
||||
"resolved": "https://registry.npmjs.org/socket.io-client/-/socket.io-client-4.5.4.tgz",
|
||||
"integrity": "sha512-ZpKteoA06RzkD32IbqILZ+Cnst4xewU7ZYK12aS1mzHftFFjpoMz69IuhP/nL25pJfao/amoPI527KnuhFm01g==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"@socket.io/component-emitter": "~3.1.0",
|
||||
"debug": "~4.3.2",
|
||||
"engine.io-client": "~6.2.3",
|
||||
"socket.io-parser": "~4.2.0"
|
||||
"socket.io-parser": "~4.2.1"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=10.0.0"
|
||||
@@ -3554,9 +3585,9 @@
|
||||
"dev": true
|
||||
},
|
||||
"node_modules/socket.io-client-v2/node_modules/socket.io-parser": {
|
||||
"version": "3.3.2",
|
||||
"resolved": "https://registry.npmjs.org/socket.io-parser/-/socket.io-parser-3.3.2.tgz",
|
||||
"integrity": "sha512-FJvDBuOALxdCI9qwRrO/Rfp9yfndRtc1jSgVgV8FDraihmSP/MLGD5PEuJrNfjALvcQ+vMDM/33AWOYP/JSjDg==",
|
||||
"version": "3.3.3",
|
||||
"resolved": "https://registry.npmjs.org/socket.io-parser/-/socket.io-parser-3.3.3.tgz",
|
||||
"integrity": "sha512-qOg87q1PMWWTeO01768Yh9ogn7chB9zkKtQnya41Y355S0UmpXgpcrFwAgjYJxu9BdKug5r5e9YtVSeWhKBUZg==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"component-emitter": "~1.3.0",
|
||||
@@ -3595,9 +3626,9 @@
|
||||
}
|
||||
},
|
||||
"node_modules/socket.io-parser": {
|
||||
"version": "4.2.0",
|
||||
"resolved": "https://registry.npmjs.org/socket.io-parser/-/socket.io-parser-4.2.0.tgz",
|
||||
"integrity": "sha512-tLfmEwcEwnlQTxFB7jibL/q2+q8dlVQzj4JdRLJ/W/G1+Fu9VSxCx1Lo+n1HvXxKnM//dUuD0xgiA7tQf57Vng==",
|
||||
"version": "4.2.2",
|
||||
"resolved": "https://registry.npmjs.org/socket.io-parser/-/socket.io-parser-4.2.2.tgz",
|
||||
"integrity": "sha512-DJtziuKypFkMMHCm2uIshOYC7QaylbtzQwiMYDuCKy3OPkjLzu4B2vAhTlqipRHHzrI0NJeBAizTK7X+6m1jVw==",
|
||||
"dependencies": {
|
||||
"@socket.io/component-emitter": "~3.1.0",
|
||||
"debug": "~4.3.1"
|
||||
@@ -4208,6 +4239,7 @@
|
||||
"version": "8.2.3",
|
||||
"resolved": "https://registry.npmjs.org/ws/-/ws-8.2.3.tgz",
|
||||
"integrity": "sha512-wBuoj1BDpC6ZQ1B7DWQBYVLphPWkm8i9Y0/3YdHjHKHiohOJ1ws+3OccDWtH+PoC9DZD5WOTrJvNbWvjS6JWaA==",
|
||||
"dev": true,
|
||||
"engines": {
|
||||
"node": ">=10.0.0"
|
||||
},
|
||||
@@ -5359,9 +5391,9 @@
|
||||
"dev": true
|
||||
},
|
||||
"dezalgo": {
|
||||
"version": "1.0.3",
|
||||
"resolved": "https://registry.npmjs.org/dezalgo/-/dezalgo-1.0.3.tgz",
|
||||
"integrity": "sha512-K7i4zNfT2kgQz3GylDw40ot9GAE47sFZ9EXHFSPP6zONLgH6kWXE0KWJchkbQJLBkRazq4APwZ4OwiFFlT95OQ==",
|
||||
"version": "1.0.4",
|
||||
"resolved": "https://registry.npmjs.org/dezalgo/-/dezalgo-1.0.4.tgz",
|
||||
"integrity": "sha512-rXSP0bf+5n0Qonsb+SVVfNfIsimO4HEtmnIpPHY8Q1UCzKlQrDMfdobr8nJOOsRgWCyMRqeSBQzmWUMq7zvVig==",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"asap": "^2.0.0",
|
||||
@@ -5396,9 +5428,9 @@
|
||||
"dev": true
|
||||
},
|
||||
"engine.io": {
|
||||
"version": "6.2.0",
|
||||
"resolved": "https://registry.npmjs.org/engine.io/-/engine.io-6.2.0.tgz",
|
||||
"integrity": "sha512-4KzwW3F3bk+KlzSOY57fj/Jx6LyRQ1nbcyIadehl+AnXjKT7gDO0ORdRi/84ixvMKTym6ZKuxvbzN62HDDU1Lg==",
|
||||
"version": "6.3.1",
|
||||
"resolved": "https://registry.npmjs.org/engine.io/-/engine.io-6.3.1.tgz",
|
||||
"integrity": "sha512-VhEArSKyCC8dv223fltbMOqaJInFZqIqLABLnD3VLhclriF9sxnAJu6ZvnIMI+p7+ByZBxXd4otTrLAeeMTImg==",
|
||||
"requires": {
|
||||
"@types/cookie": "^0.4.1",
|
||||
"@types/cors": "^2.8.12",
|
||||
@@ -5409,7 +5441,15 @@
|
||||
"cors": "~2.8.5",
|
||||
"debug": "~4.3.1",
|
||||
"engine.io-parser": "~5.0.3",
|
||||
"ws": "~8.2.3"
|
||||
"ws": "~8.11.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"ws": {
|
||||
"version": "8.11.0",
|
||||
"resolved": "https://registry.npmjs.org/ws/-/ws-8.11.0.tgz",
|
||||
"integrity": "sha512-HPG3wQd9sNQoT9xHyNCXoDUa+Xw/VevmY9FoHyQ+g+rrMn4j6FB4np7Z0OhdTgjx6MgQLK7jwSy1YecU1+4Asg==",
|
||||
"requires": {}
|
||||
}
|
||||
}
|
||||
},
|
||||
"engine.io-client": {
|
||||
@@ -5577,23 +5617,15 @@
|
||||
}
|
||||
},
|
||||
"formidable": {
|
||||
"version": "2.0.1",
|
||||
"resolved": "https://registry.npmjs.org/formidable/-/formidable-2.0.1.tgz",
|
||||
"integrity": "sha512-rjTMNbp2BpfQShhFbR3Ruk3qk2y9jKpvMW78nJgx8QKtxjDVrwbZG+wvDOmVbifHyOUOQJXxqEy6r0faRrPzTQ==",
|
||||
"version": "2.1.1",
|
||||
"resolved": "https://registry.npmjs.org/formidable/-/formidable-2.1.1.tgz",
|
||||
"integrity": "sha512-0EcS9wCFEzLvfiks7omJ+SiYJAiD+TzK4Pcw1UlUoGnhUxDcMKjt0P7x8wEb0u6OHu8Nb98WG3nxtlF5C7bvUQ==",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"dezalgo": "1.0.3",
|
||||
"hexoid": "1.0.0",
|
||||
"once": "1.4.0",
|
||||
"qs": "6.9.3"
|
||||
},
|
||||
"dependencies": {
|
||||
"qs": {
|
||||
"version": "6.9.3",
|
||||
"resolved": "https://registry.npmjs.org/qs/-/qs-6.9.3.tgz",
|
||||
"integrity": "sha512-EbZYNarm6138UKKq46tdx08Yo/q9ZhFoAXAI1meAFd2GtbRDhbZY2WQSICskT0c5q99aFzLG1D4nvTk9tqfXIw==",
|
||||
"dev": true
|
||||
}
|
||||
"dezalgo": "^1.0.4",
|
||||
"hexoid": "^1.0.0",
|
||||
"once": "^1.4.0",
|
||||
"qs": "^6.11.0"
|
||||
}
|
||||
},
|
||||
"fromentries": {
|
||||
@@ -6050,9 +6082,9 @@
|
||||
"dev": true
|
||||
},
|
||||
"json5": {
|
||||
"version": "2.2.1",
|
||||
"resolved": "https://registry.npmjs.org/json5/-/json5-2.2.1.tgz",
|
||||
"integrity": "sha512-1hqLFMSrGHRHxav9q9gNjJ5EXznIxGVO09xQRrwplcS8qs28pZ8s8hupZAmqDwZUmVZ2Qb2jnyPOWcDH8m8dlA==",
|
||||
"version": "2.2.3",
|
||||
"resolved": "https://registry.npmjs.org/json5/-/json5-2.2.3.tgz",
|
||||
"integrity": "sha512-XmOWe7eyHYH14cLdVPoyg+GOH3rYX++KpzrylJwSW98t3Nk+U8XOl8FWKOgwtzdb8lXGf6zYwDUzeHMWfxasyg==",
|
||||
"dev": true
|
||||
},
|
||||
"kind-of": {
|
||||
@@ -6931,20 +6963,31 @@
|
||||
"dev": true
|
||||
},
|
||||
"socket.io-adapter": {
|
||||
"version": "2.4.0",
|
||||
"resolved": "https://registry.npmjs.org/socket.io-adapter/-/socket.io-adapter-2.4.0.tgz",
|
||||
"integrity": "sha512-W4N+o69rkMEGVuk2D/cvca3uYsvGlMwsySWV447y99gUPghxq42BxqLNMndb+a1mm/5/7NeXVQS7RLa2XyXvYg=="
|
||||
"version": "2.5.2",
|
||||
"resolved": "https://registry.npmjs.org/socket.io-adapter/-/socket.io-adapter-2.5.2.tgz",
|
||||
"integrity": "sha512-87C3LO/NOMc+eMcpcxUBebGjkpMDkNBS9tf7KJqcDsmL936EChtVva71Dw2q4tQcuVC+hAUy4an2NO/sYXmwRA==",
|
||||
"requires": {
|
||||
"ws": "~8.11.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"ws": {
|
||||
"version": "8.11.0",
|
||||
"resolved": "https://registry.npmjs.org/ws/-/ws-8.11.0.tgz",
|
||||
"integrity": "sha512-HPG3wQd9sNQoT9xHyNCXoDUa+Xw/VevmY9FoHyQ+g+rrMn4j6FB4np7Z0OhdTgjx6MgQLK7jwSy1YecU1+4Asg==",
|
||||
"requires": {}
|
||||
}
|
||||
}
|
||||
},
|
||||
"socket.io-client": {
|
||||
"version": "4.5.3",
|
||||
"resolved": "https://registry.npmjs.org/socket.io-client/-/socket.io-client-4.5.3.tgz",
|
||||
"integrity": "sha512-I/hqDYpQ6JKwtJOf5ikM+Qz+YujZPMEl6qBLhxiP0nX+TfXKhW4KZZG8lamrD6Y5ngjmYHreESVasVCgi5Kl3A==",
|
||||
"version": "4.5.4",
|
||||
"resolved": "https://registry.npmjs.org/socket.io-client/-/socket.io-client-4.5.4.tgz",
|
||||
"integrity": "sha512-ZpKteoA06RzkD32IbqILZ+Cnst4xewU7ZYK12aS1mzHftFFjpoMz69IuhP/nL25pJfao/amoPI527KnuhFm01g==",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"@socket.io/component-emitter": "~3.1.0",
|
||||
"debug": "~4.3.2",
|
||||
"engine.io-client": "~6.2.3",
|
||||
"socket.io-parser": "~4.2.0"
|
||||
"socket.io-parser": "~4.2.1"
|
||||
}
|
||||
},
|
||||
"socket.io-client-v2": {
|
||||
@@ -7014,9 +7057,9 @@
|
||||
"dev": true
|
||||
},
|
||||
"socket.io-parser": {
|
||||
"version": "3.3.2",
|
||||
"resolved": "https://registry.npmjs.org/socket.io-parser/-/socket.io-parser-3.3.2.tgz",
|
||||
"integrity": "sha512-FJvDBuOALxdCI9qwRrO/Rfp9yfndRtc1jSgVgV8FDraihmSP/MLGD5PEuJrNfjALvcQ+vMDM/33AWOYP/JSjDg==",
|
||||
"version": "3.3.3",
|
||||
"resolved": "https://registry.npmjs.org/socket.io-parser/-/socket.io-parser-3.3.3.tgz",
|
||||
"integrity": "sha512-qOg87q1PMWWTeO01768Yh9ogn7chB9zkKtQnya41Y355S0UmpXgpcrFwAgjYJxu9BdKug5r5e9YtVSeWhKBUZg==",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"component-emitter": "~1.3.0",
|
||||
@@ -7040,9 +7083,9 @@
|
||||
}
|
||||
},
|
||||
"socket.io-parser": {
|
||||
"version": "4.2.0",
|
||||
"resolved": "https://registry.npmjs.org/socket.io-parser/-/socket.io-parser-4.2.0.tgz",
|
||||
"integrity": "sha512-tLfmEwcEwnlQTxFB7jibL/q2+q8dlVQzj4JdRLJ/W/G1+Fu9VSxCx1Lo+n1HvXxKnM//dUuD0xgiA7tQf57Vng==",
|
||||
"version": "4.2.2",
|
||||
"resolved": "https://registry.npmjs.org/socket.io-parser/-/socket.io-parser-4.2.2.tgz",
|
||||
"integrity": "sha512-DJtziuKypFkMMHCm2uIshOYC7QaylbtzQwiMYDuCKy3OPkjLzu4B2vAhTlqipRHHzrI0NJeBAizTK7X+6m1jVw==",
|
||||
"requires": {
|
||||
"@socket.io/component-emitter": "~3.1.0",
|
||||
"debug": "~4.3.1"
|
||||
@@ -7495,6 +7538,7 @@
|
||||
"version": "8.2.3",
|
||||
"resolved": "https://registry.npmjs.org/ws/-/ws-8.2.3.tgz",
|
||||
"integrity": "sha512-wBuoj1BDpC6ZQ1B7DWQBYVLphPWkm8i9Y0/3YdHjHKHiohOJ1ws+3OccDWtH+PoC9DZD5WOTrJvNbWvjS6JWaA==",
|
||||
"dev": true,
|
||||
"requires": {}
|
||||
},
|
||||
"xmlhttprequest-ssl": {
|
||||
|
||||
10
package.json
10
package.json
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "socket.io",
|
||||
"version": "4.5.3",
|
||||
"version": "4.5.4",
|
||||
"description": "node.js realtime framework server",
|
||||
"keywords": [
|
||||
"realtime",
|
||||
@@ -49,9 +49,9 @@
|
||||
"accepts": "~1.3.4",
|
||||
"base64id": "~2.0.0",
|
||||
"debug": "~4.3.2",
|
||||
"engine.io": "~6.2.0",
|
||||
"socket.io-adapter": "~2.4.0",
|
||||
"socket.io-parser": "~4.2.0"
|
||||
"engine.io": "~6.3.1",
|
||||
"socket.io-adapter": "~2.5.2",
|
||||
"socket.io-parser": "~4.2.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/mocha": "^9.0.0",
|
||||
@@ -60,7 +60,7 @@
|
||||
"nyc": "^15.1.0",
|
||||
"prettier": "^2.3.2",
|
||||
"rimraf": "^3.0.2",
|
||||
"socket.io-client": "4.5.3",
|
||||
"socket.io-client": "4.5.4",
|
||||
"socket.io-client-v2": "npm:socket.io-client@^2.4.0",
|
||||
"superagent": "^8.0.0",
|
||||
"supertest": "^6.1.6",
|
||||
|
||||
@@ -4,46 +4,13 @@ import { join } from "path";
|
||||
import { exec } from "child_process";
|
||||
import { Server } from "..";
|
||||
import expect from "expect.js";
|
||||
import { createClient, getPort } from "./support/util";
|
||||
import request from "supertest";
|
||||
|
||||
// TODO: update superagent as latest release now supports promises
|
||||
const eioHandshake = (httpServer): Promise<string> => {
|
||||
return new Promise((resolve) => {
|
||||
request(httpServer)
|
||||
.get("/socket.io/")
|
||||
.query({ transport: "polling", EIO: 4 })
|
||||
.end((err, res) => {
|
||||
const sid = JSON.parse(res.text.substring(1)).sid;
|
||||
resolve(sid);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
const eioPush = (httpServer, sid: string, body: string): Promise<void> => {
|
||||
return new Promise((resolve) => {
|
||||
request(httpServer)
|
||||
.post("/socket.io/")
|
||||
.send(body)
|
||||
.query({ transport: "polling", EIO: 4, sid })
|
||||
.expect(200)
|
||||
.end(() => {
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
const eioPoll = (httpServer, sid): Promise<string> => {
|
||||
return new Promise((resolve) => {
|
||||
request(httpServer)
|
||||
.get("/socket.io/")
|
||||
.query({ transport: "polling", EIO: 4, sid })
|
||||
.expect(200)
|
||||
.end((err, res) => {
|
||||
resolve(res.text);
|
||||
});
|
||||
});
|
||||
};
|
||||
import {
|
||||
createClient,
|
||||
eioHandshake,
|
||||
eioPoll,
|
||||
eioPush,
|
||||
getPort,
|
||||
} from "./support/util";
|
||||
|
||||
describe("close", () => {
|
||||
it("should be able to close sio sending a srv", (done) => {
|
||||
|
||||
196
test/connection-state-recovery.ts
Normal file
196
test/connection-state-recovery.ts
Normal file
@@ -0,0 +1,196 @@
|
||||
import { Server, Socket } from "..";
|
||||
import expect from "expect.js";
|
||||
import { waitFor, eioHandshake, eioPush, eioPoll } from "./support/util";
|
||||
import { createServer, Server as HttpServer } from "http";
|
||||
|
||||
async function init(httpServer: HttpServer, io: Server) {
|
||||
// Engine.IO handshake
|
||||
const sid = await eioHandshake(httpServer);
|
||||
|
||||
// Socket.IO handshake
|
||||
await eioPush(httpServer, sid, "40");
|
||||
const handshakeBody = await eioPoll(httpServer, sid);
|
||||
|
||||
expect(handshakeBody.startsWith("40")).to.be(true);
|
||||
|
||||
const handshake = JSON.parse(handshakeBody.substring(2));
|
||||
|
||||
expect(handshake.sid).to.not.be(undefined);
|
||||
// in that case, the handshake also contains a private session ID
|
||||
expect(handshake.pid).to.not.be(undefined);
|
||||
|
||||
io.emit("hello");
|
||||
|
||||
const message = await eioPoll(httpServer, sid);
|
||||
|
||||
expect(message.startsWith('42["hello"')).to.be(true);
|
||||
|
||||
const offset = JSON.parse(message.substring(2))[1];
|
||||
// in that case, each packet also includes an offset in the data array
|
||||
expect(offset).to.not.be(undefined);
|
||||
|
||||
await eioPush(httpServer, sid, "1");
|
||||
|
||||
return [handshake.sid, handshake.pid, offset];
|
||||
}
|
||||
|
||||
describe("connection state recovery", () => {
|
||||
it("should restore session and missed packets", async () => {
|
||||
const httpServer = createServer().listen(0);
|
||||
const io = new Server(httpServer, {
|
||||
connectionStateRecovery: {},
|
||||
});
|
||||
|
||||
let serverSocket;
|
||||
|
||||
io.once("connection", (socket) => {
|
||||
socket.join("room1");
|
||||
serverSocket = socket;
|
||||
});
|
||||
|
||||
const [sid, pid, offset] = await init(httpServer, io);
|
||||
|
||||
io.emit("hello1"); // broadcast
|
||||
io.to("room1").emit("hello2"); // broadcast to room
|
||||
serverSocket.emit("hello3"); // direct message
|
||||
|
||||
const newSid = await eioHandshake(httpServer);
|
||||
await eioPush(
|
||||
httpServer,
|
||||
newSid,
|
||||
`40{"pid":"${pid}","offset":"${offset}"}`
|
||||
);
|
||||
|
||||
const payload = await eioPoll(httpServer, newSid);
|
||||
const packets = payload.split("\x1e");
|
||||
|
||||
expect(packets.length).to.eql(4);
|
||||
|
||||
// note: EVENT packets are received before the CONNECT packet, which is a bit weird
|
||||
// see also: https://github.com/socketio/socket.io-deno/commit/518f534e1c205b746b1cb21fe76b187dabc96f34
|
||||
expect(packets[0].startsWith('42["hello1"')).to.be(true);
|
||||
expect(packets[1].startsWith('42["hello2"')).to.be(true);
|
||||
expect(packets[2].startsWith('42["hello3"')).to.be(true);
|
||||
expect(packets[3]).to.eql(`40{"sid":"${sid}","pid":"${pid}"}`);
|
||||
|
||||
io.close();
|
||||
});
|
||||
|
||||
it("should restore rooms and data attributes", async () => {
|
||||
const httpServer = createServer().listen(0);
|
||||
const io = new Server(httpServer, {
|
||||
connectionStateRecovery: {},
|
||||
});
|
||||
|
||||
io.once("connection", (socket) => {
|
||||
expect(socket.recovered).to.eql(false);
|
||||
|
||||
socket.join("room1");
|
||||
socket.join("room2");
|
||||
socket.data.foo = "bar";
|
||||
});
|
||||
|
||||
const [sid, pid, offset] = await init(httpServer, io);
|
||||
|
||||
const newSid = await eioHandshake(httpServer);
|
||||
|
||||
const [socket] = await Promise.all([
|
||||
waitFor<Socket>(io, "connection"),
|
||||
eioPush(httpServer, newSid, `40{"pid":"${pid}","offset":"${offset}"}`),
|
||||
]);
|
||||
|
||||
expect(socket.id).to.eql(sid);
|
||||
expect(socket.recovered).to.eql(true);
|
||||
|
||||
expect(socket.rooms.has(socket.id)).to.eql(true);
|
||||
expect(socket.rooms.has("room1")).to.eql(true);
|
||||
expect(socket.rooms.has("room2")).to.eql(true);
|
||||
|
||||
expect(socket.data.foo).to.eql("bar");
|
||||
|
||||
await eioPoll(httpServer, newSid); // drain buffer
|
||||
io.close();
|
||||
});
|
||||
|
||||
it("should not run middlewares upon recovery by default", async () => {
|
||||
const httpServer = createServer().listen(0);
|
||||
const io = new Server(httpServer, {
|
||||
connectionStateRecovery: {},
|
||||
});
|
||||
|
||||
const [_, pid, offset] = await init(httpServer, io);
|
||||
|
||||
io.use((socket, next) => {
|
||||
socket.data.middlewareWasCalled = true;
|
||||
|
||||
next();
|
||||
});
|
||||
|
||||
const newSid = await eioHandshake(httpServer);
|
||||
|
||||
const [socket] = await Promise.all([
|
||||
waitFor<Socket>(io, "connection"),
|
||||
eioPush(httpServer, newSid, `40{"pid":"${pid}","offset":"${offset}"}`),
|
||||
]);
|
||||
|
||||
expect(socket.recovered).to.be(true);
|
||||
expect(socket.data.middlewareWasCalled).to.be(undefined);
|
||||
|
||||
await eioPoll(httpServer, newSid); // drain buffer
|
||||
io.close();
|
||||
});
|
||||
|
||||
it("should run middlewares even upon recovery", async () => {
|
||||
const httpServer = createServer().listen(0);
|
||||
const io = new Server(httpServer, {
|
||||
connectionStateRecovery: {
|
||||
skipMiddlewares: false,
|
||||
},
|
||||
});
|
||||
|
||||
const [_, pid, offset] = await init(httpServer, io);
|
||||
|
||||
io.use((socket, next) => {
|
||||
socket.data.middlewareWasCalled = true;
|
||||
|
||||
next();
|
||||
});
|
||||
|
||||
const newSid = await eioHandshake(httpServer);
|
||||
|
||||
const [socket] = await Promise.all([
|
||||
waitFor<Socket>(io, "connection"),
|
||||
eioPush(httpServer, newSid, `40{"pid":"${pid}","offset":"${offset}"}`),
|
||||
]);
|
||||
|
||||
expect(socket.recovered).to.be(true);
|
||||
expect(socket.data.middlewareWasCalled).to.be(true);
|
||||
|
||||
await eioPoll(httpServer, newSid); // drain buffer
|
||||
io.close();
|
||||
});
|
||||
|
||||
it("should fail to restore an unknown session", async () => {
|
||||
const httpServer = createServer().listen(0);
|
||||
const io = new Server(httpServer, {
|
||||
connectionStateRecovery: {},
|
||||
});
|
||||
|
||||
// Engine.IO handshake
|
||||
const sid = await eioHandshake(httpServer);
|
||||
|
||||
// Socket.IO handshake
|
||||
await eioPush(httpServer, sid, '40{"pid":"foo","offset":"bar"}');
|
||||
|
||||
const handshakeBody = await eioPoll(httpServer, sid);
|
||||
|
||||
expect(handshakeBody.startsWith("40")).to.be(true);
|
||||
|
||||
const handshake = JSON.parse(handshakeBody.substring(2));
|
||||
|
||||
expect(handshake.sid).to.not.eql("foo");
|
||||
expect(handshake.pid).to.not.eql("bar");
|
||||
|
||||
io.close();
|
||||
});
|
||||
});
|
||||
@@ -20,4 +20,5 @@ describe("socket.io", () => {
|
||||
require("./socket-timeout");
|
||||
require("./uws");
|
||||
require("./utility-methods");
|
||||
require("./connection-state-recovery");
|
||||
});
|
||||
|
||||
@@ -471,6 +471,74 @@ describe("messaging many", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("should broadcast and expect multiple acknowledgements (promise)", (done) => {
|
||||
const io = new Server(0);
|
||||
const socket1 = createClient(io, "/", { multiplex: false });
|
||||
const socket2 = createClient(io, "/", { multiplex: false });
|
||||
const socket3 = createClient(io, "/", { multiplex: false });
|
||||
|
||||
socket1.on("some event", (cb) => {
|
||||
cb(1);
|
||||
});
|
||||
|
||||
socket2.on("some event", (cb) => {
|
||||
cb(2);
|
||||
});
|
||||
|
||||
socket3.on("some event", (cb) => {
|
||||
cb(3);
|
||||
});
|
||||
|
||||
Promise.all([
|
||||
waitFor(socket1, "connect"),
|
||||
waitFor(socket2, "connect"),
|
||||
waitFor(socket3, "connect"),
|
||||
]).then(async () => {
|
||||
const responses = await io.timeout(2000).emitWithAck("some event");
|
||||
expect(responses).to.contain(1, 2, 3);
|
||||
|
||||
success(done, io, socket1, socket2, socket3);
|
||||
});
|
||||
});
|
||||
|
||||
it("should fail when a client does not acknowledge the event in the given delay (promise)", (done) => {
|
||||
const io = new Server(0);
|
||||
const socket1 = createClient(io, "/", { multiplex: false });
|
||||
const socket2 = createClient(io, "/", { multiplex: false });
|
||||
const socket3 = createClient(io, "/", { multiplex: false });
|
||||
|
||||
socket1.on("some event", (cb) => {
|
||||
cb(1);
|
||||
});
|
||||
|
||||
socket2.on("some event", (cb) => {
|
||||
cb(2);
|
||||
});
|
||||
|
||||
socket3.on("some event", () => {
|
||||
// timeout
|
||||
});
|
||||
|
||||
Promise.all([
|
||||
waitFor(socket1, "connect"),
|
||||
waitFor(socket2, "connect"),
|
||||
waitFor(socket3, "connect"),
|
||||
]).then(async () => {
|
||||
try {
|
||||
await io.timeout(200).emitWithAck("some event");
|
||||
expect.fail();
|
||||
} catch (err) {
|
||||
expect(err).to.be.an(Error);
|
||||
// @ts-ignore
|
||||
expect(err.responses).to.have.length(2);
|
||||
// @ts-ignore
|
||||
expect(err.responses).to.contain(1, 2);
|
||||
|
||||
success(done, io, socket1, socket2, socket3);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it("should broadcast and return if the packet is sent to 0 client", (done) => {
|
||||
const io = new Server(0);
|
||||
const socket1 = createClient(io, "/", { multiplex: false });
|
||||
@@ -498,4 +566,22 @@ describe("messaging many", () => {
|
||||
success(done, io, socket1, socket2, socket3);
|
||||
});
|
||||
});
|
||||
|
||||
it("should precompute the WebSocket frame when broadcasting", (done) => {
|
||||
const io = new Server(0);
|
||||
const socket = createClient(io, "/chat", {
|
||||
transports: ["websocket"],
|
||||
});
|
||||
const partialDone = createPartialDone(2, successFn(done, io, socket));
|
||||
|
||||
io.of("/chat").on("connection", (s) => {
|
||||
s.conn.once("packetCreate", (packet) => {
|
||||
expect(packet.options.wsPreEncodedFrame).to.be.an(Array);
|
||||
partialDone();
|
||||
});
|
||||
io.of("/chat").compress(false).emit("woot", "hi");
|
||||
});
|
||||
|
||||
socket.on("woot", partialDone);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -473,6 +473,24 @@ describe("namespaces", () => {
|
||||
io.of("/nsp");
|
||||
});
|
||||
|
||||
it("should not clean up a non-dynamic namespace", (done) => {
|
||||
const io = new Server(0, { cleanupEmptyChildNamespaces: true });
|
||||
const c1 = createClient(io, "/chat");
|
||||
|
||||
c1.on("connect", () => {
|
||||
c1.disconnect();
|
||||
|
||||
// Give it some time to disconnect the client
|
||||
setTimeout(() => {
|
||||
expect(io._nsps.has("/chat")).to.be(true);
|
||||
expect(io._nsps.get("/chat")!.sockets.size).to.be(0);
|
||||
success(done, io);
|
||||
}, 100);
|
||||
});
|
||||
|
||||
io.of("/chat");
|
||||
});
|
||||
|
||||
describe("dynamic namespaces", () => {
|
||||
it("should allow connections to dynamic namespaces with a regex", (done) => {
|
||||
const io = new Server(0);
|
||||
@@ -571,5 +589,68 @@ describe("namespaces", () => {
|
||||
one.on("message", handler);
|
||||
two.on("message", handler);
|
||||
});
|
||||
|
||||
it("should clean up namespace when cleanupEmptyChildNamespaces is on and there are no more sockets in a namespace", (done) => {
|
||||
const io = new Server(0, { cleanupEmptyChildNamespaces: true });
|
||||
const c1 = createClient(io, "/dynamic-101");
|
||||
|
||||
c1.on("connect", () => {
|
||||
c1.disconnect();
|
||||
|
||||
// Give it some time to disconnect and clean up the namespace
|
||||
setTimeout(() => {
|
||||
expect(io._nsps.has("/dynamic-101")).to.be(false);
|
||||
success(done, io);
|
||||
}, 100);
|
||||
});
|
||||
|
||||
io.of(/^\/dynamic-\d+$/);
|
||||
});
|
||||
|
||||
it("should allow a client to connect to a cleaned up namespace", (done) => {
|
||||
const io = new Server(0, { cleanupEmptyChildNamespaces: true });
|
||||
const c1 = createClient(io, "/dynamic-101");
|
||||
|
||||
c1.on("connect", () => {
|
||||
c1.disconnect();
|
||||
|
||||
// Give it some time to disconnect and clean up the namespace
|
||||
setTimeout(() => {
|
||||
expect(io._nsps.has("/dynamic-101")).to.be(false);
|
||||
|
||||
const c2 = createClient(io, "/dynamic-101");
|
||||
|
||||
c2.on("connect", () => {
|
||||
success(done, io, c2);
|
||||
});
|
||||
|
||||
c2.on("connect_error", () => {
|
||||
done(
|
||||
new Error("Client got error when connecting to dynamic namespace")
|
||||
);
|
||||
});
|
||||
}, 100);
|
||||
});
|
||||
|
||||
io.of(/^\/dynamic-\d+$/);
|
||||
});
|
||||
|
||||
it("should not clean up namespace when cleanupEmptyChildNamespaces is off and there are no more sockets in a namespace", (done) => {
|
||||
const io = new Server(0);
|
||||
const c1 = createClient(io, "/dynamic-101");
|
||||
|
||||
c1.on("connect", () => {
|
||||
c1.disconnect();
|
||||
|
||||
// Give it some time to disconnect and clean up the namespace
|
||||
setTimeout(() => {
|
||||
expect(io._nsps.has("/dynamic-101")).to.be(true);
|
||||
expect(io._nsps.get("/dynamic-101")!.sockets.size).to.be(0);
|
||||
success(done, io);
|
||||
}, 100);
|
||||
});
|
||||
|
||||
io.of(/^\/dynamic-\d+$/);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -16,7 +16,9 @@ describe("server attachment", () => {
|
||||
.buffer(true)
|
||||
.end((err, res) => {
|
||||
if (err) return done(err);
|
||||
expect(res.headers["content-type"]).to.be("application/javascript");
|
||||
expect(res.headers["content-type"]).to.be(
|
||||
"application/javascript; charset=utf-8"
|
||||
);
|
||||
expect(res.headers.etag).to.be('"' + clientVersion + '"');
|
||||
expect(res.headers["x-sourcemap"]).to.be(undefined);
|
||||
expect(res.text).to.match(/engine\.io/);
|
||||
@@ -33,7 +35,9 @@ describe("server attachment", () => {
|
||||
.buffer(true)
|
||||
.end((err, res) => {
|
||||
if (err) return done(err);
|
||||
expect(res.headers["content-type"]).to.be("application/json");
|
||||
expect(res.headers["content-type"]).to.be(
|
||||
"application/json; charset=utf-8"
|
||||
);
|
||||
expect(res.headers.etag).to.be('"' + clientVersion + '"');
|
||||
expect(res.text).to.match(/engine\.io/);
|
||||
expect(res.status).to.be(200);
|
||||
|
||||
@@ -54,4 +54,34 @@ describe("timeout", () => {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it("should timeout if the client does not acknowledge the event (promise)", (done) => {
|
||||
const io = new Server(0);
|
||||
const client = createClient(io, "/");
|
||||
|
||||
io.on("connection", async (socket) => {
|
||||
try {
|
||||
await socket.timeout(50).emitWithAck("unknown");
|
||||
expect.fail();
|
||||
} catch (err) {
|
||||
expect(err).to.be.an(Error);
|
||||
success(done, io, client);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it("should not timeout if the client does acknowledge the event (promise)", (done) => {
|
||||
const io = new Server(0);
|
||||
const client = createClient(io, "/");
|
||||
|
||||
client.on("echo", (arg, cb) => {
|
||||
cb(arg);
|
||||
});
|
||||
|
||||
io.on("connection", async (socket) => {
|
||||
const value = await socket.timeout(50).emitWithAck("echo", 42);
|
||||
expect(value).to.be(42);
|
||||
success(done, io, client);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -92,6 +92,28 @@ describe("server", () => {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("emitWithAck", () => {
|
||||
it("accepts any parameters", () => {
|
||||
const srv = createServer();
|
||||
const sio = new Server(srv);
|
||||
srv.listen(async () => {
|
||||
const value = await sio
|
||||
.timeout(1000)
|
||||
.emitWithAck("ackFromServerSingleArg", true, "123");
|
||||
expectType<any>(value);
|
||||
|
||||
sio.on("connection", async (s) => {
|
||||
const value1 = await s.emitWithAck(
|
||||
"ackFromServerSingleArg",
|
||||
true,
|
||||
"123"
|
||||
);
|
||||
expectType<any>(value1);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("single event map", () => {
|
||||
@@ -167,10 +189,32 @@ describe("server", () => {
|
||||
describe("listen and emit event maps", () => {
|
||||
interface ClientToServerEvents {
|
||||
helloFromClient: (message: string) => void;
|
||||
ackFromClient: (
|
||||
a: string,
|
||||
b: number,
|
||||
ack: (c: string, d: number) => void
|
||||
) => void;
|
||||
}
|
||||
|
||||
interface ServerToClientEvents {
|
||||
helloFromServer: (message: string, x: number) => void;
|
||||
ackFromServer: (
|
||||
a: boolean,
|
||||
b: string,
|
||||
ack: (c: boolean, d: string) => void
|
||||
) => void;
|
||||
|
||||
ackFromServerSingleArg: (
|
||||
a: boolean,
|
||||
b: string,
|
||||
ack: (c: string) => void
|
||||
) => void;
|
||||
|
||||
multipleAckFromServer: (
|
||||
a: boolean,
|
||||
b: string,
|
||||
ack: (c: string) => void
|
||||
) => void;
|
||||
}
|
||||
|
||||
describe("on", () => {
|
||||
@@ -185,6 +229,13 @@ describe("server", () => {
|
||||
expectType<string>(message);
|
||||
done();
|
||||
});
|
||||
|
||||
s.on("ackFromClient", (a, b, cb) => {
|
||||
expectType<string>(a);
|
||||
expectType<number>(b);
|
||||
expectType<(c: string, d: number) => void>(cb);
|
||||
cb("123", 456);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -213,8 +264,41 @@ describe("server", () => {
|
||||
sio.to("room").emit("helloFromServer", "hi", 1);
|
||||
sio.timeout(1000).emit("helloFromServer", "hi", 1);
|
||||
|
||||
sio
|
||||
.timeout(1000)
|
||||
.emit("multipleAckFromServer", true, "123", (err, c) => {
|
||||
expectType<Error>(err);
|
||||
expectType<string[]>(c);
|
||||
});
|
||||
|
||||
sio.on("connection", (s) => {
|
||||
s.emit("helloFromServer", "hi", 10);
|
||||
|
||||
s.emit("ackFromServer", true, "123", (c, d) => {
|
||||
expectType<boolean>(c);
|
||||
expectType<string>(d);
|
||||
});
|
||||
|
||||
s.timeout(1000).emit("ackFromServer", true, "123", (err, c, d) => {
|
||||
expectType<Error>(err);
|
||||
expectType<boolean>(c);
|
||||
expectType<string>(d);
|
||||
});
|
||||
|
||||
s.timeout(1000)
|
||||
.to("room")
|
||||
.emit("multipleAckFromServer", true, "123", (err, c) => {
|
||||
expectType<Error>(err);
|
||||
expectType<string[]>(c);
|
||||
});
|
||||
|
||||
s.to("room")
|
||||
.timeout(1000)
|
||||
.emit("multipleAckFromServer", true, "123", (err, c) => {
|
||||
expectType<Error>(err);
|
||||
expectType<string[]>(c);
|
||||
});
|
||||
|
||||
done();
|
||||
});
|
||||
});
|
||||
@@ -240,6 +324,42 @@ describe("server", () => {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("emitWithAck", () => {
|
||||
it("accepts arguments of the correct types", (done) => {
|
||||
const srv = createServer();
|
||||
const sio = new Server<ClientToServerEvents, ServerToClientEvents>(srv);
|
||||
srv.listen(async () => {
|
||||
const value = await sio
|
||||
.timeout(1000)
|
||||
.emitWithAck("multipleAckFromServer", true, "123");
|
||||
expectType<string[]>(value);
|
||||
|
||||
sio.on("connection", async (s) => {
|
||||
const value1 = await s
|
||||
.timeout(1000)
|
||||
.to("room")
|
||||
.emitWithAck("multipleAckFromServer", true, "123");
|
||||
expectType<string[]>(value1);
|
||||
|
||||
const value2 = await s
|
||||
.to("room")
|
||||
.timeout(1000)
|
||||
.emitWithAck("multipleAckFromServer", true, "123");
|
||||
expectType<string[]>(value2);
|
||||
|
||||
const value3 = await s.emitWithAck(
|
||||
"ackFromServerSingleArg",
|
||||
true,
|
||||
"123"
|
||||
);
|
||||
expectType<string>(value3);
|
||||
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("listen and emit event maps for the serverSideEmit method", () => {
|
||||
@@ -253,6 +373,7 @@ describe("server", () => {
|
||||
|
||||
interface InterServerEvents {
|
||||
helloFromServerToServer: (message: string, x: number) => void;
|
||||
ackFromServerToServer: (foo: string, cb: (bar: number) => void) => void;
|
||||
}
|
||||
|
||||
describe("on", () => {
|
||||
@@ -267,7 +388,7 @@ describe("server", () => {
|
||||
expectType<
|
||||
Server<ClientToServerEvents, ServerToClientEvents, InterServerEvents>
|
||||
>(sio);
|
||||
srv.listen(() => {
|
||||
srv.listen(async () => {
|
||||
sio.serverSideEmit("helloFromServerToServer", "hello", 10);
|
||||
sio
|
||||
.of("/test")
|
||||
@@ -281,6 +402,22 @@ describe("server", () => {
|
||||
expectType<string>(message);
|
||||
expectType<number>(x);
|
||||
});
|
||||
|
||||
sio.serverSideEmit("ackFromServerToServer", "foo", (err, bar) => {
|
||||
expectType<Error>(err);
|
||||
expectType<number[]>(bar);
|
||||
});
|
||||
|
||||
const value = await sio.serverSideEmitWithAck(
|
||||
"ackFromServerToServer",
|
||||
"foo"
|
||||
);
|
||||
expectType<number[]>(value);
|
||||
|
||||
sio.on("ackFromServerToServer", (foo, cb) => {
|
||||
expectType<string>(foo);
|
||||
expectType<(bar: number) => void>(cb);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,6 +1,12 @@
|
||||
import fs = require("fs");
|
||||
import { join } from "path";
|
||||
import { createClient, getPort, success, successFn } from "./support/util";
|
||||
import {
|
||||
createClient,
|
||||
createPartialDone,
|
||||
getPort,
|
||||
success,
|
||||
successFn,
|
||||
} from "./support/util";
|
||||
import { Server } from "..";
|
||||
import expect from "expect.js";
|
||||
|
||||
@@ -599,6 +605,24 @@ describe("socket", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("should emit an event and wait for the acknowledgement", (done) => {
|
||||
const io = new Server(0);
|
||||
const socket = createClient(io);
|
||||
|
||||
io.on("connection", async (s) => {
|
||||
socket.on("hi", (a, b, fn) => {
|
||||
expect(a).to.be(1);
|
||||
expect(b).to.be(2);
|
||||
fn(3);
|
||||
});
|
||||
|
||||
const val = await s.emitWithAck("hi", 1, 2);
|
||||
expect(val).to.be(3);
|
||||
|
||||
success(done, io, socket);
|
||||
});
|
||||
});
|
||||
|
||||
it("should have access to the client", (done) => {
|
||||
const io = new Server(0);
|
||||
const socket = createClient(io);
|
||||
@@ -731,7 +755,7 @@ describe("socket", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("should enable compresion by default", (done) => {
|
||||
it("should enable compression by default", (done) => {
|
||||
const io = new Server(0);
|
||||
const socket = createClient(io, "/chat");
|
||||
|
||||
@@ -740,11 +764,11 @@ describe("socket", () => {
|
||||
expect(packet.options.compress).to.be(true);
|
||||
success(done, io, socket);
|
||||
});
|
||||
io.of("/chat").emit("woot", "hi");
|
||||
s.emit("woot", "hi");
|
||||
});
|
||||
});
|
||||
|
||||
it("should disable compresion", (done) => {
|
||||
it("should disable compression", (done) => {
|
||||
const io = new Server(0);
|
||||
const socket = createClient(io, "/chat");
|
||||
|
||||
@@ -753,7 +777,7 @@ describe("socket", () => {
|
||||
expect(packet.options.compress).to.be(false);
|
||||
success(done, io, socket);
|
||||
});
|
||||
io.of("/chat").compress(false).emit("woot", "hi");
|
||||
s.compress(false).emit("woot", "hi");
|
||||
});
|
||||
});
|
||||
|
||||
@@ -995,6 +1019,22 @@ describe("socket", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("should call listener when broadcasting binary data", (done) => {
|
||||
const io = new Server(0);
|
||||
const clientSocket = createClient(io, "/", { multiplex: false });
|
||||
|
||||
io.on("connection", (socket) => {
|
||||
socket.onAnyOutgoing((event, arg1) => {
|
||||
expect(event).to.be("my-event");
|
||||
expect(arg1).to.be.an(Uint8Array);
|
||||
|
||||
success(done, io, clientSocket);
|
||||
});
|
||||
|
||||
io.emit("my-event", Uint8Array.of(1, 2, 3));
|
||||
});
|
||||
});
|
||||
|
||||
it("should prepend listener", (done) => {
|
||||
const io = new Server(0);
|
||||
const clientSocket = createClient(io, "/", { multiplex: false });
|
||||
@@ -1039,5 +1079,30 @@ describe("socket", () => {
|
||||
socket.emit("my-event", "123");
|
||||
});
|
||||
});
|
||||
|
||||
it("should disconnect all namespaces when calling disconnect(true)", (done) => {
|
||||
const io = new Server(0);
|
||||
io.of("/foo");
|
||||
io.of("/bar");
|
||||
|
||||
const socket1 = createClient(io, "/", {
|
||||
transports: ["websocket"],
|
||||
});
|
||||
const socket2 = createClient(io, "/foo");
|
||||
const socket3 = createClient(io, "/bar");
|
||||
|
||||
io.of("/bar").on("connection", (socket) => {
|
||||
socket.disconnect(true);
|
||||
});
|
||||
|
||||
const partialDone = createPartialDone(
|
||||
3,
|
||||
successFn(done, io, socket1, socket2, socket3)
|
||||
);
|
||||
|
||||
socket1.on("disconnect", partialDone);
|
||||
socket2.on("disconnect", partialDone);
|
||||
socket3.on("disconnect", partialDone);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -5,6 +5,7 @@ import {
|
||||
Socket as ClientSocket,
|
||||
SocketOptions,
|
||||
} from "socket.io-client";
|
||||
import request from "supertest";
|
||||
|
||||
const expect = require("expect.js");
|
||||
const i = expect.stringify;
|
||||
@@ -73,8 +74,46 @@ export function createPartialDone(count: number, done: (err?: Error) => void) {
|
||||
};
|
||||
}
|
||||
|
||||
export function waitFor(emitter, event) {
|
||||
return new Promise((resolve) => {
|
||||
export function waitFor<T = unknown>(emitter, event) {
|
||||
return new Promise<T>((resolve) => {
|
||||
emitter.once(event, resolve);
|
||||
});
|
||||
}
|
||||
|
||||
// TODO: update superagent as latest release now supports promises
|
||||
export function eioHandshake(httpServer): Promise<string> {
|
||||
return new Promise((resolve) => {
|
||||
request(httpServer)
|
||||
.get("/socket.io/")
|
||||
.query({ transport: "polling", EIO: 4 })
|
||||
.end((err, res) => {
|
||||
const sid = JSON.parse(res.text.substring(1)).sid;
|
||||
resolve(sid);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export function eioPush(httpServer, sid: string, body: string): Promise<void> {
|
||||
return new Promise((resolve) => {
|
||||
request(httpServer)
|
||||
.post("/socket.io/")
|
||||
.send(body)
|
||||
.query({ transport: "polling", EIO: 4, sid })
|
||||
.expect(200)
|
||||
.end(() => {
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export function eioPoll(httpServer, sid): Promise<string> {
|
||||
return new Promise((resolve) => {
|
||||
request(httpServer)
|
||||
.get("/socket.io/")
|
||||
.query({ transport: "polling", EIO: 4, sid })
|
||||
.expect(200)
|
||||
.end((err, res) => {
|
||||
resolve(res.text);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@@ -200,7 +200,9 @@ describe("socket.io with uWebSocket.js-based engine", () => {
|
||||
.buffer(true)
|
||||
.end((err, res) => {
|
||||
if (err) return done(err);
|
||||
expect(res.headers["content-type"]).to.be("application/javascript");
|
||||
expect(res.headers["content-type"]).to.be(
|
||||
"application/javascript; charset=utf-8"
|
||||
);
|
||||
expect(res.headers.etag).to.be('"' + clientVersion + '"');
|
||||
expect(res.headers["x-sourcemap"]).to.be(undefined);
|
||||
expect(res.text).to.match(/engine\.io/);
|
||||
|
||||
Reference in New Issue
Block a user