@chainlink/external-adapter-framework 0.0.10 → 0.0.12

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 (269) hide show
  1. package/.c8rc.json +3 -0
  2. package/.eslintignore +9 -0
  3. package/.eslintrc.js +96 -0
  4. package/.github/README.MD +17 -0
  5. package/.github/actions/setup/action.yaml +13 -0
  6. package/.github/workflows/main.yaml +39 -0
  7. package/.github/workflows/publish.yaml +20 -0
  8. package/.prettierignore +13 -0
  9. package/.yarnrc +0 -0
  10. package/README.md +103 -0
  11. package/dist/src/adapter.d.ts +135 -0
  12. package/dist/src/adapter.js +145 -0
  13. package/dist/src/background-executor.d.ts +11 -0
  14. package/{background-executor.js → dist/src/background-executor.js} +0 -0
  15. package/{cache → dist/src/cache}/factory.d.ts +0 -0
  16. package/dist/src/cache/factory.js +55 -0
  17. package/dist/src/cache/index.d.ts +94 -0
  18. package/dist/src/cache/index.js +173 -0
  19. package/{cache → dist/src/cache}/local.d.ts +0 -0
  20. package/{cache → dist/src/cache}/local.js +0 -0
  21. package/{cache → dist/src/cache}/metrics.d.ts +0 -0
  22. package/{cache → dist/src/cache}/metrics.js +0 -0
  23. package/{cache → dist/src/cache}/redis.d.ts +0 -0
  24. package/dist/src/cache/redis.js +100 -0
  25. package/dist/src/chainlink-external-adapter-framework-0.0.6.tgz +0 -0
  26. package/dist/src/config/index.d.ts +214 -0
  27. package/dist/src/config/index.js +380 -0
  28. package/{config → dist/src/config}/provider-limits.d.ts +0 -0
  29. package/{config → dist/src/config}/provider-limits.js +1 -1
  30. package/dist/src/examples/bank-frick/accounts.d.ts +39 -0
  31. package/dist/src/examples/bank-frick/accounts.js +192 -0
  32. package/dist/src/examples/bank-frick/config/index.d.ts +4 -0
  33. package/dist/src/examples/bank-frick/config/index.js +54 -0
  34. package/dist/src/examples/bank-frick/index.d.ts +2 -0
  35. package/dist/src/examples/bank-frick/index.js +15 -0
  36. package/dist/src/examples/bank-frick/util.d.ts +4 -0
  37. package/dist/src/examples/bank-frick/util.js +39 -0
  38. package/dist/src/examples/coingecko/batch-warming.d.ts +7 -0
  39. package/dist/src/examples/coingecko/batch-warming.js +53 -0
  40. package/dist/src/examples/coingecko/index.d.ts +2 -0
  41. package/dist/src/examples/coingecko/index.js +11 -0
  42. package/dist/src/examples/coingecko/rest.d.ts +12 -0
  43. package/dist/src/examples/coingecko/rest.js +51 -0
  44. package/{examples → dist/src/examples}/ncfx/config/index.d.ts +0 -0
  45. package/{examples → dist/src/examples}/ncfx/config/index.js +0 -0
  46. package/dist/src/examples/ncfx/index.d.ts +13 -0
  47. package/dist/src/examples/ncfx/index.js +11 -0
  48. package/dist/src/examples/ncfx/websocket.d.ts +47 -0
  49. package/dist/src/examples/ncfx/websocket.js +73 -0
  50. package/dist/src/index.d.ts +11 -0
  51. package/dist/src/index.js +128 -0
  52. package/{metrics → dist/src/metrics}/constants.d.ts +0 -0
  53. package/{metrics → dist/src/metrics}/constants.js +0 -0
  54. package/{metrics → dist/src/metrics}/index.d.ts +0 -0
  55. package/dist/src/metrics/index.js +122 -0
  56. package/dist/src/metrics/util.d.ts +7 -0
  57. package/dist/src/metrics/util.js +9 -0
  58. package/{adapter.d.ts → dist/src/package/adapter.d.ts} +0 -0
  59. package/{adapter.js → dist/src/package/adapter.js} +0 -0
  60. package/{background-executor.d.ts → dist/src/package/background-executor.d.ts} +0 -0
  61. package/dist/src/package/background-executor.js +45 -0
  62. package/dist/src/package/cache/factory.d.ts +6 -0
  63. package/{cache → dist/src/package/cache}/factory.js +0 -0
  64. package/{cache → dist/src/package/cache}/index.d.ts +0 -0
  65. package/{cache → dist/src/package/cache}/index.js +0 -0
  66. package/dist/src/package/cache/local.d.ts +23 -0
  67. package/dist/src/package/cache/local.js +83 -0
  68. package/dist/src/package/cache/metrics.d.ts +27 -0
  69. package/dist/src/package/cache/metrics.js +120 -0
  70. package/dist/src/package/cache/redis.d.ts +16 -0
  71. package/{cache → dist/src/package/cache}/redis.js +0 -0
  72. package/{config → dist/src/package/config}/index.d.ts +0 -0
  73. package/{config → dist/src/package/config}/index.js +0 -0
  74. package/dist/src/package/config/provider-limits.d.ts +31 -0
  75. package/dist/src/package/config/provider-limits.js +76 -0
  76. package/{examples → dist/src/package/examples}/coingecko/batch-warming.d.ts +0 -0
  77. package/{examples → dist/src/package/examples}/coingecko/batch-warming.js +0 -0
  78. package/{examples → dist/src/package/examples}/coingecko/index.d.ts +0 -0
  79. package/{examples → dist/src/package/examples}/coingecko/index.js +0 -0
  80. package/{examples → dist/src/package/examples}/coingecko/rest.d.ts +0 -0
  81. package/{examples → dist/src/package/examples}/coingecko/rest.js +0 -0
  82. package/dist/src/package/examples/ncfx/config/index.d.ts +12 -0
  83. package/dist/src/package/examples/ncfx/config/index.js +15 -0
  84. package/{examples → dist/src/package/examples}/ncfx/index.d.ts +0 -0
  85. package/{examples → dist/src/package/examples}/ncfx/index.js +0 -0
  86. package/{examples → dist/src/package/examples}/ncfx/websocket.d.ts +0 -0
  87. package/{examples → dist/src/package/examples}/ncfx/websocket.js +0 -0
  88. package/{index.d.ts → dist/src/package/index.d.ts} +0 -0
  89. package/{index.js → dist/src/package/index.js} +0 -0
  90. package/dist/src/package/metrics/constants.d.ts +16 -0
  91. package/dist/src/package/metrics/constants.js +25 -0
  92. package/dist/src/package/metrics/index.d.ts +15 -0
  93. package/{metrics → dist/src/package/metrics}/index.js +0 -0
  94. package/{metrics → dist/src/package/metrics}/util.d.ts +0 -0
  95. package/{metrics → dist/src/package/metrics}/util.js +0 -0
  96. package/dist/src/package/package.json +72 -0
  97. package/{rate-limiting → dist/src/package/rate-limiting}/background/fixed-frequency.d.ts +0 -0
  98. package/{rate-limiting → dist/src/package/rate-limiting}/background/fixed-frequency.js +0 -0
  99. package/{rate-limiting → dist/src/package/rate-limiting}/index.d.ts +0 -0
  100. package/{rate-limiting → dist/src/package/rate-limiting}/index.js +0 -0
  101. package/{rate-limiting → dist/src/package/rate-limiting}/metrics.d.ts +0 -0
  102. package/{rate-limiting → dist/src/package/rate-limiting}/metrics.js +0 -0
  103. package/{rate-limiting → dist/src/package/rate-limiting}/request/simple-counting.d.ts +0 -0
  104. package/{rate-limiting → dist/src/package/rate-limiting}/request/simple-counting.js +0 -0
  105. package/{test.d.ts → dist/src/package/test.d.ts} +0 -0
  106. package/{test.js → dist/src/package/test.js} +0 -0
  107. package/{transports → dist/src/package/transports}/batch-warming.d.ts +0 -0
  108. package/{transports → dist/src/package/transports}/batch-warming.js +0 -0
  109. package/{transports → dist/src/package/transports}/index.d.ts +0 -0
  110. package/{transports → dist/src/package/transports}/index.js +0 -0
  111. package/{transports → dist/src/package/transports}/metrics.d.ts +0 -0
  112. package/{transports → dist/src/package/transports}/metrics.js +0 -0
  113. package/{transports → dist/src/package/transports}/rest.d.ts +0 -0
  114. package/{transports → dist/src/package/transports}/rest.js +0 -0
  115. package/{transports → dist/src/package/transports}/util.d.ts +0 -0
  116. package/{transports → dist/src/package/transports}/util.js +0 -0
  117. package/{transports → dist/src/package/transports}/websocket.d.ts +0 -0
  118. package/{transports → dist/src/package/transports}/websocket.js +0 -0
  119. package/{util → dist/src/package/util}/expiring-sorted-set.d.ts +0 -0
  120. package/{util → dist/src/package/util}/expiring-sorted-set.js +0 -0
  121. package/{util → dist/src/package/util}/index.d.ts +0 -0
  122. package/{util → dist/src/package/util}/index.js +0 -0
  123. package/{util → dist/src/package/util}/logger.d.ts +0 -0
  124. package/{util → dist/src/package/util}/logger.js +0 -0
  125. package/{util → dist/src/package/util}/request.d.ts +0 -0
  126. package/{util → dist/src/package/util}/request.js +0 -0
  127. package/{validation → dist/src/package/validation}/error.d.ts +0 -0
  128. package/{validation → dist/src/package/validation}/error.js +0 -0
  129. package/{validation → dist/src/package/validation}/index.d.ts +0 -0
  130. package/{validation → dist/src/package/validation}/index.js +0 -0
  131. package/{validation → dist/src/package/validation}/input-params.d.ts +0 -0
  132. package/{validation → dist/src/package/validation}/input-params.js +0 -0
  133. package/{validation → dist/src/package/validation}/override-functions.d.ts +0 -0
  134. package/{validation → dist/src/package/validation}/override-functions.js +0 -0
  135. package/{validation → dist/src/package/validation}/preset-tokens.json +0 -0
  136. package/{validation → dist/src/package/validation}/validator.d.ts +0 -0
  137. package/{validation → dist/src/package/validation}/validator.js +0 -0
  138. package/dist/src/package.json +72 -0
  139. package/dist/src/rate-limiting/background/fixed-frequency.d.ts +11 -0
  140. package/dist/src/rate-limiting/background/fixed-frequency.js +35 -0
  141. package/dist/src/rate-limiting/index.d.ts +55 -0
  142. package/dist/src/rate-limiting/index.js +63 -0
  143. package/dist/src/rate-limiting/metrics.d.ts +3 -0
  144. package/dist/src/rate-limiting/metrics.js +44 -0
  145. package/dist/src/rate-limiting/request/simple-counting.d.ts +21 -0
  146. package/dist/src/rate-limiting/request/simple-counting.js +62 -0
  147. package/dist/src/test.d.ts +1 -0
  148. package/dist/src/test.js +6 -0
  149. package/dist/src/transports/batch-warming.d.ts +35 -0
  150. package/dist/src/transports/batch-warming.js +101 -0
  151. package/dist/src/transports/index.d.ts +70 -0
  152. package/dist/src/transports/index.js +87 -0
  153. package/dist/src/transports/metrics.d.ts +22 -0
  154. package/dist/src/transports/metrics.js +105 -0
  155. package/dist/src/transports/rest.d.ts +44 -0
  156. package/dist/src/transports/rest.js +131 -0
  157. package/dist/src/transports/util.d.ts +8 -0
  158. package/dist/src/transports/util.js +85 -0
  159. package/dist/src/transports/websocket.d.ts +80 -0
  160. package/dist/src/transports/websocket.js +166 -0
  161. package/dist/src/util/expiring-sorted-set.d.ts +21 -0
  162. package/dist/src/util/expiring-sorted-set.js +47 -0
  163. package/dist/src/util/index.d.ts +12 -0
  164. package/dist/src/util/index.js +35 -0
  165. package/dist/src/util/logger.d.ts +42 -0
  166. package/dist/src/util/logger.js +62 -0
  167. package/dist/src/util/request.d.ts +57 -0
  168. package/dist/src/util/request.js +2 -0
  169. package/dist/src/util/subscription-set/expiring-sorted-set.d.ts +22 -0
  170. package/dist/src/util/subscription-set/expiring-sorted-set.js +47 -0
  171. package/dist/src/util/subscription-set/subscription-set.d.ts +18 -0
  172. package/dist/src/util/subscription-set/subscription-set.js +19 -0
  173. package/dist/src/util/test-payload-loader.d.ts +25 -0
  174. package/dist/src/util/test-payload-loader.js +83 -0
  175. package/dist/src/validation/error.d.ts +50 -0
  176. package/dist/src/validation/error.js +79 -0
  177. package/dist/src/validation/index.d.ts +5 -0
  178. package/dist/src/validation/index.js +91 -0
  179. package/dist/src/validation/input-params.d.ts +15 -0
  180. package/dist/src/validation/input-params.js +30 -0
  181. package/dist/src/validation/override-functions.d.ts +3 -0
  182. package/dist/src/validation/override-functions.js +40 -0
  183. package/dist/src/validation/preset-tokens.json +23 -0
  184. package/dist/src/validation/validator.d.ts +47 -0
  185. package/dist/src/validation/validator.js +303 -0
  186. package/docker-compose.yaml +35 -0
  187. package/env.sh +54 -0
  188. package/env2.sh +55 -0
  189. package/package.json +5 -3
  190. package/publish.sh +0 -0
  191. package/src/adapter.ts +263 -0
  192. package/src/background-executor.ts +52 -0
  193. package/src/cache/factory.ts +26 -0
  194. package/src/cache/index.ts +258 -0
  195. package/src/cache/local.ts +73 -0
  196. package/src/cache/metrics.ts +112 -0
  197. package/src/cache/redis.ts +93 -0
  198. package/src/config/index.ts +517 -0
  199. package/src/config/provider-limits.ts +130 -0
  200. package/src/examples/bank-frick/README.MD +10 -0
  201. package/src/examples/bank-frick/accounts.ts +246 -0
  202. package/src/examples/bank-frick/config/index.ts +53 -0
  203. package/src/examples/bank-frick/index.ts +13 -0
  204. package/src/examples/bank-frick/types.d.ts +38 -0
  205. package/src/examples/bank-frick/util.ts +55 -0
  206. package/src/examples/coingecko/batch-warming.ts +78 -0
  207. package/src/examples/coingecko/index.ts +9 -0
  208. package/src/examples/coingecko/rest.ts +77 -0
  209. package/src/examples/ncfx/config/index.ts +12 -0
  210. package/src/examples/ncfx/index.ts +9 -0
  211. package/src/examples/ncfx/websocket.ts +99 -0
  212. package/src/index.ts +149 -0
  213. package/src/metrics/constants.ts +23 -0
  214. package/src/metrics/index.ts +115 -0
  215. package/src/metrics/util.ts +18 -0
  216. package/src/rate-limiting/background/fixed-frequency.ts +45 -0
  217. package/src/rate-limiting/index.ts +100 -0
  218. package/src/rate-limiting/metrics.ts +18 -0
  219. package/src/rate-limiting/request/simple-counting.ts +76 -0
  220. package/src/test.ts +5 -0
  221. package/src/transports/batch-warming.ts +122 -0
  222. package/src/transports/index.ts +152 -0
  223. package/src/transports/metrics.ts +95 -0
  224. package/src/transports/rest.ts +164 -0
  225. package/src/transports/util.ts +63 -0
  226. package/src/transports/websocket.ts +245 -0
  227. package/src/util/index.ts +22 -0
  228. package/src/util/logger.ts +69 -0
  229. package/src/util/request.ts +117 -0
  230. package/src/util/subscription-set/expiring-sorted-set.ts +54 -0
  231. package/src/util/subscription-set/subscription-set.ts +35 -0
  232. package/src/util/test-payload-loader.ts +87 -0
  233. package/src/validation/error.ts +116 -0
  234. package/src/validation/index.ts +110 -0
  235. package/src/validation/input-params.ts +45 -0
  236. package/src/validation/override-functions.ts +44 -0
  237. package/src/validation/preset-tokens.json +23 -0
  238. package/src/validation/validator.ts +384 -0
  239. package/test/adapter.test.ts +27 -0
  240. package/test/background-executor.test.ts +108 -0
  241. package/test/cache/cache-key.test.ts +114 -0
  242. package/test/cache/helper.ts +100 -0
  243. package/test/cache/local.test.ts +54 -0
  244. package/test/cache/redis.test.ts +89 -0
  245. package/test/correlation.test.ts +114 -0
  246. package/test/index.test.ts +37 -0
  247. package/test/metrics/feed-id.test.ts +38 -0
  248. package/test/metrics/helper.ts +14 -0
  249. package/test/metrics/labels.test.ts +36 -0
  250. package/test/metrics/metrics.test.ts +267 -0
  251. package/test/metrics/redis-metrics.test.ts +113 -0
  252. package/test/metrics/warmer-metrics.test.ts +192 -0
  253. package/test/metrics/ws-metrics.test.ts +225 -0
  254. package/test/rate-limit-config.test.ts +242 -0
  255. package/test/smoke.test.ts +166 -0
  256. package/test/transports/batch.test.ts +465 -0
  257. package/test/transports/rest.test.ts +242 -0
  258. package/test/transports/websocket.test.ts +183 -0
  259. package/test/tsconfig.json +5 -0
  260. package/test/util.ts +77 -0
  261. package/test/validation.test.ts +178 -0
  262. package/test-payload-fail.json +3 -0
  263. package/test-payload.js +22 -0
  264. package/test-payload.json +7 -0
  265. package/test.sh +20 -0
  266. package/test2.sh +2 -0
  267. package/tsconfig.json +25 -0
  268. package/typedoc.json +6 -0
  269. package/webpack.config.js +23 -0
@@ -0,0 +1,55 @@
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;
@@ -0,0 +1,94 @@
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;
@@ -0,0 +1,173 @@
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;
File without changes
File without changes
File without changes
File without changes
File without changes
@@ -0,0 +1,100 @@
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
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.RedisCache = void 0;
27
+ const util_1 = require("../util");
28
+ const cacheMetrics = __importStar(require("./metrics"));
29
+ const logger = (0, util_1.makeLogger)('RedisCache');
30
+ /**
31
+ * Redis implementation of a Cache. It uses a simple js Object, storing entries with both
32
+ * a value and an expiration timestamp. Expired entries are deleted on reads (i.e. no background gc/upkeep).
33
+ *
34
+ * @typeParam T - the type for the entries' values
35
+ */
36
+ class RedisCache {
37
+ constructor(client) {
38
+ this.client = client;
39
+ }
40
+ async get(key) {
41
+ logger.trace(`Getting key ${key}`);
42
+ const value = await this.client.get(key);
43
+ // Record get command sent to Redis
44
+ cacheMetrics.redisCommandsSentCount
45
+ .labels({ status: cacheMetrics.CMD_SENT_STATUS.SUCCESS, function_name: 'get' })
46
+ .inc();
47
+ if (!value) {
48
+ logger.debug(`No entry in redis cache for key "${key}", returning undefined`);
49
+ return undefined;
50
+ }
51
+ return JSON.parse(value);
52
+ }
53
+ async delete(key) {
54
+ logger.trace(`Deleting key ${key}`);
55
+ await this.client.del(key);
56
+ // Record delete command sent to Redis
57
+ cacheMetrics.redisCommandsSentCount
58
+ .labels({ status: cacheMetrics.CMD_SENT_STATUS.SUCCESS, function_name: 'delete' })
59
+ .inc();
60
+ }
61
+ async set(key, value, ttl) {
62
+ logger.trace(`Setting key ${key}`);
63
+ await this.client.set(key, JSON.stringify(value), 'PX', ttl);
64
+ // Only record metrics if feed Id is present, otherwise assuming value is not adapter response to record
65
+ const feedId = value.meta?.metrics?.feedId;
66
+ if (feedId) {
67
+ // Record cache set count, max age, and staleness (set to 0 for cache set)
68
+ const label = cacheMetrics.cacheMetricsLabel(key, feedId, cacheMetrics.CacheTypes.Redis);
69
+ cacheMetrics.cacheSet(label, ttl);
70
+ }
71
+ // Record set command sent to Redis
72
+ cacheMetrics.redisCommandsSentCount
73
+ .labels({ status: cacheMetrics.CMD_SENT_STATUS.SUCCESS, function_name: 'set' })
74
+ .inc();
75
+ }
76
+ async setMany(entries, ttl) {
77
+ logger.trace(`Setting a bunch of keys`);
78
+ // Unfortunately, there's no ttl for mset
79
+ let chain = this.client.multi();
80
+ for (const entry of entries) {
81
+ chain = chain.set(entry.key, JSON.stringify(entry.value), 'PX', ttl);
82
+ }
83
+ await chain.exec();
84
+ // Loop again, but this time to record these in metrics
85
+ for (const entry of entries) {
86
+ // Only record metrics if feed Id is present, otherwise assuming value is not adapter response to record
87
+ const feedId = entry.value.meta?.metrics?.feedId;
88
+ if (feedId) {
89
+ // Record cache set count, max age, and staleness (set to 0 for cache set)
90
+ const label = cacheMetrics.cacheMetricsLabel(entry.key, feedId, cacheMetrics.CacheTypes.Redis);
91
+ cacheMetrics.cacheSet(label, ttl);
92
+ }
93
+ }
94
+ // Record setMany command sent to Redis
95
+ cacheMetrics.redisCommandsSentCount
96
+ .labels({ status: cacheMetrics.CMD_SENT_STATUS.SUCCESS, function_name: 'exec' })
97
+ .inc();
98
+ }
99
+ }
100
+ exports.RedisCache = RedisCache;
@@ -0,0 +1,214 @@
1
+ export declare const BaseSettings: {
2
+ readonly API_ENDPOINT: {
3
+ readonly description: "The URL that the certain transports use to retrieve data";
4
+ readonly type: "string";
5
+ };
6
+ readonly API_KEY: {
7
+ readonly description: "Default setting for an EA key";
8
+ readonly type: "string";
9
+ };
10
+ readonly BASE_URL: {
11
+ readonly description: "Starting path for the EA handler endpoint";
12
+ readonly type: "string";
13
+ readonly default: "/";
14
+ };
15
+ readonly CACHE_MAX_AGE: {
16
+ readonly description: "Maximum amount of time (in ms) that a response will stay cached";
17
+ readonly type: "number";
18
+ readonly default: 90000;
19
+ };
20
+ readonly CACHE_REDIS_HOST: {
21
+ readonly description: "Hostname for the Redis instance to be used";
22
+ readonly type: "string";
23
+ readonly default: "127.0.0.1";
24
+ };
25
+ readonly CACHE_REDIS_PORT: {
26
+ readonly description: "Port for the Redis instance to be used";
27
+ readonly type: "number";
28
+ readonly default: 6379;
29
+ };
30
+ readonly CACHE_TYPE: {
31
+ readonly description: "The type of cache to use throughout the EA";
32
+ readonly type: "enum";
33
+ readonly default: "local";
34
+ readonly options: readonly ["local", "redis"];
35
+ };
36
+ readonly CORRELATION_ID_ENABLED: {
37
+ readonly description: "Flag to enable correlation IDs for sent requests in logging";
38
+ readonly type: "boolean";
39
+ readonly default: true;
40
+ };
41
+ readonly EA_PORT: {
42
+ readonly description: "Port through which the EA will listen for REST requests (if mode is set to \"reader\" or \"reader-writer\")";
43
+ readonly type: "number";
44
+ readonly default: 8080;
45
+ };
46
+ readonly EXPERIMENTAL_METRICS_ENABLED: {
47
+ readonly description: "Flag to specify whether or not to collect metrics. Used as fallback for METRICS_ENABLED";
48
+ readonly type: "boolean";
49
+ readonly default: true;
50
+ };
51
+ readonly LOG_LEVEL: {
52
+ readonly description: "Minimum level required for logs to be output";
53
+ readonly type: "string";
54
+ readonly default: "info";
55
+ };
56
+ readonly METRICS_ENABLED: {
57
+ readonly description: "Flag to specify whether or not to startup the metrics server";
58
+ readonly type: "boolean";
59
+ readonly default: true;
60
+ };
61
+ readonly METRICS_NAME: {
62
+ readonly description: "Metrics name";
63
+ readonly type: "string";
64
+ };
65
+ readonly METRICS_PORT: {
66
+ readonly description: "Port metrics will be exposed to";
67
+ readonly type: "number";
68
+ readonly default: 9080;
69
+ };
70
+ readonly METRICS_USE_BASE_URL: {
71
+ readonly description: "Flag to specify whether or not to prepend the BASE_URL to the metrics endpoint";
72
+ readonly type: "boolean";
73
+ };
74
+ readonly RATE_LIMIT_API_TIER: {
75
+ readonly description: "Rate limiting tier to use from the available options for the adapter. If not present, the adapter will run using the first tier on the list.";
76
+ readonly type: "string";
77
+ };
78
+ readonly WARMUP_SUBSCRIPTION_TTL: {
79
+ readonly type: "number";
80
+ readonly description: "TTL for batch warmer subscriptions";
81
+ readonly default: 10000;
82
+ };
83
+ readonly WS_SUBSCRIPTION_TTL: {
84
+ readonly description: "";
85
+ readonly type: "number";
86
+ readonly default: 120000;
87
+ };
88
+ readonly CACHE_POLLING_MAX_RETRIES: {
89
+ readonly description: "Max amount of times to attempt to find EA response in the cache after the Transport has been set up";
90
+ readonly type: "number";
91
+ readonly default: 10;
92
+ };
93
+ readonly CACHE_POLLING_SLEEP_MS: {
94
+ readonly description: "The number of ms to sleep between each retry to fetch the EA response in the cache";
95
+ readonly type: "number";
96
+ readonly default: 200;
97
+ };
98
+ readonly DEFAULT_CACHE_KEY: {
99
+ readonly description: "Default key to be used when one cannot be determined from request parameters";
100
+ readonly type: "string";
101
+ readonly default: "DEFAULT_CACHE_KEY";
102
+ };
103
+ readonly EA_HOST: {
104
+ readonly description: "Host this EA will listen for REST requests on (if mode is set to \"reader\" or \"reader-writer\")";
105
+ readonly type: "string";
106
+ readonly default: "::";
107
+ };
108
+ readonly EA_MODE: {
109
+ readonly description: "Port this EA will listen for REST requests on (if mode is set to \"reader\" or \"reader-writer\")";
110
+ readonly type: "enum";
111
+ readonly default: "reader-writer";
112
+ readonly options: readonly ["reader", "writer", "reader-writer"];
113
+ };
114
+ readonly REST_TRANSPORT_MAX_RATE_LIMIT_RETRIES: {
115
+ readonly description: "Maximum amount of times the Rest Transport will attempt to set up a request when blocked by the rate limiter";
116
+ readonly type: "number";
117
+ readonly default: 3;
118
+ };
119
+ readonly REST_TRANSPORT_MS_BETWEEN_RATE_LIMIT_RETRIES: {
120
+ readonly description: "Time that the Rest Transport will wait between retries when blocked by the rate limiter";
121
+ readonly type: "number";
122
+ readonly default: 400;
123
+ };
124
+ readonly MAX_COMMON_KEY_SIZE: {
125
+ readonly description: "Maximum amount of characters that the common part of the cache key or feed ID can have";
126
+ readonly type: "number";
127
+ readonly default: 300;
128
+ readonly validate: (value?: number) => "MAX_COMMON_KEY_SIZE must be a number between 150 and 500" | undefined;
129
+ };
130
+ readonly SMOKE_TEST_PAYLOAD_FILE_NAME: {
131
+ readonly description: "Name of the test payload file used for the smoke endpoint";
132
+ readonly type: "string";
133
+ };
134
+ };
135
+ export declare const buildAdapterConfig: <CustomSettings extends CustomSettingsType<CustomSettings> = EmptySettings>({ overrides, customSettings, }: {
136
+ overrides?: Partial<BaseAdapterConfig> | undefined;
137
+ customSettings?: SettingsMap | undefined;
138
+ }) => AdapterConfig<CustomSettings>;
139
+ declare type SettingValueType = string | number | boolean;
140
+ declare type SettingType<C extends Setting> = C['type'] extends 'string' ? string : C['type'] extends 'number' ? number : C['type'] extends 'boolean' ? boolean : C['type'] extends 'enum' ? C['options'] extends readonly string[] ? C['options'][number] : never : never;
141
+ declare type BaseSettingsType = typeof BaseSettings;
142
+ export declare type Setting = {
143
+ type: 'string';
144
+ description: string;
145
+ options?: never;
146
+ default?: string;
147
+ validate?: (value?: string) => ValidationErrorMessage;
148
+ required?: false;
149
+ } | {
150
+ type: 'string';
151
+ description: string;
152
+ options?: never;
153
+ default?: string;
154
+ validate?: (value: string) => ValidationErrorMessage;
155
+ required: true;
156
+ } | {
157
+ type: 'number';
158
+ description: string;
159
+ options?: never;
160
+ default?: number;
161
+ validate?: (value?: number) => ValidationErrorMessage;
162
+ required?: false;
163
+ } | {
164
+ type: 'number';
165
+ description: string;
166
+ options?: never;
167
+ default?: number;
168
+ validate?: (value: number) => ValidationErrorMessage;
169
+ required: true;
170
+ } | {
171
+ type: 'boolean';
172
+ description: string;
173
+ options?: never;
174
+ default?: boolean;
175
+ validate?: (value?: boolean) => ValidationErrorMessage;
176
+ required?: false;
177
+ } | {
178
+ type: 'boolean';
179
+ description: string;
180
+ options?: never;
181
+ default?: boolean;
182
+ validate?: (value: boolean) => ValidationErrorMessage;
183
+ required: true;
184
+ } | {
185
+ type: 'enum';
186
+ description: string;
187
+ default?: string;
188
+ options: readonly string[];
189
+ validate?: (value?: string) => ValidationErrorMessage;
190
+ required?: false;
191
+ } | {
192
+ type: 'enum';
193
+ description: string;
194
+ default?: string;
195
+ options: readonly string[];
196
+ validate?: (value: string) => ValidationErrorMessage;
197
+ required: true;
198
+ };
199
+ export declare type AdapterConfigFromSettings<T extends SettingsMap> = {
200
+ -readonly [K in keyof T as T[K] extends {
201
+ default: SettingValueType;
202
+ } ? K : T[K]['required'] extends true ? K : never]: SettingType<T[K]>;
203
+ } & {
204
+ -readonly [K in keyof T as T[K] extends {
205
+ default: SettingValueType;
206
+ } ? never : T[K]['required'] extends true ? never : K]?: SettingType<T[K]> | undefined;
207
+ };
208
+ export declare type BaseAdapterConfig = AdapterConfigFromSettings<BaseSettingsType>;
209
+ export declare type AdapterConfig<T extends CustomSettingsType<T> = SettingsMap> = AdapterConfigFromSettings<T> & BaseAdapterConfig;
210
+ export declare type CustomSettingsType<T = SettingsMap> = Record<keyof T, Setting>;
211
+ export declare type EmptySettings = Record<string, never>;
212
+ export declare type SettingsMap = Record<string, Setting>;
213
+ export declare type ValidationErrorMessage = string | undefined;
214
+ export {};