diff --git a/lib/proxy.js b/lib/proxy.js index 1f209b6c..c6c5dc6f 100644 --- a/lib/proxy.js +++ b/lib/proxy.js @@ -1,4 +1,4 @@ -import { pathToRegexp } from 'path-to-regexp'; +import { match } from 'path-to-regexp'; import Metrics from '@metrics/client'; import { URL } from 'url'; import abslog from 'abslog'; @@ -23,8 +23,7 @@ export default class PodiumProxy { #proxy; #metrics; #histogram; - #pathnameEntries; - #pathnameParser; + #pathnameMatcher; /** * @constructor @@ -40,17 +39,12 @@ export default class PodiumProxy { this.#prefix = utils.pathnameBuilder(prefix); this.#log = abslog(logger); - this.#pathnameEntries = []; - this.#pathnameParser = pathToRegexp( - utils.pathnameBuilder( - this.#pathname, - this.#prefix, - ':podiumPodletName', - ':podiumProxyName', - ':podiumProxyExtras*', - ), - this.#pathnameEntries, + const expresslikePath = utils.pathnameBuilder( + this.#pathname, + this.#prefix, + '/:podiumPodletName/:podiumProxyName{/*podiumProxyExtras}', ); + this.#pathnameMatcher = match(expresslikePath); this.#proxy = Proxy.createProxy({ proxyTimeout: timeout, @@ -172,7 +166,7 @@ export default class PodiumProxy { }); return new Promise((resolve, reject) => { - const match = this.#pathnameParser.exec(incoming.url.pathname); + const match = this.#pathnameMatcher(incoming.url.pathname); let errored = false; if (match) { @@ -184,13 +178,12 @@ export default class PodiumProxy { // Turn matched uri parameters into an object of parameters const params = {}; - for (let i = 1; i < match.length; i += 1) { - const key = this.#pathnameEntries[i - 1]; - params[key.name] = match[i]; + for (const [key, value] of Object.entries(match.params)) { + // This should never really be an array, but let's be extra safe + params[key] = Array.isArray(value) ? value[0] : value; } const key = `${params.podiumPodletName}/${params.podiumProxyName}`; - // See if podlet has a matching proxy entry. // If so we want to proxy. If not, skip rest of processing if (!this.#registry.has(key)) { diff --git a/package.json b/package.json index 6631d45a..8743a522 100644 --- a/package.json +++ b/package.json @@ -33,7 +33,7 @@ "lint": "eslint .", "lint:fix": "eslint --fix .", "test": "run-s test:*", - "test:unit": "tap tests/*.js --disable-coverage --allow-empty-coverage", + "test:unit": "tap tests/*.test.js --disable-coverage --allow-empty-coverage", "test:types": "tsc --project tsconfig.test.json", "types": "tsc --declaration --emitDeclarationOnly" }, @@ -43,7 +43,7 @@ "@podium/schemas": "5.0.6", "@podium/utils": "5.2.0", "abslog": "2.4.4", - "path-to-regexp": "6.3.0" + "path-to-regexp": "8.1.0" }, "devDependencies": { "@babel/eslint-parser": "7.24.7",