"use strict"; const conversions = require("webidl-conversions"); // https://fetch.spec.whatwg.org/#concept-bodyinit-extract (note: always UTF-8) // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#multipart%2Fform-data-encoding-algorithm // https://andreubotella.github.io/multipart-form-data/ const utf8Encoder = new TextEncoder(); const contentDispositionPrefix = utf8Encoder.encode(`Content-Disposition: form-data; name="`); const filenamePrefix = utf8Encoder.encode(`; filename="`); const contentType = utf8Encoder.encode(`Content-Type: `); // https://andreubotella.github.io/multipart-form-data/#multipart-form-data-boundary function generateBoundary() { let boundary = "--------------------------"; for (let i = 0; i < 24; ++i) { boundary += Math.floor(Math.random() * 10).toString(16); } return utf8Encoder.encode(boundary); } // https://andreubotella.github.io/multipart-form-data/#escape-a-multipart-form-data-name function escapeName(name, isFilename = false) { if (isFilename) { name = conversions.USVString(name); } else { name = name.replace(/\r(?!\n)|(? { const boundary = generateBoundary(); const outputChunks = []; for (const entry of entries) { const chunkBytes = [ 45, 45, // `--` ...boundary, 0x0D, 0x0A ]; chunkBytes.push(...contentDispositionPrefix, ...escapeName(entry.name), 0x22 /* `"` */); let { value } = entry; if (typeof value === "string") { chunkBytes.push(0x0D, 0x0A, 0x0D, 0x0A); value = value.replace(/\r(?!\n)|(? { return Buffer.concat(chunks); };