{"version":3,"file":"location.cjs","sourceRoot":"","sources":["../../../src/snaps/location/location.ts"],"names":[],"mappings":";;;AAEA,2CAAyC;AAEzC,qCAAsC;AACtC,uCAAwC;AAExC,mCAAoC;AA0CpC;;;;;;GAMG;AACH,SAAgB,kBAAkB,CAChC,QAAsB,EACtB,IAAgC;IAEhC,MAAM,SAAS,GAAG,IAAI,EAAE,SAAS,IAAI,KAAK,CAAC;IAC3C,MAAM,UAAU,GAAG,IAAI,EAAE,UAAU,IAAI,KAAK,CAAC;IAC7C,MAAM,IAAI,GAAG,IAAI,GAAG,CAAC,QAAQ,CAAC,CAAC;IAC/B,QAAQ,IAAI,CAAC,QAAQ,EAAE,CAAC;QACtB,KAAK,MAAM;YACT,OAAO,IAAI,iBAAW,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;QACrC,KAAK,QAAQ;YACX,IAAA,cAAM,EAAC,UAAU,EAAE,IAAI,SAAS,CAAC,mCAAmC,CAAC,CAAC,CAAC;YACvE,OAAO,IAAI,qBAAa,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;QACvC,KAAK,OAAO,CAAC;QACb,KAAK,QAAQ;YACX,IAAA,cAAM,EACJ,SAAS,EACT,IAAI,SAAS,CAAC,gDAAgD,CAAC,CAChE,CAAC;YACF,OAAO,IAAI,mBAAY,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;QACtC;YACE,MAAM,IAAI,SAAS,CACjB,iBAAiB,IAAI,CAAC,QAAQ,2BAA2B,CAC1D,CAAC;IACN,CAAC;AACH,CAAC;AAzBD,gDAyBC","sourcesContent":["import type { SnapManifest, VirtualFile } from '@metamask/snaps-utils';\nimport type { SemVerRange } from '@metamask/utils';\nimport { assert } from '@metamask/utils';\n\nimport { HttpLocation } from './http';\nimport { LocalLocation } from './local';\nimport type { NpmOptions } from './npm';\nimport { NpmLocation } from './npm';\n\ndeclare module '@metamask/snaps-utils' {\n  // This needs to be an interface in order to allow for declaration merging.\n  // eslint-disable-next-line @typescript-eslint/consistent-type-definitions\n  interface DataMap {\n    /**\n     * Fully qualified, canonical path for the file in {@link https://github.com/MetaMask/SIPs/blob/main/SIPS/sip-8.md SIP-8 } URI format.\n     */\n    canonicalPath: string;\n  }\n}\n\nexport type SnapLocation = {\n  /**\n   * All files are relative to the manifest, except the manifest itself.\n   */\n  manifest(): Promise<VirtualFile<SnapManifest>>;\n  fetch(path: string): Promise<VirtualFile>;\n\n  readonly shouldAlwaysReload?: boolean;\n};\n\nexport type DetectSnapLocationOptions = NpmOptions & {\n  /**\n   * The function used to fetch data.\n   *\n   * @default globalThis.fetch\n   */\n  fetch?: typeof fetch;\n  /**\n   * @default false\n   */\n  allowHttp?: boolean;\n  /**\n   * @default false\n   */\n  allowLocal?: boolean;\n\n  resolveVersion?: (range: SemVerRange) => Promise<SemVerRange>;\n};\n\n/**\n * Auto-magically detects which SnapLocation object to create based on the provided {@link location}.\n *\n * @param location - A {@link https://github.com/MetaMask/SIPs/blob/main/SIPS/sip-8.md SIP-8} uri.\n * @param opts - NPM options and feature flags.\n * @returns SnapLocation based on url.\n */\nexport function detectSnapLocation(\n  location: string | URL,\n  opts?: DetectSnapLocationOptions,\n): SnapLocation {\n  const allowHttp = opts?.allowHttp ?? false;\n  const allowLocal = opts?.allowLocal ?? false;\n  const root = new URL(location);\n  switch (root.protocol) {\n    case 'npm:':\n      return new NpmLocation(root, opts);\n    case 'local:':\n      assert(allowLocal, new TypeError('Fetching local snaps is disabled.'));\n      return new LocalLocation(root, opts);\n    case 'http:':\n    case 'https:':\n      assert(\n        allowHttp,\n        new TypeError('Fetching snaps through http/https is disabled.'),\n      );\n      return new HttpLocation(root, opts);\n    default:\n      throw new TypeError(\n        `Unrecognized \"${root.protocol}\" snap location protocol.`,\n      );\n  }\n}\n"]}