@hocuspocus/provider 1.0.0-alpha.4 → 1.0.0-alpha.40

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.
Files changed (132) hide show
  1. package/dist/{hocuspocus-provider.js → hocuspocus-provider.cjs} +362 -186
  2. package/dist/hocuspocus-provider.cjs.map +1 -0
  3. package/dist/hocuspocus-provider.esm.js +359 -181
  4. package/dist/hocuspocus-provider.esm.js.map +1 -1
  5. package/dist/packages/common/src/CloseEvents.d.ts +23 -0
  6. package/dist/packages/common/src/auth.d.ts +6 -0
  7. package/dist/packages/common/src/awarenessStatesToArray.d.ts +3 -0
  8. package/dist/packages/common/src/index.d.ts +4 -0
  9. package/dist/packages/common/src/types.d.ts +10 -0
  10. package/dist/packages/extension-database/src/Database.d.ts +30 -0
  11. package/dist/packages/extension-database/src/index.d.ts +1 -0
  12. package/dist/packages/extension-logger/src/Logger.d.ts +67 -0
  13. package/dist/packages/extension-logger/src/index.d.ts +1 -0
  14. package/dist/packages/{monitor → extension-monitor}/src/Collector.d.ts +4 -5
  15. package/dist/packages/{monitor → extension-monitor}/src/Dashboard.d.ts +2 -2
  16. package/dist/packages/{monitor → extension-monitor}/src/Storage.d.ts +0 -0
  17. package/dist/packages/{monitor → extension-monitor}/src/index.d.ts +3 -5
  18. package/dist/packages/extension-redis/src/Redis.d.ts +98 -0
  19. package/dist/packages/extension-redis/src/index.d.ts +1 -0
  20. package/dist/packages/extension-sqlite/src/SQLite.d.ts +26 -0
  21. package/dist/packages/extension-sqlite/src/index.d.ts +1 -0
  22. package/dist/packages/extension-throttle/src/index.d.ts +24 -0
  23. package/dist/packages/{webhook → extension-webhook}/src/index.d.ts +5 -11
  24. package/dist/packages/provider/src/EventEmitter.d.ts +1 -1
  25. package/dist/packages/provider/src/HocuspocusCloudProvider.d.ts +11 -0
  26. package/dist/packages/provider/src/HocuspocusProvider.d.ts +116 -32
  27. package/dist/packages/provider/src/IncomingMessage.d.ts +9 -6
  28. package/dist/packages/provider/src/MessageReceiver.d.ts +3 -2
  29. package/dist/packages/provider/src/MessageSender.d.ts +4 -9
  30. package/dist/packages/provider/src/OutgoingMessage.d.ts +5 -4
  31. package/dist/packages/provider/src/OutgoingMessages/AuthenticationMessage.d.ts +7 -0
  32. package/dist/packages/provider/src/OutgoingMessages/UpdateMessage.d.ts +1 -2
  33. package/dist/packages/provider/src/index.d.ts +1 -1
  34. package/dist/packages/provider/src/types.d.ts +54 -2
  35. package/dist/packages/server/src/Connection.d.ts +16 -7
  36. package/dist/packages/server/src/Debugger.d.ts +14 -0
  37. package/dist/packages/server/src/Document.d.ts +11 -5
  38. package/dist/packages/server/src/Hocuspocus.d.ts +63 -17
  39. package/dist/packages/server/src/IncomingMessage.d.ts +11 -7
  40. package/dist/packages/server/src/MessageReceiver.d.ts +13 -0
  41. package/dist/packages/server/src/OutgoingMessage.d.ts +6 -0
  42. package/dist/packages/server/src/index.d.ts +6 -0
  43. package/dist/packages/server/src/types.d.ts +168 -26
  44. package/dist/{demos/backend/src/create-document.d.ts → playground/backend/src/default.d.ts} +0 -0
  45. package/dist/{demos → playground}/backend/src/express.d.ts +0 -0
  46. package/dist/{demos → playground}/backend/src/koa.d.ts +0 -0
  47. package/dist/{demos/backend/src/minimal.d.ts → playground/backend/src/load-document.d.ts} +0 -0
  48. package/dist/{demos → playground}/backend/src/monitor.d.ts +0 -0
  49. package/dist/{demos/backend/src/webhook.d.ts → playground/backend/src/redis.d.ts} +0 -0
  50. package/dist/playground/backend/src/slow.d.ts +1 -0
  51. package/dist/playground/backend/src/webhook.d.ts +1 -0
  52. package/dist/tests/extension-database/fetch.d.ts +1 -0
  53. package/dist/tests/extension-logger/onListen.d.ts +1 -0
  54. package/dist/tests/extension-redis/closeConnections.d.ts +1 -0
  55. package/dist/tests/extension-redis/getConnectionCount.d.ts +1 -0
  56. package/dist/tests/extension-redis/getDocumentsCount.d.ts +1 -0
  57. package/dist/tests/extension-redis/onAwarenessChange.d.ts +1 -0
  58. package/dist/tests/extension-redis/onChange.d.ts +1 -0
  59. package/dist/tests/extension-redis/onStoreDocument.d.ts +1 -0
  60. package/dist/tests/extension-throttle/configuration.d.ts +1 -0
  61. package/dist/tests/provider/configuration.d.ts +1 -0
  62. package/dist/tests/provider/observe.d.ts +1 -0
  63. package/dist/tests/provider/observeDeep.d.ts +1 -0
  64. package/dist/tests/provider/onAuthenticated.d.ts +1 -0
  65. package/dist/tests/provider/onAuthenticationFailed.d.ts +1 -0
  66. package/dist/tests/provider/onAwarenessChange.d.ts +1 -0
  67. package/dist/tests/provider/onAwarenessUpdate.d.ts +1 -0
  68. package/dist/tests/provider/onClose.d.ts +1 -0
  69. package/dist/tests/provider/onConnect.d.ts +1 -0
  70. package/dist/tests/provider/onDisconnect.d.ts +1 -0
  71. package/dist/tests/provider/onMessage.d.ts +1 -0
  72. package/dist/tests/provider/onOpen.d.ts +1 -0
  73. package/dist/tests/provider/onSynced.d.ts +1 -0
  74. package/dist/tests/server/address.d.ts +1 -0
  75. package/dist/tests/server/afterStoreDocument.d.ts +1 -0
  76. package/dist/tests/server/closeConnections.d.ts +1 -0
  77. package/dist/tests/server/getConnectionsCount.d.ts +1 -0
  78. package/dist/tests/server/getDocumentName.d.ts +1 -0
  79. package/dist/tests/server/getDocumentsCount.d.ts +1 -0
  80. package/dist/tests/server/getMessageLogs.d.ts +1 -0
  81. package/dist/tests/server/listen.d.ts +1 -0
  82. package/dist/tests/server/onAuthenticate.d.ts +1 -0
  83. package/dist/tests/server/onAwarenessUpdate.d.ts +1 -0
  84. package/dist/tests/server/onChange.d.ts +1 -0
  85. package/dist/tests/server/onConfigure.d.ts +1 -0
  86. package/dist/tests/server/onConnect.d.ts +1 -0
  87. package/dist/tests/server/onDestroy.d.ts +1 -0
  88. package/dist/tests/server/onDisconnect.d.ts +1 -0
  89. package/dist/tests/server/onListen.d.ts +1 -0
  90. package/dist/tests/server/onLoadDocument.d.ts +1 -0
  91. package/dist/tests/server/onRequest.d.ts +1 -0
  92. package/dist/tests/server/onStoreDocument.d.ts +1 -0
  93. package/dist/tests/server/onUpgrade.d.ts +1 -0
  94. package/dist/tests/server/requiresAuthentication.d.ts +1 -0
  95. package/dist/tests/transformer/TiptapTransformer.d.ts +1 -0
  96. package/dist/tests/utils/createDirectory.d.ts +1 -0
  97. package/dist/tests/utils/flushRedis.d.ts +1 -0
  98. package/dist/tests/utils/index.d.ts +8 -0
  99. package/dist/tests/utils/newHocuspocus.d.ts +2 -0
  100. package/dist/tests/utils/newHocuspocusProvider.d.ts +3 -0
  101. package/dist/tests/utils/randomInteger.d.ts +1 -0
  102. package/dist/tests/utils/redisConnectionSettings.d.ts +4 -0
  103. package/dist/tests/utils/removeDirectory.d.ts +1 -0
  104. package/dist/tests/utils/retryableAssertion.d.ts +2 -0
  105. package/dist/tests/utils/sleep.d.ts +1 -0
  106. package/package.json +15 -11
  107. package/src/EventEmitter.ts +1 -1
  108. package/src/HocuspocusCloudProvider.ts +34 -0
  109. package/src/HocuspocusProvider.ts +389 -159
  110. package/src/IncomingMessage.ts +35 -11
  111. package/src/MessageReceiver.ts +56 -24
  112. package/src/MessageSender.ts +5 -17
  113. package/src/OutgoingMessage.ts +9 -9
  114. package/src/OutgoingMessages/AuthenticationMessage.ts +21 -0
  115. package/src/OutgoingMessages/AwarenessMessage.ts +1 -1
  116. package/src/OutgoingMessages/SyncStepOneMessage.ts +0 -1
  117. package/src/OutgoingMessages/UpdateMessage.ts +4 -4
  118. package/src/index.ts +1 -1
  119. package/src/types.ts +70 -3
  120. package/CHANGELOG.md +0 -24
  121. package/dist/hocuspocus-provider.js.map +0 -1
  122. package/dist/packages/logger/src/index.d.ts +0 -13
  123. package/dist/packages/provider/src/utils/awarenessStatesToArray.d.ts +0 -4
  124. package/dist/packages/provider/src/utils/index.d.ts +0 -1
  125. package/dist/packages/redis/src/Redis.d.ts +0 -22
  126. package/dist/packages/redis/src/RedisCluster.d.ts +0 -4
  127. package/dist/packages/redis/src/index.d.ts +0 -2
  128. package/dist/packages/rocksdb/src/index.d.ts +0 -30
  129. package/dist/packages/server/src/CloseEvents.d.ts +0 -3
  130. package/dist/packages/throttle/src/index.d.ts +0 -28
  131. package/src/utils/awarenessStatesToArray.ts +0 -8
  132. package/src/utils/index.ts +0 -1
@@ -0,0 +1,23 @@
1
+ export interface CloseEvent {
2
+ code: number;
3
+ reason: string;
4
+ }
5
+ /**
6
+ * The server successfully processed the request, asks that the requester reset
7
+ * its document view, and is not returning any content.
8
+ */
9
+ export declare const ResetConnection: CloseEvent;
10
+ /**
11
+ * Similar to Forbidden, but specifically for use when authentication is required and has
12
+ * failed or has not yet been provided.
13
+ */
14
+ export declare const Unauthorized: CloseEvent;
15
+ /**
16
+ * The request contained valid data and was understood by the server, but the server
17
+ * is refusing action.
18
+ */
19
+ export declare const Forbidden: CloseEvent;
20
+ /**
21
+ * The server timed out waiting for the request.
22
+ */
23
+ export declare const ConnectionTimeout: CloseEvent;
@@ -0,0 +1,6 @@
1
+ import * as encoding from 'lib0/encoding';
2
+ import * as decoding from 'lib0/decoding';
3
+ export declare const writeAuthentication: (encoder: encoding.Encoder, auth: string) => void;
4
+ export declare const writePermissionDenied: (encoder: encoding.Encoder, reason: string) => void;
5
+ export declare const writeAuthenticated: (encoder: encoding.Encoder) => void;
6
+ export declare const readAuthMessage: (decoder: decoding.Decoder, permissionDeniedHandler: (reason: string) => void, authenticatedHandler: () => void) => void;
@@ -0,0 +1,3 @@
1
+ export declare const awarenessStatesToArray: (states: Map<number, Record<string, any>>) => {
2
+ clientId: number;
3
+ }[];
@@ -0,0 +1,4 @@
1
+ export * from './auth';
2
+ export * from './CloseEvents';
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
+ }
@@ -0,0 +1,30 @@
1
+ import { Extension, onChangePayload, onLoadDocumentPayload, storePayload, fetchPayload } from '@hocuspocus/server';
2
+ export interface DatabaseConfiguration {
3
+ /**
4
+ * Pass a Promise to retrieve updates from your database. The Promise should resolve to
5
+ * an array of items with Y.js-compatible binary data.
6
+ */
7
+ fetch: (data: fetchPayload) => Promise<Uint8Array | null>;
8
+ /**
9
+ * Pass a function to store updates in your database.
10
+ */
11
+ store: (data: storePayload) => void;
12
+ }
13
+ export declare class Database implements Extension {
14
+ /**
15
+ * Default configuration
16
+ */
17
+ configuration: DatabaseConfiguration;
18
+ /**
19
+ * Constructor
20
+ */
21
+ constructor(configuration: Partial<DatabaseConfiguration>);
22
+ /**
23
+ * Get stored data from the database.
24
+ */
25
+ onLoadDocument(data: onLoadDocumentPayload): Promise<any>;
26
+ /**
27
+ * Store new updates in the database.
28
+ */
29
+ onStoreDocument(data: onChangePayload): Promise<void>;
30
+ }
@@ -0,0 +1 @@
1
+ export * from './Database';
@@ -0,0 +1,67 @@
1
+ import { Extension, onChangePayload, onConfigurePayload, onConnectPayload, onLoadDocumentPayload, onDestroyPayload, onDisconnectPayload, onRequestPayload, onUpgradePayload } from '@hocuspocus/server';
2
+ export interface LoggerConfiguration {
3
+ /**
4
+ * Prepend all logging message with a string.
5
+ *
6
+ * @deprecated
7
+ */
8
+ prefix: null | string;
9
+ /**
10
+ * Whether to log something for the `onLoadDocument` hook.
11
+ */
12
+ onLoadDocument: boolean;
13
+ /**
14
+ * Whether to log something for the `onChange` hook.
15
+ */
16
+ onChange: boolean;
17
+ /**
18
+ * Whether to log something for the `onStoreDocument` hook.
19
+ */
20
+ onStoreDocument: boolean;
21
+ /**
22
+ * Whether to log something for the `onConnect` hook.
23
+ */
24
+ onConnect: boolean;
25
+ /**
26
+ * Whether to log something for the `onDisconnect` hook.
27
+ */
28
+ onDisconnect: boolean;
29
+ /**
30
+ * Whether to log something for the `onUpgrade` hook.
31
+ */
32
+ onUpgrade: boolean;
33
+ /**
34
+ * Whether to log something for the `onRequest` hook.
35
+ */
36
+ onRequest: boolean;
37
+ /**
38
+ * Whether to log something for the `onDestroy` hook.
39
+ */
40
+ onDestroy: boolean;
41
+ /**
42
+ * Whether to log something for the `onConfigure` hook.
43
+ */
44
+ onConfigure: boolean;
45
+ /**
46
+ * A log function, if none is provided output will go to console
47
+ */
48
+ log: (...args: any[]) => void;
49
+ }
50
+ export declare class Logger implements Extension {
51
+ name: string | null;
52
+ configuration: LoggerConfiguration;
53
+ /**
54
+ * Constructor
55
+ */
56
+ constructor(configuration?: Partial<LoggerConfiguration>);
57
+ onConfigure(data: onConfigurePayload): Promise<void>;
58
+ onLoadDocument(data: onLoadDocumentPayload): Promise<void>;
59
+ onChange(data: onChangePayload): Promise<void>;
60
+ onStoreDocument(data: onDisconnectPayload): Promise<void>;
61
+ onConnect(data: onConnectPayload): Promise<void>;
62
+ onDisconnect(data: onDisconnectPayload): Promise<void>;
63
+ onUpgrade(data: onUpgradePayload): Promise<void>;
64
+ onRequest(data: onRequestPayload): Promise<void>;
65
+ onDestroy(data: onDestroyPayload): Promise<void>;
66
+ private log;
67
+ }
@@ -0,0 +1 @@
1
+ export * from './Logger';
@@ -1,9 +1,8 @@
1
1
  /// <reference types="node" />
2
- import { Configuration, onConnectPayload, onDisconnectPayload, onCreateDocumentPayload, onChangePayload } from '@hocuspocus/server';
2
+ import { Configuration, onConnectPayload, onDisconnectPayload, onLoadDocumentPayload, onChangePayload } from '@hocuspocus/server';
3
3
  export declare class Collector {
4
4
  serverConfiguration: Partial<Configuration>;
5
5
  version: string;
6
- yjsVersion: string;
7
6
  connections: {};
8
7
  messages: {};
9
8
  messageCounter: number;
@@ -30,7 +29,7 @@ export declare class Collector {
30
29
  connectionCount(): {
31
30
  count: string | number;
32
31
  };
33
- createDocument(data: onCreateDocumentPayload): {
32
+ createDocument(data: onLoadDocumentPayload): {
34
33
  action: string;
35
34
  document: any;
36
35
  documentName: string;
@@ -51,12 +50,12 @@ export declare class Collector {
51
50
  documents(): {};
52
51
  info(): Promise<{
53
52
  configuration: Partial<Configuration>;
54
- ipAddress: string;
53
+ ipAddress: string | null;
55
54
  nodeVersion: string;
56
55
  platform: NodeJS.Platform;
57
56
  started: string;
58
57
  version: string;
59
- yjsVersion: string;
60
58
  }>;
59
+ private getIpAddress;
61
60
  private static readableYDoc;
62
61
  }
@@ -1,6 +1,6 @@
1
1
  /// <reference types="node" />
2
2
  import { IncomingMessage, ServerResponse } from 'http';
3
- import WebSocket from 'ws';
3
+ import WebSocket, { WebSocketServer } from 'ws';
4
4
  import { Socket } from 'net';
5
5
  import { Storage } from './Storage';
6
6
  export interface Configuration {
@@ -12,7 +12,7 @@ export interface Configuration {
12
12
  }
13
13
  export declare class Dashboard {
14
14
  configuration: Configuration;
15
- websocketServer: WebSocket.Server;
15
+ websocketServer: WebSocketServer;
16
16
  connections: Map<WebSocket, any>;
17
17
  /**
18
18
  * Constructor
@@ -1,5 +1,5 @@
1
1
  /// <reference types="node" />
2
- import { Extension, onChangePayload, onConfigurePayload, onConnectPayload, onCreateDocumentPayload, onDestroyPayload, onDisconnectPayload, onListenPayload, 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,11 +30,9 @@ 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
- onCreateDocument(data: onCreateDocumentPayload): Promise<void>;
35
+ onLoadDocument(data: onLoadDocumentPayload): Promise<void>;
36
36
  onChange(data: onChangePayload): Promise<void>;
37
- onListen(data: onListenPayload): Promise<void>;
38
- onDestroy(data: onDestroyPayload): Promise<void>;
39
37
  onConfigure(data: onConfigurePayload): Promise<void>;
40
38
  }
@@ -0,0 +1,98 @@
1
+ import RedisClient from 'ioredis';
2
+ import Redlock from 'redlock';
3
+ import { Document, Extension, afterLoadDocumentPayload, afterStoreDocumentPayload, onDisconnectPayload, onStoreDocumentPayload, onAwarenessUpdatePayload, onChangePayload, Debugger, onConfigurePayload, onListenPayload } from '@hocuspocus/server';
4
+ export interface Configuration {
5
+ /**
6
+ * Redis port
7
+ */
8
+ port: number;
9
+ /**
10
+ * Redis host
11
+ */
12
+ host: string;
13
+ /**
14
+ * Options passed directly to Redis constructor
15
+ *
16
+ * https://github.com/luin/ioredis/blob/master/API.md#new-redisport-host-options
17
+ */
18
+ options?: RedisClient.RedisOptions;
19
+ /**
20
+ * An unique instance name, required to filter messages in Redis.
21
+ * If none is provided an unique id is generated.
22
+ */
23
+ identifier: string;
24
+ /**
25
+ * Namespace for Redis keys, if none is provided 'hocuspocus' is used
26
+ */
27
+ prefix: string;
28
+ /**
29
+ * The maximum time for the Redis lock in ms (in case it can’t be released).
30
+ */
31
+ lockTimeout: number;
32
+ }
33
+ export declare class Redis implements Extension {
34
+ /**
35
+ * Make sure to give that extension a higher priority, so
36
+ * the `onStoreDocument` hook is able to intercept the chain,
37
+ * before documents are stored to the database.
38
+ */
39
+ priority: number;
40
+ configuration: Configuration;
41
+ pub: RedisClient.Redis;
42
+ sub: RedisClient.Redis;
43
+ documents: Map<string, Document>;
44
+ redlock: Redlock;
45
+ locks: Map<string, Redlock.Lock>;
46
+ logger: Debugger;
47
+ constructor(configuration: Partial<Configuration>);
48
+ onConfigure({ instance }: onConfigurePayload): Promise<void>;
49
+ onListen({ configuration }: onListenPayload): Promise<void>;
50
+ private getKey;
51
+ private pubKey;
52
+ private subKey;
53
+ private lockKey;
54
+ /**
55
+ * Once a document is laoded, subscribe to the channel in Redis.
56
+ */
57
+ afterLoadDocument({ documentName, document }: afterLoadDocumentPayload): Promise<unknown>;
58
+ /**
59
+ * Publish the first sync step through Redis.
60
+ */
61
+ private publishFirstSyncStep;
62
+ /**
63
+ * Let’s ask Redis who is connected already.
64
+ */
65
+ private requestAwarenessFromOtherInstances;
66
+ /**
67
+ * Before the document is stored, make sure to set a lock in Redis.
68
+ * That’s meant to avoid conflicts with other instances trying to store the document.
69
+ */
70
+ onStoreDocument({ documentName }: onStoreDocumentPayload): Promise<unknown>;
71
+ /**
72
+ * Release the Redis lock, so other instances can store documents.
73
+ */
74
+ afterStoreDocument({ documentName }: afterStoreDocumentPayload): Promise<void>;
75
+ /**
76
+ * Handle awareness update messages received directly by this Hocuspocus instance.
77
+ */
78
+ onAwarenessUpdate({ documentName, awareness, added, updated, removed, }: onAwarenessUpdatePayload): Promise<number>;
79
+ /**
80
+ * Handle incoming messages published on all subscribed document channels.
81
+ * Note that this will also include messages from ourselves as it is not possible
82
+ * in Redis to filter these.
83
+ */
84
+ private handleIncomingMessage;
85
+ /**
86
+ * if the ydoc changed, we'll need to inform other Hocuspocus servers about it.
87
+ */
88
+ onChange(data: onChangePayload): Promise<any>;
89
+ /**
90
+ * Make sure to *not* listen for further changes, when there’s
91
+ * noone connected anymore.
92
+ */
93
+ onDisconnect: ({ documentName, clientsCount }: onDisconnectPayload) => Promise<void>;
94
+ /**
95
+ * Kill the Redlock connection immediately.
96
+ */
97
+ onDestroy(): Promise<void>;
98
+ }
@@ -0,0 +1 @@
1
+ export * from './Redis';
@@ -0,0 +1,26 @@
1
+ import { Database, DatabaseConfiguration } from '@hocuspocus/extension-database';
2
+ import sqlite3 from 'sqlite3';
3
+ export declare const schema = "CREATE TABLE IF NOT EXISTS \"documents\" (\n \"name\" varchar(255) NOT NULL,\n \"data\" blob NOT NULL,\n UNIQUE(name)\n)";
4
+ export declare const selectQuery = "\n SELECT data FROM \"documents\" WHERE name = $name ORDER BY rowid DESC\n";
5
+ export declare const upsertQuery = "\n INSERT INTO \"documents\" (\"name\", \"data\") VALUES ($name, $data)\n ON CONFLICT(name) DO UPDATE SET data = $data\n";
6
+ export interface SQLiteConfiguration extends DatabaseConfiguration {
7
+ /**
8
+ * Valid values are filenames, ":memory:" for an anonymous in-memory database and an empty
9
+ * string for an anonymous disk-based database. Anonymous databases are not persisted and
10
+ * when closing the database handle, their contents are lost.
11
+ *
12
+ * https://github.com/mapbox/node-sqlite3/wiki/API#new-sqlite3databasefilename-mode-callback
13
+ */
14
+ database: string;
15
+ /**
16
+ * The database schema to create.
17
+ */
18
+ schema: string;
19
+ }
20
+ export declare class SQLite extends Database {
21
+ db?: sqlite3.Database;
22
+ configuration: SQLiteConfiguration;
23
+ constructor(configuration?: Partial<SQLiteConfiguration>);
24
+ onConfigure(): Promise<void>;
25
+ onListen(): Promise<void>;
26
+ }
@@ -0,0 +1 @@
1
+ export * from './SQLite';
@@ -0,0 +1,24 @@
1
+ import { Extension, onConnectPayload } from '@hocuspocus/server';
2
+ export interface ThrottleConfiguration {
3
+ throttle: number | null | false;
4
+ banTime: number;
5
+ }
6
+ export declare class Throttle implements Extension {
7
+ configuration: ThrottleConfiguration;
8
+ connectionsByIp: Map<string, Array<number>>;
9
+ bannedIps: Map<string, number>;
10
+ /**
11
+ * Constructor
12
+ */
13
+ constructor(configuration?: Partial<ThrottleConfiguration>);
14
+ /**
15
+ * Throttle requests
16
+ * @private
17
+ */
18
+ private throttle;
19
+ /**
20
+ * onConnect hook
21
+ * @param data
22
+ */
23
+ onConnect(data: onConnectPayload): Promise<any>;
24
+ }
@@ -1,9 +1,8 @@
1
1
  /// <reference types="node" />
2
- import { Extension, onChangePayload, onConfigurePayload, onConnectPayload, onCreateDocumentPayload, onDestroyPayload, onDisconnectPayload, onListenPayload, onRequestPayload, onUpgradePayload } from '@hocuspocus/server';
2
+ import { Extension, onChangePayload, onConnectPayload, onLoadDocumentPayload, onDisconnectPayload } from '@hocuspocus/server';
3
3
  import { Doc } from 'yjs';
4
4
  import { Transformer } from '@hocuspocus/transformer';
5
5
  import { AxiosResponse } from 'axios';
6
- import Timeout = NodeJS.Timeout;
7
6
  export declare enum Events {
8
7
  onChange = "change",
9
8
  onConnect = "connect",
@@ -24,7 +23,7 @@ export interface Configuration {
24
23
  export declare class Webhook implements Extension {
25
24
  configuration: Configuration;
26
25
  debounced: Map<string, {
27
- timeout: Timeout;
26
+ timeout: NodeJS.Timeout;
28
27
  start: number;
29
28
  }>;
30
29
  /**
@@ -42,23 +41,18 @@ export declare class Webhook implements Extension {
42
41
  /**
43
42
  * Send a request to the given url containing the given data
44
43
  */
45
- sendRequest(event: Events, payload: any): Promise<AxiosResponse<any>>;
44
+ sendRequest(event: Events, payload: any): Promise<AxiosResponse<any, any>>;
46
45
  /**
47
46
  * onChange hook
48
47
  */
49
48
  onChange(data: onChangePayload): Promise<void>;
50
49
  /**
51
- * onCreateDocument hook
50
+ * onLoadDocument hook
52
51
  */
53
- onCreateDocument(data: onCreateDocumentPayload): Promise<void>;
52
+ onLoadDocument(data: onLoadDocumentPayload): Promise<void>;
54
53
  /**
55
54
  * onConnect hook
56
55
  */
57
56
  onConnect(data: onConnectPayload): Promise<any>;
58
57
  onDisconnect(data: onDisconnectPayload): Promise<void>;
59
- onUpgrade(data: onUpgradePayload): Promise<void>;
60
- onRequest(data: onRequestPayload): Promise<void>;
61
- onListen(data: onListenPayload): Promise<void>;
62
- onDestroy(data: onDestroyPayload): Promise<void>;
63
- onConfigure(data: onConfigurePayload): Promise<void>;
64
58
  }
@@ -5,5 +5,5 @@ export default class EventEmitter {
5
5
  on(event: string, fn: Function): this;
6
6
  protected emit(event: string, ...args: any): this;
7
7
  off(event: string, fn?: Function): this;
8
- protected removeAllListeners(): void;
8
+ removeAllListeners(): void;
9
9
  }
@@ -0,0 +1,11 @@
1
+ import { HocuspocusProvider, HocuspocusProviderConfiguration } from './HocuspocusProvider';
2
+ export declare type HocuspocusCloudProviderConfiguration = Required<Pick<HocuspocusProviderConfiguration, 'name'>> & Partial<HocuspocusProviderConfiguration> & AdditionalHocuspocusCloudProviderConfiguration;
3
+ export interface AdditionalHocuspocusCloudProviderConfiguration {
4
+ /**
5
+ * A Hocuspocus Cloud key, get one here: https://hocuspocus.cloud/
6
+ */
7
+ key: string;
8
+ }
9
+ export declare class HocuspocusCloudProvider extends HocuspocusProvider {
10
+ constructor(configuration: HocuspocusCloudProviderConfiguration);
11
+ }
@@ -1,78 +1,162 @@
1
1
  import * as Y from 'yjs';
2
2
  import { Awareness } from 'y-protocols/awareness';
3
3
  import * as mutex from 'lib0/mutex';
4
- import { CloseEvent, MessageEvent, OpenEvent } from 'ws';
4
+ import type { Event, CloseEvent, MessageEvent } from 'ws';
5
5
  import EventEmitter from './EventEmitter';
6
- import { OutgoingMessage } from './OutgoingMessage';
7
- export declare enum WebSocketStatus {
8
- Connecting = "connecting",
9
- Connected = "connected",
10
- Disconnected = "disconnected"
11
- }
12
- export interface HocuspocusProviderOptions {
6
+ import { ConstructableOutgoingMessage, onAuthenticationFailedParameters, onCloseParameters, onDisconnectParameters, onMessageParameters, onOpenParameters, onOutgoingMessageParameters, onStatusParameters, onSyncedParameters, WebSocketStatus } from './types';
7
+ import { onAwarenessChangeParameters, onAwarenessUpdateParameters } from '.';
8
+ export declare type HocuspocusProviderConfiguration = Required<Pick<CompleteHocuspocusProviderConfiguration, 'url' | 'name'>> & Partial<CompleteHocuspocusProviderConfiguration>;
9
+ export interface CompleteHocuspocusProviderConfiguration {
10
+ /**
11
+ * URL of your @hocuspocus/server instance
12
+ */
13
13
  url: string;
14
+ /**
15
+ * The identifier/name of your document
16
+ */
14
17
  name: string;
18
+ /**
19
+ * The actual Y.js document
20
+ */
15
21
  document: Y.Doc;
22
+ /**
23
+ * Pass `false` to start the connection manually.
24
+ */
16
25
  connect: boolean;
26
+ /**
27
+ * Pass false to disable broadcasting between browser tabs.
28
+ */
29
+ broadcast: boolean;
30
+ /**
31
+ * An Awareness instance to keep the presence state of all clients.
32
+ */
17
33
  awareness: Awareness;
34
+ /**
35
+ * A token that’s sent to the backend for authentication purposes.
36
+ */
37
+ token: string | (() => string) | (() => Promise<string>) | null;
38
+ /**
39
+ * URL parameters that should be added.
40
+ */
18
41
  parameters: {
19
42
  [key: string]: any;
20
43
  };
44
+ /**
45
+ * An optional WebSocket polyfill, for example for Node.js
46
+ */
21
47
  WebSocketPolyfill: any;
48
+ /**
49
+ * Force syncing the document in the defined interval.
50
+ */
22
51
  forceSyncInterval: false | number;
23
- reconnectTimeoutBase: number;
24
- maxReconnectTimeout: number;
52
+ /**
53
+ * Disconnect when no message is received for the defined amount of milliseconds.
54
+ */
25
55
  messageReconnectTimeout: number;
26
- onOpen: (event: OpenEvent) => void;
56
+ /**
57
+ * The delay between each attempt in milliseconds. You can provide a factor to have the delay grow exponentially.
58
+ */
59
+ delay: number;
60
+ /**
61
+ * The intialDelay is the amount of time to wait before making the first attempt. This option should typically be 0 since you typically want the first attempt to happen immediately.
62
+ */
63
+ initialDelay: number;
64
+ /**
65
+ * The factor option is used to grow the delay exponentially.
66
+ */
67
+ factor: number;
68
+ /**
69
+ * The maximum number of attempts or 0 if there is no limit on number of attempts.
70
+ */
71
+ maxAttempts: number;
72
+ /**
73
+ * minDelay is used to set a lower bound of delay when jitter is enabled. This property has no effect if jitter is disabled.
74
+ */
75
+ minDelay: number;
76
+ /**
77
+ * The maxDelay option is used to set an upper bound for the delay when factor is enabled. A value of 0 can be provided if there should be no upper bound when calculating delay.
78
+ */
79
+ maxDelay: number;
80
+ /**
81
+ * If jitter is true then the calculated delay will be a random integer value between minDelay and the calculated delay for the current iteration.
82
+ */
83
+ jitter: boolean;
84
+ /**
85
+ * A timeout in milliseconds. If timeout is non-zero then a timer is set using setTimeout. If the timeout is triggered then future attempts will be aborted.
86
+ */
87
+ timeout: number;
88
+ onAuthenticated: () => void;
89
+ onAuthenticationFailed: (data: onAuthenticationFailedParameters) => void;
90
+ onOpen: (data: onOpenParameters) => void;
27
91
  onConnect: () => void;
28
- onMessage: (event: MessageEvent) => void;
29
- onOutgoingMessage: (message: OutgoingMessage) => void;
30
- onSynced: () => void;
31
- onDisconnect: (event: CloseEvent) => void;
32
- onClose: (event: CloseEvent) => void;
92
+ onMessage: (data: onMessageParameters) => void;
93
+ onOutgoingMessage: (data: onOutgoingMessageParameters) => void;
94
+ onStatus: (data: onStatusParameters) => void;
95
+ onSynced: (data: onSyncedParameters) => void;
96
+ onDisconnect: (data: onDisconnectParameters) => void;
97
+ onClose: (data: onCloseParameters) => void;
33
98
  onDestroy: () => void;
34
- onAwarenessChange: (states: any) => void;
35
- debug: boolean;
99
+ onAwarenessUpdate: (data: onAwarenessUpdateParameters) => void;
100
+ onAwarenessChange: (data: onAwarenessChangeParameters) => void;
101
+ /**
102
+ * Don’t output any warnings.
103
+ */
104
+ quiet: boolean;
36
105
  }
37
106
  export declare class HocuspocusProvider extends EventEmitter {
38
- options: HocuspocusProviderOptions;
39
- awareness: Awareness;
107
+ configuration: CompleteHocuspocusProviderConfiguration;
40
108
  subscribedToBroadcastChannel: boolean;
41
- webSocket: any;
109
+ webSocket: WebSocket | null;
42
110
  shouldConnect: boolean;
43
111
  status: WebSocketStatus;
44
- failedConnectionAttempts: number;
45
112
  isSynced: boolean;
113
+ unsyncedChanges: number;
114
+ isAuthenticated: boolean;
46
115
  lastMessageReceived: number;
47
116
  mux: mutex.mutex;
48
117
  intervals: any;
49
- constructor(options?: Partial<HocuspocusProviderOptions>);
50
- setOptions(options?: Partial<HocuspocusProviderOptions>): void;
118
+ connectionAttempt: {
119
+ resolve: (value?: any) => void;
120
+ reject: (reason?: any) => void;
121
+ } | null;
122
+ constructor(configuration: HocuspocusProviderConfiguration);
123
+ setConfiguration(configuration?: Partial<HocuspocusProviderConfiguration>): void;
124
+ boundConnect: () => Promise<void>;
125
+ connect(): Promise<void>;
126
+ createWebSocketConnection(): Promise<unknown>;
127
+ resolveConnectionAttempt(): void;
128
+ stopConnectionAttempt(): void;
129
+ rejectConnectionAttempt(): void;
51
130
  get document(): Y.Doc;
52
131
  get awareness(): Awareness;
132
+ get hasUnsyncedChanges(): boolean;
53
133
  checkConnection(): void;
54
134
  forceSync(): void;
55
- registerBeforeUnloadEventListener(): void;
135
+ boundBeforeUnload: () => void;
136
+ beforeUnload(): void;
137
+ registerEventListeners(): void;
56
138
  documentUpdateHandler(update: Uint8Array, origin: any): void;
57
139
  awarenessUpdateHandler({ added, updated, removed }: any, origin: any): void;
140
+ permissionDeniedHandler(reason: string): void;
141
+ authenticatedHandler(): void;
58
142
  get serverUrl(): string;
59
143
  get url(): string;
60
144
  get synced(): boolean;
61
145
  set synced(state: boolean);
62
- connect(): void;
146
+ get isAuthenticationRequired(): boolean;
63
147
  disconnect(): void;
64
- createWebSocketConnection(): void;
65
- onOpen(event: OpenEvent): void;
66
- webSocketConnectionEstablished(): void;
67
- send(Message: OutgoingMessage, args: any, broadcast?: boolean): void;
148
+ onOpen(event: Event): Promise<void>;
149
+ getToken(): Promise<string | null>;
150
+ startSync(): void;
151
+ send(Message: ConstructableOutgoingMessage, args: any, broadcast?: boolean): void;
68
152
  onMessage(event: MessageEvent): void;
69
153
  onClose(event: CloseEvent): void;
70
154
  destroy(): void;
71
155
  get broadcastChannel(): string;
156
+ boundBroadcastChannelSubscriber: (data: ArrayBuffer) => void;
72
157
  broadcastChannelSubscriber(data: ArrayBuffer): void;
73
158
  subscribeToBroadcastChannel(): void;
74
159
  disconnectBroadcastChannel(): void;
75
- broadcast(Message: OutgoingMessage, args: any): void;
76
- log(message: string): void;
160
+ broadcast(Message: ConstructableOutgoingMessage, args?: any): void;
77
161
  setAwarenessField(key: string, value: any): void;
78
162
  }