@midwayjs/koa 3.0.1 → 3.0.2
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/dist/config/config.default.js.map +1 -0
- package/dist/configuration.js.map +1 -0
- package/dist/framework.js.map +1 -0
- package/dist/index.js.map +1 -0
- package/dist/interface.js.map +1 -0
- package/dist/logger.js.map +1 -0
- package/dist/middleware/bodyparser.middleware.js.map +1 -0
- package/dist/middleware/fav.middleware.js.map +1 -0
- package/dist/onerror.js +1 -0
- package/dist/onerror.js.map +1 -0
- package/dist/utils.d.ts +1 -1
- package/dist/utils.js +7 -0
- package/dist/utils.js.map +1 -0
- package/package.json +8 -7
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.default.js","sourceRoot":"","sources":["../../src/config/config.default.ts"],"names":[],"mappings":";;;AAAA;;GAEG;AACU,QAAA,GAAG,GAAG,EAAE,CAAC;AAEtB;;;GAGG;AACU,QAAA,IAAI,GAAG,EAAE,CAAC;AAEvB;;;;;;GAMG;AACU,QAAA,OAAO,GAAG;AACrB,0BAA0B;AAC1B,+BAA+B;CAChC,CAAC;AAEW,QAAA,OAAO,GAAG,EAAE,CAAC;AAE1B;;;;;;;;;;;;;GAaG;AACU,QAAA,UAAU,GAAG;IACxB,MAAM,EAAE,IAAI;IACZ,QAAQ,EAAE,MAAM;IAChB,SAAS,EAAE,KAAK;IAChB,SAAS,EAAE,KAAK;IAChB,SAAS,EAAE,KAAK;IAChB,MAAM,EAAE,IAAI;IACZ,iFAAiF;IACjF,WAAW,EAAE;QACX,UAAU,EAAE,GAAG;QACf,KAAK,EAAE,CAAC;QACR,cAAc,EAAE,IAAI;KACrB;IACD,OAAO,CAAC,GAAG;QACT,GAAG,CAAC,OAAO,IAAI,2BAA2B,CAAC;QAC3C,MAAM,GAAG,CAAC;IACZ,CAAC;CACF,CAAC;AAEW,QAAA,QAAQ,GAAG;IACtB,MAAM,EAAE,IAAI;IACZ,OAAO,EAAE,SAAS;CACnB,CAAC","sourcesContent":["/**\n * koa server options\n */\nexport const koa = {};\n\n/**\n * The key that signing cookies. It can contain multiple keys seperated by `,`.\n * @member {String} Config#keys\n */\nexport const keys = '';\n\n/**\n * default cookie options\n *\n * @member Config#cookies\n * @property {String} sameSite - SameSite property, defaults is ''\n * @property {Boolean} httpOnly - httpOnly property, defaults is true\n */\nexport const cookies = {\n // httpOnly: true | false,\n // sameSite: 'none|lax|strict',\n};\n\nexport const onerror = {};\n\n/**\n * @member Config#bodyParser\n * @property {Boolean} enable - enable bodyParser or not, default is true\n * @property {String | RegExp | Function | Array} ignore - won't parse request body when url path hit ignore pattern, can not set `ignore` when `match` presented\n * @property {String | RegExp | Function | Array} match - will parse request body only when url path hit match pattern\n * @property {String} encoding - body's encoding type,default is utf8\n * @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\n * @property {String} jsonLimit - limit of the json body, default is 1mb\n * @property {String} textLimit - limit of the text body, default is 1mb\n * @property {Boolean} strict - when set to true, JSON parser will only accept arrays and objects. Default is true\n * @property {Number} queryString.arrayLimit - urlencoded body array's max length, default is 100\n * @property {Number} queryString.depth - urlencoded body object's max depth, default is 5\n * @property {Number} queryString.parameterLimit - urlencoded body maximum parameters, default is 1000\n */\nexport const bodyParser = {\n enable: true,\n encoding: 'utf8',\n formLimit: '1mb',\n jsonLimit: '1mb',\n textLimit: '1mb',\n strict: true,\n // @see https://github.com/hapijs/qs/blob/master/lib/parse.js#L8 for more options\n queryString: {\n arrayLimit: 100,\n depth: 5,\n parameterLimit: 1000,\n },\n onerror(err) {\n err.message += ', check bodyParser config';\n throw err;\n },\n};\n\nexport const siteFile = {\n enable: true,\n favicon: undefined,\n};\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"configuration.js","sourceRoot":"","sources":["../src/configuration.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,mDAK6B;AAC7B,yCAIwB;AACxB,6CAA6C;AAC7C,2CAAiD;AACjD,yDAAyD;AACzD,8EAA0E;AAC1E,gEAAiE;AAWjE,IAAa,gBAAgB,GAA7B,MAAa,gBAAgB;IAW3B,IAAI;QACF,2BAA2B;QAC3B,IAAI,CAAC,gBAAgB,CAAC,wBAAwB,CAC5C,gCAAoB,EACpB,OAAO,CAAC,EAAE;YACR,OAAO,IAAA,0BAAmB,EACxB,OAAO,CAAC,QAAQ,CAAC,IAAI,EACrB,OAAO,CAAC,QAAQ,CAAC,YAAY,EAC7B,OAAO,CAAC,eAAe,CACxB,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC,CAAC,EAAE,OAAO,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC;QAClD,CAAC,CACF,CAAC;IACJ,CAAC;IAED,KAAK,CAAC,OAAO;QACX,IAAI,CAAC,YAAY,CAAC,aAAa,CAAC,CAAC,mCAAkB,EAAE,4CAAoB,CAAC,CAAC,CAAC;IAC9E,CAAC;CACF,CAAA;AA1BC;IADC,IAAA,kBAAM,GAAE;8BACS,6BAAsB;0DAAC;AAGzC;IADC,IAAA,kBAAM,GAAE;8BACK,8BAAkB;sDAAC;AAGjC;IADC,IAAA,kBAAM,GAAE;8BACM,0BAAmB;uDAAC;AAGnC;IADC,IAAA,gBAAI,GAAE;;;;4CAaN;AAvBU,gBAAgB;IAT5B,IAAA,yBAAa,EAAC;QACb,SAAS,EAAE,KAAK;QAChB,OAAO,EAAE,CAAC,OAAO,CAAC;QAClB,aAAa,EAAE;YACb;gBACE,OAAO,EAAE,aAAa;aACvB;SACF;KACF,CAAC;GACW,gBAAgB,CA4B5B;AA5BY,4CAAgB","sourcesContent":["import {\n Configuration,\n Init,\n Inject,\n WEB_ROUTER_PARAM_KEY,\n} from '@midwayjs/decorator';\nimport {\n extractKoaLikeValue,\n MidwayConfigService,\n MidwayDecoratorService,\n} from '@midwayjs/core';\nimport * as session from '@midwayjs/session';\nimport { MidwayKoaFramework } from './framework';\nimport * as DefaultConfig from './config/config.default';\nimport { BodyParserMiddleware } from './middleware/bodyparser.middleware';\nimport { SiteFileMiddleware } from './middleware/fav.middleware';\n\n@Configuration({\n namespace: 'koa',\n imports: [session],\n importConfigs: [\n {\n default: DefaultConfig,\n },\n ],\n})\nexport class KoaConfiguration {\n @Inject()\n decoratorService: MidwayDecoratorService;\n\n @Inject()\n koaFramework: MidwayKoaFramework;\n\n @Inject()\n configService: MidwayConfigService;\n\n @Init()\n init() {\n // register param decorator\n this.decoratorService.registerParameterHandler(\n WEB_ROUTER_PARAM_KEY,\n options => {\n return extractKoaLikeValue(\n options.metadata.type,\n options.metadata.propertyData,\n options.originParamType\n )(options.originArgs[0], options.originArgs[1]);\n }\n );\n }\n\n async onReady() {\n this.koaFramework.useMiddleware([SiteFileMiddleware, BodyParserMiddleware]);\n }\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"framework.js","sourceRoot":"","sources":["../src/framework.ts"],"names":[],"mappings":";;;;;;;;;AAAA,yCAYwB;AACxB,+CAA4C;AAE5C,mDAAgD;AAOhD,sCAAsC;AAEtC,2BAA2B;AAE3B,uCAAyC;AAEzC,MAAM,OAAO,GAAG,MAAM,CAAC,iBAAiB,CAAC,CAAC;AAE1C,MAAM,sBAAuB,SAAQ,6BAA8B;IACjE,YAAqB,GAAG;QACtB,KAAK,CAAC,GAAG,CAAC,CAAC;QADQ,QAAG,GAAH,GAAG,CAAA;IAExB,CAAC;IAED,YAAY,CAAC,aAAkB;QAC7B,MAAM,MAAM,GAAG,IAAI,MAAM,CAAC,aAAa,CAAC,CAAC;QACzC,MAAM,CAAC,MAAM,CAAC,aAAa,CAAC,MAAM,CAAC,CAAC;QACpC,OAAO,MAAM,CAAC;IAChB,CAAC;IAED,kBAAkB,CAAC,SAAqB;QACtC,OAAO,IAAI,CAAC,qBAAqB,CAAC,SAAS,CAAC,CAAC;IAC/C,CAAC;CACF;AAGD,IAAa,kBAAkB,GAA/B,MAAa,kBAAmB,S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M,gBAAgB;QACrB,OAAO,SAAS,CAAC;IACnB,CAAC;IAEM,SAAS;QACd,OAAO,IAAI,CAAC,MAAM,CAAC;IACrB,CAAC;IAEM,OAAO;QACZ,OAAO,OAAO,CAAC,GAAG,CAAC,gBAAgB,CAAC;IACtC,CAAC;IAEM,aAAa,CAClB,UAAmE;QAEnE,IAAI,CAAC,iBAAiB,CAAC,UAAU,CAAC,UAAU,CAAC,CAAC;IAChD,CAAC;IAEM,SAAS,CACd,MAA2D;QAE3D,IAAI,CAAC,aAAa,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC;IACvC,CAAC;CACF,CAAA;AA3LY,kBAAkB;IAD9B,IAAA,qBAAS,GAAE;GACC,kBAAkB,CA2L9B;AA3LY,gDAAkB","sourcesContent":["import {\n BaseFramework,\n CommonFilterUnion,\n CommonMiddlewareUnion,\n HTTP_SERVER_KEY,\n IMidwayBootstrapOptions,\n MidwayFrameworkType,\n PathFileUtil,\n RouterInfo,\n WebControllerGenerator,\n MidwayConfigMissingError,\n httpError,\n} from '@midwayjs/core';\nimport { Cookies } from '@midwayjs/cookies';\n\nimport { Framework } from '@midwayjs/decorator';\nimport {\n IMidwayKoaApplication,\n IMidwayKoaConfigurationOptions,\n IMidwayKoaContext,\n IWebMiddleware,\n} from './interface';\nimport * as Router from '@koa/router';\nimport type { DefaultState, Middleware, Next } from 'koa';\nimport * as koa from 'koa';\nimport { Server } from 'net';\nimport { setupOnerror } from './onerror';\n\nconst COOKIES = Symbol('context#cookies');\n\nclass KoaControllerGenerator extends WebControllerGenerator<Router> {\n constructor(readonly app) {\n super(app);\n }\n\n createRouter(routerOptions: any): Router {\n const router = new Router(routerOptions);\n router.prefix(routerOptions.prefix);\n return router;\n }\n\n generateController(routeInfo: RouterInfo) {\n return this.generateKoaController(routeInfo);\n }\n}\n\n@Framework()\nexport class MidwayKoaFramework extends BaseFramework<\n IMidwayKoaApplication,\n IMidwayKoaContext,\n IMidwayKoaConfigurationOptions,\n Next\n> {\n private server: Server;\n private generator: KoaControllerGenerator;\n\n configure(): IMidwayKoaConfigurationOptions {\n return this.configService.getConfiguration('koa');\n }\n\n async applicationInitialize(options: Partial<IMidwayBootstrapOptions>) {\n const appKeys =\n this.configService.getConfiguration('keys') ||\n this.configurationOptions['keys'];\n if (!appKeys) {\n throw new MidwayConfigMissingError('config.keys');\n }\n\n const cookieOptions = this.configService.getConfiguration('cookies');\n\n this.app = new koa<DefaultState, IMidwayKoaContext>({\n keys: [].concat(appKeys),\n }) as IMidwayKoaApplication;\n\n Object.defineProperty(this.app.context, 'cookies', {\n get() {\n if (!this[COOKIES]) {\n this[COOKIES] = new Cookies(this, this.app.keys, cookieOptions);\n }\n return this[COOKIES];\n },\n enumerable: true,\n });\n\n const onerrorConfig = this.configService.getConfiguration('onerror');\n setupOnerror(this.app, onerrorConfig, this.logger);\n\n // not found middleware\n const notFound = async (ctx, next) => {\n await next();\n if (!ctx._matchedRoute) {\n throw new httpError.NotFoundError();\n }\n };\n\n // root middleware\n const midwayRootMiddleware = async (ctx, next) => {\n this.app.createAnonymousContext(ctx);\n await (\n await this.applyMiddleware(notFound)\n )(ctx, next);\n };\n this.app.use(midwayRootMiddleware);\n\n this.generator = new KoaControllerGenerator(this.app);\n\n this.defineApplicationProperties();\n\n // hack use method\n (this.app as any).originUse = this.app.use;\n this.app.use = this.app.useMiddleware as any;\n }\n\n async loadMidwayController() {\n await this.generator.loadMidwayController(\n this.configurationOptions.globalPrefix,\n newRouter => {\n const dispatchFn = newRouter.middleware();\n dispatchFn._name = `midwayController(${\n newRouter?.opts?.prefix || '/'\n })`;\n this.app.use(dispatchFn);\n }\n );\n }\n\n /**\n * wrap controller string to middleware function\n */\n public generateController(\n routeInfo: RouterInfo\n ): Middleware<DefaultState, IMidwayKoaContext> {\n return this.generator.generateKoaController(routeInfo);\n }\n\n /**\n * @deprecated\n * @param middlewareId\n */\n public async generateMiddleware(middlewareId: any) {\n const mwIns = await this.getApplicationContext().getAsync<IWebMiddleware>(\n middlewareId\n );\n return mwIns.resolve();\n }\n\n public async run(): Promise<void> {\n // load controller\n await this.loadMidwayController();\n // restore use method\n this.app.use = (this.app as any).originUse;\n\n // https config\n if (this.configurationOptions.key && this.configurationOptions.cert) {\n this.configurationOptions.key = PathFileUtil.getFileContentSync(\n this.configurationOptions.key\n );\n this.configurationOptions.cert = PathFileUtil.getFileContentSync(\n this.configurationOptions.cert\n );\n this.configurationOptions.ca = PathFileUtil.getFileContentSync(\n this.configurationOptions.ca\n );\n\n if (this.configurationOptions.http2) {\n this.server = require('http2').createSecureServer(\n this.configurationOptions,\n this.app.callback()\n );\n } else {\n this.server = require('https').createServer(\n this.configurationOptions,\n this.app.callback()\n );\n }\n } else {\n if (this.configurationOptions.http2) {\n this.server = require('http2').createServer(this.app.callback());\n } else {\n this.server = require('http').createServer(this.app.callback());\n }\n }\n // register httpServer to applicationContext\n this.applicationContext.registerObject(HTTP_SERVER_KEY, this.server);\n\n // set port and listen server\n const customPort =\n process.env.MIDWAY_HTTP_PORT ?? this.configurationOptions.port;\n if (customPort) {\n new Promise<void>(resolve => {\n const args: any[] = [customPort];\n if (this.configurationOptions.hostname) {\n args.push(this.configurationOptions.hostname);\n }\n args.push(() => {\n resolve();\n });\n this.server.listen(...args);\n process.env.MIDWAY_HTTP_PORT = String(customPort);\n });\n }\n }\n\n public async beforeStop() {\n this.server.close();\n }\n\n public getFrameworkType(): MidwayFrameworkType {\n return MidwayFrameworkType.WEB_KOA;\n }\n\n public getFrameworkName() {\n return 'web:koa';\n }\n\n public getServer() {\n return this.server;\n }\n\n public getPort() {\n return process.env.MIDWAY_HTTP_PORT;\n }\n\n public useMiddleware(\n Middleware: CommonMiddlewareUnion<IMidwayKoaContext, Next, unknown>\n ) {\n this.middlewareManager.insertLast(Middleware);\n }\n\n public useFilter(\n Filter: CommonFilterUnion<IMidwayKoaContext, Next, unknown>\n ) {\n this.filterManager.useFilter(Filter);\n }\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;AAAA,yCAA8D;AAArD,sGAAA,kBAAkB,OAAa;AACxC,mCAAkD;AAAzC,gHAAA,sBAAsB,OAAA;AAC/B,8CAA4B;AAC5B,iDAAoE;AAA3D,8GAAA,gBAAgB,OAAiB;AAC1C,8DAA4C;AAC5C,qEAAmD","sourcesContent":["export { MidwayKoaFramework as Framework } from './framework';\nexport { MidwayKoaContextLogger } from './logger';\nexport * from './interface';\nexport { KoaConfiguration as Configuration } from './configuration';\nexport * from './middleware/fav.middleware';\nexport * from './middleware/bodyparser.middleware';\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"interface.js","sourceRoot":"","sources":["../src/interface.ts"],"names":[],"mappings":"","sourcesContent":["import { IConfigurationOptions, IMidwayApplication, IMidwayContext } from '@midwayjs/core';\nimport * as koa from 'koa';\nimport { Context as KoaContext, DefaultState, Middleware, Next } from 'koa';\nimport { RouterParamValue } from '@midwayjs/decorator';\nimport { CookieSetOptions } from '@midwayjs/cookies';\n\nexport type IMidwayKoaContext = IMidwayContext<KoaContext>;\nexport type IMidwayKoaApplication = IMidwayApplication<IMidwayKoaContext, koa<DefaultState, IMidwayKoaContext> & {\n generateController(\n controllerMapping: string,\n routeArgsInfo?: RouterParamValue[],\n routerResponseData?: any []\n ): Middleware<DefaultState, IMidwayKoaContext>;\n /**\n * @deprecated\n * @param middlewareId\n */\n generateMiddleware(middlewareId: any): Promise<Middleware<DefaultState, IMidwayKoaContext>>;\n}>;\n\n/**\n * @deprecated use NextFunction definition\n */\nexport type IMidwayKoaNext = Next;\nexport type NextFunction = Next;\n\nexport interface IMidwayKoaConfigurationOptions extends IConfigurationOptions {\n /**\n * cookies sign keys\n */\n keys?: string[];\n /**\n * application http port\n */\n port?: number;\n /**\n * application hostname, 127.0.0.1 as default\n */\n hostname?: string;\n /**\n * https key\n */\n key?: string | Buffer | Array<Buffer | Object>;\n /**\n * https cert\n */\n cert?: string | Buffer | Array<string | Buffer>;\n /**\n * https ca\n */\n ca?: string | Buffer | Array<string | Buffer>;\n /**\n * http2 support\n */\n http2?: boolean;\n /**\n * http global prefix\n */\n globalPrefix?: string;\n}\n\nexport type MiddlewareParamArray = Array<Middleware<DefaultState, IMidwayKoaContext>>;\n\nexport interface IWebMiddleware {\n resolve(): koa.Middleware<DefaultState, IMidwayKoaContext>;\n}\n\nexport type Application = IMidwayKoaApplication;\n\nexport interface Context extends IMidwayKoaContext {}\n\ninterface BodyParserOptions {\n enable?: boolean;\n /**\n * parser will only parse when request type hits enableTypes, default is ['json', 'form'].\n */\n enableTypes?: string[];\n\n /**\n * requested encoding. Default is utf-8 by co-body\n */\n encode?: string;\n\n /**\n * limit of the urlencoded body. If the body ends up being larger than this limit\n * a 413 error code is returned. Default is 56kb\n */\n formLimit?: string;\n\n /**\n * limit of the json body. Default is 1mb\n */\n jsonLimit?: string;\n\n /**\n * limit of the text body. Default is 1mb.\n */\n textLimit?: string;\n\n /**\n * limit of the xml body. Default is 1mb.\n */\n xmlLimit?: string;\n\n /**\n * when set to true, JSON parser will only accept arrays and objects. Default is true\n */\n strict?: boolean;\n\n /**\n * custom json request detect function. Default is null\n */\n detectJSON?: ((ctx: IMidwayKoaContext) => boolean);\n\n /**\n * support extend types\n */\n extendTypes?: {\n json?: string[];\n form?: string[];\n text?: string[];\n };\n\n /**\n * support custom error handle\n */\n onerror?: ((err: Error, ctx: IMidwayKoaContext) => void);\n}\n\ndeclare module '@midwayjs/core/dist/interface' {\n interface MidwayConfig {\n keys?: string | string[];\n koa?: IMidwayKoaConfigurationOptions;\n cookies?: CookieSetOptions;\n /**\n * onerror middleware options\n */\n onerror?: {\n text?: (err: Error, ctx: IMidwayKoaContext) => void;\n json?: (err: Error, ctx: IMidwayKoaContext) => void;\n html?: (err: Error, ctx: IMidwayKoaContext) => void;\n redirect?: string;\n accepts?: (...args) => any;\n },\n bodyParser?: BodyParserOptions;\n siteFile?: {\n enable?: boolean;\n favicon?: undefined | string | Buffer\n };\n }\n}\n\ndeclare module 'koa' {\n interface Request {\n body?: any;\n rawBody: string;\n }\n}\n\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"logger.js","sourceRoot":"","sources":["../src/logger.ts"],"names":[],"mappings":";;;AAAA,6CAAuD;AAGvD,MAAa,sBAAuB,SAAQ,4BAAsC;IAChF,kBAAkB;QAChB,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC;QACrB,wDAAwD;QACxD,MAAM,MAAM,GAAG,GAAG,CAAC,MAAM,IAAI,GAAG,CAAC;QACjC,MAAM,OAAO,GAAG,CAAC,GAAG,CAAC,MAAM,IAAI,GAAG,CAAC,MAAM,CAAC,OAAO,CAAC,IAAI,GAAG,CAAC;QAC1D,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,GAAG,GAAG,CAAC,SAAS,CAAC;QACvC,OAAO,CACL,MAAM;YACN,GAAG;YACH,GAAG,CAAC,EAAE;YACN,GAAG;YACH,OAAO;YACP,GAAG;YACH,GAAG;YACH,KAAK;YACL,GAAG,CAAC,MAAM;YACV,GAAG;YACH,GAAG,CAAC,GAAG,CACR,CAAC;IACJ,CAAC;CACF;AArBD,wDAqBC","sourcesContent":["import { MidwayContextLogger } from '@midwayjs/logger';\nimport { IMidwayKoaContext } from './interface';\n\nexport class MidwayKoaContextLogger extends MidwayContextLogger<IMidwayKoaContext> {\n formatContextLabel() {\n const ctx = this.ctx;\n // format: '[$userId/$ip/$traceId/$use_ms $method $url]'\n const userId = ctx.userId || '-';\n const traceId = (ctx.tracer && ctx.tracer.traceId) || '-';\n const use = Date.now() - ctx.startTime;\n return (\n userId +\n '/' +\n ctx.ip +\n '/' +\n traceId +\n '/' +\n use +\n 'ms ' +\n ctx.method +\n ' ' +\n ctx.url\n );\n }\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bodyparser.middleware.js","sourceRoot":"","sources":["../../src/middleware/bodyparser.middleware.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,gDAAgD;AAChD,mDAAyD;AAGzD,IAAa,oBAAoB,GAAjC,MAAa,oBAAoB;IAI/B,OAAO;QACL,4BAA4B;QAC5B,IAAI,IAAI,CAAC,gBAAgB,CAAC,MAAM,EAAE;YAChC,OAAO,aAAa,CAAC,IAAI,CAAC,gBAAgB,CAAC,CAAC;SAC7C;IACH,CAAC;IAED,MAAM,CAAC,OAAO;QACZ,OAAO,YAAY,CAAC;IACtB,CAAC;CACF,CAAA;AAZC;IADC,IAAA,kBAAM,EAAC,YAAY,CAAC;;8DACJ;AAFN,oBAAoB;IADhC,IAAA,sBAAU,GAAE;GACA,oBAAoB,CAchC;AAdY,oDAAoB","sourcesContent":["import * as koaBodyParser from 'koa-bodyparser';\nimport { Config, Middleware } from '@midwayjs/decorator';\n\n@Middleware()\nexport class BodyParserMiddleware {\n @Config('bodyParser')\n bodyparserConfig;\n\n resolve() {\n // use bodyparser middleware\n if (this.bodyparserConfig.enable) {\n return koaBodyParser(this.bodyparserConfig);\n }\n }\n\n static getName() {\n return 'bodyParser';\n }\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fav.middleware.js","sourceRoot":"","sources":["../../src/middleware/fav.middleware.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,mDAAyD;AAEzD,MAAM,IAAI,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;AAC7B,MAAM,OAAO,GAAG,yBAAyB,CAAC,CAAC,UAAU;AAGrD,IAAa,kBAAkB,GAA/B,MAAa,kBAAkB;IAI7B,OAAO;QACL,4BAA4B;QAC5B,IAAI,IAAI,CAAC,cAAc,CAAC,MAAM,EAAE;YAC9B,OAAO,KAAK,EAAE,GAAG,EAAE,IAAI,EAAE,EAAE;gBACzB,IAAI,GAAG,CAAC,MAAM,KAAK,MAAM,IAAI,GAAG,CAAC,MAAM,KAAK,KAAK;oBAAE,OAAO,IAAI,EAAE,CAAC;gBACjE,wBAAwB;gBACxB,IAAI,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,KAAK,GAAG;oBAAE,OAAO,IAAI,EAAE,CAAC;gBAEvC,IAAI,GAAG,CAAC,IAAI,KAAK,cAAc,EAAE;oBAC/B,OAAO,IAAI,EAAE,CAAC;iBACf;gBAED,IAAI,OAAO,GAAG,IAAI,CAAC,cAAc,CAAC,SAAS,CAAC,CAAC;gBAC7C,IAAI,OAAO,KAAK,SAAS,EAAE;oBACzB,OAAO,GAAG,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;iBAC3B;gBACD,IAAI,CAAC,OAAO;oBAAE,OAAO,IAAI,EAAE,CAAC;gBAC5B,iBAAiB;gBACjB,IAAI,OAAO,OAAO,KAAK,QAAQ;oBAAE,OAAO,GAAG,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC;gBAE9D,8BAA8B;gBAC9B,oBAAoB;gBACpB,IAAI,MAAM,CAAC,QAAQ,CAAC,OAAO,CAAC,EAAE;oBAC5B,GAAG,CAAC,GAAG,CAAC,eAAe,EAAE,OAAO,CAAC,CAAC;oBAClC,GAAG,CAAC,IAAI,GAAG,OAAO,CAAC;oBACnB,GAAG,CAAC,IAAI,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;oBAClC,OAAO;iBACR;gBAED,OAAO,IAAI,EAAE,CAAC;YAChB,CAAC,CAAC;SACH;IACH,CAAC;IAED,MAAM,CAAC,OAAO;QACZ,OAAO,UAAU,CAAC;IACpB,CAAC;CACF,CAAA;AAvCC;IADC,IAAA,kBAAM,EAAC,UAAU,CAAC;;0DACJ;AAFJ,kBAAkB;IAD9B,IAAA,sBAAU,GAAE;GACA,kBAAkB,CAyC9B;AAzCY,gDAAkB","sourcesContent":["import { Config, Middleware } from '@midwayjs/decorator';\n\nconst path = require('path');\nconst MAX_AGE = 'public, max-age=2592000'; // 30 days\n\n@Middleware()\nexport class SiteFileMiddleware {\n @Config('siteFile')\n siteFileConfig;\n\n resolve() {\n // use bodyparser middleware\n if (this.siteFileConfig.enable) {\n return async (ctx, next) => {\n if (ctx.method !== 'HEAD' && ctx.method !== 'GET') return next();\n /* istanbul ignore if */\n if (ctx.path[0] !== '/') return next();\n\n if (ctx.path !== '/favicon.ico') {\n return next();\n }\n\n let content = this.siteFileConfig['favicon'];\n if (content === undefined) {\n content = Buffer.from('');\n }\n if (!content) return next();\n // content is url\n if (typeof content === 'string') return ctx.redirect(content);\n\n // '/robots.txt': Buffer <xx..\n // content is buffer\n if (Buffer.isBuffer(content)) {\n ctx.set('cache-control', MAX_AGE);\n ctx.body = content;\n ctx.type = path.extname(ctx.path);\n return;\n }\n\n return next();\n };\n }\n }\n\n static getName() {\n return 'siteFile';\n }\n}\n"]}
|
package/dist/onerror.js
CHANGED
|
@@ -54,6 +54,7 @@ function setupOnerror(app, config, logger) {
|
|
|
54
54
|
}
|
|
55
55
|
ctx.body = utils_1.tpl
|
|
56
56
|
.replace('{{status}}', (0, utils_1.escapeHtml)(err.status))
|
|
57
|
+
.replace('{{errorCode}}', (0, utils_1.escapeHtml)(err.code))
|
|
57
58
|
.replace('{{stack}}', (0, utils_1.escapeHtml)(err.stack));
|
|
58
59
|
ctx.type = 'html';
|
|
59
60
|
},
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"onerror.js","sourceRoot":"","sources":["../src/onerror.ts"],"names":[],"mappings":";;;AAAA,6BAA6B;AAC7B,mCAOiB;AAEjB,SAAgB,YAAY,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM;IAC9C,MAAM,YAAY,GAAG,MAAM,CAAC,MAAM,CAChC;QACE,qCAAqC;QACrC,OAAO;YACL,MAAM,EAAE,GAAG,MAAM,CAAC,OAAO,IAAI,eAAO,CAAC;YACrC,OAAO,EAAE,CAAC,IAAI,CAAC,CAAC;QAClB,CAAC;QACD;;;;WAIG;QACH,IAAI,CAAC,GAA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SACxC;aAAM;YACL,IAAI,YAAY,CAAC,QAAQ,IAAI,IAAI,KAAK,MAAM,EAAE;gBAC5C,IAAI,CAAC,QAAQ,CAAC,YAAY,CAAC,QAAQ,CAAC,CAAC;aACtC;iBAAM;gBACL,YAAY,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,IAAI,EAAE,GAAG,EAAE,IAAI,CAAC,CAAC;gBACzC,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;aAClB;SACF;QAED,IAAI,IAAI,KAAK,MAAM,EAAE;YACnB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;SACvC;QACD,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IAC1B,CAAC,CAAC;AACJ,CAAC;AAzKD,oCAyKC","sourcesContent":["import * as http from 'http';\nimport {\n accepts,\n detectStatus,\n isProduction,\n sendToWormhole,\n escapeHtml,\n tpl,\n} from './utils';\n\nexport function setupOnerror(app, config, logger) {\n const errorOptions = Object.assign(\n {\n // support customize accepts function\n accepts() {\n const fn = config.accepts || accepts;\n return fn(this);\n },\n /**\n * default text error handler\n * @param {Error} err\n * @param ctx\n */\n text(err, ctx) {\n // unset all headers, and set those specified\n ctx.res._headers = {};\n ctx.set(err.headers);\n\n if (isProduction(app)) {\n ctx.body = http.STATUS_CODES[ctx.status];\n } else {\n ctx.body = err.message;\n }\n },\n /**\n * default html error handler\n * @param {Error} err\n */\n html(err, ctx) {\n const status = detectStatus(err);\n if (isProduction(app)) {\n // 5xx\n if (status >= 500) {\n ctx.status = 500;\n ctx.body = `<h2>Internal Server Error, real status: ${status}</h2>`;\n return;\n } else {\n // 4xx\n ctx.status = status;\n ctx.body = `<h2>${status} ${http.STATUS_CODES[status]}</h2>`;\n return;\n }\n }\n\n // show simple error format for unittest\n if (app.getEnv() === 'unittest' || app.getEnv() === 'test') {\n ctx.status = status;\n ctx.body = `${err.name}: ${err.message}\\n${err.stack}`;\n return;\n }\n\n ctx.body = tpl\n .replace('{{status}}', escapeHtml(err.status))\n .replace('{{errorCode}}', escapeHtml(err.code))\n .replace('{{stack}}', escapeHtml(err.stack));\n ctx.type = 'html';\n },\n /**\n * default json error handler\n * @param {Error} err\n * @param ctx\n */\n json(err, ctx) {\n const status = detectStatus(err);\n const code = err.code || err.type;\n\n if (isProduction(app)) {\n if (status >= 500) {\n ctx.body = { code, message: http.STATUS_CODES[status] };\n } else {\n ctx.body = { code, message: err.message };\n }\n } else {\n ctx.body = { code, message: err.message, stack: err.stack };\n }\n },\n },\n config\n );\n\n app.on('error', (err, ctx) => {\n ctx = ctx || app.createAnonymousContext();\n const status = detectStatus(err);\n // 5xx\n if (status >= 500) {\n try {\n ctx.logger.error(err);\n } catch (ex) {\n logger.error(err);\n logger.error(ex);\n }\n return;\n }\n\n // 4xx\n try {\n ctx.logger.warn(err);\n } catch (ex) {\n logger.warn(err);\n logger.error(ex);\n }\n });\n\n app.context.onerror = function (err) {\n // don't do anything if there is no error.\n // this allows you to pass `this.onerror`\n // to node-style callbacks.\n if (err == null) return;\n\n // ignore all pedding request stream\n if (this.req) sendToWormhole(this.req);\n\n // wrap non-error object\n if (!(err instanceof Error)) {\n const newError = new Error('non-error thrown: ' + err);\n // err maybe an object, try to copy the name, message and stack to the new error instance\n if (err) {\n if (err.name) newError.name = err.name;\n if (err.message) newError.message = err.message;\n if (err.stack) newError.stack = err.stack;\n if (err.status) newError['status'] = err.status;\n if (err.headers) newError['headers'] = err.headers;\n }\n err = newError;\n }\n\n const headerSent = this.headerSent || !this.writable;\n if (headerSent) err.headerSent = true;\n\n // delegate\n app.emit('error', err, this);\n\n // nothing we can do here other\n // than delegate to the app-level\n // handler and log.\n if (headerSent) return;\n\n // ENOENT support\n if (err.code === 'ENOENT') err.status = 404;\n\n if (typeof err.status !== 'number' || !http.STATUS_CODES[err.status]) {\n err.status = 500;\n }\n this.status = err.status;\n\n this.set(err.headers);\n let type = 'text';\n if (errorOptions.accepts) {\n type = errorOptions.accepts.call(this, 'html', 'text', 'json');\n } else {\n type = this.accepts('html', 'text', 'json');\n }\n type = type || 'text';\n if (errorOptions.all) {\n errorOptions.all.call(this, err, this);\n } else {\n if (errorOptions.redirect && type !== 'json') {\n this.redirect(errorOptions.redirect);\n } else {\n errorOptions[type].call(this, err, this);\n this.type = type;\n }\n }\n\n if (type === 'json') {\n this.body = JSON.stringify(this.body);\n }\n this.res.end(this.body);\n };\n}\n"]}
|
package/dist/utils.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ export declare function detectStatus(err: any): any;
|
|
|
2
2
|
export declare function accepts(ctx: any): "json" | "js" | "html";
|
|
3
3
|
export declare function sendToWormhole(stream: any): Promise<void>;
|
|
4
4
|
export declare function isProduction(app: any): boolean;
|
|
5
|
-
export declare const tpl = "\n<!DOCTYPE html>\n<html>\n <head>\n <title>Error - {{status}}</title>\n <meta name=\"viewport\" content=\"user-scalable=no, width=device-width, initial-scale=1.0, maximum-scale=1.0\">\n\n <style>\n body {\n padding: 50px 80px;\n font: 14px \"Helvetica Neue\", Helvetica, sans-serif;\n }\n\n h1 {\n font-size: 2em;\n margin-bottom: 5px;\n }\n\n pre {\n font-size: .8em;\n }\n </style>\n </head>\n <body>\n <div id=\"error\">\n <h1>Error</h1>\n <p>Looks like something broke!</p>\n <pre>\n <code>\n{{stack}}\n </code>\n </pre>\n </div>\n </body>\n</html>";
|
|
5
|
+
export declare const tpl = "\n<!DOCTYPE html>\n<html>\n <head>\n <title>Error - {{status}}</title>\n <meta name=\"viewport\" content=\"user-scalable=no, width=device-width, initial-scale=1.0, maximum-scale=1.0\">\n\n <style>\n body {\n padding: 50px 80px;\n font: 14px \"Helvetica Neue\", Helvetica, sans-serif;\n }\n\n h1 {\n font-size: 2em;\n margin-bottom: 5px;\n }\n\n pre {\n font-size: .8em;\n }\n </style>\n </head>\n <body>\n <div id=\"error\">\n <h1>Error</h1>\n <p>Looks like something broke!</p>\n <p><h2>Stack</h2></p>\n <pre>\n <code>\n{{stack}}\n </code>\n </pre>\n <p><h2>Error Code</h2></p>\n <pre>\n <code>\n{{errorCode}}\n </code>\n </pre>\n </div>\n </body>\n</html>";
|
|
6
6
|
/**
|
|
7
7
|
* Escape special characters in the given string of text.
|
|
8
8
|
*
|
package/dist/utils.js
CHANGED
|
@@ -84,11 +84,18 @@ exports.tpl = `
|
|
|
84
84
|
<div id="error">
|
|
85
85
|
<h1>Error</h1>
|
|
86
86
|
<p>Looks like something broke!</p>
|
|
87
|
+
<p><h2>Stack</h2></p>
|
|
87
88
|
<pre>
|
|
88
89
|
<code>
|
|
89
90
|
{{stack}}
|
|
90
91
|
</code>
|
|
91
92
|
</pre>
|
|
93
|
+
<p><h2>Error Code</h2></p>
|
|
94
|
+
<pre>
|
|
95
|
+
<code>
|
|
96
|
+
{{errorCode}}
|
|
97
|
+
</code>
|
|
98
|
+
</pre>
|
|
92
99
|
</div>
|
|
93
100
|
</body>
|
|
94
101
|
</html>`;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":";;;AAAA,SAAgB,YAAY,CAAC,GAAG;IAC9B,gBAAgB;IAChB,IAAI,MAAM,GAAG,GAAG,CAAC,MAAM,IAAI,GAAG,CAAC;IAC/B,IAAI,MAAM,GAAG,GAAG,EAAE;QAChB,oEAAoE;QACpE,MAAM,GAAG,GAAG,CAAC;KACd;IACD,OAAO,MAAM,CAAC;AAChB,CAAC;AARD,oCAQC;AAED,SAAgB,OAAO,CAAC,GAAG;IACzB,IAAI,GAAG,CAAC,UAAU;QAAE,OAAO,MAAM,CAAC;IAClC,IAAI,GAAG,CAAC,WAAW;QAAE,OAAO,IAAI,CAAC;IACjC,OAAO,MAAM,CAAC;AAChB,CAAC;AAJD,0BAIC;AAED,SAAgB,cAAc,CAAC,MAAM;IACnC,OAAO,IAAI,OAAO,CAAO,OAAO,CAAC,EAAE;QACjC,IAA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K,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;AAC7E,CAAC;AA3CD,gCA2CC","sourcesContent":["export function detectStatus(err) {\n // detect status\n let status = err.status || 500;\n if (status < 200) {\n // invalid status consider as 500, like urllib will return -1 status\n status = 500;\n }\n return status;\n}\n\nexport function accepts(ctx) {\n if (ctx.acceptJSON) return 'json';\n if (ctx.acceptJSONP) return 'js';\n return 'html';\n}\n\nexport function sendToWormhole(stream): Promise<void> {\n return new Promise<void>(resolve => {\n if (typeof stream.resume !== 'function') {\n return resolve();\n }\n\n // unpipe it\n stream.unpipe && stream.unpipe();\n // enable resume first\n stream.resume();\n\n if (stream._readableState && stream._readableState.ended) {\n return resolve();\n }\n if (!stream.readable || stream.destroyed) {\n return resolve();\n }\n\n function cleanup() {\n stream.removeListener('end', onEnd);\n stream.removeListener('close', onEnd);\n stream.removeListener('error', onError);\n }\n\n function onEnd() {\n cleanup();\n resolve();\n }\n\n function onError() {\n cleanup();\n resolve();\n }\n\n stream.on('end', onEnd);\n stream.on('close', onEnd);\n stream.on('error', onError);\n });\n}\n\nexport function isProduction(app) {\n return app.getEnv() !== 'local' && app.getEnv() !== 'unittest';\n}\n\nexport const tpl = `\n<!DOCTYPE html>\n<html>\n <head>\n <title>Error - {{status}}</title>\n <meta name=\"viewport\" content=\"user-scalable=no, width=device-width, initial-scale=1.0, maximum-scale=1.0\">\n\n <style>\n body {\n padding: 50px 80px;\n font: 14px \"Helvetica Neue\", Helvetica, sans-serif;\n }\n\n h1 {\n font-size: 2em;\n margin-bottom: 5px;\n }\n\n pre {\n font-size: .8em;\n }\n </style>\n </head>\n <body>\n <div id=\"error\">\n <h1>Error</h1>\n <p>Looks like something broke!</p>\n <p><h2>Stack</h2></p>\n <pre>\n <code>\n{{stack}}\n </code>\n </pre>\n <p><h2>Error Code</h2></p>\n <pre>\n <code>\n{{errorCode}}\n </code>\n </pre>\n </div>\n </body>\n</html>`;\n\nconst matchHtmlRegExp = /[\"'&<>]/;\n/**\n * Escape special characters in the given string of text.\n *\n * @param {string} string The string to escape for inserting into HTML\n * @return {string}\n * @public\n */\n\nexport function escapeHtml(string) {\n const str = '' + string;\n const match = matchHtmlRegExp.exec(str);\n\n if (!match) {\n return str;\n }\n\n let escape;\n let html = '';\n let index = 0;\n let lastIndex = 0;\n\n for (index = match.index; index < str.length; index++) {\n switch (str.charCodeAt(index)) {\n case 34: // \"\n escape = '"';\n break;\n case 38: // &\n escape = '&';\n break;\n case 39: // '\n escape = ''';\n break;\n case 60: // <\n escape = '<';\n break;\n case 62: // >\n escape = '>';\n break;\n default:\n continue;\n }\n\n if (lastIndex !== index) {\n html += str.substring(lastIndex, index);\n }\n\n lastIndex = index + 1;\n html += escape;\n }\n\n return lastIndex !== index ? html + str.substring(lastIndex, index) : html;\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@midwayjs/koa",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.2",
|
|
4
4
|
"description": "Midway Web Framework for KOA",
|
|
5
5
|
"main": "dist/index",
|
|
6
6
|
"typings": "dist/index.d.ts",
|
|
@@ -19,13 +19,14 @@
|
|
|
19
19
|
],
|
|
20
20
|
"files": [
|
|
21
21
|
"dist/**/*.js",
|
|
22
|
-
"dist/**/*.d.ts"
|
|
22
|
+
"dist/**/*.d.ts",
|
|
23
|
+
"dist/**/*.js.map"
|
|
23
24
|
],
|
|
24
25
|
"license": "MIT",
|
|
25
26
|
"devDependencies": {
|
|
26
|
-
"@midwayjs/decorator": "^3.0.
|
|
27
|
+
"@midwayjs/decorator": "^3.0.2",
|
|
27
28
|
"@midwayjs/logger": "^2.14.0",
|
|
28
|
-
"@midwayjs/mock": "^3.0.
|
|
29
|
+
"@midwayjs/mock": "^3.0.2",
|
|
29
30
|
"@types/koa": "2.13.4",
|
|
30
31
|
"@types/koa-router": "7.4.4",
|
|
31
32
|
"fs-extra": "10.0.0"
|
|
@@ -33,8 +34,8 @@
|
|
|
33
34
|
"dependencies": {
|
|
34
35
|
"@koa/router": "^10.0.0",
|
|
35
36
|
"@midwayjs/cookies": "1.0.1",
|
|
36
|
-
"@midwayjs/core": "^3.0.
|
|
37
|
-
"@midwayjs/session": "^3.0.
|
|
37
|
+
"@midwayjs/core": "^3.0.2",
|
|
38
|
+
"@midwayjs/session": "^3.0.2",
|
|
38
39
|
"koa": "2.13.4",
|
|
39
40
|
"koa-bodyparser": "4.3.0"
|
|
40
41
|
},
|
|
@@ -46,5 +47,5 @@
|
|
|
46
47
|
"engines": {
|
|
47
48
|
"node": ">=12"
|
|
48
49
|
},
|
|
49
|
-
"gitHead": "
|
|
50
|
+
"gitHead": "ca77247d229978a736e79bb208579c014ed226fc"
|
|
50
51
|
}
|