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QACD,MAAM,MAAM,GAAG,MAAM,QAAQ,CAAC,WAAW,EAAE,CAAC;QAC5C,MAAM,KAAK,GAAG,IAAI,WAAW,CAAC;YAC5B,KAAK,EAAE,IAAI,UAAU,CAAC,MAAM,CAAC;YAC7B,IAAI,EAAE,YAAY;YAClB,IAAI,EAAE,EAAE,aAAa,EAAE;SACxB,CAAC,CAAC;QACH,MAAM,CACJ,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,YAAY,CAAC,EAC7B,iDAAiD,CAClD,CAAC;QACF,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,YAAY,EAAE,KAAK,CAAC,CAAC;QAEpC,OAAO,IAAI,CAAC,KAAK,CAAC,YAAY,CAAC,CAAC;IAClC,CAAC;IAED,IAAI,IAAI;QACN,OAAO,IAAI,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IAC3B,CAAC;IAED,wEAAwE;IACxE,WAAW;IACX,gDAAgD;IACxC,WAAW,CAAC,IAAY;QAC9B,MAAM,CAAC,CAAC,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE,+BAA+B,CAAC,CAAC;QAC/D,OAAO,IAAI,GAAG,CAAC,IAAI,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC;IACjC,CAAC;CACF","sourcesContent":["import type { SnapManifest } from '@metamask/snaps-utils';\nimport {\n  VirtualFile,\n  HttpSnapIdStruct,\n  NpmSnapFileNames,\n  createSnapManifest,\n  normalizeRelative,\n  parseJson,\n} from '@metamask/snaps-utils';\nimport { assert, assertStruct } from '@metamask/utils';\n\nimport type { SnapLocation } from './location';\n\nexport type HttpOptions = {\n  /**\n   * @default fetch\n   */\n  fetch?: typeof fetch;\n  fetchOptions?: RequestInit;\n};\n\nexport class HttpLocation implements SnapLocation {\n  // TODO: Either fix this lint violation or explain why it's necessary to\n  //  ignore.\n  // eslint-disable-next-line no-restricted-syntax\n  private readonly cache = new Map<string, VirtualFile>();\n\n  // TODO: Either fix this lint violation or explain why it's necessary to\n  //  ignore.\n  // eslint-disable-next-line no-restricted-syntax\n  private validatedManifest?: VirtualFile<SnapManifest>;\n\n  // TODO: Either fix this lint violation or explain why it's necessary to\n  //  ignore.\n  // eslint-disable-next-line no-restricted-syntax\n  private readonly url: URL;\n\n  // TODO: Either fix this lint violation or explain why it's necessary to\n  //  ignore.\n  // eslint-disable-next-line no-restricted-syntax\n  private readonly fetchFn: typeof fetch;\n\n  // TODO: Either fix this lint violation or explain why it's necessary to\n  //  ignore.\n  // eslint-disable-next-line no-restricted-syntax\n  private readonly fetchOptions?: RequestInit;\n\n  constructor(url: URL, opts: HttpOptions = {}) {\n    assertStruct(url.toString(), HttpSnapIdStruct, 'Invalid Snap Id: ');\n    this.fetchFn = opts.fetch ?? globalThis.fetch.bind(undefined);\n    this.fetchOptions = opts.fetchOptions;\n    this.url = url;\n  }\n\n  async manifest(): Promise<VirtualFile<SnapManifest>> {\n    if (this.validatedManifest) {\n      return this.validatedManifest.clone();\n    }\n\n    // jest-fetch-mock doesn't handle new URL(), we need to convert .toString()\n    const canonicalPath = new URL(\n      NpmSnapFileNames.Manifest,\n      this.url,\n    ).toString();\n\n    const response = await this.fetchFn(canonicalPath, this.fetchOptions);\n    if (!response.ok) {\n      throw new Error(\n        `Failed to fetch \"${canonicalPath}\". Status code: ${response.status}.`,\n      );\n    }\n    const contents = await response.text();\n    const manifest = parseJson(contents);\n    const vfile = new VirtualFile<SnapManifest>({\n      value: contents,\n      result: createSnapManifest(manifest),\n      path: NpmSnapFileNames.Manifest,\n      data: { canonicalPath },\n    });\n    this.validatedManifest = vfile;\n\n    return this.manifest();\n  }\n\n  async fetch(path: string): Promise<VirtualFile> {\n    const relativePath = normalizeRelative(path);\n    const cached = this.cache.get(relativePath);\n    if (cached !== undefined) {\n      return cached.clone();\n    }\n\n    const canonicalPath = this.toCanonical(relativePath).toString();\n    const response = await this.fetchFn(canonicalPath, this.fetchOptions);\n    if (!response.ok) {\n      throw new Error(\n        `Failed to fetch \"${canonicalPath}\". Status code: ${response.status}.`,\n      );\n    }\n    const buffer = await response.arrayBuffer();\n    const vfile = new VirtualFile({\n      value: new Uint8Array(buffer),\n      path: relativePath,\n      data: { canonicalPath },\n    });\n    assert(\n      !this.cache.has(relativePath),\n      'Corrupted cache, multiple files with same path.',\n    );\n    this.cache.set(relativePath, vfile);\n\n    return this.fetch(relativePath);\n  }\n\n  get root(): URL {\n    return new URL(this.url);\n  }\n\n  // TODO: Either fix this lint violation or explain why it's necessary to\n  //  ignore.\n  // eslint-disable-next-line no-restricted-syntax\n  private toCanonical(path: string): URL {\n    assert(!path.startsWith('/'), 'Tried to parse absolute path.');\n    return new URL(path, this.url);\n  }\n}\n"]}