@hocuspocus/provider 1.0.0-alpha.34 → 1.0.0-alpha.37

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.
Files changed (30) hide show
  1. package/dist/hocuspocus-provider.cjs +23 -18
  2. package/dist/hocuspocus-provider.cjs.map +1 -1
  3. package/dist/hocuspocus-provider.esm.js +23 -18
  4. package/dist/hocuspocus-provider.esm.js.map +1 -1
  5. package/dist/packages/extension-database/src/Database.d.ts +3 -5
  6. package/dist/packages/extension-monitor/src/Collector.d.ts +2 -3
  7. package/dist/packages/extension-redis/src/Redis.d.ts +91 -9
  8. package/dist/packages/extension-redis/src/index.d.ts +0 -1
  9. package/dist/packages/provider/src/HocuspocusProvider.d.ts +15 -21
  10. package/dist/packages/provider/src/types.d.ts +33 -0
  11. package/dist/packages/server/src/Document.d.ts +1 -1
  12. package/dist/packages/server/src/Hocuspocus.d.ts +6 -2
  13. package/dist/packages/server/src/MessageReceiver.d.ts +1 -1
  14. package/dist/packages/server/src/OutgoingMessage.d.ts +1 -0
  15. package/dist/packages/server/src/index.d.ts +3 -4
  16. package/dist/packages/server/src/types.d.ts +26 -7
  17. package/dist/tests/{extension-redis-rewrite/closeConnections.d.ts → extension-database/fetch.d.ts} +0 -0
  18. package/dist/tests/{extension-redis-rewrite/getConnectionCount.d.ts → extension-redis/closeConnections.d.ts} +0 -0
  19. package/dist/tests/{extension-redis-rewrite/getDocumentsCount.d.ts → extension-redis/getConnectionCount.d.ts} +0 -0
  20. package/dist/tests/{extension-redis-rewrite/onAwarenessChange.d.ts → extension-redis/getDocumentsCount.d.ts} +0 -0
  21. package/dist/tests/{extension-redis-rewrite/onChange.d.ts → extension-redis/onAwarenessChange.d.ts} +0 -0
  22. package/dist/tests/{extension-redis-rewrite/onStoreDocument.d.ts → extension-redis/onChange.d.ts} +0 -0
  23. package/dist/tests/extension-redis/{onLoadDocument.d.ts → onStoreDocument.d.ts} +0 -0
  24. package/dist/tests/utils/index.d.ts +1 -0
  25. package/dist/tests/utils/randomInteger.d.ts +1 -0
  26. package/package.json +5 -5
  27. package/src/HocuspocusProvider.ts +33 -29
  28. package/src/types.ts +42 -0
  29. package/dist/packages/extension-redis/src/RedisCluster.d.ts +0 -4
  30. package/dist/tests/extension-redis/onSynced.d.ts +0 -1
@@ -1,12 +1,10 @@
1
- import { Extension, onChangePayload, onLoadDocumentPayload, storePayload } from '@hocuspocus/server';
1
+ import { Extension, onChangePayload, onLoadDocumentPayload, storePayload, fetchPayload } from '@hocuspocus/server';
2
2
  export interface DatabaseConfiguration {
3
3
  /**
4
4
  * Pass a Promise to retrieve updates from your database. The Promise should resolve to
5
5
  * an array of items with Y.js-compatible binary data.
6
6
  */
7
- fetch: ({ documentName }: {
8
- documentName: string;
9
- }) => Promise<Uint8Array | null>;
7
+ fetch: (data: fetchPayload) => Promise<Uint8Array | null>;
10
8
  /**
11
9
  * Pass a function to store updates in your database.
12
10
  */
@@ -24,7 +22,7 @@ export declare class Database implements Extension {
24
22
  /**
25
23
  * Get stored data from the database.
26
24
  */
27
- onLoadDocument({ document, documentName }: onLoadDocumentPayload): Promise<any>;
25
+ onLoadDocument(data: onLoadDocumentPayload): Promise<any>;
28
26
  /**
29
27
  * Store new updates in the database.
30
28
  */
@@ -3,7 +3,6 @@ import { Configuration, onConnectPayload, onDisconnectPayload, onLoadDocumentPay
3
3
  export declare class Collector {
4
4
  serverConfiguration: Partial<Configuration>;
5
5
  version: string;
6
- yjsVersion: string;
7
6
  connections: {};
8
7
  messages: {};
9
8
  messageCounter: number;
@@ -51,12 +50,12 @@ export declare class Collector {
51
50
  documents(): {};
52
51
  info(): Promise<{
53
52
  configuration: Partial<Configuration>;
54
- ipAddress: string;
53
+ ipAddress: string | null;
55
54
  nodeVersion: string;
56
55
  platform: NodeJS.Platform;
57
56
  started: string;
58
57
  version: string;
59
- yjsVersion: string;
60
58
  }>;
59
+ private getIpAddress;
61
60
  private static readableYDoc;
62
61
  }
@@ -1,16 +1,98 @@
1
- import { RedisPersistence } from 'y-redis';
2
- import { Extension, onConnectPayload, onLoadDocumentPayload, onDisconnectPayload } from '@hocuspocus/server';
1
+ import RedisClient from 'ioredis';
2
+ import Redlock from 'redlock';
3
+ import { Document, Extension, afterLoadDocumentPayload, afterStoreDocumentPayload, onDisconnectPayload, onStoreDocumentPayload, onAwarenessUpdatePayload, onChangePayload, Debugger, onConfigurePayload, onListenPayload } from '@hocuspocus/server';
3
4
  export interface Configuration {
5
+ /**
6
+ * Redis port
7
+ */
8
+ port: number;
9
+ /**
10
+ * Redis host
11
+ */
12
+ host: string;
13
+ /**
14
+ * Options passed directly to Redis constructor
15
+ *
16
+ * https://github.com/luin/ioredis/blob/master/API.md#new-redisport-host-options
17
+ */
18
+ options?: RedisClient.RedisOptions;
19
+ /**
20
+ * An unique instance name, required to filter messages in Redis.
21
+ * If none is provided an unique id is generated.
22
+ */
23
+ identifier: string;
24
+ /**
25
+ * Namespace for Redis keys, if none is provided 'hocuspocus' is used
26
+ */
27
+ prefix: string;
28
+ /**
29
+ * The maximum time for the Redis lock in ms (in case it can’t be released).
30
+ */
31
+ lockTimeout: number;
4
32
  }
5
33
  export declare class Redis implements Extension {
34
+ /**
35
+ * Make sure to give that extension a higher priority, so
36
+ * the `onStoreDocument` hook is able to intercept the chain,
37
+ * before documents are stored to the database.
38
+ */
39
+ priority: number;
6
40
  configuration: Configuration;
7
- cluster: boolean;
8
- persistence: RedisPersistence | undefined;
41
+ pub: RedisClient.Redis;
42
+ sub: RedisClient.Redis;
43
+ documents: Map<string, Document>;
44
+ redlock: Redlock;
45
+ locks: Map<string, Redlock.Lock>;
46
+ logger: Debugger;
47
+ constructor(configuration: Partial<Configuration>);
48
+ onConfigure({ instance }: onConfigurePayload): Promise<void>;
49
+ onListen({ configuration }: onListenPayload): Promise<void>;
50
+ private getKey;
51
+ private pubKey;
52
+ private subKey;
53
+ private lockKey;
54
+ /**
55
+ * Once a document is laoded, subscribe to the channel in Redis.
56
+ */
57
+ afterLoadDocument({ documentName, document }: afterLoadDocumentPayload): Promise<unknown>;
58
+ /**
59
+ * Publish the first sync step through Redis.
60
+ */
61
+ private publishFirstSyncStep;
62
+ /**
63
+ * Let’s ask Redis who is connected already.
64
+ */
65
+ private requestAwarenessFromOtherInstances;
66
+ /**
67
+ * Before the document is stored, make sure to set a lock in Redis.
68
+ * That’s meant to avoid conflicts with other instances trying to store the document.
69
+ */
70
+ onStoreDocument({ documentName }: onStoreDocumentPayload): Promise<unknown>;
71
+ /**
72
+ * Release the Redis lock, so other instances can store documents.
73
+ */
74
+ afterStoreDocument({ documentName }: afterStoreDocumentPayload): Promise<void>;
75
+ /**
76
+ * Handle awareness update messages received directly by this Hocuspocus instance.
77
+ */
78
+ onAwarenessUpdate({ documentName, awareness }: onAwarenessUpdatePayload): Promise<number>;
79
+ /**
80
+ * Handle incoming messages published on all subscribed document channels.
81
+ * Note that this will also include messages from ourselves as it is not possible
82
+ * in Redis to filter these.
83
+ */
84
+ private handleIncomingMessage;
85
+ /**
86
+ * if the ydoc changed, we'll need to inform other Hocuspocus servers about it.
87
+ */
88
+ onChange(data: onChangePayload): Promise<any>;
89
+ /**
90
+ * Make sure to *not* listen for further changes, when there’s
91
+ * noone connected anymore.
92
+ */
93
+ onDisconnect: ({ documentName, clientsCount }: onDisconnectPayload) => Promise<void>;
9
94
  /**
10
- * Constructor
95
+ * Kill the Redlock connection immediately.
11
96
  */
12
- constructor(configuration?: Partial<Configuration>);
13
- onLoadDocument(data: onLoadDocumentPayload): Promise<import("yjs").Doc | undefined>;
14
- onConnect(data: onConnectPayload): Promise<void>;
15
- onDisconnect(data: onDisconnectPayload): Promise<void>;
97
+ onDestroy(): Promise<void>;
16
98
  }
@@ -1,2 +1 @@
1
1
  export * from './Redis';
2
- export * from './RedisCluster';
@@ -3,14 +3,8 @@ import { Awareness } from 'y-protocols/awareness';
3
3
  import * as mutex from 'lib0/mutex';
4
4
  import type { Event, CloseEvent, MessageEvent } from 'ws';
5
5
  import EventEmitter from './EventEmitter';
6
- import { OutgoingMessage } from './OutgoingMessage';
7
- import { ConstructableOutgoingMessage } from './types';
6
+ import { ConstructableOutgoingMessage, onAuthenticationFailedParameters, onCloseParameters, onDisconnectParameters, onMessageParameters, onOpenParameters, onOutgoingMessageParameters, onStatusParameters, onSyncedParameters, WebSocketStatus } from './types';
8
7
  import { onAwarenessChangeParameters, onAwarenessUpdateParameters } from '.';
9
- export declare enum WebSocketStatus {
10
- Connecting = "connecting",
11
- Connected = "connected",
12
- Disconnected = "disconnected"
13
- }
14
8
  export declare type HocuspocusProviderConfiguration = Required<Pick<CompleteHocuspocusProviderConfiguration, 'url' | 'name'>> & Partial<CompleteHocuspocusProviderConfiguration>;
15
9
  export interface CompleteHocuspocusProviderConfiguration {
16
10
  /**
@@ -92,22 +86,18 @@ export interface CompleteHocuspocusProviderConfiguration {
92
86
  */
93
87
  timeout: number;
94
88
  onAuthenticated: () => void;
95
- onAuthenticationFailed: ({ reason }: {
96
- reason: string;
97
- }) => void;
98
- onOpen: (event: Event) => void;
89
+ onAuthenticationFailed: (data: onAuthenticationFailedParameters) => void;
90
+ onOpen: (data: onOpenParameters) => void;
99
91
  onConnect: () => void;
100
- onMessage: (event: MessageEvent) => void;
101
- onOutgoingMessage: (message: OutgoingMessage) => void;
102
- onStatus: (status: any) => void;
103
- onSynced: ({ state }: {
104
- state: boolean;
105
- }) => void;
106
- onDisconnect: (event: CloseEvent) => void;
107
- onClose: (event: CloseEvent) => void;
92
+ onMessage: (data: onMessageParameters) => void;
93
+ onOutgoingMessage: (data: onOutgoingMessageParameters) => void;
94
+ onStatus: (data: onStatusParameters) => void;
95
+ onSynced: (data: onSyncedParameters) => void;
96
+ onDisconnect: (data: onDisconnectParameters) => void;
97
+ onClose: (data: onCloseParameters) => void;
108
98
  onDestroy: () => void;
109
- onAwarenessUpdate: ({ states }: onAwarenessUpdateParameters) => void;
110
- onAwarenessChange: ({ states }: onAwarenessChangeParameters) => void;
99
+ onAwarenessUpdate: (data: onAwarenessUpdateParameters) => void;
100
+ onAwarenessChange: (data: onAwarenessChangeParameters) => void;
111
101
  /**
112
102
  * Don’t output any warnings.
113
103
  */
@@ -130,6 +120,7 @@ export declare class HocuspocusProvider extends EventEmitter {
130
120
  } | null;
131
121
  constructor(configuration: HocuspocusProviderConfiguration);
132
122
  setConfiguration(configuration?: Partial<HocuspocusProviderConfiguration>): void;
123
+ boundConnect: () => Promise<void>;
133
124
  connect(): Promise<void>;
134
125
  createWebSocketConnection(): Promise<unknown>;
135
126
  resolveConnectionAttempt(): void;
@@ -139,6 +130,8 @@ export declare class HocuspocusProvider extends EventEmitter {
139
130
  get awareness(): Awareness;
140
131
  checkConnection(): void;
141
132
  forceSync(): void;
133
+ boundBeforeUnload: () => void;
134
+ beforeUnload(): void;
142
135
  registerEventListeners(): void;
143
136
  documentUpdateHandler(update: Uint8Array, origin: any): void;
144
137
  awarenessUpdateHandler({ added, updated, removed }: any, origin: any): void;
@@ -158,6 +151,7 @@ export declare class HocuspocusProvider extends EventEmitter {
158
151
  onClose(event: CloseEvent): void;
159
152
  destroy(): void;
160
153
  get broadcastChannel(): string;
154
+ boundBroadcastChannelSubscriber: (data: ArrayBuffer) => void;
161
155
  broadcastChannelSubscriber(data: ArrayBuffer): void;
162
156
  subscribeToBroadcastChannel(): void;
163
157
  disconnectBroadcastChannel(): void;
@@ -1,18 +1,26 @@
1
1
  import { Awareness } from 'y-protocols/awareness';
2
2
  import * as Y from 'yjs';
3
3
  import { Encoder } from 'lib0/encoding';
4
+ import type { Event, CloseEvent, MessageEvent } from 'ws';
4
5
  import { AuthenticationMessage } from './OutgoingMessages/AuthenticationMessage';
5
6
  import { AwarenessMessage } from './OutgoingMessages/AwarenessMessage';
6
7
  import { QueryAwarenessMessage } from './OutgoingMessages/QueryAwarenessMessage';
7
8
  import { SyncStepOneMessage } from './OutgoingMessages/SyncStepOneMessage';
8
9
  import { SyncStepTwoMessage } from './OutgoingMessages/SyncStepTwoMessage';
9
10
  import { UpdateMessage } from './OutgoingMessages/UpdateMessage';
11
+ import { IncomingMessage } from './IncomingMessage';
12
+ import { OutgoingMessage } from './OutgoingMessage';
10
13
  export declare enum MessageType {
11
14
  Sync = 0,
12
15
  Awareness = 1,
13
16
  Auth = 2,
14
17
  QueryAwareness = 3
15
18
  }
19
+ export declare enum WebSocketStatus {
20
+ Connecting = "connecting",
21
+ Connected = "connected",
22
+ Disconnected = "disconnected"
23
+ }
16
24
  export interface OutgoingMessageInterface {
17
25
  encoder: Encoder;
18
26
  type?: MessageType;
@@ -32,6 +40,31 @@ export interface Constructable<T> {
32
40
  new (...args: any): T;
33
41
  }
34
42
  export declare type ConstructableOutgoingMessage = Constructable<AuthenticationMessage> | Constructable<AwarenessMessage> | Constructable<QueryAwarenessMessage> | Constructable<SyncStepOneMessage> | Constructable<SyncStepTwoMessage> | Constructable<UpdateMessage>;
43
+ export declare type onAuthenticationFailedParameters = {
44
+ reason: string;
45
+ };
46
+ export declare type onOpenParameters = {
47
+ event: Event;
48
+ };
49
+ export declare type onMessageParameters = {
50
+ event: MessageEvent;
51
+ message: IncomingMessage;
52
+ };
53
+ export declare type onOutgoingMessageParameters = {
54
+ message: OutgoingMessage;
55
+ };
56
+ export declare type onStatusParameters = {
57
+ status: WebSocketStatus;
58
+ };
59
+ export declare type onSyncedParameters = {
60
+ state: boolean;
61
+ };
62
+ export declare type onDisconnectParameters = {
63
+ event: CloseEvent;
64
+ };
65
+ export declare type onCloseParameters = {
66
+ event: CloseEvent;
67
+ };
35
68
  export declare type onAwarenessUpdateParameters = {
36
69
  states: StatesArray;
37
70
  };
@@ -19,7 +19,7 @@ export declare class Document extends Doc {
19
19
  /**
20
20
  * Constructor.
21
21
  */
22
- constructor(name: string, logger: Debugger);
22
+ constructor(name: string, logger: Debugger, yDocOptions: {});
23
23
  /**
24
24
  * Check if the Document is empty
25
25
  */
@@ -1,7 +1,7 @@
1
1
  /// <reference types="node" />
2
2
  import WebSocket, { AddressInfo, WebSocketServer } from 'ws';
3
3
  import { IncomingMessage, Server as HTTPServer } from 'http';
4
- import { Configuration, Hook } from './types';
4
+ import { Configuration, HookName, HookPayload } from './types';
5
5
  import Document from './Document';
6
6
  import { Debugger } from './Debugger';
7
7
  import { onListenPayload } from '.';
@@ -12,6 +12,10 @@ export declare const defaultConfiguration: {
12
12
  debounce: number;
13
13
  maxDebounce: number;
14
14
  quiet: boolean;
15
+ yDocOptions: {
16
+ gc: boolean;
17
+ gcFilter: () => boolean;
18
+ };
15
19
  };
16
20
  /**
17
21
  * Hocuspocus Server
@@ -88,7 +92,7 @@ export declare class Hocuspocus {
88
92
  * Run the given hook on all configured extensions.
89
93
  * Runs the given callback after each hook.
90
94
  */
91
- hooks(name: Hook, payload: any, callback?: Function | null): Promise<any>;
95
+ hooks(name: HookName, payload: HookPayload, callback?: Function | null): Promise<any>;
92
96
  /**
93
97
  * Get parameters by the given request
94
98
  */
@@ -8,6 +8,6 @@ export declare class MessageReceiver {
8
8
  logger: Debugger;
9
9
  constructor(message: IncomingMessage, logger: Debugger);
10
10
  apply(document: Document, connection?: Connection, reply?: (message: Uint8Array) => void): void;
11
- readSyncMessage(message: IncomingMessage, document: Document, connection?: Connection, reply?: (message: Uint8Array) => void): 0 | 2 | 1;
11
+ readSyncMessage(message: IncomingMessage, document: Document, connection?: Connection, reply?: (message: Uint8Array) => void, requestFirstSync?: boolean): 0 | 2 | 1;
12
12
  applyQueryAwarenessMessage(awareness: Awareness, reply?: (message: Uint8Array) => void): void;
13
13
  }
@@ -7,6 +7,7 @@ export declare class OutgoingMessage {
7
7
  category?: string;
8
8
  constructor();
9
9
  createSyncMessage(): OutgoingMessage;
10
+ createSyncReplyMessage(): OutgoingMessage;
10
11
  createAwarenessUpdateMessage(awareness: Awareness, changedClients?: Array<any>): OutgoingMessage;
11
12
  writeQueryAwareness(): OutgoingMessage;
12
13
  writeAuthenticated(): OutgoingMessage;
@@ -1,9 +1,8 @@
1
- export * from './Hocuspocus';
2
1
  export * from './Connection';
2
+ export * from './Debugger';
3
3
  export * from './Document';
4
+ export * from './Hocuspocus';
4
5
  export * from './IncomingMessage';
6
+ export * from './MessageReceiver';
5
7
  export * from './OutgoingMessage';
6
8
  export * from './types';
7
- export * from './MessageReceiver';
8
- export * from './Document';
9
- export * from './Connection';
@@ -1,7 +1,6 @@
1
1
  /// <reference types="node" />
2
2
  import { IncomingHttpHeaders, IncomingMessage, ServerResponse } from 'http';
3
3
  import { URLSearchParams } from 'url';
4
- import { Socket } from 'net';
5
4
  import { Awareness } from 'y-protocols/awareness';
6
5
  import Document from './Document';
7
6
  import { Hocuspocus } from './Hocuspocus';
@@ -10,7 +9,8 @@ export declare enum MessageType {
10
9
  Sync = 0,
11
10
  Awareness = 1,
12
11
  Auth = 2,
13
- QueryAwareness = 3
12
+ QueryAwareness = 3,
13
+ SyncReply = 4
14
14
  }
15
15
  export interface AwarenessUpdate {
16
16
  added: Array<any>;
@@ -44,11 +44,12 @@ export interface Extension {
44
44
  onDisconnect?(data: onDisconnectPayload): Promise<any>;
45
45
  onDestroy?(data: onDestroyPayload): Promise<any>;
46
46
  }
47
- export declare type Hook = 'onConfigure' | 'onListen' | 'onUpgrade' | 'onConnect' | 'connected' | 'onAuthenticate' |
47
+ export declare type HookName = 'onConfigure' | 'onListen' | 'onUpgrade' | 'onConnect' | 'connected' | 'onAuthenticate' |
48
48
  /**
49
49
  * @deprecated onCreateDocument is deprecated, use onLoadDocument instead
50
50
  */
51
51
  'onCreateDocument' | 'onLoadDocument' | 'afterLoadDocument' | 'onChange' | 'onStoreDocument' | 'afterStoreDocument' | 'onAwarenessUpdate' | 'onRequest' | 'onDisconnect' | 'onDestroy';
52
+ export declare type HookPayload = onConfigurePayload | onListenPayload | onUpgradePayload | onConnectPayload | connectedPayload | onAuthenticatePayload | onLoadDocumentPayload | onChangePayload | onStoreDocumentPayload | afterStoreDocumentPayload | onAwarenessUpdatePayload | onRequestPayload | onDisconnectPayload | onDestroyPayload;
52
53
  export interface Configuration extends Extension {
53
54
  /**
54
55
  * A name for the instance, used for logging.
@@ -79,6 +80,13 @@ export interface Configuration extends Extension {
79
80
  * By default, the servers show a start screen. If passed false, the server will start quietly.
80
81
  */
81
82
  quiet: boolean;
83
+ /**
84
+ * options to pass to the ydoc document
85
+ */
86
+ yDocOptions: {
87
+ gc: boolean;
88
+ gcFilter: () => boolean;
89
+ };
82
90
  /**
83
91
  * Function which returns the (customized) document name based on the request
84
92
  */
@@ -179,6 +187,16 @@ export declare type StatesArray = {
179
187
  clientId: number;
180
188
  [key: string | number]: any;
181
189
  }[];
190
+ export interface fetchPayload {
191
+ context: any;
192
+ document: Document;
193
+ documentName: string;
194
+ instance: Hocuspocus;
195
+ requestHeaders: IncomingHttpHeaders;
196
+ requestParameters: URLSearchParams;
197
+ socketId: string;
198
+ connection: ConnectionConfiguration;
199
+ }
182
200
  export interface storePayload extends onStoreDocumentPayload {
183
201
  state: Buffer;
184
202
  }
@@ -198,20 +216,21 @@ export interface onRequestPayload {
198
216
  instance: Hocuspocus;
199
217
  }
200
218
  export interface onUpgradePayload {
201
- head: any;
202
219
  request: IncomingMessage;
203
- socket: Socket;
220
+ socket: any;
221
+ head: any;
204
222
  instance: Hocuspocus;
205
223
  }
206
224
  export interface onListenPayload {
225
+ instance: Hocuspocus;
226
+ configuration: Configuration;
207
227
  port: number;
208
228
  }
209
229
  export interface onDestroyPayload {
210
230
  instance: Hocuspocus;
211
231
  }
212
232
  export interface onConfigurePayload {
233
+ instance: Hocuspocus;
213
234
  configuration: Configuration;
214
235
  version: string;
215
- yjsVersion: string;
216
- instance: Hocuspocus;
217
236
  }
@@ -2,6 +2,7 @@ export * from './createDirectory';
2
2
  export * from './flushRedis';
3
3
  export * from './newHocuspocus';
4
4
  export * from './newHocuspocusProvider';
5
+ export * from './randomInteger';
5
6
  export * from './redisConnectionSettings';
6
7
  export * from './removeDirectory';
7
8
  export * from './sleep';
@@ -0,0 +1 @@
1
+ export declare const randomInteger: (min: number, max: number) => number;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hocuspocus/provider",
3
- "version": "1.0.0-alpha.34",
3
+ "version": "1.0.0-alpha.37",
4
4
  "description": "hocuspocus provider",
5
5
  "homepage": "https://hocuspocus.dev",
6
6
  "keywords": [
@@ -11,7 +11,7 @@
11
11
  ],
12
12
  "license": "MIT",
13
13
  "type": "module",
14
- "main": "dist/hocuspocus-provider.esm.js",
14
+ "main": "dist/hocuspocus-provider.cjs",
15
15
  "module": "dist/hocuspocus-provider.esm.js",
16
16
  "types": "dist/packages/provider/src/index.d.ts",
17
17
  "exports": {
@@ -28,11 +28,11 @@
28
28
  "dist"
29
29
  ],
30
30
  "dependencies": {
31
- "@hocuspocus/common": "^1.0.0-alpha.9",
31
+ "@hocuspocus/common": "^1.0.0-alpha.11",
32
32
  "@lifeomic/attempt": "^3.0.2",
33
33
  "lib0": "^0.2.46",
34
34
  "y-protocols": "^1.0.5",
35
- "yjs": "^13.5.28"
35
+ "yjs": "^13.5.29"
36
36
  },
37
- "gitHead": "673dd4b2c723e71bf9b33b8f7804ab35c8417417"
37
+ "gitHead": "af278e7c5d91c74afefd9234379c8179c8b91e6d"
38
38
  }
@@ -19,16 +19,11 @@ import { QueryAwarenessMessage } from './OutgoingMessages/QueryAwarenessMessage'
19
19
  import { AuthenticationMessage } from './OutgoingMessages/AuthenticationMessage'
20
20
  import { AwarenessMessage } from './OutgoingMessages/AwarenessMessage'
21
21
  import { UpdateMessage } from './OutgoingMessages/UpdateMessage'
22
- import { OutgoingMessage } from './OutgoingMessage'
23
- import { ConstructableOutgoingMessage } from './types'
22
+ import {
23
+ ConstructableOutgoingMessage, onAuthenticationFailedParameters, onCloseParameters, onDisconnectParameters, onMessageParameters, onOpenParameters, onOutgoingMessageParameters, onStatusParameters, onSyncedParameters, WebSocketStatus,
24
+ } from './types'
24
25
  import { onAwarenessChangeParameters, onAwarenessUpdateParameters } from '.'
25
26
 
26
- export enum WebSocketStatus {
27
- Connecting = 'connecting',
28
- Connected = 'connected',
29
- Disconnected = 'disconnected',
30
- }
31
-
32
27
  export type HocuspocusProviderConfiguration =
33
28
  Required<Pick<CompleteHocuspocusProviderConfiguration, 'url' | 'name'>>
34
29
  & Partial<CompleteHocuspocusProviderConfiguration>
@@ -111,18 +106,18 @@ export interface CompleteHocuspocusProviderConfiguration {
111
106
  */
112
107
  timeout: number,
113
108
  onAuthenticated: () => void,
114
- onAuthenticationFailed: ({ reason }: { reason: string }) => void,
115
- onOpen: (event: Event) => void,
109
+ onAuthenticationFailed: (data: onAuthenticationFailedParameters) => void,
110
+ onOpen: (data: onOpenParameters) => void,
116
111
  onConnect: () => void,
117
- onMessage: (event: MessageEvent) => void,
118
- onOutgoingMessage: (message: OutgoingMessage) => void,
119
- onStatus: (status: any) => void,
120
- onSynced: ({ state }: { state: boolean }) => void,
121
- onDisconnect: (event: CloseEvent) => void,
122
- onClose: (event: CloseEvent) => void,
112
+ onMessage: (data: onMessageParameters) => void,
113
+ onOutgoingMessage: (data: onOutgoingMessageParameters) => void,
114
+ onStatus: (data: onStatusParameters) => void,
115
+ onSynced: (data: onSyncedParameters) => void,
116
+ onDisconnect: (data: onDisconnectParameters) => void,
117
+ onClose: (data: onCloseParameters) => void,
123
118
  onDestroy: () => void,
124
- onAwarenessUpdate: ({ states }: onAwarenessUpdateParameters) => void,
125
- onAwarenessChange: ({ states }: onAwarenessChangeParameters) => void,
119
+ onAwarenessUpdate: (data: onAwarenessUpdateParameters) => void,
120
+ onAwarenessChange: (data: onAwarenessChangeParameters) => void,
126
121
  /**
127
122
  * Don’t output any warnings.
128
123
  */
@@ -264,6 +259,8 @@ export class HocuspocusProvider extends EventEmitter {
264
259
  this.configuration = { ...this.configuration, ...configuration }
265
260
  }
266
261
 
262
+ boundConnect = this.connect.bind(this)
263
+
267
264
  async connect() {
268
265
  if (this.status === WebSocketStatus.Connected) {
269
266
  return
@@ -313,7 +310,7 @@ export class HocuspocusProvider extends EventEmitter {
313
310
  // Reset the status
314
311
  this.synced = false
315
312
  this.status = WebSocketStatus.Connecting
316
- this.emit('status', { status: 'connecting' })
313
+ this.emit('status', { status: WebSocketStatus.Connecting })
317
314
 
318
315
  // Store resolve/reject for later use
319
316
  this.connectionAttempt = {
@@ -328,7 +325,7 @@ export class HocuspocusProvider extends EventEmitter {
328
325
  this.connectionAttempt = null
329
326
 
330
327
  this.status = WebSocketStatus.Connected
331
- this.emit('status', { status: 'connected' })
328
+ this.emit('status', { status: WebSocketStatus.Connected })
332
329
  this.emit('connect')
333
330
  }
334
331
 
@@ -378,15 +375,19 @@ export class HocuspocusProvider extends EventEmitter {
378
375
  this.send(SyncStepOneMessage, { document: this.document })
379
376
  }
380
377
 
378
+ boundBeforeUnload = this.beforeUnload.bind(this)
379
+
380
+ beforeUnload() {
381
+ removeAwarenessStates(this.awareness, [this.document.clientID], 'window unload')
382
+ }
383
+
381
384
  registerEventListeners() {
382
385
  if (typeof window === 'undefined') {
383
386
  return
384
387
  }
385
388
 
386
- window.addEventListener('online', this.connect.bind(this))
387
- window.addEventListener('beforeunload', () => {
388
- removeAwarenessStates(this.awareness, [this.document.clientID], 'window unload')
389
- })
389
+ window.addEventListener('online', this.boundConnect)
390
+ window.addEventListener('beforeunload', this.boundBeforeUnload)
390
391
  }
391
392
 
392
393
  documentUpdateHandler(update: Uint8Array, origin: any) {
@@ -541,7 +542,7 @@ export class HocuspocusProvider extends EventEmitter {
541
542
  )
542
543
 
543
544
  this.status = WebSocketStatus.Disconnected
544
- this.emit('status', { status: 'disconnected' })
545
+ this.emit('status', { status: WebSocketStatus.Disconnected })
545
546
  this.emit('disconnect', { event })
546
547
  }
547
548
 
@@ -579,7 +580,7 @@ export class HocuspocusProvider extends EventEmitter {
579
580
 
580
581
  // Let’s update the connection status.
581
582
  this.status = WebSocketStatus.Disconnected
582
- this.emit('status', { status: 'disconnected' })
583
+ this.emit('status', { status: WebSocketStatus.Disconnected })
583
584
  this.emit('disconnect', { event })
584
585
  }
585
586
 
@@ -610,13 +611,16 @@ export class HocuspocusProvider extends EventEmitter {
610
611
  return
611
612
  }
612
613
 
613
- window.removeEventListener('online', this.connect.bind(this))
614
+ window.removeEventListener('online', this.boundConnect)
615
+ window.removeEventListener('beforeunload', this.boundBeforeUnload)
614
616
  }
615
617
 
616
618
  get broadcastChannel() {
617
619
  return `${this.serverUrl}/${this.configuration.name}`
618
620
  }
619
621
 
622
+ boundBroadcastChannelSubscriber = this.broadcastChannelSubscriber.bind(this)
623
+
620
624
  broadcastChannelSubscriber(data: ArrayBuffer) {
621
625
  this.mux(() => {
622
626
  const message = new IncomingMessage(data)
@@ -628,7 +632,7 @@ export class HocuspocusProvider extends EventEmitter {
628
632
 
629
633
  subscribeToBroadcastChannel() {
630
634
  if (!this.subscribedToBroadcastChannel) {
631
- bc.subscribe(this.broadcastChannel, this.broadcastChannelSubscriber.bind(this))
635
+ bc.subscribe(this.broadcastChannel, this.boundBroadcastChannelSubscriber)
632
636
  this.subscribedToBroadcastChannel = true
633
637
  }
634
638
 
@@ -649,7 +653,7 @@ export class HocuspocusProvider extends EventEmitter {
649
653
  }, true)
650
654
 
651
655
  if (this.subscribedToBroadcastChannel) {
652
- bc.unsubscribe(this.broadcastChannel, this.broadcastChannelSubscriber.bind(this))
656
+ bc.unsubscribe(this.broadcastChannel, this.boundBroadcastChannelSubscriber)
653
657
  this.subscribedToBroadcastChannel = false
654
658
  }
655
659
  }