import { ReadableStream } from 'stream/web'; import * as PropertySymbol from '../../PropertySymbol.js'; import MultipartReader from './MultipartReader.js'; import DOMExceptionNameEnum from '../../exception/DOMExceptionNameEnum.js'; import { Buffer } from 'buffer'; /** * Multipart form data factory. * * Based on: * https://github.com/node-fetch/node-fetch/blob/main/src/utils/multipart-parser.js (MIT) */ export default class MultipartFormDataParser { /** * Returns form data. * * @param window Window. * @param body Body. * @param contentType Content type header value. * @returns Form data. */ static async streamToFormData(window, body, contentType) { if (!/multipart/i.test(contentType)) { throw new window.DOMException(`Failed to build FormData object: The "content-type" header isn't of type "multipart/form-data".`, DOMExceptionNameEnum.invalidStateError); } const match = contentType.match(/boundary=(?:"([^"]+)"|([^;]+))/i); if (!match) { throw new window.DOMException(`Failed to build FormData object: The "content-type" header doesn't contain any multipart boundary.`, DOMExceptionNameEnum.invalidStateError); } const bodyReader = body.getReader(); const reader = new MultipartReader(window, match[1] || match[2]); const chunks = []; let buffer; const bytes = 0; let readResult = await bodyReader.read(); while (!readResult.done) { if (body[PropertySymbol.error]) { throw body[PropertySymbol.error]; } if (body[PropertySymbol.aborted]) { throw new window.DOMException('Failed to read response body: The stream was aborted.', DOMExceptionNameEnum.abortError); } reader.write(readResult.value); readResult = await bodyReader.read(); } try { buffer = typeof chunks[0] === 'string' ? Buffer.from(chunks.join('')) : Buffer.concat(chunks, bytes); } catch (error) { throw new window.DOMException(`Could not create Buffer from response body. Error: ${error.message}.`, DOMExceptionNameEnum.invalidStateError); } return { formData: reader.end(), buffer }; } /** * Converts a FormData object to a ReadableStream. * * @param formData FormData. * @returns Stream and type. */ static formDataToStream(formData) { const boundary = '----HappyDOMFormDataBoundary' + Math.random().toString(36); const chunks = []; const prefix = `--${boundary}\r\nContent-Disposition: form-data; name="`; for (const [name, value] of formData) { if (typeof value === 'string') { chunks.push(Buffer.from(`${prefix}${this.escapeName(name)}"\r\n\r\n${value.replace(/\r(?!\n)|(?