forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwebstorage.js
More file actions
57 lines (46 loc) · 1.3 KB
/
webstorage.js
File metadata and controls
57 lines (46 loc) · 1.3 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
'use strict';
const {
MathAbs,
ObjectDefineProperty,
StringPrototypeCharCodeAt,
} = primordials;
const { emitExperimentalWarning } = require('internal/util');
const { kConstructorKey, Storage } = internalBinding('webstorage');
const { basename, join } = require('path');
emitExperimentalWarning('Web storage');
module.exports = { Storage };
let lazyLocalStorage;
let lazySessionStorage;
ObjectDefineProperty(module.exports, 'localStorage', {
__proto__: null,
configurable: true,
enumerable: true,
get() {
if (lazyLocalStorage === undefined) {
const entry = process.argv[1] ?? process.argv[0];
const base = basename(entry);
const hash = hashCode(entry);
const location = join(process.cwd(), `${base}.${hash}.localstorage`);
lazyLocalStorage = new Storage(kConstructorKey, location);
}
return lazyLocalStorage;
},
});
ObjectDefineProperty(module.exports, 'sessionStorage', {
__proto__: null,
configurable: true,
enumerable: true,
get() {
if (lazySessionStorage === undefined) {
lazySessionStorage = new Storage(kConstructorKey, ':memory:');
}
return lazySessionStorage;
},
});
function hashCode(s) {
let h = 0;
for (let i = 0; i < s.length; ++i) {
h = (h << 5) - h + StringPrototypeCharCodeAt(s, i) | 0;
}
return MathAbs(h);
}