kuzzle 2.16.1 → 2.16.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.
@@ -19,13 +19,37 @@
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 };
22
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
23
+ if (k2 === undefined) k2 = k;
24
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
25
+ }) : (function(o, m, k, k2) {
26
+ if (k2 === undefined) k2 = k;
27
+ o[k2] = m[k];
28
+ }));
29
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
30
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
31
+ }) : function(o, v) {
32
+ o["default"] = v;
33
+ });
34
+ var __importStar = (this && this.__importStar) || function (mod) {
35
+ if (mod && mod.__esModule) return mod;
36
+ var result = {};
37
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
38
+ __setModuleDefault(result, mod);
39
+ return result;
24
40
  };
25
41
  Object.defineProperty(exports, "__esModule", { value: true });
26
42
  exports.RequestInput = exports.RequestResource = void 0;
27
43
  const internalError_1 = require("../../kerror/errors/internalError");
28
- const assertType_1 = __importDefault(require("../../util/assertType"));
44
+ const assert = __importStar(require("../../util/assertType"));
45
+ // private properties
46
+ // \u200b is a zero width space, used to masquerade console.log output
47
+ const _jwt = 'jwt\u200b';
48
+ const _volatile = 'volatile\u200b';
49
+ const _body = 'body\u200b';
50
+ const _headers = 'headers\u200b';
51
+ const _controller = 'controller\u200b';
52
+ const _action = 'action\u200b';
29
53
  // any property not listed here will be copied into
30
54
  // RequestInput.args
31
55
  const resourceProperties = new Set([
@@ -44,7 +68,6 @@ class RequestResource {
44
68
  }
45
69
  /**
46
70
  * Document ID
47
- * @deprecated
48
71
  */
49
72
  get _id() {
50
73
  return this.args._id;
@@ -54,7 +77,6 @@ class RequestResource {
54
77
  }
55
78
  /**
56
79
  * Index name
57
- * @deprecated
58
80
  */
59
81
  get index() {
60
82
  return this.args.index;
@@ -64,7 +86,6 @@ class RequestResource {
64
86
  }
65
87
  /**
66
88
  * Collection name
67
- * @deprecated
68
89
  */
69
90
  get collection() {
70
91
  return this.args.collection;
@@ -93,109 +114,14 @@ class RequestInput {
93
114
  * Any undefined option is set to null
94
115
  */
95
116
  constructor(data) {
96
- /**
97
- * Authentication token.
98
- * @example
99
- * // original JSON request sent to Kuzzle
100
- * {
101
- * controller
102
- * action,
103
- * _id,
104
- * index,
105
- * collection,
106
- * jwt, <== that
107
- * refresh,
108
- * foobar,
109
- * volatile,
110
- * body
111
- * }
112
- */
113
- this.jwt = null;
114
- /**
115
- * API controller name.
116
- * @example
117
- * // original JSON request sent to Kuzzle
118
- * {
119
- * controller <== that
120
- * action,
121
- * _id,
122
- * index,
123
- * collection,
124
- * jwt,
125
- * refresh,
126
- * foobar,
127
- * volatile,
128
- * body
129
- * }
130
- */
131
- this.controller = null;
132
- /**
133
- * API action name.
134
- * @example
135
- * // original JSON request sent to Kuzzle
136
- * {
137
- * controller
138
- * action, <== that
139
- * _id,
140
- * index,
141
- * collection,
142
- * jwt,
143
- * refresh,
144
- * foobar,
145
- * volatile,
146
- * body
147
- * }
148
- */
149
- this.action = null;
150
- /**
151
- * Request body.
152
- * In Http it's the request body parsed.
153
- * @example
154
- * // original JSON request sent to Kuzzle
155
- * {
156
- * controller
157
- * action,
158
- * _id,
159
- * index,
160
- * collection,
161
- * jwt,
162
- * refresh,
163
- * foobar,
164
- * volatile,
165
- * body <== that
166
- * }
167
- */
168
- this.body = {};
169
- /**
170
- * Request headers (Http only).
171
- */
172
- this.headers = null;
173
- /**
174
- * Volatile object.
175
- * @example
176
- * // original JSON request sent to Kuzzle
177
- * {
178
- * controller
179
- * action,
180
- * _id,
181
- * index,
182
- * collection,
183
- * jwt,
184
- * refresh,
185
- * foobar,
186
- * volatile, <== that
187
- * body
188
- * }
189
- */
190
- this.volatile = null;
191
117
  if (!data || typeof data !== 'object' || Array.isArray(data)) {
192
118
  throw new internalError_1.InternalError('Input request data must be a non-null object');
193
119
  }
194
- this.jwt = null;
195
- this.volatile = null;
196
- this.body = null;
197
- this.controller = null;
198
- this.action = null;
120
+ this[_jwt] = null;
121
+ this[_volatile] = null;
122
+ this[_body] = null;
123
+ this[_controller] = null;
124
+ this[_action] = null;
199
125
  // default value to null for former "resources" to avoid breaking
200
126
  this.args = {};
201
127
  this.resource = new RequestResource(this.args);
@@ -205,17 +131,151 @@ class RequestInput {
205
131
  this.args[k] = data[k];
206
132
  }
207
133
  }
208
- if (data.jwt) {
209
- this.jwt = data.jwt;
210
- }
211
- if (data.volatile) {
212
- this.volatile = data.volatile;
134
+ // @deprecated - RequestContext.connection.misc.headers should be used instead
135
+ // initialize `_headers` property after the population of `this.args` attribute
136
+ // `this.headers` can contain protocol specific headers and should be
137
+ // set after the Request construction
138
+ // `args.headers` can be an attribute coming from data itself.
139
+ this[_headers] = null;
140
+ Object.seal(this);
141
+ this.jwt = data.jwt;
142
+ this.volatile = data.volatile;
143
+ this.body = data.body;
144
+ this.controller = data.controller;
145
+ this.action = data.action;
146
+ }
147
+ /**
148
+ * Authentication token.
149
+ * @example
150
+ * // original JSON request sent to Kuzzle
151
+ * {
152
+ * controller
153
+ * action,
154
+ * _id,
155
+ * index,
156
+ * collection,
157
+ * jwt, <== that
158
+ * refresh,
159
+ * foobar,
160
+ * volatile,
161
+ * body
162
+ * }
163
+ */
164
+ get jwt() {
165
+ return this[_jwt];
166
+ }
167
+ set jwt(str) {
168
+ this[_jwt] = assert.assertString('jwt', str);
169
+ }
170
+ /**
171
+ * API controller name.
172
+ * @example
173
+ * // original JSON request sent to Kuzzle
174
+ * {
175
+ * controller <== that
176
+ * action,
177
+ * _id,
178
+ * index,
179
+ * collection,
180
+ * jwt,
181
+ * refresh,
182
+ * foobar,
183
+ * volatile,
184
+ * body
185
+ * }
186
+ */
187
+ get controller() {
188
+ return this[_controller];
189
+ }
190
+ set controller(str) {
191
+ // can only be set once
192
+ if (!this[_controller]) {
193
+ this[_controller] = assert.assertString('controller', str);
213
194
  }
214
- if (data.body) {
215
- this.body = data.body;
195
+ }
196
+ /**
197
+ * API action name.
198
+ * @example
199
+ * // original JSON request sent to Kuzzle
200
+ * {
201
+ * controller
202
+ * action, <== that
203
+ * _id,
204
+ * index,
205
+ * collection,
206
+ * jwt,
207
+ * refresh,
208
+ * foobar,
209
+ * volatile,
210
+ * body
211
+ * }
212
+ */
213
+ get action() {
214
+ return this[_action];
215
+ }
216
+ set action(str) {
217
+ // can only be set once
218
+ if (!this[_action]) {
219
+ this[_action] = assert.assertString('action', str);
216
220
  }
217
- this.controller = assertType_1.default.assertString('controller', data.controller);
218
- this.action = assertType_1.default.assertString('controller', data.action);
221
+ }
222
+ /**
223
+ * Request body.
224
+ * In Http it's the request body parsed.
225
+ * @example
226
+ * // original JSON request sent to Kuzzle
227
+ * {
228
+ * controller
229
+ * action,
230
+ * _id,
231
+ * index,
232
+ * collection,
233
+ * jwt,
234
+ * refresh,
235
+ * foobar,
236
+ * volatile,
237
+ * body <== that
238
+ * }
239
+ */
240
+ get body() {
241
+ return this[_body];
242
+ }
243
+ set body(obj) {
244
+ this[_body] = assert.assertObject('body', obj);
245
+ }
246
+ /**
247
+ * Request headers (Http only).
248
+ *
249
+ * @deprecated Use RequestContext.connection.misc.headers instead
250
+ */
251
+ get headers() {
252
+ return this[_headers];
253
+ }
254
+ set headers(obj) {
255
+ this[_headers] = assert.assertObject('headers', obj);
256
+ }
257
+ /**
258
+ * Volatile object.
259
+ * @example
260
+ * // original JSON request sent to Kuzzle
261
+ * {
262
+ * controller
263
+ * action,
264
+ * _id,
265
+ * index,
266
+ * collection,
267
+ * jwt,
268
+ * refresh,
269
+ * foobar,
270
+ * volatile, <== that
271
+ * body
272
+ * }
273
+ */
274
+ get volatile() {
275
+ return this[_volatile];
276
+ }
277
+ set volatile(obj) {
278
+ this[_volatile] = assert.assertObject('volatile', obj);
219
279
  }
220
280
  }
221
281
  exports.RequestInput = RequestInput;
@@ -1,19 +1,17 @@
1
1
  import { JSONObject } from 'kuzzle-sdk';
2
- import '../../../lib/types/Global';
3
2
  import { Deprecation } from '../../types';
4
3
  import { KuzzleError } from '../../kerror/errors/kuzzleError';
5
- import { KuzzleRequest } from './kuzzleRequest';
6
4
  export declare class Headers {
7
5
  headers: JSONObject;
8
6
  private namesMap;
9
- proxy: any;
7
+ private proxy;
10
8
  constructor();
11
9
  /**
12
10
  * Gets a header value
13
11
  *
14
12
  * @param name Header name. Could be a string (case-insensitive) or a symbol
15
13
  */
16
- getHeader(name: any): string;
14
+ getHeader(name: any): string | void;
17
15
  removeHeader(name: string): boolean;
18
16
  setHeader(name: string, value: string): boolean;
19
17
  }
@@ -21,22 +19,20 @@ export declare class Headers {
21
19
  * Kuzzle normalized API response
22
20
  */
23
21
  export declare class RequestResponse {
24
- private request;
25
- private _headers;
26
22
  /**
27
23
  * If sets to true, "result" content will not be wrapped in a Kuzzle response
28
24
  */
29
25
  raw: boolean;
30
- constructor(request: KuzzleRequest);
26
+ constructor(request: any);
31
27
  /**
32
28
  * Get the parent request deprecations
33
29
  */
34
- get deprecations(): Array<Deprecation>;
30
+ get deprecations(): Array<Deprecation> | void;
35
31
  /**
36
32
  * Set the parent request deprecations
37
33
  * @param {Object[]} deprecations
38
34
  */
39
- set deprecations(deprecations: Array<Deprecation>);
35
+ set deprecations(deprecations: Array<Deprecation> | void);
40
36
  /**
41
37
  * Get the parent request status
42
38
  * @returns {number}
@@ -103,16 +99,16 @@ export declare class RequestResponse {
103
99
  /**
104
100
  * Gets a header value (case-insensitive)
105
101
  */
106
- getHeader(name: string): string;
102
+ getHeader(name: string): string | null;
107
103
  /**
108
104
  * Deletes a header (case-insensitive)
109
105
  */
110
- removeHeader(name: string): boolean;
106
+ removeHeader(name: string): any;
111
107
  /**
112
108
  * Sets a new array. Behaves the same as Node.js' HTTP response.setHeader
113
109
  * method (@see https://nodejs.org/api/http.html#http_response_setheader_name_value)
114
110
  */
115
- setHeader(name: string, value: string): boolean;
111
+ setHeader(name: string, value: string): any;
116
112
  /**
117
113
  * Adds new multiple headers.
118
114
  */
@@ -40,8 +40,11 @@ var __importStar = (this && this.__importStar) || function (mod) {
40
40
  };
41
41
  Object.defineProperty(exports, "__esModule", { value: true });
42
42
  exports.RequestResponse = exports.Headers = void 0;
43
- require("../../../lib/types/Global");
44
43
  const assert = __importStar(require("../../util/assertType"));
44
+ // private properties
45
+ // \u200b is a zero width space, used to masquerade console.log output
46
+ const _request = 'request\u200b';
47
+ const _headers = 'headers\u200b';
45
48
  class Headers {
46
49
  constructor() {
47
50
  this.namesMap = new Map();
@@ -51,10 +54,7 @@ class Headers {
51
54
  get: (target, name) => this.getHeader(name),
52
55
  set: (target, name, value) => this.setHeader(name, value),
53
56
  });
54
- // eslint-disable-next-line dot-notation
55
- if (global['_kuzzle'] && global.kuzzle) {
56
- this.setHeader('X-Kuzzle-Node', global.kuzzle.id);
57
- }
57
+ this.setHeader('X-Kuzzle-Node', global.kuzzle.id);
58
58
  }
59
59
  /**
60
60
  * Gets a header value
@@ -144,104 +144,98 @@ exports.Headers = Headers;
144
144
  class RequestResponse {
145
145
  constructor(request) {
146
146
  this.raw = false;
147
- // Make a private property otherwise the tests are ending in recursive loop
148
- Reflect.defineProperty(this, 'request', {
149
- value: request,
150
- });
151
- this._headers = new Headers();
147
+ this[_request] = request;
148
+ this[_headers] = new Headers();
149
+ Object.seal(this);
152
150
  }
153
151
  /**
154
152
  * Get the parent request deprecations
155
153
  */
156
154
  get deprecations() {
157
- return this.request.deprecations;
155
+ return this[_request].deprecations;
158
156
  }
159
157
  /**
160
158
  * Set the parent request deprecations
161
159
  * @param {Object[]} deprecations
162
160
  */
163
161
  set deprecations(deprecations) {
164
- this.request.deprecations = deprecations;
162
+ this[_request].deprecations = deprecations;
165
163
  }
166
164
  /**
167
165
  * Get the parent request status
168
166
  * @returns {number}
169
167
  */
170
168
  get status() {
171
- return this.request.status;
169
+ return this[_request].status;
172
170
  }
173
171
  set status(s) {
174
- this.request.status = s;
172
+ this[_request].status = s;
175
173
  }
176
174
  /**
177
175
  * Request error
178
176
  */
179
177
  get error() {
180
- return this.request.error;
178
+ return this[_request].error;
181
179
  }
182
180
  set error(e) {
183
- this.request.setError(e);
181
+ this[_request].setError(e);
184
182
  }
185
183
  /**
186
184
  * Request external ID
187
185
  */
188
186
  get requestId() {
189
- return this.request.id;
187
+ return this[_request].id;
190
188
  }
191
189
  /**
192
190
  * API controller name
193
191
  */
194
192
  get controller() {
195
- return this.request.input.controller;
193
+ return this[_request].input.controller;
196
194
  }
197
195
  /**
198
196
  * API action name
199
197
  */
200
198
  get action() {
201
- return this.request.input.action;
199
+ return this[_request].input.action;
202
200
  }
203
201
  /**
204
202
  * Collection name
205
203
  */
206
204
  get collection() {
207
- return this.request.input.resource.collection;
205
+ return this[_request].input.resource.collection;
208
206
  }
209
207
  /**
210
208
  * Index name
211
209
  */
212
210
  get index() {
213
- return this.request.input.resource.index;
211
+ return this[_request].input.resource.index;
214
212
  }
215
213
  /**
216
214
  * Volatile object
217
215
  */
218
216
  get volatile() {
219
- return this.request.input.volatile;
217
+ return this[_request].input.volatile;
220
218
  }
221
219
  /**
222
220
  * Response headers
223
221
  */
224
222
  get headers() {
225
- return this._headers.proxy;
223
+ return this[_headers].proxy;
226
224
  }
227
225
  /**
228
226
  * Request result
229
227
  */
230
228
  get result() {
231
- return this.request.result;
229
+ return this[_request].result;
232
230
  }
233
231
  set result(r) {
234
- this.request.setResult(r);
232
+ this[_request].setResult(r);
235
233
  }
236
234
  /**
237
235
  * Node identifier
238
236
  */
239
237
  get node() {
240
- // eslint-disable-next-line dot-notation
241
- if (global['_kuzzle'] && global.kuzzle) {
242
- return global.kuzzle.id;
243
- }
244
- return null;
238
+ return global.kuzzle.id;
245
239
  }
246
240
  /**
247
241
  * Configure the response
@@ -271,20 +265,20 @@ class RequestResponse {
271
265
  * Gets a header value (case-insensitive)
272
266
  */
273
267
  getHeader(name) {
274
- return this._headers.getHeader(name);
268
+ return this[_headers].getHeader(name);
275
269
  }
276
270
  /**
277
271
  * Deletes a header (case-insensitive)
278
272
  */
279
273
  removeHeader(name) {
280
- return this._headers.removeHeader(name);
274
+ return this[_headers].removeHeader(name);
281
275
  }
282
276
  /**
283
277
  * Sets a new array. Behaves the same as Node.js' HTTP response.setHeader
284
278
  * method (@see https://nodejs.org/api/http.html#http_response_setheader_name_value)
285
279
  */
286
280
  setHeader(name, value) {
287
- return this._headers.setHeader(name, value);
281
+ return this[_headers].setHeader(name, value);
288
282
  }
289
283
  /**
290
284
  * Adds new multiple headers.
@@ -307,7 +301,7 @@ class RequestResponse {
307
301
  if (this.raw === true) {
308
302
  return {
309
303
  content: this.result,
310
- headers: this._headers.headers,
304
+ headers: this.headers,
311
305
  raw: true,
312
306
  requestId: this.requestId,
313
307
  status: this.status,
@@ -327,7 +321,7 @@ class RequestResponse {
327
321
  status: this.status,
328
322
  volatile: this.volatile,
329
323
  },
330
- headers: this._headers.headers,
324
+ headers: this.headers,
331
325
  raw: false,
332
326
  requestId: this.requestId,
333
327
  status: this.status,
@@ -141,9 +141,6 @@ class BackendController extends index_1.ApplicationManager {
141
141
  this._add(controller.name, controller.definition);
142
142
  }
143
143
  _add(name, definition) {
144
- // Check definition here to throw error early
145
- // with the corresponding line number
146
- this._application.PluginObject.checkControllerDefinition(name, definition, { application: true });
147
144
  if (this._application._controllers[name]) {
148
145
  throw assertionError.get('invalid_controller_definition', name, 'A controller with this name already exists');
149
146
  }
@@ -264,7 +264,7 @@ class Plugin {
264
264
  return PLUGIN_NAME_REGEX.test(name);
265
265
  }
266
266
 
267
- static checkControllerDefinition (name, definition, { application=false } = {}) {
267
+ static async checkControllerDefinition (name, definition, { application=false } = {}) {
268
268
  if (typeof name !== 'string') {
269
269
  throw assertionError.get(
270
270
  'invalid_controller_definition',
@@ -344,7 +344,7 @@ class Plugin {
344
344
  }
345
345
 
346
346
  if (route.openapi) {
347
- checkOpenAPISpecification(name, action, route);
347
+ await checkOpenAPISpecification(name, action, route);
348
348
  }
349
349
  }
350
350
  }
@@ -365,20 +365,24 @@ function checkHttpRouteProperties (route, action, name, application) {
365
365
  }
366
366
  }
367
367
 
368
- function checkOpenAPISpecification (controller, action, route) {
368
+ async function checkOpenAPISpecification (controller, action, route) {
369
369
  if (! isPlainObject(route.openapi)) {
370
370
  throw assertionError.get(
371
371
  'invalid_controller_definition',
372
372
  controller,
373
- 'The "openapi" property must be an object');
373
+ 'The "openapi" property of the route definition must be an object');
374
374
  }
375
375
 
376
+ const routePath = route.path.charAt(0) === '/'
377
+ ? route.path
378
+ : `/_/${route.path}`;
379
+
376
380
  // Set :param notation to {param}
377
- const formattedPath = route.path.replace(/\/:([^/]*)/g,'/{$1}');
381
+ const formattedPath = routePath.replace(/\/:([^/]*)/g,'/{$1}');
378
382
 
379
- const { error, warning } = enforcer({
383
+ const { error, warning } = await enforcer({
380
384
  /* eslint-disable sort-keys */
381
- openapi: '3.0.1',
385
+ openapi: '3.0.0',
382
386
  info: {
383
387
  title: 'Kuzzle API',
384
388
  version: require('../../../package').version
@@ -394,8 +398,14 @@ function checkOpenAPISpecification (controller, action, route) {
394
398
  if (warning) {
395
399
  global.kuzzle.log.warn(`Warning for OpenAPI specification in "${controller}:${action}", ${route.openapi} : ${warning}`);
396
400
  }
401
+
397
402
  if (error) {
398
- throw controllerError.get('invalid_openapi_schema', controller, action, route.openapi, error);
403
+ throw controllerError.get(
404
+ 'invalid_openapi_schema',
405
+ controller,
406
+ action,
407
+ route.openapi,
408
+ error);
399
409
  }
400
410
  }
401
411
 
@@ -265,9 +265,7 @@ class PluginsManager {
265
265
 
266
266
  debug('[%s] plugin started', plugin.name);
267
267
 
268
- if (! plugin.application) {
269
- this.loadedPlugins.push(plugin.name);
270
- }
268
+ this.loadedPlugins.push(plugin.name);
271
269
 
272
270
  return null;
273
271
  });
@@ -680,7 +678,7 @@ class PluginsManager {
680
678
  'Native controllers cannot be overriden');
681
679
  }
682
680
 
683
- Plugin.checkControllerDefinition(controller, definition);
681
+ await Plugin.checkControllerDefinition(controller, definition);
684
682
 
685
683
  for (const [action, actionDefinition ] of Object.entries(definition.actions)) {
686
684
  let apiController = this.controllers.get(controller);