Skip to content

Commit 38ae5c4

Browse files
vsemozhetbytMylesBorins
authored andcommitted
doc, lib, test: do not re-require needlessly
PR-URL: #14244 Reviewed-By: Alexey Orlenko <[email protected]>
1 parent 792acc1 commit 38ae5c4

19 files changed

+65
-79
lines changed

doc/api/child_process.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -1046,8 +1046,9 @@ socket to the child process. The example below spawns two children that each
10461046
handle connections with "normal" or "special" priority:
10471047

10481048
```js
1049-
const normal = require('child_process').fork('subprocess.js', ['normal']);
1050-
const special = require('child_process').fork('subprocess.js', ['special']);
1049+
const { fork } = require('child_process');
1050+
const normal = fork('subprocess.js', ['normal']);
1051+
const special = fork('subprocess.js', ['special']);
10511052

10521053
// Open up the server and send sockets to child
10531054
const server = require('net').createServer();

lib/internal/process.js

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict';
22

3+
const util = require('util');
4+
35
var _lazyConstants = null;
46

57
function lazyConstants() {
@@ -185,10 +187,8 @@ function setupKillAndExit() {
185187
}
186188
}
187189

188-
if (err) {
189-
const errnoException = require('util')._errnoException;
190-
throw errnoException(err, 'kill');
191-
}
190+
if (err)
191+
throw util._errnoException(err, 'kill');
192192

193193
return true;
194194
};
@@ -220,8 +220,7 @@ function setupSignalHandlers() {
220220
const err = wrap.start(signum);
221221
if (err) {
222222
wrap.close();
223-
const errnoException = require('util')._errnoException;
224-
throw errnoException(err, 'uv_signal_start');
223+
throw util._errnoException(err, 'uv_signal_start');
225224
}
226225

227226
signalWraps[type] = wrap;
@@ -261,9 +260,8 @@ function setupChannel() {
261260

262261

263262
function setupRawDebug() {
264-
const format = require('util').format;
265263
const rawDebug = process._rawDebug;
266264
process._rawDebug = function() {
267-
rawDebug(format.apply(null, arguments));
265+
rawDebug(util.format.apply(null, arguments));
268266
};
269267
}

lib/repl.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ try {
6161
}
6262

6363
// hack for repl require to work properly with node_modules folders
64-
module.paths = require('module')._nodeModulePaths(module.filename);
64+
module.paths = Module._nodeModulePaths(module.filename);
6565

6666
// If obj.hasOwnProperty has been overridden, then calling
6767
// obj.hasOwnProperty(prop) will break.
@@ -869,7 +869,7 @@ function complete(line, callback) {
869869
filter = match[1];
870870
var dir, files, f, name, base, ext, abs, subfiles, s;
871871
group = [];
872-
var paths = module.paths.concat(require('module').globalPaths);
872+
var paths = module.paths.concat(Module.globalPaths);
873873
for (i = 0; i < paths.length; i++) {
874874
dir = path.resolve(paths[i], subdir);
875875
try {

test/common/index.js

+4-12
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const path = require('path');
44
const fs = require('fs');
55
const assert = require('assert');
66
const os = require('os');
7-
const child_process = require('child_process');
7+
const { exec, execSync, spawn, spawnSync } = require('child_process');
88
const stream = require('stream');
99
const util = require('util');
1010
const Timer = process.binding('timer_wrap').Timer;
@@ -121,7 +121,7 @@ Object.defineProperty(exports, 'inFreeBSDJail', {
121121
if (inFreeBSDJail !== null) return inFreeBSDJail;
122122

123123
if (exports.isFreeBSD &&
124-
child_process.execSync('sysctl -n security.jail.jailed').toString() ===
124+
execSync('sysctl -n security.jail.jailed').toString() ===
125125
'1\n') {
126126
inFreeBSDJail = true;
127127
} else {
@@ -168,7 +168,7 @@ Object.defineProperty(exports, 'opensslCli', {get: function() {
168168

169169
if (exports.isWindows) opensslCli += '.exe';
170170

171-
const opensslCmd = child_process.spawnSync(opensslCli, ['version']);
171+
const opensslCmd = spawnSync(opensslCli, ['version']);
172172
if (opensslCmd.status !== 0 || opensslCmd.error !== undefined) {
173173
// openssl command cannot be executed
174174
opensslCli = false;
@@ -219,7 +219,7 @@ exports.childShouldThrowAndAbort = function() {
219219
}
220220
testCmd += `"${process.argv[0]}" --abort-on-uncaught-exception `;
221221
testCmd += `"${process.argv[1]}" child`;
222-
const child = child_process.exec(testCmd);
222+
const child = exec(testCmd);
223223
child.on('exit', function onExit(exitCode, signal) {
224224
const errMsg = 'Test should have aborted ' +
225225
`but instead exited with exit code ${exitCode}` +
@@ -239,8 +239,6 @@ exports.ddCommand = function(filename, kilobytes) {
239239

240240

241241
exports.spawnCat = function(options) {
242-
const spawn = require('child_process').spawn;
243-
244242
if (exports.isWindows) {
245243
return spawn('more', [], options);
246244
} else {
@@ -250,8 +248,6 @@ exports.spawnCat = function(options) {
250248

251249

252250
exports.spawnSyncCat = function(options) {
253-
const spawnSync = require('child_process').spawnSync;
254-
255251
if (exports.isWindows) {
256252
return spawnSync('more', [], options);
257253
} else {
@@ -261,8 +257,6 @@ exports.spawnSyncCat = function(options) {
261257

262258

263259
exports.spawnPwd = function(options) {
264-
const spawn = require('child_process').spawn;
265-
266260
if (exports.isWindows) {
267261
return spawn('cmd.exe', ['/c', 'cd'], options);
268262
} else {
@@ -272,8 +266,6 @@ exports.spawnPwd = function(options) {
272266

273267

274268
exports.spawnSyncPwd = function(options) {
275-
const spawnSync = require('child_process').spawnSync;
276-
277269
if (exports.isWindows) {
278270
return spawnSync('cmd.exe', ['/c', 'cd'], options);
279271
} else {

test/parallel/test-assert-typedarray-deepequal.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
require('../common');
44
const assert = require('assert');
5-
const a = require('assert');
65

76
function makeBlock(f) {
87
const args = Array.prototype.slice.call(arguments, 1);
@@ -51,7 +50,8 @@ equalArrayPairs.forEach((arrayPair) => {
5150

5251
notEqualArrayPairs.forEach((arrayPair) => {
5352
assert.throws(
54-
makeBlock(a.deepEqual, arrayPair[0], arrayPair[1]),
55-
a.AssertionError
53+
// eslint-disable-next-line no-restricted-properties
54+
makeBlock(assert.deepEqual, arrayPair[0], arrayPair[1]),
55+
assert.AssertionError
5656
);
5757
});

test/parallel/test-assert.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22
require('../common');
33
const assert = require('assert');
4-
const a = require('assert');
4+
const a = assert;
55

66
function makeBlock(f) {
77
const args = Array.prototype.slice.call(arguments, 1);

test/parallel/test-child-process-fork-and-spawn.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
'use strict';
22
const common = require('../common');
33
const assert = require('assert');
4-
const spawn = require('child_process').spawn;
5-
const fork = require('child_process').fork;
4+
const { fork, spawn } = require('child_process');
65

76
// Fork, then spawn. The spawned process should not hang.
87
switch (process.argv[2] || '') {

test/parallel/test-child-process-send-returns-boolean.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ const common = require('../common');
33
const assert = require('assert');
44
const path = require('path');
55
const net = require('net');
6-
const fork = require('child_process').fork;
7-
const spawn = require('child_process').spawn;
6+
const { fork, spawn } = require('child_process');
87

98
const emptyFile = path.join(common.fixturesDir, 'empty.js');
109

test/parallel/test-domain-exit-dispose.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
'use strict';
2-
require('../common');
32
const common = require('../common');
43
const assert = require('assert');
54
const domain = require('domain');

test/parallel/test-http-invalidheaderfield2.js

+9-10
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
require('../common');
33
const assert = require('assert');
44
const inspect = require('util').inspect;
5-
const checkIsHttpToken = require('_http_common')._checkIsHttpToken;
6-
const checkInvalidHeaderChar = require('_http_common')._checkInvalidHeaderChar;
5+
const { _checkIsHttpToken, _checkInvalidHeaderChar } = require('_http_common');
76

87
// Good header field names
98
[
@@ -29,8 +28,8 @@ const checkInvalidHeaderChar = require('_http_common')._checkInvalidHeaderChar;
2928
'3.14159265359'
3029
].forEach(function(str) {
3130
assert.strictEqual(
32-
checkIsHttpToken(str), true,
33-
`checkIsHttpToken(${inspect(str)}) unexpectedly failed`);
31+
_checkIsHttpToken(str), true,
32+
`_checkIsHttpToken(${inspect(str)}) unexpectedly failed`);
3433
});
3534
// Bad header field names
3635
[
@@ -55,8 +54,8 @@ const checkInvalidHeaderChar = require('_http_common')._checkInvalidHeaderChar;
5554
'This,That'
5655
].forEach(function(str) {
5756
assert.strictEqual(
58-
checkIsHttpToken(str), false,
59-
`checkIsHttpToken(${inspect(str)}) unexpectedly succeeded`);
57+
_checkIsHttpToken(str), false,
58+
`_checkIsHttpToken(${inspect(str)}) unexpectedly succeeded`);
6059
});
6160

6261

@@ -68,8 +67,8 @@ const checkInvalidHeaderChar = require('_http_common')._checkInvalidHeaderChar;
6867
'!@#$%^&*()-_=+\\;\':"[]{}<>,./?|~`'
6968
].forEach(function(str) {
7069
assert.strictEqual(
71-
checkInvalidHeaderChar(str), false,
72-
`checkInvalidHeaderChar(${inspect(str)}) unexpectedly failed`);
70+
_checkInvalidHeaderChar(str), false,
71+
`_checkInvalidHeaderChar(${inspect(str)}) unexpectedly failed`);
7372
});
7473

7574
// Bad header field values
@@ -84,6 +83,6 @@ const checkInvalidHeaderChar = require('_http_common')._checkInvalidHeaderChar;
8483
'Ding!\x07'
8584
].forEach(function(str) {
8685
assert.strictEqual(
87-
checkInvalidHeaderChar(str), true,
88-
`checkInvalidHeaderChar(${inspect(str)}) unexpectedly succeeded`);
86+
_checkInvalidHeaderChar(str), true,
87+
`_checkInvalidHeaderChar(${inspect(str)}) unexpectedly succeeded`);
8988
});

test/parallel/test-listen-fd-detached.js

-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ function parent() {
6464
}).listen(0, function() {
6565
console.error('server listening on %d', this.address().port);
6666

67-
const spawn = require('child_process').spawn;
6867
const child = spawn(process.execPath, [__filename, 'child'], {
6968
stdio: [ 'ignore', 'ignore', 'ignore', server._handle ],
7069
detached: true

test/parallel/test-net-pause-resume-connecting.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -19,35 +19,35 @@ const server = net.createServer(function(conn) {
1919

2020
server.listen(0, function() {
2121
// Client 1
22-
conn = require('net').createConnection(this.address().port, 'localhost');
22+
conn = net.createConnection(this.address().port, 'localhost');
2323
conn.resume();
2424
conn.on('data', onDataOk);
2525

2626

2727
// Client 2
28-
conn = require('net').createConnection(this.address().port, 'localhost');
28+
conn = net.createConnection(this.address().port, 'localhost');
2929
conn.pause();
3030
conn.resume();
3131
conn.on('data', onDataOk);
3232

3333

3434
// Client 3
35-
conn = require('net').createConnection(this.address().port, 'localhost');
35+
conn = net.createConnection(this.address().port, 'localhost');
3636
conn.pause();
3737
conn.on('data', common.mustNotCall());
3838
scheduleTearDown(conn);
3939

4040

4141
// Client 4
42-
conn = require('net').createConnection(this.address().port, 'localhost');
42+
conn = net.createConnection(this.address().port, 'localhost');
4343
conn.resume();
4444
conn.pause();
4545
conn.resume();
4646
conn.on('data', onDataOk);
4747

4848

4949
// Client 5
50-
conn = require('net').createConnection(this.address().port, 'localhost');
50+
conn = net.createConnection(this.address().port, 'localhost');
5151
conn.resume();
5252
conn.resume();
5353
conn.pause();

test/parallel/test-process-exit-code.js

+14-13
Original file line numberDiff line numberDiff line change
@@ -62,22 +62,23 @@ function child5() {
6262
}
6363

6464
function parent() {
65+
const { spawn } = require('child_process');
66+
const node = process.execPath;
67+
const f = __filename;
68+
const option = { stdio: [ 0, 1, 'ignore' ] };
69+
70+
const test = (arg, exit) => {
71+
spawn(node, [f, arg], option).on('exit', (code) => {
72+
assert.strictEqual(
73+
code, exit,
74+
`wrong exit for ${arg}\nexpected:${exit} but got:${code}`);
75+
console.log('ok - %s exited with %d', arg, exit);
76+
});
77+
};
78+
6579
test('child1', 42);
6680
test('child2', 42);
6781
test('child3', 0);
6882
test('child4', 1);
6983
test('child5', 99);
7084
}
71-
72-
function test(arg, exit) {
73-
const spawn = require('child_process').spawn;
74-
const node = process.execPath;
75-
const f = __filename;
76-
const option = { stdio: [ 0, 1, 'ignore' ] };
77-
spawn(node, [f, arg], option).on('exit', function(code) {
78-
assert.strictEqual(
79-
code, exit,
80-
`wrong exit for ${arg}\nexpected:${exit} but got:${code}`);
81-
console.log('ok - %s exited with %d', arg, exit);
82-
});
83-
}

test/parallel/test-readline-interface.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ const assert = require('assert');
44
const readline = require('readline');
55
const EventEmitter = require('events').EventEmitter;
66
const inherits = require('util').inherits;
7-
const Writable = require('stream').Writable;
8-
const Readable = require('stream').Readable;
7+
const { Writable, Readable } = require('stream');
98

109
function FakeInput() {
1110
EventEmitter.call(this);

test/parallel/test-require-symlink.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ const common = require('../common');
44
const assert = require('assert');
55
const path = require('path');
66
const fs = require('fs');
7-
const exec = require('child_process').exec;
8-
const spawn = require('child_process').spawn;
7+
const { exec, spawn } = require('child_process');
98

109
common.refreshTmpDir();
1110

test/parallel/test-tls-session-cache.js

+9-8
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,22 @@ if (!common.opensslCli)
77
if (!common.hasCrypto)
88
common.skip('missing crypto');
99

10+
const assert = require('assert');
11+
const tls = require('tls');
12+
const fs = require('fs');
13+
const { join } = require('path');
14+
const { spawn } = require('child_process');
15+
16+
const keyFile = join(common.fixturesDir, 'agent.key');
17+
const certFile = join(common.fixturesDir, 'agent.crt');
18+
1019
doTest({ tickets: false }, function() {
1120
doTest({ tickets: true }, function() {
1221
console.error('all done');
1322
});
1423
});
1524

1625
function doTest(testOptions, callback) {
17-
const assert = require('assert');
18-
const tls = require('tls');
19-
const fs = require('fs');
20-
const join = require('path').join;
21-
const spawn = require('child_process').spawn;
22-
23-
const keyFile = join(common.fixturesDir, 'agent.key');
24-
const certFile = join(common.fixturesDir, 'agent.crt');
2526
const key = fs.readFileSync(keyFile);
2627
const cert = fs.readFileSync(certFile);
2728
const options = {

0 commit comments

Comments
 (0)