{"version":3,"file":"hasConfigurableGlobal-Befl_QJD.cjs","names":["#globals","errors: Array<Error>","currentOwner: Owner | null","descriptor: PropertyDescriptor | undefined"],"sources":["../../src/utils/globalsRegistry.ts","../../src/utils/hasConfigurableGlobal.ts"],"sourcesContent":["import { invariant } from 'outvariant'\n\nclass GlobalsRegistry {\n  #globals = new Map<keyof typeof globalThis, () => void>()\n\n  public replaceGlobal<K extends keyof typeof globalThis>(\n    key: K,\n    nextValue: (typeof globalThis)[K]\n  ): () => void {\n    invariant(\n      !this.#globals.has(key),\n      `Failed to replace a global value at \"${key}\": already replaced.`\n    )\n\n    const match = getDeepPropertyDescriptor(globalThis, key)\n\n    if (typeof match === 'undefined') {\n      console.warn(\n        `Failed to replace a global value at \"${key}\": not a global value.`\n      )\n      return () => {}\n    }\n\n    Object.defineProperty(globalThis, key, {\n      value: nextValue,\n      enumerable: true,\n      configurable: true,\n    })\n\n    const restoreGlobal = () => {\n      if (!this.#globals.has(key)) {\n        return\n      }\n\n      if (match.owner === globalThis) {\n        Object.defineProperty(match.owner, key, match.descriptor)\n      } else {\n        /**\n         * @todo Delete the proxy property set by the registry.\n         * If the owner isn't `globalThis`, the property is likely nested in the prototype.\n         * The registry does not meddle with those, they are left intact.\n         */\n        Reflect.deleteProperty(globalThis, key)\n      }\n\n      this.#globals.delete(key)\n    }\n\n    this.#globals.set(key, restoreGlobal)\n\n    return restoreGlobal\n  }\n\n  public restoreAllGlobals(): void {\n    const errors: Array<Error> = []\n\n    for (const [, restoreGlobal] of this.#globals) {\n      try {\n        restoreGlobal()\n      } catch (error) {\n        if (error instanceof Error) {\n          errors.push(error)\n        } else {\n          throw error\n        }\n      }\n    }\n\n    if (errors.length > 0) {\n      throw new AggregateError(errors, 'FOO!')\n    }\n  }\n}\n\nexport const globalsRegistry = new GlobalsRegistry()\n\ninterface DeepDescriptorMatch {\n  owner: object\n  descriptor: PropertyDescriptor\n}\n\n/**\n * Returns a property descriptor for the given property on the owner.\n * Walks down the prototype chain if the property does not exist on the owner.\n * Handy for getting a global property descriptor where `globalThis` is\n * replaced with a controlled class (e.g. ServiceWorkerGlobalScope).\n */\nexport function getDeepPropertyDescriptor<Owner extends object>(\n  owner: Owner,\n  key: keyof Owner\n): DeepDescriptorMatch | undefined {\n  let currentOwner: Owner | null = owner\n  let descriptor: PropertyDescriptor | undefined\n\n  while (currentOwner) {\n    descriptor = Object.getOwnPropertyDescriptor(currentOwner, key)\n\n    if (descriptor) {\n      return {\n        owner: currentOwner,\n        descriptor,\n      }\n    }\n\n    currentOwner = Object.getPrototypeOf(currentOwner)\n  }\n}\n","import { getDeepPropertyDescriptor } from './globalsRegistry'\n\n/**\n * Returns a boolean indicating whether the given global property\n * is defined and is configurable.\n */\nexport function hasConfigurableGlobal(\n  propertyName: keyof typeof globalThis\n): boolean {\n  const match = getDeepPropertyDescriptor(globalThis, propertyName)\n\n  // The property is not set at all.\n  if (typeof match === 'undefined') {\n    return false\n  }\n\n  const { descriptor } = match\n\n  // The property is set to a getter that returns undefined.\n  if (\n    typeof descriptor.get === 'function' &&\n    typeof descriptor.get() === 'undefined'\n  ) {\n    return false\n  }\n\n  // The property is set to a value equal to undefined.\n  if (typeof descriptor.get === 'undefined' && descriptor.value == null) {\n    return false\n  }\n\n  if (typeof descriptor.set === 'undefined' && !descriptor.configurable) {\n    console.error(\n      `[MSW] Failed to apply interceptor: the global \\`${propertyName}\\` property is non-configurable. This is likely an issue with your environment. If you are using a framework, please open an issue about this in their repository.`\n    )\n    return false\n  }\n\n  return true\n}\n"],"mappings":";;;;AAEA,IAAM,kBAAN,MAAsB;CACpB,2BAAW,IAAI,KAA0C;CAEzD,AAAO,cACL,KACA,WACY;AACZ,4BACE,CAAC,MAAKA,QAAS,IAAI,IAAI,EACvB,wCAAwC,IAAI,sBAC7C;EAED,MAAM,QAAQ,0BAA0B,YAAY,IAAI;AAExD,MAAI,OAAO,UAAU,aAAa;AAChC,WAAQ,KACN,wCAAwC,IAAI,wBAC7C;AACD,gBAAa;;A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