Archived
1
0

Use Set for proxy domains

This commit is contained in:
Asher 2020-04-02 11:49:23 -05:00
parent 498becd11f
commit aaa6c279a1
No known key found for this signature in database
GPG Key ID: D63C1EF81242354A
2 changed files with 5 additions and 7 deletions

View File

@ -17,7 +17,7 @@ export class ProxyHttpProvider extends HttpProvider implements HttpProxyProvider
/** /**
* Proxy domains are stored here without the leading `*.` * Proxy domains are stored here without the leading `*.`
*/ */
public readonly proxyDomains: string[] public readonly proxyDomains: Set<string>
private readonly proxy = proxy.createProxyServer({}) private readonly proxy = proxy.createProxyServer({})
/** /**
@ -26,7 +26,7 @@ export class ProxyHttpProvider extends HttpProvider implements HttpProxyProvider
*/ */
public constructor(options: HttpProviderOptions, proxyDomains: string[] = []) { public constructor(options: HttpProviderOptions, proxyDomains: string[] = []) {
super(options) super(options)
this.proxyDomains = proxyDomains.map((d) => d.replace(/^\*\./, "")).filter((d, i, arr) => arr.indexOf(d) === i) this.proxyDomains = new Set(proxyDomains.map((d) => d.replace(/^\*\./, "")))
this.proxy.on("error", (error) => logger.warn(error.message)) this.proxy.on("error", (error) => logger.warn(error.message))
// Intercept the response to rewrite absolute redirects against the base path. // Intercept the response to rewrite absolute redirects against the base path.
this.proxy.on("proxyRes", (response, request: Request) => { this.proxy.on("proxyRes", (response, request: Request) => {
@ -124,7 +124,7 @@ export class ProxyHttpProvider extends HttpProvider implements HttpProxyProvider
// There must be an exact match. // There must be an exact match.
const port = parts.shift() const port = parts.shift()
const proxyDomain = parts.join(".") const proxyDomain = parts.join(".")
if (!port || !this.proxyDomains.includes(proxyDomain)) { if (!port || !this.proxyDomains.has(proxyDomain)) {
return undefined return undefined
} }

View File

@ -94,10 +94,8 @@ const main = async (args: Args): Promise<void> => {
logger.info(" - Not serving HTTPS") logger.info(" - Not serving HTTPS")
} }
if (proxy.proxyDomains.length === 1) { if (proxy.proxyDomains.size > 0) {
logger.info(` - Proxying *.${proxy.proxyDomains[0]}`) logger.info(` - Proxying the following domain${proxy.proxyDomains.size === 1 ? "" : "s"}:`)
} else if (proxy.proxyDomains.length > 1) {
logger.info(" - Proxying the following domains:")
proxy.proxyDomains.forEach((domain) => logger.info(` - *.${domain}`)) proxy.proxyDomains.forEach((domain) => logger.info(` - *.${domain}`))
} }