-
Notifications
You must be signed in to change notification settings - Fork 128
/
Copy pathfunnel.js
1118 lines (981 loc) · 31.9 KB
/
funnel.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Kuzzle, a backend software, self-hostable and ready to use
* to power modern apps
*
* Copyright 2015-2022 Kuzzle
* mailto: support AT kuzzle.io
* website: http://kuzzle.io
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
"use strict";
const Bluebird = require("bluebird");
const Deque = require("denque");
const Cookie = require("cookie");
const kuzzleStateEnum = require("../kuzzle/kuzzleStateEnum");
const { KuzzleError } = require("../kerror/errors");
const {
AdminController,
AuthController,
BulkController,
ClusterController,
CollectionController,
DebugController,
DocumentController,
IndexController,
MemoryStorageController,
RealtimeController,
SecurityController,
ServerController,
} = require("./controllers");
const { documentEventAliases } = require("../config/documentEventAliases");
const DocumentExtractor = require("./documentExtractor");
const sdkCompatibility = require("../config/sdkCompatibility");
const RateLimiter = require("./rateLimiter");
const kerror = require("../kerror");
const debug = require("../util/debug")("kuzzle:funnel");
const processError = kerror.wrap("api", "process");
const { has } = require("../util/safeObject");
const { HttpStream } = require("../types");
// Actions of the auth controller that does not necessite to verify the token
// when cookie auth is active
const SKIP_TOKEN_VERIF_ACTIONS = ["login", "checkToken", "logout"];
/**
* @class PendingRequest
* @param {Request} request
* @param {Function} fn
* @param {Object} context
*/
class PendingRequest {
constructor(request, fn, context) {
this.request = request;
this.fn = fn;
this.context = context;
}
}
/**
* @class Funnel
*/
class Funnel {
constructor() {
this.overloaded = false;
this.concurrentRequests = 0;
this.controllers = new Map();
this.pendingRequestsQueue = new Deque();
this.pendingRequestsById = new Map();
this.lastOverloadTime = 0;
this.overloadWarned = false;
this.lastWarningTime = 0;
this.rateLimiter = new RateLimiter();
this.lastDumpedErrors = {};
this.loadDocumentEventAliases();
this.sdkCompatibility = sdkCompatibility;
}
init() {
/**
* Returns true if the controller is one of Kuzzle native's one
*
* @param {String} name
*
* @returns {Boolean}
*/
global.kuzzle.onAsk("kuzzle:api:funnel:controller:isNative", (name) =>
this.isNativeController(name)
);
/**
* Returns inner metrics from the Funnel
* @returns {Object}
*/
global.kuzzle.onAsk("kuzzle:api:funnel:metrics", () => this.metrics());
this.rateLimiter.init();
this.controllers.set("auth", new AuthController());
this.controllers.set("bulk", new BulkController());
this.controllers.set("cluster", new ClusterController());
this.controllers.set("collection", new CollectionController());
this.controllers.set("debug", new DebugController());
this.controllers.set("document", new DocumentController());
this.controllers.set("index", new IndexController());
this.controllers.set("realtime", new RealtimeController());
this.controllers.set("security", new SecurityController());
this.controllers.set("server", new ServerController());
this.controllers.set("admin", new AdminController());
const msController = new MemoryStorageController();
this.controllers.set("memoryStorage", msController);
this.controllers.set("ms", msController);
const initPromises = Array.from(this.controllers.keys()).map((ctrl) =>
this.controllers.get(ctrl).init()
);
return Bluebird.all(initPromises);
}
loadDocumentEventAliases() {
this.documentEventAliases = documentEventAliases;
this.documentEventAliases.mirrorList = {};
Object.keys(documentEventAliases.list).forEach((alias) => {
documentEventAliases.list[alias].forEach((aliasOf) => {
this.documentEventAliases.mirrorList[aliasOf] = alias;
});
});
}
/**
* Asks the overload-protection system for an execution slot.
*
* Returns immediately true if the request can be
* executed.
*
* Otherwise, false is returned, and the caller MUST
* stop the request execution.
* In this case:
* - if it can be bufferized, then the request is left untouched
* and the executor function will be called later when a slot
* becomes available
* - if the buffer limit has been reached, a ServiceUnavailable error
* is set to the request. In that case, the executor is free to
* retry submitting the request later, or to abort it and return
* the request as it is
*
* @param {Function} fn - The function to be executed as soon as the slot is
* available.
* @param {Object} context - The execution context of the `fn` param.
* @param {String} request - The request object that will be passed as
* argument to `fn`.
* @returns {Boolean} - A boolean telling whether the request has been
* immediately executed or not.
*/
throttle(fn, context, request) {
if (global.kuzzle.state === kuzzleStateEnum.SHUTTING_DOWN) {
throw processError.get("shutting_down");
}
if (global.kuzzle.state === kuzzleStateEnum.NOT_ENOUGH_NODES) {
throw processError.get("not_enough_nodes");
}
const isRequestFromDebugSession =
request.context &&
request.context.connection &&
request.context.connection.misc &&
request.context.connection.misc.internal &&
request.context.connection.misc.internal.debugSession;
if (this.overloaded) {
const now = Date.now();
if (
this.pendingRequestsQueue.length >
global.kuzzle.config.limits.requestsBufferWarningThreshold &&
(this.lastWarningTime === 0 || this.lastWarningTime < now - 500)
) {
const overloadPercentage =
Math.round(
(10000 * this.pendingRequestsQueue.length) /
global.kuzzle.config.limits.requestsBufferSize
) / 100;
global.kuzzle.emit("core:overload", overloadPercentage);
global.kuzzle.log.warn(
`[!WARNING!] Kuzzle overloaded: ${overloadPercentage}%. Delaying requests...`
);
this.overloadWarned = true;
this.lastWarningTime = now;
}
}
if (
this.concurrentRequests < global.kuzzle.config.limits.concurrentRequests
) {
if (this.pendingRequestsById.has(request.internalId)) {
this.pendingRequestsById.delete(request.internalId);
}
// Execute fn immediately, since the slot is available
fn.call(context, request);
return true;
}
/*
If kuzzle is overloaded, check the requests cache.
There are two possibilities:
1- the cache limit has not been reached: the request is cached
and will be played as soon as the config.limits.concurrentRequests
property allows it
2- the number of cached requests is equal to the requestsBufferSize property.
The request is then discarded and an error is returned to the sender
*/
if (
this.pendingRequestsQueue.length >=
global.kuzzle.config.limits.requestsBufferSize &&
!isRequestFromDebugSession
) {
const error = processError.get("overloaded");
global.kuzzle.emit("log:error", error);
global.kuzzle.log.error(error);
throw error;
}
if (!this.pendingRequestsById.has(request.internalId)) {
this.pendingRequestsById.set(
request.internalId,
new PendingRequest(request, fn, context)
);
if (isRequestFromDebugSession) {
// Push at the front to prioritize debug requests
this.pendingRequestsQueue.unshift(request.internalId);
} else {
this.pendingRequestsQueue.push(request.internalId);
}
if (!this.overloaded) {
this.overloaded = true;
/*
/!\ Invoking this function here with setTimeout() leads to V8 deoptimizing
the entire getRequestSlot method (as of node.js 6.9.1),
because of an "out of bounds" heuristic error (caused by node's
setTimeout code written in JS? this needs further investigations)
We get better performances by keeping this method optimized by crankshaft
even if this means executing this function once for nothing each
time we start overload mode.
*/
this._playPendingRequests();
}
}
return false;
}
/**
* Execute the API request by
* 1/ check domain from origin header
* 2/ asking for a request slot,
* 3/ verify that the user is still connected
* 4/ checking if the requesting user has the right credentials
* 5/ send the request itself to the corresponding controller+action
*
* @param {Request} request
* @param {Function} callback
* @returns {Number} -1: request delayed, 0: request processing, 1: error
*/
execute(request, callback) {
if (!request.input.controller || !request.input.controller.length) {
callback(
kerror.get("api", "assert", "missing_argument", "controller"),
request
);
return 1;
}
if (!request.input.action || !request.input.action.length) {
callback(
kerror.get("api", "assert", "missing_argument", "action"),
request
);
return 1;
}
// If there is a body, retrieve the targets if any
const targets = request.getArray("targets", []);
/**
* Only index and collection as a pair or the targets parameter is allowed at the same time
* otherwise we throw an error because we don't know which one to use to verify the rights
*/
if (
targets.length > 0 &&
(request.input.args.index || request.input.args.collection)
) {
callback(
kerror.get(
"api",
"assert",
"mutually_exclusive",
"index, collection",
"targets"
),
request
);
return 1;
}
if (
request.input.headers &&
has(request.input.headers, "origin") &&
!this._isOriginAuthorized(request.input.headers.origin)
) {
debug(
"Reject request, unauthorized origin %s",
request.input.headers.origin
);
return this._executeError(
kerror.get(
"api",
"process",
"unauthorized_origin",
request.input.headers.origin
),
request,
true,
callback
);
}
try {
const executing = this.throttle(
(req) => {
// if the connection is closed there is no need to execute the request
// => discarding it
if (!global.kuzzle.router.isConnectionAlive(req.context)) {
debug("Client connection dead: dropping request: %a", req.input);
callback(processError.get("connection_dropped"), req);
return;
}
debug(
"Starting request %s:%s [%s]: %j",
req.input.controller,
req.input.action,
req.id,
req.input
);
global.kuzzle.asyncStore.run(() => {
global.kuzzle.asyncStore.set("REQUEST", req);
global.kuzzle
.pipe("request:beforeExecution", req)
.then((modifiedRequest) => {
let _request;
return this.checkRights(modifiedRequest)
.then((newModifiedRequest) => {
_request = newModifiedRequest;
return this.rateLimiter.isAllowed(_request);
})
.then((allowed) => {
if (!allowed) {
if (
request.input.controller === "auth" &&
request.input.action === "login"
) {
throw processError.get("too_many_logins_requests");
}
throw processError.get("too_many_requests");
}
return this.processRequest(_request);
})
.then((processResult) => {
debug(
"Request %s successfully executed. Result: %a",
modifiedRequest.id,
processResult
);
return global.kuzzle
.pipe("request:afterExecution", {
request: _request,
result: processResult,
success: true,
})
.then((pipeEvent) => {
callback(null, pipeEvent.result);
// disables a bluebird warning in dev. mode triggered when
// a promise is created and not returned
return null;
});
})
.catch((err) => {
debug(
"Error processing request %s: %a",
modifiedRequest.id,
err
);
return global.kuzzle
.pipe("request:afterExecution", {
error: err,
request: modifiedRequest,
success: false,
})
.then((pipeEvent) =>
this._executeError(
pipeEvent.error,
pipeEvent.request,
true,
callback
)
);
});
})
.catch((err) => {
debug("Error processing request %s: %a", req.id, err);
return global.kuzzle
.pipe("request:afterExecution", {
error: err,
request: req,
success: false,
})
.then((pipeEvent) =>
this._executeError(
pipeEvent.error,
pipeEvent.request,
true,
callback
)
);
});
});
},
this,
request
);
return executing ? 0 : -1;
} catch (error) {
request.setError(error);
callback(error, request);
return 1;
}
}
/**
* Checks if an error is worth dumping Kuzzle. If so,
* creates a dump.
*
* @param {KuzzleError|*} err
*/
handleErrorDump(err) {
const handledErrors = global.kuzzle.config.dump.handledErrors;
if (global.kuzzle.config.dump.enabled && handledErrors.enabled) {
setImmediate(() => {
const errorType =
typeof err === "object" && err.name ? err.name : typeof err;
if (handledErrors.whitelist.indexOf(errorType) > -1) {
const now = Date.now();
// JSON.stringify(new NativeError()) === '{}'
// i.e. Error, SyntaxError, TypeError, ReferenceError, etc.
global.kuzzle.log.error(
err instanceof Error && !(err instanceof KuzzleError)
? `${err.message}\n${err.stack}`
: err
);
if (
!this.lastDumpedErrors[errorType] ||
this.lastDumpedErrors[errorType] < now - handledErrors.minInterval
) {
// simplify error message to use it in folder dump name
let errorMessage = err.message;
if (errorMessage.indexOf("\n") > -1) {
errorMessage = errorMessage.split("\n")[0];
}
errorMessage = errorMessage
.toLowerCase()
.replace(/[^a-zA-Z0-9-_]/g, "-")
.split("-")
.filter((value) => value !== "")
.join("-");
global.kuzzle.dump(
`handled-${errorType.toLocaleLowerCase()}-${errorMessage}`
);
}
this.lastDumpedErrors[errorType] = now;
}
});
}
}
/**
* Checks if a user has the necessary rights to execute the action
*
* @param {Request} request
* @returns {Promise<Request>}
*/
async checkRights(request) {
if (
!global.kuzzle.config.http.cookieAuthentication &&
request.getBoolean("cookieAuth")
) {
throw kerror.get("security", "cookie", "unsupported");
}
let skipTokenVerification = false;
// When the Support of Cookie as Authentication Token is enabled we check if an auth token cookie is present
// When a request is made with cookieAuth set to true
// We try to use the auth token cookie if present as auth token
// otherwise check for auth token as input
if (request.input.headers && has(request.input.headers, "cookie")) {
let cookie;
try {
cookie = Cookie.parse(request.input.headers.cookie);
} catch (error) {
throw kerror.get("security", "cookie", "invalid");
}
// if cookie is present and not null, and a token is present we should throw because we don't know which one to use
if (cookie.authToken && cookie.authToken !== "null") {
if (!global.kuzzle.config.http.cookieAuthentication) {
throw kerror.get("security", "cookie", "unsupported");
}
if (request.input.jwt) {
throw kerror.get(
"security",
"token",
"verification_error",
"Both token and cookie are present, could not decide which one to use"
);
}
request.input.jwt = cookie.authToken;
skipTokenVerification =
request.getBoolean("cookieAuth") &&
request.input.controller === "auth" &&
SKIP_TOKEN_VERIF_ACTIONS.includes(request.input.action);
}
}
try {
// If the verification should be skipped, we pass a null token,
// this way the verification will be made as anonymous
const token = skipTokenVerification ? null : request.input.jwt;
request.context.token = await global.kuzzle.ask(
"core:security:token:verify",
token
);
} catch (error) {
await global.kuzzle.pipe("request:onUnauthorized", request);
throw error;
}
const userId = request.context.token.userId;
request.context.user = await global.kuzzle.ask(
"core:security:user:get",
userId
);
// If we have a token, link the connection with the token,
// this way the connection can be notified when the token has expired.
if (
global.kuzzle.config.internal.notifiableProtocols.includes(
request.context.connection.protocol
)
) {
global.kuzzle.tokenManager.link(
request.context.token,
request.context.connection.id
);
}
if (!(await request.context.user.isActionAllowed(request))) {
// anonymous user => 401 (Unauthorized) error
// logged-in user with insufficient permissions => 403 (Forbidden) error
const error = kerror.get(
"security",
"rights",
userId === "-1" ? "unauthorized" : "forbidden",
request.input.controller,
request.input.action,
request.context.user._id
);
request.setError(error);
await global.kuzzle.pipe("request:onUnauthorized", request);
throw error;
}
if (
global.kuzzle.config.plugins.common.failsafeMode &&
!this._isLogin(request) &&
!request.context.user.profileIds.includes("admin")
) {
await global.kuzzle.pipe("request:onUnauthorized", request);
throw kerror.get("security", "rights", "failsafe_mode_admin_only");
}
return global.kuzzle.pipe("request:onAuthorized", request);
}
_isLogin(request) {
return (
request.input.controller === "auth" && request.input.action === "login"
);
}
/**
* Executes the request immediately.
* /!\ To be used only by methods having already passed the overload check.
*
* @param {Request} request
* @returns {Promise}
*/
async processRequest(request) {
const controller = this.getController(request);
global.kuzzle.statistics.startRequest(request);
this.concurrentRequests++;
let _request = request;
try {
await this._checkSdkVersion(_request);
_request = await this.performDocumentAlias(_request, "before");
_request = await global.kuzzle.pipe(
this.getEventName(_request, "before"),
_request
);
const responseData = await doAction(controller, _request);
_request.setResult(responseData, {
status: _request.status === 102 ? 200 : _request.status,
});
if (
!this.isNativeController(_request.input.controller) &&
!_request.response.raw
) {
// check if the plugin response can be serialized
try {
if (!(responseData instanceof HttpStream)) {
JSON.stringify(responseData);
}
} catch (e) {
_request.setResult(null);
throw kerror.get("plugin", "controller", "unserializable_response");
}
}
_request = await global.kuzzle.pipe(
this.getEventName(_request, "after"),
_request
);
_request = await this.performDocumentAlias(_request, "after");
_request = await global.kuzzle.pipe("request:onSuccess", _request);
global.kuzzle.statistics.completedRequest(_request);
} catch (error) {
return this.handleProcessRequestError(_request, _request, error);
} finally {
this.concurrentRequests--;
}
return _request;
}
/**
* Triggers generic:document:* events
*
* @warning Critical code section
*
* @param {KuzzleRequest} request
* @param {String} prefix
*
* @returns {Promise<KuzzleRequest>}
*/
async performDocumentAlias(request, prefix) {
const { controller, action } = request.input;
const mustTrigger =
controller === "document" &&
this.documentEventAliases.mirrorList[action] &&
(prefix !== "before" ||
!this.documentEventAliases.notBefore.includes(action));
if (!mustTrigger) {
return request;
}
const alias = this.documentEventAliases.mirrorList[action];
const event = `${this.documentEventAliases.namespace}:${prefix}${capitalize(
alias
)}`;
const extractor = new DocumentExtractor(request);
const documents = await global.kuzzle.pipe(
event,
extractor.extract(),
request
);
return extractor.insert(documents);
}
/**
* Exposes API requests execution to plugins
*
* Similar to execute, except that:
* - plugin requests do not trigger API events
* - plugin requests are not counted towards requests statistics
* - the overload protection mechanism is disabled
*
* @param {Request} request
* @returns {Promise}
*/
async executePluginRequest(request) {
try {
return await doAction(this.getController(request), request);
} catch (e) {
this.handleErrorDump(e);
throw e;
}
}
async handleProcessRequestError(modifiedRequest, request, error) {
let _error = this._wrapError(request, error);
modifiedRequest.setError(_error);
try {
const updated = await global.kuzzle.pipe(
this.getEventName(modifiedRequest, "error"),
modifiedRequest
);
// If there is no pipe attached on this event, the same request is
// passed in resolve and we should reject it
if (updated.error !== null) {
throw updated.error;
}
// Pipe recovered from the error: returned the new result
return updated;
} catch (err) {
_error = this._wrapError(request, err);
}
// Handling the error thrown by the error pipe
modifiedRequest.setError(_error);
global.kuzzle.statistics.failedRequest(request);
try {
const updated = await global.kuzzle.pipe(
"request:onError",
modifiedRequest
);
if (updated === modifiedRequest) {
throw modifiedRequest.error;
}
return updated;
} catch (err) {
throw this._wrapError(request, err);
}
}
/**
* Helper function meant to normalize event names
* by retrieving controller aliases' original names.
*
* @param {Request} Executed request
* @param {string} prefix - event prefix
* @returns {string} event name
*/
getEventName(request, prefix) {
const event =
request.input.controller === "memoryStorage"
? "ms"
: request.input.controller;
return `${event}:${prefix}${capitalize(request.input.action)}`;
}
/**
* Returns the number of remaining requests
*
* @returns {number}
*/
get remainingRequests() {
return this.concurrentRequests + this.pendingRequestsQueue.length;
}
/**
* Return the controller corresponding to the action asked by the request
*
* @param {Request} request
* @returns {Object} controller object
* @throws {BadRequestError} If the asked controller or action is unknown
*/
getController(request) {
for (const controllers of [
this.controllers,
global.kuzzle.pluginsManager.controllers,
]) {
const controller = controllers.get(request.input.controller);
if (controller) {
if (controller._isAction(request.input.action)) {
return controller;
}
throw processError.get(
"action_not_found",
request.input.controller,
request.input.action
);
}
}
throw processError.get("controller_not_found", request.input.controller);
}
/**
* Tell if the controller is a native controller or not
* @param {String} controller
* @returns {Boolean}
*/
isNativeController(controller) {
return this.controllers.has(controller);
}
/**
* Returns inner metrics from the Funnel
* @returns {Object}
*/
metrics() {
return {
concurrentRequests: this.concurrentRequests,
pendingRequests: this.pendingRequestsQueue.length,
};
}
/**
* If the request is coming from an official SDK,
* then checks the compatibility of the SDK against current Kuzzle version.
*
* @param {Request} request
*
* @throws
*/
_checkSdkVersion(request) {
if (!global.kuzzle.config.server.strictSdkVersion) {
return;
}
const sdkVersion =
request.input.volatile && request.input.volatile.sdkVersion;
const sdkName = request.input.volatile && request.input.volatile.sdkName;
// sdkVersion property is only used by Kuzzle v1 SDKs
if (sdkVersion) {
throw processError.get(
"incompatible_sdk_version",
sdkVersion,
"Kuzzle v2"
);
}
if (!sdkName || typeof sdkName !== "string") {
return;
}
const separatorIdx = sdkName.indexOf("@"),
name = sdkName.substr(0, separatorIdx),
version = sdkName.substr(separatorIdx + 1);
if (name.length === 0 || version.length === 0) {
return;
}
const requirements = this.sdkCompatibility[name];
if (!requirements) {
return;
}
if (!satisfiesMajor(version, requirements)) {
const hint = `min: ${requirements.min || "none"}, max: ${
requirements.max || "none"
}`;
throw processError.get("incompatible_sdk_version", version, hint);
}
}
/**
* Populates the given request with the error and calls the callback
*
* @param {Error} error
* @param {Request} request
* @param {boolean} asError - if set to true, calls the callback with its first argument as error
* @param {Function} callback
* @returns {null}
* @private
*/
_executeError(error, request, asError, callback) {
request.setError(error);
if (asError) {
callback(error, request);
this.handleErrorDump(error);
} else {
callback(null, request);
}
return null;
}
/**
* Background task. Checks if there are any requests in cache, and replay them
* if Kuzzle is not overloaded anymore,
*/
_playPendingRequests() {
// If there is room to play bufferized requests, do it now. If not, retry later
const quantityToInject = Math.min(
this.pendingRequestsQueue.length,
global.kuzzle.config.limits.concurrentRequests - this.concurrentRequests
);
if (quantityToInject > 0) {
for (let i = 0; i < quantityToInject; i++) {
const pendingItem = this.pendingRequestsById.get(
this.pendingRequestsQueue.peekFront()
);
try {
if (
this.throttle(
pendingItem.fn,
pendingItem.context,
pendingItem.request
)
) {
this.pendingRequestsQueue.shift();
} else {
break;
}
} catch (error) {
break;
}
}
}
if (this.pendingRequestsQueue.length > 0) {
setTimeout(() => this._playPendingRequests(), 0);
} else {
const now = Date.now();
// No request remaining in cache => stop the background task and return to normal behavior
this.overloaded = false;
if (
this.overloadWarned &&
(this.lastOverloadTime === 0 || this.lastOverloadTime < now - 500)
) {