Research and resolve errors in reconfiguring fetch in use-snow.js
https://github.com/MetaMask/metamask-extension/pull/24631 fixes a problem on MV3 builds, whereby attempting chrome.notifications.create resulted in failure because that code attempts to use a scuttled fetch.
We attempted to fix the problem by setting a custom tamed fetch on the global to be scuttled. This required that we cache the fetch on globalThis. However, once that was attempted (see the PRs branch state as of b31e0aff79f369fb3569da4e2af1f15ce42bfd7e), a strange problem occured:
Okay, so there is a new issue that I don't understand. I am now caching fetch by placing this at the top of use-snow.js:
const { fetch, Error, Object } = globalThis; const { hasOwn, defineProperty } = Object;That fetch is something that is defined within sentry-install.js The problem is that the fetch called within > https://github.com/chromium/chromium/blob/main/extensions/renderer/resources/image_util.js#L23-L25 is now that cached fetch (defined within sentry-install.js) and not the tamedFetch that was added to the realm with defineProperty
We could observe that the problem did not occur if we reached into globalThis.fetch and got the original/native fetch, as was done in 3a4a55b780ef8a0bc355ad0daeb91521b0eb863b. However, that was just for demonstration purposes.
We do not yet understand why the tamed fetch was not called when globalThis.fetch was cached, nor why it was called when only the native fetch nested within the sentry fetch was cached.
For the time being, to unblock the release of MV3, we have added an exception to the scuttling of fetch. However, we want to investigate this further and remove that exception with a solution that gets to the root of the problem.