form_data.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. 'use strict';
  2. var CombinedStream = require('combined-stream');
  3. var util = require('util');
  4. var path = require('path');
  5. var http = require('http');
  6. var https = require('https');
  7. var parseUrl = require('url').parse;
  8. var fs = require('fs');
  9. var Stream = require('stream').Stream;
  10. var crypto = require('crypto');
  11. var mime = require('mime-types');
  12. var asynckit = require('asynckit');
  13. var setToStringTag = require('es-set-tostringtag');
  14. var hasOwn = require('hasown');
  15. var populate = require('./populate.js');
  16. /**
  17. * Create readable "multipart/form-data" streams.
  18. * Can be used to submit forms
  19. * and file uploads to other web applications.
  20. *
  21. * @constructor
  22. * @param {object} options - Properties to be added/overriden for FormData and CombinedStream
  23. */
  24. function FormData(options) {
  25. if (!(this instanceof FormData)) {
  26. return new FormData(options);
  27. }
  28. this._overheadLength = 0;
  29. this._valueLength = 0;
  30. this._valuesToMeasure = [];
  31. CombinedStream.call(this);
  32. options = options || {}; // eslint-disable-line no-param-reassign
  33. for (var option in options) { // eslint-disable-line no-restricted-syntax
  34. this[option] = options[option];
  35. }
  36. }
  37. // make it a Stream
  38. util.inherits(FormData, CombinedStream);
  39. FormData.LINE_BREAK = '\r\n';
  40. FormData.DEFAULT_CONTENT_TYPE = 'application/octet-stream';
  41. FormData.prototype.append = function (field, value, options) {
  42. options = options || {}; // eslint-disable-line no-param-reassign
  43. // allow filename as single option
  44. if (typeof options === 'string') {
  45. options = { filename: options }; // eslint-disable-line no-param-reassign
  46. }
  47. var append = CombinedStream.prototype.append.bind(this);
  48. // all that streamy business can't handle numbers
  49. if (typeof value === 'number' || value == null) {
  50. value = String(value); // eslint-disable-line no-param-reassign
  51. }
  52. // https://github.com/felixge/node-form-data/issues/38
  53. if (Array.isArray(value)) {
  54. /*
  55. * Please convert your array into string
  56. * the way web server expects it
  57. */
  58. this._error(new Error('Arrays are not supported.'));
  59. return;
  60. }
  61. var header = this._multiPartHeader(field, value, options);
  62. var footer = this._multiPartFooter();
  63. append(header);
  64. append(value);
  65. append(footer);
  66. // pass along options.knownLength
  67. this._trackLength(header, value, options);
  68. };
  69. FormData.prototype._trackLength = function (header, value, options) {
  70. var valueLength = 0;
  71. /*
  72. * used w/ getLengthSync(), when length is known.
  73. * e.g. for streaming directly from a remote server,
  74. * w/ a known file a size, and not wanting to wait for
  75. * incoming file to finish to get its size.
  76. */
  77. if (options.knownLength != null) {
  78. valueLength += Number(options.knownLength);
  79. } else if (Buffer.isBuffer(value)) {
  80. valueLength = value.length;
  81. } else if (typeof value === 'string') {
  82. valueLength = Buffer.byteLength(value);
  83. }
  84. this._valueLength += valueLength;
  85. // @check why add CRLF? does this account for custom/multiple CRLFs?
  86. this._overheadLength += Buffer.byteLength(header) + FormData.LINE_BREAK.length;
  87. // empty or either doesn't have path or not an http response or not a stream
  88. if (!value || (!value.path && !(value.readable && hasOwn(value, 'httpVersion')) && !(value instanceof Stream))) {
  89. return;
  90. }
  91. // no need to bother with the length
  92. if (!options.knownLength) {
  93. this._valuesToMeasure.push(value);
  94. }
  95. };
  96. FormData.prototype._lengthRetriever = function (value, callback) {
  97. if (hasOwn(value, 'fd')) {
  98. // take read range into a account
  99. // `end` = Infinity –> read file till the end
  100. //
  101. // TODO: Looks like there is bug in Node fs.createReadStream
  102. // it doesn't respect `end` options without `start` options
  103. // Fix it when node fixes it.
  104. // https://github.com/joyent/node/issues/7819
  105. if (value.end != undefined && value.end != Infinity && value.start != undefined) {
  106. // when end specified
  107. // no need to calculate range
  108. // inclusive, starts with 0
  109. callback(null, value.end + 1 - (value.start ? value.start : 0)); // eslint-disable-line callback-return
  110. // not that fast snoopy
  111. } else {
  112. // still need to fetch file size from fs
  113. fs.stat(value.path, function (err, stat) {
  114. if (err) {
  115. callback(err);
  116. return;
  117. }
  118. // update final size based on the range options
  119. var fileSize = stat.size - (value.start ? value.start : 0);
  120. callback(null, fileSize);
  121. });
  122. }
  123. // or http response
  124. } else if (hasOwn(value, 'httpVersion')) {
  125. callback(null, Number(value.headers['content-length'])); // eslint-disable-line callback-return
  126. // or request stream http://github.com/mikeal/request
  127. } else if (hasOwn(value, 'httpModule')) {
  128. // wait till response come back
  129. value.on('response', function (response) {
  130. value.pause();
  131. callback(null, Number(response.headers['content-length']));
  132. });
  133. value.resume();
  134. // something else
  135. } else {
  136. callback('Unknown stream'); // eslint-disable-line callback-return
  137. }
  138. };
  139. FormData.prototype._multiPartHeader = function (field, value, options) {
  140. /*
  141. * custom header specified (as string)?
  142. * it becomes responsible for boundary
  143. * (e.g. to handle extra CRLFs on .NET servers)
  144. */
  145. if (typeof options.header === 'string') {
  146. return options.header;
  147. }
  148. var contentDisposition = this._getContentDisposition(value, options);
  149. var contentType = this._getContentType(value, options);
  150. var contents = '';
  151. var headers = {
  152. // add custom disposition as third element or keep it two elements if not
  153. 'Content-Disposition': ['form-data', 'name="' + field + '"'].concat(contentDisposition || []),
  154. // if no content type. allow it to be empty array
  155. 'Content-Type': [].concat(contentType || [])
  156. };
  157. // allow custom headers.
  158. if (typeof options.header === 'object') {
  159. populate(headers, options.header);
  160. }
  161. var header;
  162. for (var prop in headers) { // eslint-disable-line no-restricted-syntax
  163. if (hasOwn(headers, prop)) {
  164. header = headers[prop];
  165. // skip nullish headers.
  166. if (header == null) {
  167. continue; // eslint-disable-line no-restricted-syntax, no-continue
  168. }
  169. // convert all headers to arrays.
  170. if (!Array.isArray(header)) {
  171. header = [header];
  172. }
  173. // add non-empty headers.
  174. if (header.length) {
  175. contents += prop + ': ' + header.join('; ') + FormData.LINE_BREAK;
  176. }
  177. }
  178. }
  179. return '--' + this.getBoundary() + FormData.LINE_BREAK + contents + FormData.LINE_BREAK;
  180. };
  181. FormData.prototype._getContentDisposition = function (value, options) { // eslint-disable-line consistent-return
  182. var filename;
  183. if (typeof options.filepath === 'string') {
  184. // custom filepath for relative paths
  185. filename = path.normalize(options.filepath).replace(/\\/g, '/');
  186. } else if (options.filename || (value && (value.name || value.path))) {
  187. /*
  188. * custom filename take precedence
  189. * formidable and the browser add a name property
  190. * fs- and request- streams have path property
  191. */
  192. filename = path.basename(options.filename || (value && (value.name || value.path)));
  193. } else if (value && value.readable && hasOwn(value, 'httpVersion')) {
  194. // or try http response
  195. filename = path.basename(value.client._httpMessage.path || '');
  196. }
  197. if (filename) {
  198. return 'filename="' + filename + '"';
  199. }
  200. };
  201. FormData.prototype._getContentType = function (value, options) {
  202. // use custom content-type above all
  203. var contentType = options.contentType;
  204. // or try `name` from formidable, browser
  205. if (!contentType && value && value.name) {
  206. contentType = mime.lookup(value.name);
  207. }
  208. // or try `path` from fs-, request- streams
  209. if (!contentType && value && value.path) {
  210. contentType = mime.lookup(value.path);
  211. }
  212. // or if it's http-reponse
  213. if (!contentType && value && value.readable && hasOwn(value, 'httpVersion')) {
  214. contentType = value.headers['content-type'];
  215. }
  216. // or guess it from the filepath or filename
  217. if (!contentType && (options.filepath || options.filename)) {
  218. contentType = mime.lookup(options.filepath || options.filename);
  219. }
  220. // fallback to the default content type if `value` is not simple value
  221. if (!contentType && value && typeof value === 'object') {
  222. contentType = FormData.DEFAULT_CONTENT_TYPE;
  223. }
  224. return contentType;
  225. };
  226. FormData.prototype._multiPartFooter = function () {
  227. return function (next) {
  228. var footer = FormData.LINE_BREAK;
  229. var lastPart = this._streams.length === 0;
  230. if (lastPart) {
  231. footer += this._lastBoundary();
  232. }
  233. next(footer);
  234. }.bind(this);
  235. };
  236. FormData.prototype._lastBoundary = function () {
  237. return '--' + this.getBoundary() + '--' + FormData.LINE_BREAK;
  238. };
  239. FormData.prototype.getHeaders = function (userHeaders) {
  240. var header;
  241. var formHeaders = {
  242. 'content-type': 'multipart/form-data; boundary=' + this.getBoundary()
  243. };
  244. for (header in userHeaders) { // eslint-disable-line no-restricted-syntax
  245. if (hasOwn(userHeaders, header)) {
  246. formHeaders[header.toLowerCase()] = userHeaders[header];
  247. }
  248. }
  249. return formHeaders;
  250. };
  251. FormData.prototype.setBoundary = function (boundary) {
  252. if (typeof boundary !== 'string') {
  253. throw new TypeError('FormData boundary must be a string');
  254. }
  255. this._boundary = boundary;
  256. };
  257. FormData.prototype.getBoundary = function () {
  258. if (!this._boundary) {
  259. this._generateBoundary();
  260. }
  261. return this._boundary;
  262. };
  263. FormData.prototype.getBuffer = function () {
  264. var dataBuffer = new Buffer.alloc(0); // eslint-disable-line new-cap
  265. var boundary = this.getBoundary();
  266. // Create the form content. Add Line breaks to the end of data.
  267. for (var i = 0, len = this._streams.length; i < len; i++) {
  268. if (typeof this._streams[i] !== 'function') {
  269. // Add content to the buffer.
  270. if (Buffer.isBuffer(this._streams[i])) {
  271. dataBuffer = Buffer.concat([dataBuffer, this._streams[i]]);
  272. } else {
  273. dataBuffer = Buffer.concat([dataBuffer, Buffer.from(this._streams[i])]);
  274. }
  275. // Add break after content.
  276. if (typeof this._streams[i] !== 'string' || this._streams[i].substring(2, boundary.length + 2) !== boundary) {
  277. dataBuffer = Buffer.concat([dataBuffer, Buffer.from(FormData.LINE_BREAK)]);
  278. }
  279. }
  280. }
  281. // Add the footer and return the Buffer object.
  282. return Buffer.concat([dataBuffer, Buffer.from(this._lastBoundary())]);
  283. };
  284. FormData.prototype._generateBoundary = function () {
  285. // This generates a 50 character boundary similar to those used by Firefox.
  286. // They are optimized for boyer-moore parsing.
  287. this._boundary = '--------------------------' + crypto.randomBytes(12).toString('hex');
  288. };
  289. // Note: getLengthSync DOESN'T calculate streams length
  290. // As workaround one can calculate file size manually and add it as knownLength option
  291. FormData.prototype.getLengthSync = function () {
  292. var knownLength = this._overheadLength + this._valueLength;
  293. // Don't get confused, there are 3 "internal" streams for each keyval pair so it basically checks if there is any value added to the form
  294. if (this._streams.length) {
  295. knownLength += this._lastBoundary().length;
  296. }
  297. // https://github.com/form-data/form-data/issues/40
  298. if (!this.hasKnownLength()) {
  299. /*
  300. * Some async length retrievers are present
  301. * therefore synchronous length calculation is false.
  302. * Please use getLength(callback) to get proper length
  303. */
  304. this._error(new Error('Cannot calculate proper length in synchronous way.'));
  305. }
  306. return knownLength;
  307. };
  308. // Public API to check if length of added values is known
  309. // https://github.com/form-data/form-data/issues/196
  310. // https://github.com/form-data/form-data/issues/262
  311. FormData.prototype.hasKnownLength = function () {
  312. var hasKnownLength = true;
  313. if (this._valuesToMeasure.length) {
  314. hasKnownLength = false;
  315. }
  316. return hasKnownLength;
  317. };
  318. FormData.prototype.getLength = function (cb) {
  319. var knownLength = this._overheadLength + this._valueLength;
  320. if (this._streams.length) {
  321. knownLength += this._lastBoundary().length;
  322. }
  323. if (!this._valuesToMeasure.length) {
  324. process.nextTick(cb.bind(this, null, knownLength));
  325. return;
  326. }
  327. asynckit.parallel(this._valuesToMeasure, this._lengthRetriever, function (err, values) {
  328. if (err) {
  329. cb(err);
  330. return;
  331. }
  332. values.forEach(function (length) {
  333. knownLength += length;
  334. });
  335. cb(null, knownLength);
  336. });
  337. };
  338. FormData.prototype.submit = function (params, cb) {
  339. var request;
  340. var options;
  341. var defaults = { method: 'post' };
  342. // parse provided url if it's string or treat it as options object
  343. if (typeof params === 'string') {
  344. params = parseUrl(params); // eslint-disable-line no-param-reassign
  345. /* eslint sort-keys: 0 */
  346. options = populate({
  347. port: params.port,
  348. path: params.pathname,
  349. host: params.hostname,
  350. protocol: params.protocol
  351. }, defaults);
  352. } else { // use custom params
  353. options = populate(params, defaults);
  354. // if no port provided use default one
  355. if (!options.port) {
  356. options.port = options.protocol === 'https:' ? 443 : 80;
  357. }
  358. }
  359. // put that good code in getHeaders to some use
  360. options.headers = this.getHeaders(params.headers);
  361. // https if specified, fallback to http in any other case
  362. if (options.protocol === 'https:') {
  363. request = https.request(options);
  364. } else {
  365. request = http.request(options);
  366. }
  367. // get content length and fire away
  368. this.getLength(function (err, length) {
  369. if (err && err !== 'Unknown stream') {
  370. this._error(err);
  371. return;
  372. }
  373. // add content length
  374. if (length) {
  375. request.setHeader('Content-Length', length);
  376. }
  377. this.pipe(request);
  378. if (cb) {
  379. var onResponse;
  380. var callback = function (error, responce) {
  381. request.removeListener('error', callback);
  382. request.removeListener('response', onResponse);
  383. return cb.call(this, error, responce); // eslint-disable-line no-invalid-this
  384. };
  385. onResponse = callback.bind(this, null);
  386. request.on('error', callback);
  387. request.on('response', onResponse);
  388. }
  389. }.bind(this));
  390. return request;
  391. };
  392. FormData.prototype._error = function (err) {
  393. if (!this.error) {
  394. this.error = err;
  395. this.pause();
  396. this.emit('error', err);
  397. }
  398. };
  399. FormData.prototype.toString = function () {
  400. return '[object FormData]';
  401. };
  402. setToStringTag(FormData, 'FormData');
  403. // Public API
  404. module.exports = FormData;