Skip to content

Commit

Permalink
feat: Support manifest with array of proxy endpoints (#226)
Browse files Browse the repository at this point in the history
Co-authored-by: Trygve Lie <trygve.lie@finn.no>
  • Loading branch information
trygve-lie and Trygve Lie authored Sep 21, 2022
1 parent ea28608 commit 330384b
Show file tree
Hide file tree
Showing 5 changed files with 566 additions and 98 deletions.
83 changes: 36 additions & 47 deletions lib/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { URL } from 'url';
import abslog from 'abslog';
import * as schemas from '@podium/schemas';
import * as utils from '@podium/utils';
import Cache from 'ttl-mem-cache';
import Proxy from 'http-proxy';

export default class PodiumProxy {
Expand All @@ -21,7 +20,6 @@ export default class PodiumProxy {
pathname = '/',
prefix = '/podium-resource',
timeout = 20000,
maxAge = Infinity,
logger = null,
} = {}) {
this.#pathname = utils.pathnameBuilder(pathname);
Expand All @@ -48,32 +46,7 @@ export default class PodiumProxy {
);
});

this.#registry = new Cache({
ttl: maxAge,
});
this.#registry.on('error', (error) => {
this.#log.error(
'Error emitted by the registry in @podium/proxy module',
error,
);
});
this.#registry.on('set', (key, item) => {
Object.keys(item.proxy).forEach((name) => {
const path = utils.pathnameBuilder(
this.#pathname,
this.#prefix,
key,
name,
);
this.#log.debug(
`a proxy endpoint is mounted at pathname: ${path} pointing to: ${item.proxy[name]}`,
);
});
});

this.#registry.on('dispose', (key) => {
this.#log.debug(`dispose proxy item on key "${key}"`);
});
this.#registry = new Map();

this.#metrics = new Metrics();
this.#metrics.on('error', (error) => {
Expand Down Expand Up @@ -120,7 +93,37 @@ export default class PodiumProxy {
'The value for the required argument "manifest" is not defined or not valid.',
);

this.#registry.set(obj.name, obj, Infinity);
if (Array.isArray(obj.proxy)) {
obj.proxy.forEach((item) => {
this.#registry.set(`${obj.name}/${item.name}`, item.target);

const path = utils.pathnameBuilder(
this.#pathname,
this.#prefix,
obj.name,
item.name,
);
this.#log.debug(
`a proxy endpoint is mounted at pathname: ${path} pointing to: ${item.target}`,
);
});
} else {
// NOTE: This can be removed when the object notation is removed from the schema
// and the manifest only support an array for the proxy property.
Object.keys(obj.proxy).forEach((key) => {
this.#registry.set(`${obj.name}/${key}`, obj.proxy[key]);

const path = utils.pathnameBuilder(
this.#pathname,
this.#prefix,
obj.name,
key,
);
this.#log.debug(
`a proxy endpoint is mounted at pathname: ${path} pointing to: ${obj.proxy[key]}`,
);
});
}
}

process(incoming) {
Expand Down Expand Up @@ -155,24 +158,18 @@ export default class PodiumProxy {
params[key.name] = match[i];
}

// See if "podiumPodletName" matches a podlet in registry.
// If so we might want to proxy. If not, skip rest of processing
const manifest = this.#registry.get(params.podiumPodletName);
if (!manifest) {
endTimer({ labels: { podlet: params.podiumPodletName } });
resolve(incoming);
return;
}
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
let target = manifest.proxy[params.podiumProxyName];
if (!target) {
if (!this.#registry.has(key)) {
endTimer({ labels: { podlet: params.podiumPodletName } });
resolve(incoming);
return;
}

let target = this.#registry.get(key);

// See if proxy entry is relative or not.
// In a layout server it will never be relative since the
// client will resolve relative paths in the manifest.
Expand Down Expand Up @@ -259,14 +256,6 @@ export default class PodiumProxy {
});
}

dump() {
return this.#registry.dump();
}

load(dump) {
return this.#registry.load(dump);
}

get [Symbol.toStringTag]() {
return 'PodiumProxy';
}
Expand Down
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,11 @@
},
"dependencies": {
"@metrics/client": "2.5.0",
"@podium/schemas": "5.0.0-next.4",
"@podium/schemas": "5.0.0-next.6",
"@podium/utils": "5.0.0-next.6",
"abslog": "2.4.0",
"http-proxy": "1.18.1",
"path-to-regexp": "6.2.1",
"ttl-mem-cache": "4.1.0"
"path-to-regexp": "6.2.1"
},
"devDependencies": {
"@semantic-release/changelog": "6.0.1",
Expand Down
Loading

0 comments on commit 330384b

Please sign in to comment.