diff options
author | Leo K <crowlkats@toaxl.com> | 2021-06-05 22:56:59 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-06-05 22:56:59 +0200 |
commit | bb0c90cadbb99784681a2acac1fd65ac7f802297 (patch) | |
tree | 6d5e1f668ee54c3ae6ed5ff63f987499f882a6cf /extensions/crypto/01_crypto.js | |
parent | 368c784d7f6feff6bb483ca2b02cf2c44ea29c26 (diff) |
fix(crypto): change Crypto to interface (#10853)
Co-authored-by: Luca Casonato <hello@lcas.dev>
Diffstat (limited to 'extensions/crypto/01_crypto.js')
-rw-r--r-- | extensions/crypto/01_crypto.js | 84 |
1 files changed, 49 insertions, 35 deletions
diff --git a/extensions/crypto/01_crypto.js b/extensions/crypto/01_crypto.js index f18b6d5aa..2b3982c32 100644 --- a/extensions/crypto/01_crypto.js +++ b/extensions/crypto/01_crypto.js @@ -3,48 +3,62 @@ ((window) => { const core = window.Deno.core; + const webidl = window.__bootstrap.webidl; - function getRandomValues(arrayBufferView) { - if (!ArrayBuffer.isView(arrayBufferView)) { - throw new TypeError( - "Argument 1 does not implement interface ArrayBufferView", - ); + class Crypto { + constructor() { + webidl.illegalConstructor(); } - if ( - !( - arrayBufferView instanceof Int8Array || - arrayBufferView instanceof Uint8Array || - arrayBufferView instanceof Int16Array || - arrayBufferView instanceof Uint16Array || - arrayBufferView instanceof Int32Array || - arrayBufferView instanceof Uint32Array || - arrayBufferView instanceof Uint8ClampedArray - ) - ) { - throw new DOMException( - "The provided ArrayBufferView is not an integer array type", - "TypeMismatchError", + + getRandomValues(arrayBufferView) { + webidl.assertBranded(this, Crypto); + const prefix = "Failed to execute 'getRandomValues' on 'Crypto'"; + webidl.requiredArguments(arguments.length, 1, { prefix }); + arrayBufferView = webidl.converters.ArrayBufferView(arrayBufferView, { + prefix, + context: "Argument 1", + }); + if ( + !( + arrayBufferView instanceof Int8Array || + arrayBufferView instanceof Uint8Array || + arrayBufferView instanceof Int16Array || + arrayBufferView instanceof Uint16Array || + arrayBufferView instanceof Int32Array || + arrayBufferView instanceof Uint32Array || + arrayBufferView instanceof Uint8ClampedArray + ) + ) { + throw new DOMException( + "The provided ArrayBufferView is not an integer array type", + "TypeMismatchError", + ); + } + const ui8 = new Uint8Array( + arrayBufferView.buffer, + arrayBufferView.byteOffset, + arrayBufferView.byteLength, ); + core.opSync("op_crypto_get_random_values", ui8); + return arrayBufferView; + } + + randomUUID() { + webidl.assertBranded(this, Crypto); + return core.opSync("op_crypto_random_uuid"); } - const ui8 = new Uint8Array( - arrayBufferView.buffer, - arrayBufferView.byteOffset, - arrayBufferView.byteLength, - ); - core.opSync("op_crypto_get_random_values", ui8); - return arrayBufferView; - } - function randomUUID() { - return core.opSync("op_crypto_random_uuid"); + get [Symbol.toStringTag]() { + return "Crypto"; + } + + [Symbol.for("Deno.customInspect")](inspect) { + return `${this.constructor.name} ${inspect({})}`; + } } - window.crypto = { - getRandomValues, - randomUUID, - }; window.__bootstrap.crypto = { - getRandomValues, - randomUUID, + crypto: webidl.createBranded(Crypto), + Crypto, }; })(this); |