@kevisual/router 0.0.5 → 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-browser.d.ts +40 -10
- package/dist/router-browser.js +26 -3
- package/dist/router.d.ts +54 -23
- package/dist/router.js +49 -8
- package/package.json +1 -1
package/dist/router-browser.d.ts
CHANGED
|
@@ -65,9 +65,18 @@ type RouteContext<T = {
|
|
|
65
65
|
end?: boolean;
|
|
66
66
|
queryRouter?: QueryRouter;
|
|
67
67
|
error?: any;
|
|
68
|
+
/** 请求 route的返回结果,包函ctx */
|
|
68
69
|
call?: (message: {
|
|
69
70
|
path: string;
|
|
70
|
-
key
|
|
71
|
+
key?: string;
|
|
72
|
+
payload?: any;
|
|
73
|
+
}, ctx?: RouteContext & {
|
|
74
|
+
[key: string]: any;
|
|
75
|
+
}) => Promise<any>;
|
|
76
|
+
/** 请求 route的返回结果,不包函ctx */
|
|
77
|
+
queryRoute?: (message: {
|
|
78
|
+
path: string;
|
|
79
|
+
key?: string;
|
|
71
80
|
payload?: any;
|
|
72
81
|
}, ctx?: RouteContext & {
|
|
73
82
|
[key: string]: any;
|
|
@@ -225,13 +234,36 @@ declare class QueryRouter {
|
|
|
225
234
|
}, ctx?: RouteContext & {
|
|
226
235
|
[key: string]: any;
|
|
227
236
|
}): Promise<any>;
|
|
237
|
+
/**
|
|
238
|
+
* 返回的数据包含所有的context的请求返回的内容,可做其他处理
|
|
239
|
+
* @param message
|
|
240
|
+
* @param ctx
|
|
241
|
+
* @returns
|
|
242
|
+
*/
|
|
228
243
|
call(message: {
|
|
229
244
|
path: string;
|
|
230
|
-
key
|
|
245
|
+
key?: string;
|
|
231
246
|
payload?: any;
|
|
232
247
|
}, ctx?: RouteContext & {
|
|
233
248
|
[key: string]: any;
|
|
234
249
|
}): Promise<any>;
|
|
250
|
+
/**
|
|
251
|
+
* 请求 result 的数据
|
|
252
|
+
* @param message
|
|
253
|
+
* @param ctx
|
|
254
|
+
* @returns
|
|
255
|
+
*/
|
|
256
|
+
queryRoute(message: {
|
|
257
|
+
path: string;
|
|
258
|
+
key?: string;
|
|
259
|
+
payload?: any;
|
|
260
|
+
}, ctx?: RouteContext & {
|
|
261
|
+
[key: string]: any;
|
|
262
|
+
}): Promise<{
|
|
263
|
+
code: any;
|
|
264
|
+
data: any;
|
|
265
|
+
message: any;
|
|
266
|
+
}>;
|
|
235
267
|
setContext(ctx: RouteContext): Promise<void>;
|
|
236
268
|
getList(): RouteInfo[];
|
|
237
269
|
getHandle<T = any>(router: QueryRouter, wrapperFn?: HandleFn<T>, ctx?: RouteContext): (msg: {
|
|
@@ -284,16 +316,14 @@ declare class QueryRouterServer extends QueryRouter {
|
|
|
284
316
|
route(path: string, key?: string): Route;
|
|
285
317
|
route(path: string, opts?: RouteOpts): Route;
|
|
286
318
|
route(path: string, key?: string, opts?: RouteOpts): Route;
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
[key: string]: any;
|
|
293
|
-
}): Promise<any>;
|
|
319
|
+
/**
|
|
320
|
+
* 等于queryRoute,但是调用了handle
|
|
321
|
+
* @param param0
|
|
322
|
+
* @returns
|
|
323
|
+
*/
|
|
294
324
|
run({ path, key, payload }: {
|
|
295
325
|
path: string;
|
|
296
|
-
key
|
|
326
|
+
key?: string;
|
|
297
327
|
payload?: any;
|
|
298
328
|
}): Promise<any>;
|
|
299
329
|
}
|
package/dist/router-browser.js
CHANGED
|
@@ -5865,12 +5865,33 @@ class QueryRouter {
|
|
|
5865
5865
|
// TODO: 是否需要queryRouter,函数内部处理router路由执行,这应该是避免去内部去包含的功能过
|
|
5866
5866
|
ctx.queryRouter = this;
|
|
5867
5867
|
ctx.call = this.call.bind(this);
|
|
5868
|
+
ctx.queryRoute = this.queryRoute.bind(this);
|
|
5868
5869
|
ctx.index = 0;
|
|
5869
5870
|
return await this.runRoute(path, key, ctx);
|
|
5870
5871
|
}
|
|
5872
|
+
/**
|
|
5873
|
+
* 返回的数据包含所有的context的请求返回的内容,可做其他处理
|
|
5874
|
+
* @param message
|
|
5875
|
+
* @param ctx
|
|
5876
|
+
* @returns
|
|
5877
|
+
*/
|
|
5871
5878
|
async call(message, ctx) {
|
|
5872
5879
|
return await this.parse(message, { ...this.context, ...ctx });
|
|
5873
5880
|
}
|
|
5881
|
+
/**
|
|
5882
|
+
* 请求 result 的数据
|
|
5883
|
+
* @param message
|
|
5884
|
+
* @param ctx
|
|
5885
|
+
* @returns
|
|
5886
|
+
*/
|
|
5887
|
+
async queryRoute(message, ctx) {
|
|
5888
|
+
const res = await this.parse(message, { ...this.context, ...ctx });
|
|
5889
|
+
return {
|
|
5890
|
+
code: res.code,
|
|
5891
|
+
data: res.body,
|
|
5892
|
+
message: res.message,
|
|
5893
|
+
};
|
|
5894
|
+
}
|
|
5874
5895
|
async setContext(ctx) {
|
|
5875
5896
|
this.context = ctx;
|
|
5876
5897
|
}
|
|
@@ -5947,9 +5968,11 @@ class QueryRouterServer extends QueryRouter {
|
|
|
5947
5968
|
}
|
|
5948
5969
|
return new Route(path, key, opts);
|
|
5949
5970
|
}
|
|
5950
|
-
|
|
5951
|
-
|
|
5952
|
-
|
|
5971
|
+
/**
|
|
5972
|
+
* 等于queryRoute,但是调用了handle
|
|
5973
|
+
* @param param0
|
|
5974
|
+
* @returns
|
|
5975
|
+
*/
|
|
5953
5976
|
async run({ path, key, payload }) {
|
|
5954
5977
|
const handle = this.handle;
|
|
5955
5978
|
const resultError = (error, code = 500) => {
|
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 = {
|
|
@@ -68,9 +69,18 @@ type RouteContext<T = {
|
|
|
68
69
|
end?: boolean;
|
|
69
70
|
queryRouter?: QueryRouter;
|
|
70
71
|
error?: any;
|
|
72
|
+
/** 请求 route的返回结果,包函ctx */
|
|
71
73
|
call?: (message: {
|
|
72
74
|
path: string;
|
|
73
|
-
key
|
|
75
|
+
key?: string;
|
|
76
|
+
payload?: any;
|
|
77
|
+
}, ctx?: RouteContext & {
|
|
78
|
+
[key: string]: any;
|
|
79
|
+
}) => Promise<any>;
|
|
80
|
+
/** 请求 route的返回结果,不包函ctx */
|
|
81
|
+
queryRoute?: (message: {
|
|
82
|
+
path: string;
|
|
83
|
+
key?: string;
|
|
74
84
|
payload?: any;
|
|
75
85
|
}, ctx?: RouteContext & {
|
|
76
86
|
[key: string]: any;
|
|
@@ -228,13 +238,36 @@ declare class QueryRouter {
|
|
|
228
238
|
}, ctx?: RouteContext & {
|
|
229
239
|
[key: string]: any;
|
|
230
240
|
}): Promise<any>;
|
|
241
|
+
/**
|
|
242
|
+
* 返回的数据包含所有的context的请求返回的内容,可做其他处理
|
|
243
|
+
* @param message
|
|
244
|
+
* @param ctx
|
|
245
|
+
* @returns
|
|
246
|
+
*/
|
|
231
247
|
call(message: {
|
|
232
248
|
path: string;
|
|
233
|
-
key
|
|
249
|
+
key?: string;
|
|
234
250
|
payload?: any;
|
|
235
251
|
}, ctx?: RouteContext & {
|
|
236
252
|
[key: string]: any;
|
|
237
253
|
}): Promise<any>;
|
|
254
|
+
/**
|
|
255
|
+
* 请求 result 的数据
|
|
256
|
+
* @param message
|
|
257
|
+
* @param ctx
|
|
258
|
+
* @returns
|
|
259
|
+
*/
|
|
260
|
+
queryRoute(message: {
|
|
261
|
+
path: string;
|
|
262
|
+
key?: string;
|
|
263
|
+
payload?: any;
|
|
264
|
+
}, ctx?: RouteContext & {
|
|
265
|
+
[key: string]: any;
|
|
266
|
+
}): Promise<{
|
|
267
|
+
code: any;
|
|
268
|
+
data: any;
|
|
269
|
+
message: any;
|
|
270
|
+
}>;
|
|
238
271
|
setContext(ctx: RouteContext): Promise<void>;
|
|
239
272
|
getList(): RouteInfo[];
|
|
240
273
|
getHandle<T = any>(router: QueryRouter, wrapperFn?: HandleFn<T>, ctx?: RouteContext): (msg: {
|
|
@@ -287,16 +320,14 @@ declare class QueryRouterServer extends QueryRouter {
|
|
|
287
320
|
route(path: string, key?: string): Route;
|
|
288
321
|
route(path: string, opts?: RouteOpts): Route;
|
|
289
322
|
route(path: string, key?: string, opts?: RouteOpts): Route;
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
[key: string]: any;
|
|
296
|
-
}): Promise<any>;
|
|
323
|
+
/**
|
|
324
|
+
* 等于queryRoute,但是调用了handle
|
|
325
|
+
* @param param0
|
|
326
|
+
* @returns
|
|
327
|
+
*/
|
|
297
328
|
run({ path, key, payload }: {
|
|
298
329
|
path: string;
|
|
299
|
-
key
|
|
330
|
+
key?: string;
|
|
300
331
|
payload?: any;
|
|
301
332
|
}): Promise<any>;
|
|
302
333
|
}
|
|
@@ -348,7 +379,7 @@ type ServerOpts = {
|
|
|
348
379
|
[key: string]: any;
|
|
349
380
|
}) => any;
|
|
350
381
|
cors?: Cors;
|
|
351
|
-
|
|
382
|
+
httpType?: 'http' | 'https' | 'http2';
|
|
352
383
|
httpsKey?: string;
|
|
353
384
|
httpsCert?: string;
|
|
354
385
|
};
|
|
@@ -359,7 +390,7 @@ declare class Server {
|
|
|
359
390
|
private _callback;
|
|
360
391
|
private cors;
|
|
361
392
|
private hasOn;
|
|
362
|
-
private
|
|
393
|
+
private httpType;
|
|
363
394
|
private options;
|
|
364
395
|
constructor(opts?: ServerOpts);
|
|
365
396
|
listen(port: number, hostname?: string, backlog?: number, listeningListener?: () => void): void;
|
|
@@ -370,7 +401,7 @@ declare class Server {
|
|
|
370
401
|
listen(path: string, listeningListener?: () => void): void;
|
|
371
402
|
listen(handle: any, backlog?: number, listeningListener?: () => void): void;
|
|
372
403
|
listen(handle: any, listeningListener?: () => void): void;
|
|
373
|
-
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>;
|
|
374
405
|
setHandle(handle?: any): void;
|
|
375
406
|
/**
|
|
376
407
|
* get callback
|
|
@@ -386,7 +417,7 @@ declare class Server {
|
|
|
386
417
|
*/
|
|
387
418
|
on(listener: Listener | Listener[]): void;
|
|
388
419
|
get callback(): any;
|
|
389
|
-
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>;
|
|
390
421
|
}
|
|
391
422
|
|
|
392
423
|
/**
|
|
@@ -467,14 +498,7 @@ type AppOptions<T = {}> = {
|
|
|
467
498
|
/** handle msg 关联 */
|
|
468
499
|
routerHandle?: RouterHandle;
|
|
469
500
|
routerContext?: RouteContext<T>;
|
|
470
|
-
serverOptions?:
|
|
471
|
-
path?: string;
|
|
472
|
-
cors?: Cors;
|
|
473
|
-
handle?: any;
|
|
474
|
-
isHTTPS?: boolean;
|
|
475
|
-
httpsKey?: string;
|
|
476
|
-
httpsCert?: string;
|
|
477
|
-
};
|
|
501
|
+
serverOptions?: ServerOpts;
|
|
478
502
|
io?: boolean;
|
|
479
503
|
ioOpts?: {
|
|
480
504
|
routerHandle?: RouterHandle;
|
|
@@ -505,11 +529,18 @@ declare class App<T = {}> {
|
|
|
505
529
|
route(path: string, key?: string, opts?: RouteOpts): Route;
|
|
506
530
|
call(message: {
|
|
507
531
|
path: string;
|
|
508
|
-
key
|
|
532
|
+
key?: string;
|
|
509
533
|
payload?: any;
|
|
510
534
|
}, ctx?: RouteContext & {
|
|
511
535
|
[key: string]: any;
|
|
512
536
|
}): Promise<any>;
|
|
537
|
+
queryRoute(path: string, key?: string, payload?: any, ctx?: RouteContext & {
|
|
538
|
+
[key: string]: any;
|
|
539
|
+
}): Promise<{
|
|
540
|
+
code: any;
|
|
541
|
+
data: any;
|
|
542
|
+
message: any;
|
|
543
|
+
}>;
|
|
513
544
|
exportRoutes(): Route[];
|
|
514
545
|
importRoutes(routes: any[]): void;
|
|
515
546
|
importApp(app: App): void;
|
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
|
|
|
@@ -5884,12 +5885,33 @@ class QueryRouter {
|
|
|
5884
5885
|
// TODO: 是否需要queryRouter,函数内部处理router路由执行,这应该是避免去内部去包含的功能过
|
|
5885
5886
|
ctx.queryRouter = this;
|
|
5886
5887
|
ctx.call = this.call.bind(this);
|
|
5888
|
+
ctx.queryRoute = this.queryRoute.bind(this);
|
|
5887
5889
|
ctx.index = 0;
|
|
5888
5890
|
return await this.runRoute(path, key, ctx);
|
|
5889
5891
|
}
|
|
5892
|
+
/**
|
|
5893
|
+
* 返回的数据包含所有的context的请求返回的内容,可做其他处理
|
|
5894
|
+
* @param message
|
|
5895
|
+
* @param ctx
|
|
5896
|
+
* @returns
|
|
5897
|
+
*/
|
|
5890
5898
|
async call(message, ctx) {
|
|
5891
5899
|
return await this.parse(message, { ...this.context, ...ctx });
|
|
5892
5900
|
}
|
|
5901
|
+
/**
|
|
5902
|
+
* 请求 result 的数据
|
|
5903
|
+
* @param message
|
|
5904
|
+
* @param ctx
|
|
5905
|
+
* @returns
|
|
5906
|
+
*/
|
|
5907
|
+
async queryRoute(message, ctx) {
|
|
5908
|
+
const res = await this.parse(message, { ...this.context, ...ctx });
|
|
5909
|
+
return {
|
|
5910
|
+
code: res.code,
|
|
5911
|
+
data: res.body,
|
|
5912
|
+
message: res.message,
|
|
5913
|
+
};
|
|
5914
|
+
}
|
|
5893
5915
|
async setContext(ctx) {
|
|
5894
5916
|
this.context = ctx;
|
|
5895
5917
|
}
|
|
@@ -5966,9 +5988,11 @@ class QueryRouterServer extends QueryRouter {
|
|
|
5966
5988
|
}
|
|
5967
5989
|
return new Route(path, key, opts);
|
|
5968
5990
|
}
|
|
5969
|
-
|
|
5970
|
-
|
|
5971
|
-
|
|
5991
|
+
/**
|
|
5992
|
+
* 等于queryRoute,但是调用了handle
|
|
5993
|
+
* @param param0
|
|
5994
|
+
* @returns
|
|
5995
|
+
*/
|
|
5972
5996
|
async run({ path, key, payload }) {
|
|
5973
5997
|
const handle = this.handle;
|
|
5974
5998
|
const resultError = (error, code = 500) => {
|
|
@@ -6136,7 +6160,7 @@ class Server {
|
|
|
6136
6160
|
_callback;
|
|
6137
6161
|
cors;
|
|
6138
6162
|
hasOn = false;
|
|
6139
|
-
|
|
6163
|
+
httpType = 'http';
|
|
6140
6164
|
options = {
|
|
6141
6165
|
key: '',
|
|
6142
6166
|
cert: '',
|
|
@@ -6145,7 +6169,7 @@ class Server {
|
|
|
6145
6169
|
this.path = opts?.path || '/api/router';
|
|
6146
6170
|
this.handle = opts?.handle;
|
|
6147
6171
|
this.cors = opts?.cors;
|
|
6148
|
-
this.
|
|
6172
|
+
this.httpType = opts?.httpType || 'http';
|
|
6149
6173
|
this.options = {
|
|
6150
6174
|
key: opts?.httpsKey || '',
|
|
6151
6175
|
cert: opts?.httpsCert || '',
|
|
@@ -6159,13 +6183,26 @@ class Server {
|
|
|
6159
6183
|
}
|
|
6160
6184
|
createServer() {
|
|
6161
6185
|
let server;
|
|
6162
|
-
|
|
6186
|
+
const httpType = this.httpType;
|
|
6187
|
+
if (httpType === 'https') {
|
|
6163
6188
|
if (this.options.key && this.options.cert) {
|
|
6164
6189
|
server = https.createServer({
|
|
6165
6190
|
key: this.options.key,
|
|
6166
6191
|
cert: this.options.cert,
|
|
6167
6192
|
});
|
|
6168
|
-
|
|
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
|
+
});
|
|
6169
6206
|
return server;
|
|
6170
6207
|
}
|
|
6171
6208
|
else {
|
|
@@ -6188,6 +6225,7 @@ class Server {
|
|
|
6188
6225
|
const handle = this.handle;
|
|
6189
6226
|
const cors = this.cors;
|
|
6190
6227
|
const _callback = async (req, res) => {
|
|
6228
|
+
// only handle /api/router
|
|
6191
6229
|
if (req.url === '/favicon.ico') {
|
|
6192
6230
|
return;
|
|
6193
6231
|
}
|
|
@@ -6212,7 +6250,7 @@ class Server {
|
|
|
6212
6250
|
return;
|
|
6213
6251
|
}
|
|
6214
6252
|
}
|
|
6215
|
-
res.writeHead(200); //
|
|
6253
|
+
res.writeHead(200); // 设置响应头,给予其他任何listen 知道headersSent,它已经被响应了
|
|
6216
6254
|
const url = req.url;
|
|
6217
6255
|
if (!url.startsWith(path)) {
|
|
6218
6256
|
res.end(resultError(`not path:[${path}]`));
|
|
@@ -6482,6 +6520,9 @@ class App {
|
|
|
6482
6520
|
const router = this.router;
|
|
6483
6521
|
return await router.call(message, ctx);
|
|
6484
6522
|
}
|
|
6523
|
+
async queryRoute(path, key, payload, ctx) {
|
|
6524
|
+
return await this.router.queryRoute({ path, key, payload }, ctx);
|
|
6525
|
+
}
|
|
6485
6526
|
exportRoutes() {
|
|
6486
6527
|
return this.router.exportRoutes();
|
|
6487
6528
|
}
|