@mednours/backon 0.7.1 → 0.9.0
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/auth-core.d.ts +36 -5
- package/dist/auth-core.d.ts.map +1 -1
- package/dist/cache-control-core.d.ts +17 -0
- package/dist/cache-control-core.d.ts.map +1 -0
- package/dist/cache-control.d.ts +9 -0
- package/dist/cache-control.d.ts.map +1 -0
- package/dist/create-app.d.ts +8 -0
- package/dist/create-app.d.ts.map +1 -1
- package/dist/define-route.d.ts +1 -1
- package/dist/define-route.d.ts.map +1 -1
- package/dist/error.d.ts.map +1 -1
- package/dist/express/cache-control.d.ts +9 -0
- package/dist/express/cache-control.d.ts.map +1 -0
- package/dist/express/create-app.d.ts +5 -0
- package/dist/express/create-app.d.ts.map +1 -1
- package/dist/express/define-route.d.ts +1 -1
- package/dist/express/define-route.d.ts.map +1 -1
- package/dist/express/index.d.ts +4 -1
- package/dist/express/index.d.ts.map +1 -1
- package/dist/express/index.js +181 -32
- package/dist/express/openapi.d.ts.map +1 -1
- package/dist/express/rate-limit.d.ts +6 -0
- package/dist/express/rate-limit.d.ts.map +1 -1
- package/dist/express/require-auth.d.ts +2 -11
- package/dist/express/require-auth.d.ts.map +1 -1
- package/dist/express/router-registry.d.ts.map +1 -1
- package/dist/express/stream.d.ts.map +1 -1
- package/dist/express/upload.d.ts.map +1 -1
- package/dist/index.d.ts +5 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +216 -43
- package/dist/openapi-core.d.ts +1 -1
- package/dist/openapi-core.d.ts.map +1 -1
- package/dist/openapi.d.ts.map +1 -1
- package/dist/rate-limit-core.d.ts +5 -1
- package/dist/rate-limit-core.d.ts.map +1 -1
- package/dist/rate-limit.d.ts +6 -0
- package/dist/rate-limit.d.ts.map +1 -1
- package/dist/require-auth.d.ts +3 -14
- package/dist/require-auth.d.ts.map +1 -1
- package/dist/serve.d.ts +12 -1
- package/dist/serve.d.ts.map +1 -1
- package/dist/stream.d.ts.map +1 -1
- package/dist/upload.d.ts.map +1 -1
- package/dist/websocket.d.ts.map +1 -1
- package/package.json +13 -14
- package/src/auth-core.ts +108 -9
- package/src/cache-control-core.ts +32 -0
- package/src/cache-control.ts +18 -0
- package/src/create-app.ts +25 -9
- package/src/define-route.ts +17 -5
- package/src/express/cache-control.ts +22 -0
- package/src/express/create-app.ts +24 -6
- package/src/express/define-route.ts +4 -4
- package/src/express/index.ts +5 -1
- package/src/express/openapi.ts +7 -5
- package/src/express/rate-limit.ts +15 -4
- package/src/express/require-auth.ts +11 -14
- package/src/express/router-registry.ts +6 -2
- package/src/index.ts +6 -1
- package/src/openapi-core.ts +24 -7
- package/src/openapi.ts +8 -3
- package/src/rate-limit-core.ts +29 -1
- package/src/rate-limit.ts +15 -6
- package/src/require-auth.ts +10 -17
- package/src/serve.ts +48 -16
|
@@ -1,17 +1,8 @@
|
|
|
1
1
|
import type { Request, Response, NextFunction } from "express";
|
|
2
2
|
import { type VerifyJwtOptions } from "../auth-core";
|
|
3
3
|
/**
|
|
4
|
-
* Express equivalent of the Hono requireAuth: verifies a bearer
|
|
5
|
-
* stashes the decoded payload on res.locals.auth
|
|
6
|
-
* `c.get("auth")` in a defineRoute handler (BackonContext.get() reads
|
|
7
|
-
* res.locals) or directly as `res.locals.auth` in plain Express middleware.
|
|
8
|
-
* Calls next(AppError(401, "unauthorized")) on a missing, malformed, or
|
|
9
|
-
* invalid token; requires attachErrorHandler(app) to be registered
|
|
10
|
-
* downstream (serve() does this automatically — see create-app.ts).
|
|
11
|
-
*
|
|
12
|
-
* Verification only, same as the Hono version — issuing tokens is left to
|
|
13
|
-
* the consuming project. Apply per-route or per-router, not necessarily
|
|
14
|
-
* globally.
|
|
4
|
+
* Express equivalent of the Hono requireAuth: verifies a bearer (or optional
|
|
5
|
+
* cookie) JWT and stashes the decoded payload on `res.locals.auth`.
|
|
15
6
|
*/
|
|
16
7
|
export declare function requireAuth(options: VerifyJwtOptions): (req: Request, res: Response, next: NextFunction) => Promise<void>;
|
|
17
8
|
//# sourceMappingURL=require-auth.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"require-auth.d.ts","sourceRoot":"","sources":["../../src/express/require-auth.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAE/D,OAAO,
|
|
1
|
+
{"version":3,"file":"require-auth.d.ts","sourceRoot":"","sources":["../../src/express/require-auth.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAE/D,OAAO,EAA+B,KAAK,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAElF;;;GAGG;AACH,wBAAgB,WAAW,CAAC,OAAO,EAAE,gBAAgB,SACF,OAAO,OAAO,QAAQ,QAAQ,YAAY,mBAsB5F"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"router-registry.d.ts","sourceRoot":"","sources":["../../src/express/router-registry.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,MAAM,MAAM,UAAU,GAClB;IAAE,IAAI,EAAE,OAAO,CAAC;IAAC,MAAM,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC;IAAC,MAAM,CAAC,EAAE,OAAO,CAAA;CAAE,GACjE;IAAE,IAAI,EAAE,OAAO,CAAC;IAAC,MAAM,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,aAAa,CAAA;CAAE,CAAC;AAE7D,MAAM,WAAW,aAAa;IAC5B,eAAe,EAAE,UAAU,EAAE,CAAC;CAC/B;AAYD,oHAAoH;AACpH,wBAAgB,WAAW,CAAC,CAAC,SAAS,MAAM,EAAE,MAAM,EAAE,CAAC,GAAG,CAAC,GAAG,aAAa,
|
|
1
|
+
{"version":3,"file":"router-registry.d.ts","sourceRoot":"","sources":["../../src/express/router-registry.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,MAAM,MAAM,UAAU,GAClB;IAAE,IAAI,EAAE,OAAO,CAAC;IAAC,MAAM,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC;IAAC,MAAM,CAAC,EAAE,OAAO,CAAA;CAAE,GACjE;IAAE,IAAI,EAAE,OAAO,CAAC;IAAC,MAAM,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,aAAa,CAAA;CAAE,CAAC;AAE7D,MAAM,WAAW,aAAa;IAC5B,eAAe,EAAE,UAAU,EAAE,CAAC;CAC/B;AAYD,oHAAoH;AACpH,wBAAgB,WAAW,CAAC,CAAC,SAAS,MAAM,EAAE,MAAM,EAAE,CAAC,GAAG,CAAC,GAAG,aAAa,CAuC1E;AAOD,sHAAsH;AACtH,wBAAgB,aAAa,CAC3B,IAAI,EAAE,aAAa,EACnB,MAAM,SAAK,GACV,KAAK,CAAC;IAAE,MAAM,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC;IAAC,MAAM,CAAC,EAAE,OAAO,CAAA;CAAE,CAAC,CAU3D"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"stream.d.ts","sourceRoot":"","sources":["../../src/express/stream.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAC/D,OAAO,EAAiB,KAAK,aAAa,EAAE,MAAM,SAAS,CAAC;AAE5D,MAAM,MAAM,YAAY,GAAG,CAAC,KAAK,EAAE,MAAM,GAAG,UAAU,KAAK,IAAI,CAAC;AAEhE;;;;;;;;;;;;GAYG;AACH,wBAAgB,YAAY,CAAC,OAAO,EAAE,CAAC,CAAC,EAAE,aAAa,EAAE,KAAK,EAAE,YAAY,KAAK,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,
|
|
1
|
+
{"version":3,"file":"stream.d.ts","sourceRoot":"","sources":["../../src/express/stream.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAC/D,OAAO,EAAiB,KAAK,aAAa,EAAE,MAAM,SAAS,CAAC;AAE5D,MAAM,MAAM,YAAY,GAAG,CAAC,KAAK,EAAE,MAAM,GAAG,UAAU,KAAK,IAAI,CAAC;AAEhE;;;;;;;;;;;;GAYG;AACH,wBAAgB,YAAY,CAAC,OAAO,EAAE,CAAC,CAAC,EAAE,aAAa,EAAE,KAAK,EAAE,YAAY,KAAK,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,SAChF,OAAO,OAAO,QAAQ,QAAQ,YAAY,KAAG,OAAO,CAAC,IAAI,CAAC,CAW9E"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"upload.d.ts","sourceRoot":"","sources":["../../src/express/upload.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAG/D,OAAO,EAAiB,KAAK,aAAa,EAAE,MAAM,SAAS,CAAC;AAO5D,MAAM,WAAW,aAAa;IAC5B,wEAAwE;IACxE,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,6FAA6F;IAC7F,gBAAgB,CAAC,EAAE,MAAM,EAAE,CAAC;CAC7B;AAED,MAAM,MAAM,aAAa,GAAG,CAC1B,CAAC,EAAE,aAAa,EAChB,IAAI,EAAE;IAAE,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC,CAAA;CAAE,KACnD,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;AAEhC;;;;;;;;;;;;GAYG;AACH,wBAAgB,YAAY,CAAC,OAAO,EAAE,aAAa,EAAE,OAAO,EAAE,aAAa,
|
|
1
|
+
{"version":3,"file":"upload.d.ts","sourceRoot":"","sources":["../../src/express/upload.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAG/D,OAAO,EAAiB,KAAK,aAAa,EAAE,MAAM,SAAS,CAAC;AAO5D,MAAM,WAAW,aAAa;IAC5B,wEAAwE;IACxE,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,6FAA6F;IAC7F,gBAAgB,CAAC,EAAE,MAAM,EAAE,CAAC;CAC7B;AAED,MAAM,MAAM,aAAa,GAAG,CAC1B,CAAC,EAAE,aAAa,EAChB,IAAI,EAAE;IAAE,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC,CAAA;CAAE,KACnD,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;AAEhC;;;;;;;;;;;;GAYG;AACH,wBAAgB,YAAY,CAAC,OAAO,EAAE,aAAa,EAAE,OAAO,EAAE,aAAa,SAO5D,OAAO,OAAO,QAAQ,QAAQ,YAAY,KAAG,IAAI,CAwB/D"}
|
package/dist/index.d.ts
CHANGED
|
@@ -8,7 +8,10 @@ export type { ErrorResponseBody } from "./error";
|
|
|
8
8
|
export { logger } from "./logger";
|
|
9
9
|
export { requireEnv } from "./require-env";
|
|
10
10
|
export { requireAuth } from "./require-auth";
|
|
11
|
-
export
|
|
11
|
+
export { signJwt } from "./auth-core";
|
|
12
|
+
export type { VerifyJwtOptions, SignJwtOptions } from "./auth-core";
|
|
13
|
+
export { cacheControl } from "./cache-control";
|
|
14
|
+
export type { CacheControlOptions } from "./cache-control";
|
|
12
15
|
export { rateLimit } from "./rate-limit";
|
|
13
16
|
export type { RateLimitOptions } from "./rate-limit";
|
|
14
17
|
export { defineWebSocket } from "./websocket";
|
|
@@ -29,4 +32,5 @@ export type { BackonContext, Env } from "./types";
|
|
|
29
32
|
*/
|
|
30
33
|
export { Hono } from "hono";
|
|
31
34
|
export type { Next } from "hono";
|
|
35
|
+
export { setCookie, getCookie, deleteCookie, setSignedCookie, getSignedCookie } from "hono/cookie";
|
|
32
36
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AACzC,YAAY,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAErD,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAEhC,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAC7C,YAAY,EAAE,WAAW,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAE3E,OAAO,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AACnC,YAAY,EAAE,iBAAiB,EAAE,MAAM,SAAS,CAAC;AAEjD,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAElC,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAE3C,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAC7C,YAAY,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AACzC,YAAY,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAErD,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAEhC,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAC7C,YAAY,EAAE,WAAW,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAE3E,OAAO,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AACnC,YAAY,EAAE,iBAAiB,EAAE,MAAM,SAAS,CAAC;AAEjD,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAElC,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAE3C,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAC7C,OAAO,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AACtC,YAAY,EAAE,gBAAgB,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAEpE,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAC/C,YAAY,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAC;AAE3D,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AACzC,YAAY,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAErD,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAC9C,YAAY,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAErD,OAAO,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AACxC,YAAY,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AAE7C,OAAO,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AACxC,YAAY,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAE7D,OAAO,EAAE,mBAAmB,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AAC9D,YAAY,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAChD,OAAO,EAAE,gBAAgB,EAAE,sBAAsB,EAAE,MAAM,gBAAgB,CAAC;AAE1E,YAAY,EAAE,aAAa,EAAE,GAAG,EAAE,MAAM,SAAS,CAAC;AAElD;;;;;GAKG;AACH,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAC5B,YAAY,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AACjC,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,YAAY,EAAE,eAAe,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -49,7 +49,7 @@ var DEFAULT_BODY_LIMIT_BYTES = 1024 * 1024;
|
|
|
49
49
|
function resolveAllowedOrigins(options) {
|
|
50
50
|
if (options.allowedOrigins) return options.allowedOrigins;
|
|
51
51
|
const fromEnv = process.env.ALLOWED_ORIGINS;
|
|
52
|
-
return fromEnv ? fromEnv.split(",").map((origin) => origin.trim()) : [];
|
|
52
|
+
return fromEnv ? fromEnv.split(",").map((origin) => origin.trim()).filter(Boolean) : [];
|
|
53
53
|
}
|
|
54
54
|
function createApp(options = {}) {
|
|
55
55
|
const app = new Hono();
|
|
@@ -58,17 +58,23 @@ function createApp(options = {}) {
|
|
|
58
58
|
app.use(
|
|
59
59
|
"*",
|
|
60
60
|
cors({
|
|
61
|
-
origin: resolveAllowedOrigins(options)
|
|
61
|
+
origin: resolveAllowedOrigins(options),
|
|
62
|
+
credentials: options.credentials ?? false
|
|
62
63
|
})
|
|
63
64
|
);
|
|
64
65
|
app.use("*", compress());
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
66
|
+
const jsonBodyLimit = bodyLimit({
|
|
67
|
+
maxSize: options.bodyLimitBytes ?? DEFAULT_BODY_LIMIT_BYTES,
|
|
68
|
+
onError: (c) => c.json({ error: { code: "payload_too_large", message: "Request body too large" } }, 413)
|
|
69
|
+
});
|
|
70
|
+
app.use("*", async (c, next) => {
|
|
71
|
+
const contentType = c.req.header("content-type")?.toLowerCase() ?? "";
|
|
72
|
+
if (contentType.startsWith("multipart/form-data")) {
|
|
73
|
+
await next();
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
return jsonBodyLimit(c, next);
|
|
77
|
+
});
|
|
72
78
|
app.get("/health", (c) => c.json({ status: "ok" }));
|
|
73
79
|
app.onError((err, c) => {
|
|
74
80
|
if (err instanceof AppError) {
|
|
@@ -85,21 +91,43 @@ function createApp(options = {}) {
|
|
|
85
91
|
}
|
|
86
92
|
|
|
87
93
|
// src/serve.ts
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
const
|
|
91
|
-
|
|
94
|
+
function resolveBoundPort(server, fallback) {
|
|
95
|
+
if (typeof server.port === "number") return server.port;
|
|
96
|
+
const addr = server.address?.();
|
|
97
|
+
if (addr && typeof addr === "object" && typeof addr.port === "number") return addr.port;
|
|
98
|
+
return fallback;
|
|
99
|
+
}
|
|
100
|
+
async function closeServerGracefully(server) {
|
|
101
|
+
if (server.stop) {
|
|
102
|
+
await server.stop(false);
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
if (server.close) {
|
|
106
|
+
await new Promise((resolve) => {
|
|
107
|
+
server.close(() => resolve());
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
function createShutdownHandler(server, exit = (code) => process.exit(code)) {
|
|
92
112
|
let shuttingDown = false;
|
|
93
|
-
|
|
113
|
+
return async (signal) => {
|
|
94
114
|
if (shuttingDown) return;
|
|
95
115
|
shuttingDown = true;
|
|
96
116
|
logger.info({ signal }, "shutting down");
|
|
97
|
-
server
|
|
98
|
-
|
|
99
|
-
process.exit(0);
|
|
117
|
+
await closeServerGracefully(server);
|
|
118
|
+
exit(0);
|
|
100
119
|
};
|
|
101
|
-
|
|
102
|
-
|
|
120
|
+
}
|
|
121
|
+
async function serve(app, port = Number(process.env.PORT) || 3e3) {
|
|
122
|
+
const isBun = typeof Bun !== "undefined";
|
|
123
|
+
const server = isBun ? await serveOnBun(app, port) : await serveOnNode(app, port);
|
|
124
|
+
const boundPort = resolveBoundPort(server, port);
|
|
125
|
+
logger.info({ port: boundPort, runtime: isBun ? "bun" : "node" }, "backon app listening");
|
|
126
|
+
const shutdown = createShutdownHandler(server);
|
|
127
|
+
const onSigterm = () => void shutdown("SIGTERM");
|
|
128
|
+
const onSigint = () => void shutdown("SIGINT");
|
|
129
|
+
process.once("SIGTERM", onSigterm);
|
|
130
|
+
process.once("SIGINT", onSigint);
|
|
103
131
|
return server;
|
|
104
132
|
}
|
|
105
133
|
async function serveOnBun(app, port) {
|
|
@@ -119,16 +147,29 @@ async function serveOnNode(app, port) {
|
|
|
119
147
|
}
|
|
120
148
|
|
|
121
149
|
// src/define-route.ts
|
|
150
|
+
import { z } from "zod";
|
|
122
151
|
function defineRoute(schema, handler) {
|
|
123
152
|
const wrapped = async (c) => {
|
|
124
153
|
let body = void 0;
|
|
125
154
|
let query = void 0;
|
|
126
155
|
let params = void 0;
|
|
127
156
|
if (schema.body) {
|
|
128
|
-
const
|
|
157
|
+
const contentLength = c.req.header("content-length");
|
|
158
|
+
const hasBody = c.req.raw.body !== null && contentLength !== "0";
|
|
159
|
+
let raw = {};
|
|
160
|
+
if (hasBody) {
|
|
161
|
+
const text = await c.req.text();
|
|
162
|
+
if (text.length > 0) {
|
|
163
|
+
try {
|
|
164
|
+
raw = JSON.parse(text);
|
|
165
|
+
} catch {
|
|
166
|
+
throw new AppError(400, "invalid_body", "Invalid JSON request body");
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
129
170
|
const parsed = schema.body.safeParse(raw);
|
|
130
171
|
if (!parsed.success) {
|
|
131
|
-
throw new AppError(400, "invalid_body", "Request body failed validation", parsed.error
|
|
172
|
+
throw new AppError(400, "invalid_body", "Request body failed validation", z.flattenError(parsed.error));
|
|
132
173
|
}
|
|
133
174
|
body = parsed.data;
|
|
134
175
|
}
|
|
@@ -136,7 +177,7 @@ function defineRoute(schema, handler) {
|
|
|
136
177
|
const raw = c.req.query();
|
|
137
178
|
const parsed = schema.query.safeParse(raw);
|
|
138
179
|
if (!parsed.success) {
|
|
139
|
-
throw new AppError(400, "invalid_query", "Query params failed validation", parsed.error
|
|
180
|
+
throw new AppError(400, "invalid_query", "Query params failed validation", z.flattenError(parsed.error));
|
|
140
181
|
}
|
|
141
182
|
query = parsed.data;
|
|
142
183
|
}
|
|
@@ -144,7 +185,7 @@ function defineRoute(schema, handler) {
|
|
|
144
185
|
const raw = c.req.param();
|
|
145
186
|
const parsed = schema.params.safeParse(raw);
|
|
146
187
|
if (!parsed.success) {
|
|
147
|
-
throw new AppError(400, "invalid_params", "Route params failed validation", parsed.error
|
|
188
|
+
throw new AppError(400, "invalid_params", "Route params failed validation", z.flattenError(parsed.error));
|
|
148
189
|
}
|
|
149
190
|
params = parsed.data;
|
|
150
191
|
}
|
|
@@ -166,24 +207,83 @@ ${issues}`);
|
|
|
166
207
|
}
|
|
167
208
|
|
|
168
209
|
// src/auth-core.ts
|
|
169
|
-
import { verify } from "hono/jwt";
|
|
210
|
+
import { sign, verify } from "hono/jwt";
|
|
170
211
|
var DEFAULT_ALG = "HS256";
|
|
212
|
+
var textEncoder = new TextEncoder();
|
|
213
|
+
function signJwt(payload, options) {
|
|
214
|
+
const finalPayload = options.expiresInSeconds !== void 0 && payload.exp === void 0 ? { ...payload, exp: Math.floor(Date.now() / 1e3) + options.expiresInSeconds } : payload;
|
|
215
|
+
return sign(finalPayload, options.secret, options.alg ?? DEFAULT_ALG);
|
|
216
|
+
}
|
|
171
217
|
function verifyJwt(token, options) {
|
|
172
218
|
return verify(token, options.secret, options.alg ?? DEFAULT_ALG);
|
|
173
219
|
}
|
|
174
220
|
function extractBearerToken(header) {
|
|
175
221
|
if (!header) return void 0;
|
|
176
|
-
const
|
|
177
|
-
|
|
178
|
-
|
|
222
|
+
const match = header.trim().match(/^Bearer\s+(\S+)$/i);
|
|
223
|
+
return match ? match[1] : void 0;
|
|
224
|
+
}
|
|
225
|
+
function parseCookieHeader(cookieHeader, name) {
|
|
226
|
+
if (!cookieHeader) return void 0;
|
|
227
|
+
for (const part of cookieHeader.split(";")) {
|
|
228
|
+
const eq = part.indexOf("=");
|
|
229
|
+
if (eq === -1) continue;
|
|
230
|
+
const key = part.slice(0, eq).trim();
|
|
231
|
+
if (key !== name) continue;
|
|
232
|
+
const raw = part.slice(eq + 1).trim();
|
|
233
|
+
try {
|
|
234
|
+
return decodeURIComponent(raw);
|
|
235
|
+
} catch {
|
|
236
|
+
return raw;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
return void 0;
|
|
240
|
+
}
|
|
241
|
+
async function verifySignedCookieValue(signedValue, secret) {
|
|
242
|
+
const dot = signedValue.lastIndexOf(".");
|
|
243
|
+
if (dot <= 0) return void 0;
|
|
244
|
+
const value = signedValue.slice(0, dot);
|
|
245
|
+
const sigB64 = signedValue.slice(dot + 1);
|
|
246
|
+
if (sigB64.length !== 44 || !sigB64.endsWith("=")) return void 0;
|
|
247
|
+
let sigBytes;
|
|
248
|
+
try {
|
|
249
|
+
const binary = atob(sigB64);
|
|
250
|
+
sigBytes = new Uint8Array(new ArrayBuffer(binary.length));
|
|
251
|
+
for (let i = 0; i < binary.length; i++) sigBytes[i] = binary.charCodeAt(i);
|
|
252
|
+
} catch {
|
|
253
|
+
return void 0;
|
|
254
|
+
}
|
|
255
|
+
const key = await crypto.subtle.importKey(
|
|
256
|
+
"raw",
|
|
257
|
+
textEncoder.encode(secret),
|
|
258
|
+
{ name: "HMAC", hash: "SHA-256" },
|
|
259
|
+
false,
|
|
260
|
+
["verify"]
|
|
261
|
+
);
|
|
262
|
+
const valid = await crypto.subtle.verify("HMAC", key, sigBytes, textEncoder.encode(value));
|
|
263
|
+
return valid ? value : void 0;
|
|
264
|
+
}
|
|
265
|
+
async function extractAuthToken(authHeader, cookieHeader, options) {
|
|
266
|
+
const bearer = extractBearerToken(authHeader);
|
|
267
|
+
if (bearer) return bearer;
|
|
268
|
+
if (!options.cookieName) return void 0;
|
|
269
|
+
const rawCookie = parseCookieHeader(cookieHeader, options.cookieName);
|
|
270
|
+
if (!rawCookie) return void 0;
|
|
271
|
+
if (options.cookieSecret) {
|
|
272
|
+
return verifySignedCookieValue(rawCookie, options.cookieSecret);
|
|
273
|
+
}
|
|
274
|
+
return rawCookie;
|
|
179
275
|
}
|
|
180
276
|
|
|
181
277
|
// src/require-auth.ts
|
|
182
278
|
function requireAuth(options) {
|
|
183
279
|
return async function requireAuthMiddleware(c, next) {
|
|
184
|
-
const token =
|
|
280
|
+
const token = await extractAuthToken(c.req.header("authorization"), c.req.header("cookie"), options);
|
|
185
281
|
if (!token) {
|
|
186
|
-
throw new AppError(
|
|
282
|
+
throw new AppError(
|
|
283
|
+
401,
|
|
284
|
+
"unauthorized",
|
|
285
|
+
options.cookieName ? "Missing authentication token" : "Missing or malformed Authorization header"
|
|
286
|
+
);
|
|
187
287
|
}
|
|
188
288
|
try {
|
|
189
289
|
const payload = await verifyJwt(token, options);
|
|
@@ -195,20 +295,63 @@ function requireAuth(options) {
|
|
|
195
295
|
};
|
|
196
296
|
}
|
|
197
297
|
|
|
298
|
+
// src/cache-control-core.ts
|
|
299
|
+
function buildCacheControlHeader(options) {
|
|
300
|
+
if (options.noStore) return "no-store";
|
|
301
|
+
const directives = [options.private ? "private" : "public"];
|
|
302
|
+
const maxAge = options.maxAge ?? (options.sMaxAge !== void 0 ? 0 : void 0);
|
|
303
|
+
if (maxAge !== void 0) directives.push(`max-age=${Math.max(0, Math.floor(maxAge))}`);
|
|
304
|
+
if (!options.private && options.sMaxAge !== void 0) {
|
|
305
|
+
directives.push(`s-maxage=${Math.max(0, Math.floor(options.sMaxAge))}`);
|
|
306
|
+
}
|
|
307
|
+
if (options.staleWhileRevalidate !== void 0) {
|
|
308
|
+
directives.push(`stale-while-revalidate=${Math.max(0, Math.floor(options.staleWhileRevalidate))}`);
|
|
309
|
+
}
|
|
310
|
+
if (options.immutable) directives.push("immutable");
|
|
311
|
+
return directives.join(", ");
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
// src/cache-control.ts
|
|
315
|
+
function cacheControl(options) {
|
|
316
|
+
const headerValue = buildCacheControlHeader(options);
|
|
317
|
+
return async function cacheControlMiddleware(c, next) {
|
|
318
|
+
await next();
|
|
319
|
+
if (c.res.status < 400 && !c.res.headers.has("cache-control")) {
|
|
320
|
+
c.header("Cache-Control", headerValue);
|
|
321
|
+
}
|
|
322
|
+
};
|
|
323
|
+
}
|
|
324
|
+
|
|
198
325
|
// src/rate-limit-core.ts
|
|
326
|
+
var DEFAULT_MAX_KEYS = 1e4;
|
|
327
|
+
function extractForwardedIp(forwardedFor, realIp) {
|
|
328
|
+
const firstForwarded = forwardedFor?.split(",").map((part) => part.trim()).find((part) => part.length > 0);
|
|
329
|
+
if (firstForwarded) return firstForwarded;
|
|
330
|
+
const trimmedRealIp = realIp?.trim();
|
|
331
|
+
return trimmedRealIp || void 0;
|
|
332
|
+
}
|
|
199
333
|
var RateLimiter = class {
|
|
200
|
-
constructor(windowMs, max) {
|
|
334
|
+
constructor(windowMs, max, maxKeys = DEFAULT_MAX_KEYS) {
|
|
201
335
|
this.windowMs = windowMs;
|
|
202
336
|
this.max = max;
|
|
337
|
+
this.maxKeys = maxKeys;
|
|
203
338
|
}
|
|
204
339
|
windowMs;
|
|
205
340
|
max;
|
|
341
|
+
maxKeys;
|
|
206
342
|
buckets = /* @__PURE__ */ new Map();
|
|
207
343
|
checksSinceSweep = 0;
|
|
208
344
|
check(key, now = Date.now()) {
|
|
209
345
|
this.maybeSweep(now);
|
|
210
346
|
let bucket = this.buckets.get(key);
|
|
211
347
|
if (!bucket || bucket.resetAt <= now) {
|
|
348
|
+
if (!bucket && this.buckets.size >= this.maxKeys) {
|
|
349
|
+
this.sweepExpired(now);
|
|
350
|
+
if (this.buckets.size >= this.maxKeys) {
|
|
351
|
+
const oldestKey = this.buckets.keys().next().value;
|
|
352
|
+
if (oldestKey !== void 0) this.buckets.delete(oldestKey);
|
|
353
|
+
}
|
|
354
|
+
}
|
|
212
355
|
bucket = { count: 0, resetAt: now + this.windowMs };
|
|
213
356
|
this.buckets.set(key, bucket);
|
|
214
357
|
}
|
|
@@ -223,6 +366,9 @@ var RateLimiter = class {
|
|
|
223
366
|
this.checksSinceSweep += 1;
|
|
224
367
|
if (this.checksSinceSweep < 1e3) return;
|
|
225
368
|
this.checksSinceSweep = 0;
|
|
369
|
+
this.sweepExpired(now);
|
|
370
|
+
}
|
|
371
|
+
sweepExpired(now) {
|
|
226
372
|
for (const [key, bucket] of this.buckets) {
|
|
227
373
|
if (bucket.resetAt <= now) this.buckets.delete(key);
|
|
228
374
|
}
|
|
@@ -234,7 +380,8 @@ var DEFAULT_WINDOW_MS = 6e4;
|
|
|
234
380
|
var DEFAULT_MAX = 60;
|
|
235
381
|
function rateLimit(options = {}) {
|
|
236
382
|
const limiter = new RateLimiter(options.windowMs ?? DEFAULT_WINDOW_MS, options.max ?? DEFAULT_MAX);
|
|
237
|
-
const
|
|
383
|
+
const trustProxy = options.trustProxy ?? false;
|
|
384
|
+
const keyGenerator = options.keyGenerator ?? ((c) => defaultKey(c, trustProxy));
|
|
238
385
|
return async function rateLimitMiddleware(c, next) {
|
|
239
386
|
const result = limiter.check(keyGenerator(c));
|
|
240
387
|
if (!result.allowed) {
|
|
@@ -246,8 +393,12 @@ function rateLimit(options = {}) {
|
|
|
246
393
|
await next();
|
|
247
394
|
};
|
|
248
395
|
}
|
|
249
|
-
function defaultKey(c) {
|
|
250
|
-
|
|
396
|
+
function defaultKey(c, trustProxy) {
|
|
397
|
+
if (trustProxy) {
|
|
398
|
+
const forwarded = extractForwardedIp(c.req.header("x-forwarded-for"), c.req.header("x-real-ip"));
|
|
399
|
+
if (forwarded) return forwarded;
|
|
400
|
+
}
|
|
401
|
+
return "unknown";
|
|
251
402
|
}
|
|
252
403
|
|
|
253
404
|
// src/websocket.ts
|
|
@@ -310,18 +461,21 @@ function defineUpload(options, handler) {
|
|
|
310
461
|
}
|
|
311
462
|
|
|
312
463
|
// src/openapi-core.ts
|
|
313
|
-
import {
|
|
464
|
+
import { z as z2 } from "zod";
|
|
314
465
|
function toOpenApiPath(path) {
|
|
315
466
|
return path.replace(/:([A-Za-z0-9_]+)/g, "{$1}");
|
|
316
467
|
}
|
|
317
468
|
function paramNames(path) {
|
|
318
469
|
return [...path.matchAll(/:([A-Za-z0-9_]+)/g)].map((match) => match[1]).filter((name) => name !== void 0);
|
|
319
470
|
}
|
|
471
|
+
function toOpenApiSchema(schema) {
|
|
472
|
+
return z2.toJSONSchema(schema, { target: "openapi-3.0", unrepresentable: "any" });
|
|
473
|
+
}
|
|
320
474
|
function buildOperation(path, schema) {
|
|
321
475
|
const operation = { responses: { "200": { description: "Success" } } };
|
|
322
476
|
const parameters = [];
|
|
323
477
|
if (schema.params) {
|
|
324
|
-
const jsonSchema =
|
|
478
|
+
const jsonSchema = toOpenApiSchema(schema.params);
|
|
325
479
|
for (const name of paramNames(path)) {
|
|
326
480
|
parameters.push({
|
|
327
481
|
name,
|
|
@@ -332,7 +486,7 @@ function buildOperation(path, schema) {
|
|
|
332
486
|
}
|
|
333
487
|
}
|
|
334
488
|
if (schema.query) {
|
|
335
|
-
const jsonSchema =
|
|
489
|
+
const jsonSchema = toOpenApiSchema(schema.query);
|
|
336
490
|
const required = new Set(jsonSchema.required ?? []);
|
|
337
491
|
for (const [name, propSchema] of Object.entries(jsonSchema.properties ?? {})) {
|
|
338
492
|
parameters.push({ name, in: "query", required: required.has(name), schema: propSchema });
|
|
@@ -342,7 +496,7 @@ function buildOperation(path, schema) {
|
|
|
342
496
|
if (schema.body) {
|
|
343
497
|
operation.requestBody = {
|
|
344
498
|
required: true,
|
|
345
|
-
content: { "application/json": { schema:
|
|
499
|
+
content: { "application/json": { schema: toOpenApiSchema(schema.body) } }
|
|
346
500
|
};
|
|
347
501
|
}
|
|
348
502
|
return operation;
|
|
@@ -365,12 +519,18 @@ function buildOpenApiDocument(routes, options) {
|
|
|
365
519
|
paths
|
|
366
520
|
};
|
|
367
521
|
}
|
|
522
|
+
function escapeHtml(value) {
|
|
523
|
+
return value.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'");
|
|
524
|
+
}
|
|
525
|
+
function serializeInlineJson(value) {
|
|
526
|
+
return JSON.stringify(value).replace(/</g, "\\u003c");
|
|
527
|
+
}
|
|
368
528
|
function pageShell(title, config) {
|
|
369
529
|
const fullConfig = { showDeveloperTools: "never", ...config };
|
|
370
530
|
return `<!doctype html>
|
|
371
531
|
<html>
|
|
372
532
|
<head>
|
|
373
|
-
<title>${title}</title>
|
|
533
|
+
<title>${escapeHtml(title)}</title>
|
|
374
534
|
<meta charset="utf-8" />
|
|
375
535
|
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
376
536
|
</head>
|
|
@@ -378,7 +538,7 @@ function pageShell(title, config) {
|
|
|
378
538
|
<div id="api-reference"></div>
|
|
379
539
|
<script src="https://cdn.jsdelivr.net/npm/@scalar/api-reference"></script>
|
|
380
540
|
<script>
|
|
381
|
-
Scalar.createApiReference("#api-reference", ${
|
|
541
|
+
Scalar.createApiReference("#api-reference", ${serializeInlineJson(fullConfig)});
|
|
382
542
|
</script>
|
|
383
543
|
</body>
|
|
384
544
|
</html>
|
|
@@ -402,28 +562,41 @@ function generateOpenApiSpec(app, options = {}) {
|
|
|
402
562
|
return buildOpenApiDocument(routes, options);
|
|
403
563
|
}
|
|
404
564
|
function serveApiDocs(app, path = "/docs", options = {}) {
|
|
405
|
-
const
|
|
406
|
-
|
|
407
|
-
|
|
565
|
+
const basePath = path === "/" ? "" : path.replace(/\/+$/, "");
|
|
566
|
+
const docPath = basePath || "/";
|
|
567
|
+
const specPath = `${basePath}/openapi.json`;
|
|
568
|
+
const spec = generateOpenApiSpec(app, options);
|
|
569
|
+
const specJson = JSON.stringify(spec);
|
|
570
|
+
const html = apiReferenceHtmlInline(spec, options.title);
|
|
571
|
+
app.get(specPath, (c) => c.body(specJson, 200, { "Content-Type": "application/json; charset=UTF-8" }));
|
|
572
|
+
app.get(docPath, (c) => c.html(html));
|
|
408
573
|
}
|
|
409
574
|
|
|
410
575
|
// src/index.ts
|
|
411
576
|
import { Hono as Hono2 } from "hono";
|
|
577
|
+
import { setCookie, getCookie, deleteCookie, setSignedCookie, getSignedCookie } from "hono/cookie";
|
|
412
578
|
export {
|
|
413
579
|
AppError,
|
|
414
580
|
Hono2 as Hono,
|
|
415
581
|
apiReferenceHtml,
|
|
416
582
|
apiReferenceHtmlInline,
|
|
583
|
+
cacheControl,
|
|
417
584
|
createApp,
|
|
418
585
|
defineRoute,
|
|
419
586
|
defineStream,
|
|
420
587
|
defineUpload,
|
|
421
588
|
defineWebSocket,
|
|
589
|
+
deleteCookie,
|
|
422
590
|
generateOpenApiSpec,
|
|
591
|
+
getCookie,
|
|
592
|
+
getSignedCookie,
|
|
423
593
|
logger,
|
|
424
594
|
rateLimit,
|
|
425
595
|
requireAuth,
|
|
426
596
|
requireEnv,
|
|
427
597
|
serve,
|
|
428
|
-
serveApiDocs
|
|
598
|
+
serveApiDocs,
|
|
599
|
+
setCookie,
|
|
600
|
+
setSignedCookie,
|
|
601
|
+
signJwt
|
|
429
602
|
};
|
package/dist/openapi-core.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"openapi-core.d.ts","sourceRoot":"","sources":["../src/openapi-core.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"openapi-core.d.ts","sourceRoot":"","sources":["../src/openapi-core.ts"],"names":[],"mappings":"AAAA,OAAO,EAAK,KAAK,OAAO,EAAE,MAAM,KAAK,CAAC;AAEtC,MAAM,WAAW,cAAc;IAC7B,yBAAyB;IACzB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,2BAA2B;IAC3B,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB;AAED,MAAM,WAAW,eAAe;IAC9B,IAAI,CAAC,EAAE,OAAO,CAAC,OAAO,CAAC,CAAC;IACxB,KAAK,CAAC,EAAE,OAAO,CAAC,OAAO,CAAC,CAAC;IACzB,MAAM,CAAC,EAAE,OAAO,CAAC,OAAO,CAAC,CAAC;CAC3B;AAED,MAAM,WAAW,cAAc;IAC7B,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,CAAC,EAAE,eAAe,CAAC;CAC1B;AAED,qGAAqG;AACrG,wBAAgB,aAAa,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAElD;AAgDD;;;;;;;GAOG;AACH,wBAAgB,oBAAoB,CAAC,MAAM,EAAE,cAAc,EAAE,EAAE,OAAO,EAAE,cAAc,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAmB/G;AAwCD;;;;;;;;;;GAUG;AACH,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,SAAkB,GAAG,MAAM,CAEjF;AAED;;;;;;;GAOG;AACH,wBAAgB,sBAAsB,CAAC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,KAAK,SAAkB,GAAG,MAAM,CAErG"}
|
package/dist/openapi.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"openapi.d.ts","sourceRoot":"","sources":["../src/openapi.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AACjC,OAAO,EAAgD,KAAK,cAAc,EAAuB,MAAM,gBAAgB,CAAC;AAGxH,YAAY,EAAE,cAAc,EAAE,CAAC;AAI/B;;;;;GAKG;AACH,wBAAgB,mBAAmB,CAAC,GAAG,EAAE,IAAI,CAAC,GAAG,CAAC,EAAE,OAAO,GAAE,cAAmB,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAUzG;AAED;;;;;;;;;GASG;AACH,wBAAgB,YAAY,CAAC,GAAG,EAAE,IAAI,CAAC,GAAG,CAAC,EAAE,IAAI,SAAU,EAAE,OAAO,GAAE,cAAmB,GAAG,IAAI,
|
|
1
|
+
{"version":3,"file":"openapi.d.ts","sourceRoot":"","sources":["../src/openapi.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AACjC,OAAO,EAAgD,KAAK,cAAc,EAAuB,MAAM,gBAAgB,CAAC;AAGxH,YAAY,EAAE,cAAc,EAAE,CAAC;AAI/B;;;;;GAKG;AACH,wBAAgB,mBAAmB,CAAC,GAAG,EAAE,IAAI,CAAC,GAAG,CAAC,EAAE,OAAO,GAAE,cAAmB,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAUzG;AAED;;;;;;;;;GASG;AACH,wBAAgB,YAAY,CAAC,GAAG,EAAE,IAAI,CAAC,GAAG,CAAC,EAAE,IAAI,SAAU,EAAE,OAAO,GAAE,cAAmB,GAAG,IAAI,CAS/F"}
|
|
@@ -14,14 +14,18 @@ export interface RateLimitCheckResult {
|
|
|
14
14
|
/** Milliseconds until this key's window resets. Only meaningful when allowed is false. */
|
|
15
15
|
retryAfterMs: number;
|
|
16
16
|
}
|
|
17
|
+
/** Extracts the leftmost non-empty client IP from proxy headers, ignoring blank values. */
|
|
18
|
+
export declare function extractForwardedIp(forwardedFor: string | undefined, realIp: string | undefined): string | undefined;
|
|
17
19
|
export declare class RateLimiter {
|
|
18
20
|
private readonly windowMs;
|
|
19
21
|
private readonly max;
|
|
22
|
+
private readonly maxKeys;
|
|
20
23
|
private readonly buckets;
|
|
21
24
|
private checksSinceSweep;
|
|
22
|
-
constructor(windowMs: number, max: number);
|
|
25
|
+
constructor(windowMs: number, max: number, maxKeys?: number);
|
|
23
26
|
check(key: string, now?: number): RateLimitCheckResult;
|
|
24
27
|
/** Opportunistic cleanup so long-lived processes don't accumulate one bucket per distinct key forever. */
|
|
25
28
|
private maybeSweep;
|
|
29
|
+
private sweepExpired;
|
|
26
30
|
}
|
|
27
31
|
//# sourceMappingURL=rate-limit-core.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rate-limit-core.d.ts","sourceRoot":"","sources":["../src/rate-limit-core.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAOH,MAAM,WAAW,oBAAoB;IACnC,OAAO,EAAE,OAAO,CAAC;IACjB,0FAA0F;IAC1F,YAAY,EAAE,MAAM,CAAC;CACtB;AAED,qBAAa,WAAW;IAKpB,OAAO,CAAC,QAAQ,CAAC,QAAQ;IACzB,OAAO,CAAC,QAAQ,CAAC,GAAG;
|
|
1
|
+
{"version":3,"file":"rate-limit-core.d.ts","sourceRoot":"","sources":["../src/rate-limit-core.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAOH,MAAM,WAAW,oBAAoB;IACnC,OAAO,EAAE,OAAO,CAAC;IACjB,0FAA0F;IAC1F,YAAY,EAAE,MAAM,CAAC;CACtB;AAID,2FAA2F;AAC3F,wBAAgB,kBAAkB,CAChC,YAAY,EAAE,MAAM,GAAG,SAAS,EAChC,MAAM,EAAE,MAAM,GAAG,SAAS,GACzB,MAAM,GAAG,SAAS,CAQpB;AAED,qBAAa,WAAW;IAKpB,OAAO,CAAC,QAAQ,CAAC,QAAQ;IACzB,OAAO,CAAC,QAAQ,CAAC,GAAG;IACpB,OAAO,CAAC,QAAQ,CAAC,OAAO;IAN1B,OAAO,CAAC,QAAQ,CAAC,OAAO,CAA6B;IACrD,OAAO,CAAC,gBAAgB,CAAK;IAE7B,YACmB,QAAQ,EAAE,MAAM,EAChB,GAAG,EAAE,MAAM,EACX,OAAO,GAAE,MAAyB,EACjD;IAEJ,KAAK,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,GAAE,MAAmB,GAAG,oBAAoB,CAuBjE;IAED,0GAA0G;IAC1G,OAAO,CAAC,UAAU;IAOlB,OAAO,CAAC,YAAY;CAKrB"}
|
package/dist/rate-limit.d.ts
CHANGED
|
@@ -5,6 +5,12 @@ export interface RateLimitOptions {
|
|
|
5
5
|
windowMs?: number;
|
|
6
6
|
/** Max requests allowed per key within the window. Defaults to 60. */
|
|
7
7
|
max?: number;
|
|
8
|
+
/**
|
|
9
|
+
* Trust `X-Forwarded-For` / `X-Real-IP` headers when deriving the default key.
|
|
10
|
+
* Off by default because untrusted clients can spoof those headers; enable
|
|
11
|
+
* only when running behind a reverse proxy that sets/overwrites them.
|
|
12
|
+
*/
|
|
13
|
+
trustProxy?: boolean;
|
|
8
14
|
/** How to derive the bucket key from a request. Defaults to the caller's IP. */
|
|
9
15
|
keyGenerator?: (c: Context<Env>) => string;
|
|
10
16
|
}
|
package/dist/rate-limit.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rate-limit.d.ts","sourceRoot":"","sources":["../src/rate-limit.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAC1C,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,SAAS,CAAC;AAInC,MAAM,WAAW,gBAAgB;IAC/B,wDAAwD;IACxD,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,sEAAsE;IACtE,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,gFAAgF;IAChF,YAAY,CAAC,EAAE,CAAC,CAAC,EAAE,OAAO,CAAC,GAAG,CAAC,KAAK,MAAM,CAAC;CAC5C;AAKD;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,SAAS,CAAC,OAAO,GAAE,gBAAqB,
|
|
1
|
+
{"version":3,"file":"rate-limit.d.ts","sourceRoot":"","sources":["../src/rate-limit.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAC1C,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,SAAS,CAAC;AAInC,MAAM,WAAW,gBAAgB;IAC/B,wDAAwD;IACxD,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,sEAAsE;IACtE,GAAG,CAAC,EAAE,MAAM,CAAC;IACb;;;;OAIG;IACH,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,gFAAgF;IAChF,YAAY,CAAC,EAAE,CAAC,CAAC,EAAE,OAAO,CAAC,GAAG,CAAC,KAAK,MAAM,CAAC;CAC5C;AAKD;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,SAAS,CAAC,OAAO,GAAE,gBAAqB,OAKT,OAAO,CAAC,GAAG,CAAC,QAAQ,IAAI,mBAUtE"}
|
package/dist/require-auth.d.ts
CHANGED
|
@@ -2,20 +2,9 @@ import type { Context, Next } from "hono";
|
|
|
2
2
|
import type { Env } from "./types";
|
|
3
3
|
import { type VerifyJwtOptions } from "./auth-core";
|
|
4
4
|
/**
|
|
5
|
-
* Verifies a bearer JWT and stashes the decoded payload
|
|
6
|
-
* `auth`, retrievable in any handler downstream via
|
|
7
|
-
* AppError(401, "unauthorized") if
|
|
8
|
-
* token fails verification (bad signature, expired, wrong algorithm) —
|
|
9
|
-
* createApp()'s error handler turns that into the standard error JSON shape,
|
|
10
|
-
* same as any other AppError.
|
|
11
|
-
*
|
|
12
|
-
* This is verification only: issuing tokens (login, signup, refresh
|
|
13
|
-
* rotation, revocation) is left to the consuming project, since that's
|
|
14
|
-
* genuinely different per project and baking in an opinion there risks
|
|
15
|
-
* being wrong for whatever project actually uses it. Apply it per-route or
|
|
16
|
-
* per-group, not globally:
|
|
17
|
-
*
|
|
18
|
-
* protectedRoutes.use("*", requireAuth({ secret: process.env.JWT_SECRET! }));
|
|
5
|
+
* Verifies a bearer (or optional cookie) JWT and stashes the decoded payload
|
|
6
|
+
* on the context as `auth`, retrievable in any handler downstream via
|
|
7
|
+
* `c.get("auth")`. Throws AppError(401, "unauthorized") if missing or invalid.
|
|
19
8
|
*/
|
|
20
9
|
export declare function requireAuth(options: VerifyJwtOptions): (c: Context<Env>, next: Next) => Promise<void>;
|
|
21
10
|
//# sourceMappingURL=require-auth.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"require-auth.d.ts","sourceRoot":"","sources":["../src/require-auth.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAC1C,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,SAAS,CAAC;AAEnC,OAAO,
|
|
1
|
+
{"version":3,"file":"require-auth.d.ts","sourceRoot":"","sources":["../src/require-auth.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAC1C,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,SAAS,CAAC;AAEnC,OAAO,EAA+B,KAAK,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAEjF;;;;GAIG;AACH,wBAAgB,WAAW,CAAC,OAAO,EAAE,gBAAgB,OACJ,OAAO,CAAC,GAAG,CAAC,QAAQ,IAAI,mBAmBxE"}
|
package/dist/serve.d.ts
CHANGED
|
@@ -2,9 +2,20 @@ import type { Hono } from "hono";
|
|
|
2
2
|
import type { Env } from "./types";
|
|
3
3
|
/** The bits of Bun's/`@hono/node-server`'s return value shutdown actually needs — not either library's real type, just the overlap. */
|
|
4
4
|
interface StoppableServer {
|
|
5
|
-
stop?: () => void
|
|
5
|
+
stop?: (closeActiveConnections?: boolean) => void | Promise<void>;
|
|
6
6
|
close?: (callback?: (err?: Error) => void) => void;
|
|
7
|
+
port?: number;
|
|
8
|
+
address?: () => {
|
|
9
|
+
port?: number;
|
|
10
|
+
} | string | null;
|
|
7
11
|
}
|
|
12
|
+
/** Stops accepting new connections and waits for in-flight requests to complete. */
|
|
13
|
+
export declare function closeServerGracefully(server: StoppableServer): Promise<void>;
|
|
14
|
+
/**
|
|
15
|
+
* Creates a signal-safe shutdown handler that stops accepting new connections,
|
|
16
|
+
* waits for in-flight requests to finish, and then invokes `exit(0)`.
|
|
17
|
+
*/
|
|
18
|
+
export declare function createShutdownHandler(server: StoppableServer, exit?: (code: number) => void): (signal: string) => Promise<void>;
|
|
8
19
|
/**
|
|
9
20
|
* Starts the app — `Bun.serve()` when running under Bun (the default, and
|
|
10
21
|
* the faster of the two), `@hono/node-server`'s `serve()` when running
|