-
-
Notifications
You must be signed in to change notification settings - Fork 35.5k
Expand file tree
/
Copy pathtest-webstorage.js
More file actions
160 lines (139 loc) Β· 5.32 KB
/
test-webstorage.js
File metadata and controls
160 lines (139 loc) Β· 5.32 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
'use strict';
const { skipIfSQLiteMissing, spawnPromisified } = require('../common');
skipIfSQLiteMissing();
const tmpdir = require('../common/tmpdir');
const assert = require('node:assert');
const { join } = require('node:path');
const { readdir } = require('node:fs/promises');
const { test, describe } = require('node:test');
let cnt = 0;
tmpdir.refresh();
function nextLocalStorage() {
return join(tmpdir.path, `${++cnt}.localstorage`);
}
test('constructor is illegal', async () => {
const cp = await spawnPromisified(process.execPath, [
'--experimental-webstorage',
'--localstorage-file', nextLocalStorage(),
'-e', 'new Storage',
]);
assert.strictEqual(cp.code, 1);
assert.strictEqual(cp.signal, null);
assert.strictEqual(cp.stdout, '');
assert(cp.stderr.includes(`TypeError: Illegal constructor\n`));
assert(cp.stderr.includes(`code: 'ERR_ILLEGAL_CONSTRUCTOR'\n`));
});
test('disabled without --experimental-webstorage', async () => {
for (const api of ['Storage', 'localStorage', 'sessionStorage']) {
const cp = await spawnPromisified(process.execPath, ['-e', api]);
assert.strictEqual(cp.code, 1);
assert.strictEqual(cp.signal, null);
assert.strictEqual(cp.stdout, '');
assert(cp.stderr.includes(`ReferenceError: ${api} is not defined`));
}
});
test('emits a warning when used', async () => {
for (const api of ['Storage', 'localStorage', 'sessionStorage']) {
const cp = await spawnPromisified(process.execPath, [
'--experimental-webstorage',
'--localstorage-file', nextLocalStorage(),
'-e', api,
]);
assert.strictEqual(cp.code, 0);
assert.strictEqual(cp.signal, null);
assert.strictEqual(cp.stdout, '');
assert.match(cp.stderr, /ExperimentalWarning: Web Storage/);
}
});
test('Storage instances cannot be created in userland', async () => {
const cp = await spawnPromisified(process.execPath, [
'--experimental-webstorage', '-e', 'new globalThis.Storage()',
]);
assert.strictEqual(cp.code, 1);
assert.strictEqual(cp.signal, null);
assert.strictEqual(cp.stdout, '');
assert.match(cp.stderr, /Error: Illegal constructor/);
});
test('sessionStorage is not persisted', async () => {
let cp = await spawnPromisified(process.execPath, [
'--experimental-webstorage', '-pe', 'sessionStorage.foo = "barbaz"',
]);
assert.strictEqual(cp.code, 0);
assert.match(cp.stdout, /barbaz/);
cp = await spawnPromisified(process.execPath, [
'--experimental-webstorage', '-pe', 'sessionStorage.foo',
]);
assert.strictEqual(cp.code, 0);
assert.match(cp.stdout, /undefined/);
assert.strictEqual((await readdir(tmpdir.path)).length, 0);
});
test('localStorage throws without --localstorage-file ', async () => {
const cp = await spawnPromisified(process.execPath, [
'--experimental-webstorage',
'-pe', 'localStorage === globalThis.localStorage',
]);
assert.strictEqual(cp.code, 1);
assert.strictEqual(cp.signal, null);
assert.strictEqual(cp.stdout, '');
assert.match(cp.stderr, /The argument '--localstorage-file' is an invalid localStorage location/);
});
test('localStorage is not persisted if it is unused', async () => {
const cp = await spawnPromisified(process.execPath, [
'--experimental-webstorage',
'--localstorage-file', nextLocalStorage(),
'-pe', 'localStorage === globalThis.localStorage',
]);
assert.strictEqual(cp.code, 0);
assert.match(cp.stdout, /true/);
assert.strictEqual((await readdir(tmpdir.path)).length, 0);
});
test('localStorage is persisted if it is used', async () => {
const localStorageFile = nextLocalStorage();
let cp = await spawnPromisified(process.execPath, [
'--experimental-webstorage',
'--localstorage-file', localStorageFile,
'-pe', 'localStorage.foo = "barbaz"',
]);
assert.strictEqual(cp.code, 0);
assert.match(cp.stdout, /barbaz/);
const entries = await readdir(tmpdir.path);
assert.strictEqual(entries.length, 1);
assert.match(entries[0], /\d+\.localstorage/);
cp = await spawnPromisified(process.execPath, [
'--experimental-webstorage',
'--localstorage-file', localStorageFile,
'-pe', 'localStorage.foo',
]);
assert.strictEqual(cp.code, 0);
assert.match(cp.stdout, /barbaz/);
});
describe('webstorage quota for localStorage and sessionStorage', () => {
const MAX_STORAGE_SIZE = 10 * 1024 * 1024;
test('localStorage can store and retrieve a max of 10 MB quota', async () => {
const localStorageFile = nextLocalStorage();
const cp = await spawnPromisified(process.execPath, [
'--experimental-webstorage',
'--localstorage-file', localStorageFile,
// Each character is 2 bytes
'-pe', `
localStorage['a'.repeat(${MAX_STORAGE_SIZE} / 2)] = '';
console.error('filled');
localStorage.anything = 'should fail';
`,
]);
assert.match(cp.stderr, /filled/);
assert.match(cp.stderr, /QuotaExceededError: Setting the value exceeded the quota/);
});
test('sessionStorage can store a max of 10 MB quota', async () => {
const cp = await spawnPromisified(process.execPath, [
'--experimental-webstorage',
// Each character is 2 bytes
'-pe', `sessionStorage['a'.repeat(${MAX_STORAGE_SIZE} / 2)] = '';
console.error('filled');
sessionStorage.anything = 'should fail';
`,
]);
assert.match(cp.stderr, /filled/);
assert.match(cp.stderr, /QuotaExceededError/);
});
});