@chainlink/external-adapter-framework 0.5.0 → 0.5.2

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 (192) hide show
  1. package/adapter/basic.d.ts +90 -0
  2. package/adapter/basic.js +325 -0
  3. package/adapter/basic.js.map +1 -0
  4. package/adapter/endpoint.d.ts +17 -0
  5. package/adapter/endpoint.js +20 -0
  6. package/adapter/endpoint.js.map +1 -0
  7. package/adapter/index.d.ts +4 -0
  8. package/adapter/index.js +21 -0
  9. package/adapter/index.js.map +1 -0
  10. package/adapter/price.d.ts +77 -0
  11. package/adapter/price.js +88 -0
  12. package/adapter/price.js.map +1 -0
  13. package/adapter/types.d.ts +124 -0
  14. package/adapter/types.js +3 -0
  15. package/adapter/types.js.map +1 -0
  16. package/background-executor.d.ts +9 -0
  17. package/background-executor.js +97 -0
  18. package/background-executor.js.map +1 -0
  19. package/cache/factory.d.ts +6 -0
  20. package/cache/factory.js +24 -0
  21. package/cache/factory.js.map +1 -0
  22. package/cache/index.d.ts +87 -0
  23. package/cache/index.js +133 -0
  24. package/cache/index.js.map +1 -0
  25. package/cache/local.d.ts +23 -0
  26. package/cache/local.js +84 -0
  27. package/cache/local.js.map +1 -0
  28. package/cache/metrics.d.ts +27 -0
  29. package/cache/metrics.js +121 -0
  30. package/cache/metrics.js.map +1 -0
  31. package/cache/redis.d.ts +16 -0
  32. package/cache/redis.js +101 -0
  33. package/cache/redis.js.map +1 -0
  34. package/config/index.d.ts +298 -0
  35. package/config/index.js +359 -0
  36. package/config/index.js.map +1 -0
  37. package/config/provider-limits.d.ts +27 -0
  38. package/config/provider-limits.js +75 -0
  39. package/config/provider-limits.js.map +1 -0
  40. package/examples/bank-frick/accounts.d.ts +45 -0
  41. package/examples/bank-frick/accounts.js +203 -0
  42. package/examples/bank-frick/accounts.js.map +1 -0
  43. package/examples/bank-frick/config/index.d.ts +17 -0
  44. package/examples/bank-frick/config/index.js +55 -0
  45. package/examples/bank-frick/config/index.js.map +1 -0
  46. package/examples/bank-frick/index.d.ts +2 -0
  47. package/examples/bank-frick/index.js +16 -0
  48. package/examples/bank-frick/index.js.map +1 -0
  49. package/examples/bank-frick/util.d.ts +4 -0
  50. package/examples/bank-frick/util.js +40 -0
  51. package/examples/bank-frick/util.js.map +1 -0
  52. package/examples/coingecko/src/config/index.d.ts +2 -0
  53. package/examples/coingecko/src/config/index.js +6 -0
  54. package/examples/coingecko/src/config/index.js.map +1 -0
  55. package/examples/coingecko/src/config/overrides.json +10825 -0
  56. package/examples/coingecko/src/crypto-utils.d.ts +62 -0
  57. package/examples/coingecko/src/crypto-utils.js +60 -0
  58. package/examples/coingecko/src/crypto-utils.js.map +1 -0
  59. package/examples/coingecko/src/endpoint/coins.d.ts +26 -0
  60. package/examples/coingecko/src/endpoint/coins.js +37 -0
  61. package/examples/coingecko/src/endpoint/coins.js.map +1 -0
  62. package/examples/coingecko/src/endpoint/crypto-marketcap.d.ts +3 -0
  63. package/examples/coingecko/src/endpoint/crypto-marketcap.js +30 -0
  64. package/examples/coingecko/src/endpoint/crypto-marketcap.js.map +1 -0
  65. package/examples/coingecko/src/endpoint/crypto-volume.d.ts +3 -0
  66. package/examples/coingecko/src/endpoint/crypto-volume.js +30 -0
  67. package/examples/coingecko/src/endpoint/crypto-volume.js.map +1 -0
  68. package/examples/coingecko/src/endpoint/crypto.d.ts +3 -0
  69. package/examples/coingecko/src/endpoint/crypto.js +28 -0
  70. package/examples/coingecko/src/endpoint/crypto.js.map +1 -0
  71. package/examples/coingecko/src/endpoint/dominance.d.ts +3 -0
  72. package/examples/coingecko/src/endpoint/dominance.js +28 -0
  73. package/examples/coingecko/src/endpoint/dominance.js.map +1 -0
  74. package/examples/coingecko/src/endpoint/global-marketcap.d.ts +3 -0
  75. package/examples/coingecko/src/endpoint/global-marketcap.js +28 -0
  76. package/examples/coingecko/src/endpoint/global-marketcap.js.map +1 -0
  77. package/examples/coingecko/src/endpoint/index.d.ts +6 -0
  78. package/examples/coingecko/src/endpoint/index.js +16 -0
  79. package/examples/coingecko/src/endpoint/index.js.map +1 -0
  80. package/examples/coingecko/src/global-utils.d.ts +42 -0
  81. package/examples/coingecko/src/global-utils.js +47 -0
  82. package/examples/coingecko/src/global-utils.js.map +1 -0
  83. package/examples/coingecko/src/index.d.ts +4 -0
  84. package/examples/coingecko/src/index.js +19 -0
  85. package/examples/coingecko/src/index.js.map +1 -0
  86. package/examples/cryptocompare/src/config/index.d.ts +2 -0
  87. package/examples/cryptocompare/src/config/index.js +6 -0
  88. package/examples/cryptocompare/src/config/index.js.map +1 -0
  89. package/examples/cryptocompare/src/endpoints/crypto.d.ts +40 -0
  90. package/examples/cryptocompare/src/endpoints/crypto.js +54 -0
  91. package/examples/cryptocompare/src/endpoints/crypto.js.map +1 -0
  92. package/examples/cryptocompare/src/endpoints/index.d.ts +1 -0
  93. package/examples/cryptocompare/src/endpoints/index.js +6 -0
  94. package/examples/cryptocompare/src/endpoints/index.js.map +1 -0
  95. package/examples/cryptocompare/src/index.d.ts +4 -0
  96. package/examples/cryptocompare/src/index.js +14 -0
  97. package/examples/cryptocompare/src/index.js.map +1 -0
  98. package/examples/genesis/config/index.d.ts +7 -0
  99. package/examples/genesis/config/index.js +11 -0
  100. package/examples/genesis/config/index.js.map +1 -0
  101. package/examples/genesis/index.d.ts +2 -0
  102. package/examples/genesis/index.js +13 -0
  103. package/examples/genesis/index.js.map +1 -0
  104. package/examples/genesis/sseStream.d.ts +27 -0
  105. package/examples/genesis/sseStream.js +149 -0
  106. package/examples/genesis/sseStream.js.map +1 -0
  107. package/index.d.ts +17 -0
  108. package/index.js +174 -0
  109. package/index.js.map +1 -0
  110. package/metrics/constants.d.ts +16 -0
  111. package/metrics/constants.js +26 -0
  112. package/metrics/constants.js.map +1 -0
  113. package/metrics/index.d.ts +19 -0
  114. package/metrics/index.js +139 -0
  115. package/metrics/index.js.map +1 -0
  116. package/metrics/util.d.ts +7 -0
  117. package/metrics/util.js +10 -0
  118. package/metrics/util.js.map +1 -0
  119. package/package.json +50 -42
  120. package/rate-limiting/background/fixed-frequency.d.ts +10 -0
  121. package/rate-limiting/background/fixed-frequency.js +36 -0
  122. package/rate-limiting/background/fixed-frequency.js.map +1 -0
  123. package/rate-limiting/index.d.ts +56 -0
  124. package/rate-limiting/index.js +86 -0
  125. package/rate-limiting/index.js.map +1 -0
  126. package/rate-limiting/metrics.d.ts +3 -0
  127. package/rate-limiting/metrics.js +45 -0
  128. package/rate-limiting/metrics.js.map +1 -0
  129. package/rate-limiting/request/simple-counting.d.ts +20 -0
  130. package/rate-limiting/request/simple-counting.js +63 -0
  131. package/rate-limiting/request/simple-counting.js.map +1 -0
  132. package/transports/batch-warming.d.ts +53 -0
  133. package/transports/batch-warming.js +136 -0
  134. package/transports/batch-warming.js.map +1 -0
  135. package/transports/index.d.ts +108 -0
  136. package/transports/index.js +56 -0
  137. package/transports/index.js.map +1 -0
  138. package/transports/metrics.d.ts +25 -0
  139. package/transports/metrics.js +122 -0
  140. package/transports/metrics.js.map +1 -0
  141. package/transports/rest.d.ts +68 -0
  142. package/transports/rest.js +131 -0
  143. package/transports/rest.js.map +1 -0
  144. package/transports/routing.d.ts +21 -0
  145. package/transports/routing.js +50 -0
  146. package/transports/routing.js.map +1 -0
  147. package/transports/sse.d.ts +61 -0
  148. package/transports/sse.js +97 -0
  149. package/transports/sse.js.map +1 -0
  150. package/transports/util.d.ts +9 -0
  151. package/transports/util.js +87 -0
  152. package/transports/util.js.map +1 -0
  153. package/transports/websocket.d.ts +95 -0
  154. package/transports/websocket.js +164 -0
  155. package/transports/websocket.js.map +1 -0
  156. package/util/censor/censor-list.d.ts +9 -0
  157. package/util/censor/censor-list.js +13 -0
  158. package/util/censor/censor-list.js.map +1 -0
  159. package/util/index.d.ts +13 -0
  160. package/util/index.js +38 -0
  161. package/util/index.js.map +1 -0
  162. package/util/logger.d.ts +49 -0
  163. package/util/logger.js +123 -0
  164. package/util/logger.js.map +1 -0
  165. package/util/request.d.ts +138 -0
  166. package/util/request.js +3 -0
  167. package/util/request.js.map +1 -0
  168. package/util/subscription-set/expiring-sorted-set.d.ts +21 -0
  169. package/util/subscription-set/expiring-sorted-set.js +36 -0
  170. package/util/subscription-set/expiring-sorted-set.js.map +1 -0
  171. package/util/subscription-set/redis-sorted-set.d.ts +9 -0
  172. package/util/subscription-set/redis-sorted-set.js +29 -0
  173. package/util/subscription-set/redis-sorted-set.js.map +1 -0
  174. package/util/subscription-set/subscription-set.d.ts +19 -0
  175. package/util/subscription-set/subscription-set.js +28 -0
  176. package/util/subscription-set/subscription-set.js.map +1 -0
  177. package/util/test-payload-loader.d.ts +26 -0
  178. package/util/test-payload-loader.js +85 -0
  179. package/util/test-payload-loader.js.map +1 -0
  180. package/validation/error.d.ts +48 -0
  181. package/validation/error.js +78 -0
  182. package/validation/error.js.map +1 -0
  183. package/validation/index.d.ts +5 -0
  184. package/validation/index.js +102 -0
  185. package/validation/index.js.map +1 -0
  186. package/validation/input-params.d.ts +14 -0
  187. package/validation/input-params.js +3 -0
  188. package/validation/input-params.js.map +1 -0
  189. package/validation/input-validator.d.ts +16 -0
  190. package/validation/input-validator.js +123 -0
  191. package/validation/input-validator.js.map +1 -0
  192. package/README.md +0 -102
@@ -0,0 +1,131 @@
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.RestTransport = void 0;
27
+ const util_1 = require("../util");
28
+ const error_1 = require("../validation/error");
29
+ const util_2 = require("./util");
30
+ const rateLimitMetrics = __importStar(require("../rate-limiting/metrics"));
31
+ const IN_FLIGHT_PREFIX = 'InFlight';
32
+ const logger = (0, util_1.makeLogger)('RestTransport');
33
+ /**
34
+ * Transport implementation that takes incoming requests, transforms them into a DataProvider request,
35
+ * and executes that request returning the response immediately from the `setup` function.
36
+ * Optionally, setting the `coalescing` option to `true` will make it so once a request is in flight,
37
+ * new adapter requests for the same feed will not trigger a new one, but return an empty promise from
38
+ * the setup instead so the normal cache polling mechanism is used.
39
+ *
40
+ * @typeParam T - Helper struct type that will be used to pass types to the generic parameters (check [[RestTransportGenerics]])
41
+ */
42
+ class RestTransport {
43
+ constructor(config) {
44
+ this.config = config;
45
+ }
46
+ async initialize(dependencies, config) {
47
+ this.inFlightPrefix = `${IN_FLIGHT_PREFIX}-`;
48
+ this.cache = dependencies.cache;
49
+ this.inFlightCache = dependencies.cache;
50
+ this.rateLimiter = dependencies.requestRateLimiter;
51
+ // Allow enabling/disabling request coalescing through env var
52
+ this.config.options.requestCoalescing.enabled = config.REQUEST_COALESCING_ENABLED;
53
+ this.config.options.requestCoalescing.entropyMax = config.REQUEST_COALESCING_ENTROPY_MAX;
54
+ }
55
+ async waitUntilUnderRateLimit(options, retry = 0) {
56
+ if (this.rateLimiter.isUnderLimits()) {
57
+ logger.trace('Incoming request would not be under limits, moving on');
58
+ return;
59
+ }
60
+ if (retry >= options.maxRetries) {
61
+ throw new error_1.AdapterError({
62
+ statusCode: 504,
63
+ message: `REST Transport timed out while waiting for rate limit availability (max retries: ${options.maxRetries})`,
64
+ });
65
+ }
66
+ logger.debug(`Request would be over rate limits, sleeping for ${options.msBetweenRetries}`);
67
+ await (0, util_1.sleep)(options.msBetweenRetries);
68
+ await this.waitUntilUnderRateLimit(options, retry + 1);
69
+ }
70
+ async foregroundExecute(req, config) {
71
+ // Add some entropy here because of possible scenario where the key won't be set before multiple
72
+ // other instances in a burst request try to access the coalescing key.
73
+ const randomMs = Math.random() * (this.config.options.requestCoalescing.entropyMax || 0);
74
+ await (0, util_1.sleep)(randomMs);
75
+ // Check if request is in flight if coalescing is enabled
76
+ const inFlight = this.config.options.requestCoalescing.enabled &&
77
+ (await this.cache.get(this.inFlightPrefix + req.requestContext.cacheKey));
78
+ if (inFlight) {
79
+ logger.debug('Request is in flight, transport has been set up');
80
+ return;
81
+ }
82
+ else if (this.config.options.requestCoalescing.enabled) {
83
+ // If it wasn't in flight and coalescing is disabled, register it as in flight
84
+ const ttl = config.REST_TRANSPORT_MAX_RATE_LIMIT_RETRIES *
85
+ config.REST_TRANSPORT_MS_BETWEEN_RATE_LIMIT_RETRIES;
86
+ logger.debug('Setting up rest transport, setting request in flight in cache');
87
+ await this.inFlightCache.set(this.inFlightPrefix + req.requestContext.cacheKey, true, ttl + 100); // Can't use Infinity for things like Redis
88
+ }
89
+ const request = await this.config.prepareRequest(req, config);
90
+ logger.trace('Check if we are under rate limits to perform request');
91
+ await this.waitUntilUnderRateLimit({
92
+ maxRetries: config.REST_TRANSPORT_MAX_RATE_LIMIT_RETRIES,
93
+ msBetweenRetries: config.REST_TRANSPORT_MS_BETWEEN_RATE_LIMIT_RETRIES,
94
+ });
95
+ logger.trace('Sending request to data provider...');
96
+ const providerResponse = await (0, util_2.axiosRequest)(request, config);
97
+ logger.debug(`Got response from provider, parsing (raw body: ${providerResponse.data})`);
98
+ const parsedResponse = await this.config.parseResponse(req, providerResponse, config);
99
+ if (config.API_VERBOSE) {
100
+ parsedResponse.data = providerResponse.data;
101
+ }
102
+ if (config.METRICS_ENABLED && config.EXPERIMENTAL_METRICS_ENABLED) {
103
+ parsedResponse.maxAge = Date.now() + config.CACHE_MAX_AGE;
104
+ parsedResponse.meta = {
105
+ metrics: { feedId: req.requestContext.meta?.metrics?.feedId || 'N/A' },
106
+ };
107
+ }
108
+ logger.debug('Setting provider response in cache');
109
+ await this.cache.set(req.requestContext.cacheKey, parsedResponse, config.CACHE_MAX_AGE);
110
+ // Record cost of data provider call
111
+ const cost = rateLimitMetrics.retrieveCost(providerResponse.data);
112
+ rateLimitMetrics.rateLimitCreditsSpentTotal
113
+ .labels({
114
+ feed_id: req.requestContext.meta?.metrics?.feedId || 'N/A',
115
+ participant_id: req.requestContext.cacheKey,
116
+ })
117
+ .inc(cost);
118
+ // Update cacheHit flag in request meta for metrics use
119
+ req.requestContext.meta = {
120
+ ...req.requestContext.meta,
121
+ metrics: { ...req.requestContext.meta?.metrics, cacheHit: false },
122
+ };
123
+ if (this.config.options.requestCoalescing.enabled) {
124
+ logger.debug('Set provider response in cache, removing in flight from cache');
125
+ await this.cache.delete(this.inFlightPrefix);
126
+ }
127
+ return parsedResponse;
128
+ }
129
+ }
130
+ exports.RestTransport = RestTransport;
131
+ //# sourceMappingURL=rest.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rest.js","sourceRoot":"","sources":["../../../src/transports/rest.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAIA,kCAA2C;AAG3C,+CAAkD;AAClD,iCAAqC;AACrC,2EAA4D;AAK5D,MAAM,gBAAgB,GAAG,UAAU,CAAA;AAEnC,MAAM,MAAM,GAAG,IAAA,iBAAU,EAAC,eAAe,CAAC,CAAA;AAuB1C;;;;;;;;GAQG;AACH,MAAa,aAAa;IAMxB,YACY,MAkBT;QAlBS,WAAM,GAAN,MAAM,CAkBf;IACA,CAAC;IAEJ,KAAK,CAAC,UAAU,CACd,YAAiC,EACjC,MAA0C;QAE1C,IAAI,CAAC,cAAc,GAAG,GA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,EAAE;SAClE,CAAA;QAED,IAAI,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,iBAAiB,CAAC,OAAO,EAAE;YACjD,MAAM,CAAC,KAAK,CAAC,+DAA+D,CAAC,CAAA;YAC7E,MAAM,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC,cAAc,CAAC,CAAA;SAC7C;QAED,OAAO,cAAc,CAAA;IACvB,CAAC;CACF;AArJD,sCAqJC"}
@@ -0,0 +1,21 @@
1
+ import { AdapterDependencies } from '../adapter';
2
+ import { AdapterConfig } from '../config';
3
+ import { AdapterRequest, AdapterResponse } from '../util/request';
4
+ import { MetaTransport, Transport, TransportGenerics } from './';
5
+ /**
6
+ * Transport implementation that takes 2 or more transports and a function that determines with transport to use.
7
+ *
8
+ * @typeParam T - Helper struct type that will be used to pass types to the generic parameters (check [[TransportGenerics]])
9
+ */
10
+ export declare class RoutingTransport<T extends TransportGenerics> implements MetaTransport<T> {
11
+ transports: {
12
+ [key: string]: Transport<T>;
13
+ };
14
+ private route;
15
+ constructor(transports: {
16
+ [key: string]: Transport<T>;
17
+ }, route: (req: AdapterRequest<T['Request']>, adapterConfig?: AdapterConfig<T['CustomSettings']>) => string);
18
+ initialize(dependencies: AdapterDependencies, adapterConfig: AdapterConfig<T['CustomSettings']>, endpointName: string): Promise<void>;
19
+ private resolveTransport;
20
+ foregroundExecute(req: AdapterRequest<T['Request']>, adapterConfig: AdapterConfig<T['CustomSettings']>): Promise<AdapterResponse<any> | void>;
21
+ }
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RoutingTransport = void 0;
4
+ const util_1 = require("../util");
5
+ const error_1 = require("../validation/error");
6
+ const logger = (0, util_1.makeLogger)('RoutingTransport');
7
+ /**
8
+ * Transport implementation that takes 2 or more transports and a function that determines with transport to use.
9
+ *
10
+ * @typeParam T - Helper struct type that will be used to pass types to the generic parameters (check [[TransportGenerics]])
11
+ */
12
+ class RoutingTransport {
13
+ constructor(
14
+ // This is public for tests, which sometimes need the underlying transport for things like ticking the clock
15
+ transports,
16
+ // Route should return to a string key that corresponds to a transport in the transports object
17
+ route) {
18
+ this.transports = transports;
19
+ this.route = route;
20
+ }
21
+ async initialize(dependencies, adapterConfig, endpointName) {
22
+ logger.debug(`Initializing ${Object.keys(this.transports).length} transports`);
23
+ Object.entries(this.transports).forEach(([k, v]) => {
24
+ logger.debug(`Initializing transport ${k} (${v.constructor.name})`);
25
+ v.initialize(dependencies, adapterConfig, endpointName);
26
+ });
27
+ }
28
+ resolveTransport(req, adapterConfig) {
29
+ logger.debug(`routing request using`, req.params);
30
+ const key = this.route(req, adapterConfig || undefined);
31
+ if (!this.transports[key]) {
32
+ throw new error_1.AdapterError({ statusCode: 400, message: `No transport found for ${key}` });
33
+ }
34
+ const transport = this.transports[key];
35
+ logger.debug(`Found ${transport.constructor.name} corresponding to ${key}`);
36
+ return transport;
37
+ }
38
+ // Allow AdapterResponse<any> below since we don't know what the adapter will return
39
+ /* eslint-disable @typescript-eslint/no-explicit-any */
40
+ async foregroundExecute(req, adapterConfig) {
41
+ const transport = this.resolveTransport(req, adapterConfig);
42
+ if (transport.foregroundExecute) {
43
+ logger.debug(`Running foregroundExecute on retrieved transport`);
44
+ return transport.foregroundExecute(req, adapterConfig);
45
+ }
46
+ logger.debug(`Retrieved transport doesn't implement foregroundExecute`);
47
+ }
48
+ }
49
+ exports.RoutingTransport = RoutingTransport;
50
+ //# sourceMappingURL=routing.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"routing.js","sourceRoot":"","sources":["../../../src/transports/routing.ts"],"names":[],"mappings":";;;AAEA,kCAAoC;AAEpC,+CAAkD;AAGlD,MAAM,MAAM,GAAG,IAAA,iBAAU,EAAC,kBAAkB,CAAC,CAAA;AAE7C;;;;GAIG;AACH,MAAa,gBAAgB;IAI3B;IACE,6GAA6G;IACtG,UAA2C;IAElD,+FAA+F;IACvF,KAGG;QANJ,eAAU,GA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D;QAEjD,MAAM,SAAS,GAAG,IAAI,CAAC,gBAAgB,CAAC,GAAG,EAAE,aAAa,CAAC,CAAA;QAC3D,IAAI,SAAS,CAAC,iBAAiB,EAAE;YAC/B,MAAM,CAAC,KAAK,CAAC,kDAAkD,CAAC,CAAA;YAChE,OAAO,SAAS,CAAC,iBAAiB,CAAC,GAAG,EAAE,aAAa,CAAC,CAAA;SACvD;QACD,MAAM,CAAC,KAAK,CAAC,yDAAyD,CAAC,CAAA;IACzE,CAAC;CACF;AAtDD,4CAsDC"}
@@ -0,0 +1,61 @@
1
+ import { AxiosRequestConfig } from 'axios';
2
+ import EventSource from 'eventsource';
3
+ import { AdapterContext, AdapterDependencies } from '../adapter';
4
+ import { Cache } from '../cache';
5
+ import { AdapterConfig } from '../config';
6
+ import { SubscriptionSet } from '../util';
7
+ import { AdapterRequest, AdapterResponse, ProviderResult } from '../util/request';
8
+ import { Transport, TransportGenerics } from './';
9
+ export interface SSEConfig {
10
+ url: string;
11
+ eventSourceInitDict?: EventSource.EventSourceInitDict;
12
+ }
13
+ /**
14
+ * Helper struct type that will be used to pass types to the generic parameters of a Transport.
15
+ * Extends the common TransportGenerics, adding Provider specific types for this Batch endpoint.
16
+ */
17
+ declare type SSETransportGenerics = TransportGenerics & {
18
+ /**
19
+ * Type details for any provider specific interfaces.
20
+ */
21
+ Provider: {
22
+ /**
23
+ * Structure of the body of the request that will be sent to the data provider.
24
+ */
25
+ RequestBody: unknown;
26
+ };
27
+ };
28
+ /**
29
+ * Transport implementation that establishes a long lived connection to a server using the SSE protocol and subcribes to updates.
30
+ *
31
+ * @typeParam T - Helper struct type that will be used to pass types to the generic parameters (check [[SSETransportGenerics]])
32
+ */
33
+ export declare class SSETransport<T extends SSETransportGenerics> implements Transport<T> {
34
+ private config;
35
+ EventSource: typeof EventSource;
36
+ cache: Cache<AdapterResponse<T['Response']>>;
37
+ eventListeners: {
38
+ type: string;
39
+ parseResponse: (evt: MessageEvent) => ProviderResult<T>;
40
+ }[];
41
+ sseConnection?: EventSource;
42
+ subscriptionSet: SubscriptionSet<T['Request']['Params']>;
43
+ timeOfLastReq: number;
44
+ localSubscriptions: T['Request']['Params'][];
45
+ constructor(config: {
46
+ prepareSSEConnectionConfig: (params: T['Request']['Params'][], context: AdapterContext<T>) => SSEConfig;
47
+ prepareKeepAliveRequest: (context: AdapterContext<T>) => AxiosRequestConfig<T['Provider']['RequestBody']>;
48
+ prepareSubscriptionRequest: (params: T['Request']['Params'][], context: AdapterContext<T>) => AxiosRequestConfig<T['Provider']['RequestBody']>;
49
+ prepareUnsubscriptionRequest: (params: T['Request']['Params'][], context: AdapterContext<T>) => AxiosRequestConfig<T['Provider']['RequestBody']>;
50
+ eventListeners: {
51
+ type: string;
52
+ parseResponse: (evt: MessageEvent) => ProviderResult<T>[];
53
+ }[];
54
+ keepaliveSleepMs?: number;
55
+ pollingSleepMs?: number;
56
+ });
57
+ initialize(dependencies: AdapterDependencies, config: AdapterConfig<T['CustomSettings']>, endpointName: string): Promise<void>;
58
+ registerRequest(req: AdapterRequest<T['Request']>, config: AdapterConfig<T['CustomSettings']>): Promise<void>;
59
+ backgroundExecute(context: AdapterContext<T>): Promise<number>;
60
+ }
61
+ export {};
@@ -0,0 +1,97 @@
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.SSETransport = void 0;
7
+ const axios_1 = __importDefault(require("axios"));
8
+ const eventsource_1 = __importDefault(require("eventsource"));
9
+ const util_1 = require("../util");
10
+ const _1 = require("./");
11
+ const logger = (0, util_1.makeLogger)('SSETransport');
12
+ /**
13
+ * Transport implementation that establishes a long lived connection to a server using the SSE protocol and subcribes to updates.
14
+ *
15
+ * @typeParam T - Helper struct type that will be used to pass types to the generic parameters (check [[SSETransportGenerics]])
16
+ */
17
+ class SSETransport {
18
+ constructor(config) {
19
+ this.config = config;
20
+ this.EventSource = eventsource_1.default;
21
+ this.timeOfLastReq = 0;
22
+ // The double sets serve to create a simple polling mechanism instead of needing a subscription
23
+ // This one would not; this is always local state
24
+ this.localSubscriptions = [];
25
+ }
26
+ async initialize(dependencies, config, endpointName) {
27
+ this.cache = dependencies.cache;
28
+ this.subscriptionSet = dependencies.subscriptionSetFactory.buildSet(endpointName);
29
+ if (dependencies.eventSource) {
30
+ this.EventSource = dependencies.eventSource;
31
+ }
32
+ }
33
+ async registerRequest(req, config) {
34
+ logger.debug(`Adding entry to subscription set (ttl: ${config.SSE_SUBSCRIPTION_TTL}): [${req.requestContext.cacheKey}] = ${req.requestContext.data}`);
35
+ await this.subscriptionSet.add(req.requestContext.cacheKey, req.requestContext.data, config.SSE_SUBSCRIPTION_TTL);
36
+ }
37
+ // Unlike cache warming, this execute will manage subscriptions
38
+ async backgroundExecute(context) {
39
+ logger.debug('Starting background execute, getting subscriptions from sorted set');
40
+ const desiredSubs = await this.subscriptionSet.getAll();
41
+ logger.debug('Generating delta (subscribes & unsubscribes)');
42
+ const subscribeParams = desiredSubs.filter((s) => !this.localSubscriptions.includes(s));
43
+ const unsubscribeParams = this.localSubscriptions.filter((s) => !desiredSubs.includes(s));
44
+ logger.debug(`${subscribeParams.length} new subscriptions; ${unsubscribeParams.length} to unsubscribe`);
45
+ if (subscribeParams.length) {
46
+ logger.trace(`Will subscribe to: ${subscribeParams}`);
47
+ }
48
+ if (unsubscribeParams.length) {
49
+ logger.trace(`Will unsubscribe to: ${unsubscribeParams}`);
50
+ }
51
+ if ((subscribeParams.length || unsubscribeParams.length) &&
52
+ (!this.sseConnection || this.sseConnection.readyState !== this.sseConnection.OPEN)) {
53
+ logger.debug('No established connection and new subscriptions available, connecting to SSE');
54
+ const sseConfig = this.config.prepareSSEConnectionConfig(subscribeParams, context);
55
+ this.sseConnection = new this.EventSource(sseConfig.url, sseConfig.eventSourceInitDict);
56
+ const eventHandlerGenerator = (listener) => {
57
+ return (e) => {
58
+ const providerResponses = listener.parseResponse(e);
59
+ const cacheEntries = (0, _1.buildCacheEntriesFromResults)(providerResponses, context);
60
+ this.cache.setMany(cacheEntries, context.adapterConfig.CACHE_MAX_AGE);
61
+ };
62
+ };
63
+ this.config.eventListeners.forEach((listener) => {
64
+ this.sseConnection?.addEventListener(listener.type, eventHandlerGenerator(listener));
65
+ });
66
+ }
67
+ const makeRequest = async (req) => {
68
+ try {
69
+ const res = await axios_1.default.request(req);
70
+ logger.debug(res.data, `response status ${res.statusText} from keepalive request`);
71
+ }
72
+ catch (err) {
73
+ logger.error(err, `Error on keepalive request`);
74
+ }
75
+ this.timeOfLastReq = Date.now();
76
+ };
77
+ if (subscribeParams.length) {
78
+ const subscribeRequest = this.config.prepareSubscriptionRequest(subscribeParams, context);
79
+ makeRequest(subscribeRequest);
80
+ }
81
+ if (unsubscribeParams.length) {
82
+ const unsubscribeRequest = this.config.prepareUnsubscriptionRequest(unsubscribeParams, context);
83
+ makeRequest(unsubscribeRequest);
84
+ }
85
+ if (desiredSubs.length &&
86
+ Date.now() - this.timeOfLastReq > context.adapterConfig.SSE_KEEPALIVE_SLEEP) {
87
+ const prepareKeepAliveRequest = this.config.prepareKeepAliveRequest(context);
88
+ makeRequest(prepareKeepAliveRequest);
89
+ }
90
+ logger.debug('Setting local state to cache value');
91
+ this.localSubscriptions = desiredSubs;
92
+ logger.debug('Background execute complete');
93
+ return context.adapterConfig.SSE_SUBSCRIPTION_UPDATE_SLEEP;
94
+ }
95
+ }
96
+ exports.SSETransport = SSETransport;
97
+ //# sourceMappingURL=sse.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sse.js","sourceRoot":"","sources":["../../../src/transports/sse.ts"],"names":[],"mappings":";;;;;;AAAA,kDAAiD;AACjD,8DAAqC;AAIrC,kCAAqD;AAErD,yBAA+E;AAE/E,MAAM,MAAM,GAAG,IAAA,iBAAU,EAAC,cAAc,CAAC,CAAA;AAuBzC;;;;GAIG;AACH,MAAa,YAAY;IAevB,YACU,MAsBP;QA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AAC,uBAAuB,CAAC,OAAO,CAAC,CAAA;YAC5E,WAAW,CAAC,uBAAuB,CAAC,CAAA;SACrC;QAED,MAAM,CAAC,KAAK,CAAC,oCAAoC,CAAC,CAAA;QAClD,IAAI,CAAC,kBAAkB,GAAG,WAAW,CAAA;QAErC,MAAM,CAAC,KAAK,CAAC,6BAA6B,CAAC,CAAA;QAC3C,OAAO,OAAO,CAAC,aAAa,CAAC,6BAA6B,CAAA;IAC5D,CAAC;CACF;AA9ID,oCA8IC"}
@@ -0,0 +1,9 @@
1
+ import { AxiosRequestConfig, AxiosResponse } from 'axios';
2
+ import { AdapterConfig, SettingsMap } from '../config';
3
+ /**
4
+ * Performs axios request along with metrics recording and error handling
5
+ *
6
+ * @param request - axios request config
7
+ * @returns axios response for the request
8
+ */
9
+ export declare function axiosRequest<ProviderRequestBody, ProviderResponseBody, CustomSettings extends SettingsMap>(request: AxiosRequestConfig<ProviderRequestBody>, config: AdapterConfig<CustomSettings>): Promise<AxiosResponse<ProviderResponseBody>>;
@@ -0,0 +1,87 @@
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.axiosRequest = void 0;
30
+ const axios_1 = __importDefault(require("axios"));
31
+ const error_1 = require("../validation/error");
32
+ const transportMetrics = __importStar(require("./metrics"));
33
+ /**
34
+ * Performs axios request along with metrics recording and error handling
35
+ *
36
+ * @param request - axios request config
37
+ * @returns axios response for the request
38
+ */
39
+ async function axiosRequest(request, config) {
40
+ const responseTimer = transportMetrics.dataProviderRequestDurationSeconds.startTimer();
41
+ let providerResponse;
42
+ try {
43
+ request.timeout = config.API_TIMEOUT;
44
+ providerResponse = await axios_1.default.request(request);
45
+ }
46
+ catch (e) {
47
+ const error = e;
48
+ // Request error
49
+ let providerStatusCode;
50
+ let adapterError;
51
+ if (error.code === 'ECONNABORTED') {
52
+ adapterError = new error_1.AdapterTimeoutError({});
53
+ providerStatusCode = error?.response?.status ?? 504;
54
+ adapterError.name = 'Data Provider Request Timeout error';
55
+ }
56
+ else if (error?.response?.status) {
57
+ adapterError = new error_1.AdapterDataProviderError({});
58
+ providerStatusCode = error?.response?.status;
59
+ }
60
+ else {
61
+ adapterError = new error_1.AdapterConnectionError({});
62
+ providerStatusCode = 0; // 0 -> connection error
63
+ }
64
+ // Record count of failed data provider request
65
+ transportMetrics.dataProviderRequests
66
+ .labels(transportMetrics.dataProviderMetricsLabel(providerStatusCode, request.method))
67
+ .inc();
68
+ adapterError.statusCode = 200;
69
+ adapterError.providerStatusCode = providerStatusCode;
70
+ adapterError.message = error?.message;
71
+ adapterError.cause = error;
72
+ adapterError.errorResponse = error?.response?.data;
73
+ adapterError.url = request.url;
74
+ throw adapterError;
75
+ }
76
+ finally {
77
+ // Record time taken for data provider request for success or failure
78
+ responseTimer();
79
+ }
80
+ // Record count of successful data provider requests
81
+ transportMetrics.dataProviderRequests
82
+ .labels(transportMetrics.dataProviderMetricsLabel(providerResponse.status, request.method))
83
+ .inc();
84
+ return providerResponse;
85
+ }
86
+ exports.axiosRequest = axiosRequest;
87
+ //# sourceMappingURL=util.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"util.js","sourceRoot":"","sources":["../../../src/transports/util.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,kDAA4E;AAE5E,+CAK4B;AAC5B,4DAA6C;AAE7C;;;;;GAKG;AACI,KAAK,UAAU,YAAY,CAKhC,OAAgD,EAChD,MAAqC;IAErC,MAAM,aAAa,GAAG,gBAAgB,CAAC,kCAAkC,CAAC,UAAU,EAAE,CAAA;IACtF,IAAI,gBAAqD,CAAA;IACzD,IAAI;QACF,OAAO,CAAC,OAAO,GAAG,MAAM,CAAC,WAAW,CAAA;QACpC,gBAAgB,GAAG,MAAM,eAAK,CAAC,OAAO,CAAuB,OAAO,CAAC,CAAA;KACtE;IAAC,OAAO,CAAU,EAAE;QACnB,MAAM,KAAK,GAAG,CAAe,CAAA;QAC7B,gBAAgB;QAChB,IAAI,kBAAsC,CAAA;QAC1C,IAAI,YAA0B,CAAA;QAC9B,IAAI,KAAK,CAAC,IAAI,KAAK,cAAc,EAAE;YACjC,YAAY,GAAG,IAAI,2BAAmB,CAAC,EAAE,CAAC,CAAA;YAC1C,kBAAkB,GAAG,KAAK,EAAE,QAAQ,EAAE,MAAM,IAAI,GAAG,CAAA;YACnD,YAAY,CAAC,IAAI,GAAG,qCAAqC,CAAA;SAC1D;aAAM,IAAI,KAAK,EAAE,QAAQ,EAAE,MAAM,EAAE;YAClC,YAAY,GAAG,IAAI,gCAAwB,CAAC,EAAE,CAAC,CAAA;YAC/C,kBAAkB,GAAG,KAAK,EAAE,QAAQ,EAAE,MAAM,CAAA;SAC7C;aAAM;YACL,YAAY,GAAG,IAAI,8BAAsB,CAAC,EAAE,CAAC,CAAA;YAC7C,kBAAkB,GAAG,CAAC,CAAA,CAAC,wBAAwB;SAChD;QACD,+CAA+C;QAC/C,gBAAgB,CAAC,oBAAoB;aAClC,MAAM,CAAC,gBAAgB,CAAC,wBAAwB,CAAC,kBAAkB,EAAE,OAAO,CAAC,MAAM,CAAC,CAAC;aACrF,GAAG,EAAE,CAAA;QAER,YAAY,CAAC,UAAU,GAAG,GAAG,CAAA;QAC7B,YAAY,CAAC,kBAAkB,GAAG,kBAAkB,CAAA;QACpD,YAAY,CAAC,OAAO,GAAG,KAAK,EAAE,OAAO,CAAA;QACrC,YAAY,CAAC,KAAK,GAAG,KAAK,CAAA;QAC1B,YAAY,CAAC,aAAa,GAAG,KAAK,EAAE,QAAQ,EAAE,IAAI,CAAA;QAClD,YAAY,CAAC,GAAG,GAAG,OAAO,CAAC,GAAG,CAAA;QAE9B,MAAM,YAAY,CAAA;KACnB;YAAS;QACR,qEAAqE;QACrE,aAAa,EAAE,CAAA;KAChB;IAED,oDAAoD;IACpD,gBAAgB,CAAC,oBAAoB;SAClC,MAAM,CAAC,gBAAgB,CAAC,wBAAwB,CAAC,gBAAgB,CAAC,MAAM,EAAE,OAAO,CAAC,MAAM,CAAC,CAAC;SAC1F,GAAG,EAAE,CAAA;IAER,OAAO,gBAAgB,CAAA;AACzB,CAAC;AArDD,oCAqDC"}
@@ -0,0 +1,95 @@
1
+ import WebSocket, { ClientOptions, RawData } from 'ws';
2
+ import { AdapterContext, AdapterDependencies } from '../adapter';
3
+ import { Cache } from '../cache';
4
+ import { AdapterConfig } from '../config';
5
+ import { BackgroundExecuteRateLimiter } from '../rate-limiting';
6
+ import { SubscriptionSet } from '../util';
7
+ import { AdapterRequest, AdapterResponse, ProviderResult } from '../util/request';
8
+ import { Transport, TransportGenerics } from './';
9
+ export { WebSocket, RawData as WebSocketRawData };
10
+ declare type WebSocketClass = new (url: string, protocols?: string | string[] | undefined, options?: ClientOptions) => 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<T extends WebsocketTransportGenerics> {
20
+ /** Endpoint to which to open the WS connection*/
21
+ url: (context: AdapterContext<T>) => string;
22
+ /** Optional parameters used when establishing the WebSocket connection */
23
+ options?: (context: AdapterContext<T>) => ClientOptions;
24
+ /** Map of handlers for different WS lifecycle events */
25
+ handlers: {
26
+ /**
27
+ * Handles when the WS is successfully opened
28
+ *
29
+ * @param wsConnection - the WebSocket with an established connection
30
+ * @returns an empty Promise, or void
31
+ */
32
+ open: (wsConnection: WebSocket, context: AdapterContext<T>) => Promise<void> | void;
33
+ /**
34
+ * Handles when the WS receives a message
35
+ *
36
+ * @param message - the message received by the WS
37
+ * @param context - the background context for the Adapter
38
+ * @returns a list of cache entries of adapter responses to set in the cache
39
+ */
40
+ message: (message: T['Provider']['WsMessage'], context: AdapterContext<T>) => ProviderResult<T>[] | undefined;
41
+ };
42
+ /** Map of "builders", functions that will be used to prepare specific WS messages */
43
+ builders: {
44
+ /**
45
+ * Builds a WS message that will be sent to subscribe to a specific feed
46
+ *
47
+ * @param params - the body of the adapter request
48
+ * @returns the WS message (can be any type as long as the [[WebSocket]] doesn't complain)
49
+ */
50
+ subscribeMessage: (params: T['Request']['Params']) => unknown;
51
+ /**
52
+ * Builds a WS message that will be sent to unsubscribe to a specific feed
53
+ *
54
+ * @param params - the body of the adapter request
55
+ * @returns the WS message (can be any type as long as the [[WebSocket]] doesn't complain)
56
+ */
57
+ unsubscribeMessage: (params: T['Request']['Params']) => unknown;
58
+ };
59
+ }
60
+ /**
61
+ * Helper struct type that will be used to pass types to the generic parameters of a Transport.
62
+ * Extends the common TransportGenerics, adding Provider specific types for this Batch endpoint.
63
+ */
64
+ declare type WebsocketTransportGenerics = TransportGenerics & {
65
+ /**
66
+ * Type details for any provider specific interfaces.
67
+ */
68
+ Provider: {
69
+ /**
70
+ * Structure of any message that will come through the websocket connection.
71
+ */
72
+ WsMessage: unknown;
73
+ };
74
+ };
75
+ /**
76
+ * Transport implementation that takes incoming requests, adds them to an [[subscriptionSet]] and,
77
+ * through a WebSocket connection, subscribes to the relevant feeds to populate the cache.
78
+ *
79
+ * @typeParam T - Helper struct type that will be used to pass types to the generic parameters (check [[WebsocketTransportGenerics]])
80
+ */
81
+ export declare class WebSocketTransport<T extends WebsocketTransportGenerics> implements Transport<T> {
82
+ private config;
83
+ cache: Cache<AdapterResponse<T['Response']>>;
84
+ rateLimiter: BackgroundExecuteRateLimiter;
85
+ subscriptionSet: SubscriptionSet<T['Request']['Params']>;
86
+ localSubscriptions: T['Request']['Params'][];
87
+ wsConnection: WebSocket;
88
+ constructor(config: WebSocketTransportConfig<T>);
89
+ initialize(dependencies: AdapterDependencies, config: AdapterConfig<T['CustomSettings']>, endpointName: string): Promise<void>;
90
+ registerRequest(req: AdapterRequest<T['Request']>, config: AdapterConfig<T['CustomSettings']>): Promise<void>;
91
+ serializeMessage(payload: unknown): string;
92
+ deserializeMessage(data: WebSocket.Data): T['Provider']['WsMessage'];
93
+ establishWsConnection(context: AdapterContext<T>): Promise<unknown>;
94
+ backgroundExecute(context: AdapterContext<T>): Promise<number>;
95
+ }