File tree Expand file tree Collapse file tree 5 files changed +28
-32
lines changed
Expand file tree Collapse file tree 5 files changed +28
-32
lines changed Original file line number Diff line number Diff line change 11'use strict' ;
22const {
33 ObjectDefineProperties,
4- Proxy,
54} = primordials ;
65const { getOptionValue } = require ( 'internal/options' ) ;
76const { kConstructorKey, Storage } = internalBinding ( 'webstorage' ) ;
@@ -13,42 +12,24 @@ module.exports = { Storage };
1312let lazyLocalStorage ;
1413let lazySessionStorage ;
1514
15+ let localStorageWarningEmitted = false ;
16+
1617ObjectDefineProperties ( module . exports , {
1718 __proto__ : null ,
1819 localStorage : {
1920 __proto__ : null ,
2021 configurable : true ,
2122 enumerable : true ,
2223 get ( ) {
23- if ( lazyLocalStorage === undefined ) {
24+ if ( lazyLocalStorage === undefined && ! localStorageWarningEmitted ) {
2425 const location = getOptionValue ( '--localstorage-file' ) ;
25-
2626 if ( location === '' ) {
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- }
43-
44- return false ;
45- } ,
46- } ;
47-
48- lazyLocalStorage = new Proxy ( { } , handler ) ;
49- } else {
50- lazyLocalStorage = new Storage ( kConstructorKey , getValidatedPath ( location ) ) ;
27+ process . emitWarning ( 'Cannot initialize local storage without a `--localstorage-file` path' ) ;
28+ localStorageWarningEmitted = true ;
29+ return undefined ;
5130 }
31+
32+ lazyLocalStorage = new Storage ( kConstructorKey , getValidatedPath ( location ) ) ;
5233 }
5334
5435 return lazyLocalStorage ;
Original file line number Diff line number Diff line change @@ -70,6 +70,9 @@ const hasSQLite = Boolean(process.versions.sqlite);
7070
7171const hasQuic = hasCrypto && ! ! process . features . quic ;
7272
73+ const hasLocalStorage = hasSQLite &&
74+ process . execArgv . find ( ( arg ) => arg . startsWith ( '--localstorage-file=' ) ) !== undefined ;
75+
7376/**
7477 * Parse test metadata from the specified file.
7578 * @param {string } filename - The name of the file to parse.
@@ -362,7 +365,6 @@ const knownGlobals = new Set([
362365 'CompressionStream' ,
363366 'DecompressionStream' ,
364367 'Storage' ,
365- 'localStorage' ,
366368 'sessionStorage' ,
367369] . forEach ( ( i ) => {
368370 if ( globalThis [ i ] !== undefined ) {
@@ -377,6 +379,10 @@ if (hasCrypto) {
377379 knownGlobals . add ( globalThis . SubtleCrypto ) ;
378380}
379381
382+ if ( hasLocalStorage ) {
383+ knownGlobals . add ( globalThis . localStorage ) ;
384+ }
385+
380386const { Worker } = require ( 'node:worker_threads' ) ;
381387knownGlobals . add ( Worker ) ;
382388
@@ -401,6 +407,11 @@ if (process.env.NODE_TEST_KNOWN_GLOBALS !== '0') {
401407 if ( val === 'crypto' && ! hasCrypto ) {
402408 continue ;
403409 }
410+ // globalThis.localStorage is a getter that warns if Node.js was
411+ // executed without a --localstorage-file path.
412+ if ( val === 'localStorage' && ! hasLocalStorage ) {
413+ continue ;
414+ }
404415 if ( ! knownGlobals . has ( globalThis [ val ] ) ) {
405416 leaked . push ( val ) ;
406417 }
@@ -952,6 +963,7 @@ const common = {
952963 hasQuic,
953964 hasInspector,
954965 hasSQLite,
966+ hasLocalStorage,
955967 invalidArgTypeHelper,
956968 isAlive,
957969 isASan,
Original file line number Diff line number Diff line change @@ -19,6 +19,7 @@ const {
1919 hasQuic,
2020 hasInspector,
2121 hasSQLite,
22+ hasLocalStorage,
2223 hasIntl,
2324 hasIPv6,
2425 isAIX,
@@ -72,6 +73,7 @@ export {
7273 hasQuic ,
7374 hasInspector ,
7475 hasSQLite ,
76+ hasLocalStorage ,
7577 hasIntl ,
7678 hasIPv6 ,
7779 isAIX ,
Original file line number Diff line number Diff line change 11'use strict' ;
2- const { hasCrypto } = require ( '../common' ) ;
2+ const { hasCrypto, hasLocalStorage } = require ( '../common' ) ;
33const { test } = require ( 'node:test' ) ;
44const assert = require ( 'assert' ) ;
55
@@ -12,7 +12,7 @@ const assert = require('assert');
1212if ( 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' ) ;
Original file line number Diff line number Diff line change @@ -43,11 +43,12 @@ test('sessionStorage is not persisted', async () => {
4343
4444test ( 'localStorage emits a warning when used without --localstorage-file ' , async ( ) => {
4545 const cp = await spawnPromisified ( process . execPath , [
46- '-pe' , 'localStorage.length ' ,
46+ '-pe' , 'localStorage' ,
4747 ] ) ;
4848 assert . strictEqual ( cp . code , 0 ) ;
4949 assert . strictEqual ( cp . signal , null ) ;
50- assert . match ( cp . stderr , / W a r n i n g : ` - - l o c a l s t o r a g e - f i l e ` w a s p r o v i d e d w i t h o u t a v a l i d p a t h / ) ;
50+ assert . match ( cp . stdout , / u n d e f i n e d / ) ;
51+ assert . match ( cp . stderr , / W a r n i n g : C a n n o t i n i t i a l i z e l o c a l s t o r a g e w i t h o u t a ` - - l o c a l s t o r a g e - f i l e ` p a t h / ) ;
5152} ) ;
5253
5354test ( 'localStorage is not persisted if it is unused' , async ( ) => {
You can’t perform that action at this time.
0 commit comments