UNPKG

@remix-run/server-runtime

Version:
61 lines (55 loc) 1.86 kB
/** * @remix-run/server-runtime v2.16.8 * * Copyright (c) Remix Software Inc. * * This source code is licensed under the MIT license found in the * LICENSE.md file in the root directory of this source tree. * * @license MIT */ 'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var multipartParser = require('@web3-storage/multipart-parser'); // @ts-ignore function composeUploadHandlers(...handlers) { return async part => { for (let handler of handlers) { let value = await handler(part); if (typeof value !== "undefined" && value !== null) { return value; } } return undefined; }; } /** * Allows you to handle multipart forms (file uploads) for your app. * * TODO: Update this comment * @see https://remix.run/utils/parse-multipart-form-data */ async function parseMultipartFormData(request, uploadHandler) { let contentType = request.headers.get("Content-Type") || ""; let [type, boundary] = contentType.split(/\s*;\s*boundary=/); if (!request.body || !boundary || type !== "multipart/form-data") { throw new TypeError("Could not parse content as FormData."); } let formData = new FormData(); let parts = multipartParser.streamMultipart(request.body, boundary); for await (let part of parts) { if (part.done) break; if (typeof part.filename === "string") { // only pass basename as the multipart/form-data spec recommends // https://datatracker.ietf.org/doc/html/rfc7578#section-4.2 part.filename = part.filename.split(/[/\\]/).pop(); } let value = await uploadHandler(part); if (typeof value !== "undefined" && value !== null) { formData.append(part.name, value); } } return formData; } exports.composeUploadHandlers = composeUploadHandlers; exports.parseMultipartFormData = parseMultipartFormData;