This repository has been archived by the owner on Jun 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathclient.ts
696 lines (633 loc) · 19.9 KB
/
client.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
import {
CircuitBreaker,
CircuitBreakerOptions,
Metrics as CircuitBreakerMetrics,
CircuitBreakerPublicApi
} from "./circuit-breaker";
import { operation } from "./retry";
import * as url from "url";
import {
ConnectionTimeoutError,
NetworkError,
ReadTimeoutError,
request,
RequestError,
ServiceClientRequestOptions,
ServiceClientResponse,
TimingPhases,
Timings,
UserTimeoutError,
BodyStreamError
} from "./request";
export {
CircuitBreaker,
CircuitBreakerOptions,
CircuitBreakerMetrics,
CircuitBreakerPublicApi,
ServiceClientResponse,
ServiceClientRequestOptions
};
/**
* This interface defines factory function for getting a circuit breaker
*/
export type CircuitBreakerFactory = (
params: ServiceClientRequestOptions
) => CircuitBreaker;
/**
* A request filter may introduce one or both functions in this interface. For more information
* regarding request filters, refer to the readme of this project.
*/
export interface ServiceClientRequestFilter {
/**
* This callback is called before the requests is done.
* You can short-circuit the request by both returning
* a ServiceClient.Response object which is helpful for
* implementing caching or mocking. You could also
* fail the request by throwing an Error.
* @throws {Error}
*/
request?: (
requestOptions: ServiceClientRequestOptions
) =>
| ServiceClientResponse
| ServiceClientRequestOptions
| Promise<ServiceClientResponse | ServiceClientRequestOptions>;
/**
* This callback is called after the response has arrived.
* @throws {Error}
*/
response?: (
response: ServiceClientResponse
) => ServiceClientResponse | Promise<ServiceClientResponse>;
}
/**
* This interface describes all the options that may be passed to the service client at construction time.
*/
export class ServiceClientOptions {
/**
* This is the only mandatory option when creating a service client. All other properties have
* reasonable defaults.
*/
public hostname = "";
/**
* Name of the client. Primarily used in errors. Defaults to hostname.
*/
public name?: string;
/**
* If this property is not provided, the {@link ServiceClient.DEFAULT_FILTERS} will be used.
*/
public filters?: ServiceClientRequestFilter[];
/**
* should the service client record request timings?
*/
public timing?: boolean;
public autoParseJson?: boolean;
public retryOptions?: {
retries?: number;
factor?: number;
minTimeout?: number;
maxTimeout?: number;
randomize?: boolean;
shouldRetry?: (
err?: ServiceClientError,
req?: ServiceClientRequestOptions
) => boolean;
onRetry?: (
currentAttempt?: number,
err?: ServiceClientError,
req?: ServiceClientRequestOptions
) => void;
};
public circuitBreaker?: false | CircuitBreakerOptions | CircuitBreakerFactory;
public defaultRequestOptions?: Partial<ServiceClientRequestOptions>;
}
/**
* Internal only, this interface guarantees, that the service client has all options available at runtime.
*/
class ServiceClientStrictOptions {
public hostname: string;
public filters: ServiceClientRequestFilter[];
public timing: boolean;
public autoParseJson: boolean;
public retryOptions: {
retries: number;
factor: number;
minTimeout: number;
maxTimeout: number;
randomize: boolean;
shouldRetry: (
err?: ServiceClientError,
req?: ServiceClientRequestOptions
) => boolean;
onRetry: (
currentAttempt?: number,
err?: ServiceClientError,
req?: ServiceClientRequestOptions
) => void;
};
public defaultRequestOptions: ServiceClientRequestOptions;
constructor(options: ServiceClientOptions) {
if (!options.hostname) {
throw new Error("Please provide a `hostname` for this client");
}
this.hostname = options.hostname;
this.filters = Array.isArray(options.filters)
? options.filters
: [...ServiceClient.DEFAULT_FILTERS];
this.timing = Boolean(options.timing);
const autoParseJson = options.autoParseJson;
this.autoParseJson = autoParseJson === undefined ? true : autoParseJson;
this.retryOptions = {
factor: 2,
maxTimeout: 400,
minTimeout: 200,
randomize: true,
retries: 0,
shouldRetry() {
return true;
},
onRetry() {
/* do nothing */
},
...options.retryOptions
};
if (
(this.retryOptions.minTimeout || 0) > (this.retryOptions.maxTimeout || 0)
) {
throw new TypeError(
"The `maxTimeout` must be equal to or greater than the `minTimeout`"
);
}
this.defaultRequestOptions = {
pathname: "/",
protocol: "https:",
timeout: 2000,
...options.defaultRequestOptions
};
}
}
/**
* A custom error returned in case something goes wrong.
*/
export abstract class ServiceClientError extends Error {
public timings?: Timings;
public timingPhases?: TimingPhases;
public retryErrors: ServiceClientError[];
/**
* Use `instanceof` checks instead.
* @deprecated since 0.9.0
*/
public type: string;
protected constructor(
originalError: Error,
type: string,
public response?: ServiceClientResponse,
name = "ServiceClient"
) {
super(`${name}: ${type}. ${originalError.message || ""}`);
this.type = type;
this.retryErrors = [];
// Does not copy `message` from the original error
Object.assign(this, originalError);
const timingSource: {
timings?: Timings;
timingPhases?: TimingPhases;
// This is necessary to shut up TypeScript as otherwise it treats
// types with all optional properties differently
// eslint-disable-next-line @typescript-eslint/ban-types
constructor: Function;
} = response || originalError;
this.timings = timingSource.timings;
this.timingPhases = timingSource.timingPhases;
}
}
export class CircuitOpenError extends ServiceClientError {
constructor(originalError: Error, name: string) {
super(originalError, ServiceClient.CIRCUIT_OPEN, undefined, name);
}
}
export class BodyParseError extends ServiceClientError {
constructor(
originalError: Error,
public response: ServiceClientResponse,
name: string
) {
super(originalError, ServiceClient.BODY_PARSE_FAILED, response, name);
}
}
export class RequestFilterError extends ServiceClientError {
constructor(originalError: Error, name: string) {
super(originalError, ServiceClient.REQUEST_FILTER_FAILED, undefined, name);
}
}
export class ResponseFilterError extends ServiceClientError {
constructor(
originalError: Error,
public response: ServiceClientResponse,
name: string
) {
super(originalError, ServiceClient.RESPONSE_FILTER_FAILED, response, name);
}
}
export class RequestNetworkError extends ServiceClientError {
public requestOptions: ServiceClientRequestOptions;
constructor(originalError: RequestError, name: string) {
super(originalError, ServiceClient.REQUEST_FAILED, undefined, name);
this.requestOptions = originalError.requestOptions;
}
}
export class RequestConnectionTimeoutError extends ServiceClientError {
public requestOptions: ServiceClientRequestOptions;
constructor(originalError: RequestError, name: string) {
super(originalError, ServiceClient.REQUEST_FAILED, undefined, name);
this.requestOptions = originalError.requestOptions;
}
}
export class RequestReadTimeoutError extends ServiceClientError {
public requestOptions: ServiceClientRequestOptions;
constructor(originalError: RequestError, name: string) {
super(originalError, ServiceClient.REQUEST_FAILED, undefined, name);
this.requestOptions = originalError.requestOptions;
}
}
export class RequestUserTimeoutError extends ServiceClientError {
public requestOptions: ServiceClientRequestOptions;
constructor(originalError: RequestError, name: string) {
super(originalError, ServiceClient.REQUEST_FAILED, undefined, name);
this.requestOptions = originalError.requestOptions;
}
}
export class RequestBodyStreamError extends ServiceClientError {
public requestOptions: ServiceClientRequestOptions;
constructor(originalError: RequestError, name: string) {
super(originalError, ServiceClient.REQUEST_FAILED, undefined, name);
this.requestOptions = originalError.requestOptions;
}
}
export class ShouldRetryRejectedError extends ServiceClientError {
constructor(originalError: Error, type: string, name: string) {
super(originalError, type, undefined, name);
}
}
export class MaximumRetriesReachedError extends ServiceClientError {
constructor(originalError: Error, type: string, name: string) {
super(originalError, type, undefined, name);
}
}
export class InternalError extends ServiceClientError {
constructor(originalError: Error, name: string) {
super(originalError, ServiceClient.INTERNAL_ERROR, undefined, name);
}
}
const JSON_CONTENT_TYPE_REGEX = /application\/(.*?[+])?json/i;
/**
* This function takes a response and if it is of type json, tries to parse the body.
*/
const decodeResponse = (
client: ServiceClient,
response: ServiceClientResponse
): ServiceClientResponse => {
const contentType =
response.headers["content-type"] ||
(response.body ? "application/json" : "");
if (
typeof response.body === "string" &&
JSON_CONTENT_TYPE_REGEX.test(contentType)
) {
try {
response.body = JSON.parse(response.body);
} catch (error) {
throw new BodyParseError(error, response, client.name);
}
}
return response;
};
/**
* Reducer function to unwind response filters.
*/
const unwindResponseFilters = (
promise: Promise<ServiceClientResponse>,
filter: ServiceClientRequestFilter
): Promise<ServiceClientResponse> => {
return promise.then(params =>
filter.response ? filter.response(params) : params
);
};
/**
* Actually performs the request and applies the available filters in their respective phases.
*/
const requestWithFilters = (
client: ServiceClient,
requestOptions: ServiceClientRequestOptions,
filters: ServiceClientRequestFilter[],
autoParseJson: boolean
): Promise<ServiceClientResponse> => {
const pendingResponseFilters: ServiceClientRequestFilter[] = [];
const requestFilterPromise = filters.reduce(
(
promise: Promise<ServiceClientResponse | ServiceClientRequestOptions>,
filter
) => {
return promise.then(params => {
if (params instanceof ServiceClientResponse) {
return params;
}
const filtered = filter.request ? filter.request(params) : params;
// also apply this filter when unwinding the chain
pendingResponseFilters.unshift(filter);
return filtered;
});
},
Promise.resolve(requestOptions)
);
return requestFilterPromise
.catch((error: Error) => {
throw new RequestFilterError(error, client.name);
})
.then(paramsOrResponse =>
paramsOrResponse instanceof ServiceClientResponse
? paramsOrResponse
: request(paramsOrResponse).catch((error: RequestError) => {
if (error instanceof ConnectionTimeoutError) {
throw new RequestConnectionTimeoutError(error, client.name);
} else if (error instanceof UserTimeoutError) {
throw new RequestUserTimeoutError(error, client.name);
} else if (error instanceof BodyStreamError) {
throw new RequestBodyStreamError(error, client.name);
} else if (error instanceof ReadTimeoutError) {
throw new RequestReadTimeoutError(error, client.name);
} else if (error instanceof NetworkError) {
throw new RequestNetworkError(error, client.name);
} else {
throw error;
}
})
)
.then(rawResponse =>
autoParseJson ? decodeResponse(client, rawResponse) : rawResponse
)
.then(resp =>
pendingResponseFilters
.reduce(unwindResponseFilters, Promise.resolve(resp))
.catch(error => {
throw new ResponseFilterError(error, resp, client.name);
})
);
};
const noop = () => {
/* do nothing */
};
const noopBreaker: CircuitBreakerPublicApi = {
run(command) {
command(noop, noop);
},
forceClose: () => null,
forceOpen: () => null,
unforce: () => null,
isOpen: () => false
};
const buildStatusCodeFilter = (
isError: (statusCode: number) => boolean
): ServiceClientRequestFilter => {
return {
response(response: ServiceClientResponse) {
if (isError(response.statusCode)) {
return Promise.reject(
new Error(`Response status ${response.statusCode}`)
);
}
return Promise.resolve(response);
}
};
};
export class ServiceClient {
/**
* This filter will mark 5xx responses as failures. This is relevant for the circuit breaker.
*/
public static treat5xxAsError: ServiceClientRequestFilter = buildStatusCodeFilter(
statusCode => statusCode >= 500
);
/**
* This filter will mark 4xx responses as failures. This is relevant for the circuit breaker.
*
* This is not the default behaviour!
*/
public static treat4xxAsError: ServiceClientRequestFilter = buildStatusCodeFilter(
statusCode => statusCode >= 400 && statusCode < 500
);
/**
* Use `instanceof BodyParseError` check instead
* @deprecated since 0.9.0
*/
public static BODY_PARSE_FAILED = "Parsing of the response body failed";
/**
* Use `instanceof RequestFailedError` check instead
* @deprecated since 0.9.0
*/
public static REQUEST_FAILED = "HTTP Request failed";
/**
* Use `instanceof RequestFilterError` check instead
* @deprecated since 0.9.0
*/
public static REQUEST_FILTER_FAILED =
"Request filter marked request as failed";
/**
* Use `instanceof ResponseFilterError` check instead
* @deprecated since 0.9.0
*/
public static RESPONSE_FILTER_FAILED =
"Response filter marked request as failed";
/**
* Use `instanceof CircuitOpenError` check instead
* @deprecated since 0.9.0
*/
public static CIRCUIT_OPEN =
"Circuit breaker is open and prevented the request";
/**
* Use `instanceof CircuitOpenError` check instead
* @deprecated since 0.9.0
*/
public static INTERNAL_ERROR =
"Perron internal error due to a bug or misconfiguration";
/**
* Default list of post-filters which includes
* `ServiceClient.treat5xxAsError`
*/
public static DEFAULT_FILTERS: ReadonlyArray<
ServiceClientRequestFilter
> = Object.freeze([ServiceClient.treat5xxAsError]);
/**
* Interface that the response will implement.
* Deprecated: Use ServiceClientResponse instead.
* @see ServiceClientResponse
* @deprecated
*/
public static Response = ServiceClientResponse;
/**
* Interface, that errors will implement.
* Deprecated: Use ServiceClientError instead.
* @see ServiceClientError
* @deprecated
*/
public static Error = ServiceClientError;
public name: string;
private breaker?: CircuitBreaker;
private breakerFactory?: CircuitBreakerFactory;
private options: ServiceClientStrictOptions;
/**
* A ServiceClient can be constructed with all defaults by simply providing a URL, that can be parsed
* by nodes url parser. Alternatively, provide actual ServiceClientOptions, that implement the
* @{link ServiceClientOptions} interface.
*/
constructor(optionsOrUrl: ServiceClientOptions | string) {
let options: ServiceClientOptions;
if (typeof optionsOrUrl === "string") {
const {
port,
protocol,
query,
hostname = "",
pathname = "/"
} = url.parse(optionsOrUrl, true);
options = {
hostname,
defaultRequestOptions: {
port,
protocol,
query,
// pathname will be overwritten in actual usage, we just guarantee a sane default
pathname
}
};
} else {
options = optionsOrUrl;
}
if (typeof options.circuitBreaker === "object") {
const breakerOptions = {
windowDuration: 10000,
numBuckets: 10,
timeoutDuration: 2000,
errorThreshold: 50,
volumeThreshold: 10,
...options.circuitBreaker
};
this.breaker = new CircuitBreaker(breakerOptions);
}
if (typeof options.circuitBreaker === "function") {
this.breakerFactory = options.circuitBreaker;
}
this.options = new ServiceClientStrictOptions(options);
this.name = options.name || options.hostname;
}
/**
* Return an instance of a CircuitBreaker based on params.
* Choses between a factory and a single static breaker
*/
public getCircuitBreaker(
params: ServiceClientRequestOptions
): CircuitBreakerPublicApi {
if (this.breaker) {
return this.breaker;
}
if (this.breakerFactory) {
return this.breakerFactory(params);
}
return noopBreaker;
}
/**
* Perform a request to the service using given @{link ServiceClientRequestOptions}, returning the result in a promise.
*/
public request(
userParams: ServiceClientRequestOptions
): Promise<ServiceClientResponse> {
const params = { ...this.options.defaultRequestOptions, ...userParams };
params.hostname = this.options.hostname;
params.port = params.port || (params.protocol === "https:" ? 443 : 80);
params.timing =
params.timing !== undefined ? params.timing : this.options.timing;
params.headers = {
accept: "application/json",
...params.headers
};
const {
retries,
factor,
minTimeout,
maxTimeout,
randomize,
shouldRetry,
onRetry
} = this.options.retryOptions;
const opts = {
retries,
factor,
minTimeout,
maxTimeout,
randomize
};
const retryErrors: ServiceClientError[] = [];
return new Promise<ServiceClientResponse>((resolve, reject) => {
const breaker = this.getCircuitBreaker(params);
const retryOperation = operation(opts, (currentAttempt: number) => {
breaker.run(
(success: () => void, failure: () => void) => {
return requestWithFilters(
this,
params,
this.options.filters || [],
this.options.autoParseJson
)
.then((result: ServiceClientResponse) => {
success();
result.retryErrors = retryErrors;
resolve(result);
})
.catch((error: ServiceClientError) => {
retryErrors.push(error);
failure();
if (!shouldRetry(error, params)) {
reject(
new ShouldRetryRejectedError(error, error.type, this.name)
);
return;
}
if (!retryOperation.retry()) {
// Wrapping error when user does not want retries would result
// in bad developer experience where you always have to unwrap it
// knowing there is only one error inside, so we do not do that.
if (retries === 0) {
reject(error);
} else {
reject(
new MaximumRetriesReachedError(
error,
error.type,
this.name
)
);
}
return;
}
onRetry(currentAttempt + 1, error, params);
});
},
() => {
reject(new CircuitOpenError(new Error(), this.name));
}
);
});
retryOperation.attempt();
}).catch((error: unknown) => {
const rawError =
error instanceof Error ? error : new Error(String(error));
const wrappedError =
rawError instanceof ServiceClientError
? rawError
: new InternalError(rawError, this.name);
wrappedError.retryErrors = retryErrors;
throw wrappedError;
});
}
}
Object.freeze(ServiceClient);
export default ServiceClient;