@chainlink/external-adapter-framework 0.0.15 → 0.0.17
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.js +128 -0
- package/background-executor.js +45 -0
- package/cache/factory.js +58 -0
- package/cache/index.js +173 -0
- package/cache/local.js +83 -0
- package/cache/metrics.js +120 -0
- package/cache/redis.js +100 -0
- package/chainlink-external-adapter-framework-v0.0.6.tgz +0 -0
- package/config/index.js +366 -0
- package/config/provider-limits.js +74 -0
- package/examples/bank-frick/accounts.js +192 -0
- package/examples/bank-frick/config/index.js +54 -0
- package/examples/bank-frick/index.js +15 -0
- package/examples/bank-frick/util.js +39 -0
- package/examples/coingecko/batch-warming.js +53 -0
- package/examples/coingecko/index.js +11 -0
- package/examples/coingecko/rest.js +51 -0
- package/examples/coingecko/src/config/index.js +13 -0
- package/examples/coingecko/src/config/overrides.json +10826 -0
- package/examples/coingecko/src/cryptoUtils.js +41 -0
- package/examples/coingecko/src/endpoint/coins.js +33 -0
- package/examples/coingecko/src/endpoint/crypto-marketcap.js +46 -0
- package/examples/coingecko/src/endpoint/crypto-volume.js +46 -0
- package/examples/coingecko/src/endpoint/crypto.js +47 -0
- package/examples/coingecko/src/endpoint/dominance.js +26 -0
- package/examples/coingecko/src/endpoint/global-marketcap.js +26 -0
- package/examples/coingecko/src/endpoint/index.js +15 -0
- package/examples/coingecko/src/globalUtils.js +48 -0
- package/examples/coingecko/src/index.js +14 -0
- package/examples/coingecko/test/e2e/adapter.test.js +262 -0
- package/examples/coingecko/test/integration/adapter.test.js +264 -0
- package/examples/coingecko/test/integration/capturedRequests.json +1 -0
- package/examples/coingecko/test/integration/fixtures.js +41 -0
- package/examples/coingecko-old/batch-warming.js +53 -0
- package/examples/coingecko-old/index.js +11 -0
- package/examples/coingecko-old/rest.js +51 -0
- package/examples/ncfx/config/index.js +15 -0
- package/examples/ncfx/index.js +11 -0
- package/examples/ncfx/websocket.js +73 -0
- package/index.js +127 -0
- package/metrics/constants.js +25 -0
- package/metrics/index.js +122 -0
- package/metrics/util.js +9 -0
- package/package.json +5 -15
- package/rate-limiting/background/fixed-frequency.js +35 -0
- package/rate-limiting/index.js +84 -0
- package/rate-limiting/metrics.js +44 -0
- package/rate-limiting/request/simple-counting.js +62 -0
- package/test.js +6 -0
- package/transports/batch-warming.js +101 -0
- package/transports/index.js +87 -0
- package/transports/metrics.js +105 -0
- package/transports/rest.js +138 -0
- package/transports/util.js +86 -0
- package/transports/websocket.js +166 -0
- package/util/index.js +35 -0
- package/util/logger.js +62 -0
- package/util/recordRequests.js +45 -0
- package/util/request.js +2 -0
- package/util/subscription-set/expiring-sorted-set.js +47 -0
- package/util/subscription-set/subscription-set.js +19 -0
- package/util/test-payload-loader.js +83 -0
- package/validation/error.js +79 -0
- package/validation/index.js +91 -0
- package/validation/input-params.js +30 -0
- package/validation/override-functions.js +40 -0
- package/validation/overrideFunctions.js +40 -0
- package/validation/preset-tokens.json +23 -0
- package/validation/validator.js +303 -0
- package/.c8rc.json +0 -3
- package/.eslintignore +0 -10
- package/.eslintrc.js +0 -96
- package/.github/README.MD +0 -42
- package/.github/actions/setup/action.yaml +0 -13
- package/.github/workflows/label.yaml +0 -39
- package/.github/workflows/main.yaml +0 -39
- package/.github/workflows/publish.yaml +0 -17
- package/.prettierignore +0 -13
- package/.yarnrc +0 -0
- package/README.md +0 -103
- package/dist/examples/coingecko/test/e2e/adapter.test.ts.js +0 -82953
- package/dist/examples/coingecko/test/integration/adapter.test.ts.js +0 -91672
- package/dist/main.js +0 -72703
- package/docker-compose.yaml +0 -35
- package/env.sh +0 -54
- package/env2.sh +0 -55
- package/jest.config.js +0 -5
- package/publish.sh +0 -0
- package/src/adapter.ts +0 -263
- package/src/background-executor.ts +0 -52
- package/src/cache/factory.ts +0 -26
- package/src/cache/index.ts +0 -258
- package/src/cache/local.ts +0 -73
- package/src/cache/metrics.ts +0 -112
- package/src/cache/redis.ts +0 -93
- package/src/config/index.ts +0 -517
- package/src/config/provider-limits.ts +0 -127
- package/src/examples/bank-frick/README.MD +0 -10
- package/src/examples/bank-frick/accounts.ts +0 -246
- package/src/examples/bank-frick/config/index.ts +0 -53
- package/src/examples/bank-frick/index.ts +0 -13
- package/src/examples/bank-frick/types.d.ts +0 -38
- package/src/examples/bank-frick/util.ts +0 -55
- package/src/examples/coingecko/src/config/index.ts +0 -12
- package/src/examples/coingecko/src/config/overrides.json +0 -10826
- package/src/examples/coingecko/src/cryptoUtils.ts +0 -88
- package/src/examples/coingecko/src/endpoint/coins.ts +0 -54
- package/src/examples/coingecko/src/endpoint/crypto-marketcap.ts +0 -66
- package/src/examples/coingecko/src/endpoint/crypto-volume.ts +0 -66
- package/src/examples/coingecko/src/endpoint/crypto.ts +0 -63
- package/src/examples/coingecko/src/endpoint/dominance.ts +0 -40
- package/src/examples/coingecko/src/endpoint/global-marketcap.ts +0 -40
- package/src/examples/coingecko/src/endpoint/index.ts +0 -6
- package/src/examples/coingecko/src/globalUtils.ts +0 -78
- package/src/examples/coingecko/src/index.ts +0 -17
- package/src/examples/coingecko/test/e2e/adapter.test.ts +0 -278
- package/src/examples/coingecko/test/integration/__snapshots__/adapter.test.ts.snap +0 -15
- package/src/examples/coingecko/test/integration/adapter.test.ts +0 -281
- package/src/examples/coingecko/test/integration/capturedRequests.json +0 -1
- package/src/examples/coingecko/test/integration/fixtures.ts +0 -42
- package/src/examples/coingecko-old/batch-warming.ts +0 -79
- package/src/examples/coingecko-old/index.ts +0 -9
- package/src/examples/coingecko-old/rest.ts +0 -77
- package/src/examples/ncfx/config/index.ts +0 -12
- package/src/examples/ncfx/index.ts +0 -9
- package/src/examples/ncfx/websocket.ts +0 -99
- package/src/index.ts +0 -149
- package/src/metrics/constants.ts +0 -23
- package/src/metrics/index.ts +0 -115
- package/src/metrics/util.ts +0 -18
- package/src/rate-limiting/background/fixed-frequency.ts +0 -45
- package/src/rate-limiting/index.ts +0 -100
- package/src/rate-limiting/metrics.ts +0 -18
- package/src/rate-limiting/request/simple-counting.ts +0 -76
- package/src/transports/batch-warming.ts +0 -127
- package/src/transports/index.ts +0 -152
- package/src/transports/metrics.ts +0 -95
- package/src/transports/rest.ts +0 -168
- package/src/transports/util.ts +0 -63
- package/src/transports/websocket.ts +0 -245
- package/src/util/index.ts +0 -23
- package/src/util/logger.ts +0 -69
- package/src/util/recordRequests.ts +0 -47
- package/src/util/request.ts +0 -117
- package/src/util/subscription-set/expiring-sorted-set.ts +0 -54
- package/src/util/subscription-set/subscription-set.ts +0 -35
- package/src/util/test-payload-loader.ts +0 -87
- package/src/validation/error.ts +0 -116
- package/src/validation/index.ts +0 -110
- package/src/validation/input-params.ts +0 -45
- package/src/validation/override-functions.ts +0 -44
- package/src/validation/overrideFunctions.ts +0 -44
- package/src/validation/preset-tokens.json +0 -23
- package/src/validation/validator.ts +0 -384
- package/test/adapter.test.ts +0 -27
- package/test/background-executor.test.ts +0 -108
- package/test/cache/cache-key.test.ts +0 -114
- package/test/cache/helper.ts +0 -100
- package/test/cache/local.test.ts +0 -54
- package/test/cache/redis.test.ts +0 -89
- package/test/correlation.test.ts +0 -114
- package/test/index.test.ts +0 -37
- package/test/metrics/feed-id.test.ts +0 -38
- package/test/metrics/helper.ts +0 -14
- package/test/metrics/labels.test.ts +0 -36
- package/test/metrics/metrics.test.ts +0 -267
- package/test/metrics/redis-metrics.test.ts +0 -113
- package/test/metrics/warmer-metrics.test.ts +0 -193
- package/test/metrics/ws-metrics.test.ts +0 -225
- package/test/rate-limit-config.test.ts +0 -242
- package/test/smoke/smoke.test.ts +0 -166
- package/test/smoke/test-payload-fail.json +0 -3
- package/test/smoke/test-payload.js +0 -22
- package/test/smoke/test-payload.json +0 -7
- package/test/transports/batch.test.ts +0 -466
- package/test/transports/rest.test.ts +0 -242
- package/test/transports/websocket.test.ts +0 -183
- package/test/tsconfig.json +0 -5
- package/test/util.ts +0 -77
- package/test/validation.test.ts +0 -178
- package/test.sh +0 -20
- package/test2.sh +0 -2
- package/tsconfig.json +0 -28
- package/typedoc.json +0 -6
- package/webpack.config.js +0 -57
- package/yarn-error.log +0 -3778
package/cache/redis.js
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
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.RedisCache = void 0;
|
|
27
|
+
const util_1 = require("../util");
|
|
28
|
+
const cacheMetrics = __importStar(require("./metrics"));
|
|
29
|
+
const logger = (0, util_1.makeLogger)('RedisCache');
|
|
30
|
+
/**
|
|
31
|
+
* Redis implementation of a Cache. It uses a simple js Object, storing entries with both
|
|
32
|
+
* a value and an expiration timestamp. Expired entries are deleted on reads (i.e. no background gc/upkeep).
|
|
33
|
+
*
|
|
34
|
+
* @typeParam T - the type for the entries' values
|
|
35
|
+
*/
|
|
36
|
+
class RedisCache {
|
|
37
|
+
constructor(client) {
|
|
38
|
+
this.client = client;
|
|
39
|
+
}
|
|
40
|
+
async get(key) {
|
|
41
|
+
logger.trace(`Getting key ${key}`);
|
|
42
|
+
const value = await this.client.get(key);
|
|
43
|
+
// Record get command sent to Redis
|
|
44
|
+
cacheMetrics.redisCommandsSentCount
|
|
45
|
+
.labels({ status: cacheMetrics.CMD_SENT_STATUS.SUCCESS, function_name: 'get' })
|
|
46
|
+
.inc();
|
|
47
|
+
if (!value) {
|
|
48
|
+
logger.debug(`No entry in redis cache for key "${key}", returning undefined`);
|
|
49
|
+
return undefined;
|
|
50
|
+
}
|
|
51
|
+
return JSON.parse(value);
|
|
52
|
+
}
|
|
53
|
+
async delete(key) {
|
|
54
|
+
logger.trace(`Deleting key ${key}`);
|
|
55
|
+
await this.client.del(key);
|
|
56
|
+
// Record delete command sent to Redis
|
|
57
|
+
cacheMetrics.redisCommandsSentCount
|
|
58
|
+
.labels({ status: cacheMetrics.CMD_SENT_STATUS.SUCCESS, function_name: 'delete' })
|
|
59
|
+
.inc();
|
|
60
|
+
}
|
|
61
|
+
async set(key, value, ttl) {
|
|
62
|
+
logger.trace(`Setting key ${key}`);
|
|
63
|
+
await this.client.set(key, JSON.stringify(value), 'PX', ttl);
|
|
64
|
+
// Only record metrics if feed Id is present, otherwise assuming value is not adapter response to record
|
|
65
|
+
const feedId = value.meta?.metrics?.feedId;
|
|
66
|
+
if (feedId) {
|
|
67
|
+
// Record cache set count, max age, and staleness (set to 0 for cache set)
|
|
68
|
+
const label = cacheMetrics.cacheMetricsLabel(key, feedId, cacheMetrics.CacheTypes.Redis);
|
|
69
|
+
cacheMetrics.cacheSet(label, ttl);
|
|
70
|
+
}
|
|
71
|
+
// Record set command sent to Redis
|
|
72
|
+
cacheMetrics.redisCommandsSentCount
|
|
73
|
+
.labels({ status: cacheMetrics.CMD_SENT_STATUS.SUCCESS, function_name: 'set' })
|
|
74
|
+
.inc();
|
|
75
|
+
}
|
|
76
|
+
async setMany(entries, ttl) {
|
|
77
|
+
logger.trace(`Setting a bunch of keys`);
|
|
78
|
+
// Unfortunately, there's no ttl for mset
|
|
79
|
+
let chain = this.client.multi();
|
|
80
|
+
for (const entry of entries) {
|
|
81
|
+
chain = chain.set(entry.key, JSON.stringify(entry.value), 'PX', ttl);
|
|
82
|
+
}
|
|
83
|
+
await chain.exec();
|
|
84
|
+
// Loop again, but this time to record these in metrics
|
|
85
|
+
for (const entry of entries) {
|
|
86
|
+
// Only record metrics if feed Id is present, otherwise assuming value is not adapter response to record
|
|
87
|
+
const feedId = entry.value.meta?.metrics?.feedId;
|
|
88
|
+
if (feedId) {
|
|
89
|
+
// Record cache set count, max age, and staleness (set to 0 for cache set)
|
|
90
|
+
const label = cacheMetrics.cacheMetricsLabel(entry.key, feedId, cacheMetrics.CacheTypes.Redis);
|
|
91
|
+
cacheMetrics.cacheSet(label, ttl);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
// Record setMany command sent to Redis
|
|
95
|
+
cacheMetrics.redisCommandsSentCount
|
|
96
|
+
.labels({ status: cacheMetrics.CMD_SENT_STATUS.SUCCESS, function_name: 'exec' })
|
|
97
|
+
.inc();
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
exports.RedisCache = RedisCache;
|
|
Binary file
|
package/config/index.js
ADDED
|
@@ -0,0 +1,366 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Import { getRandomRequiredEnv, getRandomEnv, getEnv } from '../util'
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.buildAdapterConfig = exports.BaseSettings = void 0;
|
|
5
|
+
exports.BaseSettings = {
|
|
6
|
+
// V2 compat
|
|
7
|
+
// ADAPTER_URL: {
|
|
8
|
+
// description: 'The URL of another adapter from which data needs to be retrieved',
|
|
9
|
+
// type: 'string',
|
|
10
|
+
// },
|
|
11
|
+
API_ENDPOINT: {
|
|
12
|
+
description: 'The URL that certain transports use to retrieve data',
|
|
13
|
+
type: 'string',
|
|
14
|
+
},
|
|
15
|
+
API_KEY: {
|
|
16
|
+
description: 'Default setting for an EA key',
|
|
17
|
+
type: 'string',
|
|
18
|
+
},
|
|
19
|
+
API_TIMEOUT: {
|
|
20
|
+
description: 'The number of milliseconds a request can be pending before returning a timeout error for data provider request',
|
|
21
|
+
type: 'number',
|
|
22
|
+
default: 30000,
|
|
23
|
+
},
|
|
24
|
+
API_VERBOSE: {
|
|
25
|
+
description: 'Toggle whether the response from the EA should contain just the results or also include the full response body from the queried API.',
|
|
26
|
+
type: 'boolean',
|
|
27
|
+
default: false,
|
|
28
|
+
},
|
|
29
|
+
BASE_URL: {
|
|
30
|
+
description: 'Starting path for the EA handler endpoint',
|
|
31
|
+
type: 'string',
|
|
32
|
+
default: '/',
|
|
33
|
+
},
|
|
34
|
+
CACHE_MAX_AGE: {
|
|
35
|
+
description: 'Maximum amount of time (in ms) that a response will stay cached',
|
|
36
|
+
type: 'number',
|
|
37
|
+
default: 90000,
|
|
38
|
+
},
|
|
39
|
+
// CACHE_MAX_ITEMS: {
|
|
40
|
+
// type: 'number',
|
|
41
|
+
// default: 1000,
|
|
42
|
+
// },
|
|
43
|
+
// CACHE_MIN_AGE: {
|
|
44
|
+
// type: 'number',
|
|
45
|
+
// default: 30000,
|
|
46
|
+
// },
|
|
47
|
+
// CACHE_REDIS_CONNECTION_TIMEOUT: {
|
|
48
|
+
// type: 'number',
|
|
49
|
+
// default: 15000,
|
|
50
|
+
// },
|
|
51
|
+
CACHE_REDIS_HOST: {
|
|
52
|
+
description: 'Hostname for the Redis instance to be used',
|
|
53
|
+
type: 'string',
|
|
54
|
+
default: '127.0.0.1',
|
|
55
|
+
},
|
|
56
|
+
// CACHE_REDIS_MAX_QUEUED_ITEMS: {
|
|
57
|
+
// description: 'Maximum length of the client internal command queue',
|
|
58
|
+
// type: 'number',
|
|
59
|
+
// default: 500,
|
|
60
|
+
// },
|
|
61
|
+
// CACHE_REDIS_MAX_RECONNECT_COOLDOWN: {
|
|
62
|
+
// type: 'number',
|
|
63
|
+
// default: 3000,
|
|
64
|
+
// },
|
|
65
|
+
CACHE_REDIS_PASSWORD: {
|
|
66
|
+
description: 'The password required for redis auth',
|
|
67
|
+
type: 'string',
|
|
68
|
+
},
|
|
69
|
+
CACHE_REDIS_PATH: {
|
|
70
|
+
description: 'The UNIX socket string of the Redis server',
|
|
71
|
+
type: 'string',
|
|
72
|
+
},
|
|
73
|
+
CACHE_REDIS_PORT: {
|
|
74
|
+
description: 'Port for the Redis instance to be used',
|
|
75
|
+
type: 'number',
|
|
76
|
+
default: 6379,
|
|
77
|
+
},
|
|
78
|
+
CACHE_REDIS_TIMEOUT: {
|
|
79
|
+
description: 'Timeout to fail a Redis server request if no response (ms)',
|
|
80
|
+
type: 'number',
|
|
81
|
+
default: 500,
|
|
82
|
+
},
|
|
83
|
+
// CACHE_REDIS_URL: {
|
|
84
|
+
// type: 'string',
|
|
85
|
+
// },
|
|
86
|
+
CACHE_TYPE: {
|
|
87
|
+
description: 'The type of cache to use throughout the EA',
|
|
88
|
+
type: 'enum',
|
|
89
|
+
default: 'local',
|
|
90
|
+
options: ['local', 'redis'],
|
|
91
|
+
},
|
|
92
|
+
CORRELATION_ID_ENABLED: {
|
|
93
|
+
description: 'Flag to enable correlation IDs for sent requests in logging',
|
|
94
|
+
type: 'boolean',
|
|
95
|
+
default: true,
|
|
96
|
+
},
|
|
97
|
+
// DATA_PROVIDER_URL: {
|
|
98
|
+
// description: 'Legacy variable that has the same functionality as ADAPTER_URL',
|
|
99
|
+
// type: 'string',
|
|
100
|
+
// },
|
|
101
|
+
DEBUG: {
|
|
102
|
+
description: 'Toggles debug mode',
|
|
103
|
+
type: 'boolean',
|
|
104
|
+
default: false,
|
|
105
|
+
},
|
|
106
|
+
EA_PORT: {
|
|
107
|
+
description: 'Port through which the EA will listen for REST requests (if mode is set to "reader" or "reader-writer")',
|
|
108
|
+
type: 'number',
|
|
109
|
+
default: 8080,
|
|
110
|
+
},
|
|
111
|
+
// ERROR_CAPACITY: {
|
|
112
|
+
// type: 'number',
|
|
113
|
+
// },
|
|
114
|
+
EXPERIMENTAL_METRICS_ENABLED: {
|
|
115
|
+
description: 'Flag to specify whether or not to collect metrics. Used as fallback for METRICS_ENABLED',
|
|
116
|
+
type: 'boolean',
|
|
117
|
+
default: true,
|
|
118
|
+
},
|
|
119
|
+
LOG_LEVEL: {
|
|
120
|
+
description: 'Minimum level required for logs to be output',
|
|
121
|
+
type: 'string',
|
|
122
|
+
default: 'info',
|
|
123
|
+
},
|
|
124
|
+
METRICS_ENABLED: {
|
|
125
|
+
description: 'Flag to specify whether or not to startup the metrics server',
|
|
126
|
+
type: 'boolean',
|
|
127
|
+
default: true,
|
|
128
|
+
},
|
|
129
|
+
METRICS_NAME: {
|
|
130
|
+
description: 'Metrics name',
|
|
131
|
+
type: 'string',
|
|
132
|
+
},
|
|
133
|
+
METRICS_PORT: {
|
|
134
|
+
description: 'Port metrics will be exposed to',
|
|
135
|
+
type: 'number',
|
|
136
|
+
default: 9080,
|
|
137
|
+
},
|
|
138
|
+
METRICS_USE_BASE_URL: {
|
|
139
|
+
description: 'Flag to specify whether or not to prepend the BASE_URL to the metrics endpoint',
|
|
140
|
+
type: 'boolean',
|
|
141
|
+
},
|
|
142
|
+
RATE_LIMIT_API_TIER: {
|
|
143
|
+
description: 'Rate limiting tier to use from the available options for the adapter. If not present, the adapter will run using the first tier on the list.',
|
|
144
|
+
type: 'string',
|
|
145
|
+
},
|
|
146
|
+
RATE_LIMIT_CAPACITY: {
|
|
147
|
+
description: 'Used as rate limit capacity per minute and ignores tier settings if defined',
|
|
148
|
+
type: 'number',
|
|
149
|
+
},
|
|
150
|
+
RATE_LIMIT_CAPACITY_MINUTE: {
|
|
151
|
+
description: 'Used as rate limit capacity per minute and ignores tier settings if defined. Supercedes RATE_LIMIT_CAPACITY if both vars are set',
|
|
152
|
+
type: 'number',
|
|
153
|
+
},
|
|
154
|
+
RATE_LIMIT_CAPACITY_SECOND: {
|
|
155
|
+
description: 'Used as rate limit capacity per second and ignores tier settings if defined',
|
|
156
|
+
type: 'number',
|
|
157
|
+
},
|
|
158
|
+
REQUEST_COALESCING_ENABLED: {
|
|
159
|
+
description: 'Enable request coalescing',
|
|
160
|
+
type: 'boolean',
|
|
161
|
+
},
|
|
162
|
+
// REQUEST_COALESCING_ENTROPY_MAX: {
|
|
163
|
+
// type: 'number',
|
|
164
|
+
// default: 0,
|
|
165
|
+
// },
|
|
166
|
+
// REQUEST_COALESCING_INTERVAL_COEFFICIENT: {
|
|
167
|
+
// type: 'number',
|
|
168
|
+
// default: 2,
|
|
169
|
+
// },
|
|
170
|
+
// REQUEST_COALESCING_INTERVAL_MAX: {
|
|
171
|
+
// type: 'number',
|
|
172
|
+
// default: 1000,
|
|
173
|
+
// },
|
|
174
|
+
// REQUEST_COALESCING_INTERVAL: {
|
|
175
|
+
// type: 'number',
|
|
176
|
+
// default: 100,
|
|
177
|
+
// },
|
|
178
|
+
// REQUEST_COALESCING_MAX_RETRIES: {
|
|
179
|
+
// type: 'number',
|
|
180
|
+
// default: 5,
|
|
181
|
+
// },
|
|
182
|
+
// RETRY: {
|
|
183
|
+
// type: 'boolean',
|
|
184
|
+
// default: 1,
|
|
185
|
+
// },
|
|
186
|
+
// SERVER_RATE_LIMIT_MAX: {
|
|
187
|
+
// type: 'number',
|
|
188
|
+
// default: 250,
|
|
189
|
+
// },
|
|
190
|
+
// SERVER_SLOW_DOWN_AFTER_FACTOR: {
|
|
191
|
+
// type: 'number',
|
|
192
|
+
// default: 0.8,
|
|
193
|
+
// },
|
|
194
|
+
// SERVER_SLOW_DOWN_DELAY_MS: {
|
|
195
|
+
// type: 'number',
|
|
196
|
+
// default: 500,
|
|
197
|
+
// },
|
|
198
|
+
// WARMUP_ENABLED: {
|
|
199
|
+
// type: 'boolean',
|
|
200
|
+
// default: true,
|
|
201
|
+
// },
|
|
202
|
+
// WARMUP_INTERVAL: {
|
|
203
|
+
// type: 'number',
|
|
204
|
+
// },
|
|
205
|
+
WARMUP_SUBSCRIPTION_TTL: {
|
|
206
|
+
type: 'number',
|
|
207
|
+
description: 'TTL for batch warmer subscriptions',
|
|
208
|
+
default: 10000,
|
|
209
|
+
},
|
|
210
|
+
// WARMUP_UNHEALTHY_THRESHOLD: {
|
|
211
|
+
// type: 'number',
|
|
212
|
+
// default: 3,
|
|
213
|
+
// },
|
|
214
|
+
// WS_API_ENDPOINT: {
|
|
215
|
+
// type: 'string',
|
|
216
|
+
// },
|
|
217
|
+
// WS_API_KEY: {
|
|
218
|
+
// type: 'string',
|
|
219
|
+
// },
|
|
220
|
+
// WS_CONNECTION_KEY: {
|
|
221
|
+
// type: 'string',
|
|
222
|
+
// default: 1,
|
|
223
|
+
// },
|
|
224
|
+
// WS_CONNECTION_LIMIT: {
|
|
225
|
+
// type: 'number',
|
|
226
|
+
// default: 1,
|
|
227
|
+
// },
|
|
228
|
+
// WS_CONNECTION_RETRY_DELAY: {
|
|
229
|
+
// type: 'number',
|
|
230
|
+
// default: 1000,
|
|
231
|
+
// },
|
|
232
|
+
// WS_CONNECTION_RETRY_LIMIT: {
|
|
233
|
+
// type: 'number',
|
|
234
|
+
// default: 3,
|
|
235
|
+
// },
|
|
236
|
+
// WS_CONNECTION_TTL: {
|
|
237
|
+
// type: 'number',
|
|
238
|
+
// default: 70000,
|
|
239
|
+
// },
|
|
240
|
+
// WS_ENABLED: {
|
|
241
|
+
// type: 'boolean',
|
|
242
|
+
// default: false,
|
|
243
|
+
// },
|
|
244
|
+
// WS_SUBSCRIPTION_LIMIT: {
|
|
245
|
+
// type: 'number',
|
|
246
|
+
// default: 10,
|
|
247
|
+
// },
|
|
248
|
+
// WS_SUBSCRIPTION_PRIORITY_LIST: {
|
|
249
|
+
// type: 'string',
|
|
250
|
+
// },
|
|
251
|
+
WS_SUBSCRIPTION_TTL: {
|
|
252
|
+
description: '',
|
|
253
|
+
type: 'number',
|
|
254
|
+
default: 120000,
|
|
255
|
+
},
|
|
256
|
+
// WS_SUBSCRIPTION_UNRESPONSIVE_TTL: {
|
|
257
|
+
// type: 'number',
|
|
258
|
+
// default: false,
|
|
259
|
+
// },
|
|
260
|
+
// WS_TIME_UNTIL_HANDLE_NEXT_MESSAGE_OVERRIDE: {
|
|
261
|
+
// type: 'number',
|
|
262
|
+
// },
|
|
263
|
+
// V3
|
|
264
|
+
CACHE_POLLING_MAX_RETRIES: {
|
|
265
|
+
description: 'Max amount of times to attempt to find EA response in the cache after the Transport has been set up',
|
|
266
|
+
type: 'number',
|
|
267
|
+
default: 10,
|
|
268
|
+
},
|
|
269
|
+
CACHE_POLLING_SLEEP_MS: {
|
|
270
|
+
description: 'The number of ms to sleep between each retry to fetch the EA response in the cache',
|
|
271
|
+
type: 'number',
|
|
272
|
+
default: 200,
|
|
273
|
+
},
|
|
274
|
+
DEFAULT_CACHE_KEY: {
|
|
275
|
+
description: 'Default key to be used when one cannot be determined from request parameters',
|
|
276
|
+
type: 'string',
|
|
277
|
+
default: 'DEFAULT_CACHE_KEY',
|
|
278
|
+
},
|
|
279
|
+
EA_HOST: {
|
|
280
|
+
description: 'Host this EA will listen for REST requests on (if mode is set to "reader" or "reader-writer")',
|
|
281
|
+
type: 'string',
|
|
282
|
+
default: '::',
|
|
283
|
+
},
|
|
284
|
+
EA_MODE: {
|
|
285
|
+
description: 'Port this EA will listen for REST requests on (if mode is set to "reader" or "reader-writer")',
|
|
286
|
+
type: 'enum',
|
|
287
|
+
default: 'reader-writer',
|
|
288
|
+
options: ['reader', 'writer', 'reader-writer'],
|
|
289
|
+
},
|
|
290
|
+
MAX_COMMON_KEY_SIZE: {
|
|
291
|
+
description: 'Maximum amount of characters that the common part of the cache key or feed ID can have',
|
|
292
|
+
type: 'number',
|
|
293
|
+
default: 300,
|
|
294
|
+
validate: (value) => {
|
|
295
|
+
if (!(value && value >= 150 && value <= 500)) {
|
|
296
|
+
return 'MAX_COMMON_KEY_SIZE must be a number between 150 and 500';
|
|
297
|
+
}
|
|
298
|
+
},
|
|
299
|
+
},
|
|
300
|
+
REST_TRANSPORT_MAX_RATE_LIMIT_RETRIES: {
|
|
301
|
+
description: 'Maximum amount of times the Rest Transport will attempt to set up a request when blocked by the rate limiter',
|
|
302
|
+
type: 'number',
|
|
303
|
+
default: 3,
|
|
304
|
+
},
|
|
305
|
+
REST_TRANSPORT_MS_BETWEEN_RATE_LIMIT_RETRIES: {
|
|
306
|
+
description: 'Time that the Rest Transport will wait between retries when blocked by the rate limiter',
|
|
307
|
+
type: 'number',
|
|
308
|
+
default: 400,
|
|
309
|
+
},
|
|
310
|
+
SMOKE_TEST_PAYLOAD_FILE_NAME: {
|
|
311
|
+
description: 'Name of the test payload file used for the smoke endpoint',
|
|
312
|
+
type: 'string',
|
|
313
|
+
},
|
|
314
|
+
};
|
|
315
|
+
const buildAdapterConfig = ({ overrides = {}, customSettings = {}, }) => {
|
|
316
|
+
const validationErrors = [];
|
|
317
|
+
const vars = {};
|
|
318
|
+
// Iterate base adapter env vars
|
|
319
|
+
for (const [key, config] of Object.entries(exports.BaseSettings)) {
|
|
320
|
+
const value = getEnv(key, config) ?? overrides?.[key] ?? config.default;
|
|
321
|
+
vars[key] = value;
|
|
322
|
+
}
|
|
323
|
+
// Iterate custom vars
|
|
324
|
+
for (const [key, config] of Object.entries(customSettings)) {
|
|
325
|
+
if (exports.BaseSettings[key]) {
|
|
326
|
+
throw new Error(`Custom env var "${key}" declared, but a base framework env var with that name already exists.`);
|
|
327
|
+
}
|
|
328
|
+
const value = getEnv(key, config) ?? config.default;
|
|
329
|
+
// Check if a required setting has been provided
|
|
330
|
+
if (config.required && value === null) {
|
|
331
|
+
validationErrors.push(`${key}: Value is required, but none was provided`);
|
|
332
|
+
}
|
|
333
|
+
else if (config.validate) {
|
|
334
|
+
// Cast validate to unknown because TS can't select one of multiple variants of the validate function signature
|
|
335
|
+
const validationRes = config.validate(value);
|
|
336
|
+
if (validationRes) {
|
|
337
|
+
validationErrors.push(`${key}: ${validationRes}`);
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
vars[key] = value;
|
|
341
|
+
}
|
|
342
|
+
if (validationErrors.length > 0) {
|
|
343
|
+
throw new Error(`Validation failed for the following variables:\n ${validationErrors.join('\n')}`);
|
|
344
|
+
}
|
|
345
|
+
return vars;
|
|
346
|
+
};
|
|
347
|
+
exports.buildAdapterConfig = buildAdapterConfig;
|
|
348
|
+
const getEnv = (name, config) => {
|
|
349
|
+
const value = process.env[name];
|
|
350
|
+
if (!value) {
|
|
351
|
+
return null;
|
|
352
|
+
}
|
|
353
|
+
switch (config.type) {
|
|
354
|
+
case 'string':
|
|
355
|
+
return value;
|
|
356
|
+
case 'number':
|
|
357
|
+
return parseInt(value);
|
|
358
|
+
case 'boolean':
|
|
359
|
+
return value === 'true';
|
|
360
|
+
case 'enum':
|
|
361
|
+
if (!config.options?.includes(value)) {
|
|
362
|
+
throw new Error(`Env var "${name}" has value "${value}" which is not included in the valid options (${config.options})`);
|
|
363
|
+
}
|
|
364
|
+
return value;
|
|
365
|
+
}
|
|
366
|
+
};
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getWSLimits = exports.getRateLimit = exports.getHTTPLimit = exports.DEFAULT_WS_SUBSCRIPTIONS = exports.DEFAULT_WS_CONNECTIONS = exports.BURST_UNDEFINED_QUOTA_MULTIPLE = exports.DEFAULT_MINUTE_RATE_LIMIT = void 0;
|
|
4
|
+
const util_1 = require("../util");
|
|
5
|
+
exports.DEFAULT_MINUTE_RATE_LIMIT = 60;
|
|
6
|
+
exports.BURST_UNDEFINED_QUOTA_MULTIPLE = 2;
|
|
7
|
+
exports.DEFAULT_WS_CONNECTIONS = 2;
|
|
8
|
+
exports.DEFAULT_WS_SUBSCRIPTIONS = 10;
|
|
9
|
+
const logger = (0, util_1.makeLogger)('ProviderLimits');
|
|
10
|
+
const getHTTPLimit = (provider, limits, tier, timeframe) => {
|
|
11
|
+
const providerLimit = getProviderLimits(provider, limits, tier, 'http');
|
|
12
|
+
return providerLimit?.[timeframe] || 0;
|
|
13
|
+
};
|
|
14
|
+
exports.getHTTPLimit = getHTTPLimit;
|
|
15
|
+
const getRateLimit = (provider, limits, tier) => {
|
|
16
|
+
const providerLimit = getProviderLimits(provider, limits, tier, 'http');
|
|
17
|
+
return calculateRateLimit(providerLimit);
|
|
18
|
+
};
|
|
19
|
+
exports.getRateLimit = getRateLimit;
|
|
20
|
+
const getWSLimits = (provider, limits, tier) => {
|
|
21
|
+
const providerLimit = getProviderLimits(provider, limits, tier, 'ws');
|
|
22
|
+
return calculateWSLimits(providerLimit);
|
|
23
|
+
};
|
|
24
|
+
exports.getWSLimits = getWSLimits;
|
|
25
|
+
const getProviderLimits = (provider, limits, tier, protocol) => {
|
|
26
|
+
const providerConfig = parseLimits(limits);
|
|
27
|
+
if (!providerConfig) {
|
|
28
|
+
throw new Error(`Rate Limit: Provider: "${provider}" doesn't match any provider spec in limits.json`);
|
|
29
|
+
}
|
|
30
|
+
const protocolConfig = providerConfig[protocol];
|
|
31
|
+
if (!protocolConfig) {
|
|
32
|
+
throw new Error(`Rate Limit: "${provider}" doesn't have any configuration for ${protocol} in limits.json`);
|
|
33
|
+
}
|
|
34
|
+
let limitsConfig = protocolConfig[tier.toLowerCase()];
|
|
35
|
+
if (!limitsConfig) {
|
|
36
|
+
logger.debug(`Rate Limit: "${provider} does not have tier ${tier} defined. Falling back to lowest tier"`);
|
|
37
|
+
limitsConfig = Object.values(protocolConfig)?.[0];
|
|
38
|
+
}
|
|
39
|
+
if (!limitsConfig) {
|
|
40
|
+
throw new Error(`Rate Limit: Provider: "${provider}" has no tiers defined for ${protocol} in limits.json`);
|
|
41
|
+
}
|
|
42
|
+
return limitsConfig;
|
|
43
|
+
};
|
|
44
|
+
const objectKeysToLowercase = (obj) => {
|
|
45
|
+
const lower = {};
|
|
46
|
+
for (const key in obj) {
|
|
47
|
+
lower[key.toLowerCase()] = obj[key];
|
|
48
|
+
}
|
|
49
|
+
return lower;
|
|
50
|
+
};
|
|
51
|
+
const parseLimits = (limits) => {
|
|
52
|
+
limits.http = objectKeysToLowercase(limits.http);
|
|
53
|
+
limits.ws = objectKeysToLowercase(limits.ws);
|
|
54
|
+
return limits;
|
|
55
|
+
};
|
|
56
|
+
const calculateWSLimits = (providerLimit) => {
|
|
57
|
+
return {
|
|
58
|
+
connections: providerLimit.connections,
|
|
59
|
+
subscriptions: providerLimit.subscriptions,
|
|
60
|
+
};
|
|
61
|
+
};
|
|
62
|
+
const calculateRateLimit = (providerLimit) => {
|
|
63
|
+
let quota = providerLimit.rateLimit1m;
|
|
64
|
+
if (!quota && providerLimit?.rateLimit1h) {
|
|
65
|
+
quota = providerLimit?.rateLimit1h / 60;
|
|
66
|
+
}
|
|
67
|
+
else if (!quota && providerLimit?.rateLimit1s) {
|
|
68
|
+
quota = providerLimit?.rateLimit1s * 60;
|
|
69
|
+
}
|
|
70
|
+
return {
|
|
71
|
+
second: providerLimit?.rateLimit1s || (quota / 60) * exports.BURST_UNDEFINED_QUOTA_MULTIPLE,
|
|
72
|
+
minute: quota,
|
|
73
|
+
};
|
|
74
|
+
};
|