@chainlink/external-adapter-framework 0.0.6 → 0.0.7

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 (161) hide show
  1. package/dist/adapter.js +60 -0
  2. package/dist/background-executor.js +45 -0
  3. package/dist/cache/factory.js +57 -0
  4. package/dist/cache/index.js +163 -0
  5. package/dist/cache/local.js +83 -0
  6. package/dist/cache/metrics.js +114 -0
  7. package/dist/cache/redis.js +100 -0
  8. package/dist/config/index.js +364 -0
  9. package/dist/config/provider-limits.js +75 -0
  10. package/dist/examples/coingecko/batch-warming.js +52 -0
  11. package/dist/examples/coingecko/index.js +10 -0
  12. package/dist/examples/coingecko/rest.js +50 -0
  13. package/dist/examples/ncfx/config/index.js +15 -0
  14. package/dist/examples/ncfx/index.js +10 -0
  15. package/dist/examples/ncfx/websocket.js +72 -0
  16. package/dist/index.js +89 -0
  17. package/dist/metrics/constants.js +25 -0
  18. package/dist/metrics/index.js +76 -0
  19. package/dist/metrics/util.js +9 -0
  20. package/dist/rate-limiting/factory.js +33 -0
  21. package/dist/rate-limiting/index.js +36 -0
  22. package/dist/rate-limiting/metrics.js +32 -0
  23. package/dist/rate-limiting/nop-limiter.js +15 -0
  24. package/dist/rate-limiting/simple-counting.js +61 -0
  25. package/dist/src/adapter.js +112 -0
  26. package/dist/src/background-executor.js +45 -0
  27. package/dist/src/cache/factory.js +57 -0
  28. package/dist/src/cache/index.js +165 -0
  29. package/dist/src/cache/local.js +83 -0
  30. package/dist/src/cache/metrics.js +114 -0
  31. package/dist/src/cache/redis.js +100 -0
  32. package/dist/src/config/index.js +366 -0
  33. package/dist/src/config/provider-limits.js +75 -0
  34. package/dist/src/examples/bank-frick/accounts.js +191 -0
  35. package/dist/src/examples/bank-frick/config/index.js +45 -0
  36. package/dist/src/examples/bank-frick/index.js +14 -0
  37. package/dist/src/examples/bank-frick/util.js +39 -0
  38. package/dist/src/examples/coingecko/batch-warming.js +52 -0
  39. package/dist/src/examples/coingecko/index.js +10 -0
  40. package/dist/src/examples/coingecko/rest.js +50 -0
  41. package/dist/src/examples/ncfx/config/index.js +15 -0
  42. package/dist/src/examples/ncfx/index.js +10 -0
  43. package/dist/src/examples/ncfx/websocket.js +72 -0
  44. package/dist/src/index.js +89 -0
  45. package/dist/src/metrics/constants.js +25 -0
  46. package/dist/src/metrics/index.js +76 -0
  47. package/dist/src/metrics/util.js +9 -0
  48. package/dist/src/rate-limiting/background/fixed-frequency.js +37 -0
  49. package/dist/src/rate-limiting/index.js +63 -0
  50. package/dist/src/rate-limiting/metrics.js +32 -0
  51. package/dist/src/rate-limiting/request/simple-counting.js +62 -0
  52. package/dist/src/test.js +6 -0
  53. package/dist/src/transports/batch-warming.js +55 -0
  54. package/dist/src/transports/index.js +85 -0
  55. package/dist/src/transports/metrics.js +119 -0
  56. package/dist/src/transports/rest.js +93 -0
  57. package/dist/src/transports/util.js +85 -0
  58. package/dist/src/transports/websocket.js +175 -0
  59. package/dist/src/util/expiring-sorted-set.js +47 -0
  60. package/dist/src/util/index.js +35 -0
  61. package/dist/src/util/logger.js +62 -0
  62. package/dist/src/util/request.js +2 -0
  63. package/dist/src/validation/error.js +41 -0
  64. package/dist/src/validation/index.js +84 -0
  65. package/dist/src/validation/input-params.js +30 -0
  66. package/dist/src/validation/override-functions.js +40 -0
  67. package/dist/src/validation/preset-tokens.json +23 -0
  68. package/dist/src/validation/validator.js +303 -0
  69. package/dist/test.js +6 -0
  70. package/dist/transports/batch-warming.js +57 -0
  71. package/dist/transports/index.js +76 -0
  72. package/dist/transports/metrics.js +133 -0
  73. package/dist/transports/rest.js +91 -0
  74. package/dist/transports/util.js +85 -0
  75. package/dist/transports/websocket.js +171 -0
  76. package/dist/util/expiring-sorted-set.js +47 -0
  77. package/dist/util/index.js +35 -0
  78. package/dist/util/logger.js +62 -0
  79. package/dist/util/request.js +2 -0
  80. package/dist/validation/error.js +41 -0
  81. package/dist/validation/index.js +82 -0
  82. package/dist/validation/input-params.js +30 -0
  83. package/dist/validation/overrideFunctions.js +42 -0
  84. package/dist/validation/presetTokens.json +23 -0
  85. package/dist/validation/validator.js +303 -0
  86. package/package.json +6 -2
  87. package/.c8rc.json +0 -3
  88. package/.eslintignore +0 -9
  89. package/.eslintrc.js +0 -96
  90. package/.github/README.MD +0 -17
  91. package/.github/actions/setup/action.yaml +0 -13
  92. package/.github/workflows/main.yaml +0 -39
  93. package/.github/workflows/publish.yaml +0 -17
  94. package/.prettierignore +0 -13
  95. package/.yarnrc +0 -0
  96. package/docker-compose.yaml +0 -35
  97. package/src/adapter.ts +0 -236
  98. package/src/background-executor.ts +0 -53
  99. package/src/cache/factory.ts +0 -28
  100. package/src/cache/index.ts +0 -236
  101. package/src/cache/local.ts +0 -73
  102. package/src/cache/metrics.ts +0 -112
  103. package/src/cache/redis.ts +0 -93
  104. package/src/config/index.ts +0 -501
  105. package/src/config/provider-limits.ts +0 -130
  106. package/src/examples/coingecko/batch-warming.ts +0 -79
  107. package/src/examples/coingecko/index.ts +0 -9
  108. package/src/examples/coingecko/rest.ts +0 -77
  109. package/src/examples/ncfx/config/index.ts +0 -12
  110. package/src/examples/ncfx/index.ts +0 -9
  111. package/src/examples/ncfx/websocket.ts +0 -100
  112. package/src/index.ts +0 -106
  113. package/src/metrics/constants.ts +0 -23
  114. package/src/metrics/index.ts +0 -116
  115. package/src/metrics/util.ts +0 -11
  116. package/src/rate-limiting/background/fixed-frequency.ts +0 -47
  117. package/src/rate-limiting/index.ts +0 -100
  118. package/src/rate-limiting/metrics.ts +0 -18
  119. package/src/rate-limiting/request/simple-counting.ts +0 -76
  120. package/src/test.ts +0 -5
  121. package/src/transports/batch-warming.ts +0 -121
  122. package/src/transports/index.ts +0 -173
  123. package/src/transports/metrics.ts +0 -95
  124. package/src/transports/rest.ts +0 -161
  125. package/src/transports/util.ts +0 -63
  126. package/src/transports/websocket.ts +0 -238
  127. package/src/util/expiring-sorted-set.ts +0 -52
  128. package/src/util/index.ts +0 -20
  129. package/src/util/logger.ts +0 -69
  130. package/src/util/request.ts +0 -115
  131. package/src/validation/error.ts +0 -116
  132. package/src/validation/index.ts +0 -101
  133. package/src/validation/input-params.ts +0 -45
  134. package/src/validation/override-functions.ts +0 -44
  135. package/src/validation/preset-tokens.json +0 -23
  136. package/src/validation/validator.ts +0 -384
  137. package/test/adapter.test.ts +0 -27
  138. package/test/background-executor.test.ts +0 -109
  139. package/test/cache/cache-key.test.ts +0 -96
  140. package/test/cache/helper.ts +0 -101
  141. package/test/cache/local.test.ts +0 -54
  142. package/test/cache/redis.test.ts +0 -89
  143. package/test/correlation.test.ts +0 -114
  144. package/test/index.test.ts +0 -37
  145. package/test/metrics/feed-id.test.ts +0 -33
  146. package/test/metrics/helper.ts +0 -14
  147. package/test/metrics/labels.test.ts +0 -36
  148. package/test/metrics/metrics.test.ts +0 -267
  149. package/test/metrics/redis-metrics.test.ts +0 -113
  150. package/test/metrics/warmer-metrics.test.ts +0 -192
  151. package/test/metrics/ws-metrics.test.ts +0 -225
  152. package/test/rate-limit-config.test.ts +0 -243
  153. package/test/transports/batch.test.ts +0 -465
  154. package/test/transports/rest.test.ts +0 -242
  155. package/test/transports/websocket.test.ts +0 -183
  156. package/test/tsconfig.json +0 -5
  157. package/test/util.ts +0 -76
  158. package/test/validation.test.ts +0 -169
  159. package/test.sh +0 -20
  160. package/tsconfig.json +0 -24
  161. package/typedoc.json +0 -6
@@ -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
+ // TODO: Remove non used in v3 ones
8
+ API_ENDPOINT: {
9
+ description: 'The URL that the certain transports use to retrieve data',
10
+ type: 'string',
11
+ },
12
+ API_KEY: {
13
+ description: 'Default setting for an EA key',
14
+ type: 'string',
15
+ },
16
+ // API_TIMEOUT: {
17
+ // type: 'number',
18
+ // default: 30000,
19
+ // },
20
+ // API_VERBOSE: {
21
+ // type: 'boolean',
22
+ // },
23
+ BASE_URL: {
24
+ description: 'Starting path for the EA handler endpoint',
25
+ type: 'string',
26
+ default: '/',
27
+ },
28
+ // CACHE_ENABLED: {
29
+ // type: 'boolean',
30
+ // default: true,
31
+ // },
32
+ // CACHE_KEY_GROUP: {
33
+ // type: 'string',
34
+ // },
35
+ CACHE_MAX_AGE: {
36
+ description: 'Maximum amount of time (in ms) that a response will stay cached',
37
+ type: 'number',
38
+ default: 90000,
39
+ },
40
+ // CACHE_MAX_ITEMS: {
41
+ // type: 'number',
42
+ // default: 1000,
43
+ // },
44
+ // CACHE_MIN_AGE: {
45
+ // type: 'number',
46
+ // default: 30000,
47
+ // },
48
+ // CACHE_REDIS_CONNECTION_TIMEOUT: {
49
+ // type: 'number',
50
+ // default: 15000,
51
+ // },
52
+ CACHE_REDIS_HOST: {
53
+ description: 'Hostname for the Redis instance to be used',
54
+ type: 'string',
55
+ default: '127.0.0.1',
56
+ },
57
+ // CACHE_REDIS_MAX_QUEUED_ITEMS: {
58
+ // type: 'number',
59
+ // default: 100,
60
+ // },
61
+ // CACHE_REDIS_MAX_RECONNECT_COOLDOWN: {
62
+ // type: 'number',
63
+ // default: 3000,
64
+ // },
65
+ // CACHE_REDIS_PASSWORD: {
66
+ // type: 'string',
67
+ // },
68
+ // CACHE_REDIS_PATH: {
69
+ // type: 'string',
70
+ // },
71
+ CACHE_REDIS_PORT: {
72
+ description: 'Port for the Redis instance to be used',
73
+ type: 'number',
74
+ default: 6379,
75
+ },
76
+ // CACHE_REDIS_TIMEOUT: {
77
+ // type: 'number',
78
+ // default: 500,
79
+ // },
80
+ // CACHE_REDIS_URL: {
81
+ // type: 'string',
82
+ // },
83
+ CACHE_TYPE: {
84
+ description: 'The type of cache to use throughout the EA',
85
+ type: 'enum',
86
+ default: 'local',
87
+ options: ['local', 'redis'],
88
+ },
89
+ // CACHE_UPDATE_AGE_ON_GET: {
90
+ // type: 'boolean',
91
+ // default: false,
92
+ // },
93
+ CORRELATION_ID_ENABLED: {
94
+ description: 'Flag to enable correlation IDs for sent requests in logging',
95
+ type: 'boolean',
96
+ default: true,
97
+ },
98
+ // DEBUG: {
99
+ // type: 'boolean',
100
+ // },
101
+ // DEFAULT_WS_HEARTBEAT_INTERVAL: {
102
+ // type: 'number',
103
+ // default: 30000,
104
+ // },
105
+ EA_PORT: {
106
+ description: 'Port through which the EA will listen for REST requests (if mode is set to "reader" or "reader-writer")',
107
+ type: 'number',
108
+ default: 8080,
109
+ },
110
+ // ENV_ADAPTER_URL: {
111
+ // type: 'string',
112
+ // },
113
+ // ERROR_CAPACITY: {
114
+ // type: 'number',
115
+ // },
116
+ EXPERIMENTAL_METRICS_ENABLED: {
117
+ description: 'Flag to specify whether or not to collect metrics. Used as fallback for METRICS_ENABLED',
118
+ type: 'boolean',
119
+ default: true,
120
+ },
121
+ // LEGACY_ENV_ADAPTER_URL: {
122
+ // type: 'string',
123
+ // },
124
+ LOG_LEVEL: {
125
+ description: 'Minimum level required for logs to be output',
126
+ type: 'string',
127
+ default: 'info',
128
+ },
129
+ METRICS_ENABLED: {
130
+ description: 'Flag to specify whether or not to startup the metrics server',
131
+ type: 'boolean',
132
+ default: true,
133
+ },
134
+ METRICS_NAME: {
135
+ description: 'Metrics name',
136
+ type: 'string',
137
+ },
138
+ METRICS_PORT: {
139
+ description: 'Port metrics will be exposed to',
140
+ type: 'number',
141
+ default: 9080,
142
+ },
143
+ METRICS_USE_BASE_URL: {
144
+ description: 'Flag to specify whether or not to prepend the BASE_URL to the metrics endpoint',
145
+ type: 'boolean',
146
+ },
147
+ RATE_LIMIT_API_TIER: {
148
+ 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.',
149
+ type: 'string',
150
+ },
151
+ // RATE_LIMIT_CAPACITY_MINUTE: {
152
+ // type: 'number',
153
+ // },
154
+ // RATE_LIMIT_CAPACITY_SECOND: {
155
+ // type: 'number',
156
+ // },
157
+ // RATE_LIMIT_CAPACITY: {
158
+ // type: 'number',
159
+ // },
160
+ // RATE_LIMIT_ENABLED: {
161
+ // type: 'boolean',
162
+ // default: true,
163
+ // },
164
+ // RECORD: {
165
+ // type: 'boolean',
166
+ // },
167
+ // REQUEST_COALESCING_ENABLED: {
168
+ // type: 'boolean',
169
+ // },
170
+ // REQUEST_COALESCING_ENTROPY_MAX: {
171
+ // type: 'number',
172
+ // default: 0,
173
+ // },
174
+ // REQUEST_COALESCING_INTERVAL_COEFFICIENT: {
175
+ // type: 'number',
176
+ // default: 2,
177
+ // },
178
+ // REQUEST_COALESCING_INTERVAL_MAX: {
179
+ // type: 'number',
180
+ // default: 1000,
181
+ // },
182
+ // REQUEST_COALESCING_INTERVAL: {
183
+ // type: 'number',
184
+ // default: 100,
185
+ // },
186
+ // REQUEST_COALESCING_MAX_RETRIES: {
187
+ // type: 'number',
188
+ // default: 5,
189
+ // },
190
+ // RETRY: {
191
+ // type: 'boolean',
192
+ // default: 1,
193
+ // },
194
+ // SERVER_RATE_LIMIT_MAX: {
195
+ // type: 'number',
196
+ // default: 250,
197
+ // },
198
+ // SERVER_SLOW_DOWN_AFTER_FACTOR: {
199
+ // type: 'number',
200
+ // default: 0.8,
201
+ // },
202
+ // SERVER_SLOW_DOWN_DELAY_MS: {
203
+ // type: 'number',
204
+ // default: 500,
205
+ // },
206
+ // TIMEOUT: {
207
+ // type: 'number',
208
+ // },
209
+ // WARMUP_ENABLED: {
210
+ // type: 'boolean',
211
+ // default: true,
212
+ // },
213
+ // WARMUP_INTERVAL: {
214
+ // type: 'number',
215
+ // },
216
+ WARMUP_SUBSCRIPTION_TTL: {
217
+ type: 'number',
218
+ description: 'TTL for batch warmer subscriptions',
219
+ default: 60000,
220
+ },
221
+ // WARMUP_UNHEALTHY_THRESHOLD: {
222
+ // type: 'number',
223
+ // default: 3,
224
+ // },
225
+ // WS_API_ENDPOINT: {
226
+ // type: 'string',
227
+ // },
228
+ // WS_API_KEY: {
229
+ // type: 'string',
230
+ // },
231
+ // WS_CONNECTION_KEY: {
232
+ // type: 'string',
233
+ // default: 1,
234
+ // },
235
+ // WS_CONNECTION_LIMIT: {
236
+ // type: 'number',
237
+ // default: 1,
238
+ // },
239
+ // WS_CONNECTION_RETRY_DELAY: {
240
+ // type: 'number',
241
+ // default: 1000,
242
+ // },
243
+ // WS_CONNECTION_RETRY_LIMIT: {
244
+ // type: 'number',
245
+ // default: 3,
246
+ // },
247
+ // WS_CONNECTION_TTL: {
248
+ // type: 'number',
249
+ // default: 70000,
250
+ // },
251
+ // WS_ENABLED: {
252
+ // type: 'boolean',
253
+ // default: false,
254
+ // },
255
+ // WS_SUBSCRIPTION_LIMIT: {
256
+ // type: 'number',
257
+ // default: 10,
258
+ // },
259
+ // WS_SUBSCRIPTION_PRIORITY_LIST: {
260
+ // type: 'string',
261
+ // },
262
+ // WS_SUBSCRIPTION_TTL: {
263
+ // type: 'number',
264
+ // default: 120000,
265
+ // },
266
+ // WS_SUBSCRIPTION_UNRESPONSIVE_TTL: {
267
+ // type: 'number',
268
+ // default: false,
269
+ // },
270
+ // WS_TIME_UNTIL_HANDLE_NEXT_MESSAGE_OVERRIDE: {
271
+ // type: 'number',
272
+ // },
273
+ // V3
274
+ CACHE_POLLING_MAX_RETRIES: {
275
+ description: 'Max amount of times to attempt to find EA response in the cache after the Transport has been set up',
276
+ type: 'number',
277
+ default: 10,
278
+ },
279
+ CACHE_POLLING_SLEEP_MS: {
280
+ description: 'The number of ms to sleep between each retry to fetch the EA response in the cache',
281
+ type: 'number',
282
+ default: 200,
283
+ },
284
+ DEFAULT_CACHE_KEY: {
285
+ description: 'Default key to be used when one cannot be determined from request parameters',
286
+ type: 'string',
287
+ default: 'DEFAULT_CACHE_KEY',
288
+ },
289
+ EA_HOST: {
290
+ description: 'Host this EA will listen for REST requests on (if mode is set to "reader" or "reader-writer")',
291
+ type: 'string',
292
+ default: '::',
293
+ },
294
+ EA_MODE: {
295
+ description: 'Port this EA will listen for REST requests on (if mode is set to "reader" or "reader-writer")',
296
+ type: 'enum',
297
+ default: 'reader-writer',
298
+ options: ['reader', 'writer', 'reader-writer'],
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
+ // CACHE_KEY_IGNORED_PROPS : {
311
+ // description: 'Properties to ignore when generating a feed ID for requests',
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,75 @@
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 parseLimits = (limits) => {
45
+ const _mapObject = (fn) => (o) => Object.fromEntries(Object.entries(o).map(fn));
46
+ const _formatProtocol = _mapObject((entry) => {
47
+ const [tierName, rest] = entry;
48
+ return [tierName.toLowerCase(), { ...rest }];
49
+ });
50
+ const _formatProvider = (ls) => {
51
+ const http = _formatProtocol(ls.http);
52
+ const ws = _formatProtocol(ls?.ws);
53
+ return { http, ws };
54
+ };
55
+ return _formatProvider(limits);
56
+ };
57
+ const calculateWSLimits = (providerLimit) => {
58
+ return {
59
+ connections: providerLimit.connections,
60
+ subscriptions: providerLimit.subscriptions,
61
+ };
62
+ };
63
+ const calculateRateLimit = (providerLimit) => {
64
+ let quota = providerLimit.rateLimit1m;
65
+ if (!quota && providerLimit?.rateLimit1h) {
66
+ quota = providerLimit?.rateLimit1h / 60;
67
+ }
68
+ else if (!quota && providerLimit?.rateLimit1s) {
69
+ quota = providerLimit?.rateLimit1s * 60;
70
+ }
71
+ return {
72
+ second: providerLimit?.rateLimit1s || (quota / 60) * exports.BURST_UNDEFINED_QUOTA_MULTIPLE,
73
+ minute: quota,
74
+ };
75
+ };
@@ -0,0 +1,191 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.accountsRestEndpoint = exports.BankFrickAccountsTransport = void 0;
7
+ const axios_1 = __importDefault(require("axios"));
8
+ const util_1 = require("./util");
9
+ const util_2 = require("../../util");
10
+ const error_1 = require("../../validation/error");
11
+ const logger = (0, util_2.makeLogger)('BankFrickTransport');
12
+ // Note: this is a shallow pattern that only checks for a country code since IBANs in the sandbox are invalid
13
+ const ibanPattern = /^[A-Z]{2}[A-Z\d]{14,30}$/;
14
+ const inputParameters = {
15
+ ibanIDs: {
16
+ description: 'The list of account ids included in the sum of balances',
17
+ required: true,
18
+ type: 'array',
19
+ },
20
+ signingAlgorithm: {
21
+ description: 'What signing algorithm is used to sign and verify authorization data, one of rsa-sha256, rsa-sha384, or rsa-sha512',
22
+ required: false,
23
+ type: 'string',
24
+ default: 'rsa-sha512',
25
+ options: ['rsa-sha256', 'rsa-sha384', 'rsa-sha512'],
26
+ },
27
+ };
28
+ // See here for all expected error returned by the API: https://developers.bankfrick.li/docs#errors
29
+ const AuthErrors = {
30
+ 401: 'No JWT token provided or token is invalid',
31
+ 403: 'API key is invalid or any other condition is hindering the login', // Unclear if this is fatal or not
32
+ };
33
+ const FatalErrors = {
34
+ 400: "Invalid parameters passed to Bank Frick's API",
35
+ 423: "Authorization is valid, but the user's account is locked",
36
+ };
37
+ /**
38
+ * RestTransport implementation for Bank Frick, which has unusually complex requirements for an EA
39
+ * The RestTransport is generally built to make a single request and return a single response.
40
+ * This transport instead is used to fetch and process pages of data, and also requires a JWT to run
41
+ *
42
+ * This transport does all the heavy lifting in setup(), which is where the paging happens, and it
43
+ * also has complex retry logic that will attempt to refresh the JWT when certain HTTP errors occur
44
+ */
45
+ class BankFrickAccountsTransport {
46
+ async initialize(dependencies) {
47
+ this.cache = dependencies.cache;
48
+ }
49
+ async hasBeenSetUp() {
50
+ return false; // Return false since we aren't coalescing requests
51
+ }
52
+ /**
53
+ * Creates an AxiosRequestConfig object for fetching a page of accounts from the Bank Frick API
54
+ */
55
+ prepareRequest(firstPosition, inputParams, config) {
56
+ const { API_ENDPOINT, BASE_URL, PAGE_SIZE } = config;
57
+ return {
58
+ baseURL: API_ENDPOINT + BASE_URL,
59
+ url: 'accounts',
60
+ method: 'GET',
61
+ params: {
62
+ firstPosition,
63
+ maxResults: PAGE_SIZE,
64
+ },
65
+ headers: {
66
+ Authorization: `Bearer ${this.token}`,
67
+ },
68
+ };
69
+ }
70
+ /**
71
+ * Request with retry logic for Bank Frick's API. In addition to standard retry logic, this function
72
+ * compares errors against expected errors from the Bank Frick API, and will throw without retries
73
+ * on a known fatal error, or try to refresh the JWT on a known auth error
74
+ **/
75
+ async makeRequest(axiosRequest, config, signingAlgorithm) {
76
+ let retryNumber = 0;
77
+ let response = await axios_1.default.request(axiosRequest);
78
+ while (response.status !== 200) {
79
+ retryNumber++;
80
+ logger.warn('Encountered error when fetching accounts from Bank Frick:', response.status, response.statusText);
81
+ // Evaluate whether the error was fatal, auth, or transient and whether we've exceeded the max number of retries.
82
+ // Throw on fatal, refresh token on auth error, pass on transient until we've exhausted our retries
83
+ if (FatalErrors[response.status]) {
84
+ throw new error_1.AdapterError({
85
+ statusCode: response.status,
86
+ message: response.statusText,
87
+ });
88
+ }
89
+ else if (AuthErrors[response.status]) {
90
+ // We've encountered a known auth error, so try to refresh the token before making another request
91
+ logger.info('Auth error received from the Bank Frick API, attempting to refresh the token');
92
+ this.token = await (0, util_1.generateJWT)(config, signingAlgorithm);
93
+ }
94
+ else if (retryNumber === config.REST_TRANSPORT_MAX_RATE_LIMIT_RETRIES) {
95
+ throw new error_1.AdapterError({
96
+ statusCode: 504,
97
+ message: `Bank Frick transport hit the max number of retries (${config.REST_TRANSPORT_MAX_RATE_LIMIT_RETRIES} retries) and aborted`,
98
+ });
99
+ }
100
+ logger.debug(`Sleeping for ${config.REST_TRANSPORT_MS_BETWEEN_RATE_LIMIT_RETRIES}ms before retrying`);
101
+ await (0, util_2.sleep)(config.REST_TRANSPORT_MS_BETWEEN_RATE_LIMIT_RETRIES);
102
+ response = await axios_1.default.request(axiosRequest);
103
+ }
104
+ return response;
105
+ }
106
+ validateInputParams(params) {
107
+ const encounteredIds = {};
108
+ const errors = [];
109
+ const { ibanIDs } = params;
110
+ ibanIDs.forEach((v) => {
111
+ if (!v.match(ibanPattern)) {
112
+ errors.push(`Invalid IBAN: ${v}`);
113
+ }
114
+ encounteredIds[v] += 1;
115
+ if (encounteredIds[v] > 1) {
116
+ errors.push(`The following IBAN appears more than once in the input parameters: ${v}`);
117
+ }
118
+ });
119
+ return errors;
120
+ }
121
+ /**
122
+ * Fetches pages of data from the Bank Frick API, scans for accounts by IBAN, and returns the balance
123
+ * of all found accounts. Returns a 404 if any IBAN isn't found.
124
+ */
125
+ async setup(req, config) {
126
+ const { ibanIDs, signingAlgorithm } = req.requestContext.data;
127
+ const { PAGE_SIZE = 500 } = config;
128
+ logger.debug(`Validating input: ${JSON.stringify(req.requestContext.data)}`);
129
+ // Scan ibanIDs for duplicates and invalid IBANs
130
+ const validationErrors = this.validateInputParams(req.requestContext.data);
131
+ if (validationErrors.length > 0) {
132
+ throw new error_1.AdapterError({
133
+ statusCode: 420,
134
+ message: `Received the following errors when validating inputParameters:\n ${validationErrors.join('\n')}`,
135
+ });
136
+ }
137
+ // Refresh the token if it isn't set
138
+ if (!this.token) {
139
+ this.token = await (0, util_1.generateJWT)(config, signingAlgorithm);
140
+ }
141
+ let sum = 0;
142
+ let position = 0;
143
+ const keys = ibanIDs;
144
+ logger.info("Fetching accounts from Bank Frick's API...");
145
+ while (keys.length > 0) {
146
+ // TODO Fetching and processing pages can be run concurrently
147
+ const axiosRequest = this.prepareRequest(position, req.requestContext.data, config);
148
+ const response = await this.makeRequest(axiosRequest, config, signingAlgorithm);
149
+ logger.debug(`Evaluating accounts from page ${position / PAGE_SIZE}`);
150
+ response.data.accounts.forEach((v) => {
151
+ logger.trace(`Evaluating ${v.account} (iban: ${v.iban}, type: ${v.type})`);
152
+ const index = ibanIDs.indexOf(v.iban);
153
+ if (index > -1) {
154
+ keys.splice(index, 1);
155
+ sum += v.balance;
156
+ logger.trace(`Found account: ${v.account} (iban: ${v.iban}) with balance of ${v.balance}`);
157
+ logger.trace(`Running sum: ${sum}, number of ibans left to find: ${keys.length}/${ibanIDs.length}`);
158
+ }
159
+ });
160
+ if (!response.data.moreResults) {
161
+ logger.debug('No more results, breaking out of account query loop');
162
+ break;
163
+ }
164
+ position += PAGE_SIZE || 0;
165
+ }
166
+ // 404 if one or more accounts were not found
167
+ if (keys.length > 0) {
168
+ logger.error(`Could not find all accounts, returning 404: ${keys.join(', ')}`);
169
+ const res = {
170
+ data: 0,
171
+ statusCode: 404,
172
+ result: 0,
173
+ };
174
+ await this.cache.set(req.requestContext.cacheKey, res, config.CACHE_MAX_AGE);
175
+ }
176
+ logger.debug('Was able to find all accounts, returning balance across all accounts: ', sum);
177
+ const res = {
178
+ data: sum,
179
+ statusCode: 200,
180
+ result: sum,
181
+ };
182
+ await this.cache.set(req.requestContext.cacheKey, res, config.CACHE_MAX_AGE);
183
+ return res;
184
+ }
185
+ }
186
+ exports.BankFrickAccountsTransport = BankFrickAccountsTransport;
187
+ exports.accountsRestEndpoint = {
188
+ name: 'accounts',
189
+ transport: new BankFrickAccountsTransport(),
190
+ inputParameters,
191
+ };
@@ -0,0 +1,45 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.customSettings = exports.signingAlgorithms = void 0;
7
+ const crypto_1 = __importDefault(require("crypto"));
8
+ const util_1 = require("../../../util");
9
+ const logger = (0, util_1.makeLogger)('BankFrickConfig');
10
+ // Used for enum options and validation
11
+ exports.signingAlgorithms = ['rsa-sha256', 'rsa-sha384', 'rsa-sha512'];
12
+ const MAX_PAGE_SIZE = 500;
13
+ exports.customSettings = {
14
+ PAGE_SIZE: {
15
+ description: 'The number of accounts to fetch per call to /accounts. Must be >= 1 and <= 500.',
16
+ type: 'enum',
17
+ required: false,
18
+ default: MAX_PAGE_SIZE.toString(),
19
+ options: [...Array(MAX_PAGE_SIZE).keys()].map((i) => (i + 1).toString()), // 1..MAX_PAGE_SIZE
20
+ },
21
+ PRIVATE_KEY: {
22
+ description: '',
23
+ type: 'string',
24
+ required: true,
25
+ validate: (value) => {
26
+ if (!value) {
27
+ return 'PRIVATE_KEY is a required variable, was undefined or empty';
28
+ }
29
+ else if (typeof value !== 'string') {
30
+ return `PRIVATE_KEY must be a string, was ${typeof value}`;
31
+ }
32
+ for (const algorithm of exports.signingAlgorithms) {
33
+ const body = { example: 123 };
34
+ try {
35
+ crypto_1.default.sign(algorithm, Buffer.from(JSON.stringify(body)), value);
36
+ return ''; // Any successful sign means the key is valid
37
+ }
38
+ catch {
39
+ logger.debug(`Failed to sign a dummy body using PRIVATE_KEY with algorithm ${algorithm}`);
40
+ }
41
+ }
42
+ return `PRIVATE_KEY could not be used to sign a dummy body using any of ${exports.signingAlgorithms}`;
43
+ },
44
+ },
45
+ };
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.adapter = void 0;
4
+ const accounts_1 = require("./accounts");
5
+ const config_1 = require("./config");
6
+ exports.adapter = {
7
+ name: 'bank-frick',
8
+ defaultEndpoint: 'accounts',
9
+ endpoints: [accounts_1.accountsRestEndpoint],
10
+ customSettings: config_1.customSettings,
11
+ envDefaultOverrides: {
12
+ API_ENDPOINT: 'https://olbsandbox.bankfrick.li/webapi/v2',
13
+ },
14
+ };