Skip to content

Commit 81ab41b

Browse files
committed
[Refactor] use hasown
1 parent c4bbb13 commit 81ab41b

6 files changed

Lines changed: 24 additions & 17 deletions

File tree

lib/form_data.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ var Stream = require('stream').Stream;
99
var mime = require('mime-types');
1010
var asynckit = require('asynckit');
1111
var setToStringTag = require('es-set-tostringtag');
12+
var hasOwn = require('hasown');
1213
var populate = require('./populate.js');
1314

1415
// Public API
@@ -103,7 +104,7 @@ FormData.prototype._trackLength = function(header, value, options) {
103104
FormData.LINE_BREAK.length;
104105

105106
// empty or either doesn't have path or not an http response or not a stream
106-
if (!value || ( !value.path && !(value.readable && Object.prototype.hasOwnProperty.call(value, 'httpVersion')) && !(value instanceof Stream))) {
107+
if (!value || ( !value.path && !(value.readable && hasOwn(value, 'httpVersion')) && !(value instanceof Stream))) {
107108
return;
108109
}
109110

@@ -114,7 +115,7 @@ FormData.prototype._trackLength = function(header, value, options) {
114115
};
115116

116117
FormData.prototype._lengthRetriever = function(value, callback) {
117-
if (Object.prototype.hasOwnProperty.call(value, 'fd')) {
118+
if (hasOwn(value, 'fd')) {
118119

119120
// take read range into a account
120121
// `end` = Infinity –> read file till the end
@@ -149,11 +150,11 @@ FormData.prototype._lengthRetriever = function(value, callback) {
149150
}
150151

151152
// or http response
152-
} else if (Object.prototype.hasOwnProperty.call(value, 'httpVersion')) {
153+
} else if (hasOwn(value, 'httpVersion')) {
153154
callback(null, +value.headers['content-length']);
154155

155156
// or request stream http://github.com/mikeal/request
156-
} else if (Object.prototype.hasOwnProperty.call(value, 'httpModule')) {
157+
} else if (hasOwn(value, 'httpModule')) {
157158
// wait till response come back
158159
value.on('response', function(response) {
159160
value.pause();
@@ -193,7 +194,7 @@ FormData.prototype._multiPartHeader = function(field, value, options) {
193194

194195
var header;
195196
for (var prop in headers) {
196-
if (Object.prototype.hasOwnProperty.call(headers, prop)) {
197+
if (hasOwn(headers, prop)) {
197198
header = headers[prop];
198199

199200
// skip nullish headers.
@@ -230,7 +231,7 @@ FormData.prototype._getContentDisposition = function(value, options) {
230231
// formidable and the browser add a name property
231232
// fs- and request- streams have path property
232233
filename = path.basename(options.filename || value.name || value.path);
233-
} else if (value.readable && Object.prototype.hasOwnProperty.call(value, 'httpVersion')) {
234+
} else if (value.readable && hasOwn(value, 'httpVersion')) {
234235
// or try http response
235236
filename = path.basename(value.client._httpMessage.path || '');
236237
}
@@ -258,7 +259,7 @@ FormData.prototype._getContentType = function(value, options) {
258259
}
259260

260261
// or if it's http-reponse
261-
if (!contentType && value.readable && Object.prototype.hasOwnProperty.call(value, 'httpVersion')) {
262+
if (!contentType && value.readable && hasOwn(value, 'httpVersion')) {
262263
contentType = value.headers['content-type'];
263264
}
264265

@@ -299,7 +300,7 @@ FormData.prototype.getHeaders = function(userHeaders) {
299300
};
300301

301302
for (header in userHeaders) {
302-
if (Object.prototype.hasOwnProperty.call(userHeaders, header)) {
303+
if (hasOwn(userHeaders, header)) {
303304
formHeaders[header.toLowerCase()] = userHeaders[header];
304305
}
305306
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"asynckit": "^0.4.0",
4646
"combined-stream": "^1.0.8",
4747
"es-set-tostringtag": "^2.1.0",
48+
"hasown": "^2.0.2",
4849
"mime-types": "^2.1.12"
4950
},
5051
"devDependencies": {

test/common.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ var fake = require('fake');
55
var mime = require('mime-types');
66
var http = require('http');
77
var IncomingForm = require('formidable').IncomingForm;
8+
var hasOwn = require('hasown');
89

910
var common = module.exports;
1011

@@ -58,7 +59,7 @@ common.actions = {};
5859
common.actions.populateFields = function (form, fields) {
5960
var field;
6061
for (var name in fields) {
61-
if (Object.prototype.hasOwnProperty.call(fields, name)) {
62+
if (hasOwn(fields, name)) {
6263
field = fields[name];
6364
// important to append ReadStreams within the same tick
6465
if ((typeof field.value == 'function')) {

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ var assert = common.assert;
33
var http = require('http');
44
var mime = require('mime-types');
55
var fs = require('fs');
6+
var hasOwn = require('hasown');
7+
68
var FormData = require(common.dir.lib + '/form_data');
79

810
// wrap non simple values into function
@@ -68,7 +70,7 @@ server.listen(common.port, function() {
6870
var form = new FormData();
6971

7072
for (var name in FIELDS) {
71-
if (Object.prototype.hasOwnProperty.call(FIELDS, name)) {
73+
if (hasOwn(FIELDS, name)) {
7274
var field = FIELDS[name];
7375
// important to append ReadStreams within the same tick
7476
if ((typeof field.value == 'function')) {

test/integration/test-pipe.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ var request = require('request');
66
var fs = require('fs');
77
var FormData = require(common.dir.lib + '/form_data');
88
var IncomingForm = require('formidable').IncomingForm;
9+
var hasOwn = require('hasown');
910

1011
var remoteFile = 'http://localhost:' + common.staticPort + '/unicycle.jpg';
1112

@@ -50,7 +51,7 @@ server.listen(common.port, function() {
5051
var form = new FormData();
5152

5253
for (var name in FIELDS) {
53-
if (Object.prototype.hasOwnProperty.call(FIELDS, name)) {
54+
if (hasOwn(FIELDS, name)) {
5455
var field = FIELDS[name];
5556
// important to append ReadStreams within the same tick
5657
if ((typeof field.value == 'function')) {

test/integration/test-ranged-filestream.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ test ranged fs.createReadStream
33
re: https://github.com/felixge/node-form-data/issues/71
44
*/
55

6-
var common = require('../common');
7-
var assert = common.assert;
8-
var http = require('http');
9-
var fs = require('fs');
6+
var common = require('../common');
7+
var assert = common.assert;
8+
var http = require('http');
9+
var fs = require('fs');
10+
var hasOwn = require('hasown');
1011

11-
var FormData = require(common.dir.lib + '/form_data');
12+
var FormData = require(common.dir.lib + '/form_data');
1213
var IncomingForm = require('formidable').IncomingForm;
1314

1415
var testSubjects = {
@@ -80,7 +81,7 @@ server.listen(common.port, function() {
8081

8182
// add test subjects to the form
8283
for (name in testSubjects) {
83-
if (Object.prototype.hasOwnProperty.call(testSubjects, name)) {
84+
if (hasOwn(testSubjects, name)) {
8485
options = {encoding: 'utf8'};
8586

8687
if (testSubjects[name].start) { options.start = testSubjects[name].start; }

0 commit comments

Comments
 (0)