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