Files
node/test/parallel/test-webstorage.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

141 lines
4.5 KiB
JavaScript

'use strict';
const { skipIfSQLiteMissing, spawnPromisified } = require('../common');
skipIfSQLiteMissing();
const tmpdir = require('../common/tmpdir');
const assert = require('node:assert');
const { join } = require('node:path');
const { readdir } = require('node:fs/promises');
const { test, describe } = require('node:test');
let cnt = 0;
tmpdir.refresh();
function nextLocalStorage() {
return join(tmpdir.path, `${++cnt}.localstorage`);
}
test('Storage instances cannot be created in userland', async () => {
const cp = await spawnPromisified(process.execPath, [
'-e', 'new globalThis.Storage()',
]);
assert.strictEqual(cp.code, 1);
assert.strictEqual(cp.signal, null);
assert.strictEqual(cp.stdout, '');
assert.match(cp.stderr, /Error: Illegal constructor/);
});
test('sessionStorage is not persisted', async () => {
let cp = await spawnPromisified(process.execPath, [
'-pe', 'sessionStorage.foo = "barbaz"',
]);
assert.strictEqual(cp.code, 0);
assert.match(cp.stdout, /barbaz/);
cp = await spawnPromisified(process.execPath, [
'-pe', 'sessionStorage.foo',
]);
assert.strictEqual(cp.code, 0);
assert.match(cp.stdout, /undefined/);
assert.strictEqual((await readdir(tmpdir.path)).length, 0);
});
test('localStorage returns undefined and warns without --localstorage-file', async () => {
const cp = await spawnPromisified(process.execPath, [
'-pe', 'localStorage',
]);
assert.strictEqual(cp.code, 0);
assert.strictEqual(cp.signal, null);
assert.match(cp.stdout, /undefined/);
assert.match(cp.stderr, /ExperimentalWarning:.*localStorage is not available/);
});
test('localStorage is not enumerable without --localstorage-file', async () => {
const cp = await spawnPromisified(process.execPath, [
'-pe', 'Object.keys(globalThis).includes("localStorage")',
]);
assert.strictEqual(cp.code, 0);
assert.match(cp.stdout, /false/);
});
test('localStorage is not persisted if it is unused', async () => {
const cp = await spawnPromisified(process.execPath, [
'--localstorage-file', nextLocalStorage(),
'-pe', 'localStorage === globalThis.localStorage',
]);
assert.strictEqual(cp.code, 0);
assert.match(cp.stdout, /true/);
assert.strictEqual((await readdir(tmpdir.path)).length, 0);
});
test('localStorage is persisted if it is used', async () => {
const localStorageFile = nextLocalStorage();
let cp = await spawnPromisified(process.execPath, [
'--localstorage-file', localStorageFile,
'-pe', 'localStorage.foo = "barbaz"',
]);
assert.strictEqual(cp.code, 0);
assert.match(cp.stdout, /barbaz/);
const entries = await readdir(tmpdir.path);
assert.strictEqual(entries.length, 1);
assert.match(entries[0], /\d+\.localstorage/);
cp = await spawnPromisified(process.execPath, [
'--localstorage-file', localStorageFile,
'-pe', 'localStorage.foo',
]);
assert.strictEqual(cp.code, 0);
assert.match(cp.stdout, /barbaz/);
});
describe('webstorage quota for localStorage and sessionStorage', () => {
const MAX_STORAGE_SIZE = 10 * 1024 * 1024;
for (const storage of ['localStorage', 'sessionStorage']) {
test(`${storage} can store a max of 10 MB quota`, async () => {
const args = [];
if (storage === 'localStorage') {
args.push('--localstorage-file', nextLocalStorage());
}
// Each character is 2 bytes
args.push('-e', `
const assert = require('assert');
${storage}['a'.repeat(${MAX_STORAGE_SIZE} / 2)] = '';
assert.throws(
() => { ${storage}.anything = 'should fail'; },
(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 cp = await spawnPromisified(process.execPath, args);
assert.strictEqual(cp.code, 0);
});
}
});
test('disabled with --no-webstorage', async () => {
for (const api of ['Storage', 'localStorage', 'sessionStorage']) {
const cp = await spawnPromisified(process.execPath, [
'--no-webstorage',
'--localstorage-file',
'./test/fixtures/localstoragefile-global-test',
'-e',
api,
]);
assert.strictEqual(cp.code, 1);
assert.strictEqual(cp.signal, null);
assert.strictEqual(cp.stdout, '');
assert(cp.stderr.includes(`ReferenceError: ${api} is not defined`));
}
});