@chainlink/external-adapter-framework 0.0.7 → 0.0.10

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 (127) hide show
  1. package/adapter.d.ts +88 -0
  2. package/{dist/src/adapter.js → adapter.js} +0 -0
  3. package/background-executor.d.ts +11 -0
  4. package/{dist/background-executor.js → background-executor.js} +0 -0
  5. package/cache/factory.d.ts +6 -0
  6. package/{dist/cache → cache}/factory.js +0 -0
  7. package/cache/index.d.ts +90 -0
  8. package/{dist/src/cache → cache}/index.js +6 -2
  9. package/cache/local.d.ts +23 -0
  10. package/{dist/cache → cache}/local.js +1 -1
  11. package/cache/metrics.d.ts +27 -0
  12. package/{dist/cache → cache}/metrics.js +7 -1
  13. package/cache/redis.d.ts +16 -0
  14. package/{dist/cache → cache}/redis.js +2 -2
  15. package/config/index.d.ts +195 -0
  16. package/{dist/src/config → config}/index.js +5 -6
  17. package/config/provider-limits.d.ts +31 -0
  18. package/{dist/src/config → config}/provider-limits.js +1 -1
  19. package/examples/coingecko/batch-warming.d.ts +2 -0
  20. package/{dist/examples → examples}/coingecko/batch-warming.js +0 -0
  21. package/examples/coingecko/index.d.ts +2 -0
  22. package/{dist/examples → examples}/coingecko/index.js +0 -0
  23. package/examples/coingecko/rest.d.ts +2 -0
  24. package/{dist/examples → examples}/coingecko/rest.js +0 -0
  25. package/examples/ncfx/config/index.d.ts +12 -0
  26. package/{dist/examples → examples}/ncfx/config/index.js +0 -0
  27. package/examples/ncfx/index.d.ts +2 -0
  28. package/{dist/examples → examples}/ncfx/index.js +0 -0
  29. package/examples/ncfx/websocket.d.ts +36 -0
  30. package/{dist/examples → examples}/ncfx/websocket.js +0 -0
  31. package/index.d.ts +12 -0
  32. package/{dist/index.js → index.js} +3 -0
  33. package/metrics/constants.d.ts +16 -0
  34. package/{dist/src/metrics → metrics}/constants.js +1 -1
  35. package/metrics/index.d.ts +15 -0
  36. package/{dist/metrics → metrics}/index.js +48 -1
  37. package/metrics/util.d.ts +3 -0
  38. package/{dist/metrics → metrics}/util.js +0 -0
  39. package/package.json +2 -6
  40. package/rate-limiting/background/fixed-frequency.d.ts +10 -0
  41. package/{dist/src/rate-limiting → rate-limiting}/background/fixed-frequency.js +0 -0
  42. package/rate-limiting/index.d.ts +54 -0
  43. package/{dist/src/rate-limiting → rate-limiting}/index.js +0 -0
  44. package/rate-limiting/metrics.d.ts +3 -0
  45. package/{dist/src/rate-limiting → rate-limiting}/metrics.js +14 -2
  46. package/rate-limiting/request/simple-counting.d.ts +20 -0
  47. package/{dist/src/rate-limiting → rate-limiting}/request/simple-counting.js +0 -0
  48. package/test.d.ts +1 -0
  49. package/{dist/test.js → test.js} +0 -0
  50. package/transports/batch-warming.d.ts +34 -0
  51. package/{dist/src/transports → transports}/batch-warming.js +47 -1
  52. package/transports/index.d.ts +87 -0
  53. package/{dist/src/transports → transports}/index.js +5 -3
  54. package/transports/metrics.d.ts +21 -0
  55. package/{dist/src/transports → transports}/metrics.js +28 -42
  56. package/transports/rest.d.ts +43 -0
  57. package/{dist/src/transports → transports}/rest.js +38 -2
  58. package/transports/util.d.ts +8 -0
  59. package/{dist/src/transports → transports}/util.js +16 -16
  60. package/transports/websocket.d.ts +80 -0
  61. package/{dist/src/transports → transports}/websocket.js +8 -14
  62. package/util/expiring-sorted-set.d.ts +21 -0
  63. package/{dist/src/util → util}/expiring-sorted-set.js +0 -0
  64. package/util/index.d.ts +11 -0
  65. package/{dist/src/util → util}/index.js +0 -0
  66. package/util/logger.d.ts +42 -0
  67. package/{dist/src/util → util}/logger.js +0 -0
  68. package/util/request.d.ts +55 -0
  69. package/{dist/src/util → util}/request.js +0 -0
  70. package/validation/error.d.ts +50 -0
  71. package/validation/error.js +79 -0
  72. package/validation/index.d.ts +5 -0
  73. package/{dist/src/validation → validation}/index.js +8 -6
  74. package/validation/input-params.d.ts +15 -0
  75. package/{dist/src/validation → validation}/input-params.js +0 -0
  76. package/validation/override-functions.d.ts +3 -0
  77. package/{dist/src/validation → validation}/override-functions.js +0 -0
  78. package/{dist/src/validation → validation}/preset-tokens.json +0 -0
  79. package/validation/validator.d.ts +47 -0
  80. package/{dist/src/validation → validation}/validator.js +0 -0
  81. package/README.md +0 -103
  82. package/dist/adapter.js +0 -60
  83. package/dist/cache/index.js +0 -163
  84. package/dist/config/index.js +0 -364
  85. package/dist/config/provider-limits.js +0 -75
  86. package/dist/metrics/constants.js +0 -25
  87. package/dist/rate-limiting/factory.js +0 -33
  88. package/dist/rate-limiting/index.js +0 -36
  89. package/dist/rate-limiting/metrics.js +0 -32
  90. package/dist/rate-limiting/nop-limiter.js +0 -15
  91. package/dist/rate-limiting/simple-counting.js +0 -61
  92. package/dist/src/background-executor.js +0 -45
  93. package/dist/src/cache/factory.js +0 -57
  94. package/dist/src/cache/local.js +0 -83
  95. package/dist/src/cache/metrics.js +0 -114
  96. package/dist/src/cache/redis.js +0 -100
  97. package/dist/src/examples/bank-frick/accounts.js +0 -191
  98. package/dist/src/examples/bank-frick/config/index.js +0 -45
  99. package/dist/src/examples/bank-frick/index.js +0 -14
  100. package/dist/src/examples/bank-frick/util.js +0 -39
  101. package/dist/src/examples/coingecko/batch-warming.js +0 -52
  102. package/dist/src/examples/coingecko/index.js +0 -10
  103. package/dist/src/examples/coingecko/rest.js +0 -50
  104. package/dist/src/examples/ncfx/config/index.js +0 -15
  105. package/dist/src/examples/ncfx/index.js +0 -10
  106. package/dist/src/examples/ncfx/websocket.js +0 -72
  107. package/dist/src/index.js +0 -89
  108. package/dist/src/metrics/index.js +0 -76
  109. package/dist/src/metrics/util.js +0 -9
  110. package/dist/src/test.js +0 -6
  111. package/dist/src/validation/error.js +0 -41
  112. package/dist/transports/batch-warming.js +0 -57
  113. package/dist/transports/index.js +0 -76
  114. package/dist/transports/metrics.js +0 -133
  115. package/dist/transports/rest.js +0 -91
  116. package/dist/transports/util.js +0 -85
  117. package/dist/transports/websocket.js +0 -171
  118. package/dist/util/expiring-sorted-set.js +0 -47
  119. package/dist/util/index.js +0 -35
  120. package/dist/util/logger.js +0 -62
  121. package/dist/util/request.js +0 -2
  122. package/dist/validation/error.js +0 -41
  123. package/dist/validation/index.js +0 -82
  124. package/dist/validation/input-params.js +0 -30
  125. package/dist/validation/overrideFunctions.js +0 -42
  126. package/dist/validation/presetTokens.json +0 -23
  127. package/dist/validation/validator.js +0 -303
@@ -1,133 +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
- Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.cacheWarmerCount = exports.wsMessageTotal = exports.wsSubscriptionErrors = exports.wsSubscriptionTotal = exports.wsSubscriptionActive = exports.wsConnectionRetries = exports.wsConnectionErrors = exports.wsConnectionActive = exports.messageLabels = exports.subscriptionErrorLabels = exports.subscriptionLabels = exports.connectionErrorLabels = exports.connectionLabels = exports.dataProviderRequestDurationSeconds = exports.dataProviderRequests = exports.dataProviderMetricsLabel = void 0;
27
- const client = __importStar(require("prom-client"));
28
- const constants_1 = require("../metrics/constants");
29
- // Data Provider Metrics
30
- const dataProviderMetricsLabel = (providerStatusCode, method = 'get') => ({
31
- provider_status_code: providerStatusCode,
32
- method: method.toUpperCase()
33
- });
34
- exports.dataProviderMetricsLabel = dataProviderMetricsLabel;
35
- exports.dataProviderRequests = new client.Counter({
36
- name: 'data_provider_requests',
37
- help: 'The number of http requests that are made to a data provider',
38
- labelNames: ['method', 'provider_status_code'],
39
- });
40
- exports.dataProviderRequestDurationSeconds = new client.Histogram({
41
- name: 'data_provider_request_duration_seconds',
42
- help: 'A histogram bucket of the distribution of data provider request durations',
43
- buckets: constants_1.requestDurationBuckets,
44
- });
45
- // Websocket Metrics
46
- const connectionLabels = (key) => ({
47
- // Key,
48
- });
49
- exports.connectionLabels = connectionLabels;
50
- const connectionErrorLabels = (message) => ({
51
- // Key,
52
- message,
53
- });
54
- exports.connectionErrorLabels = connectionErrorLabels;
55
- const subscriptionLabels = (
56
- // Connection_key: string,
57
- feed_id, cache_key) => ({
58
- // Connection_key,
59
- feed_id,
60
- subscription_key: cache_key,
61
- });
62
- exports.subscriptionLabels = subscriptionLabels;
63
- const subscriptionErrorLabels = (
64
- // Connection_key: string,
65
- input, message, type) => ({
66
- // Connection_key,
67
- feed_id: input ? input.metricsMeta?.feedId : 'N/A',
68
- message,
69
- subscription_key: input.cacheKey ? input.cacheKey : 'N/A',
70
- type,
71
- });
72
- exports.subscriptionErrorLabels = subscriptionErrorLabels;
73
- const messageLabels = (feed_id, cache_key) => ({
74
- feed_id,
75
- subscription_key: cache_key,
76
- });
77
- exports.messageLabels = messageLabels;
78
- exports.wsConnectionActive = new client.Gauge({
79
- name: 'ws_connection_active',
80
- help: 'The number of active connections',
81
- labelNames: ['url'],
82
- });
83
- exports.wsConnectionErrors = new client.Counter({
84
- name: 'ws_connection_errors',
85
- help: 'The number of connection errors',
86
- labelNames: ['url', 'message'],
87
- });
88
- // Doesn't seem to be any retry connection functionality yet
89
- exports.wsConnectionRetries = new client.Counter({
90
- name: 'ws_connection_retries',
91
- help: 'The number of connection retries',
92
- labelNames: ['url'],
93
- });
94
- exports.wsSubscriptionActive = new client.Gauge({
95
- name: 'ws_subscription_active',
96
- help: 'The number of currently active subscriptions',
97
- labelNames: [
98
- 'connection_url',
99
- 'feed_id',
100
- 'subscription_key',
101
- ],
102
- });
103
- exports.wsSubscriptionTotal = new client.Counter({
104
- name: 'ws_subscription_total',
105
- help: 'The number of subscriptions opened in total',
106
- labelNames: [
107
- 'connection_url',
108
- 'feed_id',
109
- 'subscription_key',
110
- ],
111
- });
112
- exports.wsSubscriptionErrors = new client.Counter({
113
- name: 'ws_subscription_errors',
114
- help: 'The number of subscriptions errors',
115
- labelNames: [
116
- 'connection_url',
117
- 'feed_id',
118
- 'subscription_key',
119
- 'message',
120
- 'type',
121
- ],
122
- });
123
- exports.wsMessageTotal = new client.Counter({
124
- name: 'ws_message_total',
125
- help: 'The number of messages received in total',
126
- labelNames: ['feed_id', 'subscription_key'],
127
- });
128
- // Cache Warmer Metrics
129
- exports.cacheWarmerCount = new client.Gauge({
130
- name: 'cache_warmer_get_count',
131
- help: 'The number of cache warmers running',
132
- labelNames: ['isBatched'],
133
- });
@@ -1,91 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.RestTransport = void 0;
4
- const util_1 = require("../util");
5
- const error_1 = require("../validation/error");
6
- const util_2 = require("./util");
7
- const IN_FLIGHT_PREFIX = 'InFlight';
8
- const logger = (0, util_1.makeLogger)('RestTransport');
9
- /**
10
- * Transport implementation that takes incoming requests, transforms them into a DataProvider request,
11
- * and executes that request returning the response immediately from the `setup` function.
12
- * Optionally, setting the `coalescing` option to `true` will make it so once a request is in flight,
13
- * new adapter requests for the same feed will not trigger a new one, but return an empty promise from
14
- * the setup instead so the normal cache polling mechanism is used.
15
- *
16
- * @typeParam AdapterParams - interface for the adapter request body
17
- * @typeParam ProviderRequestBody - interface for the body of the request to the Data Provider
18
- * @typeParam ProviderResponseBody - interface for the body of the Data Provider's response
19
- */
20
- class RestTransport {
21
- constructor(config) {
22
- this.config = config;
23
- }
24
- async initialize(dependencies) {
25
- this.inFlightPrefix = `${IN_FLIGHT_PREFIX}-`;
26
- this.cache = dependencies.cache;
27
- this.rateLimiter = dependencies.rateLimiter;
28
- }
29
- async hasBeenSetUp(req) {
30
- if (!this.config.options.coalescing) {
31
- return false;
32
- }
33
- // Check if request is in flight
34
- const inFlight = await this.cache.get(this.inFlightPrefix + req.requestContext.cacheKey);
35
- if (inFlight) {
36
- logger.debug('Request is in flight, transport has been set up');
37
- return true;
38
- }
39
- else {
40
- logger.debug('Request not in flight, transport not set up');
41
- return false;
42
- }
43
- }
44
- async waitUntilUnderRateLimit(options, retry = 0) {
45
- if (this.rateLimiter.isUnderLimits()) {
46
- logger.trace('Incoming request would not be under limits, moving on');
47
- return;
48
- }
49
- if (retry >= options.maxRetries) {
50
- throw new error_1.AdapterError({
51
- statusCode: 504,
52
- message: `REST Transport timed out while waiting for rate limit availability (max retries: ${options.maxRetries})`,
53
- });
54
- }
55
- logger.debug(`Request would be over rate limits, sleeping for ${options.msBetweenRetries}`);
56
- await (0, util_1.sleep)(options.msBetweenRetries);
57
- await this.waitUntilUnderRateLimit(options, retry + 1);
58
- }
59
- async setup(req, config) {
60
- if (this.config.options.coalescing) {
61
- logger.debug('Setting up rest transport, setting request in flight in cache');
62
- // TODO: Should this use a separate cache?
63
- // TODO: Set viable ttl to approximate timeout from API
64
- // TODO: Make this ttl configurable
65
- await this.cache.set(this.inFlightPrefix + req.requestContext.cacheKey, true, 2000); // Can't use Infinity for things like Redis
66
- }
67
- const request = await this.config.prepareRequest(req, config);
68
- logger.trace('Check if we are under rate limits to perform request');
69
- await this.waitUntilUnderRateLimit({
70
- maxRetries: config.REST_TRANSPORT_MAX_RATE_LIMIT_RETRIES,
71
- msBetweenRetries: config.REST_TRANSPORT_MS_BETWEEN_RATE_LIMIT_RETRIES,
72
- });
73
- logger.trace('Sending request to data provider...');
74
- const providerResponse = await (0, util_2.axiosRequest)(request);
75
- logger.debug(`Got response from provider, parsing (raw body: ${providerResponse.data})`); // TODO: Sensitive data?
76
- const parsedResponse = await this.config.parseResponse(req, providerResponse, config);
77
- // TODO: Potentially create function to add all telemetry data
78
- parsedResponse.maxAge = Date.now() + config.CACHE_MAX_AGE;
79
- parsedResponse.metricsMeta = { feedId: req.requestContext.metricsMeta?.feedId || 'N/A' };
80
- logger.debug('Setting provider response in cache');
81
- await this.cache.set(req.requestContext.cacheKey, parsedResponse, config.CACHE_MAX_AGE);
82
- // TODO: Record rate limit credit with cost, default 1
83
- // TODO: move this to a try/catch/finally
84
- if (this.config.options.coalescing) {
85
- logger.debug('Set provider response in cache, removing in flight from cache');
86
- await this.cache.delete(this.inFlightPrefix);
87
- }
88
- return parsedResponse;
89
- }
90
- }
91
- exports.RestTransport = RestTransport;
@@ -1,85 +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.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) {
40
- const responseTimer = transportMetrics.dataProviderRequestDurationSeconds.startTimer();
41
- let providerResponse;
42
- try {
43
- providerResponse = await axios_1.default.request(request);
44
- }
45
- catch (e) {
46
- const error = e;
47
- // Request error
48
- let providerStatusCode;
49
- let name;
50
- let statusCode;
51
- if (error.code === 'ECONNABORTED') {
52
- providerStatusCode = error?.response?.status ?? 504;
53
- name = 'Data Provider Request Timeout error';
54
- statusCode = 504;
55
- }
56
- else {
57
- providerStatusCode = error?.response?.status ?? 0; // 0 -> connection error
58
- statusCode = 200;
59
- }
60
- // Record count of failed data provider request
61
- transportMetrics.dataProviderRequests
62
- .labels(transportMetrics.dataProviderMetricsLabel(providerStatusCode, request.method))
63
- .inc();
64
- // TODO: Use specific AdapterErrors once ported over (AdapterTimeoutError, AdapterConnectionError, AdapterDataProviderError)
65
- throw new error_1.AdapterError({
66
- statusCode,
67
- name,
68
- providerStatusCode,
69
- message: error?.message,
70
- cause: error,
71
- errorResponse: error?.response?.data,
72
- url: request.url,
73
- });
74
- }
75
- finally {
76
- // Record time taken for data provider request for success or failure
77
- responseTimer();
78
- }
79
- // Record count of successful data provider requests
80
- transportMetrics.dataProviderRequests
81
- .labels(transportMetrics.dataProviderMetricsLabel(providerResponse.status, request.method))
82
- .inc();
83
- return providerResponse;
84
- }
85
- exports.axiosRequest = axiosRequest;
@@ -1,171 +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_PERIOD = 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
- exports.DEFAULT_WS_PERIOD = 500;
37
- const logger = (0, util_1.makeLogger)('WebSocketTransport');
38
- class WebSocketClassProvider {
39
- static set(ctor) {
40
- this.ctor = ctor;
41
- }
42
- static get() {
43
- return this.ctor;
44
- }
45
- }
46
- exports.WebSocketClassProvider = WebSocketClassProvider;
47
- WebSocketClassProvider.ctor = ws_1.default;
48
- /**
49
- * Transport implementation that takes incoming requests, adds them to an [[ExpiringSortedSet]] and,
50
- * through a WebSocket connection, subscribes to the relevant feeds to populate the cache.
51
- *
52
- * @typeParam AdapterParams - interface for the adapter request body
53
- * @typeParam ProviderDataMessage - interface for a WS message containing processable data (i.e. not part of open/close/login/etc)
54
- */
55
- class WebSocketTransport {
56
- constructor(config) {
57
- this.config = config;
58
- // The double sets serve to create a simple polling mechanism instead of needing a subscription
59
- // This one would be either local, redis, etc
60
- this.expiringSortedSet = new util_1.ExpiringSortedSet(); // TODO: Move to dependencies, inject
61
- // This one would not; this is always local state
62
- this.localSubscriptions = [];
63
- }
64
- async initialize(dependencies) {
65
- this.cache = dependencies.cache;
66
- }
67
- async hasBeenSetUp(req) {
68
- return !!this.expiringSortedSet.get(req.requestContext.cacheKey);
69
- }
70
- async setup(req) {
71
- logger.debug(`Adding entry to subscription set: [${req.requestContext.cacheKey}] = ${req.requestContext.data}`);
72
- this.expiringSortedSet.add(req.requestContext.cacheKey, req.requestContext.data, exports.DEFAULT_WS_TTL);
73
- }
74
- // TODO: Maybe we don't do this, and leave the preparation on the adapter's side?
75
- // TODO: Maybe we store adapter params pre-prepared? That would be more efficient
76
- // Assuming always JSON payloads for now, makes it all cleaner
77
- serializeMessage(payload) {
78
- return typeof payload === 'string' ? payload : JSON.stringify(payload);
79
- }
80
- deserializeMessage(data) {
81
- return JSON.parse(data.toString());
82
- }
83
- establishWsConnection(context) {
84
- return new Promise((resolve) => {
85
- const ctor = WebSocketClassProvider.get();
86
- this.wsConnection = new ctor(this.config.url);
87
- this.wsConnection.addEventListener('open', async (event) => {
88
- logger.debug(`Opened websocket connection.`);
89
- await this.config.handlers.open(this.wsConnection, context);
90
- logger.debug('Successfully executed connection opened handler');
91
- // Record active ws connections by incrementing count on open
92
- // Using URL in label since connection_key is removed from v3
93
- transportMetrics.wsConnectionActive.labels({ url: this.config.url }).inc();
94
- resolve(true);
95
- });
96
- this.wsConnection.addEventListener('message', async (event) => {
97
- // TODO: Assuming JSON always, maybe use BSON also?
98
- const parsed = this.deserializeMessage(event.data);
99
- logger.trace(`Got ws message: ${parsed}`);
100
- const results = this.config.handlers.message(parsed, context);
101
- if (Array.isArray(results)) {
102
- const responses = (0, _1.buildCacheEntriesFromResults)(results, context);
103
- logger.trace(`Writing ${responses.length} responses to cache`);
104
- await this.cache.setMany(responses, context.adapterConfig.CACHE_MAX_AGE);
105
- }
106
- });
107
- this.wsConnection.addEventListener('error', async (event) => {
108
- // Record connection error count
109
- transportMetrics.wsConnectionErrors.labels(transportMetrics.connectionErrorLabels(event.message)).inc();
110
- });
111
- this.wsConnection.addEventListener('close', (event) => {
112
- logger.debug(`Closed websocket connection. Code: ${event.code} ; reason: ${event.reason?.toString()}`);
113
- // Record active ws connections by decrementing count on close
114
- // Using URL in label since connection_key is removed from v3
115
- transportMetrics.wsConnectionActive.labels({ url: this.config.url }).dec();
116
- });
117
- });
118
- }
119
- // Unlike cache warming, this execute will manage subscriptions
120
- async backgroundExecute(context) {
121
- logger.debug('Starting background execute, getting subscriptions from sorted set');
122
- const desiredSubs = this.expiringSortedSet.getAll();
123
- logger.debug('Generating delta (subscribes & unsubscribes)');
124
- // TODO: More efficient algorithm, this is really easy to read, but high(er) time complexity
125
- const subscribes = desiredSubs
126
- .filter((s) => !this.localSubscriptions.includes(s))
127
- .map(this.config.builders.subscribeMessage)
128
- .map(this.serializeMessage);
129
- const unsubscribes = this.localSubscriptions
130
- .filter((s) => !desiredSubs.includes(s))
131
- .map(this.config.builders.unsubscribeMessage)
132
- .map(this.serializeMessage);
133
- if (subscribes.length || unsubscribes.length) {
134
- logger.debug(`${subscribes.length} new subscriptions; ${unsubscribes.length} to unsubscribe`);
135
- logger.trace('Will subscribe to:', subscribes);
136
- logger.trace('Will unsubscribe to:', unsubscribes);
137
- }
138
- // New subs && no connection -> connect -> add subs
139
- // No new subs && no connection -> skip
140
- // New subs && connection -> add subs
141
- // No new subs && connection -> unsubs only
142
- if (!subscribes.length && !this.wsConnection) {
143
- logger.debug('No entries in subscription set and no established connection, skipping');
144
- return exports.DEFAULT_WS_PERIOD;
145
- }
146
- if (!this.wsConnection && subscribes.length) {
147
- logger.debug('No established connection and new subscriptions available, connecting to WS');
148
- await this.establishWsConnection(context);
149
- }
150
- // TODO: Close connection at some point?
151
- // TODO: Finish ws metrics
152
- logger.debug('Sending subs/unsubs if there are any');
153
- const messages = unsubscribes.concat(subscribes);
154
- for (const message of messages) {
155
- logger.trace(`Sending message: ${JSON.stringify(message)}`);
156
- this.wsConnection.send(message);
157
- // Record total number of ws messages sent
158
- // ws_message_total.labels(messageLabels(this.config.key)).inc()
159
- }
160
- // Record number of total subscriptions made
161
- // ws_subscription_total.labels(subscriptionLabels(this.config.key)).inc(subscribes.length())
162
- // Record number of active ws subscriptions
163
- // const activeSubCount = desiredSubs ? desiredSubs.length : 0
164
- // ws_subscription_active.labels(subscriptionLabels(this.config.key)).set(activeSubCount)
165
- logger.debug('Setting local state to cache value');
166
- this.localSubscriptions = desiredSubs;
167
- logger.debug('Background execute complete');
168
- return exports.DEFAULT_WS_PERIOD;
169
- }
170
- }
171
- exports.WebSocketTransport = WebSocketTransport;
@@ -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,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("./expiring-sorted-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;
@@ -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;
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,41 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.AdapterError = void 0;
4
- class AdapterError extends Error {
5
- constructor({ jobRunID = '1', status = 'errored', statusCode = 500, name = 'AdapterError', message = 'An error occurred.', cause, url, errorResponse, feedID, providerStatusCode, }) {
6
- super(message);
7
- this.jobRunID = jobRunID;
8
- this.status = status;
9
- this.statusCode = statusCode;
10
- this.name = name;
11
- this.message = message;
12
- this.cause = cause;
13
- if (url) {
14
- this.url = url;
15
- }
16
- if (feedID) {
17
- this.feedID = feedID;
18
- }
19
- this.errorResponse = errorResponse;
20
- this.providerStatusCode = providerStatusCode;
21
- }
22
- toJSONResponse() {
23
- const showDebugInfo = process.env['NODE_ENV'] === 'development' || process.env['DEBUG'] === 'true';
24
- const errorBasic = {
25
- name: this.name,
26
- message: this.message,
27
- url: this.url,
28
- errorResponse: this.errorResponse,
29
- feedID: this.feedID,
30
- };
31
- const errorFull = { ...errorBasic, stack: this.stack, cause: this.cause };
32
- return {
33
- jobRunID: this.jobRunID,
34
- status: this.status,
35
- statusCode: this.statusCode,
36
- providerStatusCode: this.providerStatusCode,
37
- error: showDebugInfo ? errorFull : errorBasic,
38
- };
39
- }
40
- }
41
- exports.AdapterError = AdapterError;