@hocuspocus/provider 1.0.0-alpha.25 → 1.0.0-alpha.29

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 (31) hide show
  1. package/dist/hocuspocus-provider.cjs +132 -330
  2. package/dist/hocuspocus-provider.cjs.map +1 -1
  3. package/dist/hocuspocus-provider.esm.js +130 -329
  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/index.d.ts +1 -0
  7. package/dist/packages/extension-database/src/Database.d.ts +36 -0
  8. package/dist/packages/extension-database/src/index.d.ts +1 -0
  9. package/dist/packages/extension-logger/src/Logger.d.ts +2 -8
  10. package/dist/packages/extension-sqlite/src/SQLite.d.ts +26 -0
  11. package/dist/packages/extension-sqlite/src/index.d.ts +1 -0
  12. package/dist/packages/extension-webhook/src/index.d.ts +1 -1
  13. package/dist/packages/provider/src/HocuspocusCloudProvider.d.ts +11 -0
  14. package/dist/packages/provider/src/HocuspocusProvider.d.ts +10 -5
  15. package/dist/packages/provider/src/index.d.ts +1 -0
  16. package/dist/packages/server/src/Connection.d.ts +1 -1
  17. package/dist/packages/server/src/Hocuspocus.d.ts +15 -5
  18. package/dist/packages/server/src/types.d.ts +17 -8
  19. package/dist/{demos/backend/src/express.d.ts → playground/backend/src/default.d.ts} +0 -0
  20. package/dist/{demos/backend/src/koa.d.ts → playground/backend/src/express.d.ts} +0 -0
  21. package/dist/{demos/backend/src/load-document.d.ts → playground/backend/src/koa.d.ts} +0 -0
  22. package/dist/{demos/backend/src/minimal.d.ts → playground/backend/src/load-document.d.ts} +0 -0
  23. package/dist/{demos → playground}/backend/src/monitor.d.ts +0 -0
  24. package/dist/{demos → playground}/backend/src/redis.d.ts +0 -0
  25. package/dist/{demos → playground}/backend/src/slow.d.ts +0 -0
  26. package/dist/{demos → playground}/backend/src/webhook.d.ts +0 -0
  27. package/package.json +6 -8
  28. package/src/HocuspocusCloudProvider.ts +34 -0
  29. package/src/HocuspocusProvider.ts +95 -63
  30. package/src/index.ts +1 -0
  31. package/dist/packages/server/src/CloseEvents.d.ts +0 -4
@@ -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;
@@ -1 +1,2 @@
1
1
  export * from './auth';
2
+ export * from './CloseEvents';
@@ -0,0 +1,36 @@
1
+ /// <reference types="node" />
2
+ import { Extension, onChangePayload, onLoadDocumentPayload } from '@hocuspocus/server';
3
+ export interface DatabaseConfiguration {
4
+ /**
5
+ * Pass a Promise to retrieve updates from your database. The Promise should resolve to
6
+ * an array of items with Y.js-compatible binary data.
7
+ */
8
+ fetchUpdates: ({ documentName }: {
9
+ documentName: string;
10
+ }) => Promise<Uint8Array[]>;
11
+ /**
12
+ * Pass a function to store updates in your database.
13
+ */
14
+ storeUpdate: ({ update, documentName }: {
15
+ update: Buffer;
16
+ documentName: string;
17
+ }) => void;
18
+ }
19
+ export declare class Database implements Extension {
20
+ /**
21
+ * Default configuration
22
+ */
23
+ configuration: DatabaseConfiguration;
24
+ /**
25
+ * Constructor
26
+ */
27
+ constructor(configuration: Partial<DatabaseConfiguration>);
28
+ /**
29
+ * Get stored data from the database.
30
+ */
31
+ onLoadDocument({ document, documentName }: onLoadDocumentPayload): Promise<any>;
32
+ /**
33
+ * Store new updates in the database.
34
+ */
35
+ onChange({ document, documentName }: onChangePayload): Promise<void>;
36
+ }
@@ -0,0 +1 @@
1
+ export * from './Database';
@@ -1,4 +1,4 @@
1
- import { Extension, onChangePayload, onConfigurePayload, onConnectPayload, onLoadDocumentPayload, onDestroyPayload, onDisconnectPayload, onListenPayload, onRequestPayload, onUpgradePayload } from '@hocuspocus/server';
1
+ import { Extension, onChangePayload, onConfigurePayload, onConnectPayload, onLoadDocumentPayload, onDestroyPayload, onDisconnectPayload, onRequestPayload, onUpgradePayload } from '@hocuspocus/server';
2
2
  export interface LoggerConfiguration {
3
3
  /**
4
4
  * Prepend all logging message with a string.
@@ -30,10 +30,6 @@ export interface LoggerConfiguration {
30
30
  * Whether to log something for the `onRequest` hook.
31
31
  */
32
32
  onRequest: boolean;
33
- /**
34
- * Whether to log something for the `onListen` hook.
35
- */
36
- onListen: boolean;
37
33
  /**
38
34
  * Whether to log something for the `onDestroy` hook.
39
35
  */
@@ -54,15 +50,13 @@ export declare class Logger implements Extension {
54
50
  * Constructor
55
51
  */
56
52
  constructor(configuration?: Partial<LoggerConfiguration>);
53
+ onConfigure(data: onConfigurePayload): Promise<void>;
57
54
  onLoadDocument(data: onLoadDocumentPayload): Promise<void>;
58
55
  onChange(data: onChangePayload): Promise<void>;
59
56
  onConnect(data: onConnectPayload): Promise<void>;
60
57
  onDisconnect(data: onDisconnectPayload): Promise<void>;
61
58
  onUpgrade(data: onUpgradePayload): Promise<void>;
62
59
  onRequest(data: onRequestPayload): Promise<void>;
63
- onListen(data: onListenPayload): Promise<void>;
64
60
  onDestroy(data: onDestroyPayload): Promise<void>;
65
- onConfigure(data: onConfigurePayload): Promise<void>;
66
- private logRawText;
67
61
  private log;
68
62
  }
@@ -0,0 +1,26 @@
1
+ import { Database, DatabaseConfiguration } from '@hocuspocus/extension-database';
2
+ import sqlite3 from 'sqlite3';
3
+ export interface SQLiteConfiguration extends DatabaseConfiguration {
4
+ /**
5
+ * Valid values are filenames, ":memory:" for an anonymous in-memory database and an empty
6
+ * string for an anonymous disk-based database. Anonymous databases are not persisted and
7
+ * when closing the database handle, their contents are lost.
8
+ *
9
+ * https://github.com/mapbox/node-sqlite3/wiki/API#new-sqlite3databasefilename-mode-callback
10
+ */
11
+ database: string;
12
+ /**
13
+ * The database schema to create.
14
+ */
15
+ schema: string;
16
+ }
17
+ export declare class SQLite extends Database {
18
+ db?: sqlite3.Database;
19
+ configuration: SQLiteConfiguration;
20
+ /**
21
+ * Constructor
22
+ */
23
+ constructor(configuration?: Partial<SQLiteConfiguration>);
24
+ onListen(): Promise<void>;
25
+ onConfigure(): Promise<void>;
26
+ }
@@ -0,0 +1 @@
1
+ export * from './SQLite';
@@ -42,7 +42,7 @@ export declare class Webhook implements Extension {
42
42
  /**
43
43
  * Send a request to the given url containing the given data
44
44
  */
45
- sendRequest(event: Events, payload: any): Promise<AxiosResponse<unknown, any>>;
45
+ sendRequest(event: Events, payload: any): Promise<AxiosResponse<any, any>>;
46
46
  /**
47
47
  * onChange hook
48
48
  */
@@ -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
+ }
@@ -10,7 +10,8 @@ export declare enum WebSocketStatus {
10
10
  Connected = "connected",
11
11
  Disconnected = "disconnected"
12
12
  }
13
- export interface HocuspocusProviderOptions {
13
+ export declare type HocuspocusProviderConfiguration = Required<Pick<CompleteHocuspocusProviderConfiguration, 'url' | 'name'>> & Partial<CompleteHocuspocusProviderConfiguration>;
14
+ export interface CompleteHocuspocusProviderConfiguration {
14
15
  /**
15
16
  * URL of your @hocuspocus/server instance
16
17
  */
@@ -104,9 +105,13 @@ export interface HocuspocusProviderOptions {
104
105
  onDestroy: () => void;
105
106
  onAwarenessUpdate: (states: any) => void;
106
107
  onAwarenessChange: (states: any) => void;
108
+ /**
109
+ * Don’t output any warnings.
110
+ */
111
+ quiet: boolean;
107
112
  }
108
113
  export declare class HocuspocusProvider extends EventEmitter {
109
- options: HocuspocusProviderOptions;
114
+ configuration: CompleteHocuspocusProviderConfiguration;
110
115
  subscribedToBroadcastChannel: boolean;
111
116
  webSocket: WebSocket | null;
112
117
  shouldConnect: boolean;
@@ -120,8 +125,8 @@ export declare class HocuspocusProvider extends EventEmitter {
120
125
  resolve: (value?: any) => void;
121
126
  reject: (reason?: any) => void;
122
127
  } | null;
123
- constructor(options?: Partial<HocuspocusProviderOptions>);
124
- setOptions(options?: Partial<HocuspocusProviderOptions>): void;
128
+ constructor(configuration: HocuspocusProviderConfiguration);
129
+ setConfiguration(configuration?: Partial<HocuspocusProviderConfiguration>): void;
125
130
  connect(): Promise<void>;
126
131
  createWebSocketConnection(): Promise<unknown>;
127
132
  resolveConnectionAttempt(): void;
@@ -130,7 +135,7 @@ export declare class HocuspocusProvider extends EventEmitter {
130
135
  get awareness(): Awareness;
131
136
  checkConnection(): void;
132
137
  forceSync(): void;
133
- registerBeforeUnloadEventListener(): void;
138
+ registerEventListeners(): void;
134
139
  documentUpdateHandler(update: Uint8Array, origin: any): void;
135
140
  awarenessUpdateHandler({ added, updated, removed }: any, origin: any): void;
136
141
  permissionDeniedHandler(reason: string): void;
@@ -1,3 +1,4 @@
1
1
  export * from './HocuspocusProvider';
2
+ export * from './HocuspocusCloudProvider';
2
3
  export * from './types';
3
4
  export * from './utils';
@@ -2,8 +2,8 @@
2
2
  import AsyncLock from 'async-lock';
3
3
  import WebSocket from 'ws';
4
4
  import { IncomingMessage as HTTPIncomingMessage } from 'http';
5
+ import { CloseEvent } from '@hocuspocus/common';
5
6
  import Document from './Document';
6
- import { CloseEvent } from './types';
7
7
  import { MessageLogger } from './Debugger';
8
8
  declare class Connection {
9
9
  webSocket: WebSocket;
@@ -3,13 +3,15 @@ import WebSocket, { WebSocketServer } from 'ws';
3
3
  import { IncomingMessage, Server as HTTPServer } from 'http';
4
4
  import { Configuration, Hook } from './types';
5
5
  import { MessageLogger } from './Debugger';
6
+ import { onListenPayload } from '.';
6
7
  export declare const defaultConfiguration: {
7
8
  name: null;
8
9
  port: number;
9
10
  timeout: number;
11
+ quiet: boolean;
10
12
  };
11
13
  /**
12
- * Hocuspocus server
14
+ * Hocuspocus Server
13
15
  */
14
16
  export declare class Hocuspocus {
15
17
  configuration: Configuration;
@@ -21,11 +23,12 @@ export declare class Hocuspocus {
21
23
  * Configure the server
22
24
  */
23
25
  configure(configuration: Partial<Configuration>): Hocuspocus;
24
- get authenticationRequired(): boolean;
26
+ get requiresAuthentication(): boolean;
25
27
  /**
26
28
  * Start the server
27
29
  */
28
- listen(): Promise<void>;
30
+ listen(portOrCallback?: number | ((data: onListenPayload) => Promise<any>) | null, callback?: any): Promise<void>;
31
+ private showStartScreen;
29
32
  /**
30
33
  * Get the total number of active documents
31
34
  */
@@ -43,7 +46,14 @@ export declare class Hocuspocus {
43
46
  */
44
47
  destroy(): Promise<any>;
45
48
  /**
46
- * Handle the incoming WebSocket connection
49
+ * The `handleConnection` method receives incoming WebSocket connections,
50
+ * runs all hooks:
51
+ *
52
+ * - onConnect for all connections
53
+ * - onAuthenticate only if required
54
+ *
55
+ * … and if nothings fails it’ll fully establish the connection and
56
+ * load the Document then.
47
57
  */
48
58
  handleConnection(incoming: WebSocket, request: IncomingMessage, documentName: string, context?: any): void;
49
59
  /**
@@ -75,7 +85,7 @@ export declare class Hocuspocus {
75
85
  * Get document name by the given request
76
86
  * @private
77
87
  */
78
- private static getDocumentName;
88
+ private getDocumentNameFromRequest;
79
89
  enableDebugging(): void;
80
90
  enableMessageLogging(): void;
81
91
  disableLogging(): void;
@@ -25,8 +25,9 @@ export interface AwarenessUpdate {
25
25
  updated: Array<any>;
26
26
  removed: Array<any>;
27
27
  }
28
- export interface ConnectionConfig {
28
+ export interface ConnectionConfiguration {
29
29
  readOnly: boolean;
30
+ requiresAuthentication: boolean;
30
31
  isAuthenticated: boolean;
31
32
  }
32
33
  export interface Extension {
@@ -67,6 +68,18 @@ export interface Configuration extends Extension {
67
68
  * Defines in which interval the server sends a ping, and closes the connection when no pong is sent back.
68
69
  */
69
70
  timeout: number;
71
+ /**
72
+ * By default, the servers show a start screen. If passed false, the server will start quietly.
73
+ */
74
+ quiet: boolean;
75
+ /**
76
+ * Function which returns the (customized) document name based on the request
77
+ */
78
+ getDocumentName?(data: {
79
+ documentName: string;
80
+ request: IncomingMessage;
81
+ requestParameters: URLSearchParams;
82
+ }): string | Promise<string>;
70
83
  }
71
84
  export interface onAuthenticatePayload {
72
85
  documentName: string;
@@ -75,7 +88,7 @@ export interface onAuthenticatePayload {
75
88
  requestParameters: URLSearchParams;
76
89
  socketId: string;
77
90
  token: string;
78
- connection: ConnectionConfig;
91
+ connection: ConnectionConfiguration;
79
92
  }
80
93
  export interface onConnectPayload {
81
94
  documentName: string;
@@ -84,7 +97,7 @@ export interface onConnectPayload {
84
97
  requestHeaders: IncomingHttpHeaders;
85
98
  requestParameters: URLSearchParams;
86
99
  socketId: string;
87
- connection: ConnectionConfig;
100
+ connection: ConnectionConfiguration;
88
101
  }
89
102
  export interface onLoadDocumentPayload {
90
103
  context: any;
@@ -94,7 +107,7 @@ export interface onLoadDocumentPayload {
94
107
  requestHeaders: IncomingHttpHeaders;
95
108
  requestParameters: URLSearchParams;
96
109
  socketId: string;
97
- connection: ConnectionConfig;
110
+ connection: ConnectionConfiguration;
98
111
  }
99
112
  export interface onChangePayload {
100
113
  clientsCount: number;
@@ -140,7 +153,3 @@ export interface onConfigurePayload {
140
153
  yjsVersion: string;
141
154
  instance: Hocuspocus;
142
155
  }
143
- export interface CloseEvent {
144
- code: number;
145
- reason: string;
146
- }
File without changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hocuspocus/provider",
3
- "version": "1.0.0-alpha.25",
3
+ "version": "1.0.0-alpha.29",
4
4
  "description": "hocuspocus provider",
5
5
  "homepage": "https://hocuspocus.dev",
6
6
  "keywords": [
@@ -28,13 +28,11 @@
28
28
  "dist"
29
29
  ],
30
30
  "dependencies": {
31
- "@lifeomic/attempt": "^3.0.0",
32
- "lib0": "^0.2.42",
31
+ "@hocuspocus/common": "^1.0.0-alpha.4",
32
+ "@lifeomic/attempt": "^3.0.1",
33
+ "lib0": "^0.2.43",
33
34
  "y-protocols": "^1.0.5",
34
- "yjs": "^13.5.0"
35
+ "yjs": "^13.5.22"
35
36
  },
36
- "gitHead": "cd788b6a315f608ef531524409abdce1e6790726",
37
- "publishConfig": {
38
- "access": "public"
39
- }
37
+ "gitHead": "87b715d1d28c603702879f4413c17d2c4a38c51a"
40
38
  }
@@ -0,0 +1,34 @@
1
+ import {
2
+ HocuspocusProvider,
3
+ HocuspocusProviderConfiguration,
4
+ } from './HocuspocusProvider'
5
+
6
+ export type HocuspocusCloudProviderConfiguration =
7
+ Required<Pick<HocuspocusProviderConfiguration, 'name'>> &
8
+ Partial<HocuspocusProviderConfiguration> &
9
+ AdditionalHocuspocusCloudProviderConfiguration
10
+
11
+ export interface AdditionalHocuspocusCloudProviderConfiguration {
12
+ /**
13
+ * A Hocuspocus Cloud key, get one here: https://hocuspocus.cloud/
14
+ */
15
+ key: string,
16
+ }
17
+
18
+ export class HocuspocusCloudProvider extends HocuspocusProvider {
19
+ constructor(configuration: HocuspocusCloudProviderConfiguration) {
20
+ if (!configuration.url) {
21
+ configuration.url = 'wss://connect.hocuspocus.cloud'
22
+ }
23
+
24
+ if (configuration.key) {
25
+ if (!configuration.parameters) {
26
+ configuration.parameters = {}
27
+ }
28
+
29
+ configuration.parameters.key = configuration.key
30
+ }
31
+
32
+ super(configuration as HocuspocusProviderConfiguration)
33
+ }
34
+ }