|
2 | 2 |
|
3 | 3 | 'use strict' |
4 | 4 |
|
5 | | -require('formdata-polyfill') |
6 | | - |
7 | 5 | var debug = require('debug')('httpsnippet') |
| 6 | +var es = require('event-stream') |
| 7 | +var MultiPartForm = require('form-data') |
8 | 8 | var qs = require('querystring') |
9 | 9 | var reducer = require('./helpers/reducer') |
10 | 10 | var targets = require('./targets') |
@@ -106,21 +106,49 @@ HTTPSnippet.prototype.prepare = function (request) { |
106 | 106 | request.postData.mimeType = 'multipart/form-data' |
107 | 107 |
|
108 | 108 | if (request.postData.params) { |
109 | | - var form = new FormData() |
| 109 | + var form = new MultiPartForm() |
| 110 | + |
| 111 | + // The `form-data` module returns one of two things: a native FormData object, or its own polyfill. Since this |
| 112 | + // polyfill support the full API of the native FormData object, when this library is running in a browser |
| 113 | + // environment it'll fail on two things: |
| 114 | + // |
| 115 | + // - The API for `form.append()` has three arguments and the third should only be present when the second is a |
| 116 | + // Blob or USVString. |
| 117 | + // - `FormData.pipe()` isn't a function. |
| 118 | + // |
| 119 | + // Since the native FormData object is iterable, we easily detect what version of `form-data` we're working |
| 120 | + // with here to allow `multipart/form-data` requests to be compiled under both browser and Node environments. |
| 121 | + // This hack sucks yeah, but it's the only way we can use this library in the browser as if we code this against |
| 122 | + // just the native FormData object, we can't polyfill it back into Node because Blob and File objects, which |
| 123 | + // something like `formdata-polyfill` requires, don't exist there. |
| 124 | + const isNativeFormData = (typeof form[Symbol.iterator] === 'function'); |
110 | 125 |
|
111 | 126 | // easter egg |
112 | 127 | const boundary = '---011000010111000001101001' |
113 | 128 |
|
114 | 129 | request.postData.params.forEach(function (param) { |
115 | | - if (isBlob(param.value)) { |
116 | | - form.append(param.name, param.value || '', param.fileName || null) |
| 130 | + if (isNativeFormData) { |
| 131 | + if (isBlob(param.value)) { |
| 132 | + form.append(param.name, param.value || '', param.fileName || null) |
| 133 | + } else { |
| 134 | + form.append(param.name, param.value || '') |
| 135 | + } |
117 | 136 | } else { |
118 | | - form.append(param.name, param.value || '') |
| 137 | + form.append(param.name, param.value || '', { |
| 138 | + filename: param.fileName || null, |
| 139 | + contentType: param.contentType || null |
| 140 | + }) |
119 | 141 | } |
120 | 142 | }) |
121 | 143 |
|
122 | | - for (var data of formDataIterator(form, boundary)) { |
123 | | - request.postData.text += data |
| 144 | + if (isNativeFormData) { |
| 145 | + for (var data of formDataIterator(form, boundary)) { |
| 146 | + request.postData.text += data |
| 147 | + } |
| 148 | + } else { |
| 149 | + form.pipe(es.map(function (data, cb) { |
| 150 | + request.postData.text += data |
| 151 | + })) |
124 | 152 | } |
125 | 153 |
|
126 | 154 | request.postData.boundary = boundary |
|
0 commit comments