@hocuspocus/provider 2.0.0-alpha.0 → 2.0.0-alpha.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.
@@ -87,6 +87,7 @@ export declare class HocuspocusProvider extends EventEmitter {
87
87
  set synced(state: boolean);
88
88
  receiveStateless(payload: string): void;
89
89
  get isAuthenticationRequired(): boolean;
90
+ connect(): Promise<unknown>;
90
91
  disconnect(): void;
91
92
  onOpen(event: Event): Promise<void>;
92
93
  getToken(): Promise<string | null>;
@@ -0,0 +1,8 @@
1
+ import * as encoding from 'lib0/encoding';
2
+ import { MessageType, OutgoingMessageArguments } from '../types';
3
+ import { OutgoingMessage } from '../OutgoingMessage';
4
+ export declare class CloseMessage extends OutgoingMessage {
5
+ type: MessageType;
6
+ description: string;
7
+ get(args: Partial<OutgoingMessageArguments>): encoding.Encoder;
8
+ }
@@ -15,7 +15,8 @@ export declare enum MessageType {
15
15
  Awareness = 1,
16
16
  Auth = 2,
17
17
  QueryAwareness = 3,
18
- Stateless = 5
18
+ Stateless = 5,
19
+ CLOSE = 7
19
20
  }
20
21
  export declare enum WebSocketStatus {
21
22
  Connecting = "connecting",
@@ -23,10 +23,14 @@ export declare class Connection {
23
23
  * Constructor.
24
24
  */
25
25
  constructor(connection: WebSocket, request: HTTPIncomingMessage, document: Document, timeout: number, socketId: string, context: any, readOnly: boolean | undefined, logger: Debugger);
26
+ boundClose: (event?: CloseEvent | undefined) => void;
27
+ boundHandleMessage: (data: Uint8Array) => void;
28
+ boundHandlePong: () => void;
29
+ handlePong(): void;
26
30
  /**
27
31
  * Set a callback that will be triggered when the connection is closed
28
32
  */
29
- onClose(callback: (document: Document) => void): Connection;
33
+ onClose(callback: (document: Document, event?: CloseEvent) => void): Connection;
30
34
  /**
31
35
  * Set a callback that will be triggered when an stateless message is received
32
36
  */
@@ -13,7 +13,8 @@ export declare enum MessageType {
13
13
  QueryAwareness = 3,
14
14
  SyncReply = 4,
15
15
  Stateless = 5,
16
- BroadcastStateless = 6
16
+ BroadcastStateless = 6,
17
+ CLOSE = 7
17
18
  }
18
19
  export interface AwarenessUpdate {
19
20
  added: Array<any>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hocuspocus/provider",
3
- "version": "2.0.0-alpha.0",
3
+ "version": "2.0.0-alpha.1",
4
4
  "description": "hocuspocus provider",
5
5
  "homepage": "https://hocuspocus.dev",
6
6
  "keywords": [
@@ -28,9 +28,10 @@
28
28
  "dist"
29
29
  ],
30
30
  "dependencies": {
31
- "@hocuspocus/common": "^2.0.0-alpha.0",
31
+ "@hocuspocus/common": "^2.0.0-alpha.1",
32
32
  "@lifeomic/attempt": "^3.0.2",
33
- "lib0": "^0.2.47"
33
+ "lib0": "^0.2.47",
34
+ "ws": "^7.5.9"
34
35
  },
35
36
  "peerDependencies": {
36
37
  "y-protocols": "^1.0.5",
@@ -28,6 +28,7 @@ import {
28
28
  } from './types'
29
29
  import { HocuspocusProviderWebsocket } from './HocuspocusProviderWebsocket'
30
30
  import { StatelessMessage } from './OutgoingMessages/StatelessMessage'
31
+ import { CloseMessage } from './OutgoingMessages/CloseMessage'
31
32
  import { onAwarenessChangeParameters, onAwarenessUpdateParameters } from '.'
32
33
 
33
34
  export type HocuspocusProviderConfiguration =
@@ -281,6 +282,11 @@ export class HocuspocusProvider extends EventEmitter {
281
282
  return !!this.configuration.token && !this.isAuthenticated
282
283
  }
283
284
 
285
+ // not needed, but provides backward compatibility with e.g. lexicla/yjs
286
+ async connect() {
287
+ return this.configuration.websocketProvider.connect()
288
+ }
289
+
284
290
  disconnect() {
285
291
  this.disconnectBroadcastChannel()
286
292
  this.configuration.websocketProvider.detach(this)
@@ -294,7 +300,6 @@ export class HocuspocusProvider extends EventEmitter {
294
300
  token: await this.getToken(),
295
301
  documentName: this.configuration.name,
296
302
  })
297
- return
298
303
  }
299
304
 
300
305
  this.startSync()
@@ -376,6 +381,8 @@ export class HocuspocusProvider extends EventEmitter {
376
381
 
377
382
  this.removeAllListeners()
378
383
 
384
+ this.send(CloseMessage, { documentName: this.configuration.name })
385
+
379
386
  if (typeof window === 'undefined') {
380
387
  return
381
388
  }
@@ -0,0 +1,16 @@
1
+ import * as encoding from 'lib0/encoding'
2
+ import { MessageType, OutgoingMessageArguments } from '../types'
3
+ import { OutgoingMessage } from '../OutgoingMessage'
4
+
5
+ export class CloseMessage extends OutgoingMessage {
6
+ type = MessageType.CLOSE
7
+
8
+ description = 'Ask the server to close the connection'
9
+
10
+ get(args: Partial<OutgoingMessageArguments>) {
11
+ encoding.writeVarString(this.encoder, args.documentName!)
12
+ encoding.writeVarUint(this.encoder, this.type)
13
+
14
+ return this.encoder
15
+ }
16
+ }
package/src/types.ts CHANGED
@@ -17,6 +17,7 @@ export enum MessageType {
17
17
  Auth = 2,
18
18
  QueryAwareness = 3,
19
19
  Stateless = 5,
20
+ CLOSE = 7,
20
21
  }
21
22
 
22
23
  export enum WebSocketStatus {