Skip to content

Commit 426ba9a

Browse files
committed
[eslint] use a shared config
1 parent 2094191 commit 426ba9a

35 files changed

+316
-276
lines changed

.eslintrc

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
{
2+
"root": true,
3+
4+
"extends": "@ljharb",
5+
26
"rules": {
37
"indent": [2, 2, {"SwitchCase": 2}],
4-
"curly": [2, "multi-line"],
8+
// "curly": [2, "multi-line"],
59
"handle-callback-err": [2, "^err"],
610
"valid-jsdoc": [2, {
711
"requireReturn": false,
@@ -27,36 +31,15 @@
2731
"no-new": 0,
2832
"eqeqeq": 0,
2933

30-
"object-curly-spacing": [2, "always"],
31-
"linebreak-style": [2, "unix"],
32-
"no-mixed-requires": 0,
33-
"no-console": 2,
34-
"space-before-function-paren": [2, { "anonymous": "always", "named": "never", "asyncArrow": "always" }],
35-
"space-before-blocks": [2, { "functions": "always", "keywords": "always", "classes": "always" }],
36-
"space-in-parens": [2, "never"],
37-
"space-infix-ops": [2],
38-
"space-unary-ops": [2, { "words": true, "nonwords": false }],
39-
"spaced-comment": [2, "always", { "markers": ["!"] }],
40-
"no-irregular-whitespace": 2,
41-
"no-mixed-spaces-and-tabs": [2, false],
42-
"semi-spacing": [2, { "before": false, "after": true }],
43-
"no-multi-spaces": [2, { "ignoreEOLComments": false }],
44-
"no-regex-spaces": [2],
45-
"no-spaced-func": [2],
46-
"no-trailing-spaces": [2, {
47-
"skipBlankLines": false,
48-
"ignoreComments": false
49-
}],
50-
"no-whitespace-before-property": [2],
51-
// "semi-style": [2, "last"],
52-
"key-spacing": [2, {
53-
"beforeColon": false,
54-
"afterColon": true,
55-
"mode": "strict"
56-
}],
57-
"quotes": [2, "single", "avoid-escape"],
58-
"semi": [2, "always"],
59-
"eol-last": [2, "always"],
34+
"no-underscore-dangle": 0,
35+
"camelcase": 0,
36+
"func-style": 0,
37+
"max-lines": 0,
38+
"id-length": 1,
39+
"multiline-comment-style": 0,
40+
41+
"no-plusplus": 1,
42+
"no-caller": 1,
6043
},
6144

6245
"env": {

lib/browser.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1+
'use strict';
2+
13
/* eslint-env browser */
2-
module.exports = typeof self == 'object' ? self.FormData : window.FormData;
4+
module.exports = typeof self === 'object' ? self.FormData : window.FormData;

lib/form_data.js

Lines changed: 70 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
'use strict';
2+
13
var CombinedStream = require('combined-stream');
24
var util = require('util');
35
var path = require('path');
@@ -12,19 +14,13 @@ var setToStringTag = require('es-set-tostringtag');
1214
var hasOwn = require('hasown');
1315
var populate = require('./populate.js');
1416

15-
// Public API
16-
module.exports = FormData;
17-
18-
// make it a Stream
19-
util.inherits(FormData, CombinedStream);
20-
2117
/**
2218
* Create readable "multipart/form-data" streams.
2319
* Can be used to submit forms
2420
* and file uploads to other web applications.
2521
*
2622
* @constructor
27-
* @param {Object} options - Properties to be added/overriden for FormData and CombinedStream
23+
* @param {object} options - Properties to be added/overriden for FormData and CombinedStream
2824
*/
2925
function FormData(options) {
3026
if (!(this instanceof FormData)) {
@@ -37,34 +33,39 @@ function FormData(options) {
3733

3834
CombinedStream.call(this);
3935

40-
options = options || {};
41-
for (var option in options) {
36+
options = options || {}; // eslint-disable-line no-param-reassign
37+
for (var option in options) { // eslint-disable-line no-restricted-syntax
4238
this[option] = options[option];
4339
}
4440
}
4541

42+
// make it a Stream
43+
util.inherits(FormData, CombinedStream);
44+
4645
FormData.LINE_BREAK = '\r\n';
4746
FormData.DEFAULT_CONTENT_TYPE = 'application/octet-stream';
4847

4948
FormData.prototype.append = function (field, value, options) {
50-
options = options || {};
49+
options = options || {}; // eslint-disable-line no-param-reassign
5150

5251
// allow filename as single option
53-
if (typeof options == 'string') {
54-
options = { filename: options };
52+
if (typeof options === 'string') {
53+
options = { filename: options }; // eslint-disable-line no-param-reassign
5554
}
5655

5756
var append = CombinedStream.prototype.append.bind(this);
5857

5958
// all that streamy business can't handle numbers
60-
if (typeof value == 'number') {
61-
value = '' + value;
59+
if (typeof value === 'number') {
60+
value = String(value); // eslint-disable-line no-param-reassign
6261
}
6362

6463
// https://github.com/felixge/node-form-data/issues/38
6564
if (Array.isArray(value)) {
66-
// Please convert your array into string
67-
// the way web server expects it
65+
/*
66+
* Please convert your array into string
67+
* the way web server expects it
68+
*/
6869
this._error(new Error('Arrays are not supported.'));
6970
return;
7071
}
@@ -83,12 +84,14 @@ FormData.prototype.append = function (field, value, options) {
8384
FormData.prototype._trackLength = function (header, value, options) {
8485
var valueLength = 0;
8586

86-
// used w/ getLengthSync(), when length is known.
87-
// e.g. for streaming directly from a remote server,
88-
// w/ a known file a size, and not wanting to wait for
89-
// incoming file to finish to get its size.
87+
/*
88+
* used w/ getLengthSync(), when length is known.
89+
* e.g. for streaming directly from a remote server,
90+
* w/ a known file a size, and not wanting to wait for
91+
* incoming file to finish to get its size.
92+
*/
9093
if (options.knownLength != null) {
91-
valueLength += +options.knownLength;
94+
valueLength += Number(options.knownLength);
9295
} else if (Buffer.isBuffer(value)) {
9396
valueLength = value.length;
9497
} else if (typeof value === 'string') {
@@ -98,9 +101,7 @@ FormData.prototype._trackLength = function (header, value, options) {
98101
this._valueLength += valueLength;
99102

100103
// @check why add CRLF? does this account for custom/multiple CRLFs?
101-
this._overheadLength +=
102-
Buffer.byteLength(header) +
103-
FormData.LINE_BREAK.length;
104+
this._overheadLength += Buffer.byteLength(header) + FormData.LINE_BREAK.length;
104105

105106
// empty or either doesn't have path or not an http response or not a stream
106107
if (!value || (!value.path && !(value.readable && hasOwn(value, 'httpVersion')) && !(value instanceof Stream))) {
@@ -126,7 +127,7 @@ FormData.prototype._lengthRetriever = function (value, callback) {
126127
// when end specified
127128
// no need to calculate range
128129
// inclusive, starts with 0
129-
callback(null, value.end + 1 - (value.start ? value.start : 0));
130+
callback(null, value.end + 1 - (value.start ? value.start : 0)); // eslint-disable-line callback-return
130131

131132
// not that fast snoopy
132133
} else {
@@ -145,28 +146,30 @@ FormData.prototype._lengthRetriever = function (value, callback) {
145146

146147
// or http response
147148
} else if (hasOwn(value, 'httpVersion')) {
148-
callback(null, +value.headers['content-length']);
149+
callback(null, Number(value.headers['content-length'])); // eslint-disable-line callback-return
149150

150151
// or request stream http://github.com/mikeal/request
151152
} else if (hasOwn(value, 'httpModule')) {
152153
// wait till response come back
153154
value.on('response', function (response) {
154155
value.pause();
155-
callback(null, +response.headers['content-length']);
156+
callback(null, Number(response.headers['content-length']));
156157
});
157158
value.resume();
158159

159160
// something else
160161
} else {
161-
callback('Unknown stream');
162+
callback('Unknown stream'); // eslint-disable-line callback-return
162163
}
163164
};
164165

165166
FormData.prototype._multiPartHeader = function (field, value, options) {
166-
// custom header specified (as string)?
167-
// it becomes responsible for boundary
168-
// (e.g. to handle extra CRLFs on .NET servers)
169-
if (typeof options.header == 'string') {
167+
/*
168+
* custom header specified (as string)?
169+
* it becomes responsible for boundary
170+
* (e.g. to handle extra CRLFs on .NET servers)
171+
*/
172+
if (typeof options.header === 'string') {
170173
return options.header;
171174
}
172175

@@ -182,18 +185,18 @@ FormData.prototype._multiPartHeader = function (field, value, options) {
182185
};
183186

184187
// allow custom headers.
185-
if (typeof options.header == 'object') {
188+
if (typeof options.header === 'object') {
186189
populate(headers, options.header);
187190
}
188191

189192
var header;
190-
for (var prop in headers) {
193+
for (var prop in headers) { // eslint-disable-line no-restricted-syntax
191194
if (hasOwn(headers, prop)) {
192195
header = headers[prop];
193196

194197
// skip nullish headers.
195198
if (header == null) {
196-
continue;
199+
continue; // eslint-disable-line no-restricted-syntax, no-continue
197200
}
198201

199202
// convert all headers to arrays.
@@ -211,16 +214,18 @@ FormData.prototype._multiPartHeader = function (field, value, options) {
211214
return '--' + this.getBoundary() + FormData.LINE_BREAK + contents + FormData.LINE_BREAK;
212215
};
213216

214-
FormData.prototype._getContentDisposition = function (value, options) {
217+
FormData.prototype._getContentDisposition = function (value, options) { // eslint-disable-line consistent-return
215218
var filename;
216219

217220
if (typeof options.filepath === 'string') {
218221
// custom filepath for relative paths
219222
filename = path.normalize(options.filepath).replace(/\\/g, '/');
220223
} else if (options.filename || value.name || value.path) {
221-
// custom filename take precedence
222-
// formidable and the browser add a name property
223-
// fs- and request- streams have path property
224+
/*
225+
* custom filename take precedence
226+
* formidable and the browser add a name property
227+
* fs- and request- streams have path property
228+
*/
224229
filename = path.basename(options.filename || value.name || value.path);
225230
} else if (value.readable && hasOwn(value, 'httpVersion')) {
226231
// or try http response
@@ -257,7 +262,7 @@ FormData.prototype._getContentType = function (value, options) {
257262
}
258263

259264
// fallback to the default content type if `value` is not simple value
260-
if (!contentType && typeof value == 'object') {
265+
if (!contentType && typeof value === 'object') {
261266
contentType = FormData.DEFAULT_CONTENT_TYPE;
262267
}
263268

@@ -268,7 +273,7 @@ FormData.prototype._multiPartFooter = function () {
268273
return function (next) {
269274
var footer = FormData.LINE_BREAK;
270275

271-
var lastPart = (this._streams.length === 0);
276+
var lastPart = this._streams.length === 0;
272277
if (lastPart) {
273278
footer += this._lastBoundary();
274279
}
@@ -287,7 +292,7 @@ FormData.prototype.getHeaders = function (userHeaders) {
287292
'content-type': 'multipart/form-data; boundary=' + this.getBoundary()
288293
};
289294

290-
for (header in userHeaders) {
295+
for (header in userHeaders) { // eslint-disable-line no-restricted-syntax
291296
if (hasOwn(userHeaders, header)) {
292297
formHeaders[header.toLowerCase()] = userHeaders[header];
293298
}
@@ -309,7 +314,7 @@ FormData.prototype.getBoundary = function () {
309314
};
310315

311316
FormData.prototype.getBuffer = function () {
312-
var dataBuffer = new Buffer.alloc(0);
317+
var dataBuffer = new Buffer.alloc(0); // eslint-disable-line new-cap
313318
var boundary = this.getBoundary();
314319

315320
// Create the form content. Add Line breaks to the end of data.
@@ -335,6 +340,7 @@ FormData.prototype.getBuffer = function () {
335340

336341
FormData.prototype._generateBoundary = function () {
337342
// This generates a 50 character boundary similar to those used by Firefox.
343+
338344
// They are optimized for boyer-moore parsing.
339345
var boundary = '--------------------------';
340346
for (var i = 0; i < 24; i++) {
@@ -345,22 +351,22 @@ FormData.prototype._generateBoundary = function () {
345351
};
346352

347353
// Note: getLengthSync DOESN'T calculate streams length
348-
// As workaround one can calculate file size manually
349-
// and add it as knownLength option
354+
// As workaround one can calculate file size manually and add it as knownLength option
350355
FormData.prototype.getLengthSync = function () {
351356
var knownLength = this._overheadLength + this._valueLength;
352357

353-
// Don't get confused, there are 3 "internal" streams for each keyval pair
354-
// so it basically checks if there is any value added to the form
358+
// Don't get confused, there are 3 "internal" streams for each keyval pair so it basically checks if there is any value added to the form
355359
if (this._streams.length) {
356360
knownLength += this._lastBoundary().length;
357361
}
358362

359363
// https://github.com/form-data/form-data/issues/40
360364
if (!this.hasKnownLength()) {
361-
// Some async length retrievers are present
362-
// therefore synchronous length calculation is false.
363-
// Please use getLength(callback) to get proper length
365+
/*
366+
* Some async length retrievers are present
367+
* therefore synchronous length calculation is false.
368+
* Please use getLength(callback) to get proper length
369+
*/
364370
this._error(new Error('Cannot calculate proper length in synchronous way.'));
365371
}
366372

@@ -407,15 +413,14 @@ FormData.prototype.getLength = function (cb) {
407413
};
408414

409415
FormData.prototype.submit = function (params, cb) {
410-
var request
411-
, options
412-
, defaults = { method: 'post' }
413-
;
414-
415-
// parse provided url if it's string
416-
// or treat it as options object
417-
if (typeof params == 'string') {
418-
params = parseUrl(params);
416+
var request;
417+
var options;
418+
var defaults = { method: 'post' };
419+
420+
// parse provided url if it's string or treat it as options object
421+
if (typeof params === 'string') {
422+
params = parseUrl(params); // eslint-disable-line no-param-reassign
423+
/* eslint sort-keys: 0 */
419424
options = populate({
420425
port: params.port,
421426
path: params.pathname,
@@ -426,15 +431,15 @@ FormData.prototype.submit = function (params, cb) {
426431
options = populate(params, defaults);
427432
// if no port provided use default one
428433
if (!options.port) {
429-
options.port = options.protocol == 'https:' ? 443 : 80;
434+
options.port = options.protocol === 'https:' ? 443 : 80;
430435
}
431436
}
432437

433438
// put that good code in getHeaders to some use
434439
options.headers = this.getHeaders(params.headers);
435440

436441
// https if specified, fallback to http in any other case
437-
if (options.protocol == 'https:') {
442+
if (options.protocol === 'https:') {
438443
request = https.request(options);
439444
} else {
440445
request = http.request(options);
@@ -460,7 +465,7 @@ FormData.prototype.submit = function (params, cb) {
460465
request.removeListener('error', callback);
461466
request.removeListener('response', onResponse);
462467

463-
return cb.call(this, error, responce);
468+
return cb.call(this, error, responce); // eslint-disable-line no-invalid-this
464469
};
465470

466471
onResponse = callback.bind(this, null);
@@ -485,3 +490,6 @@ FormData.prototype.toString = function () {
485490
return '[object FormData]';
486491
};
487492
setToStringTag(FormData, 'FormData');
493+
494+
// Public API
495+
module.exports = FormData;

0 commit comments

Comments
 (0)