From a7c1d0908a7deb308cfe3b1ad9a54f7bc630905b Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Sat, 17 Nov 2018 00:18:55 +0800 Subject: [PATCH] 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. PR-URL: https://github.com/nodejs/node/pull/24396 Reviewed-By: Refael Ackermann Reviewed-By: Sam Roberts Reviewed-By: Anna Henningsen Reviewed-By: Tiancheng "Timothy" Gu Reviewed-By: Colin Ihrig --- 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];