kuzzle 2.16.11 → 2.17.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 (187) hide show
  1. package/lib/api/controllers/adminController.js +3 -3
  2. package/lib/api/controllers/authController.js +12 -12
  3. package/lib/api/controllers/baseController.js +60 -3
  4. package/lib/api/controllers/clusterController.js +1 -1
  5. package/lib/api/controllers/collectionController.js +7 -5
  6. package/lib/api/controllers/documentController.js +130 -17
  7. package/lib/api/controllers/indexController.js +1 -1
  8. package/lib/api/controllers/memoryStorageController.js +39 -38
  9. package/lib/api/controllers/realtimeController.js +1 -1
  10. package/lib/api/controllers/securityController.js +50 -50
  11. package/lib/api/controllers/serverController.js +73 -27
  12. package/lib/api/documentExtractor.js +3 -3
  13. package/lib/api/funnel.js +44 -21
  14. package/lib/api/httpRoutes.js +9 -4
  15. package/lib/api/openapi/OpenApiManager.d.ts +11 -0
  16. package/lib/api/openapi/OpenApiManager.js +96 -0
  17. package/lib/api/openapi/{document → components/document}/count.yaml +2 -2
  18. package/lib/api/openapi/{document → components/document}/create.yaml +2 -2
  19. package/lib/api/openapi/{document → components/document}/createOrReplace.yaml +2 -2
  20. package/lib/api/openapi/{document → components/document}/delete.yaml +1 -1
  21. package/lib/api/openapi/{document → components/document}/deleteByQuery.yaml +2 -2
  22. package/lib/api/openapi/{document → components/document}/exists.yaml +1 -1
  23. package/lib/api/openapi/{document → components/document}/get.yaml +1 -1
  24. package/lib/api/openapi/{document → components/document}/index.d.ts +2 -0
  25. package/lib/api/openapi/{document → components/document}/index.js +7 -2
  26. package/lib/api/openapi/{document → components/document}/replace.yaml +2 -2
  27. package/lib/api/openapi/{document → components/document}/scroll.yaml +1 -1
  28. package/lib/api/openapi/{document → components/document}/update.yaml +2 -2
  29. package/lib/api/openapi/components/document/validate.yaml +42 -0
  30. package/lib/api/openapi/components/index.d.ts +2 -0
  31. package/lib/api/openapi/components/index.js +18 -0
  32. package/lib/api/openapi/{payloads.yaml → components/payloads.yaml} +0 -0
  33. package/lib/api/openapi/index.d.ts +1 -2
  34. package/lib/api/openapi/index.js +1 -5
  35. package/lib/api/openapi/openApiGenerator.d.ts +7 -0
  36. package/lib/api/openapi/openApiGenerator.js +133 -0
  37. package/lib/api/request/kuzzleRequest.js +8 -6
  38. package/lib/cluster/node.js +9 -9
  39. package/lib/cluster/publisher.js +1 -1
  40. package/lib/cluster/state.js +20 -4
  41. package/lib/cluster/subscriber.js +1 -1
  42. package/lib/cluster/workers/IDCardRenewer.js +2 -2
  43. package/lib/config/default.config.js +1 -0
  44. package/lib/config/index.js +6 -6
  45. package/lib/core/auth/passportResponse.js +6 -6
  46. package/lib/core/auth/passportWrapper.js +5 -5
  47. package/lib/core/backend/backend.d.ts +11 -3
  48. package/lib/core/backend/backend.js +22 -17
  49. package/lib/core/backend/backendConfig.d.ts +5 -1
  50. package/lib/core/backend/backendConfig.js +25 -2
  51. package/lib/core/backend/backendController.js +21 -5
  52. package/lib/core/backend/backendErrors.d.ts +58 -0
  53. package/lib/core/backend/backendErrors.js +121 -0
  54. package/lib/core/backend/backendHook.js +21 -5
  55. package/lib/core/backend/backendImport.js +21 -5
  56. package/lib/core/backend/backendOpenApi.d.ts +9 -0
  57. package/lib/core/backend/backendOpenApi.js +69 -0
  58. package/lib/core/backend/backendPipe.js +21 -5
  59. package/lib/core/backend/backendPlugin.js +22 -3
  60. package/lib/core/backend/backendVault.js +21 -2
  61. package/lib/core/backend/index.d.ts +2 -0
  62. package/lib/core/backend/index.js +2 -0
  63. package/lib/core/network/accessLogger.js +6 -6
  64. package/lib/core/network/clientConnection.js +1 -1
  65. package/lib/core/network/entryPoint.js +5 -5
  66. package/lib/core/network/httpRouter/index.js +5 -5
  67. package/lib/core/network/httpRouter/routeHandler.js +3 -3
  68. package/lib/core/network/httpRouter/routePart.js +5 -5
  69. package/lib/core/network/protocolManifest.js +1 -1
  70. package/lib/core/network/protocols/httpMessage.js +2 -2
  71. package/lib/core/network/protocols/httpwsProtocol.js +228 -50
  72. package/lib/core/network/protocols/mqttProtocol.js +3 -3
  73. package/lib/core/network/protocols/protocol.js +3 -3
  74. package/lib/core/network/router.js +7 -6
  75. package/lib/core/plugin/plugin.js +38 -64
  76. package/lib/core/plugin/pluginContext.js +22 -3
  77. package/lib/core/plugin/pluginManifest.js +3 -3
  78. package/lib/core/plugin/pluginRepository.js +5 -5
  79. package/lib/core/plugin/pluginsManager.js +29 -28
  80. package/lib/core/realtime/channel.js +20 -4
  81. package/lib/core/realtime/hotelClerk.js +24 -5
  82. package/lib/core/realtime/notification/server.js +1 -1
  83. package/lib/core/realtime/notification/user.js +1 -1
  84. package/lib/core/realtime/notifier.js +5 -5
  85. package/lib/core/security/index.js +1 -1
  86. package/lib/core/security/profileRepository.d.ts +176 -0
  87. package/lib/core/security/profileRepository.js +445 -443
  88. package/lib/core/security/roleRepository.js +16 -16
  89. package/lib/core/security/securityLoader.js +2 -2
  90. package/lib/core/security/tokenRepository.js +11 -11
  91. package/lib/core/security/userRepository.js +8 -8
  92. package/lib/core/shared/abstractManifest.js +4 -4
  93. package/lib/core/shared/repository.js +5 -5
  94. package/lib/core/shared/sdk/embeddedSdk.js +21 -2
  95. package/lib/core/shared/sdk/funnelProtocol.js +1 -1
  96. package/lib/core/shared/sdk/impersonatedSdk.js +1 -1
  97. package/lib/core/shared/store.js +30 -23
  98. package/lib/core/statistics/statistics.js +17 -17
  99. package/lib/core/storage/clientAdapter.js +45 -10
  100. package/lib/core/storage/indexCache.js +20 -4
  101. package/lib/core/validation/baseType.js +5 -5
  102. package/lib/core/validation/types/anything.js +1 -1
  103. package/lib/core/validation/types/boolean.js +2 -2
  104. package/lib/core/validation/types/date.js +9 -9
  105. package/lib/core/validation/types/email.js +5 -5
  106. package/lib/core/validation/types/enum.js +6 -6
  107. package/lib/core/validation/types/geoPoint.js +2 -2
  108. package/lib/core/validation/types/geoShape.js +28 -25
  109. package/lib/core/validation/types/integer.js +4 -4
  110. package/lib/core/validation/types/ipAddress.js +7 -6
  111. package/lib/core/validation/types/numeric.js +4 -4
  112. package/lib/core/validation/types/object.js +5 -5
  113. package/lib/core/validation/types/string.js +5 -5
  114. package/lib/core/validation/types/url.js +7 -6
  115. package/lib/core/validation/validation.js +95 -84
  116. package/lib/kerror/codes/1-services.json +12 -0
  117. package/lib/kerror/codes/2-api.json +12 -0
  118. package/lib/kerror/codes/3-network.json +12 -0
  119. package/lib/kerror/codes/4-plugin.json +6 -0
  120. package/lib/kerror/codes/index.js +11 -11
  121. package/lib/kerror/errors/multipleErrorsError.d.ts +1 -1
  122. package/lib/kerror/errors/multipleErrorsError.js +3 -3
  123. package/lib/kerror/index.d.ts +82 -0
  124. package/lib/kerror/index.js +176 -143
  125. package/lib/kuzzle/dumpGenerator.js +3 -3
  126. package/lib/kuzzle/event/kuzzleEventEmitter.js +4 -4
  127. package/lib/kuzzle/event/pipeRunner.js +1 -1
  128. package/lib/kuzzle/event/waterfall.js +6 -6
  129. package/lib/kuzzle/kuzzle.js +59 -9
  130. package/lib/kuzzle/log.js +3 -3
  131. package/lib/kuzzle/vault.js +3 -3
  132. package/lib/model/security/profile.d.ts +54 -0
  133. package/lib/model/security/profile.js +192 -232
  134. package/lib/model/security/rights.js +1 -1
  135. package/lib/model/security/role.d.ts +40 -0
  136. package/lib/model/security/role.js +174 -190
  137. package/lib/model/security/user.d.ts +29 -0
  138. package/lib/model/security/user.js +103 -52
  139. package/lib/model/storage/apiKey.js +2 -2
  140. package/lib/model/storage/baseModel.js +3 -3
  141. package/lib/service/cache/redis.js +7 -7
  142. package/lib/service/storage/elasticsearch.js +152 -90
  143. package/lib/service/storage/esWrapper.js +2 -3
  144. package/lib/types/ControllerDefinition.d.ts +3 -3
  145. package/lib/types/ControllerRights.d.ts +22 -0
  146. package/lib/types/ControllerRights.js +23 -0
  147. package/lib/types/HttpStream.d.ts +32 -0
  148. package/lib/types/HttpStream.js +70 -0
  149. package/lib/types/OpenApiDefinition.d.ts +43 -0
  150. package/lib/types/{config/StorageService/StorageServiceElasticsearchConfiguration.js → OpenApiDefinition.js} +1 -1
  151. package/lib/types/Plugin.js +20 -4
  152. package/lib/types/Policy.d.ts +25 -0
  153. package/lib/types/{InternalLogger.js → Policy.js} +2 -2
  154. package/lib/types/PolicyRestrictions.d.ts +21 -0
  155. package/lib/types/PolicyRestrictions.js +23 -0
  156. package/lib/types/Target.d.ts +15 -0
  157. package/lib/types/Target.js +23 -0
  158. package/lib/types/config/KuzzleConfiguration.d.ts +4 -0
  159. package/lib/types/config/ServicesConfiguration.d.ts +2 -2
  160. package/lib/types/config/{StorageService/StorageServiceElasticsearchConfiguration.d.ts → storageEngine/StorageEngineElasticsearchConfiguration.d.ts} +10 -3
  161. package/lib/types/config/storageEngine/StorageEngineElasticsearchConfiguration.js +3 -0
  162. package/lib/types/errors/ErrorDefinition.d.ts +27 -0
  163. package/lib/types/errors/ErrorDefinition.js +3 -0
  164. package/lib/types/errors/ErrorDomains.d.ts +17 -0
  165. package/lib/types/errors/ErrorDomains.js +3 -0
  166. package/lib/types/index.d.ts +9 -1
  167. package/lib/types/index.js +9 -1
  168. package/lib/util/array.d.ts +11 -0
  169. package/lib/util/array.js +57 -0
  170. package/lib/util/assertType.js +6 -6
  171. package/lib/util/bufferedPassThrough.d.ts +76 -0
  172. package/lib/util/bufferedPassThrough.js +161 -0
  173. package/lib/util/deprecate.js +7 -5
  174. package/lib/util/didYouMean.js +1 -1
  175. package/lib/util/dump-collection.d.ts +3 -0
  176. package/lib/util/dump-collection.js +284 -0
  177. package/lib/util/extractFields.js +2 -2
  178. package/lib/util/inflector.d.ts +8 -0
  179. package/lib/util/inflector.js +16 -0
  180. package/lib/util/mutex.js +21 -2
  181. package/lib/util/requestAssertions.js +7 -7
  182. package/lib/util/wildcard.js +55 -0
  183. package/package-lock.json +535 -75
  184. package/package.json +5 -3
  185. package/lib/api/openApiGenerator.d.ts +0 -7
  186. package/lib/api/openApiGenerator.js +0 -197
  187. package/lib/types/InternalLogger.d.ts +0 -25
@@ -32,7 +32,7 @@ const { BadRequestError } = require('../kerror/errors');
32
32
  * @param {*} data
33
33
  * @return {object}
34
34
  */
35
- function assertObject(attr, data) {
35
+ function assertObject (attr, data) {
36
36
  if (data === null || data === undefined) {
37
37
  return null;
38
38
  }
@@ -54,12 +54,12 @@ function assertObject(attr, data) {
54
54
  * @param {*} data
55
55
  * @return {array}
56
56
  */
57
- function assertArray(attr, data, type) {
57
+ function assertArray (attr, data, type) {
58
58
  if (data === null || data === undefined) {
59
59
  return [];
60
60
  }
61
61
 
62
- if (!Array.isArray(data)) {
62
+ if (! Array.isArray(data)) {
63
63
  throw new BadRequestError(`Attribute ${attr} must be of type "array"`);
64
64
  }
65
65
 
@@ -87,7 +87,7 @@ function assertArray(attr, data, type) {
87
87
  * @param {*} data
88
88
  * @return {null|string}
89
89
  */
90
- function assertString(attr, data) {
90
+ function assertString (attr, data) {
91
91
  if (data === null || data === undefined) {
92
92
  return null;
93
93
  }
@@ -108,8 +108,8 @@ function assertString(attr, data) {
108
108
  * @param {*} data
109
109
  * @return {number}
110
110
  */
111
- function assertInteger(attr, data) {
112
- if (!Number.isInteger(data)) {
111
+ function assertInteger (attr, data) {
112
+ if (! Number.isInteger(data)) {
113
113
  throw new BadRequestError(`Attribute ${attr} must be an integer`);
114
114
  }
115
115
 
@@ -0,0 +1,76 @@
1
+ /// <reference types="node" />
2
+ import stream from 'stream';
3
+ declare type Encoding = BufferEncoding | 'buffer';
4
+ declare type ChunkData = Buffer | string;
5
+ declare type Chunk = {
6
+ chunk: ChunkData;
7
+ encoding: Encoding;
8
+ };
9
+ declare type Callback = (error?: Error) => void;
10
+ /**
11
+ * This streams accumulate chunks data into a buffer until the amount of data is equal or exceed the buffer size.
12
+ * Then, it emits a single chunk with the accumulated data.
13
+ *
14
+ * This stream is useful when you want to reduce the amount of chunk produced by a stream.
15
+ * This can helps reducing the number of syscall made by the stream consumer.
16
+ */
17
+ export declare class BufferedPassThrough extends stream.Duplex {
18
+ private bufferSize;
19
+ private buffer;
20
+ private offset;
21
+ constructor(options?: stream.DuplexOptions);
22
+ /**
23
+ * Writes a string or buffer to the internal buffer starting at the internal buffer offset.
24
+ * Data will be copied from the given start to the end position.
25
+ *
26
+ * @param data
27
+ * @param start Where to start copying/writing data from
28
+ * @param end Where to end copying/writing data from
29
+ * @param encoding Type of encoding to use
30
+ * @returns How many bytes have been copied / written to the internal buffer.
31
+ */
32
+ private writeToBuffer;
33
+ private writeChunks;
34
+ /**
35
+ * Writes a chunk of data to the internal buffer.
36
+ * If the internal buffer is full, it will be pushed to the stream.
37
+ * @param chunk
38
+ * @param encoding
39
+ */
40
+ private writeChunkData;
41
+ /**
42
+ * @override from stream.Duplex
43
+ * Internal method called when data needs to be written.
44
+ * @param chunkData
45
+ * @param encoding
46
+ * @param callback
47
+ */
48
+ _write(chunkData: ChunkData, encoding: Encoding, callback: Callback): void;
49
+ /**
50
+ * @override from stream.Duplex
51
+ * Internal method called when multiple chunks were stored and needs to be written.
52
+ * @param chunks
53
+ * @param callback
54
+ */
55
+ _writev(chunks: Chunk[], callback: Callback): void;
56
+ /**
57
+ * @override from stream.Duplex
58
+ * Internal method called when the stream is drained, to resume pushing data.
59
+ * @param size
60
+ */
61
+ _read(): void;
62
+ /**
63
+ * @override from stream.Duplex
64
+ * Internal method called when the stream is ended.
65
+ * @param callback
66
+ */
67
+ _final(callback: Callback): void;
68
+ /**
69
+ * @override from stream.Duplex
70
+ * Internal method called when stream is destroyed.
71
+ * @param err
72
+ * @param callback
73
+ */
74
+ _destroy(err: Error, callback: Callback): void;
75
+ }
76
+ export {};
@@ -0,0 +1,161 @@
1
+ "use strict";
2
+ /*
3
+ * Kuzzle, a backend software, self-hostable and ready to use
4
+ * to power modern apps
5
+ *
6
+ * Copyright 2015-2022 Kuzzle
7
+ * mailto: support AT kuzzle.io
8
+ * website: http://kuzzle.io
9
+ *
10
+ * Licensed under the Apache License, Version 2.0 (the "License");
11
+ * you may not use this file except in compliance with the License.
12
+ * You may obtain a copy of the License at
13
+ *
14
+ * https://www.apache.org/licenses/LICENSE-2.0
15
+ *
16
+ * Unless required by applicable law or agreed to in writing, software
17
+ * distributed under the License is distributed on an "AS IS" BASIS,
18
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
+ * See the License for the specific language governing permissions and
20
+ * limitations under the License.
21
+ */
22
+ var __importDefault = (this && this.__importDefault) || function (mod) {
23
+ return (mod && mod.__esModule) ? mod : { "default": mod };
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.BufferedPassThrough = void 0;
27
+ const stream_1 = __importDefault(require("stream"));
28
+ /**
29
+ * This streams accumulate chunks data into a buffer until the amount of data is equal or exceed the buffer size.
30
+ * Then, it emits a single chunk with the accumulated data.
31
+ *
32
+ * This stream is useful when you want to reduce the amount of chunk produced by a stream.
33
+ * This can helps reducing the number of syscall made by the stream consumer.
34
+ */
35
+ class BufferedPassThrough extends stream_1.default.Duplex {
36
+ constructor(options = { highWaterMark: 8196 }) {
37
+ super(options);
38
+ this.bufferSize = options.highWaterMark;
39
+ this.buffer = Buffer.alloc(options.highWaterMark);
40
+ this.offset = 0;
41
+ }
42
+ /**
43
+ * Writes a string or buffer to the internal buffer starting at the internal buffer offset.
44
+ * Data will be copied from the given start to the end position.
45
+ *
46
+ * @param data
47
+ * @param start Where to start copying/writing data from
48
+ * @param end Where to end copying/writing data from
49
+ * @param encoding Type of encoding to use
50
+ * @returns How many bytes have been copied / written to the internal buffer.
51
+ */
52
+ writeToBuffer(data, start, end, encoding) {
53
+ if (encoding === 'buffer') {
54
+ return data.copy(this.buffer, this.offset, start, end);
55
+ }
56
+ if (start === 0 && end === data.length) {
57
+ return this.buffer.write(data, this.offset, end - start, encoding);
58
+ }
59
+ return this.buffer.write(data.slice(start, end), this.offset, end - start, encoding);
60
+ }
61
+ async writeChunks(chunks) {
62
+ for (const chunk of chunks) {
63
+ await this.writeChunkData(chunk.chunk, chunk.encoding);
64
+ }
65
+ }
66
+ /**
67
+ * Writes a chunk of data to the internal buffer.
68
+ * If the internal buffer is full, it will be pushed to the stream.
69
+ * @param chunk
70
+ * @param encoding
71
+ */
72
+ async writeChunkData(chunk, encoding) {
73
+ let chunkOffset = 0;
74
+ while (chunkOffset < chunk.length) {
75
+ const remainingChunkSize = chunk.length - chunkOffset;
76
+ const remainingBufferSize = this.bufferSize - this.offset;
77
+ // If the remaining bytes in the chunk exceed the remaining bytes in the internal buffer
78
+ if (remainingChunkSize >= remainingBufferSize) {
79
+ // Write as much as the remaining bytes as possible to the buffer from t
80
+ chunkOffset += this.writeToBuffer(chunk, chunkOffset, chunkOffset + remainingBufferSize, encoding);
81
+ // Sends the whole buffer to the stream since it is full
82
+ if (!this.push(this.buffer)) {
83
+ await new Promise(res => {
84
+ this.once('_drained', res);
85
+ });
86
+ }
87
+ this.offset = 0; // Reset the offset
88
+ }
89
+ else {
90
+ // Write the whole chunk to the buffer
91
+ const size = this.writeToBuffer(chunk, chunkOffset, chunk.length, encoding);
92
+ // Increase the internal buffer offset
93
+ this.offset += size;
94
+ break;
95
+ }
96
+ }
97
+ }
98
+ /**
99
+ * @override from stream.Duplex
100
+ * Internal method called when data needs to be written.
101
+ * @param chunkData
102
+ * @param encoding
103
+ * @param callback
104
+ */
105
+ _write(chunkData, encoding, callback) {
106
+ this.writeChunkData(chunkData, encoding).then(() => {
107
+ callback();
108
+ });
109
+ }
110
+ /**
111
+ * @override from stream.Duplex
112
+ * Internal method called when multiple chunks were stored and needs to be written.
113
+ * @param chunks
114
+ * @param callback
115
+ */
116
+ _writev(chunks, callback) {
117
+ this.writeChunks(chunks).then(() => {
118
+ callback();
119
+ });
120
+ }
121
+ /**
122
+ * @override from stream.Duplex
123
+ * Internal method called when the stream is drained, to resume pushing data.
124
+ * @param size
125
+ */
126
+ _read() {
127
+ // Emits an internal _drained event when the internal buffer is drained.
128
+ this.emit('_drained');
129
+ }
130
+ /**
131
+ * @override from stream.Duplex
132
+ * Internal method called when the stream is ended.
133
+ * @param callback
134
+ */
135
+ _final(callback) {
136
+ if (this.buffer && this.offset > 0) {
137
+ // Push last bit of data
138
+ this.push(this.buffer.slice(0, this.offset));
139
+ }
140
+ this.push(null); // Close the stream
141
+ this.buffer = null;
142
+ callback();
143
+ }
144
+ /**
145
+ * @override from stream.Duplex
146
+ * Internal method called when stream is destroyed.
147
+ * @param err
148
+ * @param callback
149
+ */
150
+ _destroy(err, callback) {
151
+ if (this.buffer && this.offset > 0) {
152
+ // Push last bit of data
153
+ this.push(this.buffer.slice(0, this.offset));
154
+ }
155
+ this.push(null); // Close the stream
156
+ this.buffer = null;
157
+ callback(err);
158
+ }
159
+ }
160
+ exports.BufferedPassThrough = BufferedPassThrough;
161
+ //# sourceMappingURL=bufferedPassThrough.js.map
@@ -35,15 +35,17 @@ const warnIfDeprecated = (logger, deprecations, member) => {
35
35
  .find(deprecated => deprecated === member),
36
36
  alternative = deprecations[deprecatedProperty];
37
37
 
38
- if (!deprecatedProperty) {
38
+ if (! deprecatedProperty) {
39
39
  return;
40
40
  }
41
41
 
42
42
  if (alternative && typeof alternative.message === 'string') {
43
43
  deprecationWarning(logger, alternative.message);
44
- } else if (typeof alternative === 'string') {
44
+ }
45
+ else if (typeof alternative === 'string') {
45
46
  deprecationWarning(logger, `Use of '${deprecatedProperty}' property is deprecated. Please, use '${alternative}' instead.`);
46
- } else {
47
+ }
48
+ else {
47
49
  deprecationWarning(logger, `Use of '${deprecatedProperty}' property is deprecated.`);
48
50
  }
49
51
  };
@@ -54,7 +56,7 @@ const deprecateProperties = (logger, target, deprecations = {}) => {
54
56
  }
55
57
 
56
58
  return new Proxy(target, {
57
- get(object, key, ...rest) {
59
+ get (object, key, ...rest) {
58
60
  if (key === '__isProxy') {
59
61
  return true;
60
62
  }
@@ -64,7 +66,7 @@ const deprecateProperties = (logger, target, deprecations = {}) => {
64
66
  return Reflect.get(object, key, ...rest);
65
67
  },
66
68
 
67
- set(object, key, ...rest) {
69
+ set (object, key, ...rest) {
68
70
  warnIfDeprecated(logger, deprecations, key);
69
71
 
70
72
  return Reflect.set(object, key, ...rest);
@@ -23,7 +23,7 @@
23
23
 
24
24
  const didYouMean = require('didyoumean');
25
25
 
26
- function printDidYouMean(...args) {
26
+ function printDidYouMean (...args) {
27
27
  if (global.NODE_ENV !== 'development') {
28
28
  return '';
29
29
  }
@@ -0,0 +1,3 @@
1
+ import { JSONObject } from '../../index';
2
+ import { HttpStream } from '../types';
3
+ export declare function dumpCollectionDocuments(index: string, collection: string, query?: any, format?: string, fields?: string[], options?: JSONObject): HttpStream;
@@ -0,0 +1,284 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
11
+ }) : function(o, v) {
12
+ o["default"] = v;
13
+ });
14
+ var __importStar = (this && this.__importStar) || function (mod) {
15
+ if (mod && mod.__esModule) return mod;
16
+ var result = {};
17
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
+ __setModuleDefault(result, mod);
19
+ return result;
20
+ };
21
+ var __importDefault = (this && this.__importDefault) || function (mod) {
22
+ return (mod && mod.__esModule) ? mod : { "default": mod };
23
+ };
24
+ Object.defineProperty(exports, "__esModule", { value: true });
25
+ exports.dumpCollectionDocuments = void 0;
26
+ const ndjson_1 = __importDefault(require("ndjson"));
27
+ const get_1 = __importDefault(require("lodash/get"));
28
+ const isObject_1 = __importDefault(require("lodash/isObject"));
29
+ const kerror = __importStar(require("../kerror"));
30
+ const bufferedPassThrough_1 = require("./bufferedPassThrough");
31
+ const types_1 = require("../types");
32
+ /**
33
+ * Flatten an object transform:
34
+ * {
35
+ * title: "kuzzle",
36
+ * info : {
37
+ * tag: "news"
38
+ * }
39
+ * }
40
+ *
41
+ * Into an object like:
42
+ * {
43
+ * title: "kuzzle",
44
+ * info.tag: news
45
+ * }
46
+ *
47
+ * @param {Object} target the object we have to flatten
48
+ * @returns {Object} the flattened object
49
+ */
50
+ function flattenObject(target) {
51
+ const output = {};
52
+ flattenStep(output, target);
53
+ return output;
54
+ }
55
+ function flattenStep(output, object, prev = null) {
56
+ const keys = Object.keys(object);
57
+ for (let i = 0; i < keys.length; i++) {
58
+ const key = keys[i];
59
+ const value = object[key];
60
+ const newKey = prev ? prev + '.' + key : key;
61
+ if (Object.prototype.toString.call(value) === '[object Object]') {
62
+ output[newKey] = value;
63
+ flattenStep(output, value, newKey);
64
+ }
65
+ output[newKey] = value;
66
+ }
67
+ }
68
+ /**
69
+ * Extract fields from mapping by removing the properties from es mapping
70
+ *
71
+ * @param mapping
72
+ * @returns
73
+ */
74
+ function extractMappingFields(mapping) {
75
+ const newMapping = {};
76
+ for (const key of Object.keys(mapping)) {
77
+ if (key === 'properties' && (0, isObject_1.default)(mapping[key])) {
78
+ newMapping[key] = extractMappingFields(mapping[key]);
79
+ }
80
+ else if ((0, isObject_1.default)(mapping[key]) && mapping[key].type) {
81
+ newMapping[key] = mapping[key].type;
82
+ }
83
+ else {
84
+ newMapping[key] = mapping[key];
85
+ }
86
+ }
87
+ return newMapping;
88
+ }
89
+ /**
90
+ * An iteration-order-safe version of lodash.values
91
+ *
92
+ * @param object The object containing the values
93
+ * @param fields The field names to pick in the right order
94
+ * @returns The values in the same order as the fields
95
+ * @see https://lodash.com/docs/4.17.15#values
96
+ */
97
+ function pickValues(object, fields) {
98
+ return fields.map(f => formatValueForCSV((0, get_1.default)(object, f)));
99
+ }
100
+ /**
101
+ * Formats the value for correct CSV output, avoiding to return
102
+ * values that would badly serialize in CSV.
103
+ *
104
+ * @param value The value to format
105
+ * @returns The value or a string telling the value is not scalar
106
+ */
107
+ function formatValueForCSV(value) {
108
+ if ((0, isObject_1.default)(value)) {
109
+ return '[OBJECT]';
110
+ }
111
+ return value;
112
+ }
113
+ class AbstractDumper {
114
+ constructor(index, collection, query = {}, writeStream, options = {
115
+ fieldsName: {},
116
+ scroll: '5s',
117
+ separator: ',',
118
+ size: 10,
119
+ }) {
120
+ this.index = index;
121
+ this.collection = collection;
122
+ this.query = query;
123
+ this.writeStream = writeStream;
124
+ this.options = options;
125
+ if (!writeStream) {
126
+ throw kerror.get('api', 'assert', 'missing_argument', 'writeStream');
127
+ }
128
+ }
129
+ /**
130
+ * One-shot call before the dump. Can be used to
131
+ * perform setup operations before dumping.
132
+ *
133
+ * @returns void
134
+ */
135
+ // eslint-disable-next-line @typescript-eslint/no-empty-function
136
+ async setup() { }
137
+ /**
138
+ * One-shot call before iterating over the data. Can be
139
+ * used to write the header of the dumped output.
140
+ */
141
+ // eslint-disable-next-line @typescript-eslint/no-empty-function
142
+ async writeHeader() { }
143
+ // eslint-disable-next-line @typescript-eslint/no-empty-function
144
+ async tearDown() { }
145
+ async scroll(scrollId) {
146
+ if (!scrollId) {
147
+ return null;
148
+ }
149
+ try {
150
+ return await global.kuzzle.ask('core:storage:public:document:scroll', scrollId, { scrollTTL: this.options.scroll });
151
+ }
152
+ catch {
153
+ return null;
154
+ }
155
+ }
156
+ /**
157
+ * The loop that iterates over the documents of the collection and
158
+ * calls all the other hooks.
159
+ *
160
+ * @returns a promise resolving when the dump is finished.
161
+ */
162
+ async dump() {
163
+ const waitWrite = new Promise((resolve, reject) => this.writeStream ? this.writeStream.on('finish', resolve) : reject());
164
+ this.writeStream.on('error', error => {
165
+ throw error;
166
+ });
167
+ await this.setup();
168
+ await this.writeHeader();
169
+ let results = await global.kuzzle.ask('core:storage:public:document:search', this.index, this.collection, this.query, {
170
+ lang: this.options.lang,
171
+ scroll: this.options.scroll,
172
+ size: this.options.size,
173
+ });
174
+ do {
175
+ for (const hit of results.hits) {
176
+ await this.onResult({
177
+ _id: hit._id,
178
+ _source: hit._source
179
+ });
180
+ }
181
+ } while ((results = await this.scroll(results.scrollId)));
182
+ await this.tearDown();
183
+ this.writeStream.end();
184
+ return waitWrite;
185
+ }
186
+ }
187
+ class JSONLDumper extends AbstractDumper {
188
+ constructor() {
189
+ super(...arguments);
190
+ this.ndjsonStream = ndjson_1.default.stringify();
191
+ }
192
+ async setup() {
193
+ this.ndjsonStream.on('data', (line) => {
194
+ this.writeStream.write(line);
195
+ });
196
+ }
197
+ async writeHeader() {
198
+ await this.writeLine({
199
+ collection: this.collection,
200
+ index: this.index,
201
+ type: 'collection',
202
+ });
203
+ }
204
+ writeLine(content) {
205
+ return new Promise(resolve => {
206
+ if (this.ndjsonStream.write(content)) {
207
+ resolve();
208
+ }
209
+ else {
210
+ this.ndjsonStream.once('drain', resolve);
211
+ }
212
+ });
213
+ }
214
+ onResult(document) {
215
+ return this.writeLine({
216
+ _id: document._id,
217
+ body: document._source,
218
+ });
219
+ }
220
+ get fileExtension() {
221
+ return 'jsonl';
222
+ }
223
+ }
224
+ class CSVDumper extends AbstractDumper {
225
+ constructor(index, collection, query = {}, writeStream, options, fields) {
226
+ super(index, collection, query, writeStream, options);
227
+ this.fields = fields;
228
+ }
229
+ get fileExtension() {
230
+ return 'csv';
231
+ }
232
+ async setup() {
233
+ if (!this.fields.length) {
234
+ // If no field has been selected, then all fields are selected.
235
+ const mappings = await global.kuzzle.ask('core:storage:public:mappings:get', this.index, this.collection);
236
+ if (!mappings.properties) {
237
+ return;
238
+ }
239
+ this.fields = Object.keys(flattenObject(extractMappingFields(mappings.properties)));
240
+ }
241
+ else if (this.fields.includes('_id')) {
242
+ // Delete '_id' from the selected fields, since IDs are
243
+ // _always_ exported.
244
+ this.fields.splice(this.fields.indexOf('_id'), 1);
245
+ }
246
+ }
247
+ writeHeader() {
248
+ const mappedFieldsName = ['_id', ...this.fields].map(field => {
249
+ return this.options.fieldsName[field] || field;
250
+ });
251
+ return this.writeLine(mappedFieldsName.join(this.options.separator));
252
+ }
253
+ writeLine(content) {
254
+ return new Promise(resolve => {
255
+ if (this.writeStream.write(`${content}\n`)) {
256
+ resolve();
257
+ }
258
+ else {
259
+ this.writeStream.once('drain', resolve);
260
+ }
261
+ });
262
+ }
263
+ onResult(document) {
264
+ const values = [document._id, ...pickValues(document._source, this.fields)];
265
+ return this.writeLine(values.join(this.options.separator));
266
+ }
267
+ }
268
+ function dumpCollectionDocuments(index, collection, query = {}, format = 'jsonl', fields = [], options = {}) {
269
+ let dumper;
270
+ const writableStream = new bufferedPassThrough_1.BufferedPassThrough({ highWaterMark: 16384 });
271
+ switch (format.toLowerCase()) {
272
+ case 'csv':
273
+ dumper = new CSVDumper(index, collection, query, writableStream, options, fields);
274
+ dumper.dump();
275
+ break;
276
+ default:
277
+ dumper = new JSONLDumper(index, collection, query, writableStream, options);
278
+ dumper.dump();
279
+ break;
280
+ }
281
+ return new types_1.HttpStream(writableStream);
282
+ }
283
+ exports.dumpCollectionDocuments = dumpCollectionDocuments;
284
+ //# sourceMappingURL=dump-collection.js.map
@@ -32,7 +32,7 @@ const { isPlainObject } = require('./safeObject');
32
32
  *
33
33
  * @returns { Array<String> | Array<{ key: String, value: any }> }
34
34
  */
35
- function extractFields(document, {
35
+ function extractFields (document, {
36
36
  fieldsToIgnore = [],
37
37
  alsoExtractValues = false,
38
38
  } = {}, {
@@ -54,7 +54,7 @@ function extractFields(document, {
54
54
  { extractedFields, path: currentPath });
55
55
  }
56
56
  else if (alsoExtractValues) {
57
- extractedFields.push({key: currentPath, value});
57
+ extractedFields.push({ key: currentPath, value });
58
58
  }
59
59
  else {
60
60
  extractedFields.push(currentPath);
@@ -14,4 +14,12 @@ export declare class Inflector {
14
14
  * @param string String to transform the first letter in uppercase
15
15
  */
16
16
  static upFirst(string: string): string;
17
+ /**
18
+ * Converts a string to PascalCase
19
+ */
20
+ static pascalCase(string: string): string;
21
+ /**
22
+ * Converts a string to camelCase
23
+ */
24
+ static camelCase(string: any): string;
17
25
  }
@@ -19,8 +19,12 @@
19
19
  * See the License for the specific language governing permissions and
20
20
  * limitations under the License.
21
21
  */
22
+ var __importDefault = (this && this.__importDefault) || function (mod) {
23
+ return (mod && mod.__esModule) ? mod : { "default": mod };
24
+ };
22
25
  Object.defineProperty(exports, "__esModule", { value: true });
23
26
  exports.Inflector = void 0;
27
+ const lodash_1 = __importDefault(require("lodash"));
24
28
  class Inflector {
25
29
  /**
26
30
  * Converts a string to kebab-case
@@ -46,6 +50,18 @@ class Inflector {
46
50
  static upFirst(string) {
47
51
  return string.charAt(0).toUpperCase() + string.slice(1);
48
52
  }
53
+ /**
54
+ * Converts a string to PascalCase
55
+ */
56
+ static pascalCase(string) {
57
+ return lodash_1.default.startCase(string).replace(/ /g, '');
58
+ }
59
+ /**
60
+ * Converts a string to camelCase
61
+ */
62
+ static camelCase(string) {
63
+ return lodash_1.default.camelCase(string);
64
+ }
49
65
  }
50
66
  exports.Inflector = Inflector;
51
67
  //# sourceMappingURL=inflector.js.map