You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I use Koa @koa/router to serve multiple routers from the same webserver.
Each router serves one or more domains, sometimes including specific subdomains.
My suggestion is to enhance the type signature of Router.RouterOptions["host"] to support an array of strings, matched by equality to any member, in addition to the current single string equality and RegExp test.
I suppose the source code change would look like this: (line 771 in /lib/router.js)
Router.prototype.matchHost = function (input) {
const { host } = this;
if (!host) {
return true;
}
if (!input) {
return false;
}
if (typeof host === 'string') {
return input === host;
}
if (typeof host === 'object' && host instanceof RegExp) {
return host.test(input);
}
+ if (Array.isArray(host)) {+ return host.includes(input);+ }
};
With the proposed change, the 'parent' koa app would only have the responsibility of providing an array of valid domains:
I can currently accomplish similar results 2 separate ways, neither of which is as simple as I would like:
Workaround 1.
Build a dynamic RegExp from config at runtime, and assign to Router.RouterOptions["host"]
- requires escaping period characters from domain
- The ECMA proposal to ease this is still in infancy: https://github.com/tc39/proposal-regex-escaping
- cumbersome to handle subdomains without using wildcards or accidental performance pitfall
- pseudo example of my usage:
I use Koa @koa/router to serve multiple routers from the same webserver.
Each router serves one or more domains, sometimes including specific subdomains.
My suggestion is to enhance the type signature of
Router.RouterOptions["host"]
to support an array of strings, matched by equality to any member, in addition to the current single string equality and RegExp test.I suppose the source code change would look like this: (line 771 in
/lib/router.js
)With the proposed change, the 'parent' koa app would only have the responsibility of providing an array of valid domains:
I can currently accomplish similar results 2 separate ways, neither of which is as simple as I would like:
Workaround 1.
Build a dynamic RegExp from config at runtime, and assign to
Router.RouterOptions["host"]
- requires escaping period characters from domain
- The ECMA proposal to ease this is still in infancy: https://github.com/tc39/proposal-regex-escaping
- cumbersome to handle subdomains without using wildcards or accidental performance pitfall
- pseudo example of my usage:
Workaround 2.
Execute router middleware conditionally, only if domain matches manual check
- Described generically here: https://github.com/koajs/examples/blob/master/conditional-middleware/app.js
- here is pseudo example of my usage:
Checklist
The text was updated successfully, but these errors were encountered: