@yumerijs/core 3.0.1 → 3.0.3
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/context.js +8 -4
- package/dist/core.js +5 -1
- package/dist/route.d.ts +8 -2
- package/dist/route.js +27 -12
- package/package.json +1 -1
package/dist/context.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { Route } from './route.js';
|
|
2
1
|
import path from 'path';
|
|
3
2
|
/**
|
|
4
3
|
* 插件上下文对象
|
|
@@ -57,10 +56,15 @@ export class Context {
|
|
|
57
56
|
* @returns Route 实例
|
|
58
57
|
*/
|
|
59
58
|
route(routepath) {
|
|
60
|
-
|
|
59
|
+
// `root` is a special fallback route name, not a filesystem path.
|
|
60
|
+
// URL paths must use POSIX separators even when Yumeri runs on Windows.
|
|
61
|
+
const realpath = routepath === 'root'
|
|
62
|
+
? 'root'
|
|
63
|
+
: path.posix.join(this.childpath, routepath);
|
|
61
64
|
if (this.core.routes[realpath]) {
|
|
62
|
-
|
|
63
|
-
|
|
65
|
+
// Reuse the registered route so the same path can be split across
|
|
66
|
+
// multiple declarations, e.g. one GET handler and one POST handler.
|
|
67
|
+
return this.core.routes[realpath];
|
|
64
68
|
}
|
|
65
69
|
this.routes.push(realpath);
|
|
66
70
|
return this.core.route(realpath, this);
|
package/dist/core.js
CHANGED
|
@@ -228,6 +228,10 @@ export class Core {
|
|
|
228
228
|
const route = this.routes[routePath];
|
|
229
229
|
const result = route.match(pathname, session.client?.headers?.host);
|
|
230
230
|
if (result) {
|
|
231
|
+
const method = session.client?.req?.method ?? 'GET';
|
|
232
|
+
if (!route.hasHandler(method)) {
|
|
233
|
+
continue;
|
|
234
|
+
}
|
|
231
235
|
try {
|
|
232
236
|
this.emit('request:start', {
|
|
233
237
|
path: pathname,
|
|
@@ -254,7 +258,7 @@ export class Core {
|
|
|
254
258
|
}
|
|
255
259
|
else {
|
|
256
260
|
const start = Date.now();
|
|
257
|
-
await route.executeHandler(session, queryParams, result.pathParams, result.hostParams);
|
|
261
|
+
await route.executeHandler(session, queryParams, result.pathParams, result.hostParams, method);
|
|
258
262
|
this.emit('route:end', {
|
|
259
263
|
path: pathname,
|
|
260
264
|
route: routePath,
|
package/dist/route.d.ts
CHANGED
|
@@ -8,6 +8,7 @@ import { Middleware } from './middleware.js';
|
|
|
8
8
|
import { WebSocketServer } from 'ws';
|
|
9
9
|
import { Context } from './context.js';
|
|
10
10
|
export type RouteHandler = (session: Session, queryParams: URLSearchParams, ...pathParams: string[]) => Promise<void> | void;
|
|
11
|
+
type RouteMethod = string | string[];
|
|
11
12
|
/**
|
|
12
13
|
* Route class using segment-based matching (no fragile capture-group-index mapping).
|
|
13
14
|
* Behavior matches the SimpleRouter in the webpage:
|
|
@@ -20,7 +21,9 @@ export declare class Route {
|
|
|
20
21
|
path: string;
|
|
21
22
|
private segments;
|
|
22
23
|
private paramsInfo;
|
|
23
|
-
private
|
|
24
|
+
private handlers;
|
|
25
|
+
private pendingActionMethods;
|
|
26
|
+
private methodsConfigured;
|
|
24
27
|
middlewares: Middleware[];
|
|
25
28
|
allowedMethods: string[];
|
|
26
29
|
ws: WebSocketServer | null;
|
|
@@ -38,6 +41,7 @@ export declare class Route {
|
|
|
38
41
|
* @returns this
|
|
39
42
|
*/
|
|
40
43
|
action(handler: RouteHandler): this;
|
|
44
|
+
action(method: RouteMethod, handler: RouteHandler): this;
|
|
41
45
|
/**
|
|
42
46
|
* 设置允许的host
|
|
43
47
|
* @param host host列表
|
|
@@ -67,7 +71,8 @@ export declare class Route {
|
|
|
67
71
|
* @param params 查询参数
|
|
68
72
|
* @param pathParams 路由参数
|
|
69
73
|
*/
|
|
70
|
-
executeHandler(session: Session, params: URLSearchParams, pathParams: string[], hostParams: Record<string, string
|
|
74
|
+
executeHandler(session: Session, params: URLSearchParams, pathParams: string[], hostParams: Record<string, string>, method?: string): Promise<void>;
|
|
75
|
+
hasHandler(method: string): boolean;
|
|
71
76
|
/**
|
|
72
77
|
* 设置可用方法
|
|
73
78
|
* @param methods 可用方法
|
|
@@ -80,3 +85,4 @@ export declare class Route {
|
|
|
80
85
|
*/
|
|
81
86
|
wsOn(event: string, handler: (...args: any[]) => void): this;
|
|
82
87
|
}
|
|
88
|
+
export {};
|
package/dist/route.js
CHANGED
|
@@ -225,7 +225,9 @@ export class Route {
|
|
|
225
225
|
path;
|
|
226
226
|
segments;
|
|
227
227
|
paramsInfo;
|
|
228
|
-
|
|
228
|
+
handlers = {};
|
|
229
|
+
pendingActionMethods = null;
|
|
230
|
+
methodsConfigured = false;
|
|
229
231
|
middlewares = [];
|
|
230
232
|
allowedMethods = ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD'];
|
|
231
233
|
ws = null;
|
|
@@ -243,13 +245,17 @@ export class Route {
|
|
|
243
245
|
this.segments = segments;
|
|
244
246
|
this.paramsInfo = params;
|
|
245
247
|
}
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
248
|
+
action(methodOrHandler, maybeHandler) {
|
|
249
|
+
const handler = typeof methodOrHandler === 'function' ? methodOrHandler : maybeHandler;
|
|
250
|
+
if (!handler)
|
|
251
|
+
return this;
|
|
252
|
+
const methods = typeof methodOrHandler === 'function'
|
|
253
|
+
? (this.pendingActionMethods ?? this.allowedMethods)
|
|
254
|
+
: (Array.isArray(methodOrHandler) ? methodOrHandler : [methodOrHandler]);
|
|
255
|
+
for (const method of methods) {
|
|
256
|
+
this.handlers[method.toUpperCase()] = handler;
|
|
257
|
+
}
|
|
258
|
+
this.pendingActionMethods = null;
|
|
253
259
|
return this;
|
|
254
260
|
}
|
|
255
261
|
/**
|
|
@@ -459,18 +465,27 @@ export class Route {
|
|
|
459
465
|
* @param params 查询参数
|
|
460
466
|
* @param pathParams 路由参数
|
|
461
467
|
*/
|
|
462
|
-
async executeHandler(session, params, pathParams, hostParams) {
|
|
463
|
-
|
|
464
|
-
|
|
468
|
+
async executeHandler(session, params, pathParams, hostParams, method) {
|
|
469
|
+
const handler = this.handlers[(method || session.client?.req?.method || 'GET').toUpperCase()];
|
|
470
|
+
if (handler) {
|
|
471
|
+
await handler(session, params, ...pathParams, ...Object.values(hostParams));
|
|
465
472
|
}
|
|
466
473
|
}
|
|
474
|
+
hasHandler(method) {
|
|
475
|
+
return !!this.handlers[method.toUpperCase()];
|
|
476
|
+
}
|
|
467
477
|
/**
|
|
468
478
|
* 设置可用方法
|
|
469
479
|
* @param methods 可用方法
|
|
470
480
|
* @returns this
|
|
471
481
|
*/
|
|
472
482
|
methods(...methods) {
|
|
473
|
-
|
|
483
|
+
const normalized = methods.map(method => method.toUpperCase());
|
|
484
|
+
this.allowedMethods = this.methodsConfigured
|
|
485
|
+
? [...new Set([...this.allowedMethods, ...normalized])]
|
|
486
|
+
: normalized;
|
|
487
|
+
this.methodsConfigured = true;
|
|
488
|
+
this.pendingActionMethods = normalized;
|
|
474
489
|
return this;
|
|
475
490
|
}
|
|
476
491
|
/**
|