kuzzle 2.17.6 → 2.18.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 (211) hide show
  1. package/bin/start-kuzzle-server +1 -1
  2. package/lib/api/controllers/adminController.js +1 -1
  3. package/lib/api/controllers/authController.js +7 -2
  4. package/lib/api/controllers/baseController.js +1 -1
  5. package/lib/api/controllers/bulkController.js +1 -1
  6. package/lib/api/controllers/clusterController.js +1 -1
  7. package/lib/api/controllers/collectionController.js +1 -1
  8. package/lib/api/controllers/documentController.js +7 -2
  9. package/lib/api/controllers/index.js +1 -1
  10. package/lib/api/controllers/indexController.js +11 -7
  11. package/lib/api/controllers/memoryStorageController.js +1 -1
  12. package/lib/api/controllers/realtimeController.js +1 -1
  13. package/lib/api/controllers/securityController.js +55 -23
  14. package/lib/api/controllers/serverController.js +1 -1
  15. package/lib/api/documentExtractor.js +1 -1
  16. package/lib/api/funnel.js +1 -1
  17. package/lib/api/httpRoutes.js +7 -5
  18. package/lib/api/openapi/OpenApiManager.js +5 -1
  19. package/lib/api/openapi/components/document/get.yaml +1 -1
  20. package/lib/api/openapi/components/document/index.d.ts +2 -0
  21. package/lib/api/openapi/components/document/index.js +6 -1
  22. package/lib/api/openapi/components/document/mCreateOrReplace.yaml +93 -0
  23. package/lib/api/openapi/components/document/update.yaml +1 -1
  24. package/lib/api/openapi/components/index.d.ts +1 -0
  25. package/lib/api/openapi/components/index.js +1 -0
  26. package/lib/api/openapi/components/security/index.d.ts +2 -0
  27. package/lib/api/openapi/components/security/index.js +10 -0
  28. package/lib/api/openapi/components/security/upsertUser.yaml +59 -0
  29. package/lib/api/openapi/openApiGenerator.js +1 -1
  30. package/lib/api/rateLimiter.js +1 -1
  31. package/lib/api/request/kuzzleRequest.d.ts +21 -0
  32. package/lib/api/request/kuzzleRequest.js +49 -3
  33. package/lib/api/request/requestContext.js +1 -1
  34. package/lib/api/request/requestInput.js +1 -1
  35. package/lib/api/request/requestResponse.js +1 -1
  36. package/lib/cluster/command.js +1 -1
  37. package/lib/cluster/idCardHandler.js +1 -1
  38. package/lib/cluster/index.js +1 -1
  39. package/lib/cluster/node.js +8 -8
  40. package/lib/cluster/publisher.js +1 -1
  41. package/lib/cluster/state.js +1 -1
  42. package/lib/cluster/subscriber.js +4 -1
  43. package/lib/config/documentEventAliases.js +1 -1
  44. package/lib/config/index.js +1 -1
  45. package/lib/core/auth/formatProcessing.js +1 -1
  46. package/lib/core/auth/passportResponse.js +1 -1
  47. package/lib/core/auth/passportWrapper.js +1 -1
  48. package/lib/core/auth/tokenManager.js +1 -1
  49. package/lib/core/backend/applicationManager.js +1 -1
  50. package/lib/core/backend/backend.js +1 -1
  51. package/lib/core/backend/backendCluster.js +1 -1
  52. package/lib/core/backend/backendConfig.js +1 -1
  53. package/lib/core/backend/backendController.js +1 -1
  54. package/lib/core/backend/backendErrors.js +1 -1
  55. package/lib/core/backend/backendHook.js +1 -1
  56. package/lib/core/backend/backendImport.js +1 -1
  57. package/lib/core/backend/backendOpenApi.js +1 -1
  58. package/lib/core/backend/backendPipe.js +1 -1
  59. package/lib/core/backend/backendPlugin.js +1 -1
  60. package/lib/core/backend/backendStorage.js +1 -1
  61. package/lib/core/backend/backendVault.js +1 -1
  62. package/lib/core/backend/internalLogger.js +1 -1
  63. package/lib/core/cache/cacheDbEnum.js +1 -1
  64. package/lib/core/cache/cacheEngine.js +1 -1
  65. package/lib/core/network/accessLogger.js +12 -7
  66. package/lib/core/network/clientConnection.js +1 -1
  67. package/lib/core/network/context.js +1 -1
  68. package/lib/core/network/entryPoint.js +6 -6
  69. package/lib/core/network/httpRouter/index.js +1 -1
  70. package/lib/core/network/httpRouter/routeHandler.js +1 -1
  71. package/lib/core/network/httpRouter/routePart.js +1 -1
  72. package/lib/core/network/protocolManifest.js +1 -1
  73. package/lib/core/network/protocols/httpMessage.js +1 -1
  74. package/lib/core/network/protocols/httpwsProtocol.js +5 -10
  75. package/lib/core/network/protocols/internalProtocol.js +1 -1
  76. package/lib/core/network/protocols/mqttProtocol.js +4 -3
  77. package/lib/core/network/protocols/protocol.js +1 -1
  78. package/lib/core/network/router.js +11 -2
  79. package/lib/core/plugin/plugin.js +1 -1
  80. package/lib/core/plugin/pluginContext.js +1 -1
  81. package/lib/core/plugin/pluginManifest.js +1 -1
  82. package/lib/core/plugin/pluginRepository.js +1 -1
  83. package/lib/core/plugin/pluginsManager.js +1 -1
  84. package/lib/core/plugin/privilegedContext.js +1 -1
  85. package/lib/core/realtime/actionEnum.js +1 -1
  86. package/lib/core/realtime/channel.js +1 -1
  87. package/lib/core/realtime/connectionRooms.js +1 -1
  88. package/lib/core/realtime/hotelClerk.js +66 -24
  89. package/lib/core/realtime/index.js +1 -1
  90. package/lib/core/realtime/notification/document.js +1 -1
  91. package/lib/core/realtime/notification/index.js +1 -1
  92. package/lib/core/realtime/notification/server.js +1 -1
  93. package/lib/core/realtime/notification/user.js +1 -1
  94. package/lib/core/realtime/notifier.js +1 -1
  95. package/lib/core/realtime/room.js +1 -1
  96. package/lib/core/realtime/subscription.js +1 -1
  97. package/lib/core/security/index.js +1 -1
  98. package/lib/core/security/profileRepository.js +1 -1
  99. package/lib/core/security/roleRepository.js +1 -1
  100. package/lib/core/security/securityLoader.js +1 -1
  101. package/lib/core/security/tokenRepository.js +1 -1
  102. package/lib/core/security/userRepository.js +1 -1
  103. package/lib/core/shared/KoncordeWrapper.js +1 -1
  104. package/lib/core/shared/abstractManifest.js +1 -1
  105. package/lib/core/shared/repository.js +1 -1
  106. package/lib/core/shared/sdk/embeddedSdk.d.ts +2 -2
  107. package/lib/core/shared/sdk/embeddedSdk.js +4 -3
  108. package/lib/core/shared/sdk/funnelProtocol.d.ts +13 -0
  109. package/lib/core/shared/sdk/funnelProtocol.js +76 -81
  110. package/lib/core/shared/sdk/impersonatedSdk.js +1 -1
  111. package/lib/core/shared/store.js +1 -1
  112. package/lib/core/statistics/index.js +1 -1
  113. package/lib/core/statistics/statistics.js +1 -1
  114. package/lib/core/storage/clientAdapter.js +1 -1
  115. package/lib/core/storage/indexCache.js +1 -1
  116. package/lib/core/storage/storageEngine.js +1 -1
  117. package/lib/core/storage/storeScopeEnum.js +1 -1
  118. package/lib/core/validation/baseType.js +1 -1
  119. package/lib/core/validation/index.js +1 -1
  120. package/lib/core/validation/types/anything.js +1 -1
  121. package/lib/core/validation/types/boolean.js +1 -1
  122. package/lib/core/validation/types/date.js +1 -1
  123. package/lib/core/validation/types/email.js +1 -1
  124. package/lib/core/validation/types/enum.js +1 -1
  125. package/lib/core/validation/types/geoPoint.js +1 -1
  126. package/lib/core/validation/types/geoShape.js +1 -1
  127. package/lib/core/validation/types/integer.js +1 -1
  128. package/lib/core/validation/types/ipAddress.js +1 -1
  129. package/lib/core/validation/types/numeric.js +1 -1
  130. package/lib/core/validation/types/object.js +1 -1
  131. package/lib/core/validation/types/string.js +1 -1
  132. package/lib/core/validation/types/url.js +1 -1
  133. package/lib/core/validation/validation.js +1 -1
  134. package/lib/kerror/codes/1-services.json +1 -1
  135. package/lib/kerror/codes/index.js +1 -1
  136. package/lib/kerror/errors/badRequestError.js +1 -1
  137. package/lib/kerror/errors/externalServiceError.js +1 -1
  138. package/lib/kerror/errors/forbiddenError.js +1 -1
  139. package/lib/kerror/errors/gatewayTimeoutError.js +1 -1
  140. package/lib/kerror/errors/index.js +1 -1
  141. package/lib/kerror/errors/internalError.js +1 -1
  142. package/lib/kerror/errors/kuzzleError.d.ts +1 -1
  143. package/lib/kerror/errors/kuzzleError.js +1 -1
  144. package/lib/kerror/errors/multipleErrorsError.js +1 -1
  145. package/lib/kerror/errors/notFoundError.js +1 -1
  146. package/lib/kerror/errors/partialError.js +1 -1
  147. package/lib/kerror/errors/pluginImplementationError.js +1 -1
  148. package/lib/kerror/errors/preconditionError.js +1 -1
  149. package/lib/kerror/errors/serviceUnavailableError.js +1 -1
  150. package/lib/kerror/errors/sizeLimitError.js +1 -1
  151. package/lib/kerror/errors/tooManyRequestsError.js +1 -1
  152. package/lib/kerror/errors/unauthorizedError.js +1 -1
  153. package/lib/kerror/index.js +2 -4
  154. package/lib/kuzzle/dumpGenerator.js +1 -1
  155. package/lib/kuzzle/event/kuzzleEventEmitter.js +39 -1
  156. package/lib/kuzzle/event/pipeRunner.js +1 -1
  157. package/lib/kuzzle/event/waterfall.js +1 -1
  158. package/lib/kuzzle/index.js +1 -1
  159. package/lib/kuzzle/internalIndexHandler.js +1 -1
  160. package/lib/kuzzle/kuzzle.js +1 -1
  161. package/lib/kuzzle/kuzzleStateEnum.js +1 -1
  162. package/lib/kuzzle/log.js +1 -1
  163. package/lib/kuzzle/vault.js +1 -1
  164. package/lib/model/security/profile.js +1 -1
  165. package/lib/model/security/rights.js +1 -1
  166. package/lib/model/security/role.js +1 -1
  167. package/lib/model/security/token.js +1 -1
  168. package/lib/model/security/user.js +6 -7
  169. package/lib/model/storage/apiKey.js +1 -1
  170. package/lib/model/storage/baseModel.js +1 -1
  171. package/lib/service/cache/redis.js +1 -1
  172. package/lib/service/service.js +1 -1
  173. package/lib/service/storage/elasticsearch.js +108 -39
  174. package/lib/service/storage/esWrapper.js +1 -1
  175. package/lib/service/storage/queryTranslator.js +1 -1
  176. package/lib/types/Controller.js +1 -1
  177. package/lib/types/ControllerDefinition.js +1 -1
  178. package/lib/types/Deprecation.js +1 -1
  179. package/lib/types/EventHandler.js +1 -1
  180. package/lib/types/KuzzleDocument.d.ts +3 -0
  181. package/lib/types/Plugin.js +1 -1
  182. package/lib/types/PluginManifest.js +1 -1
  183. package/lib/types/RequestPayload.js +1 -1
  184. package/lib/types/ResponsePayload.js +1 -1
  185. package/lib/types/StrategyDefinition.js +1 -1
  186. package/lib/types/Token.js +1 -1
  187. package/lib/types/User.js +1 -1
  188. package/lib/types/index.d.ts +0 -1
  189. package/lib/types/index.js +1 -2
  190. package/lib/util/array.js +1 -1
  191. package/lib/util/assertType.js +1 -1
  192. package/lib/util/asyncStore.js +1 -1
  193. package/lib/util/debug.js +1 -1
  194. package/lib/util/deprecate.js +1 -1
  195. package/lib/util/didYouMean.js +1 -1
  196. package/lib/util/dump-collection.d.ts +35 -0
  197. package/lib/util/dump-collection.js +11 -8
  198. package/lib/util/extractFields.js +1 -1
  199. package/lib/util/inflector.js +1 -1
  200. package/lib/util/koncordeCompat.js +1 -1
  201. package/lib/util/memoize.js +1 -1
  202. package/lib/util/mutex.js +1 -1
  203. package/lib/util/name-generator.js +1 -1
  204. package/lib/util/promback.js +1 -1
  205. package/lib/util/requestAssertions.js +1 -1
  206. package/lib/util/safeObject.js +1 -1
  207. package/lib/util/stackTrace.js +56 -17
  208. package/lib/util/wildcard.js +1 -1
  209. package/package-lock.json +102 -83
  210. package/package.json +18 -17
  211. package/lib/core/network/removeErrorStack.js +0 -56
@@ -2,7 +2,7 @@
2
2
  * Kuzzle, a backend software, self-hostable and ready to use
3
3
  * to power modern apps
4
4
  *
5
- * Copyright 2015-2020 Kuzzle
5
+ * Copyright 2015-2022 Kuzzle
6
6
  * mailto: support AT kuzzle.io
7
7
  * website: http://kuzzle.io
8
8
  *
@@ -67,6 +67,7 @@ class KuzzleEventEmitter extends EventEmitter {
67
67
 
68
68
  this.corePipes = new Map();
69
69
  this.coreAnswerers = new Map();
70
+ this.coreSyncedAnswerers = new Map();
70
71
  }
71
72
 
72
73
  /**
@@ -101,6 +102,21 @@ class KuzzleEventEmitter extends EventEmitter {
101
102
  this.coreAnswerers.set(event, fn);
102
103
  }
103
104
 
105
+
106
+ /**
107
+ * Registers a core 'callback' answerer
108
+ * There can only be 0 or 1 answerer per callback event.
109
+ *
110
+ * @param {String} event
111
+ * @param {Function} fn
112
+ */
113
+ onCall (event, fn) {
114
+ assert(typeof fn === 'function', `Cannot register callback for event "${event}": "${fn}" is not a function`);
115
+ assert(! this.coreSyncedAnswerers.has(event), `Cannot register callback for event "${event}": a callback has already been registered`);
116
+
117
+ this.coreSyncedAnswerers.set(event, fn);
118
+ }
119
+
104
120
  /**
105
121
  * Emits an event and all its wildcarded versions
106
122
  *
@@ -199,6 +215,28 @@ class KuzzleEventEmitter extends EventEmitter {
199
215
  return response;
200
216
  }
201
217
 
218
+ /**
219
+ * Calls a callback to get information about the provided payload
220
+ */
221
+ call (event, ...payload) {
222
+ debug('Triggering callback "%s" with payload: %o', event, payload);
223
+
224
+ const fn = this.coreSyncedAnswerers.get(event);
225
+
226
+ if (! fn) {
227
+ throw kerror.get('core', 'fatal', 'assertion_failed', `the requested callback event '${event}' doesn't have an answerer`);
228
+ }
229
+
230
+ const response = fn(...payload);
231
+
232
+ getWildcardEvents(event).forEach(ev => super.emit(ev, {
233
+ args: payload,
234
+ response,
235
+ }));
236
+
237
+ return response;
238
+ }
239
+
202
240
  /**
203
241
  * Registers a plugin hook.
204
242
  * Catch any error in the handler and emit the hook:onError event.
@@ -2,7 +2,7 @@
2
2
  * Kuzzle, a backend software, self-hostable and ready to use
3
3
  * to power modern apps
4
4
  *
5
- * Copyright 2015-2020 Kuzzle
5
+ * Copyright 2015-2022 Kuzzle
6
6
  * mailto: support AT kuzzle.io
7
7
  * website: http://kuzzle.io
8
8
  *
@@ -2,7 +2,7 @@
2
2
  * Kuzzle, a backend software, self-hostable and ready to use
3
3
  * to power modern apps
4
4
  *
5
- * Copyright 2015-2020 Kuzzle
5
+ * Copyright 2015-2022 Kuzzle
6
6
  * mailto: support AT kuzzle.io
7
7
  * website: http://kuzzle.io
8
8
  *
@@ -2,7 +2,7 @@
2
2
  * Kuzzle, a backend software, self-hostable and ready to use
3
3
  * to power modern apps
4
4
  *
5
- * Copyright 2015-2020 Kuzzle
5
+ * Copyright 2015-2022 Kuzzle
6
6
  * mailto: support AT kuzzle.io
7
7
  * website: http://kuzzle.io
8
8
  *
@@ -2,7 +2,7 @@
2
2
  * Kuzzle, a backend software, self-hostable and ready to use
3
3
  * to power modern apps
4
4
  *
5
- * Copyright 2015-2020 Kuzzle
5
+ * Copyright 2015-2022 Kuzzle
6
6
  * mailto: support AT kuzzle.io
7
7
  * website: http://kuzzle.io
8
8
  *
@@ -3,7 +3,7 @@
3
3
  * Kuzzle, a backend software, self-hostable and ready to use
4
4
  * to power modern apps
5
5
  *
6
- * Copyright 2015-2020 Kuzzle
6
+ * Copyright 2015-2022 Kuzzle
7
7
  * mailto: support AT kuzzle.io
8
8
  * website: http://kuzzle.io
9
9
  *
@@ -2,7 +2,7 @@
2
2
  * Kuzzle, a backend software, self-hostable and ready to use
3
3
  * to power modern apps
4
4
  *
5
- * Copyright 2015-2020 Kuzzle
5
+ * Copyright 2015-2022 Kuzzle
6
6
  * mailto: support AT kuzzle.io
7
7
  * website: http://kuzzle.io
8
8
  *
package/lib/kuzzle/log.js CHANGED
@@ -2,7 +2,7 @@
2
2
  * Kuzzle, a backend software, self-hostable and ready to use
3
3
  * to power modern apps
4
4
  *
5
- * Copyright 2015-2020 Kuzzle
5
+ * Copyright 2015-2022 Kuzzle
6
6
  * mailto: support AT kuzzle.io
7
7
  * website: http://kuzzle.io
8
8
  *
@@ -2,7 +2,7 @@
2
2
  * Kuzzle, a backend software, self-hostable and ready to use
3
3
  * to power modern apps
4
4
  *
5
- * Copyright 2015-2020 Kuzzle
5
+ * Copyright 2015-2022 Kuzzle
6
6
  * mailto: support AT kuzzle.io
7
7
  * website: http://kuzzle.io
8
8
  *
@@ -3,7 +3,7 @@
3
3
  * Kuzzle, a backend software, self-hostable and ready to use
4
4
  * to power modern apps
5
5
  *
6
- * Copyright 2015-2020 Kuzzle
6
+ * Copyright 2015-2022 Kuzzle
7
7
  * mailto: support AT kuzzle.io
8
8
  * website: http://kuzzle.io
9
9
  *
@@ -2,7 +2,7 @@
2
2
  * Kuzzle, a backend software, self-hostable and ready to use
3
3
  * to power modern apps
4
4
  *
5
- * Copyright 2015-2020 Kuzzle
5
+ * Copyright 2015-2022 Kuzzle
6
6
  * mailto: support AT kuzzle.io
7
7
  * website: http://kuzzle.io
8
8
  *
@@ -3,7 +3,7 @@
3
3
  * Kuzzle, a backend software, self-hostable and ready to use
4
4
  * to power modern apps
5
5
  *
6
- * Copyright 2015-2020 Kuzzle
6
+ * Copyright 2015-2022 Kuzzle
7
7
  * mailto: support AT kuzzle.io
8
8
  * website: http://kuzzle.io
9
9
  *
@@ -3,7 +3,7 @@
3
3
  * Kuzzle, a backend software, self-hostable and ready to use
4
4
  * to power modern apps
5
5
  *
6
- * Copyright 2015-2020 Kuzzle
6
+ * Copyright 2015-2022 Kuzzle
7
7
  * mailto: support AT kuzzle.io
8
8
  * website: http://kuzzle.io
9
9
  *
@@ -3,7 +3,7 @@
3
3
  * Kuzzle, a backend software, self-hostable and ready to use
4
4
  * to power modern apps
5
5
  *
6
- * Copyright 2015-2020 Kuzzle
6
+ * Copyright 2015-2022 Kuzzle
7
7
  * mailto: support AT kuzzle.io
8
8
  * website: http://kuzzle.io
9
9
  *
@@ -47,9 +47,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
47
47
  };
48
48
  Object.defineProperty(exports, "__esModule", { value: true });
49
49
  exports.User = void 0;
50
- const rights_1 = __importDefault(require("./rights"));
51
- const bluebird_1 = __importDefault(require("bluebird"));
52
50
  const lodash_1 = __importDefault(require("lodash"));
51
+ const rights_1 = __importDefault(require("./rights"));
53
52
  const kerror = __importStar(require("../../kerror"));
54
53
  /**
55
54
  * @class User
@@ -73,7 +72,7 @@ class User {
73
72
  */
74
73
  async getRights() {
75
74
  const profiles = await this.getProfiles();
76
- const results = await bluebird_1.default.map(profiles, p => p.getRights());
75
+ const results = await Promise.all(profiles.map(p => p.getRights()));
77
76
  const rights = {};
78
77
  results.forEach(right => lodash_1.default.assignWith(rights, right, rights_1.default.merge));
79
78
  return rights;
@@ -97,15 +96,15 @@ class User {
97
96
  return false;
98
97
  }
99
98
  // Every target must be allowed by at least one profile
100
- return this.areTargetsAllowed(profiles, targets);
99
+ return this.areTargetsAllowed(request, profiles, targets);
101
100
  }
102
101
  /**
103
102
  * Verifies that every targets are allowed by at least one profile,
104
103
  * while skipping the ones that includes a wildcard since they will be expanded
105
104
  * later on, based on index and collections authorized for the given user.
106
105
  */
107
- async areTargetsAllowed(profiles, targets) {
108
- const profilesPolicies = await bluebird_1.default.map(profiles, profile => profile.getAllowedPolicies());
106
+ async areTargetsAllowed(request, profiles, targets) {
107
+ const profilesPolicies = await Promise.all(profiles.map(profile => profile.getAllowedPolicies(request)));
109
108
  // Every target must be allowed by at least one profile
110
109
  for (const target of targets) {
111
110
  // Skip targets with no Index or Collection
@@ -2,7 +2,7 @@
2
2
  * Kuzzle, a backend software, self-hostable and ready to use
3
3
  * to power modern apps
4
4
  *
5
- * Copyright 2015-2020 Kuzzle
5
+ * Copyright 2015-2022 Kuzzle
6
6
  * mailto: support AT kuzzle.io
7
7
  * website: http://kuzzle.io
8
8
  *
@@ -2,7 +2,7 @@
2
2
  * Kuzzle, a backend software, self-hostable and ready to use
3
3
  * to power modern apps
4
4
  *
5
- * Copyright 2015-2020 Kuzzle
5
+ * Copyright 2015-2022 Kuzzle
6
6
  * mailto: support AT kuzzle.io
7
7
  * website: http://kuzzle.io
8
8
  *
@@ -2,7 +2,7 @@
2
2
  * Kuzzle, a backend software, self-hostable and ready to use
3
3
  * to power modern apps
4
4
  *
5
- * Copyright 2015-2020 Kuzzle
5
+ * Copyright 2015-2022 Kuzzle
6
6
  * mailto: support AT kuzzle.io
7
7
  * website: http://kuzzle.io
8
8
  *
@@ -2,7 +2,7 @@
2
2
  * Kuzzle, a backend software, self-hostable and ready to use
3
3
  * to power modern apps
4
4
  *
5
- * Copyright 2015-2020 Kuzzle
5
+ * Copyright 2015-2022 Kuzzle
6
6
  * mailto: support AT kuzzle.io
7
7
  * website: http://kuzzle.io
8
8
  *
@@ -2,7 +2,7 @@
2
2
  * Kuzzle, a backend software, self-hostable and ready to use
3
3
  * to power modern apps
4
4
  *
5
- * Copyright 2015-2020 Kuzzle
5
+ * Copyright 2015-2022 Kuzzle
6
6
  * mailto: support AT kuzzle.io
7
7
  * website: http://kuzzle.io
8
8
  *
@@ -243,7 +243,8 @@ class ElasticSearch extends Service {
243
243
  continue;
244
244
  }
245
245
 
246
- const alias = await this._getAliasFromIndice(indice);
246
+ const aliases = await this._getAliasFromIndice(indice);
247
+ const alias = aliases[0];
247
248
  const indexName = this._extractIndex(alias);
248
249
  const collectionName = this._extractCollection(alias);
249
250
 
@@ -303,20 +304,20 @@ class ElasticSearch extends Service {
303
304
  }
304
305
  }
305
306
 
306
- let fetched = await global.kuzzle.ask('core:cache:internal:get', cacheKey);
307
+ const stringifiedScrollInfo = await global.kuzzle.ask('core:cache:internal:get', cacheKey);
307
308
 
308
- if (! fetched) {
309
+ if (! stringifiedScrollInfo) {
309
310
  throw kerror.get('unknown_scroll_id');
310
311
  }
311
312
 
312
- fetched = Number.parseInt(fetched);
313
+ const scrollInfo = JSON.parse(stringifiedScrollInfo);
313
314
 
314
315
  try {
315
316
  const { body } = await this._client.scroll(esRequest);
316
317
 
317
- fetched += body.hits.hits.length;
318
+ scrollInfo.fetched += body.hits.hits.length;
318
319
 
319
- if (fetched >= body.hits.total.value) {
320
+ if (scrollInfo.fetched >= body.hits.total.value) {
320
321
  debug('Last scroll page fetched: deleting scroll %s', body._scroll_id);
321
322
  await global.kuzzle.ask('core:cache:internal:del', cacheKey);
322
323
  await this.clearScroll(body._scroll_id);
@@ -325,15 +326,15 @@ class ElasticSearch extends Service {
325
326
  await global.kuzzle.ask(
326
327
  'core:cache:internal:store',
327
328
  cacheKey,
328
- fetched,
329
+ JSON.stringify(scrollInfo),
329
330
  {
330
331
  ttl: ms(_scrollTTL) || this.scrollTTL,
331
332
  });
332
333
  }
333
334
 
334
- body.remaining = body.hits.total.value - fetched;
335
+ body.remaining = body.hits.total.value - scrollInfo.fetched;
335
336
 
336
- return await this._formatSearchResult(body);
337
+ return await this._formatSearchResult(body, scrollInfo);
337
338
  }
338
339
  catch (error) {
339
340
  throw this._esWrapper.formatESError(error);
@@ -401,35 +402,101 @@ class ElasticSearch extends Service {
401
402
  await global.kuzzle.ask(
402
403
  'core:cache:internal:store',
403
404
  SCROLL_CACHE_PREFIX + global.kuzzle.hash(body._scroll_id),
404
- body.hits.hits.length,
405
+ JSON.stringify({
406
+ collection,
407
+ fetched: body.hits.hits.length,
408
+ index,
409
+ targets
410
+ }),
405
411
  { ttl });
406
412
 
407
413
  body.remaining = body.hits.total.value - body.hits.hits.length;
408
414
  }
409
415
 
410
- return await this._formatSearchResult(body);
416
+ return await this._formatSearchResult(body, {
417
+ collection,
418
+ index,
419
+ targets
420
+ });
411
421
  }
412
422
  catch (error) {
413
423
  throw this._esWrapper.formatESError(error);
414
424
  }
415
425
  }
416
426
 
417
- async _formatSearchResult (body) {
427
+ /**
428
+ * Generate a map that associate an alias to a pair of index and collection
429
+ *
430
+ * @param {*} targets
431
+ * @returns
432
+ */
433
+ _mapTargetsToAlias (targets) {
434
+ const aliasToTargets = {};
435
+
436
+ for (const target of targets) {
437
+ for (const targetCollection of target.collections) {
438
+ const alias = this._getAlias(target.index, targetCollection);
439
+ if (! aliasToTargets[alias]) {
440
+ aliasToTargets[alias] = {
441
+ collection: targetCollection,
442
+ index: target.index,
443
+ };
444
+ }
445
+ }
446
+ }
447
+
448
+ return aliasToTargets;
449
+ }
450
+
451
+ async _formatSearchResult (body, searchInfo = {}) {
452
+ let aliasToTargets = {};
453
+ const aliasCache = new Map();
454
+
455
+ if (searchInfo.targets) {
456
+ /**
457
+ * We need to map the alias to the target index and collection,
458
+ * so we can later retrieve informations about an index & collection
459
+ * based on its alias.
460
+ */
461
+ aliasToTargets = this._mapTargetsToAlias(searchInfo.targets);
462
+ }
418
463
 
419
464
  const formatHit = async (hit) => {
420
- let alias = null;
465
+ let index = searchInfo.index;
466
+ let collection = searchInfo.collection;
467
+
468
+ /**
469
+ * If the search has been done on multiple targets, we need to
470
+ * retrieve the appropriate index and collection based on the alias
471
+ */
472
+ if (hit._index && searchInfo.targets) {
473
+
474
+ // Caching to reduce call to ES
475
+ let aliases = aliasCache.get(hit._index);
476
+ if (! aliases) {
477
+ // Retrieve all the alias associated to one index
478
+ aliases = await this._getAliasFromIndice(hit._index);
479
+ aliasCache.set(hit._index, aliases);
480
+ }
421
481
 
422
- if (hit._index) {
423
- alias = await this._getAliasFromIndice(hit._index);
482
+ /**
483
+ * Since multiple alias can point to the same index in ES, we need to
484
+ * find the first alias that exists in the map of aliases associated
485
+ * to the targets.
486
+ */
487
+ const alias = aliases.find(_alias => aliasToTargets[_alias]);
488
+ // Retrieve index and collection information based on the matching alias
489
+ index = aliasToTargets[alias].index;
490
+ collection = aliasToTargets[alias].collection;
424
491
  }
425
492
 
426
493
  return {
427
494
  _id: hit._id,
428
495
  _score: hit._score,
429
496
  _source: hit._source,
430
- collection: alias ? this._extractCollection(alias) : undefined,
497
+ collection,
431
498
  highlight: hit.highlight,
432
- index: alias ? this._extractIndex(alias) : undefined,
499
+ index,
433
500
  };
434
501
  };
435
502
 
@@ -1819,22 +1886,14 @@ class ElasticSearch extends Service {
1819
1886
  index: await this._getIndice(index, collection)
1820
1887
  };
1821
1888
 
1822
- const mutex = new Mutex(`hiddenCollection/delete/${index}`);
1823
1889
  try {
1824
1890
  await this._client.indices.delete(esRequest);
1825
1891
 
1826
- await mutex.lock();
1827
-
1828
- if (! await this._hasHiddenCollection(index)) {
1829
- await this._createHiddenCollection(index);
1830
- }
1892
+ await this._createHiddenCollection(index);
1831
1893
  }
1832
1894
  catch (e) {
1833
1895
  throw this._esWrapper.formatESError(e);
1834
1896
  }
1835
- finally {
1836
- await mutex.unlock();
1837
- }
1838
1897
 
1839
1898
  return null;
1840
1899
  }
@@ -2148,7 +2207,7 @@ class ElasticSearch extends Service {
2148
2207
  index,
2149
2208
  collection,
2150
2209
  documents,
2151
- { refresh, timeout, userId = null, injectKuzzleMeta = true, limits = true } = {}) {
2210
+ { refresh, timeout, userId = null, injectKuzzleMeta = true, limits = true, source = true } = {}) {
2152
2211
  let kuzzleMeta = {};
2153
2212
 
2154
2213
  if (injectKuzzleMeta) {
@@ -2192,7 +2251,7 @@ class ElasticSearch extends Service {
2192
2251
  }
2193
2252
  /* end critical code section */
2194
2253
 
2195
- return this._mExecute(esRequest, extractedDocuments, rejected, { limits });
2254
+ return this._mExecute(esRequest, extractedDocuments, rejected, { limits, source });
2196
2255
  }
2197
2256
 
2198
2257
  /**
@@ -2564,7 +2623,7 @@ class ElasticSearch extends Service {
2564
2623
  *
2565
2624
  * @returns {Promise.<Object[]>} results
2566
2625
  */
2567
- async _mExecute (esRequest, documents, partialErrors, { limits = true } = {}) {
2626
+ async _mExecute (esRequest, documents, partialErrors, { limits = true, source = true } = {}) {
2568
2627
  assertWellFormedRefresh(esRequest);
2569
2628
 
2570
2629
  if ( limits
@@ -2618,7 +2677,7 @@ class ElasticSearch extends Service {
2618
2677
  else {
2619
2678
  successes.push({
2620
2679
  _id: result._id,
2621
- _source: documents[i]._source,
2680
+ _source: source ? documents[i]._source : undefined,
2622
2681
  _version: result._version,
2623
2682
  created: result.result === 'created',
2624
2683
  get: result.get,
@@ -2792,7 +2851,7 @@ class ElasticSearch extends Service {
2792
2851
  throw kerror.get('unknown_index_collection');
2793
2852
  }
2794
2853
  else if (body.length > 1) {
2795
- throw kerror.get('multiple_indice_alias', 'alias', 'indices');
2854
+ throw kerror.get('multiple_indice_alias', `"alias" starting with "${ALIAS_PREFIX}"`, '"indices"');
2796
2855
  }
2797
2856
 
2798
2857
  return body[0].index;
@@ -2838,20 +2897,19 @@ class ElasticSearch extends Service {
2838
2897
  * @param {String} indice
2839
2898
  *
2840
2899
  * @returns {String} Alias name (eg: '@&nepali.liia')
2841
- * @throws If there is not exactly one alias associated
2900
+ * @throws If there is not exactly one alias associated that is prefixed with @
2842
2901
  */
2843
2902
  async _getAliasFromIndice (indice) {
2844
2903
  const { body } = await this._client.indices.getAlias({ index: indice });
2845
- const aliases = Object.keys(body[indice].aliases);
2904
+ const aliases =
2905
+ Object.keys(body[indice].aliases)
2906
+ .filter(alias => alias.startsWith(ALIAS_PREFIX));
2846
2907
 
2847
2908
  if (aliases.length < 1) {
2848
2909
  throw kerror.get('unknown_index_collection');
2849
2910
  }
2850
- else if (aliases.length > 1) {
2851
- throw kerror.get('multiple_indice_alias', 'indice', 'aliases');
2852
- }
2853
2911
 
2854
- return aliases[0];
2912
+ return aliases;
2855
2913
  }
2856
2914
 
2857
2915
  /**
@@ -2960,13 +3018,21 @@ class ElasticSearch extends Service {
2960
3018
  }
2961
3019
 
2962
3020
  /**
2963
- * Creates the hidden collection on the provided index.
3021
+ * Creates the hidden collection on the provided index if it does not already
3022
+ * exists
2964
3023
  *
2965
3024
  * @param {String} index Index name
2966
3025
  */
2967
3026
  async _createHiddenCollection (index) {
3027
+ const mutex = new Mutex(`hiddenCollection/${index}`);
2968
3028
 
2969
3029
  try {
3030
+ await mutex.lock();
3031
+
3032
+ if (await this._hasHiddenCollection(index)) {
3033
+ return;
3034
+ }
3035
+
2970
3036
  await this._client.indices.create({
2971
3037
  body: {
2972
3038
  aliases: {
@@ -2983,6 +3049,9 @@ class ElasticSearch extends Service {
2983
3049
  catch (e) {
2984
3050
  throw this._esWrapper.formatESError(e);
2985
3051
  }
3052
+ finally {
3053
+ await mutex.unlock();
3054
+ }
2986
3055
  }
2987
3056
 
2988
3057
  /**
@@ -2,7 +2,7 @@
2
2
  * Kuzzle, a backend software, self-hostable and ready to use
3
3
  * to power modern apps
4
4
  *
5
- * Copyright 2015-2020 Kuzzle
5
+ * Copyright 2015-2022 Kuzzle
6
6
  * mailto: support AT kuzzle.io
7
7
  * website: http://kuzzle.io
8
8
  *
@@ -2,7 +2,7 @@
2
2
  * Kuzzle, a backend software, self-hostable and ready to use
3
3
  * to power modern apps
4
4
  *
5
- * Copyright 2015-2020 Kuzzle
5
+ * Copyright 2015-2022 Kuzzle
6
6
  * mailto: support AT kuzzle.io
7
7
  * website: http://kuzzle.io
8
8
  *
@@ -3,7 +3,7 @@
3
3
  * Kuzzle, a backend software, self-hostable and ready to use
4
4
  * to power modern apps
5
5
  *
6
- * Copyright 2015-2020 Kuzzle
6
+ * Copyright 2015-2022 Kuzzle
7
7
  * mailto: support AT kuzzle.io
8
8
  * website: http://kuzzle.io
9
9
  *
@@ -3,7 +3,7 @@
3
3
  * Kuzzle, a backend software, self-hostable and ready to use
4
4
  * to power modern apps
5
5
  *
6
- * Copyright 2015-2020 Kuzzle
6
+ * Copyright 2015-2022 Kuzzle
7
7
  * mailto: support AT kuzzle.io
8
8
  * website: http://kuzzle.io
9
9
  *
@@ -3,7 +3,7 @@
3
3
  * Kuzzle, a backend software, self-hostable and ready to use
4
4
  * to power modern apps
5
5
  *
6
- * Copyright 2015-2020 Kuzzle
6
+ * Copyright 2015-2022 Kuzzle
7
7
  * mailto: support AT kuzzle.io
8
8
  * website: http://kuzzle.io
9
9
  *
@@ -3,7 +3,7 @@
3
3
  * Kuzzle, a backend software, self-hostable and ready to use
4
4
  * to power modern apps
5
5
  *
6
- * Copyright 2015-2020 Kuzzle
6
+ * Copyright 2015-2022 Kuzzle
7
7
  * mailto: support AT kuzzle.io
8
8
  * website: http://kuzzle.io
9
9
  *
@@ -1,4 +1,7 @@
1
1
  import { JSONObject } from 'kuzzle-sdk';
2
+ /**
3
+ * @deprecated Use KDocument instead (See https://docs.kuzzle.io/sdk/js/7/essentials/strong-typing/)
4
+ */
2
5
  export interface KuzzleDocument {
3
6
  _id: string;
4
7
  _source: JSONObject;
@@ -3,7 +3,7 @@
3
3
  * Kuzzle, a backend software, self-hostable and ready to use
4
4
  * to power modern apps
5
5
  *
6
- * Copyright 2015-2020 Kuzzle
6
+ * Copyright 2015-2022 Kuzzle
7
7
  * mailto: support AT kuzzle.io
8
8
  * website: http://kuzzle.io
9
9
  *
@@ -3,7 +3,7 @@
3
3
  * Kuzzle, a backend software, self-hostable and ready to use
4
4
  * to power modern apps
5
5
  *
6
- * Copyright 2015-2020 Kuzzle
6
+ * Copyright 2015-2022 Kuzzle
7
7
  * mailto: support AT kuzzle.io
8
8
  * website: http://kuzzle.io
9
9
  *
@@ -3,7 +3,7 @@
3
3
  * Kuzzle, a backend software, self-hostable and ready to use
4
4
  * to power modern apps
5
5
  *
6
- * Copyright 2015-2020 Kuzzle
6
+ * Copyright 2015-2022 Kuzzle
7
7
  * mailto: support AT kuzzle.io
8
8
  * website: http://kuzzle.io
9
9
  *