-
Notifications
You must be signed in to change notification settings - Fork 0
/
koa-vhost.js
30 lines (27 loc) · 1 KB
/
koa-vhost.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
function assert(value, err) {
if (Boolean(value) === false) {
throw err;
}
}
function createComparison(vhosts) {
if (typeof vhosts === 'function') {
return vhosts;
} else if (Array.isArray(vhosts)) {
vhosts = vhosts.map(hostname => `${hostname}`);
return ({ hostname }) => vhosts.includes(hostname);
} else if (Object.prototype.toString.call(vhosts) === '[object RegExp]') {
return ({ hostname }) => vhosts.test(hostname);
} else if (typeof vhosts === 'string') {
return ({ hostname }) => vhosts === hostname;
} else {
return null;
}
}
module.exports = function createVhosts(vhosts, middleware) {
const compare = createComparison(vhosts);
assert(typeof compare === 'function', new TypeError('Expected vhosts to be a: function | Array | RegExp | string'));
assert(typeof middleware === 'function', new TypeError('Expected vhost middleware to be a function'));
return async function vhost(ctx, next) {
return (await compare(ctx)) ? middleware(ctx, next) : next();
};
};