调整依赖; 优化跨域配置

master
yutent 2025-01-06 10:58:17 +08:00
parent edce8b8cd6
commit b2de266b92
4 changed files with 19 additions and 14 deletions

10
index.d.ts vendored
View File

@ -1,5 +1,9 @@
declare module '@gm5/core' { declare module '@gm5/core' {
import { Server } from 'http' import { Server } from 'http'
import Request from '@gm5/request'
import Response from '@gm5/response'
declare type Mountable = string[]
interface Middleware { interface Middleware {
(req: Request, res: Response, next: () => void): void (req: Request, res: Response, next: () => void): void
@ -7,7 +11,7 @@ declare module '@gm5/core' {
interface Installable { interface Installable {
name: string name: string
install: (args: any) => any install: (args?: Record<string, any>) => any
} }
export default class Five { export default class Five {
@ -17,12 +21,12 @@ declare module '@gm5/core' {
get(key: string): any get(key: string): any
use(middleware: Middleware | Installable, args?: any): this use(middleware: Middleware | Installable | Mountable, args?: Record<string, any>): this
listen(port?: number, callback?: () => void): 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 export function createApp(conf?: object): Five
} }

View File

@ -161,13 +161,11 @@ class Five {
// 路由中间件要在最后 // 路由中间件要在最后
this.use(createRouter()) this.use(createRouter())
this.set({ port }) this.set({ port })
this.#online = true this.#online = true
this.#server = http.createServer() this.#server = http.createServer()
this.#server this.#server
.on('request', (req, res) => { .on('request', (req, res) => {
let request = new Request(req, res) let request = new Request(req, res)
@ -182,6 +180,10 @@ class Five {
console.log('Server successfully started ...') console.log('Server successfully started ...')
console.log('%s://%s:%d\n', 'http', '127.0.0.1', port) console.log('%s://%s:%d\n', 'http', '127.0.0.1', port)
} }
// 未开启跨域时, 移除跨域中间件, 以提高性能
if (!this.get('cors').enabled) {
this.#middlewares.shift()
}
}) })
.on('error', err => { .on('error', err => {
console.error(err) console.error(err)

View File

@ -4,8 +4,6 @@
* @date 2020/09/18 14:55:49 * @date 2020/09/18 14:55:49
*/ */
import { parse } from 'node:url'
export function createCors() { export function createCors() {
return function (req, res, next) { return function (req, res, next) {
let opts = this.get('cors') let opts = this.get('cors')
@ -17,7 +15,7 @@ export function createCors() {
if (!origin) { if (!origin) {
return next() return next()
} }
let { hostname, host, protocol } = parse(origin) let { hostname, host, protocol } = new URL(origin)
if (opts.origin.length) { if (opts.origin.length) {
let pass = false let pass = false

View File

@ -1,23 +1,24 @@
{ {
"name": "@gm5/core", "name": "@gm5/core",
"version": "2.0.4", "version": "3.0.0",
"type": "module", "type": "module",
"description": "Five.js, 一个轻量级的nodejs mvc框架 旨在简单易用, 5分钟即可上手", "description": "Five.js, 一个轻量级的nodejs mvc框架 旨在简单易用, 5分钟即可上手",
"author": "yutent <yutent.io@gmail.com>", "author": "yutent <yutent.io@gmail.com>",
"main": "index.js", "main": "index.js",
"types": "index.d.ts", "types": "index.d.ts",
"dependencies": { "dependencies": {
"@gm5/session": "^2.0.0",
"@gm5/request": "^2.0.3", "@gm5/request": "^2.0.3",
"@gm5/response": "^2.0.1", "@gm5/response": "^2.0.1",
"@gm5/controller": "^2.0.2", "@gm5/controller": "^2.0.2",
"@gm5/jwt": "^2.0.2", "crypto.js": "^3.3.4",
"@gm5/views": "^2.0.0",
"crypto.js": "^3.1.2",
"es.shim": "^2.2.0", "es.shim": "^2.2.0",
"iofs": "^1.5.3" "iofs": "^1.5.3"
}, },
"devDependencies": {}, "optionalDependencies": {
"@gm5/session": "^3.0.0",
"@gm5/jwt": "^3.0.1",
"@gm5/views": "^3.0.0"
},
"repository": { "repository": {
"type": "git", "type": "git",
"url": "https://git.wkit.fun/gm5/core.git" "url": "https://git.wkit.fun/gm5/core.git"