Skip to content

Commit

Permalink
streams: refactor LazyTransform to internal/
Browse files Browse the repository at this point in the history
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: #2566
Reviewed-By: Fedor Indutny <fedor.indutny@gmail.com>
  • Loading branch information
brendanashworth authored and Fishrock123 committed Sep 15, 2015
1 parent 6108ea9 commit eaa8e60
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 31 deletions.
32 changes: 1 addition & 31 deletions lib/crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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))
Expand Down
39 changes: 39 additions & 0 deletions lib/internal/streams/lazy_transform.js
Original file line number Diff line number Diff line change
@@ -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
});
});
1 change: 1 addition & 0 deletions node.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
'lib/internal/socket_list.js',
'lib/internal/repl.js',
'lib/internal/util.js',
'lib/internal/streams/lazy_transform.js',
],
},

Expand Down

0 comments on commit eaa8e60

Please sign in to comment.