forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebstorage.js
More file actions
77 lines (70 loc) · 2.44 KB
/
webstorage.js
File metadata and controls
77 lines (70 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
'use strict';
const {
ObjectDefineProperties,
} = primordials;
const { hasInspector } = internalBinding('config');
const { getOptionValue } = require('internal/options');
const { kConstructorKey, Storage } = internalBinding('webstorage');
const { getValidatedPath } = require('internal/fs/utils');
const kInMemoryPath = ':memory:';
module.exports = { Storage };
let lazyLocalStorage;
let lazySessionStorage;
let lazyInspectorStorage;
let localStorageWarned = false;
// Check at load time if localStorage file is provided to determine enumerability.
// If not provided, localStorage is non-enumerable to avoid breaking {...globalThis}.
const localStorageLocation = getOptionValue('--localstorage-file');
const experimentalStorageInspection =
hasInspector && getOptionValue('--experimental-storage-inspection');
function getInspectorStorage() {
if (lazyInspectorStorage === undefined) {
lazyInspectorStorage = require('internal/inspector/webstorage');
}
return lazyInspectorStorage;
}
ObjectDefineProperties(module.exports, {
__proto__: null,
localStorage: {
__proto__: null,
configurable: true,
enumerable: localStorageLocation !== '',
get() {
if (lazyLocalStorage === undefined) {
if (localStorageLocation === '') {
if (!localStorageWarned) {
localStorageWarned = true;
process.emitWarning(
'localStorage is not available because --localstorage-file was not provided.',
'ExperimentalWarning',
);
}
return undefined;
}
if (experimentalStorageInspection) {
const { InspectorLocalStorage } = getInspectorStorage();
lazyLocalStorage = new InspectorLocalStorage(kConstructorKey, getValidatedPath(localStorageLocation), true);
} else {
lazyLocalStorage = new Storage(kConstructorKey, getValidatedPath(localStorageLocation));
}
}
return lazyLocalStorage;
},
},
sessionStorage: {
__proto__: null,
configurable: true,
enumerable: true,
get() {
if (lazySessionStorage === undefined) {
if (experimentalStorageInspection) {
const { InspectorSessionStorage } = getInspectorStorage();
lazySessionStorage = new InspectorSessionStorage(kConstructorKey, kInMemoryPath, false);
} else {
lazySessionStorage = new Storage(kConstructorKey, kInMemoryPath);
}
}
return lazySessionStorage;
},
},
});