kuzzle 2.19.2 → 2.19.5

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 (293) 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 +91 -90
  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 +4 -4
  71. package/lib/core/backend/backend.js +34 -31
  72. package/lib/core/backend/backendCluster.d.ts +8 -9
  73. package/lib/core/backend/backendCluster.js +8 -8
  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 +3 -3
  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 +3 -3
  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 +26 -25
  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 +3 -2
  227. package/lib/types/DebugModule.js +1 -1
  228. package/lib/types/EventHandler.d.ts +31 -1
  229. package/lib/types/Global.d.ts +1 -1
  230. package/lib/types/HttpStream.d.ts +2 -1
  231. package/lib/types/HttpStream.js +7 -5
  232. package/lib/types/Kuzzle.d.ts +1 -1
  233. package/lib/types/KuzzleDocument.d.ts +1 -1
  234. package/lib/types/OpenApiDefinition.d.ts +1 -1
  235. package/lib/types/PasswordPolicy.d.ts +1 -1
  236. package/lib/types/Plugin.d.ts +8 -8
  237. package/lib/types/Plugin.js +2 -2
  238. package/lib/types/Policy.d.ts +1 -1
  239. package/lib/types/RequestPayload.d.ts +1 -1
  240. package/lib/types/ResponsePayload.d.ts +1 -1
  241. package/lib/types/Token.d.ts +1 -1
  242. package/lib/types/User.d.ts +1 -1
  243. package/lib/types/config/DumpConfiguration.d.ts +8 -8
  244. package/lib/types/config/HttpConfiguration.d.ts +1 -1
  245. package/lib/types/config/KuzzleConfiguration.d.ts +1 -1
  246. package/lib/types/config/LimitsConfiguration.d.ts +8 -8
  247. package/lib/types/config/PluginsConfiguration.d.ts +4 -4
  248. package/lib/types/config/SecurityConfiguration.d.ts +62 -62
  249. package/lib/types/config/ServerConfiguration.d.ts +55 -55
  250. package/lib/types/config/ServicesConfiguration.d.ts +2 -2
  251. package/lib/types/config/internalCache/InternalCacheRedisConfiguration.d.ts +10 -10
  252. package/lib/types/config/publicCache/PublicCacheRedisConfiguration.d.ts +3 -3
  253. package/lib/types/config/storageEngine/StorageEngineElasticsearchConfiguration.d.ts +194 -110
  254. package/lib/types/errors/ErrorDefinition.d.ts +1 -1
  255. package/lib/types/errors/ErrorDomains.d.ts +1 -1
  256. package/lib/types/events/EventGenericDocument.d.ts +26 -0
  257. package/lib/types/events/EventGenericDocument.js +3 -0
  258. package/lib/types/index.d.ts +39 -38
  259. package/lib/types/index.js +1 -0
  260. package/lib/types/realtime/RealtimeScope.d.ts +1 -1
  261. package/lib/types/realtime/RealtimeUsers.d.ts +1 -1
  262. package/lib/util/assertType.js +13 -11
  263. package/lib/util/async.d.ts +1 -0
  264. package/lib/util/async.js +61 -0
  265. package/lib/util/asyncStore.js +19 -21
  266. package/lib/util/bufferedPassThrough.d.ts +3 -2
  267. package/lib/util/bufferedPassThrough.js +4 -4
  268. package/lib/util/bytes.js +9 -13
  269. package/lib/util/crypto.js +1 -1
  270. package/lib/util/debug.js +5 -5
  271. package/lib/util/deprecate.js +24 -21
  272. package/lib/util/didYouMean.js +7 -7
  273. package/lib/util/dump-collection.d.ts +2 -2
  274. package/lib/util/dump-collection.js +26 -26
  275. package/lib/util/esRequest.d.ts +1 -0
  276. package/lib/util/esRequest.js +62 -0
  277. package/lib/util/extractFields.js +24 -25
  278. package/lib/util/inflector.js +5 -5
  279. package/lib/util/koncordeCompat.d.ts +2 -2
  280. package/lib/util/koncordeCompat.js +5 -5
  281. package/lib/util/memoize.js +3 -5
  282. package/lib/util/mutex.d.ts +19 -1
  283. package/lib/util/mutex.js +39 -12
  284. package/lib/util/name-generator.js +1331 -1331
  285. package/lib/util/promback.js +8 -10
  286. package/lib/util/readYamlFile.d.ts +1 -1
  287. package/lib/util/readYamlFile.js +1 -1
  288. package/lib/util/requestAssertions.js +34 -34
  289. package/lib/util/safeObject.js +5 -5
  290. package/lib/util/stackTrace.js +20 -22
  291. package/lib/util/wildcard.js +15 -15
  292. package/package.json +28 -29
  293. package/npm-shrinkwrap.json +0 -19422
@@ -19,11 +19,12 @@
19
19
  * limitations under the License.
20
20
  */
21
21
 
22
- 'use strict';
22
+ "use strict";
23
23
 
24
- const Bluebird = require('bluebird');
25
-
26
- const { Mutex } = require('../../util/mutex');
24
+ const { Mutex } = require("../../util/mutex");
25
+ const { promiseAllN } = require("../../util/async");
26
+ const kerror = require("../../kerror");
27
+ const { getESIndexDynamicSettings } = require("../../util/esRequest");
27
28
 
28
29
  /**
29
30
  * Wrapper around the document store.
@@ -36,7 +37,7 @@ class Store {
36
37
  * @param {String} index
37
38
  * @param {storeScopeEnum} scope
38
39
  */
39
- constructor (index, scope) {
40
+ constructor(index, scope) {
40
41
  this.index = index;
41
42
  this.scope = scope;
42
43
 
@@ -53,6 +54,7 @@ class Store {
53
54
  exists: `core:storage:${scope}:document:exist`,
54
55
  get: `core:storage:${scope}:document:get`,
55
56
  getMapping: `core:storage:${scope}:mappings:get`,
57
+ getSettings: `core:storage:${scope}:collection:settings:get`,
56
58
  mExecute: `core:storage:${scope}:document:mExecute`,
57
59
  mGet: `core:storage:${scope}:document:mGet`,
58
60
  refreshCollection: `core:storage:${scope}:collection:refresh`,
@@ -71,17 +73,20 @@ class Store {
71
73
 
72
74
  // the scroll and multiSearch method are special: they doesn't need an index parameter
73
75
  // we keep them for ease of use
74
- this.scroll = (scrollId, opts) => global.kuzzle.ask(
75
- `core:storage:${scope}:document:scroll`,
76
- scrollId,
77
- opts);
78
-
79
- this.multiSearch = (targets, searchBody, opts) => global.kuzzle.ask(
80
- `core:storage:${scope}:document:multiSearch`,
81
- targets,
82
- searchBody,
83
- opts
84
- );
76
+ this.scroll = (scrollId, opts) =>
77
+ global.kuzzle.ask(
78
+ `core:storage:${scope}:document:scroll`,
79
+ scrollId,
80
+ opts
81
+ );
82
+
83
+ this.multiSearch = (targets, searchBody, opts) =>
84
+ global.kuzzle.ask(
85
+ `core:storage:${scope}:document:multiSearch`,
86
+ targets,
87
+ searchBody,
88
+ opts
89
+ );
85
90
  }
86
91
 
87
92
  /**
@@ -91,19 +96,26 @@ class Store {
91
96
  *
92
97
  * @returns {Promise}
93
98
  */
94
- async init (collections = {}) {
95
- const mutex = new Mutex(`Store.init(${this.index})`, {
96
- timeout: -1,
99
+ async init(collections = {}) {
100
+ const creatingMutex = new Mutex(`Store.init(${this.index})`, {
101
+ timeout: 0,
97
102
  ttl: 30000,
98
103
  });
99
104
 
100
- await mutex.lock();
105
+ if (await creatingMutex.lock()) {
106
+ try {
107
+ await this.createCollections(collections, {
108
+ indexCacheOnly: false,
109
+ });
110
+ } finally {
111
+ await creatingMutex.unlock();
112
+ }
113
+ } else {
114
+ await creatingMutex.wait({ timeout: -1 });
101
115
 
102
- try {
103
- await this.createCollections(collections);
104
- }
105
- finally {
106
- await mutex.unlock();
116
+ await this.createCollections(collections, {
117
+ indexCacheOnly: true,
118
+ });
107
119
  }
108
120
  }
109
121
 
@@ -114,17 +126,100 @@ class Store {
114
126
  *
115
127
  * @returns {Promise}
116
128
  */
117
- createCollections (collections) {
118
- return Bluebird.map(
119
- Object.entries(collections),
120
- ([collection, mappings]) => {
129
+ createCollections(collections, { indexCacheOnly = false } = {}) {
130
+ return promiseAllN(
131
+ Object.entries(collections).map(([collection, config]) => async () => {
132
+ // @deprecated
133
+ if (!(config.mappings !== undefined && config.settings !== undefined)) {
134
+ // @deprecated
135
+ return global.kuzzle.ask(
136
+ `core:storage:${this.scope}:collection:create`,
137
+ this.index,
138
+ collection,
139
+ { mappings: config },
140
+ { indexCacheOnly }
141
+ );
142
+ }
143
+
144
+ // @deprecated
145
+ const isConfigDeprecated =
146
+ config.settings.number_of_shards === undefined &&
147
+ config.settings.number_of_replicas === undefined;
148
+
149
+ if (indexCacheOnly) {
150
+ return global.kuzzle.ask(
151
+ `core:storage:${this.scope}:collection:create`,
152
+ this.index,
153
+ collection,
154
+ // @deprecated
155
+ isConfigDeprecated ? { mappings: config.mappings } : config,
156
+ { indexCacheOnly }
157
+ );
158
+ }
159
+
160
+ const exist = await global.kuzzle.ask(
161
+ `core:storage:${this.scope}:collection:exist`,
162
+ this.index,
163
+ collection
164
+ );
165
+
166
+ if (exist) {
167
+ // @deprecated
168
+ const dynamicSettings = isConfigDeprecated
169
+ ? null
170
+ : getESIndexDynamicSettings(config.settings);
171
+
172
+ const existingSettings = await global.kuzzle.ask(
173
+ `core:storage:${this.scope}:collection:settings:get`,
174
+ this.index,
175
+ collection
176
+ );
177
+
178
+ if (
179
+ !isConfigDeprecated &&
180
+ parseInt(existingSettings.number_of_shards) !==
181
+ config.settings.number_of_shards
182
+ ) {
183
+ if (global.NODE_ENV === "development") {
184
+ throw kerror.get(
185
+ "storage",
186
+ "wrong_collection_number_of_shards",
187
+ collection,
188
+ this.index,
189
+ this.scope,
190
+ "number_of_shards",
191
+ config.settings.number_of_shards,
192
+ existingSettings.number_of_shards
193
+ );
194
+ }
195
+ global.kuzzle.log.warn(
196
+ `Attempt to recreate an existing collection ${collection} of index ${this.index} of scope ${this.scope} with non matching static setting : number_of_shards at ${config.settings.number_of_shards} while existing one is at ${existingSettings.number_of_shards}`
197
+ );
198
+ }
199
+
200
+ return global.kuzzle.ask(
201
+ `core:storage:${this.scope}:collection:create`,
202
+ this.index,
203
+ collection,
204
+ // @deprecated
205
+ isConfigDeprecated
206
+ ? { mappings: config.mappings }
207
+ : { mappings: config.mappings, settings: dynamicSettings },
208
+ { indexCacheOnly: true }
209
+ );
210
+ }
211
+
121
212
  return global.kuzzle.ask(
122
213
  `core:storage:${this.scope}:collection:create`,
123
214
  this.index,
124
215
  collection,
125
- { mappings });
126
- },
127
- { concurrency: 10 });
216
+ // @deprecated
217
+ isConfigDeprecated ? { mappings: config.mappings } : config,
218
+ { indexCacheOnly }
219
+ );
220
+ }),
221
+ 10
222
+ );
128
223
  }
129
224
  }
130
225
 
@@ -19,6 +19,6 @@
19
19
  * limitations under the License.
20
20
  */
21
21
 
22
- 'use strict';
22
+ "use strict";
23
23
 
24
- module.exports = require('./statistics');
24
+ module.exports = require("./statistics");
@@ -19,21 +19,21 @@
19
19
  * limitations under the License.
20
20
  */
21
21
 
22
- 'use strict';
22
+ "use strict";
23
23
 
24
- const errorApiAssert = require('../../kerror').wrap('api', 'assert');
25
- const errorStats = require('../../kerror').wrap('services', 'stats');
24
+ const errorApiAssert = require("../../kerror").wrap("api", "assert");
25
+ const errorStats = require("../../kerror").wrap("services", "stats");
26
26
 
27
27
  /**
28
28
  * @class Statistics
29
29
  * @param {Kuzzle} kuzzle
30
30
  */
31
31
  class Statistics {
32
- constructor () {
32
+ constructor() {
33
33
  // uses '{' and '}' to force all statistics frames to be stored on 1 redis
34
34
  // node
35
35
  // (see https://redis.io/topics/cluster-spec#keys-distribution-model)
36
- this.cacheKeyPrefix = '{stats/}';
36
+ this.cacheKeyPrefix = "{stats/}";
37
37
 
38
38
  this.enabled = global.kuzzle.config.stats.enabled;
39
39
  this.ttl = global.kuzzle.config.stats.ttl * 1000;
@@ -45,7 +45,7 @@ class Statistics {
45
45
  completedRequests: new Map(),
46
46
  connections: new Map(),
47
47
  failedRequests: new Map(),
48
- ongoingRequests: new Map()
48
+ ongoingRequests: new Map(),
49
49
  };
50
50
  }
51
51
 
@@ -54,24 +54,24 @@ class Statistics {
54
54
  *
55
55
  * @param {Request} request
56
56
  */
57
- startRequest (request) {
58
- if (! this.enabled) {
57
+ startRequest(request) {
58
+ if (!this.enabled) {
59
59
  return;
60
60
  }
61
61
 
62
62
  const protocol = request && request.context.connection.protocol;
63
63
 
64
- if (! protocol) {
64
+ if (!protocol) {
65
65
  return;
66
66
  }
67
67
 
68
- if (! this.currentStats.ongoingRequests.has(protocol)) {
68
+ if (!this.currentStats.ongoingRequests.has(protocol)) {
69
69
  this.currentStats.ongoingRequests.set(protocol, 1);
70
- }
71
- else {
70
+ } else {
72
71
  this.currentStats.ongoingRequests.set(
73
72
  protocol,
74
- this.currentStats.ongoingRequests.get(protocol) + 1);
73
+ this.currentStats.ongoingRequests.get(protocol) + 1
74
+ );
75
75
  }
76
76
  }
77
77
 
@@ -80,28 +80,29 @@ class Statistics {
80
80
  *
81
81
  * @param {Request} request
82
82
  */
83
- completedRequest (request) {
84
- if (! this.enabled) {
83
+ completedRequest(request) {
84
+ if (!this.enabled) {
85
85
  return;
86
86
  }
87
87
 
88
88
  const protocol = request && request.context.connection.protocol;
89
89
 
90
- if (! protocol) {
90
+ if (!protocol) {
91
91
  return;
92
92
  }
93
93
 
94
94
  this.currentStats.ongoingRequests.set(
95
95
  protocol,
96
- this.currentStats.ongoingRequests.get(protocol) - 1);
96
+ this.currentStats.ongoingRequests.get(protocol) - 1
97
+ );
97
98
 
98
- if (! this.currentStats.completedRequests.has(protocol)) {
99
+ if (!this.currentStats.completedRequests.has(protocol)) {
99
100
  this.currentStats.completedRequests.set(protocol, 1);
100
- }
101
- else {
101
+ } else {
102
102
  this.currentStats.completedRequests.set(
103
103
  protocol,
104
- this.currentStats.completedRequests.get(protocol) + 1);
104
+ this.currentStats.completedRequests.get(protocol) + 1
105
+ );
105
106
  }
106
107
  }
107
108
 
@@ -110,28 +111,29 @@ class Statistics {
110
111
  *
111
112
  * @param {Request} request
112
113
  */
113
- failedRequest (request) {
114
- if (! this.enabled) {
114
+ failedRequest(request) {
115
+ if (!this.enabled) {
115
116
  return;
116
117
  }
117
118
 
118
119
  const protocol = request && request.context.connection.protocol;
119
120
 
120
- if (! protocol) {
121
+ if (!protocol) {
121
122
  return;
122
123
  }
123
124
 
124
125
  this.currentStats.ongoingRequests.set(
125
126
  protocol,
126
- this.currentStats.ongoingRequests.get(protocol) - 1);
127
+ this.currentStats.ongoingRequests.get(protocol) - 1
128
+ );
127
129
 
128
- if (! this.currentStats.failedRequests.has(protocol)) {
130
+ if (!this.currentStats.failedRequests.has(protocol)) {
129
131
  this.currentStats.failedRequests.set(protocol, 1);
130
- }
131
- else {
132
+ } else {
132
133
  this.currentStats.failedRequests.set(
133
134
  protocol,
134
- this.currentStats.failedRequests.get(protocol) + 1);
135
+ this.currentStats.failedRequests.get(protocol) + 1
136
+ );
135
137
  }
136
138
  }
137
139
 
@@ -140,22 +142,25 @@ class Statistics {
140
142
  *
141
143
  * @param {RequestContext} requestContext
142
144
  */
143
- newConnection (requestContext) {
144
- if (! this.enabled) {
145
+ newConnection(requestContext) {
146
+ if (!this.enabled) {
145
147
  return;
146
148
  }
147
149
 
148
- if (! requestContext.connection.protocol) {
150
+ if (!requestContext.connection.protocol) {
149
151
  return;
150
152
  }
151
153
 
152
- if (! this.currentStats.connections.has(requestContext.connection.protocol)) {
154
+ if (
155
+ !this.currentStats.connections.has(requestContext.connection.protocol)
156
+ ) {
153
157
  this.currentStats.connections.set(requestContext.connection.protocol, 1);
154
- }
155
- else {
158
+ } else {
156
159
  this.currentStats.connections.set(
157
160
  requestContext.connection.protocol,
158
- this.currentStats.connections.get(requestContext.connection.protocol) + 1);
161
+ this.currentStats.connections.get(requestContext.connection.protocol) +
162
+ 1
163
+ );
159
164
  }
160
165
  }
161
166
 
@@ -164,22 +169,26 @@ class Statistics {
164
169
  *
165
170
  * @param {RequestContext} requestContext
166
171
  */
167
- dropConnection (requestContext) {
168
- if (! this.enabled) {
172
+ dropConnection(requestContext) {
173
+ if (!this.enabled) {
169
174
  return;
170
175
  }
171
176
 
172
- if (! requestContext.connection.protocol) {
177
+ if (!requestContext.connection.protocol) {
173
178
  return;
174
179
  }
175
180
 
176
- if (this.currentStats.connections.get(requestContext.connection.protocol) === 1) {
181
+ if (
182
+ this.currentStats.connections.get(requestContext.connection.protocol) ===
183
+ 1
184
+ ) {
177
185
  this.currentStats.connections.delete(requestContext.connection.protocol);
178
- }
179
- else {
186
+ } else {
180
187
  this.currentStats.connections.set(
181
188
  requestContext.connection.protocol,
182
- this.currentStats.connections.get(requestContext.connection.protocol) - 1);
189
+ this.currentStats.connections.get(requestContext.connection.protocol) -
190
+ 1
191
+ );
183
192
  }
184
193
  }
185
194
 
@@ -188,22 +197,24 @@ class Statistics {
188
197
  *
189
198
  * @returns {Promise}
190
199
  */
191
- async getLastStats () {
192
- if (! this.enabled) {
193
- throw errorStats.get('not_available');
200
+ async getLastStats() {
201
+ if (!this.enabled) {
202
+ throw errorStats.get("not_available");
194
203
  }
195
204
 
196
205
  const frame = Object.assign(
197
- { timestamp: (new Date()).getTime() },
198
- this.currentStats);
206
+ { timestamp: new Date().getTime() },
207
+ this.currentStats
208
+ );
199
209
 
200
- if (! this.lastFrame) {
210
+ if (!this.lastFrame) {
201
211
  return frame;
202
212
  }
203
213
 
204
214
  const last = await global.kuzzle.ask(
205
- 'core:cache:internal:get',
206
- this.cacheKeyPrefix + this.lastFrame);
215
+ "core:cache:internal:get",
216
+ this.cacheKeyPrefix + this.lastFrame
217
+ );
207
218
 
208
219
  return Object.assign(frame, JSON.parse(last));
209
220
  }
@@ -214,20 +225,19 @@ class Statistics {
214
225
  * @param {Request} request
215
226
  * @returns {Promise<Object>}
216
227
  */
217
- async getStats (request) {
218
- if (! this.enabled) {
219
- throw errorStats.get('not_available');
228
+ async getStats(request) {
229
+ if (!this.enabled) {
230
+ throw errorStats.get("not_available");
220
231
  }
221
232
 
222
233
  const response = {
223
234
  hits: [],
224
- total: null
235
+ total: null,
225
236
  };
226
237
  const currentDate = new Date().getTime();
227
238
  let startTime;
228
239
  let stopTime;
229
240
 
230
-
231
241
  if (request && request.input.args && request.input.args.startTime) {
232
242
  startTime = isNaN(request.input.args.startTime)
233
243
  ? new Date(request.input.args.startTime).getTime()
@@ -241,11 +251,11 @@ class Statistics {
241
251
  }
242
252
 
243
253
  if (startTime !== undefined && isNaN(startTime)) {
244
- throw errorApiAssert.get('invalid_argument', 'startTime', 'number');
254
+ throw errorApiAssert.get("invalid_argument", "startTime", "number");
245
255
  }
246
256
 
247
257
  if (stopTime !== undefined && isNaN(stopTime)) {
248
- throw errorApiAssert.get('invalid_argument', 'stopTime', 'number');
258
+ throw errorApiAssert.get("invalid_argument", "stopTime", "number");
249
259
  }
250
260
 
251
261
  if (startTime !== undefined && startTime >= currentDate) {
@@ -254,18 +264,19 @@ class Statistics {
254
264
  }
255
265
 
256
266
  const stats = {
257
- completedRequests: Object.fromEntries(this.currentStats.completedRequests),
267
+ completedRequests: Object.fromEntries(
268
+ this.currentStats.completedRequests
269
+ ),
258
270
  connections: Object.fromEntries(this.currentStats.connections),
259
271
  failedRequests: Object.fromEntries(this.currentStats.failedRequests),
260
272
  ongoingRequests: Object.fromEntries(this.currentStats.ongoingRequests),
261
273
  };
262
274
 
263
- if (! this.lastFrame) {
264
- if (! stopTime || stopTime >= currentDate) {
275
+ if (!this.lastFrame) {
276
+ if (!stopTime || stopTime >= currentDate) {
265
277
  response.hits.push(
266
- Object.assign(
267
- { timestamp: (new Date(currentDate)).getTime() },
268
- stats));
278
+ Object.assign({ timestamp: new Date(currentDate).getTime() }, stats)
279
+ );
269
280
  }
270
281
 
271
282
  response.total = response.hits.length;
@@ -274,27 +285,31 @@ class Statistics {
274
285
  }
275
286
 
276
287
  const frames = await global.kuzzle.ask(
277
- 'core:cache:internal:searchKeys',
278
- `${this.cacheKeyPrefix}*`);
288
+ "core:cache:internal:searchKeys",
289
+ `${this.cacheKeyPrefix}*`
290
+ );
279
291
 
280
292
  // Statistics keys are timestamp.
281
293
  // Ordering them guarantees stats frames to be returned in the right order
282
294
  const values = await global.kuzzle.ask(
283
- 'core:cache:internal:mget',
284
- frames.sort());
295
+ "core:cache:internal:mget",
296
+ frames.sort()
297
+ );
285
298
 
286
299
  values.forEach((v, idx) => {
287
300
  const regex = new RegExp(`^${this.cacheKeyPrefix}`);
288
- const frameDate = new Date(Number(frames[idx].replace(regex, '')));
301
+ const frameDate = new Date(Number(frames[idx].replace(regex, "")));
289
302
  const frameDateTime = frameDate.getTime();
290
303
 
291
- if ( (! startTime || startTime <= frameDateTime)
292
- && (! stopTime || stopTime >= frameDateTime)
304
+ if (
305
+ (!startTime || startTime <= frameDateTime) &&
306
+ (!stopTime || stopTime >= frameDateTime)
293
307
  ) {
294
308
  response.hits.push(
295
- Object.assign(
296
- JSON.parse(v),
297
- { timestamp: (new Date(frameDateTime)).getTime() }));
309
+ Object.assign(JSON.parse(v), {
310
+ timestamp: new Date(frameDateTime).getTime(),
311
+ })
312
+ );
298
313
  }
299
314
  });
300
315
 
@@ -308,34 +323,33 @@ class Statistics {
308
323
  *
309
324
  * @returns {Promise<Object>}
310
325
  */
311
- getAllStats () {
326
+ getAllStats() {
312
327
  return this.getStats();
313
328
  }
314
329
 
315
330
  /**
316
331
  * Init statistics component
317
332
  */
318
- init () {
319
- if (! this.enabled) {
333
+ init() {
334
+ if (!this.enabled) {
320
335
  return;
321
336
  }
322
337
 
323
338
  this.timer = setInterval(async () => {
324
339
  try {
325
340
  await this.writeStats();
326
- }
327
- catch (error) {
341
+ } catch (error) {
328
342
  global.kuzzle.log.error(`Cannot write stats frame: ${error}`);
329
343
  }
330
344
  }, this.interval);
331
345
 
332
- global.kuzzle.on('core:cache:internal:flushdb', () => {
346
+ global.kuzzle.on("core:cache:internal:flushdb", () => {
333
347
  this.lastFrame = null;
334
348
  });
335
349
  }
336
350
 
337
- async writeStats () {
338
- if (! this.enabled) {
351
+ async writeStats() {
352
+ if (!this.enabled) {
339
353
  return;
340
354
  }
341
355
 
@@ -346,12 +360,12 @@ class Statistics {
346
360
  this.currentStats.failedRequests = new Map();
347
361
 
348
362
  await global.kuzzle.ask(
349
- 'core:cache:internal:store',
363
+ "core:cache:internal:store",
350
364
  this.cacheKeyPrefix + this.lastFrame,
351
365
  stats,
352
- { ttl: this.ttl });
366
+ { ttl: this.ttl }
367
+ );
353
368
  }
354
369
  }
355
370
 
356
-
357
371
  module.exports = Statistics;