|
1 | | -import { map as eventStreamMap } from 'event-stream'; |
2 | 1 | import FormData from 'form-data'; |
3 | 2 | import { Param, PostDataCommon, Request as NpmHarRequest } from 'har-format'; |
4 | 3 | import { stringify as queryStringify } from 'querystring'; |
@@ -176,61 +175,26 @@ export class HTTPSnippet { |
176 | 175 | if (request.postData?.params) { |
177 | 176 | const form = new FormData(); |
178 | 177 |
|
179 | | - // The `form-data` module returns one of two things: a native FormData object, or its own polyfill |
180 | | - // Since the polyfill does not support the full API of the native FormData object, when this library is running in a browser environment it'll fail on two things: |
181 | | - // |
182 | | - // 1. The API for `form.append()` has three arguments and the third should only be present when the second is a |
183 | | - // Blob or USVString. |
184 | | - // 1. `FormData.pipe()` isn't a function. |
185 | | - // |
186 | | - // Since the native FormData object is iterable, we easily detect what version of `form-data` we're working with here to allow `multipart/form-data` requests to be compiled under both browser and Node environments. |
187 | | - // |
188 | | - // This hack is pretty awful but it's the only way we can use this library in the browser as if we code this against just the native FormData object, we can't polyfill that back into Node because Blob and File objects, which something like `formdata-polyfill` requires, don't exist there. |
189 | | - // @ts-expect-error TODO |
190 | | - const isNativeFormData = typeof form[Symbol.iterator] === 'function'; |
191 | | - |
192 | 178 | // TODO: THIS ABSOLUTELY MUST BE REMOVED. |
193 | 179 | // IT BREAKS SOME USE-CASES FOR MULTIPART FORMS THAT DEPEND ON BEING ABLE TO SET THE BOUNDARY. |
194 | 180 | // easter egg |
195 | 181 | const boundary = '---011000010111000001101001'; // this is binary for "api". yep. |
196 | | - if (!isNativeFormData) { |
197 | | - // @ts-expect-error THIS IS WRONG. VERY WRONG. |
198 | | - form._boundary = boundary; |
199 | | - } |
200 | 182 |
|
201 | 183 | request.postData?.params.forEach(param => { |
202 | 184 | const name = param.name; |
203 | 185 | const value = param.value || ''; |
204 | 186 | const filename = param.fileName || null; |
205 | 187 |
|
206 | | - if (isNativeFormData) { |
207 | | - if (isBlob(value)) { |
208 | | - // @ts-expect-error TODO |
209 | | - form.append(name, value, filename); |
210 | | - } else { |
211 | | - form.append(name, value); |
212 | | - } |
| 188 | + if (isBlob(value)) { |
| 189 | + // @ts-expect-error TODO |
| 190 | + form.append(name, value, filename); |
213 | 191 | } else { |
214 | | - form.append(name, value, { |
215 | | - // @ts-expect-error TODO |
216 | | - filename, |
217 | | - // @ts-expect-error TODO |
218 | | - contentType: param.contentType || null, |
219 | | - }); |
| 192 | + form.append(name, value); |
220 | 193 | } |
221 | 194 | }); |
222 | 195 |
|
223 | | - if (isNativeFormData) { |
224 | | - for (const data of formDataIterator(form, boundary)) { |
225 | | - request.postData.text += data; |
226 | | - } |
227 | | - } else { |
228 | | - form.pipe( |
229 | | - // @ts-expect-error TODO |
230 | | - eventStreamMap(data => { |
231 | | - request.postData.text += data; |
232 | | - }), |
233 | | - ); |
| 196 | + for (const data of formDataIterator(form, boundary)) { |
| 197 | + request.postData.text += data; |
234 | 198 | } |
235 | 199 |
|
236 | 200 | request.postData.boundary = boundary; |
|
0 commit comments