Files
SnowLuma/.pnpm-store/v11/files/76/6391932b2d21ac1e4022cee464002e7a07097317317f2149955239041fe06802100dd3fbdf35190db738c7d30a4798276f32dec7bfc9fe18747a5e5b525343
T
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) Skipped
docs: 仓库链接指向本镜像仓库
2026-11-13 09:27:07 +08:00

72 lines
2.3 KiB
Plaintext

Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const require_helper_route_index = require("../../helper/route/index.js");
//#region src/middleware/mount/index.ts
const defaultReplaceRequest = (c) => {
const pathPrefix = require_helper_route_index.routePath(c).replace(/\/\*$/, "");
const url = new URL(c.req.raw.url);
url.pathname = c.req.path.slice(pathPrefix.length) || "/";
return new Request(url, c.req.raw);
};
const defaultGetOptions = (c) => {
let executionContext = void 0;
try {
executionContext = c.executionCtx;
} catch {}
return [c.env, executionContext];
};
/**
* `mount()` allows you to mount applications built with other frameworks into your Hono application.
*
* @see {@link https://hono.dev/docs/api/hono#mount}
*
* @param {Function} applicationHandler - other Request Handler
* @param {MountOptions} [options] - options of `mount()`
* @returns {MiddlewareHandler} handler to register with `app.all()`
*
* @example
* ```ts
* import { Router as IttyRouter } from 'itty-router'
* import { Hono } from 'hono'
* import { mount } from 'hono/mount'
* // Create itty-router application
* const ittyRouter = IttyRouter()
* // GET /itty-router/hello
* ittyRouter.get('/hello', () => new Response('Hello from itty-router'))
*
* const app = new Hono()
* app.all('/itty-router/*', mount(ittyRouter.handle))
* ```
*
* @example
* ```ts
* const app = new Hono()
* // Send the request to another application without modification.
* app.all('/app/*', mount(anotherApp, {
* replaceRequest: (req) => req,
* }))
* ```
*/
const mount = (applicationHandler, options) => {
let replaceRequest;
let optionHandler;
if (options) {
if (typeof options === "function") optionHandler = options;
else {
optionHandler = options.optionHandler;
if (options.replaceRequest === false) replaceRequest = (request) => request;
else replaceRequest = options.replaceRequest;
}
}
const getOptions = optionHandler ? (c) => {
const options = optionHandler(c);
return Array.isArray(options) ? options : [options];
} : defaultGetOptions;
return async (c, next) => {
const res = await applicationHandler(replaceRequest ? replaceRequest(c.req.raw) : defaultReplaceRequest(c), ...getOptions(c));
if (res) return res;
await next();
};
};
//#endregion
exports.mount = mount;