@chainlink/external-adapter-framework 0.3.7 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (210) hide show
  1. package/README.md +102 -0
  2. package/package.json +42 -50
  3. package/adapter/basic.d.ts +0 -86
  4. package/adapter/basic.js +0 -323
  5. package/adapter/basic.js.map +0 -1
  6. package/adapter/endpoint.d.ts +0 -17
  7. package/adapter/endpoint.js +0 -19
  8. package/adapter/endpoint.js.map +0 -1
  9. package/adapter/index.d.ts +0 -4
  10. package/adapter/index.js +0 -21
  11. package/adapter/index.js.map +0 -1
  12. package/adapter/price.d.ts +0 -75
  13. package/adapter/price.js +0 -88
  14. package/adapter/price.js.map +0 -1
  15. package/adapter/types.d.ts +0 -118
  16. package/adapter/types.js +0 -3
  17. package/adapter/types.js.map +0 -1
  18. package/background-executor.d.ts +0 -9
  19. package/background-executor.js +0 -97
  20. package/background-executor.js.map +0 -1
  21. package/cache/factory.d.ts +0 -6
  22. package/cache/factory.js +0 -24
  23. package/cache/factory.js.map +0 -1
  24. package/cache/index.d.ts +0 -87
  25. package/cache/index.js +0 -133
  26. package/cache/index.js.map +0 -1
  27. package/cache/local.d.ts +0 -23
  28. package/cache/local.js +0 -84
  29. package/cache/local.js.map +0 -1
  30. package/cache/metrics.d.ts +0 -27
  31. package/cache/metrics.js +0 -121
  32. package/cache/metrics.js.map +0 -1
  33. package/cache/redis.d.ts +0 -16
  34. package/cache/redis.js +0 -101
  35. package/cache/redis.js.map +0 -1
  36. package/config/index.d.ts +0 -297
  37. package/config/index.js +0 -353
  38. package/config/index.js.map +0 -1
  39. package/config/provider-limits.d.ts +0 -27
  40. package/config/provider-limits.js +0 -75
  41. package/config/provider-limits.js.map +0 -1
  42. package/examples/bank-frick/accounts.d.ts +0 -52
  43. package/examples/bank-frick/accounts.js +0 -190
  44. package/examples/bank-frick/accounts.js.map +0 -1
  45. package/examples/bank-frick/config/index.d.ts +0 -17
  46. package/examples/bank-frick/config/index.js +0 -55
  47. package/examples/bank-frick/config/index.js.map +0 -1
  48. package/examples/bank-frick/index.d.ts +0 -16
  49. package/examples/bank-frick/index.js +0 -16
  50. package/examples/bank-frick/index.js.map +0 -1
  51. package/examples/bank-frick/util.d.ts +0 -4
  52. package/examples/bank-frick/util.js +0 -40
  53. package/examples/bank-frick/util.js.map +0 -1
  54. package/examples/coingecko/src/config/index.d.ts +0 -2
  55. package/examples/coingecko/src/config/index.js +0 -6
  56. package/examples/coingecko/src/config/index.js.map +0 -1
  57. package/examples/coingecko/src/config/overrides.json +0 -10825
  58. package/examples/coingecko/src/crypto-utils.d.ts +0 -48
  59. package/examples/coingecko/src/crypto-utils.js +0 -60
  60. package/examples/coingecko/src/crypto-utils.js.map +0 -1
  61. package/examples/coingecko/src/endpoint/coins.d.ts +0 -9
  62. package/examples/coingecko/src/endpoint/coins.js +0 -37
  63. package/examples/coingecko/src/endpoint/coins.js.map +0 -1
  64. package/examples/coingecko/src/endpoint/crypto-marketcap.d.ts +0 -3
  65. package/examples/coingecko/src/endpoint/crypto-marketcap.js +0 -30
  66. package/examples/coingecko/src/endpoint/crypto-marketcap.js.map +0 -1
  67. package/examples/coingecko/src/endpoint/crypto-volume.d.ts +0 -3
  68. package/examples/coingecko/src/endpoint/crypto-volume.js +0 -30
  69. package/examples/coingecko/src/endpoint/crypto-volume.js.map +0 -1
  70. package/examples/coingecko/src/endpoint/crypto.d.ts +0 -2
  71. package/examples/coingecko/src/endpoint/crypto.js +0 -28
  72. package/examples/coingecko/src/endpoint/crypto.js.map +0 -1
  73. package/examples/coingecko/src/endpoint/dominance.d.ts +0 -3
  74. package/examples/coingecko/src/endpoint/dominance.js +0 -28
  75. package/examples/coingecko/src/endpoint/dominance.js.map +0 -1
  76. package/examples/coingecko/src/endpoint/global-marketcap.d.ts +0 -3
  77. package/examples/coingecko/src/endpoint/global-marketcap.js +0 -28
  78. package/examples/coingecko/src/endpoint/global-marketcap.js.map +0 -1
  79. package/examples/coingecko/src/endpoint/index.d.ts +0 -6
  80. package/examples/coingecko/src/endpoint/index.js +0 -16
  81. package/examples/coingecko/src/endpoint/index.js.map +0 -1
  82. package/examples/coingecko/src/global-utils.d.ts +0 -27
  83. package/examples/coingecko/src/global-utils.js +0 -47
  84. package/examples/coingecko/src/global-utils.js.map +0 -1
  85. package/examples/coingecko/src/index.d.ts +0 -4
  86. package/examples/coingecko/src/index.js +0 -19
  87. package/examples/coingecko/src/index.js.map +0 -1
  88. package/examples/coingecko-old/batch-warming.d.ts +0 -7
  89. package/examples/coingecko-old/batch-warming.js +0 -54
  90. package/examples/coingecko-old/batch-warming.js.map +0 -1
  91. package/examples/coingecko-old/index.d.ts +0 -2
  92. package/examples/coingecko-old/index.js +0 -12
  93. package/examples/coingecko-old/index.js.map +0 -1
  94. package/examples/coingecko-old/rest.d.ts +0 -12
  95. package/examples/coingecko-old/rest.js +0 -55
  96. package/examples/coingecko-old/rest.js.map +0 -1
  97. package/examples/cryptocompare/src/config/index.d.ts +0 -2
  98. package/examples/cryptocompare/src/config/index.js +0 -6
  99. package/examples/cryptocompare/src/config/index.js.map +0 -1
  100. package/examples/cryptocompare/src/endpoints/crypto.d.ts +0 -28
  101. package/examples/cryptocompare/src/endpoints/crypto.js +0 -54
  102. package/examples/cryptocompare/src/endpoints/crypto.js.map +0 -1
  103. package/examples/cryptocompare/src/endpoints/index.d.ts +0 -1
  104. package/examples/cryptocompare/src/endpoints/index.js +0 -6
  105. package/examples/cryptocompare/src/endpoints/index.js.map +0 -1
  106. package/examples/cryptocompare/src/index.d.ts +0 -4
  107. package/examples/cryptocompare/src/index.js +0 -14
  108. package/examples/cryptocompare/src/index.js.map +0 -1
  109. package/examples/genesis/config/index.d.ts +0 -7
  110. package/examples/genesis/config/index.js +0 -11
  111. package/examples/genesis/config/index.js.map +0 -1
  112. package/examples/genesis/index.d.ts +0 -2
  113. package/examples/genesis/index.js +0 -13
  114. package/examples/genesis/index.js.map +0 -1
  115. package/examples/genesis/sseStream.d.ts +0 -16
  116. package/examples/genesis/sseStream.js +0 -149
  117. package/examples/genesis/sseStream.js.map +0 -1
  118. package/examples/ncfx/config/index.d.ts +0 -12
  119. package/examples/ncfx/config/index.js +0 -16
  120. package/examples/ncfx/config/index.js.map +0 -1
  121. package/examples/ncfx/index.d.ts +0 -13
  122. package/examples/ncfx/index.js +0 -12
  123. package/examples/ncfx/index.js.map +0 -1
  124. package/examples/ncfx/websocket.d.ts +0 -47
  125. package/examples/ncfx/websocket.js +0 -74
  126. package/examples/ncfx/websocket.js.map +0 -1
  127. package/index.d.ts +0 -17
  128. package/index.js +0 -174
  129. package/index.js.map +0 -1
  130. package/metrics/constants.d.ts +0 -16
  131. package/metrics/constants.js +0 -26
  132. package/metrics/constants.js.map +0 -1
  133. package/metrics/index.d.ts +0 -19
  134. package/metrics/index.js +0 -139
  135. package/metrics/index.js.map +0 -1
  136. package/metrics/util.d.ts +0 -7
  137. package/metrics/util.js +0 -10
  138. package/metrics/util.js.map +0 -1
  139. package/rate-limiting/background/fixed-frequency.d.ts +0 -11
  140. package/rate-limiting/background/fixed-frequency.js +0 -36
  141. package/rate-limiting/background/fixed-frequency.js.map +0 -1
  142. package/rate-limiting/index.d.ts +0 -56
  143. package/rate-limiting/index.js +0 -86
  144. package/rate-limiting/index.js.map +0 -1
  145. package/rate-limiting/metrics.d.ts +0 -3
  146. package/rate-limiting/metrics.js +0 -45
  147. package/rate-limiting/metrics.js.map +0 -1
  148. package/rate-limiting/request/simple-counting.d.ts +0 -21
  149. package/rate-limiting/request/simple-counting.js +0 -63
  150. package/rate-limiting/request/simple-counting.js.map +0 -1
  151. package/transports/batch-warming.d.ts +0 -35
  152. package/transports/batch-warming.js +0 -138
  153. package/transports/batch-warming.js.map +0 -1
  154. package/transports/index.d.ts +0 -87
  155. package/transports/index.js +0 -56
  156. package/transports/index.js.map +0 -1
  157. package/transports/metrics.d.ts +0 -25
  158. package/transports/metrics.js +0 -122
  159. package/transports/metrics.js.map +0 -1
  160. package/transports/rest.d.ts +0 -51
  161. package/transports/rest.js +0 -133
  162. package/transports/rest.js.map +0 -1
  163. package/transports/routing.d.ts +0 -23
  164. package/transports/routing.js +0 -52
  165. package/transports/routing.js.map +0 -1
  166. package/transports/sse.d.ts +0 -40
  167. package/transports/sse.js +0 -92
  168. package/transports/sse.js.map +0 -1
  169. package/transports/util.d.ts +0 -9
  170. package/transports/util.js +0 -87
  171. package/transports/util.js.map +0 -1
  172. package/transports/websocket.d.ts +0 -81
  173. package/transports/websocket.js +0 -165
  174. package/transports/websocket.js.map +0 -1
  175. package/util/censor/censor-list.d.ts +0 -9
  176. package/util/censor/censor-list.js +0 -13
  177. package/util/censor/censor-list.js.map +0 -1
  178. package/util/index.d.ts +0 -13
  179. package/util/index.js +0 -38
  180. package/util/index.js.map +0 -1
  181. package/util/logger.d.ts +0 -49
  182. package/util/logger.js +0 -123
  183. package/util/logger.js.map +0 -1
  184. package/util/request.d.ts +0 -102
  185. package/util/request.js +0 -3
  186. package/util/request.js.map +0 -1
  187. package/util/subscription-set/expiring-sorted-set.d.ts +0 -21
  188. package/util/subscription-set/expiring-sorted-set.js +0 -36
  189. package/util/subscription-set/expiring-sorted-set.js.map +0 -1
  190. package/util/subscription-set/redis-sorted-set.d.ts +0 -9
  191. package/util/subscription-set/redis-sorted-set.js +0 -29
  192. package/util/subscription-set/redis-sorted-set.js.map +0 -1
  193. package/util/subscription-set/subscription-set.d.ts +0 -19
  194. package/util/subscription-set/subscription-set.js +0 -28
  195. package/util/subscription-set/subscription-set.js.map +0 -1
  196. package/util/test-payload-loader.d.ts +0 -26
  197. package/util/test-payload-loader.js +0 -85
  198. package/util/test-payload-loader.js.map +0 -1
  199. package/validation/error.d.ts +0 -48
  200. package/validation/error.js +0 -78
  201. package/validation/error.js.map +0 -1
  202. package/validation/index.d.ts +0 -5
  203. package/validation/index.js +0 -91
  204. package/validation/index.js.map +0 -1
  205. package/validation/input-params.d.ts +0 -14
  206. package/validation/input-params.js +0 -3
  207. package/validation/input-params.js.map +0 -1
  208. package/validation/input-validator.d.ts +0 -16
  209. package/validation/input-validator.js +0 -123
  210. package/validation/input-validator.js.map +0 -1
package/index.js DELETED
@@ -1,174 +0,0 @@
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 DELETED
@@ -1 +0,0 @@
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;AAE9D,uCAAsE;AACtE,iCAAkF;AAClF,oEAA4D;AAC5D,6CAA2E;AAI3E,MAAM,MAAM,GAAG,IAAA,iBAAU,EAAC,MAAM,CAAC,CAAA;AAEjC,MAAM,OAAO,GAAG,OAAO,CAAC,GAAG,CAAC,qBAAqB,CAAC,CAAA;AAElD;;;;;;;GAOG;AACI,MAAM,MAAM,GAAG,KAAK,EACzB,OAAgB,EAChB,YAA2C,EACL,EAAE;IACxC,IAAI,CAAC,CAAC,OAAO,YAAY,iBAAO,CAAC,EAAE;QACjC,MAAM,IAAI,KAAK,CACb,oFAAoF,CACrF,CAAA;KACF;IAED,kFAAkF;IAClF,MAAM,OAAO,CAAC,UAAU,CAAC,YAAY,CAAC,CAAA;IAEtC,IAAI,GAAG,GAAgC,SAAS,CAAA;IAEh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MAAM;oBACd,GAAG,EAAE,GAAG;oBACR,OAAO,EAAE,OAAO;iBACjB,CAAC,CAAA;gBACF,MAAM,cAAc,GAAG,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAA;gBAChD,qCAAqC;gBACrC,IAAI,cAAc,CAAC,UAAU,GAAG,GAAG,IAAI,cAAc,CAAC,UAAU,GAAG,GAAG,EAAE;oBACtE,MAAM,KAAK,CAAC,2BAA2B,CAAC,CAAA;iBACzC;aACF;YAAC,OAAO,CAAU,EAAE;gBACnB,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAA;aACf;SACF;QACD,IAAI,MAAM,CAAC,MAAM,GAAG,CAAC,EAAE;YACrB,OAAO,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,CAAA;SACpC;QACD,OAAO,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;IACnC,CAAC,CAAC,CAAA;AACJ,CAAC"}
@@ -1,16 +0,0 @@
1
- export declare enum HttpRequestType {
2
- CACHE_HIT = "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[];
@@ -1,26 +0,0 @@
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
@@ -1 +0,0 @@
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"}
@@ -1,19 +0,0 @@
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 DELETED
@@ -1,139 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
- var __importDefault = (this && this.__importDefault) || function (mod) {
26
- return (mod && mod.__esModule) ? mod : { "default": mod };
27
- };
28
- Object.defineProperty(exports, "__esModule", { value: true });
29
- exports.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
@@ -1 +0,0 @@
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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BAAqB;IAC3B,IAAI,EAAE,sFAAsF;IAC5F,UAAU,EAAE;QACV,QAAQ;QACR,aAAa;QACb,OAAO;QACP,MAAM;QACN,kBAAkB;QAClB,SAAS;QACT,sBAAsB;KACd;CACX,CAAC,CAAA;AAEW,QAAA,0BAA0B,GAAG,IAAI,MAAM,CAAC,SAAS,CAAC;IAC7D,IAAI,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"}
package/metrics/util.d.ts DELETED
@@ -1,7 +0,0 @@
1
- import { AdapterMetricsMeta, AdapterRequestData } from '../util';
2
- import { AdapterEndpoint } from '../adapter';
3
- import { AdapterConfig, SettingsMap } from '../config';
4
- export declare const getMetricsMeta: <Params, Result, CustomSettings extends SettingsMap>({ adapterEndpoint, adapterConfig, }: {
5
- adapterEndpoint: AdapterEndpoint<Params, Result, CustomSettings>;
6
- adapterConfig: AdapterConfig<CustomSettings>;
7
- }, data: AdapterRequestData) => AdapterMetricsMeta;
package/metrics/util.js DELETED
@@ -1,10 +0,0 @@
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
@@ -1 +0,0 @@
1
- {"version":3,"file":"util.js","sourceRoot":"","sources":["../../../src/metrics/util.ts"],"names":[],"mappings":";;;AAEA,oCAA0C;AAGnC,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"}
@@ -1,11 +0,0 @@
1
- import { AdapterRateLimitTier, BackgroundExecuteRateLimiter } from '..';
2
- import { AdapterEndpoint } from '../../adapter';
3
- import { SettingsMap } from '../../config';
4
- export declare const DEFAULT_SHARED_MS_BETWEEN_REQUESTS = 5000;
5
- export declare class FixedFrequencyRateLimiter implements BackgroundExecuteRateLimiter {
6
- msBetweenRequestsMap: {
7
- [endpointName: string]: number;
8
- };
9
- initialize<Params, Result, CustomSettings extends SettingsMap>(endpoints: AdapterEndpoint<Params, Result, CustomSettings>[], limits?: AdapterRateLimitTier): this;
10
- msUntilNextExecution(endpointName: string): number;
11
- }
@@ -1,36 +0,0 @@
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
@@ -1 +0,0 @@
1
- {"version":3,"file":"fixed-frequency.js","sourceRoot":"","sources":["../../../../src/rate-limiting/background/fixed-frequency.ts"],"names":[],"mappings":";;;AAAA,0BAA8F;AAG9F,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,SAA4D,EAC5D,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,oBAAoB,IAAI,IAAI,EAAE;gBACvD,MAAM,IAAI,KAAK,CAAC,uCAAuC,QAAQ,CAAC,IAAI,WAAW,CAAC,CAAA;aACjF;YAED,IAAI,CAAC,oBAAoB,CAAC,QAAQ,CAAC,IAAI,CAAC;gBACtC,CAAC,uBAAuB,GAAG,QAAQ,CAAC,YAAY,EAAE,oBAAoB,CAAC,GAAG,GAAG,CAAA;YAE/E,MAAM,CAAC,KAAK,CACV,aAAa,QAAQ,CAAC,IAAI,MACxB,IAAI,CAAC,oBAAoB,CAAC,QAAQ,CAAC,IAAI,CAAC,GAAG,IAC7C,oBAAoB,CACrB,CAAA;SACF;QAED,OAAO,IAAI,CAAA;IACb,CAAC;IAED,oBAAoB,CAAC,YAAoB;QACvC,OAAO,IAAI,CAAC,oBAAoB,CAAC,YAAY,CAAC,CAAA;IAChD,CAAC;CACF;AAxCD,8DAwCC"}
@@ -1,56 +0,0 @@
1
- import { AdapterEndpoint } from '../adapter';
2
- import { AdapterConfig, SettingsMap } from '../config';
3
- export * from './request/simple-counting';
4
- export * from './background/fixed-frequency';
5
- export interface AdapterRateLimitTier {
6
- rateLimit1s?: number;
7
- rateLimit1m?: number;
8
- rateLimit1h?: number;
9
- note?: string;
10
- }
11
- /**
12
- * Common interface for all RateLimiter classes to implement
13
- */
14
- export interface RateLimiter {
15
- /**
16
- * Method to ensure all RateLimiters can be initialized in the same manner.
17
- *
18
- * @param limits - settings for how much throughput to allow for the Adapter
19
- * @param endpoints - list of adapter endpoints
20
- */
21
- initialize<Params, Result, CustomSettings extends SettingsMap>(endpoints: AdapterEndpoint<Params, Result, CustomSettings>[], limits: AdapterRateLimitTier): this;
22
- }
23
- /**
24
- * RequestRateLimiters perform checks agains imminent outbound requests for any transport.
25
- */
26
- export interface RequestRateLimiter extends RateLimiter {
27
- /**
28
- * This method will check using whatever strategy is implemented to determine if
29
- * this request can be processed. If so, it returns true; if not, returns false.
30
- */
31
- isUnderLimits(): boolean;
32
- }
33
- /**
34
- * BackgroundExecuteFrequencyRateLimiters will implement custom logic to calculate
35
- * the period of time to wait between background executions for a transport.
36
- */
37
- export interface BackgroundExecuteRateLimiter extends RateLimiter {
38
- msUntilNextExecution(endpointName: string): number;
39
- }
40
- /**
41
- * This method will convert all possible settings for a rate limit tier and
42
- * convert them all to requests per second, returning the lowest one
43
- *
44
- * @param limits - the rate limit tier set for the adapter
45
- * @returns the most restrictive of the set options, in requests per second
46
- */
47
- export declare const consolidateTierLimits: (limits?: AdapterRateLimitTier) => number;
48
- /**
49
- * Validates rate limiting tiers specified for the adapter, and returns the one to use.
50
- *
51
- * @param config - the adapter config containing the env vars
52
- * @param tiers - the adapter config listing the different available API tiers
53
- * @returns the specified API tier, or a default one if none are specified
54
- */
55
- export declare const getRateLimitingTier: (config: AdapterConfig, tiers?: Record<string, AdapterRateLimitTier>) => AdapterRateLimitTier | undefined;
56
- export declare const buildRateLimitTiersFromConfig: (config: AdapterConfig) => AdapterRateLimitTier | undefined;
@@ -1,86 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.buildRateLimitTiersFromConfig = exports.getRateLimitingTier = exports.consolidateTierLimits = void 0;
18
- __exportStar(require("./request/simple-counting"), exports);
19
- __exportStar(require("./background/fixed-frequency"), exports);
20
- /**
21
- * This method will convert all possible settings for a rate limit tier and
22
- * convert them all to requests per second, returning the lowest one
23
- *
24
- * @param limits - the rate limit tier set for the adapter
25
- * @returns the most restrictive of the set options, in requests per second
26
- */
27
- const consolidateTierLimits = (limits) => {
28
- const perHourLimit = (limits?.rateLimit1h || Infinity) / (60 * 60);
29
- const perMinuteLimit = (limits?.rateLimit1m || Infinity) / 60;
30
- const perSecondLimit = limits?.rateLimit1s || Infinity;
31
- return Math.min(perHourLimit, perMinuteLimit, perSecondLimit);
32
- };
33
- exports.consolidateTierLimits = consolidateTierLimits;
34
- /**
35
- * Validates rate limiting tiers specified for the adapter, and returns the one to use.
36
- *
37
- * @param config - the adapter config containing the env vars
38
- * @param tiers - the adapter config listing the different available API tiers
39
- * @returns the specified API tier, or a default one if none are specified
40
- */
41
- const getRateLimitingTier = (config, tiers) => {
42
- if (config.RATE_LIMIT_CAPACITY ||
43
- config.RATE_LIMIT_CAPACITY_MINUTE ||
44
- config.RATE_LIMIT_CAPACITY_SECOND) {
45
- return (0, exports.buildRateLimitTiersFromConfig)(config);
46
- }
47
- if (!tiers) {
48
- return;
49
- }
50
- // Check that if the tiers object is defined, it has values
51
- if (Object.values(tiers).length === 0) {
52
- throw new Error(`The tiers object is defined, but has no entries`);
53
- }
54
- // Check that the tier set in the AdapterConfig is a valid one
55
- const selectedTier = config.RATE_LIMIT_API_TIER;
56
- if (selectedTier && !tiers[selectedTier]) {
57
- const validTiersString = Object.keys(tiers)
58
- .map((t) => `"${t}"`)
59
- .join(', ');
60
- throw new Error(`The selected rate limit tier "${selectedTier}" is not valid (can be one of ${validTiersString})`);
61
- }
62
- if (!selectedTier) {
63
- // Sort the tiers by most to least restrictive
64
- const sortedTiers = Object.values(tiers).sort((t1, t2) => (0, exports.consolidateTierLimits)(t1) - (0, exports.consolidateTierLimits)(t2));
65
- return sortedTiers[0];
66
- }
67
- return tiers[selectedTier];
68
- };
69
- exports.getRateLimitingTier = getRateLimitingTier;
70
- // Creates adapter rate limit tier using the configs specified in env vars
71
- const buildRateLimitTiersFromConfig = (config) => {
72
- const rateLimit1s = config.RATE_LIMIT_CAPACITY_SECOND;
73
- let rateLimit1m;
74
- if (config.RATE_LIMIT_CAPACITY_MINUTE) {
75
- rateLimit1m = config.RATE_LIMIT_CAPACITY_MINUTE;
76
- }
77
- else if (config.RATE_LIMIT_CAPACITY) {
78
- rateLimit1m = config.RATE_LIMIT_CAPACITY;
79
- }
80
- return {
81
- rateLimit1s,
82
- rateLimit1m,
83
- };
84
- };
85
- exports.buildRateLimitTiersFromConfig = buildRateLimitTiersFromConfig;
86
- //# sourceMappingURL=index.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/rate-limiting/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAGA,4DAAyC;AACzC,+DAA4C;AA4C5C;;;;;;GAMG;AACI,MAAM,qBAAqB,GAAG,CAAC,MAA6B,EAAE,EAAE;IACrE,MAAM,YAAY,GAAG,CAAC,MAAM,EAAE,WAAW,IAAI,QAAQ,CAAC,GAAG,CAAC,EAAE,GAAG,EAAE,CAAC,CAAA;IAClE,MAAM,cAAc,GAAG,CAAC,MAAM,EAAE,WAAW,IAAI,QAAQ,CAAC,GAAG,EAAE,CAAA;IAC7D,MAAM,cAAc,GAAG,MAAM,EAAE,WAAW,IAAI,QAAQ,CAAA;IACtD,OAAO,IAAI,CAAC,GAAG,CAAC,YAAY,EAAE,cAAc,EAAE,cAAc,CAAC,CAAA;AAC/D,CAAC,CAAA;AALY,QAAA,qBAAqB,yBAKjC;AAED;;;;;;GAMG;AACI,MAAM,mBAAmB,GAAG,CACjC,MAAqB,EACrB,KAA4C,EACV,EAAE;IACpC,IACE,MAAM,CAAC,mBAAmB;QAC1B,MAAM,CAAC,0BAA0B;QACjC,MAAM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G,MAAM,CAAC,0BAA0B,CAAA;KAChD;SAAM,IAAI,MAAM,CAAC,mBAAmB,EAAE;QACrC,WAAW,GAAG,MAAM,CAAC,mBAAmB,CAAA;KACzC;IACD,OAAO;QACL,WAAW;QACX,WAAW;KACZ,CAAA;AACH,CAAC,CAAA;AAdY,QAAA,6BAA6B,iCAczC"}
@@ -1,3 +0,0 @@
1
- import * as client from 'prom-client';
2
- export declare const retrieveCost: <ProviderResponseBody>(data: ProviderResponseBody) => number;
3
- export declare const rateLimitCreditsSpentTotal: client.Counter<"feed_id" | "participant_id">;