{"version":3,"file":"contextPreprocessor.mjs","names":["processedContextCache: Record<string, object>","result: Record<string, unknown>"],"sources":["../../src/cryptosuites/contextPreprocessor.ts"],"sourcesContent":["/**\n * Context Preprocessor\n *\n * Strips @protected flags from JSON-LD contexts to allow combining\n * multiple contexts that define the same terms identically.\n *\n * This is necessary because:\n * - VC v2 and OBv3 contexts both define terms like \"description\"\n * - Both map to the same IRIs (e.g., https://schema.org/description)\n * - But both use @protected: true, which prevents redefinition\n * - Stripping @protected allows compatible contexts to work together\n */\n\n// eslint-disable-next-line @typescript-eslint/ban-ts-comment\n// @ts-ignore\nimport * as jsonld from '@digitalcredentials/jsonld'\n\n// Cache for preprocessed contexts\nconst processedContextCache: Record<string, object> = {}\n\n/**\n * Recursively remove @protected from a context object\n */\nfunction stripProtected(obj: unknown): unknown {\n  if (obj === null || obj === undefined) {\n    return obj\n  }\n\n  if (Array.isArray(obj)) {\n    return obj.map(stripProtected)\n  }\n\n  if (typeof obj === 'object') {\n    const result: Record<string, unknown> = {}\n    for (const [key, value] of Object.entries(obj as Record<string, unknown>)) {\n      // Skip @protected keys\n      if (key === '@protected') {\n        continue\n      }\n      result[key] = stripProtected(value)\n    }\n    return result\n  }\n\n  return obj\n}\n\n/**\n * Minimal logger-shape accepted by the preprocessor. Callers can pass an\n * `AgentContext['config']['logger']` here; we don't import the Credo Logger\n * type to keep this module free of `@credo-ts/core` runtime imports.\n */\nexport interface ContextPreprocessorLogger {\n  warn: (message: string, data?: Record<string, unknown>) => void\n}\n\n/**\n * Create a document loader that preprocesses contexts to remove @protected.\n *\n * When a `logger` is provided, fetch failures are reported via `logger.warn`.\n * When omitted, failures are swallowed silently — the fallback empty context\n * is returned either way.\n */\nexport function createPreprocessingDocumentLoader(logger?: ContextPreprocessorLogger) {\n  const jld = jsonld.default ?? jsonld\n  const nodeDocumentLoader = jld.documentLoaders?.node?.()\n\n  return async (url: string) => {\n    // Check cache first\n    if (processedContextCache[url]) {\n      return {\n        contextUrl: null,\n        document: processedContextCache[url],\n        documentUrl: url,\n      }\n    }\n\n    // Fetch from network\n    if (nodeDocumentLoader) {\n      try {\n        const result = await nodeDocumentLoader(url)\n\n        if (result?.document) {\n          // Strip @protected from the context\n          const processed = stripProtected(result.document)\n          processedContextCache[url] = processed as object\n\n          return {\n            contextUrl: null,\n            document: processed,\n            documentUrl: url,\n          }\n        }\n        return result\n      } catch (e) {\n        logger?.warn('[ContextPreprocessor] Failed to fetch context', { url, error: (e as Error)?.message })\n      }\n    }\n\n    // Return empty context if fetch fails\n    return {\n      contextUrl: null,\n      document: { '@context': {} },\n      documentUrl: url,\n    }\n  }\n}\n\n/**\n * Clear the processed context cache\n */\nexport function clearContextCache() {\n  for (const key of Object.keys(processedContextCache)) {\n    delete processedContextCache[key]\n  }\n}\n\n/**\n * Pre-warm the context cache by fetching common contexts\n */\nexport async function prewarmContextCache(urls: string[]) {\n  const loader = createPreprocessingDocumentLoader()\n  await Promise.all(urls.map((url) => loader(url).catch(() => null)))\n}\n"],"mappings":";;;;;;;;;;;;;;;AAkBA,MAAMA,wBAAgD,EAAE;;;;AAKxD,SAAS,eAAe,KAAuB;AAC7C,KAAI,QAAQ,QAAQ,QAAQ,OAC1B,QAAO;AAGT,KAAI,MAAM,QAAQ,IAAI,CACpB,QAAO,IAAI,IAAI,eAAe;AAGhC,KAAI,OAAO,QAAQ,UAAU;EAC3B,MAAMC,SAAkC,EAAE;AAC1C,OAAK,MAAM,CAAC,KAAK,UAAU,OAAO,QAAQ,IAA+B,EAAE;AAEzE,OAAI,QAAQ,aACV;AAEF,UAAO,OAAO,eAAe,MAAM;;AAErC,SAAO;;AAGT,QAAO;;;;;;;;;AAmBT,SAAgB,kCAAkC,QAAoC;CAEpF,MAAM,sBADM,OAAO,WAAW,QACC,iBAAiB,QAAQ;AAExD,QAAO,OAAO,QAAgB;AAE5B,MAAI,sBAAsB,KACxB,QAAO;GACL,YAAY;GACZ,UAAU,sBAAsB;GAChC,aAAa;GACd;AAIH,MAAI,mBACF,KAAI;GACF,MAAM,SAAS,MAAM,mBAAmB,IAAI;AAE5C,OAAI,QAAQ,UAAU;IAEpB,MAAM,YAAY,eAAe,OAAO,SAAS;AACjD,0BAAsB,OAAO;AAE7B,WAAO;KACL,YAAY;KACZ,UAAU;KACV,aAAa;KACd;;AAEH,UAAO;WACA,GAAG;AACV,WAAQ,KAAK,iDAAiD;IAAE;IAAK,OAAQ,GAAa;IAAS,CAAC;;AAKxG,SAAO;GACL,YAAY;GACZ,UAAU,EAAE,YAAY,EAAE,EAAE;GAC5B,aAAa;GACd"}