@hocuspocus/provider 1.0.0-alpha.30 → 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.
- package/dist/hocuspocus-provider.cjs +19 -30
- package/dist/hocuspocus-provider.cjs.map +1 -1
- package/dist/hocuspocus-provider.esm.js +19 -29
- package/dist/hocuspocus-provider.esm.js.map +1 -1
- package/dist/packages/common/src/awarenessStatesToArray.d.ts +3 -0
- package/dist/packages/common/src/index.d.ts +2 -0
- package/dist/packages/common/src/types.d.ts +10 -0
- package/dist/packages/extension-monitor/src/index.d.ts +2 -2
- package/dist/packages/extension-throttle/src/index.d.ts +6 -2
- package/dist/packages/provider/src/HocuspocusProvider.d.ts +2 -2
- package/dist/packages/provider/src/index.d.ts +0 -1
- package/dist/packages/server/src/MessageReceiver.d.ts +1 -1
- package/dist/packages/server/src/types.d.ts +11 -11
- package/dist/tests/extension-throttle/configuration.d.ts +1 -0
- package/dist/tests/provider/observe.d.ts +1 -0
- package/dist/tests/provider/observeDeep.d.ts +1 -0
- package/package.json +3 -3
- package/src/HocuspocusProvider.ts +23 -24
- package/src/index.ts +0 -1
- package/dist/packages/provider/src/utils/awarenessStatesToArray.d.ts +0 -4
- package/dist/packages/provider/src/utils/index.d.ts +0 -1
- package/src/utils/awarenessStatesToArray.ts +0 -8
- package/src/utils/index.ts +0 -1
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
-
import { Extension, onChangePayload, onConfigurePayload,
|
|
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
|
-
|
|
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>;
|
|
@@ -1,12 +1,16 @@
|
|
|
1
1
|
import { Extension, onConnectPayload } from '@hocuspocus/server';
|
|
2
|
-
export interface
|
|
2
|
+
export interface ThrottleConfiguration {
|
|
3
3
|
throttle: number | null | false;
|
|
4
4
|
banTime: number;
|
|
5
5
|
}
|
|
6
6
|
export declare class Throttle implements Extension {
|
|
7
|
-
configuration:
|
|
7
|
+
configuration: ThrottleConfiguration;
|
|
8
8
|
connectionsByIp: Map<string, Array<number>>;
|
|
9
9
|
bannedIps: Map<string, number>;
|
|
10
|
+
/**
|
|
11
|
+
* Constructor
|
|
12
|
+
*/
|
|
13
|
+
constructor(configuration?: Partial<ThrottleConfiguration>);
|
|
10
14
|
/**
|
|
11
15
|
* Throttle requests
|
|
12
16
|
* @private
|
|
@@ -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;
|
|
@@ -8,6 +8,6 @@ export declare class MessageReceiver {
|
|
|
8
8
|
logger: Debugger;
|
|
9
9
|
constructor(message: IncomingMessage, logger: Debugger);
|
|
10
10
|
apply(document: Document, connection?: Connection, reply?: (message: Uint8Array) => void): void;
|
|
11
|
-
readSyncMessage(message: IncomingMessage, document: Document, connection?: Connection, reply?: (message: Uint8Array) => void): 0 |
|
|
11
|
+
readSyncMessage(message: IncomingMessage, document: Document, connection?: Connection, reply?: (message: Uint8Array) => void): 0 | 2 | 1;
|
|
12
12
|
applyQueryAwarenessMessage(awareness: Awareness, reply?: (message: Uint8Array) => void): void;
|
|
13
13
|
}
|
|
@@ -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>;
|
|
@@ -38,6 +28,7 @@ export interface Extension {
|
|
|
38
28
|
onListen?(data: onListenPayload): Promise<any>;
|
|
39
29
|
onUpgrade?(data: onUpgradePayload): Promise<any>;
|
|
40
30
|
onConnect?(data: onConnectPayload): Promise<any>;
|
|
31
|
+
connected?(data: connectedPayload): Promise<any>;
|
|
41
32
|
onAuthenticate?(data: onAuthenticatePayload): Promise<any>;
|
|
42
33
|
/**
|
|
43
34
|
* @deprecated onCreateDocument is deprecated, use onLoadDocument instead
|
|
@@ -53,7 +44,7 @@ export interface Extension {
|
|
|
53
44
|
onDisconnect?(data: onDisconnectPayload): Promise<any>;
|
|
54
45
|
onDestroy?(data: onDestroyPayload): Promise<any>;
|
|
55
46
|
}
|
|
56
|
-
export declare type Hook = 'onConfigure' | 'onListen' | 'onUpgrade' | 'onConnect' | 'onAuthenticate' |
|
|
47
|
+
export declare type Hook = 'onConfigure' | 'onListen' | 'onUpgrade' | 'onConnect' | 'connected' | 'onAuthenticate' |
|
|
57
48
|
/**
|
|
58
49
|
* @deprecated onCreateDocument is deprecated, use onLoadDocument instead
|
|
59
50
|
*/
|
|
@@ -116,6 +107,15 @@ export interface onConnectPayload {
|
|
|
116
107
|
socketId: string;
|
|
117
108
|
connection: ConnectionConfiguration;
|
|
118
109
|
}
|
|
110
|
+
export interface connectedPayload {
|
|
111
|
+
documentName: string;
|
|
112
|
+
instance: Hocuspocus;
|
|
113
|
+
request: IncomingMessage;
|
|
114
|
+
requestHeaders: IncomingHttpHeaders;
|
|
115
|
+
requestParameters: URLSearchParams;
|
|
116
|
+
socketId: string;
|
|
117
|
+
connection: ConnectionConfiguration;
|
|
118
|
+
}
|
|
119
119
|
export interface onLoadDocumentPayload {
|
|
120
120
|
context: any;
|
|
121
121
|
document: Document;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hocuspocus/provider",
|
|
3
|
-
"version": "1.0.0-alpha.
|
|
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.
|
|
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": "
|
|
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 {
|
|
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'
|
|
@@ -18,8 +20,7 @@ import { AuthenticationMessage } from './OutgoingMessages/AuthenticationMessage'
|
|
|
18
20
|
import { AwarenessMessage } from './OutgoingMessages/AwarenessMessage'
|
|
19
21
|
import { UpdateMessage } from './OutgoingMessages/UpdateMessage'
|
|
20
22
|
import { OutgoingMessage } from './OutgoingMessage'
|
|
21
|
-
import
|
|
22
|
-
import { ConstructableOutgoingMessage, StatesArray } from './types'
|
|
23
|
+
import { ConstructableOutgoingMessage } from './types'
|
|
23
24
|
import { onAwarenessChangeParameters, onAwarenessUpdateParameters } from '.'
|
|
24
25
|
|
|
25
26
|
export enum WebSocketStatus {
|
|
@@ -325,6 +326,14 @@ export class HocuspocusProvider extends EventEmitter {
|
|
|
325
326
|
resolveConnectionAttempt() {
|
|
326
327
|
this.connectionAttempt?.resolve()
|
|
327
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
|
|
328
337
|
}
|
|
329
338
|
|
|
330
339
|
rejectConnectionAttempt() {
|
|
@@ -458,12 +467,17 @@ export class HocuspocusProvider extends EventEmitter {
|
|
|
458
467
|
}
|
|
459
468
|
}
|
|
460
469
|
|
|
461
|
-
onOpen(event: Event) {
|
|
470
|
+
async onOpen(event: Event) {
|
|
462
471
|
this.emit('open', { event })
|
|
463
472
|
|
|
464
|
-
if (this.
|
|
465
|
-
this.
|
|
473
|
+
if (this.isAuthenticationRequired) {
|
|
474
|
+
this.send(AuthenticationMessage, {
|
|
475
|
+
token: await this.getToken(),
|
|
476
|
+
})
|
|
477
|
+
return
|
|
466
478
|
}
|
|
479
|
+
|
|
480
|
+
this.startSync()
|
|
467
481
|
}
|
|
468
482
|
|
|
469
483
|
async getToken() {
|
|
@@ -475,21 +489,6 @@ export class HocuspocusProvider extends EventEmitter {
|
|
|
475
489
|
return this.configuration.token
|
|
476
490
|
}
|
|
477
491
|
|
|
478
|
-
async webSocketConnectionEstablished() {
|
|
479
|
-
this.status = WebSocketStatus.Connected
|
|
480
|
-
this.emit('status', { status: 'connected' })
|
|
481
|
-
this.emit('connect')
|
|
482
|
-
|
|
483
|
-
if (this.isAuthenticationRequired) {
|
|
484
|
-
this.send(AuthenticationMessage, {
|
|
485
|
-
token: await this.getToken(),
|
|
486
|
-
})
|
|
487
|
-
return
|
|
488
|
-
}
|
|
489
|
-
|
|
490
|
-
this.startSync()
|
|
491
|
-
}
|
|
492
|
-
|
|
493
492
|
startSync() {
|
|
494
493
|
this.send(SyncStepOneMessage, { document: this.document })
|
|
495
494
|
|
|
@@ -506,7 +505,7 @@ export class HocuspocusProvider extends EventEmitter {
|
|
|
506
505
|
this.mux(() => { this.broadcast(Message, args) })
|
|
507
506
|
}
|
|
508
507
|
|
|
509
|
-
if (this.
|
|
508
|
+
if (this.webSocket?.readyState === WsReadyStates.Open) {
|
|
510
509
|
const messageSender = new MessageSender(Message, args)
|
|
511
510
|
|
|
512
511
|
this.emit('outgoingMessage', { message: messageSender.message })
|
|
@@ -595,10 +594,10 @@ export class HocuspocusProvider extends EventEmitter {
|
|
|
595
594
|
|
|
596
595
|
removeAwarenessStates(this.awareness, [this.document.clientID], 'provider destroy')
|
|
597
596
|
|
|
598
|
-
// If there is still a connection attempt outstanding then we should
|
|
597
|
+
// If there is still a connection attempt outstanding then we should stop
|
|
599
598
|
// it before calling disconnect, otherwise it will be rejected in the onClose
|
|
600
599
|
// handler and trigger a retry
|
|
601
|
-
this.
|
|
600
|
+
this.stopConnectionAttempt()
|
|
602
601
|
|
|
603
602
|
this.disconnect()
|
|
604
603
|
|
package/src/index.ts
CHANGED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { default as awarenessStatesToArray } from './awarenessStatesToArray';
|
package/src/utils/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { default as awarenessStatesToArray } from './awarenessStatesToArray'
|