Skip to content

Commit eaa8e60

Browse files
brendanashworthFishrock123
authored andcommitted
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: #2566 Reviewed-By: Fedor Indutny <[email protected]>
1 parent 6108ea9 commit eaa8e60

File tree

3 files changed

+41
-31
lines changed

3 files changed

+41
-31
lines changed

lib/crypto.js

+1-31
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const constants = require('constants');
2020
const stream = require('stream');
2121
const util = require('util');
2222
const internalUtil = require('internal/util');
23+
const LazyTransform = require('internal/streams/lazy_transform');
2324

2425
const DH_GENERATOR = 2;
2526

@@ -42,37 +43,6 @@ const assert = require('assert');
4243
const StringDecoder = require('string_decoder').StringDecoder;
4344

4445

45-
function LazyTransform(options) {
46-
this._options = options;
47-
}
48-
util.inherits(LazyTransform, stream.Transform);
49-
50-
[
51-
'_readableState',
52-
'_writableState',
53-
'_transformState'
54-
].forEach(function(prop, i, props) {
55-
Object.defineProperty(LazyTransform.prototype, prop, {
56-
get: function() {
57-
stream.Transform.call(this, this._options);
58-
this._writableState.decodeStrings = false;
59-
this._writableState.defaultEncoding = 'binary';
60-
return this[prop];
61-
},
62-
set: function(val) {
63-
Object.defineProperty(this, prop, {
64-
value: val,
65-
enumerable: true,
66-
configurable: true,
67-
writable: true
68-
});
69-
},
70-
configurable: true,
71-
enumerable: true
72-
});
73-
});
74-
75-
7646
exports.createHash = exports.Hash = Hash;
7747
function Hash(algorithm, options) {
7848
if (!(this instanceof Hash))
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// LazyTransform is a special type of Transform stream that is lazily loaded.
2+
// This is used for performance with bi-API-ship: when two APIs are available
3+
// for the stream, one conventional and one non-conventional.
4+
'use strict';
5+
6+
const stream = require('stream');
7+
const util = require('util');
8+
9+
module.exports = LazyTransform;
10+
11+
function LazyTransform(options) {
12+
this._options = options;
13+
}
14+
util.inherits(LazyTransform, stream.Transform);
15+
16+
[
17+
'_readableState',
18+
'_writableState',
19+
'_transformState'
20+
].forEach(function(prop, i, props) {
21+
Object.defineProperty(LazyTransform.prototype, prop, {
22+
get: function() {
23+
stream.Transform.call(this, this._options);
24+
this._writableState.decodeStrings = false;
25+
this._writableState.defaultEncoding = 'binary';
26+
return this[prop];
27+
},
28+
set: function(val) {
29+
Object.defineProperty(this, prop, {
30+
value: val,
31+
enumerable: true,
32+
configurable: true,
33+
writable: true
34+
});
35+
},
36+
configurable: true,
37+
enumerable: true
38+
});
39+
});

node.gyp

+1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
'lib/internal/socket_list.js',
7474
'lib/internal/repl.js',
7575
'lib/internal/util.js',
76+
'lib/internal/streams/lazy_transform.js',
7677
],
7778
},
7879

0 commit comments

Comments
 (0)