lib: throw from localStorage getter on missing storage path · nodejs/node@4fbb1ab · GitHub
Skip to content

Commit 4fbb1ab

Browse files
Renegade334aduh95
authored andcommitted
lib: throw from localStorage getter on missing storage path
PR-URL: #60351 Reviewed-By: Jacob Smith <jacob@frende.me> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com> Reviewed-By: Matthew Aitken <maitken033380023@gmail.com>
1 parent 74719da commit 4fbb1ab

5 files changed

Lines changed: 35 additions & 33 deletions

File tree

lib/internal/webstorage.js

Lines changed: 9 additions & 26 deletions

test/common/index.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@ const hasSQLite = Boolean(process.versions.sqlite);
5959

6060
const hasQuic = hasCrypto && !!process.features.quic;
6161

62+
const hasLocalStorage = (() => {
63+
try {
64+
return hasSQLite && globalThis.localStorage !== undefined;
65+
} catch {
66+
return false;
67+
}
68+
})();
69+
6270
/**
6371
* Parse test metadata from the specified file.
6472
* @param {string} filename - The name of the file to parse.
@@ -350,7 +358,6 @@ const knownGlobals = new Set([
350358
'CompressionStream',
351359
'DecompressionStream',
352360
'Storage',
353-
'localStorage',
354361
'sessionStorage',
355362
].forEach((i) => {
356363
if (globalThis[i] !== undefined) {
@@ -365,6 +372,10 @@ if (hasCrypto) {
365372
knownGlobals.add(globalThis.SubtleCrypto);
366373
}
367374

375+
if (hasLocalStorage) {
376+
knownGlobals.add(globalThis.localStorage);
377+
}
378+
368379
const { Worker } = require('node:worker_threads');
369380
knownGlobals.add(Worker);
370381

@@ -389,6 +400,11 @@ if (process.env.NODE_TEST_KNOWN_GLOBALS !== '0') {
389400
if (val === 'crypto' && !hasCrypto) {
390401
continue;
391402
}
403+
// globalThis.localStorage is a getter that throws if Node.js was
404+
// executed without a --localstorage-file path.
405+
if (val === 'localStorage' && !hasLocalStorage) {
406+
continue;
407+
}
392408
if (!knownGlobals.has(globalThis[val])) {
393409
leaked.push(val);
394410
}
@@ -933,6 +949,7 @@ const common = {
933949
hasQuic,
934950
hasInspector,
935951
hasSQLite,
952+
hasLocalStorage,
936953
invalidArgTypeHelper,
937954
isAlive,
938955
isASan,

test/common/index.mjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const {
1919
hasQuic,
2020
hasInspector,
2121
hasSQLite,
22+
hasLocalStorage,
2223
hasIntl,
2324
hasIPv6,
2425
isAIX,
@@ -71,6 +72,7 @@ export {
7172
hasQuic,
7273
hasInspector,
7374
hasSQLite,
75+
hasLocalStorage,
7476
hasIntl,
7577
hasIPv6,
7678
isAIX,

test/parallel/test-assert-checktag.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'use strict';
2-
const { hasCrypto } = require('../common');
2+
const { hasCrypto, hasLocalStorage } = require('../common');
33
const { test } = require('node:test');
44
const assert = require('assert');
55

@@ -12,7 +12,7 @@ const assert = require('assert');
1212
if (process.stdout.isTTY)
1313
process.env.NODE_DISABLE_COLORS = '1';
1414

15-
test('', { skip: !hasCrypto }, () => {
15+
test({ skip: !hasCrypto || !hasLocalStorage }, () => {
1616
// See https://github.com/nodejs/node/issues/10258
1717
{
1818
const date = new Date('2016');

test/parallel/test-webstorage.js

Lines changed: 4 additions & 4 deletions

0 commit comments

Comments
 (0)