From 152a8f6e3d1a1af4245cc408a60975530bacf18b Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Sat, 17 Nov 2018 00:18:55 +0800 Subject: [PATCH 1/3] stream: do not use crypto.DEFAULT_ENCODING in lazy_transform.js The default encoding can be retrieved via `require('internal/crypto/util').getDefaultEncoding` instead of the deprecated crypto.DEFAULT_ENCODING which triggers a warning. Background: The require chain goes like this: ``` internal/streams/lazy_transform.js -> crypto.js -> internal/crypto/cipher.js (uses LazyTransform in the global scope) -> internal/streams/lazy_transform.js ``` So when `internal/streams/lazy_transform.js` is required before `lib/crypto.js`, we have a circular dependency and since `internal/crypto/cipher.js` uses destructuring to use LazyTransform we will get an error. And it can also trigger a warning if lazy_transform.js is the first file that touches crypto.DEFAULT_ENCODING. --- lib/internal/streams/lazy_transform.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/internal/streams/lazy_transform.js b/lib/internal/streams/lazy_transform.js index 80e0bacf29df7c..c4d8c64b3dd041 100644 --- a/lib/internal/streams/lazy_transform.js +++ b/lib/internal/streams/lazy_transform.js @@ -5,7 +5,10 @@ const stream = require('stream'); const util = require('util'); -const crypto = require('crypto'); + +const { + getDefaultEncoding +} = require('internal/crypto/util'); module.exports = LazyTransform; @@ -22,7 +25,7 @@ function makeGetter(name) { this._writableState.decodeStrings = false; if (!this._options || !this._options.defaultEncoding) { - this._writableState.defaultEncoding = crypto.DEFAULT_ENCODING; + this._writableState.defaultEncoding = getDefaultEncoding(); } return this[name]; From 987fe8772a15c4dc75d0973b8bebc14af072c66b Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Sat, 17 Nov 2018 04:35:03 +0800 Subject: [PATCH 2/3] test: add a test to make sure the modules can be required independently This patch adds a test that makes sure all the modules (internal or not) can be independently required - that is, when there is circular dependency, one module does not rely on another module being loaded first to initialize, instead it should lazily load that dependency after initialization. --- test/sequential/test-native-module-deps.js | 43 ++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 test/sequential/test-native-module-deps.js diff --git a/test/sequential/test-native-module-deps.js b/test/sequential/test-native-module-deps.js new file mode 100644 index 00000000000000..706d566e583b69 --- /dev/null +++ b/test/sequential/test-native-module-deps.js @@ -0,0 +1,43 @@ +'use strict'; + +// This tests that all the native modules can be loaded independently +// Flags: --expose-internals + +if (process.argv[3]) { + require(process.argv[3]); + return; +} + +require('../common'); +const { + cachableBuiltins +} = require('internal/bootstrap/cache'); +const { fork } = require('child_process'); +const assert = require('assert'); + +for (const key of cachableBuiltins) { + run(key); +} + +function run(key) { + const child = fork(__filename, + [ '--expose-internals', key ], + { silent: true } + ); + + let stdout = ''; + let stderr = ''; + child.stdout.on('data', (data) => (stdout += data.toString())); + child.stderr.on('data', (data) => (stderr += data.toString())); + child.on('close', (code) => { + if (code === 0) { + return; + } + console.log(`Failed to require ${key}`) + console.log('----- stderr ----') + console.log(stderr); + console.log('----- stdout ----') + console.log(stdout); + assert.strictEqual(code, 0); + }); +} From 7925ff04a5eb99778b96e400fb9fde8b9e1ebf62 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Thu, 22 Nov 2018 16:20:45 +0900 Subject: [PATCH 3/3] fixup! test: add a test to make sure the modules can be required independently --- test/sequential/test-native-module-deps.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test/sequential/test-native-module-deps.js b/test/sequential/test-native-module-deps.js index 706d566e583b69..f01c55a7c36822 100644 --- a/test/sequential/test-native-module-deps.js +++ b/test/sequential/test-native-module-deps.js @@ -21,8 +21,8 @@ for (const key of cachableBuiltins) { function run(key) { const child = fork(__filename, - [ '--expose-internals', key ], - { silent: true } + [ '--expose-internals', key ], + { silent: true } ); let stdout = ''; @@ -33,10 +33,10 @@ function run(key) { if (code === 0) { return; } - console.log(`Failed to require ${key}`) - console.log('----- stderr ----') + console.log(`Failed to require ${key}`); + console.log('----- stderr ----'); console.log(stderr); - console.log('----- stdout ----') + console.log('----- stdout ----'); console.log(stdout); assert.strictEqual(code, 0); });