Skip to content

Commit 855caa8

Browse files
indutnyisaacs
authored andcommitted
crypto: initialize transform lazily
1 parent 008ab12 commit 855caa8

File tree

1 file changed

+30
-12
lines changed

1 file changed

+30
-12
lines changed

lib/crypto.js

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -150,15 +150,33 @@ exports.createCredentials = function(options, context) {
150150
};
151151

152152

153+
function LazyTransform(options) {
154+
this._options = options;
155+
}
156+
util.inherits(LazyTransform, stream.Transform);
157+
158+
['read', 'write', 'end'].forEach(function(action, i, actions) {
159+
LazyTransform.prototype[action] = function() {
160+
stream.Transform.call(this, this._options);
161+
162+
actions.forEach(function(action) {
163+
this[action] = stream.Transform.prototype[action];
164+
}, this);
165+
166+
return this[action].apply(this, arguments);
167+
};
168+
});
169+
170+
153171
exports.createHash = exports.Hash = Hash;
154172
function Hash(algorithm, options) {
155173
if (!(this instanceof Hash))
156174
return new Hash(algorithm);
157175
this._binding = new binding.Hash(algorithm);
158-
stream.Transform.call(this, options);
176+
LazyTransform.call(this, options);
159177
}
160178

161-
util.inherits(Hash, stream.Transform);
179+
util.inherits(Hash, LazyTransform);
162180

163181
Hash.prototype._transform = function(chunk, encoding, callback) {
164182
this._binding.update(chunk, encoding);
@@ -194,10 +212,10 @@ function Hmac(hmac, key, options) {
194212
return new Hmac(hmac, key);
195213
this._binding = new binding.Hmac();
196214
this._binding.init(hmac, toBuf(key));
197-
stream.Transform.call(this, options);
215+
LazyTransform.call(this, options);
198216
}
199217

200-
util.inherits(Hmac, stream.Transform);
218+
util.inherits(Hmac, LazyTransform);
201219

202220
Hmac.prototype.update = Hash.prototype.update;
203221
Hmac.prototype.digest = Hash.prototype.digest;
@@ -221,10 +239,10 @@ function Cipher(cipher, password, options) {
221239
this._binding.init(cipher, toBuf(password));
222240
this._decoder = null;
223241

224-
stream.Transform.call(this, options);
242+
LazyTransform.call(this, options);
225243
}
226244

227-
util.inherits(Cipher, stream.Transform);
245+
util.inherits(Cipher, LazyTransform);
228246

229247
Cipher.prototype._transform = function(chunk, encoding, callback) {
230248
this.push(this._binding.update(chunk, encoding));
@@ -280,10 +298,10 @@ function Cipheriv(cipher, key, iv, options) {
280298
this._binding.initiv(cipher, toBuf(key), toBuf(iv));
281299
this._decoder = null;
282300

283-
stream.Transform.call(this, options);
301+
LazyTransform.call(this, options);
284302
}
285303

286-
util.inherits(Cipheriv, stream.Transform);
304+
util.inherits(Cipheriv, LazyTransform);
287305

288306
Cipheriv.prototype._transform = Cipher.prototype._transform;
289307
Cipheriv.prototype._flush = Cipher.prototype._flush;
@@ -302,10 +320,10 @@ function Decipher(cipher, password, options) {
302320
this._binding.init(cipher, toBuf(password));
303321
this._decoder = null;
304322

305-
stream.Transform.call(this, options);
323+
LazyTransform.call(this, options);
306324
}
307325

308-
util.inherits(Decipher, stream.Transform);
326+
util.inherits(Decipher, LazyTransform);
309327

310328
Decipher.prototype._transform = Cipher.prototype._transform;
311329
Decipher.prototype._flush = Cipher.prototype._flush;
@@ -325,10 +343,10 @@ function Decipheriv(cipher, key, iv, options) {
325343
this._binding.initiv(cipher, toBuf(key), toBuf(iv));
326344
this._decoder = null;
327345

328-
stream.Transform.call(this, options);
346+
LazyTransform.call(this, options);
329347
}
330348

331-
util.inherits(Decipheriv, stream.Transform);
349+
util.inherits(Decipheriv, LazyTransform);
332350

333351
Decipheriv.prototype._transform = Cipher.prototype._transform;
334352
Decipheriv.prototype._flush = Cipher.prototype._flush;

0 commit comments

Comments
 (0)