Z User
phase7: MCP stdio smoke test (handshake + tools/list + get_roots 6:74) — 4/4 tests PASS
54a6341 Download node_modules/hono/dist/cjs/request.js from bravesigma/quran-md-plugin: direct link, hf CLI and curl.
- Browser
- Download file 9.12 kB
-
https://huggingface.co/datasets/bravesigma/quran-md-plugin/resolve/main/node_modules/hono/dist/cjs/request.js
- Command line
-
hf download hf://datasets/bravesigma/quran-md-plugin/node_modules/hono/dist/cjs/request.js
-
curl -L -o request.js https://huggingface.co/datasets/bravesigma/quran-md-plugin/resolve/main/node_modules/hono/dist/cjs/request.js
9.12 kB
| Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); | |
| const require_http_exception = require("./http-exception.js"); | |
| const require_request_constants = require("./request/constants.js"); | |
| const require_utils_body = require("./utils/body.js"); | |
| const require_utils_url = require("./utils/url.js"); | |
| //#region src/request.ts | |
| var HonoRequest = class { | |
| /** | |
| * `.raw` can get the raw Request object. | |
| * | |
| * @see {@link https://hono.dev/docs/api/request#raw} | |
| * | |
| * @example | |
| * ```ts | |
| * // For Cloudflare Workers | |
| * app.post('/', async (c) => { | |
| * const metadata = c.req.raw.cf?.hostMetadata? | |
| * ... | |
| * }) | |
| * ``` | |
| */ | |
| raw; | |
| #validatedData; | |
| #matchResult; | |
| routeIndex = 0; | |
| /** | |
| * `.path` can get the pathname of the request. | |
| * | |
| * @see {@link https://hono.dev/docs/api/request#path} | |
| * | |
| * @example | |
| * ```ts | |
| * app.get('/about/me', (c) => { | |
| * const pathname = c.req.path // `/about/me` | |
| * }) | |
| * ``` | |
| */ | |
| path; | |
| bodyCache = {}; | |
| constructor(request, path = "/", matchResult = [[]]) { | |
| this.raw = request; | |
| this.path = path; | |
| this.#matchResult = matchResult; | |
| } | |
| param(key) { | |
| return key ? this.#getDecodedParam(key) : this.#getAllDecodedParams(); | |
| } | |
| #getDecodedParam(key) { | |
| const paramKey = this.#matchResult[0][this.routeIndex]?.[1][key]; | |
| const param = this.#getParamValue(paramKey); | |
| return param && require_utils_url.tryDecodeURIComponent(param); | |
| } | |
| #getAllDecodedParams() { | |
| const decoded = {}; | |
| const keys = Object.keys(this.#matchResult[0][this.routeIndex]?.[1] ?? {}); | |
| for (const key of keys) { | |
| const value = this.#getParamValue(this.#matchResult[0][this.routeIndex][1][key]); | |
| if (value !== void 0) decoded[key] = require_utils_url.tryDecodeURIComponent(value); | |
| } | |
| return decoded; | |
| } | |
| #getParamValue(paramKey) { | |
| return this.#matchResult[1] ? this.#matchResult[1][paramKey] : paramKey; | |
| } | |
| query(key) { | |
| return require_utils_url.getQueryParam(this.url, key); | |
| } | |
| queries(key) { | |
| return require_utils_url.getQueryParams(this.url, key); | |
| } | |
| header(name) { | |
| if (name) return this.raw.headers.get(name) ?? void 0; | |
| const headerData = Object.create(null); | |
| this.raw.headers.forEach((value, key) => { | |
| headerData[key] = value; | |
| }); | |
| return headerData; | |
| } | |
| async parseBody(options) { | |
| return require_utils_body.parseBody(this, options); | |
| } | |
| #cachedBody = (key) => { | |
| const { bodyCache, raw } = this; | |
| const cachedBody = bodyCache[key]; | |
| if (cachedBody) return cachedBody; | |
| for (const anyCachedKey in bodyCache) return bodyCache[anyCachedKey].then((body) => { | |
| if (anyCachedKey === "json") body = JSON.stringify(body); | |
| const contentType = anyCachedKey === "formData" ? void 0 : raw.headers.get("content-type"); | |
| return new Response(body, { headers: contentType ? { "Content-Type": contentType } : void 0 })[key](); | |
| }); | |
| return bodyCache[key] = raw[key](); | |
| }; | |
| /** | |
| * `.json()` can parse Request body of type `application/json` | |
| * | |
| * @see {@link https://hono.dev/docs/api/request#json} | |
| * | |
| * @example | |
| * ```ts | |
| * app.post('/entry', async (c) => { | |
| * const body = await c.req.json() | |
| * }) | |
| * ``` | |
| */ | |
| json() { | |
| return this.#cachedBody("text").then((text) => JSON.parse(text)); | |
| } | |
| /** | |
| * `.text()` can parse Request body of type `text/plain` | |
| * | |
| * @see {@link https://hono.dev/docs/api/request#text} | |
| * | |
| * @example | |
| * ```ts | |
| * app.post('/entry', async (c) => { | |
| * const body = await c.req.text() | |
| * }) | |
| * ``` | |
| */ | |
| text() { | |
| return this.#cachedBody("text"); | |
| } | |
| /** | |
| * `.arrayBuffer()` parse Request body as an `ArrayBuffer` | |
| * | |
| * @see {@link https://hono.dev/docs/api/request#arraybuffer} | |
| * | |
| * @example | |
| * ```ts | |
| * app.post('/entry', async (c) => { | |
| * const body = await c.req.arrayBuffer() | |
| * }) | |
| * ``` | |
| */ | |
| arrayBuffer() { | |
| return this.#cachedBody("arrayBuffer"); | |
| } | |
| /** | |
| * `.bytes()` parses the request body as a `Uint8Array`. | |
| * | |
| * @see {@link https://hono.dev/docs/api/request#bytes} | |
| * | |
| * @example | |
| * ```ts | |
| * app.post('/entry', async (c) => { | |
| * const body = await c.req.bytes() | |
| * }) | |
| * ``` | |
| */ | |
| bytes() { | |
| return this.#cachedBody("arrayBuffer").then((buffer) => new Uint8Array(buffer)); | |
| } | |
| /** | |
| * Parses the request body as a `Blob`. | |
| * @example | |
| * ```ts | |
| * app.post('/entry', async (c) => { | |
| * const body = await c.req.blob(); | |
| * }); | |
| * ``` | |
| * @see https://hono.dev/docs/api/request#blob | |
| */ | |
| blob() { | |
| return this.#cachedBody("blob"); | |
| } | |
| /** | |
| * Parses the request body as `FormData`. | |
| * @example | |
| * ```ts | |
| * app.post('/entry', async (c) => { | |
| * const body = await c.req.formData(); | |
| * }); | |
| * ``` | |
| * @see https://hono.dev/docs/api/request#formdata | |
| */ | |
| formData() { | |
| return this.#cachedBody("formData"); | |
| } | |
| /** | |
| * Adds validated data to the request. | |
| * | |
| * @param target - The target of the validation. | |
| * @param data - The validated data to add. | |
| */ | |
| addValidatedData(target, data) { | |
| (this.#validatedData ??= {})[target] = data; | |
| } | |
| valid(target) { | |
| return this.#validatedData?.[target]; | |
| } | |
| /** | |
| * `.url` can get the request url strings. | |
| * | |
| * @see {@link https://hono.dev/docs/api/request#url} | |
| * | |
| * @example | |
| * ```ts | |
| * app.get('/about/me', (c) => { | |
| * const url = c.req.url // `http://localhost:8787/about/me` | |
| * ... | |
| * }) | |
| * ``` | |
| */ | |
| get url() { | |
| return this.raw.url; | |
| } | |
| /** | |
| * `.method` can get the method name of the request. | |
| * | |
| * @see {@link https://hono.dev/docs/api/request#method} | |
| * | |
| * @example | |
| * ```ts | |
| * app.get('/about/me', (c) => { | |
| * const method = c.req.method // `GET` | |
| * }) | |
| * ``` | |
| */ | |
| get method() { | |
| return this.raw.method; | |
| } | |
| get [require_request_constants.GET_MATCH_RESULT]() { | |
| return this.#matchResult; | |
| } | |
| /** | |
| * `.matchedRoutes` can return a matched route in the handler | |
| * | |
| * @deprecated | |
| * | |
| * Use matchedRoutes helper defined in "hono/route" instead. | |
| * | |
| * @see {@link https://hono.dev/docs/api/request#matchedroutes} | |
| * | |
| * @example | |
| * ```ts | |
| * app.use('*', async function logger(c, next) { | |
| * await next() | |
| * c.req.matchedRoutes.forEach(({ handler, method, path }, i) => { | |
| * const name = handler.name || (handler.length < 2 ? '[handler]' : '[middleware]') | |
| * console.log( | |
| * method, | |
| * ' ', | |
| * path, | |
| * ' '.repeat(Math.max(10 - path.length, 0)), | |
| * name, | |
| * i === c.req.routeIndex ? '<- respond from here' : '' | |
| * ) | |
| * }) | |
| * }) | |
| * ``` | |
| */ | |
| get matchedRoutes() { | |
| return this.#matchResult[0].map(([[, route]]) => route); | |
| } | |
| /** | |
| * `.routePath` can retrieve the path registered within the handler | |
| * | |
| * @deprecated | |
| * | |
| * Use routePath helper defined in "hono/route" instead. | |
| * | |
| * @see {@link https://hono.dev/docs/api/request#routepath} | |
| * | |
| * @example | |
| * ```ts | |
| * app.get('/posts/:id', (c) => { | |
| * return c.json({ path: c.req.routePath }) | |
| * }) | |
| * ``` | |
| */ | |
| get routePath() { | |
| return this.#matchResult[0].map(([[, route]]) => route)[this.routeIndex].path; | |
| } | |
| }; | |
| /** | |
| * Clones a HonoRequest's underlying raw Request object. | |
| * | |
| * This utility handles both consumed and unconsumed request bodies: | |
| * - If the request body hasn't been consumed, it uses the native `clone()` method | |
| * - If the request body has been consumed, it reconstructs a new Request using cached body data | |
| * | |
| * This is particularly useful when you need to: | |
| * - Process the same request body multiple times | |
| * - Pass requests to external services after validation | |
| * | |
| * @param req - The HonoRequest object to clone | |
| * @returns A Promise that resolves to a new Request object with the same properties | |
| * @throws {HTTPException} If the request body was consumed directly via `req.raw` | |
| * without using HonoRequest methods (e.g., `req.json()`, `req.text()`), making it | |
| * impossible to reconstruct the body from cache | |
| * | |
| * @example | |
| * ```ts | |
| * // Clone after consuming the body (e.g., after validation) | |
| * app.post('/forward', | |
| * validator('json', (data) => data), | |
| * async (c) => { | |
| * const validated = c.req.valid('json') | |
| * // Body has been consumed, but cloneRawRequest still works | |
| * const clonedReq = await cloneRawRequest(c.req) | |
| * return fetch('http://backend-service.com', clonedReq) | |
| * } | |
| * ) | |
| * ``` | |
| */ | |
| const cloneRawRequest = async (req) => { | |
| if (!req.raw.bodyUsed) return req.raw.clone(); | |
| const cacheKey = Object.keys(req.bodyCache)[0]; | |
| if (!cacheKey) throw new require_http_exception.HTTPException(500, { message: "Cannot clone request: body was already consumed and not cached. Please use HonoRequest methods (e.g., req.json(), req.text()) instead of consuming req.raw directly." }); | |
| let body = await req[cacheKey](); | |
| const headers = req.header(); | |
| if (cacheKey === "json") { | |
| body = JSON.stringify(body); | |
| delete headers["content-length"]; | |
| } else if (body instanceof FormData) { | |
| delete headers["content-type"]; | |
| delete headers["content-length"]; | |
| } | |
| const requestInit = { | |
| body, | |
| cache: req.raw.cache, | |
| credentials: req.raw.credentials, | |
| headers, | |
| integrity: req.raw.integrity, | |
| keepalive: req.raw.keepalive, | |
| method: req.method, | |
| mode: req.raw.mode, | |
| redirect: req.raw.redirect, | |
| referrer: req.raw.referrer, | |
| referrerPolicy: req.raw.referrerPolicy, | |
| signal: req.raw.signal | |
| }; | |
| return new Request(req.url, requestInit); | |
| }; | |
| //#endregion | |
| exports.HonoRequest = HonoRequest; | |
| exports.cloneRawRequest = cloneRawRequest; | |