Commit Graph

1110 Commits

Author SHA1 Message Date
Damien Arrachequesne
088dcb4dff feat: add the "maxPayload" field in the handshake details
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}
```

Related: https://github.com/socketio/socket.io-client/issues/1531
2022-03-10 14:20:54 +01:00
Damien Arrachequesne
657f04e0b8 chore: add Node.js 16 in the test matrix
See also: https://github.com/nodejs/Release
2022-03-10 14:20:36 +01:00
Damien Arrachequesne
e24b27b8ef refactor: return an HTTP 413 response for too large payloads
Before this, the connection was closed abrutly with an HTTP 502
response.

See also: f8100f9237

Related: https://github.com/socketio/socket.io/issues/4293
2022-02-28 07:21:53 +01:00
Damien Arrachequesne
ce3fe9d80b chore(release): 6.1.3
Diff: https://github.com/socketio/engine.io/compare/6.1.2...6.1.3
2022-02-23 07:40:31 +01:00
Damien Arrachequesne
1bc5b1a2fd chore: bump engine.io-parser to version 5.0.3
In order to make sure the types added in [1] are included.

Related:

- https://github.com/socketio/engine.io/issues/639
- https://github.com/socketio/engine.io/issues/640

[1]: ad5bd7ddf5
2022-02-23 07:36:58 +01:00
e3dio
5df4f18f3e perf(uws): remove nested inner functions 2022-02-23 07:16:25 +01:00
e3dio
3367440308 fix(uws): properly handle chunked content (#642)
With the engine based on µWebSockets.js (introduced in version 6.1.0),
a huge request body split in multiple chunks would throw the following
error:

> node:buffer:254
>   TypedArrayPrototypeSet(target, source, targetStart);
>   ^
>
> TypeError: Cannot perform %TypedArray%.prototype.set on a detached ArrayBuffer
>     at Buffer.set (<anonymous>)
>     at _copyActual (node:buffer:254:3)
> node:buffer:254
>   TypedArrayPrototypeSet(target, source, targetStart);
>   ^
>
> TypeError: Cannot perform %TypedArray%.prototype.set on a detached ArrayBuffer
>     at Buffer.set (<anonymous>)
>     at _copyActual (node:buffer:254:3)
>     at Function.concat (node:buffer:562:12)
>     at onEnd (.../node_modules/engine.io/build/transports-uws/polling.js:126:32)
>     at .../node_modules/engine.io/build/transports-uws/polling.js:143:17

Note: µWebSockets.js does not currently support chunked transfer
encoding.
2022-02-23 07:16:25 +01:00
Jeffrey van Norden
a463d268ed fix(typings): allow CorsOptionsDelegate as cors options (#641)
Reference: https://www.npmjs.com/package/cors#configuring-cors-asynchronously

Related: 54a59cd8f0
2022-02-17 06:36:49 +01:00
Damien Arrachequesne
90fb0a99ac chore(release): 6.1.2
Diff: https://github.com/socketio/engine.io/compare/6.1.1...6.1.2
2022-01-18 18:00:23 +01:00
Damien Arrachequesne
e122e4be7b refactor: add additional types
Merged from https://github.com/socketio/engine.io/pull/630
2022-01-18 17:55:55 +01:00
Damien Arrachequesne
3f1e312a2c chore: bump package-lock.json file version
Note to future self: URL for dependencies from GitHub must use
"git+https" instead of "git+ssh" to make the CI pass, else you will
encounter the following error:

```
npm ERR! Error while executing:
npm ERR! /usr/bin/git ls-remote -h -t ssh://git@github.com/uNetworking/uWebSockets.js.git
npm ERR!
npm ERR! Warning: Permanently added the RSA host key for IP address '140.82.112.3' to the list of known hosts.
npm ERR! git@github.com: Permission denied (publickey).
npm ERR! fatal: Could not read from remote repository.
npm ERR!
npm ERR! Please make sure you have the correct access rights
npm ERR! and the repository exists.
npm ERR!
npm ERR! exited with error code: 128
```
2022-01-18 17:53:58 +01:00
Damien Arrachequesne
45112a30d1 fix(uws): fix HTTP long-polling with CORS
When binding to an uWebSockets.js application, the server could crash
with the following error:

```
TypeError: res.onData is not a function
    at Polling.onDataRequest (build/transports-uws/polling.js:133:13)
    at Polling.onRequest (build/transports-uws/polling.js:47:18)
    at callback (build/userver.js:80:56)
```

Related: https://github.com/socketio/engine.io/issues/637
2022-01-18 17:49:28 +01:00
Yosi Attias
49bb7cf665 fix(uws): expose additional uWebSockets.js options (#634)
You can now pass additional options:

```js
const { App } = require("uWebSockets.js");
const { uServer } = require("engine.io");

const app = new App();
const server = new uServer();

server.attach(app, {
  compression: uWS.DEDICATED_COMPRESSOR_128KB, // defaults to none
  idleTimeout: 60, // defaults to 120
  maxBackpressure: 8 * 1024 // defaults to 1024 * 1024
});

app.listen(3000);
```

Related: https://github.com/socketio/engine.io/issues/633
2022-01-14 08:57:46 +01:00
Damien Arrachequesne
8b4d6a8176 fix(uws): handle invalid websocket upgrades
When binding to an uWebSockets.js App, there was an unhandled case that
could crash the server:

```
curl "http://localhost:3000/engine.io/?EIO=4&transport=websocket"
```

would result in:

```
Error: Returning from a request handler without responding or attaching an abort handler is forbidden!
terminate called without an active exception
```

Note: this does not apply to the default server based on ws, because
the error was caught elsewhere in the source code.

Related: https://github.com/socketio/socket.io/issues/4250
2022-01-14 08:18:03 +01:00
dependabot[bot]
a84595a04e chore(deps): bump engine.io from 4.0.0 to 4.1.2 in /examples/latency (#636)
Bumps [engine.io](https://github.com/socketio/engine.io) from 4.0.0 to 4.1.2.
- [Release notes](https://github.com/socketio/engine.io/releases)
- [Changelog](https://github.com/socketio/engine.io/blob/4.1.2/CHANGELOG.md)
- [Commits](https://github.com/socketio/engine.io/compare/4.0.0...4.1.2)

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

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

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2022-01-14 07:59:43 +01:00
Damien Arrachequesne
f3b761dc56 chore(release): 6.1.1
Diff: https://github.com/socketio/engine.io/compare/6.1.0...6.1.1
2022-01-11 16:08:20 +01:00
Damien Arrachequesne
c0e194d449 fix: properly handle invalid data sent by a malicious websocket client
**IMPORTANT SECURITY FIX**

A malicious client could send a specially crafted HTTP request,
triggering an uncaught exception and killing the Node.js process:

> RangeError: Invalid WebSocket frame: RSV2 and RSV3 must be clear
>   at Receiver.getInfo (/.../node_modules/ws/lib/receiver.js:176:14)
>   at Receiver.startLoop (/.../node_modules/ws/lib/receiver.js:136:22)
>   at Receiver._write (/.../node_modules/ws/lib/receiver.js:83:10)
>   at writeOrBuffer (internal/streams/writable.js:358:12)

This bug was introduced by [1], included in `engine.io@4.0.0`, so
previous releases are not impacted.

[1]: f3c291fa61

Thanks to Marcus Wejderot from Mevisio for the responsible disclosure.
2022-01-11 15:52:15 +01:00
Jeff Winder
b04967b52e refactor: import Node's setTimeout & clearTimeout to prevent ambiguity (#632) 2021-12-14 09:09:23 +01:00
Damien Arrachequesne
4c306af945 chore(release): 6.1.0
Diff: https://github.com/socketio/engine.io/compare/6.0.0...6.1.0
2021-11-08 08:37:22 +01:00
Damien Arrachequesne
972de60ffb docs: add changelog for release 6.0.1
Merged from the 6.0.x branch.
2021-11-08 08:20:30 +01:00
Damien Arrachequesne
6d9f0c364a chore: bump mocha to version 9.1.3
The uWebSocket.js App has no close method for now, so we can't properly
stop the engine at the end of the tests, hence the "--exit".
2021-11-08 08:18:18 +01:00
Damien Arrachequesne
35b6b505df test: replace "s" package by string interpolation 2021-11-08 08:17:25 +01:00
Damien Arrachequesne
ed50fc346b fix: fix payload encoding for v3 clients
The v3 parser (used for compatibility with older clients) was broken
during the migration to TypeScript ([1]).

This was not caught in the test suite because the Node.js client does
not support binary packet in polling mode (packets are base64-encoded).

[1]: c0d6eaa1ba

Backported from 6.0.x branch: 3f42262fd2
2021-11-06 08:03:57 +01:00
Damien Arrachequesne
271e2df94d feat: add an implementation based on uWebSockets.js
```js
const { App } = require("uWebSockets.js");
const { uServer } = require("engine.io");

const app = new App();
const server = new uServer();

server.attach(app);

app.listen(3000);
```

Reference: https://github.com/uNetworking/uWebSockets.js

Related: https://github.com/socketio/engine.io/issues/578
2021-11-02 22:46:09 +01:00
Tom Atkinson
37474c7e67 perf: refresh ping timer (#628)
Reference: https://nodejs.org/docs/latest/api/timers.html#timeoutrefresh
2021-11-01 21:33:15 +01:00
Damien Arrachequesne
fe5d97fc3d chore(release): 6.0.0
Diff: https://github.com/socketio/engine.io/compare/5.2.0...6.0.0
2021-10-08 16:12:32 +02:00
Damien Arrachequesne
401f4b6069 chore: add an ES module wrapper 2021-10-08 15:23:57 +02:00
Damien Arrachequesne
64d5754511 chore: bump ws
Release notes: https://github.com/websockets/ws/releases/tag/8.0.0
2021-10-08 15:05:51 +02:00
Damien Arrachequesne
c0d6eaa1ba chore: migrate to TypeScript
Related: https://github.com/socketio/engine.io/issues/510
2021-10-08 14:55:30 +02:00
mingkaili
18a6eb89fb docs: remove the extra bracket in README (#627) 2021-09-09 07:21:04 +02:00
dependabot[bot]
ebb36f298d chore(deps): bump xmlhttprequest-ssl from 1.5.5 to 2.0.0 (#626)
Bumps [xmlhttprequest-ssl](https://github.com/mjwwit/node-XMLHttpRequest) from 1.5.5 to 2.0.0.
- [Release notes](https://github.com/mjwwit/node-XMLHttpRequest/releases)
- [Commits](https://github.com/mjwwit/node-XMLHttpRequest/compare/1.5.5...2.0.0)

---
updated-dependencies:
- dependency-name: xmlhttprequest-ssl
  dependency-type: indirect
...

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

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2021-08-29 09:08:48 +02:00
Damien Arrachequesne
313ca50ce8 chore(release): 5.2.0
Diff: https://github.com/socketio/engine.io/compare/5.1.1...5.2.0
2021-08-29 08:32:20 +02:00
Kamil93
b9dc43c3e1 docs: add documentation about heartbeat event (#623) 2021-08-29 08:25:06 +02:00
dependabot[bot]
7e9e544a9a chore(deps): bump ws from 7.4.2 to 7.4.6 (#621)
Bumps [ws](https://github.com/websockets/ws) from 7.4.2 to 7.4.6.
- [Release notes](https://github.com/websockets/ws/releases)
- [Commits](https://github.com/websockets/ws/compare/7.4.2...7.4.6)

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

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2021-05-30 23:05:15 +02:00
Damien Arrachequesne
733ad4dc4e chore(release): 5.1.1
Diff: https://github.com/socketio/engine.io/compare/5.1.0...5.1.1
2021-05-17 00:29:34 +02:00
Damien Arrachequesne
43606865e5 fix: properly close the websocket connection upon handshake error
This should fix the following error:

> TypeError: Cannot read property 'writeHead' of undefined

This bug was introduced by [1], because the `if (res !== undefined) { ... }`
check was removed.

But `res` is indeed undefined when the client connects with WebSocket
directly, in that case we need to manually write the response content
(in the abortUpgrade method).

Please note that the previous behavior was invalid too, since the
WebSocket connection was left open when an error occurred during the
handshake.

[1]: 7096e98a02
2021-05-17 00:22:39 +02:00
dependabot[bot]
29bd4fe800 chore(deps): bump lodash from 4.17.20 to 4.17.21 (#619)
Bumps [lodash](https://github.com/lodash/lodash) from 4.17.20 to 4.17.21.
- [Release notes](https://github.com/lodash/lodash/releases)
- [Commits](https://github.com/lodash/lodash/compare/4.17.20...4.17.21)

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

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2021-05-17 00:21:34 +02:00
Damien Arrachequesne
8c9bd9262f chore(release): 5.1.0
Diff: https://github.com/socketio/engine.io/compare/5.0.0...5.1.0
2021-05-04 10:31:23 +02:00
Damien Arrachequesne
7706b123df perf: add a "wsPreEncoded" writing option
This option will be used when broadcasting a packet to multiple clients,
in order to only encode the packet once.

Usage:

```js
socket.write("test", {
  wsPreEncoded: "4test"
});
```

Note: pre-encoding the content with HTTP long-polling is a bit harder,
since the concatenation of the packets is specific to each client.
2021-05-04 08:44:06 +02:00
Branislav Katreniak
ad5306aeae perf(websocket): fix write back-pressure (#618)
This change should reduce memory usage when many packets are emitted to
many clients in a burst.

Co-authored-by: Branislav Katreniak <bkatreniak@slido.com>
2021-05-04 08:35:23 +02:00
Damien Arrachequesne
4c0aa73e06 refactor: remove "self" references 2021-04-30 14:38:31 +02:00
Damien Arrachequesne
252754353a feat: add the "initial_headers" and "headers" events
Those events will be emitted before the response headers are written to
the socket:

- "initial_headers": on the first request of the connection
- "headers": on all requests (HTTP long-polling and WebSocket upgrade)

Syntax:

```js

server.on("initial_headers", (headers, req) => {
  headers["test"] = "123";
  headers["set-cookie"] = "mycookie=456";
});

server.on("headers", (headers, req) => {
  headers["test"] = "789";
});
```

Related:

- https://github.com/socketio/engine.io/issues/557
- https://github.com/socketio/socket.io/issues/3630
2021-04-30 14:38:11 +02:00
Damien Arrachequesne
7096e98a02 feat: add a "connection_error" event
The "connection_error" event will be emitted when one of the following
errors occurs:

- Transport unknown
- Session ID unknown
- Bad handshake method
- Bad request
- Forbidden
- Unsupported protocol version

Syntax:

```js
server.on("connection_error", (err) => {
  console.log(err.req);		// the request object
  console.log(err.code);	// the error code, for example 1
  console.log(err.message);	// the error message, for example "Session ID unknown"
  console.log(err.context);     // some additional error context
});
```

Related:

- https://github.com/socketio/socket.io/issues/3819
- https://github.com/socketio/engine.io/issues/576
2021-04-30 13:04:28 +02:00
Damien Arrachequesne
887ba06536 chore(release): 5.0.0
Diff: https://github.com/socketio/engine.io/compare/4.1.1...5.0.0
2021-03-10 10:20:02 +01:00
Damien Arrachequesne
edb734316f feat: remove dynamic require() with wsEngine
This change is necessary to get rid of:

> Critical dependency: the request of a dependency is an expression

when bundling the server with webpack.

BREAKING CHANGE: the syntax of the "wsEngine" option is updated

Before:

```js
const eioServer = require("engine.io")(httpServer, {
  wsEngine: "eiows"
});
```

After:

```js
const eioServer = require("engine.io")(httpServer, {
  wsEngine: require("eiows").Server
});
```

Related: https://github.com/socketio/engine.io/issues/609
2021-03-09 08:30:05 +01:00
Simone Mazzoni
868d89111d fix: set default protocol version to 3 (#616)
socket.io-client-swift libs version <=15.2.0, which uses protocol
version 3, do not explicitly add the EIO query parameter at transport
initialization. This omission leads the server to treat the client as
a client that supports the protocol version 4, previously set as
default, which is not correct for those versions of the client lib.

From socket.io-client-swift version v16.0.0 the EIO query parameter is
explicitly passed to specify the protocol version supported, but since
the allowEIO3 parameter aims to make the server compatible with
previous versions which in most of the cases are already used in
production and not easily upgradable, it makes more sense to default
the EIO version to 3 if not explicitly set by the client since the
newer client versions pass the EIO protocol version in query
parameters.

Related: https://github.com/socketio/socket.io/issues/3794
2021-03-09 08:23:36 +01:00
Damien Arrachequesne
5a7fa132c4 feat: increase the default value of pingTimeout
This value was updated from 60000 to 5000 in [1], included in
`engine.io@3.2.0` (Feb 2018).

The reasoning back then:

Some users experienced long delays between disconnection on the
server-side and on the client-side. The "disconnect" event would take a
long time to fire in the browser, probably due to a timer being
delayed. Hence the change.

That being said, the current value (5s) now causes unexpected
disconnections when a big payload is sent over a slow network, because
it prevents the ping-pong packets from being exchanged between the
client and the server. This can also happen when a synchronous task
blocks the server for more than 5 seconds.

The new value (20s) thus seems like a good balance between quick
disconnection detection and tolerance to various delays.

Note: pingInterval + pingTimeout is still below the threshold of React
Native, which complains if a timer is set with a delay of more than 1
minute.

[1]: 65b1ad1b8a

Related:

- https://github.com/socketio/socket.io/issues/2770
- https://github.com/socketio/socket.io/issues/2769
- https://github.com/socketio/socket.io/issues/3054
- https://github.com/socketio/socket.io/issues/3376
2021-03-02 08:49:58 +01:00
Damien Arrachequesne
9534355b89 chore(release): 4.1.1
Diff: https://github.com/socketio/engine.io/compare/4.1.0...4.1.1
2021-02-02 10:51:04 +01:00
Damien Arrachequesne
ae840fa8ed chore: point towards the master branch for the CI badge
Reference: https://docs.github.com/en/actions/managing-workflow-runs/adding-a-workflow-status-badge#using-the-branch-parameter
2021-02-02 10:49:24 +01:00
Damien Arrachequesne
ff2b8aba48 fix: do not reset the ping timer after upgrade
There was two issues with this behavior:

- v3 clients (with allowEIO3: true) were also receiving a "ping" after
a successful upgrade, which is incorrect (in v3, it's the client that
sends the "ping", and the server answers with a "pong")

- the ping timer is not reset after upgrade on the client-side, so an
upgrade which took longer than the `pingTimeout` duration could lead to
a "ping timeout" error on the client-side

I think the latter issue is present since the initial implementation.

Related: https://github.com/socketio/socket.io-client-swift/pull/1309#issuecomment-768475704
2021-02-02 10:48:02 +01:00