kuzzle 2.56.0 → 2.57.0-beta.2

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 (408) hide show
  1. package/dist/bin/copy-binaries.js +71 -4
  2. package/dist/bin/start-kuzzle-server +191 -97
  3. package/dist/index.d.ts +31 -1
  4. package/dist/index.js +45 -2
  5. package/dist/lib/api/controllers/adminController.d.ts +4 -3
  6. package/dist/lib/api/controllers/adminController.js +1 -2
  7. package/dist/lib/api/controllers/authController.d.ts +37 -16
  8. package/dist/lib/api/controllers/authController.js +88 -39
  9. package/dist/lib/api/controllers/baseController.d.ts +10 -5
  10. package/dist/lib/api/controllers/baseController.js +36 -15
  11. package/dist/lib/api/controllers/bulkController.d.ts +11 -7
  12. package/dist/lib/api/controllers/bulkController.js +28 -5
  13. package/dist/lib/api/controllers/clusterController.d.ts +2 -2
  14. package/dist/lib/api/controllers/clusterController.js +3 -3
  15. package/dist/lib/api/controllers/collectionController.d.ts +57 -21
  16. package/dist/lib/api/controllers/collectionController.js +40 -11
  17. package/dist/lib/api/controllers/debugController.d.ts +1 -1
  18. package/dist/lib/api/controllers/documentController.d.ts +137 -28
  19. package/dist/lib/api/controllers/documentController.js +156 -125
  20. package/dist/lib/api/controllers/index.d.ts +27 -12
  21. package/dist/lib/api/controllers/index.js +28 -13
  22. package/dist/lib/api/controllers/indexController.d.ts +17 -10
  23. package/dist/lib/api/controllers/indexController.js +13 -11
  24. package/dist/lib/api/controllers/memoryStorageController.d.ts +2 -5
  25. package/dist/lib/api/controllers/memoryStorageController.js +199 -127
  26. package/dist/lib/api/controllers/realtimeController.d.ts +19 -9
  27. package/dist/lib/api/controllers/realtimeController.js +8 -4
  28. package/dist/lib/api/controllers/securityController.d.ts +42 -29
  29. package/dist/lib/api/controllers/securityController.js +35 -19
  30. package/dist/lib/api/controllers/serverController.d.ts +114 -19
  31. package/dist/lib/api/controllers/serverController.js +69 -29
  32. package/dist/lib/api/documentExtractor.d.ts +9 -7
  33. package/dist/lib/api/documentExtractor.js +105 -41
  34. package/dist/lib/api/funnel.d.ts +131 -29
  35. package/dist/lib/api/funnel.js +436 -225
  36. package/dist/lib/api/httpRoutes.d.ts +16 -11
  37. package/dist/lib/api/httpRoutes.js +272 -2
  38. package/dist/lib/api/openapi/OpenApiManager.d.ts +1 -1
  39. package/dist/lib/api/openapi/openApiGenerator.d.ts +1 -1
  40. package/dist/lib/api/rateLimiter.d.ts +15 -5
  41. package/dist/lib/api/rateLimiter.js +42 -14
  42. package/dist/lib/api/request/kuzzleRequest.d.ts +83 -12
  43. package/dist/lib/api/request/kuzzleRequest.js +88 -23
  44. package/dist/lib/api/request/requestContext.d.ts +20 -5
  45. package/dist/lib/api/request/requestContext.js +14 -4
  46. package/dist/lib/api/request/requestInput.d.ts +37 -6
  47. package/dist/lib/api/request/requestInput.js +22 -9
  48. package/dist/lib/api/request/requestResponse.d.ts +45 -9
  49. package/dist/lib/api/request/requestResponse.js +34 -3
  50. package/dist/lib/cluster/command.d.ts +131 -37
  51. package/dist/lib/cluster/command.js +243 -71
  52. package/dist/lib/cluster/idCardHandler.d.ts +18 -5
  53. package/dist/lib/cluster/idCardHandler.js +52 -19
  54. package/dist/lib/cluster/index.d.ts +8 -2
  55. package/dist/lib/cluster/index.js +6 -2
  56. package/dist/lib/cluster/node.d.ts +127 -49
  57. package/dist/lib/cluster/node.js +412 -179
  58. package/dist/lib/cluster/protobuf/command.proto +18 -0
  59. package/dist/lib/cluster/protobuf/commandMessages.d.ts +104 -0
  60. package/dist/lib/cluster/protobuf/commandMessages.js +30 -0
  61. package/dist/lib/cluster/protobuf/syncMessages.d.ts +184 -0
  62. package/dist/lib/cluster/protobuf/syncMessages.js +23 -0
  63. package/dist/lib/cluster/publisher.d.ts +118 -78
  64. package/dist/lib/cluster/publisher.js +174 -72
  65. package/dist/lib/cluster/state.d.ts +15 -9
  66. package/dist/lib/cluster/state.js +12 -8
  67. package/dist/lib/cluster/subscriber.d.ts +185 -131
  68. package/dist/lib/cluster/subscriber.js +357 -95
  69. package/dist/lib/cluster/workers/IDCardRenewer.d.ts +39 -8
  70. package/dist/lib/cluster/workers/IDCardRenewer.js +88 -23
  71. package/dist/lib/config/default.config.d.ts +2 -2
  72. package/dist/lib/config/default.config.js +10 -3
  73. package/dist/lib/config/documentEventAliases.d.ts +1 -1
  74. package/dist/lib/config/index.d.ts +2 -5
  75. package/dist/lib/config/index.js +30 -17
  76. package/dist/lib/core/auth/formatProcessing.d.ts +3 -3
  77. package/dist/lib/core/auth/passportResponse.d.ts +1 -1
  78. package/dist/lib/core/auth/passportWrapper.d.ts +1 -1
  79. package/dist/lib/core/auth/passportWrapper.js +14 -13
  80. package/dist/lib/core/auth/tokenManager.d.ts +24 -3
  81. package/dist/lib/core/auth/tokenManager.js +47 -19
  82. package/dist/lib/core/backend/applicationManager.d.ts +2 -2
  83. package/dist/lib/core/backend/backend.d.ts +16 -5
  84. package/dist/lib/core/backend/backend.js +14 -6
  85. package/dist/lib/core/backend/backendCluster.d.ts +1 -1
  86. package/dist/lib/core/backend/backendConfig.d.ts +17 -5
  87. package/dist/lib/core/backend/backendConfig.js +1 -0
  88. package/dist/lib/core/backend/backendController.d.ts +1 -1
  89. package/dist/lib/core/backend/backendController.js +8 -1
  90. package/dist/lib/core/backend/backendErrors.d.ts +15 -8
  91. package/dist/lib/core/backend/backendErrors.js +5 -0
  92. package/dist/lib/core/backend/backendHook.d.ts +1 -1
  93. package/dist/lib/core/backend/backendImport.d.ts +1 -1
  94. package/dist/lib/core/backend/backendOpenApi.d.ts +3 -2
  95. package/dist/lib/core/backend/backendPipe.d.ts +1 -1
  96. package/dist/lib/core/backend/backendPipe.js +9 -1
  97. package/dist/lib/core/backend/backendPlugin.d.ts +2 -2
  98. package/dist/lib/core/backend/backendSubscription.d.ts +2 -2
  99. package/dist/lib/core/backend/backendVault.d.ts +1 -1
  100. package/dist/lib/core/backend/backendVault.js +8 -1
  101. package/dist/lib/core/cache/cacheEngine.d.ts +7 -4
  102. package/dist/lib/core/cache/cacheEngine.js +25 -8
  103. package/dist/lib/core/debug/kuzzleDebugger.d.ts +2 -1
  104. package/dist/lib/core/debug/kuzzleDebugger.js +31 -17
  105. package/dist/lib/core/network/accessLogger.d.ts +33 -18
  106. package/dist/lib/core/network/accessLogger.js +135 -73
  107. package/dist/lib/core/network/clientConnection.d.ts +13 -11
  108. package/dist/lib/core/network/clientConnection.js +33 -9
  109. package/dist/lib/core/network/context.d.ts +17 -22
  110. package/dist/lib/core/network/context.js +43 -26
  111. package/dist/lib/core/network/entryPoint.d.ts +38 -16
  112. package/dist/lib/core/network/entryPoint.js +119 -48
  113. package/dist/lib/core/network/httpRouter/index.d.ts +20 -50
  114. package/dist/lib/core/network/httpRouter/index.js +61 -75
  115. package/dist/lib/core/network/httpRouter/routeHandler.d.ts +17 -32
  116. package/dist/lib/core/network/httpRouter/routeHandler.js +26 -36
  117. package/dist/lib/core/network/httpRouter/routePart.d.ts +9 -18
  118. package/dist/lib/core/network/httpRouter/routePart.js +61 -43
  119. package/dist/lib/core/network/httpRouter/routeTypes.d.ts +3 -0
  120. package/dist/lib/core/network/httpRouter/routeTypes.js +23 -0
  121. package/dist/lib/core/network/networkEntryPoint.d.ts +20 -0
  122. package/dist/lib/core/network/networkEntryPoint.js +23 -0
  123. package/dist/lib/core/network/protocolManifest.d.ts +5 -4
  124. package/dist/lib/core/network/protocolManifest.js +6 -3
  125. package/dist/lib/core/network/protocols/httpMessage.d.ts +25 -18
  126. package/dist/lib/core/network/protocols/httpMessage.js +14 -11
  127. package/dist/lib/core/network/protocols/httpwsProtocol.d.ts +84 -93
  128. package/dist/lib/core/network/protocols/httpwsProtocol.js +284 -129
  129. package/dist/lib/core/network/protocols/internalProtocol.d.ts +26 -15
  130. package/dist/lib/core/network/protocols/internalProtocol.js +17 -16
  131. package/dist/lib/core/network/protocols/mqttProtocol.d.ts +56 -32
  132. package/dist/lib/core/network/protocols/mqttProtocol.js +91 -44
  133. package/dist/lib/core/network/protocols/protocol.d.ts +85 -21
  134. package/dist/lib/core/network/protocols/protocol.js +83 -27
  135. package/dist/lib/core/network/router.d.ts +13 -22
  136. package/dist/lib/core/network/router.js +59 -46
  137. package/dist/lib/core/plugin/plugin.d.ts +57 -51
  138. package/dist/lib/core/plugin/plugin.js +218 -103
  139. package/dist/lib/core/plugin/pluginContext.d.ts +54 -11
  140. package/dist/lib/core/plugin/pluginContext.js +99 -58
  141. package/dist/lib/core/plugin/pluginManifest.d.ts +6 -2
  142. package/dist/lib/core/plugin/pluginManifest.js +18 -11
  143. package/dist/lib/core/plugin/pluginRepository.d.ts +35 -44
  144. package/dist/lib/core/plugin/pluginRepository.js +35 -50
  145. package/dist/lib/core/plugin/pluginsManager.d.ts +128 -46
  146. package/dist/lib/core/plugin/pluginsManager.js +468 -231
  147. package/dist/lib/core/plugin/privilegedContext.d.ts +4 -9
  148. package/dist/lib/core/plugin/privilegedContext.js +5 -10
  149. package/dist/lib/core/realtime/actionEnum.d.ts +12 -21
  150. package/dist/lib/core/realtime/actionEnum.js +6 -7
  151. package/dist/lib/core/realtime/channel.d.ts +11 -5
  152. package/dist/lib/core/realtime/channel.js +17 -2
  153. package/dist/lib/core/realtime/connectionRooms.d.ts +2 -2
  154. package/dist/lib/core/realtime/hotelClerk.d.ts +12 -6
  155. package/dist/lib/core/realtime/hotelClerk.js +29 -11
  156. package/dist/lib/core/realtime/index.d.ts +8 -3
  157. package/dist/lib/core/realtime/index.js +12 -5
  158. package/dist/lib/core/realtime/notification/document.d.ts +54 -25
  159. package/dist/lib/core/realtime/notification/document.js +10 -13
  160. package/dist/lib/core/realtime/notification/index.d.ts +9 -3
  161. package/dist/lib/core/realtime/notification/index.js +10 -4
  162. package/dist/lib/core/realtime/notification/server.d.ts +7 -10
  163. package/dist/lib/core/realtime/notification/server.js +4 -7
  164. package/dist/lib/core/realtime/notification/user.d.ts +45 -21
  165. package/dist/lib/core/realtime/notification/user.js +8 -9
  166. package/dist/lib/core/realtime/notifier.d.ts +52 -40
  167. package/dist/lib/core/realtime/notifier.js +60 -25
  168. package/dist/lib/core/realtime/room.d.ts +1 -1
  169. package/dist/lib/core/realtime/subscription.d.ts +11 -6
  170. package/dist/lib/core/realtime/subscription.js +5 -1
  171. package/dist/lib/core/security/index.d.ts +14 -6
  172. package/dist/lib/core/security/index.js +27 -13
  173. package/dist/lib/core/security/profileRepository.d.ts +24 -19
  174. package/dist/lib/core/security/profileRepository.js +23 -10
  175. package/dist/lib/core/security/roleRepository.d.ts +60 -23
  176. package/dist/lib/core/security/roleRepository.js +122 -53
  177. package/dist/lib/core/security/securityLoader.d.ts +33 -18
  178. package/dist/lib/core/security/securityLoader.js +43 -16
  179. package/dist/lib/core/security/tokenRepository.d.ts +21 -7
  180. package/dist/lib/core/security/tokenRepository.js +50 -23
  181. package/dist/lib/core/security/userRepository.d.ts +44 -21
  182. package/dist/lib/core/security/userRepository.js +59 -19
  183. package/dist/lib/core/shared/KoncordeWrapper.d.ts +1 -1
  184. package/dist/lib/core/shared/ObjectRepository.d.ts +80 -18
  185. package/dist/lib/core/shared/ObjectRepository.js +57 -29
  186. package/dist/lib/core/shared/abstractManifest.d.ts +21 -8
  187. package/dist/lib/core/shared/abstractManifest.js +57 -17
  188. package/dist/lib/core/shared/sdk/embeddedSdk.d.ts +3 -1
  189. package/dist/lib/core/shared/sdk/embeddedSdk.js +8 -7
  190. package/dist/lib/core/shared/sdk/funnelProtocol.d.ts +2 -1
  191. package/dist/lib/core/shared/sdk/funnelProtocol.js +12 -3
  192. package/dist/lib/core/shared/sdk/impersonatedSdk.d.ts +11 -5
  193. package/dist/lib/core/shared/sdk/impersonatedSdk.js +22 -9
  194. package/dist/lib/core/shared/store.d.ts +45 -29
  195. package/dist/lib/core/shared/store.js +48 -27
  196. package/dist/lib/core/statistics/statistics.d.ts +1 -1
  197. package/dist/lib/core/storage/clientAdapter.d.ts +39 -21
  198. package/dist/lib/core/storage/clientAdapter.js +88 -48
  199. package/dist/lib/core/storage/indexCache.d.ts +8 -0
  200. package/dist/lib/core/storage/indexCache.js +16 -7
  201. package/dist/lib/core/storage/storageEngine.d.ts +7 -6
  202. package/dist/lib/core/storage/storageEngine.js +11 -8
  203. package/dist/lib/core/validation/baseType.d.ts +22 -22
  204. package/dist/lib/core/validation/baseType.js +11 -26
  205. package/dist/lib/core/validation/specification.d.ts +148 -0
  206. package/dist/lib/core/validation/specification.js +34 -0
  207. package/dist/lib/core/validation/typeOptions.d.ts +62 -0
  208. package/dist/lib/core/validation/typeOptions.js +23 -0
  209. package/dist/lib/core/validation/types/anything.d.ts +3 -6
  210. package/dist/lib/core/validation/types/anything.js +7 -7
  211. package/dist/lib/core/validation/types/boolean.d.ts +5 -12
  212. package/dist/lib/core/validation/types/boolean.js +7 -12
  213. package/dist/lib/core/validation/types/date.d.ts +19 -11
  214. package/dist/lib/core/validation/types/date.js +212 -161
  215. package/dist/lib/core/validation/types/email.d.ts +7 -11
  216. package/dist/lib/core/validation/types/email.js +13 -17
  217. package/dist/lib/core/validation/types/enum.d.ts +7 -11
  218. package/dist/lib/core/validation/types/enum.js +39 -20
  219. package/dist/lib/core/validation/types/geoPoint.d.ts +5 -13
  220. package/dist/lib/core/validation/types/geoPoint.js +10 -15
  221. package/dist/lib/core/validation/types/geoShape.d.ts +22 -21
  222. package/dist/lib/core/validation/types/geoShape.js +113 -129
  223. package/dist/lib/core/validation/types/geoShapeUtils.d.ts +15 -0
  224. package/dist/lib/core/validation/types/geoShapeUtils.js +75 -0
  225. package/dist/lib/core/validation/types/integer.d.ts +7 -5
  226. package/dist/lib/core/validation/types/integer.js +7 -13
  227. package/dist/lib/core/validation/types/ipAddress.d.ts +7 -11
  228. package/dist/lib/core/validation/types/ipAddress.js +13 -17
  229. package/dist/lib/core/validation/types/numeric.d.ts +7 -11
  230. package/dist/lib/core/validation/types/numeric.js +22 -29
  231. package/dist/lib/core/validation/types/object.d.ts +8 -15
  232. package/dist/lib/core/validation/types/object.js +12 -21
  233. package/dist/lib/core/validation/types/string.d.ts +7 -11
  234. package/dist/lib/core/validation/types/string.js +22 -29
  235. package/dist/lib/core/validation/types/url.d.ts +7 -11
  236. package/dist/lib/core/validation/types/url.js +14 -18
  237. package/dist/lib/core/validation/validation.d.ts +119 -80
  238. package/dist/lib/core/validation/validation.js +470 -412
  239. package/dist/lib/core/validation/validationUtils.d.ts +31 -0
  240. package/dist/lib/core/validation/validationUtils.js +190 -0
  241. package/dist/lib/kerror/codes/4-plugin.json +0 -12
  242. package/dist/lib/kerror/codes/index.d.ts +12 -2
  243. package/dist/lib/kerror/codes/index.js +5 -4
  244. package/dist/lib/kerror/errors/badRequestError.d.ts +1 -1
  245. package/dist/lib/kerror/errors/badRequestError.js +1 -1
  246. package/dist/lib/kerror/errors/externalServiceError.d.ts +1 -1
  247. package/dist/lib/kerror/errors/externalServiceError.js +1 -1
  248. package/dist/lib/kerror/errors/forbiddenError.d.ts +1 -1
  249. package/dist/lib/kerror/errors/forbiddenError.js +1 -1
  250. package/dist/lib/kerror/errors/gatewayTimeoutError.d.ts +1 -1
  251. package/dist/lib/kerror/errors/gatewayTimeoutError.js +1 -1
  252. package/dist/lib/kerror/errors/internalError.d.ts +1 -1
  253. package/dist/lib/kerror/errors/internalError.js +1 -1
  254. package/dist/lib/kerror/errors/kuzzleError.d.ts +32 -3
  255. package/dist/lib/kerror/errors/kuzzleError.js +21 -7
  256. package/dist/lib/kerror/errors/multipleErrorsError.d.ts +2 -2
  257. package/dist/lib/kerror/errors/multipleErrorsError.js +2 -2
  258. package/dist/lib/kerror/errors/notFoundError.d.ts +1 -1
  259. package/dist/lib/kerror/errors/notFoundError.js +1 -1
  260. package/dist/lib/kerror/errors/partialError.d.ts +8 -2
  261. package/dist/lib/kerror/errors/partialError.js +22 -9
  262. package/dist/lib/kerror/errors/pluginImplementationError.d.ts +9 -1
  263. package/dist/lib/kerror/errors/pluginImplementationError.js +9 -3
  264. package/dist/lib/kerror/errors/preconditionError.d.ts +1 -1
  265. package/dist/lib/kerror/errors/preconditionError.js +1 -1
  266. package/dist/lib/kerror/errors/serviceUnavailableError.d.ts +1 -1
  267. package/dist/lib/kerror/errors/serviceUnavailableError.js +1 -1
  268. package/dist/lib/kerror/errors/sizeLimitError.d.ts +1 -1
  269. package/dist/lib/kerror/errors/sizeLimitError.js +1 -1
  270. package/dist/lib/kerror/errors/tooManyRequestsError.d.ts +1 -1
  271. package/dist/lib/kerror/errors/tooManyRequestsError.js +1 -1
  272. package/dist/lib/kerror/errors/unauthorizedError.d.ts +1 -1
  273. package/dist/lib/kerror/errors/unauthorizedError.js +1 -1
  274. package/dist/lib/kerror/index.d.ts +19 -13
  275. package/dist/lib/kerror/index.js +60 -19
  276. package/dist/lib/kuzzle/Logger.d.ts +2 -2
  277. package/dist/lib/kuzzle/dumpGenerator.d.ts +12 -8
  278. package/dist/lib/kuzzle/dumpGenerator.js +130 -62
  279. package/dist/lib/kuzzle/event/KuzzleEventEmitter.d.ts +18 -9
  280. package/dist/lib/kuzzle/event/KuzzleEventEmitter.js +45 -18
  281. package/dist/lib/kuzzle/event/pipeRunner.d.ts +37 -16
  282. package/dist/lib/kuzzle/event/pipeRunner.js +73 -26
  283. package/dist/lib/kuzzle/event/waterfall.d.ts +11 -1
  284. package/dist/lib/kuzzle/event/waterfall.js +38 -13
  285. package/dist/lib/kuzzle/internalIndexHandler.d.ts +11 -174
  286. package/dist/lib/kuzzle/internalIndexHandler.js +58 -29
  287. package/dist/lib/kuzzle/kuzzle.d.ts +25 -10
  288. package/dist/lib/kuzzle/kuzzle.js +74 -17
  289. package/dist/lib/kuzzle/kuzzleStateEnum.d.ts +16 -12
  290. package/dist/lib/kuzzle/kuzzleStateEnum.js +5 -7
  291. package/dist/lib/kuzzle/vault.d.ts +5 -1
  292. package/dist/lib/kuzzle/vault.js +29 -17
  293. package/dist/lib/model/security/profile.d.ts +34 -5
  294. package/dist/lib/model/security/profile.js +6 -5
  295. package/dist/lib/model/security/rights.d.ts +12 -4
  296. package/dist/lib/model/security/rights.js +7 -6
  297. package/dist/lib/model/security/role.d.ts +8 -4
  298. package/dist/lib/model/security/role.js +12 -6
  299. package/dist/lib/model/security/token.d.ts +8 -0
  300. package/dist/lib/model/security/user.d.ts +8 -2
  301. package/dist/lib/model/security/user.js +7 -4
  302. package/dist/lib/model/storage/apiKey.d.ts +55 -26
  303. package/dist/lib/model/storage/apiKey.js +97 -24
  304. package/dist/lib/model/storage/baseModel.d.ts +77 -66
  305. package/dist/lib/model/storage/baseModel.js +21 -34
  306. package/dist/lib/service/cache/redis.d.ts +53 -36
  307. package/dist/lib/service/cache/redis.js +148 -71
  308. package/dist/lib/service/service.d.ts +14 -19
  309. package/dist/lib/service/service.js +41 -22
  310. package/dist/lib/service/storage/7/elasticsearch.d.ts +136 -93
  311. package/dist/lib/service/storage/7/elasticsearch.js +218 -165
  312. package/dist/lib/service/storage/7/esWrapper.d.ts +30 -13
  313. package/dist/lib/service/storage/7/esWrapper.js +58 -37
  314. package/dist/lib/service/storage/8/elasticsearch.d.ts +153 -119
  315. package/dist/lib/service/storage/8/elasticsearch.js +286 -213
  316. package/dist/lib/service/storage/8/esWrapper.d.ts +30 -13
  317. package/dist/lib/service/storage/8/esWrapper.js +59 -38
  318. package/dist/lib/service/storage/commons/queryTranslator.d.ts +3 -3
  319. package/dist/lib/service/storage/commons/queryTranslator.js +71 -35
  320. package/dist/lib/types/ApiRoute.d.ts +12 -0
  321. package/dist/lib/types/ApiRoute.js +23 -0
  322. package/dist/lib/types/ClientConnection.d.ts +1 -1
  323. package/dist/lib/types/EventHandler.d.ts +10 -0
  324. package/dist/lib/types/Global.d.ts +8 -3
  325. package/dist/lib/types/HttpMessage.d.ts +2 -2
  326. package/dist/lib/types/HttpStream.d.ts +1 -1
  327. package/dist/lib/types/HttpStream.js +3 -1
  328. package/dist/lib/types/JSONObject.d.ts +9 -0
  329. package/dist/lib/types/JSONObject.js +23 -0
  330. package/dist/lib/types/Kuzzle.d.ts +5 -3
  331. package/dist/lib/types/KuzzleDocument.d.ts +1 -1
  332. package/dist/lib/types/KuzzleWebSocket.d.ts +24 -0
  333. package/dist/lib/types/KuzzleWebSocket.js +23 -0
  334. package/dist/lib/types/OpenApiDefinition.d.ts +1 -1
  335. package/dist/lib/types/Plugin.d.ts +33 -8
  336. package/dist/lib/types/PluginInstance.d.ts +67 -0
  337. package/dist/lib/types/PluginInstance.js +23 -0
  338. package/dist/lib/types/Policy.d.ts +1 -1
  339. package/dist/lib/types/Target.d.ts +8 -0
  340. package/dist/lib/types/Token.d.ts +14 -2
  341. package/dist/lib/types/config/HttpConfiguration.d.ts +5 -0
  342. package/dist/lib/types/config/KuzzleConfiguration.d.ts +102 -1
  343. package/dist/lib/types/config/PluginsConfiguration.d.ts +3 -2
  344. package/dist/lib/types/config/SecurityConfiguration.d.ts +6 -2
  345. package/dist/lib/types/config/ServerConfiguration.d.ts +2 -2
  346. package/dist/lib/types/config/ServicesConfiguration.d.ts +1 -1
  347. package/dist/lib/types/config/cache/BaseCacheRedisConfiguration.d.ts +131 -0
  348. package/dist/lib/types/config/cache/BaseCacheRedisConfiguration.js +3 -0
  349. package/dist/lib/types/config/internalCache/InternalCacheRedisConfiguration.d.ts +2 -106
  350. package/dist/lib/types/config/publicCache/PublicCacheRedisConfiguration.d.ts +2 -26
  351. package/dist/lib/types/config/storageEngine/StorageEngineElasticsearchConfiguration.d.ts +3 -0
  352. package/dist/lib/types/controllers/Controller.d.ts +12 -3
  353. package/dist/lib/types/controllers/ControllerDefinition.d.ts +2 -2
  354. package/dist/lib/types/controllers/{adminControlller.type.js → adminController.type.js} +1 -1
  355. package/dist/lib/types/controllers/authController.type.d.ts +2 -2
  356. package/dist/lib/types/core/auth/formatProcessing.type.d.ts +7 -1
  357. package/dist/lib/types/errors/ErrorDomains.d.ts +1 -1
  358. package/dist/lib/types/events/EventGenericDocument.d.ts +10 -9
  359. package/dist/lib/types/events/EventProtocol.d.ts +4 -4
  360. package/dist/lib/types/index.d.ts +7 -0
  361. package/dist/lib/types/index.js +7 -0
  362. package/dist/lib/types/shared/StoreCollectionsDefinition.d.ts +1 -1
  363. package/dist/lib/types/storage/7/Elasticsearch.d.ts +62 -13
  364. package/dist/lib/types/storage/8/Elasticsearch.d.ts +62 -13
  365. package/dist/lib/util/Inflector.d.ts +4 -1
  366. package/dist/lib/util/Inflector.js +0 -3
  367. package/dist/lib/util/assertType.d.ts +24 -27
  368. package/dist/lib/util/assertType.js +28 -39
  369. package/dist/lib/util/async.js +19 -6
  370. package/dist/lib/util/asyncStore.js +1 -1
  371. package/dist/lib/util/bufferedPassThrough.d.ts +9 -0
  372. package/dist/lib/util/bufferedPassThrough.js +27 -13
  373. package/dist/lib/util/bytes.d.ts +1 -1
  374. package/dist/lib/util/bytes.js +6 -2
  375. package/dist/lib/util/debug.d.ts +3 -1
  376. package/dist/lib/util/debug.js +42 -10
  377. package/dist/lib/util/deprecate.d.ts +9 -1
  378. package/dist/lib/util/deprecate.js +10 -6
  379. package/dist/lib/util/didYouMean.d.ts +2 -1
  380. package/dist/lib/util/didYouMean.js +8 -4
  381. package/dist/lib/util/distributedLock.d.ts +56 -0
  382. package/dist/lib/util/distributedLock.js +166 -0
  383. package/dist/lib/util/dump-collection.d.ts +1 -1
  384. package/dist/lib/util/dump-collection.js +4 -5
  385. package/dist/lib/util/extractFields.d.ts +15 -11
  386. package/dist/lib/util/extractFields.js +8 -9
  387. package/dist/lib/util/koncordeCompat.d.ts +2 -2
  388. package/dist/lib/util/koncordeCompat.js +33 -0
  389. package/dist/lib/util/memoize.d.ts +7 -6
  390. package/dist/lib/util/memoize.js +3 -3
  391. package/dist/lib/util/mutex.d.ts +22 -2
  392. package/dist/lib/util/mutex.js +39 -1
  393. package/dist/lib/util/name-generator.js +14 -2
  394. package/dist/lib/util/promback.d.ts +20 -11
  395. package/dist/lib/util/promback.js +14 -7
  396. package/dist/lib/util/requestAssertions.d.ts +30 -10
  397. package/dist/lib/util/requestAssertions.js +125 -121
  398. package/dist/lib/util/safeObject.d.ts +3 -3
  399. package/dist/lib/util/safeObject.js +7 -3
  400. package/dist/lib/util/stackTrace.d.ts +13 -5
  401. package/dist/lib/util/stackTrace.js +17 -12
  402. package/dist/lib/util/thrown.d.ts +13 -0
  403. package/dist/lib/util/thrown.js +46 -0
  404. package/dist/package.json +26 -19
  405. package/package.json +26 -19
  406. package/dist/lib/util/wildcard.d.ts +0 -1
  407. package/dist/lib/util/wildcard.js +0 -46
  408. /package/dist/lib/types/controllers/{adminControlller.type.d.ts → adminController.type.d.ts} +0 -0
@@ -102,8 +102,6 @@ class ES7 {
102
102
  this._scope = scope;
103
103
  this._indexPrefix =
104
104
  scope === storeScopeEnum_1.storeScopeEnum.PRIVATE ? PRIVATE_PREFIX : PUBLIC_PREFIX;
105
- this._client = null;
106
- this._esWrapper = null;
107
105
  this._esVersion = null;
108
106
  this._translator = new queryTranslator_1.QueryTranslator();
109
107
  // Allowed root key of a search query
@@ -160,9 +158,11 @@ class ES7 {
160
158
  await this.waitForElasticsearch();
161
159
  this._esWrapper = new esWrapper_1.default(this._client);
162
160
  const { body: { version }, } = await this._client.info();
163
- if (version &&
164
- !semver_1.default.satisfies(semver_1.default.coerce(version.number), ">= 7.0.0")) {
165
- throw kerror.get("services", "storage", "version_mismatch", version.number);
161
+ if (version) {
162
+ const coerced = semver_1.default.coerce(version.number);
163
+ if (coerced === null || !semver_1.default.satisfies(coerced, ">= 7.0.0")) {
164
+ throw kerror.get("services", "storage", "version_mismatch", version.number);
165
+ }
166
166
  }
167
167
  this._esVersion = version;
168
168
  }
@@ -216,7 +216,7 @@ class ES7 {
216
216
  const { body } = await this._client.indices.stats(esRequest);
217
217
  const indexes = {};
218
218
  let size = 0;
219
- for (const [indice, indiceInfo] of Object.entries(body.indices)) {
219
+ for (const [indice, indiceInfo] of Object.entries(body.indices ?? {})) {
220
220
  const infos = indiceInfo;
221
221
  // Ignore non-Kuzzle indices
222
222
  if (!indice.startsWith(PRIVATE_PREFIX) &&
@@ -231,19 +231,17 @@ class ES7 {
231
231
  collectionName === HIDDEN_COLLECTION) {
232
232
  continue;
233
233
  }
234
- if (!indexes[indexName]) {
235
- indexes[indexName] = {
236
- collections: [],
237
- name: indexName,
238
- size: 0,
239
- };
240
- }
241
- indexes[indexName].collections.push({
234
+ const indexStats = (indexes[indexName] ?? (indexes[indexName] = {
235
+ collections: [],
236
+ name: indexName,
237
+ size: 0,
238
+ }));
239
+ indexStats.collections.push({
242
240
  documentCount: infos.total.docs.count,
243
241
  name: collectionName,
244
242
  size: infos.total.store.size_in_bytes,
245
243
  });
246
- indexes[indexName].size += infos.total.store.size_in_bytes;
244
+ indexStats.size += infos.total.store.size_in_bytes;
247
245
  size += infos.total.store.size_in_bytes;
248
246
  }
249
247
  return {
@@ -270,7 +268,7 @@ class ES7 {
270
268
  const cacheKey = SCROLL_CACHE_PREFIX + global.kuzzle.hash(esRequest.scroll_id);
271
269
  this.logger.debug("Scroll: %o", esRequest);
272
270
  if (_scrollTTL) {
273
- const scrollDuration = (0, ms_1.default)(_scrollTTL);
271
+ const scrollDuration = (0, ms_1.default)(_scrollTTL) ?? 0;
274
272
  if (scrollDuration > this.maxScrollDuration) {
275
273
  throw kerror.get("services", "storage", "scroll_duration_too_great", _scrollTTL);
276
274
  }
@@ -313,16 +311,12 @@ class ES7 {
313
311
  async search({ index, collection, searchBody, targets, } = {}, { from, size, scroll, } = {}) {
314
312
  let esIndexes;
315
313
  if (targets && targets.length > 0) {
316
- const indexes = new Set();
317
- for (const target of targets) {
318
- for (const targetCollection of target.collections) {
319
- const alias = this._getAlias(target.index, targetCollection);
320
- indexes.add(alias);
321
- }
322
- }
323
- esIndexes = Array.from(indexes).join(",");
314
+ esIndexes = this._targetsToIndexes(targets);
324
315
  }
325
316
  else {
317
+ if (index === undefined) {
318
+ throw kerror.get("services", "storage", "missing_argument", "index");
319
+ }
326
320
  esIndexes = this._getAlias(index, collection);
327
321
  }
328
322
  const esRequest = {
@@ -334,7 +328,7 @@ class ES7 {
334
328
  trackTotalHits: true,
335
329
  };
336
330
  if (scroll) {
337
- const scrollDuration = (0, ms_1.default)(scroll);
331
+ const scrollDuration = (0, ms_1.default)(scroll) ?? 0;
338
332
  if (scrollDuration > this.maxScrollDuration) {
339
333
  throw kerror.get("services", "storage", "scroll_duration_too_great", scroll);
340
334
  }
@@ -363,6 +357,20 @@ class ES7 {
363
357
  throw this._esWrapper.formatESError(error);
364
358
  }
365
359
  }
360
+ /**
361
+ * The comma-separated alias list a multi-target search is run against.
362
+ *
363
+ * @param targets - targets already through `assertTargetsAreValid`
364
+ */
365
+ _targetsToIndexes(targets) {
366
+ const indexes = new Set();
367
+ for (const target of targets) {
368
+ for (const targetCollection of target.collections) {
369
+ indexes.add(this._getAlias(target.index, targetCollection));
370
+ }
371
+ }
372
+ return Array.from(indexes).join(",");
373
+ }
366
374
  /**
367
375
  * Generate a map that associate an alias to a pair of index and collection
368
376
  *
@@ -415,10 +423,15 @@ class ES7 {
415
423
  * find the first alias that exists in the map of aliases associated
416
424
  * to the targets.
417
425
  */
418
- const alias = aliases.find((_alias) => aliasToTargets[_alias]);
419
426
  // Retrieve index and collection information based on the matching alias
420
- index = aliasToTargets[alias].index;
421
- collection = aliasToTargets[alias].collection;
427
+ for (const _alias of aliases) {
428
+ const target = aliasToTargets[_alias];
429
+ if (target) {
430
+ index = target.index;
431
+ collection = target.collection;
432
+ break;
433
+ }
434
+ }
422
435
  }
423
436
  return {
424
437
  _id: hit._id,
@@ -498,7 +511,7 @@ class ES7 {
498
511
  */
499
512
  async mGet(index, collection, ids) {
500
513
  if (ids.length === 0) {
501
- return { errors: [], item: [] };
514
+ return { errors: [], items: [] };
502
515
  }
503
516
  const esRequest = {
504
517
  body: {
@@ -847,7 +860,7 @@ class ES7 {
847
860
  return {
848
861
  deleted: body.deleted,
849
862
  documents,
850
- failures: body.failures.map(({ shardId, reason }) => ({
863
+ failures: (body.failures ?? []).map(({ shardId, reason }) => ({
851
864
  reason,
852
865
  shardId,
853
866
  })),
@@ -954,10 +967,14 @@ class ES7 {
954
967
  * @returns {Promise.<{ successes: [_id, _source, _status], errors: [ document, status, reason ] }>}
955
968
  */
956
969
  async bulkUpdateByQuery(index, collection, query, changes, { refresh = false, } = {}) {
970
+ // `body` is optional on the request params, so the script is written
971
+ // through the local the request holds by reference rather than read back
972
+ // off the request.
973
+ const requestBody = {
974
+ query: this._sanitizeSearchBody({ query }).query,
975
+ };
957
976
  const esRequest = {
958
- body: {
959
- query: this._sanitizeSearchBody({ query }).query,
960
- },
977
+ body: requestBody,
961
978
  index: this._getAlias(index, collection),
962
979
  refresh,
963
980
  };
@@ -971,7 +988,7 @@ class ES7 {
971
988
  script.params[key] = value;
972
989
  }
973
990
  if (script.source !== "") {
974
- esRequest.body.script = script;
991
+ requestBody.script = script;
975
992
  }
976
993
  this.logger.debug("Bulk Update by query: %o", esRequest);
977
994
  let response;
@@ -981,8 +998,9 @@ class ES7 {
981
998
  catch (error) {
982
999
  throw this._esWrapper.formatESError(error);
983
1000
  }
984
- if (response.body.failures.length) {
985
- const errors = response.body.failures.map(({ shardId, reason }) => ({
1001
+ const failures = response.body.failures ?? [];
1002
+ if (failures.length) {
1003
+ const errors = failures.map(({ shardId, reason }) => ({
986
1004
  reason,
987
1005
  shardId,
988
1006
  }));
@@ -1018,7 +1036,7 @@ class ES7 {
1018
1036
  const client = this._client;
1019
1037
  let results = [];
1020
1038
  let processed = 0;
1021
- let scrollId = null;
1039
+ let scrollId;
1022
1040
  try {
1023
1041
  results = await new bluebird_1.default((resolve, reject) => {
1024
1042
  this._client.search(esRequest, async function getMoreUntilDone(error, { body: { hits, _scroll_id } }) {
@@ -1063,7 +1081,7 @@ class ES7 {
1063
1081
  this._assertValidIndexAndCollection(index);
1064
1082
  let body;
1065
1083
  try {
1066
- body = (await this._client.cat.aliases({ format: "json" })).body;
1084
+ body = await this._catAliases();
1067
1085
  }
1068
1086
  catch (error) {
1069
1087
  throw this._esWrapper.formatESError(error);
@@ -1109,14 +1127,15 @@ class ES7 {
1109
1127
  finally {
1110
1128
  await mutex.unlock();
1111
1129
  }
1112
- const esRequest = {
1113
- body: {
1114
- aliases: {
1115
- [this._getAlias(index, collection)]: {},
1116
- },
1117
- mappings: {},
1118
- settings,
1130
+ const requestBody = {
1131
+ aliases: {
1132
+ [this._getAlias(index, collection)]: {},
1119
1133
  },
1134
+ mappings: {},
1135
+ settings,
1136
+ };
1137
+ const esRequest = {
1138
+ body: requestBody,
1120
1139
  index: await this._getAvailableIndice(index, collection),
1121
1140
  wait_for_active_shards: await this._getWaitForActiveShards(),
1122
1141
  };
@@ -1126,16 +1145,16 @@ class ES7 {
1126
1145
  return this.updateCollection(index, collection, { mappings, settings });
1127
1146
  }
1128
1147
  this._checkMappings(mappings);
1129
- esRequest.body.mappings = {
1148
+ requestBody.mappings = {
1130
1149
  _meta: mappings._meta || this._config.commonMapping._meta,
1131
1150
  dynamic: mappings.dynamic || this._config.commonMapping.dynamic,
1132
1151
  properties: lodash_1.default.merge(mappings.properties, this._config.commonMapping.properties),
1133
1152
  };
1134
- esRequest.body.settings.number_of_replicas =
1135
- esRequest.body.settings.number_of_replicas ||
1153
+ settings.number_of_replicas =
1154
+ settings.number_of_replicas ||
1136
1155
  this._config.defaultSettings.number_of_replicas;
1137
- esRequest.body.settings.number_of_shards =
1138
- esRequest.body.settings.number_of_shards ||
1156
+ settings.number_of_shards =
1157
+ settings.number_of_shards ||
1139
1158
  this._config.defaultSettings.number_of_shards;
1140
1159
  try {
1141
1160
  await this._client.indices.create(esRequest);
@@ -1450,34 +1469,18 @@ class ES7 {
1450
1469
  *
1451
1470
  * bulk body can contain more than 10K elements
1452
1471
  */
1453
- for (let i = 0; i < body.items.length; i++) {
1454
- const row = body.items[i];
1455
- const action = Object.keys(row)[0];
1456
- const item = row[action];
1472
+ for (const row of body.items) {
1473
+ // Elasticsearch answers exactly one action per row; a row carrying none
1474
+ // describes no document, so there is nothing to report on it.
1475
+ const [action = ""] = Object.keys(row);
1476
+ const item = Object.values(row)[0];
1477
+ if (item === undefined) {
1478
+ continue;
1479
+ }
1457
1480
  if (item.status >= 400) {
1458
- const error = {
1459
- _id: item._id,
1460
- status: item.status,
1461
- };
1462
- // update action contain body in "doc" field
1463
- // the delete action is not followed by an action payload
1464
- if (action === "update") {
1465
- error._source = documents[idx + 1].doc;
1466
- error._source._kuzzle_info = undefined;
1467
- }
1468
- else if (action !== "delete") {
1469
- error._source = documents[idx + 1];
1470
- error._source._kuzzle_info = undefined;
1471
- }
1472
- // ES response does not systematicaly include an error object
1473
- // (e.g. delete action with 404 status)
1474
- if (item.error) {
1475
- error.error = {
1476
- reason: item.error.reason,
1477
- type: item.error.type,
1478
- };
1479
- }
1480
- result.errors.push({ [action]: error });
1481
+ result.errors.push({
1482
+ [action]: this._importError(action, item, documents[idx + 1]),
1483
+ });
1481
1484
  }
1482
1485
  else {
1483
1486
  result.items.push({
@@ -1493,6 +1496,35 @@ class ES7 {
1493
1496
  /* end critical code section */
1494
1497
  return result;
1495
1498
  }
1499
+ /**
1500
+ * The error entry `import` reports for one rejected bulk row.
1501
+ *
1502
+ * @param action - the bulk action Elasticsearch echoed back
1503
+ * @param item - that action's answer
1504
+ * @param payload - the action's payload, absent for a delete
1505
+ */
1506
+ _importError(action, item, payload) {
1507
+ const error = {
1508
+ _id: item._id,
1509
+ status: item.status,
1510
+ };
1511
+ // update action contain body in "doc" field
1512
+ // the delete action is not followed by an action payload
1513
+ if (action !== "delete") {
1514
+ const source = (action === "update" ? payload?.doc : payload) ?? {};
1515
+ source._kuzzle_info = undefined;
1516
+ error._source = source;
1517
+ }
1518
+ // ES response does not systematicaly include an error object
1519
+ // (e.g. delete action with 404 status)
1520
+ if (item.error) {
1521
+ error.error = {
1522
+ reason: item.error.reason,
1523
+ type: item.error.type,
1524
+ };
1525
+ }
1526
+ return error;
1527
+ }
1496
1528
  /**
1497
1529
  * Retrieves the complete list of existing collections in the current index
1498
1530
  *
@@ -1504,7 +1536,7 @@ class ES7 {
1504
1536
  async listCollections(index, { includeHidden = false } = {}) {
1505
1537
  let body;
1506
1538
  try {
1507
- ({ body } = await this._client.cat.aliases({ format: "json" }));
1539
+ body = await this._catAliases();
1508
1540
  }
1509
1541
  catch (error) {
1510
1542
  throw this._esWrapper.formatESError(error);
@@ -1521,7 +1553,7 @@ class ES7 {
1521
1553
  async listIndexes() {
1522
1554
  let body;
1523
1555
  try {
1524
- ({ body } = await this._client.cat.aliases({ format: "json" }));
1556
+ body = await this._catAliases();
1525
1557
  }
1526
1558
  catch (error) {
1527
1559
  throw this._esWrapper.formatESError(error);
@@ -1538,7 +1570,7 @@ class ES7 {
1538
1570
  async getSchema() {
1539
1571
  let body;
1540
1572
  try {
1541
- ({ body } = await this._client.cat.aliases({ format: "json" }));
1573
+ body = await this._catAliases();
1542
1574
  }
1543
1575
  catch (error) {
1544
1576
  throw this._esWrapper.formatESError(error);
@@ -1558,7 +1590,7 @@ class ES7 {
1558
1590
  async listAliases() {
1559
1591
  let body;
1560
1592
  try {
1561
- ({ body } = await this._client.cat.aliases({ format: "json" }));
1593
+ body = await this._catAliases();
1562
1594
  }
1563
1595
  catch (error) {
1564
1596
  throw this._esWrapper.formatESError(error);
@@ -1618,7 +1650,7 @@ class ES7 {
1618
1650
  }
1619
1651
  const deleted = new Set();
1620
1652
  try {
1621
- const { body } = await this._client.cat.aliases({ format: "json" });
1653
+ const body = await this._catAliases();
1622
1654
  const esRequest = body.reduce((request, { alias, index: indice }) => {
1623
1655
  const index = this._extractIndex(alias);
1624
1656
  if (alias[INDEX_PREFIX_POSITION_IN_ALIAS] !== this._indexPrefix ||
@@ -1712,7 +1744,7 @@ class ES7 {
1712
1744
  */
1713
1745
  async mExists(index, collection, ids) {
1714
1746
  if (ids.length === 0) {
1715
- return { errors: [], item: [] };
1747
+ return { errors: [], items: [] };
1716
1748
  }
1717
1749
  const esRequest = {
1718
1750
  _source: "false",
@@ -1776,7 +1808,7 @@ class ES7 {
1776
1808
  const collections = await this.listCollections(index, {
1777
1809
  includeHidden: true,
1778
1810
  });
1779
- return collections.some((col) => col === HIDDEN_COLLECTION);
1811
+ return collections.includes(HIDDEN_COLLECTION);
1780
1812
  }
1781
1813
  /**
1782
1814
  * Creates multiple documents at once.
@@ -1819,8 +1851,8 @@ class ES7 {
1819
1851
  *
1820
1852
  * request can contain more than 10K elements
1821
1853
  */
1822
- for (let i = 0, idx = 0; i < extractedDocuments.length; i++) {
1823
- const document = extractedDocuments[i];
1854
+ let idx = 0;
1855
+ for (const document of extractedDocuments) {
1824
1856
  // Documents are retrieved in the same order than we got them from user
1825
1857
  if (typeof document._id === "string" && existingDocuments[idx]) {
1826
1858
  if (existingDocuments[idx].found) {
@@ -1840,15 +1872,13 @@ class ES7 {
1840
1872
  _id: document._id,
1841
1873
  _index: alias,
1842
1874
  },
1843
- });
1844
- esRequest.body.push(document._source);
1875
+ }, document._source);
1845
1876
  toImport.push(document);
1846
1877
  }
1847
1878
  idx++;
1848
1879
  }
1849
1880
  else {
1850
- esRequest.body.push({ index: { _index: alias } });
1851
- esRequest.body.push(document._source);
1881
+ esRequest.body.push({ index: { _index: alias } }, document._source);
1852
1882
  toImport.push(document);
1853
1883
  }
1854
1884
  }
@@ -1885,20 +1915,18 @@ class ES7 {
1885
1915
  timeout,
1886
1916
  };
1887
1917
  const { rejected, extractedDocuments } = this._extractMDocuments(documents, kuzzleMeta);
1888
- esRequest.body = [];
1889
1918
  /**
1890
1919
  * @warning Critical code section
1891
1920
  *
1892
1921
  * request can contain more than 10K elements
1893
1922
  */
1894
- for (let i = 0; i < extractedDocuments.length; i++) {
1923
+ for (const extractedDocument of extractedDocuments) {
1895
1924
  esRequest.body.push({
1896
1925
  index: {
1897
- _id: extractedDocuments[i]._id,
1926
+ _id: extractedDocument._id,
1898
1927
  _index: alias,
1899
1928
  },
1900
- });
1901
- esRequest.body.push(extractedDocuments[i]._source);
1929
+ }, extractedDocument._source);
1902
1930
  }
1903
1931
  /* end critical code section */
1904
1932
  return this._mExecute(esRequest, extractedDocuments, rejected, {
@@ -1918,7 +1946,7 @@ class ES7 {
1918
1946
  *
1919
1947
  * @returns {Promise.<Object>} { items, errors }
1920
1948
  */
1921
- async mUpdate(index, collection, documents, { refresh = undefined, retryOnConflict = 0, timeout = undefined, userId = null, } = {}) {
1949
+ async mUpdate(index, collection, documents, { refresh, retryOnConflict = 0, timeout, userId = null, } = {}) {
1922
1950
  const alias = this._getAlias(index, collection), toImport = [], esRequest = {
1923
1951
  body: [],
1924
1952
  index: alias,
@@ -1935,19 +1963,18 @@ class ES7 {
1935
1963
  *
1936
1964
  * request can contain more than 10K elements
1937
1965
  */
1938
- for (let i = 0; i < extractedDocuments.length; i++) {
1939
- const extractedDocument = extractedDocuments[i];
1966
+ for (const extractedDocument of extractedDocuments) {
1940
1967
  if (typeof extractedDocument._id === "string") {
1968
+ // _source: true => makes ES return the updated document source in the
1969
+ // response. Required by the real-time notifier component
1941
1970
  esRequest.body.push({
1942
1971
  update: {
1943
1972
  _id: extractedDocument._id,
1944
1973
  _index: alias,
1945
- retry_on_conflict: retryOnConflict || this._config.defaults.onUpdateConflictRetries,
1974
+ retry_on_conflict: retryOnConflict ||
1975
+ this._config.defaults.onUpdateConflictRetries,
1946
1976
  },
1947
- });
1948
- // _source: true => makes ES return the updated document source in the
1949
- // response. Required by the real-time notifier component
1950
- esRequest.body.push({
1977
+ }, {
1951
1978
  _source: true,
1952
1979
  doc: extractedDocument._source,
1953
1980
  });
@@ -2021,17 +2048,17 @@ class ES7 {
2021
2048
  *
2022
2049
  * request can contain more than 10K elements
2023
2050
  */
2024
- for (let i = 0; i < extractedDocuments.length; i++) {
2051
+ for (const extractedDocument of extractedDocuments) {
2025
2052
  esRequest.body.push({
2026
2053
  update: {
2027
- _id: extractedDocuments[i]._id,
2054
+ _id: extractedDocument._id,
2028
2055
  _index: alias,
2029
2056
  _source: true,
2030
2057
  retry_on_conflict: retryOnConflict || this._config.defaults.onUpdateConflictRetries,
2031
2058
  },
2032
2059
  }, {
2033
- doc: extractedDocuments[i]._source.changes,
2034
- upsert: extractedDocuments[i]._source.default,
2060
+ doc: extractedDocument._source.changes,
2061
+ upsert: extractedDocument._source.default,
2035
2062
  });
2036
2063
  // _source: true
2037
2064
  // Makes ES return the updated document source in the response.
@@ -2095,8 +2122,7 @@ class ES7 {
2095
2122
  *
2096
2123
  * request can contain more than 10K elements
2097
2124
  */
2098
- for (let i = 0; i < extractedDocuments.length; i++) {
2099
- const document = extractedDocuments[i];
2125
+ for (const [i, document] of extractedDocuments.entries()) {
2100
2126
  // Documents are retrieved in the same order than we got them from user
2101
2127
  if (existingDocuments[i]?.found) {
2102
2128
  esRequest.body.push({
@@ -2104,8 +2130,7 @@ class ES7 {
2104
2130
  _id: document._id,
2105
2131
  _index: alias,
2106
2132
  },
2107
- });
2108
- esRequest.body.push(document._source);
2133
+ }, document._source);
2109
2134
  toImport.push(document);
2110
2135
  }
2111
2136
  else {
@@ -2164,8 +2189,7 @@ class ES7 {
2164
2189
  *
2165
2190
  * request can contain more than 10K elements
2166
2191
  */
2167
- for (let i = 0; i < validIds.length; i++) {
2168
- const validId = validIds[i];
2192
+ for (const validId of validIds) {
2169
2193
  const item = items[idx];
2170
2194
  if (item && item._id === validId) {
2171
2195
  query.ids.values.push(validId);
@@ -2204,48 +2228,38 @@ class ES7 {
2204
2228
  if (this._hasExceededLimit(limits, documents)) {
2205
2229
  return kerror.reject("services", "storage", "write_limit_exceeded");
2206
2230
  }
2207
- let response = { body: { items: [] } };
2231
+ let items = [];
2208
2232
  if (documents.length > 0) {
2209
2233
  try {
2210
- response = await this._client.bulk(esRequest);
2234
+ ({
2235
+ body: { items },
2236
+ } = await this._client.bulk(esRequest));
2211
2237
  }
2212
2238
  catch (error) {
2213
2239
  throw this._esWrapper.formatESError(error);
2214
2240
  }
2215
2241
  }
2216
- const body = response.body;
2217
2242
  const successes = [];
2218
2243
  /**
2219
2244
  * @warning Critical code section
2220
2245
  *
2221
2246
  * request can contain more than 10K elements
2222
2247
  */
2223
- for (let i = 0; i < body.items.length; i++) {
2224
- const item = body.items[i];
2225
- const result = item[Object.keys(item)[0]];
2248
+ for (const [i, item] of items.entries()) {
2249
+ const result = Object.values(item)[0];
2250
+ const document = documents[i];
2251
+ // An answer carries exactly one result per document sent; a row without
2252
+ // its counterpart is one neither branch below could describe.
2253
+ if (result === undefined || document === undefined) {
2254
+ continue;
2255
+ }
2226
2256
  if (result.status >= 400) {
2227
- if (result.status === 404) {
2228
- partialErrors.push({
2229
- document: {
2230
- _id: documents[i]._id,
2231
- body: documents[i]._source,
2232
- },
2233
- reason: "document not found",
2234
- status: result.status,
2235
- });
2236
- }
2237
- else {
2238
- partialErrors.push({
2239
- document: documents[i],
2240
- reason: result.error.reason,
2241
- status: result.status,
2242
- });
2243
- }
2257
+ partialErrors.push(this._mExecuteRejection(result, document));
2244
2258
  }
2245
2259
  else {
2246
2260
  successes.push({
2247
2261
  _id: result._id,
2248
- _source: source ? documents[i]._source : undefined,
2262
+ _source: source ? document._source : undefined,
2249
2263
  _version: result._version,
2250
2264
  created: result.result === "created",
2251
2265
  get: result.get,
@@ -2260,6 +2274,29 @@ class ES7 {
2260
2274
  items: successes,
2261
2275
  };
2262
2276
  }
2277
+ /**
2278
+ * The partial-error entry `_mExecute` reports for one rejected bulk row.
2279
+ *
2280
+ * A 404 answers no reason of its own, and reports the document by id and
2281
+ * body rather than whole, which is what the m* callers read back.
2282
+ */
2283
+ _mExecuteRejection(result, document) {
2284
+ if (result.status === 404) {
2285
+ return {
2286
+ document: {
2287
+ _id: document._id,
2288
+ body: document._source,
2289
+ },
2290
+ reason: "document not found",
2291
+ status: result.status,
2292
+ };
2293
+ }
2294
+ return {
2295
+ document,
2296
+ reason: result.error?.reason,
2297
+ status: result.status,
2298
+ };
2299
+ }
2263
2300
  /**
2264
2301
  * Extracts, injects metadata and validates documents contained
2265
2302
  * in a Request
@@ -2281,8 +2318,7 @@ class ES7 {
2281
2318
  *
2282
2319
  * request can contain more than 10K elements
2283
2320
  */
2284
- for (let i = 0; i < documents.length; i++) {
2285
- const document = documents[i];
2321
+ for (const document of documents) {
2286
2322
  if (!(0, safeObject_1.isPlainObject)(document.body) && !prepareMUpsert) {
2287
2323
  rejected.push({
2288
2324
  document,
@@ -2414,17 +2450,15 @@ class ES7 {
2414
2450
  */
2415
2451
  async _getIndice(index, collection) {
2416
2452
  const alias = `${ALIAS_PREFIX}${this._indexPrefix}${index}${NAME_SEPARATOR}${collection}`;
2417
- const { body } = await this._client.cat.aliases({
2418
- format: "json",
2419
- name: alias,
2420
- });
2421
- if (body.length < 1) {
2453
+ const body = await this._catAliases(alias);
2454
+ const [record] = body;
2455
+ if (record === undefined) {
2422
2456
  throw kerror.get("services", "storage", "unknown_index_collection");
2423
2457
  }
2424
2458
  else if (body.length > 1) {
2425
2459
  throw kerror.get("services", "storage", "multiple_indice_alias", `"alias" starting with "${ALIAS_PREFIX}"`, '"indices"');
2426
2460
  }
2427
- return body[0].index;
2461
+ return record.index;
2428
2462
  }
2429
2463
  /**
2430
2464
  * Given an ES Request returns the settings of the corresponding indice.
@@ -2478,13 +2512,30 @@ class ES7 {
2478
2512
  * @returns {String} Alias name (eg: '@&nepali.liia')
2479
2513
  * @throws If there is not exactly one alias associated that is prefixed with @
2480
2514
  */
2515
+ /**
2516
+ * `cat.aliases` answers untyped rows whose `alias` and `index` the client
2517
+ * declares as optional. Defaulting them once here is what lets every caller
2518
+ * index into an alias without repeating the check — and an empty alias fails
2519
+ * the index-prefix test every caller applies, which is what a row without one
2520
+ * used to throw on.
2521
+ */
2522
+ async _catAliases(name) {
2523
+ const { body } = await this._client.cat.aliases(name === undefined ? { format: "json" } : { format: "json", name });
2524
+ return body.map(({ alias, index }) => ({
2525
+ alias: alias ?? "",
2526
+ index: index ?? "",
2527
+ }));
2528
+ }
2481
2529
  async _getAliasFromIndice(indice) {
2482
2530
  const { body } = await this._client.indices.getAlias({ index: indice });
2483
- const aliases = Object.keys(body[indice].aliases).filter((alias) => alias.startsWith(ALIAS_PREFIX));
2484
- if (aliases.length < 1) {
2531
+ const indiceAliases = body[indice]?.aliases ?? {};
2532
+ const [first, ...others] = Object.keys(indiceAliases).filter((alias) => alias.startsWith(ALIAS_PREFIX));
2533
+ // The non-empty tuple is what lets every caller read aliases[0] without a
2534
+ // guard the throw below already made redundant.
2535
+ if (first === undefined) {
2485
2536
  throw kerror.get("services", "storage", "unknown_index_collection");
2486
2537
  }
2487
- return aliases;
2538
+ return [first, ...others];
2488
2539
  }
2489
2540
  /**
2490
2541
  * Check for each indice whether it has an alias or not.
@@ -2496,11 +2547,13 @@ class ES7 {
2496
2547
  async generateMissingAliases() {
2497
2548
  try {
2498
2549
  const { body } = await this._client.cat.indices({ format: "json" });
2499
- const indices = body.map(({ index: indice }) => indice);
2550
+ const indices = body.flatMap(({ index: indice }) => indice === undefined ? [] : [indice]);
2500
2551
  const aliases = await this.listAliases();
2501
2552
  const indicesWithoutAlias = indices.filter((indice) => indice[INDEX_PREFIX_POSITION_IN_INDICE] === this._indexPrefix &&
2502
2553
  !aliases.some((alias) => alias.indice === indice));
2503
- const esRequest = { body: { actions: [] } };
2554
+ const esRequest = {
2555
+ body: { actions: [] },
2556
+ };
2504
2557
  for (const indice of indicesWithoutAlias) {
2505
2558
  esRequest.body.actions.push({
2506
2559
  add: { alias: `${ALIAS_PREFIX}${indice}`, index: indice },
@@ -2560,16 +2613,16 @@ class ES7 {
2560
2613
  _extractSchema(aliases, { includeHidden = false } = {}) {
2561
2614
  const schema = {};
2562
2615
  for (const alias of aliases) {
2563
- const [indexName, collectionName] = alias
2616
+ // `split` always yields a first element; the defaults are what make that
2617
+ // readable to the compiler.
2618
+ const [indexName = "", collectionName = ""] = alias
2564
2619
  .slice(INDEX_PREFIX_POSITION_IN_ALIAS + 1)
2565
2620
  .split(NAME_SEPARATOR);
2566
2621
  if (alias[INDEX_PREFIX_POSITION_IN_ALIAS] === this._indexPrefix &&
2567
2622
  (collectionName !== HIDDEN_COLLECTION || includeHidden)) {
2568
- if (!schema[indexName]) {
2569
- schema[indexName] = [];
2570
- }
2571
- if (!schema[indexName].includes(collectionName)) {
2572
- schema[indexName].push(collectionName);
2623
+ const collections = (schema[indexName] ?? (schema[indexName] = []));
2624
+ if (!collections.includes(collectionName)) {
2625
+ collections.push(collectionName);
2573
2626
  }
2574
2627
  }
2575
2628
  }
@@ -2665,7 +2718,7 @@ class ES7 {
2665
2718
  *
2666
2719
  * @param {Object} searchBody - ES search body (with query, aggregations, sort, etc)
2667
2720
  */
2668
- _sanitizeSearchBody(searchBody) {
2721
+ _sanitizeSearchBody(searchBody = {}) {
2669
2722
  // Only allow a whitelist of top level properties
2670
2723
  for (const key of Object.keys(searchBody)) {
2671
2724
  if (searchBody[key] !== undefined && !this.searchBodyKeys.includes(key)) {
@@ -2786,7 +2839,7 @@ class ES7 {
2786
2839
  await bluebird_1.default.delay(1000);
2787
2840
  }
2788
2841
  }
2789
- catch (e) {
2842
+ catch {
2790
2843
  await bluebird_1.default.delay(1000);
2791
2844
  }
2792
2845
  }
@@ -2817,9 +2870,8 @@ class ES7 {
2817
2870
  */
2818
2871
  let lastAction = "";
2819
2872
  const actionNames = ["index", "create", "update", "delete"];
2820
- for (let i = 0; i < esRequest.body.length; i++) {
2821
- const item = esRequest.body[i];
2822
- const action = Object.keys(item)[0];
2873
+ for (const item of esRequest.body) {
2874
+ const action = Object.keys(item)[0] ?? "";
2823
2875
  if (actionNames.indexOf(action) !== -1) {
2824
2876
  lastAction = action;
2825
2877
  item[action]._index = alias;
@@ -2926,10 +2978,11 @@ function _isObjectNameValid(name) {
2926
2978
  if (name === "_all") {
2927
2979
  return false;
2928
2980
  }
2929
- let valid = true;
2930
- for (let i = 0; valid && i < FORBIDDEN_CHARS.length; i++) {
2931
- valid = !name.includes(FORBIDDEN_CHARS[i]);
2981
+ for (const forbiddenChar of FORBIDDEN_CHARS) {
2982
+ if (name.includes(forbiddenChar)) {
2983
+ return false;
2984
+ }
2932
2985
  }
2933
- return valid;
2986
+ return true;
2934
2987
  }
2935
2988
  //# sourceMappingURL=elasticsearch.js.map