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