Skip to content

Commit adb5d69

Browse files
panvarichardlau
authored andcommitted
crypto: update CryptoKey symbol properties
PR-URL: #50897 Reviewed-By: Antoine du Hamel <[email protected]>
1 parent 07da4e9 commit adb5d69

File tree

1 file changed

+65
-19
lines changed

1 file changed

+65
-19
lines changed

lib/internal/crypto/keys.js

+65-19
Original file line numberDiff line numberDiff line change
@@ -703,34 +703,83 @@ ObjectDefineProperties(CryptoKey.prototype, {
703703
},
704704
});
705705

706+
/**
707+
* @param {InternalCryptoKey} key
708+
* @param {KeyObject} keyObject
709+
* @param {object} algorithm
710+
* @param {boolean} extractable
711+
* @param {Set<string>} keyUsages
712+
*/
713+
function defineCryptoKeyProperties(
714+
key,
715+
keyObject,
716+
algorithm,
717+
extractable,
718+
keyUsages,
719+
) {
720+
// Using symbol properties here currently instead of private
721+
// properties because (for now) the performance penalty of
722+
// private fields is still too high.
723+
ObjectDefineProperties(key, {
724+
[kKeyObject]: {
725+
__proto__: null,
726+
value: keyObject,
727+
enumerable: false,
728+
configurable: false,
729+
writable: false,
730+
},
731+
[kAlgorithm]: {
732+
__proto__: null,
733+
value: algorithm,
734+
enumerable: false,
735+
configurable: false,
736+
writable: false,
737+
},
738+
[kExtractable]: {
739+
__proto__: null,
740+
value: extractable,
741+
enumerable: false,
742+
configurable: false,
743+
writable: false,
744+
},
745+
[kKeyUsages]: {
746+
__proto__: null,
747+
value: keyUsages,
748+
enumerable: false,
749+
configurable: false,
750+
writable: false,
751+
},
752+
});
753+
}
754+
706755
// All internal code must use new InternalCryptoKey to create
707756
// CryptoKey instances. The CryptoKey class is exposed to end
708757
// user code but is not permitted to be constructed directly.
709758
// Using makeTransferable also allows the CryptoKey to be
710759
// cloned to Workers.
711760
class InternalCryptoKey {
712-
constructor(
713-
keyObject,
714-
algorithm,
715-
keyUsages,
716-
extractable) {
717-
// Using symbol properties here currently instead of private
718-
// properties because (for now) the performance penalty of
719-
// private fields is still too high.
720-
this[kKeyObject] = keyObject;
721-
this[kAlgorithm] = algorithm;
722-
this[kExtractable] = extractable;
723-
this[kKeyUsages] = keyUsages;
761+
constructor(keyObject, algorithm, keyUsages, extractable) {
762+
// When constructed during transfer the properties get assigned
763+
// in the kDeserialize call.
764+
if (keyObject) {
765+
defineCryptoKeyProperties(
766+
this,
767+
keyObject,
768+
algorithm,
769+
extractable,
770+
keyUsages,
771+
);
772+
}
724773

725774
// eslint-disable-next-line no-constructor-return
726775
return makeTransferable(this);
727776
}
728777

729778
[kClone]() {
730779
const keyObject = this[kKeyObject];
731-
const algorithm = this.algorithm;
732-
const extractable = this.extractable;
733-
const usages = this.usages;
780+
const algorithm = this[kAlgorithm];
781+
const extractable = this[kExtractable];
782+
const usages = this[kKeyUsages];
734783

735784
return {
736785
data: {
@@ -744,10 +793,7 @@ class InternalCryptoKey {
744793
}
745794

746795
[kDeserialize]({ keyObject, algorithm, usages, extractable }) {
747-
this[kKeyObject] = keyObject;
748-
this[kAlgorithm] = algorithm;
749-
this[kKeyUsages] = usages;
750-
this[kExtractable] = extractable;
796+
defineCryptoKeyProperties(this, keyObject, algorithm, extractable, usages);
751797
}
752798
}
753799
InternalCryptoKey.prototype.constructor = CryptoKey;

0 commit comments

Comments
 (0)