@chainlink/external-adapter-framework 0.0.10 → 0.0.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/adapter.d.ts +22 -3
- package/adapter.js +5 -2
- package/cache/factory.js +0 -2
- package/cache/index.d.ts +6 -2
- package/cache/index.js +13 -9
- package/cache/redis.js +5 -5
- package/chainlink-external-adapter-framework-0.0.6.tgz +0 -0
- package/config/index.d.ts +15 -1
- package/config/index.js +19 -4
- package/config/provider-limits.js +5 -1
- package/examples/bank-frick/accounts.d.ts +39 -0
- package/examples/bank-frick/accounts.js +191 -0
- package/examples/bank-frick/config/index.d.ts +4 -0
- package/examples/bank-frick/config/index.js +54 -0
- package/examples/bank-frick/index.d.ts +2 -0
- package/examples/bank-frick/index.js +14 -0
- package/examples/bank-frick/util.d.ts +4 -0
- package/examples/bank-frick/util.js +39 -0
- package/index.d.ts +1 -2
- package/index.js +42 -1
- package/metrics/index.js +0 -1
- package/metrics/util.d.ts +5 -1
- package/metrics/util.js +2 -2
- package/package/adapter.d.ts +88 -0
- package/package/adapter.js +112 -0
- package/package/background-executor.d.ts +11 -0
- package/package/background-executor.js +45 -0
- package/package/cache/factory.d.ts +6 -0
- package/package/cache/factory.js +57 -0
- package/package/cache/index.d.ts +90 -0
- package/package/cache/index.js +169 -0
- package/package/cache/local.d.ts +23 -0
- package/package/cache/local.js +83 -0
- package/package/cache/metrics.d.ts +27 -0
- package/package/cache/metrics.js +120 -0
- package/package/cache/redis.d.ts +16 -0
- package/package/cache/redis.js +100 -0
- package/package/config/index.d.ts +195 -0
- package/package/config/index.js +365 -0
- package/package/config/provider-limits.d.ts +31 -0
- package/package/config/provider-limits.js +76 -0
- package/package/examples/coingecko/batch-warming.d.ts +2 -0
- package/package/examples/coingecko/batch-warming.js +52 -0
- package/package/examples/coingecko/index.d.ts +2 -0
- package/package/examples/coingecko/index.js +10 -0
- package/package/examples/coingecko/rest.d.ts +2 -0
- package/package/examples/coingecko/rest.js +50 -0
- package/package/examples/ncfx/config/index.d.ts +12 -0
- package/package/examples/ncfx/config/index.js +15 -0
- package/package/examples/ncfx/index.d.ts +2 -0
- package/package/examples/ncfx/index.js +10 -0
- package/package/examples/ncfx/websocket.d.ts +36 -0
- package/package/examples/ncfx/websocket.js +72 -0
- package/package/index.d.ts +12 -0
- package/package/index.js +92 -0
- package/package/metrics/constants.d.ts +16 -0
- package/package/metrics/constants.js +25 -0
- package/package/metrics/index.d.ts +15 -0
- package/package/metrics/index.js +123 -0
- package/package/metrics/util.d.ts +3 -0
- package/package/metrics/util.js +9 -0
- package/package/package.json +72 -0
- package/package/rate-limiting/background/fixed-frequency.d.ts +10 -0
- package/package/rate-limiting/background/fixed-frequency.js +37 -0
- package/package/rate-limiting/index.d.ts +54 -0
- package/package/rate-limiting/index.js +63 -0
- package/package/rate-limiting/metrics.d.ts +3 -0
- package/package/rate-limiting/metrics.js +44 -0
- package/package/rate-limiting/request/simple-counting.d.ts +20 -0
- package/package/rate-limiting/request/simple-counting.js +62 -0
- package/package/test.d.ts +1 -0
- package/package/test.js +6 -0
- package/package/transports/batch-warming.d.ts +34 -0
- package/package/transports/batch-warming.js +101 -0
- package/package/transports/index.d.ts +87 -0
- package/package/transports/index.js +87 -0
- package/package/transports/metrics.d.ts +21 -0
- package/package/transports/metrics.js +105 -0
- package/package/transports/rest.d.ts +43 -0
- package/package/transports/rest.js +129 -0
- package/package/transports/util.d.ts +8 -0
- package/package/transports/util.js +85 -0
- package/package/transports/websocket.d.ts +80 -0
- package/package/transports/websocket.js +169 -0
- package/package/util/expiring-sorted-set.d.ts +21 -0
- package/package/util/expiring-sorted-set.js +47 -0
- package/package/util/index.d.ts +11 -0
- package/package/util/index.js +35 -0
- package/package/util/logger.d.ts +42 -0
- package/package/util/logger.js +62 -0
- package/package/util/request.d.ts +55 -0
- package/package/util/request.js +2 -0
- package/package/validation/error.d.ts +50 -0
- package/package/validation/error.js +79 -0
- package/package/validation/index.d.ts +5 -0
- package/package/validation/index.js +86 -0
- package/package/validation/input-params.d.ts +15 -0
- package/package/validation/input-params.js +30 -0
- package/package/validation/override-functions.d.ts +3 -0
- package/package/validation/override-functions.js +40 -0
- package/package/validation/preset-tokens.json +23 -0
- package/package/validation/validator.d.ts +47 -0
- package/package/validation/validator.js +303 -0
- package/package.json +5 -3
- package/rate-limiting/background/fixed-frequency.js +0 -2
- package/test.js +2 -2
- package/transports/batch-warming.d.ts +4 -3
- package/transports/batch-warming.js +4 -4
- package/transports/index.d.ts +4 -21
- package/transports/index.js +3 -3
- package/transports/metrics.d.ts +1 -1
- package/transports/metrics.js +2 -2
- package/transports/rest.d.ts +2 -1
- package/transports/rest.js +3 -1
- package/transports/websocket.d.ts +5 -4
- package/transports/websocket.js +5 -6
- package/util/index.d.ts +2 -1
- package/util/index.js +1 -1
- package/util/request.d.ts +3 -1
- package/util/subscription-set/expiring-sorted-set.d.ts +22 -0
- package/util/subscription-set/expiring-sorted-set.js +47 -0
- package/util/subscription-set/subscription-set.d.ts +18 -0
- package/util/subscription-set/subscription-set.js +19 -0
- package/util/test-payload-loader.d.ts +25 -0
- package/util/test-payload-loader.js +83 -0
- package/validation/error.d.ts +2 -2
- package/validation/error.js +1 -1
- package/validation/index.js +8 -3
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ExpiringSortedSet = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* This class implements a set of unique items, each of which has an expiration timestamp.
|
|
6
|
+
* On reads, items that have expired will be deleted from the set and not returned.
|
|
7
|
+
*
|
|
8
|
+
* @typeParam T - the type of the set entries' values
|
|
9
|
+
*/
|
|
10
|
+
class ExpiringSortedSet {
|
|
11
|
+
constructor() {
|
|
12
|
+
this.map = new Map();
|
|
13
|
+
}
|
|
14
|
+
add(key, value, ttl) {
|
|
15
|
+
this.map.set(key, {
|
|
16
|
+
value,
|
|
17
|
+
expirationTimestamp: Date.now() + ttl,
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
get(key) {
|
|
21
|
+
const entry = this.map.get(key);
|
|
22
|
+
if (!entry) {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
else if (entry.expirationTimestamp < Date.now()) {
|
|
26
|
+
return entry.value;
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
this.map.delete(key);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
getAll() {
|
|
33
|
+
const results = [];
|
|
34
|
+
const now = Date.now();
|
|
35
|
+
// Since we're iterating, might as well prune here
|
|
36
|
+
for (const [key, entry] of this.map.entries()) {
|
|
37
|
+
if (entry.expirationTimestamp < now) {
|
|
38
|
+
this.map.delete(key); // In theory, this shouldn't happen frequently for feeds
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
results.push(entry.value);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return results;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
exports.ExpiringSortedSet = ExpiringSortedSet;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { PromiseOrValue } from '..';
|
|
2
|
+
import { AdapterConfig } from '../../config';
|
|
3
|
+
/**
|
|
4
|
+
* Set to hold items to subscribe to from a provider (regardless of protocol)
|
|
5
|
+
*/
|
|
6
|
+
export interface SubscriptionSet<T> {
|
|
7
|
+
/** Add a new subscription to the set */
|
|
8
|
+
add(key: string, value: T, ttl: number): PromiseOrValue<void>;
|
|
9
|
+
/** Get a specific subscription from the set */
|
|
10
|
+
get(key: string): PromiseOrValue<T | undefined>;
|
|
11
|
+
/** Get all subscriptions from the set as a list */
|
|
12
|
+
getAll(): PromiseOrValue<T[]>;
|
|
13
|
+
}
|
|
14
|
+
export declare class SubscriptionSetFactory {
|
|
15
|
+
private cacheType;
|
|
16
|
+
constructor(config: AdapterConfig);
|
|
17
|
+
buildSet<T>(): SubscriptionSet<T>;
|
|
18
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SubscriptionSetFactory = void 0;
|
|
4
|
+
const expiring_sorted_set_1 = require("./expiring-sorted-set");
|
|
5
|
+
class SubscriptionSetFactory {
|
|
6
|
+
constructor(config) {
|
|
7
|
+
this.cacheType = config.CACHE_TYPE;
|
|
8
|
+
}
|
|
9
|
+
buildSet() {
|
|
10
|
+
switch (this.cacheType) {
|
|
11
|
+
case 'local':
|
|
12
|
+
return new expiring_sorted_set_1.ExpiringSortedSet();
|
|
13
|
+
case 'redis':
|
|
14
|
+
// TODO: Implement redis set
|
|
15
|
+
return new expiring_sorted_set_1.ExpiringSortedSet();
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
exports.SubscriptionSetFactory = SubscriptionSetFactory;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { AdapterRequestData } from './request';
|
|
2
|
+
/**
|
|
3
|
+
* The test payload read in from filesystem
|
|
4
|
+
*/
|
|
5
|
+
export interface Payload {
|
|
6
|
+
requests: Array<AdapterRequestData>;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Test payload with discriminated union so we can tell when we should just do
|
|
10
|
+
* a simple liveness check rather than a sample request
|
|
11
|
+
*/
|
|
12
|
+
declare type TestPayload = (Payload & {
|
|
13
|
+
isDefault: false;
|
|
14
|
+
}) | {
|
|
15
|
+
isDefault: true;
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Load in a JSON file containing a test payload for the current adapter,
|
|
19
|
+
* used in healthchecks to make sample requests
|
|
20
|
+
*
|
|
21
|
+
* @param fileName - name of file that contains the test payload data for the smoke endpoint
|
|
22
|
+
* @returns the parsed payload with individual requests
|
|
23
|
+
*/
|
|
24
|
+
export declare function loadTestPayload(fileName?: string): TestPayload;
|
|
25
|
+
export {};
|
|
@@ -0,0 +1,83 @@
|
|
|
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.loadTestPayload = void 0;
|
|
7
|
+
const ajv_1 = __importDefault(require("ajv"));
|
|
8
|
+
const fs_1 = __importDefault(require("fs"));
|
|
9
|
+
const path_1 = __importDefault(require("path"));
|
|
10
|
+
const logger_1 = require("./logger");
|
|
11
|
+
const logger = (0, logger_1.makeLogger)('TestPayloadLoader');
|
|
12
|
+
/**
|
|
13
|
+
* Load in a JSON file containing a test payload for the current adapter,
|
|
14
|
+
* used in healthchecks to make sample requests
|
|
15
|
+
*
|
|
16
|
+
* @param fileName - name of file that contains the test payload data for the smoke endpoint
|
|
17
|
+
* @returns the parsed payload with individual requests
|
|
18
|
+
*/
|
|
19
|
+
function loadTestPayload(fileName) {
|
|
20
|
+
const ajv = new ajv_1.default();
|
|
21
|
+
const schema = {
|
|
22
|
+
type: 'object',
|
|
23
|
+
required: ['requests'],
|
|
24
|
+
properties: {
|
|
25
|
+
requests: {
|
|
26
|
+
type: 'array',
|
|
27
|
+
items: {
|
|
28
|
+
type: 'object',
|
|
29
|
+
required: [],
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
const validate = ajv.compile(schema);
|
|
35
|
+
try {
|
|
36
|
+
const payload = resolvePayload(fileName);
|
|
37
|
+
if (!validate(payload) || !payload?.requests) {
|
|
38
|
+
throw Error(JSON.stringify(validate?.errors || 'Could not validate schema for test payload'));
|
|
39
|
+
}
|
|
40
|
+
return { ...payload, isDefault: false };
|
|
41
|
+
}
|
|
42
|
+
catch (e) {
|
|
43
|
+
logger.warn(`Could not load payload: ${e.message}`);
|
|
44
|
+
logger.warn('Falling back to default empty payload');
|
|
45
|
+
return { isDefault: true };
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
exports.loadTestPayload = loadTestPayload;
|
|
49
|
+
function resolvePayload(fileName) {
|
|
50
|
+
try {
|
|
51
|
+
let payload = null;
|
|
52
|
+
// Find test payload by specified name, fallback to default names if not found
|
|
53
|
+
if (fileName && fs_1.default.existsSync(path_1.default.join(process.cwd(), fileName))) {
|
|
54
|
+
payload = require(path_1.default.join(process.cwd(), fileName));
|
|
55
|
+
}
|
|
56
|
+
else if (fileName && fs_1.default.existsSync(`./${fileName}`)) {
|
|
57
|
+
payload = require(`./${fileName}`);
|
|
58
|
+
}
|
|
59
|
+
// Search for test payload js first if no filename specified or found
|
|
60
|
+
else if (fs_1.default.existsSync(path_1.default.resolve('.', 'test-payload.js'))) {
|
|
61
|
+
payload = require(path_1.default.join(process.cwd(), 'test-payload.js'));
|
|
62
|
+
}
|
|
63
|
+
else if (fs_1.default.existsSync(`./test-payload.js`)) {
|
|
64
|
+
payload = require(`./test-payload.js`);
|
|
65
|
+
}
|
|
66
|
+
// Search for test payload json second if no filename specified or found
|
|
67
|
+
else if (fs_1.default.existsSync(path_1.default.join(process.cwd(), 'test-payload.json'))) {
|
|
68
|
+
payload = require(path_1.default.join(process.cwd(), 'test-payload.json'));
|
|
69
|
+
}
|
|
70
|
+
else if (fs_1.default.existsSync(`./test-payload.json`)) {
|
|
71
|
+
payload = require(`./test-payload.json`);
|
|
72
|
+
}
|
|
73
|
+
if (typeof payload === 'string') {
|
|
74
|
+
return JSON.parse(payload);
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
return payload;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
catch {
|
|
81
|
+
return null;
|
|
82
|
+
}
|
|
83
|
+
}
|
package/validation/error.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { HttpRequestType } from
|
|
1
|
+
import { HttpRequestType } from '../metrics/constants';
|
|
2
2
|
declare type ErrorBasic = {
|
|
3
3
|
name: string;
|
|
4
4
|
message: string;
|
|
@@ -26,7 +26,7 @@ export declare class AdapterError extends Error {
|
|
|
26
26
|
metricsLabel?: HttpRequestType;
|
|
27
27
|
name: string;
|
|
28
28
|
message: string;
|
|
29
|
-
constructor({ jobRunID, status, statusCode, name, message, cause, url, errorResponse, feedID, providerStatusCode, metricsLabel }: Partial<AdapterError>);
|
|
29
|
+
constructor({ jobRunID, status, statusCode, name, message, cause, url, errorResponse, feedID, providerStatusCode, metricsLabel, }: Partial<AdapterError>);
|
|
30
30
|
toJSONResponse(): AdapterErrorResponse;
|
|
31
31
|
}
|
|
32
32
|
export declare class AdapterInputError extends AdapterError {
|
package/validation/error.js
CHANGED
|
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.AdapterCustomError = exports.AdapterConnectionError = exports.AdapterDataProviderError = exports.AdapterTimeoutError = exports.AdapterRateLimitError = exports.AdapterInputError = exports.AdapterError = void 0;
|
|
4
4
|
const constants_1 = require("../metrics/constants");
|
|
5
5
|
class AdapterError extends Error {
|
|
6
|
-
constructor({ jobRunID = '1', status = 'errored', statusCode = 500, name = 'AdapterError', message = 'An error occurred.', cause, url, errorResponse, feedID, providerStatusCode, metricsLabel = constants_1.HttpRequestType.ADAPTER_ERROR }) {
|
|
6
|
+
constructor({ jobRunID = '1', status = 'errored', statusCode = 500, name = 'AdapterError', message = 'An error occurred.', cause, url, errorResponse, feedID, providerStatusCode, metricsLabel = constants_1.HttpRequestType.ADAPTER_ERROR, }) {
|
|
7
7
|
super(message);
|
|
8
8
|
this.jobRunID = jobRunID;
|
|
9
9
|
this.status = status;
|
package/validation/index.js
CHANGED
|
@@ -22,7 +22,9 @@ const validatorMiddleware = (adapter) => (req, reply, done) => {
|
|
|
22
22
|
});
|
|
23
23
|
}
|
|
24
24
|
// Make endpoints case insensitive
|
|
25
|
-
const endpointParam = req.body.endpoint?.toLowerCase() ||
|
|
25
|
+
const endpointParam = req.body.endpoint?.toLowerCase() ||
|
|
26
|
+
req.body.data?.endpoint?.toLowerCase() ||
|
|
27
|
+
adapter.defaultEndpoint;
|
|
26
28
|
if (!endpointParam) {
|
|
27
29
|
throw new error_1.AdapterInputError({
|
|
28
30
|
message: `Request body does not specify an endpoint, and there is no default endpoint configured for this adapter.`,
|
|
@@ -32,7 +34,7 @@ const validatorMiddleware = (adapter) => (req, reply, done) => {
|
|
|
32
34
|
const endpoint = adapter.endpointsMap[endpointParam];
|
|
33
35
|
if (!endpoint) {
|
|
34
36
|
throw new error_1.AdapterInputError({
|
|
35
|
-
message: `Adapter does not have a "${
|
|
37
|
+
message: `Adapter does not have a "${endpointParam}" endpoint.`,
|
|
36
38
|
statusCode: 404,
|
|
37
39
|
});
|
|
38
40
|
}
|
|
@@ -48,7 +50,10 @@ const validatorMiddleware = (adapter) => (req, reply, done) => {
|
|
|
48
50
|
if (adapter.config.METRICS_ENABLED && adapter.config.EXPERIMENTAL_METRICS_ENABLED) {
|
|
49
51
|
// Add metrics meta which includes feedId to the request
|
|
50
52
|
// Perform prior to overrides to maintain consistent Feed IDs across adapters
|
|
51
|
-
const metrics = (0, util_1.getMetricsMeta)(
|
|
53
|
+
const metrics = (0, util_1.getMetricsMeta)({
|
|
54
|
+
adapterEndpoint: endpoint,
|
|
55
|
+
adapterConfig: adapter.config,
|
|
56
|
+
}, validator.validated.data);
|
|
52
57
|
req.requestContext = { ...req.requestContext, meta: { metrics } };
|
|
53
58
|
}
|
|
54
59
|
// TODO: Support `includes` and `tokenOverrides` overrides as needed
|