node/test/parallel/test-webcrypto-random.js
Filip Skokan b328bf74bd
lib,src: implement QuotaExceededError as DOMException-derived interface
Implement QuotaExceededError as a DOMException-derived interface per the
WebIDL specification update.

QuotaExceededError is now a proper constructor exposed as a global
[Exposed=*] interface that extends DOMException with optional `quota`
and `requested` attributes (both nullable doubles, defaulting to null).

The constructor validates that quota and requested are finite,
non-negative, and that requested is not less than quota when both are
provided.

QuotaExceededError is [Serializable] and supports structuredClone,
preserving the quota and requested values across the serialization
boundary.

Callers updated:
- crypto.getRandomValues() now throws a QuotaExceededError instance
- WebStorage (C++) now constructs QuotaExceededError directly

Refs: https://redirect.github.com/whatwg/webidl/pull/1465
Fixes: https://github.com/nodejs/node/issues/58987
PR-URL: https://github.com/nodejs/node/pull/62293

Reviewed-By: Mattias Buelens <mattias@buelens.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
2026-03-19 14:41:50 +01:00

84 lines
1.8 KiB
JavaScript

'use strict';
const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');
const { Buffer } = require('buffer');
const assert = require('assert');
const { crypto } = globalThis;
[
undefined, null, '', 1, {}, [],
new Float32Array(1),
new Float64Array(1),
new DataView(new ArrayBuffer(1)),
].forEach((i) => {
assert.throws(
() => crypto.getRandomValues(i),
{ name: 'TypeMismatchError', code: 17 },
);
});
{
const buf = new Uint8Array(0);
crypto.getRandomValues(buf);
}
const intTypedConstructors = [
Int8Array,
Int16Array,
Int32Array,
Uint8Array,
Uint16Array,
Uint32Array,
Uint8ClampedArray,
BigInt64Array,
BigUint64Array,
];
for (const ctor of intTypedConstructors) {
const buf = new ctor(10);
const before = Buffer.from(buf.buffer).toString('hex');
crypto.getRandomValues(buf);
const after = Buffer.from(buf.buffer).toString('hex');
assert.notStrictEqual(before, after);
}
{
const buf = Buffer.alloc(10);
const before = buf.toString('hex');
crypto.getRandomValues(buf);
const after = buf.toString('hex');
assert.notStrictEqual(before, after);
}
{
let kData;
try {
kData = Buffer.alloc(65536 + 1);
} catch {
// Ignore if error here.
}
if (kData !== undefined) {
assert.throws(
() => crypto.getRandomValues(kData),
(err) => {
assert.strictEqual(err.name, 'QuotaExceededError');
assert.strictEqual(err.code, 22);
assert(err instanceof DOMException);
assert(err instanceof QuotaExceededError);
assert.strictEqual(err.quota, null);
assert.strictEqual(err.requested, null);
return true;
},
);
}
}
{
const typedArray = new Uint8Array(32);
assert.strictEqual(crypto.getRandomValues(typedArray), typedArray);
}