@chainlink/external-adapter-framework 0.0.12 → 0.0.15

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 (220) hide show
  1. package/.eslintignore +1 -0
  2. package/.github/README.MD +35 -10
  3. package/.github/workflows/label.yaml +39 -0
  4. package/.github/workflows/publish.yaml +0 -3
  5. package/dist/examples/coingecko/test/e2e/adapter.test.ts.js +82953 -0
  6. package/dist/examples/coingecko/test/integration/adapter.test.ts.js +91672 -0
  7. package/dist/main.js +72703 -0
  8. package/jest.config.js +5 -0
  9. package/package.json +14 -3
  10. package/src/config/provider-limits.ts +14 -17
  11. package/src/examples/coingecko/src/config/index.ts +12 -0
  12. package/src/examples/coingecko/src/config/overrides.json +10826 -0
  13. package/src/examples/coingecko/src/cryptoUtils.ts +88 -0
  14. package/src/examples/coingecko/src/endpoint/coins.ts +54 -0
  15. package/src/examples/coingecko/src/endpoint/crypto-marketcap.ts +66 -0
  16. package/src/examples/coingecko/src/endpoint/crypto-volume.ts +66 -0
  17. package/src/examples/coingecko/src/endpoint/crypto.ts +63 -0
  18. package/src/examples/coingecko/src/endpoint/dominance.ts +40 -0
  19. package/src/examples/coingecko/src/endpoint/global-marketcap.ts +40 -0
  20. package/src/examples/coingecko/src/endpoint/index.ts +6 -0
  21. package/src/examples/coingecko/src/globalUtils.ts +78 -0
  22. package/src/examples/coingecko/src/index.ts +17 -0
  23. package/src/examples/coingecko/test/e2e/adapter.test.ts +278 -0
  24. package/src/examples/coingecko/test/integration/__snapshots__/adapter.test.ts.snap +15 -0
  25. package/src/examples/coingecko/test/integration/adapter.test.ts +281 -0
  26. package/src/examples/coingecko/test/integration/capturedRequests.json +1 -0
  27. package/src/examples/coingecko/test/integration/fixtures.ts +42 -0
  28. package/src/examples/{coingecko → coingecko-old}/batch-warming.ts +1 -0
  29. package/src/examples/{coingecko → coingecko-old}/index.ts +0 -0
  30. package/src/examples/{coingecko → coingecko-old}/rest.ts +0 -0
  31. package/src/transports/batch-warming.ts +7 -2
  32. package/src/transports/rest.ts +5 -1
  33. package/src/util/index.ts +1 -0
  34. package/src/util/recordRequests.ts +47 -0
  35. package/src/validation/overrideFunctions.ts +44 -0
  36. package/test/metrics/warmer-metrics.test.ts +1 -0
  37. package/test/{smoke.test.ts → smoke/smoke.test.ts} +13 -13
  38. package/{test-payload-fail.json → test/smoke/test-payload-fail.json} +0 -0
  39. package/{test-payload.js → test/smoke/test-payload.js} +0 -0
  40. package/{test-payload.json → test/smoke/test-payload.json} +0 -0
  41. package/test/transports/batch.test.ts +1 -0
  42. package/tsconfig.json +4 -1
  43. package/webpack.config.js +39 -5
  44. package/yarn-error.log +3778 -0
  45. package/dist/src/adapter.d.ts +0 -135
  46. package/dist/src/adapter.js +0 -145
  47. package/dist/src/background-executor.d.ts +0 -11
  48. package/dist/src/background-executor.js +0 -45
  49. package/dist/src/cache/factory.d.ts +0 -6
  50. package/dist/src/cache/factory.js +0 -55
  51. package/dist/src/cache/index.d.ts +0 -94
  52. package/dist/src/cache/index.js +0 -173
  53. package/dist/src/cache/local.d.ts +0 -23
  54. package/dist/src/cache/local.js +0 -83
  55. package/dist/src/cache/metrics.d.ts +0 -27
  56. package/dist/src/cache/metrics.js +0 -120
  57. package/dist/src/cache/redis.d.ts +0 -16
  58. package/dist/src/cache/redis.js +0 -100
  59. package/dist/src/chainlink-external-adapter-framework-0.0.6.tgz +0 -0
  60. package/dist/src/config/index.d.ts +0 -214
  61. package/dist/src/config/index.js +0 -380
  62. package/dist/src/config/provider-limits.d.ts +0 -31
  63. package/dist/src/config/provider-limits.js +0 -75
  64. package/dist/src/examples/bank-frick/accounts.d.ts +0 -39
  65. package/dist/src/examples/bank-frick/accounts.js +0 -192
  66. package/dist/src/examples/bank-frick/config/index.d.ts +0 -4
  67. package/dist/src/examples/bank-frick/config/index.js +0 -54
  68. package/dist/src/examples/bank-frick/index.d.ts +0 -2
  69. package/dist/src/examples/bank-frick/index.js +0 -15
  70. package/dist/src/examples/bank-frick/util.d.ts +0 -4
  71. package/dist/src/examples/bank-frick/util.js +0 -39
  72. package/dist/src/examples/coingecko/batch-warming.d.ts +0 -7
  73. package/dist/src/examples/coingecko/batch-warming.js +0 -53
  74. package/dist/src/examples/coingecko/index.d.ts +0 -2
  75. package/dist/src/examples/coingecko/index.js +0 -11
  76. package/dist/src/examples/coingecko/rest.d.ts +0 -12
  77. package/dist/src/examples/coingecko/rest.js +0 -51
  78. package/dist/src/examples/ncfx/config/index.d.ts +0 -12
  79. package/dist/src/examples/ncfx/config/index.js +0 -15
  80. package/dist/src/examples/ncfx/index.d.ts +0 -13
  81. package/dist/src/examples/ncfx/index.js +0 -11
  82. package/dist/src/examples/ncfx/websocket.d.ts +0 -47
  83. package/dist/src/examples/ncfx/websocket.js +0 -73
  84. package/dist/src/index.d.ts +0 -11
  85. package/dist/src/index.js +0 -128
  86. package/dist/src/metrics/constants.d.ts +0 -16
  87. package/dist/src/metrics/constants.js +0 -25
  88. package/dist/src/metrics/index.d.ts +0 -15
  89. package/dist/src/metrics/index.js +0 -122
  90. package/dist/src/metrics/util.d.ts +0 -7
  91. package/dist/src/metrics/util.js +0 -9
  92. package/dist/src/package/adapter.d.ts +0 -88
  93. package/dist/src/package/adapter.js +0 -112
  94. package/dist/src/package/background-executor.d.ts +0 -11
  95. package/dist/src/package/background-executor.js +0 -45
  96. package/dist/src/package/cache/factory.d.ts +0 -6
  97. package/dist/src/package/cache/factory.js +0 -57
  98. package/dist/src/package/cache/index.d.ts +0 -90
  99. package/dist/src/package/cache/index.js +0 -169
  100. package/dist/src/package/cache/local.d.ts +0 -23
  101. package/dist/src/package/cache/local.js +0 -83
  102. package/dist/src/package/cache/metrics.d.ts +0 -27
  103. package/dist/src/package/cache/metrics.js +0 -120
  104. package/dist/src/package/cache/redis.d.ts +0 -16
  105. package/dist/src/package/cache/redis.js +0 -100
  106. package/dist/src/package/config/index.d.ts +0 -195
  107. package/dist/src/package/config/index.js +0 -365
  108. package/dist/src/package/config/provider-limits.d.ts +0 -31
  109. package/dist/src/package/config/provider-limits.js +0 -76
  110. package/dist/src/package/examples/coingecko/batch-warming.d.ts +0 -2
  111. package/dist/src/package/examples/coingecko/batch-warming.js +0 -52
  112. package/dist/src/package/examples/coingecko/index.d.ts +0 -2
  113. package/dist/src/package/examples/coingecko/index.js +0 -10
  114. package/dist/src/package/examples/coingecko/rest.d.ts +0 -2
  115. package/dist/src/package/examples/coingecko/rest.js +0 -50
  116. package/dist/src/package/examples/ncfx/config/index.d.ts +0 -12
  117. package/dist/src/package/examples/ncfx/config/index.js +0 -15
  118. package/dist/src/package/examples/ncfx/index.d.ts +0 -2
  119. package/dist/src/package/examples/ncfx/index.js +0 -10
  120. package/dist/src/package/examples/ncfx/websocket.d.ts +0 -36
  121. package/dist/src/package/examples/ncfx/websocket.js +0 -72
  122. package/dist/src/package/index.d.ts +0 -12
  123. package/dist/src/package/index.js +0 -92
  124. package/dist/src/package/metrics/constants.d.ts +0 -16
  125. package/dist/src/package/metrics/constants.js +0 -25
  126. package/dist/src/package/metrics/index.d.ts +0 -15
  127. package/dist/src/package/metrics/index.js +0 -123
  128. package/dist/src/package/metrics/util.d.ts +0 -3
  129. package/dist/src/package/metrics/util.js +0 -9
  130. package/dist/src/package/package.json +0 -72
  131. package/dist/src/package/rate-limiting/background/fixed-frequency.d.ts +0 -10
  132. package/dist/src/package/rate-limiting/background/fixed-frequency.js +0 -37
  133. package/dist/src/package/rate-limiting/index.d.ts +0 -54
  134. package/dist/src/package/rate-limiting/index.js +0 -63
  135. package/dist/src/package/rate-limiting/metrics.d.ts +0 -3
  136. package/dist/src/package/rate-limiting/metrics.js +0 -44
  137. package/dist/src/package/rate-limiting/request/simple-counting.d.ts +0 -20
  138. package/dist/src/package/rate-limiting/request/simple-counting.js +0 -62
  139. package/dist/src/package/test.d.ts +0 -1
  140. package/dist/src/package/test.js +0 -6
  141. package/dist/src/package/transports/batch-warming.d.ts +0 -34
  142. package/dist/src/package/transports/batch-warming.js +0 -101
  143. package/dist/src/package/transports/index.d.ts +0 -87
  144. package/dist/src/package/transports/index.js +0 -87
  145. package/dist/src/package/transports/metrics.d.ts +0 -21
  146. package/dist/src/package/transports/metrics.js +0 -105
  147. package/dist/src/package/transports/rest.d.ts +0 -43
  148. package/dist/src/package/transports/rest.js +0 -129
  149. package/dist/src/package/transports/util.d.ts +0 -8
  150. package/dist/src/package/transports/util.js +0 -85
  151. package/dist/src/package/transports/websocket.d.ts +0 -80
  152. package/dist/src/package/transports/websocket.js +0 -169
  153. package/dist/src/package/util/expiring-sorted-set.d.ts +0 -21
  154. package/dist/src/package/util/expiring-sorted-set.js +0 -47
  155. package/dist/src/package/util/index.d.ts +0 -11
  156. package/dist/src/package/util/index.js +0 -35
  157. package/dist/src/package/util/logger.d.ts +0 -42
  158. package/dist/src/package/util/logger.js +0 -62
  159. package/dist/src/package/util/request.d.ts +0 -55
  160. package/dist/src/package/util/request.js +0 -2
  161. package/dist/src/package/validation/error.d.ts +0 -50
  162. package/dist/src/package/validation/error.js +0 -79
  163. package/dist/src/package/validation/index.d.ts +0 -5
  164. package/dist/src/package/validation/index.js +0 -86
  165. package/dist/src/package/validation/input-params.d.ts +0 -15
  166. package/dist/src/package/validation/input-params.js +0 -30
  167. package/dist/src/package/validation/override-functions.d.ts +0 -3
  168. package/dist/src/package/validation/override-functions.js +0 -40
  169. package/dist/src/package/validation/preset-tokens.json +0 -23
  170. package/dist/src/package/validation/validator.d.ts +0 -47
  171. package/dist/src/package/validation/validator.js +0 -303
  172. package/dist/src/package.json +0 -72
  173. package/dist/src/rate-limiting/background/fixed-frequency.d.ts +0 -11
  174. package/dist/src/rate-limiting/background/fixed-frequency.js +0 -35
  175. package/dist/src/rate-limiting/index.d.ts +0 -55
  176. package/dist/src/rate-limiting/index.js +0 -63
  177. package/dist/src/rate-limiting/metrics.d.ts +0 -3
  178. package/dist/src/rate-limiting/metrics.js +0 -44
  179. package/dist/src/rate-limiting/request/simple-counting.d.ts +0 -21
  180. package/dist/src/rate-limiting/request/simple-counting.js +0 -62
  181. package/dist/src/test.d.ts +0 -1
  182. package/dist/src/test.js +0 -6
  183. package/dist/src/transports/batch-warming.d.ts +0 -35
  184. package/dist/src/transports/batch-warming.js +0 -101
  185. package/dist/src/transports/index.d.ts +0 -70
  186. package/dist/src/transports/index.js +0 -87
  187. package/dist/src/transports/metrics.d.ts +0 -22
  188. package/dist/src/transports/metrics.js +0 -105
  189. package/dist/src/transports/rest.d.ts +0 -44
  190. package/dist/src/transports/rest.js +0 -131
  191. package/dist/src/transports/util.d.ts +0 -8
  192. package/dist/src/transports/util.js +0 -85
  193. package/dist/src/transports/websocket.d.ts +0 -80
  194. package/dist/src/transports/websocket.js +0 -166
  195. package/dist/src/util/expiring-sorted-set.d.ts +0 -21
  196. package/dist/src/util/expiring-sorted-set.js +0 -47
  197. package/dist/src/util/index.d.ts +0 -12
  198. package/dist/src/util/index.js +0 -35
  199. package/dist/src/util/logger.d.ts +0 -42
  200. package/dist/src/util/logger.js +0 -62
  201. package/dist/src/util/request.d.ts +0 -57
  202. package/dist/src/util/request.js +0 -2
  203. package/dist/src/util/subscription-set/expiring-sorted-set.d.ts +0 -22
  204. package/dist/src/util/subscription-set/expiring-sorted-set.js +0 -47
  205. package/dist/src/util/subscription-set/subscription-set.d.ts +0 -18
  206. package/dist/src/util/subscription-set/subscription-set.js +0 -19
  207. package/dist/src/util/test-payload-loader.d.ts +0 -25
  208. package/dist/src/util/test-payload-loader.js +0 -83
  209. package/dist/src/validation/error.d.ts +0 -50
  210. package/dist/src/validation/error.js +0 -79
  211. package/dist/src/validation/index.d.ts +0 -5
  212. package/dist/src/validation/index.js +0 -91
  213. package/dist/src/validation/input-params.d.ts +0 -15
  214. package/dist/src/validation/input-params.js +0 -30
  215. package/dist/src/validation/override-functions.d.ts +0 -3
  216. package/dist/src/validation/override-functions.js +0 -40
  217. package/dist/src/validation/preset-tokens.json +0 -23
  218. package/dist/src/validation/validator.d.ts +0 -47
  219. package/dist/src/validation/validator.js +0 -303
  220. package/src/test.ts +0 -5
@@ -1,135 +0,0 @@
1
- import { Cache } from './cache';
2
- import { AdapterConfig, BaseAdapterConfig, CustomSettingsType, SettingsMap } from './config';
3
- import { AdapterRateLimitTier, BackgroundExecuteRateLimiter, RequestRateLimiter } from './rate-limiting';
4
- import { Transport } from './transports';
5
- import { SubscriptionSetFactory } from './util';
6
- import { InputParameters } from './validation/input-params';
7
- /**
8
- * Dependencies that will be injected into the Adapter on startup
9
- */
10
- export interface AdapterDependencies {
11
- cache: Cache;
12
- requestRateLimiter: RequestRateLimiter;
13
- backgroundExecuteRateLimiter: BackgroundExecuteRateLimiter;
14
- subscriptionSetFactory: SubscriptionSetFactory;
15
- }
16
- /**
17
- * Context that will be used on background executions of a Transport.
18
- * For example, the endpointName used to log statements or generate Cache keys.
19
- */
20
- export interface AdapterContext<CustomSettings extends CustomSettingsType<CustomSettings> = SettingsMap> {
21
- adapterEndpoint: AdapterEndpoint<unknown, unknown, CustomSettings>;
22
- adapterConfig: AdapterConfig<CustomSettings>;
23
- }
24
- /**
25
- * Structure to describe rate limits specs for the Adapter
26
- */
27
- interface AdapterRateLimitingConfig {
28
- /** Adapter rate limits, gotten from the specific tier requested */
29
- tiers: Record<string, AdapterRateLimitTier>;
30
- }
31
- /**
32
- * Main structure of an External Adapter
33
- */
34
- export interface AdapterParams<CustomSettings extends SettingsMap> {
35
- /** Name of the adapter */
36
- name: string;
37
- /** If present, the string that will be used for requests with no specified endpoint */
38
- defaultEndpoint?: string;
39
- /**
40
- * List of [[AdapterEndpoint]]s in the adapter. This is purposefully typed any; it's the correct type in this case.
41
- *
42
- * When you try to create an adapter and you provide an endpoint, if these generics were set to "unknown" instead
43
- * what would happen is that Typescript would check if the types match, and would fail to assign unknown to the
44
- * specific Params or Result in the transport itself.
45
- * We also can't use generics, because if we had more than one transport with different requests (very likely)
46
- * then those new types wouldn't match with each other.
47
- */
48
- endpoints: AdapterEndpoint<any, any, CustomSettings>[];
49
- /** Map of overrides to the default config values for an Adapter */
50
- envDefaultOverrides?: Partial<BaseAdapterConfig>;
51
- /** List of custom env vars for this particular adapter (e.g. RPC_URL) */
52
- customSettings?: SettingsMap;
53
- /** Configuration relevant to outbound (EA --\> DP) communication rate limiting */
54
- rateLimiting?: AdapterRateLimitingConfig;
55
- /** Overrides for converting the 'base' parameter that are hardcoded into the adapter. */
56
- overrides?: Record<string, string>;
57
- }
58
- /**
59
- * Structure to describe rate limits specs for a specific adapter endpoint
60
- */
61
- export interface EndpointRateLimitingConfig {
62
- /**
63
- * How much of the total limit for the adapter will be assigned to this specific endpoint.
64
- * Should be a non-zero positive number up to 100.
65
- * Endpoints in the same adapter without a specific allocation will divide the remaining limits equally.
66
- */
67
- allocationPercentage: number;
68
- }
69
- /**
70
- * Structure to describe a specific endpoint in an [[Adapter]]
71
- */
72
- export interface AdapterEndpointParams<Params, Result, CustomSettings extends SettingsMap> {
73
- /** Name that will be used to match input params to this endpoint (case insensitive) */
74
- name: string;
75
- /** List of alternative endpoint names that will resolve to this same transport (case insensitive) */
76
- aliases?: string[];
77
- /** Transport that will be used to handle data processing and communication for this endpoint */
78
- transport: Transport<Params, Result, CustomSettings>;
79
- /** Specification of what the body of a request hitting this endpoint should look like (used for validation) */
80
- inputParameters: InputParameters;
81
- /** Specific details related to the rate limiting for this endpoint in particular */
82
- rateLimiting?: EndpointRateLimitingConfig;
83
- }
84
- export declare class AdapterEndpoint<Params, Result, CustomSettings extends SettingsMap> implements AdapterEndpointParams<Params, Result, CustomSettings> {
85
- name: string;
86
- aliases?: string[] | undefined;
87
- transport: Transport<Params, Result, CustomSettings>;
88
- inputParameters: InputParameters;
89
- rateLimiting?: EndpointRateLimitingConfig | undefined;
90
- constructor(params: AdapterEndpointParams<Params, Result, CustomSettings>);
91
- }
92
- export declare class Adapter<CustomSettings extends SettingsMap = SettingsMap> implements AdapterParams<CustomSettings> {
93
- name: string;
94
- defaultEndpoint?: string | undefined;
95
- endpoints: AdapterEndpoint<unknown, unknown, CustomSettings>[];
96
- envDefaultOverrides?: Partial<BaseAdapterConfig> | undefined;
97
- customSettings?: SettingsMap | undefined;
98
- rateLimiting?: AdapterRateLimitingConfig | undefined;
99
- overrides?: Record<string, string> | undefined;
100
- initialized: boolean;
101
- /** Object containing alias translations for all endpoints */
102
- endpointsMap: Record<string, AdapterEndpoint<unknown, unknown, CustomSettings>>;
103
- /** Initialized dependencies that the adapter will use */
104
- dependencies: AdapterDependencies;
105
- /** Configuration params for various adapter properties */
106
- config: AdapterConfig;
107
- constructor(params: AdapterParams<CustomSettings>);
108
- /**
109
- * Initializes all of the [[Transport]]s in the adapter, passing along any [[AdapterDependencies]] and [[AdapterConfig]].
110
- * Additionally, it builds a map out of all the endpoint names and aliases (checking for duplicates).
111
- */
112
- initialize(dependencies?: Partial<AdapterDependencies>): Promise<void>;
113
- /**
114
- * Takes an adapter and normalizes all endpoint names and aliases, as well as the default endpoint.
115
- * i.e. makes them lowercase for now
116
- * @param adapter - an instance of an Adapter
117
- */
118
- private normalizeEndpointNames;
119
- /**
120
- * This function will take an adapter structure and go through each endpoint, calculating
121
- * each one's allocation of the total rate limits that are set for the adapter as a whole.
122
- *
123
- * @param adapter - the adapter to initialize rate limiting for
124
- */
125
- private calculateRateLimitAllocations;
126
- /**
127
- * This function will process dependencies for an adapter, such as caches or rate limiters,
128
- * in order to inject them into transports and other relevant places later in the lifecycle.
129
- *
130
- * @param inputDependencies - a partial obj of initialized dependencies to override the created ones
131
- * @returns a set of AdapterDependencies all initialized
132
- */
133
- initializeDependencies(inputDependencies?: Partial<AdapterDependencies>): AdapterDependencies;
134
- }
135
- export {};
@@ -1,145 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Adapter = exports.AdapterEndpoint = void 0;
4
- const cache_1 = require("./cache");
5
- const config_1 = require("./config");
6
- const rate_limiting_1 = require("./rate-limiting");
7
- const util_1 = require("./util");
8
- const logger = (0, util_1.makeLogger)('Adapter');
9
- class AdapterEndpoint {
10
- constructor(params) {
11
- this.name = params.name;
12
- this.aliases = params.aliases;
13
- this.transport = params.transport;
14
- this.inputParameters = params.inputParameters;
15
- this.rateLimiting = params.rateLimiting;
16
- }
17
- }
18
- exports.AdapterEndpoint = AdapterEndpoint;
19
- // /**
20
- // * Structure to describe an adapter that has been initialized
21
- // */
22
- // export interface InitializedAdapter<Params, Result, CustomSettings extends SettingsMap>
23
- // extends AdapterParams<Params, Result, CustomSettings> {
24
- // /** Object containing alias translations for all endpoints */
25
- // endpointsMap: Record<string, AdapterEndpoint<Params, Result, CustomSettings>>
26
- // /** Initialized dependencies that the adapter will use */
27
- // dependencies: AdapterDependencies
28
- // /** Configuration params for various adapter properties */
29
- // config: AdapterConfig
30
- // }
31
- class Adapter {
32
- constructor(params) {
33
- // After initialization
34
- this.initialized = false;
35
- /** Object containing alias translations for all endpoints */
36
- this.endpointsMap = {};
37
- // Copy over params
38
- this.name = params.name;
39
- this.defaultEndpoint = params.defaultEndpoint?.toLowerCase();
40
- this.endpoints = params.endpoints;
41
- this.envDefaultOverrides = params.envDefaultOverrides;
42
- this.customSettings = params.customSettings;
43
- this.rateLimiting = params.rateLimiting;
44
- this.overrides = params.overrides;
45
- this.config = (0, config_1.buildAdapterConfig)({
46
- overrides: this.envDefaultOverrides,
47
- customSettings: this.customSettings,
48
- });
49
- this.normalizeEndpointNames();
50
- this.calculateRateLimitAllocations();
51
- }
52
- /**
53
- * Initializes all of the [[Transport]]s in the adapter, passing along any [[AdapterDependencies]] and [[AdapterConfig]].
54
- * Additionally, it builds a map out of all the endpoint names and aliases (checking for duplicates).
55
- */
56
- async initialize(dependencies) {
57
- if (this.initialized) {
58
- throw new Error('This adapter has already been initialized!');
59
- }
60
- this.dependencies = this.initializeDependencies(dependencies);
61
- for (const endpoint of this.endpoints) {
62
- // Add aliases to map to use in validation
63
- const aliases = [endpoint.name, ...(endpoint.aliases || [])];
64
- for (const alias of aliases) {
65
- if (this.endpointsMap[alias]) {
66
- throw new Error(`Duplicate endpoint / alias: "${alias}"`);
67
- }
68
- this.endpointsMap[alias] = endpoint;
69
- }
70
- logger.debug(`Initializing transport for endpoint "${endpoint.name}"...`);
71
- await endpoint.transport.initialize(this.dependencies);
72
- }
73
- logger.debug('Adapter initialization complete.');
74
- this.initialized = true;
75
- }
76
- /**
77
- * Takes an adapter and normalizes all endpoint names and aliases, as well as the default endpoint.
78
- * i.e. makes them lowercase for now
79
- * @param adapter - an instance of an Adapter
80
- */
81
- normalizeEndpointNames() {
82
- for (const endpoint of this.endpoints) {
83
- endpoint.name = endpoint.name.toLowerCase();
84
- endpoint.aliases = endpoint.aliases?.map((a) => a.toLowerCase());
85
- }
86
- }
87
- /**
88
- * This function will take an adapter structure and go through each endpoint, calculating
89
- * each one's allocation of the total rate limits that are set for the adapter as a whole.
90
- *
91
- * @param adapter - the adapter to initialize rate limiting for
92
- */
93
- calculateRateLimitAllocations() {
94
- const numberOfEndpoints = this.endpoints.length;
95
- const endpointsWithExplicitAllocations = this.endpoints.filter((e) => e.rateLimiting);
96
- const totalExplicitAllocation = endpointsWithExplicitAllocations
97
- .map((e) => e.rateLimiting?.allocationPercentage || 0)
98
- .reduce((sum, next) => sum + next, 0);
99
- if (totalExplicitAllocation > 100) {
100
- throw new Error('The total allocation set for all endpoints summed cannot exceed 100%');
101
- }
102
- if (totalExplicitAllocation === 100 &&
103
- numberOfEndpoints - endpointsWithExplicitAllocations.length > 0) {
104
- throw new Error('The explicit allocation is at 100% but there are endpoints with implicit allocation');
105
- }
106
- const implicitAllocation = 100 - totalExplicitAllocation;
107
- logger.debug('Adapter rate limit allocations:');
108
- for (const endpoint of this.endpoints) {
109
- if (!endpoint.rateLimiting) {
110
- endpoint.rateLimiting = {
111
- allocationPercentage: implicitAllocation / (numberOfEndpoints - endpointsWithExplicitAllocations.length),
112
- };
113
- }
114
- logger.debug(`Endpoint [${endpoint.name}] - ${endpoint.rateLimiting?.allocationPercentage}%`);
115
- }
116
- }
117
- /**
118
- * This function will process dependencies for an adapter, such as caches or rate limiters,
119
- * in order to inject them into transports and other relevant places later in the lifecycle.
120
- *
121
- * @param inputDependencies - a partial obj of initialized dependencies to override the created ones
122
- * @returns a set of AdapterDependencies all initialized
123
- */
124
- initializeDependencies(inputDependencies) {
125
- const dependencies = inputDependencies || {};
126
- if (!dependencies.cache) {
127
- dependencies.cache = cache_1.CacheFactory.buildCache(this.config);
128
- }
129
- // In the future we might want something more complex, but for now it's better to simplify
130
- // and just use the same rate limiting for everything. Once we have a more complex use case we
131
- // can think of ways to make this more configurable.
132
- const rateLimitingTier = (0, rate_limiting_1.getRateLimitingTier)(this.rateLimiting?.tiers, this.config.RATE_LIMIT_API_TIER);
133
- if (!dependencies.requestRateLimiter) {
134
- dependencies.requestRateLimiter = new rate_limiting_1.SimpleCountingRateLimiter().initialize(this.endpoints, rateLimitingTier);
135
- }
136
- if (!dependencies.backgroundExecuteRateLimiter) {
137
- dependencies.backgroundExecuteRateLimiter = new rate_limiting_1.FixedFrequencyRateLimiter().initialize(this.endpoints, rateLimitingTier);
138
- }
139
- if (!dependencies.subscriptionSetFactory) {
140
- dependencies.subscriptionSetFactory = new util_1.SubscriptionSetFactory(this.config);
141
- }
142
- return dependencies;
143
- }
144
- }
145
- exports.Adapter = Adapter;
@@ -1,11 +0,0 @@
1
- /// <reference types="node" />
2
- import { Server } from 'http';
3
- import { Adapter } from './adapter';
4
- /**
5
- * Very simple background loop that will call the [[Transport.backgroundExecute]] functions in all Transports.
6
- * It gets the time in ms to wait as the return value from those functions, and sleeps until next execution.
7
- *
8
- * @param adapter - an initialized External Adapter
9
- * @param server - the http server to attach an on close listener to
10
- */
11
- export declare function callBackgroundExecutes(adapter: Adapter, server?: Server): Promise<void>;
@@ -1,45 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.callBackgroundExecutes = void 0;
4
- const util_1 = require("./util");
5
- const logger = (0, util_1.makeLogger)('BackgroundExecutor');
6
- /**
7
- * Very simple background loop that will call the [[Transport.backgroundExecute]] functions in all Transports.
8
- * It gets the time in ms to wait as the return value from those functions, and sleeps until next execution.
9
- *
10
- * @param adapter - an initialized External Adapter
11
- * @param server - the http server to attach an on close listener to
12
- */
13
- async function callBackgroundExecutes(adapter, server) {
14
- // Set up variable to check later on to see if we need to stop this background "thread"
15
- // If no server is provided, the listener won't be set and serverClosed will always be false
16
- let serverClosed = false;
17
- server?.on('close', () => {
18
- serverClosed = true;
19
- });
20
- for (const endpoint of adapter.endpoints) {
21
- const backgroundExecute = endpoint.transport.backgroundExecute?.bind(endpoint.transport);
22
- if (!backgroundExecute) {
23
- logger.debug(`Endpoint "${endpoint.name}" has no background execute, skipping...`);
24
- continue;
25
- }
26
- const context = {
27
- adapterEndpoint: endpoint,
28
- adapterConfig: adapter.config,
29
- };
30
- const handler = async () => {
31
- if (serverClosed) {
32
- logger.info('Server closed, stopping recursive backgroundExecute handler chain');
33
- return;
34
- }
35
- logger.debug(`Calling background execute for endpoint "${endpoint.name}"`);
36
- const timeToWait = await backgroundExecute(context);
37
- logger.debug(`Finished background execute for endpoint "${endpoint.name}", sleeping for ${timeToWait}ms`);
38
- await (0, util_1.sleep)(timeToWait);
39
- handler();
40
- };
41
- // Start recursive async calls
42
- handler();
43
- }
44
- }
45
- exports.callBackgroundExecutes = callBackgroundExecutes;
@@ -1,6 +0,0 @@
1
- import { AdapterConfig } from '../config';
2
- import { LocalCache } from './local';
3
- import { RedisCache } from './redis';
4
- export declare class CacheFactory {
5
- static buildCache(config: AdapterConfig): LocalCache<unknown> | RedisCache<unknown> | undefined;
6
- }
@@ -1,55 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
- var __importDefault = (this && this.__importDefault) || function (mod) {
26
- return (mod && mod.__esModule) ? mod : { "default": mod };
27
- };
28
- Object.defineProperty(exports, "__esModule", { value: true });
29
- exports.CacheFactory = void 0;
30
- const ioredis_1 = __importDefault(require("ioredis"));
31
- const util_1 = require("../util");
32
- const local_1 = require("./local");
33
- const cacheMetrics = __importStar(require("./metrics"));
34
- const redis_1 = require("./redis");
35
- const logger = (0, util_1.makeLogger)('CacheFactory');
36
- class CacheFactory {
37
- static buildCache(config) {
38
- logger.info(`Using "${config.CACHE_TYPE}" cache.`);
39
- if (config.CACHE_TYPE === 'local') {
40
- return new local_1.LocalCache();
41
- }
42
- else if (config.CACHE_TYPE === 'redis') {
43
- const redis = new ioredis_1.default({
44
- enableAutoPipelining: true,
45
- host: config.CACHE_REDIS_HOST,
46
- port: config.CACHE_REDIS_PORT,
47
- });
48
- redis.on('connect', () => {
49
- cacheMetrics.redisConnectionsOpen.inc();
50
- });
51
- return new redis_1.RedisCache(redis);
52
- }
53
- }
54
- }
55
- exports.CacheFactory = CacheFactory;
@@ -1,94 +0,0 @@
1
- import { AdapterEndpoint } from '../adapter';
2
- import { AdapterConfig, SettingsMap } from '../config';
3
- import { AdapterMiddlewareBuilder, AdapterResponse, sleep } from '../util';
4
- export * from './local';
5
- export * from './redis';
6
- export * from './factory';
7
- /**
8
- * An object describing an entry in the cache.
9
- * @typeParam T - the type of the entry's value
10
- */
11
- export interface CacheEntry<T> {
12
- key: string;
13
- value: T;
14
- }
15
- /**
16
- * Generic interface for a local or remote Cache.
17
- * @typeParam T - the type of the cache entries' values
18
- */
19
- export interface Cache<T = unknown> {
20
- /**
21
- * Gets an item from the Cache.
22
- *
23
- * @param key - the key of the desired entry for which to fetch its value
24
- * @returns a Promise of the entry's value, or undefined if not found / expired.
25
- */
26
- get: (key: string) => Promise<T | undefined>;
27
- /**
28
- * Sets an item in the Cache.
29
- *
30
- * @param key - the key of the new entry
31
- * @param value - the value of the new entry
32
- * @param ttl - the time in milliseconds until the entry expires
33
- * @returns an empty Promise that resolves when the entry has been set
34
- */
35
- set: (key: string, value: T, ttl: number) => Promise<void>;
36
- /**
37
- * Sets a list of items in the Cache.
38
- *
39
- * @param entries - a list of cache entries
40
- * @param ttl - the time in milliseconds until the entries expire
41
- * @returns an empty Promise that resolves when all entries have been set
42
- */
43
- setMany: (entries: CacheEntry<T>[], ttl: number) => Promise<void>;
44
- /**
45
- * Deletes the specified item from the Cache
46
- *
47
- * @param key - the key of the entry to be deleted
48
- * @returns an empty Promise that resolves when the entry has been deleted
49
- */
50
- delete: (key: string) => Promise<void>;
51
- }
52
- export declare const calculateCacheKey: <Params, Result, CustomSettings extends SettingsMap>({ adapterEndpoint, adapterConfig, }: {
53
- adapterEndpoint: AdapterEndpoint<Params, Result, CustomSettings>;
54
- adapterConfig: AdapterConfig<CustomSettings>;
55
- }, data: unknown) => string;
56
- export declare const calculateFeedId: <Params, Result, CustomSettings extends SettingsMap>({ adapterEndpoint, adapterConfig, }: {
57
- adapterEndpoint: AdapterEndpoint<Params, Result, CustomSettings>;
58
- adapterConfig: AdapterConfig<CustomSettings>;
59
- }, data: unknown) => string;
60
- /**
61
- * Calculates a unique key from the provided data.
62
- *
63
- * @param data - the request data/body, i.e. the adapter input params
64
- * @param paramNames - the keys from adapter endpoint input parameters
65
- * @returns the calculated unique key
66
- *
67
- * @example
68
- * ```
69
- * calculateKey({ base: 'ETH', quote: 'BTC' }, ['base','quote'])
70
- * // equals `|base:eth|quote:btc`
71
- * ```
72
- */
73
- export declare const calculateKey: <CustomSettings extends SettingsMap>(data: unknown, paramNames: string[], adapterConfig: AdapterConfig<CustomSettings>) => string;
74
- /**
75
- * Polls the provided Cache for an AdapterResponse set in the provided key. If the maximum
76
- * amount of retries is exceeded, it returns undefined instead.
77
- *
78
- * @param cache - a Cache instance
79
- * @param key - the key generated from an AdapterRequest that corresponds to the desired AdapterResponse
80
- * @param retry - current retry, only for internal use
81
- * @returns the AdapterResponse if found, else undefined
82
- */
83
- export declare const pollResponseFromCache: (cache: Cache<AdapterResponse>, key: string, options: {
84
- maxRetries: number;
85
- sleep: number;
86
- }, retry?: number) => Promise<AdapterResponse | undefined>;
87
- /**
88
- * Given a Cache instance in the adapter dependencies, builds a middleware function that will perform
89
- * a get from said Cache and return that if found; otherwise it'll continue the middleware chain.
90
- *
91
- * @param adapter - an initialized adapter
92
- * @returns the cache middleware function
93
- */
94
- export declare const buildCacheMiddleware: AdapterMiddlewareBuilder;
@@ -1,173 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
26
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
27
- };
28
- Object.defineProperty(exports, "__esModule", { value: true });
29
- exports.buildCacheMiddleware = exports.pollResponseFromCache = exports.calculateKey = exports.calculateFeedId = exports.calculateCacheKey = void 0;
30
- const util_1 = require("../util");
31
- const cacheMetrics = __importStar(require("./metrics"));
32
- __exportStar(require("./local"), exports);
33
- __exportStar(require("./redis"), exports);
34
- __exportStar(require("./factory"), exports);
35
- const logger = (0, util_1.makeLogger)('Cache');
36
- // Uses calculateKey to generate a unique key from the endpoint name, data, and input parameters
37
- const calculateCacheKey = ({ adapterEndpoint, adapterConfig, }, data) => {
38
- const paramNames = Object.keys(adapterEndpoint.inputParameters);
39
- if (paramNames.length === 0) {
40
- logger.trace(`Using default cache key ${adapterConfig.DEFAULT_CACHE_KEY}`);
41
- return adapterConfig.DEFAULT_CACHE_KEY;
42
- }
43
- return `${adapterEndpoint.name}-${(0, exports.calculateKey)(data, paramNames, adapterConfig)}`;
44
- };
45
- exports.calculateCacheKey = calculateCacheKey;
46
- const calculateFeedId = ({ adapterEndpoint, adapterConfig, }, data) => {
47
- const paramNames = Object.keys(adapterEndpoint.inputParameters);
48
- if (paramNames.length === 0) {
49
- logger.trace(`Cannot generate Feed ID without input parameters`);
50
- return 'N/A';
51
- }
52
- return (0, exports.calculateKey)(data, paramNames, adapterConfig);
53
- };
54
- exports.calculateFeedId = calculateFeedId;
55
- /**
56
- * Calculates a unique key from the provided data.
57
- *
58
- * @param data - the request data/body, i.e. the adapter input params
59
- * @param paramNames - the keys from adapter endpoint input parameters
60
- * @returns the calculated unique key
61
- *
62
- * @example
63
- * ```
64
- * calculateKey({ base: 'ETH', quote: 'BTC' }, ['base','quote'])
65
- * // equals `|base:eth|quote:btc`
66
- * ```
67
- */
68
- const calculateKey = (data, paramNames, adapterConfig) => {
69
- if (data && typeof data !== 'object') {
70
- throw new Error('Data to calculate cache key should be an object');
71
- }
72
- const params = data;
73
- let cacheKey = '';
74
- for (const paramName of paramNames) {
75
- // Ignore overrides param when generating cache keys
76
- if (paramName === 'overrides') {
77
- continue;
78
- }
79
- const param = params[paramName];
80
- if (param === undefined) {
81
- continue;
82
- }
83
- cacheKey += `|${paramName}:`;
84
- switch (typeof param) {
85
- case 'string':
86
- cacheKey += param.toLowerCase();
87
- break;
88
- case 'number':
89
- case 'boolean':
90
- cacheKey += param.toString();
91
- break;
92
- case 'object':
93
- // Force cache keys to only use performant properties of the input params.
94
- // If the object were to be used, we'd have to sort its properties.
95
- logger.debug(`Property "${paramName}" in request parameters is of type object, and won't be used in the cacheKey`);
96
- }
97
- }
98
- if (cacheKey.length > adapterConfig.MAX_COMMON_KEY_SIZE) {
99
- logger.warn(`Generated cache key for adapter request is bigger than the MAX_COMMON_KEY_SIZE and will be truncated`);
100
- cacheKey = cacheKey.slice(0, adapterConfig.MAX_COMMON_KEY_SIZE);
101
- }
102
- logger.trace(`Generated cache key for request: "${cacheKey}"`);
103
- return cacheKey;
104
- };
105
- exports.calculateKey = calculateKey;
106
- // Calculate the amount of time the non-expired entry has been in the cache
107
- const calculateStaleness = (expirationTimestamp, ttl) => {
108
- if (expirationTimestamp) {
109
- const createTimestamp = expirationTimestamp - ttl;
110
- return (Date.now() - createTimestamp) / 1000;
111
- }
112
- else {
113
- // If expirationTimestamp is not available, staleness cannot be calculated
114
- // Defaults to ttl for metrics purposes
115
- return ttl;
116
- }
117
- };
118
- /**
119
- * Polls the provided Cache for an AdapterResponse set in the provided key. If the maximum
120
- * amount of retries is exceeded, it returns undefined instead.
121
- *
122
- * @param cache - a Cache instance
123
- * @param key - the key generated from an AdapterRequest that corresponds to the desired AdapterResponse
124
- * @param retry - current retry, only for internal use
125
- * @returns the AdapterResponse if found, else undefined
126
- */
127
- const pollResponseFromCache = async (cache, key, options, retry = 0) => {
128
- if (retry > options.maxRetries) {
129
- // Ideally this shouldn't happen often (p99 of reqs should be found in the cache)
130
- logger.info('Exceeded max cache polling retries');
131
- return undefined;
132
- }
133
- logger.trace('Getting response from cache...');
134
- const response = await cache.get(key);
135
- if (response) {
136
- logger.trace('Got response from cache');
137
- return response;
138
- }
139
- if (options.maxRetries === 0) {
140
- logger.debug(`Response not found, retries disabled`);
141
- return undefined;
142
- }
143
- logger.debug(`Response not found, sleeping ${options.sleep} milliseconds...`);
144
- await (0, util_1.sleep)(options.sleep);
145
- return (0, exports.pollResponseFromCache)(cache, key, options, retry + 1);
146
- };
147
- exports.pollResponseFromCache = pollResponseFromCache;
148
- /**
149
- * Given a Cache instance in the adapter dependencies, builds a middleware function that will perform
150
- * a get from said Cache and return that if found; otherwise it'll continue the middleware chain.
151
- *
152
- * @param adapter - an initialized adapter
153
- * @returns the cache middleware function
154
- */
155
- const buildCacheMiddleware = (adapter) => async (req, res) => {
156
- const response = await adapter.dependencies.cache.get(req.requestContext.cacheKey);
157
- if (response) {
158
- logger.debug('Found response from cache, sending that');
159
- if (adapter.config.METRICS_ENABLED && adapter.config.EXPERIMENTAL_METRICS_ENABLED) {
160
- const label = cacheMetrics.cacheMetricsLabel(req.requestContext.cacheKey, req.requestContext.meta?.metrics?.feedId || 'N/A', adapter.config.CACHE_TYPE);
161
- // Record cache staleness and cache get count and value
162
- const staleness = calculateStaleness(response.maxAge, adapter.config.CACHE_MAX_AGE);
163
- cacheMetrics.cacheGet(label, response.result, staleness);
164
- req.requestContext.meta = {
165
- ...req.requestContext.meta,
166
- metrics: { ...req.requestContext.meta?.metrics, cacheHit: true },
167
- };
168
- }
169
- return res.send(response);
170
- }
171
- logger.debug('Did not find response in cache, moving to next middleware');
172
- };
173
- exports.buildCacheMiddleware = buildCacheMiddleware;