-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathconnection.ts
1233 lines (1185 loc) · 34.8 KB
/
connection.ts
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
/**
* ```ts
* import type { ArangoApiResponse } from "arangojs/connection";
* ```
*
* The "connection" module provides connection related types for TypeScript.
*
* @packageDocumentation
*/
import * as administration from "./administration.js";
import * as configuration from "./configuration.js";
import * as databases from "./databases.js";
import * as errors from "./errors.js";
import { ERROR_ARANGO_CONFLICT } from "./lib/codes.js";
import * as util from "./lib/util.js";
import { LinkedList } from "./lib/x3-linkedlist.js";
const MIME_JSON = /\/(json|javascript)(\W|$)/;
const LEADER_ENDPOINT_HEADER = "x-arango-endpoint";
const REASON_TIMEOUT = "timeout";
//#region Host
/**
* @internal
*/
type Host = {
/**
* @internal
*
* Perform a fetch request against this host.
*
* @param pathname - URL path, relative to the server URL.
* @param options - Options for this fetch request.
*/
fetch: (
options: Omit<
RequestOptions,
| "maxRetries"
| "retryOnConflict"
| "allowDirtyRead"
| "hostUrl"
| "expectBinary"
| "isBinary"
>
) => Promise<globalThis.Response & { request: globalThis.Request }>;
/**
* @internal
*
* Close the pending request, if any.
*/
close: () => void;
};
/**
* @internal
*
* Create a function for performing fetch requests against a given host.
*
* @param arangojsHostUrl - Base URL of the host, i.e. protocol, port and domain name.
* @param options - Options to use for all fetch requests.
*/
function createHost(arangojsHostUrl: string, agentOptions?: any): Host {
const baseUrl = new URL(arangojsHostUrl);
let fetch = globalThis.fetch;
let createDispatcher: (() => Promise<any>) | undefined;
let dispatcher: any;
let socketPath: string | undefined;
if (arangojsHostUrl.match(/^\w+:\/\/unix:\//)) {
socketPath = baseUrl.pathname;
baseUrl.hostname = "localhost";
baseUrl.pathname = "/";
agentOptions = {
...agentOptions,
connect: {
...agentOptions?.connect,
socketPath,
},
};
}
if (agentOptions) {
createDispatcher = async () => {
let undici: any;
try {
// Prevent overzealous bundlers from attempting to bundle undici
const undiciName = "undici";
undici = await import(undiciName);
} catch (cause) {
if (socketPath) {
throw new Error("Undici is required for Unix domain sockets", {
cause,
});
}
throw new Error("Undici is required when using config.agentOptions", {
cause,
});
}
fetch = undici.fetch;
return new undici.Agent(agentOptions);
};
}
const pending = new Map<string, AbortController>();
return {
async fetch({
method,
pathname,
search,
headers: requestHeaders,
body,
timeout,
fetchOptions,
beforeRequest,
afterResponse,
}: Omit<
RequestOptions,
| "maxRetries"
| "retryOnConflict"
| "allowDirtyRead"
| "hostUrl"
| "expectBinary"
| "isBinary"
>) {
const url = new URL(pathname + baseUrl.search, baseUrl);
if (search) {
const searchParams =
search instanceof URLSearchParams
? search
: new URLSearchParams(search);
for (const [key, value] of searchParams) {
url.searchParams.append(key, value);
}
}
const headers = new Headers(requestHeaders);
if (!headers.has("authorization")) {
headers.set(
"authorization",
`Basic ${btoa(
`${baseUrl.username || "root"}:${baseUrl.password || ""}`
)}`
);
}
const abortController = new AbortController();
const signal = abortController.signal;
if (createDispatcher) {
dispatcher = await createDispatcher();
createDispatcher = undefined;
}
const request = new Request(url, {
...fetchOptions,
dispatcher,
method,
headers,
body,
signal,
} as globalThis.RequestInit);
if (beforeRequest) {
const p = beforeRequest(request);
if (p instanceof Promise) await p;
}
const requestId = util.generateRequestId();
pending.set(requestId, abortController);
let clearTimer: (() => void) | undefined;
if (timeout) {
clearTimer = util.createTimer(timeout, () => {
clearTimer = undefined;
abortController.abort(REASON_TIMEOUT);
});
}
let response: globalThis.Response & { request: globalThis.Request };
try {
response = Object.assign(await fetch(request), {
request,
arangojsHostUrl,
});
if (fetchOptions?.redirect === "manual" && isRedirect(response)) {
throw new errors.HttpError(response);
}
} catch (e: unknown) {
const cause = e instanceof Error ? e : new Error(String(e));
let error: errors.NetworkError;
if (cause instanceof errors.NetworkError) {
error = cause;
} else if (signal.aborted) {
const reason =
typeof signal.reason == "string" ? signal.reason : undefined;
if (reason === REASON_TIMEOUT) {
error = new errors.ResponseTimeoutError(undefined, request, {
cause,
});
} else {
error = new errors.RequestAbortedError(reason, request, { cause });
}
} else if (cause instanceof TypeError) {
error = new errors.FetchFailedError(undefined, request, { cause });
} else {
error = new errors.NetworkError(cause.message, request, { cause });
}
if (afterResponse) {
const p = afterResponse(error);
if (p instanceof Promise) await p;
}
throw error;
} finally {
clearTimer?.();
pending.delete(requestId);
}
if (afterResponse) {
const p = afterResponse(null, response);
if (p instanceof Promise) await p;
}
return response;
},
close() {
if (!pending.size) return;
const controllers = [...pending.values()];
pending.clear();
for (const controller of controllers) {
try {
controller.abort();
} catch (e) {
// noop
}
}
},
};
}
//#endregion
//#region Response types
const STATUS_CODE_DEFAULT_MESSAGES = {
0: "Network Error",
300: "Multiple Choices",
301: "Moved Permanently",
302: "Found",
303: "See Other",
304: "Not Modified",
307: "Temporary Redirect",
308: "Permanent Redirect",
400: "Bad Request",
401: "Unauthorized",
402: "Payment Required",
403: "Forbidden",
404: "Not Found",
405: "Method Not Allowed",
406: "Not Acceptable",
407: "Proxy Authentication Required",
408: "Request Timeout",
409: "Conflict",
410: "Gone",
411: "Length Required",
412: "Precondition Failed",
413: "Payload Too Large",
414: "Request-URI Too Long",
415: "Unsupported Media Type",
416: "Requested Range Not Satisfiable",
417: "Expectation Failed",
418: "I'm a teapot",
421: "Misdirected Request",
422: "Unprocessable Entity",
423: "Locked",
424: "Failed Dependency",
426: "Upgrade Required",
428: "Precondition Required",
429: "Too Many Requests",
431: "Request Header Fields Too Large",
444: "Connection Closed Without Response",
451: "Unavailable For Legal Reasons",
499: "Client Closed Request",
500: "Internal Server Error",
501: "Not Implemented",
502: "Bad Gateway",
503: "Service Unavailable",
504: "Gateway Timeout",
505: "HTTP Version Not Supported",
506: "Variant Also Negotiates",
507: "Insufficient Storage",
508: "Loop Detected",
510: "Not Extended",
511: "Network Authentication Required",
599: "Network Connect Timeout Error",
};
type KnownStatusCode = keyof typeof STATUS_CODE_DEFAULT_MESSAGES;
const KNOWN_STATUS_CODES = Object.keys(STATUS_CODE_DEFAULT_MESSAGES).map((k) =>
Number(k)
) as KnownStatusCode[];
const REDIRECT_CODES = [301, 302, 303, 307, 308] satisfies KnownStatusCode[];
type RedirectStatusCode = (typeof REDIRECT_CODES)[number];
/**
* @internal
*
* Indicates whether the given status code can be translated to a known status
* message.
*/
function isKnownStatusCode(code: number): code is KnownStatusCode {
return KNOWN_STATUS_CODES.includes(code as KnownStatusCode);
}
/**
* @internal
*
* Indicates whether the given status code represents a redirect.
*/
function isRedirect(response: ProcessedResponse): boolean {
return REDIRECT_CODES.includes(response.status as RedirectStatusCode);
}
/**
* Returns the status message for the given response's status code or the
* status text of the response.
*/
export function getStatusMessage(response: ProcessedResponse): string {
if (isKnownStatusCode(response.status)) {
return STATUS_CODE_DEFAULT_MESSAGES[response.status];
}
if (response.statusText) return response.statusText;
return "Unknown response status";
}
/**
* Generic properties shared by all ArangoDB HTTP API responses.
*/
export type ArangoResponseMetadata = {
/**
* Indicates that the request was successful.
*/
error: false;
/**
* Response status code, typically `200`.
*/
code: number;
};
/**
* Extends the given base type `T` with the generic HTTP API response properties.
*/
export type ArangoApiResponse<T> = T & ArangoResponseMetadata;
/**
* Indicates whether the given value represents an ArangoDB error response.
*/
export function isArangoErrorResponse(
body: unknown
): body is ArangoErrorResponse {
if (!body || typeof body !== "object") return false;
const obj = body as Record<string, unknown>;
return (
obj.error === true &&
typeof obj.errorMessage === "string" &&
typeof obj.errorNum === "number" &&
(obj.code === undefined || typeof obj.code === "number")
);
}
/**
* Interface representing an ArangoDB error response.
*/
export type ArangoErrorResponse = {
/**
* Indicates that the request resulted in an error.
*/
error: true;
/**
* Intended response status code as provided in the response body.
*/
code?: number;
/**
* Error message as provided in the response body.
*/
errorMessage: string;
/**
* ArangoDB error code as provided in the response body.
*
* See the [ArangoDB error documentation](https://docs.arangodb.com/stable/develop/error-codes-and-meanings/)
* for more information.
*/
errorNum: number;
};
/**
* Processed response object.
*/
export interface ProcessedResponse<T = any> extends globalThis.Response {
/**
* @internal
*
* Identifier of the ArangoDB host that served this request.
*/
arangojsHostUrl?: string;
/**
* Fetch request object.
*/
request: globalThis.Request;
/**
* Parsed response body.
*/
parsedBody?: T;
}
//#endregion
//#region Request options
/**
* Options available for requests made with the Fetch API.
*/
export type CommonFetchOptions = {
/**
* Headers object containing any additional headers to send with the request.
*
* Note that the `Authorization` header will be overridden if the `auth`
* configuration option is set.
*/
headers?:
| string[][]
| Record<string, string | ReadonlyArray<string>>
| Headers;
/**
* Controls whether the socket should be reused for subsequent requests.
*
* Default: `false`
*/
keepalive?: boolean;
/**
* Controls what to do when the response status code is a redirect.
*
* - `"error"`: Abort with a network error.
* - `"follow"`: Automatically follow redirects.
* - `"manual"`: Abort with an `HttpError`.
*
* Default: `"follow"`
*/
redirect?: "error" | "follow" | "manual";
/**
* Value to use for the `Referer` header.
*
* If set to `"about:client"`, the default value for the context in which the
* request is made will be used.
*
* Default: `"about:client"`
*/
referrer?: string;
/**
* (Browser only.) Controls the Attribution Reporting API specific behavior.
*
* See the [MDN Web Docs](https://developer.mozilla.org/en-US/docs/Web/API/RequestInit)
* for more information on the available options.
*/
attributionReporting?: any;
/**
* (Browser only.) Cache mode to use for the request.
*
* See [the Fetch API specification](https://fetch.spec.whatwg.org/#request-class)
* or the [MDN Web Docs](https://developer.mozilla.org/en-US/docs/Web/API/RequestInit)
* for more information on the available options.
*/
cache?: string;
/**
* (Browser only.) Controls sending of credentials and cookies.
*
* - `"omit"`: Never send cookies.
* - `"include"`: Always send cookies.
* - `"same-origin"`: Only send cookies if the request is to the same origin.
*
* Default: `"same-origin"`
*/
credentials?: "omit" | "include" | "same-origin";
/**
* (Node.js only.) Undici `Dispatcher` instance to use for the request.
*
* Defaults to the global dispatcher.
*/
dispatcher?: any;
/**
* (Browser only.) Sets cross-origin behavior for the request.
*
* See [the Fetch API specification](https://fetch.spec.whatwg.org/#request-class)
* or the [MDN Web Docs](https://developer.mozilla.org/en-US/docs/Web/API/RequestInit)
* for more information on the available options.
*
* Default: `"cors"`
*/
mode?: string;
/**
* (Browser only.) Request priority relative to other requests of the same type.
*
* See [the Fetch API specification](https://fetch.spec.whatwg.org/#request-class)
* or the [MDN Web Docs](https://developer.mozilla.org/en-US/docs/Web/API/RequestInit)
* for more information on the available options.
*
* Default: `"auto"`
*/
priority?: "low" | "high" | "auto";
/**
* (Browser only.) Policy to use for the `Referer` header, equivalent to the
* semantics of the `Referrer-Policy` header.
*
* See [the Fetch API specification](https://fetch.spec.whatwg.org/#request-class)
* or the [MDN Web Docs](https://developer.mozilla.org/en-US/docs/Web/API/RequestInit)
* for more information on the available options.
*/
referrerPolicy?: string;
};
/**
* Fetch-specific options for performing a request with arangojs.
*/
export type FetchOptions = CommonFetchOptions & {
/**
* Subresource integrity value to use for the request, formatted as
* `<hash-algorithm>-<hash-source>`.
*/
integrity?: `${string}-${string}`;
};
/**
* Options that can be shared globally for all requests made with arangojs.
*/
export type CommonRequestOptions = {
/**
* Determines the behavior when a request fails because the underlying
* connection to the server could not be opened
* (e.g. [`ECONNREFUSED` in Node.js](https://nodejs.org/api/errors.html#errors_common_system_errors)):
*
* - `false`: the request fails immediately.
*
* - `0`: the request is retried until a server can be reached but only a
* total number of times matching the number of known servers (including
* the initial failed request).
*
* - any other number: the request is retried until a server can be reached
* or the request has been retried a total of `maxRetries` number of times
* (not including the initial failed request).
*
* When working with a single server, the retries (if any) will be made to
* the same server.
*
* This setting currently has no effect when using arangojs in a browser.
*
* **Note**: Requests bound to a specific server (e.g. fetching query results)
* will never be retried automatically and ignore this setting.
*
* **Note**: To set the number of retries when a write-write conflict is
* encountered, see `retryOnConflict` instead.
*
* Default: `0`
*/
maxRetries?: false | number;
/**
* If set to a positive number, requests will automatically be retried at
* most this many times if they result in a write-write conflict.
*
* Default: `0`
*/
retryOnConflict?: number;
/**
* Time in milliseconds after which arangojs will abort the request if the
* socket has not already timed out.
*/
timeout?: number;
/**
* Whether ArangoDB is allowed to perform a dirty read to respond to the
* request. If set to `true`, the response may reflect a dirty state from
* a non-authoritative server.
*
* Default: `false`
*/
allowDirtyRead?: boolean;
/**
* Callback that will be invoked with the finished request object before it
* is finalized. In the browser the request may already have been sent.
*
* @param req - Request object or XHR instance used for this request.
*/
beforeRequest?: (req: globalThis.Request) => void | Promise<void>;
/**
* Callback that will be invoked when the server response has been received
* and processed or when the request has been failed without a response.
*
* The originating request will be available as the `request` property
* on either the error or response object.
*
* @param err - Error encountered when handling this request or `null`.
* @param res - Response object for this request, if no error occurred.
*/
afterResponse?: (
err: errors.NetworkError | null,
res?: globalThis.Response & { request: globalThis.Request }
) => void | Promise<void>;
};
/**
* Options for performing a request with arangojs.
*/
export type RequestOptions = CommonRequestOptions & {
/**
* @internal
*
* Identifier of a specific ArangoDB host to use when more than one is known.
*/
hostUrl?: string;
/**
* HTTP method to use in order to perform the request.
*
* Default: `"GET"`
*/
method?: string;
/**
* URL path, relative to the server domain.
*/
pathname?: string;
/**
* URL parameters to pass as part of the query string.
*/
search?: URLSearchParams | Record<string, any>;
/**
* Headers object containing any additional headers to send with the request.
*
* Note that the `Authorization` header will be overridden if the `auth`
* configuration option is set.
*/
headers?:
| string[][]
| Record<string, string | ReadonlyArray<string>>
| Headers;
/**
* Request body data.
*/
body?: any;
/**
* Additional options to pass to the `fetch` function.
*/
fetchOptions?: Omit<FetchOptions, "headers">;
/**
* If set to `true`, the request body will not be converted to JSON and
* instead passed as-is.
*/
isBinary?: boolean;
/**
* If set to `true`, the response body will not be interpreted as JSON and
* instead passed as-is.
*/
expectBinary?: boolean;
};
//#endregion
//#region Connection class
/**
* @internal
*/
type Task<T = any> = {
stack?: () => string;
resolve: (result: T) => void;
reject: (error: unknown) => void;
transform?: (res: ProcessedResponse<any>) => T;
retries: number;
conflicts: number;
options: RequestOptions;
};
/**
* Indicates whether the given value represents a {@link Connection}.
*
* @param connection - A value that might be a connection.
*
* @internal
*/
export function isArangoConnection(connection: any): connection is Connection {
return Boolean(connection && connection.isArangoConnection);
}
/**
* Represents a connection pool shared by one or more databases.
*
* @internal
*/
export class Connection {
protected _activeTasks: number = 0;
protected _arangoVersion: number;
protected _loadBalancingStrategy: configuration.LoadBalancingStrategy;
protected _taskPoolSize: number;
protected _commonRequestOptions: CommonRequestOptions;
protected _commonFetchOptions: CommonFetchOptions & { headers: Headers };
protected _queue = new LinkedList<Task>();
protected _databases = new Map<string, databases.Database>();
protected _hosts: Host[] = [];
protected _hostUrls: string[] = [];
protected _activeHostUrl: string;
protected _activeDirtyHostUrl: string;
protected _transactionId: string | null = null;
protected _onError?: (err: Error) => void | Promise<void>;
protected _precaptureStackTraces: boolean;
protected _queueTimes = new LinkedList<[number, number]>();
protected _responseQueueTimeSamples: number;
/**
* @internal
*
* Creates a new `Connection` instance.
*
* @param config - An object with configuration options.
*
*/
constructor(config: Omit<configuration.ConfigOptions, "databaseName"> = {}) {
const {
url = "http://127.0.0.1:8529",
auth,
arangoVersion = 31100,
loadBalancingStrategy = "NONE",
maxRetries = 0,
poolSize = 3 *
(loadBalancingStrategy === "ROUND_ROBIN" && Array.isArray(url)
? url.length
: 1),
fetchOptions: { headers, ...commonFetchOptions } = {},
onError,
precaptureStackTraces = false,
responseQueueTimeSamples = 10,
...commonRequestOptions
} = config;
const URLS = Array.isArray(url) ? url : [url];
this._loadBalancingStrategy = loadBalancingStrategy;
this._precaptureStackTraces = precaptureStackTraces;
this._responseQueueTimeSamples =
responseQueueTimeSamples < 0 ? Infinity : responseQueueTimeSamples;
this._arangoVersion = arangoVersion;
this._taskPoolSize = poolSize;
this._onError = onError;
this._commonRequestOptions = commonRequestOptions;
this._commonFetchOptions = {
headers: new Headers(headers),
...commonFetchOptions,
};
this._commonFetchOptions.headers.set(
"x-arango-version",
String(arangoVersion)
);
this._commonFetchOptions.headers.set(
"x-arango-driver",
`arangojs/${process.env.ARANGOJS_VERSION} (cloud)`
);
this.addToHostList(URLS);
if (auth) {
if (configuration.isBearerAuth(auth)) {
this.setBearerAuth(auth);
} else {
this.setBasicAuth(auth);
}
}
if (this._loadBalancingStrategy === "ONE_RANDOM") {
this._activeHostUrl =
this._hostUrls[Math.floor(Math.random() * this._hostUrls.length)];
this._activeDirtyHostUrl =
this._hostUrls[Math.floor(Math.random() * this._hostUrls.length)];
} else {
this._activeHostUrl = this._hostUrls[0];
this._activeDirtyHostUrl = this._hostUrls[0];
}
}
/**
* @internal
*
* Indicates that this object represents an ArangoDB connection.
*/
get isArangoConnection(): true {
return true;
}
get queueTime(): administration.QueueTimeMetrics {
return {
getLatest: () => this._queueTimes.last?.value[1],
getValues: () => Array.from(this._queueTimes.values()),
getAvg: () => {
let avg = 0;
for (const [, [, value]] of this._queueTimes) {
avg += value / this._queueTimes.length;
}
return avg;
},
};
}
protected async _runQueue() {
if (this._activeTasks >= this._taskPoolSize) return;
const task = this._queue.shift();
if (!task) return;
let hostUrl = this._activeHostUrl;
try {
this._activeTasks += 1;
if (task.options.hostUrl !== undefined) {
hostUrl = task.options.hostUrl;
} else if (task.options.allowDirtyRead) {
hostUrl = this._activeDirtyHostUrl;
const i = this._hostUrls.indexOf(this._activeDirtyHostUrl) + 1;
this._activeDirtyHostUrl = this._hostUrls[i % this._hostUrls.length];
} else if (this._loadBalancingStrategy === "ROUND_ROBIN") {
const i = this._hostUrls.indexOf(this._activeHostUrl) + 1;
this._activeHostUrl = this._hostUrls[i % this._hostUrls.length];
}
const host = this._hosts[this._hostUrls.indexOf(hostUrl)];
const res: globalThis.Response & {
request: globalThis.Request;
arangojsHostUrl: string;
parsedBody?: any;
} = Object.assign(await host.fetch(task.options), {
arangojsHostUrl: hostUrl,
});
const leaderEndpoint = res.headers.get(LEADER_ENDPOINT_HEADER);
if (res.status === 503 && leaderEndpoint) {
const [cleanUrl] = this.addToHostList(leaderEndpoint);
task.options.hostUrl = cleanUrl;
if (this._activeHostUrl === hostUrl) {
this._activeHostUrl = cleanUrl;
}
this._queue.push(task);
return;
}
const queueTime = res.headers.get("x-arango-queue-time-seconds");
if (queueTime) {
this._queueTimes.push([Date.now(), Number(queueTime)]);
while (this._responseQueueTimeSamples < this._queueTimes.length) {
this._queueTimes.shift();
}
}
const contentType = res.headers.get("content-type");
if (res.status >= 400) {
if (contentType?.match(MIME_JSON)) {
const errorResponse = res.clone();
let errorBody: any;
try {
errorBody = await errorResponse.json();
} catch {
// noop
}
if (isArangoErrorResponse(errorBody)) {
res.parsedBody = errorBody;
throw errors.ArangoError.from(res);
}
}
throw new errors.HttpError(res);
}
if (res.body) {
if (task.options.expectBinary) {
res.parsedBody = await res.blob();
} else if (contentType?.match(MIME_JSON)) {
res.parsedBody = await res.json();
} else {
res.parsedBody = await res.text();
}
}
let result: any = res;
if (task.transform) result = task.transform(res);
task.resolve(result);
} catch (e: unknown) {
const err = e as Error;
if (
!task.options.allowDirtyRead &&
this._hosts.length > 1 &&
this._activeHostUrl === hostUrl &&
this._loadBalancingStrategy !== "ROUND_ROBIN"
) {
const i = this._hostUrls.indexOf(this._activeHostUrl) + 1;
this._activeHostUrl = this._hostUrls[i % this._hostUrls.length];
}
if (
errors.isArangoError(err) &&
err.errorNum === ERROR_ARANGO_CONFLICT &&
task.options.retryOnConflict &&
task.conflicts < task.options.retryOnConflict
) {
task.conflicts += 1;
this._queue.push(task);
return;
}
if (
(errors.isNetworkError(err) || errors.isArangoError(err)) &&
err.isSafeToRetry &&
task.options.hostUrl === undefined &&
this._commonRequestOptions.maxRetries !== false &&
task.retries <
(this._commonRequestOptions.maxRetries || this._hosts.length - 1)
) {
task.retries += 1;
this._queue.push(task);
return;
}
if (task.stack) {
err.stack += task.stack();
}
if (this._onError) {
try {
const p = this._onError(err);
if (p instanceof Promise) await p;
} catch (e) {
(e as Error).cause = err;
task.reject(e);
return;
}
}
task.reject(err);
} finally {
this._activeTasks -= 1;
setTimeout(() => this._runQueue(), 0);
}
}
setBearerAuth(auth: configuration.BearerAuthCredentials) {
this.setHeader("authorization", `Bearer ${auth.token}`);
}
setBasicAuth(auth: configuration.BasicAuthCredentials) {
this.setHeader(
"authorization",
`Basic ${btoa(`${auth.username}:${auth.password}`)}`
);
}
setResponseQueueTimeSamples(responseQueueTimeSamples: number) {
if (responseQueueTimeSamples < 0) {
responseQueueTimeSamples = Infinity;
}
this._responseQueueTimeSamples = responseQueueTimeSamples;
while (this._responseQueueTimeSamples < this._queueTimes.length) {
this._queueTimes.shift();
}
}
/**
* @internal
*
* Fetches a {@link databases.Database} instance for the given database name from the
* internal cache, if available.
*
* @param databaseName - Name of the database.
*/
database(databaseName: string): databases.Database | undefined;
/**
* @internal
*
* Adds a {@link databases.Database} instance for the given database name to the
* internal cache.
*
* @param databaseName - Name of the database.
* @param database - Database instance to add to the cache.
*/
database(
databaseName: string,
database: databases.Database
): databases.Database;
/**
* @internal
*
* Clears any {@link databases.Database} instance stored for the given database name
* from the internal cache, if present.
*
* @param databaseName - Name of the database.
* @param database - Must be `null`.
*/
database(databaseName: string, database: null): undefined;
database(
databaseName: string,
database?: databases.Database | null
): databases.Database | undefined {
if (database === null) {
this._databases.delete(databaseName);
return undefined;
}
if (!database) {
return this._databases.get(databaseName);
}
this._databases.set(databaseName, database);
return database;
}
/**
* @internal
*
* Replaces the host list with the given URLs.
*
* See {@link Connection#acquireHostList}.
*
* @param urls - URLs to use as host list.
*/
setHostList(urls: string[]): void {
const cleanUrls = urls.map((url) => util.normalizeUrl(url));
this._hosts.splice(
0,
this._hosts.length,
...cleanUrls.map((url) => {
const i = this._hostUrls.indexOf(url);
if (i !== -1) return this._hosts[i];
return createHost(url);
})
);
this._hostUrls.splice(0, this._hostUrls.length, ...cleanUrls);
}