From 7a97f3f43b8f4b63ea3e7105550694fc02c4691a Mon Sep 17 00:00:00 2001 From: Filip Skokan Date: Wed, 14 Dec 2022 11:04:15 +0100 Subject: [PATCH] crypto: fix CryptoKey prototype WPT PR-URL: https://github.com/nodejs/node/pull/45857 Reviewed-By: Antoine du Hamel Reviewed-By: James M Snell --- lib/internal/crypto/keys.js | 65 +++++++++++++++++++------------------ 1 file changed, 33 insertions(+), 32 deletions(-) diff --git a/lib/internal/crypto/keys.js b/lib/internal/crypto/keys.js index 5e4ab819af5b3d..968116d6bf2ea7 100644 --- a/lib/internal/crypto/keys.js +++ b/lib/internal/crypto/keys.js @@ -56,7 +56,7 @@ const { } = require('internal/util/types'); const { - JSTransferable, + makeTransferable, kClone, kDeserialize, } = require('internal/worker/js_transferable'); @@ -630,14 +630,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(); } @@ -682,30 +680,6 @@ class CryptoKey extends JSTransferable { throw new ERR_INVALID_THIS('CryptoKey'); return ArrayFrom(this[kKeyUsages]); } - - [kClone]() { - const keyObject = this[kKeyObject]; - const algorithm = this.algorithm; - const extractable = this.extractable; - const usages = this.usages; - - return { - data: { - keyObject, - algorithm, - usages, - extractable, - }, - deserializeInfo: 'internal/crypto/keys:InternalCryptoKey' - }; - } - - [kDeserialize]({ keyObject, algorithm, usages, extractable }) { - this[kKeyObject] = keyObject; - this[kAlgorithm] = algorithm; - this[kKeyUsages] = usages; - this[kExtractable] = extractable; - } } ObjectDefineProperties(CryptoKey.prototype, { @@ -718,13 +692,14 @@ ObjectDefineProperties(CryptoKey.prototype, { // 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 { +// Using makeTransferable also allows the CryptoKey to be +// cloned to Workers. +class InternalCryptoKey { 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. @@ -732,9 +707,35 @@ class InternalCryptoKey extends JSTransferable { this[kAlgorithm] = algorithm; this[kExtractable] = extractable; this[kKeyUsages] = 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; + + return { + data: { + keyObject, + algorithm, + usages, + extractable, + }, + deserializeInfo: 'internal/crypto/keys:InternalCryptoKey' + }; + } + + [kDeserialize]({ keyObject, algorithm, usages, extractable }) { + this[kKeyObject] = keyObject; + this[kAlgorithm] = algorithm; + this[kKeyUsages] = usages; + this[kExtractable] = extractable; + } +} InternalCryptoKey.prototype.constructor = CryptoKey; ObjectSetPrototypeOf(InternalCryptoKey.prototype, CryptoKey.prototype);