Skip to content

Commit

Permalink
Merge pull request #140 from alexindigo/master
Browse files Browse the repository at this point in the history
House cleaning. Added `pre-commit`.
  • Loading branch information
alexindigo committed Jul 30, 2015
2 parents 6e6a289 + a5ac58c commit c174f1b
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 37 deletions.
14 changes: 9 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
*.un~
*.sublime-*
/node_modules/*
/test/tmp
/.idea
*.iml
*.sublime-*
*.un~

.idea

sftp-config.json

node_modules/
test/tmp/
21 changes: 12 additions & 9 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -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/
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
40 changes: 30 additions & 10 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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');
Expand All @@ -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();
});

```
Expand Down Expand Up @@ -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);
Expand All @@ -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.
12 changes: 8 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
{
"author": "Felix Geisendörfer <felix@debuggable.com> (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"
},
Expand All @@ -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"
}
}
30 changes: 21 additions & 9 deletions test/integration/test-ranged-filestream.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});

Expand Down Expand Up @@ -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();
});

});

0 comments on commit c174f1b

Please sign in to comment.