0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-31 03:36:06 -05:00
denoland-deno/extensions/crypto/01_crypto.js
Leo K bb0c90cadb
fix(crypto): change Crypto to interface (#10853)
Co-authored-by: Luca Casonato <hello@lcas.dev>
2021-06-05 22:56:59 +02:00

64 lines
1.8 KiB
JavaScript

// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
"use strict";
((window) => {
const core = window.Deno.core;
const webidl = window.__bootstrap.webidl;
class Crypto {
constructor() {
webidl.illegalConstructor();
}
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");
}
get [Symbol.toStringTag]() {
return "Crypto";
}
[Symbol.for("Deno.customInspect")](inspect) {
return `${this.constructor.name} ${inspect({})}`;
}
}
window.__bootstrap.crypto = {
crypto: webidl.createBranded(Crypto),
Crypto,
};
})(this);