core/index.js

208 lines
4.9 KiB
JavaScript
Raw Normal View History

2020-09-15 18:35:00 +08:00
/**
* 框架核心
2020-09-28 10:03:27 +08:00
* @author yutent<yutent.io@gmail.com>
* @date 2020/09/28 10:01:47
2020-09-15 18:35:00 +08:00
*/
2020-09-15 19:06:27 +08:00
import 'es.shim' // 加载拓展方法
import http from 'node:http'
import { parse, join } from 'node:path'
2020-09-18 18:14:47 +08:00
import fs from 'iofs'
2020-09-18 18:14:47 +08:00
import Request from '@gm5/request'
import Response from '@gm5/response'
2023-10-25 18:48:55 +08:00
2023-10-27 19:16:59 +08:00
import { noop, readonlyProp } from './lib.js'
import config from './config/index.js'
2020-09-15 19:06:27 +08:00
2023-10-27 19:16:59 +08:00
import { createCors } from './middleware/cors.js'
2023-11-02 10:54:37 +08:00
import { createRouter } from './middleware/router.js'
2020-09-15 18:35:00 +08:00
2022-12-21 11:47:18 +08:00
process.on('uncaughtException', err => {
console.error('UncaughtException: ', err)
})
2023-10-25 18:48:55 +08:00
class Five {
2023-10-24 15:24:55 +08:00
#config = config
2025-01-03 18:54:52 +08:00
#controllers = {}
2023-10-27 19:16:59 +08:00
#middlewares = [createCors()]
2020-09-30 14:25:08 +08:00
2023-10-31 18:52:54 +08:00
#server = null
2024-12-17 16:09:18 +08:00
#online = false
2023-10-31 18:52:54 +08:00
2023-10-27 19:16:59 +08:00
constructor() {
readonlyProp(this, 'state', Object.create(null))
}
2020-09-15 18:35:00 +08:00
2024-12-17 16:09:18 +08:00
/**
* 循环顺序执行中间件, 直到执行完所有中间件或没有调用next
*/
2023-10-25 18:48:55 +08:00
async #loop(req, res, idx = 0) {
let fn = this.#middlewares[idx]
if (fn) {
await fn.call(this, req, res, _ => {
idx++
this.#loop(req, res, idx)
})
}
}
2024-12-17 16:09:18 +08:00
// 注入实例化对象到实例池中
#install({ name, install }, args) {
this['$$' + name] = install.call(this, args)
return this
}
2024-12-18 11:37:15 +08:00
async #preload(list) {
for (let item of list) {
let { name } = parse(item)
if (name.startsWith('.')) {
return
}
// 如果是目录,则默认加载index.js, 其他文件不加载
// 交由index.js自行处理, 用于复杂的应用
if (fs.isdir(item)) {
item = join(item, './index.js')
}
try {
let { default: Module } = await import(item)
2025-01-03 18:54:52 +08:00
this.#controllers[name] = Module
2024-12-18 11:37:15 +08:00
} catch (err) {
console.error(err)
}
}
}
2020-09-15 18:35:00 +08:00
/*------------------------------------------------------------------------*/
2023-11-01 18:46:47 +08:00
get server() {
2023-10-31 18:52:54 +08:00
return this.#server
}
/*------------------------------------------------------------------------*/
2020-09-15 18:35:00 +08:00
// 注册属性到全局Five对象
set(obj) {
for (let i in obj) {
if (typeof obj[i] === 'object' && !Array.isArray(obj[i])) {
2023-10-24 15:24:55 +08:00
if (!this.#config[i]) {
this.#config[i] = obj[i]
2020-09-15 18:35:00 +08:00
} else {
try {
2023-10-24 15:24:55 +08:00
Object.assign(this.#config[i], obj[i])
2020-09-15 18:35:00 +08:00
} catch (err) {
console.error(err)
2020-09-15 18:35:00 +08:00
}
}
} else {
2023-10-24 15:24:55 +08:00
this.#config[i] = obj[i]
2020-09-15 18:35:00 +08:00
}
}
return this
}
// 获取全局配置
get(key) {
2023-10-24 15:24:55 +08:00
return this.#config[key]
2020-09-15 18:35:00 +08:00
}
2024-12-17 16:09:18 +08:00
/**
* 加载中间件
* 与别的中间件用法有些不一样, 回调的传入参数中的req和res,
* 并非原生的request对象和response对象,
* 而是框架内部封装过的,可通过origin属性访问原生的对象
* @param {*} middleware
* @returns Five
*/
use(middleware = noop, args) {
if (this.#online) {
return console.error('Server already started, cannot use middleware')
}
if (typeof middleware === 'function') {
this.#middlewares.push(middleware)
} else if (typeof middleware === 'object' && typeof middleware.install === 'function') {
this.#install(middleware, args)
2024-12-18 11:37:15 +08:00
} else if (Array.isArray(middleware)) {
this.#preload(middleware)
2024-12-17 16:09:18 +08:00
} else {
throw TypeError('argument must be a function or installable object')
2020-09-15 18:35:00 +08:00
}
2024-12-17 16:09:18 +08:00
return this
}
2024-12-17 16:09:18 +08:00
/**
* 注入中间件, 与use方法不同, install方法会立即执行
* @deprecated 请使用use方法, 将在下个版本中移除
*/
install(middleware, args) {
return this.#install(middleware, args)
2020-09-15 18:35:00 +08:00
}
2020-09-22 19:58:29 +08:00
2024-12-18 11:37:15 +08:00
/**
* 预加载应用, 缓存以提高性能
* @deprecated 请使用use方法, 将在下个版本中移除
*/
2020-09-15 18:35:00 +08:00
preload(dir) {
2024-12-18 11:40:00 +08:00
this.#preload(mount(dir))
2020-09-15 18:35:00 +08:00
return this
}
2023-10-24 15:24:55 +08:00
$load(name) {
2025-01-03 18:54:52 +08:00
return this.#controllers[name]
2023-10-24 15:24:55 +08:00
}
2020-09-15 18:35:00 +08:00
// 启动http服务
2024-12-17 16:09:18 +08:00
listen(port = 3000, callback = noop) {
2024-12-18 11:54:39 +08:00
if (this.#online) {
return console.error('Server already started')
}
2023-10-24 15:24:55 +08:00
2025-01-03 18:54:52 +08:00
// 路由中间件要在最后
this.use(createRouter())
2024-12-18 11:54:39 +08:00
this.set({ port })
2024-12-17 16:09:18 +08:00
this.#online = true
2023-10-31 18:52:54 +08:00
this.#server = http.createServer()
2020-09-15 18:35:00 +08:00
2023-10-31 18:52:54 +08:00
this.#server
2023-10-25 18:48:55 +08:00
.on('request', (req, res) => {
let request = new Request(req, res)
let response = new Response(req, res)
2020-09-23 19:03:44 +08:00
2023-10-25 18:48:55 +08:00
response.set('X-Powered-By', this.get('X-Powered-By') || 'Five.js')
this.#loop(request, response)
2023-10-24 15:24:55 +08:00
})
.on('listening', _ => {
2023-10-27 19:16:59 +08:00
if (this.get('debug')) {
2023-10-25 18:48:55 +08:00
console.log('Server successfully started ...')
2024-12-17 16:09:18 +08:00
console.log('%s://%s:%d\n', 'http', '127.0.0.1', port)
2023-10-25 18:48:55 +08:00
}
2025-01-06 10:58:17 +08:00
// 未开启跨域时, 移除跨域中间件, 以提高性能
if (!this.get('cors').enabled) {
this.#middlewares.shift()
}
2023-10-24 15:24:55 +08:00
})
.on('error', err => {
console.error(err)
})
2024-12-17 16:09:18 +08:00
.listen(port)
2020-09-15 18:35:00 +08:00
2024-12-17 16:09:18 +08:00
callback.call(this, this.#server)
2023-10-24 15:24:55 +08:00
return this
2020-09-15 18:35:00 +08:00
}
}
2023-10-24 15:24:55 +08:00
2024-12-18 11:40:00 +08:00
export function mount(dir) {
2024-12-18 11:37:15 +08:00
let list = fs.ls(dir) || []
return list
}
export function createApp(conf = {}) {
let app = new Five()
app.set(conf)
return app
2023-10-24 15:24:55 +08:00
}