|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const { Storage } = internalBinding('webstorage'); |
| 4 | +const { DOMStorage } = require('inspector'); |
| 5 | +const path = require('path'); |
| 6 | +const { getOptionValue } = require('internal/options'); |
| 7 | + |
| 8 | +class InspectorLocalStorage extends Storage { |
| 9 | + setItem(key, value) { |
| 10 | + const oldValue = this.getItem(key); |
| 11 | + super.setItem(key, value); |
| 12 | + if (oldValue == null) { |
| 13 | + itemAdded(key, value, true); |
| 14 | + } else { |
| 15 | + itemUpdated(key, oldValue, value, true); |
| 16 | + } |
| 17 | + } |
| 18 | + |
| 19 | + removeItem(key) { |
| 20 | + super.removeItem(key); |
| 21 | + itemRemoved(key, true); |
| 22 | + } |
| 23 | + |
| 24 | + clear() { |
| 25 | + super.clear(); |
| 26 | + itemsCleared(true); |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +const InspectorSessionStorage = class extends Storage { |
| 31 | + setItem(key, value) { |
| 32 | + const oldValue = this.getItem(key); |
| 33 | + super.setItem(key, value); |
| 34 | + if (oldValue == null) { |
| 35 | + itemAdded(key, value, false); |
| 36 | + } else { |
| 37 | + itemUpdated(key, oldValue, value, false); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + removeItem(key) { |
| 42 | + super.removeItem(key); |
| 43 | + itemRemoved(key, false); |
| 44 | + } |
| 45 | + |
| 46 | + clear() { |
| 47 | + super.clear(); |
| 48 | + itemsCleared(false); |
| 49 | + } |
| 50 | +}; |
| 51 | + |
| 52 | +function itemAdded(key, value, isLocalStorage) { |
| 53 | + DOMStorage.domStorageItemAdded({ |
| 54 | + key, |
| 55 | + newValue: value, |
| 56 | + storageId: { |
| 57 | + securityOrigin: '', |
| 58 | + isLocalStorage, |
| 59 | + storageKey: getStorageKey(), |
| 60 | + }, |
| 61 | + }); |
| 62 | +} |
| 63 | + |
| 64 | +function itemUpdated(key, oldValue, newValue, isLocalStorage) { |
| 65 | + DOMStorage.domStorageItemUpdated({ |
| 66 | + key, |
| 67 | + oldValue, |
| 68 | + newValue, |
| 69 | + storageId: { |
| 70 | + securityOrigin: '', |
| 71 | + isLocalStorage, |
| 72 | + storageKey: getStorageKey(), |
| 73 | + }, |
| 74 | + }); |
| 75 | +} |
| 76 | + |
| 77 | +function itemRemoved(key, isLocalStorage) { |
| 78 | + DOMStorage.domStorageItemRemoved({ |
| 79 | + key, |
| 80 | + storageId: { |
| 81 | + securityOrigin: '', |
| 82 | + isLocalStorage, |
| 83 | + storageKey: getStorageKey(), |
| 84 | + }, |
| 85 | + }); |
| 86 | +} |
| 87 | + |
| 88 | +function itemsCleared(isLocalStorage) { |
| 89 | + DOMStorage.domStorageItemsCleared({ |
| 90 | + storageId: { |
| 91 | + securityOrigin: '', |
| 92 | + isLocalStorage, |
| 93 | + storageKey: getStorageKey(), |
| 94 | + }, |
| 95 | + }); |
| 96 | +} |
| 97 | + |
| 98 | +function getStorageKey() { |
| 99 | + const localStorageFile = getOptionValue('--localstorage-file'); |
| 100 | + const resolvedAbsolutePath = path.resolve(localStorageFile); |
| 101 | + return 'file://' + resolvedAbsolutePath; |
| 102 | +} |
| 103 | + |
| 104 | +module.exports = { |
| 105 | + InspectorLocalStorage, |
| 106 | + InspectorSessionStorage, |
| 107 | +}; |
0 commit comments