fa-consul 1.0.1

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.
Files changed (125) hide show
  1. package/README.md +112 -0
  2. package/dist/__tests__/lib/logger.d.ts +3 -0
  3. package/dist/__tests__/lib/logger.d.ts.map +1 -0
  4. package/dist/__tests__/lib/logger.js +25 -0
  5. package/dist/__tests__/lib/logger.js.map +1 -0
  6. package/dist/access-points/access-points-updater.d.ts +19 -0
  7. package/dist/access-points/access-points-updater.d.ts.map +1 -0
  8. package/dist/access-points/access-points-updater.js +141 -0
  9. package/dist/access-points/access-points-updater.js.map +1 -0
  10. package/dist/access-points/access-points-utils.d.ts +4 -0
  11. package/dist/access-points/access-points-utils.d.ts.map +1 -0
  12. package/dist/access-points/access-points-utils.js +27 -0
  13. package/dist/access-points/access-points-utils.js.map +1 -0
  14. package/dist/access-points/access-points.d.ts +20 -0
  15. package/dist/access-points/access-points.d.ts.map +1 -0
  16. package/dist/access-points/access-points.js +166 -0
  17. package/dist/access-points/access-points.js.map +1 -0
  18. package/dist/constants.d.ts +7 -0
  19. package/dist/constants.d.ts.map +1 -0
  20. package/dist/constants.js +7 -0
  21. package/dist/constants.js.map +1 -0
  22. package/dist/consul-client/endpoints/agent.d.ts +31 -0
  23. package/dist/consul-client/endpoints/agent.d.ts.map +1 -0
  24. package/dist/consul-client/endpoints/agent.js +87 -0
  25. package/dist/consul-client/endpoints/agent.js.map +1 -0
  26. package/dist/consul-client/endpoints/catalog.d.ts +11 -0
  27. package/dist/consul-client/endpoints/catalog.d.ts.map +1 -0
  28. package/dist/consul-client/endpoints/catalog.js +14 -0
  29. package/dist/consul-client/endpoints/catalog.js.map +1 -0
  30. package/dist/consul-client/endpoints/health.d.ts +18 -0
  31. package/dist/consul-client/endpoints/health.d.ts.map +1 -0
  32. package/dist/consul-client/endpoints/health.js +28 -0
  33. package/dist/consul-client/endpoints/health.js.map +1 -0
  34. package/dist/consul-client/http-client.d.ts +26 -0
  35. package/dist/consul-client/http-client.d.ts.map +1 -0
  36. package/dist/consul-client/http-client.js +139 -0
  37. package/dist/consul-client/http-client.js.map +1 -0
  38. package/dist/consul-client/index.d.ts +16 -0
  39. package/dist/consul-client/index.d.ts.map +1 -0
  40. package/dist/consul-client/index.js +25 -0
  41. package/dist/consul-client/index.js.map +1 -0
  42. package/dist/consul-client/types.d.ts +126 -0
  43. package/dist/consul-client/types.d.ts.map +1 -0
  44. package/dist/consul-client/types.js +2 -0
  45. package/dist/consul-client/types.js.map +1 -0
  46. package/dist/cyclic-register.d.ts +3 -0
  47. package/dist/cyclic-register.d.ts.map +1 -0
  48. package/dist/cyclic-register.js +75 -0
  49. package/dist/cyclic-register.js.map +1 -0
  50. package/dist/get-api.d.ts +5 -0
  51. package/dist/get-api.d.ts.map +1 -0
  52. package/dist/get-api.js +51 -0
  53. package/dist/get-api.js.map +1 -0
  54. package/dist/get-register-config.d.ts +4 -0
  55. package/dist/get-register-config.d.ts.map +1 -0
  56. package/dist/get-register-config.js +94 -0
  57. package/dist/get-register-config.js.map +1 -0
  58. package/dist/index.d.ts +12 -0
  59. package/dist/index.d.ts.map +1 -0
  60. package/dist/index.js +11 -0
  61. package/dist/index.js.map +1 -0
  62. package/dist/interfaces.d.ts +251 -0
  63. package/dist/interfaces.d.ts.map +1 -0
  64. package/dist/interfaces.js +2 -0
  65. package/dist/interfaces.js.map +1 -0
  66. package/dist/lib/color.d.ts +40 -0
  67. package/dist/lib/color.d.ts.map +1 -0
  68. package/dist/lib/color.js +41 -0
  69. package/dist/lib/color.js.map +1 -0
  70. package/dist/lib/curl-text.d.ts +3 -0
  71. package/dist/lib/curl-text.d.ts.map +1 -0
  72. package/dist/lib/curl-text.js +73 -0
  73. package/dist/lib/curl-text.js.map +1 -0
  74. package/dist/lib/fqdn.d.ts +3 -0
  75. package/dist/lib/fqdn.d.ts.map +1 -0
  76. package/dist/lib/fqdn.js +40 -0
  77. package/dist/lib/fqdn.js.map +1 -0
  78. package/dist/lib/hash.d.ts +3 -0
  79. package/dist/lib/hash.d.ts.map +1 -0
  80. package/dist/lib/hash.js +64 -0
  81. package/dist/lib/hash.js.map +1 -0
  82. package/dist/lib/http-request-text.d.ts +4 -0
  83. package/dist/lib/http-request-text.d.ts.map +1 -0
  84. package/dist/lib/http-request-text.js +21 -0
  85. package/dist/lib/http-request-text.js.map +1 -0
  86. package/dist/lib/logger-stub.d.ts +9 -0
  87. package/dist/lib/logger-stub.d.ts.map +1 -0
  88. package/dist/lib/logger-stub.js +10 -0
  89. package/dist/lib/logger-stub.js.map +1 -0
  90. package/dist/lib/utils.d.ts +17 -0
  91. package/dist/lib/utils.d.ts.map +1 -0
  92. package/dist/lib/utils.js +164 -0
  93. package/dist/lib/utils.js.map +1 -0
  94. package/dist/prepare-consul-api.d.ts +4 -0
  95. package/dist/prepare-consul-api.d.ts.map +1 -0
  96. package/dist/prepare-consul-api.js +380 -0
  97. package/dist/prepare-consul-api.js.map +1 -0
  98. package/dist/src/get-register-config.d.ts +4 -0
  99. package/dist/src/get-register-config.d.ts.map +1 -0
  100. package/dist/src/get-register-config.js +94 -0
  101. package/dist/src/get-register-config.js.map +1 -0
  102. package/package.json +65 -0
  103. package/src/access-points/access-points-updater.ts +154 -0
  104. package/src/access-points/access-points-utils.ts +31 -0
  105. package/src/access-points/access-points.ts +185 -0
  106. package/src/constants.ts +7 -0
  107. package/src/consul-client/endpoints/agent.ts +91 -0
  108. package/src/consul-client/endpoints/catalog.ts +13 -0
  109. package/src/consul-client/endpoints/health.ts +31 -0
  110. package/src/consul-client/http-client.ts +166 -0
  111. package/src/consul-client/index.ts +31 -0
  112. package/src/consul-client/types.ts +134 -0
  113. package/src/cyclic-register.ts +94 -0
  114. package/src/get-api.ts +62 -0
  115. package/src/get-register-config.ts +102 -0
  116. package/src/index.ts +58 -0
  117. package/src/interfaces.ts +276 -0
  118. package/src/lib/color.ts +43 -0
  119. package/src/lib/curl-text.ts +91 -0
  120. package/src/lib/fqdn.ts +45 -0
  121. package/src/lib/hash.ts +56 -0
  122. package/src/lib/http-request-text.ts +24 -0
  123. package/src/lib/logger-stub.ts +11 -0
  124. package/src/lib/utils.ts +174 -0
  125. package/src/prepare-consul-api.ts +426 -0
@@ -0,0 +1,276 @@
1
+ import EventEmitter from 'events';
2
+
3
+ import { AccessPoints } from './access-points/access-points';
4
+ import { RegisterCheck, ConsulClientOptions } from './consul-client';
5
+
6
+ export type Maybe<T> = T | undefined;
7
+ export type Nullable<T> = T | null;
8
+ export type TBooleanLike = 'true' | 'false' | 'yes' | 'no' | '1' | '0' | 1 | 0;
9
+
10
+ export interface ISocketInfo {
11
+ host: string;
12
+ port: string | number;
13
+ }
14
+
15
+ export interface IRegisterCheck extends RegisterCheck {
16
+ name?: string;
17
+ tcp?: string;
18
+ dockercontainerid?: string;
19
+ shell?: string;
20
+ timeout?: string;
21
+ deregistercriticalserviceafter?: string;
22
+ }
23
+
24
+ export interface IRegisterConfig {
25
+ id: string;
26
+ name: string;
27
+ tags?: string[];
28
+ address?: string;
29
+ port?: number;
30
+ meta?: Record<string, string>;
31
+ check?: IRegisterCheck;
32
+ checks?: IRegisterCheck[];
33
+ connect?: any;
34
+ proxy?: any;
35
+ taggedAddresses?: any;
36
+ }
37
+
38
+ export type TRegisterType = 'if-not-registered' | 'if-config-differ' | 'force';
39
+
40
+ export interface IRegisterOptions {
41
+ registerType?: TRegisterType,
42
+ deleteOtherInstance?: boolean,
43
+ noAlreadyRegisteredMessage?: boolean,
44
+ }
45
+
46
+ export type TRegisterResult = 'already' | 'just' | false;
47
+
48
+ export interface IConsulAgentOptions extends ConsulClientOptions {
49
+ host: string;
50
+ port: string;
51
+ dc?: string | undefined;
52
+ defaults?: { token?: string | undefined } | undefined;
53
+ }
54
+
55
+ export interface IFullConsulAgentOptions {
56
+ reg: IConsulAgentOptions,
57
+ dev: IConsulAgentOptions,
58
+ prd: IConsulAgentOptions
59
+ }
60
+
61
+ export type TLoggerMethod = (...args: unknown[]) => any;
62
+
63
+ export interface ILogger {
64
+ silly: TLoggerMethod;
65
+ debug: TLoggerMethod;
66
+ info: TLoggerMethod;
67
+ warn: TLoggerMethod;
68
+ error: TLoggerMethod;
69
+ }
70
+
71
+ export interface IMeta {
72
+ [prop: string]: Nullable<string | number | boolean>,
73
+ }
74
+
75
+ export interface IAccessPoint {
76
+ consulServiceName: string,
77
+ id?: string,
78
+ title?: string,
79
+ port?: number | null,
80
+ host?: string | null,
81
+ setProps?: (data: Record<string, any> | null) => IAccessPoint | undefined,
82
+ isAP?: true,
83
+ meta?: IMeta | undefined,
84
+ isReachable?: boolean,
85
+ lastSuccessUpdate?: number,
86
+ idHostPortUpdated?: boolean,
87
+ getChanges?: () => [string, any, any][] | undefined,
88
+ updateIntervalIfSuccessMillis?: number,
89
+ noConsul?: boolean,
90
+ retrieveProps?: (host: string, meta?: any) => { host: string; port: number | null | undefined },
91
+
92
+ [propName: string]: any
93
+ }
94
+
95
+ export interface IAccessPointsMethods {
96
+ addAP?: (apKey: string, apData: any) => IAccessPoint | undefined,
97
+ setAP?: (apKey: string, apData: Record<string, any> | null) => IAccessPoint | undefined,
98
+ getAP?: (accessPointKey: string, andNotIsAP?: boolean) => IAccessPoint | undefined,
99
+ get?: (accessPointKey?: string, andNotIsAP?: boolean) => { [apKey: string]: IAccessPoint } | IAccessPoint | undefined,
100
+ }
101
+
102
+ export type IAccessPoints = {
103
+ [apKey: string]: IAccessPoint,
104
+ } & IAccessPointsMethods
105
+
106
+ export interface IConsulAgentConfig {
107
+ host?: string, // || FQDN || env.HOST_HOSTNAME || config.consul.service?.host || '127.0.0.1'
108
+ port?: string, // || 8500
109
+ secure?: string | TBooleanLike | boolean,
110
+ token?: string,
111
+ dc?: string;
112
+ }
113
+
114
+ export interface IFullConsulAgentConfig {
115
+ reg: IConsulAgentConfig,
116
+ dev?: IConsulAgentConfig,
117
+ prd?: IConsulAgentConfig
118
+ }
119
+
120
+ export interface IAFConsulConfig {
121
+ agent: IFullConsulAgentConfig,
122
+ check?: IRegisterCheck,
123
+ service: {
124
+ id?: string,
125
+ name: string,
126
+ instance: string,
127
+ version: string,
128
+ description: string,
129
+ tags?: string | string[],
130
+ meta?: string | IMeta,
131
+ host?: Nullable<string>,
132
+ port?: Nullable<string | number>
133
+ /**
134
+ * if false - disable the registration of the service with the consul
135
+ * Does not affect anything directly in this package and should be used where the package is imported
136
+ */
137
+ enable?: boolean,
138
+ /**
139
+ * @deprecated use `enable` instead
140
+ * if true - disable the registration of the service with the consul.
141
+ * For backward compatibility, has an increased priority of enable.
142
+ *
143
+ * Does not affect anything directly in this package and should be used where the package is imported
144
+ */
145
+ noRegOnStart?: boolean,
146
+ },
147
+ }
148
+
149
+ export interface IAFConfig {
150
+ accessPoints?: IAccessPoints | AccessPoints,
151
+ consul: IAFConsulConfig,
152
+ webServer: any,
153
+ service?: {
154
+ id?: string,
155
+ address?: string,
156
+ fromService?: string,
157
+ };
158
+ }
159
+
160
+ export type TCommonFnResult = any;
161
+
162
+ type TMethod<T> = (...args: any[]) => T;
163
+
164
+ export interface ICLOptions {
165
+ config: IAFConfig,
166
+ logger?: ILogger,
167
+ em?: EventEmitter,
168
+
169
+ envCode?: string,
170
+ getConsulUIAddress?: TMethod<string>,
171
+ hash?: string,
172
+ }
173
+
174
+ export interface IConsulServiceInfo {
175
+ ID: string,
176
+ Service: string,
177
+ Tags?: string[],
178
+ Meta?: IMeta,
179
+ Port: number,
180
+ Address: string,
181
+ Weights?: { Passing: number, Warning: number },
182
+ EnableTagOverride?: boolean,
183
+ Datacenter?: string,
184
+
185
+ // Service attributes
186
+ Proxy?: object, // { MeshGateway: {}, Expose: {} }
187
+ Connect?: object, // { MeshGateway: {}, Expose: {} }
188
+ CreateIndex?: number,
189
+ ModifyIndex?: number,
190
+
191
+ [prop: string]: any,
192
+ }
193
+
194
+ export interface IConsulNodeInfo {
195
+ ID: string,
196
+ Node?: string,
197
+ Address: string,
198
+ Datacenter?: string,
199
+ TaggedAddresses?: object, // { lan: <ip>, lan_ipv4: <ip>, wan: <ip>, wan_ipv4: <ip> }
200
+ Meta?: IMeta,
201
+ CreateIndex?: number,
202
+ ModifyIndex?: number,
203
+ }
204
+
205
+ export interface IConsulHealthServiceInfo {
206
+ Node?: IConsulNodeInfo,
207
+ Service?: IConsulServiceInfo,
208
+ Checks?: any[]
209
+ }
210
+
211
+ export interface IAPIArgs {
212
+ agentOptions?: IConsulAgentOptions | undefined,
213
+ options?: any;
214
+ withError?: boolean;
215
+ result?: any;
216
+ agentType?: keyof IFullConsulAgentConfig | undefined;
217
+ }
218
+
219
+ export interface IConsulAPI {
220
+ agentServiceList: (apiArgs?: IAPIArgs) => Promise<{ [serviceName: string]: IConsulServiceInfo }>,
221
+
222
+ catalogServiceList (dc: string, apiArgs?: IAPIArgs): Promise<{ [serviceId: string]: string[] }>,
223
+
224
+ consulHealthService: (apiArgs: IAPIArgs) => Promise<IConsulHealthServiceInfo[]>,
225
+ getServiceInfo: (serviceName: string) => Promise<IConsulServiceInfo | undefined>,
226
+ getServiceSocket: (serviceName: string, defaults: ISocketInfo) => Promise<ISocketInfo>,
227
+ agentServiceRegister: (options: IRegisterConfig, withError?: boolean) => Promise<boolean>,
228
+ agentServiceDeregister: (serviceId: string, apiArgs?: IAPIArgs) => Promise<boolean>,
229
+ deregisterIfNeed: (serviceId: string, agentOptions?: IConsulAgentOptions) => Promise<boolean>,
230
+ agentMembers: (apiArgs?: IAPIArgs) => Promise<TCommonFnResult>,
231
+ checkIfServiceRegistered: (serviceIdOrName: string, apiArgs?: IAPIArgs) => Promise<IConsulHealthServiceInfo | undefined>,
232
+ registerService: (registerConfig: IRegisterConfig, registerOptions: IRegisterOptions) => Promise<TRegisterResult>,
233
+
234
+ agentOptions: IFullConsulAgentOptions,
235
+ getConsulAgentOptions: (clOptions: ICLOptions) => Promise<IFullConsulAgentOptions>,
236
+ }
237
+
238
+ export interface ICyclicStartArgs {
239
+ cLOptions?: ICLOptions,
240
+ registerInterval?: number,
241
+ registerType?: TRegisterType,
242
+ deleteOtherInstance?: boolean,
243
+ noAlreadyRegisteredMessage?: boolean,
244
+ }
245
+
246
+ export interface IRegisterCyclic {
247
+ isStarted: boolean,
248
+ skipNextRegisterAttemptUntil: number,
249
+ healthCheckIntervalMillis: number,
250
+ registerIntervalMillis: number,
251
+ options: ICLOptions,
252
+ _timerId: ReturnType<typeof setTimeout>,
253
+ _logger: ILogger,
254
+
255
+ start: (cyclicStartArgs?: ICyclicStartArgs) => Promise<-1 | 0 | 1>
256
+ stop: () => void
257
+ }
258
+
259
+ export interface IAFConsulAPI extends IConsulAPI {
260
+ registerConfig: IRegisterConfig,
261
+ getConsulUIAddress: TMethod<string>,
262
+ serviceId: string,
263
+ register: {
264
+ once: (registerType?: TRegisterType) => Promise<TRegisterResult>,
265
+ cyclic: IRegisterCyclic,
266
+ }
267
+ deregister: (svcId?: string, agentHost?: string, agentPort?: string) => Promise<boolean>
268
+ consulUI?: string,
269
+ }
270
+
271
+ export interface ICache<T> {
272
+ [hash: string]: {
273
+ created: number,
274
+ value: T
275
+ }
276
+ }
@@ -0,0 +1,43 @@
1
+ // noinspection JSUnusedGlobalSymbols
2
+
3
+ export const reset = '\x1b[0m';
4
+ export const rs = reset;
5
+ export const clear = '\x1b[K';
6
+ export const bold = '\x1b[1m';
7
+ export const boldOff = '\x1b[21m';
8
+ export const italics = '\x1b[3m';
9
+ export const italicsOff = '\x1b[23m';
10
+ export const def = '\x1b[39m';
11
+
12
+ export const white = '\x1b[37m';
13
+ export const w = white;
14
+ export const black = '\x1b[30m';
15
+ export const bk = black;
16
+ export const red = '\x1b[31m';
17
+ export const r = red;
18
+ export const green = '\x1b[32m';
19
+ export const g = green;
20
+ export const yellow = '\x1b[33m';
21
+ export const y = yellow;
22
+ export const blue = '\x1b[34m';
23
+ export const b = blue;
24
+ export const magenta = '\x1b[35m';
25
+ export const m = magenta;
26
+ export const cyan = '\x1b[36m';
27
+ export const c = cyan;
28
+
29
+ export const lWhite = '\x1b[97m';
30
+ export const lw = lWhite;
31
+ export const lGray = '\x1b[90m';
32
+ export const lRed = '\x1b[91m';
33
+ export const lr = lRed;
34
+ export const lGreen = '\x1b[92m';
35
+ export const lg = lGreen;
36
+ export const lYellow = '\x1b[93m';
37
+ export const ly = lYellow;
38
+ export const lBlue = '\x1b[94m';
39
+ export const lb = lBlue;
40
+ export const lMagenta = '\x1b[95m';
41
+ export const lm = lMagenta;
42
+ export const lCyan = '\x1b[96m';
43
+ export const lc = lCyan;
@@ -0,0 +1,91 @@
1
+ import { RequestInfo } from '../consul-client';
2
+
3
+ class ToCurl {
4
+ private readonly request: {
5
+ method: string;
6
+ headers: Record<string, string>;
7
+ url: string;
8
+ params?: Record<string, string | boolean | undefined> | undefined;
9
+ data?: unknown;
10
+ };
11
+
12
+ constructor (req: RequestInfo) {
13
+ let data: unknown;
14
+ if (req.body) {
15
+ try {
16
+ data = JSON.parse(req.body);
17
+ } catch {
18
+ data = req.body;
19
+ }
20
+ }
21
+ this.request = {
22
+ method: req.method,
23
+ headers: req.headers,
24
+ url: req.url.split('?')[0] || req.url,
25
+ ...(req.query ? { params: req.query } : {}),
26
+ ...(data !== undefined ? { data } : {}),
27
+ };
28
+ }
29
+
30
+ getHeaders () {
31
+ const { headers } = this.request;
32
+ let curlHeaders = '';
33
+
34
+ Object.keys(headers).forEach((property) => {
35
+ if ({}.hasOwnProperty.call(headers, property)) {
36
+ if (property !== 'content-length' && property !== 'Content-Length') {
37
+ curlHeaders += ` -H "${property}:${headers[property]}"`;
38
+ }
39
+ }
40
+ });
41
+
42
+ return curlHeaders.trim();
43
+ }
44
+
45
+ getMethod () {
46
+ return `-X ${this.request.method.toUpperCase()}`;
47
+ }
48
+
49
+ getBody () {
50
+ const r = this.request;
51
+ const { data } = r;
52
+ if (
53
+ typeof data !== 'undefined'
54
+ && data !== ''
55
+ && data !== null
56
+ && r.method.toUpperCase() !== 'GET'
57
+ ) {
58
+ const d = typeof data === 'object' || Object.prototype.toString.call(data) === '[object Array]'
59
+ ? JSON.stringify(data, undefined, 2)
60
+ : data;
61
+ return `--data '${d}'`;
62
+ }
63
+ return '';
64
+ }
65
+
66
+ getUrl () {
67
+ return this.request.url;
68
+ }
69
+
70
+ getQueryString () {
71
+ const { params } = this.request;
72
+ if (!params) {return '';}
73
+
74
+ return Object.keys(params)
75
+ .filter((param) => params[param] !== undefined && params[param] !== '')
76
+ .reduce((qs, param) => `${qs}${qs ? '&' : '?'}${param}=${params[param]}`, '');
77
+ }
78
+
79
+ getBuiltURL () {
80
+ return (this.getUrl() + this.getQueryString()).trim();
81
+ }
82
+
83
+ getCURL () {
84
+ return `curl -i ${this.getMethod()} "${this.getBuiltURL()}" ${this.getHeaders()} ${this.getBody()}`;
85
+ }
86
+ }
87
+
88
+ export const getCurlText = (req: RequestInfo) => {
89
+ const instance = new ToCurl(req);
90
+ return instance.getCURL();
91
+ };
@@ -0,0 +1,45 @@
1
+ import * as dns from 'dns';
2
+ import * as os from 'os';
3
+
4
+ import { ICache } from '../interfaces';
5
+
6
+ import { minimizeCache } from './utils';
7
+
8
+ // Returns fully qualified domain name
9
+ export const getFQDN = (h?: string, withError?: boolean, onlyDomain?: boolean): Promise<string | null> => {
10
+ h = h || os.hostname();
11
+ return new Promise((resolve, reject) => {
12
+ dns.lookup(h as string, { hints: dns.ADDRCONFIG }, (err: any, ip: string) => {
13
+ if (err) {
14
+ return withError ? reject(err) : resolve(null);
15
+ }
16
+ dns.lookupService(ip, 0, (err2, hostname) => {
17
+ if (err2) {
18
+ return withError ? reject(err2) : resolve(null);
19
+ }
20
+ if (onlyDomain && !/\.[a-z]+$/i.test(hostname)) {
21
+ resolve(null);
22
+ return;
23
+ }
24
+ resolve(hostname);
25
+ });
26
+ });
27
+ });
28
+ };
29
+
30
+ const fqdnCache: ICache<string> = {};
31
+
32
+ export const getFQDNCached = async (...args: any[]): Promise<string | null> => {
33
+ const hostNameOrIP = args[0] || os.hostname() || '-';
34
+ minimizeCache(fqdnCache, 10);
35
+ if (!fqdnCache[hostNameOrIP]) {
36
+ const fqdn = await getFQDN(...args);
37
+ if (fqdn) {
38
+ fqdnCache[hostNameOrIP] = {
39
+ created: Date.now(),
40
+ value: fqdn,
41
+ };
42
+ }
43
+ }
44
+ return fqdnCache[hostNameOrIP]?.value || null;
45
+ };
@@ -0,0 +1,56 @@
1
+ import { pick } from 'af-tools-ts';
2
+ import * as XXH from 'xxhashjs';
3
+
4
+ import { IAFConfig, ICLOptions } from '../interfaces';
5
+
6
+ import { isObject } from './utils';
7
+
8
+ const getHash = (data: any, base: '32' | '64' = '32', seed: number = 0xCAFEBABE) => {
9
+ let stringToHash = '';
10
+ if (data === undefined) {
11
+ stringToHash = '#thisisundefined#';
12
+ } else if (data === null) {
13
+ stringToHash = '#thisisnull#';
14
+ } else if (data === '') {
15
+ stringToHash = '#thisisemptystring#';
16
+ } else if (Array.isArray(data)) {
17
+ const arr = data.map((value) => ([value, getHash(String(value) + (typeof value), '32', seed)]));
18
+ arr.sort(([, a], [, b]) => {
19
+ if (a < b) {return -1;}
20
+ if (a > b) {return 1;}
21
+ return 0;
22
+ });
23
+ stringToHash = JSON.stringify(arr.map(([v]) => v));
24
+ } else if (isObject(data)) {
25
+ const op = Object.prototype.toString.call(data); // === '[object Date]'
26
+ switch (op) {
27
+ case '[object Function]':
28
+ case '[object Date]':
29
+ stringToHash += data.toString();
30
+ break;
31
+ // case '[object Object]':
32
+ default: {
33
+ const keys = Object.keys(data).sort();
34
+ keys.forEach((key) => {
35
+ stringToHash += key + getHash(data[key], base, seed);
36
+ });
37
+ }
38
+ }
39
+ } else if (typeof data === 'string') {
40
+ stringToHash = data;
41
+ } else if (typeof data === 'number') {
42
+ stringToHash = `i${String(data)}`;
43
+ } else if (typeof data === 'boolean') {
44
+ stringToHash = `b${data ? 1 : 0}`;
45
+ } else if (typeof data === 'function') {
46
+ stringToHash = `f${data.toString()}`;
47
+ }
48
+ return XXH[`h${base}`](stringToHash, seed).toString(16);
49
+ };
50
+
51
+ export const getConfigHash = (options: ICLOptions): string => {
52
+ const opt = pick(options.config, ['consul', 'webServer']) as IAFConfig;
53
+ const hash = getHash(opt);
54
+ options.hash = hash;
55
+ return hash;
56
+ };
@@ -0,0 +1,24 @@
1
+ import { RequestInfo } from '../consul-client';
2
+
3
+ const getHttpRequestText = (request: RequestInfo) => {
4
+ const { hostname, port, path, method, headers, body } = request;
5
+ let res = `###\n${method} ${request.protocol}//${hostname}${port ? `:${port}` : ''}${path}`;
6
+
7
+ Object.entries(headers)
8
+ .forEach(([headerName, value]) => {
9
+ if (headerName !== 'content-length' && headerName !== 'Content-Length') {
10
+ res += `\n${headerName}: ${value}`;
11
+ }
12
+ });
13
+
14
+ if ((method === 'POST' || method === 'PUT') && body) {
15
+ try {
16
+ res += `\n\n${JSON.stringify(JSON.parse(body), undefined, 2)}`;
17
+ } catch {
18
+ res += `\n\n${body}`;
19
+ }
20
+ }
21
+ return res;
22
+ };
23
+
24
+ export default getHttpRequestText;
@@ -0,0 +1,11 @@
1
+ /* eslint-disable no-console */
2
+
3
+ const loggerStub = {
4
+ silly: console.log,
5
+ debug: console.log,
6
+ info: console.log,
7
+ warn: console.log,
8
+ error: console.log,
9
+ };
10
+
11
+ export default loggerStub;