Files
SnowLuma/.pnpm-store/v11/files/1d/ebe2c095dee3febacd36a69608d3046022e87d296a372a4550583f758a7e3719deef3849e4e78bf5ff7de4af62440201dac9ab853ae0ae0dfe3425ea7c78a9
TanTan 2a69b23b47
Dev Build / cleanup (push) Waiting to run
Dev Build / typecheck (push) Waiting to run
docs: 仓库链接指向本镜像仓库
2026-11-13 09:27:07 +08:00

71 lines
2.1 KiB
Plaintext

import { routePath } from "../../helper/route/index.js";
//#region src/middleware/mount/index.ts
const defaultReplaceRequest = (c) => {
const pathPrefix = 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
export { mount };