@chainlink/external-adapter-framework 0.8.1 → 0.9.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.
- package/adapter/basic.d.ts +6 -50
- package/adapter/basic.js +2 -165
- package/adapter/basic.js.map +1 -1
- package/adapter/endpoint.d.ts +6 -6
- package/adapter/endpoint.js +0 -1
- package/adapter/endpoint.js.map +1 -1
- package/adapter/price.d.ts +8 -38
- package/adapter/price.js +1 -43
- package/adapter/price.js.map +1 -1
- package/adapter/types.d.ts +14 -37
- package/background-executor.js +4 -19
- package/background-executor.js.map +1 -1
- package/cache/index.d.ts +16 -9
- package/cache/index.js +46 -4
- package/cache/index.js.map +1 -1
- package/config/index.d.ts +11 -19
- package/config/index.js +39 -54
- package/config/index.js.map +1 -1
- package/examples/bank-frick/accounts.d.ts +25 -17
- package/examples/bank-frick/accounts.js +13 -23
- package/examples/bank-frick/accounts.js.map +1 -1
- package/examples/bank-frick/config/index.d.ts +0 -5
- package/examples/bank-frick/config/index.js +0 -5
- package/examples/bank-frick/config/index.js.map +1 -1
- package/examples/bank-frick/index.d.ts +15 -1
- package/examples/bank-frick/index.js +1 -1
- package/examples/coingecko/src/config/index.d.ts +0 -7
- package/examples/coingecko/src/config/index.js +1 -8
- package/examples/coingecko/src/config/index.js.map +1 -1
- package/examples/coingecko/src/crypto-utils.d.ts +4 -19
- package/examples/coingecko/src/crypto-utils.js +2 -1
- package/examples/coingecko/src/crypto-utils.js.map +1 -1
- package/examples/coingecko/src/endpoint/coins.d.ts +3 -20
- package/examples/coingecko/src/endpoint/coins.js +2 -2
- package/examples/coingecko/src/endpoint/coins.js.map +1 -1
- package/examples/coingecko/src/endpoint/crypto-marketcap.d.ts +2 -2
- package/examples/coingecko/src/endpoint/crypto-marketcap.js +2 -2
- package/examples/coingecko/src/endpoint/crypto-marketcap.js.map +1 -1
- package/examples/coingecko/src/endpoint/crypto-volume.d.ts +2 -2
- package/examples/coingecko/src/endpoint/crypto-volume.js +2 -2
- package/examples/coingecko/src/endpoint/crypto-volume.js.map +1 -1
- package/examples/coingecko/src/endpoint/crypto.d.ts +4 -2
- package/examples/coingecko/src/endpoint/crypto.js +2 -2
- package/examples/coingecko/src/endpoint/crypto.js.map +1 -1
- package/examples/coingecko/src/endpoint/dominance.d.ts +2 -2
- package/examples/coingecko/src/endpoint/dominance.js +2 -2
- package/examples/coingecko/src/endpoint/dominance.js.map +1 -1
- package/examples/coingecko/src/endpoint/global-marketcap.d.ts +2 -2
- package/examples/coingecko/src/endpoint/global-marketcap.js +2 -2
- package/examples/coingecko/src/endpoint/global-marketcap.js.map +1 -1
- package/examples/coingecko/src/global-utils.d.ts +3 -18
- package/examples/coingecko/src/global-utils.js.map +1 -1
- package/examples/coingecko/src/index.js +1 -3
- package/examples/coingecko/src/index.js.map +1 -1
- package/examples/coingecko-old/batch-warming.d.ts +7 -0
- package/examples/coingecko-old/batch-warming.js +54 -0
- package/examples/coingecko-old/batch-warming.js.map +1 -0
- package/examples/coingecko-old/index.d.ts +2 -0
- package/examples/coingecko-old/index.js +12 -0
- package/examples/coingecko-old/index.js.map +1 -0
- package/examples/coingecko-old/rest.d.ts +12 -0
- package/examples/coingecko-old/rest.js +55 -0
- package/examples/coingecko-old/rest.js.map +1 -0
- package/examples/cryptocompare/src/config/index.d.ts +0 -7
- package/examples/cryptocompare/src/config/index.js +1 -8
- package/examples/cryptocompare/src/config/index.js.map +1 -1
- package/examples/cryptocompare/src/endpoints/crypto.d.ts +2 -14
- package/examples/cryptocompare/src/endpoints/crypto.js.map +1 -1
- package/examples/cryptocompare/src/index.js +1 -3
- package/examples/cryptocompare/src/index.js.map +1 -1
- package/examples/genesis/config/index.d.ts +0 -5
- package/examples/genesis/config/index.js +0 -5
- package/examples/genesis/config/index.js.map +1 -1
- package/examples/genesis/index.js +1 -1
- package/examples/genesis/sseStream.d.ts +2 -13
- package/examples/genesis/sseStream.js +1 -4
- package/examples/genesis/sseStream.js.map +1 -1
- package/examples/ncfx/config/index.d.ts +12 -0
- package/examples/ncfx/config/index.js +16 -0
- package/examples/ncfx/config/index.js.map +1 -0
- package/examples/ncfx/index.d.ts +13 -0
- package/examples/ncfx/index.js +12 -0
- package/examples/ncfx/index.js.map +1 -0
- package/examples/ncfx/websocket.d.ts +47 -0
- package/examples/ncfx/websocket.js +74 -0
- package/examples/ncfx/websocket.js.map +1 -0
- package/index.d.ts +0 -1
- package/index.js +6 -4
- package/index.js.map +1 -1
- package/metrics/index.d.ts +0 -2
- package/metrics/index.js +4 -5
- package/metrics/index.js.map +1 -1
- package/metrics/util.d.ts +5 -5
- package/metrics/util.js.map +1 -1
- package/package.json +9 -7
- package/rate-limiting/background/fixed-frequency.d.ts +3 -2
- package/rate-limiting/background/fixed-frequency.js.map +1 -1
- package/rate-limiting/index.d.ts +4 -4
- package/rate-limiting/index.js +1 -1
- package/rate-limiting/index.js.map +1 -1
- package/rate-limiting/request/simple-counting.d.ts +3 -2
- package/rate-limiting/request/simple-counting.js.map +1 -1
- package/transports/batch-warming.d.ts +17 -39
- package/transports/batch-warming.js +16 -43
- package/transports/batch-warming.js.map +1 -1
- package/transports/index.d.ts +31 -65
- package/transports/index.js +59 -2
- package/transports/index.js.map +1 -1
- package/transports/metrics.d.ts +2 -2
- package/transports/metrics.js +2 -2
- package/transports/metrics.js.map +1 -1
- package/transports/rest.d.ts +30 -42
- package/transports/rest.js +25 -17
- package/transports/rest.js.map +1 -1
- package/transports/sse.d.ts +20 -40
- package/transports/sse.js +8 -10
- package/transports/sse.js.map +1 -1
- package/transports/websocket.d.ts +25 -40
- package/transports/websocket.js +16 -13
- package/transports/websocket.js.map +1 -1
- package/util/logger.d.ts +0 -2
- package/util/logger.js +7 -4
- package/util/logger.js.map +1 -1
- package/util/request.d.ts +10 -46
- package/util/subscription-set/subscription-set.d.ts +1 -1
- package/util/subscription-set/subscription-set.js +1 -1
- package/util/subscription-set/subscription-set.js.map +1 -1
- package/util/test-payload-loader.d.ts +0 -1
- package/util/test-payload-loader.js +1 -2
- package/util/test-payload-loader.js.map +1 -1
- package/validation/error.d.ts +3 -1
- package/validation/error.js +4 -2
- package/validation/error.js.map +1 -1
- package/validation/index.js +17 -26
- package/validation/index.js.map +1 -1
- package/validation/input-params.d.ts +1 -0
- package/validation/input-params.js +28 -0
- package/validation/input-params.js.map +1 -1
- package/validation/input-validator.js +2 -1
- package/validation/input-validator.js.map +1 -1
- package/validation/override-functions.d.ts +3 -0
- package/validation/override-functions.js +41 -0
- package/validation/override-functions.js.map +1 -0
- package/README.md +0 -67
- package/transports/routing.d.ts +0 -21
- package/transports/routing.js +0 -50
- package/transports/routing.js.map +0 -1
package/transports/index.d.ts
CHANGED
|
@@ -1,33 +1,12 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { FastifyReply } from 'fastify';
|
|
2
|
+
import { AdapterContext, AdapterDependencies, Adapter } from '../adapter';
|
|
3
|
+
import { CacheEntry } from '../cache';
|
|
3
4
|
import { AdapterConfig, SettingsMap } from '../config';
|
|
4
|
-
import { AdapterRequest, AdapterResponse, ProviderResult
|
|
5
|
+
import { AdapterRequest, AdapterResponse, ProviderResult } from '../util/request';
|
|
5
6
|
export * from './batch-warming';
|
|
6
7
|
export * from './rest';
|
|
7
|
-
export * from './sse';
|
|
8
8
|
export * from './websocket';
|
|
9
|
-
|
|
10
|
-
* Helper struct type that will be used to pass types to the generic parameters of a Transport.
|
|
11
|
-
* Within the transport, given a generic param T extends TransportGenerics, these will be used like so:
|
|
12
|
-
*
|
|
13
|
-
* const request: AdapterRequest\<T['Request']\> = ...
|
|
14
|
-
*
|
|
15
|
-
* (ignore the slash characters, they're just for proper doc syntax \<)
|
|
16
|
-
*/
|
|
17
|
-
export declare type TransportGenerics = {
|
|
18
|
-
/**
|
|
19
|
-
* Type details about incoming Adapter requests to this Transport
|
|
20
|
-
*/
|
|
21
|
-
Request: RequestGenerics;
|
|
22
|
-
/**
|
|
23
|
-
* Type details about outbound responses from this Transport
|
|
24
|
-
*/
|
|
25
|
-
Response: ResponseGenerics;
|
|
26
|
-
/**
|
|
27
|
-
* Type for any custom settings used for this Transport
|
|
28
|
-
*/
|
|
29
|
-
CustomSettings: SettingsMap;
|
|
30
|
-
};
|
|
9
|
+
export * from './sse';
|
|
31
10
|
/**
|
|
32
11
|
* Generic interface for a Transport.
|
|
33
12
|
* A Transport defines the way in which an AdapterEndpoint will process incoming requests to
|
|
@@ -36,10 +15,10 @@ export declare type TransportGenerics = {
|
|
|
36
15
|
* This separation gives us the ability of splitting these concerns, and optionally parallelizing
|
|
37
16
|
* the reading and writing of data to a centralized Cache.
|
|
38
17
|
*
|
|
39
|
-
* @typeParam
|
|
18
|
+
* @typeParam Params - the structure of the AdapterRequest's body
|
|
19
|
+
* @typeParam Result - the structure of the AdapterResponse's body
|
|
40
20
|
*/
|
|
41
|
-
export interface Transport<
|
|
42
|
-
cache?: Cache<AdapterResponse<T['Response']>>;
|
|
21
|
+
export interface Transport<Params, Result, CustomSettings extends SettingsMap> {
|
|
43
22
|
/**
|
|
44
23
|
* Initializes the transport in the Adapter context.
|
|
45
24
|
*
|
|
@@ -47,56 +26,33 @@ export interface Transport<T extends TransportGenerics> {
|
|
|
47
26
|
* @param config - Adapter config containing env vars
|
|
48
27
|
* @returns an empty Promise
|
|
49
28
|
*/
|
|
50
|
-
initialize: (dependencies: AdapterDependencies, config: AdapterConfig
|
|
29
|
+
initialize: (dependencies: AdapterDependencies, config: AdapterConfig, endpointName: string) => Promise<void>;
|
|
51
30
|
/**
|
|
52
|
-
*
|
|
53
|
-
* This
|
|
31
|
+
* Checks if the Transport has already set up the incoming request.
|
|
32
|
+
* This will likely use the cacheKey generated in the provided request.
|
|
54
33
|
*
|
|
55
34
|
* @param req - the incoming AdapterRequest
|
|
56
|
-
* @
|
|
57
|
-
* @returns an empty Promise
|
|
35
|
+
* @returns a Promise that returns true if setup is in place
|
|
58
36
|
*/
|
|
59
|
-
|
|
37
|
+
hasBeenSetUp: (req: AdapterRequest<Params>) => Promise<boolean>;
|
|
60
38
|
/**
|
|
61
|
-
*
|
|
62
|
-
*
|
|
63
|
-
*
|
|
64
|
-
* to this method with more than one EA instance. It's heavily encouraged to instead rework adapter logic
|
|
65
|
-
* to perform as much of the work as possible (or all of it) in the backgroundExecute method.
|
|
39
|
+
* Sets up the incoming request within the Transport.
|
|
40
|
+
* In other words, it means that the Adapter has recognized this request, and will begin
|
|
41
|
+
* fetching the necessary data from a Data Provider.
|
|
66
42
|
*
|
|
67
43
|
* @param req - the incoming AdapterRequest
|
|
68
44
|
* @param config - common configuration for the Adapter as a whole
|
|
69
45
|
* @returns a Promise that _optionally_ returns an AdapterResponse, if the Transport has the capability of
|
|
70
46
|
* immediately fetching data and returning it without the background process.
|
|
71
47
|
*/
|
|
72
|
-
|
|
73
|
-
Data: T['Response']['Data'];
|
|
74
|
-
Result: T['Response']['Result'];
|
|
75
|
-
}> | void>;
|
|
48
|
+
setup: (req: AdapterRequest<Params>, config: AdapterConfig<CustomSettings>) => Promise<AdapterResponse<Result> | void>;
|
|
76
49
|
/**
|
|
77
|
-
*
|
|
78
|
-
*
|
|
50
|
+
* If the Transport relies on an async mechanism, implementing this function will make it
|
|
51
|
+
* so the background task executor calls it on Adapter startup.
|
|
79
52
|
*
|
|
80
53
|
* @param context - background context for the execution (e.g. endpoint name)
|
|
81
|
-
* @returns an empty Promise
|
|
82
54
|
*/
|
|
83
|
-
backgroundExecute?: (context: AdapterContext<
|
|
84
|
-
}
|
|
85
|
-
/**
|
|
86
|
-
* Generic interface for a Transport that contains or manages other transports.
|
|
87
|
-
* Some transports, such as the RoutingTransport, do not directly fetch data from a Data Provider, but
|
|
88
|
-
* instead coordinate the execution of other transports for fetching and transforming data.
|
|
89
|
-
* These transports have alternative initialization procedures, which typically involve
|
|
90
|
-
* initializing each downstream transport.
|
|
91
|
-
*
|
|
92
|
-
* @typeParam Params - the structure of the AdapterRequest's body
|
|
93
|
-
* @typeParam Result - the structure of the AdapterResponse's body
|
|
94
|
-
* @typeParam CustomSettings - custom settings for the Adapter, or the default SettingsMap
|
|
95
|
-
*/
|
|
96
|
-
export interface MetaTransport<T extends TransportGenerics> extends Transport<T> {
|
|
97
|
-
transports: {
|
|
98
|
-
[key: string]: Transport<T>;
|
|
99
|
-
};
|
|
55
|
+
backgroundExecute?: (context: AdapterContext<CustomSettings>) => Promise<number>;
|
|
100
56
|
}
|
|
101
57
|
/**
|
|
102
58
|
* Helper method to build cache entries to set after getting a bunch of responses from a DP.
|
|
@@ -105,4 +61,14 @@ export interface MetaTransport<T extends TransportGenerics> extends Transport<T>
|
|
|
105
61
|
* @param context - context for the Adapter
|
|
106
62
|
* @returns a list of CacheEntries of AdapterResponses
|
|
107
63
|
*/
|
|
108
|
-
export declare const buildCacheEntriesFromResults: <
|
|
64
|
+
export declare const buildCacheEntriesFromResults: <Params, CustomSettings extends SettingsMap>(results: ProviderResult<Params>[], context: AdapterContext<CustomSettings>) => CacheEntry<AdapterResponse<{
|
|
65
|
+
result: unknown;
|
|
66
|
+
}>>[];
|
|
67
|
+
/**
|
|
68
|
+
* Takes an Adapter, its configuration, and its dependencies, and it creates an express middleware
|
|
69
|
+
* that will pass along the AdapterRequest to the appropriate Transport (acc. to the endpoint in the req.)
|
|
70
|
+
*
|
|
71
|
+
* @param adapter - main adapter object, already initialized
|
|
72
|
+
* @returns the transport handler middleware function
|
|
73
|
+
*/
|
|
74
|
+
export declare const buildTransportHandler: (adapter: Adapter) => (req: AdapterRequest, reply: FastifyReply) => Promise<never>;
|
package/transports/index.js
CHANGED
|
@@ -10,16 +10,31 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (functi
|
|
|
10
10
|
if (k2 === undefined) k2 = k;
|
|
11
11
|
o[k2] = m[k];
|
|
12
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
|
+
};
|
|
13
25
|
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
26
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
27
|
};
|
|
16
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.buildCacheEntriesFromResults = void 0;
|
|
29
|
+
exports.buildTransportHandler = exports.buildCacheEntriesFromResults = void 0;
|
|
18
30
|
const cache_1 = require("../cache");
|
|
31
|
+
const util_1 = require("../util");
|
|
32
|
+
const transportMetrics = __importStar(require("./metrics"));
|
|
19
33
|
__exportStar(require("./batch-warming"), exports);
|
|
20
34
|
__exportStar(require("./rest"), exports);
|
|
21
|
-
__exportStar(require("./sse"), exports);
|
|
22
35
|
__exportStar(require("./websocket"), exports);
|
|
36
|
+
__exportStar(require("./sse"), exports);
|
|
37
|
+
const logger = (0, util_1.makeLogger)('Transport');
|
|
23
38
|
/**
|
|
24
39
|
* Helper method to build cache entries to set after getting a bunch of responses from a DP.
|
|
25
40
|
*
|
|
@@ -53,4 +68,46 @@ const buildCacheEntriesFromResults = (results, context) => results.map((r) => {
|
|
|
53
68
|
return cacheEntry;
|
|
54
69
|
});
|
|
55
70
|
exports.buildCacheEntriesFromResults = buildCacheEntriesFromResults;
|
|
71
|
+
/**
|
|
72
|
+
* Takes an Adapter, its configuration, and its dependencies, and it creates an express middleware
|
|
73
|
+
* that will pass along the AdapterRequest to the appropriate Transport (acc. to the endpoint in the req.)
|
|
74
|
+
*
|
|
75
|
+
* @param adapter - main adapter object, already initialized
|
|
76
|
+
* @returns the transport handler middleware function
|
|
77
|
+
*/
|
|
78
|
+
const buildTransportHandler = (adapter) => async (req, reply) => {
|
|
79
|
+
// Get transport, must be here because it's already checked in the validator
|
|
80
|
+
const transport = adapter.endpointsMap[req.requestContext.endpointName].transport;
|
|
81
|
+
// Set up transport if it hasn't been done already
|
|
82
|
+
if (!(await transport.hasBeenSetUp(req))) {
|
|
83
|
+
logger.debug('Transport not set up yet, doing so...');
|
|
84
|
+
const immediateResponse = await transport.setup(req, adapter.config);
|
|
85
|
+
if (immediateResponse) {
|
|
86
|
+
logger.debug('Got immediate response from transport, sending as response');
|
|
87
|
+
return reply.send(immediateResponse);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
// Observe the idle time taken for polling response
|
|
91
|
+
const metricsTimer = transportMetrics.transportPollingDurationSeconds
|
|
92
|
+
.labels({ endpoint: req.requestContext.endpointName })
|
|
93
|
+
.startTimer();
|
|
94
|
+
logger.debug('Transport is set up, polling cache for response...');
|
|
95
|
+
const response = await (0, cache_1.pollResponseFromCache)(adapter.dependencies.cache, req.requestContext.cacheKey, {
|
|
96
|
+
maxRetries: adapter.config.CACHE_POLLING_MAX_RETRIES,
|
|
97
|
+
sleep: adapter.config.CACHE_POLLING_SLEEP_MS,
|
|
98
|
+
});
|
|
99
|
+
metricsTimer({ succeeded: String(!!response) });
|
|
100
|
+
if (response) {
|
|
101
|
+
logger.debug('Got a response from the cache, sending that back');
|
|
102
|
+
return reply.send(response);
|
|
103
|
+
}
|
|
104
|
+
// Record polling mechanism failure to return response
|
|
105
|
+
transportMetrics.transportPollingFailureCount
|
|
106
|
+
.labels({ endpoint: req.requestContext.endpointName })
|
|
107
|
+
.inc();
|
|
108
|
+
logger.debug('Ran out of polling attempts, returning timeout');
|
|
109
|
+
reply.statusCode = 504;
|
|
110
|
+
return reply.send();
|
|
111
|
+
};
|
|
112
|
+
exports.buildTransportHandler = buildTransportHandler;
|
|
56
113
|
//# sourceMappingURL=index.js.map
|
package/transports/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/transports/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/transports/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEA,oCAMiB;AAEjB,kCAAoC;AAEpC,4DAA6C;AAE7C,kDAA+B;AAC/B,yCAAsB;AACtB,8CAA2B;AAC3B,wCAAqB;AAErB,MAAM,MAAM,GAAG,IAAA,iBAAU,EAAC,WAAW,CAAC,CAAA;AA4DtC;;;;;;GAMG;AACI,MAAM,4BAA4B,GAAG,CAC1C,OAAiC,EACjC,OAAuC,EACa,EAAE,CACtD,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE;IAChB,MAAM,UAAU,GAAG;QACjB,GAAG,EAAE,IAAA,yBAAiB,EAAC,OAAO,EAAE,CAAC,CAAC,MAAM,CAAC;QACzC,KAAK,EAAE;YACL,MAAM,EAAE,CAAC,CAAC,KAAK;YACf,UAAU,EAAE,GAAG;YACf,IAAI,EAAE;gBACJ,MAAM,EAAE,CAAC,CAAC,KAAK;aAChB;SACF;KACF,CAAA;IACD,IACE,OAAO,CAAC,aAAa,CAAC,eAAe;QACrC,OAAO,CAAC,aAAa,CAAC,4BAA4B,EAClD;QACA,MAAM,OAAO,GAAG;YACd,MAAM,EAAE,IAAI,CAAC,GAAG,EAAE,GAAG,OAAO,CAAC,aAAa,CAAC,aAAa;YACxD,IAAI,EAAE;gBACJ,OAAO,EAAE;oBACP,MAAM,EAAE,IAAA,uBAAe,EAAC,OAAyB,EAAE,CAAC,CAAC,MAAM,CAAC;iBAC7D;aACF;SACF,CAAA;QACD,UAAU,CAAC,KAAK,GAAG,EAAE,GAAG,UAAU,CAAC,KAAK,EAAE,GAAG,OAAO,EAAE,CAAA;KACvD;IACD,OAAO,UAAU,CAAA;AACnB,C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,IAAI,QAAQ,EAAE;QACZ,MAAM,CAAC,KAAK,CAAC,kDAAkD,CAAC,CAAA;QAChE,OAAO,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAA;KAC5B;IAED,sDAAsD;IACtD,gBAAgB,CAAC,4BAA4B;SAC1C,MAAM,CAAC,EAAE,QAAQ,EAAE,GAAG,CAAC,cAAc,CAAC,YAAY,EAAE,CAAC;SACrD,GAAG,EAAE,CAAA;IAER,MAAM,CAAC,KAAK,CAAC,gDAAgD,CAAC,CAAA;IAC9D,KAAK,CAAC,UAAU,GAAG,GAAG,CAAA;IAEtB,OAAO,KAAK,CAAC,IAAI,EAAE,CAAA;AACrB,CAAC,CAAA;AA7CU,QAAA,qBAAqB,yBA6C/B"}
|
package/transports/metrics.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as client from 'prom-client';
|
|
2
|
-
import { TransportGenerics } from '.';
|
|
3
2
|
import { AdapterContext } from '../adapter';
|
|
3
|
+
import { SettingsMap } from '../config';
|
|
4
4
|
export declare const dataProviderMetricsLabel: (providerStatusCode?: number, method?: string) => {
|
|
5
5
|
provider_status_code: number | undefined;
|
|
6
6
|
method: string;
|
|
@@ -14,7 +14,7 @@ export declare const messageSubsLabels: (feed_id: string, cache_key: string) =>
|
|
|
14
14
|
feed_id: string;
|
|
15
15
|
subscription_key: string;
|
|
16
16
|
};
|
|
17
|
-
export declare const recordWsMessageMetrics: <
|
|
17
|
+
export declare const recordWsMessageMetrics: <AdapterParams, CustomSettings extends SettingsMap>(context: AdapterContext<CustomSettings>, subscribes: AdapterParams[], unsubscrices: AdapterParams[]) => void;
|
|
18
18
|
export declare const wsConnectionActive: client.Gauge<"url">;
|
|
19
19
|
export declare const wsConnectionErrors: client.Counter<"message" | "url">;
|
|
20
20
|
export declare const wsSubscriptionActive: client.Gauge<"feed_id" | "connection_url" | "subscription_key">;
|
package/transports/metrics.js
CHANGED
|
@@ -57,7 +57,7 @@ exports.messageSubsLabels = messageSubsLabels;
|
|
|
57
57
|
// Record WS message and subscription metrics
|
|
58
58
|
// Recalculate cacheKey and feedId for metrics
|
|
59
59
|
// since avoiding storing extra info in expiring sorted set
|
|
60
|
-
const recordWsMessageMetrics = (context, subscribes,
|
|
60
|
+
const recordWsMessageMetrics = (context, subscribes, unsubscrices) => {
|
|
61
61
|
subscribes.forEach((param) => {
|
|
62
62
|
const feedId = (0, cache_1.calculateFeedId)(context, param);
|
|
63
63
|
const cacheKey = (0, cache_1.calculateCacheKey)(context, param);
|
|
@@ -68,7 +68,7 @@ const recordWsMessageMetrics = (context, subscribes, unsubscribes) => {
|
|
|
68
68
|
// Record number of active ws subscriptions
|
|
69
69
|
exports.wsSubscriptionActive.labels((0, exports.messageSubsLabels)(feedId, cacheKey)).inc();
|
|
70
70
|
});
|
|
71
|
-
|
|
71
|
+
unsubscrices.forEach((param) => {
|
|
72
72
|
const feedId = (0, cache_1.calculateFeedId)(context, param);
|
|
73
73
|
const cacheKey = (0, cache_1.calculateCacheKey)(context, param);
|
|
74
74
|
// Record total number of ws messages sent
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"metrics.js","sourceRoot":"","sources":["../../../src/transports/metrics.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,oDAAqC;
|
|
1
|
+
{"version":3,"file":"metrics.js","sourceRoot":"","sources":["../../../src/transports/metrics.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,oDAAqC;AAErC,oCAA6D;AAE7D,oDAA6D;AAE7D,wBAAwB;AACjB,MAAM,wBAAwB,GAAG,CAAC,kBAA2B,EAAE,MAAM,GAAG,KAAK,EAAE,EAAE,CAAC,CAAC;IACxF,oBAAoB,EAAE,kBAAkB;IACxC,MAAM,EAAE,MAAM,CAAC,WAAW,EAAE;CAC7B,CAAC,CAAA;AAHW,QAAA,wBAAwB,4BAGnC;AAEW,QAAA,oBAAoB,GAAG,IAAI,MAAM,CAA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AAuB;IAC7B,IAAI,EAAE,6CAA6C;IACnD,UAAU,EAAE,CAAC,gBAAgB,EAAE,SAAS,EAAE,kBAAkB,CAAU;CACvE,CAAC,CAAA;AAEW,QAAA,cAAc,GAAG,IAAI,MAAM,CAAC,OAAO,CAAC;IAC/C,IAAI,EAAE,kBAAkB;IACxB,IAAI,EAAE,0CAA0C;IAChD,UAAU,EAAE,CAAC,SAAS,EAAE,kBAAkB,CAAU;CACrD,CAAC,CAAA;AAEF,sBAAsB;AACT,QAAA,6BAA6B,GAAG,IAAI,MAAM,CAAC,KAAK,CAAC;IAC5D,IAAI,EAAE,mCAAmC;IACzC,IAAI,EAAE,0DAA0D;IAChE,UAAU,EAAE,CAAC,UAAU,EAAE,gBAAgB,CAAU;CACpD,CAAC,CAAA;AAEW,QAAA,4BAA4B,GAAG,IAAI,MAAM,CAAC,OAAO,CAAC;IAC7D,IAAI,EAAE,iCAAiC;IACvC,IAAI,EAAE,+FAA+F;IACrG,UAAU,EAAE,CAAC,UAAU,CAAU;CAClC,CAAC,CAAA;AAEW,QAAA,+BAA+B,GAAG,IAAI,MAAM,CAAC,KAAK,CAAC;IAC9D,IAAI,EAAE,oCAAoC;IAC1C,IAAI,EAAE,iFAAiF;IACvF,UAAU,EAAE,CAAC,UAAU,EAAE,WAAW,CAAU;CAC/C,CAAC,CAAA"}
|
package/transports/rest.d.ts
CHANGED
|
@@ -1,43 +1,11 @@
|
|
|
1
1
|
import { AdapterRequest, AdapterResponse } from '../util/request';
|
|
2
|
-
import { Transport
|
|
2
|
+
import { Transport } from './';
|
|
3
3
|
import { Cache } from '../cache';
|
|
4
4
|
import { AxiosRequestConfig, AxiosResponse } from 'axios';
|
|
5
|
-
import { AdapterConfig } from '../config';
|
|
5
|
+
import { AdapterConfig, SettingsMap } from '../config';
|
|
6
6
|
import { RequestRateLimiter } from '../rate-limiting';
|
|
7
7
|
import { AdapterDependencies } from '../adapter';
|
|
8
8
|
export { AxiosRequestConfig as HttpRequestConfig, AxiosResponse as HttpResponse };
|
|
9
|
-
/**
|
|
10
|
-
* Helper struct type that will be used to pass types to the generic parameters of a Transport.
|
|
11
|
-
* Extends the common TransportGenerics, adding Provider specific types for this Batch endpoint.
|
|
12
|
-
*/
|
|
13
|
-
declare type RestTransportGenerics = TransportGenerics & {
|
|
14
|
-
/**
|
|
15
|
-
* Type details for any provider specific interfaces.
|
|
16
|
-
*/
|
|
17
|
-
Provider: {
|
|
18
|
-
/**
|
|
19
|
-
* Structure of the body of the request that will be sent to the data provider.
|
|
20
|
-
*/
|
|
21
|
-
RequestBody: unknown;
|
|
22
|
-
/**
|
|
23
|
-
* Structure for the body of the response coming from the data provider.
|
|
24
|
-
*/
|
|
25
|
-
ResponseBody: unknown;
|
|
26
|
-
};
|
|
27
|
-
};
|
|
28
|
-
/**
|
|
29
|
-
* Config object that is provided to the RestTransport constructor.
|
|
30
|
-
*/
|
|
31
|
-
export interface RestTransportConfig<T extends RestTransportGenerics> {
|
|
32
|
-
prepareRequest: (req: AdapterRequest<T['Request']>, config: AdapterConfig<T['CustomSettings']>) => AxiosRequestConfig<T['Provider']['RequestBody']> | Promise<AxiosRequestConfig<T['Provider']['RequestBody']>>;
|
|
33
|
-
parseResponse: (req: AdapterRequest<T['Request']>, res: AxiosResponse<T['Provider']['ResponseBody']>, config: AdapterConfig<T['CustomSettings']>) => AdapterResponse<T['Response']> | Promise<AdapterResponse<T['Response']>>;
|
|
34
|
-
options: {
|
|
35
|
-
requestCoalescing: {
|
|
36
|
-
enabled: boolean;
|
|
37
|
-
entropyMax?: number;
|
|
38
|
-
};
|
|
39
|
-
};
|
|
40
|
-
}
|
|
41
9
|
/**
|
|
42
10
|
* Transport implementation that takes incoming requests, transforms them into a DataProvider request,
|
|
43
11
|
* and executes that request returning the response immediately from the `setup` function.
|
|
@@ -45,19 +13,39 @@ export interface RestTransportConfig<T extends RestTransportGenerics> {
|
|
|
45
13
|
* new adapter requests for the same feed will not trigger a new one, but return an empty promise from
|
|
46
14
|
* the setup instead so the normal cache polling mechanism is used.
|
|
47
15
|
*
|
|
48
|
-
* @typeParam
|
|
16
|
+
* @typeParam AdapterParams - interface for the adapter request body
|
|
17
|
+
* @typeParam ProviderRequestBody - interface for the body of the request to the Data Provider
|
|
18
|
+
* @typeParam ProviderResponseBody - interface for the body of the Data Provider's response
|
|
49
19
|
*/
|
|
50
|
-
export declare class RestTransport<
|
|
51
|
-
protected config:
|
|
20
|
+
export declare class RestTransport<AdapterParams, ProviderRequestBody, ProviderResponseBody, CustomSettings extends SettingsMap> implements Transport<AdapterParams, ProviderResponseBody, CustomSettings> {
|
|
21
|
+
protected config: {
|
|
22
|
+
prepareRequest: (req: AdapterRequest<AdapterParams>, config: AdapterConfig<CustomSettings>) => AxiosRequestConfig<ProviderRequestBody> | Promise<AxiosRequestConfig<ProviderRequestBody>>;
|
|
23
|
+
parseResponse: (req: AdapterRequest<AdapterParams>, res: AxiosResponse<ProviderResponseBody>, config: AdapterConfig<CustomSettings>) => AdapterResponse<ProviderResponseBody> | Promise<AdapterResponse<ProviderResponseBody>>;
|
|
24
|
+
options: {
|
|
25
|
+
requestCoalescing: {
|
|
26
|
+
enabled: boolean;
|
|
27
|
+
entropyMax?: number;
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
};
|
|
52
31
|
inFlightPrefix: string;
|
|
53
|
-
cache: Cache
|
|
54
|
-
inFlightCache: Cache<boolean>;
|
|
32
|
+
cache: Cache;
|
|
55
33
|
rateLimiter: RequestRateLimiter;
|
|
56
|
-
constructor(config:
|
|
57
|
-
|
|
34
|
+
constructor(config: {
|
|
35
|
+
prepareRequest: (req: AdapterRequest<AdapterParams>, config: AdapterConfig<CustomSettings>) => AxiosRequestConfig<ProviderRequestBody> | Promise<AxiosRequestConfig<ProviderRequestBody>>;
|
|
36
|
+
parseResponse: (req: AdapterRequest<AdapterParams>, res: AxiosResponse<ProviderResponseBody>, config: AdapterConfig<CustomSettings>) => AdapterResponse<ProviderResponseBody> | Promise<AdapterResponse<ProviderResponseBody>>;
|
|
37
|
+
options: {
|
|
38
|
+
requestCoalescing: {
|
|
39
|
+
enabled: boolean;
|
|
40
|
+
entropyMax?: number;
|
|
41
|
+
};
|
|
42
|
+
};
|
|
43
|
+
});
|
|
44
|
+
initialize(dependencies: AdapterDependencies, config: AdapterConfig): Promise<void>;
|
|
45
|
+
hasBeenSetUp(req: AdapterRequest<AdapterParams>): Promise<boolean>;
|
|
58
46
|
protected waitUntilUnderRateLimit(options: {
|
|
59
47
|
maxRetries: number;
|
|
60
48
|
msBetweenRetries: number;
|
|
61
49
|
}, retry?: number): Promise<void>;
|
|
62
|
-
|
|
50
|
+
setup(req: AdapterRequest<AdapterParams>, config: AdapterConfig<CustomSettings>): Promise<AdapterResponse<ProviderResponseBody> | undefined>;
|
|
63
51
|
}
|
package/transports/rest.js
CHANGED
|
@@ -37,7 +37,9 @@ const logger = (0, util_1.makeLogger)('RestTransport');
|
|
|
37
37
|
* new adapter requests for the same feed will not trigger a new one, but return an empty promise from
|
|
38
38
|
* the setup instead so the normal cache polling mechanism is used.
|
|
39
39
|
*
|
|
40
|
-
* @typeParam
|
|
40
|
+
* @typeParam AdapterParams - interface for the adapter request body
|
|
41
|
+
* @typeParam ProviderRequestBody - interface for the body of the request to the Data Provider
|
|
42
|
+
* @typeParam ProviderResponseBody - interface for the body of the Data Provider's response
|
|
41
43
|
*/
|
|
42
44
|
class RestTransport {
|
|
43
45
|
constructor(config) {
|
|
@@ -46,12 +48,30 @@ class RestTransport {
|
|
|
46
48
|
async initialize(dependencies, config) {
|
|
47
49
|
this.inFlightPrefix = `${IN_FLIGHT_PREFIX}-`;
|
|
48
50
|
this.cache = dependencies.cache;
|
|
49
|
-
this.inFlightCache = dependencies.cache;
|
|
50
51
|
this.rateLimiter = dependencies.requestRateLimiter;
|
|
51
52
|
// Allow enabling/disabling request coalescing through env var
|
|
52
53
|
this.config.options.requestCoalescing.enabled = config.REQUEST_COALESCING_ENABLED;
|
|
53
54
|
this.config.options.requestCoalescing.entropyMax = config.REQUEST_COALESCING_ENTROPY_MAX;
|
|
54
55
|
}
|
|
56
|
+
async hasBeenSetUp(req) {
|
|
57
|
+
if (!this.config.options.requestCoalescing.enabled) {
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
// Add some entropy here because of possible scenario where the key won't be set before multiple
|
|
61
|
+
// other instances in a burst request try to access the coalescing key.
|
|
62
|
+
const randomMs = Math.random() * (this.config.options.requestCoalescing.entropyMax || 0);
|
|
63
|
+
await (0, util_1.sleep)(randomMs);
|
|
64
|
+
// Check if request is in flight
|
|
65
|
+
const inFlight = await this.cache.get(this.inFlightPrefix + req.requestContext.cacheKey);
|
|
66
|
+
if (inFlight) {
|
|
67
|
+
logger.debug('Request is in flight, transport has been set up');
|
|
68
|
+
return true;
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
logger.debug('Request not in flight, transport not set up');
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
55
75
|
async waitUntilUnderRateLimit(options, retry = 0) {
|
|
56
76
|
if (this.rateLimiter.isUnderLimits()) {
|
|
57
77
|
logger.trace('Incoming request would not be under limits, moving on');
|
|
@@ -67,24 +87,12 @@ class RestTransport {
|
|
|
67
87
|
await (0, util_1.sleep)(options.msBetweenRetries);
|
|
68
88
|
await this.waitUntilUnderRateLimit(options, retry + 1);
|
|
69
89
|
}
|
|
70
|
-
async
|
|
71
|
-
|
|
72
|
-
// other instances in a burst request try to access the coalescing key.
|
|
73
|
-
const randomMs = Math.random() * (this.config.options.requestCoalescing.entropyMax || 0);
|
|
74
|
-
await (0, util_1.sleep)(randomMs);
|
|
75
|
-
// Check if request is in flight if coalescing is enabled
|
|
76
|
-
const inFlight = this.config.options.requestCoalescing.enabled &&
|
|
77
|
-
(await this.cache.get(this.inFlightPrefix + req.requestContext.cacheKey));
|
|
78
|
-
if (inFlight) {
|
|
79
|
-
logger.debug('Request is in flight, transport has been set up');
|
|
80
|
-
return;
|
|
81
|
-
}
|
|
82
|
-
else if (this.config.options.requestCoalescing.enabled) {
|
|
83
|
-
// If it wasn't in flight and coalescing is disabled, register it as in flight
|
|
90
|
+
async setup(req, config) {
|
|
91
|
+
if (this.config.options.requestCoalescing.enabled) {
|
|
84
92
|
const ttl = config.REST_TRANSPORT_MAX_RATE_LIMIT_RETRIES *
|
|
85
93
|
config.REST_TRANSPORT_MS_BETWEEN_RATE_LIMIT_RETRIES;
|
|
86
94
|
logger.debug('Setting up rest transport, setting request in flight in cache');
|
|
87
|
-
await this.
|
|
95
|
+
await this.cache.set(this.inFlightPrefix + req.requestContext.cacheKey, true, ttl + 100); // Can't use Infinity for things like Redis
|
|
88
96
|
}
|
|
89
97
|
const request = await this.config.prepareRequest(req, config);
|
|
90
98
|
logger.trace('Check if we are under rate limits to perform request');
|
package/transports/rest.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rest.js","sourceRoot":"","sources":["../../../src/transports/rest.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAIA,kCAA2C;AAG3C,+CAAkD;AAClD,iCAAqC;AACrC,2EAA4D;AAK5D,MAAM,gBAAgB,GAAG,UAAU,CAAA;AAEnC,MAAM,MAAM,GAAG,IAAA,iBAAU,EAAC,eAAe,CAAC,CAAA;
|
|
1
|
+
{"version":3,"file":"rest.js","sourceRoot":"","sources":["../../../src/transports/rest.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAIA,kCAA2C;AAG3C,+CAAkD;AAClD,iCAAqC;AACrC,2EAA4D;AAK5D,MAAM,gBAAgB,GAAG,UAAU,CAAA;AAEnC,MAAM,MAAM,GAAG,IAAA,iBAAU,EAAC,eAAe,CAAC,CAAA;AAE1C;;;;;;;;;;GAUG;AACH,MAAa,aAAa;IAWxB,YACY,MAkBT;QAlBS,WAAM,GAAN,MAAM,CAkBf;IACA,CAAC;IAEJ,KAAK,CAAC,UAAU,CAAC,YAAiC,EAAE,MAAqB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GAAG,GAAG,CAAC,cAAc,CAAC,IAAI;YAC1B,OAAO,EAAE,EAAE,GAAG,GAAG,CAAC,cAAc,CAAC,IAAI,EAAE,OAAO,EAAE,QAAQ,EAAE,KAAK,EAAE;SAClE,CAAA;QAED,IAAI,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,iBAAiB,CAAC,OAAO,EAAE;YACjD,MAAM,CAAC,KAAK,CAAC,+DAA+D,CAAC,CAAA;YAC7E,MAAM,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC,cAAc,CAAC,CAAA;SAC7C;QAED,OAAO,cAAc,CAAA;IACvB,CAAC;CACF;AA1JD,sCA0JC"}
|
package/transports/sse.d.ts
CHANGED
|
@@ -1,61 +1,41 @@
|
|
|
1
|
-
import { AxiosRequestConfig } from 'axios';
|
|
2
|
-
import EventSource from 'eventsource';
|
|
3
|
-
import { AdapterContext, AdapterDependencies } from '../adapter';
|
|
4
1
|
import { Cache } from '../cache';
|
|
5
|
-
import
|
|
2
|
+
import EventSource from 'eventsource';
|
|
3
|
+
import { SettingsMap, AdapterConfig } from '../config';
|
|
4
|
+
import { AxiosRequestConfig } from 'axios';
|
|
6
5
|
import { SubscriptionSet } from '../util';
|
|
7
|
-
import { AdapterRequest,
|
|
8
|
-
import { Transport
|
|
6
|
+
import { AdapterRequest, ProviderResult } from '../util/request';
|
|
7
|
+
import { Transport } from './';
|
|
8
|
+
import { AdapterContext, AdapterDependencies } from '../adapter';
|
|
9
9
|
export interface SSEConfig {
|
|
10
10
|
url: string;
|
|
11
11
|
eventSourceInitDict?: EventSource.EventSourceInitDict;
|
|
12
12
|
}
|
|
13
|
-
|
|
14
|
-
* Helper struct type that will be used to pass types to the generic parameters of a Transport.
|
|
15
|
-
* Extends the common TransportGenerics, adding Provider specific types for this Batch endpoint.
|
|
16
|
-
*/
|
|
17
|
-
declare type SSETransportGenerics = TransportGenerics & {
|
|
18
|
-
/**
|
|
19
|
-
* Type details for any provider specific interfaces.
|
|
20
|
-
*/
|
|
21
|
-
Provider: {
|
|
22
|
-
/**
|
|
23
|
-
* Structure of the body of the request that will be sent to the data provider.
|
|
24
|
-
*/
|
|
25
|
-
RequestBody: unknown;
|
|
26
|
-
};
|
|
27
|
-
};
|
|
28
|
-
/**
|
|
29
|
-
* Transport implementation that establishes a long lived connection to a server using the SSE protocol and subcribes to updates.
|
|
30
|
-
*
|
|
31
|
-
* @typeParam T - Helper struct type that will be used to pass types to the generic parameters (check [[SSETransportGenerics]])
|
|
32
|
-
*/
|
|
33
|
-
export declare class SSETransport<T extends SSETransportGenerics> implements Transport<T> {
|
|
13
|
+
export declare class SSETransport<AdapterParams, ProviderRequestBody, ProviderResponseBody, CustomSettings extends SettingsMap> implements Transport<AdapterParams, ProviderResponseBody, CustomSettings> {
|
|
34
14
|
private config;
|
|
35
15
|
EventSource: typeof EventSource;
|
|
36
|
-
cache: Cache
|
|
16
|
+
cache: Cache;
|
|
37
17
|
eventListeners: {
|
|
38
18
|
type: string;
|
|
39
|
-
parseResponse: (evt: MessageEvent) => ProviderResult<
|
|
19
|
+
parseResponse: (evt: MessageEvent) => ProviderResult<AdapterParams>;
|
|
40
20
|
}[];
|
|
41
21
|
sseConnection?: EventSource;
|
|
42
|
-
subscriptionSet: SubscriptionSet<
|
|
22
|
+
subscriptionSet: SubscriptionSet<AdapterParams>;
|
|
43
23
|
timeOfLastReq: number;
|
|
44
|
-
localSubscriptions:
|
|
24
|
+
localSubscriptions: AdapterParams[];
|
|
45
25
|
constructor(config: {
|
|
46
|
-
prepareSSEConnectionConfig: (params:
|
|
47
|
-
prepareKeepAliveRequest
|
|
48
|
-
prepareSubscriptionRequest: (params:
|
|
49
|
-
prepareUnsubscriptionRequest: (params:
|
|
26
|
+
prepareSSEConnectionConfig: (params: AdapterParams[], context: AdapterContext<CustomSettings>) => SSEConfig;
|
|
27
|
+
prepareKeepAliveRequest: (context: AdapterContext<CustomSettings>) => AxiosRequestConfig<ProviderRequestBody>;
|
|
28
|
+
prepareSubscriptionRequest: (params: AdapterParams[], context: AdapterContext<CustomSettings>) => AxiosRequestConfig<ProviderRequestBody>;
|
|
29
|
+
prepareUnsubscriptionRequest: (params: AdapterParams[], context: AdapterContext<CustomSettings>) => AxiosRequestConfig<ProviderRequestBody>;
|
|
50
30
|
eventListeners: {
|
|
51
31
|
type: string;
|
|
52
|
-
parseResponse: (evt: MessageEvent) => ProviderResult<
|
|
32
|
+
parseResponse: (evt: MessageEvent) => ProviderResult<AdapterParams>[];
|
|
53
33
|
}[];
|
|
54
34
|
keepaliveSleepMs?: number;
|
|
55
35
|
pollingSleepMs?: number;
|
|
56
36
|
});
|
|
57
|
-
initialize(dependencies: AdapterDependencies, config: AdapterConfig<
|
|
58
|
-
|
|
59
|
-
|
|
37
|
+
initialize(dependencies: AdapterDependencies, config: AdapterConfig<SettingsMap>, endpointName: string): Promise<void>;
|
|
38
|
+
hasBeenSetUp(): Promise<boolean>;
|
|
39
|
+
setup(req: AdapterRequest<AdapterParams>, config: AdapterConfig<CustomSettings>): Promise<void>;
|
|
40
|
+
backgroundExecute(context: AdapterContext<CustomSettings>): Promise<number>;
|
|
60
41
|
}
|
|
61
|
-
export {};
|
package/transports/sse.js
CHANGED
|
@@ -4,16 +4,11 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.SSETransport = void 0;
|
|
7
|
-
const axios_1 = __importDefault(require("axios"));
|
|
8
7
|
const eventsource_1 = __importDefault(require("eventsource"));
|
|
8
|
+
const axios_1 = __importDefault(require("axios"));
|
|
9
9
|
const util_1 = require("../util");
|
|
10
10
|
const _1 = require("./");
|
|
11
11
|
const logger = (0, util_1.makeLogger)('SSETransport');
|
|
12
|
-
/**
|
|
13
|
-
* Transport implementation that establishes a long lived connection to a server using the SSE protocol and subcribes to updates.
|
|
14
|
-
*
|
|
15
|
-
* @typeParam T - Helper struct type that will be used to pass types to the generic parameters (check [[SSETransportGenerics]])
|
|
16
|
-
*/
|
|
17
12
|
class SSETransport {
|
|
18
13
|
constructor(config) {
|
|
19
14
|
this.config = config;
|
|
@@ -30,8 +25,11 @@ class SSETransport {
|
|
|
30
25
|
this.EventSource = dependencies.eventSource;
|
|
31
26
|
}
|
|
32
27
|
}
|
|
33
|
-
async
|
|
34
|
-
|
|
28
|
+
async hasBeenSetUp() {
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
async setup(req, config) {
|
|
32
|
+
logger.debug(`Adding entry to subscription set: [${req.requestContext.cacheKey}] = ${req.requestContext.data}`);
|
|
35
33
|
await this.subscriptionSet.add(req.requestContext.cacheKey, req.requestContext.data, config.SSE_SUBSCRIPTION_TTL);
|
|
36
34
|
}
|
|
37
35
|
// Unlike cache warming, this execute will manage subscriptions
|
|
@@ -39,6 +37,7 @@ class SSETransport {
|
|
|
39
37
|
logger.debug('Starting background execute, getting subscriptions from sorted set');
|
|
40
38
|
const desiredSubs = await this.subscriptionSet.getAll();
|
|
41
39
|
logger.debug('Generating delta (subscribes & unsubscribes)');
|
|
40
|
+
// TODO: More efficient algorithm, this is really easy to read, but high(er) time complexity
|
|
42
41
|
const subscribeParams = desiredSubs.filter((s) => !this.localSubscriptions.includes(s));
|
|
43
42
|
const unsubscribeParams = this.localSubscriptions.filter((s) => !desiredSubs.includes(s));
|
|
44
43
|
logger.debug(`${subscribeParams.length} new subscriptions; ${unsubscribeParams.length} to unsubscribe`);
|
|
@@ -82,8 +81,7 @@ class SSETransport {
|
|
|
82
81
|
const unsubscribeRequest = this.config.prepareUnsubscriptionRequest(unsubscribeParams, context);
|
|
83
82
|
makeRequest(unsubscribeRequest);
|
|
84
83
|
}
|
|
85
|
-
if (
|
|
86
|
-
desiredSubs.length &&
|
|
84
|
+
if (desiredSubs.length &&
|
|
87
85
|
Date.now() - this.timeOfLastReq > context.adapterConfig.SSE_KEEPALIVE_SLEEP) {
|
|
88
86
|
const prepareKeepAliveRequest = this.config.prepareKeepAliveRequest(context);
|
|
89
87
|
makeRequest(prepareKeepAliveRequest);
|