@chainlink/external-adapter-framework 0.7.1 → 0.8.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.
- package/adapter/basic.d.ts +6 -50
- package/adapter/basic.js +2 -165
- package/adapter/basic.js.map +1 -1
- package/adapter/endpoint.d.ts +6 -6
- package/adapter/endpoint.js +0 -1
- package/adapter/endpoint.js.map +1 -1
- package/adapter/price.d.ts +8 -38
- package/adapter/price.js +1 -43
- package/adapter/price.js.map +1 -1
- package/adapter/types.d.ts +14 -37
- package/background-executor.js +4 -19
- package/background-executor.js.map +1 -1
- package/cache/index.d.ts +16 -9
- package/cache/index.js +46 -4
- package/cache/index.js.map +1 -1
- package/config/index.d.ts +1 -19
- package/config/index.js +29 -54
- package/config/index.js.map +1 -1
- package/examples/bank-frick/accounts.d.ts +25 -17
- package/examples/bank-frick/accounts.js +13 -23
- package/examples/bank-frick/accounts.js.map +1 -1
- package/examples/bank-frick/index.d.ts +15 -1
- package/examples/bank-frick/index.js +1 -1
- package/examples/coingecko/src/crypto-utils.d.ts +4 -18
- package/examples/coingecko/src/crypto-utils.js +2 -1
- package/examples/coingecko/src/crypto-utils.js.map +1 -1
- package/examples/coingecko/src/endpoint/coins.d.ts +3 -20
- package/examples/coingecko/src/endpoint/coins.js +2 -2
- package/examples/coingecko/src/endpoint/coins.js.map +1 -1
- package/examples/coingecko/src/endpoint/crypto-marketcap.d.ts +2 -2
- package/examples/coingecko/src/endpoint/crypto-marketcap.js +2 -2
- package/examples/coingecko/src/endpoint/crypto-marketcap.js.map +1 -1
- package/examples/coingecko/src/endpoint/crypto-volume.d.ts +2 -2
- package/examples/coingecko/src/endpoint/crypto-volume.js +2 -2
- package/examples/coingecko/src/endpoint/crypto-volume.js.map +1 -1
- package/examples/coingecko/src/endpoint/crypto.d.ts +4 -2
- package/examples/coingecko/src/endpoint/crypto.js +2 -2
- package/examples/coingecko/src/endpoint/crypto.js.map +1 -1
- package/examples/coingecko/src/endpoint/dominance.d.ts +2 -2
- package/examples/coingecko/src/endpoint/dominance.js +2 -2
- package/examples/coingecko/src/endpoint/dominance.js.map +1 -1
- package/examples/coingecko/src/endpoint/global-marketcap.d.ts +2 -2
- package/examples/coingecko/src/endpoint/global-marketcap.js +2 -2
- package/examples/coingecko/src/endpoint/global-marketcap.js.map +1 -1
- package/examples/coingecko/src/global-utils.d.ts +3 -18
- package/examples/coingecko/src/global-utils.js.map +1 -1
- package/examples/coingecko/src/index.js +1 -1
- package/examples/coingecko-old/batch-warming.d.ts +7 -0
- package/examples/coingecko-old/batch-warming.js +54 -0
- package/examples/coingecko-old/batch-warming.js.map +1 -0
- package/examples/coingecko-old/index.d.ts +2 -0
- package/examples/coingecko-old/index.js +12 -0
- package/examples/coingecko-old/index.js.map +1 -0
- package/examples/coingecko-old/rest.d.ts +12 -0
- package/examples/coingecko-old/rest.js +55 -0
- package/examples/coingecko-old/rest.js.map +1 -0
- package/examples/cryptocompare/src/endpoints/crypto.d.ts +2 -14
- package/examples/cryptocompare/src/endpoints/crypto.js.map +1 -1
- package/examples/cryptocompare/src/index.js +1 -1
- package/examples/genesis/index.js +1 -1
- package/examples/genesis/sseStream.d.ts +2 -13
- package/examples/genesis/sseStream.js +1 -4
- package/examples/genesis/sseStream.js.map +1 -1
- package/examples/ncfx/config/index.d.ts +12 -0
- package/examples/ncfx/config/index.js +16 -0
- package/examples/ncfx/config/index.js.map +1 -0
- package/examples/ncfx/index.d.ts +13 -0
- package/examples/ncfx/index.js +12 -0
- package/examples/ncfx/index.js.map +1 -0
- package/examples/ncfx/websocket.d.ts +47 -0
- package/examples/ncfx/websocket.js +74 -0
- package/examples/ncfx/websocket.js.map +1 -0
- package/index.d.ts +0 -1
- package/index.js +6 -4
- package/index.js.map +1 -1
- package/metrics/index.d.ts +0 -2
- package/metrics/index.js +4 -5
- package/metrics/index.js.map +1 -1
- package/metrics/util.d.ts +5 -5
- package/metrics/util.js.map +1 -1
- package/package.json +9 -7
- package/rate-limiting/background/fixed-frequency.d.ts +3 -2
- package/rate-limiting/background/fixed-frequency.js.map +1 -1
- package/rate-limiting/index.d.ts +4 -4
- package/rate-limiting/index.js +1 -1
- package/rate-limiting/index.js.map +1 -1
- package/rate-limiting/request/simple-counting.d.ts +3 -2
- package/rate-limiting/request/simple-counting.js.map +1 -1
- package/transports/batch-warming.d.ts +15 -33
- package/transports/batch-warming.js +16 -43
- package/transports/batch-warming.js.map +1 -1
- package/transports/index.d.ts +31 -65
- package/transports/index.js +59 -2
- package/transports/index.js.map +1 -1
- package/transports/metrics.d.ts +2 -2
- package/transports/metrics.js +2 -2
- package/transports/metrics.js.map +1 -1
- package/transports/rest.d.ts +14 -31
- package/transports/rest.js +25 -17
- package/transports/rest.js.map +1 -1
- package/transports/sse.d.ts +20 -40
- package/transports/sse.js +8 -10
- package/transports/sse.js.map +1 -1
- package/transports/websocket.d.ts +25 -40
- package/transports/websocket.js +13 -10
- package/transports/websocket.js.map +1 -1
- package/util/logger.d.ts +0 -2
- package/util/logger.js +7 -4
- package/util/logger.js.map +1 -1
- package/util/request.d.ts +10 -46
- package/util/subscription-set/subscription-set.d.ts +1 -1
- package/util/subscription-set/subscription-set.js +1 -1
- package/util/subscription-set/subscription-set.js.map +1 -1
- package/util/test-payload-loader.d.ts +0 -1
- package/util/test-payload-loader.js +1 -2
- package/util/test-payload-loader.js.map +1 -1
- package/validation/error.d.ts +3 -1
- package/validation/error.js +4 -2
- package/validation/error.js.map +1 -1
- package/validation/index.js +17 -26
- package/validation/index.js.map +1 -1
- package/validation/input-params.d.ts +1 -0
- package/validation/input-params.js +28 -0
- package/validation/input-params.js.map +1 -1
- package/validation/input-validator.js +2 -1
- package/validation/input-validator.js.map +1 -1
- package/validation/override-functions.d.ts +3 -0
- package/validation/override-functions.js +41 -0
- package/validation/override-functions.js.map +1 -0
- package/README.md +0 -67
- package/transports/routing.d.ts +0 -21
- package/transports/routing.js +0 -50
- package/transports/routing.js.map +0 -1
package/adapter/basic.d.ts
CHANGED
|
@@ -1,33 +1,24 @@
|
|
|
1
1
|
import { SettingsMap, BaseAdapterConfig, AdapterConfig } from '../config';
|
|
2
|
-
import { AdapterRequest, AdapterResponse, Merge } from '../util';
|
|
3
2
|
import { AdapterEndpoint } from './endpoint';
|
|
4
|
-
import { AdapterDependencies, AdapterParams, AdapterRateLimitingConfig, CustomAdapterSettings
|
|
3
|
+
import { AdapterDependencies, AdapterParams, AdapterRateLimitingConfig, CustomAdapterSettings } from './types';
|
|
5
4
|
/**
|
|
6
5
|
* Main class to represent an External Adapter
|
|
7
6
|
*/
|
|
8
|
-
export declare class Adapter<CustomSettings extends CustomAdapterSettings = SettingsMap> implements
|
|
9
|
-
name:
|
|
7
|
+
export declare class Adapter<CustomSettings extends CustomAdapterSettings = SettingsMap> implements AdapterParams<CustomSettings> {
|
|
8
|
+
name: string;
|
|
10
9
|
defaultEndpoint?: string | undefined;
|
|
11
|
-
endpoints: AdapterEndpoint<
|
|
12
|
-
CustomSettings: CustomSettings;
|
|
13
|
-
}>>[];
|
|
10
|
+
endpoints: AdapterEndpoint<unknown, unknown, CustomSettings>[];
|
|
14
11
|
envDefaultOverrides?: Partial<BaseAdapterConfig> | undefined;
|
|
15
12
|
customSettings?: SettingsMap | undefined;
|
|
16
13
|
rateLimiting?: AdapterRateLimitingConfig | undefined;
|
|
17
14
|
overrides?: Record<string, string> | undefined;
|
|
18
|
-
requestTransforms?: RequestTransform[];
|
|
19
|
-
envVarsPrefix?: string;
|
|
20
15
|
initialized: boolean;
|
|
21
16
|
/** Object containing alias translations for all endpoints */
|
|
22
|
-
endpointsMap: Record<string, AdapterEndpoint<
|
|
23
|
-
CustomSettings: CustomSettings;
|
|
24
|
-
}>>>;
|
|
17
|
+
endpointsMap: Record<string, AdapterEndpoint<unknown, unknown, CustomSettings>>;
|
|
25
18
|
/** Initialized dependencies that the adapter will use */
|
|
26
19
|
dependencies: AdapterDependencies;
|
|
27
20
|
/** Configuration params for various adapter properties */
|
|
28
|
-
config: AdapterConfig
|
|
29
|
-
/** Bootstrap function that will run when initializing the adapter */
|
|
30
|
-
private readonly bootstrap?;
|
|
21
|
+
config: AdapterConfig;
|
|
31
22
|
constructor(params: AdapterParams<CustomSettings>);
|
|
32
23
|
/**
|
|
33
24
|
* Initializes all of the [[Transport]]s in the adapter, passing along any [[AdapterDependencies]] and [[AdapterConfig]].
|
|
@@ -50,11 +41,6 @@ export declare class Adapter<CustomSettings extends CustomAdapterSettings = Sett
|
|
|
50
41
|
* using the sensitive flag in the adapter config
|
|
51
42
|
*/
|
|
52
43
|
private buildCensorList;
|
|
53
|
-
/**
|
|
54
|
-
* Logs a warning for certain configs if set to particular values.
|
|
55
|
-
* Used to warn stakeholders of potential risks.
|
|
56
|
-
*/
|
|
57
|
-
private logConfigWarnings;
|
|
58
44
|
/**
|
|
59
45
|
* This function will process dependencies for an adapter, such as caches or rate limiters,
|
|
60
46
|
* in order to inject them into transports and other relevant places later in the lifecycle.
|
|
@@ -63,34 +49,4 @@ export declare class Adapter<CustomSettings extends CustomAdapterSettings = Sett
|
|
|
63
49
|
* @returns a set of AdapterDependencies all initialized
|
|
64
50
|
*/
|
|
65
51
|
initializeDependencies(inputDependencies?: Partial<AdapterDependencies>): AdapterDependencies;
|
|
66
|
-
/**
|
|
67
|
-
* Attempts to find a value from the Cache and return that if found.
|
|
68
|
-
*
|
|
69
|
-
* @param req - the incoming request to this adapter
|
|
70
|
-
* @returns the cached value if exists
|
|
71
|
-
*/
|
|
72
|
-
findResponseInCache(req: AdapterRequest): Promise<AdapterResponse | undefined>;
|
|
73
|
-
/**
|
|
74
|
-
* Default request transform that takes requests and manipulates
|
|
75
|
-
*
|
|
76
|
-
* @param adapter - the current adapter
|
|
77
|
-
* @param req - the current adapter request
|
|
78
|
-
* @returns the modified (or new) request
|
|
79
|
-
*/
|
|
80
|
-
symbolOverrider(req: AdapterRequest): AdapterRequest<import("../util").RequestGenerics>;
|
|
81
|
-
/**
|
|
82
|
-
* Takes the incoming request and applies all request transforms in the adapter
|
|
83
|
-
*
|
|
84
|
-
* @param req - the current adapter request
|
|
85
|
-
* @returns the request after passing through all request transforms
|
|
86
|
-
*/
|
|
87
|
-
runRequestTransforms(req: AdapterRequest): void;
|
|
88
|
-
/**
|
|
89
|
-
* Function to serve as middleware to pass along the AdapterRequest to the appropriate Transport (acc. to the endpoint in the req.)
|
|
90
|
-
*
|
|
91
|
-
* @param req - the incoming request to this adapter
|
|
92
|
-
* @param replySent - a promise that resolves when the reply has already been sent
|
|
93
|
-
* @returns a simple Promise when it's done
|
|
94
|
-
*/
|
|
95
|
-
handleRequest(req: AdapterRequest, replySent: Promise<unknown>): Promise<AdapterResponse>;
|
|
96
52
|
}
|
package/adapter/basic.js
CHANGED
|
@@ -31,11 +31,9 @@ const ioredis_1 = __importDefault(require("ioredis"));
|
|
|
31
31
|
const cache_1 = require("../cache");
|
|
32
32
|
const cacheMetrics = __importStar(require("../cache/metrics"));
|
|
33
33
|
const config_1 = require("../config");
|
|
34
|
-
const transportMetrics = __importStar(require("../transports/metrics"));
|
|
35
34
|
const rate_limiting_1 = require("../rate-limiting");
|
|
36
35
|
const util_1 = require("../util");
|
|
37
36
|
const censor_list_1 = __importDefault(require("../util/censor/censor-list"));
|
|
38
|
-
const error_1 = require("../validation/error");
|
|
39
37
|
const logger = (0, util_1.makeLogger)('Adapter');
|
|
40
38
|
/**
|
|
41
39
|
* Main class to represent an External Adapter
|
|
@@ -54,12 +52,9 @@ class Adapter {
|
|
|
54
52
|
this.customSettings = params.customSettings;
|
|
55
53
|
this.rateLimiting = params.rateLimiting;
|
|
56
54
|
this.overrides = params.overrides;
|
|
57
|
-
this.requestTransforms = [this.symbolOverrider.bind(this), ...(params.requestTransforms || [])];
|
|
58
|
-
this.bootstrap = params.bootstrap;
|
|
59
55
|
this.config = (0, config_1.buildAdapterConfig)({
|
|
60
56
|
overrides: this.envDefaultOverrides,
|
|
61
57
|
customSettings: this.customSettings,
|
|
62
|
-
envVarsPrefix: this.envVarsPrefix,
|
|
63
58
|
});
|
|
64
59
|
this.normalizeEndpointNames();
|
|
65
60
|
this.calculateRateLimitAllocations();
|
|
@@ -72,13 +67,6 @@ class Adapter {
|
|
|
72
67
|
if (this.initialized) {
|
|
73
68
|
throw new Error('This adapter has already been initialized!');
|
|
74
69
|
}
|
|
75
|
-
// Building configs during initialization to avoid validation errors during construction
|
|
76
|
-
(0, config_1.validateAdapterConfig)(this.config, this.customSettings);
|
|
77
|
-
// Log warnings for risks associated with certain configs and values
|
|
78
|
-
this.logConfigWarnings();
|
|
79
|
-
if (this.bootstrap) {
|
|
80
|
-
await this.bootstrap(this);
|
|
81
|
-
}
|
|
82
70
|
this.dependencies = this.initializeDependencies(dependencies);
|
|
83
71
|
for (const endpoint of this.endpoints) {
|
|
84
72
|
// Add aliases to map to use in validation
|
|
@@ -144,10 +132,7 @@ class Adapter {
|
|
|
144
132
|
buildCensorList() {
|
|
145
133
|
const censorList = Object.entries(config_1.BaseSettings)
|
|
146
134
|
.concat(Object.entries(this.customSettings || {}))
|
|
147
|
-
.filter(([name, setting]) => setting &&
|
|
148
|
-
setting.type === 'string' &&
|
|
149
|
-
setting.sensitive &&
|
|
150
|
-
this.config[name])
|
|
135
|
+
.filter(([name, setting]) => setting && setting.type === 'string' && setting.sensitive && this.config[name])
|
|
151
136
|
.map(([name]) => ({
|
|
152
137
|
key: name,
|
|
153
138
|
// Escaping potential special characters in values before creating regex
|
|
@@ -157,19 +142,6 @@ class Adapter {
|
|
|
157
142
|
}));
|
|
158
143
|
censor_list_1.default.set(censorList);
|
|
159
144
|
}
|
|
160
|
-
/**
|
|
161
|
-
* Logs a warning for certain configs if set to particular values.
|
|
162
|
-
* Used to warn stakeholders of potential risks.
|
|
163
|
-
*/
|
|
164
|
-
logConfigWarnings() {
|
|
165
|
-
if (this.config.LOG_LEVEL.toUpperCase() === 'DEBUG' ||
|
|
166
|
-
this.config.LOG_LEVEL.toUpperCase() === 'TRACE') {
|
|
167
|
-
logger.warn(`LOG_LEVEL has been set to ${this.config.LOG_LEVEL.toUpperCase()}. Setting higher log levels results in increased memory usage and potentially slower performance.`);
|
|
168
|
-
}
|
|
169
|
-
if (this.config.DEBUG === true) {
|
|
170
|
-
logger.warn(`The adapter is running with DEBUG mode on.`);
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
145
|
/**
|
|
174
146
|
* This function will process dependencies for an adapter, such as caches or rate limiters,
|
|
175
147
|
* in order to inject them into transports and other relevant places later in the lifecycle.
|
|
@@ -181,7 +153,6 @@ class Adapter {
|
|
|
181
153
|
const dependencies = inputDependencies || {};
|
|
182
154
|
if (!dependencies.redisClient) {
|
|
183
155
|
if (this.config.CACHE_TYPE === 'redis') {
|
|
184
|
-
const maxCooldown = this.config.CACHE_REDIS_MAX_RECONNECT_COOLDOWN;
|
|
185
156
|
dependencies.redisClient = new ioredis_1.default({
|
|
186
157
|
enableAutoPipelining: true,
|
|
187
158
|
host: this.config.CACHE_REDIS_HOST,
|
|
@@ -189,13 +160,6 @@ class Adapter {
|
|
|
189
160
|
password: this.config.CACHE_REDIS_PASSWORD,
|
|
190
161
|
path: this.config.CACHE_REDIS_PATH,
|
|
191
162
|
timeout: this.config.CACHE_REDIS_TIMEOUT,
|
|
192
|
-
retryStrategy(times) {
|
|
193
|
-
cacheMetrics.redisRetriesCount.inc();
|
|
194
|
-
logger.warn(`Redis reconnect attempt #${times}`);
|
|
195
|
-
return Math.min(times * 100, maxCooldown); // Next reconnect attempt time
|
|
196
|
-
},
|
|
197
|
-
connectTimeout: this.config.CACHE_REDIS_CONNECTION_TIMEOUT,
|
|
198
|
-
maxRetriesPerRequest: 30, // Limits the number of retries before the adapter shuts down
|
|
199
163
|
});
|
|
200
164
|
dependencies.redisClient.on('connect', () => {
|
|
201
165
|
cacheMetrics.redisConnectionsOpen.inc();
|
|
@@ -213,137 +177,10 @@ class Adapter {
|
|
|
213
177
|
dependencies.backgroundExecuteRateLimiter = new rate_limiting_1.FixedFrequencyRateLimiter().initialize(this.endpoints, rateLimitingTier);
|
|
214
178
|
}
|
|
215
179
|
if (!dependencies.subscriptionSetFactory) {
|
|
216
|
-
dependencies.subscriptionSetFactory = new util_1.SubscriptionSetFactory(this.config,
|
|
180
|
+
dependencies.subscriptionSetFactory = new util_1.SubscriptionSetFactory(this.config, dependencies.redisClient, this.name);
|
|
217
181
|
}
|
|
218
182
|
return dependencies;
|
|
219
183
|
}
|
|
220
|
-
/**
|
|
221
|
-
* Attempts to find a value from the Cache and return that if found.
|
|
222
|
-
*
|
|
223
|
-
* @param req - the incoming request to this adapter
|
|
224
|
-
* @returns the cached value if exists
|
|
225
|
-
*/
|
|
226
|
-
async findResponseInCache(req) {
|
|
227
|
-
const response = await this.dependencies.cache.get(req.requestContext.cacheKey);
|
|
228
|
-
if (response) {
|
|
229
|
-
if (this.config.METRICS_ENABLED && this.config.EXPERIMENTAL_METRICS_ENABLED) {
|
|
230
|
-
const label = cacheMetrics.cacheMetricsLabel(req.requestContext.cacheKey, req.requestContext.meta?.metrics?.feedId || 'N/A', this.config.CACHE_TYPE);
|
|
231
|
-
// Record cache staleness and cache get count and value
|
|
232
|
-
const staleness = (0, cache_1.calculateStaleness)(response.maxAge, this.config.CACHE_MAX_AGE);
|
|
233
|
-
cacheMetrics.cacheGet(label, response.result, staleness);
|
|
234
|
-
req.requestContext.meta = {
|
|
235
|
-
...req.requestContext.meta,
|
|
236
|
-
metrics: { ...req.requestContext.meta?.metrics, cacheHit: true },
|
|
237
|
-
};
|
|
238
|
-
}
|
|
239
|
-
return response;
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
/**
|
|
243
|
-
* Default request transform that takes requests and manipulates
|
|
244
|
-
*
|
|
245
|
-
* @param adapter - the current adapter
|
|
246
|
-
* @param req - the current adapter request
|
|
247
|
-
* @returns the modified (or new) request
|
|
248
|
-
*/
|
|
249
|
-
symbolOverrider(req) {
|
|
250
|
-
const rawRequestBody = req.body;
|
|
251
|
-
const requestOverrides = rawRequestBody.data?.overrides?.[this.name.toLowerCase()];
|
|
252
|
-
const base = req.requestContext.data['base'];
|
|
253
|
-
if (requestOverrides?.[base]) {
|
|
254
|
-
// Perform overrides specified in the request payload
|
|
255
|
-
req.requestContext.data['base'] = requestOverrides[base];
|
|
256
|
-
}
|
|
257
|
-
else if (this.overrides?.[base]) {
|
|
258
|
-
// Perform hardcoded adapter overrides
|
|
259
|
-
req.requestContext.data['base'] = this.overrides[base];
|
|
260
|
-
}
|
|
261
|
-
return req;
|
|
262
|
-
}
|
|
263
|
-
/**
|
|
264
|
-
* Takes the incoming request and applies all request transforms in the adapter
|
|
265
|
-
*
|
|
266
|
-
* @param req - the current adapter request
|
|
267
|
-
* @returns the request after passing through all request transforms
|
|
268
|
-
*/
|
|
269
|
-
runRequestTransforms(req) {
|
|
270
|
-
if (!this.requestTransforms) {
|
|
271
|
-
return;
|
|
272
|
-
}
|
|
273
|
-
for (const transform of this.requestTransforms) {
|
|
274
|
-
transform(req);
|
|
275
|
-
}
|
|
276
|
-
}
|
|
277
|
-
/**
|
|
278
|
-
* Function to serve as middleware to pass along the AdapterRequest to the appropriate Transport (acc. to the endpoint in the req.)
|
|
279
|
-
*
|
|
280
|
-
* @param req - the incoming request to this adapter
|
|
281
|
-
* @param replySent - a promise that resolves when the reply has already been sent
|
|
282
|
-
* @returns a simple Promise when it's done
|
|
283
|
-
*/
|
|
284
|
-
async handleRequest(req, replySent) {
|
|
285
|
-
// Get transport, must be here because it's already checked in the validator
|
|
286
|
-
const transport = this.endpointsMap[req.requestContext.endpointName].transport;
|
|
287
|
-
// First try to find the response in our cache, keep it ready
|
|
288
|
-
const cachedResponse = await this.findResponseInCache(req);
|
|
289
|
-
// Next we fire off the transport's registration of the request if defined, regardless of if we already have a cached response.
|
|
290
|
-
// This is necessary to ensure things like subscription sets are updated each time we get a request
|
|
291
|
-
let requestRegistrationPromise;
|
|
292
|
-
if (transport.registerRequest) {
|
|
293
|
-
const handler = async () => {
|
|
294
|
-
// If we already have a cached response, wait for it to be sent back before continuing with registration
|
|
295
|
-
// This way we respond to incoming requests from the cache as fast as possible
|
|
296
|
-
if (cachedResponse) {
|
|
297
|
-
await replySent;
|
|
298
|
-
}
|
|
299
|
-
// Disable the non-null assertion error, since we're checking this exists in the if above
|
|
300
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
301
|
-
return transport.registerRequest(req, this.config);
|
|
302
|
-
};
|
|
303
|
-
// Execute the registration handler without blocking
|
|
304
|
-
logger.debug(`Firing request registration handler${cachedResponse ? ' (cached response already sent)' : ''}`);
|
|
305
|
-
requestRegistrationPromise = handler();
|
|
306
|
-
}
|
|
307
|
-
// Now that we have dealt with request registration, we can return the cached response if present
|
|
308
|
-
if (cachedResponse) {
|
|
309
|
-
logger.debug('Found response from cache, sending that');
|
|
310
|
-
return cachedResponse;
|
|
311
|
-
}
|
|
312
|
-
// If there was no cached response, execute the foregroundExecute if defined
|
|
313
|
-
const immediateResponse = transport.foregroundExecute && (await transport.foregroundExecute(req, this.config));
|
|
314
|
-
if (immediateResponse) {
|
|
315
|
-
logger.debug('Got immediate response from transport, sending as response');
|
|
316
|
-
return immediateResponse;
|
|
317
|
-
}
|
|
318
|
-
// Finally, either because there was no synchronous execute or because it returned an empty response,
|
|
319
|
-
// we wait for the cache to be filled (either from background work started in the sync execute, or the backgroundExecute).
|
|
320
|
-
// We can wait for the request registration to have finished here, since we're going to be sleeping for the cache anyways,
|
|
321
|
-
// and it's useful in case the registration throws a promise so that it doesn't go unhandled.
|
|
322
|
-
await requestRegistrationPromise;
|
|
323
|
-
// Observe the idle time taken for polling response
|
|
324
|
-
const metricsTimer = transportMetrics.transportPollingDurationSeconds
|
|
325
|
-
.labels({ endpoint: req.requestContext.endpointName })
|
|
326
|
-
.startTimer();
|
|
327
|
-
logger.debug('Transport is set up, polling cache for response...');
|
|
328
|
-
const response = await (0, cache_1.pollResponseFromCache)(this.dependencies.cache, req.requestContext.cacheKey, {
|
|
329
|
-
maxRetries: this.config.CACHE_POLLING_MAX_RETRIES,
|
|
330
|
-
sleep: this.config.CACHE_POLLING_SLEEP_MS,
|
|
331
|
-
});
|
|
332
|
-
metricsTimer({ succeeded: String(!!response) });
|
|
333
|
-
if (response) {
|
|
334
|
-
logger.debug('Got a response from polling the cache, sending that back');
|
|
335
|
-
return response;
|
|
336
|
-
}
|
|
337
|
-
// Record polling mechanism failure to return response
|
|
338
|
-
transportMetrics.transportPollingFailureCount
|
|
339
|
-
.labels({ endpoint: req.requestContext.endpointName })
|
|
340
|
-
.inc();
|
|
341
|
-
logger.debug('Ran out of polling attempts, returning timeout');
|
|
342
|
-
throw new error_1.AdapterTimeoutError({
|
|
343
|
-
message: 'Timed out waiting for provider result.',
|
|
344
|
-
statusCode: 504,
|
|
345
|
-
});
|
|
346
|
-
}
|
|
347
184
|
}
|
|
348
185
|
exports.Adapter = Adapter;
|
|
349
186
|
//# sourceMappingURL=basic.js.map
|
package/adapter/basic.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"basic.js","sourceRoot":"","sources":["../../../src/adapter/basic.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,sDAA2B;AAC3B,
|
|
1
|
+
{"version":3,"file":"basic.js","sourceRoot":"","sources":["../../../src/adapter/basic.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,sDAA2B;AAC3B,oCAAuC;AACvC,+DAAgD;AAChD,sCAMkB;AAClB,oDAIyB;AACzB,kCAA4D;AAC5D,6EAAuE;AASvE,MAAM,MAAM,GAAG,IAAA,iBAAU,EAAC,SAAS,CAAC,CAAA;AAEpC;;GAEG;AACH,MAAa,OAAO;IAwBlB,YAAY,MAAqC;QAZjD,uBAAuB;QACvB,gBAAW,GAAG,KAAK,CAAA;QAEnB,6DAA6D;QAC7D,iBAAY,GAAsE,EAAE,CAAA;QASlF,mBAAmB;QACnB,IAAI,CAAC,IAAI,GAAG,MAAM,CAAC,IAAI,CAAA;QACvB,IAAI,C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,IAAI,CAAC,YAAY,CAAC,4BAA4B,EAAE;YAC9C,YAAY,CAAC,4BAA4B,GAAG,IAAI,yCAAyB,EAAE,CAAC,UAAU,CACpF,IAAI,CAAC,SAAS,EACd,gBAAgB,CACjB,CAAA;SACF;QACD,IAAI,CAAC,YAAY,CAAC,sBAAsB,EAAE;YACxC,YAAY,CAAC,sBAAsB,GAAG,IAAI,6BAAsB,CAC9D,IAAI,CAAC,MAAM,EACX,YAAY,CAAC,WAAW,EACxB,IAAI,CAAC,IAAI,CACV,CAAA;SACF;QAED,OAAO,YAAmC,CAAA;IAC5C,CAAC;CACF;AA5MD,0BA4MC"}
|
package/adapter/endpoint.d.ts
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { SettingsMap } from '../config';
|
|
2
|
+
import { Transport } from '../transports';
|
|
2
3
|
import { InputParameters } from '../validation';
|
|
4
|
+
import { AdapterEndpointParams, EndpointRateLimitingConfig } from './types';
|
|
3
5
|
import { InputValidator } from '../validation/input-validator';
|
|
4
|
-
import { AdapterEndpointParams, EndpointGenerics, EndpointRateLimitingConfig } from './types';
|
|
5
6
|
/**
|
|
6
7
|
* Main class to represent an endpoint within an External Adapter
|
|
7
8
|
*/
|
|
8
|
-
export declare class AdapterEndpoint<
|
|
9
|
+
export declare class AdapterEndpoint<Params, Result, CustomSettings extends SettingsMap> implements AdapterEndpointParams<Params, Result, CustomSettings> {
|
|
9
10
|
name: string;
|
|
10
11
|
aliases?: string[] | undefined;
|
|
11
|
-
transport: Transport<
|
|
12
|
+
transport: Transport<Params, Result, CustomSettings>;
|
|
12
13
|
inputParameters: InputParameters;
|
|
13
14
|
rateLimiting?: EndpointRateLimitingConfig | undefined;
|
|
14
15
|
validator: InputValidator;
|
|
15
|
-
|
|
16
|
-
constructor(params: AdapterEndpointParams<T>);
|
|
16
|
+
constructor(params: AdapterEndpointParams<Params, Result, CustomSettings>);
|
|
17
17
|
}
|
package/adapter/endpoint.js
CHANGED
|
@@ -13,7 +13,6 @@ class AdapterEndpoint {
|
|
|
13
13
|
this.inputParameters = params.inputParameters;
|
|
14
14
|
this.rateLimiting = params.rateLimiting;
|
|
15
15
|
this.validator = new input_validator_1.InputValidator(this.inputParameters);
|
|
16
|
-
this.cacheKeyGenerator = params.cacheKeyGenerator;
|
|
17
16
|
}
|
|
18
17
|
}
|
|
19
18
|
exports.AdapterEndpoint = AdapterEndpoint;
|
package/adapter/endpoint.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"endpoint.js","sourceRoot":"","sources":["../../../src/adapter/endpoint.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"endpoint.js","sourceRoot":"","sources":["../../../src/adapter/endpoint.ts"],"names":[],"mappings":";;;AAIA,mEAA8D;AAE9D;;GAEG;AACH,MAAa,eAAe;IAU1B,YAAY,MAA6D;QACvE,IAAI,CAAC,IAAI,GAAG,MAAM,CAAC,IAAI,CAAA;QACvB,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC,OAAO,CAAA;QAC7B,IAAI,CAAC,SAAS,GAAG,MAAM,CAAC,SAAS,CAAA;QACjC,IAAI,CAAC,eAAe,GAAG,MAAM,CAAC,eAAe,CAAA;QAC7C,IAAI,CAAC,YAAY,GAAG,MAAM,CAAC,YAAY,CAAA;QACvC,IAAI,CAAC,SAAS,GAAG,IAAI,gCAAc,CAAC,IAAI,CAAC,eAAe,CAAC,CAAA;IAC3D,CAAC;CACF;AAlBD,0CAkBC"}
|
package/adapter/price.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { AdapterRequest, AdapterRequestContext, AdapterResponse, RequestGenerics } from '../util';
|
|
1
|
+
import { Adapter, AdapterEndpointParams, AdapterParams } from './index';
|
|
3
2
|
import { AdapterEndpoint } from './endpoint';
|
|
4
|
-
import {
|
|
3
|
+
import { SettingsMap } from '../config';
|
|
5
4
|
/**
|
|
6
5
|
* Type for the base input parameter config that any [[PriceEndpoint]] must extend
|
|
7
6
|
*/
|
|
@@ -10,13 +9,13 @@ export declare type PriceEndpointInputParameters = {
|
|
|
10
9
|
aliases: readonly ['from', 'coin', ...string[]];
|
|
11
10
|
type: 'string';
|
|
12
11
|
description: 'The symbol of symbols of the currency to query';
|
|
13
|
-
required:
|
|
12
|
+
required: true;
|
|
14
13
|
};
|
|
15
14
|
quote: {
|
|
16
15
|
aliases: readonly ['to', 'market', ...string[]];
|
|
17
16
|
type: 'string';
|
|
18
17
|
description: 'The symbol of the currency to convert to';
|
|
19
|
-
required:
|
|
18
|
+
required: true;
|
|
20
19
|
};
|
|
21
20
|
};
|
|
22
21
|
/**
|
|
@@ -26,52 +25,23 @@ export declare const priceEndpointInputParameters: PriceEndpointInputParameters;
|
|
|
26
25
|
/**
|
|
27
26
|
* Type for base input params for a PriceEndpoint
|
|
28
27
|
*/
|
|
29
|
-
|
|
28
|
+
declare type PriceEndpointParams = {
|
|
30
29
|
base: string;
|
|
31
30
|
quote: string;
|
|
32
31
|
};
|
|
33
|
-
/**
|
|
34
|
-
* Structure of an "includes" file.
|
|
35
|
-
* Include pairs describe an incoming price feed request, and the details specify
|
|
36
|
-
*/
|
|
37
|
-
export declare type IncludesFile = IncludePair[];
|
|
38
|
-
declare type IncludeDetails = {
|
|
39
|
-
from: string;
|
|
40
|
-
to: string;
|
|
41
|
-
inverse: boolean;
|
|
42
|
-
};
|
|
43
|
-
declare type IncludePair = {
|
|
44
|
-
from: string;
|
|
45
|
-
to: string;
|
|
46
|
-
includes: IncludeDetails[];
|
|
47
|
-
};
|
|
48
|
-
declare type IncludesMap = Record<string, Record<string, IncludeDetails>>;
|
|
49
32
|
/**
|
|
50
33
|
* A PriceEndpoint is a specific type of AdapterEndpoint. Meant to comply with standard practices for
|
|
51
34
|
* Data Feeds, its InputParameters must extend the basic ones (base/quote).
|
|
52
35
|
*/
|
|
53
|
-
export declare class PriceEndpoint<
|
|
54
|
-
constructor(params: AdapterEndpointParams<
|
|
36
|
+
export declare class PriceEndpoint<Params extends PriceEndpointParams, ProviderResponseBody, CustomSettings extends SettingsMap> extends AdapterEndpoint<Params, ProviderResponseBody, CustomSettings> {
|
|
37
|
+
constructor(params: AdapterEndpointParams<Params, ProviderResponseBody, CustomSettings> & {
|
|
55
38
|
inputParameters: PriceEndpointInputParameters;
|
|
56
39
|
});
|
|
57
40
|
}
|
|
58
|
-
declare type PriceAdapterRequest<T extends RequestGenerics> = AdapterRequest<T> & {
|
|
59
|
-
requestContext: AdapterRequestContext<T> & {
|
|
60
|
-
priceMeta: {
|
|
61
|
-
inverse: boolean;
|
|
62
|
-
};
|
|
63
|
-
};
|
|
64
|
-
};
|
|
65
41
|
/**
|
|
66
42
|
* A PriceAdapter is a specific kind of Adapter that includes at least one PriceEnpoint.
|
|
67
43
|
*/
|
|
68
44
|
export declare class PriceAdapter<CustomSettings extends SettingsMap> extends Adapter<CustomSettings> {
|
|
69
|
-
|
|
70
|
-
constructor(params: AdapterParams<CustomSettings> & {
|
|
71
|
-
includes?: IncludesFile;
|
|
72
|
-
});
|
|
73
|
-
handleRequest(req: PriceAdapterRequest<{
|
|
74
|
-
Params: PriceEndpointParams;
|
|
75
|
-
}>, replySent: Promise<unknown>): Promise<AdapterResponse>;
|
|
45
|
+
constructor(params: AdapterParams<CustomSettings>);
|
|
76
46
|
}
|
|
77
47
|
export {};
|
package/adapter/price.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.PriceAdapter = exports.PriceEndpoint = exports.priceEndpointInputParameters = void 0;
|
|
4
|
-
const endpoint_1 = require("./endpoint");
|
|
5
4
|
const index_1 = require("./index");
|
|
5
|
+
const endpoint_1 = require("./endpoint");
|
|
6
6
|
/**
|
|
7
7
|
* Base input parameter config that any [[PriceEndpoint]] must extend
|
|
8
8
|
*/
|
|
@@ -30,16 +30,6 @@ class PriceEndpoint extends endpoint_1.AdapterEndpoint {
|
|
|
30
30
|
}
|
|
31
31
|
}
|
|
32
32
|
exports.PriceEndpoint = PriceEndpoint;
|
|
33
|
-
const buildIncludesMap = (includesFile) => {
|
|
34
|
-
const includesMap = {};
|
|
35
|
-
for (const { from, to, includes } of includesFile) {
|
|
36
|
-
if (!includesMap[from]) {
|
|
37
|
-
includesMap[from] = {};
|
|
38
|
-
}
|
|
39
|
-
includesMap[from][to] = includes[0];
|
|
40
|
-
}
|
|
41
|
-
return includesMap;
|
|
42
|
-
};
|
|
43
33
|
/**
|
|
44
34
|
* A PriceAdapter is a specific kind of Adapter that includes at least one PriceEnpoint.
|
|
45
35
|
*/
|
|
@@ -50,38 +40,6 @@ class PriceAdapter extends index_1.Adapter {
|
|
|
50
40
|
throw new Error(`This PriceAdapter's list of endpoints does not contain a valid PriceEndpoint`);
|
|
51
41
|
}
|
|
52
42
|
super(params);
|
|
53
|
-
if (params.includes) {
|
|
54
|
-
// Build includes map for constant lookups
|
|
55
|
-
this.includesMap = buildIncludesMap(params.includes);
|
|
56
|
-
const requestTransform = (req) => {
|
|
57
|
-
const priceRequest = req;
|
|
58
|
-
const requestData = priceRequest.requestContext.data;
|
|
59
|
-
const includesDetails = this.includesMap?.[requestData.base]?.[requestData.quote];
|
|
60
|
-
if (includesDetails) {
|
|
61
|
-
requestData.base = includesDetails.from || requestData.base;
|
|
62
|
-
requestData.quote = includesDetails.to || requestData.quote;
|
|
63
|
-
}
|
|
64
|
-
const inverse = includesDetails?.inverse || false;
|
|
65
|
-
priceRequest.requestContext.priceMeta = {
|
|
66
|
-
inverse,
|
|
67
|
-
};
|
|
68
|
-
};
|
|
69
|
-
this.requestTransforms?.push(requestTransform);
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
async handleRequest(req, replySent) {
|
|
73
|
-
const response = await super.handleRequest(req, replySent);
|
|
74
|
-
if (this.includesMap && req.requestContext.priceMeta.inverse) {
|
|
75
|
-
// We need to search in the reverse order (quote -> base) because the request transform will have inverted the pair
|
|
76
|
-
const inverseResult = 1 / response.result;
|
|
77
|
-
response.result = inverseResult;
|
|
78
|
-
// Check if response data has a result within it
|
|
79
|
-
const data = response.data;
|
|
80
|
-
if (data?.result) {
|
|
81
|
-
data.result = inverseResult;
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
return response;
|
|
85
43
|
}
|
|
86
44
|
}
|
|
87
45
|
exports.PriceAdapter = PriceAdapter;
|
package/adapter/price.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"price.js","sourceRoot":"","sources":["../../../src/adapter/price.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"price.js","sourceRoot":"","sources":["../../../src/adapter/price.ts"],"names":[],"mappings":";;;AAAA,mCAAuE;AACvE,yCAA4C;AAqB5C;;GAEG;AACU,QAAA,4BAA4B,GAAiC;IACxE,IAAI,EAAE;QACJ,OAAO,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC;QACzB,IAAI,EAAE,QAAQ;QACd,WAAW,EAAE,gDAAgD;QAC7D,QAAQ,EAAE,IAAI;KACf;IACD,KAAK,EAAE;QACL,OAAO,EAAE,CAAC,IAAI,EAAE,QAAQ,CAAC;QACzB,IAAI,EAAE,QAAQ;QACd,WAAW,EAAE,0CAA0C;QACvD,QAAQ,EAAE,IAAI;KACf;CACF,CAAA;AAUD;;;GAGG;AACH,MAAa,aAIX,SAAQ,0BAA6D;IACrE,YACE,MAEC;QAED,KAAK,CAAC,MAAM,CAAC,CAAA;IACf,CAAC;CACF;AAZD,sCAYC;AAED;;GAEG;AACH,MAAa,YAAiD,SAAQ,eAAuB;IAC3F,YAAY,MAAqC;QAC/C,yDAAyD;QACzD,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,YAAY,aAAa,CAAC,EAAE;YAC7D,MAAM,IAAI,KAAK,CACb,8EAA8E,CAC/E,CAAA;SACF;QAED,KAAK,CAAC,MAAM,CAAC,CAAA;IACf,CAAC;CACF;AAXD,oCAWC"}
|