Z User
phase7: MCP stdio smoke test (handshake + tools/list + get_roots 6:74) — 4/4 tests PASS
54a6341 Download node_modules/hono/dist/utils/body.js from bravesigma/quran-md-plugin: direct link, hf CLI and curl.
- Browser
- Download file 3.83 kB
-
https://huggingface.co/datasets/bravesigma/quran-md-plugin/resolve/main/node_modules/hono/dist/utils/body.js
- Command line
-
hf download hf://datasets/bravesigma/quran-md-plugin/node_modules/hono/dist/utils/body.js
-
curl -L -o body.js https://huggingface.co/datasets/bravesigma/quran-md-plugin/resolve/main/node_modules/hono/dist/utils/body.js
3.83 kB
| import { bufferToFormData } from "./buffer.js"; | |
| //#region src/utils/body.ts | |
| const MAX_NESTED_OBJECTS = 1e4; | |
| const isRawRequest = (request) => "headers" in request; | |
| const parseBody = async (request, options = Object.create(null)) => { | |
| const { all = false, dot = false } = options; | |
| const mediaType = (isRawRequest(request) ? request.headers : request.raw.headers).get("Content-Type")?.split(";")[0].trim().toLowerCase(); | |
| if (mediaType === "multipart/form-data" || mediaType === "application/x-www-form-urlencoded") return parseFormData(request, { | |
| all, | |
| dot | |
| }); | |
| return {}; | |
| }; | |
| /** | |
| * Parses form data from a request. | |
| * | |
| * @template T - The type of the parsed body data. | |
| * @param {HonoRequest | Request} request - The request object containing form data. | |
| * @param {ParseBodyOptions} options - Options for parsing the form data. | |
| * @returns {Promise<T>} The parsed body data. | |
| */ | |
| async function parseFormData(request, options) { | |
| if (!isRawRequest(request) && request.bodyCache.formData) return convertFormDataToBodyData(await request.bodyCache.formData, options); | |
| const headers = isRawRequest(request) ? request.headers : request.raw.headers; | |
| const arrayBuffer = await request.arrayBuffer(); | |
| const formDataPromise = bufferToFormData(arrayBuffer, headers.get("Content-Type") || ""); | |
| if (!isRawRequest(request)) request.bodyCache.formData = formDataPromise; | |
| const formData = await formDataPromise; | |
| if (formData) return convertFormDataToBodyData(formData, options); | |
| return {}; | |
| } | |
| /** | |
| * Converts form data to body data based on the provided options. | |
| * | |
| * @template T - The type of the parsed body data. | |
| * @param {FormData} formData - The form data to convert. | |
| * @param {ParseBodyOptions} options - Options for parsing the form data. | |
| * @returns {T} The converted body data. | |
| */ | |
| function convertFormDataToBodyData(formData, options) { | |
| const form = Object.create(null); | |
| const nestingState = { count: 0 }; | |
| formData.forEach((value, key) => { | |
| if (!(options.all || key.endsWith("[]"))) form[key] = value; | |
| else handleParsingAllValues(form, key, value); | |
| }); | |
| if (options.dot) Object.entries(form).forEach(([key, value]) => { | |
| if (key.includes(".")) { | |
| handleParsingNestedValues(form, key, value, nestingState); | |
| delete form[key]; | |
| } | |
| }); | |
| return form; | |
| } | |
| /** | |
| * Handles parsing all values for a given key, supporting multiple values as arrays. | |
| * | |
| * @param {BodyData} form - The form data object. | |
| * @param {string} key - The key to parse. | |
| * @param {FormDataEntryValue} value - The value to assign. | |
| */ | |
| const handleParsingAllValues = (form, key, value) => { | |
| if (form[key] !== void 0) { | |
| if (Array.isArray(form[key])) form[key].push(value); | |
| else form[key] = [form[key], value]; | |
| } else if (!key.endsWith("[]")) form[key] = value; | |
| else form[key] = [value]; | |
| }; | |
| /** | |
| * Handles parsing nested values using dot notation keys. | |
| * | |
| * @param {BodyData} form - The form data object. | |
| * @param {string} key - The dot notation key. | |
| * @param {BodyDataValue} value - The value to assign. | |
| * @param {{ count: number }} state - The state object tracking nesting depth. | |
| */ | |
| const handleParsingNestedValues = (form, key, value, state) => { | |
| if (/(?:^|\.)(?:__proto__|constructor\.prototype)\./.test(key)) return; | |
| let nestedForm = form; | |
| const keys = key.split(".", 34); | |
| if (keys.length > 33) throwNestingLimitExceeded(); | |
| keys.forEach((key, index) => { | |
| if (index === keys.length - 1) nestedForm[key] = value; | |
| else { | |
| if (!nestedForm[key] || typeof nestedForm[key] !== "object" || Array.isArray(nestedForm[key]) || nestedForm[key] instanceof File) { | |
| if (state.count++ >= MAX_NESTED_OBJECTS) throwNestingLimitExceeded(); | |
| nestedForm[key] = Object.create(null); | |
| } | |
| nestedForm = nestedForm[key]; | |
| } | |
| }); | |
| }; | |
| const throwNestingLimitExceeded = () => { | |
| throw new Error("Nesting limit exceeded"); | |
| }; | |
| //#endregion | |
| export { parseBody }; | |