forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultipart.js
More file actions
55 lines (55 loc) · 1.7 KB
/
Copy pathmultipart.js
File metadata and controls
55 lines (55 loc) · 1.7 KB
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
"use strict";
/**
* Utility function for constructing a multipart form in Node.js.
*
* @packageDocumentation
* @internal
* @hidden
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.toForm = void 0;
const stream_1 = require("stream");
const Multipart = require("multi-part");
/**
* @internal
* @hidden
*/
function toForm(fields) {
return new Promise((resolve, reject) => {
try {
const form = new Multipart();
for (const key of Object.keys(fields)) {
let value = fields[key];
if (value === undefined)
continue;
if (!(value instanceof stream_1.Readable) &&
!(value instanceof global.Buffer) &&
(typeof value === "object" || typeof value === "function")) {
value = JSON.stringify(value);
}
form.append(key, value);
}
const stream = form.stream();
const bufs = [];
stream.on("data", (buf) => bufs.push(buf));
stream.on("end", () => {
bufs.push(Buffer.from("\r\n"));
const body = Buffer.concat(bufs);
const boundary = form.getBoundary();
const headers = {
"content-type": `multipart/form-data; boundary=${boundary}`,
"content-length": String(body.length),
};
resolve({ body, headers });
});
stream.on("error", (e) => {
reject(e);
});
}
catch (e) {
reject(e);
}
});
}
exports.toForm = toForm;
//# sourceMappingURL=multipart.js.map