Skip to content

Commit 5e7bdbb

Browse files
committed
refactor: use hostList internal
1 parent 0378f16 commit 5e7bdbb

File tree

9 files changed

+43
-49
lines changed

9 files changed

+43
-49
lines changed

core/controller-decorator/src/decorator/http/Host.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,21 @@ import assert from 'assert';
55
import { HostType } from '../../model';
66

77
export function Host(host: HostType) {
8+
9+
function parseHost(): string[] {
10+
return Array.isArray(host) ? host : [ host ];
11+
}
12+
813
function classHost(constructor: EggProtoImplClass) {
9-
ControllerInfoUtil.addControllerHost(host, constructor);
14+
ControllerInfoUtil.addControllerHosts(parseHost(), constructor);
1015
}
1116

1217
function methodHost(target: any, propertyKey: PropertyKey) {
1318
assert(typeof propertyKey === 'string',
1419
`[controller/${target.name}] expect method name be typeof string, but now is ${String(propertyKey)}`);
1520
const controllerClazz = target.constructor as EggProtoImplClass;
1621
const methodName = propertyKey as string;
17-
18-
MethodInfoUtil.setMethodHost(host, controllerClazz, methodName);
22+
MethodInfoUtil.setMethodHosts(parseHost(), controllerClazz, methodName);
1923
}
2024

2125
return function(target: any, propertyKey?: PropertyKey) {

core/controller-decorator/src/impl/http/HTTPControllerMetaBuilder.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ export class HTTPControllerMetaBuilder {
4343
const protoName = property!.name as string;
4444
const needAcl = ControllerInfoUtil.hasControllerAcl(this.clazz);
4545
const aclCode = ControllerInfoUtil.getControllerAcl(this.clazz);
46-
const host = ControllerInfoUtil.getControllerHost(this.clazz);
46+
const hosts = ControllerInfoUtil.getControllerHosts(this.clazz);
4747
const metadata = new HTTPControllerMeta(
48-
clazzName, protoName, controllerName, httpPath, httpMiddlewares, methods, needAcl, aclCode, host);
48+
clazzName, protoName, controllerName, httpPath, httpMiddlewares, methods, needAcl, aclCode, hosts);
4949
ControllerMetadataUtil.setControllerMetadata(this.clazz, metadata);
5050
for (const method of metadata.methods) {
5151
const realPath = metadata.getMethodRealPath(method);

core/controller-decorator/src/impl/http/HTTPControllerMethodMetaBuilder.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,13 @@ export class HTTPControllerMethodMetaBuilder {
105105
const middlewares = MethodInfoUtil.getMethodMiddlewares(this.clazz, this.methodName);
106106
const needAcl = MethodInfoUtil.hasMethodAcl(this.clazz, this.methodName);
107107
const aclCode = MethodInfoUtil.getMethodAcl(this.clazz, this.methodName);
108-
const host = MethodInfoUtil.getMethodHost(this.clazz, this.methodName);
108+
const hosts = MethodInfoUtil.getMethodHosts(this.clazz, this.methodName);
109109
const realPath = parentPath
110110
? path.posix.join(parentPath, httpPath)
111111
: httpPath;
112112
const paramTypeMap = this.buildParamType(realPath);
113113
const priority = this.getPriority();
114114
return new HTTPMethodMeta(
115-
this.methodName, httpPath!, httpMethod!, middlewares, contextIndex, paramTypeMap, priority, needAcl, aclCode, host);
115+
this.methodName, httpPath!, httpMethod!, middlewares, contextIndex, paramTypeMap, priority, needAcl, aclCode, hosts);
116116
}
117117
}

core/controller-decorator/src/model/HTTPControllerMeta.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import path from 'path';
22
import { ControllerMetadata } from './ControllerMetadata';
3-
import { ControllerType, HostType, MiddlewareFunc } from './types';
3+
import { ControllerType, MiddlewareFunc } from './types';
44
import { HTTPMethodMeta } from './HTTPMethodMeta';
55
import { EggPrototypeName } from '@eggjs/core-decorator';
66

@@ -14,7 +14,7 @@ export class HTTPControllerMeta implements ControllerMetadata {
1414
public readonly methods: readonly HTTPMethodMeta[];
1515
public readonly needAcl: boolean;
1616
public readonly aclCode?: string;
17-
public readonly host?: HostType;
17+
public readonly hosts?: string[];
1818

1919
constructor(
2020
className: string,
@@ -25,7 +25,7 @@ export class HTTPControllerMeta implements ControllerMetadata {
2525
methods: HTTPMethodMeta[],
2626
needAcl: boolean,
2727
aclCode: string | undefined,
28-
host: HostType | undefined,
28+
hosts: string[] | undefined,
2929
) {
3030
this.protoName = protoName;
3131
this.controllerName = controllerName;
@@ -35,7 +35,7 @@ export class HTTPControllerMeta implements ControllerMetadata {
3535
this.methods = methods;
3636
this.needAcl = needAcl;
3737
this.aclCode = aclCode;
38-
this.host = host;
38+
this.host = hosts;
3939
}
4040

4141
getMethodRealPath(method: HTTPMethodMeta) {
@@ -45,11 +45,11 @@ export class HTTPControllerMeta implements ControllerMetadata {
4545
return method.path;
4646
}
4747

48-
getMethodHost(method: HTTPMethodMeta): HostType | undefined {
49-
if (this.host) {
50-
return this.host;
48+
getMethodHosts(method: HTTPMethodMeta): string[] | undefined {
49+
if (this.hosts) {
50+
return this.hosts;
5151
}
52-
return method.host;
52+
return method.hosts;
5353
}
5454

5555
getMethodName(method: HTTPMethodMeta) {

core/controller-decorator/src/model/HTTPMethodMeta.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import assert from 'assert';
22
import pathToRegexp from 'path-to-regexp';
33
import { MethodMeta } from './MethodMeta';
4-
import { HostType, HTTPMethodEnum, HTTPParamType, MiddlewareFunc } from './types';
4+
import { HTTPMethodEnum, HTTPParamType, MiddlewareFunc } from './types';
55

66
export abstract class ParamMeta {
77
type: HTTPParamType;
@@ -73,7 +73,7 @@ export class HTTPMethodMeta implements MethodMeta {
7373
public readonly priority: number;
7474
public readonly needAcL: boolean;
7575
public readonly aclCode: string | undefined;
76-
public readonly host: HostType | undefined;
76+
public readonly hosts: string[] | undefined;
7777

7878
constructor(
7979
name: string,
@@ -85,7 +85,7 @@ export class HTTPMethodMeta implements MethodMeta {
8585
priority: number,
8686
needAcl: boolean,
8787
aclCode: string | undefined,
88-
host: HostType | undefined,
88+
hosts: string[] | undefined,
8989
) {
9090
this.name = name;
9191
this.path = path;
@@ -96,7 +96,7 @@ export class HTTPMethodMeta implements MethodMeta {
9696
this.priority = priority;
9797
this.needAcL = needAcl;
9898
this.aclCode = aclCode;
99-
this.host = host;
99+
this.hosts = hosts;
100100
}
101101
}
102102

core/controller-decorator/src/util/ControllerInfoUtil.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ControllerTypeLike, HostType, MiddlewareFunc } from '../model';
1+
import { ControllerTypeLike, MiddlewareFunc } from '../model';
22
import { EggProtoImplClass, MetadataUtil } from '@eggjs/core-decorator';
33

44
export const CONTROLLER_TYPE = Symbol.for('EggPrototype#controllerType');
@@ -45,11 +45,11 @@ export default class ControllerInfoUtil {
4545
return MetadataUtil.getMetaData(CONTROLLER_ACL, clazz);
4646
}
4747

48-
static addControllerHost(host: HostType, clazz: EggProtoImplClass) {
49-
MetadataUtil.defineMetaData(CONTROLLER_HOST, host, clazz);
48+
static addControllerHosts(hosts: string[], clazz: EggProtoImplClass) {
49+
MetadataUtil.defineMetaData(CONTROLLER_HOST, hosts, clazz);
5050
}
5151

52-
static getControllerHost(clazz: EggProtoImplClass): string | undefined {
52+
static getControllerHosts(clazz: EggProtoImplClass): string[] | undefined {
5353
return MetadataUtil.getMetaData(CONTROLLER_HOST, clazz);
5454
}
5555
}

core/controller-decorator/src/util/MethodInfoUtil.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { EggProtoImplClass, MetadataUtil } from '@eggjs/core-decorator';
2-
import { ControllerTypeLike, HostType, MiddlewareFunc } from '../model';
2+
import { ControllerTypeLike, MiddlewareFunc } from '../model';
33
import { MapUtil } from '@eggjs/tegg-common-util';
44

55
const METHOD_CONTROLLER_TYPE_MAP = Symbol.for('EggPrototype#controller#mthods');
@@ -8,7 +8,7 @@ const METHOD_CONTEXT_INDEX = Symbol.for('EggPrototype#controller#method#context'
88
const METHOD_MIDDLEWARES = Symbol.for('EggPrototype#method#middlewares');
99
const METHOD_ACL = Symbol.for('EggPrototype#method#acl');
1010

11-
type METHOD_MAP = Map<string, ControllerTypeLike | HostType>;
11+
type METHOD_MAP = Map<string, ControllerTypeLike | string[]>;
1212
type MethodContextIndexMap = Map<string, number>;
1313
type MethodMiddlewareMap = Map<string, MiddlewareFunc[]>;
1414
type MethodAclMap = Map<string, string | undefined>;
@@ -60,13 +60,13 @@ export default class MethodInfoUtil {
6060
return methodAclMap?.get(methodName);
6161
}
6262

63-
static setMethodHost(host: HostType, clazz: EggProtoImplClass, methodName: string) {
63+
static setMethodHosts(hosts: string[], clazz: EggProtoImplClass, methodName: string) {
6464
const methodControllerMap: METHOD_MAP = MetadataUtil.initOwnMapMetaData(METHOD_CONTROLLER_HOST, clazz, new Map());
65-
methodControllerMap.set(methodName, host);
65+
methodControllerMap.set(methodName, hosts);
6666
}
6767

68-
static getMethodHost(clazz: EggProtoImplClass, methodName: string): HostType | undefined {
68+
static getMethodHosts(clazz: EggProtoImplClass, methodName: string): string[] | undefined {
6969
const methodControllerMap: METHOD_MAP | undefined = MetadataUtil.getMetaData(METHOD_CONTROLLER_HOST, clazz);
70-
return methodControllerMap?.get(methodName) as HostType | undefined;
70+
return methodControllerMap?.get(methodName) as string[] | undefined;
7171
}
7272
}

core/controller-decorator/test/http/Host.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ import MethodInfoUtil from '../../src/util/MethodInfoUtil';
55

66
describe('test/Host.test.ts', () => {
77
it('controller Host work', () => {
8-
const controllerHost = ControllerInfoUtil.getControllerHost(HostController);
9-
assert(controllerHost === 'foo.eggjs.com');
8+
const controllerHost = ControllerInfoUtil.getControllerHosts(HostController);
9+
assert(controllerHost![0] === 'foo.eggjs.com');
1010
});
1111

1212
it('method Host work', () => {
13-
const methodHost = MethodInfoUtil.getMethodHost(HostController, 'bar');
14-
assert(methodHost === 'bar.eggjs.com');
13+
const methodHost = MethodInfoUtil.getMethodHosts(HostController, 'bar');
14+
assert(methodHost![0] === 'bar.eggjs.com');
1515
});
1616
});

plugin/controller/lib/impl/http/HTTPMethodRegister.ts

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import KoaRouter from 'koa-router';
33
import { Context } from 'egg';
44
import {
55
EggContext,
6-
HostType,
76
HTTPControllerMeta,
87
HTTPMethodMeta,
98
HTTPParamType,
@@ -50,22 +49,15 @@ export class HTTPMethodRegister {
5049
this.eggContainerFactory = eggContainerFactory;
5150
}
5251

53-
private hostMatch(host: HostType | undefined, target: string) {
54-
if (Array.isArray(host)) {
55-
return host.includes(target);
56-
}
57-
return host === target;
58-
}
59-
60-
private createHandler(methodMeta: HTTPMethodMeta, host: HostType | undefined) {
52+
private createHandler(methodMeta: HTTPMethodMeta, host: string | undefined) {
6153
const argsLength = methodMeta.paramMap.size;
6254
const hasContext = methodMeta.contextParamIndex !== undefined;
6355
const contextIndex = methodMeta.contextParamIndex;
6456
const methodArgsLength = argsLength + (hasContext ? 1 : 0);
6557
const self = this;
6658
return async function(ctx: Context, next: Next) {
6759
// if hosts is not empty and host is not matched, not execute
68-
if (host && !self.hostMatch(host, ctx.host)) {
60+
if (host && host !== ctx.host) {
6961
return await next();
7062
}
7163
// HTTP decorator core implement
@@ -126,9 +118,8 @@ export class HTTPMethodRegister {
126118

127119
// 2. check duplicate with host tegg controller
128120
let hostRouter;
129-
const host = this.controllerMeta.getMethodHost(this.methodMeta);
130-
const hostList = Array.isArray(host) ? host : [ host ];
131-
hostList.forEach(h => {
121+
const hosts = this.controllerMeta.getMethodHosts(this.methodMeta) || [];
122+
hosts.forEach(h => {
132123
if (h) {
133124
hostRouter = this.checkRouters.get(h);
134125
if (!hostRouter) {
@@ -170,9 +161,8 @@ export class HTTPMethodRegister {
170161
if (aclMiddleware) {
171162
methodMiddlewares.push(aclMiddleware);
172163
}
173-
const host = this.controllerMeta.getMethodHost(this.methodMeta);
174-
const hostList = Array.isArray(host) ? host : [ host ];
175-
hostList.forEach(h => {
164+
const hosts = this.controllerMeta.getMethodHosts(this.methodMeta) || [ undefined ];
165+
hosts.forEach(h => {
176166
const handler = this.createHandler(this.methodMeta, h);
177167
Reflect.apply(routerFunc, this.router,
178168
[ methodName, methodRealPath, ...methodMiddlewares, handler ]);

0 commit comments

Comments
 (0)