@hocuspocus/provider 2.6.1 → 2.7.1

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.
@@ -8,7 +8,7 @@ export interface DatabaseConfiguration {
8
8
  /**
9
9
  * Pass a function to store updates in your database.
10
10
  */
11
- store: (data: storePayload) => void;
11
+ store: (data: storePayload) => Promise<void>;
12
12
  }
13
13
  export declare class Database implements Extension {
14
14
  /**
@@ -91,7 +91,6 @@ export declare class HocuspocusProvider extends EventEmitter {
91
91
  boundBroadcastChannelSubscriber: (data: ArrayBuffer) => void;
92
92
  boundPageUnload: () => void;
93
93
  boundOnOpen: (event: Event) => Promise<void>;
94
- boundOnMessage: (event: MessageEvent) => void;
95
94
  boundOnClose: (event: CloseEvent) => void;
96
95
  boundOnStatus: ({ status }: onStatusParameters) => void;
97
96
  forwardConnect: (e: any) => this;
@@ -77,6 +77,10 @@ export interface CompleteHocuspocusProviderWebsocketConfiguration {
77
77
  * Don’t output any warnings.
78
78
  */
79
79
  quiet: boolean;
80
+ /**
81
+ * Map of attached providers keyed by documentName.
82
+ */
83
+ providerMap: Map<string, HocuspocusProvider>;
80
84
  }
81
85
  export declare class HocuspocusProviderWebsocket extends EventEmitter {
82
86
  private messageQueue;
@@ -6,6 +6,7 @@ export declare class IncomingMessage {
6
6
  encoder: Encoder;
7
7
  decoder: Decoder;
8
8
  constructor(data: any);
9
+ peekVarString(): string;
9
10
  readVarUint(): MessageType;
10
11
  readVarString(): string;
11
12
  readVarUint8Array(): Uint8Array;
@@ -10,5 +10,5 @@ export declare class DirectConnection implements DirectConnectionInterface {
10
10
  */
11
11
  constructor(document: Document, instance: Hocuspocus, context?: any);
12
12
  transact(transaction: (document: Document) => void, transactionOrigin?: any): Promise<void>;
13
- disconnect(): void;
13
+ disconnect(): Promise<void>;
14
14
  }
@@ -19,6 +19,7 @@ export declare class Document extends Doc {
19
19
  mux: mutex;
20
20
  logger: Debugger;
21
21
  isLoading: boolean;
22
+ isDestroyed: boolean;
22
23
  /**
23
24
  * Constructor.
24
25
  */
@@ -87,5 +88,6 @@ export declare class Document extends Doc {
87
88
  * Broadcast stateless message to all connections
88
89
  */
89
90
  broadcastStateless(payload: string): void;
91
+ destroy(): void;
90
92
  }
91
93
  export default Document;
@@ -82,7 +82,7 @@ export declare class Hocuspocus {
82
82
  * Create a new document by the given request
83
83
  */
84
84
  createDocument(documentName: string, request: Partial<Pick<IncomingMessage, 'headers' | 'url'>>, socketId: string, connection: ConnectionConfiguration, context?: any): Promise<Document>;
85
- storeDocumentHooks(document: Document, hookPayload: onStoreDocumentPayload): void;
85
+ storeDocumentHooks(document: Document, hookPayload: onStoreDocumentPayload): Promise<void>;
86
86
  /**
87
87
  * Run the given hook on all configured extensions.
88
88
  * Runs the given callback after each hook.
@@ -131,6 +131,7 @@ export interface onStatelessPayload {
131
131
  payload: string;
132
132
  }
133
133
  export interface onAuthenticatePayload {
134
+ context: any;
134
135
  documentName: string;
135
136
  instance: Hocuspocus;
136
137
  requestHeaders: IncomingHttpHeaders;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hocuspocus/provider",
3
- "version": "2.6.1",
3
+ "version": "2.7.1",
4
4
  "description": "hocuspocus provider",
5
5
  "homepage": "https://hocuspocus.dev",
6
6
  "keywords": [
@@ -29,10 +29,10 @@
29
29
  "dist"
30
30
  ],
31
31
  "dependencies": {
32
- "@hocuspocus/common": "^2.6.1",
32
+ "@hocuspocus/common": "^2.7.1",
33
33
  "@lifeomic/attempt": "^3.0.2",
34
34
  "lib0": "^0.2.47",
35
- "ws": "^7.5.9"
35
+ "ws": "^8.14.2"
36
36
  },
37
37
  "peerDependencies": {
38
38
  "y-protocols": "^1.0.5",
@@ -192,8 +192,6 @@ export class HocuspocusProvider extends EventEmitter {
192
192
  this.configuration.websocketProvider.on('open', this.boundOnOpen)
193
193
  this.configuration.websocketProvider.on('open', this.forwardOpen)
194
194
 
195
- this.configuration.websocketProvider.on('message', this.boundOnMessage)
196
-
197
195
  this.configuration.websocketProvider.on('close', this.boundOnClose)
198
196
  this.configuration.websocketProvider.on('close', this.configuration.onClose)
199
197
  this.configuration.websocketProvider.on('close', this.forwardClose)
@@ -234,8 +232,6 @@ export class HocuspocusProvider extends EventEmitter {
234
232
 
235
233
  boundOnOpen = this.onOpen.bind(this)
236
234
 
237
- boundOnMessage = this.onMessage.bind(this)
238
-
239
235
  boundOnClose = this.onClose.bind(this)
240
236
 
241
237
  boundOnStatus = this.onStatus.bind(this)
@@ -387,9 +383,17 @@ export class HocuspocusProvider extends EventEmitter {
387
383
 
388
384
  this.emit('open', { event })
389
385
 
386
+ let token: string | null
387
+ try {
388
+ token = await this.getToken()
389
+ } catch (error) {
390
+ this.permissionDeniedHandler(`Failed to get token: ${error}`)
391
+ return
392
+ }
393
+
390
394
  if (this.isAuthenticationRequired) {
391
395
  this.send(AuthenticationMessage, {
392
- token: await this.getToken(),
396
+ token,
393
397
  documentName: this.configuration.name,
394
398
  })
395
399
  }
@@ -439,10 +443,6 @@ export class HocuspocusProvider extends EventEmitter {
439
443
 
440
444
  const documentName = message.readVarString()
441
445
 
442
- if (documentName !== this.configuration.name) {
443
- return // message is meant for another provider
444
- }
445
-
446
446
  message.writeVarString(documentName)
447
447
 
448
448
  this.emit('message', { event, message: new IncomingMessage(event.data) })
@@ -486,7 +486,6 @@ export class HocuspocusProvider extends EventEmitter {
486
486
  this.configuration.websocketProvider.off('connect', this.forwardConnect)
487
487
  this.configuration.websocketProvider.off('open', this.boundOnOpen)
488
488
  this.configuration.websocketProvider.off('open', this.forwardOpen)
489
- this.configuration.websocketProvider.off('message', this.boundOnMessage)
490
489
  this.configuration.websocketProvider.off('close', this.boundOnClose)
491
490
  this.configuration.websocketProvider.off('close', this.configuration.onClose)
492
491
  this.configuration.websocketProvider.off('close', this.forwardClose)
@@ -14,6 +14,7 @@ import {
14
14
  onAwarenessChangeParameters, onAwarenessUpdateParameters,
15
15
  onCloseParameters, onDisconnectParameters, onMessageParameters, onOpenParameters, onOutgoingMessageParameters, onStatusParameters,
16
16
  } from './types.js'
17
+ import { IncomingMessage } from './IncomingMessage.js'
17
18
 
18
19
  export type HocusPocusWebSocket = WebSocket & { identifier: string };
19
20
 
@@ -91,6 +92,11 @@ export interface CompleteHocuspocusProviderWebsocketConfiguration {
91
92
  * Don’t output any warnings.
92
93
  */
93
94
  quiet: boolean,
95
+
96
+ /**
97
+ * Map of attached providers keyed by documentName.
98
+ */
99
+ providerMap: Map<string, HocuspocusProvider>,
94
100
  }
95
101
 
96
102
  export class HocuspocusProviderWebsocket extends EventEmitter {
@@ -134,6 +140,7 @@ export class HocuspocusProviderWebsocket extends EventEmitter {
134
140
  onAwarenessUpdate: () => null,
135
141
  onAwarenessChange: () => null,
136
142
  quiet: false,
143
+ providerMap: new Map(),
137
144
  }
138
145
 
139
146
  subscribedToBroadcastChannel = false
@@ -215,6 +222,8 @@ export class HocuspocusProviderWebsocket extends EventEmitter {
215
222
  }
216
223
 
217
224
  attach(provider: HocuspocusProvider) {
225
+ this.configuration.providerMap.set(provider.configuration.name, provider)
226
+
218
227
  if (this.status === WebSocketStatus.Disconnected && this.shouldConnect) {
219
228
  this.connect()
220
229
  }
@@ -229,7 +238,7 @@ export class HocuspocusProviderWebsocket extends EventEmitter {
229
238
  }
230
239
 
231
240
  detach(provider: HocuspocusProvider) {
232
- // tell the server to remove the listener
241
+ this.configuration.providerMap.delete(provider.configuration.name)
233
242
  }
234
243
 
235
244
  public setConfiguration(
@@ -368,6 +377,11 @@ export class HocuspocusProviderWebsocket extends EventEmitter {
368
377
  this.resolveConnectionAttempt()
369
378
 
370
379
  this.lastMessageReceived = time.getUnixTime()
380
+
381
+ const message = new IncomingMessage(event.data)
382
+ const documentName = message.peekVarString()
383
+
384
+ this.configuration.providerMap.get(documentName)?.onMessage(event)
371
385
  }
372
386
 
373
387
  resolveConnectionAttempt() {
@@ -1,5 +1,6 @@
1
1
  import {
2
2
  createDecoder,
3
+ peekVarString,
3
4
  readVarUint,
4
5
  readVarUint8Array,
5
6
  readVarString,
@@ -29,6 +30,10 @@ export class IncomingMessage {
29
30
  this.decoder = createDecoder(new Uint8Array(this.data))
30
31
  }
31
32
 
33
+ peekVarString(): string {
34
+ return peekVarString(this.decoder)
35
+ }
36
+
32
37
  readVarUint(): MessageType {
33
38
  return readVarUint(this.decoder)
34
39
  }
@@ -9,9 +9,6 @@ export class QueryAwarenessMessage extends OutgoingMessage {
9
9
 
10
10
  get(args: Partial<OutgoingMessageArguments>) {
11
11
 
12
- console.log('queryAwareness: writing string docName', args.documentName)
13
- console.log(this.encoder.cpos)
14
-
15
12
  encoding.writeVarString(this.encoder, args.documentName!)
16
13
  encoding.writeVarUint(this.encoder, this.type)
17
14