diff --git a/.gitignore b/.gitignore index 2167fd2..95f8267 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,10 @@ -*.un~ -*.sublime-* -/node_modules/* -/test/tmp -/.idea *.iml +*.sublime-* +*.un~ + +.idea + +sftp-config.json + +node_modules/ +test/tmp/ diff --git a/.npmignore b/.npmignore index d73717f..6836024 100644 --- a/.npmignore +++ b/.npmignore @@ -1,11 +1,14 @@ -*.un~ *.iml *.sublime-* -/node_modules/ -/test/ -/.idea -/.gitignore -/.npmignore -/.travis.yml -/Makefile -/sftp-config.json +*.un~ + +.idea +.gitignore +.npmignore +.travis.yml + +Makefile +sftp-config.json + +node_modules/ +test/ diff --git a/.travis.yml b/.travis.yml index 9ccb608..9bb15c8 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,3 +4,10 @@ node_js: - "0.10" - "0.12" - "iojs" +notifications: + webhooks: + urls: + - "https://webhooks.gitter.im/e/3569d7edff0d38f93cd7" + on_success: change + on_failure: always + on_start: false diff --git a/Readme.md b/Readme.md index 3772e13..4927732 100644 --- a/Readme.md +++ b/Readme.md @@ -1,8 +1,8 @@ -# Form-Data [![Build Status](https://travis-ci.org/felixge/node-form-data.png?branch=master)](https://travis-ci.org/felixge/node-form-data) [![Dependency Status](https://gemnasium.com/felixge/node-form-data.png)](https://gemnasium.com/felixge/node-form-data) +# Form-Data [![Join the chat at https://gitter.im/form-data/form-data](http://form-data.github.io/images/gitterbadge.svg)](https://gitter.im/form-data/form-data) [![Build Status](https://img.shields.io/travis/form-data/form-data/master.svg)](https://travis-ci.org/form-data/form-data) [![Dependency Status](https://img.shields.io/david/form-data/form-data.svg)](https://david-dm.org/form-data/form-data) -A module to create readable ```"multipart/form-data"``` streams. Can be used to submit forms and file uploads to other web applications. +A library to create readable ```"multipart/form-data"``` streams. Can be used to submit forms and file uploads to other web applications. -The API of this module is inspired by the [XMLHttpRequest-2 FormData Interface][xhr2-fd]. +The API of this library is inspired by the [XMLHttpRequest-2 FormData Interface][xhr2-fd]. [xhr2-fd]: http://dev.w3.org/2006/webapi/XMLHttpRequest-2/Overview.html#the-formdata-interface [streams2-thing]: http://nodejs.org/api/stream.html#stream_compatibility_with_older_node_versions @@ -43,7 +43,7 @@ http.request('http://nodejs.org/images/logo.png', function(response) { }); ``` -Or @mikeal's request stream: +Or @mikeal's [request](https://github.com/request/request) stream: ``` javascript var FormData = require('form-data'); @@ -61,7 +61,7 @@ In order to submit this form to a web application, call ```submit(url, [callback ``` javascript form.submit('http://example.org/', function(err, res) { // res – response object (http.IncomingMessage) // - res.resume(); // for node-0.10.x + res.resume(); }); ``` @@ -161,9 +161,33 @@ form.submit({ }); ``` +### Integration with other libraries + +#### Request + +Form submission using [request](https://github.com/request/request): + +```javascript +var formData = { + my_field: 'my_value', + my_file: fs.createReadStream(__dirname + '/unicycle.jpg'), +}; + +request.post({url:'http://service.com/upload', formData: formData}, function(err, httpResponse, body) { + if (err) { + return console.error('upload failed:', err); + } + console.log('Upload successful! Server responded with:', body); +}); +``` + +For more details see [request readme](https://github.com/request/request#multipartform-data-multipart-form-uploads). + +#### node-fetch + You can also submit a form using [node-fetch](https://github.com/bitinn/node-fetch): -``` javascript +```javascript var form = new FormData(); form.append('a', 1); @@ -181,10 +205,6 @@ fetch('http://example.com', { method: 'POST', body: form }) - ```getLengthSync()``` method DOESN'T calculate length for streams, use ```knownLength``` options as workaround. - If it feels like FormData hangs after submit and you're on ```node-0.10```, please check [Compatibility with Older Node Versions][streams2-thing] -## TODO - -- Add new streams (0.10) support and try really hard not to break it for 0.8.x. - ## License Form-Data is licensed under the MIT license. diff --git a/package.json b/package.json index 982af2b..1c4f70b 100644 --- a/package.json +++ b/package.json @@ -1,17 +1,20 @@ { "author": "Felix Geisendörfer (http://debuggable.com/)", "name": "form-data", - "description": "A module to create readable \"multipart/form-data\" streams. Can be used to submit forms and file uploads to other web applications.", - "version": "1.0.0-rc2", + "description": "A library to create readable \"multipart/form-data\" streams. Can be used to submit forms and file uploads to other web applications.", + "version": "1.0.0-rc3", "repository": { "type": "git", - "url": "git://github.com/felixge/node-form-data.git" + "url": "git://github.com/form-data/form-data.git" }, "main": "./lib/form_data", "browser": "./lib/browser", "scripts": { - "test": "node test/run.js" + "test": "./test/run.js" }, + "pre-commit": [ + "test" + ], "engines": { "node": ">= 0.10" }, @@ -25,6 +28,7 @@ "fake": "^0.2.2", "far": "^0.0.7", "formidable": "^1.0.17", + "pre-commit": "^1.0.10", "request": "^2.60.0" } } diff --git a/test/integration/test-ranged-filestream.js b/test/integration/test-ranged-filestream.js index 991309f..cd94d29 100644 --- a/test/integration/test-ranged-filestream.js +++ b/test/integration/test-ranged-filestream.js @@ -41,21 +41,27 @@ var server = http.createServer(function(req, res) { requestBodyLength += data.length; }); - var form = new IncomingForm({uploadDir: common.dir.tmp}); + req.on('end', function() { + // make sure total Content-Length is properly calculated + assert.equal(req.headers['content-length'], requestBodyLength); + // successfully accepted request and it's good + res.writeHead(200); + }); + var form = new IncomingForm({uploadDir: common.dir.tmp}); form.parse(req); form .on('file', function(name, file) { - - // make sure total Content-Length is properly calculated - assert.equal(req.headers['content-length'], requestBodyLength); // make sure chunks are the same size assert.equal(file.size, testSubjects[name].readSize); + // clean up tested subject + delete testSubjects[name]; }) - .on('end', function() { - res.writeHead(200); - res.end('done'); + .on('end', function() + { + // done here + res.end(); }); }); @@ -88,10 +94,16 @@ server.listen(common.port, function() { assert.strictEqual(res.statusCode, 200); + // wait for server to finish + res.on('end', function() + { + // check that all subjects were tested + assert.strictEqual(Object.keys(testSubjects).length, 0); + server.close(); + }); + // unstuck new streams res.resume(); - - server.close(); }); });