Skip to content

Commit 2b48c71

Browse files
targosBridgeAR
authored andcommittedNov 13, 2018
crypto: put legacy _handle accessors on prototypes
Creating deprecated accessors each time an object is created is very time consuming. Refs: #22747 Fixes: #24266 PR-URL: #24269 Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent 09a8f47 commit 2b48c71

File tree

5 files changed

+34
-17
lines changed

5 files changed

+34
-17
lines changed
 

‎lib/internal/crypto/cipher.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ function getUIntOption(options, key) {
7474
function createCipherBase(cipher, credential, options, decipher, iv) {
7575
const authTagLength = getUIntOption(options, 'authTagLength');
7676

77-
legacyNativeHandle(this, new CipherBase(decipher));
77+
this[kHandle] = new CipherBase(decipher);
7878
if (iv === undefined) {
7979
this[kHandle].init(cipher, credential, authTagLength);
8080
} else {
@@ -219,6 +219,8 @@ Cipher.prototype.setAAD = function setAAD(aadbuf, options) {
219219
return this;
220220
};
221221

222+
legacyNativeHandle(Cipher);
223+
222224
function Cipheriv(cipher, key, iv, options) {
223225
if (!(this instanceof Cipheriv))
224226
return new Cipheriv(cipher, key, iv, options);
@@ -254,6 +256,7 @@ function addCipherPrototypeFunctions(constructor) {
254256

255257
inherits(Cipheriv, LazyTransform);
256258
addCipherPrototypeFunctions(Cipheriv);
259+
legacyNativeHandle(Cipheriv);
257260

258261
function Decipher(cipher, password, options) {
259262
if (!(this instanceof Decipher))
@@ -264,6 +267,7 @@ function Decipher(cipher, password, options) {
264267

265268
inherits(Decipher, LazyTransform);
266269
addCipherPrototypeFunctions(Decipher);
270+
legacyNativeHandle(Decipher);
267271

268272

269273
function Decipheriv(cipher, key, iv, options) {
@@ -275,6 +279,7 @@ function Decipheriv(cipher, key, iv, options) {
275279

276280
inherits(Decipheriv, LazyTransform);
277281
addCipherPrototypeFunctions(Decipheriv);
282+
legacyNativeHandle(Decipheriv);
278283

279284
module.exports = {
280285
Cipher,

‎lib/internal/crypto/diffiehellman.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ function DiffieHellman(sizeOrKey, keyEncoding, generator, genEncoding) {
6161
else if (typeof generator !== 'number')
6262
generator = toBuf(generator, genEncoding);
6363

64-
legacyNativeHandle(this, new _DiffieHellman(sizeOrKey, generator));
64+
this[kHandle] = new _DiffieHellman(sizeOrKey, generator);
6565
Object.defineProperty(this, 'verifyError', {
6666
enumerable: true,
6767
value: this[kHandle].verifyError,
@@ -73,7 +73,7 @@ function DiffieHellman(sizeOrKey, keyEncoding, generator, genEncoding) {
7373
function DiffieHellmanGroup(name) {
7474
if (!(this instanceof DiffieHellmanGroup))
7575
return new DiffieHellmanGroup(name);
76-
legacyNativeHandle(this, new _DiffieHellmanGroup(name));
76+
this[kHandle] = new _DiffieHellmanGroup(name);
7777
Object.defineProperty(this, 'verifyError', {
7878
enumerable: true,
7979
value: this[kHandle].verifyError,
@@ -165,13 +165,16 @@ DiffieHellman.prototype.setPrivateKey = function setPrivateKey(key, encoding) {
165165
return this;
166166
};
167167

168+
legacyNativeHandle(DiffieHellman);
169+
legacyNativeHandle(DiffieHellmanGroup);
170+
168171

169172
function ECDH(curve) {
170173
if (!(this instanceof ECDH))
171174
return new ECDH(curve);
172175

173176
validateString(curve, 'curve');
174-
legacyNativeHandle(this, new _ECDH(curve));
177+
this[kHandle] = new _ECDH(curve);
175178
}
176179

177180
ECDH.prototype.computeSecret = DiffieHellman.prototype.computeSecret;
@@ -192,6 +195,8 @@ ECDH.prototype.getPublicKey = function getPublicKey(encoding, format) {
192195
return encode(key, encoding);
193196
};
194197

198+
legacyNativeHandle(ECDH);
199+
195200
ECDH.convertKey = function convertKey(key, curve, inEnc, outEnc, format) {
196201
if (typeof key !== 'string' && !isArrayBufferView(key)) {
197202
throw new ERR_INVALID_ARG_TYPE(

‎lib/internal/crypto/hash.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ function Hash(algorithm, options) {
3232
if (!(this instanceof Hash))
3333
return new Hash(algorithm, options);
3434
validateString(algorithm, 'algorithm');
35-
legacyNativeHandle(this, new _Hash(algorithm));
35+
this[kHandle] = new _Hash(algorithm);
3636
this[kState] = {
3737
[kFinalized]: false
3838
};
@@ -81,6 +81,8 @@ Hash.prototype.digest = function digest(outputEncoding) {
8181
return ret;
8282
};
8383

84+
legacyNativeHandle(Hash);
85+
8486

8587
function Hmac(hmac, key, options) {
8688
if (!(this instanceof Hmac))
@@ -90,7 +92,7 @@ function Hmac(hmac, key, options) {
9092
throw new ERR_INVALID_ARG_TYPE('key',
9193
['string', 'TypedArray', 'DataView'], key);
9294
}
93-
legacyNativeHandle(this, new _Hmac());
95+
this[kHandle] = new _Hmac();
9496
this[kHandle].init(hmac, toBuf(key));
9597
this[kState] = {
9698
[kFinalized]: false
@@ -122,6 +124,8 @@ Hmac.prototype.digest = function digest(outputEncoding) {
122124
Hmac.prototype._flush = Hash.prototype._flush;
123125
Hmac.prototype._transform = Hash.prototype._transform;
124126

127+
legacyNativeHandle(Hmac);
128+
125129
module.exports = {
126130
Hash,
127131
Hmac

‎lib/internal/crypto/sig.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function Sign(algorithm, options) {
2424
if (!(this instanceof Sign))
2525
return new Sign(algorithm, options);
2626
validateString(algorithm, 'algorithm');
27-
legacyNativeHandle(this, new _Sign());
27+
this[kHandle] = new _Sign();
2828
this[kHandle].init(algorithm);
2929

3030
Writable.call(this, options);
@@ -45,6 +45,8 @@ Sign.prototype.update = function update(data, encoding) {
4545
return this;
4646
};
4747

48+
legacyNativeHandle(Sign);
49+
4850
function getPadding(options) {
4951
return getIntOption('padding', RSA_PKCS1_PADDING, options);
5052
}
@@ -93,7 +95,7 @@ function Verify(algorithm, options) {
9395
if (!(this instanceof Verify))
9496
return new Verify(algorithm, options);
9597
validateString(algorithm, 'algorithm');
96-
legacyNativeHandle(this, new _Verify());
98+
this[kHandle] = new _Verify();
9799
this[kHandle].init(algorithm);
98100

99101
Writable.call(this, options);
@@ -121,6 +123,8 @@ Verify.prototype.verify = function verify(options, signature, sigEncoding) {
121123
return this[kHandle].verify(key, signature, rsaPadding, pssSaltLength);
122124
};
123125

126+
legacyNativeHandle(Verify);
127+
124128
module.exports = {
125129
Sign,
126130
Verify

‎lib/internal/crypto/util.js

+8-9
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,14 @@ const {
3030

3131
const kHandle = Symbol('kHandle');
3232

33-
function legacyNativeHandle(obj, handle) {
34-
obj[kHandle] = handle;
35-
Object.defineProperty(obj, '_handle', {
36-
get: deprecate(() => handle,
37-
`${obj.constructor.name}._handle is deprecated. Use the ` +
38-
'public API instead.', 'DEP0117'),
39-
set: deprecate((h) => obj[kHandle] = handle = h,
40-
`${obj.constructor.name}._handle is deprecated. Use the ` +
41-
'public API instead.', 'DEP0117'),
33+
function legacyNativeHandle(clazz) {
34+
Object.defineProperty(clazz.prototype, '_handle', {
35+
get: deprecate(function() { return this[kHandle]; },
36+
`${clazz.name}._handle is deprecated. Use the public API ` +
37+
'instead.', 'DEP0117'),
38+
set: deprecate(function(h) { this[kHandle] = h; },
39+
`${clazz.name}._handle is deprecated. Use the public API ` +
40+
'instead.', 'DEP0117'),
4241
enumerable: false
4342
});
4443
}

0 commit comments

Comments
 (0)