@styris-ame/y-engineio 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +125 -0
- package/dist/y-engineio.cjs +591 -0
- package/dist/y-engineio.cjs.map +1 -0
- package/dist/y-engineio.d.ts +135 -0
- package/dist/y-engineio.d.ts.map +1 -0
- package/package.json +61 -0
- package/src/y-engineio.js +554 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Kevin Jahns <kevin.jahns@protonmail.com>.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
# @styris-ame/y-engineio :tophat:
|
|
2
|
+
> Engine.IO Provider for Yjs
|
|
3
|
+
|
|
4
|
+
A port of [`y-websocket`](https://github.com/yjs/y-websocket) that syncs a
|
|
5
|
+
`Y.Doc` over [Engine.IO](https://github.com/socketio/engine.io) (the transport
|
|
6
|
+
layer underneath Socket.IO) instead of a raw WebSocket. It keeps the same sync +
|
|
7
|
+
awareness protocol and the same cross-tab BroadcastChannel sync.
|
|
8
|
+
|
|
9
|
+
Why Engine.IO? It negotiates a transport (HTTP long-polling → WebSocket →
|
|
10
|
+
WebTransport) and transparently falls back when WebSockets are blocked by a
|
|
11
|
+
proxy, which a raw `WebSocket` cannot do.
|
|
12
|
+
|
|
13
|
+
This is the **Yjs v13 line** (`1.x`, dist-tag `yjs13`): it targets **Yjs v13**
|
|
14
|
+
(`yjs`, `y-protocols`, `lib0@^0.2`).
|
|
15
|
+
|
|
16
|
+
## Yjs v13 vs v14
|
|
17
|
+
|
|
18
|
+
This `1.x` line (dist-tag `yjs13`) targets **Yjs v13** (`yjs`, `y-protocols`,
|
|
19
|
+
`lib0@^0.2`). For **Yjs v14** (`@y/y`, `@y/protocols`) install the `2.x` line
|
|
20
|
+
(dist-tag `yjs14`): `npm i @styris-ame/y-engineio@yjs14 @y/y`.
|
|
21
|
+
|
|
22
|
+
## Quick Start
|
|
23
|
+
|
|
24
|
+
```sh
|
|
25
|
+
npm i @styris-ame/y-engineio@yjs13 engine.io-client yjs
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
```js
|
|
29
|
+
import * as Y from 'yjs'
|
|
30
|
+
import { EngineIOProvider } from '@styris-ame/y-engineio'
|
|
31
|
+
|
|
32
|
+
const doc = new Y.Doc()
|
|
33
|
+
// note: serverUrl is just the origin — the room name is NOT part of the URL
|
|
34
|
+
const provider = new EngineIOProvider('http://localhost:1234', 'my-roomname', doc)
|
|
35
|
+
|
|
36
|
+
provider.on('status', event => {
|
|
37
|
+
console.log(event.status) // "connecting" | "connected" | "disconnected"
|
|
38
|
+
})
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## How it differs from y-websocket
|
|
42
|
+
|
|
43
|
+
| | y-websocket | @styris-ame/y-engineio |
|
|
44
|
+
|---|---|---|
|
|
45
|
+
| transport | raw `WebSocket` | engine.io-client `Socket` (polling/ws/webtransport) |
|
|
46
|
+
| URL | `serverUrl/roomname?params` | `serverUrl` only; params become engine.io `query` |
|
|
47
|
+
| room selection | encoded in the URL path | sent as the `room` handshake query param (see below) |
|
|
48
|
+
| socket field | `provider.ws` | `provider.engine` |
|
|
49
|
+
|
|
50
|
+
Everything else — the `messageSync` / `messageAwareness` / `messageAuth` /
|
|
51
|
+
`messageQueryAwareness` protocol, exponential-backoff reconnect, the
|
|
52
|
+
`socketTimeout` watchdog, `sync` events, and cross-tab BroadcastChannel sync —
|
|
53
|
+
is unchanged from y-websocket.
|
|
54
|
+
|
|
55
|
+
## Wire protocol (for server authors)
|
|
56
|
+
|
|
57
|
+
Every frame is binary (engine.io binary message) and starts with a
|
|
58
|
+
[lib0](https://github.com/dmonad/lib0)-encoded `varUint` message type:
|
|
59
|
+
|
|
60
|
+
| type | const | direction | payload |
|
|
61
|
+
|---|---|---|---|
|
|
62
|
+
| `0` | `messageSync` | both | `y-protocols/sync` message |
|
|
63
|
+
| `1` | `messageAwareness` | both | `varUint8Array` awareness update |
|
|
64
|
+
| `2` | `messageAuth` | server→client | `y-protocols/auth` message |
|
|
65
|
+
| `3` | `messageQueryAwareness` | both | (empty) |
|
|
66
|
+
|
|
67
|
+
### Room handshake
|
|
68
|
+
|
|
69
|
+
Because engine.io has a single connection endpoint (no per-room URL), the client
|
|
70
|
+
declares its room as the `room` **handshake query parameter**, alongside any
|
|
71
|
+
user-supplied `params`. engine.io-client URL-encodes query values, so the room
|
|
72
|
+
name needs no manual escaping.
|
|
73
|
+
|
|
74
|
+
The server reads `room` from the handshake query (e.g. engine.io's
|
|
75
|
+
`allowRequest` middleware or `socket.request`) and binds the connection to that
|
|
76
|
+
room before any frames are exchanged. Because it is available at handshake time,
|
|
77
|
+
the server can **reject an unknown or unauthorized room immediately** instead of
|
|
78
|
+
accepting the socket and waiting for a first frame. After the connection opens
|
|
79
|
+
the client proceeds exactly like y-websocket: it sends sync step 1 and its local
|
|
80
|
+
awareness state.
|
|
81
|
+
|
|
82
|
+
## API
|
|
83
|
+
|
|
84
|
+
```js
|
|
85
|
+
import { EngineIOProvider } from '@styris-ame/y-engineio'
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
**`new EngineIOProvider(serverUrl: string, room: string, ydoc: Y.Doc, opts?)`**
|
|
89
|
+
|
|
90
|
+
`serverUrl` is the engine.io server origin (e.g. `http://localhost:1234`). The
|
|
91
|
+
following `opts` are supported:
|
|
92
|
+
|
|
93
|
+
```js
|
|
94
|
+
{
|
|
95
|
+
// connect immediately; set false to call provider.connect() yourself
|
|
96
|
+
connect: true,
|
|
97
|
+
// query-string params -> engine.io `query`
|
|
98
|
+
params: {}, // Object<string,string>
|
|
99
|
+
// forwarded verbatim to the engine.io-client Socket
|
|
100
|
+
// (e.g. path, transports, withCredentials, extraHeaders, ...)
|
|
101
|
+
engineOptions: {},
|
|
102
|
+
// override the engine.io-client Socket constructor (e.g. for tests)
|
|
103
|
+
EngineClass: Socket,
|
|
104
|
+
// existing awareness instance
|
|
105
|
+
awareness: new awarenessProtocol.Awareness(ydoc),
|
|
106
|
+
// resend sync step 1 every N ms (-1 disables)
|
|
107
|
+
resyncInterval: -1,
|
|
108
|
+
// max backoff between reconnects (exponential)
|
|
109
|
+
maxBackoffTime: 2500,
|
|
110
|
+
// disable cross-tab BroadcastChannel sync
|
|
111
|
+
disableBc: false,
|
|
112
|
+
// close + reconnect if no message received for this long
|
|
113
|
+
socketTimeout: /* ~1.5 * awareness outdatedTimeout */
|
|
114
|
+
}
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
Instance fields & events match y-websocket, except the `ws`-prefixed fields are
|
|
118
|
+
renamed: `connected`, `connecting`, `shouldConnect`, `bcconnected`, `synced`,
|
|
119
|
+
`params`, `connect()`, `disconnect()`, `destroy()`, and the `status`, `sync`,
|
|
120
|
+
`connection-close`, `connection-error` events. The live socket is exposed as
|
|
121
|
+
`provider.engine` (an engine.io-client `Socket`) rather than `provider.ws`.
|
|
122
|
+
|
|
123
|
+
## License
|
|
124
|
+
|
|
125
|
+
[The MIT License](./LICENSE) — port of y-websocket © Kevin Jahns
|