Z User
phase7: MCP stdio smoke test (handshake + tools/list + get_roots 6:74) — 4/4 tests PASS
54a6341 Download node_modules/hono/dist/validator/validator.js from bravesigma/quran-md-plugin: direct link, hf CLI and curl.
- Browser
- Download file 2.24 kB
-
https://huggingface.co/datasets/bravesigma/quran-md-plugin/resolve/main/node_modules/hono/dist/validator/validator.js
- Command line
-
hf download hf://datasets/bravesigma/quran-md-plugin/node_modules/hono/dist/validator/validator.js
-
curl -L -o validator.js https://huggingface.co/datasets/bravesigma/quran-md-plugin/resolve/main/node_modules/hono/dist/validator/validator.js
2.24 kB
| import { HTTPException } from "../http-exception.js"; | |
| import { bufferToFormData } from "../utils/buffer.js"; | |
| import { getCookie } from "../helper/cookie/index.js"; | |
| //#region src/validator/validator.ts | |
| const jsonRegex = /^application\/([\w.-]+\+)?json$/i; | |
| const getMediaType = (contentType) => contentType.split(";")[0].trim().toLowerCase(); | |
| const validator = (target, validationFunc) => { | |
| return async (c, next) => { | |
| let value = {}; | |
| const contentType = c.req.header("Content-Type"); | |
| const mediaType = contentType ? getMediaType(contentType) : ""; | |
| switch (target) { | |
| case "json": | |
| if (!jsonRegex.test(mediaType)) break; | |
| try { | |
| value = await c.req.json(); | |
| } catch { | |
| throw new HTTPException(400, { message: "Malformed JSON in request body" }); | |
| } | |
| break; | |
| case "form": { | |
| if (mediaType !== "multipart/form-data" && mediaType !== "application/x-www-form-urlencoded") break; | |
| let formData; | |
| if (c.req.bodyCache.formData) formData = await c.req.bodyCache.formData; | |
| else try { | |
| const arrayBuffer = await c.req.arrayBuffer(); | |
| formData = await bufferToFormData(arrayBuffer, contentType); | |
| c.req.bodyCache.formData = formData; | |
| } catch (e) { | |
| let message = "Malformed FormData request."; | |
| message += e instanceof Error ? ` ${e.message}` : ` ${String(e)}`; | |
| throw new HTTPException(400, { message }); | |
| } | |
| const form = Object.create(null); | |
| formData.forEach((value, key) => { | |
| if (key.endsWith("[]")) (form[key] ??= []).push(value); | |
| else if (Array.isArray(form[key])) form[key].push(value); | |
| else if (Object.hasOwn(form, key)) form[key] = [form[key], value]; | |
| else form[key] = value; | |
| }); | |
| value = form; | |
| break; | |
| } | |
| case "query": | |
| value = Object.fromEntries(Object.entries(c.req.queries()).map(([k, v]) => { | |
| return v.length === 1 ? [k, v[0]] : [k, v]; | |
| })); | |
| break; | |
| case "param": | |
| value = c.req.param(); | |
| break; | |
| case "header": | |
| value = c.req.header(); | |
| break; | |
| case "cookie": value = getCookie(c); | |
| } | |
| const res = await validationFunc(value, c); | |
| if (res instanceof Response) return res; | |
| c.req.addValidatedData(target, res); | |
| return await next(); | |
| }; | |
| }; | |
| //#endregion | |
| export { validator }; | |