Commit Graph

24 Commits

Author SHA1 Message Date
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
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
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
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
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
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
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
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
e122e4be7b refactor: add additional types
Merged from https://github.com/socketio/engine.io/pull/630
2022-01-18 17:55:55 +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
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
c0d6eaa1ba chore: migrate to TypeScript
Related: https://github.com/socketio/engine.io/issues/510
2021-10-08 14:55:30 +02:00