diff --git a/package.json b/package.json index 4a327ca..d847d93 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "$schema": "https://json.schemastore.org/package", "name": "@kevisual/router", - "version": "0.0.6-alpha-1", + "version": "0.0.6-alpha-2", "description": "", "main": "dist/index.js", "module": "dist/index.js", diff --git a/src/app.ts b/src/app.ts index d5d1f6f..76d260c 100644 --- a/src/app.ts +++ b/src/app.ts @@ -1,5 +1,5 @@ import { QueryRouter, Route, RouteContext, RouteOpts } from './route.ts'; -import { Server, Cors } from './server/server.ts'; +import { Server, Cors, ServerOpts } from './server/server.ts'; import { WsServer } from './server/ws-server.ts'; import { CustomError } from './result/error.ts'; @@ -10,14 +10,7 @@ type AppOptions = { /** handle msg 关联 */ routerHandle?: RouterHandle; routerContext?: RouteContext; - serverOptions?: { - path?: string; - cors?: Cors; - handle?: any; - isHTTPS?: boolean; - httpsKey?: string; - httpsCert?: string; - }; + serverOptions?: ServerOpts; io?: boolean; ioOpts?: { routerHandle?: RouterHandle; routerContext?: RouteContext; path?: string }; }; diff --git a/src/server/server.ts b/src/server/server.ts index ac75881..44551de 100644 --- a/src/server/server.ts +++ b/src/server/server.ts @@ -1,5 +1,6 @@ import http, { IncomingMessage, ServerResponse } from 'http'; import https from 'https'; +import http2 from 'http2'; import { handleServer } from './handle-server.ts'; export type Listener = (...args: any[]) => void; @@ -15,7 +16,7 @@ export type ServerOpts = { /**handle Fn */ handle?: (msg?: { path: string; key?: string; [key: string]: any }) => any; cors?: Cors; - isHTTPS?: boolean; + httpType?: 'http' | 'https' | 'http2'; httpsKey?: string; httpsCert?: string; }; @@ -29,12 +30,12 @@ export const resultError = (error: string, code = 500) => { export class Server { path = '/api/router'; - private _server: http.Server; + private _server: http.Server | https.Server | http2.Http2SecureServer; public handle: ServerOpts['handle']; private _callback: any; private cors: Cors; private hasOn = false; - private isHTTPS = false; + private httpType = 'http'; private options = { key: '', cert: '', @@ -43,7 +44,7 @@ export class Server { this.path = opts?.path || '/api/router'; this.handle = opts?.handle; this.cors = opts?.cors; - this.isHTTPS = opts?.isHTTPS || false; + this.httpType = opts?.httpType || 'http'; this.options = { key: opts?.httpsKey || '', cert: opts?.httpsCert || '', @@ -64,14 +65,25 @@ export class Server { this._server.listen(...args); } createServer() { - let server: http.Server | https.Server; - if (this.isHTTPS) { + let server: http.Server | https.Server | http2.Http2SecureServer; + const httpType = this.httpType; + if (httpType === 'https') { if (this.options.key && this.options.cert) { server = https.createServer({ key: this.options.key, cert: this.options.cert, }); - console.log('https server'); + return server; + } else { + console.error('https key and cert is required'); + console.log('downgrade to http'); + } + } else if (httpType === 'http2') { + if (this.options.key && this.options.cert) { + server = http2.createSecureServer({ + key: this.options.key, + cert: this.options.cert, + }); return server; } else { console.error('https key and cert is required'); @@ -93,6 +105,7 @@ export class Server { const handle = this.handle; const cors = this.cors; const _callback = async (req: IncomingMessage, res: ServerResponse) => { + // only handle /api/router if (req.url === '/favicon.ico') { return; } @@ -118,7 +131,7 @@ export class Server { return; } } - res.writeHead(200); // 设置响应头,给予其他api知道headersSent,它已经被响应了 + res.writeHead(200); // 设置响应头,给予其他任何listen 知道headersSent,它已经被响应了 const url = req.url; if (!url.startsWith(path)) { diff --git a/src/server/ws-server.ts b/src/server/ws-server.ts index 52eb4bd..22b9937 100644 --- a/src/server/ws-server.ts +++ b/src/server/ws-server.ts @@ -4,7 +4,7 @@ import { parseIfJson } from '../utils/parse.ts'; export const createWsServer = (server: Server) => { // 将 WebSocket 服务器附加到 HTTP 服务器 - const wss = new WebSocketServer({ server: server.server }); + const wss = new WebSocketServer({ server: server.server as any }); return wss; }; type WsServerBaseOpts = {