GeezerVison is showing github.com — Revert "lib: throw from localStorage getter on missing storage path" · nodejs/node@a4dee61
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/webstorage.js +26-9Lines changed: 26 additions & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | 'use strict'; | 2 | 2 | const { | 3 | 3 | ObjectDefineProperties, | | 4 | + Proxy, | 4 | 5 | } = primordials; | 5 | 6 | const { getOptionValue } = require('internal/options'); | 6 | | -const { lazyDOMException } = require('internal/util'); | 7 | 7 | const { kConstructorKey, Storage } = internalBinding('webstorage'); | 8 | 8 | const { getValidatedPath } = require('internal/fs/utils'); | 9 | 9 | const kInMemoryPath = ':memory:'; | @@ -21,17 +21,34 @@ ObjectDefineProperties(module.exports, { | 21 | 21 | enumerable: true, | 22 | 22 | get() { | 23 | 23 | if (lazyLocalStorage === undefined) { | 24 | | - // For consistency with the web specification, throw from the accessor | 25 | | - // if the local storage path is not provided. | 26 | 24 | const location = getOptionValue('--localstorage-file'); | | 25 | + | 27 | 26 | if (location === '') { | 28 | | - throw lazyDOMException( | 29 | | - 'Cannot initialize local storage without a `--localstorage-file` path', | 30 | | - 'SecurityError', | 31 | | - ); | 32 | | - } | | 27 | + let warningEmitted = false; | | 28 | + const handler = { | | 29 | + __proto__: null, | | 30 | + get(target, prop) { | | 31 | + if (!warningEmitted) { | | 32 | + process.emitWarning('`--localstorage-file` was provided without a valid path'); | | 33 | + warningEmitted = true; | | 34 | + } | | 35 | + | | 36 | + return undefined; | | 37 | + }, | | 38 | + set(target, prop, value) { | | 39 | + if (!warningEmitted) { | | 40 | + process.emitWarning('`--localstorage-file` was provided without a valid path'); | | 41 | + warningEmitted = true; | | 42 | + } | 33 | 43 |
| 34 | | - lazyLocalStorage = new Storage(kConstructorKey, getValidatedPath(location)); | | 44 | + return false; | | 45 | + }, | | 46 | + }; | | 47 | + | | 48 | + lazyLocalStorage = new Proxy({}, handler); | | 49 | + } else { | | 50 | + lazyLocalStorage = new Storage(kConstructorKey, getValidatedPath(location)); | | 51 | + } | 35 | 52 | } | 36 | 53 |
| 37 | 54 | return lazyLocalStorage; | |
Collapse file Copy file name to clipboardExpand all lines: test/common/index.js +1-18Lines changed: 1 addition & 18 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -59,14 +59,6 @@ const hasSQLite = Boolean(process.versions.sqlite); | 59 | 59 |
| 60 | 60 | const hasQuic = hasCrypto && !!process.features.quic; | 61 | 61 |
| 62 | | -const hasLocalStorage = (() => { | 63 | | - try { | 64 | | - return hasSQLite && globalThis.localStorage !== undefined; | 65 | | - } catch { | 66 | | - return false; | 67 | | - } | 68 | | -})(); | 69 | | - | 70 | 62 | /** | 71 | 63 | * Parse test metadata from the specified file. | 72 | 64 | * @param {string} filename - The name of the file to parse. | @@ -359,6 +351,7 @@ const knownGlobals = new Set([ | 359 | 351 | 'CompressionStream', | 360 | 352 | 'DecompressionStream', | 361 | 353 | 'Storage', | | 354 | + 'localStorage', | 362 | 355 | 'sessionStorage', | 363 | 356 | ].forEach((i) => { | 364 | 357 | if (globalThis[i] !== undefined) { | @@ -373,10 +366,6 @@ if (hasCrypto) { | 373 | 366 | knownGlobals.add(globalThis.SubtleCrypto); | 374 | 367 | } | 375 | 368 |
| 376 | | -if (hasLocalStorage) { | 377 | | - knownGlobals.add(globalThis.localStorage); | 378 | | -} | 379 | | - | 380 | 369 | const { Worker } = require('node:worker_threads'); | 381 | 370 | knownGlobals.add(Worker); | 382 | 371 |
| @@ -401,11 +390,6 @@ if (process.env.NODE_TEST_KNOWN_GLOBALS !== '0') { | 401 | 390 | if (val === 'crypto' && !hasCrypto) { | 402 | 391 | continue; | 403 | 392 | } | 404 | | - // globalThis.localStorage is a getter that throws if Node.js was | 405 | | - // executed without a --localstorage-file path. | 406 | | - if (val === 'localStorage' && !hasLocalStorage) { | 407 | | - continue; | 408 | | - } | 409 | 393 | if (!knownGlobals.has(globalThis[val])) { | 410 | 394 | leaked.push(val); | 411 | 395 | } | @@ -956,7 +940,6 @@ const common = { | 956 | 940 | hasQuic, | 957 | 941 | hasInspector, | 958 | 942 | hasSQLite, | 959 | | - hasLocalStorage, | 960 | 943 | invalidArgTypeHelper, | 961 | 944 | isAlive, | 962 | 945 | isASan, | |
Collapse file Copy file name to clipboardExpand all lines: test/common/index.mjs -2Lines changed: 0 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -19,7 +19,6 @@ const { | 19 | 19 | hasQuic, | 20 | 20 | hasInspector, | 21 | 21 | hasSQLite, | 22 | | - hasLocalStorage, | 23 | 22 | hasIntl, | 24 | 23 | hasIPv6, | 25 | 24 | isAIX, | @@ -73,7 +72,6 @@ export { | 73 | 72 | hasQuic, | 74 | 73 | hasInspector, | 75 | 74 | hasSQLite, | 76 | | - hasLocalStorage, | 77 | 75 | hasIntl, | 78 | 76 | hasIPv6, | 79 | 77 | isAIX, | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-assert-checktag.js +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | 'use strict'; | 2 | | -const { hasCrypto, hasLocalStorage } = require('../common'); | | 2 | +const { hasCrypto } = require('../common'); | 3 | 3 | const { test } = require('node:test'); | 4 | 4 | const assert = require('assert'); | 5 | 5 |
| @@ -12,7 +12,7 @@ const assert = require('assert'); | 12 | 12 | if (process.stdout.isTTY) | 13 | 13 | process.env.NODE_DISABLE_COLORS = '1'; | 14 | 14 |
| 15 | | -test({ skip: !hasCrypto || !hasLocalStorage }, () => { | | 15 | +test('', { skip: !hasCrypto }, () => { | 16 | 16 | // See https://github.com/nodejs/node/issues/10258 | 17 | 17 | { | 18 | 18 | const date = new Date('2016'); | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-webstorage.js +4-4Lines changed: 4 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -41,13 +41,13 @@ test('sessionStorage is not persisted', async () => { | 41 | 41 | assert.strictEqual((await readdir(tmpdir.path)).length, 0); | 42 | 42 | }); | 43 | 43 |
| 44 | | -test('localStorage throws without --localstorage-file', async () => { | | 44 | +test('localStorage emits a warning when used without --localstorage-file ', async () => { | 45 | 45 | const cp = await spawnPromisified(process.execPath, [ | 46 | | - '-e', 'localStorage', | | 46 | + '-pe', 'localStorage.length', | 47 | 47 | ]); | 48 | | - assert.strictEqual(cp.code, 1); | | 48 | + assert.strictEqual(cp.code, 0); | 49 | 49 | assert.strictEqual(cp.signal, null); | 50 | | - assert.match(cp.stderr, /SecurityError:/); | | 50 | + assert.match(cp.stderr, /Warning: `--localstorage-file` was provided without a valid path/); | 51 | 51 | }); | 52 | 52 |
| 53 | 53 | test('localStorage is not persisted if it is unused', async () => { | |
|