From b2de266b92277d2bc9d10a743e9d0eda5f4086f1 Mon Sep 17 00:00:00 2001 From: yutent Date: Mon, 6 Jan 2025 10:58:17 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E4=BE=9D=E8=B5=96;=20?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E8=B7=A8=E5=9F=9F=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.d.ts | 10 +++++++--- index.js | 6 ++++-- middleware/cors.js | 4 +--- package.json | 13 +++++++------ 4 files changed, 19 insertions(+), 14 deletions(-) diff --git a/index.d.ts b/index.d.ts index c371236..0302ab7 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,5 +1,9 @@ declare module '@gm5/core' { import { Server } from 'http' + import Request from '@gm5/request' + import Response from '@gm5/response' + + declare type Mountable = string[] interface Middleware { (req: Request, res: Response, next: () => void): void @@ -7,7 +11,7 @@ declare module '@gm5/core' { interface Installable { name: string - install: (args: any) => any + install: (args?: Record) => any } export default class Five { @@ -17,12 +21,12 @@ declare module '@gm5/core' { get(key: string): any - use(middleware: Middleware | Installable, args?: any): this + use(middleware: Middleware | Installable | Mountable, args?: Record): this listen(port?: number, callback?: () => void): this } - export function mount(dir?: string): array + export function mount(dir?: string): Mountable export function createApp(conf?: object): Five } diff --git a/index.js b/index.js index 31b6af1..2bc528c 100644 --- a/index.js +++ b/index.js @@ -161,13 +161,11 @@ class Five { // 路由中间件要在最后 this.use(createRouter()) - this.set({ port }) this.#online = true this.#server = http.createServer() - this.#server .on('request', (req, res) => { let request = new Request(req, res) @@ -182,6 +180,10 @@ class Five { console.log('Server successfully started ...') console.log('%s://%s:%d\n', 'http', '127.0.0.1', port) } + // 未开启跨域时, 移除跨域中间件, 以提高性能 + if (!this.get('cors').enabled) { + this.#middlewares.shift() + } }) .on('error', err => { console.error(err) diff --git a/middleware/cors.js b/middleware/cors.js index c219cc2..36d7d72 100644 --- a/middleware/cors.js +++ b/middleware/cors.js @@ -4,8 +4,6 @@ * @date 2020/09/18 14:55:49 */ -import { parse } from 'node:url' - export function createCors() { return function (req, res, next) { let opts = this.get('cors') @@ -17,7 +15,7 @@ export function createCors() { if (!origin) { return next() } - let { hostname, host, protocol } = parse(origin) + let { hostname, host, protocol } = new URL(origin) if (opts.origin.length) { let pass = false diff --git a/package.json b/package.json index 40d2c67..e80c6bb 100644 --- a/package.json +++ b/package.json @@ -1,23 +1,24 @@ { "name": "@gm5/core", - "version": "2.0.4", + "version": "3.0.0", "type": "module", "description": "Five.js, 一个轻量级的nodejs mvc框架 旨在简单易用, 5分钟即可上手", "author": "yutent ", "main": "index.js", "types": "index.d.ts", "dependencies": { - "@gm5/session": "^2.0.0", "@gm5/request": "^2.0.3", "@gm5/response": "^2.0.1", "@gm5/controller": "^2.0.2", - "@gm5/jwt": "^2.0.2", - "@gm5/views": "^2.0.0", - "crypto.js": "^3.1.2", + "crypto.js": "^3.3.4", "es.shim": "^2.2.0", "iofs": "^1.5.3" }, - "devDependencies": {}, + "optionalDependencies": { + "@gm5/session": "^3.0.0", + "@gm5/jwt": "^3.0.1", + "@gm5/views": "^3.0.0" + }, "repository": { "type": "git", "url": "https://git.wkit.fun/gm5/core.git"