@kevisual/router 0.0.4-alpha-7 → 0.0.4
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-browser.d.ts +2 -0
- package/dist/router-browser.js +6 -1
- package/dist/router-sign.d.ts +15 -0
- package/dist/router-sign.js +28654 -0
- package/dist/router.d.ts +12 -0
- package/dist/router.js +40 -4
- package/package.json +7 -2
package/dist/router.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Schema } from 'zod';
|
|
2
2
|
export { Schema } from 'zod';
|
|
3
3
|
import http, { IncomingMessage, ServerResponse } from 'http';
|
|
4
|
+
import https from 'https';
|
|
4
5
|
import { WebSocketServer, WebSocket } from 'ws';
|
|
5
6
|
|
|
6
7
|
type BaseRule = {
|
|
@@ -190,6 +191,7 @@ declare class Route {
|
|
|
190
191
|
declare class QueryRouter {
|
|
191
192
|
routes: Route[];
|
|
192
193
|
maxNextRoute: number;
|
|
194
|
+
context?: RouteContext;
|
|
193
195
|
constructor();
|
|
194
196
|
add(route: Route): void;
|
|
195
197
|
/**
|
|
@@ -233,6 +235,7 @@ declare class QueryRouter {
|
|
|
233
235
|
}, ctx?: RouteContext & {
|
|
234
236
|
[key: string]: any;
|
|
235
237
|
}): Promise<any>;
|
|
238
|
+
setContext(ctx: RouteContext): Promise<void>;
|
|
236
239
|
getList(): RouteInfo[];
|
|
237
240
|
getHandle<T = any>(router: QueryRouter, wrapperFn?: HandleFn<T>, ctx?: RouteContext): (msg: {
|
|
238
241
|
path: string;
|
|
@@ -345,6 +348,9 @@ type ServerOpts = {
|
|
|
345
348
|
[key: string]: any;
|
|
346
349
|
}) => any;
|
|
347
350
|
cors?: Cors;
|
|
351
|
+
isHTTPS?: boolean;
|
|
352
|
+
httpsKey?: string;
|
|
353
|
+
httpsCert?: string;
|
|
348
354
|
};
|
|
349
355
|
declare class Server {
|
|
350
356
|
path: string;
|
|
@@ -353,6 +359,8 @@ declare class Server {
|
|
|
353
359
|
private _callback;
|
|
354
360
|
private cors;
|
|
355
361
|
private hasOn;
|
|
362
|
+
private isHTTPS;
|
|
363
|
+
private options;
|
|
356
364
|
constructor(opts?: ServerOpts);
|
|
357
365
|
listen(port: number, hostname?: string, backlog?: number, listeningListener?: () => void): void;
|
|
358
366
|
listen(port: number, hostname?: string, listeningListener?: () => void): void;
|
|
@@ -362,6 +370,7 @@ declare class Server {
|
|
|
362
370
|
listen(path: string, listeningListener?: () => void): void;
|
|
363
371
|
listen(handle: any, backlog?: number, listeningListener?: () => void): void;
|
|
364
372
|
listen(handle: any, listeningListener?: () => void): void;
|
|
373
|
+
createServer(): http.Server<typeof http.IncomingMessage, typeof http.ServerResponse> | https.Server<typeof http.IncomingMessage, typeof http.ServerResponse>;
|
|
365
374
|
setHandle(handle?: any): void;
|
|
366
375
|
/**
|
|
367
376
|
* get callback
|
|
@@ -462,6 +471,9 @@ type AppOptions<T = {}> = {
|
|
|
462
471
|
path?: string;
|
|
463
472
|
cors?: Cors;
|
|
464
473
|
handle?: any;
|
|
474
|
+
isHTTPS?: boolean;
|
|
475
|
+
httpsKey?: string;
|
|
476
|
+
httpsCert?: string;
|
|
465
477
|
};
|
|
466
478
|
io?: boolean;
|
|
467
479
|
ioOpts?: {
|
package/dist/router.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { webcrypto } from 'node:crypto';
|
|
2
2
|
import http from 'http';
|
|
3
|
+
import https from 'https';
|
|
3
4
|
import url from 'url';
|
|
4
5
|
import { WebSocketServer } from 'ws';
|
|
5
6
|
|
|
@@ -5659,6 +5660,7 @@ class Route {
|
|
|
5659
5660
|
class QueryRouter {
|
|
5660
5661
|
routes;
|
|
5661
5662
|
maxNextRoute = 40;
|
|
5663
|
+
context = {}; // default context for call
|
|
5662
5664
|
constructor() {
|
|
5663
5665
|
this.routes = [];
|
|
5664
5666
|
}
|
|
@@ -5885,7 +5887,10 @@ class QueryRouter {
|
|
|
5885
5887
|
return await this.runRoute(path, key, ctx);
|
|
5886
5888
|
}
|
|
5887
5889
|
async call(message, ctx) {
|
|
5888
|
-
return await this.parse(message, ctx);
|
|
5890
|
+
return await this.parse(message, { ...this.context, ...ctx });
|
|
5891
|
+
}
|
|
5892
|
+
async setContext(ctx) {
|
|
5893
|
+
this.context = ctx;
|
|
5889
5894
|
}
|
|
5890
5895
|
getList() {
|
|
5891
5896
|
return this.routes.map((r) => {
|
|
@@ -5930,6 +5935,7 @@ class QueryRouterServer extends QueryRouter {
|
|
|
5930
5935
|
constructor(opts) {
|
|
5931
5936
|
super();
|
|
5932
5937
|
this.handle = this.getHandle(this, opts?.handleFn, opts?.context);
|
|
5938
|
+
this.setContext(opts?.context);
|
|
5933
5939
|
}
|
|
5934
5940
|
setHandle(wrapperFn, ctx) {
|
|
5935
5941
|
this.handle = this.getHandle(this, wrapperFn, ctx);
|
|
@@ -6129,17 +6135,46 @@ class Server {
|
|
|
6129
6135
|
_callback;
|
|
6130
6136
|
cors;
|
|
6131
6137
|
hasOn = false;
|
|
6138
|
+
isHTTPS = false;
|
|
6139
|
+
options = {
|
|
6140
|
+
key: '',
|
|
6141
|
+
cert: '',
|
|
6142
|
+
};
|
|
6132
6143
|
constructor(opts) {
|
|
6133
6144
|
this.path = opts?.path || '/api/router';
|
|
6134
6145
|
this.handle = opts?.handle;
|
|
6135
6146
|
this.cors = opts?.cors;
|
|
6147
|
+
this.isHTTPS = opts?.isHTTPS || false;
|
|
6148
|
+
this.options = {
|
|
6149
|
+
key: opts?.httpsKey || '',
|
|
6150
|
+
cert: opts?.httpsCert || '',
|
|
6151
|
+
};
|
|
6136
6152
|
}
|
|
6137
6153
|
listen(...args) {
|
|
6138
|
-
this._server =
|
|
6154
|
+
this._server = this.createServer();
|
|
6139
6155
|
const callback = this.createCallback();
|
|
6140
6156
|
this._server.on('request', callback);
|
|
6141
6157
|
this._server.listen(...args);
|
|
6142
6158
|
}
|
|
6159
|
+
createServer() {
|
|
6160
|
+
let server;
|
|
6161
|
+
if (this.isHTTPS) {
|
|
6162
|
+
if (this.options.key && this.options.cert) {
|
|
6163
|
+
server = https.createServer({
|
|
6164
|
+
key: this.options.key,
|
|
6165
|
+
cert: this.options.cert,
|
|
6166
|
+
});
|
|
6167
|
+
console.log('https server');
|
|
6168
|
+
return server;
|
|
6169
|
+
}
|
|
6170
|
+
else {
|
|
6171
|
+
console.error('https key and cert is required');
|
|
6172
|
+
console.log('downgrade to http');
|
|
6173
|
+
}
|
|
6174
|
+
}
|
|
6175
|
+
server = http.createServer();
|
|
6176
|
+
return server;
|
|
6177
|
+
}
|
|
6143
6178
|
setHandle(handle) {
|
|
6144
6179
|
this.handle = handle;
|
|
6145
6180
|
}
|
|
@@ -6221,7 +6256,7 @@ class Server {
|
|
|
6221
6256
|
* @param listener
|
|
6222
6257
|
*/
|
|
6223
6258
|
on(listener) {
|
|
6224
|
-
this._server = this._server ||
|
|
6259
|
+
this._server = this._server || this.createServer();
|
|
6225
6260
|
this._server.removeAllListeners('request');
|
|
6226
6261
|
this.hasOn = true;
|
|
6227
6262
|
if (Array.isArray(listener)) {
|
|
@@ -6402,6 +6437,7 @@ class App {
|
|
|
6402
6437
|
const router = opts?.router || new QueryRouter();
|
|
6403
6438
|
const server = opts?.server || new Server(opts?.serverOptions || {});
|
|
6404
6439
|
server.setHandle(router.getHandle(router, opts?.routerHandle, opts?.routerContext));
|
|
6440
|
+
router.setContext(opts?.routerContext);
|
|
6405
6441
|
this.router = router;
|
|
6406
6442
|
this.server = server;
|
|
6407
6443
|
if (opts?.io) {
|
|
@@ -6443,7 +6479,7 @@ class App {
|
|
|
6443
6479
|
}
|
|
6444
6480
|
async call(message, ctx) {
|
|
6445
6481
|
const router = this.router;
|
|
6446
|
-
return await router.
|
|
6482
|
+
return await router.call(message, ctx);
|
|
6447
6483
|
}
|
|
6448
6484
|
exportRoutes() {
|
|
6449
6485
|
return this.router.exportRoutes();
|
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.4
|
|
4
|
+
"version": "0.0.4",
|
|
5
5
|
"description": "",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"module": "dist/index.js",
|
|
@@ -41,6 +41,7 @@
|
|
|
41
41
|
"url": "git+https://github.com/abearxiong/kevisual-router.git"
|
|
42
42
|
},
|
|
43
43
|
"dependencies": {
|
|
44
|
+
"selfsigned": "^2.4.1",
|
|
44
45
|
"ws": "^8.18.0"
|
|
45
46
|
},
|
|
46
47
|
"publishConfig": {
|
|
@@ -54,6 +55,10 @@
|
|
|
54
55
|
"./browser": {
|
|
55
56
|
"import": "./dist/router-browser.js",
|
|
56
57
|
"require": "./dist/router-browser.js"
|
|
58
|
+
},
|
|
59
|
+
"./sign": {
|
|
60
|
+
"import": "./dist/router-sign.js",
|
|
61
|
+
"require": "./dist/router-sign.js"
|
|
57
62
|
}
|
|
58
63
|
}
|
|
59
|
-
}
|
|
64
|
+
}
|