@chainlink/external-adapter-framework 0.9.0 → 0.10.0

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 (151) hide show
  1. package/README.md +67 -0
  2. package/adapter/basic.d.ts +50 -6
  3. package/adapter/basic.js +173 -4
  4. package/adapter/basic.js.map +1 -1
  5. package/adapter/endpoint.d.ts +6 -6
  6. package/adapter/endpoint.js +1 -0
  7. package/adapter/endpoint.js.map +1 -1
  8. package/adapter/price.d.ts +38 -8
  9. package/adapter/price.js +43 -1
  10. package/adapter/price.js.map +1 -1
  11. package/adapter/types.d.ts +40 -15
  12. package/background-executor.js +21 -5
  13. package/background-executor.js.map +1 -1
  14. package/cache/index.d.ts +11 -17
  15. package/cache/index.js +9 -51
  16. package/cache/index.js.map +1 -1
  17. package/config/index.d.ts +28 -11
  18. package/config/index.js +63 -46
  19. package/config/index.js.map +1 -1
  20. package/examples/bank-frick/accounts.d.ts +17 -25
  21. package/examples/bank-frick/accounts.js +23 -13
  22. package/examples/bank-frick/accounts.js.map +1 -1
  23. package/examples/bank-frick/config/index.d.ts +5 -0
  24. package/examples/bank-frick/config/index.js +5 -0
  25. package/examples/bank-frick/config/index.js.map +1 -1
  26. package/examples/bank-frick/index.d.ts +1 -15
  27. package/examples/bank-frick/index.js +1 -1
  28. package/examples/coingecko/src/config/index.d.ts +7 -0
  29. package/examples/coingecko/src/config/index.js +8 -1
  30. package/examples/coingecko/src/config/index.js.map +1 -1
  31. package/examples/coingecko/src/crypto-utils.d.ts +19 -4
  32. package/examples/coingecko/src/crypto-utils.js +1 -2
  33. package/examples/coingecko/src/crypto-utils.js.map +1 -1
  34. package/examples/coingecko/src/endpoint/coins.d.ts +20 -3
  35. package/examples/coingecko/src/endpoint/coins.js +2 -2
  36. package/examples/coingecko/src/endpoint/coins.js.map +1 -1
  37. package/examples/coingecko/src/endpoint/crypto-marketcap.d.ts +2 -2
  38. package/examples/coingecko/src/endpoint/crypto-marketcap.js +2 -2
  39. package/examples/coingecko/src/endpoint/crypto-marketcap.js.map +1 -1
  40. package/examples/coingecko/src/endpoint/crypto-volume.d.ts +2 -2
  41. package/examples/coingecko/src/endpoint/crypto-volume.js +2 -2
  42. package/examples/coingecko/src/endpoint/crypto-volume.js.map +1 -1
  43. package/examples/coingecko/src/endpoint/crypto.d.ts +2 -4
  44. package/examples/coingecko/src/endpoint/crypto.js +2 -2
  45. package/examples/coingecko/src/endpoint/crypto.js.map +1 -1
  46. package/examples/coingecko/src/endpoint/dominance.d.ts +2 -2
  47. package/examples/coingecko/src/endpoint/dominance.js +2 -2
  48. package/examples/coingecko/src/endpoint/dominance.js.map +1 -1
  49. package/examples/coingecko/src/endpoint/global-marketcap.d.ts +2 -2
  50. package/examples/coingecko/src/endpoint/global-marketcap.js +2 -2
  51. package/examples/coingecko/src/endpoint/global-marketcap.js.map +1 -1
  52. package/examples/coingecko/src/global-utils.d.ts +18 -3
  53. package/examples/coingecko/src/global-utils.js.map +1 -1
  54. package/examples/coingecko/src/index.js +3 -1
  55. package/examples/coingecko/src/index.js.map +1 -1
  56. package/examples/cryptocompare/src/config/index.d.ts +7 -0
  57. package/examples/cryptocompare/src/config/index.js +8 -1
  58. package/examples/cryptocompare/src/config/index.js.map +1 -1
  59. package/examples/cryptocompare/src/endpoints/crypto.d.ts +14 -2
  60. package/examples/cryptocompare/src/endpoints/crypto.js.map +1 -1
  61. package/examples/cryptocompare/src/index.js +3 -1
  62. package/examples/cryptocompare/src/index.js.map +1 -1
  63. package/examples/genesis/config/index.d.ts +5 -0
  64. package/examples/genesis/config/index.js +5 -0
  65. package/examples/genesis/config/index.js.map +1 -1
  66. package/examples/genesis/index.js +1 -1
  67. package/examples/genesis/sseStream.d.ts +13 -2
  68. package/examples/genesis/sseStream.js +4 -1
  69. package/examples/genesis/sseStream.js.map +1 -1
  70. package/index.d.ts +1 -0
  71. package/index.js +4 -6
  72. package/index.js.map +1 -1
  73. package/metrics/index.d.ts +2 -0
  74. package/metrics/index.js +5 -4
  75. package/metrics/index.js.map +1 -1
  76. package/metrics/util.d.ts +5 -5
  77. package/metrics/util.js +2 -2
  78. package/metrics/util.js.map +1 -1
  79. package/package.json +7 -9
  80. package/rate-limiting/background/fixed-frequency.d.ts +2 -3
  81. package/rate-limiting/background/fixed-frequency.js.map +1 -1
  82. package/rate-limiting/index.d.ts +4 -4
  83. package/rate-limiting/index.js +1 -1
  84. package/rate-limiting/index.js.map +1 -1
  85. package/rate-limiting/request/simple-counting.d.ts +2 -3
  86. package/rate-limiting/request/simple-counting.js.map +1 -1
  87. package/transports/batch-warming.d.ts +40 -18
  88. package/transports/batch-warming.js +45 -18
  89. package/transports/batch-warming.js.map +1 -1
  90. package/transports/index.d.ts +65 -31
  91. package/transports/index.js +2 -59
  92. package/transports/index.js.map +1 -1
  93. package/transports/metrics.d.ts +3 -3
  94. package/transports/metrics.js +2 -2
  95. package/transports/metrics.js.map +1 -1
  96. package/transports/rest.d.ts +42 -30
  97. package/transports/rest.js +25 -30
  98. package/transports/rest.js.map +1 -1
  99. package/transports/routing.d.ts +21 -0
  100. package/transports/routing.js +50 -0
  101. package/transports/routing.js.map +1 -0
  102. package/transports/sse.d.ts +40 -20
  103. package/transports/sse.js +10 -8
  104. package/transports/sse.js.map +1 -1
  105. package/transports/util.d.ts +3 -1
  106. package/transports/util.js +69 -39
  107. package/transports/util.js.map +1 -1
  108. package/transports/websocket.d.ts +41 -26
  109. package/transports/websocket.js +16 -19
  110. package/transports/websocket.js.map +1 -1
  111. package/util/logger.d.ts +2 -0
  112. package/util/logger.js +4 -7
  113. package/util/logger.js.map +1 -1
  114. package/util/request.d.ts +46 -10
  115. package/util/subscription-set/subscription-set.d.ts +1 -1
  116. package/util/subscription-set/subscription-set.js +1 -1
  117. package/util/subscription-set/subscription-set.js.map +1 -1
  118. package/util/test-payload-loader.d.ts +1 -0
  119. package/util/test-payload-loader.js +2 -1
  120. package/util/test-payload-loader.js.map +1 -1
  121. package/validation/error.d.ts +1 -3
  122. package/validation/error.js +2 -4
  123. package/validation/error.js.map +1 -1
  124. package/validation/index.js +28 -18
  125. package/validation/index.js.map +1 -1
  126. package/validation/input-params.d.ts +0 -1
  127. package/validation/input-params.js +0 -28
  128. package/validation/input-params.js.map +1 -1
  129. package/validation/input-validator.js +1 -2
  130. package/validation/input-validator.js.map +1 -1
  131. package/examples/coingecko-old/batch-warming.d.ts +0 -7
  132. package/examples/coingecko-old/batch-warming.js +0 -54
  133. package/examples/coingecko-old/batch-warming.js.map +0 -1
  134. package/examples/coingecko-old/index.d.ts +0 -2
  135. package/examples/coingecko-old/index.js +0 -12
  136. package/examples/coingecko-old/index.js.map +0 -1
  137. package/examples/coingecko-old/rest.d.ts +0 -12
  138. package/examples/coingecko-old/rest.js +0 -55
  139. package/examples/coingecko-old/rest.js.map +0 -1
  140. package/examples/ncfx/config/index.d.ts +0 -12
  141. package/examples/ncfx/config/index.js +0 -16
  142. package/examples/ncfx/config/index.js.map +0 -1
  143. package/examples/ncfx/index.d.ts +0 -13
  144. package/examples/ncfx/index.js +0 -12
  145. package/examples/ncfx/index.js.map +0 -1
  146. package/examples/ncfx/websocket.d.ts +0 -47
  147. package/examples/ncfx/websocket.js +0 -74
  148. package/examples/ncfx/websocket.js.map +0 -1
  149. package/validation/override-functions.d.ts +0 -3
  150. package/validation/override-functions.js +0 -41
  151. package/validation/override-functions.js.map +0 -1
@@ -37,9 +37,7 @@ const logger = (0, util_1.makeLogger)('RestTransport');
37
37
  * new adapter requests for the same feed will not trigger a new one, but return an empty promise from
38
38
  * the setup instead so the normal cache polling mechanism is used.
39
39
  *
40
- * @typeParam AdapterParams - interface for the adapter request body
41
- * @typeParam ProviderRequestBody - interface for the body of the request to the Data Provider
42
- * @typeParam ProviderResponseBody - interface for the body of the Data Provider's response
40
+ * @typeParam T - Helper struct type that will be used to pass types to the generic parameters (check [[RestTransportGenerics]])
43
41
  */
44
42
  class RestTransport {
45
43
  constructor(config) {
@@ -48,30 +46,12 @@ class RestTransport {
48
46
  async initialize(dependencies, config) {
49
47
  this.inFlightPrefix = `${IN_FLIGHT_PREFIX}-`;
50
48
  this.cache = dependencies.cache;
49
+ this.inFlightCache = dependencies.cache;
51
50
  this.rateLimiter = dependencies.requestRateLimiter;
52
51
  // Allow enabling/disabling request coalescing through env var
53
52
  this.config.options.requestCoalescing.enabled = config.REQUEST_COALESCING_ENABLED;
54
53
  this.config.options.requestCoalescing.entropyMax = config.REQUEST_COALESCING_ENTROPY_MAX;
55
54
  }
56
- async hasBeenSetUp(req) {
57
- if (!this.config.options.requestCoalescing.enabled) {
58
- return false;
59
- }
60
- // Add some entropy here because of possible scenario where the key won't be set before multiple
61
- // other instances in a burst request try to access the coalescing key.
62
- const randomMs = Math.random() * (this.config.options.requestCoalescing.entropyMax || 0);
63
- await (0, util_1.sleep)(randomMs);
64
- // Check if request is in flight
65
- const inFlight = await this.cache.get(this.inFlightPrefix + req.requestContext.cacheKey);
66
- if (inFlight) {
67
- logger.debug('Request is in flight, transport has been set up');
68
- return true;
69
- }
70
- else {
71
- logger.debug('Request not in flight, transport not set up');
72
- return false;
73
- }
74
- }
75
55
  async waitUntilUnderRateLimit(options, retry = 0) {
76
56
  if (this.rateLimiter.isUnderLimits()) {
77
57
  logger.trace('Incoming request would not be under limits, moving on');
@@ -87,21 +67,36 @@ class RestTransport {
87
67
  await (0, util_1.sleep)(options.msBetweenRetries);
88
68
  await this.waitUntilUnderRateLimit(options, retry + 1);
89
69
  }
90
- async setup(req, config) {
91
- if (this.config.options.requestCoalescing.enabled) {
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
92
84
  const ttl = config.REST_TRANSPORT_MAX_RATE_LIMIT_RETRIES *
93
85
  config.REST_TRANSPORT_MS_BETWEEN_RATE_LIMIT_RETRIES;
94
86
  logger.debug('Setting up rest transport, setting request in flight in cache');
95
- await this.cache.set(this.inFlightPrefix + req.requestContext.cacheKey, true, ttl + 100); // Can't use Infinity for things like Redis
87
+ await this.inFlightCache.set(this.inFlightPrefix + req.requestContext.cacheKey, true, ttl + 100); // Can't use Infinity for things like Redis
96
88
  }
97
89
  const request = await this.config.prepareRequest(req, config);
98
90
  logger.trace('Check if we are under rate limits to perform request');
99
- await this.waitUntilUnderRateLimit({
100
- maxRetries: config.REST_TRANSPORT_MAX_RATE_LIMIT_RETRIES,
101
- msBetweenRetries: config.REST_TRANSPORT_MS_BETWEEN_RATE_LIMIT_RETRIES,
102
- });
91
+ const checkForRateLimit = async () => {
92
+ return this.waitUntilUnderRateLimit({
93
+ maxRetries: config.REST_TRANSPORT_MAX_RATE_LIMIT_RETRIES,
94
+ msBetweenRetries: config.REST_TRANSPORT_MS_BETWEEN_RATE_LIMIT_RETRIES,
95
+ });
96
+ };
97
+ await checkForRateLimit();
103
98
  logger.trace('Sending request to data provider...');
104
- const providerResponse = await (0, util_2.axiosRequest)(request, config);
99
+ const providerResponse = await (0, util_2.axiosRequest)(request, config, checkForRateLimit);
105
100
  logger.debug(`Got response from provider, parsing (raw body: ${providerResponse.data})`);
106
101
  const parsedResponse = await this.config.parseResponse(req, providerResponse, config);
107
102
  if (config.API_VERBOSE) {
@@ -1 +1 @@
1
- {"version":3,"file":"rest.js","sourceRoot":"","sources":["../../../src/transports/rest.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAIA,kCAA2C;AAG3C,+CAAkD;AAClD,iCAAqC;AACrC,2EAA4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AAE,KAAK,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;AApID,sCAoIC"}
@@ -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,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jC,aAAiD;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"}
@@ -1,41 +1,61 @@
1
- import { Cache } from '../cache';
2
- import EventSource from 'eventsource';
3
- import { SettingsMap, AdapterConfig } from '../config';
4
1
  import { AxiosRequestConfig } from 'axios';
2
+ import EventSource from 'eventsource';
3
+ import { EndpointContext, AdapterDependencies } from '../adapter';
4
+ import { Cache } from '../cache';
5
+ import { AdapterConfig } from '../config';
5
6
  import { SubscriptionSet } from '../util';
6
- import { AdapterRequest, ProviderResult } from '../util/request';
7
- import { Transport } from './';
8
- import { AdapterContext, AdapterDependencies } from '../adapter';
7
+ import { AdapterRequest, AdapterResponse, ProviderResult } from '../util/request';
8
+ import { Transport, TransportGenerics } from './';
9
9
  export interface SSEConfig {
10
10
  url: string;
11
11
  eventSourceInitDict?: EventSource.EventSourceInitDict;
12
12
  }
13
- export declare class SSETransport<AdapterParams, ProviderRequestBody, ProviderResponseBody, CustomSettings extends SettingsMap> implements Transport<AdapterParams, ProviderResponseBody, CustomSettings> {
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> {
14
34
  private config;
15
35
  EventSource: typeof EventSource;
16
- cache: Cache;
36
+ cache: Cache<AdapterResponse<T['Response']>>;
17
37
  eventListeners: {
18
38
  type: string;
19
- parseResponse: (evt: MessageEvent) => ProviderResult<AdapterParams>;
39
+ parseResponse: (evt: MessageEvent) => ProviderResult<T>;
20
40
  }[];
21
41
  sseConnection?: EventSource;
22
- subscriptionSet: SubscriptionSet<AdapterParams>;
42
+ subscriptionSet: SubscriptionSet<T['Request']['Params']>;
23
43
  timeOfLastReq: number;
24
- localSubscriptions: AdapterParams[];
44
+ localSubscriptions: T['Request']['Params'][];
25
45
  constructor(config: {
26
- prepareSSEConnectionConfig: (params: AdapterParams[], context: AdapterContext<CustomSettings>) => SSEConfig;
27
- prepareKeepAliveRequest: (context: AdapterContext<CustomSettings>) => AxiosRequestConfig<ProviderRequestBody>;
28
- prepareSubscriptionRequest: (params: AdapterParams[], context: AdapterContext<CustomSettings>) => AxiosRequestConfig<ProviderRequestBody>;
29
- prepareUnsubscriptionRequest: (params: AdapterParams[], context: AdapterContext<CustomSettings>) => AxiosRequestConfig<ProviderRequestBody>;
46
+ prepareSSEConnectionConfig: (params: T['Request']['Params'][], context: EndpointContext<T>) => SSEConfig;
47
+ prepareKeepAliveRequest?: (context: EndpointContext<T>) => AxiosRequestConfig<T['Provider']['RequestBody']>;
48
+ prepareSubscriptionRequest: (params: T['Request']['Params'][], context: EndpointContext<T>) => AxiosRequestConfig<T['Provider']['RequestBody']>;
49
+ prepareUnsubscriptionRequest: (params: T['Request']['Params'][], context: EndpointContext<T>) => AxiosRequestConfig<T['Provider']['RequestBody']>;
30
50
  eventListeners: {
31
51
  type: string;
32
- parseResponse: (evt: MessageEvent) => ProviderResult<AdapterParams>[];
52
+ parseResponse: (evt: MessageEvent) => ProviderResult<T>[];
33
53
  }[];
34
54
  keepaliveSleepMs?: number;
35
55
  pollingSleepMs?: number;
36
56
  });
37
- initialize(dependencies: AdapterDependencies, config: AdapterConfig<SettingsMap>, endpointName: string): Promise<void>;
38
- hasBeenSetUp(): Promise<boolean>;
39
- setup(req: AdapterRequest<AdapterParams>, config: AdapterConfig<CustomSettings>): Promise<void>;
40
- backgroundExecute(context: AdapterContext<CustomSettings>): Promise<number>;
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: EndpointContext<T>): Promise<number>;
41
60
  }
61
+ export {};
package/transports/sse.js CHANGED
@@ -4,11 +4,16 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.SSETransport = void 0;
7
- const eventsource_1 = __importDefault(require("eventsource"));
8
7
  const axios_1 = __importDefault(require("axios"));
8
+ const eventsource_1 = __importDefault(require("eventsource"));
9
9
  const util_1 = require("../util");
10
10
  const _1 = require("./");
11
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
+ */
12
17
  class SSETransport {
13
18
  constructor(config) {
14
19
  this.config = config;
@@ -25,11 +30,8 @@ class SSETransport {
25
30
  this.EventSource = dependencies.eventSource;
26
31
  }
27
32
  }
28
- async hasBeenSetUp() {
29
- return false;
30
- }
31
- async setup(req, config) {
32
- logger.debug(`Adding entry to subscription set: [${req.requestContext.cacheKey}] = ${req.requestContext.data}`);
33
+ async registerRequest(req, config) {
34
+ logger.debug(`Adding entry to subscription set (ttl: ${config.SSE_SUBSCRIPTION_TTL}): [${req.requestContext.cacheKey}] = ${req.requestContext.data}`);
33
35
  await this.subscriptionSet.add(req.requestContext.cacheKey, req.requestContext.data, config.SSE_SUBSCRIPTION_TTL);
34
36
  }
35
37
  // Unlike cache warming, this execute will manage subscriptions
@@ -37,7 +39,6 @@ class SSETransport {
37
39
  logger.debug('Starting background execute, getting subscriptions from sorted set');
38
40
  const desiredSubs = await this.subscriptionSet.getAll();
39
41
  logger.debug('Generating delta (subscribes & unsubscribes)');
40
- // TODO: More efficient algorithm, this is really easy to read, but high(er) time complexity
41
42
  const subscribeParams = desiredSubs.filter((s) => !this.localSubscriptions.includes(s));
42
43
  const unsubscribeParams = this.localSubscriptions.filter((s) => !desiredSubs.includes(s));
43
44
  logger.debug(`${subscribeParams.length} new subscriptions; ${unsubscribeParams.length} to unsubscribe`);
@@ -81,7 +82,8 @@ class SSETransport {
81
82
  const unsubscribeRequest = this.config.prepareUnsubscriptionRequest(unsubscribeParams, context);
82
83
  makeRequest(unsubscribeRequest);
83
84
  }
84
- if (desiredSubs.length &&
85
+ if (this.config.prepareKeepAliveRequest &&
86
+ desiredSubs.length &&
85
87
  Date.now() - this.timeOfLastReq > context.adapterConfig.SSE_KEEPALIVE_SLEEP) {
86
88
  const prepareKeepAliveRequest = this.config.prepareKeepAliveRequest(context);
87
89
  makeRequest(prepareKeepAliveRequest);
@@ -1 +1 @@
1
- {"version":3,"file":"sse.js","sourceRoot":"","sources":["../../../src/transports/sse.ts"],"names":[],"mappings":";;;;;;AACA,8DAAqC;AAErC,kDAAiD;AACjD,kCAAqD;AAErD,yBAA4D;AAG5D,MAAM,MAAM,GAAG,IAAA,iBAAU,EAAC,cAAc,CAAC,CAAA;AAOzC,MAAa,YAAY;IAqBvB,YACU,MAsBP;QAtBO,WAAM,GAAN,MAAM,CAsBb;QArCH,gBAAW,GAAuB,qBAAW,CAAA;QAQ7C,kBAAa,GAAG,CAAC,CAAA;QAEjB,+FAA+F;QAC/F,iDAAiD;QACjD,uBAAkB,GAAoB,EAAE,CAA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MAAM,CAAC,0BAA0B,CAAC,eAAe,EAAE,OAAO,CAAC,CAAA;YACzF,WAAW,CAAC,gBAAgB,CAAC,CAAA;SAC9B;QACD,IAAI,iBAAiB,CAAC,MAAM,EAAE;YAC5B,MAAM,kBAAkB,GAAG,IAAI,CAAC,MAAM,CAAC,4BAA4B,CACjE,iBAAiB,EACjB,OAAO,CACR,CAAA;YACD,WAAW,CAAC,kBAAkB,CAAC,CAAA;SAChC;QACD,IACE,IAAI,CAAC,MAAM,CAAC,uBAAuB;YACnC,WAAW,CAAC,MAAM;YAClB,IAAI,CAAC,GAAG,EAAE,GAAG,IAAI,CAAC,aAAa,GAAG,OAAO,CAAC,aAAa,CAAC,mBAAmB,EAC3E;YACA,MAAM,uBAAuB,GAAG,IAAI,CAAC,MAAM,CAAC,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;AA/ID,oCA+IC"}
@@ -4,6 +4,8 @@ import { AdapterConfig, SettingsMap } from '../config';
4
4
  * Performs axios request along with metrics recording and error handling
5
5
  *
6
6
  * @param request - axios request config
7
+ * @param config - the adapter config containing the env vars
8
+ * @param checkForRateLimit - async function to check and wait for rate limit
7
9
  * @returns axios response for the request
8
10
  */
9
- export declare function axiosRequest<ProviderRequestBody, ProviderResponseBody, CustomSettings extends SettingsMap>(request: AxiosRequestConfig<ProviderRequestBody>, config: AdapterConfig<CustomSettings>): Promise<AxiosResponse<ProviderResponseBody>>;
11
+ export declare function axiosRequest<ProviderRequestBody, ProviderResponseBody, CustomSettings extends SettingsMap>(request: AxiosRequestConfig<ProviderRequestBody>, config: AdapterConfig<CustomSettings>, checkForRateLimit?: () => Promise<void>): Promise<AxiosResponse<ProviderResponseBody>>;
@@ -30,58 +30,88 @@ exports.axiosRequest = void 0;
30
30
  const axios_1 = __importDefault(require("axios"));
31
31
  const error_1 = require("../validation/error");
32
32
  const transportMetrics = __importStar(require("./metrics"));
33
+ const util_1 = require("../util");
33
34
  /**
34
35
  * Performs axios request along with metrics recording and error handling
35
36
  *
36
37
  * @param request - axios request config
38
+ * @param config - the adapter config containing the env vars
39
+ * @param checkForRateLimit - async function to check and wait for rate limit
37
40
  * @returns axios response for the request
38
41
  */
39
- async function axiosRequest(request, config) {
42
+ async function axiosRequest(request, config, checkForRateLimit) {
40
43
  const responseTimer = transportMetrics.dataProviderRequestDurationSeconds.startTimer();
41
44
  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';
45
+ const maxRetries = config.RETRY;
46
+ const initialExponent = 1;
47
+ const retry = async (exponent) => {
48
+ const _exponentialDelayRetry = async (exp) => {
49
+ await (0, util_1.sleep)((2 ** exp + Math.random()) * 1000);
50
+ if (checkForRateLimit) {
51
+ await checkForRateLimit();
52
+ }
53
+ return retry(exp + 1);
54
+ };
55
+ try {
56
+ request.timeout = config.API_TIMEOUT;
57
+ providerResponse = await axios_1.default.request(request);
55
58
  }
56
- else if (error?.response?.status) {
57
- adapterError = new error_1.AdapterDataProviderError({});
58
- providerStatusCode = error?.response?.status;
59
+ catch (e) {
60
+ const error = e;
61
+ // Request error
62
+ let providerStatusCode;
63
+ let adapterError;
64
+ if (error.code === 'ECONNABORTED') {
65
+ providerStatusCode = error?.response?.status ?? 504;
66
+ adapterError = new error_1.AdapterTimeoutError({
67
+ statusCode: 504,
68
+ name: 'Data Provider Request Timeout error',
69
+ providerStatusCode: error?.response?.status ?? 504,
70
+ message: error?.message,
71
+ cause: error,
72
+ errorResponse: error?.response?.data,
73
+ url: request.url,
74
+ });
75
+ // Record count of failed data provider request
76
+ transportMetrics.dataProviderRequests
77
+ .labels(transportMetrics.dataProviderMetricsLabel(providerStatusCode, request.method))
78
+ .inc();
79
+ throw adapterError;
80
+ }
81
+ if (exponent >= maxRetries) {
82
+ if (error?.response?.status) {
83
+ adapterError = new error_1.AdapterDataProviderError({});
84
+ providerStatusCode = error?.response?.status;
85
+ }
86
+ else {
87
+ adapterError = new error_1.AdapterConnectionError({});
88
+ providerStatusCode = 0; // 0 -> connection error
89
+ }
90
+ // Record count of failed data provider request
91
+ transportMetrics.dataProviderRequests
92
+ .labels(transportMetrics.dataProviderMetricsLabel(providerStatusCode, request.method))
93
+ .inc();
94
+ adapterError.statusCode = 200;
95
+ adapterError.providerStatusCode = providerStatusCode;
96
+ adapterError.message = error?.message;
97
+ adapterError.cause = error;
98
+ adapterError.errorResponse = error?.response?.data;
99
+ adapterError.url = request.url;
100
+ throw adapterError;
101
+ }
102
+ return _exponentialDelayRetry(exponent);
59
103
  }
60
- else {
61
- adapterError = new error_1.AdapterConnectionError({});
62
- providerStatusCode = 0; // 0 -> connection error
104
+ finally {
105
+ // Record time taken for data provider request for success or failure
106
+ responseTimer();
63
107
  }
64
- // Record count of failed data provider request
108
+ // Record count of successful data provider requests
65
109
  transportMetrics.dataProviderRequests
66
- .labels(transportMetrics.dataProviderMetricsLabel(providerStatusCode, request.method))
110
+ .labels(transportMetrics.dataProviderMetricsLabel(providerResponse.status, request.method))
67
111
  .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;
112
+ return providerResponse;
113
+ };
114
+ return retry(initialExponent);
85
115
  }
86
116
  exports.axiosRequest = axiosRequest;
87
117
  //# sourceMappingURL=util.js.map
@@ -1 +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;S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C;aAC1F,GAAG,EAAE,CAAA;QAER,OAAO,gBAAgB,CAAA;IACzB,CAAC,CAAA;IAED,OAAO,KAAK,CAAC,eAAe,CAAC,CAAA;AAC/B,CAAC;AAzFD,oCAyFC"}
@@ -1,12 +1,13 @@
1
- import WebSocket from 'ws';
2
- import { AdapterContext, AdapterDependencies } from '../adapter';
1
+ import WebSocket, { ClientOptions, RawData } from 'ws';
2
+ import { EndpointContext, AdapterDependencies } from '../adapter';
3
3
  import { Cache } from '../cache';
4
- import { AdapterConfig, SettingsMap } from '../config';
4
+ import { AdapterConfig } from '../config';
5
5
  import { BackgroundExecuteRateLimiter } from '../rate-limiting';
6
6
  import { SubscriptionSet } from '../util';
7
- import { AdapterRequest, ProviderResult } from '../util/request';
8
- import { Transport } from './';
9
- declare type WebSocketClass = new (url: string, protocols?: string | string[] | undefined) => WebSocket;
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;
10
11
  export declare class WebSocketClassProvider {
11
12
  static ctor: WebSocketClass;
12
13
  static set(ctor: WebSocketClass): void;
@@ -15,9 +16,11 @@ export declare class WebSocketClassProvider {
15
16
  /**
16
17
  * Config object that is provided to the WebSocketTransport constructor.
17
18
  */
18
- export interface WebSocketTransportConfig<AdapterParams, ProviderDataMessage, CustomSettings extends SettingsMap> {
19
+ export interface WebSocketTransportConfig<T extends WebsocketTransportGenerics> {
19
20
  /** Endpoint to which to open the WS connection*/
20
- url: (context: AdapterContext<CustomSettings>) => string;
21
+ url: (context: EndpointContext<T>) => Promise<string> | string;
22
+ /** Optional parameters used when establishing the WebSocket connection */
23
+ options?: (context: EndpointContext<T>) => Promise<ClientOptions> | ClientOptions;
21
24
  /** Map of handlers for different WS lifecycle events */
22
25
  handlers: {
23
26
  /**
@@ -26,7 +29,7 @@ export interface WebSocketTransportConfig<AdapterParams, ProviderDataMessage, Cu
26
29
  * @param wsConnection - the WebSocket with an established connection
27
30
  * @returns an empty Promise, or void
28
31
  */
29
- open: (wsConnection: WebSocket, context: AdapterContext<CustomSettings>) => Promise<void> | void;
32
+ open: (wsConnection: WebSocket, context: EndpointContext<T>) => Promise<void> | void;
30
33
  /**
31
34
  * Handles when the WS receives a message
32
35
  *
@@ -34,7 +37,7 @@ export interface WebSocketTransportConfig<AdapterParams, ProviderDataMessage, Cu
34
37
  * @param context - the background context for the Adapter
35
38
  * @returns a list of cache entries of adapter responses to set in the cache
36
39
  */
37
- message: (message: ProviderDataMessage, context: AdapterContext<CustomSettings>) => ProviderResult<AdapterParams>[] | undefined;
40
+ message: (message: T['Provider']['WsMessage'], context: EndpointContext<T>) => ProviderResult<T>[] | undefined;
38
41
  };
39
42
  /** Map of "builders", functions that will be used to prepare specific WS messages */
40
43
  builders: {
@@ -44,37 +47,49 @@ export interface WebSocketTransportConfig<AdapterParams, ProviderDataMessage, Cu
44
47
  * @param params - the body of the adapter request
45
48
  * @returns the WS message (can be any type as long as the [[WebSocket]] doesn't complain)
46
49
  */
47
- subscribeMessage: (params: AdapterParams) => unknown;
50
+ subscribeMessage: (params: T['Request']['Params']) => unknown;
48
51
  /**
49
52
  * Builds a WS message that will be sent to unsubscribe to a specific feed
50
53
  *
51
54
  * @param params - the body of the adapter request
52
55
  * @returns the WS message (can be any type as long as the [[WebSocket]] doesn't complain)
53
56
  */
54
- unsubscribeMessage: (params: AdapterParams) => unknown;
57
+ unsubscribeMessage: (params: T['Request']['Params']) => unknown;
55
58
  };
56
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
+ };
57
75
  /**
58
76
  * Transport implementation that takes incoming requests, adds them to an [[subscriptionSet]] and,
59
77
  * through a WebSocket connection, subscribes to the relevant feeds to populate the cache.
60
78
  *
61
- * @typeParam AdapterParams - interface for the adapter request body
62
- * @typeParam ProviderDataMessage - interface for a WS message containing processable data (i.e. not part of open/close/login/etc)
79
+ * @typeParam T - Helper struct type that will be used to pass types to the generic parameters (check [[WebsocketTransportGenerics]])
63
80
  */
64
- export declare class WebSocketTransport<AdapterParams, ProviderDataMessage, CustomSettings extends SettingsMap> implements Transport<AdapterParams, null, CustomSettings> {
81
+ export declare class WebSocketTransport<T extends WebsocketTransportGenerics> implements Transport<T> {
65
82
  private config;
66
- cache: Cache;
83
+ cache: Cache<AdapterResponse<T['Response']>>;
67
84
  rateLimiter: BackgroundExecuteRateLimiter;
68
- subscriptionSet: SubscriptionSet<AdapterParams>;
69
- localSubscriptions: AdapterParams[];
85
+ subscriptionSet: SubscriptionSet<T['Request']['Params']>;
86
+ localSubscriptions: T['Request']['Params'][];
70
87
  wsConnection: WebSocket;
71
- constructor(config: WebSocketTransportConfig<AdapterParams, ProviderDataMessage, CustomSettings>);
72
- initialize(dependencies: AdapterDependencies, _: AdapterConfig, endpointName: string): Promise<void>;
73
- hasBeenSetUp(): Promise<boolean>;
74
- setup(req: AdapterRequest<AdapterParams>, config: AdapterConfig<CustomSettings>): Promise<void>;
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>;
75
91
  serializeMessage(payload: unknown): string;
76
- deserializeMessage(data: WebSocket.Data): ProviderDataMessage;
77
- establishWsConnection(context: AdapterContext<CustomSettings>): Promise<unknown>;
78
- backgroundExecute(context: AdapterContext<CustomSettings>): Promise<number>;
92
+ deserializeMessage(data: WebSocket.Data): T['Provider']['WsMessage'];
93
+ establishWsConnection(context: EndpointContext<T>, url: string, options?: WebSocket.ClientOptions | undefined): Promise<unknown>;
94
+ backgroundExecute(context: EndpointContext<T>): Promise<number>;
79
95
  }
80
- export {};