Skip to content

Commit 1607714

Browse files
committed
Housecleaning, preparing for the release
1 parent 7837f7e commit 1607714

26 files changed

+199
-174
lines changed

.editorconfig

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# editorconfig.org
2+
root = true
3+
4+
[*]
5+
indent_style = space
6+
indent_size = 2
7+
end_of_line = lf
8+
charset = utf-8
9+
trim_trailing_whitespace = true
10+
insert_final_newline = true

.eslintrc

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"rules": {
3+
"indent": [2, 2],
4+
"quotes": [2, "single"],
5+
"linebreak-style": [2, "unix"],
6+
"semi": [2, "always"],
7+
"curly": [2, "all"],
8+
"brace-style": [2, "1tbs", { "allowSingleLine": true }],
9+
"handle-callback-err": [2, "^err"],
10+
"valid-jsdoc": [2, {
11+
"requireReturn": false,
12+
"requireReturnDescription": false,
13+
"prefer": {
14+
"return": "returns"
15+
}
16+
}],
17+
"key-spacing": 0,
18+
"strict": 0,
19+
"no-underscore-dangle": 0,
20+
"no-use-before-define": 0,
21+
"dot-notation": 0,
22+
"eol-last": 0,
23+
"no-new": 0,
24+
"semi-spacing": 0,
25+
"no-multi-spaces": 0,
26+
"eqeqeq": 0,
27+
"no-mixed-requires": 0
28+
},
29+
"env": {
30+
"node": true,
31+
"browser": true
32+
}
33+
}

Makefile

-7
This file was deleted.

lib/form_data.js

+14-18
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ FormData.prototype.append = function(field, value, options) {
3434

3535
// all that streamy business can't handle numbers
3636
if (typeof value == 'number') {
37-
value = ''+value;
37+
value = '' + value;
3838
}
3939

4040
// https://github.com/felixge/node-form-data/issues/38
@@ -84,8 +84,7 @@ FormData.prototype._trackLength = function(header, value, options) {
8484
}
8585

8686
// no need to bother with the length
87-
if (!options.knownLength)
88-
{
87+
if (!options.knownLength) {
8988
this._lengthRetrievers.push(function(next) {
9089

9190
if (value.hasOwnProperty('fd')) {
@@ -102,7 +101,7 @@ FormData.prototype._trackLength = function(header, value, options) {
102101
// when end specified
103102
// no need to calculate range
104103
// inclusive, starts with 0
105-
next(null, value.end+1 - (value.start ? value.start : 0));
104+
next(null, value.end + 1 - (value.start ? value.start : 0));
106105

107106
// not that fast snoopy
108107
} else {
@@ -230,14 +229,14 @@ FormData.prototype.getHeaders = function(userHeaders) {
230229
};
231230

232231
FormData.prototype.getCustomHeaders = function(contentType) {
233-
contentType = contentType ? contentType : 'multipart/form-data';
232+
contentType = contentType ? contentType : 'multipart/form-data';
234233

235-
var formHeaders = {
236-
'content-type': contentType + '; boundary=' + this.getBoundary(),
237-
'content-length': this.getLengthSync()
238-
};
234+
var formHeaders = {
235+
'content-type': contentType + '; boundary=' + this.getBoundary(),
236+
'content-length': this.getLengthSync()
237+
};
239238

240-
return formHeaders;
239+
return formHeaders;
241240
};
242241

243242
FormData.prototype.getBoundary = function() {
@@ -318,16 +317,17 @@ FormData.prototype.submit = function(params, cb) {
318317
// parse provided url if it's string
319318
// or treat it as options object
320319
if (typeof params == 'string') {
321-
params = parseUrl(params);
322320

321+
params = parseUrl(params);
323322
options = populate({
324323
port: params.port,
325324
path: params.pathname,
326325
host: params.hostname
327326
}, defaults);
328-
}
329-
else // use custom params
330-
{
327+
328+
// use custom params
329+
} else {
330+
331331
options = populate(params, defaults);
332332
// if no port provided use default one
333333
if (!options.port) {
@@ -375,10 +375,6 @@ FormData.prototype._error = function(err) {
375375
}
376376
};
377377

378-
/*
379-
* Santa's little helpers
380-
*/
381-
382378
// populates missing values
383379
function populate(dst, src) {
384380
for (var prop in src) {

package.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010
"main": "./lib/form_data",
1111
"browser": "./lib/browser",
1212
"scripts": {
13-
"test": "./test/run.js"
13+
"test": "./test/run.js",
14+
"lint": "eslint -c .eslintrc {lib/*,test/**/*}.js"
1415
},
1516
"pre-commit": [
17+
"lint",
1618
"test"
1719
],
1820
"engines": {
@@ -25,6 +27,7 @@
2527
},
2628
"license": "MIT",
2729
"devDependencies": {
30+
"eslint": "^0.24.1",
2831
"fake": "^0.2.2",
2932
"far": "^0.0.7",
3033
"formidable": "^1.0.17",

test/common.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ var rootDir = path.join(__dirname, '..');
77
common.dir = {
88
lib: path.join(rootDir, '/lib'),
99
fixture: path.join(rootDir, '/test/fixture'),
10-
tmp: path.join(rootDir, '/test/tmp'),
10+
tmp: path.join(rootDir, '/test/tmp')
1111
};
1212

1313
common.assert = require('assert');

test/integration/test-custom-content-type.js

+28-27
Original file line numberDiff line numberDiff line change
@@ -31,40 +31,41 @@ var FIELDS = {
3131
var fieldsPassed = false;
3232

3333
var server = http.createServer(function(req, res) {
34-
var body = '';
35-
var boundry = req.headers['content-type'].split('boundary=').pop();
36-
37-
req.on('data', function (data) { body += data.toString('utf-8'); });
38-
req.on('end', function () {
39-
// Separate body into individual files/fields and remove leading and trailing content.
40-
var fields = body.split(boundry).slice(1, -1);
41-
42-
assert.ok(fields.length === 4);
43-
44-
assert.ok(fields[0].indexOf('name="no_type"') > -1);
45-
assert.ok(fields[0].indexOf('Content-Type"') === -1);
46-
47-
assert.ok(fields[1].indexOf('name="custom_type"') > -1);
48-
assert.ok(fields[1].indexOf('Content-Type: ' + FIELDS.custom_type.expectedType) > -1);
49-
50-
assert.ok(fields[2].indexOf('name="default_type"') > -1);
51-
assert.ok(fields[2].indexOf('Content-Type: ' + FIELDS.default_type.expectedType) > -1);
52-
53-
assert.ok(fields[3].indexOf('name="implicit_type"') > -1);
54-
assert.ok(fields[3].indexOf('Content-Type: ' + FIELDS.implicit_type.expectedType) > -1);
55-
56-
fieldsPassed = true;
57-
res.end();
58-
});
34+
var body = '';
35+
var boundry = req.headers['content-type'].split('boundary=').pop();
5936

37+
req.on('data', function (data) { body += data.toString('utf-8'); });
38+
req.on('end', function () {
39+
// Separate body into individual files/fields and remove leading and trailing content.
40+
var fields = body.split(boundry).slice(1, -1);
41+
42+
assert.ok(fields.length === 4);
43+
44+
assert.ok(fields[0].indexOf('name="no_type"') > -1);
45+
assert.ok(fields[0].indexOf('Content-Type"') === -1);
46+
47+
assert.ok(fields[1].indexOf('name="custom_type"') > -1);
48+
assert.ok(fields[1].indexOf('Content-Type: ' + FIELDS.custom_type.expectedType) > -1);
49+
50+
assert.ok(fields[2].indexOf('name="default_type"') > -1);
51+
assert.ok(fields[2].indexOf('Content-Type: ' + FIELDS.default_type.expectedType) > -1);
52+
53+
assert.ok(fields[3].indexOf('name="implicit_type"') > -1);
54+
assert.ok(fields[3].indexOf('Content-Type: ' + FIELDS.implicit_type.expectedType) > -1);
55+
56+
fieldsPassed = true;
57+
res.end();
58+
});
6059
});
6160

6261
server.listen(common.port, function() {
6362

6463
var form = new FormData();
65-
64+
6665
var field;
6766
for (var name in FIELDS) {
67+
if (!FIELDS.hasOwnProperty(name)) { continue; }
68+
6869
field = FIELDS[name];
6970
// important to append ReadStreams within the same tick
7071
if ((typeof field.value == 'function')) {
@@ -95,4 +96,4 @@ server.listen(common.port, function() {
9596

9697
process.on('exit', function() {
9798
assert.ok(fieldsPassed);
98-
});
99+
});

test/integration/test-custom-filename.js

+12-12
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,21 @@ var server = http.createServer(function(req, res) {
2222
var form = new IncomingForm({uploadDir: common.dir.tmp});
2323

2424
form.parse(req, function (err, fields, files) {
25-
assert(!err);
26-
27-
assert('my_file1' in files);
28-
assert.strictEqual(files['my_file1'].name, options.filename);
29-
assert.strictEqual(files['my_file1'].type, options.contentType);
25+
assert(!err);
3026

31-
assert('my_file2' in files);
32-
assert.strictEqual(files['my_file2'].name, options.filename);
33-
assert.strictEqual(files['my_file2'].type, mime.lookup(options.filename));
27+
assert('my_file1' in files);
28+
assert.strictEqual(files['my_file1'].name, options.filename);
29+
assert.strictEqual(files['my_file1'].type, options.contentType);
3430

35-
assert('my_file3' in files);
36-
assert.strictEqual(files['my_file3'].type, FormData.DEFAULT_CONTENT_TYPE);
31+
assert('my_file2' in files);
32+
assert.strictEqual(files['my_file2'].name, options.filename);
33+
assert.strictEqual(files['my_file2'].type, mime.lookup(options.filename));
3734

38-
res.writeHead(200);
39-
res.end('done');
35+
assert('my_file3' in files);
36+
assert.strictEqual(files['my_file3'].type, FormData.DEFAULT_CONTENT_TYPE);
37+
38+
res.writeHead(200);
39+
res.end('done');
4040
});
4141
});
4242

test/integration/test-custom-headers.js

-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ var expectedLength;
1717

1818

1919
var server = http.createServer(function(req, res) {
20-
var data = '';
21-
2220
assert.ok( typeof req.headers['content-length'] !== 'undefined' );
2321
assert.equal(req.headers['content-length'], expectedLength);
2422

test/integration/test-errors.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ var http = require('http');
1010

1111
var form = new FormData();
1212

13-
var callback = fake.callback(arguments.callee.name + '-onError-append');
13+
var callback = fake.callback('testAppendArray-onError-append');
1414
fake.expectAnytime(callback, ['Arrays are not supported.']);
1515

1616
form.on('error', function(err) {
17-
// workaroud for expectAnytime handling objects
17+
// workaround for expectAnytime handling objects
1818
callback(err.message);
1919
});
2020

@@ -49,7 +49,7 @@ var http = require('http');
4949
expectedLength += form._overheadLength + form._lastBoundary().length;
5050

5151

52-
var callback = fake.callback(arguments.callee.name + '-onError-getLengthSync');
52+
var callback = fake.callback('testGetLengthSync-onError-getLengthSync');
5353
fake.expectAnytime(callback, ['Cannot calculate proper length in synchronous way.']);
5454

5555
form.on('error', function(err) {

test/integration/test-form-get-length-sync.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
var common = require('../common');
22
var assert = common.assert;
33
var FormData = require(common.dir.lib + '/form_data');
4-
var fake = require('fake').create();
54
var fs = require('fs');
65

76
(function testGetLengthSync() {
@@ -25,7 +24,7 @@ var fs = require('fs');
2524

2625
fields.forEach(function(field) {
2726
form.append(field.name, field.value);
28-
expectedLength += (''+field.value).length;
27+
expectedLength += ('' + field.value).length;
2928
});
3029

3130
expectedLength += form._overheadLength + form._lastBoundary().length;
@@ -65,7 +64,7 @@ var fs = require('fs');
6564
var stat = fs.statSync(field.value.path);
6665
expectedLength += stat.size;
6766
} else {
68-
expectedLength += (''+field.value).length;
67+
expectedLength += ('' + field.value).length;
6968
}
7069
});
7170
expectedLength += form._overheadLength + form._lastBoundary().length;

test/integration/test-form-get-length.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ var fs = require('fs');
66

77
(function testEmptyForm() {
88
var form = new FormData();
9-
var callback = fake.callback(arguments.callee.name + '-getLength');
9+
var callback = fake.callback('testEmptyForm-getLength');
1010
var calls = fake.expectAnytime(callback, [null, 0]).calls;
1111

1212
form.getLength(callback);
@@ -21,7 +21,7 @@ var fs = require('fs');
2121

2222
var form = new FormData();
2323
form.append(FIELD, VALUE);
24-
var callback = fake.callback(arguments.callee.name + '-getLength');
24+
var callback = fake.callback('testUtf8String-getLength');
2525

2626
var expectedLength =
2727
form._overheadLength +
@@ -38,7 +38,7 @@ var fs = require('fs');
3838

3939
var form = new FormData();
4040
form.append(FIELD, VALUE);
41-
var callback = fake.callback(arguments.callee.name + '-getLength');
41+
var callback = fake.callback('testBuffer-getLength');
4242

4343
var expectedLength =
4444
form._overheadLength +
@@ -54,20 +54,20 @@ var fs = require('fs');
5454
var fields = [
5555
{
5656
name: 'my_field',
57-
value: 'Test 123',
57+
value: 'Test 123'
5858
},
5959
{
6060
name: 'my_image',
61-
value: fs.createReadStream(common.dir.fixture + '/unicycle.jpg'),
61+
value: fs.createReadStream(common.dir.fixture + '/unicycle.jpg')
6262
},
6363
{
6464
name: 'my_buffer',
65-
value: new Buffer('123'),
65+
value: new Buffer('123')
6666
},
6767
{
6868
name: 'my_txt',
69-
value: fs.createReadStream(common.dir.fixture + '/veggies.txt'),
70-
},
69+
value: fs.createReadStream(common.dir.fixture + '/veggies.txt')
70+
}
7171
];
7272

7373
var form = new FormData();
@@ -85,7 +85,7 @@ var fs = require('fs');
8585

8686
expectedLength += form._overheadLength + form._lastBoundary().length;
8787

88-
var callback = fake.callback(arguments.callee.name + '-getLength');
88+
var callback = fake.callback('testStringFileBufferFile-getLength');
8989
fake.expectAnytime(callback, [null, expectedLength]);
9090
form.getLength(callback);
9191
})();

0 commit comments

Comments
 (0)