diff --git a/.changeset/few-beers-draw.md b/.changeset/few-beers-draw.md new file mode 100644 index 00000000..0ce0a974 --- /dev/null +++ b/.changeset/few-beers-draw.md @@ -0,0 +1,7 @@ +--- +"@evervault/sdk": patch +--- + +Correct logic for parsing domains from URLs supplied to the http request function: add fallback support for path alongisde pathname. + +Fix bug in parsing raw strings supplied to request to URLs by correcting object keys. diff --git a/lib/utils/httpsHelper.js b/lib/utils/httpsHelper.js index ed5ee79a..f29439ee 100644 --- a/lib/utils/httpsHelper.js +++ b/lib/utils/httpsHelper.js @@ -39,6 +39,34 @@ const certificateUtil = (evClient) => { }; }; +/** + * + * @param {Parameters} args + * @returns {{ domain: string, path: string }} + */ +function getDomainAndPathFromArgs(args) { + if (typeof args[0] === 'string') { + const parsedUrl = new URL(args[0]); + return { domain: parsedUrl.host, path: parsedUrl.pathname }; + } + + if (args[0] instanceof URL) { + return { domain: args[0].host, path: args[0].pathname }; + } + + let domain, path; + for (const arg of args) { + if (arg instanceof Object) { + domain = domain ?? arg.hostname ?? arg.host; + path = path ?? arg.pathname ?? arg.path; + } + } + return { + domain, + path, + }; +} + /** * @param {string} apiKey * @param {string} tunnelHostname @@ -56,41 +84,13 @@ const overloadHttpsModule = ( evClient, originalRequest ) => { - /** - * - * @param {Parameters} args - * @returns {{ host: string, path?: string }} - */ - function getDomainAndPathFromArgs(args) { - if (typeof args[0] === 'string') { - const parsedUrl = new URL(args[0]); - return { host: parsedUrl.host, path: parsedUrl.pathname }; - } - - if (args[0] instanceof URL) { - return { host: args[0].host, path: args[0].pathname }; - } - - let domain, path; - for (const arg of args) { - if (arg instanceof Object) { - domain = domain ?? arg.hostname ?? arg.host; - path = path ?? arg.pathname; - } - } - return { - domain, - path, - }; - } - /** * @param {Parameters} args * @returns {ReturnType} */ function wrapMethodRequest(...args) { const { domain, path } = getDomainAndPathFromArgs(args); - const shouldProxy = domainFilter(domain, path); + const shouldProxy = !!domain && domainFilter(domain, path); if ( debugRequests && !EVERVAULT_DOMAINS.some((evervault_domain) => @@ -147,4 +147,5 @@ const httpsRelayAgent = ( module.exports = { overloadHttpsModule, httpsRelayAgent, + getDomainAndPathFromArgs, }; diff --git a/tests/httpHelper.test.js b/tests/httpHelper.test.js index bc8aeb83..93f9ba90 100644 --- a/tests/httpHelper.test.js +++ b/tests/httpHelper.test.js @@ -4,6 +4,24 @@ const { expect } = require('chai'); const https = require('https'); const fs = require('fs'); +describe('getDomainAndPathFromArgs', () => { + [ + [new URL('https://a.com'), 'a.com', '/'], + [new URL('https://a.com/b/c'), 'a.com', '/b/c'], + ['https://a.com', 'a.com', '/'], + ['https://a.com/b/c', 'a.com', '/b/c'], + [{ hostname: 'a.com', pathname: '/' }, 'a.com', '/'], + [{ host: 'a.com', path: '/' }, 'a.com', '/'], + [{ yayForJavascript: 1 }, undefined, undefined], + ].forEach(([url, expectedDomain, expectedPath], i) => { + it(`works for ${JSON.stringify(url)} (${i})`, () => { + const result = httpsHelper.getDomainAndPathFromArgs([url]); + expect(result.domain).to.equal(expectedDomain); + expect(result.path).to.equal(expectedPath); + }); + }); +}); + describe('overload https requests', () => { const apiKey = 'test-api-key'; const tunnelHostname = 'relay.evervault.com';