From eaa8e60b91eaedd65befa62a4493c110c5f4a148 Mon Sep 17 00:00:00 2001 From: Brendan Ashworth Date: Wed, 26 Aug 2015 14:41:28 -0700 Subject: [PATCH] streams: refactor LazyTransform to internal/ This commit refactors LazyTransform from the crypto implementation (lib/crypto.js) into an internal module (not publicy accessible) in internal/streams/lazy_transform.js. This promotes a more modular core design and removes code bloat in crypto, as LazyTransform didn't specifically have anything to do with cryptography, but rather a fast way to support two APIs on a stream. PR-URL: https://github.com/nodejs/node/pull/2566 Reviewed-By: Fedor Indutny --- lib/crypto.js | 32 +--------------------------- lib/internal/streams/lazy_transform.js | 39 ++++++++++++++++++++++++++++++++++ node.gyp | 1 + 3 files changed, 41 insertions(+), 31 deletions(-) create mode 100644 lib/internal/streams/lazy_transform.js diff --git a/lib/crypto.js b/lib/crypto.js index bfe7837..b32d9af 100644 --- a/lib/crypto.js +++ b/lib/crypto.js @@ -20,6 +20,7 @@ const constants = require('constants'); const stream = require('stream'); const util = require('util'); const internalUtil = require('internal/util'); +const LazyTransform = require('internal/streams/lazy_transform'); const DH_GENERATOR = 2; @@ -42,37 +43,6 @@ const assert = require('assert'); const StringDecoder = require('string_decoder').StringDecoder; -function LazyTransform(options) { - this._options = options; -} -util.inherits(LazyTransform, stream.Transform); - -[ - '_readableState', - '_writableState', - '_transformState' -].forEach(function(prop, i, props) { - Object.defineProperty(LazyTransform.prototype, prop, { - get: function() { - stream.Transform.call(this, this._options); - this._writableState.decodeStrings = false; - this._writableState.defaultEncoding = 'binary'; - return this[prop]; - }, - set: function(val) { - Object.defineProperty(this, prop, { - value: val, - enumerable: true, - configurable: true, - writable: true - }); - }, - configurable: true, - enumerable: true - }); -}); - - exports.createHash = exports.Hash = Hash; function Hash(algorithm, options) { if (!(this instanceof Hash)) diff --git a/lib/internal/streams/lazy_transform.js b/lib/internal/streams/lazy_transform.js new file mode 100644 index 0000000..7e290b1 --- /dev/null +++ b/lib/internal/streams/lazy_transform.js @@ -0,0 +1,39 @@ +// LazyTransform is a special type of Transform stream that is lazily loaded. +// This is used for performance with bi-API-ship: when two APIs are available +// for the stream, one conventional and one non-conventional. +'use strict'; + +const stream = require('stream'); +const util = require('util'); + +module.exports = LazyTransform; + +function LazyTransform(options) { + this._options = options; +} +util.inherits(LazyTransform, stream.Transform); + +[ + '_readableState', + '_writableState', + '_transformState' +].forEach(function(prop, i, props) { + Object.defineProperty(LazyTransform.prototype, prop, { + get: function() { + stream.Transform.call(this, this._options); + this._writableState.decodeStrings = false; + this._writableState.defaultEncoding = 'binary'; + return this[prop]; + }, + set: function(val) { + Object.defineProperty(this, prop, { + value: val, + enumerable: true, + configurable: true, + writable: true + }); + }, + configurable: true, + enumerable: true + }); +}); diff --git a/node.gyp b/node.gyp index 0bc27ae..e7153ba 100644 --- a/node.gyp +++ b/node.gyp @@ -73,6 +73,7 @@ 'lib/internal/socket_list.js', 'lib/internal/repl.js', 'lib/internal/util.js', + 'lib/internal/streams/lazy_transform.js', ], }, -- 2.7.4