From eb023ef7df188847639556be172183d4a006fefd Mon Sep 17 00:00:00 2001 From: Vse Mozhet Byt Date: Sat, 15 Jul 2017 03:30:28 +0300 Subject: [PATCH] doc, lib, test: do not re-require needlessly Backport-PR-URL: https://github.com/nodejs/node/pull/14524 Backport-Reviewed-By: Anna Henningsen PR-URL: https://github.com/nodejs/node/pull/14244 Reviewed-By: Alexey Orlenko --- doc/api/child_process.md | 5 ++-- lib/_tls_legacy.js | 4 +-- lib/internal/process.js | 14 +++++----- lib/repl.js | 2 +- .../test-assert-typedarray-deepequal.js | 6 ++--- test/parallel/test-assert.js | 2 +- .../test-child-process-fork-and-spawn.js | 3 +-- ...test-child-process-send-returns-boolean.js | 3 +-- test/parallel/test-crypto-random.js | 4 +-- test/parallel/test-domain-exit-dispose.js | 1 - test/parallel/test-handle-wrap-isrefed.js | 4 +-- .../parallel/test-http-invalidheaderfield2.js | 19 +++++++------ test/parallel/test-listen-fd-detached.js | 1 - .../test-net-pause-resume-connecting.js | 10 +++---- test/parallel/test-process-exit-code.js | 27 ++++++++++--------- test/parallel/test-readline-interface.js | 3 +-- test/parallel/test-require-symlink.js | 3 +-- test/parallel/test-stream2-push.js | 3 +-- test/parallel/test-tls-session-cache.js | 17 ++++++------ test/pummel/test-vm-memleak.js | 5 ++-- .../sequential/test-child-process-execsync.js | 3 +-- test/sequential/test-module-loading.js | 2 +- 22 files changed, 67 insertions(+), 74 deletions(-) diff --git a/doc/api/child_process.md b/doc/api/child_process.md index 27bfac5609b10c..1801bc54575112 100755 --- a/doc/api/child_process.md +++ b/doc/api/child_process.md @@ -1136,8 +1136,9 @@ socket to the child process. The example below spawns two children that each handle connections with "normal" or "special" priority: ```js -const normal = require('child_process').fork('child.js', ['normal']); -const special = require('child_process').fork('child.js', ['special']); +const { fork } = require('child_process'); +const normal = fork('child.js', ['normal']); +const special = fork('child.js', ['special']); // Open up the server and send sockets to child. Use pauseOnConnect to prevent // the sockets from being read before they are sent to the child process. diff --git a/lib/_tls_legacy.js b/lib/_tls_legacy.js index 44f67de4e5adaa..b25be2d6a3b444 100644 --- a/lib/_tls_legacy.js +++ b/lib/_tls_legacy.js @@ -21,14 +21,14 @@ 'use strict'; -require('internal/util').assertCrypto(); +const internalUtil = require('internal/util'); +internalUtil.assertCrypto(); const assert = require('assert'); const Buffer = require('buffer').Buffer; const common = require('_tls_common'); const Connection = process.binding('crypto').Connection; const EventEmitter = require('events'); -const internalUtil = require('internal/util'); const stream = require('stream'); const Timer = process.binding('timer_wrap').Timer; const tls = require('tls'); diff --git a/lib/internal/process.js b/lib/internal/process.js index 99dd2668fc880c..2e11249cb2dd30 100644 --- a/lib/internal/process.js +++ b/lib/internal/process.js @@ -1,5 +1,7 @@ 'use strict'; +const util = require('util'); + var _lazyConstants = null; function lazyConstants() { @@ -178,10 +180,8 @@ function setupKillAndExit() { } } - if (err) { - const errnoException = require('util')._errnoException; - throw errnoException(err, 'kill'); - } + if (err) + throw util._errnoException(err, 'kill'); return true; }; @@ -212,8 +212,7 @@ function setupSignalHandlers() { const err = wrap.start(signum); if (err) { wrap.close(); - const errnoException = require('util')._errnoException; - throw errnoException(err, 'uv_signal_start'); + throw util._errnoException(err, 'uv_signal_start'); } signalWraps[type] = wrap; @@ -253,10 +252,9 @@ function setupChannel() { function setupRawDebug() { - const format = require('util').format; const rawDebug = process._rawDebug; process._rawDebug = function() { - rawDebug(format.apply(null, arguments)); + rawDebug(util.format.apply(null, arguments)); }; } diff --git a/lib/repl.js b/lib/repl.js index b6fab49eed1439..130eece3e97a5e 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -85,7 +85,7 @@ try { } // hack for repl require to work properly with node_modules folders -module.paths = require('module')._nodeModulePaths(module.filename); +module.paths = Module._nodeModulePaths(module.filename); // If obj.hasOwnProperty has been overridden, then calling // obj.hasOwnProperty(prop) will break. diff --git a/test/parallel/test-assert-typedarray-deepequal.js b/test/parallel/test-assert-typedarray-deepequal.js index f5ecb274a86a32..7eed19cc539466 100644 --- a/test/parallel/test-assert-typedarray-deepequal.js +++ b/test/parallel/test-assert-typedarray-deepequal.js @@ -2,7 +2,6 @@ require('../common'); const assert = require('assert'); -const a = require('assert'); function makeBlock(f) { const args = Array.prototype.slice.call(arguments, 1); @@ -51,7 +50,8 @@ equalArrayPairs.forEach((arrayPair) => { notEqualArrayPairs.forEach((arrayPair) => { assert.throws( - makeBlock(a.deepEqual, arrayPair[0], arrayPair[1]), - a.AssertionError + // eslint-disable-next-line no-restricted-properties + makeBlock(assert.deepEqual, arrayPair[0], arrayPair[1]), + assert.AssertionError ); }); diff --git a/test/parallel/test-assert.js b/test/parallel/test-assert.js index 1033eaf1ac1330..13ec4fd671d694 100644 --- a/test/parallel/test-assert.js +++ b/test/parallel/test-assert.js @@ -22,7 +22,7 @@ 'use strict'; const common = require('../common'); const assert = require('assert'); -const a = require('assert'); +const a = assert; function makeBlock(f) { const args = Array.prototype.slice.call(arguments, 1); diff --git a/test/parallel/test-child-process-fork-and-spawn.js b/test/parallel/test-child-process-fork-and-spawn.js index 5d4446d98a3489..88e634ba8b0522 100644 --- a/test/parallel/test-child-process-fork-and-spawn.js +++ b/test/parallel/test-child-process-fork-and-spawn.js @@ -22,8 +22,7 @@ 'use strict'; const common = require('../common'); const assert = require('assert'); -const spawn = require('child_process').spawn; -const fork = require('child_process').fork; +const { fork, spawn } = require('child_process'); // Fork, then spawn. The spawned process should not hang. switch (process.argv[2] || '') { diff --git a/test/parallel/test-child-process-send-returns-boolean.js b/test/parallel/test-child-process-send-returns-boolean.js index 379f76a67390b9..d986b633d4140b 100644 --- a/test/parallel/test-child-process-send-returns-boolean.js +++ b/test/parallel/test-child-process-send-returns-boolean.js @@ -3,8 +3,7 @@ const common = require('../common'); const assert = require('assert'); const path = require('path'); const net = require('net'); -const fork = require('child_process').fork; -const spawn = require('child_process').spawn; +const { fork, spawn } = require('child_process'); const emptyFile = path.join(common.fixturesDir, 'empty.js'); diff --git a/test/parallel/test-crypto-random.js b/test/parallel/test-crypto-random.js index 6d1e40b87fe7f3..d9fa7efd9b6a4a 100644 --- a/test/parallel/test-crypto-random.js +++ b/test/parallel/test-crypto-random.js @@ -148,6 +148,8 @@ const expectedErrorRegexp = /^TypeError: size must be a number >= 0$/; bufferTooSmall: /^RangeError: buffer too small$/, }; + const max = require('buffer').kMaxLength + 1; + for (const buf of bufs) { const len = Buffer.byteLength(buf); assert.strictEqual(len, 10, `Expected byteLength of 10, got ${len}`); @@ -168,8 +170,6 @@ const expectedErrorRegexp = /^TypeError: size must be a number >= 0$/; crypto.randomFill(buf, NaN, common.mustNotCall()); }, errMessages.offsetNotNumber); - const max = require('buffer').kMaxLength + 1; - assert.throws(() => { crypto.randomFillSync(buf, 11); }, errMessages.offsetOutOfRange); diff --git a/test/parallel/test-domain-exit-dispose.js b/test/parallel/test-domain-exit-dispose.js index be56f4dfbb2b49..0bc8adae828437 100644 --- a/test/parallel/test-domain-exit-dispose.js +++ b/test/parallel/test-domain-exit-dispose.js @@ -20,7 +20,6 @@ // USE OR OTHER DEALINGS IN THE SOFTWARE. 'use strict'; -require('../common'); const common = require('../common'); const assert = require('assert'); const domain = require('domain'); diff --git a/test/parallel/test-handle-wrap-isrefed.js b/test/parallel/test-handle-wrap-isrefed.js index 66353fcc0362b3..3fc115fdc2bab0 100644 --- a/test/parallel/test-handle-wrap-isrefed.js +++ b/test/parallel/test-handle-wrap-isrefed.js @@ -24,9 +24,10 @@ const strictEqual = require('assert').strictEqual; } +const dgram = require('dgram'); + // dgram ipv4 { - const dgram = require('dgram'); const sock4 = dgram.createSocket('udp4'); strictEqual(Object.getPrototypeOf(sock4._handle).hasOwnProperty('hasRef'), true, 'udp_wrap: ipv4: hasRef() missing'); @@ -46,7 +47,6 @@ const strictEqual = require('assert').strictEqual; // dgram ipv6 { - const dgram = require('dgram'); const sock6 = dgram.createSocket('udp6'); strictEqual(Object.getPrototypeOf(sock6._handle).hasOwnProperty('hasRef'), true, 'udp_wrap: ipv6: hasRef() missing'); diff --git a/test/parallel/test-http-invalidheaderfield2.js b/test/parallel/test-http-invalidheaderfield2.js index 2267c8565cb634..40415d9c368891 100644 --- a/test/parallel/test-http-invalidheaderfield2.js +++ b/test/parallel/test-http-invalidheaderfield2.js @@ -2,8 +2,7 @@ require('../common'); const assert = require('assert'); const inspect = require('util').inspect; -const checkIsHttpToken = require('_http_common')._checkIsHttpToken; -const checkInvalidHeaderChar = require('_http_common')._checkInvalidHeaderChar; +const { _checkIsHttpToken, _checkInvalidHeaderChar } = require('_http_common'); // Good header field names [ @@ -29,8 +28,8 @@ const checkInvalidHeaderChar = require('_http_common')._checkInvalidHeaderChar; '3.14159265359' ].forEach(function(str) { assert.strictEqual( - checkIsHttpToken(str), true, - `checkIsHttpToken(${inspect(str)}) unexpectedly failed`); + _checkIsHttpToken(str), true, + `_checkIsHttpToken(${inspect(str)}) unexpectedly failed`); }); // Bad header field names [ @@ -55,8 +54,8 @@ const checkInvalidHeaderChar = require('_http_common')._checkInvalidHeaderChar; 'This,That' ].forEach(function(str) { assert.strictEqual( - checkIsHttpToken(str), false, - `checkIsHttpToken(${inspect(str)}) unexpectedly succeeded`); + _checkIsHttpToken(str), false, + `_checkIsHttpToken(${inspect(str)}) unexpectedly succeeded`); }); @@ -68,8 +67,8 @@ const checkInvalidHeaderChar = require('_http_common')._checkInvalidHeaderChar; '!@#$%^&*()-_=+\\;\':"[]{}<>,./?|~`' ].forEach(function(str) { assert.strictEqual( - checkInvalidHeaderChar(str), false, - `checkInvalidHeaderChar(${inspect(str)}) unexpectedly failed`); + _checkInvalidHeaderChar(str), false, + `_checkInvalidHeaderChar(${inspect(str)}) unexpectedly failed`); }); // Bad header field values @@ -84,6 +83,6 @@ const checkInvalidHeaderChar = require('_http_common')._checkInvalidHeaderChar; 'Ding!\x07' ].forEach(function(str) { assert.strictEqual( - checkInvalidHeaderChar(str), true, - `checkInvalidHeaderChar(${inspect(str)}) unexpectedly succeeded`); + _checkInvalidHeaderChar(str), true, + `_checkInvalidHeaderChar(${inspect(str)}) unexpectedly succeeded`); }); diff --git a/test/parallel/test-listen-fd-detached.js b/test/parallel/test-listen-fd-detached.js index cc9cb6471e408b..b2b9c74a9a2da1 100644 --- a/test/parallel/test-listen-fd-detached.js +++ b/test/parallel/test-listen-fd-detached.js @@ -85,7 +85,6 @@ function parent() { }).listen(0, function() { console.error('server listening on %d', this.address().port); - const spawn = require('child_process').spawn; const child = spawn(process.execPath, [__filename, 'child'], { stdio: [ 'ignore', 'ignore', 'ignore', server._handle ], detached: true diff --git a/test/parallel/test-net-pause-resume-connecting.js b/test/parallel/test-net-pause-resume-connecting.js index 39d83159920f09..920522b7604556 100644 --- a/test/parallel/test-net-pause-resume-connecting.js +++ b/test/parallel/test-net-pause-resume-connecting.js @@ -40,27 +40,27 @@ const server = net.createServer(function(conn) { server.listen(0, function() { // Client 1 - conn = require('net').createConnection(this.address().port, 'localhost'); + conn = net.createConnection(this.address().port, 'localhost'); conn.resume(); conn.on('data', onDataOk); // Client 2 - conn = require('net').createConnection(this.address().port, 'localhost'); + conn = net.createConnection(this.address().port, 'localhost'); conn.pause(); conn.resume(); conn.on('data', onDataOk); // Client 3 - conn = require('net').createConnection(this.address().port, 'localhost'); + conn = net.createConnection(this.address().port, 'localhost'); conn.pause(); conn.on('data', common.mustNotCall()); scheduleTearDown(conn); // Client 4 - conn = require('net').createConnection(this.address().port, 'localhost'); + conn = net.createConnection(this.address().port, 'localhost'); conn.resume(); conn.pause(); conn.resume(); @@ -68,7 +68,7 @@ server.listen(0, function() { // Client 5 - conn = require('net').createConnection(this.address().port, 'localhost'); + conn = net.createConnection(this.address().port, 'localhost'); conn.resume(); conn.resume(); conn.pause(); diff --git a/test/parallel/test-process-exit-code.js b/test/parallel/test-process-exit-code.js index e84be0aeadc05f..4deebb54a10784 100644 --- a/test/parallel/test-process-exit-code.js +++ b/test/parallel/test-process-exit-code.js @@ -83,22 +83,23 @@ function child5() { } function parent() { + const { spawn } = require('child_process'); + const node = process.execPath; + const f = __filename; + const option = { stdio: [ 0, 1, 'ignore' ] }; + + const test = (arg, exit) => { + spawn(node, [f, arg], option).on('exit', (code) => { + assert.strictEqual( + code, exit, + `wrong exit for ${arg}\nexpected:${exit} but got:${code}`); + console.log('ok - %s exited with %d', arg, exit); + }); + }; + test('child1', 42); test('child2', 42); test('child3', 0); test('child4', 1); test('child5', 99); } - -function test(arg, exit) { - const spawn = require('child_process').spawn; - const node = process.execPath; - const f = __filename; - const option = { stdio: [ 0, 1, 'ignore' ] }; - spawn(node, [f, arg], option).on('exit', function(code) { - assert.strictEqual( - code, exit, - `wrong exit for ${arg}\nexpected:${exit} but got:${code}`); - console.log('ok - %s exited with %d', arg, exit); - }); -} diff --git a/test/parallel/test-readline-interface.js b/test/parallel/test-readline-interface.js index 5628107069a442..e3ac79c233ec5f 100644 --- a/test/parallel/test-readline-interface.js +++ b/test/parallel/test-readline-interface.js @@ -27,8 +27,7 @@ const readline = require('readline'); const internalReadline = require('internal/readline'); const EventEmitter = require('events').EventEmitter; const inherits = require('util').inherits; -const Writable = require('stream').Writable; -const Readable = require('stream').Readable; +const { Writable, Readable } = require('stream'); function FakeInput() { EventEmitter.call(this); diff --git a/test/parallel/test-require-symlink.js b/test/parallel/test-require-symlink.js index d5dc03e49dfc37..5608df4eaf4823 100644 --- a/test/parallel/test-require-symlink.js +++ b/test/parallel/test-require-symlink.js @@ -4,8 +4,7 @@ const common = require('../common'); const assert = require('assert'); const path = require('path'); const fs = require('fs'); -const exec = require('child_process').exec; -const spawn = require('child_process').spawn; +const { exec, spawn } = require('child_process'); const util = require('util'); common.refreshTmpDir(); diff --git a/test/parallel/test-stream2-push.js b/test/parallel/test-stream2-push.js index 88531d9e3dc58e..21dc303695a110 100644 --- a/test/parallel/test-stream2-push.js +++ b/test/parallel/test-stream2-push.js @@ -21,9 +21,8 @@ 'use strict'; require('../common'); -const Readable = require('stream').Readable; -const Writable = require('stream').Writable; const assert = require('assert'); +const { Readable, Writable } = require('stream'); const EE = require('events').EventEmitter; diff --git a/test/parallel/test-tls-session-cache.js b/test/parallel/test-tls-session-cache.js index 805c21b3ade248..a6d00d2c07b97d 100644 --- a/test/parallel/test-tls-session-cache.js +++ b/test/parallel/test-tls-session-cache.js @@ -28,6 +28,15 @@ if (!common.opensslCli) if (!common.hasCrypto) common.skip('missing crypto'); +const assert = require('assert'); +const tls = require('tls'); +const fs = require('fs'); +const { join } = require('path'); +const { spawn } = require('child_process'); + +const keyFile = join(common.fixturesDir, 'agent.key'); +const certFile = join(common.fixturesDir, 'agent.crt'); + doTest({ tickets: false }, function() { doTest({ tickets: true }, function() { doTest({ tickets: false, invalidSession: true }, function() { @@ -37,14 +46,6 @@ doTest({ tickets: false }, function() { }); function doTest(testOptions, callback) { - const assert = require('assert'); - const tls = require('tls'); - const fs = require('fs'); - const join = require('path').join; - const spawn = require('child_process').spawn; - - const keyFile = join(common.fixturesDir, 'agent.key'); - const certFile = join(common.fixturesDir, 'agent.crt'); const key = fs.readFileSync(keyFile); const cert = fs.readFileSync(certFile); const options = { diff --git a/test/pummel/test-vm-memleak.js b/test/pummel/test-vm-memleak.js index 79c22f030f57b1..601781fec2a5d6 100644 --- a/test/pummel/test-vm-memleak.js +++ b/test/pummel/test-vm-memleak.js @@ -24,6 +24,7 @@ require('../common'); const assert = require('assert'); +const vm = require('vm'); const start = Date.now(); let maxMem = 0; @@ -35,7 +36,7 @@ assert(ok, 'Run this test with --max_old_space_size=32.'); const interval = setInterval(function() { try { - require('vm').runInNewContext('throw 1;'); + vm.runInNewContext('throw 1;'); } catch (e) { } @@ -52,7 +53,7 @@ const interval = setInterval(function() { function testContextLeak() { for (let i = 0; i < 1000; i++) - require('vm').createContext({}); + vm.createContext({}); } process.on('exit', function() { diff --git a/test/sequential/test-child-process-execsync.js b/test/sequential/test-child-process-execsync.js index 4dc11253c9a790..4fb2915010ef8d 100644 --- a/test/sequential/test-child-process-execsync.js +++ b/test/sequential/test-child-process-execsync.js @@ -23,8 +23,7 @@ const common = require('../common'); const assert = require('assert'); -const execSync = require('child_process').execSync; -const execFileSync = require('child_process').execFileSync; +const { execFileSync, execSync } = require('child_process'); const TIMER = 200; const SLEEP = 2000; diff --git a/test/sequential/test-module-loading.js b/test/sequential/test-module-loading.js index 64c168f9ebde71..e59ec247828944 100644 --- a/test/sequential/test-module-loading.js +++ b/test/sequential/test-module-loading.js @@ -146,7 +146,7 @@ try { assert.strictEqual(e.message, 'blah'); } -assert.strictEqual(require('path').dirname(__filename), __dirname); +assert.strictEqual(path.dirname(__filename), __dirname); console.error('load custom file types with extensions'); require.extensions['.test'] = function(module, filename) {