UNPKG

msw

Version:

Seamless REST/GraphQL API mocking library for browser and Node.js.

81 lines 3.14 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var parseMultipartData_exports = {}; __export(parseMultipartData_exports, { parseMultipartData: () => parseMultipartData }); module.exports = __toCommonJS(parseMultipartData_exports); var import_headers_polyfill = require("headers-polyfill"); function parseContentHeaders(headersString) { const headers = (0, import_headers_polyfill.stringToHeaders)(headersString); const contentType = headers.get("content-type") || "text/plain"; const disposition = headers.get("content-disposition"); if (!disposition) { throw new Error('"Content-Disposition" header is required.'); } const directives = disposition.split(";").reduce((acc, chunk) => { const [name2, ...rest] = chunk.trim().split("="); acc[name2] = rest.join("="); return acc; }, {}); const name = directives.name?.slice(1, -1); const filename = directives.filename?.slice(1, -1); return { name, filename, contentType }; } function parseMultipartData(data, headers) { const contentType = headers?.get("content-type"); if (!contentType) { return void 0; } const [, ...directives] = contentType.split(/; */); const boundary = directives.filter((d) => d.startsWith("boundary=")).map((s) => s.replace(/^boundary=/, ""))[0]; if (!boundary) { return void 0; } const boundaryRegExp = new RegExp(`--+${boundary}`); const fields = data.split(boundaryRegExp).filter((chunk) => chunk.startsWith("\r\n") && chunk.endsWith("\r\n")).map((chunk) => chunk.trimStart().replace(/\r\n$/, "")); if (!fields.length) { return void 0; } const parsedBody = {}; try { for (const field of fields) { const [contentHeaders, ...rest] = field.split("\r\n\r\n"); const contentBody = rest.join("\r\n\r\n"); const { contentType: contentType2, filename, name } = parseContentHeaders(contentHeaders); const value = filename === void 0 ? contentBody : new File([contentBody], filename, { type: contentType2 }); const parsedValue = parsedBody[name]; if (parsedValue === void 0) { parsedBody[name] = value; } else if (Array.isArray(parsedValue)) { parsedBody[name] = [...parsedValue, value]; } else { parsedBody[name] = [parsedValue, value]; } } return parsedBody; } catch { return void 0; } } //# sourceMappingURL=parseMultipartData.js.map