@abraca/dabra 2.90.0 → 2.91.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/package.json +5 -11
- package/src/AbracadabraBaseProvider.ts +0 -963
- package/src/AbracadabraClient.ts +0 -1960
- package/src/AbracadabraProvider.ts +0 -915
- package/src/AbracadabraWS.ts +0 -695
- package/src/BackgroundSyncManager.ts +0 -856
- package/src/BackgroundSyncPersistence.ts +0 -140
- package/src/ChatClient.ts +0 -314
- package/src/CloseEvents.ts +0 -49
- package/src/ContentManager.ts +0 -246
- package/src/CoverReconcile.ts +0 -168
- package/src/CryptoIdentityKeystore.ts +0 -754
- package/src/DeviceRegistrationService.ts +0 -257
- package/src/DocConverters.ts +0 -1859
- package/src/DocKeyManager.ts +0 -163
- package/src/DocTypes.ts +0 -628
- package/src/DocUtils.ts +0 -251
- package/src/DocumentCache.ts +0 -210
- package/src/DocumentManager.ts +0 -410
- package/src/E2EAbracadabraProvider.ts +0 -426
- package/src/E2EOfflineStore.ts +0 -55
- package/src/EncryptedChatClient.ts +0 -173
- package/src/EncryptedY.ts +0 -145
- package/src/EventEmitter.ts +0 -59
- package/src/FileBlobStore.ts +0 -569
- package/src/IdentityDoc.ts +0 -711
- package/src/IncomingMessage.ts +0 -63
- package/src/MessageReceiver.ts +0 -151
- package/src/MessageSender.ts +0 -22
- package/src/MetaManager.ts +0 -194
- package/src/MnemonicKeyDerivation.ts +0 -171
- package/src/NodeConnect.ts +0 -63
- package/src/NodeIdentity.ts +0 -378
- package/src/NotificationsClient.ts +0 -207
- package/src/OfflineStore.ts +0 -294
- package/src/OutgoingMessage.ts +0 -25
- package/src/OutgoingMessages/AuthenticationMessage.ts +0 -23
- package/src/OutgoingMessages/AwarenessMessage.ts +0 -41
- package/src/OutgoingMessages/CloseMessage.ts +0 -17
- package/src/OutgoingMessages/QueryAwarenessMessage.ts +0 -17
- package/src/OutgoingMessages/StatelessMessage.ts +0 -18
- package/src/OutgoingMessages/SubdocMessage.ts +0 -35
- package/src/OutgoingMessages/SyncStepOneMessage.ts +0 -25
- package/src/OutgoingMessages/SyncStepTwoMessage.ts +0 -25
- package/src/OutgoingMessages/UpdateMessage.ts +0 -20
- package/src/QueryClient.ts +0 -209
- package/src/RpcClient.ts +0 -659
- package/src/SchemaTypes.ts +0 -179
- package/src/SearchIndex.ts +0 -250
- package/src/ServerRegistry.ts +0 -220
- package/src/ServerReset.ts +0 -222
- package/src/TokenManager.ts +0 -337
- package/src/TreeManager.ts +0 -894
- package/src/TreeTimestamps.ts +0 -101
- package/src/auth.ts +0 -62
- package/src/awarenessStatesToArray.ts +0 -10
- package/src/index.ts +0 -219
- package/src/messageRecord.ts +0 -121
- package/src/sync/BroadcastChannelSync.ts +0 -235
- package/src/types.ts +0 -790
- package/src/webrtc/AbracadabraWebRTC.ts +0 -683
- package/src/webrtc/DataChannelRouter.ts +0 -179
- package/src/webrtc/DevicePairingChannel.ts +0 -496
- package/src/webrtc/E2EEChannel.ts +0 -195
- package/src/webrtc/FileTransferChannel.ts +0 -367
- package/src/webrtc/ManualSignaling.ts +0 -204
- package/src/webrtc/PeerConnection.ts +0 -133
- package/src/webrtc/SignalingSocket.ts +0 -378
- package/src/webrtc/YjsDataChannel.ts +0 -203
- package/src/webrtc/index.ts +0 -26
- package/src/webrtc/types.ts +0 -177
package/src/AbracadabraWS.ts
DELETED
|
@@ -1,695 +0,0 @@
|
|
|
1
|
-
import { WsReadyStates } from "./types.ts";
|
|
2
|
-
import { retry } from "@lifeomic/attempt";
|
|
3
|
-
import * as time from "lib0/time";
|
|
4
|
-
import type { Event, MessageEvent } from "ws";
|
|
5
|
-
import EventEmitter from "./EventEmitter.ts";
|
|
6
|
-
import type { AbracadabraBaseProvider } from "./AbracadabraBaseProvider.ts";
|
|
7
|
-
import { IncomingMessage } from "./IncomingMessage.ts";
|
|
8
|
-
import { CloseMessage } from "./OutgoingMessages/CloseMessage.ts";
|
|
9
|
-
import {
|
|
10
|
-
WebSocketStatus,
|
|
11
|
-
type onAwarenessChangeParameters,
|
|
12
|
-
type onAwarenessUpdateParameters,
|
|
13
|
-
type onCloseParameters,
|
|
14
|
-
type onDisconnectParameters,
|
|
15
|
-
type onMessageParameters,
|
|
16
|
-
type onOpenParameters,
|
|
17
|
-
type onOutgoingMessageParameters,
|
|
18
|
-
type onStatusParameters,
|
|
19
|
-
} from "./types.ts";
|
|
20
|
-
|
|
21
|
-
export type AbracadabraWebSocketConn = WebSocket & { identifier: string };
|
|
22
|
-
/** @deprecated Use AbracadabraWebSocketConn */
|
|
23
|
-
export type HocuspocusWebSocket = AbracadabraWebSocketConn;
|
|
24
|
-
/** @deprecated Use AbracadabraWebSocketConn */
|
|
25
|
-
export type HocusPocusWebSocket = AbracadabraWebSocketConn;
|
|
26
|
-
|
|
27
|
-
export type AbracadabraWSConfiguration = Required<
|
|
28
|
-
Pick<CompleteAbracadabraWSConfiguration, "url">
|
|
29
|
-
> &
|
|
30
|
-
Partial<CompleteAbracadabraWSConfiguration>;
|
|
31
|
-
|
|
32
|
-
/** @deprecated Use AbracadabraWSConfiguration */
|
|
33
|
-
export type HocuspocusProviderWebsocketConfiguration = AbracadabraWSConfiguration;
|
|
34
|
-
|
|
35
|
-
export interface CompleteAbracadabraWSConfiguration {
|
|
36
|
-
/**
|
|
37
|
-
* Whether to connect automatically when creating the provider instance. Default=true
|
|
38
|
-
*/
|
|
39
|
-
autoConnect: boolean;
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* URL of your @hocuspocus/server instance
|
|
43
|
-
*/
|
|
44
|
-
url: string;
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* By default, trailing slashes are removed from the URL. Set this to true
|
|
48
|
-
* to preserve trailing slashes if your server configuration requires them.
|
|
49
|
-
*/
|
|
50
|
-
preserveTrailingSlash: boolean;
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* An optional WebSocket polyfill, for example for Node.js
|
|
54
|
-
*/
|
|
55
|
-
WebSocketPolyfill: any;
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* Disconnect and reconnect when no message is received for the defined amount
|
|
59
|
-
* of milliseconds.
|
|
60
|
-
*
|
|
61
|
-
* Defaults to 30000 ms to match y-protocols/awareness `Awareness.outdatedTime`
|
|
62
|
-
* (also 30 s). Awareness heartbeats are what normally keep this timer
|
|
63
|
-
* resetting, so this value should be ≥ the largest `outdatedTime` of any
|
|
64
|
-
* awareness instance attached to this socket. If you pass a custom awareness
|
|
65
|
-
* with a non-default `outdatedTime`, pass the matching value here too.
|
|
66
|
-
*/
|
|
67
|
-
messageReconnectTimeout: number;
|
|
68
|
-
/**
|
|
69
|
-
* The delay between each attempt in milliseconds. You can provide a factor to have the delay grow exponentially.
|
|
70
|
-
*/
|
|
71
|
-
delay: number;
|
|
72
|
-
/**
|
|
73
|
-
* The initialDelay is the amount of time to wait before making the first attempt. This option should typically be 0 since you typically want the first attempt to happen immediately.
|
|
74
|
-
*/
|
|
75
|
-
initialDelay: number;
|
|
76
|
-
/**
|
|
77
|
-
* The factor option is used to grow the delay exponentially.
|
|
78
|
-
*/
|
|
79
|
-
factor: number;
|
|
80
|
-
/**
|
|
81
|
-
* The maximum number of attempts or 0 if there is no limit on number of attempts.
|
|
82
|
-
*/
|
|
83
|
-
maxAttempts: number;
|
|
84
|
-
/**
|
|
85
|
-
* minDelay is used to set a lower bound of delay when jitter is enabled. This property has no effect if jitter is disabled.
|
|
86
|
-
*/
|
|
87
|
-
minDelay: number;
|
|
88
|
-
/**
|
|
89
|
-
* The maxDelay option is used to set an upper bound for the delay when factor is enabled. A value of 0 can be provided if there should be no upper bound when calculating delay.
|
|
90
|
-
*/
|
|
91
|
-
maxDelay: number;
|
|
92
|
-
/**
|
|
93
|
-
* If jitter is true then the calculated delay will be a random integer value between minDelay and the calculated delay for the current iteration.
|
|
94
|
-
*/
|
|
95
|
-
jitter: boolean;
|
|
96
|
-
/**
|
|
97
|
-
* A timeout in milliseconds. If timeout is non-zero then a timer is set using setTimeout. If the timeout is triggered then future attempts will be aborted.
|
|
98
|
-
*/
|
|
99
|
-
timeout: number;
|
|
100
|
-
handleTimeout: (() => Promise<unknown>) | null;
|
|
101
|
-
onOpen: (data: onOpenParameters) => void;
|
|
102
|
-
onConnect: () => void;
|
|
103
|
-
onMessage: (data: onMessageParameters) => void;
|
|
104
|
-
onOutgoingMessage: (data: onOutgoingMessageParameters) => void;
|
|
105
|
-
onStatus: (data: onStatusParameters) => void;
|
|
106
|
-
onDisconnect: (data: onDisconnectParameters) => void;
|
|
107
|
-
onClose: (data: onCloseParameters) => void;
|
|
108
|
-
onDestroy: () => void;
|
|
109
|
-
onAwarenessUpdate: (data: onAwarenessUpdateParameters) => void;
|
|
110
|
-
onAwarenessChange: (data: onAwarenessChangeParameters) => void;
|
|
111
|
-
|
|
112
|
-
/**
|
|
113
|
-
* Map of attached providers keyed by documentName.
|
|
114
|
-
*/
|
|
115
|
-
providerMap: Map<string, AbracadabraBaseProvider>;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
/** @deprecated Use CompleteAbracadabraWSConfiguration */
|
|
119
|
-
export type CompleteHocuspocusProviderWebsocketConfiguration = CompleteAbracadabraWSConfiguration;
|
|
120
|
-
|
|
121
|
-
export class AbracadabraWS extends EventEmitter {
|
|
122
|
-
private messageQueue: any[] = [];
|
|
123
|
-
|
|
124
|
-
public configuration: CompleteAbracadabraWSConfiguration = {
|
|
125
|
-
url: "",
|
|
126
|
-
autoConnect: true,
|
|
127
|
-
preserveTrailingSlash: false,
|
|
128
|
-
// @ts-ignore
|
|
129
|
-
document: undefined,
|
|
130
|
-
WebSocketPolyfill: undefined,
|
|
131
|
-
// Matches y-protocols/awareness Awareness.outdatedTime default (30 s).
|
|
132
|
-
// See the `messageReconnectTimeout` JSDoc on CompleteAbracadabraWSConfiguration.
|
|
133
|
-
messageReconnectTimeout: 30000,
|
|
134
|
-
// 1 second
|
|
135
|
-
delay: 1000,
|
|
136
|
-
// instant
|
|
137
|
-
initialDelay: 0,
|
|
138
|
-
// double the delay each time
|
|
139
|
-
factor: 2,
|
|
140
|
-
// unlimited retries
|
|
141
|
-
maxAttempts: 0,
|
|
142
|
-
// wait at least 1 second
|
|
143
|
-
minDelay: 1000,
|
|
144
|
-
// at least every 30 seconds
|
|
145
|
-
maxDelay: 30000,
|
|
146
|
-
// randomize
|
|
147
|
-
jitter: true,
|
|
148
|
-
// retry forever
|
|
149
|
-
timeout: 0,
|
|
150
|
-
onOpen: () => null,
|
|
151
|
-
onConnect: () => null,
|
|
152
|
-
onMessage: () => null,
|
|
153
|
-
onOutgoingMessage: () => null,
|
|
154
|
-
onStatus: () => null,
|
|
155
|
-
onDisconnect: () => null,
|
|
156
|
-
onClose: () => null,
|
|
157
|
-
onDestroy: () => null,
|
|
158
|
-
onAwarenessUpdate: () => null,
|
|
159
|
-
onAwarenessChange: () => null,
|
|
160
|
-
handleTimeout: null,
|
|
161
|
-
providerMap: new Map(),
|
|
162
|
-
};
|
|
163
|
-
|
|
164
|
-
webSocket: AbracadabraWebSocketConn | null = null;
|
|
165
|
-
|
|
166
|
-
webSocketHandlers: { [key: string]: any } = {};
|
|
167
|
-
|
|
168
|
-
shouldConnect = true;
|
|
169
|
-
|
|
170
|
-
status = WebSocketStatus.Disconnected;
|
|
171
|
-
|
|
172
|
-
lastMessageReceived = 0;
|
|
173
|
-
|
|
174
|
-
identifier = 0;
|
|
175
|
-
|
|
176
|
-
intervals: any = {
|
|
177
|
-
connectionChecker: null,
|
|
178
|
-
};
|
|
179
|
-
|
|
180
|
-
connectionAttempt: {
|
|
181
|
-
resolve: (value?: any) => void;
|
|
182
|
-
reject: (reason?: any) => void;
|
|
183
|
-
} | null = null;
|
|
184
|
-
|
|
185
|
-
// Bound `online`/`offline` listeners, retained so we can detach them in
|
|
186
|
-
// destroy(). Only registered in browser-like environments (see constructor).
|
|
187
|
-
private onlineListener: (() => void) | null = null;
|
|
188
|
-
|
|
189
|
-
private offlineListener: (() => void) | null = null;
|
|
190
|
-
|
|
191
|
-
constructor(configuration: AbracadabraWSConfiguration) {
|
|
192
|
-
super();
|
|
193
|
-
this.setConfiguration(configuration);
|
|
194
|
-
|
|
195
|
-
this.configuration.WebSocketPolyfill = configuration.WebSocketPolyfill
|
|
196
|
-
? configuration.WebSocketPolyfill
|
|
197
|
-
: (globalThis as { WebSocket?: typeof WebSocket }).WebSocket;
|
|
198
|
-
|
|
199
|
-
this.on("open", this.configuration.onOpen);
|
|
200
|
-
this.on("open", this.onOpen.bind(this));
|
|
201
|
-
this.on("connect", this.configuration.onConnect);
|
|
202
|
-
this.on("message", this.configuration.onMessage);
|
|
203
|
-
this.on("outgoingMessage", this.configuration.onOutgoingMessage);
|
|
204
|
-
this.on("status", this.configuration.onStatus);
|
|
205
|
-
this.on("disconnect", this.configuration.onDisconnect);
|
|
206
|
-
this.on("close", this.configuration.onClose);
|
|
207
|
-
this.on("destroy", this.configuration.onDestroy);
|
|
208
|
-
this.on("awarenessUpdate", this.configuration.onAwarenessUpdate);
|
|
209
|
-
this.on("awarenessChange", this.configuration.onAwarenessChange);
|
|
210
|
-
|
|
211
|
-
this.on("close", this.onClose.bind(this));
|
|
212
|
-
this.on("message", this.onMessage.bind(this));
|
|
213
|
-
|
|
214
|
-
this.intervals.connectionChecker = setInterval(
|
|
215
|
-
this.checkConnection.bind(this),
|
|
216
|
-
this.configuration.messageReconnectTimeout / 10,
|
|
217
|
-
);
|
|
218
|
-
|
|
219
|
-
// Offline-first reconnect gating. When the device reports it is offline we
|
|
220
|
-
// stop hammering the socket (failed attempts just flip status
|
|
221
|
-
// disconnected↔connecting and light up every connection indicator) and
|
|
222
|
-
// wait for the `online` event to resume immediately — no backoff wait.
|
|
223
|
-
if (typeof window !== "undefined" && typeof window.addEventListener === "function") {
|
|
224
|
-
this.onlineListener = this.handleOnline.bind(this);
|
|
225
|
-
this.offlineListener = this.handleOffline.bind(this);
|
|
226
|
-
window.addEventListener("online", this.onlineListener);
|
|
227
|
-
window.addEventListener("offline", this.offlineListener);
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
if (this.shouldConnect) {
|
|
231
|
-
this.connect();
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
/**
|
|
236
|
-
* Whether the device currently believes it has network connectivity.
|
|
237
|
-
*
|
|
238
|
-
* Treats "unknown" as online: in Node and other non-browser environments
|
|
239
|
-
* `navigator` (or `navigator.onLine`) is absent, and we must not gate
|
|
240
|
-
* reconnection there — only the browser exposes a trustworthy signal.
|
|
241
|
-
*/
|
|
242
|
-
get isOnline(): boolean {
|
|
243
|
-
return typeof navigator === "undefined" || navigator.onLine !== false;
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
handleOnline() {
|
|
247
|
-
// Network came back — resume immediately if we still want a connection
|
|
248
|
-
// and aren't already connected. Bypasses the backoff that a pending
|
|
249
|
-
// retryer would otherwise wait out.
|
|
250
|
-
if (this.shouldConnect && this.status !== WebSocketStatus.Connected) {
|
|
251
|
-
this.connect();
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
handleOffline() {
|
|
256
|
-
// Stop attempting while offline, but preserve `shouldConnect` so intent
|
|
257
|
-
// survives — `handleOnline`/attach will resume once connectivity returns.
|
|
258
|
-
if (this.cancelWebsocketRetry) {
|
|
259
|
-
this.cancelWebsocketRetry();
|
|
260
|
-
this.cancelWebsocketRetry = undefined;
|
|
261
|
-
}
|
|
262
|
-
try {
|
|
263
|
-
this.webSocket?.close();
|
|
264
|
-
this.messageQueue = [];
|
|
265
|
-
} catch (e) {
|
|
266
|
-
console.error(e);
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
receivedOnOpenPayload?: Event | undefined = undefined;
|
|
271
|
-
|
|
272
|
-
async onOpen(event: Event) {
|
|
273
|
-
this.status = WebSocketStatus.Connected;
|
|
274
|
-
this.emit("status", { status: WebSocketStatus.Connected });
|
|
275
|
-
|
|
276
|
-
this.cancelWebsocketRetry = undefined;
|
|
277
|
-
this.receivedOnOpenPayload = event;
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
attach(provider: AbracadabraBaseProvider) {
|
|
281
|
-
const existing = this.configuration.providerMap.get(provider.configuration.name);
|
|
282
|
-
if (existing && existing !== provider) {
|
|
283
|
-
console.debug(
|
|
284
|
-
`[AbracadabraWS] attach: replacing provider for "${provider.configuration.name}".`
|
|
285
|
-
);
|
|
286
|
-
}
|
|
287
|
-
this.configuration.providerMap.set(provider.configuration.name, provider);
|
|
288
|
-
|
|
289
|
-
if (this.status === WebSocketStatus.Disconnected && this.shouldConnect) {
|
|
290
|
-
this.connect();
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
if (this.receivedOnOpenPayload && this.status === WebSocketStatus.Connected) {
|
|
294
|
-
provider.onOpen(this.receivedOnOpenPayload);
|
|
295
|
-
}
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
detach(provider: AbracadabraBaseProvider) {
|
|
299
|
-
const name = provider.configuration.name;
|
|
300
|
-
const current = this.configuration.providerMap.get(name);
|
|
301
|
-
if (current === provider) {
|
|
302
|
-
provider.send(CloseMessage, { documentName: name });
|
|
303
|
-
this.configuration.providerMap.delete(name);
|
|
304
|
-
}
|
|
305
|
-
// If current !== provider, another provider owns this slot —
|
|
306
|
-
// don't send CloseMessage or remove it from the map.
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
public setConfiguration(
|
|
310
|
-
configuration: Partial<AbracadabraWSConfiguration> = {},
|
|
311
|
-
): void {
|
|
312
|
-
this.configuration = { ...this.configuration, ...configuration };
|
|
313
|
-
|
|
314
|
-
if (!this.configuration.autoConnect) {
|
|
315
|
-
this.shouldConnect = false;
|
|
316
|
-
}
|
|
317
|
-
}
|
|
318
|
-
|
|
319
|
-
cancelWebsocketRetry?: () => void;
|
|
320
|
-
|
|
321
|
-
async connect() {
|
|
322
|
-
if (this.status === WebSocketStatus.Connected) {
|
|
323
|
-
return;
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
// Don't attempt while the device is offline — record the intent and let
|
|
327
|
-
// the `online` event drive the resume. Avoids the reconnect storm of
|
|
328
|
-
// instantly-failing socket attempts against a known-dead network.
|
|
329
|
-
if (!this.isOnline) {
|
|
330
|
-
this.shouldConnect = true;
|
|
331
|
-
return;
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
// Always cancel any previously initiated connection retryer instances
|
|
335
|
-
if (this.cancelWebsocketRetry) {
|
|
336
|
-
this.cancelWebsocketRetry();
|
|
337
|
-
this.cancelWebsocketRetry = undefined;
|
|
338
|
-
}
|
|
339
|
-
|
|
340
|
-
this.receivedOnOpenPayload = undefined;
|
|
341
|
-
this.shouldConnect = true;
|
|
342
|
-
|
|
343
|
-
const abortableRetry = () => {
|
|
344
|
-
let cancelAttempt = false;
|
|
345
|
-
|
|
346
|
-
const retryPromise = retry(this.createWebSocketConnection.bind(this), {
|
|
347
|
-
delay: this.configuration.delay,
|
|
348
|
-
initialDelay: this.configuration.initialDelay,
|
|
349
|
-
factor: this.configuration.factor,
|
|
350
|
-
maxAttempts: this.configuration.maxAttempts,
|
|
351
|
-
minDelay: this.configuration.minDelay,
|
|
352
|
-
maxDelay: this.configuration.maxDelay,
|
|
353
|
-
jitter: this.configuration.jitter,
|
|
354
|
-
timeout: this.configuration.timeout,
|
|
355
|
-
handleTimeout: this.configuration.handleTimeout,
|
|
356
|
-
beforeAttempt: (context) => {
|
|
357
|
-
if (!this.shouldConnect || cancelAttempt) {
|
|
358
|
-
context.abort();
|
|
359
|
-
}
|
|
360
|
-
},
|
|
361
|
-
}).catch((error: any) => {
|
|
362
|
-
// If we aborted the connection attempt then don't throw an error
|
|
363
|
-
// ref: https://github.com/lifeomic/attempt/blob/master/src/index.ts#L136
|
|
364
|
-
if (error && error.code !== "ATTEMPT_ABORTED") {
|
|
365
|
-
throw error;
|
|
366
|
-
}
|
|
367
|
-
});
|
|
368
|
-
|
|
369
|
-
return {
|
|
370
|
-
retryPromise,
|
|
371
|
-
cancelFunc: () => {
|
|
372
|
-
cancelAttempt = true;
|
|
373
|
-
},
|
|
374
|
-
};
|
|
375
|
-
};
|
|
376
|
-
|
|
377
|
-
const { retryPromise, cancelFunc } = abortableRetry();
|
|
378
|
-
this.cancelWebsocketRetry = cancelFunc;
|
|
379
|
-
|
|
380
|
-
return retryPromise;
|
|
381
|
-
}
|
|
382
|
-
|
|
383
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
|
|
384
|
-
attachWebSocketListeners(ws: AbracadabraWebSocketConn, reject: Function) {
|
|
385
|
-
const { identifier } = ws;
|
|
386
|
-
const onMessageHandler = (payload: any) => this.emit("message", payload);
|
|
387
|
-
const onCloseHandler = (payload: any) => this.emit("close", { event: payload });
|
|
388
|
-
const onOpenHandler = (payload: any) => this.emit("open", payload);
|
|
389
|
-
const onErrorHandler = (err: any) => {
|
|
390
|
-
reject(err);
|
|
391
|
-
};
|
|
392
|
-
|
|
393
|
-
this.webSocketHandlers[identifier] = {
|
|
394
|
-
message: onMessageHandler,
|
|
395
|
-
close: onCloseHandler,
|
|
396
|
-
open: onOpenHandler,
|
|
397
|
-
error: onErrorHandler,
|
|
398
|
-
};
|
|
399
|
-
|
|
400
|
-
const handlers = this.webSocketHandlers[ws.identifier];
|
|
401
|
-
|
|
402
|
-
Object.keys(handlers).forEach((name) => {
|
|
403
|
-
ws.addEventListener(name, handlers[name]);
|
|
404
|
-
});
|
|
405
|
-
}
|
|
406
|
-
|
|
407
|
-
cleanupWebSocket() {
|
|
408
|
-
if (!this.webSocket) {
|
|
409
|
-
return;
|
|
410
|
-
}
|
|
411
|
-
const { identifier } = this.webSocket;
|
|
412
|
-
const handlers = this.webSocketHandlers[identifier];
|
|
413
|
-
|
|
414
|
-
Object.keys(handlers).forEach((name) => {
|
|
415
|
-
this.webSocket?.removeEventListener(name, handlers[name]);
|
|
416
|
-
delete this.webSocketHandlers[identifier];
|
|
417
|
-
});
|
|
418
|
-
|
|
419
|
-
// After removing our handlers, ensure there is always at least one error
|
|
420
|
-
// listener on the socket. Without one, any asynchronously-fired error
|
|
421
|
-
// (e.g. "WebSocket was closed before the connection was established" when
|
|
422
|
-
// closing a CONNECTING socket) will propagate as an uncaught exception in
|
|
423
|
-
// Node.js. The no-op listener absorbs these post-cleanup errors safely.
|
|
424
|
-
if (this.webSocket.readyState !== 3 /* CLOSED */) {
|
|
425
|
-
this.webSocket.addEventListener("error", () => {});
|
|
426
|
-
}
|
|
427
|
-
|
|
428
|
-
try {
|
|
429
|
-
if (this.webSocket.readyState !== 0 /* CONNECTING */ && this.webSocket.readyState !== 3 /* CLOSED */) {
|
|
430
|
-
this.webSocket.close();
|
|
431
|
-
}
|
|
432
|
-
// CONNECTING (0): already being closed by disconnect(), or will be closed imminently.
|
|
433
|
-
// CLOSED (3): nothing to do.
|
|
434
|
-
} catch (e) {
|
|
435
|
-
// Ignore errors during close
|
|
436
|
-
}
|
|
437
|
-
|
|
438
|
-
this.webSocket = null;
|
|
439
|
-
}
|
|
440
|
-
|
|
441
|
-
createWebSocketConnection() {
|
|
442
|
-
return new Promise((resolve, reject) => {
|
|
443
|
-
if (this.webSocket) {
|
|
444
|
-
this.messageQueue = [];
|
|
445
|
-
this.cleanupWebSocket();
|
|
446
|
-
}
|
|
447
|
-
this.lastMessageReceived = 0;
|
|
448
|
-
this.identifier += 1;
|
|
449
|
-
|
|
450
|
-
// Init the WebSocket connection
|
|
451
|
-
const ws = new this.configuration.WebSocketPolyfill(this.url);
|
|
452
|
-
ws.binaryType = "arraybuffer";
|
|
453
|
-
ws.identifier = this.identifier;
|
|
454
|
-
|
|
455
|
-
this.attachWebSocketListeners(ws, reject);
|
|
456
|
-
|
|
457
|
-
this.webSocket = ws;
|
|
458
|
-
|
|
459
|
-
// Reset the status
|
|
460
|
-
this.status = WebSocketStatus.Connecting;
|
|
461
|
-
this.emit("status", { status: WebSocketStatus.Connecting });
|
|
462
|
-
|
|
463
|
-
// Store resolve/reject for later use
|
|
464
|
-
this.connectionAttempt = {
|
|
465
|
-
resolve,
|
|
466
|
-
reject,
|
|
467
|
-
};
|
|
468
|
-
});
|
|
469
|
-
}
|
|
470
|
-
|
|
471
|
-
onMessage(event: MessageEvent) {
|
|
472
|
-
this.resolveConnectionAttempt();
|
|
473
|
-
|
|
474
|
-
this.lastMessageReceived = time.getUnixTime();
|
|
475
|
-
|
|
476
|
-
const message = new IncomingMessage(event.data);
|
|
477
|
-
const documentName = message.peekVarString();
|
|
478
|
-
|
|
479
|
-
try {
|
|
480
|
-
this.configuration.providerMap.get(documentName)?.onMessage(event);
|
|
481
|
-
} catch (err) {
|
|
482
|
-
console.error(`[AbracadabraWS] Provider onMessage error for "${documentName}":`, err);
|
|
483
|
-
}
|
|
484
|
-
}
|
|
485
|
-
|
|
486
|
-
resolveConnectionAttempt() {
|
|
487
|
-
if (this.connectionAttempt) {
|
|
488
|
-
this.connectionAttempt.resolve();
|
|
489
|
-
this.connectionAttempt = null;
|
|
490
|
-
|
|
491
|
-
this.status = WebSocketStatus.Connected;
|
|
492
|
-
this.emit("status", { status: WebSocketStatus.Connected });
|
|
493
|
-
this.emit("connect");
|
|
494
|
-
this.messageQueue.forEach((message) => this.send(message));
|
|
495
|
-
this.messageQueue = [];
|
|
496
|
-
}
|
|
497
|
-
}
|
|
498
|
-
|
|
499
|
-
stopConnectionAttempt() {
|
|
500
|
-
this.connectionAttempt = null;
|
|
501
|
-
}
|
|
502
|
-
|
|
503
|
-
rejectConnectionAttempt() {
|
|
504
|
-
this.connectionAttempt?.reject();
|
|
505
|
-
this.connectionAttempt = null;
|
|
506
|
-
}
|
|
507
|
-
|
|
508
|
-
closeTries = 0;
|
|
509
|
-
|
|
510
|
-
checkConnection() {
|
|
511
|
-
// Don't check the connection when it's not even established
|
|
512
|
-
if (this.status !== WebSocketStatus.Connected) {
|
|
513
|
-
return;
|
|
514
|
-
}
|
|
515
|
-
|
|
516
|
-
// Don't close the connection while waiting for the first message
|
|
517
|
-
if (!this.lastMessageReceived) {
|
|
518
|
-
return;
|
|
519
|
-
}
|
|
520
|
-
|
|
521
|
-
// Don't close the connection when a message was received recently
|
|
522
|
-
if (
|
|
523
|
-
this.configuration.messageReconnectTimeout >=
|
|
524
|
-
time.getUnixTime() - this.lastMessageReceived
|
|
525
|
-
) {
|
|
526
|
-
return;
|
|
527
|
-
}
|
|
528
|
-
|
|
529
|
-
// No message received in a long time, not even your own
|
|
530
|
-
// Awareness updates, which are updated every 15 seconds
|
|
531
|
-
// if awareness is enabled.
|
|
532
|
-
this.closeTries += 1;
|
|
533
|
-
// https://bugs.webkit.org/show_bug.cgi?id=247943
|
|
534
|
-
if (this.closeTries > 2) {
|
|
535
|
-
this.onClose({
|
|
536
|
-
event: {
|
|
537
|
-
code: 4408,
|
|
538
|
-
reason: "forced",
|
|
539
|
-
},
|
|
540
|
-
});
|
|
541
|
-
this.closeTries = 0;
|
|
542
|
-
} else {
|
|
543
|
-
this.webSocket?.close();
|
|
544
|
-
this.messageQueue = [];
|
|
545
|
-
}
|
|
546
|
-
}
|
|
547
|
-
|
|
548
|
-
get serverUrl() {
|
|
549
|
-
if (this.configuration.preserveTrailingSlash) {
|
|
550
|
-
return this.configuration.url;
|
|
551
|
-
}
|
|
552
|
-
|
|
553
|
-
// By default, ensure that the URL never ends with /
|
|
554
|
-
let url = this.configuration.url;
|
|
555
|
-
while (url[url.length - 1] === "/") {
|
|
556
|
-
url = url.slice(0, url.length - 1);
|
|
557
|
-
}
|
|
558
|
-
|
|
559
|
-
return url;
|
|
560
|
-
}
|
|
561
|
-
|
|
562
|
-
get url() {
|
|
563
|
-
return this.serverUrl;
|
|
564
|
-
}
|
|
565
|
-
|
|
566
|
-
disconnect() {
|
|
567
|
-
this.shouldConnect = false;
|
|
568
|
-
|
|
569
|
-
if (this.webSocket === null) {
|
|
570
|
-
return;
|
|
571
|
-
}
|
|
572
|
-
|
|
573
|
-
try {
|
|
574
|
-
this.webSocket.close();
|
|
575
|
-
this.messageQueue = [];
|
|
576
|
-
} catch (e) {
|
|
577
|
-
console.error(e);
|
|
578
|
-
}
|
|
579
|
-
}
|
|
580
|
-
|
|
581
|
-
/**
|
|
582
|
-
* Force a fresh socket on the SAME WS manager: drop the current connection
|
|
583
|
-
* (if any) and reconnect immediately.
|
|
584
|
-
*
|
|
585
|
-
* Unlike {@link disconnect} (which clears `shouldConnect` and gives up) this
|
|
586
|
-
* re-arms `shouldConnect`, so it also revives a socket that a permanent
|
|
587
|
-
* permission-denial turned off (see `AbracadabraBaseProvider.permissionDeniedHandler`).
|
|
588
|
-
* The WS manager and its `providerMap` are preserved, so every attached
|
|
589
|
-
* provider — and every Y.Doc + observer riding this socket — survives
|
|
590
|
-
* untouched; only the transport is recycled.
|
|
591
|
-
*
|
|
592
|
-
* The existing socket is torn down silently (handlers removed first), so no
|
|
593
|
-
* `close` event fires and no second reconnect is scheduled — `connect()`
|
|
594
|
-
* below is the single path that re-opens. Returns the `connect()` promise.
|
|
595
|
-
*/
|
|
596
|
-
reconnect() {
|
|
597
|
-
this.shouldConnect = true;
|
|
598
|
-
|
|
599
|
-
// Cancel any in-flight retryer so we don't stack two connection attempts.
|
|
600
|
-
if (this.cancelWebsocketRetry) {
|
|
601
|
-
this.cancelWebsocketRetry();
|
|
602
|
-
this.cancelWebsocketRetry = undefined;
|
|
603
|
-
}
|
|
604
|
-
|
|
605
|
-
// Silently drop the current socket (cleanupWebSocket removes our handlers
|
|
606
|
-
// before closing, so onClose never runs → no auto-reschedule).
|
|
607
|
-
if (this.webSocket) {
|
|
608
|
-
this.cleanupWebSocket();
|
|
609
|
-
}
|
|
610
|
-
|
|
611
|
-
// connect() early-returns when status is Connected; force Disconnected so
|
|
612
|
-
// it always builds a fresh socket. Surface the transition so status
|
|
613
|
-
// listeners (and the providers' synced reset) see the drop.
|
|
614
|
-
this.status = WebSocketStatus.Disconnected;
|
|
615
|
-
this.emit("status", { status: WebSocketStatus.Disconnected });
|
|
616
|
-
|
|
617
|
-
return this.connect();
|
|
618
|
-
}
|
|
619
|
-
|
|
620
|
-
send(message: any) {
|
|
621
|
-
if (this.webSocket?.readyState === WsReadyStates.Open) {
|
|
622
|
-
this.webSocket.send(message);
|
|
623
|
-
} else {
|
|
624
|
-
this.messageQueue.push(message);
|
|
625
|
-
}
|
|
626
|
-
}
|
|
627
|
-
|
|
628
|
-
onClose({ event }: onCloseParameters) {
|
|
629
|
-
this.closeTries = 0;
|
|
630
|
-
this.cleanupWebSocket();
|
|
631
|
-
|
|
632
|
-
if (this.connectionAttempt) {
|
|
633
|
-
// That connection attempt failed.
|
|
634
|
-
this.rejectConnectionAttempt();
|
|
635
|
-
}
|
|
636
|
-
|
|
637
|
-
// Let's update the connection status.
|
|
638
|
-
this.status = WebSocketStatus.Disconnected;
|
|
639
|
-
this.emit("status", { status: WebSocketStatus.Disconnected });
|
|
640
|
-
|
|
641
|
-
// Detect server-side rate-limit close (code 4429).
|
|
642
|
-
// `event` may be a CloseEvent (browser) with `.code`, or a raw number (ws/Node.js).
|
|
643
|
-
// `event` may be a CloseEvent (browser) with `.code`, or a raw number (ws/Node.js).
|
|
644
|
-
const isRateLimited = (event as any)?.code === 4429 || (event as unknown) === 4429;
|
|
645
|
-
this.emit("disconnect", { event });
|
|
646
|
-
if (isRateLimited) {
|
|
647
|
-
this.emit("rateLimited");
|
|
648
|
-
}
|
|
649
|
-
|
|
650
|
-
// trigger connect if no retry is running and we want to have a connection.
|
|
651
|
-
// Skip scheduling entirely while offline — `handleOnline` resumes the
|
|
652
|
-
// moment connectivity returns, so there's no point burning a timer (and a
|
|
653
|
-
// failed attempt) against a dead network.
|
|
654
|
-
if (!this.cancelWebsocketRetry && this.shouldConnect && this.isOnline) {
|
|
655
|
-
// Apply a much longer delay for rate-limited closes to let the server window reset.
|
|
656
|
-
const delay = isRateLimited ? 60_000 : this.configuration.delay;
|
|
657
|
-
setTimeout(() => {
|
|
658
|
-
this.connect();
|
|
659
|
-
}, delay);
|
|
660
|
-
}
|
|
661
|
-
}
|
|
662
|
-
|
|
663
|
-
destroy() {
|
|
664
|
-
// Mark as not wanting a connection FIRST so that onClose (fired by disconnect()
|
|
665
|
-
// below) does not schedule a new reconnect setTimeout after we've torn down.
|
|
666
|
-
this.shouldConnect = false;
|
|
667
|
-
|
|
668
|
-
this.emit("destroy");
|
|
669
|
-
|
|
670
|
-
if (typeof window !== "undefined" && typeof window.removeEventListener === "function") {
|
|
671
|
-
if (this.onlineListener) window.removeEventListener("online", this.onlineListener);
|
|
672
|
-
if (this.offlineListener) window.removeEventListener("offline", this.offlineListener);
|
|
673
|
-
}
|
|
674
|
-
this.onlineListener = null;
|
|
675
|
-
this.offlineListener = null;
|
|
676
|
-
|
|
677
|
-
clearInterval(this.intervals.connectionChecker);
|
|
678
|
-
|
|
679
|
-
// If there is still a connection attempt outstanding then we should stop
|
|
680
|
-
// it before calling disconnect, otherwise it will be rejected in the onClose
|
|
681
|
-
// handler and trigger a retry
|
|
682
|
-
this.stopConnectionAttempt();
|
|
683
|
-
|
|
684
|
-
this.disconnect();
|
|
685
|
-
|
|
686
|
-
this.removeAllListeners();
|
|
687
|
-
|
|
688
|
-
this.cleanupWebSocket();
|
|
689
|
-
}
|
|
690
|
-
}
|
|
691
|
-
|
|
692
|
-
/** @deprecated Use AbracadabraWS */
|
|
693
|
-
export const HocuspocusProviderWebsocket = AbracadabraWS;
|
|
694
|
-
/** @deprecated Use AbracadabraWS */
|
|
695
|
-
export type HocuspocusProviderWebsocket = AbracadabraWS;
|