From 39e5291ef9d26d50daaf5ab311eeb252b612c63f Mon Sep 17 00:00:00 2001 From: Kyle VanderBeek Date: Sat, 29 Sep 2018 18:57:50 -0700 Subject: [PATCH 01/36] Allow newer versions of combined-stream --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index a21f4b3..334862f 100644 --- a/package.json +++ b/package.json @@ -38,7 +38,7 @@ }, "dependencies": { "asynckit": "^0.4.0", - "combined-stream": "1.0.6", + "combined-stream": "^1.0.6", "mime-types": "^2.1.12" }, "devDependencies": { From b16916a568a0d06f3f8a16c31f9a8b89b7844094 Mon Sep 17 00:00:00 2001 From: Alex Indigo Date: Wed, 17 Oct 2018 00:24:13 -0700 Subject: [PATCH 02/36] Bumped version to 2.3.3 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 334862f..adacbae 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "author": "Felix Geisendörfer (http://debuggable.com/)", "name": "form-data", "description": "A library to create readable \"multipart/form-data\" streams. Can be used to submit forms and file uploads to other web applications.", - "version": "2.3.2", + "version": "2.3.3", "repository": { "type": "git", "url": "git://github.com/form-data/form-data.git" From e90a76ab3dcaa63a6f3045f8255bfbb9c25a3e4e Mon Sep 17 00:00:00 2001 From: Jeffrey Benistant Date: Wed, 20 Mar 2019 13:07:20 +0100 Subject: [PATCH 03/36] Updated: Certificate Added method: getBuffer() Added Test: to test GetBuffer() --- lib/form_data.js | 26 ++++++++++++++ test/fixture/cert.pem | 39 +++++++++++++++----- test/fixture/key.pem | 56 +++++++++++++++++++++++++---- test/integration/test-get-buffer.js | 54 ++++++++++++++++++++++++++++ 4 files changed, 159 insertions(+), 16 deletions(-) create mode 100644 test/integration/test-get-buffer.js diff --git a/lib/form_data.js b/lib/form_data.js index 3a1bb82..0cd774e 100644 --- a/lib/form_data.js +++ b/lib/form_data.js @@ -313,6 +313,32 @@ FormData.prototype.getBoundary = function() { return this._boundary; }; +FormData.prototype.getBuffer = function() { + var dataBuffer = new Buffer.alloc( 0 ); + var boundary = this.getBoundary(); + + // Create the form content. Add Line breaks to the end of data. + for (var i = 0, len = this._streams.length; i < len; i++) { + if (typeof this._streams[i] !== 'function') { + + // Add content to the buffer. + if(Buffer.isBuffer(this._streams[i])) { + dataBuffer = Buffer.concat( [dataBuffer, this._streams[i]]); + }else { + dataBuffer = Buffer.concat( [dataBuffer, Buffer.from(this._streams[i])]); + } + + // Add break after content. + if (typeof this._streams[i] !== 'string' || this._streams[i].substring( 2, boundary.length + 2 ) !== boundary) { + dataBuffer = Buffer.concat( [dataBuffer, Buffer.from(FormData.LINE_BREAK)] ); + } + } + } + + // Add the footer and return the Buffer object. + return Buffer.concat( [dataBuffer, Buffer.from(this._lastBoundary())] ); +}; + FormData.prototype._generateBoundary = function() { // This generates a 50 character boundary similar to those used by Firefox. // They are optimized for boyer-moore parsing. diff --git a/test/fixture/cert.pem b/test/fixture/cert.pem index dadbd8f..910ee2f 100644 --- a/test/fixture/cert.pem +++ b/test/fixture/cert.pem @@ -1,11 +1,32 @@ -----BEGIN CERTIFICATE----- -MIIBnDCCAUYCCQCgIldFYidw0zANBgkqhkiG9w0BAQUFADBVMQswCQYDVQQGEwJV -UzELMAkGA1UECBMCQ0ExEjAQBgNVBAcTCVBhbG8gQWx0bzERMA8GA1UEChMIRm9y -bURhdGExEjAQBgNVBAMTCWxvY2FsaG9zdDAeFw0xNTA2MTMxMzA3MTBaFw00MjEw -MjgxMzA3MTBaMFUxCzAJBgNVBAYTAlVTMQswCQYDVQQIEwJDQTESMBAGA1UEBxMJ -UGFsbyBBbHRvMREwDwYDVQQKEwhGb3JtRGF0YTESMBAGA1UEAxMJbG9jYWxob3N0 -MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAOkE8JJzLQbVEo/vRVRqiwNEbiuEooJA -099h1IJLtsPJVphSDBTtE0FLB0YIzT7bG44Drm/975XEZjoQ9TmYbTMCAwEAATAN -BgkqhkiG9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433 +hdxbvpzl0HjnruWvoXDKOWv08sYJhtMqclRQRGDSmxdwps+f5Iocap9AN2wfDmeO +cHJohZyw+8l/L2MAp19xJGfN2wRsVrOrAgMBAAGjUDBOMB0GA1UdDgQWBBR+hImn +BxHZ03bONEdB6JPPyo0kGDAfBgNVHSMEGDAWgBR+hImnBxHZ03bONEdB6JPPyo0k +GDAMBgNVHRMEBTADAQH/MA0GCSqGSIb3DQEBCwUAA4ICAQAhpfrm79WQuMbudbIz +pOjp26OO51cM4CKzH2vUXdgzjNEDHL2JwN0MiLHK0vwT5SkH1uoHRvECHIG8FHrA +t9ky7W0Xw4vza9pC5GJEcCHVFGEaPJVXA7+fg5JCjejeWV1PU1LsMLBibbikVFUN +JXTbKLL1EWbDLSX4ICJEyc3fptYB/w/lpDZqiKF2uIm2LSubo+fRg3D1mCexsXv1 +nl2g9eZkx6uhUl7yq8o5lB6Ejzr1t5npA8w0RlYpvMwagqSfblM7UgvcrEW6Cusl +ICELrCY/mfZwKvTgIRrj4Te19ARgXwkrG0KxcgDb+UCBaIca79t53AbpHVVrVPh6 +hXHOQrRMjf0taFILPJYn6RcC08zx7FCet4x5WqX4900yRZX7Lr0VLDZI8/c1gf0j +PsTA9JqARiQG8qeM5abLxSiXGPypYNkBlbf9H8WDpJgxRNK9RszogNhKdPkYzv0b +HgHrAOels9u3roR+wNVIy2PX+LFeSnfUCJjJZEeqs5ewpP9vgBxVIzszO2lZu1oq +uUEoFRPnIDA3TsYLzjw9MNMH0CeKUv+83QX7C9UuSyNdKZIgq2z/ABclgBtyFDSX +6EAL5WP55CCCiIG93A0y84rALIS7mPVBzVnxzRY96M2Ifs8GCwMi2Q5ykcVIPkyR +HVi0h+PsbIWC9+H9Ljl2vgu0cw== -----END CERTIFICATE----- diff --git a/test/fixture/key.pem b/test/fixture/key.pem index 250d668..b34edab 100644 --- a/test/fixture/key.pem +++ b/test/fixture/key.pem @@ -1,9 +1,51 @@ -----BEGIN RSA PRIVATE KEY----- -MIIBOwIB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IkHLvrI671n8AtQXNvv7OM +OCDh2Ni1X5DeZ3FrJYQpBzQM7JPVJKWOgwKCAQATd3cKqQCFgrenqbdpj6Xyrj6/ +YgGt3DmsVRkBiyXrIXTJw1nsWhTCnQMx0pV904zeR7jObkLpHK0TaZKHglhwBRpI +E4m94BSdmeTqnzDOsVGHxGlbU2FbSJQHwMNGzoK2G9XVzmQ3cqtilgEIRzwoE9tS +WBcNdtFFGSfCYeTYhOnDCgRawldGq1UQ5uzJU8Juvm3Vdtl6XttKlRJuRLaiEdlg +LliW/lhyYfeQWe/Zl8h0HPdoJUMWrWBenEQzWQWVj23REx5D6s6HnZFCf3UliRDo +/aSn0j6nfheIya3grmO1EPEeBuCw5BX4pLx8MqZFVjir3/8GbD1gm4z1uhqk -----END RSA PRIVATE KEY----- diff --git a/test/integration/test-get-buffer.js b/test/integration/test-get-buffer.js new file mode 100644 index 0000000..cca6fa0 --- /dev/null +++ b/test/integration/test-get-buffer.js @@ -0,0 +1,54 @@ +var common = require('../common'); +var assert = common.assert; + +var FormData = require(common.dir.lib + '/form_data'); + +(function testTypeIsBuffer() { + var form = new FormData(); + form.append( 'String', 'Some random string' ); + var buffer = form.getBuffer(); + + assert.equal(typeof buffer === 'object' && Buffer.isBuffer(buffer), true); +})(); + +(function testBufferIsValid() { + var form = new FormData(); + + var stringName = 'String'; + var stringValue = 'This is a random string'; + var intName = 'Int'; + var intValue = 1549873167987; + var bufferName = 'Buffer'; + var bufferValue = Buffer.from([0x00,0x4a,0x45,0x46,0x46,0x52,0x45,0x59,0x255]); + + // Fill the formData object + form.append( stringName, stringValue ); + form.append( intName, intValue ); + form.append( bufferName, bufferValue ); + + // Get the resulting Buffer + var buffer = form.getBuffer(); + + // Generate expected code. + var boundary = form.getBoundary(); + var expected = Buffer.concat( [ + Buffer.from( '--' + boundary + FormData.LINE_BREAK + + 'Content-Disposition: form-data; name="' + stringName + '"' + FormData.LINE_BREAK + + FormData.LINE_BREAK + + stringValue + FormData.LINE_BREAK + + '--' + boundary + FormData.LINE_BREAK + + 'Content-Disposition: form-data; name="' + intName + '"' + FormData.LINE_BREAK + + FormData.LINE_BREAK + + intValue + FormData.LINE_BREAK + + '--' + boundary + FormData.LINE_BREAK + + 'Content-Disposition: form-data; name="' + bufferName + '"' + FormData.LINE_BREAK + + 'Content-Type: application/octet-stream' + FormData.LINE_BREAK + + FormData.LINE_BREAK), + bufferValue, + Buffer.from( FormData.LINE_BREAK + '--' + boundary + '--' + FormData.LINE_BREAK ) + ] ); + + // Test if the buffer content, equals the expected buffer. + assert.equal(buffer.length, expected.length); + assert.equal(buffer.toString('hex'), expected.toString('hex')); +})(); From faaa68a297be7d4fca0ac4709d5b93afc1f78b5c Mon Sep 17 00:00:00 2001 From: noodles <460873206@qq.com> Date: Fri, 12 Apr 2019 18:47:13 +0800 Subject: [PATCH 04/36] enhance the method of path.basename MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit value.client._httpMessage.path maybe return undefined, and cause error: `TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received type undefined`。 Add `|| ''` to enhance quality。 --- lib/form_data.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/form_data.js b/lib/form_data.js index 3a1bb82..0fed867 100644 --- a/lib/form_data.js +++ b/lib/form_data.js @@ -230,7 +230,7 @@ FormData.prototype._getContentDisposition = function(value, options) { filename = path.basename(options.filename || value.name || value.path); } else if (value.readable && value.hasOwnProperty('httpVersion')) { // or try http response - filename = path.basename(value.client._httpMessage.path); + filename = path.basename(value.client._httpMessage.path || ''); } if (filename) { From b729a7e5b199fbaeabe1e225cd38eeeaf0ff4308 Mon Sep 17 00:00:00 2001 From: byr-gdp Date: Fri, 7 Jun 2019 00:13:05 +0800 Subject: [PATCH 05/36] docs(readme): add axios integration document --- Readme.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/Readme.md b/Readme.md index 0524d60..a8d6496 100644 --- a/Readme.md +++ b/Readme.md @@ -224,6 +224,27 @@ fetch('http://example.com', { method: 'POST', body: form }) }); ``` +#### axios + +In Node.js you can post a file using [axios](https://github.com/axios/axios): +```javascript +const form = new FormData(); +const stream = fs.createReadStream(PATH_TO_FILE); + +form.append('image', stream); + +// In Node.js environment you need to set boundary in the header field 'Content-Type' by calling method `getHeaders` +const formHeaders = form.getHeaders(); + +axios.post('http://example.com', form, { + headers: { + ...formHeaders, + }, +}) +.then((response) => response) +.catch(error => error) +``` + ## Notes - ```getLengthSync()``` method DOESN'T calculate length for streams, use ```knownLength``` options as workaround. From 4e3954dde304d27e3b95371d8c78002f3af5d5b2 Mon Sep 17 00:00:00 2001 From: byr-gdp Date: Fri, 7 Jun 2019 00:18:04 +0800 Subject: [PATCH 06/36] docs(readme): remove unnecessary bracket --- Readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Readme.md b/Readme.md index a8d6496..2c0984d 100644 --- a/Readme.md +++ b/Readme.md @@ -241,7 +241,7 @@ axios.post('http://example.com', form, { ...formHeaders, }, }) -.then((response) => response) +.then(response => response) .catch(error => error) ``` From 0f8da06c0b4c997bd2f6b09d78290d339616a950 Mon Sep 17 00:00:00 2001 From: Alex Indigo Date: Tue, 18 Jun 2019 23:45:51 -0700 Subject: [PATCH 07/36] Bumped version 2.4.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index adacbae..ad34e98 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "author": "Felix Geisendörfer (http://debuggable.com/)", "name": "form-data", "description": "A library to create readable \"multipart/form-data\" streams. Can be used to submit forms and file uploads to other web applications.", - "version": "2.3.3", + "version": "2.4.0", "repository": { "type": "git", "url": "git://github.com/form-data/form-data.git" From 8512eef436e28372f5bc88de3ca76a9cb46e6847 Mon Sep 17 00:00:00 2001 From: Alex Indigo Date: Wed, 19 Jun 2019 00:12:09 -0700 Subject: [PATCH 08/36] Updated build/test/badges --- .travis.yml | 3 ++- Readme.md | 7 +++---- appveyor.yml | 3 ++- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/.travis.yml b/.travis.yml index 37f8d8a..1f75e5b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,7 +5,8 @@ node_js: - "4" - "6" - "8" - - "9" + - "10" + - "12" os: - osx diff --git a/Readme.md b/Readme.md index 2c0984d..08c3dfe 100644 --- a/Readme.md +++ b/Readme.md @@ -6,13 +6,12 @@ The API of this library is inspired by the [XMLHttpRequest-2 FormData Interface] [xhr2-fd]: http://dev.w3.org/2006/webapi/XMLHttpRequest-2/Overview.html#the-formdata-interface -[![Linux Build](https://img.shields.io/travis/form-data/form-data/master.svg?label=linux:4.x-9.x)](https://travis-ci.org/form-data/form-data) -[![MacOS Build](https://img.shields.io/travis/form-data/form-data/master.svg?label=macos:4.x-9.x)](https://travis-ci.org/form-data/form-data) -[![Windows Build](https://img.shields.io/appveyor/ci/alexindigo/form-data/master.svg?label=windows:4.x-9.x)](https://ci.appveyor.com/project/alexindigo/form-data) +[![Linux Build](https://img.shields.io/travis/form-data/form-data/master.svg?label=linux:4.x-12.x)](https://travis-ci.org/form-data/form-data) +[![MacOS Build](https://img.shields.io/travis/form-data/form-data/master.svg?label=macos:4.x-12.x)](https://travis-ci.org/form-data/form-data) +[![Windows Build](https://img.shields.io/appveyor/ci/alexindigo/form-data/master.svg?label=windows:4.x-12.x)](https://ci.appveyor.com/project/alexindigo/form-data) [![Coverage Status](https://img.shields.io/coveralls/form-data/form-data/master.svg?label=code+coverage)](https://coveralls.io/github/form-data/form-data?branch=master) [![Dependency Status](https://img.shields.io/david/form-data/form-data.svg)](https://david-dm.org/form-data/form-data) -[![bitHound Overall Score](https://www.bithound.io/github/form-data/form-data/badges/score.svg)](https://www.bithound.io/github/form-data/form-data) ## Install diff --git a/appveyor.yml b/appveyor.yml index eac9c92..ab428f3 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -3,7 +3,8 @@ environment: - nodejs_version: '4' - nodejs_version: '6' - nodejs_version: '8' - - nodejs_version: '9' + - nodejs_version: '10' + - nodejs_version: '12' platform: - x86 - x64 From a3c0142ed91b0c7dcaf89c4f618776708f1f70a9 Mon Sep 17 00:00:00 2001 From: mapleeit <522856232@qq.com> Date: Fri, 21 Jun 2019 13:25:59 +0800 Subject: [PATCH 09/36] feat(typings): migrate TS typings from DefinitelyTyped/DefinitelyTyped here for the convenience of maintaining and usage --- .eslintignore | 1 + .eslintrc | 2 +- .gitignore | 1 + index.d.ts | 42 ++++++++++++++++++++++++++++++++++++++++++ package.json | 5 ++++- 5 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 index.d.ts diff --git a/.eslintignore b/.eslintignore index 8d87b1d..510731c 100644 --- a/.eslintignore +++ b/.eslintignore @@ -1 +1,2 @@ node_modules/* +index.d.ts diff --git a/.eslintrc b/.eslintrc index 68fb544..63de477 100644 --- a/.eslintrc +++ b/.eslintrc @@ -35,7 +35,7 @@ "semi-spacing": 0, "no-multi-spaces": 0, "eqeqeq": 0, - "no-mixed-requires": 0, + "no-mixed-requires": 0 }, "env": { "node": true diff --git a/.gitignore b/.gitignore index 8d5dba1..5790d18 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,7 @@ sftp-config.json yarn.lock +package-lock.json coverage/ node_modules/ diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..2018a18 --- /dev/null +++ b/index.d.ts @@ -0,0 +1,42 @@ +// Definitions by: Carlos Ballesteros Velasco +// Leon Yu +// BendingBender +// Maple Miao + +/// +import * as stream from 'stream'; +import * as http from 'http'; + +export = FormData; + +declare class FormData extends stream.Readable { + append(key: string, value: any, options?: FormData.AppendOptions | string): void; + getHeaders(): FormData.Headers; + submit( + params: string | FormData.SubmitOptions, + callback?: (error: Error | undefined, response: http.IncomingMessage) => void + ): http.ClientRequest; + getBuffer(): Buffer; + getBoundary(): string; + getLength(callback: (err: Error | undefined, length: number) => void): void; + getLengthSync(): number; + hasKnownLength(): boolean; +} + +declare namespace FormData { + interface Headers { + [key: string]: any; + } + + interface AppendOptions { + header?: string | Headers; + knownLength?: number; + filename?: string; + filepath?: string; + contentType?: string; + } + + interface SubmitOptions extends http.RequestOptions { + protocol?: 'https:' | 'http:'; + } +} diff --git a/package.json b/package.json index ad34e98..9937718 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,7 @@ }, "main": "./lib/form_data", "browser": "./lib/browser", + "typings": "./index.d.ts", "scripts": { "pretest": "rimraf coverage test/tmp", "test": "istanbul cover test/run.js", @@ -37,6 +38,7 @@ "node": ">= 0.12" }, "dependencies": { + "@types/node": "^12.0.8", "asynckit": "^0.4.0", "combined-stream": "^1.0.6", "mime-types": "^2.1.12" @@ -59,7 +61,8 @@ "pre-commit": "^1.1.3", "request": "2.76.0", "rimraf": "^2.5.4", - "tape": "^4.6.2" + "tape": "^4.6.2", + "typescript": "^3.5.2" }, "license": "MIT" } From 21323f3b4043a167046a4a2554c5f2825356c423 Mon Sep 17 00:00:00 2001 From: Jeffrey Benistant Date: Mon, 24 Jun 2019 23:23:01 +0200 Subject: [PATCH 10/36] - Added: public methods with information and examples to the readme file. --- Readme.md | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git a/Readme.md b/Readme.md index 08c3dfe..babe9ed 100644 --- a/Readme.md +++ b/Readme.md @@ -184,6 +184,92 @@ form.submit({ }); ``` +### Methods + +#####_void_ append( **String** _field_, **Mixed** _value_ [, **Mixed** _options_] ) +Append data to the form. You can submit about any format (string, integer, boolean, buffer, etc.). However, Arrays are not supported and need to be turned into strings by the user. +```javascript +var form = new FormData(); +form.append( 'my_string', 'my value' ); +form.append( 'my_integer', 1 ); +form.append( 'my_boolean', true ); +form.append( 'my_buffer', new Buffer(10) ); +form.append( 'my_array_as_json', JSON.stringify( ['bird','cute'] ) ) +``` + +You may provide a string for options, or an object. +```javascript +// Set filename by providing a string for options +form.append( 'my_file', fs.createReadStream('/foo/bar.jpg'), 'bar.jpg' ); + +// provide an object. +form.append( 'my_file', fs.createReadStream('/foo/bar.jpg'), {filename: 'bar.jpg', filesize: 1045} ); +``` + +#####_Array_ getHeaders( [**Array** _userHeaders_] ) +This method ads the correct `content-type` header to the provided array of `userHeaders`. + +#####_String_ getBoundary() +Return the boundary of the formData. A boundary consists of 26x `-` followed by 24 numbers +for example: +```javascript +--------------------------515890814546601021194782 +``` +_Note: The boundary must be unique and may not appear in the data._ + +#####_Buffer_ getBuffer() +Return the full formdata request package, as a Buffer. You can insert this Buffer in e.g. Axios to send multipart data. + +```javascript +var form = new FormData(); +form.append( 'my_buffer', Buffer.from([0x4a,0x42,0x20,0x52,0x6f,0x63,0x6b,0x73]) ); +form.append( 'my_file', fs.createReadStream('/foo/bar.jpg') ); + +axios.post( 'https://example.com/path/to/api', + form.getBuffer(), + form.getHeaders() + ) +``` + +#####_Integer_ getLengthSync() +Same as `getLength` but synchronous. + +_Note: getLengthSync __doesn't__ calculate streams length._ + +#####_Integer_ getLength( function _callback_ ) +Returns the `Content-Length` async. The callback is used to handle errors and continue once the length has been calculated +```javascript +this.getLength(function(err, length) { + if (err) { + this._error(err); + return; + } + + // add content length + request.setHeader('Content-Length', length); + + ... +}.bind(this)); +``` + +#####_boolean_ hasKnownLength() +Checks if the length of added values is known. + +#####_request_ submit( _params_, function _callback_ ) +Submit the form to a web application. +```javascript +var form = new FormData(); +form.append( 'my_string', 'Hello World' ); + +form.submit( 'http://example.com/', function(err, res) { + // res – response object (http.IncomingMessage) // + res.resume(); +} ); +``` + +#####_String_ toString() +Returns the form data as a string. Don't use this if you are sending files or buffers, use `getBuffer()` instead. + ### Integration with other libraries #### Request From fc61c7381fad12662df16dbc3e7621c91b886f03 Mon Sep 17 00:00:00 2001 From: Alex Indigo Date: Mon, 24 Jun 2019 20:04:26 -0700 Subject: [PATCH 11/36] Switched to Travis Windows from Appveyor --- .travis.yml | 5 +++-- appveyor.yml | 22 ---------------------- 2 files changed, 3 insertions(+), 24 deletions(-) delete mode 100644 appveyor.yml diff --git a/.travis.yml b/.travis.yml index 1f75e5b..7cf751a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,6 +11,7 @@ node_js: os: - osx - linux +- windows install: - travis_retry npm install @@ -19,8 +20,8 @@ script: - uname -a - node --version - npm --version - - npm run ci-lint - - npm run ci-test + - if [ "$TRAVIS_OS_NAME" != "windows" ]; then npm run ci-lint; fi + - if [ "$TRAVIS_OS_NAME" = "windows" ]; then npm run test; else npm run ci-test; fi - npm run check after_success: diff --git a/appveyor.yml b/appveyor.yml deleted file mode 100644 index ab428f3..0000000 --- a/appveyor.yml +++ /dev/null @@ -1,22 +0,0 @@ -environment: - matrix: - - nodejs_version: '4' - - nodejs_version: '6' - - nodejs_version: '8' - - nodejs_version: '10' - - nodejs_version: '12' -platform: - - x86 - - x64 -install: - - ps: Install-Product node $env:nodejs_version $env:platform - - npm -g install npm@2 - - set PATH=%APPDATA%\npm;%PATH% - - npm install -test_script: - - node --version - - npm --version - - npm run test -build: off -matrix: - fast_finish: true From e3d8628728f6e4817ab97deeed92f0c822661b89 Mon Sep 17 00:00:00 2001 From: Alex Indigo Date: Mon, 24 Jun 2019 21:36:38 -0700 Subject: [PATCH 12/36] Updated deps and readme --- Readme.md | 2 +- package.json | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Readme.md b/Readme.md index 08c3dfe..b117722 100644 --- a/Readme.md +++ b/Readme.md @@ -8,7 +8,7 @@ The API of this library is inspired by the [XMLHttpRequest-2 FormData Interface] [![Linux Build](https://img.shields.io/travis/form-data/form-data/master.svg?label=linux:4.x-12.x)](https://travis-ci.org/form-data/form-data) [![MacOS Build](https://img.shields.io/travis/form-data/form-data/master.svg?label=macos:4.x-12.x)](https://travis-ci.org/form-data/form-data) -[![Windows Build](https://img.shields.io/appveyor/ci/alexindigo/form-data/master.svg?label=windows:4.x-12.x)](https://ci.appveyor.com/project/alexindigo/form-data) +[![Windows Build](https://img.shields.io/travis/form-data/form-data/master.svg?label=windows:4.x-12.x)](https://travis-ci.org/form-data/form-data) [![Coverage Status](https://img.shields.io/coveralls/form-data/form-data/master.svg?label=code+coverage)](https://coveralls.io/github/form-data/form-data?branch=master) [![Dependency Status](https://img.shields.io/david/form-data/form-data.svg)](https://david-dm.org/form-data/form-data) diff --git a/package.json b/package.json index 9937718..bbdc886 100644 --- a/package.json +++ b/package.json @@ -46,9 +46,9 @@ "devDependencies": { "browserify": "^13.1.1", "browserify-istanbul": "^2.0.0", - "coveralls": "^2.11.14", + "coveralls": "^3.0.4", "cross-spawn": "^4.0.2", - "eslint": "^3.9.1", + "eslint": "^6.0.1", "fake": "^0.2.2", "far": "^0.0.7", "formidable": "^1.0.17", From 508b626bf1b460d3733d3420dc1cfd001617f6ac Mon Sep 17 00:00:00 2001 From: Alex Indigo Date: Mon, 24 Jun 2019 21:45:58 -0700 Subject: [PATCH 13/36] Bump notion of modern node to node8 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index bbdc886..dda264b 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,7 @@ "posttest": "istanbul report lcov text", "lint": "eslint lib/*.js test/*.js test/integration/*.js", "report": "istanbul report lcov text", - "ci-lint": "is-node-modern 6 && npm run lint || is-node-not-modern 6", + "ci-lint": "is-node-modern 8 && npm run lint || is-node-not-modern 8", "ci-test": "npm run test && npm run browser && npm run report", "predebug": "rimraf coverage test/tmp", "debug": "verbose=1 ./test/run.js", From 65e78f5587dc09c95189dec37ec95af0de4991e1 Mon Sep 17 00:00:00 2001 From: mapleeit <522856232@qq.com> Date: Wed, 26 Jun 2019 10:26:33 +0800 Subject: [PATCH 14/36] chore: move @types/node to devDep --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 9937718..fbc684b 100644 --- a/package.json +++ b/package.json @@ -38,12 +38,12 @@ "node": ">= 0.12" }, "dependencies": { - "@types/node": "^12.0.8", "asynckit": "^0.4.0", "combined-stream": "^1.0.6", "mime-types": "^2.1.12" }, "devDependencies": { + "@types/node": "^12.0.10", "browserify": "^13.1.1", "browserify-istanbul": "^2.0.0", "coveralls": "^2.11.14", From e93ed8df9d7f22078bc3a2c24889e9dfa11e192d Mon Sep 17 00:00:00 2001 From: Jeffrey Benistant Date: Sun, 30 Jun 2019 12:57:55 +0200 Subject: [PATCH 15/36] - Fixed: rendering of subheaders --- Readme.md | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/Readme.md b/Readme.md index babe9ed..453a5b8 100644 --- a/Readme.md +++ b/Readme.md @@ -186,7 +186,7 @@ form.submit({ ### Methods -#####_void_ append( **String** _field_, **Mixed** _value_ [, **Mixed** _options_] ) +##### _void_ append( **String** _field_, **Mixed** _value_ [, **Mixed** _options_] ) Append data to the form. You can submit about any format (string, integer, boolean, buffer, etc.). However, Arrays are not supported and need to be turned into strings by the user. ```javascript var form = new FormData(); @@ -206,10 +206,10 @@ form.append( 'my_file', fs.createReadStream('/foo/bar.jpg'), 'bar.jpg' ); form.append( 'my_file', fs.createReadStream('/foo/bar.jpg'), {filename: 'bar.jpg', filesize: 1045} ); ``` -#####_Array_ getHeaders( [**Array** _userHeaders_] ) +##### _Array_ getHeaders( [**Array** _userHeaders_] ) This method ads the correct `content-type` header to the provided array of `userHeaders`. -#####_String_ getBoundary() +##### _String_ getBoundary() Return the boundary of the formData. A boundary consists of 26x `-` followed by 24 numbers for example: ```javascript @@ -217,7 +217,7 @@ for example: ``` _Note: The boundary must be unique and may not appear in the data._ -#####_Buffer_ getBuffer() +##### _Buffer_ getBuffer() Return the full formdata request package, as a Buffer. You can insert this Buffer in e.g. Axios to send multipart data. ```javascript @@ -231,12 +231,12 @@ axios.post( 'https://example.com/path/to/api', ) ``` -#####_Integer_ getLengthSync() +##### _Integer_ getLengthSync() Same as `getLength` but synchronous. _Note: getLengthSync __doesn't__ calculate streams length._ -#####_Integer_ getLength( function _callback_ ) +##### _Integer_ getLength( function _callback_ ) Returns the `Content-Length` async. The callback is used to handle errors and continue once the length has been calculated ```javascript this.getLength(function(err, length) { @@ -252,10 +252,10 @@ this.getLength(function(err, length) { }.bind(this)); ``` -#####_boolean_ hasKnownLength() +##### _boolean_ hasKnownLength() Checks if the length of added values is known. -#####_request_ submit( _params_, function _callback_ ) +##### _request_ submit( _params_, function _callback_ ) Submit the form to a web application. ```javascript var form = new FormData(); @@ -267,7 +267,7 @@ form.submit( 'http://example.com/', function(err, res) { } ); ``` -#####_String_ toString() +##### _String_ toString() Returns the form data as a string. Don't use this if you are sending files or buffers, use `getBuffer()` instead. ### Integration with other libraries From 37350fa250782f156a998ec1fa9671866d40ac49 Mon Sep 17 00:00:00 2001 From: Jeffrey Benistant Date: Sun, 30 Jun 2019 13:28:55 +0200 Subject: [PATCH 16/36] - Fixed: Typos - Added: Links to the methods --- Readme.md | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/Readme.md b/Readme.md index 453a5b8..83b6c60 100644 --- a/Readme.md +++ b/Readme.md @@ -186,7 +186,17 @@ form.submit({ ### Methods -##### _void_ append( **String** _field_, **Mixed** _value_ [, **Mixed** _options_] ) +- [_Void_ append( **String** _field_, **Mixed** _value_ [, **Mixed** _options_] )](https://github.com/form-data/form-data#void-append-string-field-mixed-value--mixed-options-). +- [_Array_ getHeaders( [**Array** _userHeaders_] )](https://github.com/form-data/form-data#array-getheaders-array-userheaders-) +- [_String_ getBoundary()](https://github.com/form-data/form-data#string-getboundary) +- [_Buffer_ getBuffer()](https://github.com/form-data/form-data#buffer-getbuffer) +- [_Integer_ getLengthSync()](https://github.com/form-data/form-data#integer-getlengthsync) +- [_Integer_ getLength( **function** _callback_ )](https://github.com/form-data/form-data#integer-getlength-function-callback-) +- [_Boolean_ hasKnownLength()](https://github.com/form-data/form-data#boolean-hasknownlength) +- [_Request_ submit( _params_, **function** _callback_ )](https://github.com/form-data/form-data#request-submit-params-function-callback-) +- [_String_ toString()](https://github.com/form-data/form-data#string-tostring) + +#### _Void_ append( **String** _field_, **Mixed** _value_ [, **Mixed** _options_] ) Append data to the form. You can submit about any format (string, integer, boolean, buffer, etc.). However, Arrays are not supported and need to be turned into strings by the user. ```javascript var form = new FormData(); @@ -206,18 +216,18 @@ form.append( 'my_file', fs.createReadStream('/foo/bar.jpg'), 'bar.jpg' ); form.append( 'my_file', fs.createReadStream('/foo/bar.jpg'), {filename: 'bar.jpg', filesize: 1045} ); ``` -##### _Array_ getHeaders( [**Array** _userHeaders_] ) +#### _Array_ getHeaders( [**Array** _userHeaders_] ) This method ads the correct `content-type` header to the provided array of `userHeaders`. -##### _String_ getBoundary() -Return the boundary of the formData. A boundary consists of 26x `-` followed by 24 numbers +#### _String_ getBoundary() +Return the boundary of the formData. A boundary consists of 26 `-` followed by 24 numbers for example: ```javascript --------------------------515890814546601021194782 ``` _Note: The boundary must be unique and may not appear in the data._ -##### _Buffer_ getBuffer() +#### _Buffer_ getBuffer() Return the full formdata request package, as a Buffer. You can insert this Buffer in e.g. Axios to send multipart data. ```javascript @@ -231,12 +241,12 @@ axios.post( 'https://example.com/path/to/api', ) ``` -##### _Integer_ getLengthSync() +#### _Integer_ getLengthSync() Same as `getLength` but synchronous. _Note: getLengthSync __doesn't__ calculate streams length._ -##### _Integer_ getLength( function _callback_ ) +#### _Integer_ getLength( **function** _callback_ ) Returns the `Content-Length` async. The callback is used to handle errors and continue once the length has been calculated ```javascript this.getLength(function(err, length) { @@ -252,10 +262,10 @@ this.getLength(function(err, length) { }.bind(this)); ``` -##### _boolean_ hasKnownLength() +#### _Boolean_ hasKnownLength() Checks if the length of added values is known. -##### _request_ submit( _params_, function _callback_ ) +#### _Request_ submit( _params_, **function** _callback_ ) Submit the form to a web application. ```javascript var form = new FormData(); @@ -267,7 +277,7 @@ form.submit( 'http://example.com/', function(err, res) { } ); ``` -##### _String_ toString() +#### _String_ toString() Returns the form data as a string. Don't use this if you are sending files or buffers, use `getBuffer()` instead. ### Integration with other libraries From d88f912b75b666b47f8674467516eade69d2d5be Mon Sep 17 00:00:00 2001 From: Jeffrey Benistant Date: Sun, 30 Jun 2019 13:47:16 +0200 Subject: [PATCH 17/36] - Fixed: filesize is not a valid option? knownLength should be used for streams --- Readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Readme.md b/Readme.md index f9fadec..bb9fd6a 100644 --- a/Readme.md +++ b/Readme.md @@ -213,7 +213,7 @@ You may provide a string for options, or an object. form.append( 'my_file', fs.createReadStream('/foo/bar.jpg'), 'bar.jpg' ); // provide an object. -form.append( 'my_file', fs.createReadStream('/foo/bar.jpg'), {filename: 'bar.jpg', filesize: 1045} ); +form.append( 'my_file', fs.createReadStream('/foo/bar.jpg'), {filename: 'bar.jpg', contentType: 'image/jpeg', knownLength: 19806} ); ``` #### _Array_ getHeaders( [**Array** _userHeaders_] ) From 19add50afb7de66c70d189f422d16f1b886616e2 Mon Sep 17 00:00:00 2001 From: Alex Indigo Date: Wed, 3 Jul 2019 21:10:01 -0700 Subject: [PATCH 18/36] Updated dependencies --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 93ae348..ada57dc 100644 --- a/package.json +++ b/package.json @@ -47,7 +47,7 @@ "browserify": "^13.1.1", "browserify-istanbul": "^2.0.0", "coveralls": "^3.0.4", - "cross-spawn": "^4.0.2", + "cross-spawn": "^6.0.5", "eslint": "^6.0.1", "fake": "^0.2.2", "far": "^0.0.7", @@ -59,7 +59,7 @@ "phantomjs-prebuilt": "^2.1.13", "pkgfiles": "^2.3.0", "pre-commit": "^1.1.3", - "request": "2.76.0", + "request": "^2.88.0", "rimraf": "^2.5.4", "tape": "^4.6.2", "typescript": "^3.5.2" From 905f173a3f785e8d312998e765634ee451ca5f42 Mon Sep 17 00:00:00 2001 From: Alex Indigo Date: Wed, 3 Jul 2019 21:20:23 -0700 Subject: [PATCH 19/36] Bumped version to 2.5.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index ada57dc..d8bb56d 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "author": "Felix Geisendörfer (http://debuggable.com/)", "name": "form-data", "description": "A library to create readable \"multipart/form-data\" streams. Can be used to submit forms and file uploads to other web applications.", - "version": "2.4.0", + "version": "2.5.0", "repository": { "type": "git", "url": "git://github.com/form-data/form-data.git" From f4ca7f8e31f7e07df22c1aeb8e0a32a7055a64ca Mon Sep 17 00:00:00 2001 From: ScriptSmith Date: Sat, 27 Jul 2019 11:45:39 +1000 Subject: [PATCH 20/36] Fixed: getHeaders method signatures --- Readme.md | 4 ++-- index.d.ts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Readme.md b/Readme.md index bb9fd6a..c941dc3 100644 --- a/Readme.md +++ b/Readme.md @@ -187,7 +187,7 @@ form.submit({ ### Methods - [_Void_ append( **String** _field_, **Mixed** _value_ [, **Mixed** _options_] )](https://github.com/form-data/form-data#void-append-string-field-mixed-value--mixed-options-). -- [_Array_ getHeaders( [**Array** _userHeaders_] )](https://github.com/form-data/form-data#array-getheaders-array-userheaders-) +- [_Headers_ getHeaders( [**Headers** _userHeaders_] )](https://github.com/form-data/form-data#array-getheaders-array-userheaders-) - [_String_ getBoundary()](https://github.com/form-data/form-data#string-getboundary) - [_Buffer_ getBuffer()](https://github.com/form-data/form-data#buffer-getbuffer) - [_Integer_ getLengthSync()](https://github.com/form-data/form-data#integer-getlengthsync) @@ -216,7 +216,7 @@ form.append( 'my_file', fs.createReadStream('/foo/bar.jpg'), 'bar.jpg' ); form.append( 'my_file', fs.createReadStream('/foo/bar.jpg'), {filename: 'bar.jpg', contentType: 'image/jpeg', knownLength: 19806} ); ``` -#### _Array_ getHeaders( [**Array** _userHeaders_] ) +#### _Headers_ getHeaders( [**Headers** _userHeaders_] ) This method ads the correct `content-type` header to the provided array of `userHeaders`. #### _String_ getBoundary() diff --git a/index.d.ts b/index.d.ts index 2018a18..3053519 100644 --- a/index.d.ts +++ b/index.d.ts @@ -11,7 +11,7 @@ export = FormData; declare class FormData extends stream.Readable { append(key: string, value: any, options?: FormData.AppendOptions | string): void; - getHeaders(): FormData.Headers; + getHeaders(userHeaders: FormData.Headers): FormData.Headers; submit( params: string | FormData.SubmitOptions, callback?: (error: Error | undefined, response: http.IncomingMessage) => void From 09fcc980bd4ab85a39750d38db7eac8406e7c771 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Linus=20Unneb=C3=A4ck?= Date: Fri, 2 Aug 2019 19:53:22 +0100 Subject: [PATCH 21/36] Fix error in callback signatures The functions actually give `null` as the error parameter, and not `undefined`. Without this, `util.promisify` can't be typed correctly on these functions as it expects the callback to receive `Error | null`. --- index.d.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.d.ts b/index.d.ts index 2018a18..8d189b2 100644 --- a/index.d.ts +++ b/index.d.ts @@ -14,11 +14,11 @@ declare class FormData extends stream.Readable { getHeaders(): FormData.Headers; submit( params: string | FormData.SubmitOptions, - callback?: (error: Error | undefined, response: http.IncomingMessage) => void + callback?: (error: Error | null, response: http.IncomingMessage) => void ): http.ClientRequest; getBuffer(): Buffer; getBoundary(): string; - getLength(callback: (err: Error | undefined, length: number) => void): void; + getLength(callback: (err: Error | null, length: number) => void): void; getLengthSync(): number; hasKnownLength(): boolean; } From 5a3e2cf33bd76e3d2f1b1dee1cc17be157128399 Mon Sep 17 00:00:00 2001 From: Spencer Elliott Date: Fri, 9 Aug 2019 12:27:01 -0400 Subject: [PATCH 22/36] Add constructor options to TypeScript defs --- index.d.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/index.d.ts b/index.d.ts index 2018a18..18693b4 100644 --- a/index.d.ts +++ b/index.d.ts @@ -9,7 +9,13 @@ import * as http from 'http'; export = FormData; +interface Options { + maxDataSize?: number; + pauseStreams?: boolean; +} + declare class FormData extends stream.Readable { + constructor(options?: Options); append(key: string, value: any, options?: FormData.AppendOptions | string): void; getHeaders(): FormData.Headers; submit( From a6bc2d4296dbdee5d84cbab7c69bcd0eea7a12e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Linus=20Unneb=C3=A4ck?= Date: Thu, 15 Aug 2019 09:45:14 +0100 Subject: [PATCH 23/36] Bump rimraf to 2.7.1 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index d8bb56d..d444791 100644 --- a/package.json +++ b/package.json @@ -60,7 +60,7 @@ "pkgfiles": "^2.3.0", "pre-commit": "^1.1.3", "request": "^2.88.0", - "rimraf": "^2.5.4", + "rimraf": "^2.7.1", "tape": "^4.6.2", "typescript": "^3.5.2" }, From 4d41a32c0b3f85f8bbc9cf17df43befd2d5fc305 Mon Sep 17 00:00:00 2001 From: Spencer Elliott Date: Thu, 15 Aug 2019 11:06:04 -0400 Subject: [PATCH 24/36] Add remaining combined-stream options to typedef --- index.d.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/index.d.ts b/index.d.ts index 18693b4..b653daa 100644 --- a/index.d.ts +++ b/index.d.ts @@ -10,6 +10,9 @@ import * as http from 'http'; export = FormData; interface Options { + writable?: boolean; + readable?: boolean; + dataSize?: number; maxDataSize?: number; pauseStreams?: boolean; } From 5695bcac6de282b36dd5ec5f6f1f456062d0be54 Mon Sep 17 00:00:00 2001 From: Jeffrey Benistant Date: Tue, 20 Aug 2019 09:54:35 +0200 Subject: [PATCH 25/36] -Fixed: Eerror in the documentations as indicated in #439 --- Readme.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Readme.md b/Readme.md index bb9fd6a..6077db9 100644 --- a/Readme.md +++ b/Readme.md @@ -229,17 +229,17 @@ _Note: The boundary must be unique and may not appear in the data._ #### _Buffer_ getBuffer() Return the full formdata request package, as a Buffer. You can insert this Buffer in e.g. Axios to send multipart data. - ```javascript var form = new FormData(); form.append( 'my_buffer', Buffer.from([0x4a,0x42,0x20,0x52,0x6f,0x63,0x6b,0x73]) ); -form.append( 'my_file', fs.createReadStream('/foo/bar.jpg') ); +form.append( 'my_file', fs.readFileSync('/foo/bar.jpg') ); axios.post( 'https://example.com/path/to/api', form.getBuffer(), form.getHeaders() ) ``` +**Note:** Because the output is of type Buffer, you can only append types that are accepted by Buffer: *string, Buffer, ArrayBuffer, Array, or Array-like Object*. A ReadStream for example will result in an error. #### _Integer_ getLengthSync() Same as `getLength` but synchronous. From 8ce81f56cccf5466363a5eff135ad394a929f59b Mon Sep 17 00:00:00 2001 From: Alex Indigo Date: Wed, 28 Aug 2019 23:42:51 -0700 Subject: [PATCH 26/36] Bumped version 2.5.1 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index d444791..7423f20 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "author": "Felix Geisendörfer (http://debuggable.com/)", "name": "form-data", "description": "A library to create readable \"multipart/form-data\" streams. Can be used to submit forms and file uploads to other web applications.", - "version": "2.5.0", + "version": "2.5.1", "repository": { "type": "git", "url": "git://github.com/form-data/form-data.git" From 556f097ceaf21c6940eab87b5f8a16b2f195b88b Mon Sep 17 00:00:00 2001 From: Alba Mendez Date: Tue, 28 May 2019 23:16:39 +0200 Subject: [PATCH 27/36] Update combined-stream (fixes #422) --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 7423f20..181ba49 100644 --- a/package.json +++ b/package.json @@ -39,7 +39,7 @@ }, "dependencies": { "asynckit": "^0.4.0", - "combined-stream": "^1.0.6", + "combined-stream": "^1.0.8", "mime-types": "^2.1.12" }, "devDependencies": { From 7502bf09ac338827a1a6612758cc47140cb96fe0 Mon Sep 17 00:00:00 2001 From: sakshi87 Date: Tue, 3 Sep 2019 11:39:42 +0530 Subject: [PATCH 28/36] form-data: Replaced PhantomJS Dependency Signed-off-by: sakshi87 --- .travis.yml | 1 - package.json | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 7cf751a..f22a5d9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,7 +2,6 @@ sudo: false language: node_js node_js: - - "4" - "6" - "8" - "10" diff --git a/package.json b/package.json index 181ba49..70a89f3 100644 --- a/package.json +++ b/package.json @@ -56,7 +56,7 @@ "is-node-modern": "^1.0.0", "istanbul": "^0.4.5", "obake": "^0.1.2", - "phantomjs-prebuilt": "^2.1.13", + "puppeteer": "^1.19.0", "pkgfiles": "^2.3.0", "pre-commit": "^1.1.3", "request": "^2.88.0", From 2b4e4787031490942f2d1ee55c56b85a250875a7 Mon Sep 17 00:00:00 2001 From: ScriptSmith Date: Tue, 3 Sep 2019 21:58:09 +1000 Subject: [PATCH 29/36] Make userHeaders optional --- index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.d.ts b/index.d.ts index 2c880c1..d414502 100644 --- a/index.d.ts +++ b/index.d.ts @@ -20,7 +20,7 @@ interface Options { declare class FormData extends stream.Readable { constructor(options?: Options); append(key: string, value: any, options?: FormData.AppendOptions | string): void; - getHeaders(userHeaders: FormData.Headers): FormData.Headers; + getHeaders(userHeaders?: FormData.Headers): FormData.Headers; submit( params: string | FormData.SubmitOptions, callback?: (error: Error | null, response: http.IncomingMessage) => void From 80c8f746bcf4c0418ae35fbedde12fb8c01e2748 Mon Sep 17 00:00:00 2001 From: Ovyerus Date: Sat, 19 Oct 2019 16:53:35 +1100 Subject: [PATCH 30/36] Add readable stream options to constructor type --- index.d.ts | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/index.d.ts b/index.d.ts index d414502..6e52045 100644 --- a/index.d.ts +++ b/index.d.ts @@ -9,7 +9,17 @@ import * as http from 'http'; export = FormData; -interface Options { +// Extracted because @types/node doesn't export interfaces. +interface ReadableOptions { + highWaterMark?: number; + encoding?: string; + objectMode?: boolean; + read?(this: stream.Readable, size: number): void; + destroy?(this: stream.Readable, error: Error | null, callback: (error: Error | null) => void): void; + autoDestroy?: boolean; +} + +interface Options extends ReadableOptions { writable?: boolean; readable?: boolean; dataSize?: number; From 4bde68e12de1ba90fefad2e7e643f6375b902763 Mon Sep 17 00:00:00 2001 From: Ovyerus Date: Sat, 19 Oct 2019 16:54:25 +1100 Subject: [PATCH 31/36] Pass options to constructor if not used with new --- lib/form_data.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/form_data.js b/lib/form_data.js index 9c07e32..65a7798 100644 --- a/lib/form_data.js +++ b/lib/form_data.js @@ -25,7 +25,7 @@ util.inherits(FormData, CombinedStream); */ function FormData(options) { if (!(this instanceof FormData)) { - return new FormData(); + return new FormData(options); } this._overheadLength = 0; From 2aaa0da514ad92c809e210ea8588e9eefa6c6412 Mon Sep 17 00:00:00 2001 From: Bohdan Korzun Date: Tue, 29 Oct 2019 17:06:56 +0200 Subject: [PATCH 32/36] fix memory leak For many reasons, node caches and leaves in memory (for pretty lengthy amount of time) HTTPRequest instances. Form-data doesn't unsubscribe from 'error' and 'response' event, leaving references to finish / callback. This is kind-of okay in most cases (HTTPRequest's do die after a while), it might be a problem with large payloads or other memory-hungry situations. This was only tested with NodeJS 8 - future versions might change how they are working with HTTPRequest's objects. --- lib/form_data.js | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/form_data.js b/lib/form_data.js index 65a7798..ddfae2e 100644 --- a/lib/form_data.js +++ b/lib/form_data.js @@ -462,8 +462,19 @@ FormData.prototype.submit = function(params, cb) { this.pipe(request); if (cb) { - request.on('error', cb); - request.on('response', cb.bind(this, null)); + var onResponse; + + var callback = function (error, responce) { + request.removeListener('error', callback); + request.removeListener('response', onResponse); + + return cb.call(this, error, responce); + }; + + onResponse = callback.bind(this, null); + + request.on('error', callback); + request.on('response', onResponse); } }.bind(this)); From 510d939e7ff7e3b26159c75fe95cbfda409392d6 Mon Sep 17 00:00:00 2001 From: Maple Miao <522856232@qq.com> Date: Wed, 30 Oct 2019 16:17:52 +0800 Subject: [PATCH 33/36] Update package.json version up to v3.0.0, and engines node should >= 6 --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 70a89f3..6f1ebf0 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "author": "Felix Geisendörfer (http://debuggable.com/)", "name": "form-data", "description": "A library to create readable \"multipart/form-data\" streams. Can be used to submit forms and file uploads to other web applications.", - "version": "2.5.1", + "version": "3.0.0", "repository": { "type": "git", "url": "git://github.com/form-data/form-data.git" @@ -35,7 +35,7 @@ "check" ], "engines": { - "node": ">= 0.12" + "node": ">= 6" }, "dependencies": { "asynckit": "^0.4.0", From 073fd1a776e478782cca3fd7921573a36223cbdd Mon Sep 17 00:00:00 2001 From: Maple Miao <522856232@qq.com> Date: Wed, 6 Nov 2019 13:34:10 +0800 Subject: [PATCH 34/36] Update Readme.md --- Readme.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Readme.md b/Readme.md index 5f6476e..e9195bd 100644 --- a/Readme.md +++ b/Readme.md @@ -6,9 +6,9 @@ The API of this library is inspired by the [XMLHttpRequest-2 FormData Interface] [xhr2-fd]: http://dev.w3.org/2006/webapi/XMLHttpRequest-2/Overview.html#the-formdata-interface -[![Linux Build](https://img.shields.io/travis/form-data/form-data/master.svg?label=linux:4.x-12.x)](https://travis-ci.org/form-data/form-data) -[![MacOS Build](https://img.shields.io/travis/form-data/form-data/master.svg?label=macos:4.x-12.x)](https://travis-ci.org/form-data/form-data) -[![Windows Build](https://img.shields.io/travis/form-data/form-data/master.svg?label=windows:4.x-12.x)](https://travis-ci.org/form-data/form-data) +[![Linux Build](https://img.shields.io/travis/form-data/form-data/master.svg?label=linux:6.x-12.x)](https://travis-ci.org/form-data/form-data) +[![MacOS Build](https://img.shields.io/travis/form-data/form-data/master.svg?label=macos:6.x-12.x)](https://travis-ci.org/form-data/form-data) +[![Windows Build](https://img.shields.io/travis/form-data/form-data/master.svg?label=windows:6.x-12.x)](https://travis-ci.org/form-data/form-data) [![Coverage Status](https://img.shields.io/coveralls/form-data/form-data/master.svg?label=code+coverage)](https://coveralls.io/github/form-data/form-data?branch=master) [![Dependency Status](https://img.shields.io/david/form-data/form-data.svg)](https://david-dm.org/form-data/form-data) @@ -344,6 +344,7 @@ axios.post('http://example.com', form, { - ```getLengthSync()``` method DOESN'T calculate length for streams, use ```knownLength``` options as workaround. - Starting version `2.x` FormData has dropped support for `node@0.10.x`. +- Starting version `3.x` FormData has dropped support for `node@4.x`. ## License From 714ac8b3d3b116650e8581551e923039f6ecefc7 Mon Sep 17 00:00:00 2001 From: Arun Kumar Mohan Date: Sat, 16 Nov 2019 19:11:51 -0500 Subject: [PATCH 35/36] Fix typo: ads -> adds --- Readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Readme.md b/Readme.md index e9195bd..8971f69 100644 --- a/Readme.md +++ b/Readme.md @@ -217,7 +217,7 @@ form.append( 'my_file', fs.createReadStream('/foo/bar.jpg'), {filename: 'bar.jpg ``` #### _Headers_ getHeaders( [**Headers** _userHeaders_] ) -This method ads the correct `content-type` header to the provided array of `userHeaders`. +This method adds the correct `content-type` header to the provided array of `userHeaders`. #### _String_ getBoundary() Return the boundary of the formData. A boundary consists of 26 `-` followed by 24 numbers From 55d90ce4a4c22b0ea0647991d85cb946dfb7395b Mon Sep 17 00:00:00 2001 From: Charlie Saunders Date: Tue, 2 Jun 2020 02:34:49 -0400 Subject: [PATCH 36/36] feat: add setBoundary method --- Readme.md | 7 ++++++- index.d.ts | 1 + lib/form_data.js | 4 ++++ test/integration/test-set-boundary.js | 23 +++++++++++++++++++++++ 4 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 test/integration/test-set-boundary.js diff --git a/Readme.md b/Readme.md index 8971f69..7bfea3e 100644 --- a/Readme.md +++ b/Readme.md @@ -189,6 +189,7 @@ form.submit({ - [_Void_ append( **String** _field_, **Mixed** _value_ [, **Mixed** _options_] )](https://github.com/form-data/form-data#void-append-string-field-mixed-value--mixed-options-). - [_Headers_ getHeaders( [**Headers** _userHeaders_] )](https://github.com/form-data/form-data#array-getheaders-array-userheaders-) - [_String_ getBoundary()](https://github.com/form-data/form-data#string-getboundary) +- [_Void_ setBoundary()](https://github.com/form-data/form-data#void-setboundary) - [_Buffer_ getBuffer()](https://github.com/form-data/form-data#buffer-getbuffer) - [_Integer_ getLengthSync()](https://github.com/form-data/form-data#integer-getlengthsync) - [_Integer_ getLength( **function** _callback_ )](https://github.com/form-data/form-data#integer-getlength-function-callback-) @@ -220,11 +221,15 @@ form.append( 'my_file', fs.createReadStream('/foo/bar.jpg'), {filename: 'bar.jpg This method adds the correct `content-type` header to the provided array of `userHeaders`. #### _String_ getBoundary() -Return the boundary of the formData. A boundary consists of 26 `-` followed by 24 numbers +Return the boundary of the formData. By default, the boundary consists of 26 `-` followed by 24 numbers for example: ```javascript --------------------------515890814546601021194782 ``` + +#### _Void_ setBoundary(String _boundary_) +Set the boundary string, overriding the default behavior described above. + _Note: The boundary must be unique and may not appear in the data._ #### _Buffer_ getBuffer() diff --git a/index.d.ts b/index.d.ts index 6e52045..295e9e9 100644 --- a/index.d.ts +++ b/index.d.ts @@ -36,6 +36,7 @@ declare class FormData extends stream.Readable { callback?: (error: Error | null, response: http.IncomingMessage) => void ): http.ClientRequest; getBuffer(): Buffer; + setBoundary(boundary: string): void; getBoundary(): string; getLength(callback: (err: Error | null, length: number) => void): void; getLengthSync(): number; diff --git a/lib/form_data.js b/lib/form_data.js index ddfae2e..cf836b0 100644 --- a/lib/form_data.js +++ b/lib/form_data.js @@ -305,6 +305,10 @@ FormData.prototype.getHeaders = function(userHeaders) { return formHeaders; }; +FormData.prototype.setBoundary = function(boundary) { + this._boundary = boundary; +}; + FormData.prototype.getBoundary = function() { if (!this._boundary) { this._generateBoundary(); diff --git a/test/integration/test-set-boundary.js b/test/integration/test-set-boundary.js new file mode 100644 index 0000000..f1d8820 --- /dev/null +++ b/test/integration/test-set-boundary.js @@ -0,0 +1,23 @@ +var common = require('../common'); +var assert = common.assert; + +var FormData = require(common.dir.lib + '/form_data'); + +(function testSetBoundary() { + var userBoundary = '---something'; + var form = new FormData(); + form.setBoundary(userBoundary); + + assert.equal(form.getBoundary(), userBoundary); +})(); + +(function testUniqueBoundaryPerFormAfterSet() { + var userBoundary = '---something'; + var formA = new FormData(); + formA.setBoundary(userBoundary); + + var formB = new FormData(); + + assert.equal(formA.getBoundary(), userBoundary); + assert.notEqual(formA.getBoundary(), formB.getBoundary()); +})();