2019-10-24 15:54:32 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Freezes the Promise global and prevents its reassignment.
|
|
|
|
*/
|
2020-01-09 04:34:58 +01:00
|
|
|
import deepFreeze from 'deep-freeze-strict'
|
2019-10-24 15:54:32 +02:00
|
|
|
|
|
|
|
if (
|
|
|
|
process.env.IN_TEST !== 'true' &&
|
|
|
|
process.env.METAMASK_ENV !== 'test'
|
|
|
|
) {
|
|
|
|
freeze(global, 'Promise')
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Makes a key:value pair on a target object immutable, with limitations.
|
|
|
|
* The key cannot be reassigned or deleted, and the value is recursively frozen
|
|
|
|
* using Object.freeze.
|
|
|
|
*
|
|
|
|
* Because of JavaScript language limitations, this is does not mean that the
|
|
|
|
* value is completely immutable. It is, however, better than nothing.
|
|
|
|
*
|
|
|
|
* @param {Object} target - The target object to freeze a property on.
|
2020-01-13 19:36:36 +01:00
|
|
|
* @param {string} key - The key to freeze.
|
2019-10-24 15:54:32 +02:00
|
|
|
* @param {any} [value] - The value to freeze, if different from the existing value on the target.
|
|
|
|
* @param {boolean} [enumerable=true] - If given a value, whether the property is enumerable.
|
|
|
|
*/
|
|
|
|
function freeze (target, key, value, enumerable = true) {
|
|
|
|
|
|
|
|
const opts = {
|
|
|
|
configurable: false, writable: false,
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value !== undefined) {
|
|
|
|
opts.value = deepFreeze(value)
|
|
|
|
opts.enumerable = enumerable
|
|
|
|
} else {
|
|
|
|
target[key] = deepFreeze(target[key])
|
|
|
|
}
|
|
|
|
|
|
|
|
Object.defineProperty(target, key, opts)
|
|
|
|
}
|