Skip to content

Commit 1a78b5d

Browse files
committed
[Refactor] use hasown
1 parent 11d9f73 commit 1a78b5d

6 files changed

Lines changed: 18 additions & 12 deletions

File tree

lib/form_data.js

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

1516
/**
@@ -103,7 +104,7 @@ FormData.prototype._trackLength = function (header, value, options) {
103104
this._overheadLength += Buffer.byteLength(header) + FormData.LINE_BREAK.length;
104105

105106
// empty or either doesn't have path or not an http response
106-
if (!value || (!value.path && !(value.readable && Object.prototype.hasOwnProperty.call(value, 'httpVersion')))) {
107+
if (!value || (!value.path && !(value.readable && hasOwn(value, 'httpVersion')))) {
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
/*
120121
* take read range into a account
@@ -153,11 +154,11 @@ FormData.prototype._lengthRetriever = function (value, callback) {
153154
}
154155

155156
// or http response
156-
} else if (Object.prototype.hasOwnProperty.call(value, 'httpVersion')) {
157+
} else if (hasOwn(value, 'httpVersion')) {
157158
callback(null, Number(value.headers['content-length']));
158159

159160
// or request stream http://github.com/mikeal/request
160-
} else if (Object.prototype.hasOwnProperty.call(value, 'httpModule')) {
161+
} else if (hasOwn(value, 'httpModule')) {
161162
// wait till response come back
162163
value.on('response', function (response) {
163164
value.pause();
@@ -199,7 +200,7 @@ FormData.prototype._multiPartHeader = function (field, value, options) {
199200

200201
var header;
201202
for (var prop in headers) { // eslint-disable-line no-restricted-syntax
202-
if (Object.prototype.hasOwnProperty.call(headers, prop)) {
203+
if (hasOwn(headers, prop)) {
203204
header = headers[prop];
204205

205206
// skip nullish headers.
@@ -236,7 +237,7 @@ FormData.prototype._getContentDisposition = function (value, options) {
236237
* fs- and request- streams have path property
237238
*/
238239
filename = path.basename(options.filename || value.name || value.path);
239-
} else if (value.readable && Object.prototype.hasOwnProperty.call(value, 'httpVersion')) {
240+
} else if (value.readable && hasOwn(value, 'httpVersion')) {
240241
// or try http response
241242
filename = path.basename(value.client._httpMessage.path || '');
242243
}
@@ -264,7 +265,7 @@ FormData.prototype._getContentType = function (value, options) {
264265
}
265266

266267
// or if it's http-reponse
267-
if (!contentType && value.readable && Object.prototype.hasOwnProperty.call(value, 'httpVersion')) {
268+
if (!contentType && value.readable && hasOwn(value, 'httpVersion')) {
268269
contentType = value.headers['content-type'];
269270
}
270271

@@ -305,7 +306,7 @@ FormData.prototype.getHeaders = function (userHeaders) {
305306
};
306307

307308
for (header in userHeaders) { // eslint-disable-line no-restricted-syntax
308-
if (Object.prototype.hasOwnProperty.call(userHeaders, header)) {
309+
if (hasOwn(userHeaders, header)) {
309310
formHeaders[header.toLowerCase()] = userHeaders[header];
310311
}
311312
}

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.35"
4950
},
5051
"devDependencies": {

test/common.js

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

1112
var common = module.exports;
@@ -60,7 +61,7 @@ common.actions = {};
6061
common.actions.populateFields = function (form, fields) {
6162
var field;
6263
for (var name in fields) { // eslint-disable-line no-restricted-syntax
63-
if (Object.prototype.hasOwnProperty.call(fields, name)) {
64+
if (hasOwn(fields, name)) {
6465
field = fields[name];
6566
// important to append ReadStreams within the same tick
6667
if (typeof field.value === 'function') {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ var assert = common.assert;
55
var http = require('http');
66
var mime = require('mime-types');
77
var fs = require('fs');
8+
var hasOwn = require('hasown');
89
var FormData = require(common.dir.lib + '/form_data');
910

1011
/*
@@ -72,7 +73,7 @@ server.listen(common.port, function () {
7273
var form = new FormData();
7374

7475
for (var name in FIELDS) { // eslint-disable-line no-restricted-syntax
75-
if (Object.prototype.hasOwnProperty.call(FIELDS, name)) {
76+
if (hasOwn(FIELDS, name)) {
7677
var field = FIELDS[name];
7778
// important to append ReadStreams within the same tick
7879
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 http = require('http');
66
var mime = require('mime-types');
77
var request = require('request');
88
var fs = require('fs');
9+
var hasOwn = require('hasown');
910
var FormData = require(common.dir.lib + '/form_data');
1011
var IncomingForm = require('formidable').IncomingForm;
1112

@@ -53,7 +54,7 @@ server.listen(common.port, function () {
5354
var form = new FormData();
5455

5556
for (var name in FIELDS) { // eslint-disable-line no-restricted-syntax
56-
if (Object.prototype.hasOwnProperty.call(FIELDS, name)) {
57+
if (hasOwn(FIELDS, name)) {
5758
var field = FIELDS[name];
5859
// important to append ReadStreams within the same tick
5960
if (typeof field.value === 'function') {

test/integration/test-ranged-filestream.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ var common = require('../common');
99
var assert = common.assert;
1010
var http = require('http');
1111
var fs = require('fs');
12+
var hasOwn = require('hasown');
1213

1314
var FormData = require(common.dir.lib + '/form_data');
1415
var IncomingForm = require('formidable').IncomingForm;
@@ -81,7 +82,7 @@ server.listen(common.port, function () {
8182

8283
// add test subjects to the form
8384
for (name in testSubjects) { // eslint-disable-line no-restricted-syntax
84-
if (Object.prototype.hasOwnProperty.call(testSubjects, name)) {
85+
if (hasOwn(testSubjects, name)) {
8586
options = { encoding: 'utf8' };
8687

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

0 commit comments

Comments
 (0)