blob: 611f9f6a26b098cedf4768702c6de6fda2167d41 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
"use strict";
const nodeCrypto = require("crypto");
const DOMException = require("../generated/DOMException");
// https://w3c.github.io/webcrypto/#crypto-interface
class CryptoImpl {
constructor(globalObject) {
this._globalObject = globalObject;
}
// https://w3c.github.io/webcrypto/#Crypto-method-getRandomValues
getRandomValues(array) {
const typeName = getTypedArrayTypeName(array);
if (!(typeName === "Int8Array" ||
typeName === "Uint8Array" ||
typeName === "Uint8ClampedArray" ||
typeName === "Int16Array" ||
typeName === "Uint16Array" ||
typeName === "Int32Array" ||
typeName === "Uint32Array" ||
typeName === "BigInt64Array" ||
typeName === "BigUint64Array")) {
throw DOMException.create(this._globalObject, [
`getRandomValues() only accepts integer typed arrays`,
"TypeMismatchError"
]);
}
if (array.byteLength > 65536) {
throw DOMException.create(this._globalObject, [
`getRandomValues() cannot generate more than 65536 bytes of random values; ` +
`${array.byteLength} bytes were requested`,
"QuotaExceededError"
]);
}
nodeCrypto.randomFillSync(array);
return array;
}
// https://w3c.github.io/webcrypto/#Crypto-method-randomUUID
randomUUID() {
return nodeCrypto.randomUUID();
}
}
exports.implementation = CryptoImpl;
// See #3395. Subclasses of TypedArrays should properly work, but we can't rely
// on instanceof because Uint8Array may be different across different windows -
// which can happen in JSDOM when running { runScripts: "dangerously" }. As a
// solution, we imitate the behavior of instanceof by walking the proottype
// chain.
function getTypedArrayTypeName(array) {
const target = array.constructor;
const chain = [target.name];
let proto = Object.getPrototypeOf(target);
while (proto) {
chain.push(proto.name);
proto = Object.getPrototypeOf(proto);
}
while (chain.length > 0 && chain[chain.length - 1] !== "TypedArray") {
chain.pop();
}
chain.reverse();
return chain[1];
}
|