Commit Graph

443 Commits

Author SHA1 Message Date
Damien Arrachequesne
6d8a0bea49 refactor: move the req attribute to the polling class 2024-06-21 14:03:22 +02:00
Damien Arrachequesne
c310b7b6b6 refactor: improve types 2024-06-21 14:03:01 +02:00
Damien Arrachequesne
362bc78191 fix: properly call the send callback during upgrade
The "drain" event (added in [1]) had two different meanings:

- the transport is ready to be written
- the packets are sent over the wire

For the WebSocket and the WebTransport transports, those two events
happen at the same time, but this is not the case for the HTTP
long-polling transport:

- the transport is ready to be written when the client sends a GET request
- the packets are sent over the wire when the server responds to the GET request

Which caused an issue with send callbacks during an upgrade, since the
packets were written but the client would not open a new GET request.

There are now two distinct events: "ready" and "drain"

Related: https://github.com/socketio/engine.io/issues/695

[1]: 2a93f06e27
2024-06-21 11:47:41 +02:00
Damien Arrachequesne
6b9e3e458e refactor: improve types 2024-06-18 17:37:12 +02:00
Damien Arrachequesne
f521cbab6d refactor: simplify the heartbeat code 2024-06-18 17:32:06 +02:00
Damien Arrachequesne
5359bae683 perf: do not reset the hearbeat timer on each packet
This behavior was added in [1]. However, there are two problems:

- a new timer is allocated every time a packet is received, which is
wasteful

- the next heartbeat is not actually delayed, since it's the timeout
timer which gets reset, and not the interval timer

Note: delaying the next heartbeat would be a breaking change.

[1]: be7b4e7478
2024-06-18 17:17:17 +02:00
Damien Arrachequesne
9a68c8ce93 perf(websocket): use bound callbacks
Instead of allocating one temporary function for each WebSocket
`send()` call.

Regarding the test removal, the permessage-deflate threshold was
implemented in the "ws" package in [1], so it's not needed anymore.

[1]: 6b3904b42d
2024-06-17 17:47:46 +02:00
Damien Arrachequesne
62f59b6cf3 refactor: remove unnecessary array allocation
If the `packetsFn` array is empty, there is no need to allocate one new
array.
2024-06-14 01:07:48 +02:00
Damien Arrachequesne
407c3ad236 refactor: simplify the handling of the "drain" event
The two event handlers are merged into one.
2024-06-14 00:30:14 +02:00
Damien Arrachequesne
ef1c4c8bb7 refactor: remove the wsPreEncoded option
The wsPreEncoded option was added in the `socket.io-adapter` package
when broadcasting a message to multiple clients.

It was removed in [1] and is now superseded by the `wsPreEncodedFrame`
option, which directly computes the WebSocket frame once for all
clients (see [2]).

[1]: 88eee5948a
[2]: 5f7b47d40f
2024-06-13 23:55:16 +02:00
Jonathan Perret
fc21c4a05f fix: fix websocket and webtransport send callbacks (#699)
With the `websocket` transport, the callbacks which indicate that the
packets are actually written were not properly called.

Example:

```js
socket.send("hello", () => {
  // the message has been written to the underlying transport
});
```

The bug was caused by the `websocket` transport (and `webtransport` as
well) having its `supportsFraming` property set to `true`, despite
having been changed in [1] to emit a single `drain` event for each
batch of messages written to the transport like the `polling` transport
always did. Note that although [1] is partially reverted in [2], the
new `drain` event behavior is preserved as called out in that commit's
message.

The `supportsFraming` attribute was introduced in [3] (amended by [4])
as a way to distinguish transports that emit one `drain` per message
from those that emit one `drain` per batch. Since the delivery of
`send` callbacks depends on matching `drain` events with
`transport.send` calls, that distinction is vital to correct behavior.

However, now that all transports have converged to "one `drain` per
batch" behavior, this `supportsFraming` property can be retired (and
the code for calling callbacks simplified).

[1]: https://github.com/socketio/engine.io/pull/618
[2]: a65a047526
[3]: https://github.com/socketio/engine.io/pull/130
[4]: https://github.com/socketio/engine.io/pull/132

Related: https://github.com/socketio/engine.io/issues/698
2024-06-13 23:02:22 +02:00
Jonathan Perret
0efa04b584 fix(types): make socket.request writable (#697)
Related: https://github.com/socketio/engine.io/issues/696
2024-02-23 10:21:43 +01:00
Damien Arrachequesne
39937f8f4d refactor: minor cleanups 2023-11-09 12:18:55 +01:00
Damien Arrachequesne
43c1c1c1e2 refactor: simplify code 2023-11-09 12:13:15 +01:00
Damien Arrachequesne
3b5e79ef79 refactor: remove useless references
Those timers are only used during the upgrade, so there is no need to
keep those references in memory.
2023-11-09 12:05:38 +01:00
Damien Arrachequesne
f27a6c3501 refactor: remove useless reference
A reference to the initial IncomingMessage object (the first HTTP
request of the session) is kept in memory by default (`socket.request`),
so its attached ServerResponse object (`req.res`) would not be
garbage-collected. This will now be the case.

Note: the IncomingMessage object is needed in two cases:

- when working with the `express-session` middleware (`request.session`)
- when fetching the certificate of the client with `request.socket.getPeerCertificate()`

That's why removing it would be a breaking change.
2023-11-09 11:45:43 +01:00
Damien Arrachequesne
9545b44b3c refactor: add cache-control header in the polling response
This header should not be needed since the client already includes a
cache busting query parameter ("t"), but a misconfigured CDN could
ignore the query parameters and cache the server response.

Related: https://github.com/socketio/socket.io/issues/4842
2023-10-05 17:19:08 +02:00
Damien Arrachequesne
ff1c861548 fix(webtransport): properly handle abruptly closed connections
Refreshing the page with a client connected with WebTransport would
trigger the following exception:

> node:internal/process/promises:288
>            triggerUncaughtException(err, true /* fromPromise */);
>            ^
>
> [UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason "0".] {
>   code: 'ERR_UNHANDLED_REJECTION'
> }

Related: https://github.com/socketio/engine.io/issues/688
2023-10-05 16:57:33 +02:00
Damien Arrachequesne
a306db09e8 fix(webtransport): add proper framing
WebTransport being a stream-based protocol, the chunking boundaries are
not necessarily preserved. That's why we need a header indicating the
type of the payload (plain text or binary) and its length.

We will use a format inspired by the WebSocket frame:

- first bit indicates whether the payload is binary
- the next 7 bits are either:
  - 125 or less: that's the length of the payload
  - 126: the next 2 bytes represent the length of the payload
  - 127: the next 8 bytes represent the length of the payload

Reference: https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_servers#decoding_payload_length

Related:

- https://github.com/socketio/engine.io/issues/687
- https://github.com/socketio/engine.io/issues/688
2023-08-02 01:00:42 +02:00
Ben Weintraub
6dd2bc4f68 fix: prevent crash when accessing TextDecoder (#684)
The TextDecoder object was added on the global object in Node.js
v11.0.0, so older versions would throw:

> ReferenceError: TextDecoder is not defined

Reference: https://nodejs.org/api/util.html#new-textdecoderencoding-options
2023-06-27 09:01:26 +02:00
Damien Arrachequesne
1bfa9cd088 refactor: adapt to latest uWebSockets.js changes
Reference: https://github.com/uNetworking/uWebSockets.js/releases
2023-06-16 10:19:58 +02:00
Damien Arrachequesne
123b68c04f feat: add support for WebTransport
Reference: https://developer.mozilla.org/en-US/docs/Web/API/WebTransport
2023-06-11 09:42:45 +02:00
Sean Oxley
3144d27458 fix(uws): discard any write to an aborted uWS response (#682)
This bug only exists for polling transport connections running on top
of uWS.

If the remote client abruptly disconnects (thus aborting the request)
while the server is waiting on an asynchronous operation such as
compression, the server may attempt to write a response via the aborted
response object. This causes an uncaught exception to be thrown.
2023-05-31 14:27:40 +02:00
Damien Arrachequesne
fc480b4f30 fix: prevent crash when provided with an invalid query param
A specially crafted request could lead to the following exception:

> TypeError: Cannot read properties of undefined (reading 'handlesUpgrades')
>    at Server.onWebSocket (build/server.js:515:67)

This bug was introduced in [1], released in version 5.1.0 and included
in version 4.1.0 of the `socket.io` parent package. Older versions are
not impacted.

[1]: 7096e98a02
2023-05-02 01:07:40 +02:00
Damien Arrachequesne
0141951185 refactor(types): ensure compatibility with Express middlewares
In order to prevent issues like:

> error TS2345: Argument of type 'RequestHandler<ParamsDictionary, any, any, ParsedQs, Record<string, any>>' is not assignable to parameter of type 'Middleware'.
>  Types of parameters 'req' and 'req' are incompatible.
>  Type 'IncomingMessage' is missing the following properties from type 'Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>': get, header, accepts, acceptsCharsets, and 29 more.
>
>  io.engine.use(sessionMiddleware);
                 ~~~~~~~~~~~~~~~~~

Related: https://github.com/socketio/socket.io/issues/4644

We could also have use the RequestHandler type from the
@types/express-serve-static-core package, but that would add 5 new
dependencies.

See also: https://github.com/socketio/engine.io/issues/673
2023-05-02 00:51:33 +02:00
Damien Arrachequesne
8b22162903 fix(uws): prevent crash when using with middlewares
The class used to accumulate the response headers did not expose the
exact same API as its wrapped type, which could lead to the following
error in some rare cases:

> TypeError: Cannot read properties of undefined (reading 'end')
>    at Polling.onDataRequest (build/transports-uws/polling.js:109:53)
>    at Polling.onRequest (build/transports-uws/polling.js:47:18)
>    at callback (build/userver.js:94:56)
>    at uServer.verify (build/server.js:152:9)

Related: https://github.com/socketio/socket.io/issues/4643
2023-05-02 00:50:34 +02:00
Ciel
93957828be fix: include error handling for Express middlewares (#674)
Following 24786e77c5.

Reference: https://expressjs.com/en/guide/error-handling.html
2023-05-02 00:00:47 +02:00
Damien Arrachequesne
911d0e3575 refactor: return HTTP 400 upon invalid request overlap
In both cases, the error comes from the client as it should not send
multiple concurrent requests, so a HTTP 4xx code is mandated.

Related: https://github.com/socketio/engine.io/issues/650
2023-05-01 07:42:43 +02:00
Asger Hautop Drewsen
bd6d4713b0 fix(typings): make clientsCount public (#675)
Related: https://github.com/socketio/engine.io/issues/672
2023-04-19 23:25:16 +03:00
Igor Lins e Silva
6e78489486 refactor: export BaseServer class (#669)
Related: https://github.com/socketio/socket.io/issues/4621
2023-02-17 23:24:50 +01:00
Damien Arrachequesne
24786e77c5 feat: add support for Express middlewares
This commit implements middlewares at the Engine.IO level, because
Socket.IO middlewares are meant for namespace authorization and are not
executed during a classic HTTP request/response cycle.

A workaround was possible by using the allowRequest option and the
"headers" event, but this feels way cleaner and works with upgrade
requests too.

Syntax:

```js
engine.use((req, res, next) => {
  // do something

  next();
});

// with express-session
import session from "express-session";

engine.use(session({
  secret: "keyboard cat",
  resave: false,
  saveUninitialized: true,
  cookie: { secure: true }
});

// with helmet
import helmet from "helmet";

engine.use(helmet());
```

Related:

- https://github.com/socketio/engine.io/issues/668
- https://github.com/socketio/engine.io/issues/651
- https://github.com/socketio/socket.io/issues/4609
- https://github.com/socketio/socket.io/issues/3933
- a lot of other issues asking for compatibility with express-session
2023-02-06 17:01:27 +01:00
Damien Arrachequesne
69603b955a refactor: make the compress option optional
The compress option was inadvertently made mandatory in [1].

[1]: 6d87a4065a
2023-01-12 08:15:21 +01:00
Damien Arrachequesne
a65a047526 fix: wait for all packets to be sent before closing the WebSocket connection
This reverts commit [1], which was included in `engine.io@5.1.0` and
`socket.io@4.1.0`.

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

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

[1]: ad5306aeae

Related: https://github.com/socketio/engine.io/issues/648
2023-01-10 16:42:12 +01:00
Damien Arrachequesne
bc98bf1232 refactor: bump prettier to version 2.8.1
This major bump creates a lot of noise, but it is necessary for
prettier to be able to parse new syntax such as:

- typed imports: `import { type xxx } from ...`
- private attributes: `class A { #b; #c() {} }`
2023-01-10 15:22:57 +01:00
Damien Arrachequesne
33dc073172 docs: add some TODOs for the next major release 2023-01-10 14:57:58 +01:00
iifawzi
d0fd4746af feat: add the "addTrailingSlash" option (#655)
The "addTrailingSlash" option allows to control whether a trailing
slash is added to the path of the HTTP requests:

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

Related: 21a6e1219a

Signed-off-by: iifawzi <iifawzie@gmail.com>
2023-01-10 14:51:31 +01:00
Damien Arrachequesne
5e34722b0b perf: add the wsPreEncodedFrame option
This optimization is only applied if:

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

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

Related: 5f7b47d40f
2023-01-09 10:34:25 +01:00
Damien Arrachequesne
6d87a4065a refactor: add types to socket.send()
Related: https://github.com/socketio/engine.io/issues/645
2022-12-06 00:31:02 +01:00
Damien Arrachequesne
3d28229cf0 docs: add note about socket.id
Related: https://github.com/socketio/engine.io/issues/644
2022-12-05 23:57:32 +01:00
Jonathan Neve
425e833ab1 fix: catch errors when destroying invalid upgrades (#658)
Before this change, receiving an HTTP2 upgrade would make the server
crash:

> Error: read ECONNRESET
>    at TCP.onStreamRead (node:internal/stream_base_commons:217:20) {
>  errno: -104,
>  code: 'ECONNRESET',
>  syscall: 'read'
> }

This can be reproduced with Node.js v14.15.3, v16.18.1 and v18.12.1.
2022-11-20 01:55:11 +01:00
Lam Wei Li
917d1d29e1 refactor: replace deprecated String.prototype.substr() (#646)
`.substr()` is deprecated so we replace it with `.slice()` which works
similarily but isn't deprecated.

See also: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substr

Signed-off-by: Lam Wei Li <peteriman@mail.com>
2022-06-06 08:42:45 +02:00
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
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
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
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
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