UNPKG

tezx

Version:

TezX is a modern, ultra-lightweight, and high-performance JavaScript framework built specifically for Bun. It provides a minimal yet powerful API, seamless environment management, and a high-concurrency HTTP engine for building fast, scalable web applicat

60 lines (59 loc) 2.45 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useFormData = useFormData; exports.processFile = processFile; const utils_js_1 = require("../utils/utils.js"); async function useFormData(ctx, options) { const fd = await ctx.req.formData(); const result = {}; let totalFileBytes = 0; for (const [key, originalVal] of fd.entries()) { let val = originalVal; if (val instanceof File && options) { val = await processFile(val, key, options); totalFileBytes += val.size; if (typeof options.maxTotalSize === "number" && totalFileBytes > options.maxTotalSize) { throw new Error(`Total file bytes exceeded maxTotalSize=${options.maxTotalSize}`); } } else if (typeof val === "string" && options?.maxFieldSize && val.length > options.maxFieldSize) { throw new Error(`Field "${key}" length ${val.length} exceeds maxFieldSize=${options.maxFieldSize}`); } if (key in result) { if (!Array.isArray(result[key])) { result[key] = [result[key]]; } result[key].push(val); } else { result[key] = val; } if (val instanceof File && typeof options?.maxFiles === "number") { const filesForKey = Array.isArray(result[key]) ? result[key].filter((v) => v instanceof File).length : result[key] instanceof File ? 1 : 0; if (filesForKey > options.maxFiles) { throw new Error(`Field "${key}" exceeds maxFiles (${options.maxFiles})`); } } } return result; } async function processFile(file, key, options) { let name = file.name; if (options.sanitized) { name = `${Date.now()}-${(0, utils_js_1.sanitized)(name)}`; } if (Array.isArray(options.allowedTypes) && !options.allowedTypes.includes(file.type)) { throw new Error(`Field "${key}": invalid file type "${file.type}". Allowed: ${options.allowedTypes.join(", ")}`); } if (typeof options.maxSize === "number" && file.size > options.maxSize) { throw new Error(`Field "${key}": file size ${file.size} > maxSize ${options.maxSize}`); } if (name !== file.name) { return new File([await file.arrayBuffer()], name, { type: file.type }); } return file; }