@hocuspocus/provider 3.0.6-rc.0 → 3.0.8-rc.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.
@@ -4,7 +4,7 @@ import * as Y from 'yjs';
4
4
  import EventEmitter from './EventEmitter.ts';
5
5
  import type { CompleteHocuspocusProviderWebsocketConfiguration } from './HocuspocusProviderWebsocket.ts';
6
6
  import { HocuspocusProviderWebsocket } from './HocuspocusProviderWebsocket.ts';
7
- import type { ConstructableOutgoingMessage, onAuthenticationFailedParameters, onAwarenessChangeParameters, onAwarenessUpdateParameters, onCloseParameters, onDisconnectParameters, onMessageParameters, onOpenParameters, onOutgoingMessageParameters, onStatelessParameters, onSyncedParameters } from './types.ts';
7
+ import type { ConstructableOutgoingMessage, onAuthenticatedParameters, onAuthenticationFailedParameters, onAwarenessChangeParameters, onAwarenessUpdateParameters, onCloseParameters, onDisconnectParameters, onMessageParameters, onOpenParameters, onOutgoingMessageParameters, onStatelessParameters, onSyncedParameters, onUnsyncedChangesParameters } from './types.ts';
8
8
  export type HocuspocusProviderConfiguration = Required<Pick<CompleteHocuspocusProviderConfiguration, 'name'>> & Partial<CompleteHocuspocusProviderConfiguration> & (Required<Pick<CompleteHocuspocusProviderWebsocketConfiguration, 'url'>> | Required<Pick<CompleteHocuspocusProviderConfiguration, 'websocketProvider'>>);
9
9
  export interface CompleteHocuspocusProviderConfiguration {
10
10
  /**
@@ -36,7 +36,7 @@ export interface CompleteHocuspocusProviderConfiguration {
36
36
  * Force syncing the document in the defined interval.
37
37
  */
38
38
  forceSyncInterval: false | number;
39
- onAuthenticated: () => void;
39
+ onAuthenticated: (data: onAuthenticatedParameters) => void;
40
40
  onAuthenticationFailed: (data: onAuthenticationFailedParameters) => void;
41
41
  onOpen: (data: onOpenParameters) => void;
42
42
  onConnect: () => void;
@@ -49,6 +49,7 @@ export interface CompleteHocuspocusProviderConfiguration {
49
49
  onAwarenessUpdate: (data: onAwarenessUpdateParameters) => void;
50
50
  onAwarenessChange: (data: onAwarenessChangeParameters) => void;
51
51
  onStateless: (data: onStatelessParameters) => void;
52
+ onUnsyncedChanges: (data: onUnsyncedChangesParameters) => void;
52
53
  }
53
54
  export declare class AwarenessError extends Error {
54
55
  code: number;
@@ -60,7 +61,7 @@ export declare class HocuspocusProvider extends EventEmitter {
60
61
  isAuthenticated: boolean;
61
62
  authorizedScope: string | undefined;
62
63
  manageSocket: boolean;
63
- private isAttached;
64
+ private _isAttached;
64
65
  intervals: any;
65
66
  constructor(configuration: HocuspocusProviderConfiguration);
66
67
  boundDocumentUpdateHandler: (update: Uint8Array, origin: any) => void;
@@ -74,6 +75,7 @@ export declare class HocuspocusProvider extends EventEmitter {
74
75
  forwardDestroy: (e: any) => this;
75
76
  setConfiguration(configuration?: Partial<HocuspocusProviderConfiguration>): void;
76
77
  get document(): Y.Doc;
78
+ get isAttached(): boolean;
77
79
  get awareness(): Awareness | null;
78
80
  get hasUnsyncedChanges(): boolean;
79
81
  private resetUnsyncedChanges;
@@ -94,7 +96,7 @@ export declare class HocuspocusProvider extends EventEmitter {
94
96
  get synced(): boolean;
95
97
  set synced(state: boolean);
96
98
  receiveStateless(payload: string): void;
97
- connect(): Promise<void>;
99
+ connect(): Promise<unknown>;
98
100
  disconnect(): void;
99
101
  onOpen(event: Event): Promise<void>;
100
102
  getToken(): Promise<string | null>;
@@ -95,9 +95,7 @@ export declare class HocuspocusProviderWebsocket extends EventEmitter {
95
95
  } | null;
96
96
  constructor(configuration: HocuspocusProviderWebsocketConfiguration);
97
97
  receivedOnOpenPayload?: Event | undefined;
98
- receivedOnStatusPayload?: onStatusParameters | undefined;
99
98
  onOpen(event: Event): Promise<void>;
100
- onStatus(data: onStatusParameters): Promise<void>;
101
99
  attach(provider: HocuspocusProvider): void;
102
100
  detach(provider: HocuspocusProvider): void;
103
101
  setConfiguration(configuration?: Partial<HocuspocusProviderWebsocketConfiguration>): void;
@@ -49,6 +49,9 @@ export type ConstructableOutgoingMessage = Constructable<AuthenticationMessage>
49
49
  export type onAuthenticationFailedParameters = {
50
50
  reason: string;
51
51
  };
52
+ export type onAuthenticatedParameters = {
53
+ scope: 'read-write' | 'readonly';
54
+ };
52
55
  export type onOpenParameters = {
53
56
  event: Event;
54
57
  };
@@ -65,6 +68,9 @@ export type onStatusParameters = {
65
68
  export type onSyncedParameters = {
66
69
  state: boolean;
67
70
  };
71
+ export type onUnsyncedChangesParameters = {
72
+ number: number;
73
+ };
68
74
  export type onDisconnectParameters = {
69
75
  event: CloseEvent;
70
76
  };
@@ -84,103 +90,3 @@ export type StatesArray = {
84
90
  clientId: number;
85
91
  [key: string | number]: any;
86
92
  }[];
87
- export type TCollabThread<Data = any, CommentData = any> = {
88
- id: string;
89
- createdAt: number;
90
- updatedAt: number;
91
- deletedAt: number | null;
92
- resolvedAt?: string;
93
- comments: TCollabComment<CommentData>[];
94
- deletedComments: TCollabComment<CommentData>[];
95
- data: Data;
96
- };
97
- export type TCollabComment<Data = any> = {
98
- id: string;
99
- createdAt: string;
100
- updatedAt: string;
101
- deletedAt?: string;
102
- data: Data;
103
- content: any;
104
- };
105
- export type THistoryVersion = {
106
- name?: string;
107
- version: number;
108
- date: number;
109
- };
110
- export type THistoryConfiguration = {
111
- autoVersioning: boolean;
112
- currentVersion: number;
113
- stateCaptured: number;
114
- };
115
- export type THistoryAction = THistoryDocumentRevertAction | THistoryVersionCreateAction | THistoryVersionPreviewAction;
116
- export type THistoryDocumentRevertAction = {
117
- action: 'document.revert';
118
- /**
119
- * if changes haven't been persisted to a version yet, we'll create one with the specified name,
120
- * expect when `false` is passed.
121
- */
122
- currentVersionName?: string | false;
123
- /**
124
- * Name of the version that is created after the revert. Pass `false` to avoid generating a new version.
125
- */
126
- newVersionName?: string | false;
127
- };
128
- export type THistoryVersionCreateAction = {
129
- action: 'version.create';
130
- name?: string;
131
- };
132
- export type THistoryVersionPreviewAction = {
133
- action: 'version.preview';
134
- version: number;
135
- };
136
- export type THistoryEvent = THistoryVersionPreviewEvent | THistoryVersionCreatedEvent | THistoryDocumentRevertedEvent;
137
- export type THistoryVersionCreatedEvent = {
138
- event: 'version.created';
139
- version: number;
140
- };
141
- export type THistoryVersionPreviewEvent = {
142
- event: 'version.preview';
143
- version: number;
144
- ydoc: string;
145
- };
146
- export type THistoryDocumentRevertedEvent = {
147
- event: 'document.reverted';
148
- version: number;
149
- };
150
- export type DeleteCommentOptions = {
151
- /**
152
- * If `true`, the thread will also be deleted if the deleted comment was the first comment in the thread.
153
- */
154
- deleteThread?: boolean;
155
- /**
156
- * If `true`, will remove the content of the deleted comment
157
- */
158
- deleteContent?: boolean;
159
- };
160
- export type DeleteThreadOptions = {
161
- /**
162
- * If `true`, will remove the comments on the thread,
163
- * otherwise will only mark the thread as deleted
164
- * and keep the comments
165
- * @default false
166
- */
167
- deleteComments?: boolean;
168
- /**
169
- * If `true`, will forcefully remove the thread and all comments,
170
- * otherwise will only mark the thread as deleted
171
- * and keep the comments
172
- * @default false
173
- */
174
- force?: boolean;
175
- };
176
- /**
177
- * The type of thread
178
- */
179
- export type ThreadType = 'archived' | 'unarchived';
180
- export type GetThreadsOptions = {
181
- /**
182
- * The types of threads to get
183
- * @default ['unarchived']
184
- */
185
- types?: Array<ThreadType>;
186
- };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hocuspocus/provider",
3
- "version": "3.0.6-rc.0",
3
+ "version": "3.0.8-rc.0",
4
4
  "description": "hocuspocus provider",
5
5
  "homepage": "https://hocuspocus.dev",
6
6
  "keywords": [
@@ -29,7 +29,7 @@
29
29
  "dist"
30
30
  ],
31
31
  "dependencies": {
32
- "@hocuspocus/common": "^3.0.6-rc.0",
32
+ "@hocuspocus/common": "^3.0.8-rc.0",
33
33
  "@lifeomic/attempt": "^3.0.2",
34
34
  "lib0": "^0.2.87",
35
35
  "ws": "^8.17.1"
@@ -18,6 +18,7 @@ import { SyncStepOneMessage } from './OutgoingMessages/SyncStepOneMessage.ts'
18
18
  import { UpdateMessage } from './OutgoingMessages/UpdateMessage.ts'
19
19
  import type {
20
20
  ConstructableOutgoingMessage,
21
+ onAuthenticatedParameters,
21
22
  onAuthenticationFailedParameters,
22
23
  onAwarenessChangeParameters,
23
24
  onAwarenessUpdateParameters,
@@ -25,8 +26,10 @@ import type {
25
26
  onDisconnectParameters,
26
27
  onMessageParameters,
27
28
  onOpenParameters,
28
- onOutgoingMessageParameters, onStatelessParameters,
29
+ onOutgoingMessageParameters,
30
+ onStatelessParameters,
29
31
  onSyncedParameters,
32
+ onUnsyncedChangesParameters,
30
33
  } from './types.ts'
31
34
 
32
35
  export type HocuspocusProviderConfiguration =
@@ -71,7 +74,7 @@ export interface CompleteHocuspocusProviderConfiguration {
71
74
  */
72
75
  forceSyncInterval: false | number,
73
76
 
74
- onAuthenticated: () => void,
77
+ onAuthenticated: (data: onAuthenticatedParameters) => void,
75
78
  onAuthenticationFailed: (data: onAuthenticationFailedParameters) => void,
76
79
  onOpen: (data: onOpenParameters) => void,
77
80
  onConnect: () => void,
@@ -84,6 +87,7 @@ export interface CompleteHocuspocusProviderConfiguration {
84
87
  onAwarenessUpdate: (data: onAwarenessUpdateParameters) => void,
85
88
  onAwarenessChange: (data: onAwarenessChangeParameters) => void,
86
89
  onStateless: (data: onStatelessParameters) => void
90
+ onUnsyncedChanges: (data: onUnsyncedChangesParameters) => void
87
91
  }
88
92
 
89
93
  export class AwarenessError extends Error {
@@ -112,6 +116,7 @@ export class HocuspocusProvider extends EventEmitter {
112
116
  onAwarenessUpdate: () => null,
113
117
  onAwarenessChange: () => null,
114
118
  onStateless: () => null,
119
+ onUnsyncedChanges: () => null,
115
120
  }
116
121
 
117
122
  isSynced = false
@@ -125,7 +130,7 @@ export class HocuspocusProvider extends EventEmitter {
125
130
  // @internal
126
131
  manageSocket = false
127
132
 
128
- private isAttached = false
133
+ private _isAttached = false
129
134
 
130
135
  intervals: any = {
131
136
  forceSync: null,
@@ -146,6 +151,7 @@ export class HocuspocusProvider extends EventEmitter {
146
151
  this.on('awarenessUpdate', this.configuration.onAwarenessUpdate)
147
152
  this.on('awarenessChange', this.configuration.onAwarenessChange)
148
153
  this.on('stateless', this.configuration.onStateless)
154
+ this.on('unsyncedChanges', this.configuration.onUnsyncedChanges)
149
155
 
150
156
  this.on('authenticated', this.configuration.onAuthenticated)
151
157
  this.on('authenticationFailed', this.configuration.onAuthenticationFailed)
@@ -190,8 +196,6 @@ export class HocuspocusProvider extends EventEmitter {
190
196
 
191
197
  forwardConnect = (e: any) => this.emit('connect', e)
192
198
 
193
- // forwardOpen = (e: any) => this.emit('open', e)
194
-
195
199
  forwardClose = (e: any) => this.emit('close', e)
196
200
 
197
201
  forwardDisconnect = (e: any) => this.emit('disconnect', e)
@@ -214,6 +218,10 @@ export class HocuspocusProvider extends EventEmitter {
214
218
  return this.configuration.document
215
219
  }
216
220
 
221
+ public get isAttached() {
222
+ return this._isAttached
223
+ }
224
+
217
225
  get awareness() {
218
226
  return this.configuration.awareness
219
227
  }
@@ -224,12 +232,12 @@ export class HocuspocusProvider extends EventEmitter {
224
232
 
225
233
  private resetUnsyncedChanges() {
226
234
  this.unsyncedChanges = 1
227
- this.emit('unsyncedChanges', this.unsyncedChanges)
235
+ this.emit('unsyncedChanges', { number: this.unsyncedChanges })
228
236
  }
229
237
 
230
238
  incrementUnsyncedChanges() {
231
239
  this.unsyncedChanges += 1
232
- this.emit('unsyncedChanges', this.unsyncedChanges)
240
+ this.emit('unsyncedChanges', { number: this.unsyncedChanges })
233
241
  }
234
242
 
235
243
  decrementUnsyncedChanges() {
@@ -241,7 +249,7 @@ export class HocuspocusProvider extends EventEmitter {
241
249
  this.synced = true
242
250
  }
243
251
 
244
- this.emit('unsyncedChanges', this.unsyncedChanges)
252
+ this.emit('unsyncedChanges', { number: this.unsyncedChanges })
245
253
  }
246
254
 
247
255
  forceSync() {
@@ -315,10 +323,18 @@ export class HocuspocusProvider extends EventEmitter {
315
323
 
316
324
  // not needed, but provides backward compatibility with e.g. lexical/yjs
317
325
  async connect() {
326
+ if( this.manageSocket ) {
327
+ return this.configuration.websocketProvider.connect()
328
+ }
329
+
318
330
  console.warn('HocuspocusProvider::connect() is deprecated and does not do anything. Please connect/disconnect on the websocketProvider, or attach/deattach providers.')
319
331
  }
320
332
 
321
333
  disconnect() {
334
+ if( this.manageSocket ) {
335
+ return this.configuration.websocketProvider.disconnect()
336
+ }
337
+
322
338
  console.warn('HocuspocusProvider::disconnect() is deprecated and does not do anything. Please connect/disconnect on the websocketProvider, or attach/deattach providers.')
323
339
  }
324
340
 
@@ -367,6 +383,8 @@ export class HocuspocusProvider extends EventEmitter {
367
383
  }
368
384
 
369
385
  send(message: ConstructableOutgoingMessage, args: any) {
386
+ if( !this._isAttached ) return;
387
+
370
388
  const messageSender = new MessageSender(message, args)
371
389
 
372
390
  this.emit('outgoingMessage', { message: messageSender.message })
@@ -443,11 +461,11 @@ export class HocuspocusProvider extends EventEmitter {
443
461
 
444
462
  this.configuration.websocketProvider.detach(this)
445
463
 
446
- this.isAttached = false
464
+ this._isAttached = false
447
465
  }
448
466
 
449
467
  attach() {
450
- if( this.isAttached ) return
468
+ if( this._isAttached ) return
451
469
 
452
470
  this.configuration.websocketProvider.on('connect', this.configuration.onConnect)
453
471
  this.configuration.websocketProvider.on('connect', this.forwardConnect)
@@ -466,7 +484,7 @@ export class HocuspocusProvider extends EventEmitter {
466
484
 
467
485
  this.configuration.websocketProvider.attach(this)
468
486
 
469
- this.isAttached = true
487
+ this._isAttached = true
470
488
  }
471
489
 
472
490
  permissionDeniedHandler(reason: string) {
@@ -478,7 +496,7 @@ export class HocuspocusProvider extends EventEmitter {
478
496
  this.isAuthenticated = true
479
497
  this.authorizedScope = scope
480
498
 
481
- this.emit('authenticated')
499
+ this.emit('authenticated', { scope })
482
500
  }
483
501
 
484
502
  setAwarenessField(key: string, value: any) {
@@ -173,7 +173,6 @@ export class HocuspocusProviderWebsocket extends EventEmitter {
173
173
  this.on('message', this.configuration.onMessage)
174
174
  this.on('outgoingMessage', this.configuration.onOutgoingMessage)
175
175
  this.on('status', this.configuration.onStatus)
176
- this.on('status', this.onStatus.bind(this))
177
176
  this.on('disconnect', this.configuration.onDisconnect)
178
177
  this.on('close', this.configuration.onClose)
179
178
  this.on('destroy', this.configuration.onDestroy)
@@ -201,26 +200,20 @@ export class HocuspocusProviderWebsocket extends EventEmitter {
201
200
 
202
201
  receivedOnOpenPayload?: Event | undefined = undefined
203
202
 
204
- receivedOnStatusPayload?: onStatusParameters | undefined = undefined
205
203
 
206
204
  async onOpen(event: Event) {
207
205
  this.cancelWebsocketRetry = undefined
208
206
  this.receivedOnOpenPayload = event
209
207
  }
210
208
 
211
- async onStatus(data: onStatusParameters) {
212
- this.receivedOnStatusPayload = data
213
- }
214
-
215
209
  attach(provider: HocuspocusProvider) {
216
- let connectPromise: Promise<any> | undefined
217
210
  this.configuration.providerMap.set(provider.configuration.name, provider)
218
211
 
219
212
  if (this.status === WebSocketStatus.Disconnected && this.shouldConnect) {
220
- connectPromise = this.connect()
213
+ this.connect()
221
214
  }
222
215
 
223
- if (this.receivedOnOpenPayload) {
216
+ if (this.receivedOnOpenPayload && this.status === WebSocketStatus.Connected) {
224
217
  provider.onOpen(this.receivedOnOpenPayload)
225
218
  }
226
219
  }
@@ -252,7 +245,6 @@ export class HocuspocusProviderWebsocket extends EventEmitter {
252
245
  }
253
246
 
254
247
  this.receivedOnOpenPayload = undefined
255
- this.receivedOnStatusPayload = undefined
256
248
  this.shouldConnect = true
257
249
 
258
250
  const abortableRetry = () => {
package/src/types.ts CHANGED
@@ -61,6 +61,10 @@ export type onAuthenticationFailedParameters = {
61
61
  reason: string,
62
62
  }
63
63
 
64
+ export type onAuthenticatedParameters = {
65
+ scope: 'read-write' | 'readonly',
66
+ }
67
+
64
68
  export type onOpenParameters = {
65
69
  event: Event,
66
70
  }
@@ -82,6 +86,10 @@ export type onSyncedParameters = {
82
86
  state: boolean,
83
87
  }
84
88
 
89
+ export type onUnsyncedChangesParameters = {
90
+ number: number,
91
+ }
92
+
85
93
  export type onDisconnectParameters = {
86
94
  event: CloseEvent,
87
95
  }
@@ -103,129 +111,3 @@ export type onStatelessParameters = {
103
111
  }
104
112
 
105
113
  export type StatesArray = { clientId: number, [key: string | number]: any }[]
106
-
107
- // hocuspocus-pro types
108
-
109
- export type TCollabThread<Data = any, CommentData = any> = {
110
- id: string;
111
- createdAt: number;
112
- updatedAt: number;
113
- deletedAt: number | null;
114
- resolvedAt?: string; // (new Date()).toISOString()
115
- comments: TCollabComment<CommentData>[];
116
- deletedComments: TCollabComment<CommentData>[];
117
- data: Data
118
- }
119
-
120
- export type TCollabComment<Data = any> = {
121
- id: string;
122
- createdAt: string;
123
- updatedAt: string;
124
- deletedAt?: string;
125
- data: Data
126
- content: any
127
- }
128
-
129
- export type THistoryVersion = {
130
- name?: string;
131
- version: number;
132
- date: number;
133
- };
134
-
135
- export type THistoryConfiguration = {
136
- autoVersioning: boolean;
137
- currentVersion: number;
138
- stateCaptured: number; // indicates whether changes have been made since the last version
139
- };
140
-
141
- export type THistoryAction =
142
- | THistoryDocumentRevertAction
143
- | THistoryVersionCreateAction
144
- | THistoryVersionPreviewAction;
145
-
146
- export type THistoryDocumentRevertAction = {
147
- action: 'document.revert';
148
- /**
149
- * if changes haven't been persisted to a version yet, we'll create one with the specified name,
150
- * expect when `false` is passed.
151
- */
152
- currentVersionName?: string | false;
153
- /**
154
- * Name of the version that is created after the revert. Pass `false` to avoid generating a new version.
155
- */
156
- newVersionName?: string | false;
157
- };
158
-
159
- export type THistoryVersionCreateAction = {
160
- action: 'version.create';
161
- name?: string;
162
- };
163
-
164
- export type THistoryVersionPreviewAction = {
165
- action: 'version.preview';
166
- version: number;
167
- };
168
-
169
- export type THistoryEvent =
170
- | THistoryVersionPreviewEvent
171
- | THistoryVersionCreatedEvent
172
- | THistoryDocumentRevertedEvent;
173
-
174
- export type THistoryVersionCreatedEvent = {
175
- event: 'version.created';
176
- version: number;
177
- };
178
-
179
- export type THistoryVersionPreviewEvent = {
180
- event: 'version.preview';
181
- version: number;
182
- ydoc: string; // base64-encoded Uint8Array
183
- };
184
-
185
- export type THistoryDocumentRevertedEvent = {
186
- event: 'document.reverted';
187
- version: number;
188
- };
189
-
190
- export type DeleteCommentOptions = {
191
- /**
192
- * If `true`, the thread will also be deleted if the deleted comment was the first comment in the thread.
193
- */
194
- deleteThread?: boolean
195
-
196
- /**
197
- * If `true`, will remove the content of the deleted comment
198
- */
199
- deleteContent?: boolean
200
- }
201
-
202
- export type DeleteThreadOptions = {
203
- /**
204
- * If `true`, will remove the comments on the thread,
205
- * otherwise will only mark the thread as deleted
206
- * and keep the comments
207
- * @default false
208
- */
209
- deleteComments?: boolean
210
-
211
- /**
212
- * If `true`, will forcefully remove the thread and all comments,
213
- * otherwise will only mark the thread as deleted
214
- * and keep the comments
215
- * @default false
216
- */
217
- force?: boolean,
218
- }
219
-
220
- /**
221
- * The type of thread
222
- */
223
- export type ThreadType = 'archived' | 'unarchived'
224
-
225
- export type GetThreadsOptions = {
226
- /**
227
- * The types of threads to get
228
- * @default ['unarchived']
229
- */
230
- types?: Array<ThreadType>
231
- }