@hocuspocus/provider 2.0.1 → 2.0.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.
@@ -42,6 +42,11 @@ export interface Configuration {
42
42
  * The maximum time for the Redis lock in ms (in case it can’t be released).
43
43
  */
44
44
  lockTimeout: number;
45
+ /**
46
+ * A delay before onDisconnect is executed. This allows last minute updates'
47
+ * sync messages to be received by the subscription before it's closed.
48
+ */
49
+ disconnectDelay: number;
45
50
  }
46
51
  export declare class Redis implements Extension {
47
52
  /**
@@ -103,7 +108,7 @@ export declare class Redis implements Extension {
103
108
  * Make sure to *not* listen for further changes, when there’s
104
109
  * noone connected anymore.
105
110
  */
106
- onDisconnect: ({ documentName, clientsCount }: onDisconnectPayload) => Promise<void>;
111
+ onDisconnect: ({ document, documentName }: onDisconnectPayload) => Promise<void>;
107
112
  beforeBroadcastStateless(data: beforeBroadcastStatelessPayload): Promise<number>;
108
113
  /**
109
114
  * Kill the Redlock connection immediately.
@@ -77,6 +77,7 @@ export declare class HocuspocusProvider extends EventEmitter {
77
77
  get document(): Y.Doc;
78
78
  get awareness(): Awareness;
79
79
  get hasUnsyncedChanges(): boolean;
80
+ updateUnsyncedChanges(unsyncedChanges?: number): void;
80
81
  forceSync(): void;
81
82
  boundBeforeUnload: () => void;
82
83
  beforeUnload(): void;
@@ -39,7 +39,7 @@ export declare class Connection {
39
39
  /**
40
40
  * Set a callback that will be triggered before an message is handled
41
41
  */
42
- beforeHandleMessage(callback: (payload: Document, update: Uint8Array) => Promise<any>): Connection;
42
+ beforeHandleMessage(callback: (connection: Connection, update: Uint8Array) => Promise<any>): Connection;
43
43
  /**
44
44
  * Send the given message
45
45
  */
@@ -176,6 +176,7 @@ export interface beforeHandleMessagePayload {
176
176
  requestParameters: URLSearchParams;
177
177
  update: Uint8Array;
178
178
  socketId: string;
179
+ connection: Connection;
179
180
  }
180
181
  export interface beforeBroadcastStatelessPayload {
181
182
  document: Document;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,2 @@
1
+ declare const _default: import("vite").UserConfigExport;
2
+ export default _default;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hocuspocus/provider",
3
- "version": "2.0.1",
3
+ "version": "2.0.3",
4
4
  "description": "hocuspocus provider",
5
5
  "homepage": "https://hocuspocus.dev",
6
6
  "keywords": [
@@ -28,7 +28,7 @@
28
28
  "dist"
29
29
  ],
30
30
  "dependencies": {
31
- "@hocuspocus/common": "^2.0.1",
31
+ "@hocuspocus/common": "^2.0.3",
32
32
  "@lifeomic/attempt": "^3.0.2",
33
33
  "lib0": "^0.2.47",
34
34
  "ws": "^7.5.9"
@@ -213,7 +213,12 @@ export class HocuspocusProvider extends EventEmitter {
213
213
 
214
214
  public setConfiguration(configuration: Partial<HocuspocusProviderConfiguration> = {}): void {
215
215
  if (!configuration.websocketProvider && (configuration as CompleteHocuspocusProviderWebsocketConfiguration).url) {
216
- this.configuration.websocketProvider = new HocuspocusProviderWebsocket({ url: (configuration as CompleteHocuspocusProviderWebsocketConfiguration).url })
216
+ const websocketProviderConfig = configuration as CompleteHocuspocusProviderWebsocketConfiguration
217
+
218
+ this.configuration.websocketProvider = new HocuspocusProviderWebsocket({
219
+ url: websocketProviderConfig.url,
220
+ parameters: websocketProviderConfig.parameters,
221
+ })
217
222
  }
218
223
 
219
224
  this.configuration = { ...this.configuration, ...configuration }
@@ -227,10 +232,15 @@ export class HocuspocusProvider extends EventEmitter {
227
232
  return this.configuration.awareness
228
233
  }
229
234
 
230
- get hasUnsyncedChanges() {
235
+ get hasUnsyncedChanges(): boolean {
231
236
  return this.unsyncedChanges > 0
232
237
  }
233
238
 
239
+ updateUnsyncedChanges(unsyncedChanges = 0) {
240
+ this.unsyncedChanges += unsyncedChanges
241
+ this.emit('unsyncedChanges', this.unsyncedChanges)
242
+ }
243
+
234
244
  forceSync() {
235
245
  this.send(SyncStepOneMessage, { document: this.document, documentName: this.configuration.name })
236
246
  }
@@ -258,7 +268,7 @@ export class HocuspocusProvider extends EventEmitter {
258
268
  return
259
269
  }
260
270
 
261
- this.unsyncedChanges += 1
271
+ this.updateUnsyncedChanges(1)
262
272
  this.send(UpdateMessage, { update, documentName: this.configuration.name }, true)
263
273
  }
264
274
 
@@ -281,6 +291,10 @@ export class HocuspocusProvider extends EventEmitter {
281
291
  return
282
292
  }
283
293
 
294
+ if (state && this.unsyncedChanges > 0) {
295
+ this.updateUnsyncedChanges(-1 * this.unsyncedChanges)
296
+ }
297
+
284
298
  this.isSynced = state
285
299
  this.emit('synced', { state })
286
300
  this.emit('sync', { state })
@@ -88,7 +88,7 @@ export class MessageReceiver {
88
88
 
89
89
  if (syncMessageType === messageYjsUpdate || syncMessageType === messageYjsSyncStep2) {
90
90
  if (provider.unsyncedChanges > 0) {
91
- provider.unsyncedChanges -= 1
91
+ provider.updateUnsyncedChanges(-1)
92
92
  }
93
93
  }
94
94
  }
@@ -1,61 +0,0 @@
1
- /// <reference types="node" />
2
- import { Configuration, onConnectPayload, onDisconnectPayload, onLoadDocumentPayload, onChangePayload } from '@hocuspocus/server';
3
- export declare class Collector {
4
- serverConfiguration: Partial<Configuration>;
5
- version: string;
6
- connections: {};
7
- messages: {};
8
- messageCounter: number;
9
- memory(): Promise<{
10
- free: number;
11
- total: number;
12
- usage: number;
13
- }>;
14
- cpu(): Promise<{
15
- count: number;
16
- model: string;
17
- usage: number;
18
- }>;
19
- connect(data: onConnectPayload): {
20
- action: string;
21
- documentName: string;
22
- socketId: string;
23
- };
24
- disconnect(data: onDisconnectPayload): {
25
- action: string;
26
- documentName: string;
27
- socketId: string;
28
- };
29
- connectionCount(): {
30
- count: string | number;
31
- };
32
- createDocument(data: onLoadDocumentPayload): {
33
- action: string;
34
- document: any;
35
- documentName: string;
36
- socketId: string;
37
- };
38
- changeDocument(data: onChangePayload): {
39
- action: string;
40
- document: any;
41
- documentName: string;
42
- socketId: string;
43
- };
44
- messageCount(): {
45
- count: number;
46
- };
47
- documentCount(): {
48
- count: number;
49
- };
50
- documents(): {};
51
- info(): Promise<{
52
- configuration: Partial<Configuration>;
53
- ipAddress: string | null;
54
- nodeVersion: string;
55
- platform: NodeJS.Platform;
56
- started: string;
57
- version: string;
58
- }>;
59
- private getIpAddress;
60
- private static readableYDoc;
61
- }
@@ -1,30 +0,0 @@
1
- /// <reference types="node" />
2
- /// <reference types="node" />
3
- import { IncomingMessage, ServerResponse } from 'http';
4
- import { Socket } from 'net';
5
- import WebSocket, { WebSocketServer } from 'ws';
6
- import { Storage } from './Storage';
7
- export interface Configuration {
8
- password: string | undefined;
9
- path: string;
10
- port: number | undefined;
11
- storage: Storage | undefined;
12
- user: string | undefined;
13
- }
14
- export declare class Dashboard {
15
- configuration: Configuration;
16
- websocketServer: WebSocketServer;
17
- connections: Map<WebSocket, any>;
18
- /**
19
- * Constructor
20
- */
21
- constructor(configuration?: Partial<Configuration>);
22
- createServer(): void;
23
- handleRequest(request: IncomingMessage, response: ServerResponse): boolean;
24
- handleUpgrade(request: IncomingMessage, socket: Socket, head: any): boolean;
25
- handleConnection(connection: WebSocket, request: IncomingMessage): void;
26
- close(connection: WebSocket): void;
27
- send(message: string): void;
28
- private sendInitialDataToClient;
29
- private basicAuth;
30
- }
@@ -1,35 +0,0 @@
1
- /// <reference types="node" />
2
- import EventEmitter from 'events';
3
- export declare class Storage extends EventEmitter {
4
- timed: Map<string, any>;
5
- constant: Map<string, any>;
6
- /**
7
- * Get all constant values.
8
- */
9
- all(): Promise<any>;
10
- /**
11
- * Get a constant value by the given key.
12
- */
13
- get(key: string, defaultValue?: any): Promise<any>;
14
- /**
15
- * Set a constant value by the given key.
16
- */
17
- set(key: string, value: any): Promise<any>;
18
- /**
19
- * Delete a constant value by the given key.
20
- * @param key
21
- */
22
- delete(key: string): Promise<any>;
23
- /**
24
- * Get all timed values.
25
- */
26
- allTimed(): Promise<any>;
27
- /**
28
- * Add a timed value by the given key.
29
- */
30
- add(key: string, value: any): Promise<any>;
31
- /**
32
- * Remove a timed value by the given timestamp and key.
33
- */
34
- remove(key: string, timestamp: string): Promise<any>;
35
- }
@@ -1,38 +0,0 @@
1
- /// <reference types="node" />
2
- import { IncomingMessage, ServerResponse } from 'http';
3
- import { Extension, onChangePayload, onConfigurePayload, onLoadDocumentPayload, onDisconnectPayload, onRequestPayload, onUpgradePayload, connectedPayload } from '@hocuspocus/server';
4
- import WebSocket from 'ws';
5
- import { Storage } from './Storage';
6
- import { Dashboard } from './Dashboard';
7
- import { Collector } from './Collector';
8
- export interface Configuration {
9
- dashboardPath: string;
10
- enableDashboard: boolean;
11
- metricsInterval: number;
12
- osMetricsInterval: number;
13
- password: string | undefined;
14
- port: number | undefined;
15
- user: string | undefined;
16
- }
17
- export declare class Monitor implements Extension {
18
- configuration: Configuration;
19
- storage: Storage;
20
- collector: Collector;
21
- dashboard?: Dashboard;
22
- /**
23
- * Constructor
24
- */
25
- constructor(configuration?: Partial<Configuration>);
26
- private collectOsMetrics;
27
- private collectConnectionMetrics;
28
- private cleanMetrics;
29
- handleRequest(request: IncomingMessage, response: ServerResponse): boolean | undefined;
30
- handleConnection(websocket: WebSocket, request: IncomingMessage): void | undefined;
31
- onRequest({ request, response }: onRequestPayload): Promise<void>;
32
- onUpgrade({ request, socket, head }: onUpgradePayload): Promise<void>;
33
- connected(data: connectedPayload): Promise<void>;
34
- onDisconnect(data: onDisconnectPayload): Promise<void>;
35
- onLoadDocument(data: onLoadDocumentPayload): Promise<void>;
36
- onChange(data: onChangePayload): Promise<void>;
37
- onConfigure(data: onConfigurePayload): Promise<void>;
38
- }