@chainlink/external-adapter-framework 0.5.0 → 0.5.1
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 +90 -0
- package/adapter/basic.js +325 -0
- package/adapter/basic.js.map +1 -0
- package/adapter/endpoint.d.ts +17 -0
- package/adapter/endpoint.js +20 -0
- package/adapter/endpoint.js.map +1 -0
- package/adapter/index.d.ts +4 -0
- package/adapter/index.js +21 -0
- package/adapter/index.js.map +1 -0
- package/adapter/price.d.ts +77 -0
- package/adapter/price.js +88 -0
- package/adapter/price.js.map +1 -0
- package/adapter/types.d.ts +124 -0
- package/adapter/types.js +3 -0
- package/adapter/types.js.map +1 -0
- package/background-executor.d.ts +9 -0
- package/background-executor.js +97 -0
- package/background-executor.js.map +1 -0
- package/cache/factory.d.ts +6 -0
- package/cache/factory.js +24 -0
- package/cache/factory.js.map +1 -0
- package/cache/index.d.ts +87 -0
- package/cache/index.js +133 -0
- package/cache/index.js.map +1 -0
- package/cache/local.d.ts +23 -0
- package/cache/local.js +84 -0
- package/cache/local.js.map +1 -0
- package/cache/metrics.d.ts +27 -0
- package/cache/metrics.js +121 -0
- package/cache/metrics.js.map +1 -0
- package/cache/redis.d.ts +16 -0
- package/cache/redis.js +101 -0
- package/cache/redis.js.map +1 -0
- package/config/index.d.ts +298 -0
- package/config/index.js +359 -0
- package/config/index.js.map +1 -0
- package/config/provider-limits.d.ts +27 -0
- package/config/provider-limits.js +75 -0
- package/config/provider-limits.js.map +1 -0
- package/examples/bank-frick/accounts.d.ts +45 -0
- package/examples/bank-frick/accounts.js +203 -0
- package/examples/bank-frick/accounts.js.map +1 -0
- package/examples/bank-frick/config/index.d.ts +17 -0
- package/examples/bank-frick/config/index.js +55 -0
- package/examples/bank-frick/config/index.js.map +1 -0
- package/examples/bank-frick/index.d.ts +2 -0
- package/examples/bank-frick/index.js +16 -0
- package/examples/bank-frick/index.js.map +1 -0
- package/examples/bank-frick/util.d.ts +4 -0
- package/examples/bank-frick/util.js +40 -0
- package/examples/bank-frick/util.js.map +1 -0
- package/examples/coingecko/src/config/index.d.ts +2 -0
- package/examples/coingecko/src/config/index.js +6 -0
- package/examples/coingecko/src/config/index.js.map +1 -0
- package/examples/coingecko/src/config/overrides.json +10825 -0
- package/examples/coingecko/src/crypto-utils.d.ts +62 -0
- package/examples/coingecko/src/crypto-utils.js +60 -0
- package/examples/coingecko/src/crypto-utils.js.map +1 -0
- package/examples/coingecko/src/endpoint/coins.d.ts +26 -0
- package/examples/coingecko/src/endpoint/coins.js +37 -0
- package/examples/coingecko/src/endpoint/coins.js.map +1 -0
- package/examples/coingecko/src/endpoint/crypto-marketcap.d.ts +3 -0
- package/examples/coingecko/src/endpoint/crypto-marketcap.js +30 -0
- package/examples/coingecko/src/endpoint/crypto-marketcap.js.map +1 -0
- package/examples/coingecko/src/endpoint/crypto-volume.d.ts +3 -0
- package/examples/coingecko/src/endpoint/crypto-volume.js +30 -0
- package/examples/coingecko/src/endpoint/crypto-volume.js.map +1 -0
- package/examples/coingecko/src/endpoint/crypto.d.ts +3 -0
- package/examples/coingecko/src/endpoint/crypto.js +28 -0
- package/examples/coingecko/src/endpoint/crypto.js.map +1 -0
- package/examples/coingecko/src/endpoint/dominance.d.ts +3 -0
- package/examples/coingecko/src/endpoint/dominance.js +28 -0
- package/examples/coingecko/src/endpoint/dominance.js.map +1 -0
- package/examples/coingecko/src/endpoint/global-marketcap.d.ts +3 -0
- package/examples/coingecko/src/endpoint/global-marketcap.js +28 -0
- package/examples/coingecko/src/endpoint/global-marketcap.js.map +1 -0
- package/examples/coingecko/src/endpoint/index.d.ts +6 -0
- package/examples/coingecko/src/endpoint/index.js +16 -0
- package/examples/coingecko/src/endpoint/index.js.map +1 -0
- package/examples/coingecko/src/global-utils.d.ts +42 -0
- package/examples/coingecko/src/global-utils.js +47 -0
- package/examples/coingecko/src/global-utils.js.map +1 -0
- package/examples/coingecko/src/index.d.ts +4 -0
- package/examples/coingecko/src/index.js +19 -0
- package/examples/coingecko/src/index.js.map +1 -0
- 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/config/index.d.ts +2 -0
- package/examples/cryptocompare/src/config/index.js +6 -0
- package/examples/cryptocompare/src/config/index.js.map +1 -0
- package/examples/cryptocompare/src/endpoints/crypto.d.ts +40 -0
- package/examples/cryptocompare/src/endpoints/crypto.js +54 -0
- package/examples/cryptocompare/src/endpoints/crypto.js.map +1 -0
- package/examples/cryptocompare/src/endpoints/index.d.ts +1 -0
- package/examples/cryptocompare/src/endpoints/index.js +6 -0
- package/examples/cryptocompare/src/endpoints/index.js.map +1 -0
- package/examples/cryptocompare/src/index.d.ts +4 -0
- package/examples/cryptocompare/src/index.js +14 -0
- package/examples/cryptocompare/src/index.js.map +1 -0
- package/examples/genesis/config/index.d.ts +7 -0
- package/examples/genesis/config/index.js +11 -0
- package/examples/genesis/config/index.js.map +1 -0
- package/examples/genesis/index.d.ts +2 -0
- package/examples/genesis/index.js +13 -0
- package/examples/genesis/index.js.map +1 -0
- package/examples/genesis/sseStream.d.ts +27 -0
- package/examples/genesis/sseStream.js +149 -0
- package/examples/genesis/sseStream.js.map +1 -0
- 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 +2 -0
- package/examples/ncfx/index.js +12 -0
- package/examples/ncfx/index.js.map +1 -0
- package/examples/ncfx/websocket.d.ts +37 -0
- package/examples/ncfx/websocket.js +74 -0
- package/examples/ncfx/websocket.js.map +1 -0
- package/index.d.ts +17 -0
- package/index.js +174 -0
- package/index.js.map +1 -0
- package/metrics/constants.d.ts +16 -0
- package/metrics/constants.js +26 -0
- package/metrics/constants.js.map +1 -0
- package/metrics/index.d.ts +19 -0
- package/metrics/index.js +139 -0
- package/metrics/index.js.map +1 -0
- package/metrics/util.d.ts +7 -0
- package/metrics/util.js +10 -0
- package/metrics/util.js.map +1 -0
- package/package.json +50 -42
- package/rate-limiting/background/fixed-frequency.d.ts +10 -0
- package/rate-limiting/background/fixed-frequency.js +36 -0
- package/rate-limiting/background/fixed-frequency.js.map +1 -0
- package/rate-limiting/index.d.ts +56 -0
- package/rate-limiting/index.js +86 -0
- package/rate-limiting/index.js.map +1 -0
- package/rate-limiting/metrics.d.ts +3 -0
- package/rate-limiting/metrics.js +45 -0
- package/rate-limiting/metrics.js.map +1 -0
- package/rate-limiting/request/simple-counting.d.ts +20 -0
- package/rate-limiting/request/simple-counting.js +63 -0
- package/rate-limiting/request/simple-counting.js.map +1 -0
- package/transports/batch-warming.d.ts +53 -0
- package/transports/batch-warming.js +136 -0
- package/transports/batch-warming.js.map +1 -0
- package/transports/index.d.ts +108 -0
- package/transports/index.js +56 -0
- package/transports/index.js.map +1 -0
- package/transports/inference-test.d.ts +0 -0
- package/transports/inference-test.js +202 -0
- package/transports/inference-test.js.map +1 -0
- package/transports/metrics.d.ts +25 -0
- package/transports/metrics.js +122 -0
- package/transports/metrics.js.map +1 -0
- package/transports/rest.d.ts +68 -0
- package/transports/rest.js +131 -0
- package/transports/rest.js.map +1 -0
- package/transports/routing.d.ts +21 -0
- package/transports/routing.js +50 -0
- package/transports/routing.js.map +1 -0
- package/transports/sse.d.ts +61 -0
- package/transports/sse.js +97 -0
- package/transports/sse.js.map +1 -0
- package/transports/util.d.ts +9 -0
- package/transports/util.js +87 -0
- package/transports/util.js.map +1 -0
- package/transports/websocket.d.ts +95 -0
- package/transports/websocket.js +164 -0
- package/transports/websocket.js.map +1 -0
- package/util/censor/censor-list.d.ts +9 -0
- package/util/censor/censor-list.js +13 -0
- package/util/censor/censor-list.js.map +1 -0
- package/util/index.d.ts +13 -0
- package/util/index.js +38 -0
- package/util/index.js.map +1 -0
- package/util/logger.d.ts +49 -0
- package/util/logger.js +123 -0
- package/util/logger.js.map +1 -0
- package/util/request.d.ts +138 -0
- package/util/request.js +3 -0
- package/util/request.js.map +1 -0
- package/util/subscription-set/expiring-sorted-set.d.ts +21 -0
- package/util/subscription-set/expiring-sorted-set.js +36 -0
- package/util/subscription-set/expiring-sorted-set.js.map +1 -0
- package/util/subscription-set/redis-sorted-set.d.ts +9 -0
- package/util/subscription-set/redis-sorted-set.js +29 -0
- package/util/subscription-set/redis-sorted-set.js.map +1 -0
- package/util/subscription-set/subscription-set.d.ts +19 -0
- package/util/subscription-set/subscription-set.js +28 -0
- package/util/subscription-set/subscription-set.js.map +1 -0
- package/util/test-payload-loader.d.ts +26 -0
- package/util/test-payload-loader.js +85 -0
- package/util/test-payload-loader.js.map +1 -0
- package/validation/error.d.ts +48 -0
- package/validation/error.js +78 -0
- package/validation/error.js.map +1 -0
- package/validation/index.d.ts +5 -0
- package/validation/index.js +102 -0
- package/validation/index.js.map +1 -0
- package/validation/input-params.d.ts +14 -0
- package/validation/input-params.js +3 -0
- package/validation/input-params.js.map +1 -0
- package/validation/input-validator.d.ts +16 -0
- package/validation/input-validator.js +123 -0
- package/validation/input-validator.js.map +1 -0
- package/README.md +0 -102
package/index.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `prom-client` is shared between v2 and v3 when both are dependencies, resulting in metric collision errors
|
|
3
|
+
* Clearing metrics on import will wipe v2's existing register so we can replace them with v3's metrics
|
|
4
|
+
* METRICS_ENABLED=false is used to trigger this behavior because generators in the monorepo (where v2 and v3 are dependencies) tend to set it to false.
|
|
5
|
+
*/
|
|
6
|
+
import { FastifyInstance } from 'fastify';
|
|
7
|
+
import { Adapter, AdapterDependencies } from './adapter';
|
|
8
|
+
export { FastifyInstance as ServerInstance };
|
|
9
|
+
/**
|
|
10
|
+
* Main function for the framework.
|
|
11
|
+
* Initializes config and dependencies, uses those to initialize Transports, and starts listening for requests.
|
|
12
|
+
*
|
|
13
|
+
* @param adapter - an object describing an External Adapter
|
|
14
|
+
* @param dependencies - an optional object with adapter dependencies to inject
|
|
15
|
+
* @returns a Promise that resolves to the http.Server listening for connections
|
|
16
|
+
*/
|
|
17
|
+
export declare const expose: (adapter: Adapter, dependencies?: Partial<AdapterDependencies>) => Promise<FastifyInstance | undefined>;
|
package/index.js
ADDED
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* `prom-client` is shared between v2 and v3 when both are dependencies, resulting in metric collision errors
|
|
4
|
+
* Clearing metrics on import will wipe v2's existing register so we can replace them with v3's metrics
|
|
5
|
+
* METRICS_ENABLED=false is used to trigger this behavior because generators in the monorepo (where v2 and v3 are dependencies) tend to set it to false.
|
|
6
|
+
*/
|
|
7
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
8
|
+
if (k2 === undefined) k2 = k;
|
|
9
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
10
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
11
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
12
|
+
}
|
|
13
|
+
Object.defineProperty(o, k2, desc);
|
|
14
|
+
}) : (function(o, m, k, k2) {
|
|
15
|
+
if (k2 === undefined) k2 = k;
|
|
16
|
+
o[k2] = m[k];
|
|
17
|
+
}));
|
|
18
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
19
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
20
|
+
}) : function(o, v) {
|
|
21
|
+
o["default"] = v;
|
|
22
|
+
});
|
|
23
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
24
|
+
if (mod && mod.__esModule) return mod;
|
|
25
|
+
var result = {};
|
|
26
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
27
|
+
__setModuleDefault(result, mod);
|
|
28
|
+
return result;
|
|
29
|
+
};
|
|
30
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
31
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
32
|
+
};
|
|
33
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
34
|
+
exports.expose = void 0;
|
|
35
|
+
const client = __importStar(require("prom-client"));
|
|
36
|
+
// , so when it's false, we can clear any metrics before reg
|
|
37
|
+
if (process.env['METRICS_ENABLED'] === 'false') {
|
|
38
|
+
client.register.clear();
|
|
39
|
+
}
|
|
40
|
+
const fastify_1 = __importDefault(require("fastify"));
|
|
41
|
+
const path_1 = require("path");
|
|
42
|
+
const adapter_1 = require("./adapter");
|
|
43
|
+
const background_executor_1 = require("./background-executor");
|
|
44
|
+
const metrics_1 = require("./metrics");
|
|
45
|
+
const util_1 = require("./util");
|
|
46
|
+
const test_payload_loader_1 = require("./util/test-payload-loader");
|
|
47
|
+
const validation_1 = require("./validation");
|
|
48
|
+
const logger = (0, util_1.makeLogger)('Main');
|
|
49
|
+
const VERSION = process.env['npm_package_version'];
|
|
50
|
+
/**
|
|
51
|
+
* Main function for the framework.
|
|
52
|
+
* Initializes config and dependencies, uses those to initialize Transports, and starts listening for requests.
|
|
53
|
+
*
|
|
54
|
+
* @param adapter - an object describing an External Adapter
|
|
55
|
+
* @param dependencies - an optional object with adapter dependencies to inject
|
|
56
|
+
* @returns a Promise that resolves to the http.Server listening for connections
|
|
57
|
+
*/
|
|
58
|
+
const expose = async (adapter, dependencies) => {
|
|
59
|
+
if (!(adapter instanceof adapter_1.Adapter)) {
|
|
60
|
+
throw new Error('The adapter has not been initialized as an instance of the Adapter class, exiting.');
|
|
61
|
+
}
|
|
62
|
+
// Initialize adapter (create dependencies, inject them, build endpoint map, etc.)
|
|
63
|
+
await adapter.initialize(dependencies);
|
|
64
|
+
let api = undefined;
|
|
65
|
+
if (adapter.config.METRICS_ENABLED && adapter.config.EXPERIMENTAL_METRICS_ENABLED) {
|
|
66
|
+
(0, metrics_1.setupMetricsServer)(adapter.name, adapter.config);
|
|
67
|
+
}
|
|
68
|
+
// Optional Promise to indicate that the API is shutting down (for us to close background executors)
|
|
69
|
+
let apiShutdownPromise;
|
|
70
|
+
if (adapter.config.EA_MODE === 'reader' || adapter.config.EA_MODE === 'reader-writer') {
|
|
71
|
+
// Main REST API server to handle incoming requests
|
|
72
|
+
api = await buildRestApi(adapter);
|
|
73
|
+
// Add a hook on close to use on the background execution loop to stop it
|
|
74
|
+
apiShutdownPromise = new Promise((resolve) => {
|
|
75
|
+
api?.addHook('onClose', async () => resolve());
|
|
76
|
+
});
|
|
77
|
+
// Start listening for incoming requests
|
|
78
|
+
try {
|
|
79
|
+
await api.listen({
|
|
80
|
+
port: adapter.config.EA_PORT,
|
|
81
|
+
host: adapter.config.EA_HOST,
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
catch (err) {
|
|
85
|
+
logger.fatal(`There was an error when starting the EA server: ${err}`);
|
|
86
|
+
process.exit();
|
|
87
|
+
}
|
|
88
|
+
logger.info(`Listening on port ${api.server.address().port}`);
|
|
89
|
+
}
|
|
90
|
+
else {
|
|
91
|
+
logger.info('REST API is disabled; this instance will not process incoming requests.');
|
|
92
|
+
}
|
|
93
|
+
if (adapter.config.EA_MODE === 'writer' || adapter.config.EA_MODE === 'reader-writer') {
|
|
94
|
+
// Start background loop that will take care of calling any async Transports
|
|
95
|
+
logger.info('Starting background execution loop');
|
|
96
|
+
(0, background_executor_1.callBackgroundExecutes)(adapter, apiShutdownPromise);
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
logger.info('Background executor is disabled; this instance will not perform async background executes.');
|
|
100
|
+
}
|
|
101
|
+
return api;
|
|
102
|
+
};
|
|
103
|
+
exports.expose = expose;
|
|
104
|
+
async function buildRestApi(adapter) {
|
|
105
|
+
const app = (0, fastify_1.default)();
|
|
106
|
+
// Add healthcheck endpoint before middlewares to bypass them
|
|
107
|
+
app.get((0, path_1.join)(adapter.config.BASE_URL, 'health'), (req, res) => {
|
|
108
|
+
res.status(200).send({ message: 'OK', version: VERSION });
|
|
109
|
+
});
|
|
110
|
+
// Use global error handling
|
|
111
|
+
app.setErrorHandler(validation_1.errorCatchingMiddleware);
|
|
112
|
+
// Always reply with json content
|
|
113
|
+
app.addHook('preHandler', (_, reply, done) => {
|
|
114
|
+
reply.headers({ 'content-type': 'application/json; charset=utf-8' });
|
|
115
|
+
done();
|
|
116
|
+
});
|
|
117
|
+
app.register(async (router) => {
|
|
118
|
+
// Set up "middlewares" (hooks in fastify)
|
|
119
|
+
router.addHook('preHandler', (0, validation_1.validatorMiddleware)(adapter));
|
|
120
|
+
if (adapter.config['CORRELATION_ID_ENABLED']) {
|
|
121
|
+
router.addHook('onRequest', util_1.loggingContextMiddleware);
|
|
122
|
+
}
|
|
123
|
+
router.route({
|
|
124
|
+
url: adapter.config.BASE_URL,
|
|
125
|
+
method: 'POST',
|
|
126
|
+
handler: async (req, reply) => reply.send(await adapter.handleRequest(req, reply)),
|
|
127
|
+
});
|
|
128
|
+
if (adapter.config.METRICS_ENABLED && adapter.config.EXPERIMENTAL_METRICS_ENABLED) {
|
|
129
|
+
router.addHook('onResponse', metrics_1.buildMetricsMiddleware);
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
// Add smoke endpoint after middleware which are needed for tests
|
|
133
|
+
buildSmokeEndpoint(app, adapter.config);
|
|
134
|
+
return app;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Adds the /smoke endpoint to the API for smoke testing the adapter
|
|
138
|
+
*
|
|
139
|
+
* @param app - the Fastify instance
|
|
140
|
+
* @param config - the initialized adapter config
|
|
141
|
+
*/
|
|
142
|
+
function buildSmokeEndpoint(app, config) {
|
|
143
|
+
app.get((0, path_1.join)(config.BASE_URL, 'smoke'), async (_, res) => {
|
|
144
|
+
const testPayload = (0, test_payload_loader_1.loadTestPayload)(config.SMOKE_TEST_PAYLOAD_FILE_NAME);
|
|
145
|
+
if (testPayload.isDefault) {
|
|
146
|
+
return res.status(200).send('OK');
|
|
147
|
+
}
|
|
148
|
+
const errors = [];
|
|
149
|
+
for (const index in testPayload.requests) {
|
|
150
|
+
try {
|
|
151
|
+
const request = { id: index, data: testPayload.requests[index] };
|
|
152
|
+
// Use Fastify's app inject to pass smoke requests internally
|
|
153
|
+
const response = await app.inject({
|
|
154
|
+
method: 'POST',
|
|
155
|
+
url: '/',
|
|
156
|
+
payload: request,
|
|
157
|
+
});
|
|
158
|
+
const parsedResponse = JSON.parse(response.body);
|
|
159
|
+
// Throw error if not 2xx status code
|
|
160
|
+
if (parsedResponse.statusCode < 200 || parsedResponse.statusCode > 299) {
|
|
161
|
+
throw Error('Smoke test request failed');
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
catch (e) {
|
|
165
|
+
errors.push(e);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
if (errors.length > 0) {
|
|
169
|
+
return res.status(500).send(errors);
|
|
170
|
+
}
|
|
171
|
+
return res.status(200).send('OK');
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
//# sourceMappingURL=index.js.map
|
package/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";AAAA;;;;GAIG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEH,oDAAqC;AACrC,4DAA4D;AAC5D,IAAI,OAAO,CAAC,GAAG,CAAC,iBAAiB,CAAC,KAAK,OAAO,EAAE;IAC9C,MAAM,CAAC,QAAQ,CAAC,KAAK,EAAE,CAAA;CACxB;AAED,sDAAkD;AAElD,+BAA2B;AAC3B,uCAAwD;AACxD,+DAA8D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cacheHit",
|
|
3
|
+
DATA_PROVIDER_HIT = "dataProviderHit",
|
|
4
|
+
ADAPTER_ERROR = "adapterError",
|
|
5
|
+
INPUT_ERROR = "inputError",
|
|
6
|
+
RATE_LIMIT_ERROR = "rateLimitError",
|
|
7
|
+
DP_ERROR = "dataProviderError",
|
|
8
|
+
TIMEOUT_ERROR = "timeoutError",
|
|
9
|
+
CONNECTION_ERROR = "connectionError",
|
|
10
|
+
CUSTOM_ERROR = "customError"
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Maxiumum number of characters that a feedId can contain.
|
|
14
|
+
*/
|
|
15
|
+
export declare const MAX_FEED_ID_LENGTH = 300;
|
|
16
|
+
export declare const requestDurationBuckets: number[];
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.requestDurationBuckets = exports.MAX_FEED_ID_LENGTH = exports.HttpRequestType = void 0;
|
|
4
|
+
var HttpRequestType;
|
|
5
|
+
(function (HttpRequestType) {
|
|
6
|
+
HttpRequestType["CACHE_HIT"] = "cacheHit";
|
|
7
|
+
HttpRequestType["DATA_PROVIDER_HIT"] = "dataProviderHit";
|
|
8
|
+
HttpRequestType["ADAPTER_ERROR"] = "adapterError";
|
|
9
|
+
HttpRequestType["INPUT_ERROR"] = "inputError";
|
|
10
|
+
HttpRequestType["RATE_LIMIT_ERROR"] = "rateLimitError";
|
|
11
|
+
// BURST_LIMIT_ERROR = 'burstLimitError',
|
|
12
|
+
// BACKOFF_ERROR = 'backoffError',
|
|
13
|
+
HttpRequestType["DP_ERROR"] = "dataProviderError";
|
|
14
|
+
HttpRequestType["TIMEOUT_ERROR"] = "timeoutError";
|
|
15
|
+
HttpRequestType["CONNECTION_ERROR"] = "connectionError";
|
|
16
|
+
// RES_EMPTY_ERROR = 'responseEmptyError',
|
|
17
|
+
// RES_INVALID_ERROR = 'responseInvalidError',
|
|
18
|
+
HttpRequestType["CUSTOM_ERROR"] = "customError";
|
|
19
|
+
})(HttpRequestType = exports.HttpRequestType || (exports.HttpRequestType = {}));
|
|
20
|
+
/**
|
|
21
|
+
* Maxiumum number of characters that a feedId can contain.
|
|
22
|
+
*/
|
|
23
|
+
exports.MAX_FEED_ID_LENGTH = 300;
|
|
24
|
+
// We should tune these as we collect data, this is the default bucket distribution that prom comes with
|
|
25
|
+
exports.requestDurationBuckets = [0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10];
|
|
26
|
+
//# sourceMappingURL=constants.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../../../src/metrics/constants.ts"],"names":[],"mappings":";;;AAAA,IAAY,eAcX;AAdD,WAAY,eAAe;IACzB,yCAAsB,CAAA;IACtB,wDAAqC,CAAA;IACrC,iDAA8B,CAAA;IAC9B,6CAA0B,CAAA;IAC1B,sDAAmC,CAAA;IACnC,yCAAyC;IACzC,kCAAkC;IAClC,iDAA8B,CAAA;IAC9B,iDAA8B,CAAA;IAC9B,uDAAoC,CAAA;IACpC,0CAA0C;IAC1C,8CAA8C;IAC9C,+CAA4B,CAAA;AAC9B,CAAC,EAdW,eAAe,GAAf,uBAAe,KAAf,uBAAe,QAc1B;AAED;;GAEG;AACU,QAAA,kBAAkB,GAAG,GAAG,CAAA;AAErC,wGAAwG;AAC3F,QAAA,sBAAsB,GAAG,CAAC,KAAK,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,GAAG,EAAE,IAAI,EAAE,GAAG,EAAE,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,EAAE,CAAC,CAAA"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import * as client from 'prom-client';
|
|
2
|
+
import { AdapterRequest } from '../util';
|
|
3
|
+
import { AdapterConfig } from '../config';
|
|
4
|
+
import { FastifyReply, HookHandlerDoneFunction } from 'fastify';
|
|
5
|
+
import { AdapterError } from '../validation/error';
|
|
6
|
+
export declare function setupMetricsServer(name: string, config: AdapterConfig): void;
|
|
7
|
+
export declare const setupMetrics: (name: string, config: AdapterConfig) => void;
|
|
8
|
+
/**
|
|
9
|
+
* Builds metrics middleware that records end to end EA response times
|
|
10
|
+
* and count of requests
|
|
11
|
+
*
|
|
12
|
+
* @returns the cache middleware function
|
|
13
|
+
*/
|
|
14
|
+
export declare const buildMetricsMiddleware: (req: AdapterRequest, res: FastifyReply, done: HookHandlerDoneFunction) => void;
|
|
15
|
+
export declare const buildHttpRequestMetricsLabel: (feedId: string, error?: AdapterError | Error, cacheHit?: boolean) => Partial<Record<"type" | "method" | "feed_id" | "status_code" | "provider_status_code" | "retry" | "is_cache_warming", string | number>>;
|
|
16
|
+
export declare const httpRequestsTotal: client.Counter<"type" | "method" | "feed_id" | "status_code" | "provider_status_code" | "retry" | "is_cache_warming">;
|
|
17
|
+
export declare const httpRequestDurationSeconds: client.Histogram<string>;
|
|
18
|
+
export declare const bgExecuteTotal: client.Counter<"endpoint">;
|
|
19
|
+
export declare const bgExecuteDurationSeconds: client.Gauge<"endpoint">;
|
package/metrics/index.js
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
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.bgExecuteDurationSeconds = exports.bgExecuteTotal = exports.httpRequestDurationSeconds = exports.httpRequestsTotal = exports.buildHttpRequestMetricsLabel = exports.buildMetricsMiddleware = exports.setupMetrics = exports.setupMetricsServer = void 0;
|
|
30
|
+
const client = __importStar(require("prom-client"));
|
|
31
|
+
const constants_1 = require("./constants");
|
|
32
|
+
const util_1 = require("../util");
|
|
33
|
+
const fastify_1 = __importDefault(require("fastify"));
|
|
34
|
+
const path_1 = require("path");
|
|
35
|
+
const error_1 = require("../validation/error");
|
|
36
|
+
const logger = (0, util_1.makeLogger)('Metrics');
|
|
37
|
+
function setupMetricsServer(name, config) {
|
|
38
|
+
const metricsApp = (0, fastify_1.default)({
|
|
39
|
+
logger: false,
|
|
40
|
+
});
|
|
41
|
+
const metricsPort = config.METRICS_PORT;
|
|
42
|
+
const endpoint = config.METRICS_USE_BASE_URL ? (0, path_1.join)(config.BASE_URL, 'metrics') : '/metrics';
|
|
43
|
+
const eaHost = config.EA_HOST;
|
|
44
|
+
(0, exports.setupMetrics)(name, config);
|
|
45
|
+
metricsApp.get(endpoint, async (_, res) => {
|
|
46
|
+
res.type('txt');
|
|
47
|
+
res.send(await client.register.metrics());
|
|
48
|
+
});
|
|
49
|
+
metricsApp.listen({
|
|
50
|
+
port: metricsPort,
|
|
51
|
+
host: eaHost,
|
|
52
|
+
}, () => logger.info(`Monitoring listening on port ${metricsPort}!`));
|
|
53
|
+
}
|
|
54
|
+
exports.setupMetricsServer = setupMetricsServer;
|
|
55
|
+
const setupMetrics = (name, config) => {
|
|
56
|
+
client.collectDefaultMetrics();
|
|
57
|
+
client.register.setDefaultLabels({
|
|
58
|
+
app_name: config.METRICS_NAME || name || 'N/A',
|
|
59
|
+
app_version: process.env['npm_package_version'],
|
|
60
|
+
});
|
|
61
|
+
};
|
|
62
|
+
exports.setupMetrics = setupMetrics;
|
|
63
|
+
/**
|
|
64
|
+
* Builds metrics middleware that records end to end EA response times
|
|
65
|
+
* and count of requests
|
|
66
|
+
*
|
|
67
|
+
* @returns the cache middleware function
|
|
68
|
+
*/
|
|
69
|
+
const buildMetricsMiddleware = (req, res, done) => {
|
|
70
|
+
// The request context can technically be empty if the input validation failed
|
|
71
|
+
const feedId = req.requestContext?.meta?.metrics?.feedId || 'N/A';
|
|
72
|
+
const labels = (0, exports.buildHttpRequestMetricsLabel)(feedId, req.requestContext?.meta?.error, req.requestContext?.meta?.metrics?.cacheHit);
|
|
73
|
+
// Record number of requests sent to EA
|
|
74
|
+
exports.httpRequestsTotal.labels(labels).inc();
|
|
75
|
+
// Record response time of request through entire EA
|
|
76
|
+
exports.httpRequestDurationSeconds.observe(res.getResponseTime() / 1000);
|
|
77
|
+
logger.debug(`Response time for ${feedId}: ${res.getResponseTime()}ms`);
|
|
78
|
+
done();
|
|
79
|
+
};
|
|
80
|
+
exports.buildMetricsMiddleware = buildMetricsMiddleware;
|
|
81
|
+
const buildHttpRequestMetricsLabel = (feedId, error, cacheHit) => {
|
|
82
|
+
const labels = {};
|
|
83
|
+
labels.method = 'POST';
|
|
84
|
+
labels.feed_id = feedId;
|
|
85
|
+
if (error instanceof error_1.AdapterError) {
|
|
86
|
+
// If error present and an instace of AdapterError, build label from error info
|
|
87
|
+
labels.type = error?.metricsLabel || constants_1.HttpRequestType.ADAPTER_ERROR;
|
|
88
|
+
labels.status_code = error?.statusCode;
|
|
89
|
+
labels.provider_status_code = error?.providerStatusCode;
|
|
90
|
+
}
|
|
91
|
+
else if (error instanceof Error) {
|
|
92
|
+
// If error present and not instance of generic Error, unexpected failure occurred
|
|
93
|
+
labels.type = constants_1.HttpRequestType.ADAPTER_ERROR;
|
|
94
|
+
labels.status_code = 500;
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
// If no error present, request went as expected
|
|
98
|
+
labels.status_code = 200;
|
|
99
|
+
if (cacheHit) {
|
|
100
|
+
labels.type = constants_1.HttpRequestType.CACHE_HIT;
|
|
101
|
+
}
|
|
102
|
+
else {
|
|
103
|
+
labels.type = constants_1.HttpRequestType.DATA_PROVIDER_HIT;
|
|
104
|
+
labels.provider_status_code = 200;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return labels;
|
|
108
|
+
};
|
|
109
|
+
exports.buildHttpRequestMetricsLabel = buildHttpRequestMetricsLabel;
|
|
110
|
+
exports.httpRequestsTotal = new client.Counter({
|
|
111
|
+
name: 'http_requests_total',
|
|
112
|
+
help: 'The number of http requests this external adapter has serviced for its entire uptime',
|
|
113
|
+
labelNames: [
|
|
114
|
+
'method',
|
|
115
|
+
'status_code',
|
|
116
|
+
'retry',
|
|
117
|
+
'type',
|
|
118
|
+
'is_cache_warming',
|
|
119
|
+
'feed_id',
|
|
120
|
+
'provider_status_code',
|
|
121
|
+
],
|
|
122
|
+
});
|
|
123
|
+
exports.httpRequestDurationSeconds = new client.Histogram({
|
|
124
|
+
name: 'http_request_duration_seconds',
|
|
125
|
+
help: 'A histogram bucket of the distribution of http request durations',
|
|
126
|
+
buckets: constants_1.requestDurationBuckets,
|
|
127
|
+
});
|
|
128
|
+
// V3 specific metrics
|
|
129
|
+
exports.bgExecuteTotal = new client.Counter({
|
|
130
|
+
name: 'bg_execute_total',
|
|
131
|
+
help: 'The number of background executes performed per endpoint',
|
|
132
|
+
labelNames: ['endpoint'],
|
|
133
|
+
});
|
|
134
|
+
exports.bgExecuteDurationSeconds = new client.Gauge({
|
|
135
|
+
name: 'bg_execute_duration_seconds',
|
|
136
|
+
help: 'A histogram bucket of the distribution of background execute durations',
|
|
137
|
+
labelNames: ['endpoint'],
|
|
138
|
+
});
|
|
139
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/metrics/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,oDAAqC;AACrC,2CAAqE;AACrE,kCAAoD;AAEpD,sDAAwE;AACxE,+BAA2B;AAC3B,+CAAkD;AAElD,MAAM,MAAM,GAAG,IAAA,iBAAU,EAAC,SAAS,CAAC,CAAA;AAEpC,SAAgB,kBAAkB,CAAC,IAAY,EAAE,MAAqB;IACpE,MAAM,UAAU,GAAG,IAAA,iBAAO,EAAC;QACzB,MAAM,EAAE,KAAK;KACd,CAAC,CAAA;IACF,MAAM,WAAW,GAAG,MAAM,CAAC,YAAY,CAAA;IACvC,MAAM,QAAQ,GAAG,MAAM,CAAC,oBAAoB,CAAC,CAAC,CAAC,IAAA,WAAI,EAAC,MAAM,CAAC,QAAQ,EAAE,SAAS,CAAC,CAAC,CAAC,CAAC,UAAU,CAAA;IAC5F,MAAM,MAAM,GAAG,MAAM,CAAC,OAAO,CAAA;IAE7B,IAAA,oBAAY,EAAC,IAAI,EAAE,MAAM,CAAC,CAAA;IAE1B,UAAU,CAAC,GAAG,CAAC,QAAQ,EAAE,KAAK,EAAE,CAAC,EAAE,GAAG,EAAE,EAAE;QACxC,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;QACf,GAAG,CAAC,IAAI,CAAC,MAAM,MAAM,CAAC,QAAQ,CAAC,OAAO,EAAE,CAAC,CAAA;IAC3C,CAAC,CAAC,CAAA;IAEF,UAAU,CAAC,MAAM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AAI,EAAE,+BAA+B;IACrC,IAAI,EAAE,kEAAkE;IACxE,OAAO,EAAE,kCAAsB;CAChC,CAAC,CAAA;AAEF,sBAAsB;AACT,QAAA,cAAc,GAAG,IAAI,MAAM,CAAC,OAAO,CAAC;IAC/C,IAAI,EAAE,kBAAkB;IACxB,IAAI,EAAE,0DAA0D;IAChE,UAAU,EAAE,CAAC,UAAU,CAAU;CAClC,CAAC,CAAA;AAEW,QAAA,wBAAwB,GAAG,IAAI,MAAM,CAAC,KAAK,CAAC;IACvD,IAAI,EAAE,6BAA6B;IACnC,IAAI,EAAE,wEAAwE;IAC9E,UAAU,EAAE,CAAC,UAAU,CAAU;CAClC,CAAC,CAAA"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { AdapterEndpoint } from '../adapter';
|
|
2
|
+
import { AdapterConfig } from '../config';
|
|
3
|
+
import { AdapterMetricsMeta, AdapterRequestData } from '../util';
|
|
4
|
+
export declare const getMetricsMeta: <T extends import("../transports").TransportGenerics>({ adapterEndpoint, adapterConfig, }: {
|
|
5
|
+
adapterEndpoint: AdapterEndpoint<T>;
|
|
6
|
+
adapterConfig: AdapterConfig<T["CustomSettings"]>;
|
|
7
|
+
}, data: AdapterRequestData) => AdapterMetricsMeta;
|
package/metrics/util.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getMetricsMeta = void 0;
|
|
4
|
+
const cache_1 = require("../cache");
|
|
5
|
+
const getMetricsMeta = ({ adapterEndpoint, adapterConfig, }, data) => {
|
|
6
|
+
const feedId = (0, cache_1.calculateFeedId)({ adapterEndpoint, adapterConfig }, data);
|
|
7
|
+
return { feedId };
|
|
8
|
+
};
|
|
9
|
+
exports.getMetricsMeta = getMetricsMeta;
|
|
10
|
+
//# sourceMappingURL=util.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"util.js","sourceRoot":"","sources":["../../../src/metrics/util.ts"],"names":[],"mappings":";;;AACA,oCAA0C;AAInC,MAAM,cAAc,GAAG,CAC5B,EACE,eAAe,EACf,aAAa,GAId,EACD,IAAwB,EACJ,EAAE;IACtB,MAAM,MAAM,GAAG,IAAA,uBAAe,EAAC,EAAE,eAAe,EAAE,aAAa,EAAE,EAAE,IAAI,CAAC,CAAA;IACxE,OAAO,EAAE,MAAM,EAAE,CAAA;AACnB,CAAC,CAAA;AAZY,QAAA,cAAc,kBAY1B"}
|
package/package.json
CHANGED
|
@@ -1,47 +1,58 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@chainlink/external-adapter-framework",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.1",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"dependencies": {
|
|
7
|
-
"
|
|
8
|
-
"
|
|
9
|
-
"
|
|
10
|
-
"
|
|
11
|
-
"
|
|
12
|
-
"
|
|
13
|
-
"
|
|
14
|
-
"
|
|
15
|
-
"ws": "^8.5.0"
|
|
7
|
+
"ajv": "8.11.0",
|
|
8
|
+
"axios": "0.27.2",
|
|
9
|
+
"eventsource": "2.0.2",
|
|
10
|
+
"fastify": "4.7.0",
|
|
11
|
+
"ioredis": "5.2.3",
|
|
12
|
+
"pino": "8.6.1",
|
|
13
|
+
"prom-client": "13.2.0",
|
|
14
|
+
"ws": "8.9.0"
|
|
16
15
|
},
|
|
17
16
|
"scripts": {
|
|
18
|
-
"start": "ts-node src/test.ts",
|
|
19
|
-
"generate-docs": "typedoc src/**/*.ts",
|
|
20
|
-
"test": "LOG_LEVEL=error EA_PORT=0 c8 ava",
|
|
21
17
|
"build": "tsc",
|
|
22
|
-
"lint": "eslint ./src && prettier --check ./src/**/*.ts",
|
|
23
|
-
"lint-fix": "eslint --fix ./src && prettier --write ./src/**/*.ts",
|
|
24
18
|
"dev": "NODE_ENV=develop tsnd --respawn --transpile-only --project tsconfig.json './src/test.ts'",
|
|
25
|
-
"
|
|
19
|
+
"generate-docs": "typedoc src/**/*.ts",
|
|
20
|
+
"lint-fix": "eslint --max-warnings=0 --fix ./src && prettier --write ./src/**/*.ts ./*.{json,js,yaml}",
|
|
21
|
+
"lint": "eslint --max-warnings=0 ./src && prettier --check ./src/**/*.ts ./*.{json,js,yaml}",
|
|
22
|
+
"release": "./release.sh",
|
|
23
|
+
"start": "ts-node -e 'import(`./src/examples/${process.argv[1]}/src/index`).then(ea => ea.server())'",
|
|
24
|
+
"test-debug": "EA_HOST=localhost SKIP_METRICS_CLEAR=true LOG_LEVEL=trace DEBUG=true EA_PORT=0 c8 ava --verbose",
|
|
25
|
+
"test": "EA_HOST=localhost SKIP_METRICS_CLEAR=true LOG_LEVEL=error EA_PORT=0 c8 ava",
|
|
26
|
+
"verify": "yarn lint && yarn build && yarn build -p ./test/tsconfig.json && yarn test",
|
|
27
|
+
"code-coverage": "c8 check-coverage --statements 95 --lines 95 --functions 95 --branches 90"
|
|
26
28
|
},
|
|
27
29
|
"devDependencies": {
|
|
28
|
-
"@sinonjs/fake-timers": "
|
|
29
|
-
"@types/
|
|
30
|
-
"@types/
|
|
31
|
-
"@types/
|
|
32
|
-
"@types/
|
|
33
|
-
"@
|
|
34
|
-
"@
|
|
35
|
-
"
|
|
36
|
-
"
|
|
37
|
-
"
|
|
38
|
-
"
|
|
39
|
-
"eslint
|
|
40
|
-
"
|
|
41
|
-
"
|
|
42
|
-
"
|
|
43
|
-
"
|
|
44
|
-
"
|
|
30
|
+
"@sinonjs/fake-timers": "9.1.2",
|
|
31
|
+
"@types/eventsource": "1.1.9",
|
|
32
|
+
"@types/jest": "29.1.1",
|
|
33
|
+
"@types/node": "18.8.2",
|
|
34
|
+
"@types/sinonjs__fake-timers": "8.1.2",
|
|
35
|
+
"@types/supertest": "2.0.12",
|
|
36
|
+
"@types/ws": "8.5.3",
|
|
37
|
+
"@typescript-eslint/eslint-plugin": "5.39.0",
|
|
38
|
+
"@typescript-eslint/parser": "5.39.0",
|
|
39
|
+
"ava": "4.3.3",
|
|
40
|
+
"c8": "7.12.0",
|
|
41
|
+
"eslint": "8.24.0",
|
|
42
|
+
"eslint-config-prettier": "8.5.0",
|
|
43
|
+
"eslint-plugin-tsdoc": "0.2.17",
|
|
44
|
+
"jest": "29.1.2",
|
|
45
|
+
"mock-socket": "9.1.5",
|
|
46
|
+
"mocksse": "1.0.4",
|
|
47
|
+
"nock": "13.2.9",
|
|
48
|
+
"pino-pretty": "9.1.0",
|
|
49
|
+
"prettier": "2.7.1",
|
|
50
|
+
"supertest": "6.3.0",
|
|
51
|
+
"ts-jest": "29.0.3",
|
|
52
|
+
"ts-node": "10.9.1",
|
|
53
|
+
"ts-node-dev": "2.0.0",
|
|
54
|
+
"typedoc": "0.23.15",
|
|
55
|
+
"typescript": "4.8.4"
|
|
45
56
|
},
|
|
46
57
|
"prettier": {
|
|
47
58
|
"semi": false,
|
|
@@ -51,10 +62,6 @@
|
|
|
51
62
|
"trailingComma": "all",
|
|
52
63
|
"arrowParens": "always"
|
|
53
64
|
},
|
|
54
|
-
"optionalDependencies": {
|
|
55
|
-
"bufferutil": "^4.0.6",
|
|
56
|
-
"utf-8-validate": "^5.0.9"
|
|
57
|
-
},
|
|
58
65
|
"ava": {
|
|
59
66
|
"files": [
|
|
60
67
|
"test/**/*.test.ts"
|
|
@@ -65,9 +72,10 @@
|
|
|
65
72
|
"require": [
|
|
66
73
|
"ts-node/register"
|
|
67
74
|
],
|
|
68
|
-
"workerThreads": false
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
75
|
+
"workerThreads": false,
|
|
76
|
+
"environmentVariables": {
|
|
77
|
+
"METRICS_ENABLED": "false"
|
|
78
|
+
},
|
|
79
|
+
"timeout": "20s"
|
|
80
|
+
}
|
|
73
81
|
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { AdapterRateLimitTier, BackgroundExecuteRateLimiter } from '..';
|
|
2
|
+
import { AdapterEndpoint, EndpointGenerics } from '../../adapter';
|
|
3
|
+
export declare const DEFAULT_SHARED_MS_BETWEEN_REQUESTS = 5000;
|
|
4
|
+
export declare class FixedFrequencyRateLimiter implements BackgroundExecuteRateLimiter {
|
|
5
|
+
msBetweenRequestsMap: {
|
|
6
|
+
[endpointName: string]: number;
|
|
7
|
+
};
|
|
8
|
+
initialize<T extends EndpointGenerics>(endpoints: AdapterEndpoint<T>[], limits?: AdapterRateLimitTier): this;
|
|
9
|
+
msUntilNextExecution(endpointName: string): number;
|
|
10
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.FixedFrequencyRateLimiter = exports.DEFAULT_SHARED_MS_BETWEEN_REQUESTS = void 0;
|
|
4
|
+
const __1 = require("..");
|
|
5
|
+
const util_1 = require("../../util");
|
|
6
|
+
const logger = (0, util_1.makeLogger)('FixedFrequencyRateLimiter');
|
|
7
|
+
exports.DEFAULT_SHARED_MS_BETWEEN_REQUESTS = 5000;
|
|
8
|
+
class FixedFrequencyRateLimiter {
|
|
9
|
+
constructor() {
|
|
10
|
+
this.msBetweenRequestsMap = {};
|
|
11
|
+
}
|
|
12
|
+
initialize(endpoints, limits) {
|
|
13
|
+
// Translate the hourly limit into reqs per minute
|
|
14
|
+
let sharedMsBetweenRequests = 1000 / (0, __1.consolidateTierLimits)(limits);
|
|
15
|
+
// If there is no limit set, we use some reasonable number
|
|
16
|
+
if (!limits?.rateLimit1h && !limits?.rateLimit1m && !limits?.rateLimit1s) {
|
|
17
|
+
// 5s period for all seems good
|
|
18
|
+
sharedMsBetweenRequests = exports.DEFAULT_SHARED_MS_BETWEEN_REQUESTS;
|
|
19
|
+
}
|
|
20
|
+
logger.debug('Using fixed frequency batch rate limiting');
|
|
21
|
+
for (const endpoint of endpoints) {
|
|
22
|
+
if (endpoint.rateLimiting?.allocationPercentage == null) {
|
|
23
|
+
throw new Error(`Allocation percentage for endpoint "${endpoint.name}" is null`);
|
|
24
|
+
}
|
|
25
|
+
this.msBetweenRequestsMap[endpoint.name] =
|
|
26
|
+
(sharedMsBetweenRequests / endpoint.rateLimiting?.allocationPercentage) * 100;
|
|
27
|
+
logger.debug(`Endpoint [${endpoint.name}]: ${this.msBetweenRequestsMap[endpoint.name] / 1000}s between requests`);
|
|
28
|
+
}
|
|
29
|
+
return this;
|
|
30
|
+
}
|
|
31
|
+
msUntilNextExecution(endpointName) {
|
|
32
|
+
return this.msBetweenRequestsMap[endpointName];
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
exports.FixedFrequencyRateLimiter = FixedFrequencyRateLimiter;
|
|
36
|
+
//# sourceMappingURL=fixed-frequency.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fixed-frequency.js","sourceRoot":"","sources":["../../../../src/rate-limiting/background/fixed-frequency.ts"],"names":[],"mappings":";;;AAAA,0BAA8F;AAE9F,qCAAuC;AAEvC,MAAM,MAAM,GAAG,IAAA,iBAAU,EAAC,2BAA2B,CAAC,CAAA;AACzC,QAAA,kCAAkC,GAAG,IAAI,CAAA;AAEtD,MAAa,yBAAyB;IAAtC;QACE,yBAAoB,GAEhB,EAAE,CAAA;IAqCR,CAAC;IAnCC,UAAU,CACR,SAA+B,EAC/B,MAA6B;QAE7B,kDAAkD;QAClD,IAAI,uBAAuB,GAAG,IAAI,GAAG,IAAA,yBAAqB,EAAC,MAAM,CAAC,CAAA;QAElE,0DAA0D;QAC1D,IAAI,CAAC,MAAM,EAAE,WAAW,IAAI,CAAC,MAAM,EAAE,WAAW,IAAI,CAAC,MAAM,EAAE,WAAW,EAAE;YACxE,+BAA+B;YAC/B,uBAAuB,GAAG,0CAAkC,CAAA;SAC7D;QAED,MAAM,CAAC,KAAK,CAAC,2CAA2C,CAAC,CAAA;QACzD,KAAK,MAAM,QAAQ,IAAI,SAAS,EAAE;YAChC,IAAI,QAAQ,CAAC,YAAY,EAAE,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