Skip to content

Commit

Permalink
crypto: update CryptoKey symbol properties
Browse files Browse the repository at this point in the history
PR-URL: #50897
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
  • Loading branch information
panva authored and richardlau committed Mar 25, 2024
1 parent 07da4e9 commit adb5d69
Showing 1 changed file with 65 additions and 19 deletions.
84 changes: 65 additions & 19 deletions lib/internal/crypto/keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -703,34 +703,83 @@ ObjectDefineProperties(CryptoKey.prototype, {
},
});

/**
* @param {InternalCryptoKey} key
* @param {KeyObject} keyObject
* @param {object} algorithm
* @param {boolean} extractable
* @param {Set<string>} keyUsages
*/
function defineCryptoKeyProperties(
key,
keyObject,
algorithm,
extractable,
keyUsages,
) {
// Using symbol properties here currently instead of private
// properties because (for now) the performance penalty of
// private fields is still too high.
ObjectDefineProperties(key, {
[kKeyObject]: {
__proto__: null,
value: keyObject,
enumerable: false,
configurable: false,
writable: false,
},
[kAlgorithm]: {
__proto__: null,
value: algorithm,
enumerable: false,
configurable: false,
writable: false,
},
[kExtractable]: {
__proto__: null,
value: extractable,
enumerable: false,
configurable: false,
writable: false,
},
[kKeyUsages]: {
__proto__: null,
value: keyUsages,
enumerable: false,
configurable: false,
writable: false,
},
});
}

// All internal code must use new InternalCryptoKey to create
// CryptoKey instances. The CryptoKey class is exposed to end
// user code but is not permitted to be constructed directly.
// Using makeTransferable also allows the CryptoKey to be
// cloned to Workers.
class InternalCryptoKey {
constructor(
keyObject,
algorithm,
keyUsages,
extractable) {
// Using symbol properties here currently instead of private
// properties because (for now) the performance penalty of
// private fields is still too high.
this[kKeyObject] = keyObject;
this[kAlgorithm] = algorithm;
this[kExtractable] = extractable;
this[kKeyUsages] = keyUsages;
constructor(keyObject, algorithm, keyUsages, extractable) {
// When constructed during transfer the properties get assigned
// in the kDeserialize call.
if (keyObject) {
defineCryptoKeyProperties(
this,
keyObject,
algorithm,
extractable,
keyUsages,
);
}

// eslint-disable-next-line no-constructor-return
return makeTransferable(this);
}

[kClone]() {
const keyObject = this[kKeyObject];
const algorithm = this.algorithm;
const extractable = this.extractable;
const usages = this.usages;
const algorithm = this[kAlgorithm];
const extractable = this[kExtractable];
const usages = this[kKeyUsages];

return {
data: {
Expand All @@ -744,10 +793,7 @@ class InternalCryptoKey {
}

[kDeserialize]({ keyObject, algorithm, usages, extractable }) {
this[kKeyObject] = keyObject;
this[kAlgorithm] = algorithm;
this[kKeyUsages] = usages;
this[kExtractable] = extractable;
defineCryptoKeyProperties(this, keyObject, algorithm, extractable, usages);
}
}
InternalCryptoKey.prototype.constructor = CryptoKey;
Expand Down

0 comments on commit adb5d69

Please sign in to comment.