{"version":3,"file":"custom-resolver.cjs","names":["builtinModules","isDependencyPartOfPackage"],"sources":["../../src/validator/custom-resolver.ts"],"sourcesContent":["import { existsSync } from 'node:fs';\nimport { readFile } from 'node:fs/promises';\nimport type { LoadHookContext, ResolveHookContext } from 'node:module';\nimport { builtinModules } from 'node:module';\nimport { join } from 'node:path';\nimport { isDependencyPartOfPackage } from '../build/utils';\n\nconst STUB_PREFIX = 'mastra-stub:';\n\nlet _stubbedExternals: string[] | null = null;\nfunction getStubbedExternals(): string[] {\n  if (_stubbedExternals === null) {\n    try {\n      _stubbedExternals = JSON.parse(process.env.STUBBED_EXTERNALS || '[]') as string[];\n    } catch {\n      _stubbedExternals = [];\n    }\n  }\n  return _stubbedExternals;\n}\n\nconst cache = new Map<string, Record<string, string>>();\n\n/**\n * Check if a module is a Node.js builtin module\n * @param specifier - Module specifier\n * @returns True if it's a builtin module\n */\nfunction isBuiltinModule(specifier: string): boolean {\n  return (\n    builtinModules.includes(specifier) ||\n    specifier.startsWith('node:') ||\n    builtinModules.includes(specifier.replace(/^node:/, ''))\n  );\n}\n\n/**\n * Check if a module specifier is a relative or absolute path\n * @param specifier - Module specifier\n * @returns True if it's a relative or absolute path\n */\nfunction isRelativePath(specifier: string): boolean {\n  return (\n    specifier.startsWith('./') ||\n    specifier.startsWith('../') ||\n    specifier.startsWith('/') ||\n    /^[a-zA-Z]:\\\\/.test(specifier)\n  ); // Windows absolute path\n}\n\n/**\n * Get the path to resolve any external packages from\n *\n * @param url\n * @returns\n */\nasync function getParentPath(specifier: string, url: string): Promise<string | null> {\n  if (!cache.size) {\n    let moduleResolveMapLocation = process.env.MODULE_MAP;\n    if (!moduleResolveMapLocation) {\n      moduleResolveMapLocation = join(process.cwd(), 'module-resolve-map.json');\n    }\n\n    let moduleResolveMap: Record<string, Record<string, string>> = {};\n    if (existsSync(moduleResolveMapLocation)) {\n      moduleResolveMap = JSON.parse(await readFile(moduleResolveMapLocation, 'utf-8')) as Record<\n        string,\n        Record<string, string>\n      >;\n    }\n\n    for (const [id, rest] of Object.entries(moduleResolveMap)) {\n      cache.set(id, rest);\n    }\n  }\n\n  const importers = cache.get(url);\n  if (!importers) {\n    return null;\n  }\n\n  const matchedPackage = Object.keys(importers).find(external => isDependencyPartOfPackage(specifier, external));\n  if (!matchedPackage) {\n    return null;\n  }\n  const specifierParent = importers[matchedPackage]!;\n  return specifierParent;\n}\n\nexport async function resolve(\n  specifier: string,\n  context: ResolveHookContext,\n  nextResolve: (specifier: string, context: ResolveHookContext) => Promise<{ url: string }>,\n) {\n  // Don't modify builtin modules\n  if (isBuiltinModule(specifier)) {\n    return nextResolve(specifier, context);\n  }\n\n  if (isRelativePath(specifier)) {\n    return nextResolve(specifier, context);\n  }\n\n  // Stub GLOBAL_EXTERNALS packages during validation\n  const stubbedExternals = getStubbedExternals();\n  if (stubbedExternals.length > 0) {\n    const isStubbed = stubbedExternals.some(ext => isDependencyPartOfPackage(specifier, ext));\n    if (isStubbed) {\n      return { url: `${STUB_PREFIX}${specifier}`, shortCircuit: true };\n    }\n  }\n\n  if (context.parentURL) {\n    const parentPath = await getParentPath(specifier, context.parentURL);\n\n    if (parentPath) {\n      return nextResolve(specifier, {\n        ...context,\n        parentURL: parentPath,\n      });\n    }\n  }\n\n  // Continue resolution with the modified path\n  return nextResolve(specifier, context);\n}\n\nexport async function load(\n  url: string,\n  context: LoadHookContext,\n  nextLoad: (url: string, context: LoadHookContext) => Promise<{ format: string; source: string }>,\n) {\n  if (url.startsWith(STUB_PREFIX)) {\n    return { format: 'module', source: 'export default {}', shortCircuit: true };\n  }\n  return nextLoad(url, context);\n}\n"],"mappings":";;;;;;;AAOA,MAAM,cAAc;AAEpB,IAAI,oBAAqC;AACzC,SAAS,sBAAgC;CACvC,IAAI,sBAAsB,MACxB,IAAI;EACF,oBAAoB,KAAK,MAAM,QAAQ,IAAI,qBAAqB,IAAI;CACtE,QAAQ;EACN,oBAAoB,CAAC;CACvB;CAEF,OAAO;AACT;AAEA,MAAM,wB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