@kevisual/router 0.0.6-alpha-1 → 0.0.6-alpha-2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/router.d.ts CHANGED
@@ -2,6 +2,7 @@ import { Schema } from 'zod';
2
2
  export { Schema } from 'zod';
3
3
  import http, { IncomingMessage, ServerResponse } from 'http';
4
4
  import https from 'https';
5
+ import http2 from 'http2';
5
6
  import { WebSocketServer, WebSocket } from 'ws';
6
7
 
7
8
  type BaseRule = {
@@ -378,7 +379,7 @@ type ServerOpts = {
378
379
  [key: string]: any;
379
380
  }) => any;
380
381
  cors?: Cors;
381
- isHTTPS?: boolean;
382
+ httpType?: 'http' | 'https' | 'http2';
382
383
  httpsKey?: string;
383
384
  httpsCert?: string;
384
385
  };
@@ -389,7 +390,7 @@ declare class Server {
389
390
  private _callback;
390
391
  private cors;
391
392
  private hasOn;
392
- private isHTTPS;
393
+ private httpType;
393
394
  private options;
394
395
  constructor(opts?: ServerOpts);
395
396
  listen(port: number, hostname?: string, backlog?: number, listeningListener?: () => void): void;
@@ -400,7 +401,7 @@ declare class Server {
400
401
  listen(path: string, listeningListener?: () => void): void;
401
402
  listen(handle: any, backlog?: number, listeningListener?: () => void): void;
402
403
  listen(handle: any, listeningListener?: () => void): void;
403
- createServer(): http.Server<typeof http.IncomingMessage, typeof http.ServerResponse> | https.Server<typeof http.IncomingMessage, typeof http.ServerResponse>;
404
+ createServer(): http.Server<typeof http.IncomingMessage, typeof http.ServerResponse> | https.Server<typeof http.IncomingMessage, typeof http.ServerResponse> | http2.Http2SecureServer<typeof http.IncomingMessage, typeof http.ServerResponse, typeof http2.Http2ServerRequest, typeof http2.Http2ServerResponse>;
404
405
  setHandle(handle?: any): void;
405
406
  /**
406
407
  * get callback
@@ -416,7 +417,7 @@ declare class Server {
416
417
  */
417
418
  on(listener: Listener | Listener[]): void;
418
419
  get callback(): any;
419
- get server(): http.Server<typeof http.IncomingMessage, typeof http.ServerResponse>;
420
+ get server(): http.Server<typeof http.IncomingMessage, typeof http.ServerResponse> | https.Server<typeof http.IncomingMessage, typeof http.ServerResponse> | http2.Http2SecureServer<typeof http.IncomingMessage, typeof http.ServerResponse, typeof http2.Http2ServerRequest, typeof http2.Http2ServerResponse>;
420
421
  }
421
422
 
422
423
  /**
@@ -497,14 +498,7 @@ type AppOptions<T = {}> = {
497
498
  /** handle msg 关联 */
498
499
  routerHandle?: RouterHandle;
499
500
  routerContext?: RouteContext<T>;
500
- serverOptions?: {
501
- path?: string;
502
- cors?: Cors;
503
- handle?: any;
504
- isHTTPS?: boolean;
505
- httpsKey?: string;
506
- httpsCert?: string;
507
- };
501
+ serverOptions?: ServerOpts;
508
502
  io?: boolean;
509
503
  ioOpts?: {
510
504
  routerHandle?: RouterHandle;
package/dist/router.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import { webcrypto } from 'node:crypto';
2
2
  import http from 'http';
3
3
  import https from 'https';
4
+ import http2 from 'http2';
4
5
  import url from 'url';
5
6
  import { WebSocketServer } from 'ws';
6
7
 
@@ -6159,7 +6160,7 @@ class Server {
6159
6160
  _callback;
6160
6161
  cors;
6161
6162
  hasOn = false;
6162
- isHTTPS = false;
6163
+ httpType = 'http';
6163
6164
  options = {
6164
6165
  key: '',
6165
6166
  cert: '',
@@ -6168,7 +6169,7 @@ class Server {
6168
6169
  this.path = opts?.path || '/api/router';
6169
6170
  this.handle = opts?.handle;
6170
6171
  this.cors = opts?.cors;
6171
- this.isHTTPS = opts?.isHTTPS || false;
6172
+ this.httpType = opts?.httpType || 'http';
6172
6173
  this.options = {
6173
6174
  key: opts?.httpsKey || '',
6174
6175
  cert: opts?.httpsCert || '',
@@ -6182,13 +6183,26 @@ class Server {
6182
6183
  }
6183
6184
  createServer() {
6184
6185
  let server;
6185
- if (this.isHTTPS) {
6186
+ const httpType = this.httpType;
6187
+ if (httpType === 'https') {
6186
6188
  if (this.options.key && this.options.cert) {
6187
6189
  server = https.createServer({
6188
6190
  key: this.options.key,
6189
6191
  cert: this.options.cert,
6190
6192
  });
6191
- console.log('https server');
6193
+ return server;
6194
+ }
6195
+ else {
6196
+ console.error('https key and cert is required');
6197
+ console.log('downgrade to http');
6198
+ }
6199
+ }
6200
+ else if (httpType === 'http2') {
6201
+ if (this.options.key && this.options.cert) {
6202
+ server = http2.createSecureServer({
6203
+ key: this.options.key,
6204
+ cert: this.options.cert,
6205
+ });
6192
6206
  return server;
6193
6207
  }
6194
6208
  else {
@@ -6211,6 +6225,7 @@ class Server {
6211
6225
  const handle = this.handle;
6212
6226
  const cors = this.cors;
6213
6227
  const _callback = async (req, res) => {
6228
+ // only handle /api/router
6214
6229
  if (req.url === '/favicon.ico') {
6215
6230
  return;
6216
6231
  }
@@ -6235,7 +6250,7 @@ class Server {
6235
6250
  return;
6236
6251
  }
6237
6252
  }
6238
- res.writeHead(200); // 设置响应头,给予其他api知道headersSent,它已经被响应了
6253
+ res.writeHead(200); // 设置响应头,给予其他任何listen 知道headersSent,它已经被响应了
6239
6254
  const url = req.url;
6240
6255
  if (!url.startsWith(path)) {
6241
6256
  res.end(resultError(`not path:[${path}]`));
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package",
3
3
  "name": "@kevisual/router",
4
- "version": "0.0.6-alpha-1",
4
+ "version": "0.0.6-alpha-2",
5
5
  "description": "",
6
6
  "main": "dist/index.js",
7
7
  "module": "dist/index.js",