Skip to content

Commit 02f11bb

Browse files
authored
Merge pull request #2 from uformia/master
Pull updates from master
2 parents b1ef6e5 + cfcc465 commit 02f11bb

File tree

11 files changed

+96
-169
lines changed

11 files changed

+96
-169
lines changed

.travis.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
language: node_js
22
node_js:
3-
- 0.8
4-
- "0.10"
5-
- 0.11
3+
- 4
4+
- 6
5+
- 7

Readme.md

+34-130
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
# Formidable
22

3-
[![Build Status](https://secure.travis-ci.org/felixge/node-formidable.png?branch=master)](http://travis-ci.org/felixge/node-formidable)
3+
[![Build Status](https://travis-ci.org/felixge/node-formidable.svg?branch=master)](https://travis-ci.org/felixge/node-formidable)
44

55
## Purpose
66

7-
A node.js module for parsing form data, especially file uploads.
7+
A Node.js module for parsing form data, especially file uploads.
88

99
## Current status
1010

11+
**Maintainers Wanted:** Please see https://github.com/felixge/node-formidable/issues/412
12+
1113
This module was developed for [Transloadit](http://transloadit.com/), a service focused on uploading
1214
and encoding images and videos. It has been battle-tested against hundreds of GB of file uploads from
1315
a large variety of clients and is considered production-ready.
@@ -22,19 +24,12 @@ a large variety of clients and is considered production-ready.
2224

2325
## Installation
2426

25-
This is a low level package, and if you're using a high level framework such as Express, chances are it's already included in it. You can [read this discussion](http://stackoverflow.com/questions/11295554/how-to-disable-express-bodyparser-for-file-uploads-node-js) about how Formidable is integrated with Express.
26-
27-
Via [npm](http://github.com/isaacs/npm):
28-
```
29-
npm install formidable@latest
30-
```
31-
Manually:
32-
```
33-
git clone git://github.com/felixge/node-formidable.git formidable
34-
vim my.js
35-
# var formidable = require('./formidable');
27+
```sh
28+
npm i -S formidable
3629
```
3730

31+
This is a low-level package, and if you're using a high-level framework it may already be included. However, [Express v4](http://expressjs.com) does not include any multipart handling, nor does [body-parser](https://github.com/expressjs/body-parser).
32+
3833
Note: Formidable requires [gently](http://github.com/felixge/node-gently) to run the unit tests, but you won't need it for just using the library.
3934

4035
## Example
@@ -87,7 +82,7 @@ Sets encoding for incoming form fields.
8782
form.uploadDir = "/my/dir";
8883
```
8984
Sets the directory for placing file uploads in. You can move them later on using
90-
`fs.rename()`. The default is `os.tmpDir()`.
85+
`fs.rename()`. The default is `os.tmpdir()`.
9186

9287
```javascript
9388
form.keepExtensions = false;
@@ -204,35 +199,40 @@ If hash calculation was set, you can read the hex digest out of this var.
204199

205200

206201
#### 'progress'
202+
203+
Emitted after each incoming chunk of data that has been parsed. Can be used to roll your own progress bar.
204+
207205
```javascript
208206
form.on('progress', function(bytesReceived, bytesExpected) {
209207
});
210208
```
211-
Emitted after each incoming chunk of data that has been parsed. Can be used to roll your own progress bar.
212209

213210

214211

215212
#### 'field'
213+
214+
Emitted whenever a field / value pair has been received.
215+
216216
```javascript
217217
form.on('field', function(name, value) {
218218
});
219219
```
220220

221221
#### 'fileBegin'
222222

223-
Emitted whenever a field / value pair has been received.
223+
Emitted whenever a new file is detected in the upload stream. Use this event if
224+
you want to stream the file to somewhere else while buffering the upload on
225+
the file system.
226+
224227
```javascript
225228
form.on('fileBegin', function(name, file) {
226229
});
227230
```
228231

229232
#### 'file'
230233

231-
Emitted whenever a new file is detected in the upload stream. Use this even if
232-
you want to stream the file to somewhere else while buffering the upload on
233-
the file system.
234-
235234
Emitted whenever a field / file pair has been received. `file` is an instance of `File`.
235+
236236
```javascript
237237
form.on('file', function(name, file) {
238238
});
@@ -241,6 +241,7 @@ form.on('file', function(name, file) {
241241
#### 'error'
242242

243243
Emitted when there is an error processing the incoming form. A request that experiences an error is automatically paused, you will have to manually call `request.resume()` if you want the request to continue firing `'data'` events.
244+
244245
```javascript
245246
form.on('error', function(err) {
246247
});
@@ -266,7 +267,18 @@ Emitted when the entire request has been received, and all contained files have
266267

267268
## Changelog
268269

269-
### v1.0.14
270+
### v1.1.1 (2017-01-15)
271+
272+
* Fix DeprecationWarning about os.tmpDir() (Christian)
273+
* Update `buffer.write` order of arguments for Node 7 (Kornel Lesiński)
274+
* JSON Parser emits error events to the IncomingForm (alessio.montagnani)
275+
* Improved Content-Disposition parsing (Sebastien)
276+
* Access WriteStream of fs during runtime instead of include time (Jonas Amundsen)
277+
* Use built-in toString to convert buffer to hex (Charmander)
278+
* Add hash to json if present (Nick Stamas)
279+
* Add license to package.json (Simen Bekkhus)
280+
281+
### v1.0.14 (2013-05-03)
270282

271283
* Add failing hash tests. (Ben Trask)
272284
* Enable hash calculation again (Eugene Girshov)
@@ -298,120 +310,12 @@ Emitted when the entire request has been received, and all contained files have
298310
* Remove support for Node.js 0.4 & 0.6 (Andrew Kelley)
299311
* Documentation improvements (Sven Lito, Andre Azevedo)
300312
* Add support for application/octet-stream (Ion Lupascu, Chris Scribner)
301-
* Use os.tmpDir() to get tmp directory (Andrew Kelley)
313+
* Use os.tmpdir() to get tmp directory (Andrew Kelley)
302314
* Improve package.json (Andrew Kelley, Sven Lito)
303315
* Fix benchmark script (Andrew Kelley)
304316
* Fix scope issue in incoming_forms (Sven Lito)
305317
* Fix file handle leak on error (OrangeDog)
306318

307-
### v1.0.11
308-
309-
* Calculate checksums for incoming files (sreuter)
310-
* Add definition parameters to "IncomingForm" as an argument (Math-)
311-
312-
### v1.0.10
313-
314-
* Make parts to be proper Streams (Matt Robenolt)
315-
316-
### v1.0.9
317-
318-
* Emit progress when content length header parsed (Tim Koschützki)
319-
* Fix Readme syntax due to GitHub changes (goob)
320-
* Replace references to old 'sys' module in Readme with 'util' (Peter Sugihara)
321-
322-
### v1.0.8
323-
324-
* Strip potentially unsafe characters when using `keepExtensions: true`.
325-
* Switch to utest / urun for testing
326-
* Add travis build
327-
328-
### v1.0.7
329-
330-
* Remove file from package that was causing problems when installing on windows. (#102)
331-
* Fix typos in Readme (Jason Davies).
332-
333-
### v1.0.6
334-
335-
* Do not default to the default to the field name for file uploads where
336-
filename="".
337-
338-
### v1.0.5
339-
340-
* Support filename="" in multipart parts
341-
* Explain unexpected end() errors in parser better
342-
343-
**Note:** Starting with this version, formidable emits 'file' events for empty
344-
file input fields. Previously those were incorrectly emitted as regular file
345-
input fields with value = "".
346-
347-
### v1.0.4
348-
349-
* Detect a good default tmp directory regardless of platform. (#88)
350-
351-
### v1.0.3
352-
353-
* Fix problems with utf8 characters (#84) / semicolons in filenames (#58)
354-
* Small performance improvements
355-
* New test suite and fixture system
356-
357-
### v1.0.2
358-
359-
* Exclude node\_modules folder from git
360-
* Implement new `'aborted'` event
361-
* Fix files in example folder to work with recent node versions
362-
* Make gently a devDependency
363-
364-
[See Commits](https://github.com/felixge/node-formidable/compare/v1.0.1...v1.0.2)
365-
366-
### v1.0.1
367-
368-
* Fix package.json to refer to proper main directory. (#68, Dean Landolt)
369-
370-
[See Commits](https://github.com/felixge/node-formidable/compare/v1.0.0...v1.0.1)
371-
372-
### v1.0.0
373-
374-
* Add support for multipart boundaries that are quoted strings. (Jeff Craig)
375-
376-
This marks the beginning of development on version 2.0 which will include
377-
several architectural improvements.
378-
379-
[See Commits](https://github.com/felixge/node-formidable/compare/v0.9.11...v1.0.0)
380-
381-
### v0.9.11
382-
383-
* Emit `'progress'` event when receiving data, regardless of parsing it. (Tim Koschützki)
384-
* Use [W3C FileAPI Draft](http://dev.w3.org/2006/webapi/FileAPI/) properties for File class
385-
386-
**Important:** The old property names of the File class will be removed in a
387-
future release.
388-
389-
[See Commits](https://github.com/felixge/node-formidable/compare/v0.9.10...v0.9.11)
390-
391-
### Older releases
392-
393-
These releases were done before starting to maintain the above Changelog:
394-
395-
* [v0.9.10](https://github.com/felixge/node-formidable/compare/v0.9.9...v0.9.10)
396-
* [v0.9.9](https://github.com/felixge/node-formidable/compare/v0.9.8...v0.9.9)
397-
* [v0.9.8](https://github.com/felixge/node-formidable/compare/v0.9.7...v0.9.8)
398-
* [v0.9.7](https://github.com/felixge/node-formidable/compare/v0.9.6...v0.9.7)
399-
* [v0.9.6](https://github.com/felixge/node-formidable/compare/v0.9.5...v0.9.6)
400-
* [v0.9.5](https://github.com/felixge/node-formidable/compare/v0.9.4...v0.9.5)
401-
* [v0.9.4](https://github.com/felixge/node-formidable/compare/v0.9.3...v0.9.4)
402-
* [v0.9.3](https://github.com/felixge/node-formidable/compare/v0.9.2...v0.9.3)
403-
* [v0.9.2](https://github.com/felixge/node-formidable/compare/v0.9.1...v0.9.2)
404-
* [v0.9.1](https://github.com/felixge/node-formidable/compare/v0.9.0...v0.9.1)
405-
* [v0.9.0](https://github.com/felixge/node-formidable/compare/v0.8.0...v0.9.0)
406-
* [v0.9.0](https://github.com/felixge/node-formidable/compare/v0.8.0...v0.9.0)
407-
* [v0.9.0](https://github.com/felixge/node-formidable/compare/v0.8.0...v0.9.0)
408-
* [v0.9.0](https://github.com/felixge/node-formidable/compare/v0.8.0...v0.9.0)
409-
* [v0.9.0](https://github.com/felixge/node-formidable/compare/v0.8.0...v0.9.0)
410-
* [v0.9.0](https://github.com/felixge/node-formidable/compare/v0.8.0...v0.9.0)
411-
* [v0.9.0](https://github.com/felixge/node-formidable/compare/v0.8.0...v0.9.0)
412-
* [v0.9.0](https://github.com/felixge/node-formidable/compare/v0.8.0...v0.9.0)
413-
* [v0.1.0](https://github.com/felixge/node-formidable/commits/v0.1.0)
414-
415319
## License
416320

417321
Formidable is licensed under the MIT license.

benchmark/bench-multipart-parser.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ function createMultipartBuffer(boundary, size) {
5959
, tail = '\r\n--'+boundary+'--\r\n'
6060
, buffer = new Buffer(size);
6161

62-
buffer.write(head, 'ascii', 0);
63-
buffer.write(tail, 'ascii', buffer.length - tail.length);
62+
buffer.write(head, 0, 'ascii');
63+
buffer.write(tail, buffer.length - tail.length, 'ascii');
6464
return buffer;
6565
}
6666

example/post.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
require('../test/common');
1+
var common = require('../test/common');
22
var http = require('http'),
33
util = require('util'),
4-
formidable = require('formidable'),
4+
formidable = common.formidable,
5+
port = common.port,
56
server;
67

78
server = http.createServer(function(req, res) {
@@ -38,6 +39,6 @@ server = http.createServer(function(req, res) {
3839
res.end('404');
3940
}
4041
});
41-
server.listen(TEST_PORT);
42+
server.listen(port);
4243

43-
console.log('listening on http://localhost:'+TEST_PORT+'/');
44+
console.log('listening on http://localhost:'+port+'/');

example/upload.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
require('../test/common');
1+
var common = require('../test/common');
22
var http = require('http'),
33
util = require('util'),
4-
formidable = require('formidable'),
4+
os = require('os'),
5+
formidable = common.formidable,
6+
port = common.port,
57
server;
68

79
server = http.createServer(function(req, res) {
@@ -19,7 +21,7 @@ server = http.createServer(function(req, res) {
1921
files = [],
2022
fields = [];
2123

22-
form.uploadDir = TEST_TMP;
24+
form.uploadDir = os.tmpdir();
2325

2426
form
2527
.on('field', function(field, value) {
@@ -43,6 +45,6 @@ server = http.createServer(function(req, res) {
4345
res.end('404');
4446
}
4547
});
46-
server.listen(TEST_PORT);
48+
server.listen(port);
4749

48-
console.log('listening on http://localhost:'+TEST_PORT+'/');
50+
console.log('listening on http://localhost:'+port+'/');

lib/file.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
if (global.GENTLY) require = GENTLY.hijack(require);
22

33
var util = require('util'),
4-
WriteStream = require('fs').WriteStream,
4+
fs = require('fs'),
55
EventEmitter = require('events').EventEmitter,
66
crypto = require('crypto');
77

@@ -31,11 +31,11 @@ module.exports = File;
3131
util.inherits(File, EventEmitter);
3232

3333
File.prototype.open = function() {
34-
this._writeStream = new WriteStream(this.path);
34+
this._writeStream = new fs.WriteStream(this.path);
3535
};
3636

3737
File.prototype.toJSON = function() {
38-
return {
38+
var json = {
3939
size: this.size,
4040
path: this.path,
4141
name: this.name,
@@ -45,13 +45,22 @@ File.prototype.toJSON = function() {
4545
filename: this.filename,
4646
mime: this.mime
4747
};
48+
if (this.hash && this.hash != "") {
49+
json.hash = this.hash;
50+
}
51+
return json;
4852
};
4953

5054
File.prototype.write = function(buffer, cb) {
5155
var self = this;
5256
if (self.hash) {
5357
self.hash.update(buffer);
5458
}
59+
60+
if (this._writeStream.closed) {
61+
return cb();
62+
}
63+
5564
this._writeStream.write(buffer, function() {
5665
self.lastModifiedDate = new Date();
5766
self.size += buffer.length;

0 commit comments

Comments
 (0)