1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-25 03:20:23 +01:00
metamask-extension/app/scripts/lib/network-store.js
Mark Stacey 20e16d41be
Improve Sentry state pre-initialization (#20491)
* Improve Sentry state pre-initialization

Previously the masked state snapshot sent to Sentry would be blank for
errors that occured during initialization. Instead we'll now include
some basic information in all cases, and a masked copy of the persisted
state if it happens after the first time the persisted state is read.

* Add test

* Fix crash when persisted state not yet fetched

* Add descriptions for initial state hooks

* Update comments to reflect recent changes

* Re-order imports to follow conventions

* Move initial state hooks back to module-level

The initial state hooks are now setup at the top-level of their module.
This ensures that they're setup prior to later imports. Calling a
function to setup these hooks in the entrypoint module wouldn't
accomplish this even if it was run "before" the imports because ES6
imports always get hoisted to the top of the file.

The `localStore` instance wasn't available statically, so a new state
hook was introduced for retrieving the most recent retrieved persisted
state.

* Fix error e2e tests
2023-08-17 09:29:05 -02:30

89 lines
2.1 KiB
JavaScript

import log from 'loglevel';
import getFetchWithTimeout from '../../../shared/modules/fetch-with-timeout';
const fetchWithTimeout = getFetchWithTimeout();
const FIXTURE_SERVER_HOST = 'localhost';
const FIXTURE_SERVER_PORT = 12345;
const FIXTURE_SERVER_URL = `http://${FIXTURE_SERVER_HOST}:${FIXTURE_SERVER_PORT}/state.json`;
/**
* A read-only network-based storage wrapper
*/
export default class ReadOnlyNetworkStore {
constructor() {
this._initialized = false;
this._initializing = this._init();
this._state = undefined;
this.mostRecentRetrievedState = null;
}
/**
* Declares this store as compatible with the current browser
*/
isSupported = true;
/**
* Initializes by loading state from the network
*/
async _init() {
try {
const response = await fetchWithTimeout(FIXTURE_SERVER_URL);
if (response.ok) {
this._state = await response.json();
this.mostRecentRetrievedState = this._state;
}
} catch (error) {
log.debug(`Error loading network state: '${error.message}'`);
} finally {
this._initialized = true;
}
}
/**
* Returns state
*
* @returns {Promise<object>}
*/
async get() {
if (!this._initialized) {
await this._initializing;
}
return this._state;
}
/**
* Set metadata/version state
*
* @param {object} metadata - The metadata/version data to set
*/
setMetadata(metadata) {
this.metadata = metadata;
}
/**
* Set state
*
* @param {object} state - The state to set
*/
async set(state) {
if (!this.isSupported) {
throw new Error(
'Metamask- cannot persist state to local store as this browser does not support this action',
);
}
if (!state) {
throw new Error('MetaMask - updated state is missing');
}
if (!this.metadata) {
throw new Error(
'MetaMask - metadata must be set on instance of ExtensionStore before calling "set"',
);
}
if (!this._initialized) {
await this._initializing;
}
this._state = { data: state, meta: this._metadata };
}
}