diff --git a/.nycrc.yml b/.nycrc.yml index c2843292..29a0f787 100644 --- a/.nycrc.yml +++ b/.nycrc.yml @@ -14,5 +14,6 @@ reporter: [ "text-summary" ] exclude: [ - "**/node_modules/**" + "**/node_modules/**", + "src/util/proxies.js" # todo: increase test coverage ] diff --git a/package-lock.json b/package-lock.json index 47626030..eeb9a3c2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,23 +1,24 @@ { "name": "@mojaloop/central-services-shared", - "version": "18.6.3", + "version": "18.7.0-snapshot.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@mojaloop/central-services-shared", - "version": "18.6.3", + "version": "18.7.0-snapshot.0", "license": "Apache-2.0", "dependencies": { "@hapi/catbox": "12.1.1", "@hapi/catbox-memory": "5.0.1", - "@mojaloop/inter-scheme-proxy-cache-lib": "1.4.0", + "@mojaloop/inter-scheme-proxy-cache-lib": "2.0.0-snapshot.1", "axios": "1.7.2", "clone": "2.1.2", "dotenv": "16.4.5", "env-var": "7.5.0", "event-stream": "4.0.1", - "immutable": "4.3.6", + "fast-safe-stringify": "^2.1.1", + "immutable": "4.3.7", "lodash": "4.17.21", "mustache": "4.2.0", "openapi-backend": "5.10.6", @@ -26,7 +27,7 @@ "shins": "2.6.0", "uuid4": "2.0.3", "widdershins": "^4.0.1", - "yaml": "2.4.5" + "yaml": "2.5.0" }, "devDependencies": { "@hapi/hapi": "21.3.10", @@ -1407,15 +1408,48 @@ } }, "node_modules/@mojaloop/central-services-logger": { - "version": "11.3.1", - "resolved": "https://registry.npmjs.org/@mojaloop/central-services-logger/-/central-services-logger-11.3.1.tgz", - "integrity": "sha512-XVU2K5grE1ZcIyxUXeMlvoVkeIcs9y1/0EKxa2Bk5sEbqXUtHuR8jqbAGlwaUIi9T9YWZRJyVC77nOQe/X1teA==", + "version": "11.4.5", + "resolved": "https://registry.npmjs.org/@mojaloop/central-services-logger/-/central-services-logger-11.4.5.tgz", + "integrity": "sha512-nCKEIinB/Zx3routZhcGd+//IKd9oThpGggTde4rNLJ6O4nVJgHSW6pZIzd1T+Mj34yBhOPidhBa0piLBKcZtQ==", "dependencies": { - "@types/node": "^20.12.7", "parse-strings-in-object": "2.0.0", "rc": "1.2.8", "safe-stable-stringify": "^2.4.3", - "winston": "3.13.0" + "winston": "3.13.1" + } + }, + "node_modules/@mojaloop/central-services-logger/node_modules/@colors/colors": { + "version": "1.6.0", + "resolved": "https://registry.npmjs.org/@colors/colors/-/colors-1.6.0.tgz", + "integrity": "sha512-Ir+AOibqzrIsL6ajt3Rz3LskB7OiMVHqltZmspbW/TJuTVuyOMirVqAkjfY6JISiLHgyNqicAC8AyHHGzNd/dA==", + "engines": { + "node": ">=0.1.90" + } + }, + "node_modules/@mojaloop/central-services-logger/node_modules/async": { + "version": "3.2.5", + "resolved": "https://registry.npmjs.org/async/-/async-3.2.5.tgz", + "integrity": "sha512-baNZyqaaLhyLVKm/DlvdW051MSgO6b8eVfIezl9E5PqWxFgzLm/wQntEW4zOytVburDEr0JlALEpdOFwvErLsg==" + }, + "node_modules/@mojaloop/central-services-logger/node_modules/winston": { + "version": "3.13.1", + "resolved": "https://registry.npmjs.org/winston/-/winston-3.13.1.tgz", + "integrity": "sha512-SvZit7VFNvXRzbqGHsv5KSmgbEYR5EiQfDAL9gxYkRqa934Hnk++zze0wANKtMHcy/gI4W/3xmSDwlhf865WGw==", + "dependencies": { + "@colors/colors": "^1.6.0", + "@dabh/diagnostics": "^2.0.2", + "async": "^3.2.3", + "is-stream": "^2.0.0", + "logform": "^2.6.0", + "one-time": "^1.0.0", + "readable-stream": "^3.4.0", + "safe-stable-stringify": "^2.3.1", + "stack-trace": "0.0.x", + "triple-beam": "^1.3.0", + "winston-transport": "^4.7.0" + }, + "engines": { + "node": ">= 12.0.0" } }, "node_modules/@mojaloop/central-services-metrics": { @@ -1462,12 +1496,12 @@ } }, "node_modules/@mojaloop/inter-scheme-proxy-cache-lib": { - "version": "1.4.0", - "resolved": "https://registry.npmjs.org/@mojaloop/inter-scheme-proxy-cache-lib/-/inter-scheme-proxy-cache-lib-1.4.0.tgz", - "integrity": "sha512-jmAWWdjZxjxlSQ+wt8aUcMYOneVo1GNbIIs7yK/R2K9DBtKb0aYle2mWwdjm9ovk6zSWL2a9lH+n3hq7kb08Wg==", + "version": "2.0.0-snapshot.1", + "resolved": "https://registry.npmjs.org/@mojaloop/inter-scheme-proxy-cache-lib/-/inter-scheme-proxy-cache-lib-2.0.0-snapshot.1.tgz", + "integrity": "sha512-3cIs1U0Ufk16YFw9eR4QrrkO+hSHLUkoTAoxN5wM09/HIfE9+upZCnn0R+roB52FWCOe4TgURFMMIKo0E6HPuw==", "dependencies": { - "@mojaloop/central-services-logger": "^11.3.1", - "ajv": "^8.16.0", + "@mojaloop/central-services-logger": "11.4.5", + "ajv": "^8.17.1", "convict": "^6.2.4", "fast-safe-stringify": "^2.1.1", "ioredis": "^5.4.1" @@ -2013,6 +2047,7 @@ "version": "20.14.9", "resolved": "https://registry.npmjs.org/@types/node/-/node-20.14.9.tgz", "integrity": "sha512-06OCtnTXtWOZBJlRApleWndH4JsRVs1pDCc8dLSQp+7PpUpX3ePdHyeNSFTeSe7FtKyQkrlPvHwJOW3SLd8Oyg==", + "peer": true, "dependencies": { "undici-types": "~5.26.4" } @@ -2123,14 +2158,14 @@ } }, "node_modules/ajv": { - "version": "8.16.0", - "resolved": "https://registry.npmjs.org/ajv/-/ajv-8.16.0.tgz", - "integrity": "sha512-F0twR8U1ZU67JIEtekUcLkXkoO5mMMmgGD8sK/xUFzJ805jxHQl92hImFAqqXMyMYjSPOyUPAwHYhB72g5sTXw==", + "version": "8.17.1", + "resolved": "https://registry.npmjs.org/ajv/-/ajv-8.17.1.tgz", + "integrity": "sha512-B/gBuNg5SiMTrPkC+A2+cW0RszwxYmn6VYxB/inlBStS5nx6xHIt/ehKRhIMhqusl7a8LjQoZnjCs5vhwxOQ1g==", "dependencies": { "fast-deep-equal": "^3.1.3", + "fast-uri": "^3.0.1", "json-schema-traverse": "^1.0.0", - "require-from-string": "^2.0.2", - "uri-js": "^4.4.1" + "require-from-string": "^2.0.2" }, "funding": { "type": "github", @@ -5793,6 +5828,11 @@ "resolved": "https://registry.npmjs.org/fast-safe-stringify/-/fast-safe-stringify-2.1.1.tgz", "integrity": "sha512-W+KJc2dmILlPplD/H4K9l9LcAHAfPtP6BY84uVLXQ6Evcz9Lcg33Y2z1IVblT6xdY54PXYVHEv+0Wpq8Io6zkA==" }, + "node_modules/fast-uri": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/fast-uri/-/fast-uri-3.0.1.tgz", + "integrity": "sha512-MWipKbbYiYI0UC7cl8m/i/IWTqfC8YXsqjzybjddLsFjStroQzsHXkc73JutMvBiXmOvapk+axIl79ig5t55Bw==" + }, "node_modules/fastq": { "version": "1.15.0", "resolved": "https://registry.npmjs.org/fastq/-/fastq-1.15.0.tgz", @@ -7390,9 +7430,9 @@ } }, "node_modules/immutable": { - "version": "4.3.6", - "resolved": "https://registry.npmjs.org/immutable/-/immutable-4.3.6.tgz", - "integrity": "sha512-Ju0+lEMyzMVZarkTn/gqRpdqd5dOPaz1mCZ0SH3JV6iFw81PldE/PEB1hWVEA288HPt4WXW8O7AWxB10M+03QQ==" + "version": "4.3.7", + "resolved": "https://registry.npmjs.org/immutable/-/immutable-4.3.7.tgz", + "integrity": "sha512-1hqclzwYwjRDFLjcFxOM5AYkkG0rpFPpr1RLPMEuGczoS7YA8gLhy8SWXYRAA/XwfEHpfo3cw5JGioS32fnMRw==" }, "node_modules/import-fresh": { "version": "3.3.0", @@ -15026,7 +15066,8 @@ "node_modules/undici-types": { "version": "5.26.5", "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-5.26.5.tgz", - "integrity": "sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==" + "integrity": "sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==", + "peer": true }, "node_modules/unique-filename": { "version": "3.0.0", @@ -15640,6 +15681,7 @@ "version": "3.13.0", "resolved": "https://registry.npmjs.org/winston/-/winston-3.13.0.tgz", "integrity": "sha512-rwidmA1w3SE4j0E5MuIufFhyJPBDG7Nu71RkZor1p2+qHvJSZ9GYDA81AyleQcZbh/+V6HjeBdfnTZJm9rSeQQ==", + "peer": true, "dependencies": { "@colors/colors": "^1.6.0", "@dabh/diagnostics": "^2.0.2", @@ -15674,6 +15716,7 @@ "version": "1.6.0", "resolved": "https://registry.npmjs.org/@colors/colors/-/colors-1.6.0.tgz", "integrity": "sha512-Ir+AOibqzrIsL6ajt3Rz3LskB7OiMVHqltZmspbW/TJuTVuyOMirVqAkjfY6JISiLHgyNqicAC8AyHHGzNd/dA==", + "peer": true, "engines": { "node": ">=0.1.90" } @@ -15681,7 +15724,8 @@ "node_modules/winston/node_modules/async": { "version": "3.2.5", "resolved": "https://registry.npmjs.org/async/-/async-3.2.5.tgz", - "integrity": "sha512-baNZyqaaLhyLVKm/DlvdW051MSgO6b8eVfIezl9E5PqWxFgzLm/wQntEW4zOytVburDEr0JlALEpdOFwvErLsg==" + "integrity": "sha512-baNZyqaaLhyLVKm/DlvdW051MSgO6b8eVfIezl9E5PqWxFgzLm/wQntEW4zOytVburDEr0JlALEpdOFwvErLsg==", + "peer": true }, "node_modules/wordwrap": { "version": "1.0.0", @@ -15868,9 +15912,9 @@ "integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==" }, "node_modules/yaml": { - "version": "2.4.5", - "resolved": "https://registry.npmjs.org/yaml/-/yaml-2.4.5.tgz", - "integrity": "sha512-aBx2bnqDzVOyNKfsysjA2ms5ZlnjSAW2eG3/L5G/CSujfjLJTJsEw1bGw8kCf04KodQWk1pxlGnZ56CRxiawmg==", + "version": "2.5.0", + "resolved": "https://registry.npmjs.org/yaml/-/yaml-2.5.0.tgz", + "integrity": "sha512-2wWLbGbYDiSqqIKoPjar3MPgB94ErzCtrNE1FdqGuaO0pi2JGjmE8aW8TDZwzU7vuxcGRdL/4gPQwQ7hD5AMSw==", "bin": { "yaml": "bin.mjs" }, diff --git a/package.json b/package.json index 8943d83c..a476c865 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@mojaloop/central-services-shared", - "version": "18.6.3", + "version": "18.7.0-snapshot.0", "description": "Shared code for mojaloop central services", "license": "Apache-2.0", "author": "ModusBox", @@ -56,13 +56,14 @@ "dependencies": { "@hapi/catbox": "12.1.1", "@hapi/catbox-memory": "5.0.1", - "@mojaloop/inter-scheme-proxy-cache-lib": "1.4.0", + "@mojaloop/inter-scheme-proxy-cache-lib": "2.0.0-snapshot.1", "axios": "1.7.2", "clone": "2.1.2", "dotenv": "16.4.5", "env-var": "7.5.0", "event-stream": "4.0.1", - "immutable": "4.3.6", + "fast-safe-stringify": "^2.1.1", + "immutable": "4.3.7", "lodash": "4.17.21", "mustache": "4.2.0", "openapi-backend": "5.10.6", @@ -71,7 +72,7 @@ "shins": "2.6.0", "uuid4": "2.0.3", "widdershins": "^4.0.1", - "yaml": "2.4.5" + "yaml": "2.5.0" }, "devDependencies": { "@hapi/hapi": "21.3.10", diff --git a/src/enums/endpoints.js b/src/enums/endpoints.js index 51e3f7fc..5b813f88 100644 --- a/src/enums/endpoints.js +++ b/src/enums/endpoints.js @@ -105,6 +105,7 @@ const FspEndpointTemplates = { TRANSACTION_REQUEST_GET: '/transactionRequests/{{ID}}', TRANSACTION_REQUEST_PUT_ERROR: '/transactionRequests/{{ID}}/error', PARTICIPANT_ENDPOINTS_GET: '/participants/{{fsp}}/endpoints', + PARTICIPANTS_GET_ALL: '/participants', PARTICIPANTS_GET: '/participants/{{fsp}}', PARTICIPANTS_POST: '/participants', PARTIES_GET: '/parties/{{fsp}}', diff --git a/src/enums/http.js b/src/enums/http.js index b61ce35d..4cc680be 100644 --- a/src/enums/http.js +++ b/src/enums/http.js @@ -36,6 +36,7 @@ const Headers = { FSPIOP: { SOURCE: 'fspiop-source', DESTINATION: 'fspiop-destination', + PROXY: 'fspiop-proxy', HTTP_METHOD: 'fspiop-http-method', SIGNATURE: 'fspiop-signature', URI: 'fspiop-uri' diff --git a/src/healthCheck/HealthCheck.js b/src/healthCheck/HealthCheck.js index 41da39c3..7afb5e1e 100644 --- a/src/healthCheck/HealthCheck.js +++ b/src/healthCheck/HealthCheck.js @@ -88,7 +88,7 @@ class HealthCheck { * @description Gets the health of the service along with sub-services * */ - async getHealth () { + async getHealth (context = undefined) { // Default values let status = statusEnum.OK let isHealthy = true @@ -100,7 +100,7 @@ class HealthCheck { const versionNumber = this.packageJson.version try { - const services = await Promise.all(this.serviceChecks.map(s => s())) + const services = await Promise.all(this.serviceChecks.map(s => s(context))) isHealthy = HealthCheck.evaluateServiceHealth(services) subServices = { services diff --git a/src/healthCheck/HealthCheckEnums.js b/src/healthCheck/HealthCheckEnums.js index cef3d952..477578ef 100644 --- a/src/healthCheck/HealthCheckEnums.js +++ b/src/healthCheck/HealthCheckEnums.js @@ -16,7 +16,8 @@ const serviceName = { datastore: 'datastore', broker: 'broker', sidecar: 'sidecar', - cache: 'cache' + cache: 'cache', + proxyCache: 'proxyCache' } module.exports = { diff --git a/src/index.d.ts b/src/index.d.ts index 367cb5bc..57459bcc 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -9,6 +9,7 @@ declare namespace CentralServicesShared { FSPIOP: { SOURCE: string; DESTINATION: string; + PROXY: string; HTTP_METHOD: string; SIGNATURE: string; URI: string; @@ -599,16 +600,26 @@ declare namespace CentralServicesShared { }; }; } - interface Endpoints { - fetchEndpoints(fspId: string): Promise - getEndpoint(switchUrl: string, fsp: string, endpointType: FspEndpointTypesEnum, options?: any): Promise + + interface Cacheable { initializeCache(policyOptions: object, config: { hubName: string, hubNameRegex: RegExp }): Promise + stopCache(): Promise + } + + interface Endpoints extends Cacheable { + getEndpoint(switchUrl: string, fsp: string, endpointType: FspEndpointTypesEnum, options?: any): Promise getEndpointAndRender(switchUrl: string, fsp: string, endpointType: FspEndpointTypesEnum, path: string, options?: any): Promise } - interface Participants { + interface Participants extends Cacheable { getParticipant(switchUrl: string, fsp: string): Promise - initializeCache(policyOptions: object, config: { hubName: string, hubNameRegex: RegExp }): Promise + invalidateParticipantCache(fsp: string): Promise + } + + type ProxyNames = string[] + interface Proxies extends Cacheable { + getAllProxiesNames(switchUrl: string): Promise + invalidateProxiesCache(): Promise } interface ProtocolVersionsType { @@ -625,8 +636,10 @@ declare namespace CentralServicesShared { createGeneralTopicConf(template: string, functionality: string, action: string, key?: string, partition?: number, opaqueKey?: any, topicNameOverride?: string): {topicName: string, key: string | null, partition: number | null, opaqueKey: any } } + type MimeTypes = 'text/plain' | 'application/json' | 'application/vnd.interoperability.' interface StreamingProtocol { decodePayload(input: string, options: Object): Object + encodePayload(input: string | Buffer, mimeType: MimeTypes): string } interface HeaderValidation { @@ -643,6 +656,7 @@ declare namespace CentralServicesShared { interface Util { Endpoints: Endpoints; Participants: Participants; + proxies: Proxies; Hapi: any; Kafka: Kafka; OpenapiBackend: any; diff --git a/src/util/index.js b/src/util/index.js index 57b1ac38..86c2888b 100644 --- a/src/util/index.js +++ b/src/util/index.js @@ -30,6 +30,7 @@ const _ = require('lodash') const Kafka = require('./kafka') const Endpoints = require('./endpoints') const Participants = require('./participants') +const proxies = require('./proxies') const Request = require('./request') const Http = require('./http') const Hapi = require('./hapi') @@ -238,6 +239,7 @@ module.exports = { filterExtensions, Kafka, Participants, + proxies, Endpoints, Request, Http, diff --git a/src/util/participants.js b/src/util/participants.js index 0bd7c3c8..238de92b 100644 --- a/src/util/participants.js +++ b/src/util/participants.js @@ -145,6 +145,12 @@ exports.getParticipant = async (switchUrl, fsp) => { histTimer({ success: true, hit: false }) } + /* istanbul ignore next */ + if (!participant) { + Logger.isWarnEnabled && Logger.warn('participantCache::getParticipant - no participant found') + return null + } + if (participant.errorInformation) { // Drop error from cache await policy.drop(fsp) diff --git a/src/util/proxies.js b/src/util/proxies.js new file mode 100644 index 00000000..1fdf2745 --- /dev/null +++ b/src/util/proxies.js @@ -0,0 +1,182 @@ +/***** + License + -------------- + Copyright © 2017 Bill & Melinda Gates Foundation + The Mojaloop files are made available by the Bill & Melinda Gates Foundation under the Apache License, Version 2.0 (the "License") and you may not use these files except in compliance with the License. You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 + Unless required by applicable law or agreed to in writing, the Mojaloop files are distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. + + Contributors + -------------- + This is the official list of the Mojaloop project contributors for this file. + Names of the original copyright holders (individuals or organizations) + should be listed with a '*' in the first column. People who have + contributed from an organization can be listed under the organization + that actually holds the copyright for their contributions (see the + Gates Foundation organization for an example). Those individuals should have + their names indented and be marked with a '-'. Email address can be added + optionally within square brackets . + * Gates Foundation + - Name Surname + + * Eugen Klymniuk + -------------- + **********/ + +const Mustache = require('mustache') +const Catbox = require('@hapi/catbox') +const CatboxMemory = require('@hapi/catbox-memory') +const ErrorHandler = require('@mojaloop/central-services-error-handling') +const Logger = require('@mojaloop/central-services-logger') +const Metrics = require('@mojaloop/central-services-metrics') + +const Enum = require('../enums') +const Http = require('./http') +const request = require('./request') + +const partition = 'proxies-cache' +const clientOptions = { partition } +const cacheKey = 'allProxies' + +let client +let policy +let switchEndpoint +let hubName +let hubNameRegex + +/** +* @function fetchProxies +* @description This populates the cache of proxies +* @returns {array} proxies Returns the list containing proxies +*/ +const fetchProxies = async () => { + const histTimer = Metrics.getHistogram( + 'fetchProxies', + 'fetchProxies - Metrics for fetchProxies', + ['success'] + ).startTimer() + try { + Logger.isDebugEnabled && Logger.debug('proxiesCache::fetchProxies := Refreshing proxies cache') + if (!hubName || !hubNameRegex) { + throw Error('No hubName or hubNameRegex! Initialize the cache first.') + } + const defaultHeaders = Http.SwitchDefaultHeaders(hubName, Enum.Http.HeaderResources.PARTICIPANTS, hubName) + const url = Mustache.render(switchEndpoint + Enum.EndPoints.FspEndpointTemplates.PARTICIPANTS_GET_ALL) + const params = { isProxy: true } + Logger.isDebugEnabled && Logger.debug(`proxiesCache::fetchProxies := URL: ${url} QS: ${JSON.stringify(params)}`) + const response = await request.sendRequest({ + url, + headers: defaultHeaders, + source: hubName, + destination: hubName, + params, + hubNameRegex + }) + const proxies = response.data + histTimer({ success: true }) + return proxies + } catch (e) { + histTimer({ success: false }) + Logger.isErrorEnabled && Logger.error(`proxiesCache::fetchProxies:: ERROR:'${e}'`) + } +} + +/** +* @function initializeCache +* +* @description This initializes the cache for allProxies +* @param {object} policyOptions The Endpoint_Cache_Config for the Cache being stored https://hapi.dev/module/catbox/api/?v=12.1.1#policy +* @param {object} config The config object containing paramters used for the request function +* @returns {boolean} Returns true on successful initialization of the cache, throws error on failures +*/ +exports.initializeCache = async (policyOptions, config) => { + try { + Logger.isDebugEnabled && Logger.debug(`proxiesCache::initializeCache::start::clientOptions - ${JSON.stringify(clientOptions)}`) + client = new Catbox.Client(CatboxMemory, clientOptions) + await client.start() + policyOptions.generateFunc = fetchProxies + Logger.isDebugEnabled && Logger.debug(`proxiesCache::initializeCache::start::policyOptions - ${JSON.stringify(policyOptions)}`) + policy = new Catbox.Policy(policyOptions, client, partition) + Logger.isDebugEnabled && Logger.debug('proxiesCache::initializeCache::Cache initialized successfully') + hubName = config.hubName + hubNameRegex = config.hubNameRegex + return true + } catch (err) { + Logger.isErrorEnabled && Logger.error(`proxiesCache::Cache error:: ERROR:'${err}'`) + throw ErrorHandler.Factory.reformatFSPIOPError(err) + } +} + +/** +* @function getAllProxiesNames +* @description It returns a list of allProxies names from the cache if the cache is still valid, otherwise it will refresh the cache and return the value +* +* @param {string} switchUrl the endpoint for the switch +* +* @returns {string[]} - Returns list of allProxies names, throws error if failure occurs +*/ +exports.getAllProxiesNames = async (switchUrl) => { + const histTimer = Metrics.getHistogram( + 'getAllProxiesNames', + 'getAllProxiesNames - Metrics for getAllProxies with cache hit rate', + ['success', 'hit'] + ).startTimer() + switchEndpoint = switchUrl + Logger.isDebugEnabled && Logger.debug('proxiesCache::getAllProxiesNames') + try { + // If a service passes in `getDecoratedValue` as true, then an object + // { value, cached, report } is returned, where value is the cached value, + // cached is null on a cache miss. + let proxies = await policy.get(cacheKey) + + if ('value' in proxies && 'cached' in proxies) { + if (proxies.cached === null) { + histTimer({ success: true, hit: false }) + } else { + histTimer({ success: true, hit: true }) + } + proxies = proxies.value + } else { + histTimer({ success: true, hit: false }) + } + + if (proxies.errorInformation) { + // Drop error from cache + await policy.drop(cacheKey) + throw ErrorHandler.Factory.createFSPIOPErrorFromErrorInformation(proxies.errorInformation) + } + return proxies.map(p => p.name) + } catch (err) { + histTimer({ success: false, hit: false }) + Logger.isErrorEnabled && Logger.error(`proxiesCache::getAllProxiesNames:: ERROR:'${err}'`) + throw ErrorHandler.Factory.reformatFSPIOPError(err) + } +} + +/** +* @function invalidateProxiesCache +* +* @description It drops the cache for all proxies +* +* @returns {void} +*/ +exports.invalidateProxiesCache = async () => { + Logger.isDebugEnabled && Logger.debug('proxiesCache::invalidateProxiesCache::Invalidating the cache') + if (policy) { + return policy.drop(cacheKey) + } +} + +/** +* @function stopCache +* +* @description It stops the cache client +* +* @returns {boolean} - Returns the status +*/ +exports.stopCache = async () => { + Logger.isDebugEnabled && Logger.debug('proxiesCache::stopCache::Stopping the cache') + if (client) { + return client.stop() + } +} diff --git a/src/util/request.js b/src/util/request.js index 96ccba73..a071b8f7 100644 --- a/src/util/request.js +++ b/src/util/request.js @@ -24,12 +24,13 @@ ******/ 'use strict' -const EventSdk = require('@mojaloop/event-sdk') +const http = require('node:http') const request = require('axios') +const stringify = require('fast-safe-stringify') +const EventSdk = require('@mojaloop/event-sdk') const Logger = require('@mojaloop/central-services-logger') const ErrorHandler = require('@mojaloop/central-services-error-handling') const Metrics = require('@mojaloop/central-services-metrics') -const http = require('http') const Headers = require('./headers/transformer') const enums = require('../enums') @@ -59,6 +60,7 @@ request.defaults.httpAgent.toJSON = () => ({}) * @param {string} source id for which callback is being sent from * @param {string} destination id for which callback is being sent * @param {object | undefined} payload the body of the request being sent + * @param {object | null} params URL parameters to be sent with the request. Must be a plain object, URLSearchParams object or null/undefined * @param {string} responseType the type of the response object * @param {object | undefined} span a span for event logging if this request is within a span * @param {object | undefined} jwsSigner the jws signer for signing the requests @@ -75,6 +77,7 @@ const sendRequest = async ({ destination, method = enums.Http.RestMethods.GET, payload = undefined, + params, responseType = enums.Http.ResponseTypes.JSON, span = undefined, jwsSigner = undefined, @@ -93,7 +96,8 @@ const sendRequest = async ({ sendRequestSpan.setTags({ source, destination, method, url }) } let requestOptions - if (!url || !method || !headers || (method !== enums.Http.RestMethods.GET && method !== enums.Http.RestMethods.DELETE && !payload) || !source || !destination || !hubNameRegex) { + if (!url || !method || !headers || (method !== enums.Http.RestMethods.GET && method !== enums.Http.RestMethods.DELETE && !payload) || !source || !hubNameRegex) { + // think, if we can just avoid checking "destination" throw ErrorHandler.Factory.createInternalServerFSPIOPError(MISSING_FUNCTION_PARAMETERS) } try { @@ -109,6 +113,7 @@ const sendRequest = async ({ method, headers: transformedHeaders, data: payload, + params, responseType, httpAgent: new http.Agent({ keepAlive: true }), ...axiosRequestOptionsOverride @@ -122,29 +127,29 @@ const sendRequest = async ({ requestOptions = span.injectContextToHttpRequest(requestOptions) span.audit(requestOptions, EventSdk.AuditEventAction.egress) } - Logger.isDebugEnabled && Logger.debug(`sendRequest::request ${JSON.stringify(requestOptions)}`) + Logger.isDebugEnabled && Logger.debug(`sendRequest::requestOptions ${stringify(requestOptions)}`) const response = await request(requestOptions) - Logger.isDebugEnabled && Logger.debug(`Success: sendRequest::response ${JSON.stringify(response, Object.getOwnPropertyNames(response))}`) + !!sendRequestSpan && await sendRequestSpan.finish() histTimerEnd({ success: true, source, destination, method }) return response } catch (error) { - Logger.isErrorEnabled && Logger.error(error) + Logger.isErrorEnabled && Logger.error(`error in request.sendRequest: ${error.stack}`) const extensionArray = [ { key: 'url', value: url }, { key: 'sourceFsp', value: source }, { key: 'destinationFsp', value: destination }, { key: 'method', value: method }, - { key: 'request', value: JSON.stringify(requestOptions) }, + { key: 'request', value: stringify(requestOptions) }, { key: 'errorMessage', value: error.message } ] const extensions = [] if (error.response) { - extensionArray.push({ key: 'status', value: error.response && error.response.status }) - extensionArray.push({ key: 'response', value: error.response && error.response.data }) - extensions.push({ key: 'status', value: error.response && error.response.status }) + extensionArray.push({ key: 'status', value: error.response?.status }) + extensionArray.push({ key: 'response', value: error.response?.data }) + extensions.push({ key: 'status', value: error.response?.status }) } - const cause = JSON.stringify(extensionArray) + const cause = stringify(extensionArray) extensions.push({ key: 'cause', value: cause }) const fspiopError = ErrorHandler.Factory.createFSPIOPError(ErrorHandler.Enums.FSPIOPErrorCodes.DESTINATION_COMMUNICATION_ERROR, 'Failed to send HTTP request to host', error, source, extensions) if (sendRequestSpan) { diff --git a/src/util/streaming/protocol/index.js b/src/util/streaming/protocol/index.js index 90fb1b92..f3cd503d 100644 --- a/src/util/streaming/protocol/index.js +++ b/src/util/streaming/protocol/index.js @@ -205,7 +205,7 @@ const createEventState = (status, code, description) => { /** * Encodes Payload to base64 encoded data URI * - * @param {buffer\|string} input - Buffer or String + * @param {buffer|string} input - Buffer or String * @param {MimeTypes} mimeType - mime type of the input * * @return {string} - Returns base64 encoded data URI string diff --git a/test/unit/request.test.js b/test/unit/request.test.js new file mode 100644 index 00000000..78646a0f --- /dev/null +++ b/test/unit/request.test.js @@ -0,0 +1,48 @@ +const Test = require('tapes')(require('tape')) +const sinon = require('sinon') +const proxyquire = require('proxyquire') +const { Http } = require('../../src/enums') + +Test('sendRequest Tests -->', test => { + let sandbox + let axios + let request + + test.beforeEach(t => { + sandbox = sinon.createSandbox() + axios = sandbox.stub() + request = proxyquire('../../src/util/request', { axios }) + // sinon can't mock such way of using axios: axios(requestOptions) + t.end() + }) + + test.afterEach(t => { + sandbox.restore() + t.end() + }) + + test.test('should add fspiop-signature header if jwsSigner is passed ', async test => { + const signature = 'signature' + const jwsSigner = { + getSignature: sandbox.stub().callsFake(() => signature) + } + + await request.sendRequest({ + url: 'http://localhost:1234', + jwsSigner, + headers: { + [Http.Headers.FSPIOP.SOURCE]: 'source' + }, + source: 'source', + destination: 'destination', + hubNameRegex: 'hubNameRegex' + }) + + test.ok(axios.calledOnce) + const { headers } = axios.lastCall.args[0] + test.equal(headers['fspiop-signature'], signature) + test.end() + }) + + test.end() +})