strong-tie/inbound-calls
0
1/*! formdata-polyfill. MIT License. Jimmy Wärting <https://jimmy.warting.se/opensource> */2 3const escape = (str, filename) =>4 (filename ? str : str.replace(/\r?\n|\r/g, '\r\n'))5 .replace(/\n/g, '%0A')6 .replace(/\r/g, '%0D')7 .replace(/"/g, '%22')8 9/**10 * pure function to convert any formData instance to a Blob11 * instances synchronous without reading all of the files12 *13 * @param {FormData|*} formData an instance of a formData Class14 * @param {Blob|*} [BlobClass=Blob] the Blob class to use when constructing it15 */16export function formDataToBlob (formData, BlobClass = Blob) {17 const boundary = ('----formdata-polyfill-' + Math.random())18 const chunks = []19 const prefix = `--${boundary}\r\nContent-Disposition: form-data; name="`20 21 for (let [name, value] of formData) {22 if (typeof value === 'string') {23 chunks.push(prefix + escape(name) + `"\r\n\r\n${value.replace(/\r(?!\n)|(?<!\r)\n/g, '\r\n')}\r\n`)24 } else {25 chunks.push(26 prefix + escape(name) + `"; filename="${escape(value.name, 1)}"\r\n` +27 `Content-Type: ${value.type || 'application/octet-stream'}\r\n\r\n`,28 value,29 '\r\n'30 )31 }32 }33 34 chunks.push(`--${boundary}--`)35 36 return new BlobClass(chunks, {37 type: 'multipart/form-data; boundary=' + boundary38 })39}40 