Skip to content

Commit 136c2c4

Browse files
authored
Implement crypto.getRandomValues()
1 parent 2e35526 commit 136c2c4

File tree

6 files changed

+67
-0
lines changed

6 files changed

+67
-0
lines changed

lib/jsdom/browser/Window.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const External = require("../living/generated/External");
2525
const Navigator = require("../living/generated/Navigator");
2626
const Performance = require("../living/generated/Performance");
2727
const Screen = require("../living/generated/Screen");
28+
const Crypto = require("../living/generated/Crypto");
2829
const Storage = require("../living/generated/Storage");
2930
const Selection = require("../living/generated/Selection");
3031
const reportException = require("../living/helpers/runtime-script-errors");
@@ -339,6 +340,7 @@ function Window(options) {
339340
const navigator = Navigator.create(window, [], { userAgent: this._resourceLoader._userAgent });
340341
const performance = Performance.create(window, [], { rawPerformance });
341342
const screen = Screen.create(window);
343+
const crypto = Crypto.create(window);
342344
const customElementRegistry = CustomElementRegistry.create(window);
343345

344346
define(this, {
@@ -402,6 +404,9 @@ function Window(options) {
402404
get screen() {
403405
return screen;
404406
},
407+
get crypto() {
408+
return crypto;
409+
},
405410
get origin() {
406411
return window._origin;
407412
},
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
"use strict";
2+
3+
const nodeCrypto = require("crypto");
4+
const DOMException = require("domexception/webidl2js-wrapper");
5+
6+
// https://w3c.github.io/webcrypto/#crypto-interface
7+
class CryptoImpl {
8+
constructor(globalObject) {
9+
this._globalObject = globalObject;
10+
}
11+
12+
// https://w3c.github.io/webcrypto/#Crypto-method-getRandomValues
13+
getRandomValues(array) {
14+
// Note: this rejects Float32Array, Float64Array and DataView.
15+
//
16+
// We perform "instance tests" by comparing `array.constructor.name` so
17+
// that the tests will be successful across realms.
18+
const typeName = array.constructor.name;
19+
if (!(typeName === "Int8Array" ||
20+
typeName === "Uint8Array" ||
21+
typeName === "Uint8ClampedArray" ||
22+
typeName === "Int16Array" ||
23+
typeName === "Uint16Array" ||
24+
typeName === "Int32Array" ||
25+
typeName === "Uint32Array" ||
26+
typeName === "BigInt64Array" ||
27+
typeName === "BigUint64Array")) {
28+
throw DOMException.create(this._globalObject, [
29+
`getRandomValues() only accepts integer typed arrays`,
30+
"TypeMismatchError"
31+
]);
32+
}
33+
34+
if (array.byteLength > 65536) {
35+
throw DOMException.create(this._globalObject, [
36+
`getRandomValues() cannot generate more than 65536 bytes of random values; ` +
37+
`${array.byteLength} bytes were requested`,
38+
"QuotaExceededError"
39+
]);
40+
}
41+
42+
nodeCrypto.randomFillSync(array);
43+
return array;
44+
}
45+
}
46+
47+
exports.implementation = CryptoImpl;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[Exposed=(Window,Worker)]
2+
interface Crypto {
3+
ArrayBufferView getRandomValues(ArrayBufferView array);
4+
};

lib/jsdom/living/interfaces.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ const generatedInterfaces = {
146146
Performance: require("./generated/Performance"),
147147
Navigator: require("./generated/Navigator"),
148148

149+
Crypto: require("./generated/Crypto"),
150+
149151
PluginArray: require("./generated/PluginArray"),
150152
MimeTypeArray: require("./generated/MimeTypeArray"),
151153
Plugin: require("./generated/Plugin"),

scripts/webidl/convert.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ function addDir(dir) {
146146
addDir("../../lib/jsdom/living/aborting");
147147
addDir("../../lib/jsdom/living/attributes");
148148
addDir("../../lib/jsdom/living/constraint-validation");
149+
addDir("../../lib/jsdom/living/crypto");
149150
addDir("../../lib/jsdom/living/cssom");
150151
addDir("../../lib/jsdom/living/custom-elements");
151152
addDir("../../lib/jsdom/living/domparsing");

test/web-platform-tests/to-run.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ url/**: [timeout, blob URLs not implemented]
2929

3030
---
3131

32+
DIR: WebCryptoAPI
33+
34+
"*/**": [fail, Not implemented]
35+
idlharness.https.any.html: [fail, Unknown]
36+
randomUUID.https.any.html: [fail, Not implemented]
37+
38+
---
39+
3240
DIR: cors
3341

3442
304.htm: [fail-slow, Unknown]

0 commit comments

Comments
 (0)