-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformData.js
68 lines (54 loc) · 1.82 KB
/
formData.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/**
* @remix-run/server-runtime v1.5.1
*
* 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');
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/api/remix#parsemultipartformdata-node
*/
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;