@chainlink/external-adapter-framework 0.0.7 → 0.0.10

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 (127) hide show
  1. package/adapter.d.ts +88 -0
  2. package/{dist/src/adapter.js → adapter.js} +0 -0
  3. package/background-executor.d.ts +11 -0
  4. package/{dist/background-executor.js → background-executor.js} +0 -0
  5. package/cache/factory.d.ts +6 -0
  6. package/{dist/cache → cache}/factory.js +0 -0
  7. package/cache/index.d.ts +90 -0
  8. package/{dist/src/cache → cache}/index.js +6 -2
  9. package/cache/local.d.ts +23 -0
  10. package/{dist/cache → cache}/local.js +1 -1
  11. package/cache/metrics.d.ts +27 -0
  12. package/{dist/cache → cache}/metrics.js +7 -1
  13. package/cache/redis.d.ts +16 -0
  14. package/{dist/cache → cache}/redis.js +2 -2
  15. package/config/index.d.ts +195 -0
  16. package/{dist/src/config → config}/index.js +5 -6
  17. package/config/provider-limits.d.ts +31 -0
  18. package/{dist/src/config → config}/provider-limits.js +1 -1
  19. package/examples/coingecko/batch-warming.d.ts +2 -0
  20. package/{dist/examples → examples}/coingecko/batch-warming.js +0 -0
  21. package/examples/coingecko/index.d.ts +2 -0
  22. package/{dist/examples → examples}/coingecko/index.js +0 -0
  23. package/examples/coingecko/rest.d.ts +2 -0
  24. package/{dist/examples → examples}/coingecko/rest.js +0 -0
  25. package/examples/ncfx/config/index.d.ts +12 -0
  26. package/{dist/examples → examples}/ncfx/config/index.js +0 -0
  27. package/examples/ncfx/index.d.ts +2 -0
  28. package/{dist/examples → examples}/ncfx/index.js +0 -0
  29. package/examples/ncfx/websocket.d.ts +36 -0
  30. package/{dist/examples → examples}/ncfx/websocket.js +0 -0
  31. package/index.d.ts +12 -0
  32. package/{dist/index.js → index.js} +3 -0
  33. package/metrics/constants.d.ts +16 -0
  34. package/{dist/src/metrics → metrics}/constants.js +1 -1
  35. package/metrics/index.d.ts +15 -0
  36. package/{dist/metrics → metrics}/index.js +48 -1
  37. package/metrics/util.d.ts +3 -0
  38. package/{dist/metrics → metrics}/util.js +0 -0
  39. package/package.json +2 -6
  40. package/rate-limiting/background/fixed-frequency.d.ts +10 -0
  41. package/{dist/src/rate-limiting → rate-limiting}/background/fixed-frequency.js +0 -0
  42. package/rate-limiting/index.d.ts +54 -0
  43. package/{dist/src/rate-limiting → rate-limiting}/index.js +0 -0
  44. package/rate-limiting/metrics.d.ts +3 -0
  45. package/{dist/src/rate-limiting → rate-limiting}/metrics.js +14 -2
  46. package/rate-limiting/request/simple-counting.d.ts +20 -0
  47. package/{dist/src/rate-limiting → rate-limiting}/request/simple-counting.js +0 -0
  48. package/test.d.ts +1 -0
  49. package/{dist/test.js → test.js} +0 -0
  50. package/transports/batch-warming.d.ts +34 -0
  51. package/{dist/src/transports → transports}/batch-warming.js +47 -1
  52. package/transports/index.d.ts +87 -0
  53. package/{dist/src/transports → transports}/index.js +5 -3
  54. package/transports/metrics.d.ts +21 -0
  55. package/{dist/src/transports → transports}/metrics.js +28 -42
  56. package/transports/rest.d.ts +43 -0
  57. package/{dist/src/transports → transports}/rest.js +38 -2
  58. package/transports/util.d.ts +8 -0
  59. package/{dist/src/transports → transports}/util.js +16 -16
  60. package/transports/websocket.d.ts +80 -0
  61. package/{dist/src/transports → transports}/websocket.js +8 -14
  62. package/util/expiring-sorted-set.d.ts +21 -0
  63. package/{dist/src/util → util}/expiring-sorted-set.js +0 -0
  64. package/util/index.d.ts +11 -0
  65. package/{dist/src/util → util}/index.js +0 -0
  66. package/util/logger.d.ts +42 -0
  67. package/{dist/src/util → util}/logger.js +0 -0
  68. package/util/request.d.ts +55 -0
  69. package/{dist/src/util → util}/request.js +0 -0
  70. package/validation/error.d.ts +50 -0
  71. package/validation/error.js +79 -0
  72. package/validation/index.d.ts +5 -0
  73. package/{dist/src/validation → validation}/index.js +8 -6
  74. package/validation/input-params.d.ts +15 -0
  75. package/{dist/src/validation → validation}/input-params.js +0 -0
  76. package/validation/override-functions.d.ts +3 -0
  77. package/{dist/src/validation → validation}/override-functions.js +0 -0
  78. package/{dist/src/validation → validation}/preset-tokens.json +0 -0
  79. package/validation/validator.d.ts +47 -0
  80. package/{dist/src/validation → validation}/validator.js +0 -0
  81. package/README.md +0 -103
  82. package/dist/adapter.js +0 -60
  83. package/dist/cache/index.js +0 -163
  84. package/dist/config/index.js +0 -364
  85. package/dist/config/provider-limits.js +0 -75
  86. package/dist/metrics/constants.js +0 -25
  87. package/dist/rate-limiting/factory.js +0 -33
  88. package/dist/rate-limiting/index.js +0 -36
  89. package/dist/rate-limiting/metrics.js +0 -32
  90. package/dist/rate-limiting/nop-limiter.js +0 -15
  91. package/dist/rate-limiting/simple-counting.js +0 -61
  92. package/dist/src/background-executor.js +0 -45
  93. package/dist/src/cache/factory.js +0 -57
  94. package/dist/src/cache/local.js +0 -83
  95. package/dist/src/cache/metrics.js +0 -114
  96. package/dist/src/cache/redis.js +0 -100
  97. package/dist/src/examples/bank-frick/accounts.js +0 -191
  98. package/dist/src/examples/bank-frick/config/index.js +0 -45
  99. package/dist/src/examples/bank-frick/index.js +0 -14
  100. package/dist/src/examples/bank-frick/util.js +0 -39
  101. package/dist/src/examples/coingecko/batch-warming.js +0 -52
  102. package/dist/src/examples/coingecko/index.js +0 -10
  103. package/dist/src/examples/coingecko/rest.js +0 -50
  104. package/dist/src/examples/ncfx/config/index.js +0 -15
  105. package/dist/src/examples/ncfx/index.js +0 -10
  106. package/dist/src/examples/ncfx/websocket.js +0 -72
  107. package/dist/src/index.js +0 -89
  108. package/dist/src/metrics/index.js +0 -76
  109. package/dist/src/metrics/util.js +0 -9
  110. package/dist/src/test.js +0 -6
  111. package/dist/src/validation/error.js +0 -41
  112. package/dist/transports/batch-warming.js +0 -57
  113. package/dist/transports/index.js +0 -76
  114. package/dist/transports/metrics.js +0 -133
  115. package/dist/transports/rest.js +0 -91
  116. package/dist/transports/util.js +0 -85
  117. package/dist/transports/websocket.js +0 -171
  118. package/dist/util/expiring-sorted-set.js +0 -47
  119. package/dist/util/index.js +0 -35
  120. package/dist/util/logger.js +0 -62
  121. package/dist/util/request.js +0 -2
  122. package/dist/validation/error.js +0 -41
  123. package/dist/validation/index.js +0 -82
  124. package/dist/validation/input-params.js +0 -30
  125. package/dist/validation/overrideFunctions.js +0 -42
  126. package/dist/validation/presetTokens.json +0 -23
  127. package/dist/validation/validator.js +0 -303
@@ -1,39 +0,0 @@
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.generateJWT = void 0;
7
- const crypto_1 = __importDefault(require("crypto"));
8
- const axios_1 = __importDefault(require("axios"));
9
- const util_1 = require("../../util");
10
- const logger = (0, util_1.makeLogger)('BankFrickUtil');
11
- // Used by all endpoints requiring authentication
12
- const generateJWT = async (config, signingAlgorithm = 'rsa-sha512') => {
13
- logger.info("Generating a new JWT because we don't have one in config.token");
14
- const { API_KEY, PRIVATE_KEY, API_ENDPOINT } = config;
15
- // All of these are required, so validation should have failed prior to this line
16
- if (!API_KEY || !PRIVATE_KEY) {
17
- throw new Error('API_KEY, PRIVATE_KEY, and PASSWORD all must be defined to get a new token\n' +
18
- 'Received: \n' +
19
- `API_KEY: ${API_KEY}\n` +
20
- `PRIVATE_KEY: ${PRIVATE_KEY}`);
21
- }
22
- const data = {
23
- key: API_KEY,
24
- };
25
- const signature = crypto_1.default.sign(signingAlgorithm, Buffer.from(JSON.stringify(data)), PRIVATE_KEY);
26
- const options = {
27
- method: 'POST',
28
- baseURL: API_ENDPOINT,
29
- url: `authorize`,
30
- headers: {
31
- Signature: signature.toString('base64'),
32
- algorithm: signingAlgorithm,
33
- },
34
- data,
35
- };
36
- const response = await axios_1.default.request(options);
37
- return response.data.token;
38
- };
39
- exports.generateJWT = generateJWT;
@@ -1,52 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.batchEndpoint = void 0;
4
- const batch_warming_1 = require("../../transports/batch-warming");
5
- const DEFAULT_URL = 'https://pro-api.coingecko.com/api/v3';
6
- const inputParameters = {
7
- coinid: {
8
- description: 'The CoinGecko id or array of ids of the coin(s) to query (Note: because of current limitations to use a dummy base will need to be supplied)',
9
- required: false,
10
- },
11
- base: {
12
- aliases: ['from', 'coin'],
13
- description: 'The symbol or array of symbols of the currency to query',
14
- required: true,
15
- },
16
- quote: {
17
- aliases: ['to', 'market'],
18
- description: 'The symbol of the currency to convert to',
19
- required: true,
20
- },
21
- };
22
- const batchEndpointTransport = new batch_warming_1.BatchWarmingTransport({
23
- prepareRequest: (params, context) => {
24
- return {
25
- baseURL: DEFAULT_URL,
26
- url: '/simple/price',
27
- method: 'GET',
28
- params: {
29
- ids: [...new Set(params.map((p) => p.base))].join(','),
30
- vs_currencies: [...new Set(params.map((p) => p.quote))].join(','),
31
- x_cg_pro_api_key: context.adapterConfig.API_KEY,
32
- },
33
- };
34
- },
35
- parseResponse: (res) => {
36
- const entries = [];
37
- for (const [base, entry] of Object.entries(res.data)) {
38
- for (const [quote, price] of Object.entries(entry)) {
39
- entries.push({
40
- params: { base, quote },
41
- value: price,
42
- });
43
- }
44
- }
45
- return entries;
46
- },
47
- });
48
- exports.batchEndpoint = {
49
- name: 'batch',
50
- transport: batchEndpointTransport,
51
- inputParameters,
52
- };
@@ -1,10 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.adapter = void 0;
4
- const batch_warming_1 = require("./batch-warming");
5
- const rest_1 = require("./rest");
6
- exports.adapter = {
7
- name: 'coingecko',
8
- defaultEndpoint: 'batch',
9
- endpoints: [rest_1.restEndpoint, batch_warming_1.batchEndpoint],
10
- };
@@ -1,50 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.restEndpoint = void 0;
4
- const transports_1 = require("../../transports");
5
- const DEFAULT_URL = 'https://api.coingecko.com/api/v3';
6
- const inputParameters = {
7
- coinid: {
8
- description: 'The CoinGecko id or array of ids of the coin(s) to query (Note: because of current limitations to use a dummy base will need to be supplied)',
9
- required: false,
10
- },
11
- base: {
12
- aliases: ['from', 'coin'],
13
- description: 'The symbol or array of symbols of the currency to query',
14
- required: true,
15
- },
16
- quote: {
17
- aliases: ['to', 'market'],
18
- description: 'The symbol of the currency to convert to',
19
- required: true,
20
- },
21
- };
22
- const restEndpointTransport = new transports_1.RestTransport({
23
- prepareRequest: (req) => {
24
- return {
25
- baseURL: DEFAULT_URL,
26
- url: '/simple/price',
27
- method: 'GET',
28
- params: {
29
- ids: req.requestContext.data.base,
30
- vs_currencies: req.requestContext.data.quote,
31
- },
32
- };
33
- },
34
- parseResponse: (req, res) => {
35
- return {
36
- data: res.data,
37
- statusCode: 200,
38
- result: res.data[req.requestContext.data.base]?.[req.requestContext.data.quote],
39
- };
40
- },
41
- options: {
42
- coalescing: true,
43
- },
44
- });
45
- exports.restEndpoint = {
46
- name: 'rest',
47
- aliases: ['qwe'],
48
- transport: restEndpointTransport,
49
- inputParameters,
50
- };
@@ -1,15 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.customSettings = void 0;
4
- exports.customSettings = {
5
- USERNAME: {
6
- description: 'Username for the NCFX API',
7
- type: 'string',
8
- required: true,
9
- },
10
- PASSWORD: {
11
- description: 'Password for the NCFX API',
12
- type: 'string',
13
- required: true,
14
- },
15
- };
@@ -1,10 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.adapter = void 0;
4
- const config_1 = require("./config");
5
- const websocket_1 = require("./websocket");
6
- exports.adapter = {
7
- name: 'ncfx',
8
- endpoints: [websocket_1.webSocketEndpoint],
9
- customSettings: config_1.customSettings,
10
- };
@@ -1,72 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.webSocketEndpoint = exports.websocketTransport = exports.inputParameters = void 0;
4
- const websocket_1 = require("../../transports/websocket");
5
- const util_1 = require("../../util");
6
- exports.inputParameters = {
7
- base: {
8
- aliases: ['from', 'coin'],
9
- description: 'The symbol of the currency to query',
10
- required: true,
11
- },
12
- quote: {
13
- aliases: ['to', 'market'],
14
- description: 'The symbol of the currency to convert to',
15
- required: true,
16
- },
17
- };
18
- const logger = (0, util_1.makeLogger)('NcfxWebSocketTransport');
19
- exports.websocketTransport = new websocket_1.WebSocketTransport({
20
- url: 'wss://feed.newchangefx.com/cryptodata',
21
- handlers: {
22
- open(connection, context) {
23
- return new Promise((resolve, reject) => {
24
- // Set up listener
25
- connection.on('message', (data) => {
26
- const parsed = JSON.parse(data.toString());
27
- if (parsed.Message?.startsWith('Logged in as user')) {
28
- logger.info('Got logged in response, connection is ready');
29
- resolve();
30
- }
31
- else {
32
- reject(new Error('Unexpected message after WS connection open'));
33
- }
34
- });
35
- // Send login payload
36
- connection.send(JSON.stringify({
37
- request: 'login',
38
- username: context.adapterConfig.USERNAME,
39
- password: context.adapterConfig.PASSWORD,
40
- }));
41
- });
42
- },
43
- message(message) {
44
- if (!Array.isArray(message)) {
45
- logger.debug('WS message is not array, skipping');
46
- return [];
47
- }
48
- return message.map((m) => {
49
- const [base, quote] = m.currencyPair.split('/');
50
- return {
51
- params: { base, quote },
52
- value: m.offer,
53
- };
54
- });
55
- },
56
- },
57
- builders: {
58
- subscribeMessage: (params) => ({
59
- request: 'subscribe',
60
- ccy: `${params.base}/${params.quote}`,
61
- }),
62
- unsubscribeMessage: (params) => ({
63
- request: 'unsubscribe',
64
- ccy: `${params.base}/${params.quote}`,
65
- }),
66
- },
67
- });
68
- exports.webSocketEndpoint = {
69
- name: 'websocket',
70
- transport: exports.websocketTransport,
71
- inputParameters: exports.inputParameters,
72
- };
package/dist/src/index.js DELETED
@@ -1,89 +0,0 @@
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.expose = void 0;
7
- const fastify_1 = __importDefault(require("fastify"));
8
- const path_1 = require("path");
9
- const adapter_1 = require("./adapter");
10
- const background_executor_1 = require("./background-executor");
11
- const cache_1 = require("./cache");
12
- const config_1 = require("./config");
13
- const metrics_1 = require("./metrics");
14
- const transports_1 = require("./transports");
15
- const util_1 = require("./util");
16
- const validation_1 = require("./validation");
17
- const logger = (0, util_1.makeLogger)('Main');
18
- const VERSION = process.env['npm_package_version'];
19
- /**
20
- * Main function for the framework.
21
- * Initializes config and dependencies, uses those to initialize Transports, and starts listening for requests.
22
- *
23
- * @param adapter - an object describing an External Adapter
24
- * @param dependencies - an optional object with adapter dependencies to inject
25
- * @returns a Promise that resolves to the http.Server listening for connections
26
- */
27
- const expose = async (adapter, dependencies) => {
28
- const config = (0, config_1.buildAdapterConfig)({
29
- overrides: adapter.envDefaultOverrides,
30
- customSettings: adapter.customSettings,
31
- });
32
- // Initialize adapter (create dependencies, inject them, build endpoint map, etc.)
33
- const initializedAdapter = await (0, adapter_1.initializeAdapter)(adapter, config, dependencies);
34
- let server = undefined;
35
- if (config.METRICS_ENABLED && config.EXPERIMENTAL_METRICS_ENABLED) {
36
- (0, metrics_1.setupMetricsServer)(adapter.name, config);
37
- }
38
- if (config.EA_MODE === 'reader' || config.EA_MODE === 'reader-writer') {
39
- // Main REST API server to handle incoming requests
40
- const app = await buildRestApi(config, initializedAdapter);
41
- // Start listening for incoming requests
42
- try {
43
- await app.listen(config.EA_PORT, config.EA_HOST);
44
- }
45
- catch (err) {
46
- logger.fatal(`There was an error when starting the EA server: ${err}`);
47
- process.exit();
48
- }
49
- logger.info(`Listening on port ${app.server.address().port}`);
50
- server = app.server;
51
- }
52
- else {
53
- logger.info('REST API is disabled; this instance will not process incoming requests.');
54
- }
55
- if (config.EA_MODE === 'writer' || config.EA_MODE === 'reader-writer') {
56
- // Start background loop that will take care of calling any async Transports
57
- logger.info('Starting background execution loop');
58
- (0, background_executor_1.callBackgroundExecutes)(initializedAdapter, server);
59
- }
60
- else {
61
- logger.info('Background executor is disabled; this instance will not perform async background executes.');
62
- }
63
- return server;
64
- };
65
- exports.expose = expose;
66
- async function buildRestApi(config, initializedAdapter) {
67
- const app = (0, fastify_1.default)();
68
- // Add healthcheck endpoint before middlewares to bypass them
69
- app.get((0, path_1.join)(config.BASE_URL, 'health'), (req, res) => {
70
- res.status(200).send({ message: 'OK', version: VERSION });
71
- });
72
- // Use global error handling
73
- app.setErrorHandler(validation_1.errorCatchingMiddleware);
74
- const transportHandler = await (0, transports_1.buildTransportHandler)(initializedAdapter);
75
- app.register(async (router) => {
76
- // Set up "middlewares" (hooks in fastify)
77
- router.addHook('preHandler', (0, validation_1.validatorMiddleware)(initializedAdapter));
78
- router.addHook('preHandler', (0, cache_1.buildCacheMiddleware)(initializedAdapter));
79
- if (config['CORRELATION_ID_ENABLED']) {
80
- router.addHook('onRequest', util_1.loggingContextMiddleware);
81
- }
82
- router.route({
83
- url: config.BASE_URL,
84
- method: 'POST',
85
- handler: transportHandler,
86
- }); // Pass config maybe? cleaner that multiple instances / dependencies
87
- });
88
- return app;
89
- }
@@ -1,76 +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.httpRequestDurationSeconds = exports.httpRequestsTotal = 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 logger = (0, util_1.makeLogger)('Metrics');
36
- function setupMetricsServer(name, config) {
37
- const metricsApp = (0, fastify_1.default)({
38
- logger: false,
39
- });
40
- const metricsPort = config.METRICS_PORT;
41
- const endpoint = config.METRICS_USE_BASE_URL ? (0, path_1.join)(config.BASE_URL, 'metrics') : '/metrics';
42
- const eaHost = config.EA_HOST;
43
- (0, exports.setupMetrics)(name, config);
44
- metricsApp.get(endpoint, async (_, res) => {
45
- res.type('txt');
46
- res.send(await client.register.metrics());
47
- });
48
- metricsApp.listen(metricsPort, eaHost, () => logger.info(`Monitoring listening on port ${metricsPort}!`));
49
- }
50
- exports.setupMetricsServer = setupMetricsServer;
51
- const setupMetrics = (name, config) => {
52
- client.collectDefaultMetrics();
53
- client.register.setDefaultLabels({
54
- app_name: config.METRICS_NAME || name || 'N/A',
55
- app_version: config['npm_package_version'],
56
- });
57
- };
58
- exports.setupMetrics = setupMetrics;
59
- exports.httpRequestsTotal = new client.Counter({
60
- name: 'http_requests_total',
61
- help: 'The number of http requests this external adapter has serviced for its entire uptime',
62
- labelNames: [
63
- 'method',
64
- 'status_code',
65
- 'retry',
66
- 'type',
67
- 'is_cache_warming',
68
- 'feed_id',
69
- 'provider_status_code',
70
- ],
71
- });
72
- exports.httpRequestDurationSeconds = new client.Histogram({
73
- name: 'http_request_duration_seconds',
74
- help: 'A histogram bucket of the distribution of http request durations',
75
- buckets: constants_1.requestDurationBuckets,
76
- });
@@ -1,9 +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 = (endpoint, data) => {
6
- const feedId = (0, cache_1.calculateFeedId)(endpoint, data);
7
- return { feedId };
8
- };
9
- exports.getMetricsMeta = getMetricsMeta;
package/dist/src/test.js DELETED
@@ -1,6 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const _1 = require(".");
4
- const bank_frick_1 = require("./examples/bank-frick");
5
- // Start sample adapter
6
- (0, _1.expose)(bank_frick_1.adapter);
@@ -1,41 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.AdapterError = void 0;
4
- class AdapterError extends Error {
5
- constructor({ jobRunID = '1', status = 'errored', statusCode = 500, name = 'AdapterError', message = 'An error occurred.', cause, url, errorResponse, feedID, providerStatusCode, }) {
6
- super(message);
7
- this.jobRunID = jobRunID;
8
- this.status = status;
9
- this.statusCode = statusCode;
10
- this.name = name;
11
- this.message = message;
12
- this.cause = cause;
13
- if (url) {
14
- this.url = url;
15
- }
16
- if (feedID) {
17
- this.feedID = feedID;
18
- }
19
- this.errorResponse = errorResponse;
20
- this.providerStatusCode = providerStatusCode;
21
- }
22
- toJSONResponse() {
23
- const showDebugInfo = process.env['NODE_ENV'] === 'development' || process.env['DEBUG'] === 'true';
24
- const errorBasic = {
25
- name: this.name,
26
- message: this.message,
27
- url: this.url,
28
- errorResponse: this.errorResponse,
29
- feedID: this.feedID,
30
- };
31
- const errorFull = { ...errorBasic, stack: this.stack, cause: this.cause };
32
- return {
33
- jobRunID: this.jobRunID,
34
- status: this.status,
35
- statusCode: this.statusCode,
36
- providerStatusCode: this.providerStatusCode,
37
- error: showDebugInfo ? errorFull : errorBasic,
38
- };
39
- }
40
- }
41
- exports.AdapterError = AdapterError;
@@ -1,57 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.BatchWarmingTransport = exports.DEFAULT_WARMER_PERIOD = void 0;
4
- const util_1 = require("../util");
5
- const _1 = require("./");
6
- const util_2 = require("./util");
7
- const DEFAULT_WARMER_TTL = 10000;
8
- exports.DEFAULT_WARMER_PERIOD = 5000; // TODO: rate limit, this will be dynamic
9
- const logger = (0, util_1.makeLogger)('BatchWarmingTransport');
10
- /**
11
- * Transport implementation that takes incoming batches requests and keeps a warm cache of values.
12
- * Within the setup function, adapter params are added to an set that also keeps track and expires values.
13
- * In the background execute, the list of non-expired items in the set is fetched.
14
- * Then, the list is passed through the `prepareRequest` function, that returns an AxiosRequestConfig.
15
- * The Data Provider response is, they are passed through the `parseResponse` function to create a [[CacheEntry]] list.
16
- * Finally, the items in that [[CacheEntry]] list are set in the Cache so the Adapter can fetch values from there.
17
- *
18
- * @typeParam AdapterParams - interface for the adapter request body
19
- * @typeParam ProviderRequestBody - interface for the body of the request to the Data Provider
20
- * @typeParam ProviderResponseBody - interface for the body of the Data Provider's response
21
- */
22
- class BatchWarmingTransport {
23
- constructor(config) {
24
- this.config = config;
25
- this.expiringSortedSet = new util_1.ExpiringSortedSet(); // TODO: Move to dependencies, inject
26
- }
27
- async initialize(dependencies) {
28
- this.cache = dependencies.cache;
29
- }
30
- async hasBeenSetUp(req) {
31
- return !!this.expiringSortedSet.get(req.requestContext.cacheKey);
32
- }
33
- async setup(req) {
34
- logger.debug(`Adding entry to batch warming set: [${req.requestContext.cacheKey}] = ${req.requestContext.data}`);
35
- this.expiringSortedSet.add(req.requestContext.cacheKey, req.requestContext.data, DEFAULT_WARMER_TTL);
36
- }
37
- async backgroundExecute(context) {
38
- logger.debug('Starting background execute');
39
- const entries = this.expiringSortedSet.getAll();
40
- if (!entries.length) {
41
- logger.debug('No entries in batch warming set, skipping');
42
- return exports.DEFAULT_WARMER_PERIOD; // TODO: configurable, rate limit
43
- }
44
- const request = this.config.prepareRequest(entries, context);
45
- logger.debug('Sending request to data provider...');
46
- const providerResponse = await (0, util_2.axiosRequest)(request);
47
- logger.debug(`Got response from provider, parsing (raw body: ${providerResponse.data})`);
48
- const results = this.config.parseResponse(providerResponse, context);
49
- const adapterResponses = (0, _1.buildCacheEntriesFromResults)(results, context);
50
- logger.debug('Setting adapter responses in cache');
51
- await this.cache.setMany(adapterResponses, context.adapterConfig.CACHE_MAX_AGE);
52
- // TODO: Record rate limit credit with cost, default 1
53
- logger.debug('Background execute complete');
54
- return exports.DEFAULT_WARMER_PERIOD;
55
- }
56
- }
57
- exports.BatchWarmingTransport = BatchWarmingTransport;
@@ -1,76 +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.buildTransportHandler = exports.buildCacheEntriesFromResults = void 0;
18
- const cache_1 = require("../cache");
19
- const util_1 = require("../util");
20
- __exportStar(require("./batch-warming"), exports);
21
- __exportStar(require("./rest"), exports);
22
- __exportStar(require("./websocket"), exports);
23
- const logger = (0, util_1.makeLogger)('Transport');
24
- /**
25
- * Helper method to build cache entries to set after getting a bunch of responses from a DP.
26
- *
27
- * @param results - a list of results coming from a DataProvider
28
- * @param context - context for the Adapter
29
- * @returns a list of CacheEntries of AdapterResponses
30
- */
31
- const buildCacheEntriesFromResults = (results, context) => results.map((r) => ({
32
- key: (0, cache_1.calculateCacheKey)(context, r.params),
33
- value: {
34
- result: r.value,
35
- statusCode: 200,
36
- data: null,
37
- maxAge: Date.now() + context.adapterConfig.CACHE_MAX_AGE,
38
- metricsMeta: {
39
- feedId: (0, cache_1.calculateFeedId)(context.adapterEndpoint, r.params)
40
- }
41
- },
42
- }));
43
- exports.buildCacheEntriesFromResults = buildCacheEntriesFromResults;
44
- /**
45
- * Takes an Adapter, its configuration, and its dependencies, and it creates an express middleware
46
- * that will pass along the AdapterRequest to the appropriate Transport (acc. to the endpoint in the req.)
47
- *
48
- * @param adapter - main adapter object, already initialized
49
- * @returns the transport handler middleware function
50
- */
51
- const buildTransportHandler = (adapter) => async (req, reply) => {
52
- // Get transport, must be here because it's already checked in the validator
53
- const transport = adapter.endpointsMap[req.requestContext.endpointName].transport;
54
- // Set up transport if it hasn't been done already
55
- if (!(await transport.hasBeenSetUp(req))) {
56
- logger.debug('Transport not set up yet, doing so...');
57
- const immediateResponse = await transport.setup(req, adapter.config);
58
- if (immediateResponse) {
59
- logger.debug('Got immediate response from transport, sending as response');
60
- return reply.send(immediateResponse);
61
- }
62
- }
63
- logger.debug('Transport is set up, polling cache for response...');
64
- const response = await (0, cache_1.pollResponseFromCache)(adapter.dependencies.cache, req.requestContext.cacheKey, {
65
- maxRetries: adapter.config.CACHE_POLLING_MAX_RETRIES,
66
- sleep: adapter.config.CACHE_POLLING_SLEEP_MS,
67
- });
68
- if (response) {
69
- logger.debug('Got a response from the cache, sending that back');
70
- return reply.send(response);
71
- }
72
- logger.debug('Ran out of polling attempts, returning timeout');
73
- reply.statusCode = 504;
74
- return reply.send();
75
- };
76
- exports.buildTransportHandler = buildTransportHandler;