{"version":3,"file":"documentLoader.mjs","names":["jsonld"],"sources":["../../../../src/modules/vc/jsonld/documentLoader.ts"],"sourcesContent":["import type { AgentContext } from '../../../agent/context/AgentContext'\nimport { CredoError } from '../../../error/CredoError'\nimport { isDid } from '../../../utils'\nimport { DidResolverService } from '../../dids'\nimport { DEFAULT_CONTEXTS } from './contexts'\nimport type { DocumentLoader } from './jsonld'\nimport jsonld from './jsonld'\nimport { getNativeDocumentLoader } from './nativeDocumentLoader'\n\nexport type DocumentLoaderWithContext = (agentContext: AgentContext) => DocumentLoader\n\nexport function defaultDocumentLoader(\n  agentContext: AgentContext,\n  additionalContexts: Record<string, unknown> = {}\n): DocumentLoader {\n  const didResolver = agentContext.dependencyManager.resolve(DidResolverService)\n  const contexts = { ...DEFAULT_CONTEXTS, ...additionalContexts }\n\n  async function loader(url: string) {\n    // Check if in the default contexts shipped with Credo\n    if (url in contexts) {\n      return {\n        contextUrl: null,\n        documentUrl: url,\n        document: contexts[url as keyof typeof contexts],\n      }\n    }\n\n    const withoutFragment = url.split('#')[0]\n    if (withoutFragment in contexts) {\n      return {\n        contextUrl: null,\n        documentUrl: url,\n        document: contexts[withoutFragment as keyof typeof contexts],\n      }\n    }\n\n    if (isDid(url)) {\n      const result = await didResolver.resolve(agentContext, url)\n\n      if (result.didResolutionMetadata.error || !result.didDocument) {\n        throw new CredoError(`Unable to resolve DID: ${url}`)\n      }\n\n      const framed = await jsonld.frame(\n        result.didDocument.toJSON(),\n        {\n          '@context': result.didDocument.context,\n          '@embed': '@never',\n          id: url,\n        },\n        // @ts-expect-error\n        { documentLoader: this }\n      )\n\n      return {\n        contextUrl: null,\n        documentUrl: url,\n        document: framed,\n      }\n    }\n\n    // fetches the documentLoader from documentLoader.ts or documentLoader.native.ts depending on the platform at bundle time\n    const platformLoader = await getNativeDocumentLoader()\n    const nativeLoader = platformLoader.apply(jsonld, [])\n\n    return await nativeLoader(url)\n  }\n\n  return loader.bind(loader)\n}\n"],"mappings":";;;;;;;;;AAWA,SAAgB,sBACd,cACA,qBAA8C,CAAC,GAC/B;CAChB,MAAM,cAAc,aAAa,kBAAkB,QAAQ,kBAAkB;CAC7E,MAAM,WAAW;EAAE,GAAG;EAAkB,GAAG;CAAmB;CAE9D,eAAe,OAAO,KAAa;EAEjC,IAAI,OAAO,UACT,OAAO;GACL,YAAY;GACZ,aAAa;GACb,UAAU,SAAS;EACrB;EAGF,MAAM,kBAAkB,IAAI,MAAM,GAAG,CAAC,CAAC;EACvC,IAAI,mBAAmB,UACrB,OAAO;GACL,YAAY;GACZ,aAAa;GACb,UAAU,SAAS;EACrB;EAGF,IAAI,MAAM,GAAG,GAAG;GACd,MAAM,SAAS,MAAM,YAAY,QAAQ,cAAc,GAAG;GAE1D,IAAI,OAAO,sBAAsB,SAAS,CAAC,OAAO,aAChD,MAAM,IAAI,WAAW,0BAA0B,KAAK;GActD,OAAO;IACL,YAAY;IACZ,aAAa;IACb,UAAU,MAdSA,eAAO,MAC1B,OAAO,YAAY,OAAO,GAC1B;KACE,YAAY,OAAO,YAAY;KAC/B,UAAU;KACV,IAAI;IACN,GAEA,EAAE,gBAAgB,KAAK,CACzB;GAMA;EACF;EAMA,OAAO,OAFc,MADQ,wBAAwB,EAAA,CACjB,MAAMA,gBAAQ,CAAC,CAE3B,CAAC,CAAC,GAAG;CAC/B;CAEA,OAAO,OAAO,KAAK,MAAM;AAC3B"}