Skip to content

Commit

Permalink
crypto: fix CryptoKey prototype WPT
Browse files Browse the repository at this point in the history
  • Loading branch information
panva committed Dec 14, 2022
1 parent cf0a42c commit 6248494
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 26 deletions.
51 changes: 26 additions & 25 deletions lib/internal/crypto/keys.js
Expand Up @@ -54,7 +54,7 @@ const {
} = require('internal/util/types');

const {
JSTransferable,
makeTransferable,
kClone,
kDeserialize,
} = require('internal/worker/js_transferable');
Expand Down Expand Up @@ -627,14 +627,12 @@ function isKeyObject(obj) {
}

// Our implementation of CryptoKey is a simple wrapper around a KeyObject
// that adapts it to the standard interface. This implementation also
// extends the JSTransferable class, allowing the CryptoKey to be cloned
// to Workers.
// that adapts it to the standard interface.
// TODO(@jasnell): Embedder environments like electron may have issues
// here similar to other things like URL. A chromium provided CryptoKey
// will not be recognized as a Node.js CryptoKey, and vice versa. It
// would be fantastic if we could find a way of making those interop.
class CryptoKey extends JSTransferable {
class CryptoKey {
constructor() {
throw new ERR_ILLEGAL_CONSTRUCTOR();
}
Expand Down Expand Up @@ -671,6 +669,29 @@ class CryptoKey extends JSTransferable {
get usages() {
return ArrayFrom(this[kKeyUsages]);
}
}

// 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;

return makeTransferable(this);
}

[kClone]() {
const keyObject = this[kKeyObject];
Expand All @@ -697,26 +718,6 @@ class CryptoKey extends JSTransferable {
}
}

// 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.
class InternalCryptoKey extends JSTransferable {
constructor(
keyObject,
algorithm,
keyUsages,
extractable) {
super();
// 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;
}
}

InternalCryptoKey.prototype.constructor = CryptoKey;
ObjectSetPrototypeOf(InternalCryptoKey.prototype, CryptoKey.prototype);

Expand Down
1 change: 0 additions & 1 deletion test/wpt/status/WebCryptoAPI.json
Expand Up @@ -11,7 +11,6 @@
"Crypto interface: existence and properties of interface object",
"Crypto interface: calling getRandomValues(ArrayBufferView) on crypto with too few arguments must throw TypeError",
"CryptoKey interface: existence and properties of interface object",
"CryptoKey interface: existence and properties of interface prototype object",
"CryptoKey interface: attribute type",
"CryptoKey interface: attribute extractable",
"CryptoKey interface: attribute algorithm",
Expand Down

0 comments on commit 6248494

Please sign in to comment.