@hocuspocus/extension-throttle 1.0.0-alpha.48 → 1.0.0-alpha.52

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.
@@ -3,13 +3,20 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  class Throttle {
6
- constructor() {
6
+ /**
7
+ * Constructor
8
+ */
9
+ constructor(configuration) {
7
10
  this.configuration = {
8
11
  throttle: 15,
9
12
  banTime: 5,
10
13
  };
11
14
  this.connectionsByIp = new Map();
12
15
  this.bannedIps = new Map();
16
+ this.configuration = {
17
+ ...this.configuration,
18
+ ...configuration,
19
+ };
13
20
  }
14
21
  /**
15
22
  * Throttle requests
@@ -1 +1 @@
1
- {"version":3,"file":"hocuspocus-throttle.cjs","sources":["../src/index.ts"],"sourcesContent":["import {\n Extension,\n onConnectPayload,\n} from '@hocuspocus/server'\n\nexport interface Configuration {\n throttle: number | null | false,\n banTime: number,\n}\n\nexport class Throttle implements Extension {\n\n configuration: Configuration = {\n throttle: 15,\n banTime: 5,\n }\n\n connectionsByIp: Map<string, Array<number>> = new Map()\n\n bannedIps: Map<string, number> = new Map()\n\n /**\n * Throttle requests\n * @private\n */\n private throttle(ip: string): Boolean {\n if (!this.configuration.throttle) {\n return false\n }\n\n const bannedAt = this.bannedIps.get(ip) || 0\n\n if (Date.now() < (bannedAt + (this.configuration.banTime * 60 * 1000))) {\n return true\n }\n\n this.bannedIps.delete(ip)\n\n // add this connection try to the list of previous connections\n const previousConnections = this.connectionsByIp.get(ip) || []\n previousConnections.push(Date.now())\n\n // calculate the previous connections in the last minute\n const previousConnectionsInTheLastMinute = previousConnections\n .filter(timestamp => timestamp + (60 * 1000) > Date.now())\n\n this.connectionsByIp.set(ip, previousConnectionsInTheLastMinute)\n\n if (previousConnectionsInTheLastMinute.length > this.configuration.throttle) {\n this.bannedIps.set(ip, Date.now())\n return true\n }\n\n return false\n }\n\n /**\n * onConnect hook\n * @param data\n */\n onConnect(data: onConnectPayload): Promise<any> {\n const { request } = data\n\n // get the remote ip address\n const ip = request.headers['x-real-ip']\n || request.headers['x-forwarded-for']\n || request.socket.remoteAddress\n || ''\n\n // throttle the connection\n return this.throttle(<string> ip) ? Promise.reject() : Promise.resolve()\n }\n\n}\n"],"names":[],"mappings":";;;;MAUa,QAAQ;IAArB;QAEE,kBAAa,GAAkB;YAC7B,QAAQ,EAAE,EAAE;YACZ,OAAO,EAAE,CAAC;SACX,CAAA;QAED,oBAAe,GAA+B,IAAI,GAAG,EAAE,CAAA;QAEvD,cAAS,GAAwB,IAAI,GAAG,EAAE,CAAA;KAsD3C;;;;;IAhDS,QAAQ,CAAC,EAAU;QACzB,IAAI,CAAC,IAAI,CAAC,aAAa,CAAC,QAAQ,EAAE;YAChC,OAAO,KAAK,CAAA;SACb;QAED,MAAM,QAAQ,GAAG,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,EAAE,CAAC,IAAI,CAAC,CAAA;QAE5C,IAAI,IAAI,CAAC,GAAG,EAAE,IAAI,QAAQ,IAAI,IAAI,CAAC,aAAa,CAAC,OAAO,GAAG,EAAE,GAAG,IAAI,CAAC,CAAC,EAAE;YACtE,OAAO,IAAI,CAAA;SACZ;QAED,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,EAAE,CAAC,CAAA;;QAGzB,MAAM,mBAAmB,GAAG,IAAI,CAAC,eAAe,CAAC,GAAG,CAAC,EAAE,CAAC,IAAI,EAAE,CAAA;QAC9D,mBAAmB,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,CAAA;;QAGpC,MAAM,kCAAkC,GAAG,mBAAmB;aAC3D,MAAM,CAAC,SAAS,IAAI,SAAS,IAAI,EAAE,GAAG,IAAI,CAAC,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC,CAAA;QAE5D,IAAI,CAAC,eAAe,CAAC,GAAG,CAAC,EAAE,EAAE,kCAAkC,CAAC,CAAA;QAEhE,IAAI,kCAAkC,CAAC,MAAM,GAAG,IAAI,CAAC,aAAa,CAAC,QAAQ,EAAE;YAC3E,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,EAAE,EAAE,IAAI,CAAC,GAAG,EAAE,CAAC,CAAA;YAClC,OAAO,IAAI,CAAA;SACZ;QAED,OAAO,KAAK,CAAA;KACb;;;;;IAMD,SAAS,CAAC,IAAsB;QAC9B,MAAM,EAAE,OAAO,EAAE,GAAG,IAAI,CAAA;;QAGxB,MAAM,EAAE,GAAG,OAAO,CAAC,OAAO,CAAC,WAAW,CAAC;eAClC,OAAO,CAAC,OAAO,CAAC,iBAAiB,CAAC;eAClC,OAAO,CAAC,MAAM,CAAC,aAAa;eAC5B,EAAE,CAAA;;QAGP,OAAO,IAAI,CAAC,QAAQ,CAAU,EAAE,CAAC,GAAG,OAAO,CAAC,MAAM,EAAE,GAAG,OAAO,CAAC,OAAO,EAAE,CAAA;KACzE;;;;;"}
1
+ {"version":3,"file":"hocuspocus-throttle.cjs","sources":["../src/index.ts"],"sourcesContent":["import {\n Extension,\n onConnectPayload,\n} from '@hocuspocus/server'\n\nexport interface ThrottleConfiguration {\n throttle: number | null | false,\n banTime: number,\n}\n\nexport class Throttle implements Extension {\n\n configuration: ThrottleConfiguration = {\n throttle: 15,\n banTime: 5,\n }\n\n connectionsByIp: Map<string, Array<number>> = new Map()\n\n bannedIps: Map<string, number> = new Map()\n\n /**\n * Constructor\n */\n constructor(configuration?: Partial<ThrottleConfiguration>) {\n this.configuration = {\n ...this.configuration,\n ...configuration,\n }\n }\n\n /**\n * Throttle requests\n * @private\n */\n private throttle(ip: string): Boolean {\n if (!this.configuration.throttle) {\n return false\n }\n\n const bannedAt = this.bannedIps.get(ip) || 0\n\n if (Date.now() < (bannedAt + (this.configuration.banTime * 60 * 1000))) {\n return true\n }\n\n this.bannedIps.delete(ip)\n\n // add this connection try to the list of previous connections\n const previousConnections = this.connectionsByIp.get(ip) || []\n previousConnections.push(Date.now())\n\n // calculate the previous connections in the last minute\n const previousConnectionsInTheLastMinute = previousConnections\n .filter(timestamp => timestamp + (60 * 1000) > Date.now())\n\n this.connectionsByIp.set(ip, previousConnectionsInTheLastMinute)\n\n if (previousConnectionsInTheLastMinute.length > this.configuration.throttle) {\n this.bannedIps.set(ip, Date.now())\n return true\n }\n\n return false\n }\n\n /**\n * onConnect hook\n * @param data\n */\n onConnect(data: onConnectPayload): Promise<any> {\n const { request } = data\n\n // get the remote ip address\n const ip = request.headers['x-real-ip']\n || request.headers['x-forwarded-for']\n || request.socket.remoteAddress\n || ''\n\n // throttle the connection\n return this.throttle(<string> ip) ? Promise.reject() : Promise.resolve()\n }\n\n}\n"],"names":[],"mappings":";;;;MAUa,QAAQ;;;;IAcnB,YAAY,aAA8C;QAZ1D,kBAAa,GAA0B;YACrC,QAAQ,EAAE,EAAE;YACZ,OAAO,EAAE,CAAC;SACX,CAAA;QAED,oBAAe,GAA+B,IAAI,GAAG,EAAE,CAAA;QAEvD,cAAS,GAAwB,IAAI,GAAG,EAAE,CAAA;QAMxC,IAAI,CAAC,aAAa,GAAG;YACnB,GAAG,IAAI,CAAC,aAAa;YACrB,GAAG,aAAa;SACjB,CAAA;KACF;;;;;IAMO,QAAQ,CAAC,EAAU;QACzB,IAAI,CAAC,IAAI,CAAC,aAAa,CAAC,QAAQ,EAAE;YAChC,OAAO,KAAK,CAAA;SACb;QAED,MAAM,QAAQ,GAAG,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,EAAE,CAAC,IAAI,CAAC,CAAA;QAE5C,IAAI,IAAI,CAAC,GAAG,EAAE,IAAI,QAAQ,IAAI,IAAI,CAAC,aAAa,CAAC,OAAO,GAAG,EAAE,GAAG,IAAI,CAAC,CAAC,EAAE;YACtE,OAAO,IAAI,CAAA;SACZ;QAED,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,EAAE,CAAC,CAAA;;QAGzB,MAAM,mBAAmB,GAAG,IAAI,CAAC,eAAe,CAAC,GAAG,CAAC,EAAE,CAAC,IAAI,EAAE,CAAA;QAC9D,mBAAmB,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,CAAA;;QAGpC,MAAM,kCAAkC,GAAG,mBAAmB;aAC3D,MAAM,CAAC,SAAS,IAAI,SAAS,IAAI,EAAE,GAAG,IAAI,CAAC,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC,CAAA;QAE5D,IAAI,CAAC,eAAe,CAAC,GAAG,CAAC,EAAE,EAAE,kCAAkC,CAAC,CAAA;QAEhE,IAAI,kCAAkC,CAAC,MAAM,GAAG,IAAI,CAAC,aAAa,CAAC,QAAQ,EAAE;YAC3E,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,EAAE,EAAE,IAAI,CAAC,GAAG,EAAE,CAAC,CAAA;YAClC,OAAO,IAAI,CAAA;SACZ;QAED,OAAO,KAAK,CAAA;KACb;;;;;IAMD,SAAS,CAAC,IAAsB;QAC9B,MAAM,EAAE,OAAO,EAAE,GAAG,IAAI,CAAA;;QAGxB,MAAM,EAAE,GAAG,OAAO,CAAC,OAAO,CAAC,WAAW,CAAC;eAClC,OAAO,CAAC,OAAO,CAAC,iBAAiB,CAAC;eAClC,OAAO,CAAC,MAAM,CAAC,aAAa;eAC5B,EAAE,CAAA;;QAGP,OAAO,IAAI,CAAC,QAAQ,CAAU,EAAE,CAAC,GAAG,OAAO,CAAC,MAAM,EAAE,GAAG,OAAO,CAAC,OAAO,EAAE,CAAA;KACzE;;;;;"}
@@ -1,11 +1,18 @@
1
1
  class Throttle {
2
- constructor() {
2
+ /**
3
+ * Constructor
4
+ */
5
+ constructor(configuration) {
3
6
  this.configuration = {
4
7
  throttle: 15,
5
8
  banTime: 5,
6
9
  };
7
10
  this.connectionsByIp = new Map();
8
11
  this.bannedIps = new Map();
12
+ this.configuration = {
13
+ ...this.configuration,
14
+ ...configuration,
15
+ };
9
16
  }
10
17
  /**
11
18
  * Throttle requests
@@ -1 +1 @@
1
- {"version":3,"file":"hocuspocus-throttle.esm.js","sources":["../src/index.ts"],"sourcesContent":["import {\n Extension,\n onConnectPayload,\n} from '@hocuspocus/server'\n\nexport interface Configuration {\n throttle: number | null | false,\n banTime: number,\n}\n\nexport class Throttle implements Extension {\n\n configuration: Configuration = {\n throttle: 15,\n banTime: 5,\n }\n\n connectionsByIp: Map<string, Array<number>> = new Map()\n\n bannedIps: Map<string, number> = new Map()\n\n /**\n * Throttle requests\n * @private\n */\n private throttle(ip: string): Boolean {\n if (!this.configuration.throttle) {\n return false\n }\n\n const bannedAt = this.bannedIps.get(ip) || 0\n\n if (Date.now() < (bannedAt + (this.configuration.banTime * 60 * 1000))) {\n return true\n }\n\n this.bannedIps.delete(ip)\n\n // add this connection try to the list of previous connections\n const previousConnections = this.connectionsByIp.get(ip) || []\n previousConnections.push(Date.now())\n\n // calculate the previous connections in the last minute\n const previousConnectionsInTheLastMinute = previousConnections\n .filter(timestamp => timestamp + (60 * 1000) > Date.now())\n\n this.connectionsByIp.set(ip, previousConnectionsInTheLastMinute)\n\n if (previousConnectionsInTheLastMinute.length > this.configuration.throttle) {\n this.bannedIps.set(ip, Date.now())\n return true\n }\n\n return false\n }\n\n /**\n * onConnect hook\n * @param data\n */\n onConnect(data: onConnectPayload): Promise<any> {\n const { request } = data\n\n // get the remote ip address\n const ip = request.headers['x-real-ip']\n || request.headers['x-forwarded-for']\n || request.socket.remoteAddress\n || ''\n\n // throttle the connection\n return this.throttle(<string> ip) ? Promise.reject() : Promise.resolve()\n }\n\n}\n"],"names":[],"mappings":"MAUa,QAAQ;IAArB;QAEE,kBAAa,GAAkB;YAC7B,QAAQ,EAAE,EAAE;YACZ,OAAO,EAAE,CAAC;SACX,CAAA;QAED,oBAAe,GAA+B,IAAI,GAAG,EAAE,CAAA;QAEvD,cAAS,GAAwB,IAAI,GAAG,EAAE,CAAA;KAsD3C;;;;;IAhDS,QAAQ,CAAC,EAAU;QACzB,IAAI,CAAC,IAAI,CAAC,aAAa,CAAC,QAAQ,EAAE;YAChC,OAAO,KAAK,CAAA;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AAC,MAAM,EAAE,GAAG,OAAO,CAAC,OAAO,EAAE,CAAA;KACzE;;;;;"}
1
+ {"version":3,"file":"hocuspocus-throttle.esm.js","sources":["../src/index.ts"],"sourcesContent":["import {\n Extension,\n onConnectPayload,\n} from '@hocuspocus/server'\n\nexport interface ThrottleConfiguration {\n throttle: number | null | false,\n banTime: number,\n}\n\nexport class Throttle implements Extension {\n\n configuration: ThrottleConfiguration = {\n throttle: 15,\n banTime: 5,\n }\n\n connectionsByIp: Map<string, Array<number>> = new Map()\n\n bannedIps: Map<string, number> = new Map()\n\n /**\n * Constructor\n */\n constructor(configuration?: Partial<ThrottleConfiguration>) {\n this.configuration = {\n ...this.configuration,\n ...configuration,\n }\n }\n\n /**\n * Throttle requests\n * @private\n */\n private throttle(ip: string): Boolean {\n if (!this.configuration.throttle) {\n return false\n }\n\n const bannedAt = this.bannedIps.get(ip) || 0\n\n if (Date.now() < (bannedAt + (this.configuration.banTime * 60 * 1000))) {\n return true\n }\n\n this.bannedIps.delete(ip)\n\n // add this connection try to the list of previous connections\n const previousConnections = this.connectionsByIp.get(ip) || []\n previousConnections.push(Date.now())\n\n // calculate the previous connections in the last minute\n const previousConnectionsInTheLastMinute = previousConnections\n .filter(timestamp => timestamp + (60 * 1000) > Date.now())\n\n this.connectionsByIp.set(ip, previousConnectionsInTheLastMinute)\n\n if (previousConnectionsInTheLastMinute.length > this.configuration.throttle) {\n this.bannedIps.set(ip, Date.now())\n return true\n }\n\n return false\n }\n\n /**\n * onConnect hook\n * @param data\n */\n onConnect(data: onConnectPayload): Promise<any> {\n const { request } = data\n\n // get the remote ip address\n const ip = request.headers['x-real-ip']\n || request.headers['x-forwarded-for']\n || request.socket.remoteAddress\n || ''\n\n // throttle the connection\n return this.throttle(<string> ip) ? Promise.reject() : Promise.resolve()\n }\n\n}\n"],"names":[],"mappings":"MAUa,QAAQ;;;;IAcnB,YAAY,aAA8C;QAZ1D,kBAAa,GAA0B;YACrC,QAAQ,EAAE,EAAE;YACZ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,KAAK,CAAA;KACb;;;;;IAMD,SAAS,CAAC,IAAsB;QAC9B,MAAM,EAAE,OAAO,EAAE,GAAG,IAAI,CAAA;;QAGxB,MAAM,EAAE,GAAG,OAAO,CAAC,OAAO,CAAC,WAAW,CAAC;eAClC,OAAO,CAAC,OAAO,CAAC,iBAAiB,CAAC;eAClC,OAAO,CAAC,MAAM,CAAC,aAAa;eAC5B,EAAE,CAAA;;QAGP,OAAO,IAAI,CAAC,QAAQ,CAAU,EAAE,CAAC,GAAG,OAAO,CAAC,MAAM,EAAE,GAAG,OAAO,CAAC,OAAO,EAAE,CAAA;KACzE;;;;;"}
@@ -0,0 +1,3 @@
1
+ export declare const awarenessStatesToArray: (states: Map<number, Record<string, any>>) => {
2
+ clientId: number;
3
+ }[];
@@ -1,2 +1,4 @@
1
1
  export * from './auth';
2
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
+ }
@@ -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>;
@@ -1,12 +1,16 @@
1
1
  import { Extension, onConnectPayload } from '@hocuspocus/server';
2
- export interface Configuration {
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: 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;
@@ -1,4 +1,3 @@
1
1
  export * from './HocuspocusProvider';
2
2
  export * from './HocuspocusCloudProvider';
3
3
  export * from './types';
4
- export * from './utils';
@@ -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/extension-throttle",
3
- "version": "1.0.0-alpha.48",
3
+ "version": "1.0.0-alpha.52",
4
4
  "description": "hocuspocus throttle extension",
5
5
  "homepage": "https://hocuspocus.dev",
6
6
  "keywords": [
@@ -27,7 +27,7 @@
27
27
  "dist"
28
28
  ],
29
29
  "dependencies": {
30
- "@hocuspocus/server": "^1.0.0-alpha.92"
30
+ "@hocuspocus/server": "^1.0.0-alpha.96"
31
31
  },
32
- "gitHead": "125d94dc3520bb5185293c9b316f66ddd9215992"
32
+ "gitHead": "771a8cbad1016dd12080ff4b35fabb9680ec69a7"
33
33
  }
package/src/index.ts CHANGED
@@ -3,14 +3,14 @@ import {
3
3
  onConnectPayload,
4
4
  } from '@hocuspocus/server'
5
5
 
6
- export interface Configuration {
6
+ export interface ThrottleConfiguration {
7
7
  throttle: number | null | false,
8
8
  banTime: number,
9
9
  }
10
10
 
11
11
  export class Throttle implements Extension {
12
12
 
13
- configuration: Configuration = {
13
+ configuration: ThrottleConfiguration = {
14
14
  throttle: 15,
15
15
  banTime: 5,
16
16
  }
@@ -19,6 +19,16 @@ export class Throttle implements Extension {
19
19
 
20
20
  bannedIps: Map<string, number> = new Map()
21
21
 
22
+ /**
23
+ * Constructor
24
+ */
25
+ constructor(configuration?: Partial<ThrottleConfiguration>) {
26
+ this.configuration = {
27
+ ...this.configuration,
28
+ ...configuration,
29
+ }
30
+ }
31
+
22
32
  /**
23
33
  * Throttle requests
24
34
  * @private
@@ -1,4 +0,0 @@
1
- declare const _default: (states: Map<number, Record<string, any>>) => {
2
- clientId: number;
3
- }[];
4
- export default _default;
@@ -1 +0,0 @@
1
- export { default as awarenessStatesToArray } from './awarenessStatesToArray';