mirror of
https://github.com/socketio/socket.io.git
synced 2026-01-12 08:27:57 -05:00
Compare commits
23 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
00d8ee5b05 | ||
|
|
2dd5fa9dd4 | ||
|
|
a5dff0ac83 | ||
|
|
3035c25982 | ||
|
|
63f181cc12 | ||
|
|
a250e283da | ||
|
|
e5c62cad60 | ||
|
|
01d37624a8 | ||
|
|
faf914c9ab | ||
|
|
15af22fc22 | ||
|
|
d3658944e5 | ||
|
|
12b0de4f52 | ||
|
|
3d44aae381 | ||
|
|
cbf0362476 | ||
|
|
59280da20b | ||
|
|
50a4d37cb8 | ||
|
|
6458b2bef1 | ||
|
|
b56da8a99f | ||
|
|
7952312911 | ||
|
|
0d0a7a22b5 | ||
|
|
2a8565fd1e | ||
|
|
d0b22c6302 | ||
|
|
e71f3d7dbe |
4
.github/workflows/ci.yml
vendored
4
.github/workflows/ci.yml
vendored
@@ -16,7 +16,9 @@ jobs:
|
||||
|
||||
strategy:
|
||||
matrix:
|
||||
node-version: [12, 14, 16]
|
||||
node-version:
|
||||
- 16
|
||||
- 20
|
||||
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
|
||||
171
CHANGELOG.md
171
CHANGELOG.md
@@ -2,6 +2,9 @@
|
||||
|
||||
## 2023
|
||||
|
||||
- [4.7.0](#470-2023-06-22) (Jun 2023)
|
||||
- [4.6.2](#462-2023-05-31) (May 2023)
|
||||
- [4.6.1](#461-2023-02-20) (Feb 2023)
|
||||
- [4.6.0](#460-2023-02-07) (Feb 2023)
|
||||
|
||||
## 2022
|
||||
@@ -56,7 +59,124 @@
|
||||
|
||||
# Release notes
|
||||
|
||||
# [4.6.0](https://github.com/socketio/socket.io/compare/4.5.4...4.6.0) (2023-02-07)
|
||||
## [4.7.0](https://github.com/socketio/socket.io/compare/4.6.2...4.7.0) (2023-06-22)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* remove the Partial modifier from the socket.data type ([#4740](https://github.com/socketio/socket.io/issues/4740)) ([e5c62ca](https://github.com/socketio/socket.io/commit/e5c62cad60fc7d16fbb024fd9be1d1880f4e6f5f))
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
#### Support for WebTransport
|
||||
|
||||
The Engine.IO server can now use WebTransport as the underlying transport.
|
||||
|
||||
WebTransport is a web API that uses the HTTP/3 protocol as a bidirectional transport. It's intended for two-way communications between a web client and an HTTP/3 server.
|
||||
|
||||
References:
|
||||
|
||||
- https://w3c.github.io/webtransport/
|
||||
- https://developer.mozilla.org/en-US/docs/Web/API/WebTransport
|
||||
- https://developer.chrome.com/articles/webtransport/
|
||||
|
||||
Until WebTransport support lands [in Node.js](https://github.com/nodejs/node/issues/38478), you can use the `@fails-components/webtransport` package:
|
||||
|
||||
```js
|
||||
import { readFileSync } from "fs";
|
||||
import { createServer } from "https";
|
||||
import { Server } from "socket.io";
|
||||
import { Http3Server } from "@fails-components/webtransport";
|
||||
|
||||
// WARNING: the total length of the validity period MUST NOT exceed two weeks (https://w3c.github.io/webtransport/#custom-certificate-requirements)
|
||||
const cert = readFileSync("/path/to/my/cert.pem");
|
||||
const key = readFileSync("/path/to/my/key.pem");
|
||||
|
||||
const httpsServer = createServer({
|
||||
key,
|
||||
cert
|
||||
});
|
||||
|
||||
httpsServer.listen(3000);
|
||||
|
||||
const io = new Server(httpsServer, {
|
||||
transports: ["polling", "websocket", "webtransport"] // WebTransport is not enabled by default
|
||||
});
|
||||
|
||||
const h3Server = new Http3Server({
|
||||
port: 3000,
|
||||
host: "0.0.0.0",
|
||||
secret: "changeit",
|
||||
cert,
|
||||
privKey: key,
|
||||
});
|
||||
|
||||
(async () => {
|
||||
const stream = await h3Server.sessionStream("/engine.io/");
|
||||
const sessionReader = stream.getReader();
|
||||
|
||||
while (true) {
|
||||
const { done, value } = await sessionReader.read();
|
||||
if (done) {
|
||||
break;
|
||||
}
|
||||
io.engine.onWebTransportSession(value);
|
||||
}
|
||||
})();
|
||||
|
||||
h3Server.startServer();
|
||||
```
|
||||
|
||||
Added in [123b68c](https://github.com/socketio/engine.io/commit/123b68c04f9e971f59b526e0f967a488ee6b0116).
|
||||
|
||||
|
||||
#### Client bundles with CORS headers
|
||||
|
||||
The bundles will now have the right `Access-Control-Allow-xxx` headers.
|
||||
|
||||
Added in [63f181c](https://github.com/socketio/socket.io/commit/63f181cc12cbbbf94ed40eef52d60f36a1214fbe).
|
||||
|
||||
|
||||
### Dependencies
|
||||
|
||||
- [`engine.io@~6.4.2`](https://github.com/socketio/engine.io/releases/tag/6.5.0) ([diff](https://github.com/socketio/engine.io/compare/6.4.2...6.5.0))
|
||||
- [`ws@~8.11.0`](https://github.com/websockets/ws/releases/tag/8.11.0) (no change)
|
||||
|
||||
|
||||
|
||||
## [4.6.2](https://github.com/socketio/socket.io/compare/4.6.1...4.6.2) (2023-05-31)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* **exports:** move `types` condition to the top ([#4698](https://github.com/socketio/socket.io/issues/4698)) ([3d44aae](https://github.com/socketio/socket.io/commit/3d44aae381af38349fdb808d510d9f47a0c2507e))
|
||||
|
||||
|
||||
### Dependencies
|
||||
|
||||
- [`engine.io@~6.4.2`](https://github.com/socketio/engine.io/releases/tag/6.4.0) ([diff](https://github.com/socketio/engine.io/compare/6.4.1...6.4.2))
|
||||
- [`ws@~8.11.0`](https://github.com/websockets/ws/releases/tag/8.11.0) (no change)
|
||||
|
||||
|
||||
|
||||
## [4.6.1](https://github.com/socketio/socket.io/compare/4.6.0...4.6.1) (2023-02-20)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* properly handle manually created dynamic namespaces ([0d0a7a2](https://github.com/socketio/socket.io/commit/0d0a7a22b5ff95f864216c529114b7dd41738d1e))
|
||||
* **types:** fix nodenext module resolution compatibility ([#4625](https://github.com/socketio/socket.io/issues/4625)) ([d0b22c6](https://github.com/socketio/socket.io/commit/d0b22c630208669aceb7ae013180c99ef90279b0))
|
||||
|
||||
|
||||
### Dependencies
|
||||
|
||||
- [`engine.io@~6.4.1`](https://github.com/socketio/engine.io/releases/tag/6.4.1) ([diff](https://github.com/socketio/engine.io/compare/6.4.0...6.4.1))
|
||||
- [`ws@~8.11.0`](https://github.com/websockets/ws/releases/tag/8.11.0) (no change)
|
||||
|
||||
|
||||
|
||||
## [4.6.0](https://github.com/socketio/socket.io/compare/4.5.4...4.6.0) (2023-02-07)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
@@ -233,8 +353,8 @@ Added in [d0fd474](https://github.com/socketio/engine.io/commit/d0fd4746afa39629
|
||||
|
||||
### Dependencies
|
||||
|
||||
- [`engine.io@~6.4.0`](https://github.com/socketio/engine.io/releases/tag/6.4.0) ([diff](https://github.com/socketio/engine.io/compare/6.2.0...6.2.1))
|
||||
- [`ws@~8.11.0`](https://github.com/websockets/ws/releases/tag/8.11.0) ([diff](https://github.com/websockets/ws/compare/8.2.3...8.11.0))
|
||||
- [`engine.io@~6.4.0`](https://github.com/socketio/engine.io/releases/tag/6.4.0) (https://github.com/socketio/engine.io/compare/6.2.1...6.4.0)
|
||||
- [`ws@~8.11.0`](https://github.com/websockets/ws/releases/tag/8.11.0) (https://github.com/websockets/ws/compare/8.2.3...8.11.0)
|
||||
|
||||
|
||||
## [4.5.4](https://github.com/socketio/socket.io/compare/4.5.3...4.5.4) (2022-11-22)
|
||||
@@ -259,6 +379,11 @@ This release contains a bump of:
|
||||
* **typings:** accept an HTTP2 server in the constructor ([d3d0a2d](https://github.com/socketio/socket.io/commit/d3d0a2d5beaff51fd145f810bcaf6914213f8a06))
|
||||
* **typings:** apply types to "io.timeout(...).emit()" calls ([e357daf](https://github.com/socketio/socket.io/commit/e357daf5858560bc84e7e50cd36f0278d6721ea1))
|
||||
|
||||
### Dependencies
|
||||
|
||||
- [`engine.io@~6.2.0`](https://github.com/socketio/engine.io/releases/tag/6.2.1) (no change)
|
||||
- [`ws@~8.2.3`](https://github.com/websockets/ws/releases/tag/8.2.3) (no change)
|
||||
|
||||
|
||||
|
||||
## [4.5.2](https://github.com/socketio/socket.io/compare/4.5.1...4.5.2) (2022-09-02)
|
||||
@@ -269,13 +394,18 @@ This release contains a bump of:
|
||||
* prevent the socket from joining a room after disconnection ([18f3fda](https://github.com/socketio/socket.io/commit/18f3fdab12947a9fee3e9c37cfc1da97027d1473))
|
||||
* **uws:** prevent the server from crashing after upgrade ([ba497ee](https://github.com/socketio/socket.io/commit/ba497ee3eb52c4abf1464380d015d8c788714364))
|
||||
|
||||
### Dependencies
|
||||
|
||||
- [`engine.io@~6.2.0`](https://github.com/socketio/engine.io/releases/tag/6.2.0) (no change)
|
||||
- [`ws@~8.2.3`](https://github.com/websockets/ws/releases/tag/8.2.3) (no change)
|
||||
|
||||
|
||||
|
||||
# [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.
|
||||
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)
|
||||
|
||||
@@ -287,6 +417,11 @@ Security advisory: [GHSA-j4f2-536g-r55m](https://github.com/advisories/GHSA-j4f2
|
||||
* 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))
|
||||
|
||||
### Dependencies
|
||||
|
||||
- [`engine.io@~3.6.0`](https://github.com/socketio/engine.io/releases/tag/3.6.0) (https://github.com/socketio/engine.io/compare/3.5.0...3.6.0)
|
||||
- [`ws@~7.4.2`](https://github.com/websockets/ws/releases/tag/7.4.2) (no change)
|
||||
|
||||
|
||||
|
||||
## [4.5.1](https://github.com/socketio/socket.io/compare/4.5.0...4.5.1) (2022-05-17)
|
||||
@@ -297,6 +432,11 @@ Security advisory: [GHSA-j4f2-536g-r55m](https://github.com/advisories/GHSA-j4f2
|
||||
* forward the local flag to the adapter when using fetchSockets() ([30430f0](https://github.com/socketio/socket.io/commit/30430f0985f8e7c49394543d4c84913b6a15df60))
|
||||
* **typings:** add HTTPS server to accepted types ([#4351](https://github.com/socketio/socket.io/issues/4351)) ([9b43c91](https://github.com/socketio/socket.io/commit/9b43c9167cff817c60fa29dbda2ef7cd938aff51))
|
||||
|
||||
### Dependencies
|
||||
|
||||
- [`engine.io@~6.2.0`](https://github.com/socketio/engine.io/releases/tag/6.2.0) (no change)
|
||||
- [`ws@~8.2.3`](https://github.com/websockets/ws/releases/tag/8.2.3) (no change)
|
||||
|
||||
|
||||
|
||||
# [4.5.0](https://github.com/socketio/socket.io/compare/4.4.1...4.5.0) (2022-04-23)
|
||||
@@ -309,7 +449,7 @@ Security advisory: [GHSA-j4f2-536g-r55m](https://github.com/advisories/GHSA-j4f2
|
||||
|
||||
### Features
|
||||
|
||||
* add support for catch-all listeners for outgoing packets ([531104d](https://github.com/socketio/socket.io/commit/531104d332690138b7aab84d5583d6204132c8b4))
|
||||
#### Catch-all listeners for outgoing packets
|
||||
|
||||
This is similar to `onAny()`, but for outgoing packets.
|
||||
|
||||
@@ -321,7 +461,9 @@ socket.onAnyOutgoing((event, ...args) => {
|
||||
});
|
||||
```
|
||||
|
||||
* broadcast and expect multiple acks ([8b20457](https://github.com/socketio/socket.io/commit/8b204570a94979bbec307f23ca078f30f5cf07b0))
|
||||
Added in [531104d](https://github.com/socketio/socket.io/commit/531104d332690138b7aab84d5583d6204132c8b4).
|
||||
|
||||
#### Broadcast and expect multiple acknowledgements
|
||||
|
||||
Syntax:
|
||||
|
||||
@@ -331,18 +473,25 @@ io.timeout(1000).emit("some-event", (err, responses) => {
|
||||
});
|
||||
```
|
||||
|
||||
* add the "maxPayload" field in the handshake details ([088dcb4](https://github.com/socketio/engine.io/commit/088dcb4dff60df39785df13d0a33d3ceaa1dff38))
|
||||
Added in [8b20457](https://github.com/socketio/socket.io/commit/8b204570a94979bbec307f23ca078f30f5cf07b0).
|
||||
|
||||
So that clients in HTTP long-polling can decide how many packets they have to send to stay under the maxHttpBufferSize
|
||||
value.
|
||||
#### `maxHttpBufferSize` value negotiation
|
||||
|
||||
This is a backward compatible change which should not mandate a new major revision of the protocol (we stay in v4), as
|
||||
we only add a field in the JSON-encoded handshake data:
|
||||
A "maxPayload" field is now included in the Engine.IO handshake, so that clients in HTTP long-polling can decide how many packets they have to send to stay under the `maxHttpBufferSize` value.
|
||||
|
||||
This is a backward compatible change which should not mandate a new major revision of the protocol (we stay in v4), as we only add a field in the JSON-encoded handshake data:
|
||||
|
||||
```
|
||||
0{"sid":"lv_VI97HAXpY6yYWAAAC","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":5000,"maxPayload":1000000}
|
||||
```
|
||||
|
||||
Added in [088dcb4](https://github.com/socketio/engine.io/commit/088dcb4dff60df39785df13d0a33d3ceaa1dff38).
|
||||
|
||||
### Dependencies
|
||||
|
||||
- [`engine.io@~6.2.0`](https://github.com/socketio/engine.io/releases/tag/6.2.0) (https://github.com/socketio/engine.io/compare/6.1.0...6.2.0)
|
||||
- [`ws@~8.2.3`](https://github.com/websockets/ws/releases/tag/8.2.3) (no change)
|
||||
|
||||
|
||||
|
||||
## [4.4.1](https://github.com/socketio/socket.io/compare/4.4.0...4.4.1) (2022-01-06)
|
||||
|
||||
@@ -126,7 +126,7 @@ io.listen(3000);
|
||||
|
||||
Starting with **3.0**, express applications have become request handler
|
||||
functions that you pass to `http` or `http` `Server` instances. You need
|
||||
to pass the `Server` to `socket.io`, and not the express application
|
||||
to pass the `Server` to `socket.io`, not the express application
|
||||
function. Also make sure to call `.listen` on the `server`, not the `app`.
|
||||
|
||||
```js
|
||||
|
||||
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.6.0
|
||||
* Socket.IO v4.7.0
|
||||
* (c) 2014-2023 Guillermo Rauch
|
||||
* Released under the MIT License.
|
||||
*/
|
||||
@@ -355,12 +355,40 @@
|
||||
|
||||
fileReader.onload = function () {
|
||||
var content = fileReader.result.split(",")[1];
|
||||
callback("b" + content);
|
||||
callback("b" + (content || ""));
|
||||
};
|
||||
|
||||
return fileReader.readAsDataURL(data);
|
||||
};
|
||||
|
||||
function toArray(data) {
|
||||
if (data instanceof Uint8Array) {
|
||||
return data;
|
||||
} else if (data instanceof ArrayBuffer) {
|
||||
return new Uint8Array(data);
|
||||
} else {
|
||||
return new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
|
||||
}
|
||||
}
|
||||
|
||||
var TEXT_ENCODER;
|
||||
function encodePacketToBinary(packet, callback) {
|
||||
if (withNativeBlob$1 && packet.data instanceof Blob) {
|
||||
return packet.data.arrayBuffer().then(toArray).then(callback);
|
||||
} else if (withNativeArrayBuffer$2 && (packet.data instanceof ArrayBuffer || isView$1(packet.data))) {
|
||||
return callback(toArray(packet.data));
|
||||
}
|
||||
|
||||
encodePacket(packet, false, function (encoded) {
|
||||
if (!TEXT_ENCODER) {
|
||||
TEXT_ENCODER = new TextEncoder();
|
||||
}
|
||||
|
||||
callback(TEXT_ENCODER.encode(encoded));
|
||||
});
|
||||
}
|
||||
|
||||
// imported from https://github.com/socketio/base64-arraybuffer
|
||||
var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; // Use a lookup table to find the index.
|
||||
|
||||
var lookup$1 = typeof Uint8Array === 'undefined' ? [] : new Uint8Array(256);
|
||||
@@ -403,7 +431,6 @@
|
||||
};
|
||||
|
||||
var withNativeArrayBuffer$1 = typeof ArrayBuffer === "function";
|
||||
|
||||
var decodePacket = function decodePacket(encodedPacket, binaryType) {
|
||||
if (typeof encodedPacket !== "string") {
|
||||
return {
|
||||
@@ -450,12 +477,24 @@
|
||||
var mapBinary = function mapBinary(data, binaryType) {
|
||||
switch (binaryType) {
|
||||
case "blob":
|
||||
return data instanceof ArrayBuffer ? new Blob([data]) : data;
|
||||
if (data instanceof Blob) {
|
||||
// from WebSocket + binaryType "blob"
|
||||
return data;
|
||||
} else {
|
||||
// from HTTP long-polling or WebTransport
|
||||
return new Blob([data]);
|
||||
}
|
||||
|
||||
case "arraybuffer":
|
||||
default:
|
||||
return data;
|
||||
// assuming the data is already an ArrayBuffer
|
||||
if (data instanceof ArrayBuffer) {
|
||||
// from HTTP long-polling (base64) or WebSocket + binaryType "arraybuffer"
|
||||
return data;
|
||||
} else {
|
||||
// from WebTransport (Uint8Array)
|
||||
return data.buffer;
|
||||
}
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
@@ -494,6 +533,18 @@
|
||||
return packets;
|
||||
};
|
||||
|
||||
var TEXT_DECODER;
|
||||
function decodePacketFromBinary(data, isBinary, binaryType) {
|
||||
if (!TEXT_DECODER) {
|
||||
// lazily created for compatibility with old browser platforms
|
||||
TEXT_DECODER = new TextDecoder();
|
||||
} // 48 === "0".charCodeAt(0) (OPEN packet type)
|
||||
// 54 === "6".charCodeAt(0) (NOOP packet type)
|
||||
|
||||
|
||||
var isPlainBinary = isBinary || data[0] < 48 || data[0] > 54;
|
||||
return decodePacket(isPlainBinary ? data : TEXT_DECODER.decode(data), binaryType);
|
||||
}
|
||||
var protocol$1 = 4;
|
||||
|
||||
/**
|
||||
@@ -728,6 +779,46 @@
|
||||
return length;
|
||||
}
|
||||
|
||||
// imported from https://github.com/galkn/querystring
|
||||
|
||||
/**
|
||||
* Compiles a querystring
|
||||
* Returns string representation of the object
|
||||
*
|
||||
* @param {Object}
|
||||
* @api private
|
||||
*/
|
||||
function encode$1(obj) {
|
||||
var str = '';
|
||||
|
||||
for (var i in obj) {
|
||||
if (obj.hasOwnProperty(i)) {
|
||||
if (str.length) str += '&';
|
||||
str += encodeURIComponent(i) + '=' + encodeURIComponent(obj[i]);
|
||||
}
|
||||
}
|
||||
|
||||
return str;
|
||||
}
|
||||
/**
|
||||
* Parses a simple querystring into an object
|
||||
*
|
||||
* @param {String} qs
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function decode(qs) {
|
||||
var qry = {};
|
||||
var pairs = qs.split('&');
|
||||
|
||||
for (var i = 0, l = pairs.length; i < l; i++) {
|
||||
var pair = pairs[i].split('=');
|
||||
qry[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1]);
|
||||
}
|
||||
|
||||
return qry;
|
||||
}
|
||||
|
||||
var TransportError = /*#__PURE__*/function (_Error) {
|
||||
_inherits(TransportError, _Error);
|
||||
|
||||
@@ -888,6 +979,33 @@
|
||||
}, {
|
||||
key: "pause",
|
||||
value: function pause(onPause) {}
|
||||
}, {
|
||||
key: "createUri",
|
||||
value: function createUri(schema) {
|
||||
var query = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
||||
return schema + "://" + this._hostname() + this._port() + this.opts.path + this._query(query);
|
||||
}
|
||||
}, {
|
||||
key: "_hostname",
|
||||
value: function _hostname() {
|
||||
var hostname = this.opts.hostname;
|
||||
return hostname.indexOf(":") === -1 ? hostname : "[" + hostname + "]";
|
||||
}
|
||||
}, {
|
||||
key: "_port",
|
||||
value: function _port() {
|
||||
if (this.opts.port && (this.opts.secure && Number(this.opts.port !== 443) || !this.opts.secure && Number(this.opts.port) !== 80)) {
|
||||
return ":" + this.opts.port;
|
||||
} else {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
}, {
|
||||
key: "_query",
|
||||
value: function _query(query) {
|
||||
var encodedQuery = encode$1(query);
|
||||
return encodedQuery.length ? "?" + encodedQuery : "";
|
||||
}
|
||||
}]);
|
||||
|
||||
return Transport;
|
||||
@@ -909,7 +1027,7 @@
|
||||
* @api public
|
||||
*/
|
||||
|
||||
function encode$1(num) {
|
||||
function encode(num) {
|
||||
var encoded = '';
|
||||
|
||||
do {
|
||||
@@ -927,9 +1045,9 @@
|
||||
*/
|
||||
|
||||
function yeast() {
|
||||
var now = encode$1(+new Date());
|
||||
var now = encode(+new Date());
|
||||
if (now !== prev) return seed = 0, prev = now;
|
||||
return now + '.' + encode$1(seed++);
|
||||
return now + '.' + encode(seed++);
|
||||
} //
|
||||
// Map each character to its index.
|
||||
//
|
||||
@@ -938,46 +1056,6 @@
|
||||
map[alphabet[i]] = i;
|
||||
}
|
||||
|
||||
// imported from https://github.com/galkn/querystring
|
||||
|
||||
/**
|
||||
* Compiles a querystring
|
||||
* Returns string representation of the object
|
||||
*
|
||||
* @param {Object}
|
||||
* @api private
|
||||
*/
|
||||
function encode(obj) {
|
||||
var str = '';
|
||||
|
||||
for (var i in obj) {
|
||||
if (obj.hasOwnProperty(i)) {
|
||||
if (str.length) str += '&';
|
||||
str += encodeURIComponent(i) + '=' + encodeURIComponent(obj[i]);
|
||||
}
|
||||
}
|
||||
|
||||
return str;
|
||||
}
|
||||
/**
|
||||
* Parses a simple querystring into an object
|
||||
*
|
||||
* @param {String} qs
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function decode(qs) {
|
||||
var qry = {};
|
||||
var pairs = qs.split('&');
|
||||
|
||||
for (var i = 0, l = pairs.length; i < l; i++) {
|
||||
var pair = pairs[i].split('=');
|
||||
qry[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1]);
|
||||
}
|
||||
|
||||
return qry;
|
||||
}
|
||||
|
||||
// imported from https://github.com/component/has-cors
|
||||
var value = false;
|
||||
|
||||
@@ -1005,6 +1083,7 @@
|
||||
} catch (e) {}
|
||||
}
|
||||
}
|
||||
function createCookieJar() {}
|
||||
|
||||
function empty() {}
|
||||
|
||||
@@ -1043,7 +1122,6 @@
|
||||
}
|
||||
|
||||
_this.xd = typeof location !== "undefined" && opts.hostname !== location.hostname || port !== opts.port;
|
||||
_this.xs = opts.secure !== isSSL;
|
||||
}
|
||||
/**
|
||||
* XHR supports binary
|
||||
@@ -1052,6 +1130,11 @@
|
||||
|
||||
var forceBase64 = opts && opts.forceBase64;
|
||||
_this.supportsBinary = hasXHR2 && !forceBase64;
|
||||
|
||||
if (_this.opts.withCredentials) {
|
||||
_this.cookieJar = createCookieJar();
|
||||
}
|
||||
|
||||
return _this;
|
||||
}
|
||||
|
||||
@@ -1222,9 +1305,8 @@
|
||||
}, {
|
||||
key: "uri",
|
||||
value: function uri() {
|
||||
var query = this.query || {};
|
||||
var schema = this.opts.secure ? "https" : "http";
|
||||
var port = ""; // cache busting is forced
|
||||
var query = this.query || {}; // cache busting is forced
|
||||
|
||||
if (false !== this.opts.timestampRequests) {
|
||||
query[this.opts.timestampParam] = yeast();
|
||||
@@ -1232,16 +1314,9 @@
|
||||
|
||||
if (!this.supportsBinary && !query.sid) {
|
||||
query.b64 = 1;
|
||||
} // avoid port if default for schema
|
||||
|
||||
|
||||
if (this.opts.port && ("https" === schema && Number(this.opts.port) !== 443 || "http" === schema && Number(this.opts.port) !== 80)) {
|
||||
port = ":" + this.opts.port;
|
||||
}
|
||||
|
||||
var encodedQuery = encode(query);
|
||||
var ipv6 = this.opts.hostname.indexOf(":") !== -1;
|
||||
return schema + "://" + (ipv6 ? "[" + this.opts.hostname + "]" : this.opts.hostname) + port + this.opts.path + (encodedQuery.length ? "?" + encodedQuery : "");
|
||||
return this.createUri(schema, query);
|
||||
}
|
||||
/**
|
||||
* Creates a request.
|
||||
@@ -1257,7 +1332,7 @@
|
||||
|
||||
_extends(opts, {
|
||||
xd: this.xd,
|
||||
xs: this.xs
|
||||
cookieJar: this.cookieJar
|
||||
}, this.opts);
|
||||
|
||||
return new Request(this.uri(), opts);
|
||||
@@ -1327,7 +1402,6 @@
|
||||
_this8.opts = opts;
|
||||
_this8.method = opts.method || "GET";
|
||||
_this8.uri = uri;
|
||||
_this8.async = false !== opts.async;
|
||||
_this8.data = undefined !== opts.data ? opts.data : null;
|
||||
|
||||
_this8.create();
|
||||
@@ -1346,13 +1420,14 @@
|
||||
value: function create() {
|
||||
var _this9 = this;
|
||||
|
||||
var _a;
|
||||
|
||||
var opts = pick(this.opts, "agent", "pfx", "key", "passphrase", "cert", "ca", "ciphers", "rejectUnauthorized", "autoUnref");
|
||||
opts.xdomain = !!this.opts.xd;
|
||||
opts.xscheme = !!this.opts.xs;
|
||||
var xhr = this.xhr = new XHR(opts);
|
||||
|
||||
try {
|
||||
xhr.open(this.method, this.uri, this.async);
|
||||
xhr.open(this.method, this.uri, true);
|
||||
|
||||
try {
|
||||
if (this.opts.extraHeaders) {
|
||||
@@ -1374,8 +1449,9 @@
|
||||
|
||||
try {
|
||||
xhr.setRequestHeader("Accept", "*/*");
|
||||
} catch (e) {} // ie6 check
|
||||
} catch (e) {}
|
||||
|
||||
(_a = this.opts.cookieJar) === null || _a === void 0 ? void 0 : _a.addCookies(xhr); // ie6 check
|
||||
|
||||
if ("withCredentials" in xhr) {
|
||||
xhr.withCredentials = this.opts.withCredentials;
|
||||
@@ -1386,6 +1462,12 @@
|
||||
}
|
||||
|
||||
xhr.onreadystatechange = function () {
|
||||
var _a;
|
||||
|
||||
if (xhr.readyState === 3) {
|
||||
(_a = _this9.opts.cookieJar) === null || _a === void 0 ? void 0 : _a.parseCookies(xhr);
|
||||
}
|
||||
|
||||
if (4 !== xhr.readyState) return;
|
||||
|
||||
if (200 === xhr.status || 1223 === xhr.status) {
|
||||
@@ -1675,14 +1757,8 @@
|
||||
}, {
|
||||
key: "uri",
|
||||
value: function uri() {
|
||||
var query = this.query || {};
|
||||
var schema = this.opts.secure ? "wss" : "ws";
|
||||
var port = ""; // avoid port if default for schema
|
||||
|
||||
if (this.opts.port && ("wss" === schema && Number(this.opts.port) !== 443 || "ws" === schema && Number(this.opts.port) !== 80)) {
|
||||
port = ":" + this.opts.port;
|
||||
} // append timestamp to URI
|
||||
|
||||
var query = this.query || {}; // append timestamp to URI
|
||||
|
||||
if (this.opts.timestampRequests) {
|
||||
query[this.opts.timestampParam] = yeast();
|
||||
@@ -1693,9 +1769,7 @@
|
||||
query.b64 = 1;
|
||||
}
|
||||
|
||||
var encodedQuery = encode(query);
|
||||
var ipv6 = this.opts.hostname.indexOf(":") !== -1;
|
||||
return schema + "://" + (ipv6 ? "[" + this.opts.hostname + "]" : this.opts.hostname) + port + this.opts.path + (encodedQuery.length ? "?" + encodedQuery : "");
|
||||
return this.createUri(schema, query);
|
||||
}
|
||||
/**
|
||||
* Feature detection for WebSocket.
|
||||
@@ -1714,8 +1788,127 @@
|
||||
return WS;
|
||||
}(Transport);
|
||||
|
||||
function shouldIncludeBinaryHeader(packet, encoded) {
|
||||
// 48 === "0".charCodeAt(0) (OPEN packet type)
|
||||
// 54 === "6".charCodeAt(0) (NOOP packet type)
|
||||
return packet.type === "message" && typeof packet.data !== "string" && encoded[0] >= 48 && encoded[0] <= 54;
|
||||
}
|
||||
|
||||
var WT = /*#__PURE__*/function (_Transport) {
|
||||
_inherits(WT, _Transport);
|
||||
|
||||
var _super = _createSuper(WT);
|
||||
|
||||
function WT() {
|
||||
_classCallCheck(this, WT);
|
||||
|
||||
return _super.apply(this, arguments);
|
||||
}
|
||||
|
||||
_createClass(WT, [{
|
||||
key: "name",
|
||||
get: function get() {
|
||||
return "webtransport";
|
||||
}
|
||||
}, {
|
||||
key: "doOpen",
|
||||
value: function doOpen() {
|
||||
var _this = this;
|
||||
|
||||
// @ts-ignore
|
||||
if (typeof WebTransport !== "function") {
|
||||
return;
|
||||
} // @ts-ignore
|
||||
|
||||
|
||||
this.transport = new WebTransport(this.createUri("https"), this.opts.transportOptions[this.name]);
|
||||
this.transport.closed.then(function () {
|
||||
return _this.onClose();
|
||||
}); // note: we could have used async/await, but that would require some additional polyfills
|
||||
|
||||
this.transport.ready.then(function () {
|
||||
_this.transport.createBidirectionalStream().then(function (stream) {
|
||||
var reader = stream.readable.getReader();
|
||||
_this.writer = stream.writable.getWriter();
|
||||
var binaryFlag;
|
||||
|
||||
var read = function read() {
|
||||
reader.read().then(function (_ref) {
|
||||
var done = _ref.done,
|
||||
value = _ref.value;
|
||||
|
||||
if (done) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!binaryFlag && value.byteLength === 1 && value[0] === 54) {
|
||||
binaryFlag = true;
|
||||
} else {
|
||||
// TODO expose binarytype
|
||||
_this.onPacket(decodePacketFromBinary(value, binaryFlag, "arraybuffer"));
|
||||
|
||||
binaryFlag = false;
|
||||
}
|
||||
|
||||
read();
|
||||
});
|
||||
};
|
||||
|
||||
read();
|
||||
var handshake = _this.query.sid ? "0{\"sid\":\"".concat(_this.query.sid, "\"}") : "0";
|
||||
|
||||
_this.writer.write(new TextEncoder().encode(handshake)).then(function () {
|
||||
return _this.onOpen();
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
}, {
|
||||
key: "write",
|
||||
value: function write(packets) {
|
||||
var _this2 = this;
|
||||
|
||||
this.writable = false;
|
||||
|
||||
var _loop = function _loop(i) {
|
||||
var packet = packets[i];
|
||||
var lastPacket = i === packets.length - 1;
|
||||
encodePacketToBinary(packet, function (data) {
|
||||
if (shouldIncludeBinaryHeader(packet, data)) {
|
||||
_this2.writer.write(Uint8Array.of(54));
|
||||
}
|
||||
|
||||
_this2.writer.write(data).then(function () {
|
||||
if (lastPacket) {
|
||||
nextTick(function () {
|
||||
_this2.writable = true;
|
||||
|
||||
_this2.emitReserved("drain");
|
||||
}, _this2.setTimeoutFn);
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
for (var i = 0; i < packets.length; i++) {
|
||||
_loop(i);
|
||||
}
|
||||
}
|
||||
}, {
|
||||
key: "doClose",
|
||||
value: function doClose() {
|
||||
var _a;
|
||||
|
||||
(_a = this.transport) === null || _a === void 0 ? void 0 : _a.close();
|
||||
}
|
||||
}]);
|
||||
|
||||
return WT;
|
||||
}(Transport);
|
||||
|
||||
var transports = {
|
||||
websocket: WS,
|
||||
webtransport: WT,
|
||||
polling: Polling
|
||||
};
|
||||
|
||||
@@ -1841,7 +2034,7 @@
|
||||
|
||||
_this.hostname = opts.hostname || (typeof location !== "undefined" ? location.hostname : "localhost");
|
||||
_this.port = opts.port || (typeof location !== "undefined" && location.port ? location.port : _this.secure ? "443" : "80");
|
||||
_this.transports = opts.transports || ["polling", "websocket"];
|
||||
_this.transports = opts.transports || ["polling", "websocket", "webtransport"];
|
||||
_this.writeBuffer = [];
|
||||
_this.prevBufferLen = 0;
|
||||
_this.opts = _extends({
|
||||
@@ -2108,7 +2301,17 @@
|
||||
transport.once("close", onTransportClose);
|
||||
this.once("close", onclose);
|
||||
this.once("upgrading", onupgrade);
|
||||
transport.open();
|
||||
|
||||
if (this.upgrades.indexOf("webtransport") !== -1 && name !== "webtransport") {
|
||||
// favor WebTransport
|
||||
this.setTimeoutFn(function () {
|
||||
if (!failed) {
|
||||
transport.open();
|
||||
}
|
||||
}, 200);
|
||||
} else {
|
||||
transport.open();
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Called when connection is deemed open.
|
||||
@@ -2680,6 +2883,12 @@
|
||||
return data;
|
||||
}
|
||||
|
||||
/**
|
||||
* These strings must not be used as event names, as they have a special meaning.
|
||||
*/
|
||||
|
||||
var RESERVED_EVENTS$1 = ["connect", "connect_error", "disconnect", "disconnecting", "newListener", "removeListener" // used by the Node.js EventEmitter
|
||||
];
|
||||
/**
|
||||
* Protocol version.
|
||||
*
|
||||
@@ -2789,13 +2998,18 @@
|
||||
}]);
|
||||
|
||||
return Encoder;
|
||||
}();
|
||||
}(); // see https://stackoverflow.com/questions/8511281/check-if-a-value-is-an-object-in-javascript
|
||||
|
||||
function isObject(value) {
|
||||
return Object.prototype.toString.call(value) === "[object Object]";
|
||||
}
|
||||
/**
|
||||
* A socket.io Decoder instance
|
||||
*
|
||||
* @return {Object} decoder
|
||||
*/
|
||||
|
||||
|
||||
var Decoder = /*#__PURE__*/function (_Emitter) {
|
||||
_inherits(Decoder, _Emitter);
|
||||
|
||||
@@ -2974,17 +3188,17 @@
|
||||
value: function isPayloadValid(type, payload) {
|
||||
switch (type) {
|
||||
case PacketType.CONNECT:
|
||||
return _typeof(payload) === "object";
|
||||
return isObject(payload);
|
||||
|
||||
case PacketType.DISCONNECT:
|
||||
return payload === undefined;
|
||||
|
||||
case PacketType.CONNECT_ERROR:
|
||||
return typeof payload === "string" || _typeof(payload) === "object";
|
||||
return typeof payload === "string" || isObject(payload);
|
||||
|
||||
case PacketType.EVENT:
|
||||
case PacketType.BINARY_EVENT:
|
||||
return Array.isArray(payload) && payload.length > 0;
|
||||
return Array.isArray(payload) && (typeof payload[0] === "number" || typeof payload[0] === "string" && RESERVED_EVENTS$1.indexOf(payload[0]) === -1);
|
||||
|
||||
case PacketType.ACK:
|
||||
case PacketType.BINARY_ACK:
|
||||
@@ -3159,6 +3373,12 @@
|
||||
*/
|
||||
|
||||
_this._queue = [];
|
||||
/**
|
||||
* A sequence to generate the ID of the {@link QueuedPacket}.
|
||||
* @private
|
||||
*/
|
||||
|
||||
_this._queueSeq = 0;
|
||||
_this.ids = 0;
|
||||
_this.acks = {};
|
||||
_this.flags = {};
|
||||
@@ -3453,7 +3673,7 @@
|
||||
}
|
||||
|
||||
var packet = {
|
||||
id: this.ids++,
|
||||
id: this._queueSeq++,
|
||||
tryCount: 0,
|
||||
pending: false,
|
||||
args: args,
|
||||
@@ -3499,30 +3719,30 @@
|
||||
}
|
||||
/**
|
||||
* Send the first packet of the queue, and wait for an acknowledgement from the server.
|
||||
* @param force - whether to resend a packet that has not been acknowledged yet
|
||||
*
|
||||
* @private
|
||||
*/
|
||||
|
||||
}, {
|
||||
key: "_drainQueue",
|
||||
value: function _drainQueue() {
|
||||
if (this._queue.length === 0) {
|
||||
var force = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
|
||||
|
||||
if (!this.connected || this._queue.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
var packet = this._queue[0];
|
||||
|
||||
if (packet.pending) {
|
||||
if (packet.pending && !force) {
|
||||
return;
|
||||
}
|
||||
|
||||
packet.pending = true;
|
||||
packet.tryCount++;
|
||||
var currentId = this.ids;
|
||||
this.ids = packet.id; // the same id is reused for consecutive retries, in order to allow deduplication on the server side
|
||||
|
||||
this.flags = packet.flags;
|
||||
this.emit.apply(this, packet.args);
|
||||
this.ids = currentId; // restore offset
|
||||
}
|
||||
/**
|
||||
* Sends a packet.
|
||||
@@ -3759,6 +3979,8 @@
|
||||
this.connected = true;
|
||||
this.emitBuffered();
|
||||
this.emitReserved("connect");
|
||||
|
||||
this._drainQueue(true);
|
||||
}
|
||||
/**
|
||||
* Emit buffered events (received and emitted).
|
||||
@@ -4362,11 +4584,12 @@
|
||||
var openSubDestroy = on(socket, "open", function () {
|
||||
self.onopen();
|
||||
fn && fn();
|
||||
}); // emit `error`
|
||||
});
|
||||
|
||||
var errorSub = on(socket, "error", function (err) {
|
||||
self.cleanup();
|
||||
self._readyState = "closed";
|
||||
var onError = function onError(err) {
|
||||
_this2.cleanup();
|
||||
|
||||
_this2._readyState = "closed";
|
||||
|
||||
_this2.emitReserved("error", err);
|
||||
|
||||
@@ -4374,31 +4597,28 @@
|
||||
fn(err);
|
||||
} else {
|
||||
// Only do this if there is no fn to handle the error
|
||||
self.maybeReconnectOnOpen();
|
||||
_this2.maybeReconnectOnOpen();
|
||||
}
|
||||
});
|
||||
}; // emit `error`
|
||||
|
||||
|
||||
var errorSub = on(socket, "error", onError);
|
||||
|
||||
if (false !== this._timeout) {
|
||||
var timeout = this._timeout;
|
||||
|
||||
if (timeout === 0) {
|
||||
openSubDestroy(); // prevents a race condition with the 'open' event
|
||||
} // set timer
|
||||
|
||||
var timeout = this._timeout; // set timer
|
||||
|
||||
var timer = this.setTimeoutFn(function () {
|
||||
openSubDestroy();
|
||||
socket.close(); // @ts-ignore
|
||||
|
||||
socket.emit("error", new Error("timeout"));
|
||||
onError(new Error("timeout"));
|
||||
socket.close();
|
||||
}, timeout);
|
||||
|
||||
if (this.opts.autoUnref) {
|
||||
timer.unref();
|
||||
}
|
||||
|
||||
this.subs.push(function subDestroy() {
|
||||
clearTimeout(timer);
|
||||
this.subs.push(function () {
|
||||
_this2.clearTimeoutFn(timer);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -4504,9 +4724,7 @@
|
||||
if (!socket) {
|
||||
socket = new Socket(this, nsp, opts);
|
||||
this.nsps[nsp] = socket;
|
||||
}
|
||||
|
||||
if (this._autoConnect) {
|
||||
} else if (this._autoConnect && !socket.active) {
|
||||
socket.connect();
|
||||
}
|
||||
|
||||
@@ -4653,8 +4871,8 @@
|
||||
timer.unref();
|
||||
}
|
||||
|
||||
this.subs.push(function subDestroy() {
|
||||
clearTimeout(timer);
|
||||
this.subs.push(function () {
|
||||
_this4.clearTimeoutFn(timer);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
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
4
client-dist/socket.io.msgpack.min.js
vendored
4
client-dist/socket.io.msgpack.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
18
examples/basic-websocket-client/README.md
Normal file
18
examples/basic-websocket-client/README.md
Normal file
@@ -0,0 +1,18 @@
|
||||
# Basic Socket.IO client
|
||||
|
||||
Please check the associated guide: https://socket.io/how-to/build-a-basic-client
|
||||
|
||||
Content:
|
||||
|
||||
```
|
||||
├── bundle
|
||||
│ └── socket.io.min.js
|
||||
├── src
|
||||
│ └── index.js
|
||||
├── test
|
||||
│ └── index.js
|
||||
├── check-bundle-size.js
|
||||
├── package.json
|
||||
├── README.md
|
||||
└── rollup.config.js
|
||||
```
|
||||
1
examples/basic-websocket-client/bundle/socket.io.min.js
vendored
Normal file
1
examples/basic-websocket-client/bundle/socket.io.min.js
vendored
Normal file
@@ -0,0 +1 @@
|
||||
class e{#e=new Map;on(e,t){let s=this.#e.get(e);s||this.#e.set(e,s=[]),s.push(t)}emit(e,...t){const s=this.#e.get(e);if(s)for(const e of s)e.apply(null,t)}}const t="0",s="1",n="2",i="3",o="4",r={CONNECT:0,DISCONNECT:1,EVENT:2};function c(){}class a extends e{id;connected=!1;#t;#s;#n;#i;#o;#r=[];#c;#a=!0;constructor(e,t){super(),this.#t=e,this.#s=Object.assign({path:"/socket.io/",reconnectionDelay:2e3},t),this.#h()}#h(){this.#n=new WebSocket(this.#u()),this.#n.onmessage=({data:e})=>this.#p(e),this.#n.onerror=c,this.#n.onclose=()=>this.#l("transport close")}#u(){return`${this.#t.replace(/^http/,"ws")}${this.#s.path}?EIO=4&transport=websocket`}#p(e){if("string"==typeof e)switch(e[0]){case t:this.#d(e);break;case s:this.#l("transport close");break;case n:this.#T(),this.#m(i);break;case o:let c;try{c=function(e){let t=1;const s={type:parseInt(e.charAt(t++),10)};e.charAt(t)&&(s.data=JSON.parse(e.substring(t)));if(!function(e){switch(e.type){case r.CONNECT:return"object"==typeof e.data;case r.DISCONNECT:return void 0===e.data;case r.EVENT:{const t=e.data;return Array.isArray(t)&&t.length>0&&"string"==typeof t[0]}default:return!1}}(s))throw new Error("invalid format");return s}(e)}catch(e){return this.#l("parse error")}this.#f(c);break;default:this.#l("parse error")}}#d(e){let t;try{t=JSON.parse(e.substring(1))}catch(e){return this.#l("parse error")}this.#o=t.pingInterval+t.pingTimeout,this.#T(),this.#C()}#f(e){switch(e.type){case r.CONNECT:this.#g(e);break;case r.DISCONNECT:this.#a=!1,this.#l("io server disconnect");break;case r.EVENT:super.emit.apply(this,e.data);break;default:this.#l("parse error")}}#g(e){this.id=e.data.sid,this.connected=!0,this.#r.forEach((e=>this.#y(e))),this.#r.slice(0),super.emit("connect")}#l(e){this.#n&&(this.#n.onclose=c,this.#n.close()),clearTimeout(this.#i),clearTimeout(this.#c),this.connected?(this.connected=!1,this.id=void 0,super.emit("disconnect",e)):super.emit("connect_error",e),this.#a&&(this.#c=setTimeout((()=>this.#h()),this.#s.reconnectionDelay))}#T(){clearTimeout(this.#i),this.#i=setTimeout((()=>{this.#l("ping timeout")}),this.#o)}#m(e){this.#n.readyState===WebSocket.OPEN&&this.#n.send(e)}#y(e){this.#m(o+function(e){let t=""+e.type;e.data&&(t+=JSON.stringify(e.data));return t}(e))}#C(){this.#y({type:r.CONNECT})}emit(...e){const t={type:r.EVENT,data:e};this.connected?this.#y(t):this.#r.push(t)}disconnect(){this.#a=!1,this.#l("io client disconnect")}}function h(e,t){return"string"!=typeof e&&(t=e,e=location.origin),new a(e,t)}export{h as io};
|
||||
17
examples/basic-websocket-client/check-bundle-size.js
Normal file
17
examples/basic-websocket-client/check-bundle-size.js
Normal file
@@ -0,0 +1,17 @@
|
||||
import { rollup } from "rollup";
|
||||
import terser from "@rollup/plugin-terser";
|
||||
import { brotliCompressSync } from "node:zlib";
|
||||
|
||||
const rollupBuild = await rollup({
|
||||
input: "./src/index.js"
|
||||
});
|
||||
|
||||
const rollupOutput = await rollupBuild.generate({
|
||||
format: "esm",
|
||||
plugins: [terser()],
|
||||
});
|
||||
|
||||
const bundleAsString = rollupOutput.output[0].code;
|
||||
const brotliedBundle = brotliCompressSync(Buffer.from(bundleAsString));
|
||||
|
||||
console.log(`Bundle size: ${brotliedBundle.length} B`);
|
||||
18
examples/basic-websocket-client/package.json
Normal file
18
examples/basic-websocket-client/package.json
Normal file
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"type": "module",
|
||||
"devDependencies": {
|
||||
"@rollup/plugin-terser": "^0.4.0",
|
||||
"chai": "^4.3.7",
|
||||
"mocha": "^10.2.0",
|
||||
"prettier": "^2.8.4",
|
||||
"rollup": "^3.20.2",
|
||||
"socket.io": "^4.6.1",
|
||||
"ws": "^8.13.0"
|
||||
},
|
||||
"scripts": {
|
||||
"bundle": "rollup -c",
|
||||
"check-bundle-size": "node check-bundle-size.js",
|
||||
"format": "prettier -w src/ test/",
|
||||
"test": "mocha"
|
||||
}
|
||||
}
|
||||
10
examples/basic-websocket-client/rollup.config.js
Normal file
10
examples/basic-websocket-client/rollup.config.js
Normal file
@@ -0,0 +1,10 @@
|
||||
import terser from "@rollup/plugin-terser";
|
||||
|
||||
export default {
|
||||
input: "./src/index.js",
|
||||
output: {
|
||||
file: "./bundle/socket.io.min.js",
|
||||
format: "esm",
|
||||
plugins: [terser()],
|
||||
}
|
||||
};
|
||||
273
examples/basic-websocket-client/src/index.js
Normal file
273
examples/basic-websocket-client/src/index.js
Normal file
@@ -0,0 +1,273 @@
|
||||
class EventEmitter {
|
||||
#listeners = new Map();
|
||||
|
||||
on(event, listener) {
|
||||
let listeners = this.#listeners.get(event);
|
||||
if (!listeners) {
|
||||
this.#listeners.set(event, (listeners = []));
|
||||
}
|
||||
listeners.push(listener);
|
||||
}
|
||||
|
||||
emit(event, ...args) {
|
||||
const listeners = this.#listeners.get(event);
|
||||
if (listeners) {
|
||||
for (const listener of listeners) {
|
||||
listener.apply(null, args);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const EIOPacketType = {
|
||||
OPEN: "0",
|
||||
CLOSE: "1",
|
||||
PING: "2",
|
||||
PONG: "3",
|
||||
MESSAGE: "4",
|
||||
};
|
||||
|
||||
const SIOPacketType = {
|
||||
CONNECT: 0,
|
||||
DISCONNECT: 1,
|
||||
EVENT: 2,
|
||||
};
|
||||
|
||||
function noop() {}
|
||||
|
||||
class Socket extends EventEmitter {
|
||||
id;
|
||||
connected = false;
|
||||
|
||||
#uri;
|
||||
#opts;
|
||||
#ws;
|
||||
#pingTimeoutTimer;
|
||||
#pingTimeoutDelay;
|
||||
#sendBuffer = [];
|
||||
#reconnectTimer;
|
||||
#shouldReconnect = true;
|
||||
|
||||
constructor(uri, opts) {
|
||||
super();
|
||||
this.#uri = uri;
|
||||
this.#opts = Object.assign(
|
||||
{
|
||||
path: "/socket.io/",
|
||||
reconnectionDelay: 2000,
|
||||
},
|
||||
opts
|
||||
);
|
||||
this.#open();
|
||||
}
|
||||
|
||||
#open() {
|
||||
this.#ws = new WebSocket(this.#createUrl());
|
||||
this.#ws.onmessage = ({ data }) => this.#onMessage(data);
|
||||
// dummy handler for Node.js
|
||||
this.#ws.onerror = noop;
|
||||
this.#ws.onclose = () => this.#onClose("transport close");
|
||||
}
|
||||
|
||||
#createUrl() {
|
||||
const uri = this.#uri.replace(/^http/, "ws");
|
||||
const queryParams = "?EIO=4&transport=websocket";
|
||||
return `${uri}${this.#opts.path}${queryParams}`;
|
||||
}
|
||||
|
||||
#onMessage(data) {
|
||||
if (typeof data !== "string") {
|
||||
// TODO handle binary payloads
|
||||
return;
|
||||
}
|
||||
|
||||
switch (data[0]) {
|
||||
case EIOPacketType.OPEN:
|
||||
this.#onOpen(data);
|
||||
break;
|
||||
|
||||
case EIOPacketType.CLOSE:
|
||||
this.#onClose("transport close");
|
||||
break;
|
||||
|
||||
case EIOPacketType.PING:
|
||||
this.#resetPingTimeout();
|
||||
this.#send(EIOPacketType.PONG);
|
||||
break;
|
||||
|
||||
case EIOPacketType.MESSAGE:
|
||||
let packet;
|
||||
try {
|
||||
packet = decode(data);
|
||||
} catch (e) {
|
||||
return this.#onClose("parse error");
|
||||
}
|
||||
this.#onPacket(packet);
|
||||
break;
|
||||
|
||||
default:
|
||||
this.#onClose("parse error");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
#onOpen(data) {
|
||||
let handshake;
|
||||
try {
|
||||
handshake = JSON.parse(data.substring(1));
|
||||
} catch (e) {
|
||||
return this.#onClose("parse error");
|
||||
}
|
||||
this.#pingTimeoutDelay = handshake.pingInterval + handshake.pingTimeout;
|
||||
this.#resetPingTimeout();
|
||||
this.#doConnect();
|
||||
}
|
||||
|
||||
#onPacket(packet) {
|
||||
switch (packet.type) {
|
||||
case SIOPacketType.CONNECT:
|
||||
this.#onConnect(packet);
|
||||
break;
|
||||
|
||||
case SIOPacketType.DISCONNECT:
|
||||
this.#shouldReconnect = false;
|
||||
this.#onClose("io server disconnect");
|
||||
break;
|
||||
|
||||
case SIOPacketType.EVENT:
|
||||
super.emit.apply(this, packet.data);
|
||||
break;
|
||||
|
||||
default:
|
||||
this.#onClose("parse error");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
#onConnect(packet) {
|
||||
this.id = packet.data.sid;
|
||||
this.connected = true;
|
||||
|
||||
this.#sendBuffer.forEach((packet) => this.#sendPacket(packet));
|
||||
this.#sendBuffer.slice(0);
|
||||
|
||||
super.emit("connect");
|
||||
}
|
||||
|
||||
#onClose(reason) {
|
||||
if (this.#ws) {
|
||||
this.#ws.onclose = noop;
|
||||
this.#ws.close();
|
||||
}
|
||||
|
||||
clearTimeout(this.#pingTimeoutTimer);
|
||||
clearTimeout(this.#reconnectTimer);
|
||||
|
||||
if (this.connected) {
|
||||
this.connected = false;
|
||||
this.id = undefined;
|
||||
super.emit("disconnect", reason);
|
||||
} else {
|
||||
super.emit("connect_error", reason);
|
||||
}
|
||||
|
||||
if (this.#shouldReconnect) {
|
||||
this.#reconnectTimer = setTimeout(
|
||||
() => this.#open(),
|
||||
this.#opts.reconnectionDelay
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#resetPingTimeout() {
|
||||
clearTimeout(this.#pingTimeoutTimer);
|
||||
this.#pingTimeoutTimer = setTimeout(() => {
|
||||
this.#onClose("ping timeout");
|
||||
}, this.#pingTimeoutDelay);
|
||||
}
|
||||
|
||||
#send(data) {
|
||||
if (this.#ws.readyState === WebSocket.OPEN) {
|
||||
this.#ws.send(data);
|
||||
}
|
||||
}
|
||||
|
||||
#sendPacket(packet) {
|
||||
this.#send(EIOPacketType.MESSAGE + encode(packet));
|
||||
}
|
||||
|
||||
#doConnect() {
|
||||
this.#sendPacket({ type: SIOPacketType.CONNECT });
|
||||
}
|
||||
|
||||
emit(...args) {
|
||||
const packet = {
|
||||
type: SIOPacketType.EVENT,
|
||||
data: args,
|
||||
};
|
||||
|
||||
if (this.connected) {
|
||||
this.#sendPacket(packet);
|
||||
} else {
|
||||
this.#sendBuffer.push(packet);
|
||||
}
|
||||
}
|
||||
|
||||
disconnect() {
|
||||
this.#shouldReconnect = false;
|
||||
this.#onClose("io client disconnect");
|
||||
}
|
||||
}
|
||||
|
||||
function encode(packet) {
|
||||
let output = "" + packet.type;
|
||||
|
||||
if (packet.data) {
|
||||
output += JSON.stringify(packet.data);
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
function decode(data) {
|
||||
let i = 1; // skip "4" prefix
|
||||
|
||||
const packet = {
|
||||
type: parseInt(data.charAt(i++), 10),
|
||||
};
|
||||
|
||||
if (data.charAt(i)) {
|
||||
packet.data = JSON.parse(data.substring(i));
|
||||
}
|
||||
|
||||
if (!isPacketValid(packet)) {
|
||||
throw new Error("invalid format");
|
||||
}
|
||||
|
||||
return packet;
|
||||
}
|
||||
|
||||
function isPacketValid(packet) {
|
||||
switch (packet.type) {
|
||||
case SIOPacketType.CONNECT:
|
||||
return typeof packet.data === "object";
|
||||
case SIOPacketType.DISCONNECT:
|
||||
return packet.data === undefined;
|
||||
case SIOPacketType.EVENT: {
|
||||
const args = packet.data;
|
||||
return (
|
||||
Array.isArray(args) && args.length > 0 && typeof args[0] === "string"
|
||||
);
|
||||
}
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
export function io(uri, opts) {
|
||||
if (typeof uri !== "string") {
|
||||
opts = uri;
|
||||
uri = location.origin;
|
||||
}
|
||||
return new Socket(uri, opts);
|
||||
}
|
||||
162
examples/basic-websocket-client/test/index.js
Normal file
162
examples/basic-websocket-client/test/index.js
Normal file
@@ -0,0 +1,162 @@
|
||||
import { createServer } from "node:http";
|
||||
import { io as ioc } from "../src/index.js";
|
||||
import { WebSocket } from "ws";
|
||||
import { Server } from "socket.io";
|
||||
import { expect } from "chai";
|
||||
|
||||
// @ts-ignore for Node.js
|
||||
globalThis.WebSocket = WebSocket;
|
||||
|
||||
function waitFor(emitter, eventName) {
|
||||
return new Promise((resolve) => {
|
||||
emitter.on(eventName, resolve);
|
||||
});
|
||||
}
|
||||
|
||||
function sleep(delay) {
|
||||
return new Promise((resolve) => {
|
||||
setTimeout(resolve, delay);
|
||||
});
|
||||
}
|
||||
|
||||
describe("basic client", () => {
|
||||
let io, port, socket;
|
||||
|
||||
beforeEach(() => {
|
||||
const httpServer = createServer();
|
||||
io = new Server(httpServer);
|
||||
|
||||
httpServer.listen(0);
|
||||
port = httpServer.address().port;
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
io.close();
|
||||
socket.disconnect();
|
||||
});
|
||||
|
||||
it("should connect", async () => {
|
||||
socket = ioc(`ws://localhost:${port}`);
|
||||
|
||||
await waitFor(socket, "connect");
|
||||
|
||||
expect(socket.connected).to.eql(true);
|
||||
expect(socket.id).to.be.a("string");
|
||||
});
|
||||
|
||||
it("should connect with 'http://' scheme", async () => {
|
||||
socket = ioc(`http://localhost:${port}`);
|
||||
|
||||
await waitFor(socket, "connect");
|
||||
});
|
||||
|
||||
it("should connect with URL inferred from 'window.location'", async () => {
|
||||
globalThis.location = {
|
||||
origin: `http://localhost:${port}`,
|
||||
};
|
||||
socket = ioc();
|
||||
|
||||
await waitFor(socket, "connect");
|
||||
});
|
||||
|
||||
it("should fail to connect to an invalid URL", async () => {
|
||||
socket = ioc(`http://localhost:4321`);
|
||||
|
||||
await waitFor(socket, "connect_error");
|
||||
});
|
||||
|
||||
it("should receive an event", async () => {
|
||||
io.on("connection", (socket) => {
|
||||
socket.emit("foo", 123);
|
||||
});
|
||||
|
||||
socket = ioc(`ws://localhost:${port}`);
|
||||
|
||||
const value = await waitFor(socket, "foo");
|
||||
|
||||
expect(value).to.eql(123);
|
||||
});
|
||||
|
||||
it("should send an event (not buffered)", async () => {
|
||||
socket = ioc(`ws://localhost:${port}`);
|
||||
|
||||
const [serverSocket] = await Promise.all([
|
||||
waitFor(io, "connection"),
|
||||
waitFor(socket, "connect"),
|
||||
]);
|
||||
|
||||
socket.emit("foo", 456);
|
||||
|
||||
const value = await waitFor(serverSocket, "foo");
|
||||
|
||||
expect(value).to.eql(456);
|
||||
});
|
||||
|
||||
it("should send an event (buffered)", async () => {
|
||||
socket = ioc(`ws://localhost:${port}`);
|
||||
|
||||
socket.emit("foo", 789);
|
||||
|
||||
const [serverSocket] = await Promise.all([
|
||||
waitFor(io, "connection"),
|
||||
waitFor(socket, "connect"),
|
||||
]);
|
||||
|
||||
const value = await waitFor(serverSocket, "foo");
|
||||
|
||||
expect(value).to.eql(789);
|
||||
});
|
||||
|
||||
it("should reconnect", async () => {
|
||||
socket = ioc(`ws://localhost:${port}`, {
|
||||
reconnectionDelay: 50,
|
||||
});
|
||||
|
||||
await waitFor(socket, "connect");
|
||||
|
||||
io.close();
|
||||
|
||||
await waitFor(socket, "disconnect");
|
||||
|
||||
io.listen(port);
|
||||
|
||||
await waitFor(socket, "connect");
|
||||
});
|
||||
|
||||
it("should respond to PING packets", async () => {
|
||||
io.engine.opts.pingInterval = 50;
|
||||
io.engine.opts.pingTimeout = 20;
|
||||
|
||||
socket = ioc(`ws://localhost:${port}`);
|
||||
|
||||
await waitFor(socket, "connect");
|
||||
|
||||
await sleep(500);
|
||||
|
||||
expect(socket.connected).to.eql(true);
|
||||
});
|
||||
|
||||
it("should disconnect (client side)", async () => {
|
||||
socket = ioc(`ws://localhost:${port}`);
|
||||
|
||||
await waitFor(socket, "connect");
|
||||
|
||||
socket.disconnect();
|
||||
|
||||
expect(socket.connected).to.eql(false);
|
||||
expect(socket.id).to.eql(undefined);
|
||||
});
|
||||
|
||||
it("should disconnect (server side)", async () => {
|
||||
socket = ioc(`ws://localhost:${port}`);
|
||||
|
||||
const [serverSocket] = await Promise.all([
|
||||
waitFor(io, "connection"),
|
||||
waitFor(socket, "connect"),
|
||||
]);
|
||||
|
||||
serverSocket.disconnect();
|
||||
|
||||
await waitFor(socket, "disconnect");
|
||||
});
|
||||
});
|
||||
@@ -1,51 +1,53 @@
|
||||
services:
|
||||
haproxy:
|
||||
image: haproxy:1.7-alpine
|
||||
volumes:
|
||||
- ./haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro
|
||||
links:
|
||||
- server-john
|
||||
- server-paul
|
||||
- server-george
|
||||
- server-ringo
|
||||
ports:
|
||||
- "3000:80"
|
||||
|
||||
haproxy:
|
||||
build: ./haproxy
|
||||
links:
|
||||
- server-john
|
||||
- server-paul
|
||||
- server-george
|
||||
- server-ringo
|
||||
ports:
|
||||
- "3000:80"
|
||||
server-john:
|
||||
build: ./server
|
||||
links:
|
||||
- redis
|
||||
expose:
|
||||
- "3000"
|
||||
environment:
|
||||
- NAME=John
|
||||
|
||||
server-john:
|
||||
build: ./server
|
||||
links:
|
||||
- redis
|
||||
expose:
|
||||
- "3000"
|
||||
environment:
|
||||
- NAME=John
|
||||
server-paul:
|
||||
build: ./server
|
||||
links:
|
||||
- redis
|
||||
expose:
|
||||
- "3000"
|
||||
environment:
|
||||
- NAME=Paul
|
||||
|
||||
server-paul:
|
||||
build: ./server
|
||||
links:
|
||||
- redis
|
||||
expose:
|
||||
- "3000"
|
||||
environment:
|
||||
- NAME=Paul
|
||||
server-george:
|
||||
build: ./server
|
||||
links:
|
||||
- redis
|
||||
expose:
|
||||
- "3000"
|
||||
environment:
|
||||
- NAME=George
|
||||
|
||||
server-george:
|
||||
build: ./server
|
||||
links:
|
||||
- redis
|
||||
expose:
|
||||
- "3000"
|
||||
environment:
|
||||
- NAME=George
|
||||
server-ringo:
|
||||
build: ./server
|
||||
links:
|
||||
- redis
|
||||
expose:
|
||||
- "3000"
|
||||
environment:
|
||||
- NAME=Ringo
|
||||
|
||||
server-ringo:
|
||||
build: ./server
|
||||
links:
|
||||
- redis
|
||||
expose:
|
||||
- "3000"
|
||||
environment:
|
||||
- NAME=Ringo
|
||||
|
||||
redis:
|
||||
image: redis:alpine
|
||||
expose:
|
||||
- "6379"
|
||||
redis:
|
||||
image: redis:alpine
|
||||
expose:
|
||||
- "6379"
|
||||
|
||||
@@ -1,2 +0,0 @@
|
||||
FROM haproxy:1.7-alpine
|
||||
COPY haproxy.cfg /usr/local/etc/haproxy/haproxy.cfg
|
||||
@@ -1,51 +1,53 @@
|
||||
services:
|
||||
httpd:
|
||||
image: httpd:2.4-alpine
|
||||
volumes:
|
||||
- ./httpd.conf:/usr/local/apache2/conf/httpd.conf:ro
|
||||
links:
|
||||
- server-john
|
||||
- server-paul
|
||||
- server-george
|
||||
- server-ringo
|
||||
ports:
|
||||
- "3000:80"
|
||||
|
||||
httpd:
|
||||
build: ./httpd
|
||||
links:
|
||||
- server-john
|
||||
- server-paul
|
||||
- server-george
|
||||
- server-ringo
|
||||
ports:
|
||||
- "3000:80"
|
||||
server-john:
|
||||
build: ./server
|
||||
links:
|
||||
- redis
|
||||
expose:
|
||||
- "3000"
|
||||
environment:
|
||||
- NAME=John
|
||||
|
||||
server-john:
|
||||
build: ./server
|
||||
links:
|
||||
- redis
|
||||
expose:
|
||||
- "3000"
|
||||
environment:
|
||||
- NAME=John
|
||||
server-paul:
|
||||
build: ./server
|
||||
links:
|
||||
- redis
|
||||
expose:
|
||||
- "3000"
|
||||
environment:
|
||||
- NAME=Paul
|
||||
|
||||
server-paul:
|
||||
build: ./server
|
||||
links:
|
||||
- redis
|
||||
expose:
|
||||
- "3000"
|
||||
environment:
|
||||
- NAME=Paul
|
||||
server-george:
|
||||
build: ./server
|
||||
links:
|
||||
- redis
|
||||
expose:
|
||||
- "3000"
|
||||
environment:
|
||||
- NAME=George
|
||||
|
||||
server-george:
|
||||
build: ./server
|
||||
links:
|
||||
- redis
|
||||
expose:
|
||||
- "3000"
|
||||
environment:
|
||||
- NAME=George
|
||||
server-ringo:
|
||||
build: ./server
|
||||
links:
|
||||
- redis
|
||||
expose:
|
||||
- "3000"
|
||||
environment:
|
||||
- NAME=Ringo
|
||||
|
||||
server-ringo:
|
||||
build: ./server
|
||||
links:
|
||||
- redis
|
||||
expose:
|
||||
- "3000"
|
||||
environment:
|
||||
- NAME=Ringo
|
||||
|
||||
redis:
|
||||
image: redis:alpine
|
||||
expose:
|
||||
- "6379"
|
||||
redis:
|
||||
image: redis:6
|
||||
expose:
|
||||
- "6379"
|
||||
|
||||
@@ -51,4 +51,5 @@ RewriteRule /(.*) balancer://nodes_ws/$1 [P,L]
|
||||
RewriteCond %{HTTP:Upgrade} !=websocket [NC]
|
||||
RewriteRule /(.*) balancer://nodes_polling/$1 [P,L]
|
||||
|
||||
ProxyTimeout 3
|
||||
# must be bigger than pingInterval (25s by default) + pingTimeout (20s by default)
|
||||
ProxyTimeout 60
|
||||
@@ -1,2 +0,0 @@
|
||||
FROM httpd:2.4-alpine
|
||||
COPY ./httpd.conf /usr/local/apache2/conf/httpd.conf
|
||||
@@ -1,4 +1,4 @@
|
||||
FROM mhart/alpine-node:6
|
||||
FROM node:14-alpine
|
||||
|
||||
# Create app directory
|
||||
RUN mkdir -p /usr/src/app
|
||||
|
||||
@@ -1,58 +1,58 @@
|
||||
services:
|
||||
nginx:
|
||||
image: nginx:alpine
|
||||
volumes:
|
||||
- ./nginx.conf:/etc/nginx/nginx.conf:ro
|
||||
links:
|
||||
- server-john
|
||||
- server-paul
|
||||
- server-george
|
||||
- server-ringo
|
||||
ports:
|
||||
- "3000:80"
|
||||
|
||||
nginx:
|
||||
image: nginx:alpine
|
||||
volumes:
|
||||
- ./nginx.conf:/etc/nginx/nginx.conf:ro
|
||||
links:
|
||||
- server-john
|
||||
- server-paul
|
||||
- server-george
|
||||
- server-ringo
|
||||
ports:
|
||||
- "3000:80"
|
||||
server-john:
|
||||
build: ./server
|
||||
links:
|
||||
- redis
|
||||
expose:
|
||||
- "3000"
|
||||
environment:
|
||||
- NAME=John
|
||||
|
||||
server-john:
|
||||
build: ./server
|
||||
links:
|
||||
- redis
|
||||
expose:
|
||||
- "3000"
|
||||
environment:
|
||||
- NAME=John
|
||||
server-paul:
|
||||
build: ./server
|
||||
links:
|
||||
- redis
|
||||
expose:
|
||||
- "3000"
|
||||
environment:
|
||||
- NAME=Paul
|
||||
|
||||
server-paul:
|
||||
build: ./server
|
||||
links:
|
||||
- redis
|
||||
expose:
|
||||
- "3000"
|
||||
environment:
|
||||
- NAME=Paul
|
||||
server-george:
|
||||
build: ./server
|
||||
links:
|
||||
- redis
|
||||
expose:
|
||||
- "3000"
|
||||
environment:
|
||||
- NAME=George
|
||||
|
||||
server-george:
|
||||
build: ./server
|
||||
links:
|
||||
- redis
|
||||
expose:
|
||||
- "3000"
|
||||
environment:
|
||||
- NAME=George
|
||||
server-ringo:
|
||||
build: ./server
|
||||
links:
|
||||
- redis
|
||||
expose:
|
||||
- "3000"
|
||||
environment:
|
||||
- NAME=Ringo
|
||||
|
||||
server-ringo:
|
||||
build: ./server
|
||||
links:
|
||||
- redis
|
||||
expose:
|
||||
- "3000"
|
||||
environment:
|
||||
- NAME=Ringo
|
||||
client:
|
||||
build: ./client
|
||||
links:
|
||||
- nginx
|
||||
|
||||
client:
|
||||
build: ./client
|
||||
links:
|
||||
- nginx
|
||||
|
||||
redis:
|
||||
image: redis:alpine
|
||||
expose:
|
||||
- "6379"
|
||||
redis:
|
||||
image: redis:6
|
||||
expose:
|
||||
- "6379"
|
||||
|
||||
@@ -3,14 +3,14 @@
|
||||
"version": "0.1.0",
|
||||
"private": true,
|
||||
"dependencies": {
|
||||
"@testing-library/jest-dom": "^4.2.4",
|
||||
"@testing-library/react": "^9.3.2",
|
||||
"@testing-library/user-event": "^7.1.2",
|
||||
"react": "^16.13.1",
|
||||
"react-dom": "^16.13.1",
|
||||
"react-scripts": "3.4.1",
|
||||
"socket.io": "4",
|
||||
"socket.io-client": "4"
|
||||
"@testing-library/jest-dom": "^5.16.5",
|
||||
"@testing-library/react": "^14.0.0",
|
||||
"@testing-library/user-event": "^14.4.3",
|
||||
"react": "^18.2.0",
|
||||
"react-dom": "^18.2.0",
|
||||
"react-scripts": "^5.0.1",
|
||||
"socket.io": "^4.6.1",
|
||||
"socket.io-client": "^4.6.1"
|
||||
},
|
||||
"scripts": {
|
||||
"start": "react-scripts start",
|
||||
|
||||
@@ -1,14 +1,15 @@
|
||||
import React from 'react';
|
||||
import ReactDOM from 'react-dom';
|
||||
import { createRoot } from 'react-dom/client';
|
||||
import './index.css';
|
||||
import App from './App';
|
||||
import * as serviceWorker from './serviceWorker';
|
||||
|
||||
ReactDOM.render(
|
||||
const container = document.getElementById('root');
|
||||
const root = createRoot(container)
|
||||
root.render(
|
||||
<React.StrictMode>
|
||||
<App />
|
||||
</React.StrictMode>,
|
||||
document.getElementById('root')
|
||||
</React.StrictMode>
|
||||
);
|
||||
|
||||
// If you want your app to work offline and load faster, you can change
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -8,18 +8,19 @@
|
||||
"lint": "vue-cli-service lint"
|
||||
},
|
||||
"dependencies": {
|
||||
"core-js": "^3.6.5",
|
||||
"core-js": "^3.8.3",
|
||||
"socket.io-client": "^4.0.0",
|
||||
"vue": "^2.6.11"
|
||||
"vue": "^2.6.14"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@vue/cli-plugin-babel": "~4.5.0",
|
||||
"@vue/cli-plugin-eslint": "~4.5.0",
|
||||
"@vue/cli-service": "~4.5.0",
|
||||
"babel-eslint": "^10.1.0",
|
||||
"eslint": "^6.7.2",
|
||||
"eslint-plugin-vue": "^6.2.2",
|
||||
"vue-template-compiler": "^2.6.11"
|
||||
"@babel/core": "^7.12.16",
|
||||
"@babel/eslint-parser": "^7.12.16",
|
||||
"@vue/cli-plugin-babel": "~5.0.0",
|
||||
"@vue/cli-plugin-eslint": "~5.0.0",
|
||||
"@vue/cli-service": "~5.0.0",
|
||||
"eslint": "^7.32.0",
|
||||
"eslint-plugin-vue": "^8.0.3",
|
||||
"vue-template-compiler": "^2.6.14"
|
||||
},
|
||||
"eslintConfig": {
|
||||
"root": true,
|
||||
@@ -31,9 +32,11 @@
|
||||
"eslint:recommended"
|
||||
],
|
||||
"parserOptions": {
|
||||
"parser": "babel-eslint"
|
||||
"parser": "@babel/eslint-parser"
|
||||
},
|
||||
"rules": {}
|
||||
"rules": {
|
||||
"vue/multi-word-component-names": "off"
|
||||
}
|
||||
},
|
||||
"browserslist": [
|
||||
"> 1%",
|
||||
|
||||
@@ -68,7 +68,7 @@ body {
|
||||
|
||||
@font-face {
|
||||
font-family: Lato;
|
||||
src: url("/fonts/Lato-Regular.ttf");
|
||||
src: "~/public/fonts/Lato-Regular.ttf";
|
||||
}
|
||||
|
||||
#app {
|
||||
|
||||
48
lib/index.ts
48
lib/index.ts
@@ -6,12 +6,11 @@ import { createDeflate, createGzip, createBrotliCompress } from "zlib";
|
||||
import accepts = require("accepts");
|
||||
import { pipeline } from "stream";
|
||||
import path = require("path");
|
||||
import {
|
||||
attach,
|
||||
Server as Engine,
|
||||
import { attach, Server as Engine, uServer } from "engine.io";
|
||||
import type {
|
||||
ServerOptions as EngineOptions,
|
||||
AttachOptions,
|
||||
uServer,
|
||||
BaseServer,
|
||||
} from "engine.io";
|
||||
import { Client } from "./client";
|
||||
import { EventEmitter } from "events";
|
||||
@@ -41,7 +40,7 @@ import {
|
||||
SecondArg,
|
||||
} from "./typed-events";
|
||||
import { patchAdapter, restoreAdapter, serveFile } from "./uws";
|
||||
import type { BaseServer } from "engine.io/build/server";
|
||||
import corsMiddleware from "cors";
|
||||
|
||||
const debug = debugModule("socket.io:server");
|
||||
|
||||
@@ -180,6 +179,18 @@ export class Server<
|
||||
ParentNspNameMatchFn,
|
||||
ParentNamespace<ListenEvents, EmitEvents, ServerSideEvents, SocketData>
|
||||
> = new Map();
|
||||
|
||||
/**
|
||||
* A subset of the {@link parentNsps} map, only containing {@link ParentNamespace} which are based on a regular
|
||||
* expression.
|
||||
*
|
||||
* @private
|
||||
*/
|
||||
private parentNamespacesFromRegExp: Map<
|
||||
RegExp,
|
||||
ParentNamespace<ListenEvents, EmitEvents, ServerSideEvents, SocketData>
|
||||
> = new Map();
|
||||
|
||||
private _adapter?: AdapterConstructor;
|
||||
private _serveClient: boolean;
|
||||
private readonly opts: Partial<ServerOptions>;
|
||||
@@ -192,6 +203,11 @@ export class Server<
|
||||
*/
|
||||
_connectTimeout: number;
|
||||
private httpServer: http.Server | HTTPSServer | Http2SecureServer;
|
||||
private _corsMiddleware: (
|
||||
req: http.IncomingMessage,
|
||||
res: http.ServerResponse,
|
||||
next: () => void
|
||||
) => void;
|
||||
|
||||
/**
|
||||
* Server constructor.
|
||||
@@ -257,6 +273,10 @@ export class Server<
|
||||
this.attach(
|
||||
srv as http.Server | HTTPSServer | Http2SecureServer | number
|
||||
);
|
||||
|
||||
if (this.opts.cors) {
|
||||
this._corsMiddleware = corsMiddleware(this.opts.cors);
|
||||
}
|
||||
}
|
||||
|
||||
get _opts() {
|
||||
@@ -316,8 +336,6 @@ export class Server<
|
||||
}
|
||||
const namespace = this.parentNsps.get(nextFn.value)!.createChild(name);
|
||||
debug("dynamic namespace %s was created", name);
|
||||
// @ts-ignore
|
||||
this.sockets.emitReserved("new_namespace", namespace);
|
||||
fn(namespace);
|
||||
});
|
||||
};
|
||||
@@ -540,7 +558,13 @@ export class Server<
|
||||
srv.removeAllListeners("request");
|
||||
srv.on("request", (req, res) => {
|
||||
if (this.clientPathRegex.test(req.url!)) {
|
||||
this.serve(req, res);
|
||||
if (this._corsMiddleware) {
|
||||
this._corsMiddleware(req, res, () => {
|
||||
this.serve(req, res);
|
||||
});
|
||||
} else {
|
||||
this.serve(req, res);
|
||||
}
|
||||
} else {
|
||||
for (let i = 0; i < evs.length; i++) {
|
||||
evs[i].call(srv, req, res);
|
||||
@@ -693,6 +717,7 @@ export class Server<
|
||||
(nsp, conn, next) => next(null, (name as RegExp).test(nsp)),
|
||||
parentNsp
|
||||
);
|
||||
this.parentNamespacesFromRegExp.set(name, parentNsp);
|
||||
}
|
||||
if (fn) {
|
||||
// @ts-ignore
|
||||
@@ -705,6 +730,13 @@ export class Server<
|
||||
|
||||
let nsp = this._nsps.get(name);
|
||||
if (!nsp) {
|
||||
for (const [regex, parentNamespace] of this.parentNamespacesFromRegExp) {
|
||||
if (regex.test(name as string)) {
|
||||
debug("attaching namespace %s to parent namespace %s", name, regex);
|
||||
return parentNamespace.createChild(name as string);
|
||||
}
|
||||
}
|
||||
|
||||
debug("initializing namespace %s", name);
|
||||
nsp = new Namespace(this, name);
|
||||
this._nsps.set(name, nsp);
|
||||
|
||||
@@ -358,12 +358,15 @@ export class Namespace<
|
||||
typeof sessionId === "string" &&
|
||||
typeof offset === "string"
|
||||
) {
|
||||
const session = await this.adapter.restoreSession(sessionId, offset);
|
||||
let session;
|
||||
try {
|
||||
session = await this.adapter.restoreSession(sessionId, offset);
|
||||
} catch (e) {
|
||||
debug("error while restoring session: %s", e);
|
||||
}
|
||||
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);
|
||||
|
||||
@@ -11,6 +11,21 @@ import debugModule from "debug";
|
||||
|
||||
const debug = debugModule("socket.io:parent-namespace");
|
||||
|
||||
/**
|
||||
* A parent namespace is a special {@link Namespace} that holds a list of child namespaces which were created either
|
||||
* with a regular expression or with a function.
|
||||
*
|
||||
* @example
|
||||
* const parentNamespace = io.of(/\/dynamic-\d+/);
|
||||
*
|
||||
* parentNamespace.on("connection", (socket) => {
|
||||
* const childNamespace = socket.nsp;
|
||||
* }
|
||||
*
|
||||
* // will reach all the clients that are in one of the child namespaces, like "/dynamic-101"
|
||||
* parentNamespace.emit("hello", "world");
|
||||
*
|
||||
*/
|
||||
export class ParentNamespace<
|
||||
ListenEvents extends EventsMap = DefaultEventsMap,
|
||||
EmitEvents extends EventsMap = ListenEvents,
|
||||
@@ -81,6 +96,10 @@ export class ParentNamespace<
|
||||
}
|
||||
|
||||
this.server._nsps.set(name, namespace);
|
||||
|
||||
// @ts-ignore
|
||||
this.server.sockets.emitReserved("new_namespace", namespace);
|
||||
|
||||
return namespace;
|
||||
}
|
||||
|
||||
|
||||
@@ -202,7 +202,7 @@ export class Socket<
|
||||
* Additional information that can be attached to the Socket instance and which will be used in the
|
||||
* {@link Server.fetchSockets()} method.
|
||||
*/
|
||||
public data: Partial<SocketData> = {};
|
||||
public data: SocketData = {} as SocketData;
|
||||
/**
|
||||
* Whether the socket is currently connected or not.
|
||||
*
|
||||
@@ -260,7 +260,7 @@ export class Socket<
|
||||
this.id = previousSession.sid;
|
||||
this.pid = previousSession.pid;
|
||||
previousSession.rooms.forEach((room) => this.join(room));
|
||||
this.data = previousSession.data as Partial<SocketData>;
|
||||
this.data = previousSession.data as SocketData;
|
||||
previousSession.missedPackets.forEach((packet) => {
|
||||
this.packet({
|
||||
type: PacketType.EVENT,
|
||||
@@ -280,6 +280,9 @@ export class Socket<
|
||||
}
|
||||
}
|
||||
this.handshake = this.buildHandshake(auth);
|
||||
|
||||
// prevents crash when the socket receives an "error" event without listener
|
||||
this.on("error", noop);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -720,12 +723,11 @@ export class Socket<
|
||||
* @private
|
||||
*/
|
||||
_onerror(err: Error): void {
|
||||
if (this.listeners("error").length) {
|
||||
this.emitReserved("error", err);
|
||||
} else {
|
||||
console.error("Missing error handler on `socket`.");
|
||||
console.error(err.stack);
|
||||
}
|
||||
// FIXME the meaning of the "error" event is overloaded:
|
||||
// - it can be sent by the client (`socket.emit("error")`)
|
||||
// - it can be emitted when the connection encounters an error (an invalid packet for example)
|
||||
// - it can be emitted when a packet is rejected in a middleware (`socket.use()`)
|
||||
this.emitReserved("error", err);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
109
package-lock.json
generated
109
package-lock.json
generated
@@ -1,20 +1,21 @@
|
||||
{
|
||||
"name": "socket.io",
|
||||
"version": "4.5.4",
|
||||
"version": "4.6.2",
|
||||
"lockfileVersion": 2,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "socket.io",
|
||||
"version": "4.5.4",
|
||||
"version": "4.6.2",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"accepts": "~1.3.4",
|
||||
"base64id": "~2.0.0",
|
||||
"cors": "~2.8.5",
|
||||
"debug": "~4.3.2",
|
||||
"engine.io": "~6.4.0",
|
||||
"engine.io": "~6.5.0",
|
||||
"socket.io-adapter": "~2.5.2",
|
||||
"socket.io-parser": "~4.2.1"
|
||||
"socket.io-parser": "~4.2.4"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/mocha": "^9.0.0",
|
||||
@@ -23,14 +24,14 @@
|
||||
"nyc": "^15.1.0",
|
||||
"prettier": "^2.3.2",
|
||||
"rimraf": "^3.0.2",
|
||||
"socket.io-client": "4.6.0",
|
||||
"socket.io-client": "4.7.0",
|
||||
"socket.io-client-v2": "npm:socket.io-client@^2.4.0",
|
||||
"superagent": "^8.0.0",
|
||||
"supertest": "^6.1.6",
|
||||
"ts-node": "^10.2.1",
|
||||
"tsd": "^0.21.0",
|
||||
"typescript": "^4.4.2",
|
||||
"uWebSockets.js": "github:uNetworking/uWebSockets.js#v20.0.0"
|
||||
"uWebSockets.js": "github:uNetworking/uWebSockets.js#v20.30.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=10.0.0"
|
||||
@@ -1228,9 +1229,9 @@
|
||||
}
|
||||
},
|
||||
"node_modules/cookiejar": {
|
||||
"version": "2.1.3",
|
||||
"resolved": "https://registry.npmjs.org/cookiejar/-/cookiejar-2.1.3.tgz",
|
||||
"integrity": "sha512-JxbCBUdrfr6AQjOXrxoTvAMJO4HBTUIlBzslcJPAz+/KT8yk53fXun51u+RenNYvad/+Vc2DIz5o9UxlCDymFQ==",
|
||||
"version": "2.1.4",
|
||||
"resolved": "https://registry.npmjs.org/cookiejar/-/cookiejar-2.1.4.tgz",
|
||||
"integrity": "sha512-LDx6oHrK+PhzLKJU9j5S7/Y3jM/mUHvD/DeI1WQmJn652iPC5Y4TBzC9l+5OMOXlyTTA+SmVUPm0HQUwpD5Jqw==",
|
||||
"dev": true
|
||||
},
|
||||
"node_modules/cors": {
|
||||
@@ -1377,9 +1378,9 @@
|
||||
"dev": true
|
||||
},
|
||||
"node_modules/engine.io": {
|
||||
"version": "6.4.0",
|
||||
"resolved": "https://registry.npmjs.org/engine.io/-/engine.io-6.4.0.tgz",
|
||||
"integrity": "sha512-OgxY1c/RuCSeO/rTr8DIFXx76IzUUft86R7/P7MMbbkuzeqJoTNw2lmeD91IyGz41QYleIIjWeMJGgug043sfQ==",
|
||||
"version": "6.5.0",
|
||||
"resolved": "https://registry.npmjs.org/engine.io/-/engine.io-6.5.0.tgz",
|
||||
"integrity": "sha512-UlfoK1iD62Hkedw2TmuHdhDsZCGaAyp+LZ/AvnImjYBeWagA3qIEETum90d6shMeFZiDuGT66zVCdx1wKYKGGg==",
|
||||
"dependencies": {
|
||||
"@types/cookie": "^0.4.1",
|
||||
"@types/cors": "^2.8.12",
|
||||
@@ -1389,7 +1390,7 @@
|
||||
"cookie": "~0.4.1",
|
||||
"cors": "~2.8.5",
|
||||
"debug": "~4.3.1",
|
||||
"engine.io-parser": "~5.0.3",
|
||||
"engine.io-parser": "~5.1.0",
|
||||
"ws": "~8.11.0"
|
||||
},
|
||||
"engines": {
|
||||
@@ -1397,22 +1398,22 @@
|
||||
}
|
||||
},
|
||||
"node_modules/engine.io-client": {
|
||||
"version": "6.4.0",
|
||||
"resolved": "https://registry.npmjs.org/engine.io-client/-/engine.io-client-6.4.0.tgz",
|
||||
"integrity": "sha512-GyKPDyoEha+XZ7iEqam49vz6auPnNJ9ZBfy89f+rMMas8AuiMWOZ9PVzu8xb9ZC6rafUqiGHSCfu22ih66E+1g==",
|
||||
"version": "6.5.0",
|
||||
"resolved": "https://registry.npmjs.org/engine.io-client/-/engine.io-client-6.5.0.tgz",
|
||||
"integrity": "sha512-C7eN3OKggSfd5g8IDgUA9guC8TNS6CEganKT7dL6Fp3q+FobcQ/WBn2Qq2XTL1vNTiFZfDzXohvqLuR9dWejdg==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"@socket.io/component-emitter": "~3.1.0",
|
||||
"debug": "~4.3.1",
|
||||
"engine.io-parser": "~5.0.3",
|
||||
"engine.io-parser": "~5.1.0",
|
||||
"ws": "~8.11.0",
|
||||
"xmlhttprequest-ssl": "~2.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/engine.io-parser": {
|
||||
"version": "5.0.4",
|
||||
"resolved": "https://registry.npmjs.org/engine.io-parser/-/engine.io-parser-5.0.4.tgz",
|
||||
"integrity": "sha512-+nVFp+5z1E3HcToEnO7ZIj3g+3k9389DvWtvJZz0T6/eOCPIyyxehFcedoYrZQrp0LgQbD9pPXhpMBKMd5QURg==",
|
||||
"version": "5.1.0",
|
||||
"resolved": "https://registry.npmjs.org/engine.io-parser/-/engine.io-parser-5.1.0.tgz",
|
||||
"integrity": "sha512-enySgNiK5tyZFynt3z7iqBR+Bto9EVVVvDFuTT0ioHCGbzirZVGDGiQjZzEp8hWl6hd5FSVytJGuScX1C1C35w==",
|
||||
"engines": {
|
||||
"node": ">=10.0.0"
|
||||
}
|
||||
@@ -3463,15 +3464,15 @@
|
||||
}
|
||||
},
|
||||
"node_modules/socket.io-client": {
|
||||
"version": "4.6.0",
|
||||
"resolved": "https://registry.npmjs.org/socket.io-client/-/socket.io-client-4.6.0.tgz",
|
||||
"integrity": "sha512-2XOp18xnGghUICSd5ziUIS4rB0dhr6S8OvAps8y+HhOjFQlqGcf+FIh6fCIsKKZyWFxJeFPrZRNPGsHDTsz1Ug==",
|
||||
"version": "4.7.0",
|
||||
"resolved": "https://registry.npmjs.org/socket.io-client/-/socket.io-client-4.7.0.tgz",
|
||||
"integrity": "sha512-7Q8CeDrhuZzg4QLXl3tXlk5yb086oxYzehAVZRLiGCzCmtDneiHz1qHyyWcxhTgxXiokVpWQXoG/u60HoXSQew==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"@socket.io/component-emitter": "~3.1.0",
|
||||
"debug": "~4.3.2",
|
||||
"engine.io-client": "~6.4.0",
|
||||
"socket.io-parser": "~4.2.1"
|
||||
"engine.io-client": "~6.5.0",
|
||||
"socket.io-parser": "~4.2.4"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=10.0.0"
|
||||
@@ -3586,9 +3587,9 @@
|
||||
}
|
||||
},
|
||||
"node_modules/socket.io-parser": {
|
||||
"version": "4.2.2",
|
||||
"resolved": "https://registry.npmjs.org/socket.io-parser/-/socket.io-parser-4.2.2.tgz",
|
||||
"integrity": "sha512-DJtziuKypFkMMHCm2uIshOYC7QaylbtzQwiMYDuCKy3OPkjLzu4B2vAhTlqipRHHzrI0NJeBAizTK7X+6m1jVw==",
|
||||
"version": "4.2.4",
|
||||
"resolved": "https://registry.npmjs.org/socket.io-parser/-/socket.io-parser-4.2.4.tgz",
|
||||
"integrity": "sha512-/GbIKmo8ioc+NIWIhwdecY0ge+qVBSMdgxGygevmdHj24bsfgtCmcUUcQ5ZzcylGFHsN3k4HB4Cgkl96KVnuew==",
|
||||
"dependencies": {
|
||||
"@socket.io/component-emitter": "~3.1.0",
|
||||
"debug": "~4.3.1"
|
||||
@@ -4105,8 +4106,8 @@
|
||||
}
|
||||
},
|
||||
"node_modules/uWebSockets.js": {
|
||||
"version": "20.0.0",
|
||||
"resolved": "git+https://git@github.com/uNetworking/uWebSockets.js.git#4558ee00f9f1f686fffe1accbfc2e85b1af9c50f",
|
||||
"version": "20.30.0",
|
||||
"resolved": "git+https://git@github.com/uNetworking/uWebSockets.js.git#d39d4181daf5b670d44cbc1b18f8c28c85fd4142",
|
||||
"dev": true
|
||||
},
|
||||
"node_modules/v8-compile-cache-lib": {
|
||||
@@ -5271,9 +5272,9 @@
|
||||
"integrity": "sha512-aSWTXFzaKWkvHO1Ny/s+ePFpvKsPnjc551iI41v3ny/ow6tBG5Vd+FuqGNhh1LxOmVzOlGUriIlOaokOvhaStA=="
|
||||
},
|
||||
"cookiejar": {
|
||||
"version": "2.1.3",
|
||||
"resolved": "https://registry.npmjs.org/cookiejar/-/cookiejar-2.1.3.tgz",
|
||||
"integrity": "sha512-JxbCBUdrfr6AQjOXrxoTvAMJO4HBTUIlBzslcJPAz+/KT8yk53fXun51u+RenNYvad/+Vc2DIz5o9UxlCDymFQ==",
|
||||
"version": "2.1.4",
|
||||
"resolved": "https://registry.npmjs.org/cookiejar/-/cookiejar-2.1.4.tgz",
|
||||
"integrity": "sha512-LDx6oHrK+PhzLKJU9j5S7/Y3jM/mUHvD/DeI1WQmJn652iPC5Y4TBzC9l+5OMOXlyTTA+SmVUPm0HQUwpD5Jqw==",
|
||||
"dev": true
|
||||
},
|
||||
"cors": {
|
||||
@@ -5387,9 +5388,9 @@
|
||||
"dev": true
|
||||
},
|
||||
"engine.io": {
|
||||
"version": "6.4.0",
|
||||
"resolved": "https://registry.npmjs.org/engine.io/-/engine.io-6.4.0.tgz",
|
||||
"integrity": "sha512-OgxY1c/RuCSeO/rTr8DIFXx76IzUUft86R7/P7MMbbkuzeqJoTNw2lmeD91IyGz41QYleIIjWeMJGgug043sfQ==",
|
||||
"version": "6.5.0",
|
||||
"resolved": "https://registry.npmjs.org/engine.io/-/engine.io-6.5.0.tgz",
|
||||
"integrity": "sha512-UlfoK1iD62Hkedw2TmuHdhDsZCGaAyp+LZ/AvnImjYBeWagA3qIEETum90d6shMeFZiDuGT66zVCdx1wKYKGGg==",
|
||||
"requires": {
|
||||
"@types/cookie": "^0.4.1",
|
||||
"@types/cors": "^2.8.12",
|
||||
@@ -5399,27 +5400,27 @@
|
||||
"cookie": "~0.4.1",
|
||||
"cors": "~2.8.5",
|
||||
"debug": "~4.3.1",
|
||||
"engine.io-parser": "~5.0.3",
|
||||
"engine.io-parser": "~5.1.0",
|
||||
"ws": "~8.11.0"
|
||||
}
|
||||
},
|
||||
"engine.io-client": {
|
||||
"version": "6.4.0",
|
||||
"resolved": "https://registry.npmjs.org/engine.io-client/-/engine.io-client-6.4.0.tgz",
|
||||
"integrity": "sha512-GyKPDyoEha+XZ7iEqam49vz6auPnNJ9ZBfy89f+rMMas8AuiMWOZ9PVzu8xb9ZC6rafUqiGHSCfu22ih66E+1g==",
|
||||
"version": "6.5.0",
|
||||
"resolved": "https://registry.npmjs.org/engine.io-client/-/engine.io-client-6.5.0.tgz",
|
||||
"integrity": "sha512-C7eN3OKggSfd5g8IDgUA9guC8TNS6CEganKT7dL6Fp3q+FobcQ/WBn2Qq2XTL1vNTiFZfDzXohvqLuR9dWejdg==",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"@socket.io/component-emitter": "~3.1.0",
|
||||
"debug": "~4.3.1",
|
||||
"engine.io-parser": "~5.0.3",
|
||||
"engine.io-parser": "~5.1.0",
|
||||
"ws": "~8.11.0",
|
||||
"xmlhttprequest-ssl": "~2.0.0"
|
||||
}
|
||||
},
|
||||
"engine.io-parser": {
|
||||
"version": "5.0.4",
|
||||
"resolved": "https://registry.npmjs.org/engine.io-parser/-/engine.io-parser-5.0.4.tgz",
|
||||
"integrity": "sha512-+nVFp+5z1E3HcToEnO7ZIj3g+3k9389DvWtvJZz0T6/eOCPIyyxehFcedoYrZQrp0LgQbD9pPXhpMBKMd5QURg=="
|
||||
"version": "5.1.0",
|
||||
"resolved": "https://registry.npmjs.org/engine.io-parser/-/engine.io-parser-5.1.0.tgz",
|
||||
"integrity": "sha512-enySgNiK5tyZFynt3z7iqBR+Bto9EVVVvDFuTT0ioHCGbzirZVGDGiQjZzEp8hWl6hd5FSVytJGuScX1C1C35w=="
|
||||
},
|
||||
"error-ex": {
|
||||
"version": "1.3.2",
|
||||
@@ -6922,15 +6923,15 @@
|
||||
}
|
||||
},
|
||||
"socket.io-client": {
|
||||
"version": "4.6.0",
|
||||
"resolved": "https://registry.npmjs.org/socket.io-client/-/socket.io-client-4.6.0.tgz",
|
||||
"integrity": "sha512-2XOp18xnGghUICSd5ziUIS4rB0dhr6S8OvAps8y+HhOjFQlqGcf+FIh6fCIsKKZyWFxJeFPrZRNPGsHDTsz1Ug==",
|
||||
"version": "4.7.0",
|
||||
"resolved": "https://registry.npmjs.org/socket.io-client/-/socket.io-client-4.7.0.tgz",
|
||||
"integrity": "sha512-7Q8CeDrhuZzg4QLXl3tXlk5yb086oxYzehAVZRLiGCzCmtDneiHz1qHyyWcxhTgxXiokVpWQXoG/u60HoXSQew==",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"@socket.io/component-emitter": "~3.1.0",
|
||||
"debug": "~4.3.2",
|
||||
"engine.io-client": "~6.4.0",
|
||||
"socket.io-parser": "~4.2.1"
|
||||
"engine.io-client": "~6.5.0",
|
||||
"socket.io-parser": "~4.2.4"
|
||||
}
|
||||
},
|
||||
"socket.io-client-v2": {
|
||||
@@ -7026,9 +7027,9 @@
|
||||
}
|
||||
},
|
||||
"socket.io-parser": {
|
||||
"version": "4.2.2",
|
||||
"resolved": "https://registry.npmjs.org/socket.io-parser/-/socket.io-parser-4.2.2.tgz",
|
||||
"integrity": "sha512-DJtziuKypFkMMHCm2uIshOYC7QaylbtzQwiMYDuCKy3OPkjLzu4B2vAhTlqipRHHzrI0NJeBAizTK7X+6m1jVw==",
|
||||
"version": "4.2.4",
|
||||
"resolved": "https://registry.npmjs.org/socket.io-parser/-/socket.io-parser-4.2.4.tgz",
|
||||
"integrity": "sha512-/GbIKmo8ioc+NIWIhwdecY0ge+qVBSMdgxGygevmdHj24bsfgtCmcUUcQ5ZzcylGFHsN3k4HB4Cgkl96KVnuew==",
|
||||
"requires": {
|
||||
"@socket.io/component-emitter": "~3.1.0",
|
||||
"debug": "~4.3.1"
|
||||
@@ -7402,9 +7403,9 @@
|
||||
"dev": true
|
||||
},
|
||||
"uWebSockets.js": {
|
||||
"version": "git+https://git@github.com/uNetworking/uWebSockets.js.git#4558ee00f9f1f686fffe1accbfc2e85b1af9c50f",
|
||||
"version": "git+https://git@github.com/uNetworking/uWebSockets.js.git#d39d4181daf5b670d44cbc1b18f8c28c85fd4142",
|
||||
"dev": true,
|
||||
"from": "uWebSockets.js@github:uNetworking/uWebSockets.js#v20.0.0"
|
||||
"from": "uWebSockets.js@github:uNetworking/uWebSockets.js#v20.30.0"
|
||||
},
|
||||
"v8-compile-cache-lib": {
|
||||
"version": "3.0.1",
|
||||
|
||||
15
package.json
15
package.json
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "socket.io",
|
||||
"version": "4.6.0",
|
||||
"version": "4.7.0",
|
||||
"description": "node.js realtime framework server",
|
||||
"keywords": [
|
||||
"realtime",
|
||||
@@ -26,9 +26,9 @@
|
||||
"type": "commonjs",
|
||||
"main": "./dist/index.js",
|
||||
"exports": {
|
||||
"types": "./dist/index.d.ts",
|
||||
"import": "./wrapper.mjs",
|
||||
"require": "./dist/index.js",
|
||||
"types": "./dist/index.d.ts"
|
||||
"require": "./dist/index.js"
|
||||
},
|
||||
"types": "./dist/index.d.ts",
|
||||
"license": "MIT",
|
||||
@@ -48,10 +48,11 @@
|
||||
"dependencies": {
|
||||
"accepts": "~1.3.4",
|
||||
"base64id": "~2.0.0",
|
||||
"cors": "~2.8.5",
|
||||
"debug": "~4.3.2",
|
||||
"engine.io": "~6.4.0",
|
||||
"engine.io": "~6.5.0",
|
||||
"socket.io-adapter": "~2.5.2",
|
||||
"socket.io-parser": "~4.2.1"
|
||||
"socket.io-parser": "~4.2.4"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/mocha": "^9.0.0",
|
||||
@@ -60,14 +61,14 @@
|
||||
"nyc": "^15.1.0",
|
||||
"prettier": "^2.3.2",
|
||||
"rimraf": "^3.0.2",
|
||||
"socket.io-client": "4.6.0",
|
||||
"socket.io-client": "4.7.0",
|
||||
"socket.io-client-v2": "npm:socket.io-client@^2.4.0",
|
||||
"superagent": "^8.0.0",
|
||||
"supertest": "^6.1.6",
|
||||
"ts-node": "^10.2.1",
|
||||
"tsd": "^0.21.0",
|
||||
"typescript": "^4.4.2",
|
||||
"uWebSockets.js": "github:uNetworking/uWebSockets.js#v20.0.0"
|
||||
"uWebSockets.js": "github:uNetworking/uWebSockets.js#v20.30.0"
|
||||
},
|
||||
"contributors": [
|
||||
{
|
||||
|
||||
@@ -652,5 +652,16 @@ describe("namespaces", () => {
|
||||
|
||||
io.of(/^\/dynamic-\d+$/);
|
||||
});
|
||||
|
||||
it("should attach a child namespace to its parent upon manual creation", () => {
|
||||
const io = new Server(0);
|
||||
const parentNamespace = io.of(/^\/dynamic-\d+$/);
|
||||
const childNamespace = io.of("/dynamic-101");
|
||||
|
||||
// @ts-ignore
|
||||
expect(parentNamespace.children.has(childNamespace)).to.be(true);
|
||||
|
||||
io.close();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -70,6 +70,27 @@ describe("server attachment", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("should serve client with necessary CORS headers", (done) => {
|
||||
const srv = createServer();
|
||||
new Server(srv, {
|
||||
cors: {
|
||||
origin: "https://good-origin.com",
|
||||
},
|
||||
});
|
||||
request(srv)
|
||||
.get("/socket.io/socket.io.js")
|
||||
.set("origin", "https://good-origin.com")
|
||||
.buffer(true)
|
||||
.end((err, res) => {
|
||||
if (err) return done(err);
|
||||
expect(res.headers["access-control-allow-origin"]).to.be(
|
||||
"https://good-origin.com"
|
||||
);
|
||||
expect(res.status).to.be(200);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it(
|
||||
"should serve bundle with msgpack parser",
|
||||
testSource("socket.io.msgpack.min.js")
|
||||
|
||||
@@ -852,10 +852,6 @@ describe("socket", () => {
|
||||
it("should not crash when messing with Object prototype (and other globals)", (done) => {
|
||||
// @ts-ignore
|
||||
Object.prototype.foo = "bar";
|
||||
// @ts-ignore
|
||||
global.File = "";
|
||||
// @ts-ignore
|
||||
global.Blob = [];
|
||||
const io = new Server(0);
|
||||
const socket = createClient(io);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user