@hocuspocus/extension-throttle 3.4.3 → 3.4.5-rc.0

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 (142) hide show
  1. package/dist/hocuspocus-throttle.cjs +69 -86
  2. package/dist/hocuspocus-throttle.cjs.map +1 -1
  3. package/dist/hocuspocus-throttle.esm.js +68 -85
  4. package/dist/hocuspocus-throttle.esm.js.map +1 -1
  5. package/dist/index.d.ts +34 -0
  6. package/dist/index.js +69 -0
  7. package/package.json +4 -4
  8. package/dist/node_modules/@tiptap/pm/model/index.d.ts +0 -1
  9. package/dist/node_modules/@tiptap/pm/state/index.d.ts +0 -1
  10. package/dist/node_modules/@tiptap/pm/transform/index.d.ts +0 -1
  11. package/dist/node_modules/@tiptap/pm/view/index.d.ts +0 -1
  12. package/dist/packages/common/src/CloseEvents.d.ts +0 -29
  13. package/dist/packages/common/src/auth.d.ts +0 -12
  14. package/dist/packages/common/src/awarenessStatesToArray.d.ts +0 -3
  15. package/dist/packages/common/src/index.d.ts +0 -4
  16. package/dist/packages/common/src/types.d.ts +0 -10
  17. package/dist/packages/extension-database/src/Database.d.ts +0 -30
  18. package/dist/packages/extension-database/src/index.d.ts +0 -1
  19. package/dist/packages/extension-logger/src/Logger.d.ts +0 -67
  20. package/dist/packages/extension-logger/src/index.d.ts +0 -1
  21. package/dist/packages/extension-redis/src/Redis.d.ts +0 -124
  22. package/dist/packages/extension-redis/src/index.d.ts +0 -1
  23. package/dist/packages/extension-s3/src/S3.d.ts +0 -44
  24. package/dist/packages/extension-s3/src/index.d.ts +0 -1
  25. package/dist/packages/extension-sqlite/src/SQLite.d.ts +0 -27
  26. package/dist/packages/extension-sqlite/src/index.d.ts +0 -1
  27. package/dist/packages/extension-throttle/src/index.d.ts +0 -30
  28. package/dist/packages/extension-webhook/src/index.d.ts +0 -56
  29. package/dist/packages/provider/src/EventEmitter.d.ts +0 -9
  30. package/dist/packages/provider/src/HocuspocusProvider.d.ts +0 -116
  31. package/dist/packages/provider/src/HocuspocusProviderWebsocket.d.ts +0 -114
  32. package/dist/packages/provider/src/IncomingMessage.d.ts +0 -17
  33. package/dist/packages/provider/src/MessageReceiver.d.ts +0 -12
  34. package/dist/packages/provider/src/MessageSender.d.ts +0 -9
  35. package/dist/packages/provider/src/OutgoingMessage.d.ts +0 -9
  36. package/dist/packages/provider/src/OutgoingMessages/AuthenticationMessage.d.ts +0 -8
  37. package/dist/packages/provider/src/OutgoingMessages/AwarenessMessage.d.ts +0 -9
  38. package/dist/packages/provider/src/OutgoingMessages/CloseMessage.d.ts +0 -9
  39. package/dist/packages/provider/src/OutgoingMessages/QueryAwarenessMessage.d.ts +0 -9
  40. package/dist/packages/provider/src/OutgoingMessages/StatelessMessage.d.ts +0 -8
  41. package/dist/packages/provider/src/OutgoingMessages/SyncStepOneMessage.d.ts +0 -9
  42. package/dist/packages/provider/src/OutgoingMessages/SyncStepTwoMessage.d.ts +0 -9
  43. package/dist/packages/provider/src/OutgoingMessages/UpdateMessage.d.ts +0 -8
  44. package/dist/packages/provider/src/index.d.ts +0 -3
  45. package/dist/packages/provider/src/types.d.ts +0 -92
  46. package/dist/packages/server/src/ClientConnection.d.ts +0 -63
  47. package/dist/packages/server/src/Connection.d.ts +0 -71
  48. package/dist/packages/server/src/DirectConnection.d.ts +0 -14
  49. package/dist/packages/server/src/Document.d.ts +0 -92
  50. package/dist/packages/server/src/Hocuspocus.d.ts +0 -80
  51. package/dist/packages/server/src/IncomingMessage.d.ts +0 -25
  52. package/dist/packages/server/src/MessageReceiver.d.ts +0 -11
  53. package/dist/packages/server/src/OutgoingMessage.d.ts +0 -23
  54. package/dist/packages/server/src/Server.d.ts +0 -32
  55. package/dist/packages/server/src/index.d.ts +0 -9
  56. package/dist/packages/server/src/types.d.ts +0 -342
  57. package/dist/packages/server/src/util/debounce.d.ts +0 -6
  58. package/dist/packages/server/src/util/getParameters.d.ts +0 -6
  59. package/dist/packages/transformer/src/Prosemirror.d.ts +0 -11
  60. package/dist/packages/transformer/src/Tiptap.d.ts +0 -10
  61. package/dist/packages/transformer/src/index.d.ts +0 -3
  62. package/dist/packages/transformer/src/types.d.ts +0 -5
  63. package/dist/playground/backend/src/default.d.ts +0 -1
  64. package/dist/playground/backend/src/deno.d.ts +0 -1
  65. package/dist/playground/backend/src/express.d.ts +0 -1
  66. package/dist/playground/backend/src/hono.d.ts +0 -1
  67. package/dist/playground/backend/src/koa.d.ts +0 -1
  68. package/dist/playground/backend/src/load-document.d.ts +0 -1
  69. package/dist/playground/backend/src/redis.d.ts +0 -1
  70. package/dist/playground/backend/src/s3-redis.d.ts +0 -1
  71. package/dist/playground/backend/src/s3.d.ts +0 -1
  72. package/dist/playground/backend/src/slow.d.ts +0 -1
  73. package/dist/playground/backend/src/tiptapcollab.d.ts +0 -1
  74. package/dist/playground/backend/src/webhook.d.ts +0 -1
  75. package/dist/playground/frontend/app/SocketContext1.d.ts +0 -2
  76. package/dist/playground/frontend/app/SocketContext2.d.ts +0 -2
  77. package/dist/playground/frontend/next.config.d.ts +0 -3
  78. package/dist/tests/extension-database/fetch.d.ts +0 -1
  79. package/dist/tests/extension-logger/onListen.d.ts +0 -1
  80. package/dist/tests/extension-redis/onAwarenessChange.d.ts +0 -1
  81. package/dist/tests/extension-redis/onChange.d.ts +0 -1
  82. package/dist/tests/extension-redis/onStateless.d.ts +0 -1
  83. package/dist/tests/extension-redis/onStoreDocument.d.ts +0 -1
  84. package/dist/tests/extension-s3/fetch.d.ts +0 -1
  85. package/dist/tests/extension-throttle/banning.d.ts +0 -1
  86. package/dist/tests/extension-throttle/configuration.d.ts +0 -1
  87. package/dist/tests/provider/hasUnsyncedChanges.d.ts +0 -1
  88. package/dist/tests/provider/observe.d.ts +0 -1
  89. package/dist/tests/provider/observeDeep.d.ts +0 -1
  90. package/dist/tests/provider/onAuthenticated.d.ts +0 -1
  91. package/dist/tests/provider/onAuthenticationFailed.d.ts +0 -1
  92. package/dist/tests/provider/onAwarenessChange.d.ts +0 -1
  93. package/dist/tests/provider/onAwarenessUpdate.d.ts +0 -1
  94. package/dist/tests/provider/onClose.d.ts +0 -1
  95. package/dist/tests/provider/onConnect.d.ts +0 -1
  96. package/dist/tests/provider/onDisconnect.d.ts +0 -1
  97. package/dist/tests/provider/onMessage.d.ts +0 -1
  98. package/dist/tests/provider/onOpen.d.ts +0 -1
  99. package/dist/tests/provider/onStateless.d.ts +0 -1
  100. package/dist/tests/provider/onSynced.d.ts +0 -1
  101. package/dist/tests/providerwebsocket/configuration.d.ts +0 -1
  102. package/dist/tests/server/address.d.ts +0 -1
  103. package/dist/tests/server/afterLoadDocument.d.ts +0 -1
  104. package/dist/tests/server/afterStoreDocument.d.ts +0 -1
  105. package/dist/tests/server/afterUnloadDocument.d.ts +0 -1
  106. package/dist/tests/server/beforeBroadcastStateless.d.ts +0 -1
  107. package/dist/tests/server/beforeHandleMessage.d.ts +0 -1
  108. package/dist/tests/server/beforeSync.d.ts +0 -1
  109. package/dist/tests/server/beforeUnloadDocument.d.ts +0 -1
  110. package/dist/tests/server/closeConnections.d.ts +0 -1
  111. package/dist/tests/server/getConnectionsCount.d.ts +0 -1
  112. package/dist/tests/server/getDocumentsCount.d.ts +0 -1
  113. package/dist/tests/server/listen.d.ts +0 -1
  114. package/dist/tests/server/onAuthenticate.d.ts +0 -1
  115. package/dist/tests/server/onAwarenessUpdate.d.ts +0 -1
  116. package/dist/tests/server/onChange.d.ts +0 -1
  117. package/dist/tests/server/onClose.d.ts +0 -1
  118. package/dist/tests/server/onConfigure.d.ts +0 -1
  119. package/dist/tests/server/onConnect.d.ts +0 -1
  120. package/dist/tests/server/onDestroy.d.ts +0 -1
  121. package/dist/tests/server/onDisconnect.d.ts +0 -1
  122. package/dist/tests/server/onListen.d.ts +0 -1
  123. package/dist/tests/server/onLoadDocument.d.ts +0 -1
  124. package/dist/tests/server/onRequest.d.ts +0 -1
  125. package/dist/tests/server/onStateless.d.ts +0 -1
  126. package/dist/tests/server/onStoreDocument.d.ts +0 -1
  127. package/dist/tests/server/onTokenSync.d.ts +0 -1
  128. package/dist/tests/server/onUpgrade.d.ts +0 -1
  129. package/dist/tests/server/openDirectConnection.d.ts +0 -1
  130. package/dist/tests/server/websocketError.d.ts +0 -1
  131. package/dist/tests/transformer/TiptapTransformer.d.ts +0 -1
  132. package/dist/tests/utils/createDirectory.d.ts +0 -1
  133. package/dist/tests/utils/flushRedis.d.ts +0 -1
  134. package/dist/tests/utils/index.d.ts +0 -9
  135. package/dist/tests/utils/newHocuspocus.d.ts +0 -2
  136. package/dist/tests/utils/newHocuspocusProvider.d.ts +0 -3
  137. package/dist/tests/utils/newHocuspocusProviderWebsocket.d.ts +0 -4
  138. package/dist/tests/utils/randomInteger.d.ts +0 -1
  139. package/dist/tests/utils/redisConnectionSettings.d.ts +0 -4
  140. package/dist/tests/utils/removeDirectory.d.ts +0 -1
  141. package/dist/tests/utils/retryableAssertion.d.ts +0 -2
  142. package/dist/tests/utils/sleep.d.ts +0 -1
@@ -1,89 +1,72 @@
1
- 'use strict';
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
2
 
3
- class Throttle {
4
- /**
5
- * Constructor
6
- */
7
- constructor(configuration) {
8
- this.configuration = {
9
- throttle: 15,
10
- banTime: 5,
11
- consideredSeconds: 60,
12
- cleanupInterval: 90,
13
- };
14
- this.connectionsByIp = new Map();
15
- this.bannedIps = new Map();
16
- this.configuration = {
17
- ...this.configuration,
18
- ...configuration,
19
- };
20
- this.cleanupInterval = setInterval(this.clearMaps.bind(this), this.configuration.cleanupInterval * 1000);
21
- }
22
- onDestroy() {
23
- if (this.cleanupInterval) {
24
- clearInterval(this.cleanupInterval);
25
- }
26
- return Promise.resolve();
27
- }
28
- clearMaps() {
29
- this.connectionsByIp.forEach((value, key) => {
30
- const filteredValue = value.filter((timestamp) => timestamp + this.configuration.consideredSeconds * 1000 > Date.now());
31
- if (filteredValue.length) {
32
- this.connectionsByIp.set(key, filteredValue);
33
- }
34
- else {
35
- this.connectionsByIp.delete(key);
36
- }
37
- });
38
- this.bannedIps.forEach((value, key) => {
39
- if (!this.isBanned(key)) {
40
- this.bannedIps.delete(key);
41
- }
42
- });
43
- }
44
- isBanned(ip) {
45
- const bannedAt = this.bannedIps.get(ip) || 0;
46
- return Date.now() < bannedAt + this.configuration.banTime * 60 * 1000;
47
- }
48
- /**
49
- * Throttle requests
50
- * @private
51
- */
52
- throttle(ip) {
53
- if (!this.configuration.throttle) {
54
- return false;
55
- }
56
- if (this.isBanned(ip))
57
- return true;
58
- this.bannedIps.delete(ip);
59
- // add this connection try to the list of previous connections
60
- const previousConnections = this.connectionsByIp.get(ip) || [];
61
- previousConnections.push(Date.now());
62
- // calculate the previous connections in the last considered time interval
63
- const previousConnectionsInTheConsideredInterval = previousConnections.filter((timestamp) => timestamp + this.configuration.consideredSeconds * 1000 > Date.now());
64
- this.connectionsByIp.set(ip, previousConnectionsInTheConsideredInterval);
65
- if (previousConnectionsInTheConsideredInterval.length >
66
- this.configuration.throttle) {
67
- this.bannedIps.set(ip, Date.now());
68
- return true;
69
- }
70
- return false;
71
- }
72
- /**
73
- * onConnect hook
74
- * @param data
75
- */
76
- onConnect(data) {
77
- const { request } = data;
78
- // get the remote ip address
79
- const ip = request.headers["x-real-ip"] ||
80
- request.headers["x-forwarded-for"] ||
81
- request.socket.remoteAddress ||
82
- "";
83
- // throttle the connection
84
- return this.throttle(ip) ? Promise.reject() : Promise.resolve();
85
- }
86
- }
3
+ //#region packages/extension-throttle/src/index.ts
4
+ var Throttle = class {
5
+ /**
6
+ * Constructor
7
+ */
8
+ constructor(configuration) {
9
+ this.configuration = {
10
+ throttle: 15,
11
+ banTime: 5,
12
+ consideredSeconds: 60,
13
+ cleanupInterval: 90
14
+ };
15
+ this.connectionsByIp = /* @__PURE__ */ new Map();
16
+ this.bannedIps = /* @__PURE__ */ new Map();
17
+ this.configuration = {
18
+ ...this.configuration,
19
+ ...configuration
20
+ };
21
+ this.cleanupInterval = setInterval(this.clearMaps.bind(this), this.configuration.cleanupInterval * 1e3);
22
+ }
23
+ onDestroy() {
24
+ if (this.cleanupInterval) clearInterval(this.cleanupInterval);
25
+ return Promise.resolve();
26
+ }
27
+ clearMaps() {
28
+ this.connectionsByIp.forEach((value, key) => {
29
+ const filteredValue = value.filter((timestamp) => timestamp + this.configuration.consideredSeconds * 1e3 > Date.now());
30
+ if (filteredValue.length) this.connectionsByIp.set(key, filteredValue);
31
+ else this.connectionsByIp.delete(key);
32
+ });
33
+ this.bannedIps.forEach((value, key) => {
34
+ if (!this.isBanned(key)) this.bannedIps.delete(key);
35
+ });
36
+ }
37
+ isBanned(ip) {
38
+ const bannedAt = this.bannedIps.get(ip) || 0;
39
+ return Date.now() < bannedAt + this.configuration.banTime * 60 * 1e3;
40
+ }
41
+ /**
42
+ * Throttle requests
43
+ * @private
44
+ */
45
+ throttle(ip) {
46
+ if (!this.configuration.throttle) return false;
47
+ if (this.isBanned(ip)) return true;
48
+ this.bannedIps.delete(ip);
49
+ const previousConnections = this.connectionsByIp.get(ip) || [];
50
+ previousConnections.push(Date.now());
51
+ const previousConnectionsInTheConsideredInterval = previousConnections.filter((timestamp) => timestamp + this.configuration.consideredSeconds * 1e3 > Date.now());
52
+ this.connectionsByIp.set(ip, previousConnectionsInTheConsideredInterval);
53
+ if (previousConnectionsInTheConsideredInterval.length > this.configuration.throttle) {
54
+ this.bannedIps.set(ip, Date.now());
55
+ return true;
56
+ }
57
+ return false;
58
+ }
59
+ /**
60
+ * onConnect hook
61
+ * @param data
62
+ */
63
+ onConnect(data) {
64
+ const { request } = data;
65
+ const ip = request.headers["x-real-ip"] || request.headers["x-forwarded-for"] || request.socket.remoteAddress || "";
66
+ return this.throttle(ip) ? Promise.reject() : Promise.resolve();
67
+ }
68
+ };
87
69
 
70
+ //#endregion
88
71
  exports.Throttle = Throttle;
89
- //# sourceMappingURL=hocuspocus-throttle.cjs.map
72
+ //# sourceMappingURL=hocuspocus-throttle.cjs.map
@@ -1 +1 @@
1
-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,EAC1B;AACD,YAAA,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,EAAE,EAAE,IAAI,CAAC,GAAG,EAAE,CAAC;AAClC,YAAA,OAAO,IAAI;;AAGZ,QAAA,OAAO,KAAK;;AAGb;;;AAGG;AACH,IAAA,SAAS,CAAC,IAAsB,EAAA;AAC/B,QAAA,MAAM,EAAE,OAAO,EAAE,GAAG,IAAI;;AAGxB,QAAA,MAAM,EAAE,GACP,OAAO,CAAC,OAAO,CAAC,WAAW,CAAC;AAC5B,YAAA,OAAO,CAAC,OAAO,CAAC,iBAAiB,CAAC;YAClC,OAAO,CAAC,MAAM,CAAC,aAAa;AAC5B,YAAA,EAAE;;QAGH,OAAO,IAAI,CAAC,QAAQ,CAAS,EAAE,CAAC,GAAG,OAAO,CAAC,MAAM,EAAE,GAAG,OAAO,CAAC,OAAO,EAAE;;AAExE;;;;"}
1
+ {"version":3,"file":"hocuspocus-throttle.cjs","names":[],"sources":["../src/index.ts"],"sourcesContent":["import type { Extension, onConnectPayload } from \"@hocuspocus/server\";\n\nexport interface ThrottleConfiguration {\n\tthrottle: number | null | false; // how many requests within `consideredSeconds` until we're rejecting requests (setting this to 15 means the 16th request will be rejected)\n\tconsideredSeconds: number; // how many seconds to consider (default is last 60 seconds from the current connection attempt)\n\tbanTime: number; // for how long to ban after receiving too many requests (in minutes!)\n\tcleanupInterval: number; // how often to clean up the records of IPs (this won't delete ips that are still blocked or recent enough by `consideredSeconds`)\n}\n\nexport class Throttle implements Extension {\n\tconfiguration: ThrottleConfiguration = {\n\t\tthrottle: 15,\n\t\tbanTime: 5,\n\t\tconsideredSeconds: 60,\n\t\tcleanupInterval: 90,\n\t};\n\n\tconnectionsByIp: Map<string, Array<number>> = new Map();\n\n\tbannedIps: Map<string, number> = new Map();\n\n\tcleanupInterval?: NodeJS.Timeout;\n\n\t/**\n\t * Constructor\n\t */\n\tconstructor(configuration?: Partial<ThrottleConfiguration>) {\n\t\tthis.configuration = {\n\t\t\t...this.configuration,\n\t\t\t...configuration,\n\t\t};\n\n\t\tthis.cleanupInterval = setInterval(\n\t\t\tthis.clearMaps.bind(this),\n\t\t\tthis.configuration.cleanupInterval * 1000,\n\t\t);\n\t}\n\n\tonDestroy() {\n\t\tif (this.cleanupInterval) {\n\t\t\tclearInterval(this.cleanupInterval);\n\t\t}\n\n\t\treturn Promise.resolve();\n\t}\n\n\tpublic clearMaps() {\n\t\tthis.connectionsByIp.forEach((value, key) => {\n\t\t\tconst filteredValue = value.filter(\n\t\t\t\t(timestamp) =>\n\t\t\t\t\ttimestamp + this.configuration.consideredSeconds * 1000 > Date.now(),\n\t\t\t);\n\n\t\t\tif (filteredValue.length) {\n\t\t\t\tthis.connectionsByIp.set(key, filteredValue);\n\t\t\t} else {\n\t\t\t\tthis.connectionsByIp.delete(key);\n\t\t\t}\n\t\t});\n\n\t\tthis.bannedIps.forEach((value, key) => {\n\t\t\tif (!this.isBanned(key)) {\n\t\t\t\tthis.bannedIps.delete(key);\n\t\t\t}\n\t\t});\n\t}\n\n\tisBanned(ip: string) {\n\t\tconst bannedAt = this.bannedIps.get(ip) || 0;\n\t\treturn Date.now() < bannedAt + this.configuration.banTime * 60 * 1000;\n\t}\n\n\t/**\n\t * Throttle requests\n\t * @private\n\t */\n\tprivate throttle(ip: string): boolean {\n\t\tif (!this.configuration.throttle) {\n\t\t\treturn false;\n\t\t}\n\n\t\tif (this.isBanned(ip)) return true;\n\n\t\tthis.bannedIps.delete(ip);\n\n\t\t// add this connection try to the list of previous connections\n\t\tconst previousConnections = this.connectionsByIp.get(ip) || [];\n\t\tpreviousConnections.push(Date.now());\n\n\t\t// calculate the previous connections in the last considered time interval\n\t\tconst previousConnectionsInTheConsideredInterval =\n\t\t\tpreviousConnections.filter(\n\t\t\t\t(timestamp) =>\n\t\t\t\t\ttimestamp + this.configuration.consideredSeconds * 1000 > Date.now(),\n\t\t\t);\n\n\t\tthis.connectionsByIp.set(ip, previousConnectionsInTheConsideredInterval);\n\n\t\tif (\n\t\t\tpreviousConnectionsInTheConsideredInterval.length >\n\t\t\tthis.configuration.throttle\n\t\t) {\n\t\t\tthis.bannedIps.set(ip, Date.now());\n\t\t\treturn true;\n\t\t}\n\n\t\treturn false;\n\t}\n\n\t/**\n\t * onConnect hook\n\t * @param data\n\t */\n\tonConnect(data: onConnectPayload): Promise<any> {\n\t\tconst { request } = data;\n\n\t\t// get the remote ip address\n\t\tconst ip =\n\t\t\trequest.headers[\"x-real-ip\"] ||\n\t\t\trequest.headers[\"x-forwarded-for\"] ||\n\t\t\trequest.socket.remoteAddress ||\n\t\t\t\"\";\n\n\t\t// throttle the connection\n\t\treturn this.throttle(<string>ip) ? Promise.reject() : Promise.resolve();\n\t}\n}\n"],"mappings":";;;AASA,IAAa,WAAb,MAA2C;;;;CAiB1C,YAAY,eAAgD;uBAhBrB;GACtC,UAAU;GACV,SAAS;GACT,mBAAmB;GACnB,iBAAiB;GACjB;yCAE6C,IAAI,KAAK;mCAEtB,IAAI,KAAK;AAQzC,OAAK,gBAAgB;GACpB,GAAG,KAAK;GACR,GAAG;GACH;AAED,OAAK,kBAAkB,YACtB,KAAK,UAAU,KAAK,KAAK,EACzB,KAAK,cAAc,kBAAkB,IACrC;;CAGF,YAAY;AACX,MAAI,KAAK,gBACR,eAAc,KAAK,gBAAgB;AAGpC,SAAO,QAAQ,SAAS;;CAGzB,AAAO,YAAY;AAClB,OAAK,gBAAgB,SAAS,OAAO,QAAQ;GAC5C,MAAM,gBAAgB,MAAM,QAC1B,cACA,YAAY,KAAK,cAAc,oBAAoB,MAAO,KAAK,KAAK,CACrE;AAED,OAAI,cAAc,OACjB,MAAK,gBAAgB,IAAI,KAAK,cAAc;OAE5C,MAAK,gBAAgB,OAAO,IAAI;IAEhC;AAEF,OAAK,UAAU,SAAS,OAAO,QAAQ;AACtC,OAAI,CAAC,KAAK,SAAS,IAAI,CACtB,MAAK,UAAU,OAAO,IAAI;IAE1B;;CAGH,SAAS,IAAY;EACpB,MAAM,WAAW,KAAK,UAAU,IAAI,GAAG,IAAI;AAC3C,SAAO,KAAK,KAAK,GAAG,WAAW,KAAK,cAAc,UAAU,KAAK;;;;;;CAOlE,AAAQ,SAAS,IAAqB;AACrC,MAAI,CAAC,KAAK,cAAc,SACvB,QAAO;AAGR,MAAI,KAAK,SAAS,GAAG,CAAE,QAAO;AAE9B,OAAK,UAAU,OAAO,GAAG;EAGzB,MAAM,sBAAsB,KAAK,gBAAgB,IAAI,GAAG,IAAI,EAAE;AAC9D,sBAAoB,KAAK,KAAK,KAAK,CAAC;EAGpC,MAAM,6CACL,oBAAoB,QAClB,cACA,YAAY,KAAK,cAAc,oBAAoB,MAAO,KAAK,KAAK,CACrE;AAEF,OAAK,gBAAgB,IAAI,IAAI,2CAA2C;AAExE,MACC,2CAA2C,SAC3C,KAAK,cAAc,UAClB;AACD,QAAK,UAAU,IAAI,IAAI,KAAK,KAAK,CAAC;AAClC,UAAO;;AAGR,SAAO;;;;;;CAOR,UAAU,MAAsC;EAC/C,MAAM,EAAE,YAAY;EAGpB,MAAM,KACL,QAAQ,QAAQ,gBAChB,QAAQ,QAAQ,sBAChB,QAAQ,OAAO,iBACf;AAGD,SAAO,KAAK,SAAiB,GAAG,GAAG,QAAQ,QAAQ,GAAG,QAAQ,SAAS"}
@@ -1,87 +1,70 @@
1
- class Throttle {
2
- /**
3
- * Constructor
4
- */
5
- constructor(configuration) {
6
- this.configuration = {
7
- throttle: 15,
8
- banTime: 5,
9
- consideredSeconds: 60,
10
- cleanupInterval: 90,
11
- };
12
- this.connectionsByIp = new Map();
13
- this.bannedIps = new Map();
14
- this.configuration = {
15
- ...this.configuration,
16
- ...configuration,
17
- };
18
- this.cleanupInterval = setInterval(this.clearMaps.bind(this), this.configuration.cleanupInterval * 1000);
19
- }
20
- onDestroy() {
21
- if (this.cleanupInterval) {
22
- clearInterval(this.cleanupInterval);
23
- }
24
- return Promise.resolve();
25
- }
26
- clearMaps() {
27
- this.connectionsByIp.forEach((value, key) => {
28
- const filteredValue = value.filter((timestamp) => timestamp + this.configuration.consideredSeconds * 1000 > Date.now());
29
- if (filteredValue.length) {
30
- this.connectionsByIp.set(key, filteredValue);
31
- }
32
- else {
33
- this.connectionsByIp.delete(key);
34
- }
35
- });
36
- this.bannedIps.forEach((value, key) => {
37
- if (!this.isBanned(key)) {
38
- this.bannedIps.delete(key);
39
- }
40
- });
41
- }
42
- isBanned(ip) {
43
- const bannedAt = this.bannedIps.get(ip) || 0;
44
- return Date.now() < bannedAt + this.configuration.banTime * 60 * 1000;
45
- }
46
- /**
47
- * Throttle requests
48
- * @private
49
- */
50
- throttle(ip) {
51
- if (!this.configuration.throttle) {
52
- return false;
53
- }
54
- if (this.isBanned(ip))
55
- return true;
56
- this.bannedIps.delete(ip);
57
- // add this connection try to the list of previous connections
58
- const previousConnections = this.connectionsByIp.get(ip) || [];
59
- previousConnections.push(Date.now());
60
- // calculate the previous connections in the last considered time interval
61
- const previousConnectionsInTheConsideredInterval = previousConnections.filter((timestamp) => timestamp + this.configuration.consideredSeconds * 1000 > Date.now());
62
- this.connectionsByIp.set(ip, previousConnectionsInTheConsideredInterval);
63
- if (previousConnectionsInTheConsideredInterval.length >
64
- this.configuration.throttle) {
65
- this.bannedIps.set(ip, Date.now());
66
- return true;
67
- }
68
- return false;
69
- }
70
- /**
71
- * onConnect hook
72
- * @param data
73
- */
74
- onConnect(data) {
75
- const { request } = data;
76
- // get the remote ip address
77
- const ip = request.headers["x-real-ip"] ||
78
- request.headers["x-forwarded-for"] ||
79
- request.socket.remoteAddress ||
80
- "";
81
- // throttle the connection
82
- return this.throttle(ip) ? Promise.reject() : Promise.resolve();
83
- }
84
- }
1
+ //#region packages/extension-throttle/src/index.ts
2
+ var Throttle = class {
3
+ /**
4
+ * Constructor
5
+ */
6
+ constructor(configuration) {
7
+ this.configuration = {
8
+ throttle: 15,
9
+ banTime: 5,
10
+ consideredSeconds: 60,
11
+ cleanupInterval: 90
12
+ };
13
+ this.connectionsByIp = /* @__PURE__ */ new Map();
14
+ this.bannedIps = /* @__PURE__ */ new Map();
15
+ this.configuration = {
16
+ ...this.configuration,
17
+ ...configuration
18
+ };
19
+ this.cleanupInterval = setInterval(this.clearMaps.bind(this), this.configuration.cleanupInterval * 1e3);
20
+ }
21
+ onDestroy() {
22
+ if (this.cleanupInterval) clearInterval(this.cleanupInterval);
23
+ return Promise.resolve();
24
+ }
25
+ clearMaps() {
26
+ this.connectionsByIp.forEach((value, key) => {
27
+ const filteredValue = value.filter((timestamp) => timestamp + this.configuration.consideredSeconds * 1e3 > Date.now());
28
+ if (filteredValue.length) this.connectionsByIp.set(key, filteredValue);
29
+ else this.connectionsByIp.delete(key);
30
+ });
31
+ this.bannedIps.forEach((value, key) => {
32
+ if (!this.isBanned(key)) this.bannedIps.delete(key);
33
+ });
34
+ }
35
+ isBanned(ip) {
36
+ const bannedAt = this.bannedIps.get(ip) || 0;
37
+ return Date.now() < bannedAt + this.configuration.banTime * 60 * 1e3;
38
+ }
39
+ /**
40
+ * Throttle requests
41
+ * @private
42
+ */
43
+ throttle(ip) {
44
+ if (!this.configuration.throttle) return false;
45
+ if (this.isBanned(ip)) return true;
46
+ this.bannedIps.delete(ip);
47
+ const previousConnections = this.connectionsByIp.get(ip) || [];
48
+ previousConnections.push(Date.now());
49
+ const previousConnectionsInTheConsideredInterval = previousConnections.filter((timestamp) => timestamp + this.configuration.consideredSeconds * 1e3 > Date.now());
50
+ this.connectionsByIp.set(ip, previousConnectionsInTheConsideredInterval);
51
+ if (previousConnectionsInTheConsideredInterval.length > this.configuration.throttle) {
52
+ this.bannedIps.set(ip, Date.now());
53
+ return true;
54
+ }
55
+ return false;
56
+ }
57
+ /**
58
+ * onConnect hook
59
+ * @param data
60
+ */
61
+ onConnect(data) {
62
+ const { request } = data;
63
+ const ip = request.headers["x-real-ip"] || request.headers["x-forwarded-for"] || request.socket.remoteAddress || "";
64
+ return this.throttle(ip) ? Promise.reject() : Promise.resolve();
65
+ }
66
+ };
85
67
 
68
+ //#endregion
86
69
  export { Throttle };
87
- //# sourceMappingURL=hocuspocus-throttle.esm.js.map
70
+ //# sourceMappingURL=hocuspocus-throttle.esm.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"hocuspocus-throttle.esm.js","sources":["../src/index.ts"],"sourcesContent":[null],"names":[],"mappings":"MASa,QAAQ,CAAA;AAcpB;;AAEG;AACH,IAAA,WAAA,CAAY,aAA8C,EAAA;AAhB1D,QAAA,IAAA,CAAA,aAAa,GAA0B;AACtC,YAAA,QAAQ,EAAE,EAAE;AACZ,YAAA,OAAO,EAAE,CAAC;AACV,YAAA,iBAAiB,EAAE,EAAE;AACrB,YAAA,eAAe,EAAE,EAAE;SACnB;AAED,QAAA,IAAA,CAAA,eAAe,GAA+B,IAAI,GAAG,EAAE;AAEvD,QAAA,IAAA,CAAA,SAAS,GAAwB,IAAI,GAAG,EAAE;QAQzC,IAAI,CAAC,aAAa,GAAG;YACpB,GAAG,IAAI,CAAC,aAAa;AACrB,YAAA,GAAG,aAAa;SAChB;QAED,IAAI,CAAC,eAAe,GAAG,WAAW,CACjC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC,EACzB,IAAI,CAAC,aAAa,CAAC,eAAe,GAAG,IAAI,CACzC;;IAGF,SAAS,GAAA;AACR,QAAA,IAAI,IAAI,CAAC,eAAe,EAAE;AACzB,YAAA,aAAa,CAAC,IAAI,CAAC,eAAe,CAAC;;AAGpC,QAAA,OAAO,OAAO,CAAC,OAAO,EAAE;;IAGlB,SAAS,GAAA;QACf,IAAI,CAAC,eAAe,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,GAAG,KAAI;YAC3C,MAAM,aAAa,GAAG,KAAK,CAAC,MAAM,CACjC,CAAC,SAAS,KACT,SAAS,GAAG,IAAI,CAAC,aAAa,CAAC,iBAAiB,GAAG,IAAI,GAAG,IAAI,CAAC,GAAG,EAAE,CACrE;AAED,YAAA,IAAI,aAAa,CAAC,MAAM,EAAE;gBACzB,IAAI,CAAC,eAAe,CAA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AC,EAAE,EAAE,0CAA0C,CAAC;QAExE,IACC,0CAA0C,CAAC,MAAM;AACjD,YAAA,IAAI,CAAC,aAAa,CAAC,QAAQ,EAC1B;AACD,YAAA,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,EAAE,EAAE,IAAI,CAAC,GAAG,EAAE,CAAC;AAClC,YAAA,OAAO,IAAI;;AAGZ,QAAA,OAAO,KAAK;;AAGb;;;AAGG;AACH,IAAA,SAAS,CAAC,IAAsB,EAAA;AAC/B,QAAA,MAAM,EAAE,OAAO,EAAE,GAAG,IAAI;;AAGxB,QAAA,MAAM,EAAE,GACP,OAAO,CAAC,OAAO,CAAC,WAAW,CAAC;AAC5B,YAAA,OAAO,CAAC,OAAO,CAAC,iBAAiB,CAAC;YAClC,OAAO,CAAC,MAAM,CAAC,aAAa;AAC5B,YAAA,EAAE;;QAGH,OAAO,IAAI,CAAC,QAAQ,CAAS,EAAE,CAAC,GAAG,OAAO,CAAC,MAAM,EAAE,GAAG,OAAO,CAAC,OAAO,EAAE;;AAExE;;;;"}
1
+ {"version":3,"file":"hocuspocus-throttle.esm.js","names":[],"sources":["../src/index.ts"],"sourcesContent":["import type { Extension, onConnectPayload } from \"@hocuspocus/server\";\n\nexport interface ThrottleConfiguration {\n\tthrottle: number | null | false; // how many requests within `consideredSeconds` until we're rejecting requests (setting this to 15 means the 16th request will be rejected)\n\tconsideredSeconds: number; // how many seconds to consider (default is last 60 seconds from the current connection attempt)\n\tbanTime: number; // for how long to ban after receiving too many requests (in minutes!)\n\tcleanupInterval: number; // how often to clean up the records of IPs (this won't delete ips that are still blocked or recent enough by `consideredSeconds`)\n}\n\nexport class Throttle implements Extension {\n\tconfiguration: ThrottleConfiguration = {\n\t\tthrottle: 15,\n\t\tbanTime: 5,\n\t\tconsideredSeconds: 60,\n\t\tcleanupInterval: 90,\n\t};\n\n\tconnectionsByIp: Map<string, Array<number>> = new Map();\n\n\tbannedIps: Map<string, number> = new Map();\n\n\tcleanupInterval?: NodeJS.Timeout;\n\n\t/**\n\t * Constructor\n\t */\n\tconstructor(configuration?: Partial<ThrottleConfiguration>) {\n\t\tthis.configuration = {\n\t\t\t...this.configuration,\n\t\t\t...configuration,\n\t\t};\n\n\t\tthis.cleanupInterval = setInterval(\n\t\t\tthis.clearMaps.bind(this),\n\t\t\tthis.configuration.cleanupInterval * 1000,\n\t\t);\n\t}\n\n\tonDestroy() {\n\t\tif (this.cleanupInterval) {\n\t\t\tclearInterval(this.cleanupInterval);\n\t\t}\n\n\t\treturn Promise.resolve();\n\t}\n\n\tpublic clearMaps() {\n\t\tthis.connectionsByIp.forEach((value, key) => {\n\t\t\tconst filteredValue = value.filter(\n\t\t\t\t(timestamp) =>\n\t\t\t\t\ttimestamp + this.configuration.consideredSeconds * 1000 > Date.now(),\n\t\t\t);\n\n\t\t\tif (filteredValue.length) {\n\t\t\t\tthis.connectionsByIp.set(key, filteredValue);\n\t\t\t} else {\n\t\t\t\tthis.connectionsByIp.delete(key);\n\t\t\t}\n\t\t});\n\n\t\tthis.bannedIps.forEach((value, key) => {\n\t\t\tif (!this.isBanned(key)) {\n\t\t\t\tthis.bannedIps.delete(key);\n\t\t\t}\n\t\t});\n\t}\n\n\tisBanned(ip: string) {\n\t\tconst bannedAt = this.bannedIps.get(ip) || 0;\n\t\treturn Date.now() < bannedAt + this.configuration.banTime * 60 * 1000;\n\t}\n\n\t/**\n\t * Throttle requests\n\t * @private\n\t */\n\tprivate throttle(ip: string): boolean {\n\t\tif (!this.configuration.throttle) {\n\t\t\treturn false;\n\t\t}\n\n\t\tif (this.isBanned(ip)) return true;\n\n\t\tthis.bannedIps.delete(ip);\n\n\t\t// add this connection try to the list of previous connections\n\t\tconst previousConnections = this.connectionsByIp.get(ip) || [];\n\t\tpreviousConnections.push(Date.now());\n\n\t\t// calculate the previous connections in the last considered time interval\n\t\tconst previousConnectionsInTheConsideredInterval =\n\t\t\tpreviousConnections.filter(\n\t\t\t\t(timestamp) =>\n\t\t\t\t\ttimestamp + this.configuration.consideredSeconds * 1000 > Date.now(),\n\t\t\t);\n\n\t\tthis.connectionsByIp.set(ip, previousConnectionsInTheConsideredInterval);\n\n\t\tif (\n\t\t\tpreviousConnectionsInTheConsideredInterval.length >\n\t\t\tthis.configuration.throttle\n\t\t) {\n\t\t\tthis.bannedIps.set(ip, Date.now());\n\t\t\treturn true;\n\t\t}\n\n\t\treturn false;\n\t}\n\n\t/**\n\t * onConnect hook\n\t * @param data\n\t */\n\tonConnect(data: onConnectPayload): Promise<any> {\n\t\tconst { request } = data;\n\n\t\t// get the remote ip address\n\t\tconst ip =\n\t\t\trequest.headers[\"x-real-ip\"] ||\n\t\t\trequest.headers[\"x-forwarded-for\"] ||\n\t\t\trequest.socket.remoteAddress ||\n\t\t\t\"\";\n\n\t\t// throttle the connection\n\t\treturn this.throttle(<string>ip) ? Promise.reject() : Promise.resolve();\n\t}\n}\n"],"mappings":";AASA,IAAa,WAAb,MAA2C;;;;CAiB1C,YAAY,eAAgD;uBAhBrB;GACtC,UAAU;GACV,SAAS;GACT,mBAAmB;GACnB,iBAAiB;GACjB;yCAE6C,IAAI,KAAK;mCAEtB,IAAI,KAAK;AAQzC,OAAK,gBAAgB;GACpB,GAAG,KAAK;GACR,GAAG;GACH;AAED,OAAK,kBAAkB,YACtB,KAAK,UAAU,KAAK,KAAK,EACzB,KAAK,cAAc,kBAAkB,IACrC;;CAGF,YAAY;AACX,MAAI,KAAK,gBACR,eAAc,KAAK,gBAAgB;AAGpC,SAAO,QAAQ,SAAS;;CAGzB,AAAO,YAAY;AAClB,OAAK,gBAAgB,SAAS,OAAO,QAAQ;GAC5C,MAAM,gBAAgB,MAAM,QAC1B,cACA,YAAY,KAAK,cAAc,oBAAoB,MAAO,KAAK,KAAK,CACrE;AAED,OAAI,cAAc,OACjB,MAAK,gBAAgB,IAAI,KAAK,cAAc;OAE5C,MAAK,gBAAgB,OAAO,IAAI;IAEhC;AAEF,OAAK,UAAU,SAAS,OAAO,QAAQ;AACtC,OAAI,CAAC,KAAK,SAAS,IAAI,CACtB,MAAK,UAAU,OAAO,IAAI;IAE1B;;CAGH,SAAS,IAAY;EACpB,MAAM,WAAW,KAAK,UAAU,IAAI,GAAG,IAAI;AAC3C,SAAO,KAAK,KAAK,GAAG,WAAW,KAAK,cAAc,UAAU,KAAK;;;;;;CAOlE,AAAQ,SAAS,IAAqB;AACrC,MAAI,CAAC,KAAK,cAAc,SACvB,QAAO;AAGR,MAAI,KAAK,SAAS,GAAG,CAAE,QAAO;AAE9B,OAAK,UAAU,OAAO,GAAG;EAGzB,MAAM,sBAAsB,KAAK,gBAAgB,IAAI,GAAG,IAAI,EAAE;AAC9D,sBAAoB,KAAK,KAAK,KAAK,CAAC;EAGpC,MAAM,6CACL,oBAAoB,QAClB,cACA,YAAY,KAAK,cAAc,oBAAoB,MAAO,KAAK,KAAK,CACrE;AAEF,OAAK,gBAAgB,IAAI,IAAI,2CAA2C;AAExE,MACC,2CAA2C,SAC3C,KAAK,cAAc,UAClB;AACD,QAAK,UAAU,IAAI,IAAI,KAAK,KAAK,CAAC;AAClC,UAAO;;AAGR,SAAO;;;;;;CAOR,UAAU,MAAsC;EAC/C,MAAM,EAAE,YAAY;EAGpB,MAAM,KACL,QAAQ,QAAQ,gBAChB,QAAQ,QAAQ,sBAChB,QAAQ,OAAO,iBACf;AAGD,SAAO,KAAK,SAAiB,GAAG,GAAG,QAAQ,QAAQ,GAAG,QAAQ,SAAS"}
@@ -0,0 +1,34 @@
1
+ import { Extension, onConnectPayload } from "@hocuspocus/server";
2
+
3
+ //#region packages/extension-throttle/src/index.d.ts
4
+ interface ThrottleConfiguration {
5
+ throttle: number | null | false;
6
+ consideredSeconds: number;
7
+ banTime: number;
8
+ cleanupInterval: number;
9
+ }
10
+ declare class Throttle implements Extension {
11
+ configuration: ThrottleConfiguration;
12
+ connectionsByIp: Map<string, Array<number>>;
13
+ bannedIps: Map<string, number>;
14
+ cleanupInterval?: NodeJS.Timeout;
15
+ /**
16
+ * Constructor
17
+ */
18
+ constructor(configuration?: Partial<ThrottleConfiguration>);
19
+ onDestroy(): Promise<void>;
20
+ clearMaps(): void;
21
+ isBanned(ip: string): boolean;
22
+ /**
23
+ * Throttle requests
24
+ * @private
25
+ */
26
+ private throttle;
27
+ /**
28
+ * onConnect hook
29
+ * @param data
30
+ */
31
+ onConnect(data: onConnectPayload): Promise<any>;
32
+ }
33
+ //#endregion
34
+ export { Throttle, ThrottleConfiguration };
package/dist/index.js ADDED
@@ -0,0 +1,69 @@
1
+ //#region packages/extension-throttle/src/index.ts
2
+ var Throttle = class {
3
+ /**
4
+ * Constructor
5
+ */
6
+ constructor(configuration) {
7
+ this.configuration = {
8
+ throttle: 15,
9
+ banTime: 5,
10
+ consideredSeconds: 60,
11
+ cleanupInterval: 90
12
+ };
13
+ this.connectionsByIp = /* @__PURE__ */ new Map();
14
+ this.bannedIps = /* @__PURE__ */ new Map();
15
+ this.configuration = {
16
+ ...this.configuration,
17
+ ...configuration
18
+ };
19
+ this.cleanupInterval = setInterval(this.clearMaps.bind(this), this.configuration.cleanupInterval * 1e3);
20
+ }
21
+ onDestroy() {
22
+ if (this.cleanupInterval) clearInterval(this.cleanupInterval);
23
+ return Promise.resolve();
24
+ }
25
+ clearMaps() {
26
+ this.connectionsByIp.forEach((value, key) => {
27
+ const filteredValue = value.filter((timestamp) => timestamp + this.configuration.consideredSeconds * 1e3 > Date.now());
28
+ if (filteredValue.length) this.connectionsByIp.set(key, filteredValue);
29
+ else this.connectionsByIp.delete(key);
30
+ });
31
+ this.bannedIps.forEach((value, key) => {
32
+ if (!this.isBanned(key)) this.bannedIps.delete(key);
33
+ });
34
+ }
35
+ isBanned(ip) {
36
+ const bannedAt = this.bannedIps.get(ip) || 0;
37
+ return Date.now() < bannedAt + this.configuration.banTime * 60 * 1e3;
38
+ }
39
+ /**
40
+ * Throttle requests
41
+ * @private
42
+ */
43
+ throttle(ip) {
44
+ if (!this.configuration.throttle) return false;
45
+ if (this.isBanned(ip)) return true;
46
+ this.bannedIps.delete(ip);
47
+ const previousConnections = this.connectionsByIp.get(ip) || [];
48
+ previousConnections.push(Date.now());
49
+ const previousConnectionsInTheConsideredInterval = previousConnections.filter((timestamp) => timestamp + this.configuration.consideredSeconds * 1e3 > Date.now());
50
+ this.connectionsByIp.set(ip, previousConnectionsInTheConsideredInterval);
51
+ if (previousConnectionsInTheConsideredInterval.length > this.configuration.throttle) {
52
+ this.bannedIps.set(ip, Date.now());
53
+ return true;
54
+ }
55
+ return false;
56
+ }
57
+ /**
58
+ * onConnect hook
59
+ * @param data
60
+ */
61
+ onConnect(data) {
62
+ const { request } = data;
63
+ const ip = request.headers["x-real-ip"] || request.headers["x-forwarded-for"] || request.socket.remoteAddress || "";
64
+ return this.throttle(ip) ? Promise.reject() : Promise.resolve();
65
+ }
66
+ };
67
+
68
+ //#endregion
69
+ export { Throttle };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hocuspocus/extension-throttle",
3
- "version": "3.4.3",
3
+ "version": "3.4.5-rc.0",
4
4
  "description": "hocuspocus throttle extension",
5
5
  "homepage": "https://hocuspocus.dev",
6
6
  "keywords": [
@@ -12,7 +12,7 @@
12
12
  "type": "module",
13
13
  "main": "dist/hocuspocus-throttle.cjs",
14
14
  "module": "dist/hocuspocus-throttle.esm.js",
15
- "types": "dist/packages/extension-throttle/src/index.d.ts",
15
+ "types": "dist/index.d.ts",
16
16
  "exports": {
17
17
  "source": {
18
18
  "import": "./src/index.ts"
@@ -20,7 +20,7 @@
20
20
  "default": {
21
21
  "import": "./dist/hocuspocus-throttle.esm.js",
22
22
  "require": "./dist/hocuspocus-throttle.cjs",
23
- "types": "./dist/packages/extension-throttle/src/index.d.ts"
23
+ "types": "./dist/index.d.ts"
24
24
  }
25
25
  },
26
26
  "files": [
@@ -28,7 +28,7 @@
28
28
  "dist"
29
29
  ],
30
30
  "dependencies": {
31
- "@hocuspocus/server": "^3.4.3"
31
+ "@hocuspocus/server": "^3.4.5-rc.0"
32
32
  },
33
33
  "gitHead": "b3454a4ca289a84ddfb7fa5607a2d4b8d5c37e9d",
34
34
  "repository": {
@@ -1 +0,0 @@
1
- export * from 'prosemirror-model';
@@ -1 +0,0 @@
1
- export * from 'prosemirror-state';
@@ -1 +0,0 @@
1
- export * from 'prosemirror-transform';
@@ -1 +0,0 @@
1
- export * from 'prosemirror-view';
@@ -1,29 +0,0 @@
1
- export interface CloseEvent {
2
- code: number;
3
- reason: string;
4
- }
5
- /**
6
- * The server is terminating the connection because a data frame was received
7
- * that is too large.
8
- * See: https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent/code
9
- */
10
- export declare const MessageTooBig: CloseEvent;
11
- /**
12
- * The server successfully processed the request, asks that the requester reset
13
- * its document view, and is not returning any content.
14
- */
15
- export declare const ResetConnection: CloseEvent;
16
- /**
17
- * Similar to Forbidden, but specifically for use when authentication is required and has
18
- * failed or has not yet been provided.
19
- */
20
- export declare const Unauthorized: CloseEvent;
21
- /**
22
- * The request contained valid data and was understood by the server, but the server
23
- * is refusing action.
24
- */
25
- export declare const Forbidden: CloseEvent;
26
- /**
27
- * The server timed out waiting for the request.
28
- */
29
- export declare const ConnectionTimeout: CloseEvent;
@@ -1,12 +0,0 @@
1
- import * as encoding from "lib0/encoding";
2
- import * as decoding from "lib0/decoding";
3
- export declare enum AuthMessageType {
4
- Token = 0,
5
- PermissionDenied = 1,
6
- Authenticated = 2
7
- }
8
- export declare const writeAuthentication: (encoder: encoding.Encoder, auth: string) => void;
9
- export declare const writePermissionDenied: (encoder: encoding.Encoder, reason: string) => void;
10
- export declare const writeAuthenticated: (encoder: encoding.Encoder, scope: "readonly" | "read-write") => void;
11
- export declare const writeTokenSyncRequest: (encoder: encoding.Encoder) => void;
12
- export declare const readAuthMessage: (decoder: decoding.Decoder, sendToken: () => void, permissionDeniedHandler: (reason: string) => void, authenticatedHandler: (scope: string) => void) => void;
@@ -1,3 +0,0 @@
1
- export declare const awarenessStatesToArray: (states: Map<number, Record<string, any>>) => {
2
- clientId: number;
3
- }[];
@@ -1,4 +0,0 @@
1
- export * from "./auth.ts";
2
- export * from "./CloseEvents.ts";
3
- export * from "./awarenessStatesToArray.ts";
4
- export * from "./types.ts";
@@ -1,10 +0,0 @@
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,30 +0,0 @@
1
- import type { 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) => Promise<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
- }
@@ -1 +0,0 @@
1
- export * from "./Database.ts";