elit 3.1.2 → 3.1.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/build.d.mts +1 -1
- package/dist/cli.js +1 -1
- package/dist/{server-BvYkTRi4.d.ts → server-BDgmlULi.d.ts} +9 -8
- package/dist/{server-XT4I28Cr.d.mts → server-QfJoaxeO.d.mts} +9 -8
- package/dist/server.d.mts +1 -1
- package/dist/server.d.ts +9 -8
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +59 -7
- package/dist/server.mjs +59 -7
- package/dist/types.d.mts +9 -8
- package/package.json +1 -1
- package/src/server.ts +88 -14
package/dist/build.d.mts
CHANGED
package/dist/cli.js
CHANGED
|
@@ -1412,7 +1412,7 @@ var require_package = __commonJS({
|
|
|
1412
1412
|
"package.json"(exports2, module2) {
|
|
1413
1413
|
module2.exports = {
|
|
1414
1414
|
name: "elit",
|
|
1415
|
-
version: "3.1.
|
|
1415
|
+
version: "3.1.3",
|
|
1416
1416
|
description: "Optimized lightweight library for creating DOM elements with reactive state",
|
|
1417
1417
|
main: "dist/index.js",
|
|
1418
1418
|
module: "dist/index.mjs",
|
|
@@ -190,18 +190,19 @@ interface ServerRouteContext {
|
|
|
190
190
|
headers: Record<string, string | string[] | undefined>;
|
|
191
191
|
user?: any;
|
|
192
192
|
}
|
|
193
|
-
type ServerRouteHandler = (ctx: ServerRouteContext) => void | Promise<void>;
|
|
193
|
+
type ServerRouteHandler = (ctx: ServerRouteContext, next?: () => Promise<void>) => void | Promise<void>;
|
|
194
194
|
type Middleware = (ctx: ServerRouteContext, next: () => Promise<void>) => void | Promise<void>;
|
|
195
195
|
declare class ServerRouter {
|
|
196
196
|
private routes;
|
|
197
197
|
private middlewares;
|
|
198
|
-
use(
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
198
|
+
use(mw: Middleware | ((req: IncomingMessage, res: ServerResponse, next?: () => void) => any)): this;
|
|
199
|
+
private toMiddleware;
|
|
200
|
+
get: (path: string, ...handlers: Array<Middleware | ServerRouteHandler | ((req: IncomingMessage, res: ServerResponse, next?: () => void) => any)>) => this;
|
|
201
|
+
post: (path: string, ...handlers: Array<Middleware | ServerRouteHandler | ((req: IncomingMessage, res: ServerResponse, next?: () => void) => any)>) => this;
|
|
202
|
+
put: (path: string, ...handlers: Array<Middleware | ServerRouteHandler | ((req: IncomingMessage, res: ServerResponse, next?: () => void) => any)>) => this;
|
|
203
|
+
delete: (path: string, ...handlers: Array<Middleware | ServerRouteHandler | ((req: IncomingMessage, res: ServerResponse, next?: () => void) => any)>) => this;
|
|
204
|
+
patch: (path: string, ...handlers: Array<Middleware | ServerRouteHandler | ((req: IncomingMessage, res: ServerResponse, next?: () => void) => any)>) => this;
|
|
205
|
+
options: (path: string, ...handlers: Array<Middleware | ServerRouteHandler | ((req: IncomingMessage, res: ServerResponse, next?: () => void) => any)>) => this;
|
|
205
206
|
private addRoute;
|
|
206
207
|
private pathToRegex;
|
|
207
208
|
private parseQuery;
|
|
@@ -190,18 +190,19 @@ interface ServerRouteContext {
|
|
|
190
190
|
headers: Record<string, string | string[] | undefined>;
|
|
191
191
|
user?: any;
|
|
192
192
|
}
|
|
193
|
-
type ServerRouteHandler = (ctx: ServerRouteContext) => void | Promise<void>;
|
|
193
|
+
type ServerRouteHandler = (ctx: ServerRouteContext, next?: () => Promise<void>) => void | Promise<void>;
|
|
194
194
|
type Middleware = (ctx: ServerRouteContext, next: () => Promise<void>) => void | Promise<void>;
|
|
195
195
|
declare class ServerRouter {
|
|
196
196
|
private routes;
|
|
197
197
|
private middlewares;
|
|
198
|
-
use(
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
198
|
+
use(mw: Middleware | ((req: IncomingMessage, res: ServerResponse, next?: () => void) => any)): this;
|
|
199
|
+
private toMiddleware;
|
|
200
|
+
get: (path: string, ...handlers: Array<Middleware | ServerRouteHandler | ((req: IncomingMessage, res: ServerResponse, next?: () => void) => any)>) => this;
|
|
201
|
+
post: (path: string, ...handlers: Array<Middleware | ServerRouteHandler | ((req: IncomingMessage, res: ServerResponse, next?: () => void) => any)>) => this;
|
|
202
|
+
put: (path: string, ...handlers: Array<Middleware | ServerRouteHandler | ((req: IncomingMessage, res: ServerResponse, next?: () => void) => any)>) => this;
|
|
203
|
+
delete: (path: string, ...handlers: Array<Middleware | ServerRouteHandler | ((req: IncomingMessage, res: ServerResponse, next?: () => void) => any)>) => this;
|
|
204
|
+
patch: (path: string, ...handlers: Array<Middleware | ServerRouteHandler | ((req: IncomingMessage, res: ServerResponse, next?: () => void) => any)>) => this;
|
|
205
|
+
options: (path: string, ...handlers: Array<Middleware | ServerRouteHandler | ((req: IncomingMessage, res: ServerResponse, next?: () => void) => any)>) => this;
|
|
205
206
|
private addRoute;
|
|
206
207
|
private pathToRegex;
|
|
207
208
|
private parseQuery;
|
package/dist/server.d.mts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import './http.mjs';
|
|
2
2
|
import './ws.mjs';
|
|
3
|
-
export { H as HttpMethod, M as Middleware, S as ServerRouteContext, b as ServerRouteHandler, c as ServerRouter, q as SharedState, p as SharedStateOptions, o as StateChangeHandler, u as StateManager, g as bodyLimit, i as cacheControl, d as clearImportMapCache, k as compress, e as cors, v as createDevServer, n as createProxyHandler, f as errorHandler, h as html, j as json, l as logger, r as rateLimit, m as security, s as status, t as text } from './server-
|
|
3
|
+
export { H as HttpMethod, M as Middleware, S as ServerRouteContext, b as ServerRouteHandler, c as ServerRouter, q as SharedState, p as SharedStateOptions, o as StateChangeHandler, u as StateManager, g as bodyLimit, i as cacheControl, d as clearImportMapCache, k as compress, e as cors, v as createDevServer, n as createProxyHandler, f as errorHandler, h as html, j as json, l as logger, r as rateLimit, m as security, s as status, t as text } from './server-QfJoaxeO.mjs';
|
|
4
4
|
import 'node:events';
|
|
5
5
|
import 'events';
|
|
6
6
|
import 'http';
|
package/dist/server.d.ts
CHANGED
|
@@ -18,18 +18,19 @@ export interface ServerRouteContext {
|
|
|
18
18
|
headers: Record<string, string | string[] | undefined>;
|
|
19
19
|
user?: any;
|
|
20
20
|
}
|
|
21
|
-
export type ServerRouteHandler = (ctx: ServerRouteContext) => void | Promise<void>;
|
|
21
|
+
export type ServerRouteHandler = (ctx: ServerRouteContext, next?: () => Promise<void>) => void | Promise<void>;
|
|
22
22
|
export type Middleware = (ctx: ServerRouteContext, next: () => Promise<void>) => void | Promise<void>;
|
|
23
23
|
export declare class ServerRouter {
|
|
24
24
|
private routes;
|
|
25
25
|
private middlewares;
|
|
26
|
-
use(
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
26
|
+
use(mw: Middleware | ((req: IncomingMessage, res: ServerResponse, next?: () => void) => any)): this;
|
|
27
|
+
private toMiddleware;
|
|
28
|
+
get: (path: string, ...handlers: Array<Middleware | ServerRouteHandler | ((req: IncomingMessage, res: ServerResponse, next?: () => void) => any)>) => this;
|
|
29
|
+
post: (path: string, ...handlers: Array<Middleware | ServerRouteHandler | ((req: IncomingMessage, res: ServerResponse, next?: () => void) => any)>) => this;
|
|
30
|
+
put: (path: string, ...handlers: Array<Middleware | ServerRouteHandler | ((req: IncomingMessage, res: ServerResponse, next?: () => void) => any)>) => this;
|
|
31
|
+
delete: (path: string, ...handlers: Array<Middleware | ServerRouteHandler | ((req: IncomingMessage, res: ServerResponse, next?: () => void) => any)>) => this;
|
|
32
|
+
patch: (path: string, ...handlers: Array<Middleware | ServerRouteHandler | ((req: IncomingMessage, res: ServerResponse, next?: () => void) => any)>) => this;
|
|
33
|
+
options: (path: string, ...handlers: Array<Middleware | ServerRouteHandler | ((req: IncomingMessage, res: ServerResponse, next?: () => void) => any)>) => this;
|
|
33
34
|
private addRoute;
|
|
34
35
|
private pathToRegex;
|
|
35
36
|
private parseQuery;
|
package/dist/server.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAgB,eAAe,EAAE,cAAc,EAA0B,MAAM,QAAQ,CAAC;AAE/F,OAAO,EAAmB,SAAS,EAAc,MAAM,MAAM,CAAC;AAM9D,OAAO,KAAK,EAAE,gBAAgB,EAAE,SAAS,EAA4B,WAAW,EAAE,MAAM,SAAS,CAAC;AAKlG,MAAM,MAAM,UAAU,GAAG,KAAK,GAAG,MAAM,GAAG,KAAK,GAAG,QAAQ,GAAG,OAAO,GAAG,SAAS,GAAG,MAAM,CAAC;AAE1F,MAAM,WAAW,kBAAkB;IACjC,GAAG,EAAE,eAAe,CAAC;IACrB,GAAG,EAAE,cAAc,CAAC;IACpB,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAC/B,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAC9B,IAAI,EAAE,GAAG,CAAC;IACV,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,SAAS,CAAC,CAAC;IACvD,IAAI,CAAC,EAAE,GAAG,CAAC;CACZ;AAED,MAAM,MAAM,kBAAkB,GAAG,CAAC,GAAG,EAAE,kBAAkB,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;
|
|
1
|
+
{"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAgB,eAAe,EAAE,cAAc,EAA0B,MAAM,QAAQ,CAAC;AAE/F,OAAO,EAAmB,SAAS,EAAc,MAAM,MAAM,CAAC;AAM9D,OAAO,KAAK,EAAE,gBAAgB,EAAE,SAAS,EAA4B,WAAW,EAAE,MAAM,SAAS,CAAC;AAKlG,MAAM,MAAM,UAAU,GAAG,KAA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,MAAM;IAMd,IAAI,eAAe,IAAI,MAAM,CAE5B;IAED,KAAK,IAAI,IAAI;CAId;AAED,qBAAa,YAAY;IACvB,OAAO,CAAC,MAAM,CAAuC;IAErD,MAAM,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,OAAO,EAAE,kBAAkB,CAAC,CAAC,CAAC,GAAG,WAAW,CAAC,CAAC,CAAC;IAOtE,GAAG,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,GAAG,WAAW,CAAC,CAAC,CAAC,GAAG,SAAS;IAI/C,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO;IAIzB,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO;IAS5B,SAAS,CAAC,GAAG,EAAE,MAAM,EAAE,EAAE,EAAE,SAAS,GAAG,IAAI;IAI3C,WAAW,CAAC,GAAG,EAAE,MAAM,EAAE,EAAE,EAAE,SAAS,GAAG,IAAI;IAI7C,cAAc,CAAC,EAAE,EAAE,SAAS,GAAG,IAAI;IAInC,iBAAiB,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,GAAG,GAAG,IAAI;IAKhD,IAAI,IAAI,MAAM,EAAE;IAIhB,KAAK,IAAI,IAAI;CAId;AA0BD,wBAAgB,eAAe,CAAC,OAAO,EAAE,gBAAgB,GAAG,SAAS,CAyuBpE"}
|
package/dist/server.js
CHANGED
|
@@ -2670,15 +2670,63 @@ var ServerRouter = class {
|
|
|
2670
2670
|
this.patch = (path, ...handlers) => this.addRoute("PATCH", path, handlers);
|
|
2671
2671
|
this.options = (path, ...handlers) => this.addRoute("OPTIONS", path, handlers);
|
|
2672
2672
|
}
|
|
2673
|
-
|
|
2674
|
-
|
|
2673
|
+
// Accept both internal Middleware and Express-style `(req, res, next?)` functions
|
|
2674
|
+
use(mw) {
|
|
2675
|
+
this.middlewares.push(this.toMiddleware(mw));
|
|
2675
2676
|
return this;
|
|
2676
2677
|
}
|
|
2678
|
+
// Convert Express-like handler/middleware to internal Middleware
|
|
2679
|
+
toMiddleware(fn) {
|
|
2680
|
+
if (fn.length === 2 && fn.name !== "bound ") {
|
|
2681
|
+
}
|
|
2682
|
+
return async (ctx, next) => {
|
|
2683
|
+
const f = fn;
|
|
2684
|
+
if (f.length >= 3) {
|
|
2685
|
+
const expressNext = () => {
|
|
2686
|
+
void next();
|
|
2687
|
+
};
|
|
2688
|
+
const res = f(ctx.req, ctx.res, expressNext);
|
|
2689
|
+
if (res && typeof res.then === "function") await res;
|
|
2690
|
+
return;
|
|
2691
|
+
}
|
|
2692
|
+
if (f.length === 2) {
|
|
2693
|
+
const res = f(ctx.req, ctx.res);
|
|
2694
|
+
if (res && typeof res.then === "function") await res;
|
|
2695
|
+
await next();
|
|
2696
|
+
return;
|
|
2697
|
+
}
|
|
2698
|
+
const out = fn(ctx);
|
|
2699
|
+
if (out && typeof out.then === "function") await out;
|
|
2700
|
+
await next();
|
|
2701
|
+
};
|
|
2702
|
+
}
|
|
2677
2703
|
addRoute(method, path, handlers) {
|
|
2678
2704
|
const { pattern, paramNames } = this.pathToRegex(path);
|
|
2679
2705
|
if (!handlers || handlers.length === 0) throw new Error("Route must include a handler");
|
|
2680
|
-
const
|
|
2681
|
-
const
|
|
2706
|
+
const rawMiddlewares = handlers.slice(0, handlers.length - 1);
|
|
2707
|
+
const rawLast = handlers[handlers.length - 1];
|
|
2708
|
+
const middlewares = rawMiddlewares.map((h) => this.toMiddleware(h));
|
|
2709
|
+
const last = (() => {
|
|
2710
|
+
const f = rawLast;
|
|
2711
|
+
if (typeof f !== "function") throw new Error("Route handler must be a function");
|
|
2712
|
+
if (f.length >= 2) {
|
|
2713
|
+
return async (ctx) => {
|
|
2714
|
+
if (f.length >= 3) {
|
|
2715
|
+
await new Promise((resolve2) => {
|
|
2716
|
+
try {
|
|
2717
|
+
f(ctx.req, ctx.res, () => resolve2());
|
|
2718
|
+
} catch (e) {
|
|
2719
|
+
resolve2();
|
|
2720
|
+
}
|
|
2721
|
+
});
|
|
2722
|
+
} else {
|
|
2723
|
+
const res = f(ctx.req, ctx.res);
|
|
2724
|
+
if (res && typeof res.then === "function") await res;
|
|
2725
|
+
}
|
|
2726
|
+
};
|
|
2727
|
+
}
|
|
2728
|
+
return f;
|
|
2729
|
+
})();
|
|
2682
2730
|
this.routes.push({ method, pattern, paramNames, handler: last, middlewares });
|
|
2683
2731
|
return this;
|
|
2684
2732
|
}
|
|
@@ -2759,9 +2807,13 @@ var ServerRouter = class {
|
|
|
2759
2807
|
}
|
|
2760
2808
|
const ctx = { req, res, params, query: this.parseQuery(url), body, headers: req.headers };
|
|
2761
2809
|
const routeMiddlewares = route.middlewares || [];
|
|
2762
|
-
const chain = [
|
|
2763
|
-
|
|
2764
|
-
|
|
2810
|
+
const chain = [
|
|
2811
|
+
...this.middlewares,
|
|
2812
|
+
...routeMiddlewares,
|
|
2813
|
+
async (c, n) => {
|
|
2814
|
+
await route.handler(c, n);
|
|
2815
|
+
}
|
|
2816
|
+
];
|
|
2765
2817
|
let i = 0;
|
|
2766
2818
|
const next = async () => {
|
|
2767
2819
|
if (i >= chain.length) return;
|
package/dist/server.mjs
CHANGED
|
@@ -2644,15 +2644,63 @@ var ServerRouter = class {
|
|
|
2644
2644
|
this.patch = (path, ...handlers) => this.addRoute("PATCH", path, handlers);
|
|
2645
2645
|
this.options = (path, ...handlers) => this.addRoute("OPTIONS", path, handlers);
|
|
2646
2646
|
}
|
|
2647
|
-
|
|
2648
|
-
|
|
2647
|
+
// Accept both internal Middleware and Express-style `(req, res, next?)` functions
|
|
2648
|
+
use(mw) {
|
|
2649
|
+
this.middlewares.push(this.toMiddleware(mw));
|
|
2649
2650
|
return this;
|
|
2650
2651
|
}
|
|
2652
|
+
// Convert Express-like handler/middleware to internal Middleware
|
|
2653
|
+
toMiddleware(fn) {
|
|
2654
|
+
if (fn.length === 2 && fn.name !== "bound ") {
|
|
2655
|
+
}
|
|
2656
|
+
return async (ctx, next) => {
|
|
2657
|
+
const f = fn;
|
|
2658
|
+
if (f.length >= 3) {
|
|
2659
|
+
const expressNext = () => {
|
|
2660
|
+
void next();
|
|
2661
|
+
};
|
|
2662
|
+
const res = f(ctx.req, ctx.res, expressNext);
|
|
2663
|
+
if (res && typeof res.then === "function") await res;
|
|
2664
|
+
return;
|
|
2665
|
+
}
|
|
2666
|
+
if (f.length === 2) {
|
|
2667
|
+
const res = f(ctx.req, ctx.res);
|
|
2668
|
+
if (res && typeof res.then === "function") await res;
|
|
2669
|
+
await next();
|
|
2670
|
+
return;
|
|
2671
|
+
}
|
|
2672
|
+
const out = fn(ctx);
|
|
2673
|
+
if (out && typeof out.then === "function") await out;
|
|
2674
|
+
await next();
|
|
2675
|
+
};
|
|
2676
|
+
}
|
|
2651
2677
|
addRoute(method, path, handlers) {
|
|
2652
2678
|
const { pattern, paramNames } = this.pathToRegex(path);
|
|
2653
2679
|
if (!handlers || handlers.length === 0) throw new Error("Route must include a handler");
|
|
2654
|
-
const
|
|
2655
|
-
const
|
|
2680
|
+
const rawMiddlewares = handlers.slice(0, handlers.length - 1);
|
|
2681
|
+
const rawLast = handlers[handlers.length - 1];
|
|
2682
|
+
const middlewares = rawMiddlewares.map((h) => this.toMiddleware(h));
|
|
2683
|
+
const last = (() => {
|
|
2684
|
+
const f = rawLast;
|
|
2685
|
+
if (typeof f !== "function") throw new Error("Route handler must be a function");
|
|
2686
|
+
if (f.length >= 2) {
|
|
2687
|
+
return async (ctx) => {
|
|
2688
|
+
if (f.length >= 3) {
|
|
2689
|
+
await new Promise((resolve2) => {
|
|
2690
|
+
try {
|
|
2691
|
+
f(ctx.req, ctx.res, () => resolve2());
|
|
2692
|
+
} catch (e) {
|
|
2693
|
+
resolve2();
|
|
2694
|
+
}
|
|
2695
|
+
});
|
|
2696
|
+
} else {
|
|
2697
|
+
const res = f(ctx.req, ctx.res);
|
|
2698
|
+
if (res && typeof res.then === "function") await res;
|
|
2699
|
+
}
|
|
2700
|
+
};
|
|
2701
|
+
}
|
|
2702
|
+
return f;
|
|
2703
|
+
})();
|
|
2656
2704
|
this.routes.push({ method, pattern, paramNames, handler: last, middlewares });
|
|
2657
2705
|
return this;
|
|
2658
2706
|
}
|
|
@@ -2733,9 +2781,13 @@ var ServerRouter = class {
|
|
|
2733
2781
|
}
|
|
2734
2782
|
const ctx = { req, res, params, query: this.parseQuery(url), body, headers: req.headers };
|
|
2735
2783
|
const routeMiddlewares = route.middlewares || [];
|
|
2736
|
-
const chain = [
|
|
2737
|
-
|
|
2738
|
-
|
|
2784
|
+
const chain = [
|
|
2785
|
+
...this.middlewares,
|
|
2786
|
+
...routeMiddlewares,
|
|
2787
|
+
async (c, n) => {
|
|
2788
|
+
await route.handler(c, n);
|
|
2789
|
+
}
|
|
2790
|
+
];
|
|
2739
2791
|
let i = 0;
|
|
2740
2792
|
const next = async () => {
|
|
2741
2793
|
if (i >= chain.length) return;
|
package/dist/types.d.mts
CHANGED
|
@@ -157,18 +157,19 @@ interface ServerRouteContext {
|
|
|
157
157
|
headers: Record<string, string | string[] | undefined>;
|
|
158
158
|
user?: any;
|
|
159
159
|
}
|
|
160
|
-
type ServerRouteHandler = (ctx: ServerRouteContext) => void | Promise<void>;
|
|
160
|
+
type ServerRouteHandler = (ctx: ServerRouteContext, next?: () => Promise<void>) => void | Promise<void>;
|
|
161
161
|
type Middleware = (ctx: ServerRouteContext, next: () => Promise<void>) => void | Promise<void>;
|
|
162
162
|
declare class ServerRouter {
|
|
163
163
|
private routes;
|
|
164
164
|
private middlewares;
|
|
165
|
-
use(
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
165
|
+
use(mw: Middleware | ((req: IncomingMessage, res: ServerResponse, next?: () => void) => any)): this;
|
|
166
|
+
private toMiddleware;
|
|
167
|
+
get: (path: string, ...handlers: Array<Middleware | ServerRouteHandler | ((req: IncomingMessage, res: ServerResponse, next?: () => void) => any)>) => this;
|
|
168
|
+
post: (path: string, ...handlers: Array<Middleware | ServerRouteHandler | ((req: IncomingMessage, res: ServerResponse, next?: () => void) => any)>) => this;
|
|
169
|
+
put: (path: string, ...handlers: Array<Middleware | ServerRouteHandler | ((req: IncomingMessage, res: ServerResponse, next?: () => void) => any)>) => this;
|
|
170
|
+
delete: (path: string, ...handlers: Array<Middleware | ServerRouteHandler | ((req: IncomingMessage, res: ServerResponse, next?: () => void) => any)>) => this;
|
|
171
|
+
patch: (path: string, ...handlers: Array<Middleware | ServerRouteHandler | ((req: IncomingMessage, res: ServerResponse, next?: () => void) => any)>) => this;
|
|
172
|
+
options: (path: string, ...handlers: Array<Middleware | ServerRouteHandler | ((req: IncomingMessage, res: ServerResponse, next?: () => void) => any)>) => this;
|
|
172
173
|
private addRoute;
|
|
173
174
|
private pathToRegex;
|
|
174
175
|
private parseQuery;
|
package/package.json
CHANGED
package/src/server.ts
CHANGED
|
@@ -31,7 +31,7 @@ export interface ServerRouteContext {
|
|
|
31
31
|
user?: any;
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
-
export type ServerRouteHandler = (ctx: ServerRouteContext) => void | Promise<void>;
|
|
34
|
+
export type ServerRouteHandler = (ctx: ServerRouteContext, next?: () => Promise<void>) => void | Promise<void>;
|
|
35
35
|
export type Middleware = (ctx: ServerRouteContext, next: () => Promise<void>) => void | Promise<void>;
|
|
36
36
|
|
|
37
37
|
interface ServerRoute {
|
|
@@ -46,25 +46,94 @@ export class ServerRouter {
|
|
|
46
46
|
private routes: ServerRoute[] = [];
|
|
47
47
|
private middlewares: Middleware[] = [];
|
|
48
48
|
|
|
49
|
-
|
|
50
|
-
|
|
49
|
+
// Accept both internal Middleware and Express-style `(req, res, next?)` functions
|
|
50
|
+
use(mw: Middleware | ((req: IncomingMessage, res: ServerResponse, next?: () => void) => any)): this {
|
|
51
|
+
this.middlewares.push(this.toMiddleware(mw as any));
|
|
51
52
|
return this;
|
|
52
53
|
}
|
|
53
54
|
|
|
55
|
+
// Convert Express-like handler/middleware to internal Middleware
|
|
56
|
+
private toMiddleware(fn: Middleware | ServerRouteHandler | ((req: IncomingMessage, res: ServerResponse, next?: () => void) => any)): Middleware {
|
|
57
|
+
// If it's already our Middleware, return as-is
|
|
58
|
+
if ((fn as Middleware).length === 2 && (fn as any).name !== 'bound ') {
|
|
59
|
+
// Cannot reliably detect, so always wrap to normalize behavior
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return async (ctx: ServerRouteContext, next: () => Promise<void>) => {
|
|
63
|
+
const f: any = fn;
|
|
64
|
+
|
|
65
|
+
// Express-style with (req, res, next)
|
|
66
|
+
if (f.length >= 3) {
|
|
67
|
+
// Provide a next that triggers our next
|
|
68
|
+
const expressNext = () => {
|
|
69
|
+
// call our next but don't await here
|
|
70
|
+
void next();
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
const res = f(ctx.req, ctx.res, expressNext);
|
|
74
|
+
if (res && typeof res.then === 'function') await res;
|
|
75
|
+
// If express middleware didn't call next(), we simply return and stop the chain
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Express-style with (req, res) - treat as middleware that continues after completion
|
|
80
|
+
if (f.length === 2) {
|
|
81
|
+
const res = f(ctx.req, ctx.res);
|
|
82
|
+
if (res && typeof res.then === 'function') await res;
|
|
83
|
+
await next();
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// Our internal handler style (ctx) => ... - call it and continue
|
|
88
|
+
const out = (fn as ServerRouteHandler)(ctx);
|
|
89
|
+
if (out && typeof out.then === 'function') await out;
|
|
90
|
+
await next();
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
|
|
54
94
|
// Support per-route middleware: accept middleware(s) before the final handler
|
|
55
|
-
get = (path: string, ...handlers: Array<Middleware | ServerRouteHandler>): this => this.addRoute('GET', path, handlers as any);
|
|
56
|
-
post = (path: string, ...handlers: Array<Middleware | ServerRouteHandler>): this => this.addRoute('POST', path, handlers as any);
|
|
57
|
-
put = (path: string, ...handlers: Array<Middleware | ServerRouteHandler>): this => this.addRoute('PUT', path, handlers as any);
|
|
58
|
-
delete = (path: string, ...handlers: Array<Middleware | ServerRouteHandler>): this => this.addRoute('DELETE', path, handlers as any);
|
|
59
|
-
patch = (path: string, ...handlers: Array<Middleware | ServerRouteHandler>): this => this.addRoute('PATCH', path, handlers as any);
|
|
60
|
-
options = (path: string, ...handlers: Array<Middleware | ServerRouteHandler>): this => this.addRoute('OPTIONS', path, handlers as any);
|
|
61
|
-
|
|
62
|
-
private addRoute(method: HttpMethod, path: string, handlers: Array<Middleware | ServerRouteHandler>): this {
|
|
95
|
+
get = (path: string, ...handlers: Array<Middleware | ServerRouteHandler | ((req: IncomingMessage, res: ServerResponse, next?: () => void) => any)>): this => this.addRoute('GET', path, handlers as any);
|
|
96
|
+
post = (path: string, ...handlers: Array<Middleware | ServerRouteHandler | ((req: IncomingMessage, res: ServerResponse, next?: () => void) => any)>): this => this.addRoute('POST', path, handlers as any);
|
|
97
|
+
put = (path: string, ...handlers: Array<Middleware | ServerRouteHandler | ((req: IncomingMessage, res: ServerResponse, next?: () => void) => any)>): this => this.addRoute('PUT', path, handlers as any);
|
|
98
|
+
delete = (path: string, ...handlers: Array<Middleware | ServerRouteHandler | ((req: IncomingMessage, res: ServerResponse, next?: () => void) => any)>): this => this.addRoute('DELETE', path, handlers as any);
|
|
99
|
+
patch = (path: string, ...handlers: Array<Middleware | ServerRouteHandler | ((req: IncomingMessage, res: ServerResponse, next?: () => void) => any)>): this => this.addRoute('PATCH', path, handlers as any);
|
|
100
|
+
options = (path: string, ...handlers: Array<Middleware | ServerRouteHandler | ((req: IncomingMessage, res: ServerResponse, next?: () => void) => any)>): this => this.addRoute('OPTIONS', path, handlers as any);
|
|
101
|
+
|
|
102
|
+
private addRoute(method: HttpMethod, path: string, handlers: Array<Middleware | ServerRouteHandler | ((req: IncomingMessage, res: ServerResponse, next?: () => void) => any)>): this {
|
|
63
103
|
const { pattern, paramNames } = this.pathToRegex(path);
|
|
64
104
|
// Last item is the actual route handler, preceding items are middlewares
|
|
65
105
|
if (!handlers || handlers.length === 0) throw new Error('Route must include a handler');
|
|
66
|
-
const
|
|
67
|
-
const
|
|
106
|
+
const rawMiddlewares = handlers.slice(0, handlers.length - 1);
|
|
107
|
+
const rawLast = handlers[handlers.length - 1];
|
|
108
|
+
|
|
109
|
+
const middlewares = rawMiddlewares.map(h => this.toMiddleware(h as any));
|
|
110
|
+
|
|
111
|
+
// Normalize last handler: if it's express-like, wrap into ServerRouteHandler
|
|
112
|
+
const last = ((): ServerRouteHandler => {
|
|
113
|
+
const f: any = rawLast;
|
|
114
|
+
if (typeof f !== 'function') throw new Error('Route handler must be a function');
|
|
115
|
+
|
|
116
|
+
if (f.length >= 2) {
|
|
117
|
+
// Express-style final handler
|
|
118
|
+
return async (ctx: ServerRouteContext) => {
|
|
119
|
+
if (f.length >= 3) {
|
|
120
|
+
// expects next
|
|
121
|
+
await new Promise<void>((resolve) => {
|
|
122
|
+
try {
|
|
123
|
+
f(ctx.req, ctx.res, () => resolve());
|
|
124
|
+
} catch (e) { resolve(); }
|
|
125
|
+
});
|
|
126
|
+
} else {
|
|
127
|
+
const res = f(ctx.req, ctx.res);
|
|
128
|
+
if (res && typeof res.then === 'function') await res;
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// Already a ServerRouteHandler (ctx)
|
|
134
|
+
return f as ServerRouteHandler;
|
|
135
|
+
})();
|
|
136
|
+
|
|
68
137
|
this.routes.push({ method, pattern, paramNames, handler: last, middlewares });
|
|
69
138
|
return this;
|
|
70
139
|
}
|
|
@@ -166,8 +235,13 @@ export class ServerRouter {
|
|
|
166
235
|
const ctx: ServerRouteContext = { req, res, params, query: this.parseQuery(url), body, headers: req.headers as any };
|
|
167
236
|
|
|
168
237
|
// Build middleware chain: global middlewares -> route middlewares -> final handler
|
|
238
|
+
// Pass `next` to the final handler so it can optionally call await next()
|
|
169
239
|
const routeMiddlewares = route.middlewares || [];
|
|
170
|
-
const chain: Middleware[] = [
|
|
240
|
+
const chain: Middleware[] = [
|
|
241
|
+
...this.middlewares,
|
|
242
|
+
...routeMiddlewares,
|
|
243
|
+
async (c, n) => { await route.handler(c, n); }
|
|
244
|
+
];
|
|
171
245
|
|
|
172
246
|
let i = 0;
|
|
173
247
|
const next = async () => {
|