|
1 | | -// Flags: --inspect=0 --experimental-storage-inspection --localstorage-file=./localstorage.db |
2 | 1 | 'use strict'; |
3 | 2 |
|
4 | 3 | const common = require('../common'); |
5 | 4 | common.skipIfSQLiteMissing(); |
6 | 5 | common.skipIfInspectorDisabled(); |
| 6 | +const { spawnSyncAndExitWithoutError } = require('../common/child_process'); |
| 7 | +const fixtures = require('../common/fixtures'); |
7 | 8 | const tmpdir = require('../common/tmpdir'); |
8 | 9 | tmpdir.refresh(); |
9 | | -// Change cwd to tmpdir so that --localstorage-file=./localstorage.db resolves there. |
10 | | -process.chdir(tmpdir.path); |
11 | 10 |
|
12 | | -const assert = require('assert'); |
13 | | -const { DOMStorage, Session } = require('node:inspector/promises'); |
14 | | -const { pathToFileURL } = require('node:url'); |
15 | | -const path = require('node:path'); |
16 | | - |
17 | | -async function testRegisterStorage() { |
18 | | - const session = new Session(); |
19 | | - await session.connect(); |
20 | | - |
21 | | - await session.post('DOMStorage.enable'); |
22 | | - |
23 | | - const localStorageFileUrl = |
24 | | - pathToFileURL(path.join(tmpdir.path, 'localstorage.db')).href; |
25 | | - |
26 | | - const { storageKey } = await session.post('Storage.getStorageKey'); |
27 | | - assert.strictEqual( |
28 | | - storageKey.toLowerCase(), |
29 | | - localStorageFileUrl.toLowerCase() |
30 | | - ); |
31 | | - |
32 | | - await checkStorage(true); |
33 | | - await checkStorage(false); |
34 | | - session.disconnect(); |
35 | | - |
36 | | - async function checkStorage(isLocalStorage) { |
37 | | - DOMStorage.registerStorage({ |
38 | | - isLocalStorage, |
39 | | - storageMap: { |
40 | | - key1: 'value1', |
41 | | - key2: 'value2', |
42 | | - [1]: 2, |
43 | | - [true]: 'booleanKey', |
44 | | - ['ключ']: 'значение', |
45 | | - }, |
46 | | - }); |
47 | | - const result = await session.post('DOMStorage.getDOMStorageItems', { |
48 | | - storageId: { |
49 | | - isLocalStorage, |
50 | | - securityOrigin: 'node-inspector://default-dom-storage', |
51 | | - }, |
52 | | - }); |
53 | | - const sortedEntries = result.entries.sort((a, b) => |
54 | | - a[0].localeCompare(b[0]), |
55 | | - ); |
56 | | - assert.deepStrictEqual(sortedEntries, [ |
57 | | - ['1', '2'], |
58 | | - ['key1', 'value1'], |
59 | | - ['key2', 'value2'], |
60 | | - ['true', 'booleanKey'], |
61 | | - ['ключ', 'значение'], |
62 | | - ]); |
63 | | - } |
64 | | -} |
65 | | - |
66 | | -async function testGetData() { |
67 | | - await test(true); |
68 | | - await test(false); |
69 | | - |
70 | | - async function test(isLocalStorage) { |
71 | | - const webStorage = isLocalStorage ? localStorage : sessionStorage; |
72 | | - const session = new Session(); |
73 | | - webStorage.clear(); |
74 | | - await session.connect(); |
75 | | - |
76 | | - const storageKey = await session.post('Storage.getStorageKey'); |
77 | | - await session.post('DOMStorage.enable'); |
78 | | - |
79 | | - webStorage.setItem('key1', 'value'); |
80 | | - webStorage.setItem('key2', 1); |
81 | | - webStorage.setItem('key3', JSON.stringify({ a: 1 })); |
82 | | - webStorage.setItem('ключ', 'значение'); |
83 | | - |
84 | | - const result = await session.post('DOMStorage.getDOMStorageItems', { |
85 | | - storageId: { |
86 | | - isLocalStorage, |
87 | | - securityOrigin: '', |
88 | | - storageKey: storageKey.storageKey, |
89 | | - }, |
90 | | - }); |
91 | | - assert.strictEqual(result.entries.length, 4); |
92 | | - const entries = Object.fromEntries(result.entries); |
93 | | - assert.strictEqual(entries.key1, 'value'); |
94 | | - assert.strictEqual(entries.key2, '1'); |
95 | | - assert.strictEqual(entries.key3, JSON.stringify({ a: 1 })); |
96 | | - assert.strictEqual(entries['ключ'], 'значение'); |
97 | | - session.disconnect(); |
98 | | - } |
99 | | -} |
100 | | - |
101 | | -async function testEvents() { |
102 | | - await test(true); |
103 | | - await test(false); |
104 | | - async function test(isLocalStorage) { |
105 | | - const webStorage = isLocalStorage ? localStorage : sessionStorage; |
106 | | - webStorage.clear(); |
107 | | - const session = new Session(); |
108 | | - await session.connect(); |
109 | | - await session.post('DOMStorage.enable'); |
110 | | - const storageKey = await session.post('Storage.getStorageKey'); |
111 | | - session.on( |
112 | | - 'DOMStorage.domStorageItemAdded', |
113 | | - common.mustCall(({ params }) => { |
114 | | - assert.strictEqual(params.key, 'key'); |
115 | | - assert.strictEqual(params.newValue, 'value'); |
116 | | - assert.deepStrictEqual(params.storageId, { |
117 | | - securityOrigin: '', |
118 | | - isLocalStorage, |
119 | | - storageKey: storageKey.storageKey, |
120 | | - }); |
121 | | - }), |
122 | | - ); |
123 | | - webStorage.setItem('key', 'value'); |
124 | | - |
125 | | - session.on( |
126 | | - 'DOMStorage.domStorageItemUpdated', |
127 | | - common.mustCall(({ params }) => { |
128 | | - assert.strictEqual(params.key, 'key'); |
129 | | - assert.strictEqual(params.oldValue, 'value'); |
130 | | - assert.strictEqual(params.newValue, 'newValue'); |
131 | | - assert.deepStrictEqual(params.storageId, { |
132 | | - securityOrigin: '', |
133 | | - isLocalStorage, |
134 | | - storageKey: storageKey.storageKey, |
135 | | - }); |
136 | | - }), |
137 | | - ); |
138 | | - |
139 | | - webStorage.setItem('key', 'newValue'); |
140 | | - |
141 | | - session.on( |
142 | | - 'DOMStorage.domStorageItemRemoved', |
143 | | - common.mustCall(({ params }) => { |
144 | | - assert.strictEqual(params.key, 'key'); |
145 | | - assert.deepStrictEqual(params.storageId, { |
146 | | - securityOrigin: '', |
147 | | - isLocalStorage, |
148 | | - storageKey: storageKey.storageKey, |
149 | | - }); |
150 | | - }), |
151 | | - ); |
152 | | - |
153 | | - webStorage.removeItem('key'); |
154 | | - |
155 | | - session.on( |
156 | | - 'DOMStorage.domStorageItemsCleared', |
157 | | - common.mustCall(({ params }) => { |
158 | | - assert.deepStrictEqual(params.storageId, { |
159 | | - securityOrigin: '', |
160 | | - isLocalStorage, |
161 | | - storageKey: storageKey.storageKey, |
162 | | - }); |
163 | | - }), |
164 | | - ); |
165 | | - webStorage.clear(); |
166 | | - session.disconnect(); |
167 | | - } |
168 | | -} |
169 | | - |
170 | | -async function test() { |
171 | | - await testRegisterStorage(); |
172 | | - await testGetData(); |
173 | | - await testEvents(); |
174 | | -} |
175 | | -test().then(common.mustCall()); |
| 11 | +spawnSyncAndExitWithoutError(process.execPath, [ |
| 12 | + '--inspect=0', |
| 13 | + '--experimental-storage-inspection', |
| 14 | + '--localstorage-file=./localstorage.db', |
| 15 | + fixtures.path('test-inspector-dom-storage.mjs'), |
| 16 | +], { cwd: tmpdir.path }); |
0 commit comments