@chainlink/external-adapter-framework 0.0.14 → 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 (292) hide show
  1. package/.c8rc.json +3 -0
  2. package/.eslintignore +10 -0
  3. package/.eslintrc.js +96 -0
  4. package/.github/README.MD +42 -0
  5. package/.github/actions/setup/action.yaml +13 -0
  6. package/.github/workflows/label.yaml +39 -0
  7. package/.github/workflows/main.yaml +39 -0
  8. package/.github/workflows/publish.yaml +17 -0
  9. package/.prettierignore +13 -0
  10. package/.yarnrc +0 -0
  11. package/README.md +103 -0
  12. package/dist/examples/coingecko/test/e2e/adapter.test.ts.js +82953 -0
  13. package/dist/examples/coingecko/test/integration/adapter.test.ts.js +91672 -0
  14. package/dist/main.js +72703 -0
  15. package/docker-compose.yaml +35 -0
  16. package/env.sh +54 -0
  17. package/env2.sh +55 -0
  18. package/jest.config.js +5 -0
  19. package/package.json +14 -3
  20. package/publish.sh +0 -0
  21. package/src/adapter.ts +263 -0
  22. package/src/background-executor.ts +52 -0
  23. package/src/cache/factory.ts +26 -0
  24. package/src/cache/index.ts +258 -0
  25. package/src/cache/local.ts +73 -0
  26. package/src/cache/metrics.ts +112 -0
  27. package/src/cache/redis.ts +93 -0
  28. package/src/config/index.ts +517 -0
  29. package/src/config/provider-limits.ts +127 -0
  30. package/src/examples/bank-frick/README.MD +10 -0
  31. package/src/examples/bank-frick/accounts.ts +246 -0
  32. package/src/examples/bank-frick/config/index.ts +53 -0
  33. package/src/examples/bank-frick/index.ts +13 -0
  34. package/src/examples/bank-frick/types.d.ts +38 -0
  35. package/src/examples/bank-frick/util.ts +55 -0
  36. package/src/examples/coingecko/src/config/index.ts +12 -0
  37. package/src/examples/coingecko/src/config/overrides.json +10826 -0
  38. package/src/examples/coingecko/src/cryptoUtils.ts +88 -0
  39. package/src/examples/coingecko/src/endpoint/coins.ts +54 -0
  40. package/src/examples/coingecko/src/endpoint/crypto-marketcap.ts +66 -0
  41. package/src/examples/coingecko/src/endpoint/crypto-volume.ts +66 -0
  42. package/src/examples/coingecko/src/endpoint/crypto.ts +63 -0
  43. package/src/examples/coingecko/src/endpoint/dominance.ts +40 -0
  44. package/src/examples/coingecko/src/endpoint/global-marketcap.ts +40 -0
  45. package/src/examples/coingecko/src/endpoint/index.ts +6 -0
  46. package/src/examples/coingecko/src/globalUtils.ts +78 -0
  47. package/src/examples/coingecko/src/index.ts +17 -0
  48. package/src/examples/coingecko/test/e2e/adapter.test.ts +278 -0
  49. package/src/examples/coingecko/test/integration/__snapshots__/adapter.test.ts.snap +15 -0
  50. package/src/examples/coingecko/test/integration/adapter.test.ts +281 -0
  51. package/src/examples/coingecko/test/integration/capturedRequests.json +1 -0
  52. package/src/examples/coingecko/test/integration/fixtures.ts +42 -0
  53. package/src/examples/coingecko-old/batch-warming.ts +79 -0
  54. package/src/examples/coingecko-old/index.ts +9 -0
  55. package/src/examples/coingecko-old/rest.ts +77 -0
  56. package/src/examples/ncfx/config/index.ts +12 -0
  57. package/src/examples/ncfx/index.ts +9 -0
  58. package/src/examples/ncfx/websocket.ts +99 -0
  59. package/src/index.ts +149 -0
  60. package/src/metrics/constants.ts +23 -0
  61. package/src/metrics/index.ts +115 -0
  62. package/src/metrics/util.ts +18 -0
  63. package/src/rate-limiting/background/fixed-frequency.ts +45 -0
  64. package/src/rate-limiting/index.ts +100 -0
  65. package/src/rate-limiting/metrics.ts +18 -0
  66. package/src/rate-limiting/request/simple-counting.ts +76 -0
  67. package/src/transports/batch-warming.ts +127 -0
  68. package/src/transports/index.ts +152 -0
  69. package/src/transports/metrics.ts +95 -0
  70. package/src/transports/rest.ts +168 -0
  71. package/src/transports/util.ts +63 -0
  72. package/src/transports/websocket.ts +245 -0
  73. package/src/util/index.ts +23 -0
  74. package/src/util/logger.ts +69 -0
  75. package/src/util/recordRequests.ts +47 -0
  76. package/src/util/request.ts +117 -0
  77. package/src/util/subscription-set/expiring-sorted-set.ts +54 -0
  78. package/src/util/subscription-set/subscription-set.ts +35 -0
  79. package/src/util/test-payload-loader.ts +87 -0
  80. package/src/validation/error.ts +116 -0
  81. package/src/validation/index.ts +110 -0
  82. package/src/validation/input-params.ts +45 -0
  83. package/src/validation/override-functions.ts +44 -0
  84. package/src/validation/overrideFunctions.ts +44 -0
  85. package/src/validation/preset-tokens.json +23 -0
  86. package/src/validation/validator.ts +384 -0
  87. package/test/adapter.test.ts +27 -0
  88. package/test/background-executor.test.ts +108 -0
  89. package/test/cache/cache-key.test.ts +114 -0
  90. package/test/cache/helper.ts +100 -0
  91. package/test/cache/local.test.ts +54 -0
  92. package/test/cache/redis.test.ts +89 -0
  93. package/test/correlation.test.ts +114 -0
  94. package/test/index.test.ts +37 -0
  95. package/test/metrics/feed-id.test.ts +38 -0
  96. package/test/metrics/helper.ts +14 -0
  97. package/test/metrics/labels.test.ts +36 -0
  98. package/test/metrics/metrics.test.ts +267 -0
  99. package/test/metrics/redis-metrics.test.ts +113 -0
  100. package/test/metrics/warmer-metrics.test.ts +193 -0
  101. package/test/metrics/ws-metrics.test.ts +225 -0
  102. package/test/rate-limit-config.test.ts +242 -0
  103. package/test/smoke/smoke.test.ts +166 -0
  104. package/test/smoke/test-payload-fail.json +3 -0
  105. package/test/smoke/test-payload.js +22 -0
  106. package/test/smoke/test-payload.json +7 -0
  107. package/test/transports/batch.test.ts +466 -0
  108. package/test/transports/rest.test.ts +242 -0
  109. package/test/transports/websocket.test.ts +183 -0
  110. package/test/tsconfig.json +5 -0
  111. package/test/util.ts +77 -0
  112. package/test/validation.test.ts +178 -0
  113. package/test.sh +20 -0
  114. package/test2.sh +2 -0
  115. package/tsconfig.json +28 -0
  116. package/typedoc.json +6 -0
  117. package/webpack.config.js +57 -0
  118. package/yarn-error.log +3778 -0
  119. package/adapter.d.ts +0 -107
  120. package/adapter.js +0 -115
  121. package/background-executor.d.ts +0 -11
  122. package/background-executor.js +0 -45
  123. package/cache/factory.d.ts +0 -6
  124. package/cache/factory.js +0 -55
  125. package/cache/index.d.ts +0 -94
  126. package/cache/index.js +0 -173
  127. package/cache/local.d.ts +0 -23
  128. package/cache/local.js +0 -83
  129. package/cache/metrics.d.ts +0 -27
  130. package/cache/metrics.js +0 -120
  131. package/cache/redis.d.ts +0 -16
  132. package/cache/redis.js +0 -100
  133. package/chainlink-external-adapter-framework-0.0.6.tgz +0 -0
  134. package/config/index.d.ts +0 -209
  135. package/config/index.js +0 -380
  136. package/config/provider-limits.d.ts +0 -31
  137. package/config/provider-limits.js +0 -79
  138. package/examples/bank-frick/accounts.d.ts +0 -39
  139. package/examples/bank-frick/accounts.js +0 -191
  140. package/examples/bank-frick/config/index.d.ts +0 -4
  141. package/examples/bank-frick/config/index.js +0 -54
  142. package/examples/bank-frick/index.d.ts +0 -2
  143. package/examples/bank-frick/index.js +0 -14
  144. package/examples/bank-frick/util.d.ts +0 -4
  145. package/examples/bank-frick/util.js +0 -39
  146. package/examples/coingecko/batch-warming.d.ts +0 -2
  147. package/examples/coingecko/batch-warming.js +0 -52
  148. package/examples/coingecko/index.d.ts +0 -2
  149. package/examples/coingecko/index.js +0 -10
  150. package/examples/coingecko/rest.d.ts +0 -2
  151. package/examples/coingecko/rest.js +0 -50
  152. package/examples/ncfx/config/index.d.ts +0 -12
  153. package/examples/ncfx/config/index.js +0 -15
  154. package/examples/ncfx/index.d.ts +0 -2
  155. package/examples/ncfx/index.js +0 -10
  156. package/examples/ncfx/websocket.d.ts +0 -36
  157. package/examples/ncfx/websocket.js +0 -72
  158. package/index.d.ts +0 -11
  159. package/index.js +0 -133
  160. package/metrics/constants.d.ts +0 -16
  161. package/metrics/constants.js +0 -25
  162. package/metrics/index.d.ts +0 -15
  163. package/metrics/index.js +0 -122
  164. package/metrics/util.d.ts +0 -7
  165. package/metrics/util.js +0 -9
  166. package/package/adapter.d.ts +0 -88
  167. package/package/adapter.js +0 -112
  168. package/package/background-executor.d.ts +0 -11
  169. package/package/background-executor.js +0 -45
  170. package/package/cache/factory.d.ts +0 -6
  171. package/package/cache/factory.js +0 -57
  172. package/package/cache/index.d.ts +0 -90
  173. package/package/cache/index.js +0 -169
  174. package/package/cache/local.d.ts +0 -23
  175. package/package/cache/local.js +0 -83
  176. package/package/cache/metrics.d.ts +0 -27
  177. package/package/cache/metrics.js +0 -120
  178. package/package/cache/redis.d.ts +0 -16
  179. package/package/cache/redis.js +0 -100
  180. package/package/config/index.d.ts +0 -195
  181. package/package/config/index.js +0 -365
  182. package/package/config/provider-limits.d.ts +0 -31
  183. package/package/config/provider-limits.js +0 -76
  184. package/package/examples/coingecko/batch-warming.d.ts +0 -2
  185. package/package/examples/coingecko/batch-warming.js +0 -52
  186. package/package/examples/coingecko/index.d.ts +0 -2
  187. package/package/examples/coingecko/index.js +0 -10
  188. package/package/examples/coingecko/rest.d.ts +0 -2
  189. package/package/examples/coingecko/rest.js +0 -50
  190. package/package/examples/ncfx/config/index.d.ts +0 -12
  191. package/package/examples/ncfx/config/index.js +0 -15
  192. package/package/examples/ncfx/index.d.ts +0 -2
  193. package/package/examples/ncfx/index.js +0 -10
  194. package/package/examples/ncfx/websocket.d.ts +0 -36
  195. package/package/examples/ncfx/websocket.js +0 -72
  196. package/package/index.d.ts +0 -12
  197. package/package/index.js +0 -92
  198. package/package/metrics/constants.d.ts +0 -16
  199. package/package/metrics/constants.js +0 -25
  200. package/package/metrics/index.d.ts +0 -15
  201. package/package/metrics/index.js +0 -123
  202. package/package/metrics/util.d.ts +0 -3
  203. package/package/metrics/util.js +0 -9
  204. package/package/package.json +0 -72
  205. package/package/rate-limiting/background/fixed-frequency.d.ts +0 -10
  206. package/package/rate-limiting/background/fixed-frequency.js +0 -37
  207. package/package/rate-limiting/index.d.ts +0 -54
  208. package/package/rate-limiting/index.js +0 -63
  209. package/package/rate-limiting/metrics.d.ts +0 -3
  210. package/package/rate-limiting/metrics.js +0 -44
  211. package/package/rate-limiting/request/simple-counting.d.ts +0 -20
  212. package/package/rate-limiting/request/simple-counting.js +0 -62
  213. package/package/test.d.ts +0 -1
  214. package/package/test.js +0 -6
  215. package/package/transports/batch-warming.d.ts +0 -34
  216. package/package/transports/batch-warming.js +0 -101
  217. package/package/transports/index.d.ts +0 -87
  218. package/package/transports/index.js +0 -87
  219. package/package/transports/metrics.d.ts +0 -21
  220. package/package/transports/metrics.js +0 -105
  221. package/package/transports/rest.d.ts +0 -43
  222. package/package/transports/rest.js +0 -129
  223. package/package/transports/util.d.ts +0 -8
  224. package/package/transports/util.js +0 -85
  225. package/package/transports/websocket.d.ts +0 -80
  226. package/package/transports/websocket.js +0 -169
  227. package/package/util/expiring-sorted-set.d.ts +0 -21
  228. package/package/util/expiring-sorted-set.js +0 -47
  229. package/package/util/index.d.ts +0 -11
  230. package/package/util/index.js +0 -35
  231. package/package/util/logger.d.ts +0 -42
  232. package/package/util/logger.js +0 -62
  233. package/package/util/request.d.ts +0 -55
  234. package/package/util/request.js +0 -2
  235. package/package/validation/error.d.ts +0 -50
  236. package/package/validation/error.js +0 -79
  237. package/package/validation/index.d.ts +0 -5
  238. package/package/validation/index.js +0 -86
  239. package/package/validation/input-params.d.ts +0 -15
  240. package/package/validation/input-params.js +0 -30
  241. package/package/validation/override-functions.d.ts +0 -3
  242. package/package/validation/override-functions.js +0 -40
  243. package/package/validation/preset-tokens.json +0 -23
  244. package/package/validation/validator.d.ts +0 -47
  245. package/package/validation/validator.js +0 -303
  246. package/rate-limiting/background/fixed-frequency.d.ts +0 -10
  247. package/rate-limiting/background/fixed-frequency.js +0 -35
  248. package/rate-limiting/index.d.ts +0 -54
  249. package/rate-limiting/index.js +0 -63
  250. package/rate-limiting/metrics.d.ts +0 -3
  251. package/rate-limiting/metrics.js +0 -44
  252. package/rate-limiting/request/simple-counting.d.ts +0 -20
  253. package/rate-limiting/request/simple-counting.js +0 -62
  254. package/test.d.ts +0 -1
  255. package/test.js +0 -6
  256. package/transports/batch-warming.d.ts +0 -35
  257. package/transports/batch-warming.js +0 -101
  258. package/transports/index.d.ts +0 -70
  259. package/transports/index.js +0 -87
  260. package/transports/metrics.d.ts +0 -21
  261. package/transports/metrics.js +0 -105
  262. package/transports/rest.d.ts +0 -44
  263. package/transports/rest.js +0 -131
  264. package/transports/util.d.ts +0 -8
  265. package/transports/util.js +0 -85
  266. package/transports/websocket.d.ts +0 -81
  267. package/transports/websocket.js +0 -168
  268. package/util/expiring-sorted-set.d.ts +0 -21
  269. package/util/expiring-sorted-set.js +0 -47
  270. package/util/index.d.ts +0 -12
  271. package/util/index.js +0 -35
  272. package/util/logger.d.ts +0 -42
  273. package/util/logger.js +0 -62
  274. package/util/request.d.ts +0 -57
  275. package/util/request.js +0 -2
  276. package/util/subscription-set/expiring-sorted-set.d.ts +0 -22
  277. package/util/subscription-set/expiring-sorted-set.js +0 -47
  278. package/util/subscription-set/subscription-set.d.ts +0 -18
  279. package/util/subscription-set/subscription-set.js +0 -19
  280. package/util/test-payload-loader.d.ts +0 -25
  281. package/util/test-payload-loader.js +0 -83
  282. package/validation/error.d.ts +0 -50
  283. package/validation/error.js +0 -79
  284. package/validation/index.d.ts +0 -5
  285. package/validation/index.js +0 -91
  286. package/validation/input-params.d.ts +0 -15
  287. package/validation/input-params.js +0 -30
  288. package/validation/override-functions.d.ts +0 -3
  289. package/validation/override-functions.js +0 -40
  290. package/validation/preset-tokens.json +0 -23
  291. package/validation/validator.d.ts +0 -47
  292. package/validation/validator.js +0 -303
@@ -1,81 +0,0 @@
1
- import WebSocket from 'ws';
2
- import { AdapterContext, AdapterDependencies } from '../adapter';
3
- import { Cache } from '../cache';
4
- import { SettingsMap } from '../config';
5
- import { BackgroundExecuteRateLimiter } from '../rate-limiting';
6
- import { SubscriptionSet } from '../util';
7
- import { AdapterRequest, ProviderResult } from '../util/request';
8
- import { Transport } from './';
9
- export declare const DEFAULT_WS_TTL = 10000;
10
- declare type WebSocketClass = new (url: string, protocols?: string | string[] | undefined) => WebSocket;
11
- export declare class WebSocketClassProvider {
12
- static ctor: WebSocketClass;
13
- static set(ctor: WebSocketClass): void;
14
- static get(): WebSocketClass;
15
- }
16
- /**
17
- * Config object that is provided to the WebSocketTransport constructor.
18
- */
19
- export interface WebSocketTransportConfig<AdapterParams, ProviderDataMessage, CustomSettings extends SettingsMap> {
20
- /** Endpoint to which to open the WS connection*/
21
- url: string;
22
- /** Map of handlers for different WS lifecycle events */
23
- handlers: {
24
- /**
25
- * Handles when the WS is successfully opened
26
- *
27
- * @param wsConnection - the WebSocket with an established connection
28
- * @returns an empty Promise, or void
29
- */
30
- open: (wsConnection: WebSocket, context: AdapterContext<CustomSettings>) => Promise<void> | void;
31
- /**
32
- * Handles when the WS receives a message
33
- *
34
- * @param message - the message received by the WS
35
- * @param context - the background context for the Adapter
36
- * @returns a list of cache entries of adapter responses to set in the cache
37
- */
38
- message: (message: ProviderDataMessage, context: AdapterContext<CustomSettings>) => ProviderResult<AdapterParams>[];
39
- };
40
- /** Map of "builders", functions that will be used to prepare specific WS messages */
41
- builders: {
42
- /**
43
- * Builds a WS message that will be sent to subscribe to a specific feed
44
- *
45
- * @param params - the body of the adapter request
46
- * @returns the WS message (can be any type as long as the [[WebSocket]] doesn't complain)
47
- */
48
- subscribeMessage: (params: AdapterParams) => unknown;
49
- /**
50
- * Builds a WS message that will be sent to unsubscribe to a specific feed
51
- *
52
- * @param params - the body of the adapter request
53
- * @returns the WS message (can be any type as long as the [[WebSocket]] doesn't complain)
54
- */
55
- unsubscribeMessage: (params: AdapterParams) => unknown;
56
- };
57
- }
58
- /**
59
- * Transport implementation that takes incoming requests, adds them to an [[subscriptionSet]] and,
60
- * through a WebSocket connection, subscribes to the relevant feeds to populate the cache.
61
- *
62
- * @typeParam AdapterParams - interface for the adapter request body
63
- * @typeParam ProviderDataMessage - interface for a WS message containing processable data (i.e. not part of open/close/login/etc)
64
- */
65
- export declare class WebSocketTransport<AdapterParams, ProviderDataMessage, CustomSettings extends SettingsMap> implements Transport<AdapterParams, null, CustomSettings> {
66
- private config;
67
- cache: Cache;
68
- rateLimiter: BackgroundExecuteRateLimiter;
69
- subscriptionSet: SubscriptionSet<AdapterParams>;
70
- localSubscriptions: AdapterParams[];
71
- wsConnection: WebSocket;
72
- constructor(config: WebSocketTransportConfig<AdapterParams, ProviderDataMessage, CustomSettings>);
73
- initialize(dependencies: AdapterDependencies): Promise<void>;
74
- hasBeenSetUp(req: AdapterRequest<AdapterParams>): Promise<boolean>;
75
- setup(req: AdapterRequest<AdapterParams>): Promise<void>;
76
- serializeMessage(payload: unknown): string;
77
- deserializeMessage(data: WebSocket.Data): ProviderDataMessage;
78
- establishWsConnection(context: AdapterContext<CustomSettings>): Promise<unknown>;
79
- backgroundExecute(context: AdapterContext<CustomSettings>): Promise<number>;
80
- }
81
- export {};
@@ -1,168 +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.WebSocketTransport = exports.WebSocketClassProvider = exports.DEFAULT_WS_TTL = void 0;
30
- const ws_1 = __importDefault(require("ws"));
31
- const util_1 = require("../util");
32
- const _1 = require("./");
33
- const transportMetrics = __importStar(require("./metrics"));
34
- // TODO: Config
35
- exports.DEFAULT_WS_TTL = 10000;
36
- const logger = (0, util_1.makeLogger)('WebSocketTransport');
37
- class WebSocketClassProvider {
38
- static set(ctor) {
39
- this.ctor = ctor;
40
- }
41
- static get() {
42
- return this.ctor;
43
- }
44
- }
45
- exports.WebSocketClassProvider = WebSocketClassProvider;
46
- WebSocketClassProvider.ctor = ws_1.default;
47
- /**
48
- * Transport implementation that takes incoming requests, adds them to an [[subscriptionSet]] and,
49
- * through a WebSocket connection, subscribes to the relevant feeds to populate the cache.
50
- *
51
- * @typeParam AdapterParams - interface for the adapter request body
52
- * @typeParam ProviderDataMessage - interface for a WS message containing processable data (i.e. not part of open/close/login/etc)
53
- */
54
- class WebSocketTransport {
55
- constructor(config) {
56
- this.config = config;
57
- // The double sets serve to create a simple polling mechanism instead of needing a subscription
58
- // This one would not; this is always local state
59
- this.localSubscriptions = [];
60
- }
61
- async initialize(dependencies) {
62
- this.cache = dependencies.cache;
63
- this.rateLimiter = dependencies.backgroundExecuteRateLimiter;
64
- this.subscriptionSet = dependencies.subscriptionSetFactory.buildSet();
65
- }
66
- async hasBeenSetUp(req) {
67
- return !!(await this.subscriptionSet.get(req.requestContext.cacheKey));
68
- }
69
- async setup(req) {
70
- logger.debug(`Adding entry to subscription set: [${req.requestContext.cacheKey}] = ${req.requestContext.data}`);
71
- await this.subscriptionSet.add(req.requestContext.cacheKey, req.requestContext.data, exports.DEFAULT_WS_TTL);
72
- }
73
- // TODO: Maybe we don't do this, and leave the preparation on the adapter's side?
74
- // TODO: Maybe we store adapter params pre-prepared? That would be more efficient
75
- // Assuming always JSON payloads for now, makes it all cleaner
76
- serializeMessage(payload) {
77
- return typeof payload === 'string' ? payload : JSON.stringify(payload);
78
- }
79
- deserializeMessage(data) {
80
- return JSON.parse(data.toString());
81
- }
82
- establishWsConnection(context) {
83
- return new Promise((resolve) => {
84
- const ctor = WebSocketClassProvider.get();
85
- this.wsConnection = new ctor(this.config.url);
86
- this.wsConnection.addEventListener('open', async (event) => {
87
- logger.debug(`Opened websocket connection.`);
88
- await this.config.handlers.open(this.wsConnection, context);
89
- logger.debug('Successfully executed connection opened handler');
90
- // Record active ws connections by incrementing count on open
91
- // Using URL in label since connection_key is removed from v3
92
- transportMetrics.wsConnectionActive.inc();
93
- resolve(true);
94
- });
95
- this.wsConnection.addEventListener('message', async (event) => {
96
- // TODO: Assuming JSON always, maybe use BSON also?
97
- const parsed = this.deserializeMessage(event.data);
98
- logger.trace(`Got ws message: ${parsed}`);
99
- const results = this.config.handlers.message(parsed, context);
100
- if (Array.isArray(results)) {
101
- const responses = (0, _1.buildCacheEntriesFromResults)(results, context);
102
- logger.trace(`Writing ${responses.length} responses to cache`);
103
- await this.cache.setMany(responses, context.adapterConfig.CACHE_MAX_AGE);
104
- }
105
- });
106
- this.wsConnection.addEventListener('error', async (event) => {
107
- // Record connection error count
108
- transportMetrics.wsConnectionErrors
109
- .labels(transportMetrics.connectionErrorLabels(event.message))
110
- .inc();
111
- });
112
- this.wsConnection.addEventListener('close', (event) => {
113
- logger.debug(`Closed websocket connection. Code: ${event.code} ; reason: ${event.reason?.toString()}`);
114
- // Record active ws connections by decrementing count on close
115
- // Using URL in label since connection_key is removed from v3
116
- transportMetrics.wsConnectionActive.dec();
117
- });
118
- });
119
- }
120
- // Unlike cache warming, this execute will manage subscriptions
121
- async backgroundExecute(context) {
122
- logger.debug('Starting background execute, getting subscriptions from sorted set');
123
- const desiredSubs = await this.subscriptionSet.getAll();
124
- logger.debug('Generating delta (subscribes & unsubscribes)');
125
- // TODO: More efficient algorithm, this is really easy to read, but high(er) time complexity
126
- const subscribeParams = desiredSubs.filter((s) => !this.localSubscriptions.includes(s));
127
- const subscribes = subscribeParams
128
- .map(this.config.builders.subscribeMessage)
129
- .map(this.serializeMessage);
130
- const unsubscribeParams = this.localSubscriptions.filter((s) => !desiredSubs.includes(s));
131
- const unsubscribes = unsubscribeParams
132
- .map(this.config.builders.unsubscribeMessage)
133
- .map(this.serializeMessage);
134
- logger.debug(`${subscribes.length} new subscriptions; ${unsubscribes.length} to unsubscribe`);
135
- if (subscribes.length) {
136
- logger.trace(`Will subscribe to: ${subscribes}`);
137
- }
138
- if (unsubscribes.length) {
139
- logger.trace(`Will unsubscribe to: ${unsubscribes}`);
140
- }
141
- // New subs && no connection -> connect -> add subs
142
- // No new subs && no connection -> skip
143
- // New subs && connection -> add subs
144
- // No new subs && connection -> unsubs only
145
- if (!subscribes.length && !this.wsConnection) {
146
- logger.debug('No entries in subscription set and no established connection, skipping');
147
- return this.rateLimiter.msUntilNextExecution(context.adapterEndpoint.name);
148
- }
149
- if (!this.wsConnection && subscribes.length) {
150
- logger.debug('No established connection and new subscriptions available, connecting to WS');
151
- await this.establishWsConnection(context);
152
- }
153
- // TODO: Close connection at some point?
154
- logger.debug('Sending subs/unsubs if there are any');
155
- const messages = unsubscribes.concat(subscribes);
156
- for (const message of messages) {
157
- logger.trace(`Sending message: ${JSON.stringify(message)}`);
158
- this.wsConnection.send(message);
159
- }
160
- // Record WS message and subscription metrics
161
- transportMetrics.recordWsMessageMetrics(context, subscribeParams, unsubscribeParams);
162
- logger.debug('Setting local state to cache value');
163
- this.localSubscriptions = desiredSubs;
164
- logger.debug('Background execute complete');
165
- return this.rateLimiter.msUntilNextExecution(context.adapterEndpoint.name);
166
- }
167
- }
168
- exports.WebSocketTransport = WebSocketTransport;
@@ -1,21 +0,0 @@
1
- /**
2
- * An object describing an entry in the expiring sorted set.
3
- * @typeParam T - the type of the entry's value
4
- */
5
- interface ExpiringSortedSetEntry<T> {
6
- value: T;
7
- expirationTimestamp: number;
8
- }
9
- /**
10
- * This class implements a set of unique items, each of which has an expiration timestamp.
11
- * On reads, items that have expired will be deleted from the set and not returned.
12
- *
13
- * @typeParam T - the type of the set entries' values
14
- */
15
- export declare class ExpiringSortedSet<T> {
16
- map: Map<string, ExpiringSortedSetEntry<T>>;
17
- add(key: string, value: T, ttl: number): void;
18
- get(key: string): T | undefined;
19
- getAll(): T[];
20
- }
21
- export {};
@@ -1,47 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ExpiringSortedSet = void 0;
4
- /**
5
- * This class implements a set of unique items, each of which has an expiration timestamp.
6
- * On reads, items that have expired will be deleted from the set and not returned.
7
- *
8
- * @typeParam T - the type of the set entries' values
9
- */
10
- class ExpiringSortedSet {
11
- constructor() {
12
- this.map = new Map();
13
- }
14
- add(key, value, ttl) {
15
- this.map.set(key, {
16
- value,
17
- expirationTimestamp: Date.now() + ttl,
18
- });
19
- }
20
- get(key) {
21
- const entry = this.map.get(key);
22
- if (!entry) {
23
- return;
24
- }
25
- else if (entry.expirationTimestamp < Date.now()) {
26
- return entry.value;
27
- }
28
- else {
29
- this.map.delete(key);
30
- }
31
- }
32
- getAll() {
33
- const results = [];
34
- const now = Date.now();
35
- // Since we're iterating, might as well prune here
36
- for (const [key, entry] of this.map.entries()) {
37
- if (entry.expirationTimestamp < now) {
38
- this.map.delete(key); // In theory, this shouldn't happen frequently for feeds
39
- }
40
- else {
41
- results.push(entry.value);
42
- }
43
- }
44
- return results;
45
- }
46
- }
47
- exports.ExpiringSortedSet = ExpiringSortedSet;
package/util/index.d.ts DELETED
@@ -1,12 +0,0 @@
1
- export * from './request';
2
- export * from './logger';
3
- export * from './subscription-set/subscription-set';
4
- /**
5
- * Sleeps for the provided number of milliseconds
6
- * @param ms - The number of milliseconds to sleep for
7
- * @returns a Promise that resolves once the specified time passes
8
- */
9
- export declare const sleep: (ms: number) => Promise<void>;
10
- export declare const isObject: (o: unknown) => boolean;
11
- export declare const isArray: (o: unknown) => boolean;
12
- export declare type PromiseOrValue<T> = Promise<T> | T;
package/util/index.js DELETED
@@ -1,35 +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 __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.isArray = exports.isObject = exports.sleep = void 0;
18
- __exportStar(require("./request"), exports);
19
- __exportStar(require("./logger"), exports);
20
- __exportStar(require("./subscription-set/subscription-set"), exports);
21
- /**
22
- * Sleeps for the provided number of milliseconds
23
- * @param ms - The number of milliseconds to sleep for
24
- * @returns a Promise that resolves once the specified time passes
25
- */
26
- const sleep = (ms) => {
27
- return new Promise((resolve) => {
28
- setTimeout(resolve, ms);
29
- });
30
- };
31
- exports.sleep = sleep;
32
- const isObject = (o) => o !== null && typeof o === 'object' && Array.isArray(o) === false;
33
- exports.isObject = isObject;
34
- const isArray = (o) => o !== null && typeof o === 'object' && Array.isArray(o);
35
- exports.isArray = isArray;
package/util/logger.d.ts DELETED
@@ -1,42 +0,0 @@
1
- /// <reference types="node" />
2
- import pino from 'pino';
3
- import { AdapterRequest } from './request';
4
- import { FastifyReply, HookHandlerDoneFunction } from 'fastify';
5
- import { AsyncLocalStorage } from 'node:async_hooks';
6
- export declare const asyncLocalStorage: AsyncLocalStorage<unknown>;
7
- export declare type Store = {
8
- correlationId: string;
9
- };
10
- /**
11
- * Instead of using a global logger instance, we want to force using a child logger
12
- * with a specific layer set in it, so that we can filter logs by where they're output from.
13
- *
14
- * Details on what each log level represents:
15
- * "trace": Forensic debugging of issues on a local machine.
16
- * "debug": Detailed logging level to get more context from users on their environments.
17
- * "info": High-level informational messages, to describe at a glance the high level state of the system.
18
- * "warn": A mild error occurred that might require non-urgent action.
19
- * "error": An unexpected error occurred during the regular operation of a well-maintained EA.
20
- * "fatal": The EA encountered an unrecoverable problem and had to exit.
21
- *
22
- * Full reference this is based on can be found at
23
- * https://github.com/smartcontractkit/documentation/blob/main/docs/Node%20Operators/configuration-variables.md#log_level
24
- *
25
- * @param layer - the layer name to include in the logs (e.g. "SomeMiddleware", "RedisCache", etc.)
26
- * @returns a layer specific logger
27
- */
28
- export declare const makeLogger: (layer: string) => pino.Logger<{
29
- level: string;
30
- mixin(): {};
31
- transport: {
32
- target: string;
33
- options: {
34
- levelFirst: boolean;
35
- levelLabel: string;
36
- ignore: string;
37
- messageFormat: string;
38
- translateTime: string;
39
- };
40
- } | undefined;
41
- } & pino.ChildLoggerOptions>;
42
- export declare const loggingContextMiddleware: (req: AdapterRequest, res: FastifyReply, done: HookHandlerDoneFunction) => void;
package/util/logger.js DELETED
@@ -1,62 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.loggingContextMiddleware = exports.makeLogger = exports.asyncLocalStorage = void 0;
7
- const pino_1 = __importDefault(require("pino"));
8
- const config_1 = require("../config");
9
- const crypto_1 = require("crypto");
10
- const node_async_hooks_1 = require("node:async_hooks");
11
- exports.asyncLocalStorage = new node_async_hooks_1.AsyncLocalStorage();
12
- const debugTransport = {
13
- target: 'pino-pretty',
14
- options: {
15
- levelFirst: true,
16
- levelLabel: 'level',
17
- ignore: 'layer,pid,hostname',
18
- messageFormat: '[{correlationId}][{layer}] {msg}',
19
- translateTime: 'yyyy-mm-dd HH:MM:ss.l',
20
- },
21
- };
22
- // Base logger, shouldn't be used because we want layers to be specified
23
- const baseLogger = (0, pino_1.default)({
24
- level: process.env['LOG_LEVEL']?.toLowerCase() || config_1.BaseSettings.LOG_LEVEL.default,
25
- mixin() {
26
- if (process.env['CORRELATION_ID_ENABLED'] === 'true') {
27
- const store = exports.asyncLocalStorage.getStore();
28
- if (store) {
29
- return store;
30
- }
31
- }
32
- return {};
33
- },
34
- transport: process.env['DEBUG'] === 'true' ? debugTransport : undefined,
35
- });
36
- /**
37
- * Instead of using a global logger instance, we want to force using a child logger
38
- * with a specific layer set in it, so that we can filter logs by where they're output from.
39
- *
40
- * Details on what each log level represents:
41
- * "trace": Forensic debugging of issues on a local machine.
42
- * "debug": Detailed logging level to get more context from users on their environments.
43
- * "info": High-level informational messages, to describe at a glance the high level state of the system.
44
- * "warn": A mild error occurred that might require non-urgent action.
45
- * "error": An unexpected error occurred during the regular operation of a well-maintained EA.
46
- * "fatal": The EA encountered an unrecoverable problem and had to exit.
47
- *
48
- * Full reference this is based on can be found at
49
- * https://github.com/smartcontractkit/documentation/blob/main/docs/Node%20Operators/configuration-variables.md#log_level
50
- *
51
- * @param layer - the layer name to include in the logs (e.g. "SomeMiddleware", "RedisCache", etc.)
52
- * @returns a layer specific logger
53
- */
54
- const makeLogger = (layer) => baseLogger.child({ layer });
55
- exports.makeLogger = makeLogger;
56
- const loggingContextMiddleware = (req, res, done) => {
57
- const correlationId = req.headers['x-correlation-id'] || (0, crypto_1.randomUUID)();
58
- exports.asyncLocalStorage.run({ correlationId: correlationId }, () => {
59
- done();
60
- });
61
- };
62
- exports.loggingContextMiddleware = loggingContextMiddleware;
package/util/request.d.ts DELETED
@@ -1,57 +0,0 @@
1
- import { FastifyReply, FastifyRequest, HookHandlerDoneFunction } from 'fastify';
2
- import { InitializedAdapter } from '../adapter';
3
- import { AdapterError } from '../validation/error';
4
- declare module 'fastify' {
5
- interface FastifyRequest {
6
- requestContext: AdapterRequestContext;
7
- }
8
- }
9
- export interface AdapterRequestBody<T = AdapterRequestData> {
10
- endpoint?: string;
11
- data: T;
12
- id?: string;
13
- }
14
- export declare type AdapterRequestContext<T = AdapterRequestData> = {
15
- id: string;
16
- endpointName: string;
17
- cacheKey: string;
18
- data: T;
19
- meta?: AdapterRequestMeta;
20
- };
21
- export declare type AdapterRouteGeneric<T = AdapterRequestData> = {
22
- Body: AdapterRequestBody<T>;
23
- };
24
- export declare type AdapterRequest<T = AdapterRequestData> = FastifyRequest<AdapterRouteGeneric<T>> & {
25
- requestContext: AdapterRequestContext<T>;
26
- };
27
- /**
28
- * Meta info that pertains to exposing metrics
29
- */
30
- export interface AdapterRequestMeta {
31
- metrics?: AdapterMetricsMeta;
32
- error?: AdapterError | Error;
33
- }
34
- /**
35
- * Meta info that pertains to exposing metrics
36
- */
37
- export interface AdapterMetricsMeta {
38
- feedId?: string;
39
- cacheHit?: boolean;
40
- }
41
- export declare type AdapterRequestData = Record<string, unknown> & {
42
- endpoint?: string;
43
- };
44
- export interface ProviderResult<Params> {
45
- params: Params;
46
- value: unknown;
47
- }
48
- export declare type AdapterResponse<T = unknown> = {
49
- statusCode: number;
50
- data: T;
51
- result: unknown;
52
- maxAge?: number;
53
- meta?: AdapterRequestMeta;
54
- providerStatusCode?: number;
55
- };
56
- export declare type Middleware = ((req: AdapterRequest, reply: FastifyReply, done: HookHandlerDoneFunction) => FastifyReply | void) | ((req: AdapterRequest, reply: FastifyReply) => Promise<FastifyReply | void>);
57
- export declare type AdapterMiddlewareBuilder = (adapter: InitializedAdapter) => Middleware;
package/util/request.js DELETED
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,22 +0,0 @@
1
- import { SubscriptionSet } from './subscription-set';
2
- /**
3
- * An object describing an entry in the expiring sorted set.
4
- * @typeParam T - the type of the entry's value
5
- */
6
- interface ExpiringSortedSetEntry<T> {
7
- value: T;
8
- expirationTimestamp: number;
9
- }
10
- /**
11
- * This class implements a set of unique items, each of which has an expiration timestamp.
12
- * On reads, items that have expired will be deleted from the set and not returned.
13
- *
14
- * @typeParam T - the type of the set entries' values
15
- */
16
- export declare class ExpiringSortedSet<T> implements SubscriptionSet<T> {
17
- map: Map<string, ExpiringSortedSetEntry<T>>;
18
- add(key: string, value: T, ttl: number): void;
19
- get(key: string): T | undefined;
20
- getAll(): T[];
21
- }
22
- export {};
@@ -1,47 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ExpiringSortedSet = void 0;
4
- /**
5
- * This class implements a set of unique items, each of which has an expiration timestamp.
6
- * On reads, items that have expired will be deleted from the set and not returned.
7
- *
8
- * @typeParam T - the type of the set entries' values
9
- */
10
- class ExpiringSortedSet {
11
- constructor() {
12
- this.map = new Map();
13
- }
14
- add(key, value, ttl) {
15
- this.map.set(key, {
16
- value,
17
- expirationTimestamp: Date.now() + ttl,
18
- });
19
- }
20
- get(key) {
21
- const entry = this.map.get(key);
22
- if (!entry) {
23
- return;
24
- }
25
- else if (entry.expirationTimestamp < Date.now()) {
26
- return entry.value;
27
- }
28
- else {
29
- this.map.delete(key);
30
- }
31
- }
32
- getAll() {
33
- const results = [];
34
- const now = Date.now();
35
- // Since we're iterating, might as well prune here
36
- for (const [key, entry] of this.map.entries()) {
37
- if (entry.expirationTimestamp < now) {
38
- this.map.delete(key); // In theory, this shouldn't happen frequently for feeds
39
- }
40
- else {
41
- results.push(entry.value);
42
- }
43
- }
44
- return results;
45
- }
46
- }
47
- exports.ExpiringSortedSet = ExpiringSortedSet;
@@ -1,18 +0,0 @@
1
- import { PromiseOrValue } from '..';
2
- import { AdapterConfig } from '../../config';
3
- /**
4
- * Set to hold items to subscribe to from a provider (regardless of protocol)
5
- */
6
- export interface SubscriptionSet<T> {
7
- /** Add a new subscription to the set */
8
- add(key: string, value: T, ttl: number): PromiseOrValue<void>;
9
- /** Get a specific subscription from the set */
10
- get(key: string): PromiseOrValue<T | undefined>;
11
- /** Get all subscriptions from the set as a list */
12
- getAll(): PromiseOrValue<T[]>;
13
- }
14
- export declare class SubscriptionSetFactory {
15
- private cacheType;
16
- constructor(config: AdapterConfig);
17
- buildSet<T>(): SubscriptionSet<T>;
18
- }
@@ -1,19 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SubscriptionSetFactory = void 0;
4
- const expiring_sorted_set_1 = require("./expiring-sorted-set");
5
- class SubscriptionSetFactory {
6
- constructor(config) {
7
- this.cacheType = config.CACHE_TYPE;
8
- }
9
- buildSet() {
10
- switch (this.cacheType) {
11
- case 'local':
12
- return new expiring_sorted_set_1.ExpiringSortedSet();
13
- case 'redis':
14
- // TODO: Implement redis set
15
- return new expiring_sorted_set_1.ExpiringSortedSet();
16
- }
17
- }
18
- }
19
- exports.SubscriptionSetFactory = SubscriptionSetFactory;
@@ -1,25 +0,0 @@
1
- import { AdapterRequestData } from './request';
2
- /**
3
- * The test payload read in from filesystem
4
- */
5
- export interface Payload {
6
- requests: Array<AdapterRequestData>;
7
- }
8
- /**
9
- * Test payload with discriminated union so we can tell when we should just do
10
- * a simple liveness check rather than a sample request
11
- */
12
- declare type TestPayload = (Payload & {
13
- isDefault: false;
14
- }) | {
15
- isDefault: true;
16
- };
17
- /**
18
- * Load in a JSON file containing a test payload for the current adapter,
19
- * used in healthchecks to make sample requests
20
- *
21
- * @param fileName - name of file that contains the test payload data for the smoke endpoint
22
- * @returns the parsed payload with individual requests
23
- */
24
- export declare function loadTestPayload(fileName?: string): TestPayload;
25
- export {};