{"version":3,"sources":["../../src/server/load-manifest.external.ts"],"sourcesContent":["import type { DeepReadonly } from '../shared/lib/deep-readonly'\n\nimport { join } from 'path'\nimport { readFileSync } from 'fs'\nimport { runInNewContext } from 'vm'\nimport { deepFreeze } from '../shared/lib/deep-freeze'\n\nconst sharedCache = new Map<string, unknown>()\n\n/**\n * Load a manifest file from the file system. Optionally cache the manifest in\n * memory to avoid reading the file multiple times using the provided cache or\n * defaulting to a shared module cache. The manifest is frozen to prevent\n * modifications if it is cached.\n *\n * @param path the path to the manifest file\n * @param shouldCache whether to cache the manifest in memory\n * @param cache the cache to use for storing the manifest\n * @returns the manifest object\n */\nexport function loadManifest<T extends object>(\n  path: string,\n  shouldCache: false\n): T\nexport function loadManifest<T extends object>(\n  path: string,\n  shouldCache?: boolean,\n  cache?: Map<string, unknown>,\n  skipParse?: boolean\n): DeepReadonly<T>\nexport function loadManifest<T extends object>(\n  path: string,\n  shouldCache?: boolean,\n  cache?: Map<string, unknown>,\n  skipParse?: boolean,\n  handleMissing?: boolean\n): DeepReadonly<T>\nexport function loadManifest<T extends object>(\n  path: string,\n  shouldCache: boolean = true,\n  cache = sharedCache,\n  skipParse = false,\n  handleMissing?: boolean\n): T {\n  const cached = shouldCache && cache.get(path)\n  if (cached) {\n    return cached as T\n  }\n\n  let manifest: any\n\n  if (handleMissing) {\n    try {\n      manifest = readFileSync(/* turbopackIgnore: true */ path, 'utf8')\n    } catch (err) {\n      let result = {} as any\n      cache.set(path, result)\n      return result\n    }\n  } else {\n    manifest = readFileSync(/* turbopackIgnore: true */ path, 'utf8')\n  }\n\n  if (!skipParse) {\n    manifest = JSON.parse(manifest)\n\n    // Freeze the manifest so it cannot be modified if we're caching it.\n    if (shouldCache) {\n      manifest = deepFreeze(manifest)\n    }\n  }\n\n  if (shouldCache) {\n    cache.set(path, manifest)\n  }\n\n  return manifest\n}\n\nexport function evalManifest<T extends object>(\n  path: string,\n  shouldCache: false\n): T\nexport function evalManifest<T extends object>(\n  path: string,\n  shouldCache?: boolean,\n  cache?: Map<string, unknown>,\n  handleMissing?: boolean\n): DeepReadonly<T>\nexport function evalManifest<T extends object>(\n  path: string,\n  shouldCache?: true,\n  cache?: Map<string, unknown>\n): DeepReadonly<T>\nexport function evalManifest<T extends object>(\n  path: string,\n  shouldCache: boolean = true,\n  cache = sharedCache,\n  handleMissing?: boolean\n): T {\n  const cached = shouldCache && cache.get(path)\n  if (cached) {\n    return cached as T\n  }\n\n  let content: any\n  if (handleMissing) {\n    try {\n      content = readFileSync(/* turbopackIgnore: true */ path, 'utf8')\n    } catch (err) {\n      let result = {} as any\n      cache.set(path, result)\n      return result\n    }\n  } else {\n    content = readFileSync(/* turbopackIgnore: true */ path, 'utf8')\n  }\n\n  if (content.length === 0) {\n    throw new Error('Manifest file is empty')\n  }\n\n  let contextObject = {\n    process: { env: { NEXT_DEPLOYMENT_ID: process.env.NEXT_DEPLOYMENT_ID } },\n  }\n  runInNewContext(content, contextObject)\n\n  // Freeze the context object so it cannot be modified if we're caching it.\n  if (shouldCache) {\n    contextObject = deepFreeze(contextObject)\n  }\n\n  if (shouldCache) {\n    cache.set(path, contextObject)\n  }\n\n  return contextObject as T\n}\n\nexport function loadManifestFromRelativePath<T extends object>({\n  projectDir,\n  distDir,\n  manifest,\n  shouldCache,\n  cache,\n  skipParse,\n  handleMissing,\n  useEval,\n}: {\n  projectDir: string\n  distDir: string\n  manifest: string\n  shouldCache: boolean\n  cache?: Map<string, unknown>\n  skipParse?: boolean\n  handleMissing?: boolean\n  useEval?: boolean\n}): DeepReadonly<T> {\n  const manifestPath = join(\n    /* turbopackIgnore: true */ projectDir,\n    distDir,\n    manifest\n  )\n\n  if (useEval) {\n    return evalManifest<T>(manifestPath, shouldCache, cache, handleMissing)\n  }\n  return loadManifest<T>(\n    manifestPath,\n    shouldCache,\n    cache,\n    skipParse,\n    handleMissing\n  )\n}\n\nexport function clearManifestCache(path: string, cache = sharedCache): boolean {\n  return cache.delete(path)\n}\n"],"names":["clearManifestCache","evalManifest","loadManifest","loadManifestFromRelativePath","sharedCache","Map","path","shouldCache","cache","skipParse","handleMissing","cached","get","manifest","readFileSync","err","result","set","JSON","parse","deepFreeze","content","length","Error","contextObject","process","env","NEXT_DEPLOYMENT_ID","runInNewContext","projectDir","distDir","useEval","manifestPath","join","delete"],"mappings":";;;;;;;;;;;;;;;;;IAgLgBA,kBAAkB;eAAlBA;;IAlFAC,YAAY;eAAZA;;IAzDAC,YAAY;eAAZA;;IAsGAC,4BAA4B;eAA5BA;;;sBAzIK;oBACQ;oBACG;4BACL;AAE3B,MAAMC,cAAc,IAAIC;AA8BjB,SAASH,aACdI,IAAY,EACZC,cAAuB,IAAI,EAC3BC,QAAQJ,WAAW,EACnBK,YAAY,KAAK,EACjBC,aAAuB;IAEvB,MAAMC,SAASJ,eAAeC,MAAMI,GAAG,CAACN;IACxC,IAAIK,QAAQ;QACV,OAAOA;IACT;IAEA,IAAIE;IAEJ,IAAIH,eAAe;QACjB,IAAI;YACFG,WAAWC,IAAAA,gBAAY,EAAC,yBAAyB,GAAGR,MAAM;QAC5D,EAAE,OAAOS,KAAK;YACZ,IAAIC,SAAS,CAAC;YACdR,MAAMS,GAAG,CAACX,MAAMU;YAChB,OAAOA;QACT;IACF,OAAO;QACLH,WAAWC,IAAAA,gBAAY,EAAC,yBAAyB,GAAGR,MAAM;IAC5D;IAEA,IAAI,CAACG,WAAW;QACdI,WAAWK,KAAKC,KAAK,CAACN;QAEtB,oEAAoE;QACpE,IAAIN,aAAa;YACfM,WAAWO,IAAAA,sBAAU,EAACP;QACxB;IACF;IAEA,IAAIN,aAAa;QACfC,MAAMS,GAAG,CAACX,MAAMO;IAClB;IAEA,OAAOA;AACT;AAiBO,SAASZ,aACdK,IAAY,EACZC,cAAuB,IAAI,EAC3BC,QAAQJ,WAAW,EACnBM,aAAuB;IAEvB,MAAMC,SAASJ,eAAeC,MAAMI,GAAG,CAACN;IACxC,IAAIK,QAAQ;QACV,OAAOA;IACT;IAEA,IAAIU;I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