@midwayjs/koa 3.0.0-beta.9 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # midway for koa
2
2
 
3
- [![Package Quality](http://npm.packagequality.com/shield/midway-web.svg)](http://packagequality.com/#?package=midway-web)
3
+ [![Package Quality](http://npm.packagequality.com/shield/@midwayjs/koa.svg)](http://packagequality.com/#?package=@midwayjs/koa)
4
4
  [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://github.com/midwayjs/midway/pulls)
5
5
 
6
6
  this is a sub package for midway.
@@ -0,0 +1,51 @@
1
+ /**
2
+ * koa server options
3
+ */
4
+ export declare const koa: {};
5
+ /**
6
+ * The key that signing cookies. It can contain multiple keys seperated by `,`.
7
+ * @member {String} Config#keys
8
+ */
9
+ export declare const keys = "";
10
+ /**
11
+ * default cookie options
12
+ *
13
+ * @member Config#cookies
14
+ * @property {String} sameSite - SameSite property, defaults is ''
15
+ * @property {Boolean} httpOnly - httpOnly property, defaults is true
16
+ */
17
+ export declare const cookies: {};
18
+ export declare const onerror: {};
19
+ /**
20
+ * @member Config#bodyParser
21
+ * @property {Boolean} enable - enable bodyParser or not, default is true
22
+ * @property {String | RegExp | Function | Array} ignore - won't parse request body when url path hit ignore pattern, can not set `ignore` when `match` presented
23
+ * @property {String | RegExp | Function | Array} match - will parse request body only when url path hit match pattern
24
+ * @property {String} encoding - body's encoding type,default is utf8
25
+ * @property {String} formLimit - limit of the urlencoded body. If the body ends up being larger than this limit, a 413 error code is returned. Default is 1mb
26
+ * @property {String} jsonLimit - limit of the json body, default is 1mb
27
+ * @property {String} textLimit - limit of the text body, default is 1mb
28
+ * @property {Boolean} strict - when set to true, JSON parser will only accept arrays and objects. Default is true
29
+ * @property {Number} queryString.arrayLimit - urlencoded body array's max length, default is 100
30
+ * @property {Number} queryString.depth - urlencoded body object's max depth, default is 5
31
+ * @property {Number} queryString.parameterLimit - urlencoded body maximum parameters, default is 1000
32
+ */
33
+ export declare const bodyParser: {
34
+ enable: boolean;
35
+ encoding: string;
36
+ formLimit: string;
37
+ jsonLimit: string;
38
+ textLimit: string;
39
+ strict: boolean;
40
+ queryString: {
41
+ arrayLimit: number;
42
+ depth: number;
43
+ parameterLimit: number;
44
+ };
45
+ onerror(err: any): never;
46
+ };
47
+ export declare const siteFile: {
48
+ enable: boolean;
49
+ favicon: any;
50
+ };
51
+ //# sourceMappingURL=config.default.d.ts.map
@@ -0,0 +1,61 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.siteFile = exports.bodyParser = exports.onerror = exports.cookies = exports.keys = exports.koa = void 0;
4
+ /**
5
+ * koa server options
6
+ */
7
+ exports.koa = {};
8
+ /**
9
+ * The key that signing cookies. It can contain multiple keys seperated by `,`.
10
+ * @member {String} Config#keys
11
+ */
12
+ exports.keys = '';
13
+ /**
14
+ * default cookie options
15
+ *
16
+ * @member Config#cookies
17
+ * @property {String} sameSite - SameSite property, defaults is ''
18
+ * @property {Boolean} httpOnly - httpOnly property, defaults is true
19
+ */
20
+ exports.cookies = {
21
+ // httpOnly: true | false,
22
+ // sameSite: 'none|lax|strict',
23
+ };
24
+ exports.onerror = {};
25
+ /**
26
+ * @member Config#bodyParser
27
+ * @property {Boolean} enable - enable bodyParser or not, default is true
28
+ * @property {String | RegExp | Function | Array} ignore - won't parse request body when url path hit ignore pattern, can not set `ignore` when `match` presented
29
+ * @property {String | RegExp | Function | Array} match - will parse request body only when url path hit match pattern
30
+ * @property {String} encoding - body's encoding type,default is utf8
31
+ * @property {String} formLimit - limit of the urlencoded body. If the body ends up being larger than this limit, a 413 error code is returned. Default is 1mb
32
+ * @property {String} jsonLimit - limit of the json body, default is 1mb
33
+ * @property {String} textLimit - limit of the text body, default is 1mb
34
+ * @property {Boolean} strict - when set to true, JSON parser will only accept arrays and objects. Default is true
35
+ * @property {Number} queryString.arrayLimit - urlencoded body array's max length, default is 100
36
+ * @property {Number} queryString.depth - urlencoded body object's max depth, default is 5
37
+ * @property {Number} queryString.parameterLimit - urlencoded body maximum parameters, default is 1000
38
+ */
39
+ exports.bodyParser = {
40
+ enable: true,
41
+ encoding: 'utf8',
42
+ formLimit: '1mb',
43
+ jsonLimit: '1mb',
44
+ textLimit: '1mb',
45
+ strict: true,
46
+ // @see https://github.com/hapijs/qs/blob/master/lib/parse.js#L8 for more options
47
+ queryString: {
48
+ arrayLimit: 100,
49
+ depth: 5,
50
+ parameterLimit: 1000,
51
+ },
52
+ onerror(err) {
53
+ err.message += ', check bodyParser config';
54
+ throw err;
55
+ },
56
+ };
57
+ exports.siteFile = {
58
+ enable: true,
59
+ favicon: undefined,
60
+ };
61
+ //# sourceMappingURL=config.default.js.map
@@ -1,6 +1,9 @@
1
- import { MidwayDecoratorService } from '@midwayjs/core';
1
+ import { MidwayConfigService, MidwayDecoratorService } from '@midwayjs/core';
2
+ import { MidwayKoaFramework } from './framework';
2
3
  export declare class KoaConfiguration {
3
4
  decoratorService: MidwayDecoratorService;
5
+ koaFramework: MidwayKoaFramework;
6
+ configService: MidwayConfigService;
4
7
  init(): void;
5
8
  onReady(): Promise<void>;
6
9
  }
@@ -12,18 +12,34 @@ Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.KoaConfiguration = void 0;
13
13
  const decorator_1 = require("@midwayjs/decorator");
14
14
  const core_1 = require("@midwayjs/core");
15
+ const session = require("@midwayjs/session");
16
+ const framework_1 = require("./framework");
17
+ const DefaultConfig = require("./config/config.default");
18
+ const bodyparser_middleware_1 = require("./middleware/bodyparser.middleware");
19
+ const fav_middleware_1 = require("./middleware/fav.middleware");
15
20
  let KoaConfiguration = class KoaConfiguration {
16
21
  init() {
22
+ // register param decorator
17
23
  this.decoratorService.registerParameterHandler(decorator_1.WEB_ROUTER_PARAM_KEY, options => {
18
24
  return (0, core_1.extractKoaLikeValue)(options.metadata.type, options.metadata.propertyData, options.originParamType)(options.originArgs[0], options.originArgs[1]);
19
25
  });
20
26
  }
21
- async onReady() { }
27
+ async onReady() {
28
+ this.koaFramework.useMiddleware([fav_middleware_1.SiteFileMiddleware, bodyparser_middleware_1.BodyParserMiddleware]);
29
+ }
22
30
  };
23
31
  __decorate([
24
32
  (0, decorator_1.Inject)(),
25
33
  __metadata("design:type", core_1.MidwayDecoratorService)
26
34
  ], KoaConfiguration.prototype, "decoratorService", void 0);
35
+ __decorate([
36
+ (0, decorator_1.Inject)(),
37
+ __metadata("design:type", framework_1.MidwayKoaFramework)
38
+ ], KoaConfiguration.prototype, "koaFramework", void 0);
39
+ __decorate([
40
+ (0, decorator_1.Inject)(),
41
+ __metadata("design:type", core_1.MidwayConfigService)
42
+ ], KoaConfiguration.prototype, "configService", void 0);
27
43
  __decorate([
28
44
  (0, decorator_1.Init)(),
29
45
  __metadata("design:type", Function),
@@ -33,14 +49,10 @@ __decorate([
33
49
  KoaConfiguration = __decorate([
34
50
  (0, decorator_1.Configuration)({
35
51
  namespace: 'koa',
52
+ imports: [session],
36
53
  importConfigs: [
37
54
  {
38
- default: {
39
- koa: {
40
- keys: [],
41
- onerror: {},
42
- },
43
- },
55
+ default: DefaultConfig,
44
56
  },
45
57
  ],
46
58
  })
@@ -17,12 +17,13 @@ export declare class MidwayKoaFramework extends BaseFramework<IMidwayKoaApplicat
17
17
  * @deprecated
18
18
  * @param middlewareId
19
19
  */
20
- generateMiddleware(middlewareId: string): Promise<Middleware<DefaultState, IMidwayKoaContext, any>>;
20
+ generateMiddleware(middlewareId: any): Promise<Middleware<DefaultState, IMidwayKoaContext, any>>;
21
21
  run(): Promise<void>;
22
22
  beforeStop(): Promise<void>;
23
23
  getFrameworkType(): MidwayFrameworkType;
24
24
  getFrameworkName(): string;
25
25
  getServer(): Server;
26
+ getPort(): string;
26
27
  useMiddleware(Middleware: CommonMiddlewareUnion<IMidwayKoaContext, Next, unknown>): void;
27
28
  useFilter(Filter: CommonFilterUnion<IMidwayKoaContext, Next, unknown>): void;
28
29
  }
package/dist/framework.js CHANGED
@@ -8,16 +8,16 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
8
8
  Object.defineProperty(exports, "__esModule", { value: true });
9
9
  exports.MidwayKoaFramework = void 0;
10
10
  const core_1 = require("@midwayjs/core");
11
+ const cookies_1 = require("@midwayjs/cookies");
11
12
  const decorator_1 = require("@midwayjs/decorator");
12
13
  const Router = require("@koa/router");
13
14
  const koa = require("koa");
14
- const onerror = require("koa-onerror");
15
+ const onerror_1 = require("./onerror");
16
+ const COOKIES = Symbol('context#cookies');
15
17
  class KoaControllerGenerator extends core_1.WebControllerGenerator {
16
- constructor(app, applicationContext, logger) {
17
- super(applicationContext, core_1.MidwayFrameworkType.WEB_KOA, logger);
18
+ constructor(app) {
19
+ super(app);
18
20
  this.app = app;
19
- this.applicationContext = applicationContext;
20
- this.logger = logger;
21
21
  }
22
22
  createRouter(routerOptions) {
23
23
  const router = new Router(routerOptions);
@@ -33,22 +33,40 @@ let MidwayKoaFramework = class MidwayKoaFramework extends core_1.BaseFramework {
33
33
  return this.configService.getConfiguration('koa');
34
34
  }
35
35
  async applicationInitialize(options) {
36
- this.app = new koa();
37
- const appKeys = this.configurationOptions['keys'] ||
38
- this.configService.getConfiguration('keys');
39
- if (appKeys) {
40
- this.app.keys = appKeys;
36
+ const appKeys = this.configService.getConfiguration('keys') ||
37
+ this.configurationOptions['keys'];
38
+ if (!appKeys) {
39
+ throw new core_1.MidwayConfigMissingError('config.keys');
41
40
  }
42
- else {
43
- throw new core_1.MidwayConfigMissingError('koa.keys');
44
- }
45
- onerror(this.app, this.configurationOptions.onerror);
41
+ const cookieOptions = this.configService.getConfiguration('cookies');
42
+ this.app = new koa({
43
+ keys: [].concat(appKeys),
44
+ });
45
+ Object.defineProperty(this.app.context, 'cookies', {
46
+ get() {
47
+ if (!this[COOKIES]) {
48
+ this[COOKIES] = new cookies_1.Cookies(this, this.app.keys, cookieOptions);
49
+ }
50
+ return this[COOKIES];
51
+ },
52
+ enumerable: true,
53
+ });
54
+ const onerrorConfig = this.configService.getConfiguration('onerror');
55
+ (0, onerror_1.setupOnerror)(this.app, onerrorConfig, this.logger);
56
+ // not found middleware
57
+ const notFound = async (ctx, next) => {
58
+ await next();
59
+ if (!ctx._matchedRoute) {
60
+ throw new core_1.httpError.NotFoundError();
61
+ }
62
+ };
63
+ // root middleware
46
64
  const midwayRootMiddleware = async (ctx, next) => {
47
65
  this.app.createAnonymousContext(ctx);
48
- await (await this.getMiddleware())(ctx, next);
66
+ await (await this.applyMiddleware(notFound))(ctx, next);
49
67
  };
50
68
  this.app.use(midwayRootMiddleware);
51
- this.generator = new KoaControllerGenerator(this.app, this.applicationContext, this.appLogger);
69
+ this.generator = new KoaControllerGenerator(this.app);
52
70
  this.defineApplicationProperties();
53
71
  // hack use method
54
72
  this.app.originUse = this.app.use;
@@ -108,7 +126,7 @@ let MidwayKoaFramework = class MidwayKoaFramework extends core_1.BaseFramework {
108
126
  const customPort = (_a = process.env.MIDWAY_HTTP_PORT) !== null && _a !== void 0 ? _a : this.configurationOptions.port;
109
127
  if (customPort) {
110
128
  new Promise(resolve => {
111
- const args = [this.configurationOptions.port];
129
+ const args = [customPort];
112
130
  if (this.configurationOptions.hostname) {
113
131
  args.push(this.configurationOptions.hostname);
114
132
  }
@@ -132,6 +150,9 @@ let MidwayKoaFramework = class MidwayKoaFramework extends core_1.BaseFramework {
132
150
  getServer() {
133
151
  return this.server;
134
152
  }
153
+ getPort() {
154
+ return process.env.MIDWAY_HTTP_PORT;
155
+ }
135
156
  useMiddleware(Middleware) {
136
157
  this.middlewareManager.insertLast(Middleware);
137
158
  }
package/dist/index.d.ts CHANGED
@@ -2,4 +2,6 @@ export { MidwayKoaFramework as Framework } from './framework';
2
2
  export { MidwayKoaContextLogger } from './logger';
3
3
  export * from './interface';
4
4
  export { KoaConfiguration as Configuration } from './configuration';
5
+ export * from './middleware/fav.middleware';
6
+ export * from './middleware/bodyparser.middleware';
5
7
  //# sourceMappingURL=index.d.ts.map
package/dist/index.js CHANGED
@@ -18,4 +18,6 @@ Object.defineProperty(exports, "MidwayKoaContextLogger", { enumerable: true, get
18
18
  __exportStar(require("./interface"), exports);
19
19
  var configuration_1 = require("./configuration");
20
20
  Object.defineProperty(exports, "Configuration", { enumerable: true, get: function () { return configuration_1.KoaConfiguration; } });
21
+ __exportStar(require("./middleware/fav.middleware"), exports);
22
+ __exportStar(require("./middleware/bodyparser.middleware"), exports);
21
23
  //# sourceMappingURL=index.js.map
@@ -3,6 +3,7 @@ import { IConfigurationOptions, IMidwayApplication, IMidwayContext } from '@midw
3
3
  import * as koa from 'koa';
4
4
  import { Context as KoaContext, DefaultState, Middleware, Next } from 'koa';
5
5
  import { RouterParamValue } from '@midwayjs/decorator';
6
+ import { CookieSetOptions } from '@midwayjs/cookies';
6
7
  export declare type IMidwayKoaContext = IMidwayContext<KoaContext>;
7
8
  export declare type IMidwayKoaApplication = IMidwayApplication<IMidwayKoaContext, koa<DefaultState, IMidwayKoaContext> & {
8
9
  generateController(controllerMapping: string, routeArgsInfo?: RouterParamValue[], routerResponseData?: any[]): Middleware<DefaultState, IMidwayKoaContext>;
@@ -10,11 +11,18 @@ export declare type IMidwayKoaApplication = IMidwayApplication<IMidwayKoaContext
10
11
  * @deprecated
11
12
  * @param middlewareId
12
13
  */
13
- generateMiddleware(middlewareId: string): Promise<Middleware<DefaultState, IMidwayKoaContext>>;
14
+ generateMiddleware(middlewareId: any): Promise<Middleware<DefaultState, IMidwayKoaContext>>;
14
15
  }>;
16
+ /**
17
+ * @deprecated use NextFunction definition
18
+ */
15
19
  export declare type IMidwayKoaNext = Next;
16
20
  export declare type NextFunction = Next;
17
21
  export interface IMidwayKoaConfigurationOptions extends IConfigurationOptions {
22
+ /**
23
+ * cookies sign keys
24
+ */
25
+ keys?: string[];
18
26
  /**
19
27
  * application http port
20
28
  */
@@ -43,17 +51,6 @@ export interface IMidwayKoaConfigurationOptions extends IConfigurationOptions {
43
51
  * http global prefix
44
52
  */
45
53
  globalPrefix?: string;
46
- /**
47
- * onerror middleware options
48
- */
49
- onerror?: {
50
- text: (err: Error, ctx: IMidwayKoaContext) => void;
51
- json: (err: Error, ctx: IMidwayKoaContext) => void;
52
- html: (err: Error, ctx: IMidwayKoaContext) => void;
53
- redirect?: string;
54
- template?: string;
55
- accepts?: (...args: any[]) => any;
56
- };
57
54
  }
58
55
  export declare type MiddlewareParamArray = Array<Middleware<DefaultState, IMidwayKoaContext>>;
59
56
  export interface IWebMiddleware {
@@ -62,9 +59,81 @@ export interface IWebMiddleware {
62
59
  export declare type Application = IMidwayKoaApplication;
63
60
  export interface Context extends IMidwayKoaContext {
64
61
  }
62
+ interface BodyParserOptions {
63
+ enable?: boolean;
64
+ /**
65
+ * parser will only parse when request type hits enableTypes, default is ['json', 'form'].
66
+ */
67
+ enableTypes?: string[] | undefined;
68
+ /**
69
+ * requested encoding. Default is utf-8 by co-body
70
+ */
71
+ encode?: string | undefined;
72
+ /**
73
+ * limit of the urlencoded body. If the body ends up being larger than this limit
74
+ * a 413 error code is returned. Default is 56kb
75
+ */
76
+ formLimit?: string | undefined;
77
+ /**
78
+ * limit of the json body. Default is 1mb
79
+ */
80
+ jsonLimit?: string | undefined;
81
+ /**
82
+ * limit of the text body. Default is 1mb.
83
+ */
84
+ textLimit?: string | undefined;
85
+ /**
86
+ * limit of the xml body. Default is 1mb.
87
+ */
88
+ xmlLimit?: string | undefined;
89
+ /**
90
+ * when set to true, JSON parser will only accept arrays and objects. Default is true
91
+ */
92
+ strict?: boolean | undefined;
93
+ /**
94
+ * custom json request detect function. Default is null
95
+ */
96
+ detectJSON?: ((ctx: IMidwayKoaContext) => boolean) | undefined;
97
+ /**
98
+ * support extend types
99
+ */
100
+ extendTypes?: {
101
+ json?: string[] | undefined;
102
+ form?: string[] | undefined;
103
+ text?: string[] | undefined;
104
+ } | undefined;
105
+ /**
106
+ * support custom error handle
107
+ */
108
+ onerror?: ((err: Error, ctx: IMidwayKoaContext) => void) | undefined;
109
+ }
65
110
  declare module '@midwayjs/core/dist/interface' {
66
111
  interface MidwayConfig {
112
+ keys?: string | string[];
67
113
  koa?: IMidwayKoaConfigurationOptions;
114
+ cookies?: CookieSetOptions;
115
+ /**
116
+ * onerror middleware options
117
+ */
118
+ onerror?: {
119
+ text?: (err: Error, ctx: IMidwayKoaContext) => void;
120
+ json?: (err: Error, ctx: IMidwayKoaContext) => void;
121
+ html?: (err: Error, ctx: IMidwayKoaContext) => void;
122
+ redirect?: string;
123
+ accepts?: (...args: any[]) => any;
124
+ };
125
+ bodyParser?: BodyParserOptions;
126
+ siteFile?: {
127
+ enable?: boolean;
128
+ favicon?: undefined | string | Buffer;
129
+ };
130
+ }
131
+ }
132
+ declare module 'koa' {
133
+ interface Request {
134
+ body?: any;
135
+ rawBody: string;
68
136
  }
69
137
  }
138
+ export {};
70
139
  //# sourceMappingURL=interface.d.ts.map
@@ -0,0 +1,6 @@
1
+ export declare class BodyParserMiddleware {
2
+ bodyparserConfig: any;
3
+ resolve(): any;
4
+ static getName(): string;
5
+ }
6
+ //# sourceMappingURL=bodyparser.middleware.d.ts.map
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.BodyParserMiddleware = void 0;
13
+ const koaBodyParser = require("koa-bodyparser");
14
+ const decorator_1 = require("@midwayjs/decorator");
15
+ let BodyParserMiddleware = class BodyParserMiddleware {
16
+ resolve() {
17
+ // use bodyparser middleware
18
+ if (this.bodyparserConfig.enable) {
19
+ return koaBodyParser(this.bodyparserConfig);
20
+ }
21
+ }
22
+ static getName() {
23
+ return 'bodyParser';
24
+ }
25
+ };
26
+ __decorate([
27
+ (0, decorator_1.Config)('bodyParser'),
28
+ __metadata("design:type", Object)
29
+ ], BodyParserMiddleware.prototype, "bodyparserConfig", void 0);
30
+ BodyParserMiddleware = __decorate([
31
+ (0, decorator_1.Middleware)()
32
+ ], BodyParserMiddleware);
33
+ exports.BodyParserMiddleware = BodyParserMiddleware;
34
+ //# sourceMappingURL=bodyparser.middleware.js.map
@@ -0,0 +1,6 @@
1
+ export declare class SiteFileMiddleware {
2
+ siteFileConfig: any;
3
+ resolve(): (ctx: any, next: any) => Promise<any>;
4
+ static getName(): string;
5
+ }
6
+ //# sourceMappingURL=fav.middleware.d.ts.map
@@ -0,0 +1,62 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.SiteFileMiddleware = void 0;
13
+ const decorator_1 = require("@midwayjs/decorator");
14
+ const path = require('path');
15
+ const MAX_AGE = 'public, max-age=2592000'; // 30 days
16
+ let SiteFileMiddleware = class SiteFileMiddleware {
17
+ resolve() {
18
+ // use bodyparser middleware
19
+ if (this.siteFileConfig.enable) {
20
+ return async (ctx, next) => {
21
+ if (ctx.method !== 'HEAD' && ctx.method !== 'GET')
22
+ return next();
23
+ /* istanbul ignore if */
24
+ if (ctx.path[0] !== '/')
25
+ return next();
26
+ if (ctx.path !== '/favicon.ico') {
27
+ return next();
28
+ }
29
+ let content = this.siteFileConfig['favicon'];
30
+ if (content === undefined) {
31
+ content = Buffer.from('');
32
+ }
33
+ if (!content)
34
+ return next();
35
+ // content is url
36
+ if (typeof content === 'string')
37
+ return ctx.redirect(content);
38
+ // '/robots.txt': Buffer <xx..
39
+ // content is buffer
40
+ if (Buffer.isBuffer(content)) {
41
+ ctx.set('cache-control', MAX_AGE);
42
+ ctx.body = content;
43
+ ctx.type = path.extname(ctx.path);
44
+ return;
45
+ }
46
+ return next();
47
+ };
48
+ }
49
+ }
50
+ static getName() {
51
+ return 'siteFile';
52
+ }
53
+ };
54
+ __decorate([
55
+ (0, decorator_1.Config)('siteFile'),
56
+ __metadata("design:type", Object)
57
+ ], SiteFileMiddleware.prototype, "siteFileConfig", void 0);
58
+ SiteFileMiddleware = __decorate([
59
+ (0, decorator_1.Middleware)()
60
+ ], SiteFileMiddleware);
61
+ exports.SiteFileMiddleware = SiteFileMiddleware;
62
+ //# sourceMappingURL=fav.middleware.js.map
@@ -0,0 +1,2 @@
1
+ export declare function setupOnerror(app: any, config: any, logger: any): void;
2
+ //# sourceMappingURL=onerror.d.ts.map