@hocuspocus/provider 1.0.0-alpha.32 → 1.0.0-alpha.33

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.
@@ -1,3 +1,4 @@
1
1
  export * from './auth';
2
2
  export * from './CloseEvents';
3
3
  export * from './awarenessStatesToArray';
4
+ export * from './types';
@@ -0,0 +1,10 @@
1
+ /**
2
+ * State of the WebSocket connection.
3
+ * https://developer.mozilla.org/de/docs/Web/API/WebSocket/readyState
4
+ */
5
+ export declare enum WsReadyStates {
6
+ Connecting = 0,
7
+ Open = 1,
8
+ Closing = 2,
9
+ Closed = 3
10
+ }
@@ -1,5 +1,5 @@
1
1
  /// <reference types="node" />
2
- import { Extension, onChangePayload, onConfigurePayload, onConnectPayload, onLoadDocumentPayload, onDisconnectPayload, onRequestPayload, onUpgradePayload } from '@hocuspocus/server';
2
+ import { Extension, onChangePayload, onConfigurePayload, onLoadDocumentPayload, onDisconnectPayload, onRequestPayload, onUpgradePayload, connectedPayload } from '@hocuspocus/server';
3
3
  import { IncomingMessage, ServerResponse } from 'http';
4
4
  import WebSocket from 'ws';
5
5
  import { Storage } from './Storage';
@@ -30,7 +30,7 @@ export declare class Monitor implements Extension {
30
30
  handleConnection(websocket: WebSocket, request: IncomingMessage): void | undefined;
31
31
  onRequest({ request, response }: onRequestPayload): Promise<void>;
32
32
  onUpgrade({ request, socket, head }: onUpgradePayload): Promise<void>;
33
- onConnect(data: onConnectPayload): Promise<void>;
33
+ connected(data: connectedPayload): Promise<void>;
34
34
  onDisconnect(data: onDisconnectPayload): Promise<void>;
35
35
  onLoadDocument(data: onLoadDocumentPayload): Promise<void>;
36
36
  onChange(data: onChangePayload): Promise<void>;
@@ -133,6 +133,7 @@ export declare class HocuspocusProvider extends EventEmitter {
133
133
  connect(): Promise<void>;
134
134
  createWebSocketConnection(): Promise<unknown>;
135
135
  resolveConnectionAttempt(): void;
136
+ stopConnectionAttempt(): void;
136
137
  rejectConnectionAttempt(): void;
137
138
  get document(): Y.Doc;
138
139
  get awareness(): Awareness;
@@ -149,9 +150,8 @@ export declare class HocuspocusProvider extends EventEmitter {
149
150
  set synced(state: boolean);
150
151
  get isAuthenticationRequired(): boolean;
151
152
  disconnect(): void;
152
- onOpen(event: Event): void;
153
+ onOpen(event: Event): Promise<void>;
153
154
  getToken(): Promise<string | null>;
154
- webSocketConnectionEstablished(): Promise<void>;
155
155
  startSync(): void;
156
156
  send(Message: ConstructableOutgoingMessage, args: any, broadcast?: boolean): void;
157
157
  onMessage(event: MessageEvent): void;
@@ -12,16 +12,6 @@ export declare enum MessageType {
12
12
  Auth = 2,
13
13
  QueryAwareness = 3
14
14
  }
15
- /**
16
- * State of the WebSocket connection.
17
- * https://developer.mozilla.org/de/docs/Web/API/WebSocket/readyState
18
- */
19
- export declare enum WsReadyStates {
20
- Connecting = 0,
21
- Open = 1,
22
- Closing = 2,
23
- Closed = 3
24
- }
25
15
  export interface AwarenessUpdate {
26
16
  added: Array<any>;
27
17
  updated: Array<any>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hocuspocus/provider",
3
- "version": "1.0.0-alpha.32",
3
+ "version": "1.0.0-alpha.33",
4
4
  "description": "hocuspocus provider",
5
5
  "homepage": "https://hocuspocus.dev",
6
6
  "keywords": [
@@ -28,11 +28,11 @@
28
28
  "dist"
29
29
  ],
30
30
  "dependencies": {
31
- "@hocuspocus/common": "^1.0.0-alpha.7",
31
+ "@hocuspocus/common": "^1.0.0-alpha.8",
32
32
  "@lifeomic/attempt": "^3.0.2",
33
33
  "lib0": "^0.2.43",
34
34
  "y-protocols": "^1.0.5",
35
35
  "yjs": "^13.5.24"
36
36
  },
37
- "gitHead": "41c2ca3452f17e7dab914c633cde04ae68f45929"
37
+ "gitHead": "771a8cbad1016dd12080ff4b35fabb9680ec69a7"
38
38
  }
@@ -6,7 +6,9 @@ import * as mutex from 'lib0/mutex'
6
6
  import * as url from 'lib0/url'
7
7
  import type { Event, CloseEvent, MessageEvent } from 'ws'
8
8
  import { retry } from '@lifeomic/attempt'
9
- import { awarenessStatesToArray, Forbidden, Unauthorized } from '@hocuspocus/common'
9
+ import {
10
+ awarenessStatesToArray, Forbidden, Unauthorized, WsReadyStates,
11
+ } from '@hocuspocus/common'
10
12
  import EventEmitter from './EventEmitter'
11
13
  import { IncomingMessage } from './IncomingMessage'
12
14
  import { MessageReceiver } from './MessageReceiver'
@@ -324,6 +326,14 @@ export class HocuspocusProvider extends EventEmitter {
324
326
  resolveConnectionAttempt() {
325
327
  this.connectionAttempt?.resolve()
326
328
  this.connectionAttempt = null
329
+
330
+ this.status = WebSocketStatus.Connected
331
+ this.emit('status', { status: 'connected' })
332
+ this.emit('connect')
333
+ }
334
+
335
+ stopConnectionAttempt() {
336
+ this.connectionAttempt = null
327
337
  }
328
338
 
329
339
  rejectConnectionAttempt() {
@@ -457,12 +467,17 @@ export class HocuspocusProvider extends EventEmitter {
457
467
  }
458
468
  }
459
469
 
460
- onOpen(event: Event) {
470
+ async onOpen(event: Event) {
461
471
  this.emit('open', { event })
462
472
 
463
- if (this.status !== WebSocketStatus.Connected) {
464
- this.webSocketConnectionEstablished()
473
+ if (this.isAuthenticationRequired) {
474
+ this.send(AuthenticationMessage, {
475
+ token: await this.getToken(),
476
+ })
477
+ return
465
478
  }
479
+
480
+ this.startSync()
466
481
  }
467
482
 
468
483
  async getToken() {
@@ -474,21 +489,6 @@ export class HocuspocusProvider extends EventEmitter {
474
489
  return this.configuration.token
475
490
  }
476
491
 
477
- async webSocketConnectionEstablished() {
478
- this.status = WebSocketStatus.Connected
479
- this.emit('status', { status: 'connected' })
480
- this.emit('connect')
481
-
482
- if (this.isAuthenticationRequired) {
483
- this.send(AuthenticationMessage, {
484
- token: await this.getToken(),
485
- })
486
- return
487
- }
488
-
489
- this.startSync()
490
- }
491
-
492
492
  startSync() {
493
493
  this.send(SyncStepOneMessage, { document: this.document })
494
494
 
@@ -505,7 +505,7 @@ export class HocuspocusProvider extends EventEmitter {
505
505
  this.mux(() => { this.broadcast(Message, args) })
506
506
  }
507
507
 
508
- if (this.status === WebSocketStatus.Connected) {
508
+ if (this.webSocket?.readyState === WsReadyStates.Open) {
509
509
  const messageSender = new MessageSender(Message, args)
510
510
 
511
511
  this.emit('outgoingMessage', { message: messageSender.message })
@@ -594,10 +594,10 @@ export class HocuspocusProvider extends EventEmitter {
594
594
 
595
595
  removeAwarenessStates(this.awareness, [this.document.clientID], 'provider destroy')
596
596
 
597
- // If there is still a connection attempt outstanding then we should resolve
597
+ // If there is still a connection attempt outstanding then we should stop
598
598
  // it before calling disconnect, otherwise it will be rejected in the onClose
599
599
  // handler and trigger a retry
600
- this.resolveConnectionAttempt()
600
+ this.stopConnectionAttempt()
601
601
 
602
602
  this.disconnect()
603
603