Files
SnowLuma/.pnpm-store/v11/files/0e/743e6844f607fa3b2eaaa3d93f440e8f8faf2a741e87ac74ec4211df20ddca9c26985216f4106b75f8995928c0688515b094b11f7e1017ca9e9118e05409af
TanTan 2a69b23b47
Dev Build / cleanup (push) Canceled after 0s
Dev Build / typecheck (push) Canceled after 0s
Dev Build / build (linux-arm64, ubuntu-24.04-arm, linux-arm64) (push) Canceled after 0s
Dev Build / build (linux-x64, ubuntu-latest, linux-x64) (push) Canceled after 0s
Dev Build / build (win-x64, windows-latest, win32-x64) (push) Canceled after 0s
Dev Build / dispatch-dev-docker (push) Canceled after 0s
docs: 仓库链接指向本镜像仓库
2026-11-13 09:27:07 +08:00

85 lines
3.4 KiB
Plaintext

Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
require("../../router.js");
const require_router_trie_router_router = require("../../router/trie-router/router.js");
require("../../router/trie-router/index.js");
const require_helper_route_index = require("../../helper/route/index.js");
//#region src/middleware/method-not-allowed/index.ts
/**
* Method Not Allowed Middleware for Hono.
*
* Returns a `405 Method Not Allowed` response with an `Allow` header when the request path
* matches a registered route but the request method is not supported.
*
* @param {MethodNotAllowedOptions} options - The options for the middleware.
* @param {Hono} options.app - The Hono instance used by the application.
* @param {MethodNotAllowedHandler} [options.onMethodNotAllowed] - Generates the response, including its `Allow` header.
* @returns {MiddlewareHandler} The middleware handler function.
*
* @example
* ```ts
* const app = new Hono()
*
* app.use(methodNotAllowed({ app }))
*
* app.get('/hello', (c) => c.text('Hello!'))
* app.post('/hello', (c) => c.text('Posted!'))
*
* // PUT /hello -> 405 Method Not Allowed
* // Allow: GET, HEAD, POST
* ```
*
* @example
* ```ts
* app.use(
* methodNotAllowed({
* app,
* onMethodNotAllowed: (c, methods) =>
* c.json({ error: 'Method Not Allowed' }, 405, { Allow: methods.join(', ') }),
* })
* )
* ```
*/
const methodNotAllowed = (options) => {
let methodRouter;
return async function methodNotAllowed(c, next) {
const routeIndex = c.req.routeIndex;
await next();
if (c.res.status !== 404) return;
if (c.error) return;
if (!methodRouter) {
const methodsByPath = /* @__PURE__ */ new Map();
for (const route of options.app.routes) {
if (route.method === "ALL" || route.method === "HEAD") continue;
const methods = methodsByPath.get(route.path) ?? /* @__PURE__ */ new Set();
methods.add(route.method);
if (route.method === "GET") methods.add("HEAD");
methodsByPath.set(route.path, methods);
}
methodRouter = new require_router_trie_router_router.TrieRouter();
for (const [path, methods] of methodsByPath) methodRouter.add("ALL", path, [...methods]);
}
let requestPath = c.req.path;
const currentRoute = require_helper_route_index.matchedRoutes(c)[routeIndex];
const sourceRoute = options.app.routes.find((route) => route.handler === methodNotAllowed);
if (currentRoute && sourceRoute) {
const currentBasePathParts = require_helper_route_index.basePath(c, routeIndex).split("/").filter(Boolean);
const sourceBasePathLength = sourceRoute.basePath.split("/").filter(Boolean).length;
const mountBasePathParts = currentBasePathParts.slice(0, currentBasePathParts.length - sourceBasePathLength);
if (mountBasePathParts.length > 0) {
const mountBasePath = `/${mountBasePathParts.join("/")}`;
requestPath = c.req.path.slice(mountBasePath.length) || "/";
}
}
const allowedMethods = /* @__PURE__ */ new Set();
for (const [methods] of methodRouter.match("ALL", requestPath)[0]) for (const method of methods) allowedMethods.add(method);
if (allowedMethods.size === 0 || allowedMethods.has(c.req.method)) return;
c.res.headers.delete("Allow");
c.res.headers.delete("Content-Length");
const methods = [...allowedMethods];
const allow = methods.join(", ");
c.res = options.onMethodNotAllowed ? await options.onMethodNotAllowed(c, methods) : c.text("Method Not Allowed", 405, { Allow: allow });
};
};
//#endregion
exports.methodNotAllowed = methodNotAllowed;