@hocuspocus/provider 1.0.0-alpha.14 → 1.0.0-alpha.18

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.
@@ -4,30 +4,94 @@ import * as mutex from 'lib0/mutex';
4
4
  import { CloseEvent, MessageEvent, OpenEvent } from 'ws';
5
5
  import EventEmitter from './EventEmitter';
6
6
  import { OutgoingMessage } from './OutgoingMessage';
7
+ import { ConstructableOutgoingMessage } from './types';
7
8
  export declare enum WebSocketStatus {
8
9
  Connecting = "connecting",
9
10
  Connected = "connected",
10
11
  Disconnected = "disconnected"
11
12
  }
12
13
  export interface HocuspocusProviderOptions {
14
+ /**
15
+ * URL of your @hocuspocus/server instance
16
+ */
13
17
  url: string;
18
+ /**
19
+ * The identifier/name of your document
20
+ */
14
21
  name: string;
22
+ /**
23
+ * The actual Y.js document
24
+ */
15
25
  document: Y.Doc;
26
+ /**
27
+ * Pass `false` to start the connection manually.
28
+ */
16
29
  connect: boolean;
30
+ /**
31
+ * Pass false to disable broadcasting between browser tabs.
32
+ */
17
33
  broadcast: boolean;
34
+ /**
35
+ * An Awareness instance to keep the presence state of all clients.
36
+ */
18
37
  awareness: Awareness;
19
- token: string | (() => string) | (() => Promise<string>);
38
+ /**
39
+ * A token that’s sent to the backend for authentication purposes.
40
+ */
41
+ token: string | (() => string) | (() => Promise<string>) | null;
42
+ /**
43
+ * URL parameters that should be added.
44
+ */
20
45
  parameters: {
21
46
  [key: string]: any;
22
47
  };
48
+ /**
49
+ * An optional WebSocket polyfill, for example for Node.js
50
+ */
23
51
  WebSocketPolyfill: any;
52
+ /**
53
+ * Force syncing the document in the defined interval.
54
+ */
24
55
  forceSyncInterval: false | number;
25
- reconnectTimeoutBase: number;
26
- maxReconnectTimeout: number;
56
+ /**
57
+ * Disconnect when no message is received for the defined amount of milliseconds.
58
+ */
27
59
  messageReconnectTimeout: number;
60
+ /**
61
+ * The delay between each attempt in milliseconds. You can provide a factor to have the delay grow exponentially.
62
+ */
63
+ delay: number;
64
+ /**
65
+ * The intialDelay 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.
66
+ */
67
+ initialDelay: number;
68
+ /**
69
+ * The factor option is used to grow the delay exponentially.
70
+ */
71
+ factor: number;
72
+ /**
73
+ * The maximum number of attempts or 0 if there is no limit on number of attempts.
74
+ */
75
+ maxAttempts: number;
76
+ /**
77
+ * minDelay is used to set a lower bound of delay when jitter is enabled. This property has no effect if jitter is disabled.
78
+ */
79
+ minDelay: number;
80
+ /**
81
+ * 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.
82
+ */
83
+ maxDelay: number;
84
+ /**
85
+ * If jitter is true then the calculated delay will be a random integer value between minDelay and the calculated delay for the current iteration.
86
+ */
87
+ jitter: boolean;
88
+ /**
89
+ * 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.
90
+ */
91
+ timeout: number;
28
92
  onAuthenticated: () => void;
29
- onAuthenticationFailed: ({ reason: string }: {
30
- reason: any;
93
+ onAuthenticationFailed: ({ reason }: {
94
+ reason: string;
31
95
  }) => void;
32
96
  onOpen: (event: OpenEvent) => void;
33
97
  onConnect: () => void;
@@ -40,23 +104,28 @@ export interface HocuspocusProviderOptions {
40
104
  onDestroy: () => void;
41
105
  onAwarenessUpdate: (states: any) => void;
42
106
  onAwarenessChange: (states: any) => void;
43
- debug: boolean;
44
107
  }
45
108
  export declare class HocuspocusProvider extends EventEmitter {
46
109
  options: HocuspocusProviderOptions;
47
- awareness: Awareness;
48
110
  subscribedToBroadcastChannel: boolean;
49
111
  webSocket: any;
50
112
  shouldConnect: boolean;
51
113
  status: WebSocketStatus;
52
- failedConnectionAttempts: number;
53
114
  isSynced: boolean;
54
115
  isAuthenticated: boolean;
55
116
  lastMessageReceived: number;
56
117
  mux: mutex.mutex;
57
118
  intervals: any;
119
+ connectionAttempt: {
120
+ resolve: (value?: any) => void;
121
+ reject: (reason?: any) => void;
122
+ } | null;
58
123
  constructor(options?: Partial<HocuspocusProviderOptions>);
59
124
  setOptions(options?: Partial<HocuspocusProviderOptions>): void;
125
+ connect(): Promise<void>;
126
+ createWebSocketConnection(): Promise<unknown>;
127
+ resolveConnectionAttempt(): void;
128
+ rejectConnectionAttempt(): void;
60
129
  get document(): Y.Doc;
61
130
  get awareness(): Awareness;
62
131
  checkConnection(): void;
@@ -71,14 +140,12 @@ export declare class HocuspocusProvider extends EventEmitter {
71
140
  get synced(): boolean;
72
141
  set synced(state: boolean);
73
142
  get isAuthenticationRequired(): boolean;
74
- connect(): void;
75
143
  disconnect(): void;
76
- createWebSocketConnection(): void;
77
144
  onOpen(event: OpenEvent): void;
78
- getToken(): Promise<string>;
145
+ getToken(): Promise<string | null>;
79
146
  webSocketConnectionEstablished(): Promise<void>;
80
147
  startSync(): void;
81
- send(Message: OutgoingMessage, args: any, broadcast?: boolean): void;
148
+ send(Message: ConstructableOutgoingMessage, args: any, broadcast?: boolean): void;
82
149
  onMessage(event: MessageEvent): void;
83
150
  onClose(event: CloseEvent): void;
84
151
  destroy(): void;
@@ -86,7 +153,6 @@ export declare class HocuspocusProvider extends EventEmitter {
86
153
  broadcastChannelSubscriber(data: ArrayBuffer): void;
87
154
  subscribeToBroadcastChannel(): void;
88
155
  disconnectBroadcastChannel(): void;
89
- broadcast(Message: OutgoingMessage, args: any): void;
90
- log(message: string): void;
156
+ broadcast(Message: ConstructableOutgoingMessage, args?: any): void;
91
157
  setAwarenessField(key: string, value: any): void;
92
158
  }
@@ -1,15 +1,9 @@
1
1
  import { Encoder } from 'lib0/encoding';
2
- import { AuthenticationMessage } from './OutgoingMessages/AuthenticationMessage';
3
- import { AwarenessMessage } from './OutgoingMessages/AwarenessMessage';
4
- import { QueryAwarenessMessage } from './OutgoingMessages/QueryAwarenessMessage';
5
- import { SyncStepOneMessage } from './OutgoingMessages/SyncStepOneMessage';
6
- import { SyncStepTwoMessage } from './OutgoingMessages/SyncStepTwoMessage';
7
- import { UpdateMessage } from './OutgoingMessages/UpdateMessage';
8
- import { Constructable } from './types';
2
+ import { ConstructableOutgoingMessage } from './types';
9
3
  export declare class MessageSender {
10
4
  encoder: Encoder;
11
5
  message: any;
12
- constructor(Message: Constructable<AuthenticationMessage> | Constructable<AwarenessMessage> | Constructable<QueryAwarenessMessage> | Constructable<SyncStepOneMessage> | Constructable<SyncStepTwoMessage> | Constructable<UpdateMessage>, args?: any);
6
+ constructor(Message: ConstructableOutgoingMessage, args?: any);
13
7
  create(): Uint8Array;
14
8
  send(webSocket: any): void;
15
9
  broadcast(channel: string): void;
@@ -1,6 +1,12 @@
1
1
  import { Awareness } from 'y-protocols/awareness';
2
2
  import * as Y from 'yjs';
3
3
  import { Encoder } from 'lib0/encoding';
4
+ import { AuthenticationMessage } from './OutgoingMessages/AuthenticationMessage';
5
+ import { AwarenessMessage } from './OutgoingMessages/AwarenessMessage';
6
+ import { QueryAwarenessMessage } from './OutgoingMessages/QueryAwarenessMessage';
7
+ import { SyncStepOneMessage } from './OutgoingMessages/SyncStepOneMessage';
8
+ import { SyncStepTwoMessage } from './OutgoingMessages/SyncStepTwoMessage';
9
+ import { UpdateMessage } from './OutgoingMessages/UpdateMessage';
4
10
  export declare enum MessageType {
5
11
  Sync = 0,
6
12
  Awareness = 1,
@@ -25,3 +31,4 @@ export interface OutgoingMessageArguments {
25
31
  export interface Constructable<T> {
26
32
  new (...args: any): T;
27
33
  }
34
+ export declare type ConstructableOutgoingMessage = Constructable<AuthenticationMessage> | Constructable<AwarenessMessage> | Constructable<QueryAwarenessMessage> | Constructable<SyncStepOneMessage> | Constructable<SyncStepTwoMessage> | Constructable<UpdateMessage>;
@@ -40,9 +40,10 @@ declare class Connection {
40
40
  */
41
41
  private check;
42
42
  /**
43
- * Send first sync step
43
+ * Send the current document awareness to the client, if any
44
44
  * @private
45
45
  */
46
+ private sendCurrentAwareness;
46
47
  /**
47
48
  * Handle an incoming message
48
49
  * @private
@@ -43,9 +43,9 @@ declare class Document extends Doc {
43
43
  */
44
44
  removeConnection(connection: Connection): Document;
45
45
  /**
46
- * Get the number of active connections
46
+ * Get the number of active connections for this document
47
47
  */
48
- connectionsCount(): number;
48
+ getConnectionsCount(): number;
49
49
  /**
50
50
  * Get an array of registered connections
51
51
  */
@@ -25,6 +25,14 @@ export declare class Hocuspocus {
25
25
  * Start the server
26
26
  */
27
27
  listen(): Promise<void>;
28
+ /**
29
+ * Get the total number of active documents
30
+ */
31
+ getDocumentsCount(): number;
32
+ /**
33
+ * Get the total number of active connections
34
+ */
35
+ getConnectionsCount(): number;
28
36
  /**
29
37
  * Force close one or more connections
30
38
  */
@@ -77,6 +77,7 @@ export interface onCreateDocumentPayload {
77
77
  context: any;
78
78
  document: Document;
79
79
  documentName: string;
80
+ instance: Hocuspocus;
80
81
  requestHeaders: IncomingHttpHeaders;
81
82
  requestParameters: URLSearchParams;
82
83
  socketId: string;
@@ -87,6 +88,7 @@ export interface onChangePayload {
87
88
  context: any;
88
89
  document: Document;
89
90
  documentName: string;
91
+ instance: Hocuspocus;
90
92
  requestHeaders: IncomingHttpHeaders;
91
93
  requestParameters: URLSearchParams;
92
94
  update: Uint8Array;
@@ -97,6 +99,7 @@ export interface onDisconnectPayload {
97
99
  context: any;
98
100
  document: Document;
99
101
  documentName: string;
102
+ instance: Hocuspocus;
100
103
  requestHeaders: IncomingHttpHeaders;
101
104
  requestParameters: URLSearchParams;
102
105
  socketId: string;
@@ -116,6 +119,7 @@ export interface onListenPayload {
116
119
  port: number;
117
120
  }
118
121
  export interface onDestroyPayload {
122
+ instance: Hocuspocus;
119
123
  }
120
124
  export interface onConfigurePayload {
121
125
  configuration: Configuration;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hocuspocus/provider",
3
- "version": "1.0.0-alpha.14",
3
+ "version": "1.0.0-alpha.18",
4
4
  "description": "hocuspocus provider",
5
5
  "homepage": "https://hocuspocus.dev",
6
6
  "keywords": [
@@ -23,11 +23,12 @@
23
23
  "dist"
24
24
  ],
25
25
  "dependencies": {
26
+ "@lifeomic/attempt": "^3.0.0",
26
27
  "lib0": "^0.2.42",
27
28
  "y-protocols": "^1.0.5",
28
29
  "yjs": "^13.5.8"
29
30
  },
30
- "gitHead": "3b1ae61a70eca44992e40e545b2d893a4cef75b2",
31
+ "gitHead": "b3f043fa2f99dd35bd68940efcb0d5b86a165585",
31
32
  "publishConfig": {
32
33
  "access": "public"
33
34
  }