core/index.js

208 lines
4.9 KiB
JavaScript

/**
* 框架核心
* @author yutent<yutent.io@gmail.com>
* @date 2020/09/28 10:01:47
*/
import 'es.shim' // 加载拓展方法
import http from 'node:http'
import { parse, join } from 'node:path'
import fs from 'iofs'
import Request from '@gm5/request'
import Response from '@gm5/response'
import { noop, readonlyProp } from './lib.js'
import config from './config/index.js'
import { createCors } from './middleware/cors.js'
import { createRouter } from './middleware/router.js'
process.on('uncaughtException', err => {
console.error('UncaughtException: ', err)
})
class Five {
#config = config
#controllers = {}
#middlewares = [createCors()]
#server = null
#online = false
constructor() {
readonlyProp(this, 'state', Object.create(null))
}
/**
* 循环顺序执行中间件, 直到执行完所有中间件或没有调用next
*/
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)
})
}
}
// 注入实例化对象到实例池中
#install({ name, install }, args) {
this['$$' + name] = install.call(this, args)
return this
}
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)
this.#controllers[name] = Module
} catch (err) {
console.error(err)
}
}
}
/*------------------------------------------------------------------------*/
get server() {
return this.#server
}
/*------------------------------------------------------------------------*/
// 注册属性到全局Five对象
set(obj) {
for (let i in obj) {
if (typeof obj[i] === 'object' && !Array.isArray(obj[i])) {
if (!this.#config[i]) {
this.#config[i] = obj[i]
} else {
try {
Object.assign(this.#config[i], obj[i])
} catch (err) {
console.error(err)
}
}
} else {
this.#config[i] = obj[i]
}
}
return this
}
// 获取全局配置
get(key) {
return this.#config[key]
}
/**
* 加载中间件
* 与别的中间件用法有些不一样, 回调的传入参数中的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)
} else if (Array.isArray(middleware)) {
this.#preload(middleware)
} else {
throw TypeError('argument must be a function or installable object')
}
return this
}
/**
* 注入中间件, 与use方法不同, install方法会立即执行
* @deprecated 请使用use方法, 将在下个版本中移除
*/
install(middleware, args) {
return this.#install(middleware, args)
}
/**
* 预加载应用, 缓存以提高性能
* @deprecated 请使用use方法, 将在下个版本中移除
*/
preload(dir) {
this.#preload(mount(dir))
return this
}
$load(name) {
return this.#controllers[name]
}
// 启动http服务
listen(port = 3000, callback = noop) {
if (this.#online) {
return console.error('Server already started')
}
// 路由中间件要在最后
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)
let response = new Response(req, res)
response.set('X-Powered-By', this.get('X-Powered-By') || 'Five.js')
this.#loop(request, response)
})
.on('listening', _ => {
if (this.get('debug')) {
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)
})
.listen(port)
callback.call(this, this.#server)
return this
}
}
export function mount(dir) {
let list = fs.ls(dir) || []
return list
}
export function createApp(conf = {}) {
let app = new Five()
app.set(conf)
return app
}
一个轻量级的,易学的,拓展性灵活的 nodejs MVC 框架, 5 分钟即可上手。取自"Give me five"之意, 一切就是这么简单
JavaScript 100%