Skip to content

Commit 3350230

Browse files
BridgeARaddaleax
authored andcommitted
lib: remove internal util._extends() usage
This removes all internal calls to the deprecated `_extends()` function. It is slower than `Object.assign()` and the object spread notation since V8 6.8 and using the spread notation often also results in shorter code. PR-URL: #25105 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent 901d3d0 commit 3350230

17 files changed

+89
-96
lines changed

lib/_http_agent.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ function Agent(options) {
4848
this.defaultPort = 80;
4949
this.protocol = 'http:';
5050

51-
this.options = util._extend({}, options);
51+
this.options = { ...options };
5252

5353
// Don't confuse net and make it think that we're connecting to a pipe
5454
this.options.path = null;
@@ -146,8 +146,7 @@ Agent.prototype.addRequest = function addRequest(req, options, port/* legacy */,
146146
};
147147
}
148148

149-
options = util._extend({}, options);
150-
util._extend(options, this.options);
149+
options = { ...options, ...this.options };
151150
if (options.socketPath)
152151
options.path = options.socketPath;
153152

@@ -194,8 +193,7 @@ Agent.prototype.addRequest = function addRequest(req, options, port/* legacy */,
194193
};
195194

196195
Agent.prototype.createSocket = function createSocket(req, options, cb) {
197-
options = util._extend({}, options);
198-
util._extend(options, this.options);
196+
options = { ...options, ...this.options };
199197
if (options.socketPath)
200198
options.path = options.socketPath;
201199

lib/_http_client.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,11 @@ function ClientRequest(input, options, cb) {
9393

9494
if (typeof options === 'function') {
9595
cb = options;
96-
options = null;
96+
options = input || {};
97+
} else {
98+
options = Object.assign(input || {}, options);
9799
}
98100

99-
options = util._extend(input || {}, options || {});
100-
101101
var agent = options.agent;
102102
var defaultAgent = options._defaultAgent || Agent.globalAgent;
103103
if (agent === false) {

lib/_http_server.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ function Server(options, requestListener) {
280280
requestListener = options;
281281
options = {};
282282
} else if (options == null || typeof options === 'object') {
283-
options = util._extend({}, options);
283+
options = { ...options };
284284
}
285285

286286
this[kIncomingMessage] = options.IncomingMessage || IncomingMessage;

lib/_tls_wrap.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -1108,22 +1108,22 @@ function SNICallback(servername, callback) {
11081108
//
11091109
//
11101110
function normalizeConnectArgs(listArgs) {
1111-
var args = net._normalizeArgs(listArgs);
1112-
var options = args[0];
1113-
var cb = args[1];
1111+
const args = net._normalizeArgs(listArgs);
1112+
const options = args[0];
1113+
const cb = args[1];
11141114

11151115
// If args[0] was options, then normalize dealt with it.
11161116
// If args[0] is port, or args[0], args[1] is host, port, we need to
11171117
// find the options and merge them in, normalize's options has only
11181118
// the host/port/path args that it knows about, not the tls options.
11191119
// This means that options.host overrides a host arg.
11201120
if (listArgs[1] !== null && typeof listArgs[1] === 'object') {
1121-
util._extend(options, listArgs[1]);
1121+
Object.assign(options, listArgs[1]);
11221122
} else if (listArgs[2] !== null && typeof listArgs[2] === 'object') {
1123-
util._extend(options, listArgs[2]);
1123+
Object.assign(options, listArgs[2]);
11241124
}
11251125

1126-
return (cb) ? [options, cb] : [options];
1126+
return cb ? [options, cb] : [options];
11271127
}
11281128

11291129
function onConnectSecure() {
@@ -1204,14 +1204,14 @@ exports.connect = function connect(...args) {
12041204
'certificate verification.');
12051205
}
12061206

1207-
var defaults = {
1207+
options = {
12081208
rejectUnauthorized: !allowUnauthorized,
12091209
ciphers: tls.DEFAULT_CIPHERS,
12101210
checkServerIdentity: tls.checkServerIdentity,
1211-
minDHSize: 1024
1211+
minDHSize: 1024,
1212+
...options
12121213
};
12131214

1214-
options = util._extend(defaults, options || {});
12151215
if (!options.keepAlive)
12161216
options.singleUse = true;
12171217

lib/child_process.js

+19-16
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ exports.fork = function fork(modulePath /* , args, options */) {
7979
throw new ERR_INVALID_ARG_VALUE(`arguments[${pos}]`, arguments[pos]);
8080
}
8181

82-
options = util._extend({}, arguments[pos++]);
82+
options = { ...arguments[pos++] };
8383
}
8484

8585
// Prepare arguments for fork:
@@ -176,28 +176,20 @@ Object.defineProperty(exports.exec, util.promisify.custom, {
176176
});
177177

178178
exports.execFile = function execFile(file /* , args, options, callback */) {
179-
var args = [];
180-
var callback;
181-
var options = {
182-
encoding: 'utf8',
183-
timeout: 0,
184-
maxBuffer: 200 * 1024,
185-
killSignal: 'SIGTERM',
186-
cwd: null,
187-
env: null,
188-
shell: false
189-
};
179+
let args = [];
180+
let callback;
181+
let options;
190182

191183
// Parse the optional positional parameters.
192-
var pos = 1;
184+
let pos = 1;
193185
if (pos < arguments.length && Array.isArray(arguments[pos])) {
194186
args = arguments[pos++];
195187
} else if (pos < arguments.length && arguments[pos] == null) {
196188
pos++;
197189
}
198190

199191
if (pos < arguments.length && typeof arguments[pos] === 'object') {
200-
util._extend(options, arguments[pos++]);
192+
options = arguments[pos++];
201193
} else if (pos < arguments.length && arguments[pos] == null) {
202194
pos++;
203195
}
@@ -210,6 +202,17 @@ exports.execFile = function execFile(file /* , args, options, callback */) {
210202
throw new ERR_INVALID_ARG_VALUE('args', arguments[pos]);
211203
}
212204

205+
options = {
206+
encoding: 'utf8',
207+
timeout: 0,
208+
maxBuffer: 200 * 1024,
209+
killSignal: 'SIGTERM',
210+
cwd: null,
211+
env: null,
212+
shell: false,
213+
...options
214+
};
215+
213216
// Validate the timeout, if present.
214217
validateTimeout(options.timeout);
215218

@@ -580,15 +583,15 @@ function spawnSync(file, args, options) {
580583
options.stdio = _validateStdio(options.stdio || 'pipe', true).stdio;
581584

582585
if (options.input) {
583-
var stdin = options.stdio[0] = util._extend({}, options.stdio[0]);
586+
var stdin = options.stdio[0] = { ...options.stdio[0] };
584587
stdin.input = options.input;
585588
}
586589

587590
// We may want to pass data in on any given fd, ensure it is a valid buffer
588591
for (var i = 0; i < options.stdio.length; i++) {
589592
var input = options.stdio[i] && options.stdio[i].input;
590593
if (input != null) {
591-
var pipe = options.stdio[i] = util._extend({}, options.stdio[i]);
594+
var pipe = options.stdio[i] = { ...options.stdio[i] };
592595
if (isArrayBufferView(input)) {
593596
pipe.input = input;
594597
} else if (typeof input === 'string') {

lib/domain.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -350,11 +350,9 @@ Domain.prototype.run = function(fn) {
350350
function intercepted(_this, self, cb, fnargs) {
351351
if (fnargs[0] && fnargs[0] instanceof Error) {
352352
var er = fnargs[0];
353-
util._extend(er, {
354-
domainBound: cb,
355-
domainThrown: false,
356-
domain: self
357-
});
353+
er.domainBound = cb;
354+
er.domainThrown = false;
355+
er.domain = self;
358356
self.emit('error', er);
359357
return;
360358
}

lib/fs.js

+8-10
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ const {
3939
O_SYMLINK
4040
} = constants;
4141

42-
const { _extend } = require('util');
4342
const pathModule = require('path');
4443
const { isArrayBufferView } = require('internal/util/types');
4544
const binding = process.binding('fs');
@@ -1294,21 +1293,20 @@ function watchFile(filename, options, listener) {
12941293
filename = pathModule.resolve(filename);
12951294
let stat;
12961295

1297-
const defaults = {
1296+
if (options === null || typeof options !== 'object') {
1297+
listener = options;
1298+
options = null;
1299+
}
1300+
1301+
options = {
12981302
// Poll interval in milliseconds. 5007 is what libev used to use. It's
12991303
// a little on the slow side but let's stick with it for now to keep
13001304
// behavioral changes to a minimum.
13011305
interval: 5007,
1302-
persistent: true
1306+
persistent: true,
1307+
...options
13031308
};
13041309

1305-
if (options !== null && typeof options === 'object') {
1306-
options = _extend(defaults, options);
1307-
} else {
1308-
listener = options;
1309-
options = defaults;
1310-
}
1311-
13121310
if (typeof listener !== 'function') {
13131311
throw new ERR_INVALID_ARG_TYPE('listener', 'Function', listener);
13141312
}

lib/https.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ function Server(opts, requestListener) {
4646
requestListener = opts;
4747
opts = undefined;
4848
}
49-
opts = util._extend({}, opts);
49+
opts = { ...opts };
5050

5151
if (!opts.ALPNProtocols) {
5252
// http/1.0 is not defined as Protocol IDs in IANA
@@ -110,9 +110,10 @@ function createConnection(port, host, options) {
110110
const session = this._getSession(options._agentKey);
111111
if (session) {
112112
debug('reuse session for %j', options._agentKey);
113-
options = util._extend({
114-
session: session
115-
}, options);
113+
options = {
114+
session,
115+
...options
116+
};
116117
}
117118
}
118119

@@ -291,7 +292,7 @@ function request(...args) {
291292
}
292293

293294
if (args[0] && typeof args[0] !== 'function') {
294-
options = util._extend(options, args.shift());
295+
Object.assign(options, args.shift());
295296
}
296297

297298
options._defaultAgent = module.exports.globalAgent;

lib/internal/cluster/child.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use strict';
22
const assert = require('assert');
3-
const util = require('util');
43
const path = require('path');
54
const EventEmitter = require('events');
65
const { owner_symbol } = require('internal/async_hooks').symbols;
@@ -71,11 +70,12 @@ cluster._getServer = function(obj, options, cb) {
7170

7271
indexes.set(indexesKey, index);
7372

74-
const message = util._extend({
73+
const message = {
7574
act: 'queryServer',
7675
index,
77-
data: null
78-
}, options);
76+
data: null,
77+
...options
78+
};
7979

8080
message.address = address;
8181

@@ -151,7 +151,7 @@ function rr(message, indexesKey, cb) {
151151

152152
function getsockname(out) {
153153
if (key)
154-
util._extend(out, message.sockname);
154+
Object.assign(out, message.sockname);
155155

156156
return 0;
157157
}

lib/internal/cluster/master.js

+13-16
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22
const assert = require('assert');
33
const { fork } = require('child_process');
4-
const util = require('util');
54
const path = require('path');
65
const EventEmitter = require('events');
76
const RoundRobinHandle = require('internal/cluster/round_robin_handle');
@@ -47,14 +46,14 @@ if (schedulingPolicy === undefined) {
4746
cluster.schedulingPolicy = schedulingPolicy;
4847

4948
cluster.setupMaster = function(options) {
50-
var settings = {
49+
const settings = {
5150
args: process.argv.slice(2),
5251
exec: process.argv[1],
5352
execArgv: process.execArgv,
54-
silent: false
53+
silent: false,
54+
...cluster.settings,
55+
...options
5556
};
56-
util._extend(settings, cluster.settings);
57-
util._extend(settings, options || {});
5857

5958
// Tell V8 to write profile data for each process to a separate file.
6059
// Without --logfile=v8-%p.log, everything ends up in a single, unusable
@@ -101,15 +100,12 @@ function setupSettingsNT(settings) {
101100
}
102101

103102
function createWorkerProcess(id, env) {
104-
const workerEnv = util._extend({}, process.env);
103+
const workerEnv = { ...process.env, ...env, NODE_UNIQUE_ID: `${id}` };
105104
const execArgv = cluster.settings.execArgv.slice();
106105
const debugArgRegex = /--inspect(?:-brk|-port)?|--debug-port/;
107106
const nodeOptions = process.env.NODE_OPTIONS ?
108107
process.env.NODE_OPTIONS : '';
109108

110-
util._extend(workerEnv, env);
111-
workerEnv.NODE_UNIQUE_ID = '' + id;
112-
113109
if (execArgv.some((arg) => arg.match(debugArgRegex)) ||
114110
nodeOptions.match(debugArgRegex)) {
115111
let inspectPort;
@@ -315,17 +311,18 @@ function queryServer(worker, message) {
315311

316312
// Set custom server data
317313
handle.add(worker, (errno, reply, handle) => {
318-
reply = util._extend({
319-
errno: errno,
320-
key: key,
321-
ack: message.seq,
322-
data: handles.get(key).data
323-
}, reply);
314+
const { data } = handles.get(key);
324315

325316
if (errno)
326317
handles.delete(key); // Gives other workers a chance to retry.
327318

328-
send(worker, reply, handle);
319+
send(worker, {
320+
errno,
321+
key,
322+
ack: message.seq,
323+
data,
324+
...reply
325+
}, handle);
329326
});
330327
}
331328

lib/internal/cluster/utils.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
'use strict';
2-
const util = require('util');
32

43
module.exports = {
54
sendHelper,
@@ -14,12 +13,11 @@ function sendHelper(proc, message, handle, cb) {
1413
return false;
1514

1615
// Mark message as internal. See INTERNAL_PREFIX in lib/child_process.js
17-
message = util._extend({ cmd: 'NODE_CLUSTER' }, message);
16+
message = { cmd: 'NODE_CLUSTER', ...message, seq };
1817

1918
if (typeof cb === 'function')
2019
callbacks.set(seq, cb);
2120

22-
message.seq = seq;
2321
seq += 1;
2422
return proc.send(message, handle);
2523
}

lib/internal/fs/utils.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ function getOptions(options, defaultOptions) {
171171
}
172172

173173
if (typeof options === 'string') {
174-
defaultOptions = util._extend({}, defaultOptions);
174+
defaultOptions = { ...defaultOptions };
175175
defaultOptions.encoding = options;
176176
options = defaultOptions;
177177
} else if (typeof options !== 'object') {

0 commit comments

Comments
 (0)