Skip to content

Commit 4f87522

Browse files
committed
doc, lib, test: do not re-require needlessly
PR-URL: #14244 Reviewed-By: Alexey Orlenko <[email protected]>
1 parent 43bd47c commit 4f87522

23 files changed

+72
-85
lines changed

doc/api/child_process.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,8 +1136,9 @@ socket to the child process. The example below spawns two children that each
11361136
handle connections with "normal" or "special" priority:
11371137

11381138
```js
1139-
const normal = require('child_process').fork('child.js', ['normal']);
1140-
const special = require('child_process').fork('child.js', ['special']);
1139+
const { fork } = require('child_process');
1140+
const normal = fork('child.js', ['normal']);
1141+
const special = fork('child.js', ['special']);
11411142

11421143
// Open up the server and send sockets to child. Use pauseOnConnect to prevent
11431144
// the sockets from being read before they are sent to the child process.

lib/_tls_legacy.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@
2121

2222
'use strict';
2323

24-
require('internal/util').assertCrypto();
24+
const internalUtil = require('internal/util');
25+
internalUtil.assertCrypto();
2526

2627
const assert = require('assert');
2728
const Buffer = require('buffer').Buffer;
2829
const common = require('_tls_common');
2930
const Connection = process.binding('crypto').Connection;
3031
const EventEmitter = require('events');
31-
const internalUtil = require('internal/util');
3232
const stream = require('stream');
3333
const Timer = process.binding('timer_wrap').Timer;
3434
const tls = require('tls');

lib/internal/process.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
const errors = require('internal/errors');
4+
const util = require('util');
45
const constants = process.binding('constants').os.signals;
56

67
const assert = process.assert = function(x, msg) {
@@ -178,10 +179,8 @@ function setupKillAndExit() {
178179
}
179180
}
180181

181-
if (err) {
182-
const errnoException = require('util')._errnoException;
183-
throw errnoException(err, 'kill');
184-
}
182+
if (err)
183+
throw util._errnoException(err, 'kill');
185184

186185
return true;
187186
};
@@ -212,8 +211,7 @@ function setupSignalHandlers() {
212211
const err = wrap.start(signum);
213212
if (err) {
214213
wrap.close();
215-
const errnoException = require('util')._errnoException;
216-
throw errnoException(err, 'uv_signal_start');
214+
throw util._errnoException(err, 'uv_signal_start');
217215
}
218216

219217
signalWraps[type] = wrap;
@@ -253,10 +251,9 @@ function setupChannel() {
253251

254252

255253
function setupRawDebug() {
256-
const format = require('util').format;
257254
const rawDebug = process._rawDebug;
258255
process._rawDebug = function() {
259-
rawDebug(format.apply(null, arguments));
256+
rawDebug(util.format.apply(null, arguments));
260257
};
261258
}
262259

lib/repl.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ try {
8686
}
8787

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

9191
// If obj.hasOwnProperty has been overridden, then calling
9292
// obj.hasOwnProperty(prop) will break.
@@ -794,7 +794,7 @@ function complete(line, callback) {
794794
filter = match[1];
795795
var dir, files, f, name, base, ext, abs, subfiles, s;
796796
group = [];
797-
var paths = module.paths.concat(require('module').globalPaths);
797+
var paths = module.paths.concat(Module.globalPaths);
798798
for (i = 0; i < paths.length; i++) {
799799
dir = path.resolve(paths[i], subdir);
800800
try {

test/common/index.js

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,10 @@ const path = require('path');
2525
const fs = require('fs');
2626
const assert = require('assert');
2727
const os = require('os');
28-
const child_process = require('child_process');
28+
const { exec, execSync, spawn, spawnSync } = require('child_process');
2929
const stream = require('stream');
3030
const util = require('util');
3131
const Timer = process.binding('timer_wrap').Timer;
32-
const execSync = require('child_process').execSync;
3332

3433
const testRoot = process.env.NODE_TEST_DIR ?
3534
fs.realpathSync(process.env.NODE_TEST_DIR) : path.resolve(__dirname, '..');
@@ -186,7 +185,7 @@ Object.defineProperty(exports, 'inFreeBSDJail', {
186185
if (inFreeBSDJail !== null) return inFreeBSDJail;
187186

188187
if (exports.isFreeBSD &&
189-
child_process.execSync('sysctl -n security.jail.jailed').toString() ===
188+
execSync('sysctl -n security.jail.jailed').toString() ===
190189
'1\n') {
191190
inFreeBSDJail = true;
192191
} else {
@@ -233,7 +232,7 @@ Object.defineProperty(exports, 'opensslCli', {get: function() {
233232

234233
if (exports.isWindows) opensslCli += '.exe';
235234

236-
const opensslCmd = child_process.spawnSync(opensslCli, ['version']);
235+
const opensslCmd = spawnSync(opensslCli, ['version']);
237236
if (opensslCmd.status !== 0 || opensslCmd.error !== undefined) {
238237
// openssl command cannot be executed
239238
opensslCli = false;
@@ -283,7 +282,7 @@ exports.childShouldThrowAndAbort = function() {
283282
}
284283
testCmd += `"${process.argv[0]}" --abort-on-uncaught-exception `;
285284
testCmd += `"${process.argv[1]}" child`;
286-
const child = child_process.exec(testCmd);
285+
const child = exec(testCmd);
287286
child.on('exit', function onExit(exitCode, signal) {
288287
const errMsg = 'Test should have aborted ' +
289288
`but instead exited with exit code ${exitCode}` +
@@ -303,8 +302,6 @@ exports.ddCommand = function(filename, kilobytes) {
303302

304303

305304
exports.spawnPwd = function(options) {
306-
const spawn = require('child_process').spawn;
307-
308305
if (exports.isWindows) {
309306
return spawn('cmd.exe', ['/d', '/c', 'cd'], options);
310307
} else {
@@ -314,8 +311,6 @@ exports.spawnPwd = function(options) {
314311

315312

316313
exports.spawnSyncPwd = function(options) {
317-
const spawnSync = require('child_process').spawnSync;
318-
319314
if (exports.isWindows) {
320315
return spawnSync('cmd.exe', ['/d', '/c', 'cd'], options);
321316
} else {
@@ -789,7 +784,7 @@ exports.getTTYfd = function getTTYfd() {
789784
else if (!tty.isatty(tty_fd)) tty_fd++;
790785
else {
791786
try {
792-
tty_fd = require('fs').openSync('/dev/tty');
787+
tty_fd = fs.openSync('/dev/tty');
793788
} catch (e) {
794789
// There aren't any tty fd's available to use.
795790
return -1;

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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
'use strict';
2323
const common = require('../common');
2424
const assert = require('assert');
25-
const a = require('assert');
25+
const a = assert;
2626

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

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@
2222
'use strict';
2323
const common = require('../common');
2424
const assert = require('assert');
25-
const spawn = require('child_process').spawn;
26-
const fork = require('child_process').fork;
25+
const { fork, spawn } = require('child_process');
2726

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

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

Lines changed: 1 addition & 2 deletions
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-crypto-random.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ const expectedErrorRegexp = /^TypeError: size must be a number >= 0$/;
148148
bufferTooSmall: /^RangeError: buffer too small$/,
149149
};
150150

151+
const max = require('buffer').kMaxLength + 1;
152+
151153
for (const buf of bufs) {
152154
const len = Buffer.byteLength(buf);
153155
assert.strictEqual(len, 10, `Expected byteLength of 10, got ${len}`);
@@ -168,8 +170,6 @@ const expectedErrorRegexp = /^TypeError: size must be a number >= 0$/;
168170
crypto.randomFill(buf, NaN, common.mustNotCall());
169171
}, errMessages.offsetNotNumber);
170172

171-
const max = require('buffer').kMaxLength + 1;
172-
173173
assert.throws(() => {
174174
crypto.randomFillSync(buf, 11);
175175
}, errMessages.offsetOutOfRange);

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

2222
'use strict';
23-
require('../common');
2423
const common = require('../common');
2524
const assert = require('assert');
2625
const domain = require('domain');

test/parallel/test-handle-wrap-isrefed.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ const strictEqual = require('assert').strictEqual;
2424
}
2525

2626

27+
const dgram = require('dgram');
28+
2729
// dgram ipv4
2830
{
29-
const dgram = require('dgram');
3031
const sock4 = dgram.createSocket('udp4');
3132
strictEqual(Object.getPrototypeOf(sock4._handle).hasOwnProperty('hasRef'),
3233
true, 'udp_wrap: ipv4: hasRef() missing');
@@ -46,7 +47,6 @@ const strictEqual = require('assert').strictEqual;
4647

4748
// dgram ipv6
4849
{
49-
const dgram = require('dgram');
5050
const sock6 = dgram.createSocket('udp6');
5151
strictEqual(Object.getPrototypeOf(sock6._handle).hasOwnProperty('hasRef'),
5252
true, 'udp_wrap: ipv6: hasRef() missing');

test/parallel/test-http-invalidheaderfield2.js

Lines changed: 9 additions & 10 deletions
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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ function parent() {
8585
}).listen(0, function() {
8686
console.error('server listening on %d', this.address().port);
8787

88-
const spawn = require('child_process').spawn;
8988
const child = spawn(process.execPath, [__filename, 'child'], {
9089
stdio: [ 'ignore', 'ignore', 'ignore', server._handle ],
9190
detached: true

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,35 +40,35 @@ const server = net.createServer(function(conn) {
4040

4141
server.listen(0, function() {
4242
// Client 1
43-
conn = require('net').createConnection(this.address().port, 'localhost');
43+
conn = net.createConnection(this.address().port, 'localhost');
4444
conn.resume();
4545
conn.on('data', onDataOk);
4646

4747

4848
// Client 2
49-
conn = require('net').createConnection(this.address().port, 'localhost');
49+
conn = net.createConnection(this.address().port, 'localhost');
5050
conn.pause();
5151
conn.resume();
5252
conn.on('data', onDataOk);
5353

5454

5555
// Client 3
56-
conn = require('net').createConnection(this.address().port, 'localhost');
56+
conn = net.createConnection(this.address().port, 'localhost');
5757
conn.pause();
5858
conn.on('data', common.mustNotCall());
5959
scheduleTearDown(conn);
6060

6161

6262
// Client 4
63-
conn = require('net').createConnection(this.address().port, 'localhost');
63+
conn = net.createConnection(this.address().port, 'localhost');
6464
conn.resume();
6565
conn.pause();
6666
conn.resume();
6767
conn.on('data', onDataOk);
6868

6969

7070
// Client 5
71-
conn = require('net').createConnection(this.address().port, 'localhost');
71+
conn = net.createConnection(this.address().port, 'localhost');
7272
conn.resume();
7373
conn.resume();
7474
conn.pause();

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

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -83,22 +83,23 @@ function child5() {
8383
}
8484

8585
function parent() {
86+
const { spawn } = require('child_process');
87+
const node = process.execPath;
88+
const f = __filename;
89+
const option = { stdio: [ 0, 1, 'ignore' ] };
90+
91+
const test = (arg, exit) => {
92+
spawn(node, [f, arg], option).on('exit', (code) => {
93+
assert.strictEqual(
94+
code, exit,
95+
`wrong exit for ${arg}\nexpected:${exit} but got:${code}`);
96+
console.log('ok - %s exited with %d', arg, exit);
97+
});
98+
};
99+
86100
test('child1', 42);
87101
test('child2', 42);
88102
test('child3', 0);
89103
test('child4', 1);
90104
test('child5', 99);
91105
}
92-
93-
function test(arg, exit) {
94-
const spawn = require('child_process').spawn;
95-
const node = process.execPath;
96-
const f = __filename;
97-
const option = { stdio: [ 0, 1, 'ignore' ] };
98-
spawn(node, [f, arg], option).on('exit', function(code) {
99-
assert.strictEqual(
100-
code, exit,
101-
`wrong exit for ${arg}\nexpected:${exit} but got:${code}`);
102-
console.log('ok - %s exited with %d', arg, exit);
103-
});
104-
}

test/parallel/test-readline-interface.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ const readline = require('readline');
2727
const internalReadline = require('internal/readline');
2828
const EventEmitter = require('events').EventEmitter;
2929
const inherits = require('util').inherits;
30-
const Writable = require('stream').Writable;
31-
const Readable = require('stream').Readable;
30+
const { Writable, Readable } = require('stream');
3231

3332
function FakeInput() {
3433
EventEmitter.call(this);

0 commit comments

Comments
 (0)