kuzzle 2.15.2 → 2.16.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.
@@ -19,37 +19,13 @@
19
19
  * See the License for the specific language governing permissions and
20
20
  * limitations under the License.
21
21
  */
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;
22
+ var __importDefault = (this && this.__importDefault) || function (mod) {
23
+ return (mod && mod.__esModule) ? mod : { "default": mod };
40
24
  };
41
25
  Object.defineProperty(exports, "__esModule", { value: true });
42
26
  exports.RequestInput = exports.RequestResource = void 0;
43
27
  const internalError_1 = require("../../kerror/errors/internalError");
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';
28
+ const assertType_1 = __importDefault(require("../../util/assertType"));
53
29
  // any property not listed here will be copied into
54
30
  // RequestInput.args
55
31
  const resourceProperties = new Set([
@@ -68,6 +44,7 @@ class RequestResource {
68
44
  }
69
45
  /**
70
46
  * Document ID
47
+ * @deprecated
71
48
  */
72
49
  get _id() {
73
50
  return this.args._id;
@@ -77,6 +54,7 @@ class RequestResource {
77
54
  }
78
55
  /**
79
56
  * Index name
57
+ * @deprecated
80
58
  */
81
59
  get index() {
82
60
  return this.args.index;
@@ -86,6 +64,7 @@ class RequestResource {
86
64
  }
87
65
  /**
88
66
  * Collection name
67
+ * @deprecated
89
68
  */
90
69
  get collection() {
91
70
  return this.args.collection;
@@ -114,14 +93,109 @@ class RequestInput {
114
93
  * Any undefined option is set to null
115
94
  */
116
95
  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;
117
191
  if (!data || typeof data !== 'object' || Array.isArray(data)) {
118
192
  throw new internalError_1.InternalError('Input request data must be a non-null object');
119
193
  }
120
- this[_jwt] = null;
121
- this[_volatile] = null;
122
- this[_body] = null;
123
- this[_controller] = null;
124
- this[_action] = null;
194
+ this.jwt = null;
195
+ this.volatile = null;
196
+ this.body = null;
197
+ this.controller = null;
198
+ this.action = null;
125
199
  // default value to null for former "resources" to avoid breaking
126
200
  this.args = {};
127
201
  this.resource = new RequestResource(this.args);
@@ -131,149 +205,17 @@ class RequestInput {
131
205
  this.args[k] = data[k];
132
206
  }
133
207
  }
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);
208
+ if (data.jwt) {
209
+ this.jwt = data.jwt;
194
210
  }
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);
211
+ if (data.volatile) {
212
+ this.volatile = data.volatile;
220
213
  }
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
- get headers() {
250
- return this[_headers];
251
- }
252
- set headers(obj) {
253
- this[_headers] = assert.assertObject('headers', obj);
254
- }
255
- /**
256
- * Volatile object.
257
- * @example
258
- * // original JSON request sent to Kuzzle
259
- * {
260
- * controller
261
- * action,
262
- * _id,
263
- * index,
264
- * collection,
265
- * jwt,
266
- * refresh,
267
- * foobar,
268
- * volatile, <== that
269
- * body
270
- * }
271
- */
272
- get volatile() {
273
- return this[_volatile];
274
- }
275
- set volatile(obj) {
276
- this[_volatile] = assert.assertObject('volatile', obj);
214
+ if (data.body) {
215
+ this.body = data.body;
216
+ }
217
+ this.controller = assertType_1.default.assertString('controller', data.controller);
218
+ this.action = assertType_1.default.assertString('controller', data.action);
277
219
  }
278
220
  }
279
221
  exports.RequestInput = RequestInput;
@@ -1,17 +1,19 @@
1
1
  import { JSONObject } from 'kuzzle-sdk';
2
+ import '../../../lib/types/Global';
2
3
  import { Deprecation } from '../../types';
3
4
  import { KuzzleError } from '../../kerror/errors/kuzzleError';
5
+ import { KuzzleRequest } from './kuzzleRequest';
4
6
  export declare class Headers {
5
7
  headers: JSONObject;
6
8
  private namesMap;
7
- private proxy;
9
+ proxy: any;
8
10
  constructor();
9
11
  /**
10
12
  * Gets a header value
11
13
  *
12
14
  * @param name Header name. Could be a string (case-insensitive) or a symbol
13
15
  */
14
- getHeader(name: any): string | void;
16
+ getHeader(name: any): string;
15
17
  removeHeader(name: string): boolean;
16
18
  setHeader(name: string, value: string): boolean;
17
19
  }
@@ -19,20 +21,22 @@ export declare class Headers {
19
21
  * Kuzzle normalized API response
20
22
  */
21
23
  export declare class RequestResponse {
24
+ private request;
25
+ private _headers;
22
26
  /**
23
27
  * If sets to true, "result" content will not be wrapped in a Kuzzle response
24
28
  */
25
29
  raw: boolean;
26
- constructor(request: any);
30
+ constructor(request: KuzzleRequest);
27
31
  /**
28
32
  * Get the parent request deprecations
29
33
  */
30
- get deprecations(): Array<Deprecation> | void;
34
+ get deprecations(): Array<Deprecation>;
31
35
  /**
32
36
  * Set the parent request deprecations
33
37
  * @param {Object[]} deprecations
34
38
  */
35
- set deprecations(deprecations: Array<Deprecation> | void);
39
+ set deprecations(deprecations: Array<Deprecation>);
36
40
  /**
37
41
  * Get the parent request status
38
42
  * @returns {number}
@@ -99,16 +103,16 @@ export declare class RequestResponse {
99
103
  /**
100
104
  * Gets a header value (case-insensitive)
101
105
  */
102
- getHeader(name: string): string | null;
106
+ getHeader(name: string): string;
103
107
  /**
104
108
  * Deletes a header (case-insensitive)
105
109
  */
106
- removeHeader(name: string): any;
110
+ removeHeader(name: string): boolean;
107
111
  /**
108
112
  * Sets a new array. Behaves the same as Node.js' HTTP response.setHeader
109
113
  * method (@see https://nodejs.org/api/http.html#http_response_setheader_name_value)
110
114
  */
111
- setHeader(name: string, value: string): any;
115
+ setHeader(name: string, value: string): boolean;
112
116
  /**
113
117
  * Adds new multiple headers.
114
118
  */
@@ -40,11 +40,8 @@ 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");
43
44
  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';
48
45
  class Headers {
49
46
  constructor() {
50
47
  this.namesMap = new Map();
@@ -54,7 +51,10 @@ class Headers {
54
51
  get: (target, name) => this.getHeader(name),
55
52
  set: (target, name, value) => this.setHeader(name, value),
56
53
  });
57
- this.setHeader('X-Kuzzle-Node', global.kuzzle.id);
54
+ // eslint-disable-next-line dot-notation
55
+ if (global['_kuzzle'] && global.kuzzle) {
56
+ this.setHeader('X-Kuzzle-Node', global.kuzzle.id);
57
+ }
58
58
  }
59
59
  /**
60
60
  * Gets a header value
@@ -144,98 +144,104 @@ exports.Headers = Headers;
144
144
  class RequestResponse {
145
145
  constructor(request) {
146
146
  this.raw = false;
147
- this[_request] = request;
148
- this[_headers] = new Headers();
149
- Object.seal(this);
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();
150
152
  }
151
153
  /**
152
154
  * Get the parent request deprecations
153
155
  */
154
156
  get deprecations() {
155
- return this[_request].deprecations;
157
+ return this.request.deprecations;
156
158
  }
157
159
  /**
158
160
  * Set the parent request deprecations
159
161
  * @param {Object[]} deprecations
160
162
  */
161
163
  set deprecations(deprecations) {
162
- this[_request].deprecations = deprecations;
164
+ this.request.deprecations = deprecations;
163
165
  }
164
166
  /**
165
167
  * Get the parent request status
166
168
  * @returns {number}
167
169
  */
168
170
  get status() {
169
- return this[_request].status;
171
+ return this.request.status;
170
172
  }
171
173
  set status(s) {
172
- this[_request].status = s;
174
+ this.request.status = s;
173
175
  }
174
176
  /**
175
177
  * Request error
176
178
  */
177
179
  get error() {
178
- return this[_request].error;
180
+ return this.request.error;
179
181
  }
180
182
  set error(e) {
181
- this[_request].setError(e);
183
+ this.request.setError(e);
182
184
  }
183
185
  /**
184
186
  * Request external ID
185
187
  */
186
188
  get requestId() {
187
- return this[_request].id;
189
+ return this.request.id;
188
190
  }
189
191
  /**
190
192
  * API controller name
191
193
  */
192
194
  get controller() {
193
- return this[_request].input.controller;
195
+ return this.request.input.controller;
194
196
  }
195
197
  /**
196
198
  * API action name
197
199
  */
198
200
  get action() {
199
- return this[_request].input.action;
201
+ return this.request.input.action;
200
202
  }
201
203
  /**
202
204
  * Collection name
203
205
  */
204
206
  get collection() {
205
- return this[_request].input.resource.collection;
207
+ return this.request.input.resource.collection;
206
208
  }
207
209
  /**
208
210
  * Index name
209
211
  */
210
212
  get index() {
211
- return this[_request].input.resource.index;
213
+ return this.request.input.resource.index;
212
214
  }
213
215
  /**
214
216
  * Volatile object
215
217
  */
216
218
  get volatile() {
217
- return this[_request].input.volatile;
219
+ return this.request.input.volatile;
218
220
  }
219
221
  /**
220
222
  * Response headers
221
223
  */
222
224
  get headers() {
223
- return this[_headers].proxy;
225
+ return this._headers.proxy;
224
226
  }
225
227
  /**
226
228
  * Request result
227
229
  */
228
230
  get result() {
229
- return this[_request].result;
231
+ return this.request.result;
230
232
  }
231
233
  set result(r) {
232
- this[_request].setResult(r);
234
+ this.request.setResult(r);
233
235
  }
234
236
  /**
235
237
  * Node identifier
236
238
  */
237
239
  get node() {
238
- return global.kuzzle.id;
240
+ // eslint-disable-next-line dot-notation
241
+ if (global['_kuzzle'] && global.kuzzle) {
242
+ return global.kuzzle.id;
243
+ }
244
+ return null;
239
245
  }
240
246
  /**
241
247
  * Configure the response
@@ -265,20 +271,20 @@ class RequestResponse {
265
271
  * Gets a header value (case-insensitive)
266
272
  */
267
273
  getHeader(name) {
268
- return this[_headers].getHeader(name);
274
+ return this._headers.getHeader(name);
269
275
  }
270
276
  /**
271
277
  * Deletes a header (case-insensitive)
272
278
  */
273
279
  removeHeader(name) {
274
- return this[_headers].removeHeader(name);
280
+ return this._headers.removeHeader(name);
275
281
  }
276
282
  /**
277
283
  * Sets a new array. Behaves the same as Node.js' HTTP response.setHeader
278
284
  * method (@see https://nodejs.org/api/http.html#http_response_setheader_name_value)
279
285
  */
280
286
  setHeader(name, value) {
281
- return this[_headers].setHeader(name, value);
287
+ return this._headers.setHeader(name, value);
282
288
  }
283
289
  /**
284
290
  * Adds new multiple headers.
@@ -301,7 +307,7 @@ class RequestResponse {
301
307
  if (this.raw === true) {
302
308
  return {
303
309
  content: this.result,
304
- headers: this.headers,
310
+ headers: this._headers.headers,
305
311
  raw: true,
306
312
  requestId: this.requestId,
307
313
  status: this.status,
@@ -321,7 +327,7 @@ class RequestResponse {
321
327
  status: this.status,
322
328
  volatile: this.volatile,
323
329
  },
324
- headers: this.headers,
330
+ headers: this._headers.headers,
325
331
  raw: false,
326
332
  requestId: this.requestId,
327
333
  status: this.status,
@@ -46,7 +46,7 @@ module.exports = {
46
46
  (see https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS)
47
47
  */
48
48
  http: {
49
- routes: require('./httpRoutes'),
49
+ routes: require('./../api/httpRoutes'),
50
50
  accessControlAllowOrigin: '*',
51
51
  accessControlAllowOriginUseRegExp: false,
52
52
  accessControlAllowMethods: 'GET,POST,PUT,PATCH,DELETE,OPTIONS,HEAD',
@@ -138,6 +138,39 @@ class Router {
138
138
  this._executeFromHttp(route.verb, request, cb);
139
139
  });
140
140
  }
141
+
142
+ /**
143
+ * Returns inner metrics from the router
144
+ * @returns {Object}
145
+ */
146
+ global.kuzzle.onAsk(
147
+ 'core:network:router:metrics',
148
+ () => this.metrics());
149
+ }
150
+
151
+ /**
152
+ * Returns the metrics of the router
153
+ * @returns {Object}
154
+ */
155
+ metrics () {
156
+ const connectionsByProtocol = {};
157
+
158
+ for (const connection of this.connections.values()) {
159
+ const protocol = connection.connection.protocol.toLowerCase();
160
+ if (protocol === 'internal') {
161
+ continue;
162
+ }
163
+
164
+ if (connectionsByProtocol[protocol] === undefined) {
165
+ connectionsByProtocol[protocol] = 0;
166
+ }
167
+
168
+ connectionsByProtocol[protocol]++;
169
+ }
170
+
171
+ return {
172
+ connections: connectionsByProtocol,
173
+ };
141
174
  }
142
175
 
143
176
  /**
@@ -265,7 +265,9 @@ class PluginsManager {
265
265
 
266
266
  debug('[%s] plugin started', plugin.name);
267
267
 
268
- this.loadedPlugins.push(plugin.name);
268
+ if (! plugin.application) {
269
+ this.loadedPlugins.push(plugin.name);
270
+ }
269
271
 
270
272
  return null;
271
273
  });
@@ -118,6 +118,13 @@ export declare class HotelClerk {
118
118
  *
119
119
  */
120
120
  unsubscribe(connectionId: string, roomId: string, notify?: boolean): Promise<void>;
121
+ /**
122
+ * Returns inner metrics from the HotelClerk
123
+ */
124
+ metrics(): {
125
+ rooms: number;
126
+ subscriptions: number;
127
+ };
121
128
  /**
122
129
  * Deletes a room if no user has subscribed to it, and removes it also from the
123
130
  * real-time engine
@@ -136,6 +136,11 @@ class HotelClerk {
136
136
  global.kuzzle.onAsk('core:realtime:unsubscribe', (connectionId, roomId, notify) => {
137
137
  return this.unsubscribe(connectionId, roomId, notify);
138
138
  });
139
+ /**
140
+ * Returns inner metrics from the HotelClerk
141
+ * @return {{rooms: number, subscriptions: number}}
142
+ */
143
+ global.kuzzle.onAsk('core:realtime:hotelClerk:metrics', () => this.metrics());
139
144
  /**
140
145
  * Clear the hotel clerk and properly disconnect connections.
141
146
  */
@@ -410,6 +415,15 @@ class HotelClerk {
410
415
  subscription,
411
416
  });
412
417
  }
418
+ /**
419
+ * Returns inner metrics from the HotelClerk
420
+ */
421
+ metrics() {
422
+ return {
423
+ rooms: this.roomsCount,
424
+ subscriptions: this.subscriptions.size,
425
+ };
426
+ }
413
427
  /**
414
428
  * Deletes a room if no user has subscribed to it, and removes it also from the
415
429
  * real-time engine