Skip to content

Commit

Permalink
refactor: migrate to v8 of path-to-regexp
Browse files Browse the repository at this point in the history
  • Loading branch information
wkillerud committed Sep 13, 2024
1 parent 9e9e9ad commit f11099a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 20 deletions.
29 changes: 11 additions & 18 deletions lib/proxy.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -23,8 +23,7 @@ export default class PodiumProxy {
#proxy;
#metrics;
#histogram;
#pathnameEntries;
#pathnameParser;
#pathnameMatcher;

/**
* @constructor
Expand All @@ -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,
Expand Down Expand Up @@ -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) {
Expand All @@ -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)) {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand All @@ -43,7 +43,7 @@
"@podium/schemas": "5.0.6",
"@podium/utils": "5.2.0",
"abslog": "2.4.4",
"path-to-regexp": "8.0.0"
"path-to-regexp": "8.1.0"
},
"devDependencies": {
"@babel/eslint-parser": "7.24.7",
Expand Down

0 comments on commit f11099a

Please sign in to comment.