y-partyserver 2.1.1 → 2.1.3
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/dist/chunk-C0xms8kb.cjs +34 -0
- package/dist/provider/index.cjs +489 -0
- package/dist/provider/index.cjs.map +1 -0
- package/dist/provider/index.d.cts +133 -0
- package/dist/provider/index.js +422 -285
- package/dist/provider/react.cjs +34 -0
- package/dist/provider/react.cjs.map +1 -0
- package/dist/provider/react.d.cts +15 -0
- package/dist/provider/react.js +24 -13
- package/dist/server/index.cjs +366 -0
- package/dist/server/index.cjs.map +1 -0
- package/dist/server/index.d.cts +48 -0
- package/dist/server/index.js +329 -227
- package/package.json +23 -11
package/dist/provider/index.js
CHANGED
|
@@ -18,310 +18,447 @@ const messageAwareness = 1;
|
|
|
18
18
|
const messageAuth = 2;
|
|
19
19
|
const DEFAULT_DISABLE_BC = typeof window === "undefined";
|
|
20
20
|
const messageHandlers = [];
|
|
21
|
-
messageHandlers[messageSync] = (
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
21
|
+
messageHandlers[messageSync] = (
|
|
22
|
+
encoder,
|
|
23
|
+
decoder,
|
|
24
|
+
provider,
|
|
25
|
+
emitSynced,
|
|
26
|
+
_messageType
|
|
27
|
+
) => {
|
|
28
|
+
encoding.writeVarUint(encoder, messageSync);
|
|
29
|
+
const syncMessageType = syncProtocol.readSyncMessage(
|
|
30
|
+
decoder,
|
|
31
|
+
encoder,
|
|
32
|
+
provider.doc,
|
|
33
|
+
provider
|
|
34
|
+
);
|
|
35
|
+
if (
|
|
36
|
+
emitSynced &&
|
|
37
|
+
syncMessageType === syncProtocol.messageYjsSyncStep2 &&
|
|
38
|
+
!provider.synced
|
|
39
|
+
)
|
|
40
|
+
provider.synced = true;
|
|
25
41
|
};
|
|
26
|
-
messageHandlers[messageQueryAwareness] = (
|
|
27
|
-
|
|
28
|
-
|
|
42
|
+
messageHandlers[messageQueryAwareness] = (
|
|
43
|
+
encoder,
|
|
44
|
+
_decoder,
|
|
45
|
+
provider,
|
|
46
|
+
_emitSynced,
|
|
47
|
+
_messageType
|
|
48
|
+
) => {
|
|
49
|
+
encoding.writeVarUint(encoder, messageAwareness);
|
|
50
|
+
encoding.writeVarUint8Array(
|
|
51
|
+
encoder,
|
|
52
|
+
awarenessProtocol.encodeAwarenessUpdate(
|
|
53
|
+
provider.awareness,
|
|
54
|
+
Array.from(provider.awareness.getStates().keys())
|
|
55
|
+
)
|
|
56
|
+
);
|
|
29
57
|
};
|
|
30
|
-
messageHandlers[messageAwareness] = (
|
|
31
|
-
|
|
58
|
+
messageHandlers[messageAwareness] = (
|
|
59
|
+
_encoder,
|
|
60
|
+
decoder,
|
|
61
|
+
provider,
|
|
62
|
+
_emitSynced,
|
|
63
|
+
_messageType
|
|
64
|
+
) => {
|
|
65
|
+
awarenessProtocol.applyAwarenessUpdate(
|
|
66
|
+
provider.awareness,
|
|
67
|
+
decoding.readVarUint8Array(decoder),
|
|
68
|
+
provider
|
|
69
|
+
);
|
|
32
70
|
};
|
|
33
|
-
messageHandlers[messageAuth] = (
|
|
34
|
-
|
|
71
|
+
messageHandlers[messageAuth] = (
|
|
72
|
+
_encoder,
|
|
73
|
+
decoder,
|
|
74
|
+
provider,
|
|
75
|
+
_emitSynced,
|
|
76
|
+
_messageType
|
|
77
|
+
) => {
|
|
78
|
+
authProtocol.readAuthMessage(decoder, provider.doc, (_ydoc, reason) =>
|
|
79
|
+
permissionDeniedHandler(provider, reason)
|
|
80
|
+
);
|
|
35
81
|
};
|
|
36
82
|
function permissionDeniedHandler(provider, reason) {
|
|
37
|
-
|
|
83
|
+
console.warn(`Permission denied to access ${provider.url}.\n${reason}`);
|
|
38
84
|
}
|
|
39
85
|
function readMessage(provider, buf, emitSynced) {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
86
|
+
const decoder = decoding.createDecoder(buf);
|
|
87
|
+
const encoder = encoding.createEncoder();
|
|
88
|
+
const messageType = decoding.readVarUint(decoder);
|
|
89
|
+
const messageHandler = provider.messageHandlers[messageType];
|
|
90
|
+
if (messageHandler)
|
|
91
|
+
messageHandler(encoder, decoder, provider, emitSynced, messageType);
|
|
92
|
+
else console.error("Unable to compute message");
|
|
93
|
+
return encoder;
|
|
47
94
|
}
|
|
48
95
|
function setupWS(provider) {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
96
|
+
if (provider.shouldConnect && provider.ws === null) {
|
|
97
|
+
if (!provider._WS)
|
|
98
|
+
throw new Error(
|
|
99
|
+
"No WebSocket implementation available, did you forget to pass options.WebSocketPolyfill?"
|
|
100
|
+
);
|
|
101
|
+
const websocket = new provider._WS(provider.url);
|
|
102
|
+
websocket.binaryType = "arraybuffer";
|
|
103
|
+
provider.ws = websocket;
|
|
104
|
+
provider.wsconnecting = true;
|
|
105
|
+
provider.wsconnected = false;
|
|
106
|
+
provider.synced = false;
|
|
107
|
+
websocket.addEventListener("message", (event) => {
|
|
108
|
+
if (typeof event.data === "string") {
|
|
109
|
+
if (event.data.startsWith("__YPS:")) {
|
|
110
|
+
const customMessage = event.data.slice(6);
|
|
111
|
+
provider.emit("custom-message", [customMessage]);
|
|
112
|
+
}
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
provider.wsLastMessageReceived = time.getUnixTime();
|
|
116
|
+
const encoder = readMessage(provider, new Uint8Array(event.data), true);
|
|
117
|
+
if (encoding.length(encoder) > 1)
|
|
118
|
+
websocket.send(encoding.toUint8Array(encoder));
|
|
119
|
+
});
|
|
120
|
+
websocket.addEventListener("error", (event) => {
|
|
121
|
+
provider.emit("connection-error", [event, provider]);
|
|
122
|
+
});
|
|
123
|
+
websocket.addEventListener("close", (event) => {
|
|
124
|
+
provider.emit("connection-close", [event, provider]);
|
|
125
|
+
provider.ws = null;
|
|
126
|
+
provider.wsconnecting = false;
|
|
127
|
+
if (provider.wsconnected) {
|
|
128
|
+
provider.wsconnected = false;
|
|
129
|
+
provider.synced = false;
|
|
130
|
+
const removedClients = Array.from(
|
|
131
|
+
provider.awareness.getStates().keys()
|
|
132
|
+
).filter((client) => client !== provider.doc.clientID);
|
|
133
|
+
awarenessProtocol.removeAwarenessStates(
|
|
134
|
+
provider.awareness,
|
|
135
|
+
removedClients,
|
|
136
|
+
provider
|
|
137
|
+
);
|
|
138
|
+
for (const clientID of removedClients)
|
|
139
|
+
provider.awareness.meta.delete(clientID);
|
|
140
|
+
provider.emit("status", [{ status: "disconnected" }]);
|
|
141
|
+
} else provider.wsUnsuccessfulReconnects++;
|
|
142
|
+
setTimeout(
|
|
143
|
+
setupWS,
|
|
144
|
+
math.min(
|
|
145
|
+
math.pow(2, provider.wsUnsuccessfulReconnects) * 100,
|
|
146
|
+
provider.maxBackoffTime
|
|
147
|
+
),
|
|
148
|
+
provider
|
|
149
|
+
);
|
|
150
|
+
});
|
|
151
|
+
websocket.addEventListener("open", () => {
|
|
152
|
+
provider.wsLastMessageReceived = time.getUnixTime();
|
|
153
|
+
provider.wsconnecting = false;
|
|
154
|
+
provider.wsconnected = true;
|
|
155
|
+
provider.wsUnsuccessfulReconnects = 0;
|
|
156
|
+
provider.emit("status", [{ status: "connected" }]);
|
|
157
|
+
const encoder = encoding.createEncoder();
|
|
158
|
+
encoding.writeVarUint(encoder, messageSync);
|
|
159
|
+
syncProtocol.writeSyncStep1(encoder, provider.doc);
|
|
160
|
+
websocket.send(encoding.toUint8Array(encoder));
|
|
161
|
+
if (provider.awareness.getLocalState() !== null) {
|
|
162
|
+
provider.awareness.setLocalState(provider.awareness.getLocalState());
|
|
163
|
+
const encoderAwarenessState = encoding.createEncoder();
|
|
164
|
+
encoding.writeVarUint(encoderAwarenessState, messageAwareness);
|
|
165
|
+
encoding.writeVarUint8Array(
|
|
166
|
+
encoderAwarenessState,
|
|
167
|
+
awarenessProtocol.encodeAwarenessUpdate(provider.awareness, [
|
|
168
|
+
provider.doc.clientID
|
|
169
|
+
])
|
|
170
|
+
);
|
|
171
|
+
websocket.send(encoding.toUint8Array(encoderAwarenessState));
|
|
172
|
+
}
|
|
173
|
+
});
|
|
174
|
+
provider.emit("status", [{ status: "connecting" }]);
|
|
175
|
+
}
|
|
106
176
|
}
|
|
107
177
|
function broadcastMessage(provider, buf) {
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
178
|
+
const ws = provider.ws;
|
|
179
|
+
if (provider.wsconnected && ws && ws.readyState === ws.OPEN) ws.send(buf);
|
|
180
|
+
if (provider.bcconnected) bc.publish(provider.bcChannel, buf, provider);
|
|
111
181
|
}
|
|
112
182
|
const DefaultWebSocket = typeof WebSocket === "undefined" ? null : WebSocket;
|
|
113
183
|
/**
|
|
114
|
-
* Websocket Provider for Yjs. Creates a websocket connection to sync the shared document.
|
|
115
|
-
* The document name is attached to the provided url. I.e. the following example
|
|
116
|
-
* creates a websocket connection to http://localhost:1234/my-document-name
|
|
117
|
-
*
|
|
118
|
-
* @example
|
|
119
|
-
* import * as Y from 'yjs'
|
|
120
|
-
* import { WebsocketProvider } from 'y-websocket'
|
|
121
|
-
* const doc = new Y.Doc()
|
|
122
|
-
* const provider = new WebsocketProvider('http://localhost:1234', 'my-document-name', doc)
|
|
123
|
-
*
|
|
124
|
-
* @extends {Observable<string>}
|
|
125
|
-
*/
|
|
184
|
+
* Websocket Provider for Yjs. Creates a websocket connection to sync the shared document.
|
|
185
|
+
* The document name is attached to the provided url. I.e. the following example
|
|
186
|
+
* creates a websocket connection to http://localhost:1234/my-document-name
|
|
187
|
+
*
|
|
188
|
+
* @example
|
|
189
|
+
* import * as Y from 'yjs'
|
|
190
|
+
* import { WebsocketProvider } from 'y-websocket'
|
|
191
|
+
* const doc = new Y.Doc()
|
|
192
|
+
* const provider = new WebsocketProvider('http://localhost:1234', 'my-document-name', doc)
|
|
193
|
+
*
|
|
194
|
+
* @extends {Observable<string>}
|
|
195
|
+
*/
|
|
126
196
|
var WebsocketProvider = class extends Observable {
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
197
|
+
maxBackoffTime;
|
|
198
|
+
bcChannel;
|
|
199
|
+
url;
|
|
200
|
+
roomname;
|
|
201
|
+
doc;
|
|
202
|
+
_WS;
|
|
203
|
+
awareness;
|
|
204
|
+
wsconnected;
|
|
205
|
+
wsconnecting;
|
|
206
|
+
bcconnected;
|
|
207
|
+
disableBc;
|
|
208
|
+
wsUnsuccessfulReconnects;
|
|
209
|
+
messageHandlers;
|
|
210
|
+
_synced;
|
|
211
|
+
ws;
|
|
212
|
+
wsLastMessageReceived;
|
|
213
|
+
shouldConnect;
|
|
214
|
+
_resyncInterval;
|
|
215
|
+
_bcSubscriber;
|
|
216
|
+
_updateHandler;
|
|
217
|
+
_awarenessUpdateHandler;
|
|
218
|
+
_unloadHandler;
|
|
219
|
+
constructor(
|
|
220
|
+
serverUrl,
|
|
221
|
+
roomname,
|
|
222
|
+
doc,
|
|
223
|
+
{
|
|
224
|
+
connect = true,
|
|
225
|
+
awareness = new awarenessProtocol.Awareness(doc),
|
|
226
|
+
params = {},
|
|
227
|
+
isPrefixedUrl = false,
|
|
228
|
+
WebSocketPolyfill = DefaultWebSocket,
|
|
229
|
+
resyncInterval = -1,
|
|
230
|
+
maxBackoffTime = 2500,
|
|
231
|
+
disableBc = DEFAULT_DISABLE_BC
|
|
232
|
+
} = {}
|
|
233
|
+
) {
|
|
234
|
+
super();
|
|
235
|
+
while (serverUrl[serverUrl.length - 1] === "/")
|
|
236
|
+
serverUrl = serverUrl.slice(0, serverUrl.length - 1);
|
|
237
|
+
const encodedParams = url.encodeQueryParams(params);
|
|
238
|
+
this.maxBackoffTime = maxBackoffTime;
|
|
239
|
+
this.bcChannel = `${serverUrl}/${roomname}`;
|
|
240
|
+
this.url = isPrefixedUrl
|
|
241
|
+
? serverUrl
|
|
242
|
+
: `${serverUrl}/${roomname}${encodedParams.length === 0 ? "" : `?${encodedParams}`}`;
|
|
243
|
+
this.roomname = roomname;
|
|
244
|
+
this.doc = doc;
|
|
245
|
+
this._WS = WebSocketPolyfill;
|
|
246
|
+
this.awareness = awareness;
|
|
247
|
+
this.wsconnected = false;
|
|
248
|
+
this.wsconnecting = false;
|
|
249
|
+
this.bcconnected = false;
|
|
250
|
+
this.disableBc = disableBc;
|
|
251
|
+
this.wsUnsuccessfulReconnects = 0;
|
|
252
|
+
this.messageHandlers = messageHandlers.slice();
|
|
253
|
+
this._synced = false;
|
|
254
|
+
this.ws = null;
|
|
255
|
+
this.wsLastMessageReceived = 0;
|
|
256
|
+
this.shouldConnect = connect;
|
|
257
|
+
this._resyncInterval = 0;
|
|
258
|
+
if (resyncInterval > 0)
|
|
259
|
+
this._resyncInterval = setInterval(() => {
|
|
260
|
+
if (this.ws && this.ws.readyState === WebSocket.OPEN) {
|
|
261
|
+
const encoder = encoding.createEncoder();
|
|
262
|
+
encoding.writeVarUint(encoder, messageSync);
|
|
263
|
+
syncProtocol.writeSyncStep1(encoder, doc);
|
|
264
|
+
this.ws.send(encoding.toUint8Array(encoder));
|
|
265
|
+
}
|
|
266
|
+
}, resyncInterval);
|
|
267
|
+
this._bcSubscriber = (data, origin) => {
|
|
268
|
+
if (origin !== this) {
|
|
269
|
+
const encoder = readMessage(this, new Uint8Array(data), false);
|
|
270
|
+
if (encoding.length(encoder) > 1)
|
|
271
|
+
bc.publish(this.bcChannel, encoding.toUint8Array(encoder), this);
|
|
272
|
+
}
|
|
273
|
+
};
|
|
274
|
+
/**
|
|
275
|
+
* Listens to Yjs updates and sends them to remote peers (ws and broadcastchannel)
|
|
276
|
+
*/
|
|
277
|
+
this._updateHandler = (update, origin) => {
|
|
278
|
+
if (origin !== this) {
|
|
279
|
+
const encoder = encoding.createEncoder();
|
|
280
|
+
encoding.writeVarUint(encoder, messageSync);
|
|
281
|
+
syncProtocol.writeUpdate(encoder, update);
|
|
282
|
+
broadcastMessage(this, encoding.toUint8Array(encoder));
|
|
283
|
+
}
|
|
284
|
+
};
|
|
285
|
+
this.doc.on("update", this._updateHandler);
|
|
286
|
+
this._awarenessUpdateHandler = ({ added, updated, removed }, _origin) => {
|
|
287
|
+
const changedClients = added.concat(updated).concat(removed);
|
|
288
|
+
const encoder = encoding.createEncoder();
|
|
289
|
+
encoding.writeVarUint(encoder, messageAwareness);
|
|
290
|
+
encoding.writeVarUint8Array(
|
|
291
|
+
encoder,
|
|
292
|
+
awarenessProtocol.encodeAwarenessUpdate(awareness, changedClients)
|
|
293
|
+
);
|
|
294
|
+
broadcastMessage(this, encoding.toUint8Array(encoder));
|
|
295
|
+
};
|
|
296
|
+
this._unloadHandler = () => {
|
|
297
|
+
awarenessProtocol.removeAwarenessStates(
|
|
298
|
+
this.awareness,
|
|
299
|
+
[doc.clientID],
|
|
300
|
+
"window unload"
|
|
301
|
+
);
|
|
302
|
+
};
|
|
303
|
+
if (typeof window !== "undefined")
|
|
304
|
+
window.addEventListener("unload", this._unloadHandler);
|
|
305
|
+
else if (typeof process !== "undefined" && typeof process.on === "function")
|
|
306
|
+
process.on("exit", this._unloadHandler);
|
|
307
|
+
awareness.on("change", this._awarenessUpdateHandler);
|
|
308
|
+
clearInterval(awareness._checkInterval);
|
|
309
|
+
if (connect) this.connect();
|
|
310
|
+
}
|
|
311
|
+
/**
|
|
312
|
+
* @type {boolean}
|
|
313
|
+
*/
|
|
314
|
+
get synced() {
|
|
315
|
+
return this._synced;
|
|
316
|
+
}
|
|
317
|
+
set synced(state) {
|
|
318
|
+
if (this._synced !== state) {
|
|
319
|
+
this._synced = state;
|
|
320
|
+
this.emit("synced", [state]);
|
|
321
|
+
this.emit("sync", [state]);
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
destroy() {
|
|
325
|
+
if (this._resyncInterval !== 0) clearInterval(this._resyncInterval);
|
|
326
|
+
this.disconnect();
|
|
327
|
+
if (typeof window !== "undefined")
|
|
328
|
+
window.removeEventListener("unload", this._unloadHandler);
|
|
329
|
+
else if (
|
|
330
|
+
typeof process !== "undefined" &&
|
|
331
|
+
typeof process.off === "function"
|
|
332
|
+
)
|
|
333
|
+
process.off("exit", this._unloadHandler);
|
|
334
|
+
this.awareness.off("change", this._awarenessUpdateHandler);
|
|
335
|
+
this.doc.off("update", this._updateHandler);
|
|
336
|
+
super.destroy();
|
|
337
|
+
}
|
|
338
|
+
connectBc() {
|
|
339
|
+
if (this.disableBc) return;
|
|
340
|
+
if (!this.bcconnected) {
|
|
341
|
+
bc.subscribe(this.bcChannel, this._bcSubscriber);
|
|
342
|
+
this.bcconnected = true;
|
|
343
|
+
}
|
|
344
|
+
const encoderSync = encoding.createEncoder();
|
|
345
|
+
encoding.writeVarUint(encoderSync, messageSync);
|
|
346
|
+
syncProtocol.writeSyncStep1(encoderSync, this.doc);
|
|
347
|
+
bc.publish(this.bcChannel, encoding.toUint8Array(encoderSync), this);
|
|
348
|
+
const encoderState = encoding.createEncoder();
|
|
349
|
+
encoding.writeVarUint(encoderState, messageSync);
|
|
350
|
+
syncProtocol.writeSyncStep2(encoderState, this.doc);
|
|
351
|
+
bc.publish(this.bcChannel, encoding.toUint8Array(encoderState), this);
|
|
352
|
+
const encoderAwarenessQuery = encoding.createEncoder();
|
|
353
|
+
encoding.writeVarUint(encoderAwarenessQuery, messageQueryAwareness);
|
|
354
|
+
bc.publish(
|
|
355
|
+
this.bcChannel,
|
|
356
|
+
encoding.toUint8Array(encoderAwarenessQuery),
|
|
357
|
+
this
|
|
358
|
+
);
|
|
359
|
+
const encoderAwarenessState = encoding.createEncoder();
|
|
360
|
+
encoding.writeVarUint(encoderAwarenessState, messageAwareness);
|
|
361
|
+
encoding.writeVarUint8Array(
|
|
362
|
+
encoderAwarenessState,
|
|
363
|
+
awarenessProtocol.encodeAwarenessUpdate(this.awareness, [
|
|
364
|
+
this.doc.clientID
|
|
365
|
+
])
|
|
366
|
+
);
|
|
367
|
+
bc.publish(
|
|
368
|
+
this.bcChannel,
|
|
369
|
+
encoding.toUint8Array(encoderAwarenessState),
|
|
370
|
+
this
|
|
371
|
+
);
|
|
372
|
+
}
|
|
373
|
+
disconnectBc() {
|
|
374
|
+
const encoder = encoding.createEncoder();
|
|
375
|
+
encoding.writeVarUint(encoder, messageAwareness);
|
|
376
|
+
encoding.writeVarUint8Array(
|
|
377
|
+
encoder,
|
|
378
|
+
awarenessProtocol.encodeAwarenessUpdate(
|
|
379
|
+
this.awareness,
|
|
380
|
+
[this.doc.clientID],
|
|
381
|
+
/* @__PURE__ */ new Map()
|
|
382
|
+
)
|
|
383
|
+
);
|
|
384
|
+
broadcastMessage(this, encoding.toUint8Array(encoder));
|
|
385
|
+
if (this.bcconnected) {
|
|
386
|
+
bc.unsubscribe(this.bcChannel, this._bcSubscriber);
|
|
387
|
+
this.bcconnected = false;
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
disconnect() {
|
|
391
|
+
this.shouldConnect = false;
|
|
392
|
+
this.disconnectBc();
|
|
393
|
+
if (this.ws !== null) this.ws.close();
|
|
394
|
+
}
|
|
395
|
+
connect() {
|
|
396
|
+
this.shouldConnect = true;
|
|
397
|
+
if (!this.wsconnected && this.ws === null) {
|
|
398
|
+
setupWS(this);
|
|
399
|
+
this.connectBc();
|
|
400
|
+
}
|
|
401
|
+
}
|
|
279
402
|
};
|
|
280
403
|
function assertType(value, label, type) {
|
|
281
|
-
|
|
404
|
+
if (typeof value !== type)
|
|
405
|
+
throw new Error(
|
|
406
|
+
`Invalid "${label}" parameter provided to YProvider. Expected: ${type}, received: ${value}`
|
|
407
|
+
);
|
|
282
408
|
}
|
|
283
409
|
var YProvider = class extends WebsocketProvider {
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
410
|
+
id;
|
|
411
|
+
#params;
|
|
412
|
+
constructor(host, room, doc, options = {}) {
|
|
413
|
+
assertType(host, "host", "string");
|
|
414
|
+
assertType(room, "room", "string");
|
|
415
|
+
host = host.replace(/^(http|https|ws|wss):\/\//, "");
|
|
416
|
+
if (host.endsWith("/")) host = host.slice(0, -1);
|
|
417
|
+
const serverUrl = `${options.protocol || (host.startsWith("localhost:") || host.startsWith("127.0.0.1:") || host.startsWith("192.168.") || host.startsWith("10.") || (host.startsWith("172.") && host.split(".")[1] >= "16" && host.split(".")[1] <= "31") ? "ws" : "wss")}://${host}${options.prefix || `/parties/${options.party || "main"}`}`;
|
|
418
|
+
const id = options.connectionId ?? nanoid(10);
|
|
419
|
+
const { params, connect = true, ...rest } = options;
|
|
420
|
+
const baseOptions = {
|
|
421
|
+
...rest,
|
|
422
|
+
isPrefixedUrl: !!options.prefix,
|
|
423
|
+
connect: false
|
|
424
|
+
};
|
|
425
|
+
super(serverUrl, room, doc ?? new Doc(), baseOptions);
|
|
426
|
+
this.id = id;
|
|
427
|
+
this.#params = params;
|
|
428
|
+
if (connect) this.connect();
|
|
429
|
+
}
|
|
430
|
+
async connect() {
|
|
431
|
+
try {
|
|
432
|
+
const nextParams =
|
|
433
|
+
typeof this.#params === "function"
|
|
434
|
+
? await this.#params()
|
|
435
|
+
: this.#params;
|
|
436
|
+
const urlParams = new URLSearchParams([["_pk", this.id]]);
|
|
437
|
+
if (nextParams) {
|
|
438
|
+
for (const [key, value] of Object.entries(nextParams))
|
|
439
|
+
if (value !== null && value !== void 0) urlParams.append(key, value);
|
|
440
|
+
}
|
|
441
|
+
const nextUrl = new URL(this.url);
|
|
442
|
+
nextUrl.search = urlParams.toString();
|
|
443
|
+
this.url = nextUrl.toString();
|
|
444
|
+
super.connect();
|
|
445
|
+
} catch (err) {
|
|
446
|
+
console.error("Failed to open connecton to PartyServer", err);
|
|
447
|
+
throw err;
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
sendMessage(message) {
|
|
451
|
+
this.ws?.send(`__YPS:${message}`);
|
|
452
|
+
}
|
|
323
453
|
};
|
|
324
454
|
|
|
325
455
|
//#endregion
|
|
326
|
-
export {
|
|
327
|
-
|
|
456
|
+
export {
|
|
457
|
+
WebsocketProvider,
|
|
458
|
+
YProvider as default,
|
|
459
|
+
messageAuth,
|
|
460
|
+
messageAwareness,
|
|
461
|
+
messageQueryAwareness,
|
|
462
|
+
messageSync
|
|
463
|
+
};
|
|
464
|
+
//# sourceMappingURL=index.js.map
|