kuzzle 2.19.2 → 2.19.3

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 (289) hide show
  1. package/lib/api/controllers/adminController.js +94 -80
  2. package/lib/api/controllers/authController.js +239 -212
  3. package/lib/api/controllers/baseController.js +89 -51
  4. package/lib/api/controllers/bulkController.js +62 -49
  5. package/lib/api/controllers/clusterController.js +6 -8
  6. package/lib/api/controllers/collectionController.js +140 -129
  7. package/lib/api/controllers/debugController.d.ts +2 -2
  8. package/lib/api/controllers/debugController.js +33 -31
  9. package/lib/api/controllers/documentController.js +365 -274
  10. package/lib/api/controllers/index.js +13 -13
  11. package/lib/api/controllers/indexController.js +46 -50
  12. package/lib/api/controllers/memoryStorageController.js +410 -360
  13. package/lib/api/controllers/realtimeController.js +37 -36
  14. package/lib/api/controllers/securityController.js +553 -412
  15. package/lib/api/controllers/serverController.js +111 -104
  16. package/lib/api/documentExtractor.js +75 -68
  17. package/lib/api/funnel.js +411 -312
  18. package/lib/api/httpRoutes.js +1493 -324
  19. package/lib/api/openapi/OpenApiManager.d.ts +1 -1
  20. package/lib/api/openapi/OpenApiManager.js +22 -22
  21. package/lib/api/openapi/components/document/count.yaml +1 -1
  22. package/lib/api/openapi/components/document/create.yaml +2 -2
  23. package/lib/api/openapi/components/document/delete.yaml +1 -1
  24. package/lib/api/openapi/components/document/deleteByQuery.yaml +1 -1
  25. package/lib/api/openapi/components/document/exists.yaml +1 -1
  26. package/lib/api/openapi/components/document/get.yaml +2 -2
  27. package/lib/api/openapi/components/document/index.js +12 -12
  28. package/lib/api/openapi/components/document/replace.yaml +1 -1
  29. package/lib/api/openapi/components/document/scroll.yaml +1 -1
  30. package/lib/api/openapi/components/document/validate.yaml +1 -1
  31. package/lib/api/openapi/components/index.d.ts +2 -2
  32. package/lib/api/openapi/components/index.js +1 -1
  33. package/lib/api/openapi/components/security/index.js +1 -1
  34. package/lib/api/openapi/components/security/upsertUser.yaml +2 -3
  35. package/lib/api/openapi/index.d.ts +1 -1
  36. package/lib/api/openapi/openApiGenerator.d.ts +1 -1
  37. package/lib/api/openapi/openApiGenerator.js +7 -7
  38. package/lib/api/rateLimiter.js +12 -13
  39. package/lib/api/request/index.d.ts +4 -4
  40. package/lib/api/request/kuzzleRequest.d.ts +9 -9
  41. package/lib/api/request/kuzzleRequest.js +89 -87
  42. package/lib/api/request/requestContext.d.ts +2 -2
  43. package/lib/api/request/requestContext.js +17 -17
  44. package/lib/api/request/requestInput.d.ts +1 -1
  45. package/lib/api/request/requestInput.js +19 -19
  46. package/lib/api/request/requestResponse.d.ts +4 -4
  47. package/lib/api/request/requestResponse.js +31 -33
  48. package/lib/cluster/command.js +48 -44
  49. package/lib/cluster/idCardHandler.d.ts +1 -1
  50. package/lib/cluster/idCardHandler.js +15 -15
  51. package/lib/cluster/index.js +2 -2
  52. package/lib/cluster/node.js +301 -269
  53. package/lib/cluster/publisher.js +45 -46
  54. package/lib/cluster/state.d.ts +5 -5
  55. package/lib/cluster/state.js +8 -8
  56. package/lib/cluster/subscriber.js +163 -113
  57. package/lib/cluster/workers/IDCardRenewer.js +33 -32
  58. package/lib/config/default.config.d.ts +1 -1
  59. package/lib/config/default.config.js +212 -171
  60. package/lib/config/documentEventAliases.js +6 -6
  61. package/lib/config/index.js +161 -98
  62. package/lib/config/sdkCompatibility.json +8 -8
  63. package/lib/core/auth/formatProcessing.js +7 -7
  64. package/lib/core/auth/passportResponse.js +7 -7
  65. package/lib/core/auth/passportWrapper.js +34 -30
  66. package/lib/core/auth/tokenManager.d.ts +2 -2
  67. package/lib/core/auth/tokenManager.js +11 -10
  68. package/lib/core/backend/applicationManager.d.ts +1 -1
  69. package/lib/core/backend/applicationManager.js +2 -2
  70. package/lib/core/backend/backend.d.ts +3 -3
  71. package/lib/core/backend/backend.js +34 -31
  72. package/lib/core/backend/backendCluster.d.ts +2 -2
  73. package/lib/core/backend/backendCluster.js +5 -5
  74. package/lib/core/backend/backendConfig.d.ts +2 -2
  75. package/lib/core/backend/backendConfig.js +3 -3
  76. package/lib/core/backend/backendController.d.ts +2 -2
  77. package/lib/core/backend/backendController.js +9 -10
  78. package/lib/core/backend/backendErrors.d.ts +3 -3
  79. package/lib/core/backend/backendErrors.js +2 -1
  80. package/lib/core/backend/backendHook.d.ts +2 -2
  81. package/lib/core/backend/backendHook.js +5 -5
  82. package/lib/core/backend/backendImport.d.ts +3 -3
  83. package/lib/core/backend/backendImport.js +23 -23
  84. package/lib/core/backend/backendOpenApi.d.ts +2 -2
  85. package/lib/core/backend/backendOpenApi.js +16 -16
  86. package/lib/core/backend/backendPipe.d.ts +2 -2
  87. package/lib/core/backend/backendPipe.js +6 -6
  88. package/lib/core/backend/backendPlugin.d.ts +4 -4
  89. package/lib/core/backend/backendPlugin.js +14 -14
  90. package/lib/core/backend/backendStorage.d.ts +2 -2
  91. package/lib/core/backend/backendStorage.js +1 -2
  92. package/lib/core/backend/backendVault.d.ts +2 -2
  93. package/lib/core/backend/backendVault.js +3 -3
  94. package/lib/core/backend/index.d.ts +14 -14
  95. package/lib/core/backend/internalLogger.d.ts +1 -1
  96. package/lib/core/backend/internalLogger.js +5 -5
  97. package/lib/core/cache/cacheDbEnum.js +4 -4
  98. package/lib/core/cache/cacheEngine.js +79 -85
  99. package/lib/core/network/accessLogger.js +126 -120
  100. package/lib/core/network/clientConnection.js +5 -5
  101. package/lib/core/network/context.js +8 -8
  102. package/lib/core/network/entryPoint.js +100 -85
  103. package/lib/core/network/httpRouter/index.js +63 -60
  104. package/lib/core/network/httpRouter/routeHandler.js +18 -19
  105. package/lib/core/network/httpRouter/routePart.js +23 -19
  106. package/lib/core/network/protocolManifest.js +3 -3
  107. package/lib/core/network/protocols/httpMessage.js +8 -10
  108. package/lib/core/network/protocols/httpwsProtocol.js +305 -250
  109. package/lib/core/network/protocols/internalProtocol.js +27 -24
  110. package/lib/core/network/protocols/mqttProtocol.js +106 -96
  111. package/lib/core/network/protocols/protocol.js +20 -17
  112. package/lib/core/network/router.js +56 -46
  113. package/lib/core/plugin/plugin.js +151 -120
  114. package/lib/core/plugin/pluginContext.d.ts +7 -7
  115. package/lib/core/plugin/pluginContext.js +48 -44
  116. package/lib/core/plugin/pluginManifest.js +13 -12
  117. package/lib/core/plugin/pluginRepository.js +26 -27
  118. package/lib/core/plugin/pluginsManager.js +425 -304
  119. package/lib/core/plugin/privilegedContext.js +3 -3
  120. package/lib/core/realtime/actionEnum.js +1 -1
  121. package/lib/core/realtime/channel.d.ts +1 -1
  122. package/lib/core/realtime/channel.js +22 -22
  123. package/lib/core/realtime/connectionRooms.d.ts +1 -1
  124. package/lib/core/realtime/hotelClerk.d.ts +2 -2
  125. package/lib/core/realtime/hotelClerk.js +53 -50
  126. package/lib/core/realtime/index.js +5 -5
  127. package/lib/core/realtime/notification/document.js +25 -25
  128. package/lib/core/realtime/notification/index.js +4 -4
  129. package/lib/core/realtime/notification/server.js +3 -3
  130. package/lib/core/realtime/notification/user.js +4 -4
  131. package/lib/core/realtime/notifier.js +113 -75
  132. package/lib/core/realtime/room.d.ts +1 -1
  133. package/lib/core/realtime/subscription.d.ts +1 -1
  134. package/lib/core/realtime/subscription.js +1 -1
  135. package/lib/core/security/index.js +8 -8
  136. package/lib/core/security/profileRepository.d.ts +6 -6
  137. package/lib/core/security/profileRepository.js +48 -45
  138. package/lib/core/security/roleRepository.js +127 -115
  139. package/lib/core/security/securityLoader.js +70 -63
  140. package/lib/core/security/tokenRepository.js +132 -118
  141. package/lib/core/security/userRepository.js +104 -88
  142. package/lib/core/shared/KoncordeWrapper.d.ts +1 -1
  143. package/lib/core/shared/KoncordeWrapper.js +3 -1
  144. package/lib/core/shared/abstractManifest.js +22 -23
  145. package/lib/core/shared/repository.js +69 -67
  146. package/lib/core/shared/sdk/embeddedSdk.d.ts +2 -2
  147. package/lib/core/shared/sdk/embeddedSdk.js +36 -32
  148. package/lib/core/shared/sdk/funnelProtocol.d.ts +1 -1
  149. package/lib/core/shared/sdk/funnelProtocol.js +11 -11
  150. package/lib/core/shared/sdk/impersonatedSdk.js +19 -18
  151. package/lib/core/shared/store.js +127 -32
  152. package/lib/core/statistics/index.js +2 -2
  153. package/lib/core/statistics/statistics.js +99 -85
  154. package/lib/core/storage/clientAdapter.js +219 -136
  155. package/lib/core/storage/indexCache.js +3 -3
  156. package/lib/core/storage/storageEngine.js +10 -13
  157. package/lib/core/storage/storeScopeEnum.js +3 -3
  158. package/lib/core/validation/baseType.js +12 -10
  159. package/lib/core/validation/index.js +2 -2
  160. package/lib/core/validation/types/anything.js +4 -4
  161. package/lib/core/validation/types/boolean.js +7 -7
  162. package/lib/core/validation/types/date.js +165 -131
  163. package/lib/core/validation/types/email.js +18 -21
  164. package/lib/core/validation/types/enum.js +34 -21
  165. package/lib/core/validation/types/geoPoint.js +7 -7
  166. package/lib/core/validation/types/geoShape.js +148 -125
  167. package/lib/core/validation/types/integer.js +9 -9
  168. package/lib/core/validation/types/ipAddress.js +17 -19
  169. package/lib/core/validation/types/numeric.js +36 -29
  170. package/lib/core/validation/types/object.js +19 -19
  171. package/lib/core/validation/types/string.js +36 -29
  172. package/lib/core/validation/types/url.js +17 -19
  173. package/lib/core/validation/validation.js +422 -378
  174. package/lib/kerror/codes/1-services.json +7 -1
  175. package/lib/kerror/codes/4-plugin.json +2 -2
  176. package/lib/kerror/codes/index.js +85 -63
  177. package/lib/kerror/errors/badRequestError.d.ts +1 -1
  178. package/lib/kerror/errors/externalServiceError.d.ts +1 -1
  179. package/lib/kerror/errors/forbiddenError.d.ts +1 -1
  180. package/lib/kerror/errors/gatewayTimeoutError.d.ts +1 -1
  181. package/lib/kerror/errors/index.d.ts +15 -15
  182. package/lib/kerror/errors/internalError.d.ts +1 -1
  183. package/lib/kerror/errors/kuzzleError.d.ts +1 -1
  184. package/lib/kerror/errors/multipleErrorsError.d.ts +1 -1
  185. package/lib/kerror/errors/multipleErrorsError.js +1 -1
  186. package/lib/kerror/errors/notFoundError.d.ts +1 -1
  187. package/lib/kerror/errors/partialError.d.ts +1 -1
  188. package/lib/kerror/errors/partialError.js +1 -1
  189. package/lib/kerror/errors/pluginImplementationError.d.ts +1 -1
  190. package/lib/kerror/errors/pluginImplementationError.js +2 -1
  191. package/lib/kerror/errors/preconditionError.d.ts +1 -1
  192. package/lib/kerror/errors/serviceUnavailableError.d.ts +1 -1
  193. package/lib/kerror/errors/sizeLimitError.d.ts +1 -1
  194. package/lib/kerror/errors/tooManyRequestsError.d.ts +1 -1
  195. package/lib/kerror/errors/unauthorizedError.d.ts +1 -1
  196. package/lib/kerror/index.d.ts +3 -3
  197. package/lib/kerror/index.js +17 -16
  198. package/lib/kuzzle/dumpGenerator.js +130 -114
  199. package/lib/kuzzle/event/kuzzleEventEmitter.js +96 -70
  200. package/lib/kuzzle/event/pipeRunner.js +25 -24
  201. package/lib/kuzzle/event/waterfall.js +13 -15
  202. package/lib/kuzzle/index.js +2 -2
  203. package/lib/kuzzle/internalIndexHandler.js +80 -59
  204. package/lib/kuzzle/kuzzle.js +99 -99
  205. package/lib/kuzzle/kuzzleStateEnum.js +1 -1
  206. package/lib/kuzzle/log.js +23 -18
  207. package/lib/kuzzle/vault.js +34 -19
  208. package/lib/model/security/profile.d.ts +3 -3
  209. package/lib/model/security/profile.js +38 -37
  210. package/lib/model/security/rights.js +5 -5
  211. package/lib/model/security/role.d.ts +3 -3
  212. package/lib/model/security/role.js +25 -26
  213. package/lib/model/security/token.d.ts +1 -1
  214. package/lib/model/security/token.js +4 -4
  215. package/lib/model/security/user.d.ts +2 -2
  216. package/lib/model/security/user.js +9 -9
  217. package/lib/model/storage/apiKey.js +43 -33
  218. package/lib/model/storage/baseModel.js +49 -45
  219. package/lib/service/cache/redis.js +60 -55
  220. package/lib/service/service.js +17 -17
  221. package/lib/service/storage/elasticsearch.js +839 -755
  222. package/lib/service/storage/esWrapper.js +103 -86
  223. package/lib/service/storage/queryTranslator.js +52 -59
  224. package/lib/types/Controller.d.ts +3 -3
  225. package/lib/types/ControllerDefinition.d.ts +3 -3
  226. package/lib/types/DebugModule.d.ts +2 -2
  227. package/lib/types/DebugModule.js +1 -1
  228. package/lib/types/Global.d.ts +1 -1
  229. package/lib/types/HttpStream.d.ts +2 -1
  230. package/lib/types/HttpStream.js +7 -5
  231. package/lib/types/Kuzzle.d.ts +1 -1
  232. package/lib/types/KuzzleDocument.d.ts +1 -1
  233. package/lib/types/OpenApiDefinition.d.ts +1 -1
  234. package/lib/types/PasswordPolicy.d.ts +1 -1
  235. package/lib/types/Plugin.d.ts +6 -6
  236. package/lib/types/Plugin.js +2 -2
  237. package/lib/types/Policy.d.ts +1 -1
  238. package/lib/types/RequestPayload.d.ts +1 -1
  239. package/lib/types/ResponsePayload.d.ts +1 -1
  240. package/lib/types/Token.d.ts +1 -1
  241. package/lib/types/User.d.ts +1 -1
  242. package/lib/types/config/DumpConfiguration.d.ts +8 -8
  243. package/lib/types/config/HttpConfiguration.d.ts +1 -1
  244. package/lib/types/config/KuzzleConfiguration.d.ts +1 -1
  245. package/lib/types/config/LimitsConfiguration.d.ts +8 -8
  246. package/lib/types/config/PluginsConfiguration.d.ts +4 -4
  247. package/lib/types/config/SecurityConfiguration.d.ts +62 -62
  248. package/lib/types/config/ServerConfiguration.d.ts +55 -55
  249. package/lib/types/config/ServicesConfiguration.d.ts +2 -2
  250. package/lib/types/config/internalCache/InternalCacheRedisConfiguration.d.ts +10 -10
  251. package/lib/types/config/publicCache/PublicCacheRedisConfiguration.d.ts +3 -3
  252. package/lib/types/config/storageEngine/StorageEngineElasticsearchConfiguration.d.ts +194 -110
  253. package/lib/types/errors/ErrorDefinition.d.ts +1 -1
  254. package/lib/types/errors/ErrorDomains.d.ts +1 -1
  255. package/lib/types/index.d.ts +38 -38
  256. package/lib/types/realtime/RealtimeScope.d.ts +1 -1
  257. package/lib/types/realtime/RealtimeUsers.d.ts +1 -1
  258. package/lib/util/assertType.js +13 -11
  259. package/lib/util/async.d.ts +1 -0
  260. package/lib/util/async.js +61 -0
  261. package/lib/util/asyncStore.js +19 -21
  262. package/lib/util/bufferedPassThrough.d.ts +2 -2
  263. package/lib/util/bufferedPassThrough.js +4 -4
  264. package/lib/util/bytes.js +9 -13
  265. package/lib/util/crypto.js +1 -1
  266. package/lib/util/debug.js +5 -5
  267. package/lib/util/deprecate.js +24 -21
  268. package/lib/util/didYouMean.js +7 -7
  269. package/lib/util/dump-collection.d.ts +2 -2
  270. package/lib/util/dump-collection.js +26 -26
  271. package/lib/util/esRequest.d.ts +1 -0
  272. package/lib/util/esRequest.js +62 -0
  273. package/lib/util/extractFields.js +24 -25
  274. package/lib/util/inflector.js +5 -5
  275. package/lib/util/koncordeCompat.d.ts +2 -2
  276. package/lib/util/koncordeCompat.js +5 -5
  277. package/lib/util/memoize.js +3 -5
  278. package/lib/util/mutex.d.ts +19 -1
  279. package/lib/util/mutex.js +39 -12
  280. package/lib/util/name-generator.js +1331 -1331
  281. package/lib/util/promback.js +8 -10
  282. package/lib/util/readYamlFile.d.ts +1 -1
  283. package/lib/util/readYamlFile.js +1 -1
  284. package/lib/util/requestAssertions.js +34 -34
  285. package/lib/util/safeObject.js +5 -5
  286. package/lib/util/stackTrace.js +20 -22
  287. package/lib/util/wildcard.js +15 -15
  288. package/package.json +6 -6
  289. package/npm-shrinkwrap.json +0 -19422
@@ -35,12 +35,12 @@ class Inflector {
35
35
  * @returns kebab-case-string
36
36
  */
37
37
  static kebabCase(string) {
38
- return string
38
+ return (string
39
39
  // get all lowercase letters that are near to uppercase ones
40
- .replace(/([a-z])([A-Z])/g, '$1-$2')
40
+ .replace(/([a-z])([A-Z])/g, "$1-$2")
41
41
  // replace all spaces and low dash
42
- .replace(/[\s_]+/g, '-')
43
- .toLowerCase();
42
+ .replace(/[\s_]+/g, "-")
43
+ .toLowerCase());
44
44
  }
45
45
  /**
46
46
  * Returns a string with the first letter in uppercase
@@ -54,7 +54,7 @@ class Inflector {
54
54
  * Converts a string to PascalCase
55
55
  */
56
56
  static pascalCase(string) {
57
- return lodash_1.default.startCase(string).replace(/ /g, '');
57
+ return lodash_1.default.startCase(string).replace(/ /g, "");
58
58
  }
59
59
  /**
60
60
  * Converts a string to camelCase
@@ -1,5 +1,5 @@
1
- import { JSONObject } from 'kuzzle-sdk';
2
- import { Koncorde } from 'koncorde';
1
+ import { JSONObject } from "kuzzle-sdk";
2
+ import { Koncorde } from "koncorde";
3
3
  /**
4
4
  * Builds a Koncorde v4 index name from the old fashioned index+collection
5
5
  * arguments
@@ -26,7 +26,7 @@ exports.getIndexes = exports.getCollections = exports.koncordeTest = exports.fro
26
26
  */
27
27
  const lodash_1 = require("lodash");
28
28
  // No collision possible: "/" is forbidden in index (or collection) names
29
- const SEPARATOR = '/';
29
+ const SEPARATOR = "/";
30
30
  /**
31
31
  * Builds a Koncorde v4 index name from the old fashioned index+collection
32
32
  * arguments
@@ -84,8 +84,8 @@ function getCollections(koncorde, index) {
84
84
  const indexPrefix = `${index}${SEPARATOR}`;
85
85
  return koncorde
86
86
  .getIndexes()
87
- .filter(i => i.startsWith(indexPrefix))
88
- .map(i => i.substr(indexPrefix.length));
87
+ .filter((i) => i.startsWith(indexPrefix))
88
+ .map((i) => i.substr(indexPrefix.length));
89
89
  }
90
90
  exports.getCollections = getCollections;
91
91
  /**
@@ -98,8 +98,8 @@ exports.getCollections = getCollections;
98
98
  function getIndexes(koncorde) {
99
99
  const indexes = koncorde
100
100
  .getIndexes()
101
- .filter(i => i !== '(default)')
102
- .map(i => i.substr(0, i.indexOf(SEPARATOR)));
101
+ .filter((i) => i !== "(default)")
102
+ .map((i) => i.substr(0, i.indexOf(SEPARATOR)));
103
103
  return (0, lodash_1.uniq)(indexes);
104
104
  }
105
105
  exports.getIndexes = getIndexes;
@@ -19,13 +19,11 @@
19
19
  * limitations under the License.
20
20
  */
21
21
 
22
- 'use strict';
22
+ "use strict";
23
23
 
24
- function memoize (fn, resolver = null) {
24
+ function memoize(fn, resolver = null) {
25
25
  const memoized = (...args) => {
26
- const key = memoized.resolver !== null
27
- ? memoized.resolver(args)
28
- : args[0];
26
+ const key = memoized.resolver !== null ? memoized.resolver(args) : args[0];
29
27
 
30
28
  let result = memoized.cache.get(key);
31
29
 
@@ -1,4 +1,4 @@
1
- import '../types/Global';
1
+ import "../types/Global";
2
2
  /**
3
3
  * Mutex class options
4
4
  */
@@ -66,5 +66,23 @@ export declare class Mutex {
66
66
  * @return {Promise}
67
67
  */
68
68
  unlock(): Promise<void>;
69
+ /**
70
+ * Wait for the resource to be unlocked.
71
+ * Return true if it waited until the unlocking
72
+ * and false if it waited until the timeout
73
+ *
74
+ * @param {Object} [Options] - mutex options
75
+ * - `attemptDelay`: delay between 2 resource check (default: `this.attemptDelay`)
76
+ * - `timeout`: mutex wait acquisition timeout, in milliseconds (default: `this.timeout`)
77
+ * If -1, will try to acquire the lock indefinitely.
78
+ * If 0, locking will fail immediately if it cannot lock with
79
+ * its 1st attempt.
80
+ *
81
+ * @return {Promise.<boolean>} True if the ressource has been unlocked before the `timeout`
82
+ */
83
+ wait({ attemptDelay, timeout, }: {
84
+ attemptDelay?: number;
85
+ timeout?: number;
86
+ }): Promise<boolean>;
69
87
  get locked(): boolean;
70
88
  }
package/lib/util/mutex.js CHANGED
@@ -52,8 +52,8 @@ const bluebird_1 = __importDefault(require("bluebird"));
52
52
  const kerror = __importStar(require("../kerror"));
53
53
  const debug_1 = __importDefault(require("./debug"));
54
54
  require("../types/Global");
55
- const debug = (0, debug_1.default)('kuzzle:mutex');
56
- const fatal = kerror.wrap('core', 'fatal');
55
+ const debug = (0, debug_1.default)("kuzzle:mutex");
56
+ const fatal = kerror.wrap("core", "fatal");
57
57
  // LUA script for Redis: we want our mutexes to only delete the lock they
58
58
  // acquired. Prevents removing locks acquired by other processes if unlocking
59
59
  // occurs after the lock expires
@@ -98,7 +98,7 @@ class Mutex {
98
98
  constructor(resource, { attemptDelay = 200, timeout = -1, ttl = 5000 } = {}) {
99
99
  this._locked = false;
100
100
  this.resource = resource;
101
- this.mutexId = `${global.kuzzle.id}/${(0, crypto_1.randomBytes)(16).toString('hex')}`;
101
+ this.mutexId = `${global.kuzzle.id}/${(0, crypto_1.randomBytes)(16).toString("hex")}`;
102
102
  this.attemptDelay = attemptDelay;
103
103
  this.timeout = timeout;
104
104
  this.ttl = ttl;
@@ -112,21 +112,22 @@ class Mutex {
112
112
  */
113
113
  async lock() {
114
114
  if (this._locked) {
115
- throw fatal.get('assertion_failed', `resource "${this.resource}" already locked by this mutex (id: ${this.mutexId})`);
115
+ throw fatal.get("assertion_failed", `resource "${this.resource}" already locked by this mutex (id: ${this.mutexId})`);
116
116
  }
117
117
  let duration = 0;
118
118
  do {
119
- this._locked = await global.kuzzle.ask('core:cache:internal:store', this.resource, this.mutexId, { onlyIfNew: true, ttl: this.ttl });
119
+ this._locked = await global.kuzzle.ask("core:cache:internal:store", this.resource, this.mutexId, { onlyIfNew: true, ttl: this.ttl });
120
120
  duration += this.attemptDelay;
121
121
  if (!this._locked && (this.timeout === -1 || duration <= this.timeout)) {
122
122
  await bluebird_1.default.delay(this.attemptDelay);
123
123
  }
124
- } while (!this._locked && (this.timeout === -1 || duration <= this.timeout));
124
+ } while (!this._locked &&
125
+ (this.timeout === -1 || duration <= this.timeout));
125
126
  if (!this._locked) {
126
- debug('Failed to lock %s (mutex id: %s)', this.resource, this.mutexId);
127
+ debug("Failed to lock %s (mutex id: %s)", this.resource, this.mutexId);
127
128
  return false;
128
129
  }
129
- debug('Resource %s locked (mutex id: %s)', this.resource, this.mutexId);
130
+ debug("Resource %s locked (mutex id: %s)", this.resource, this.mutexId);
130
131
  return true;
131
132
  }
132
133
  /**
@@ -136,15 +137,41 @@ class Mutex {
136
137
  */
137
138
  async unlock() {
138
139
  if (!this._locked) {
139
- throw fatal.get('assertion_failed', `tried to unlock the resource "${this.resource}", which is not locked (mutex id: ${this.mutexId})`);
140
+ throw fatal.get("assertion_failed", `tried to unlock the resource "${this.resource}", which is not locked (mutex id: ${this.mutexId})`);
140
141
  }
141
142
  if (!delScriptRegistered) {
142
- await global.kuzzle.ask('core:cache:internal:script:define', 'delIfValueEqual', 1, delIfValueEqualLua);
143
+ await global.kuzzle.ask("core:cache:internal:script:define", "delIfValueEqual", 1, delIfValueEqualLua);
143
144
  delScriptRegistered = true;
144
145
  }
145
- await global.kuzzle.ask('core:cache:internal:script:execute', 'delIfValueEqual', this.resource, this.mutexId);
146
+ await global.kuzzle.ask("core:cache:internal:script:execute", "delIfValueEqual", this.resource, this.mutexId);
146
147
  this._locked = false;
147
- debug('Resource %s freed (mutex id: %s)', this.resource, this.mutexId);
148
+ debug("Resource %s freed (mutex id: %s)", this.resource, this.mutexId);
149
+ }
150
+ /**
151
+ * Wait for the resource to be unlocked.
152
+ * Return true if it waited until the unlocking
153
+ * and false if it waited until the timeout
154
+ *
155
+ * @param {Object} [Options] - mutex options
156
+ * - `attemptDelay`: delay between 2 resource check (default: `this.attemptDelay`)
157
+ * - `timeout`: mutex wait acquisition timeout, in milliseconds (default: `this.timeout`)
158
+ * If -1, will try to acquire the lock indefinitely.
159
+ * If 0, locking will fail immediately if it cannot lock with
160
+ * its 1st attempt.
161
+ *
162
+ * @return {Promise.<boolean>} True if the ressource has been unlocked before the `timeout`
163
+ */
164
+ async wait({ attemptDelay = this.attemptDelay, timeout = this.timeout, }) {
165
+ let duration = 0;
166
+ let isLocked = true;
167
+ do {
168
+ isLocked = await global.kuzzle.ask("core:cache:internal:get", this.resource);
169
+ duration += attemptDelay;
170
+ if (isLocked && (timeout === -1 || duration <= timeout)) {
171
+ await bluebird_1.default.delay(attemptDelay);
172
+ }
173
+ } while (isLocked && (timeout === -1 || duration <= timeout));
174
+ return !isLocked;
148
175
  }
149
176
  get locked() {
150
177
  return this._locked;