|
| 1 | +// Export schemas object that loads from the namespaced cache |
| 2 | +// The cache is namespaced by consumer package name and version to avoid conflicts |
| 3 | + |
| 4 | +import { createRequire } from 'module'; |
| 5 | +import { fileURLToPath } from 'url'; |
| 6 | +import { dirname, join, basename } from 'path'; |
| 7 | +import { readFileSync } from 'fs'; |
| 8 | + |
| 9 | +const __filename = fileURLToPath(import.meta.url); |
| 10 | +const __dirname = dirname(__filename); |
| 11 | +const require = createRequire(import.meta.url); |
| 12 | + |
| 13 | +/** |
| 14 | + * Find the consumer's package.json by looking at who is importing this module |
| 15 | + * Uses the stack trace to find the actual caller's location |
| 16 | + */ |
| 17 | +function findConsumerPackageFromCaller() { |
| 18 | + // Get the stack trace |
| 19 | + const originalPrepareStackTrace = Error.prepareStackTrace; |
| 20 | + Error.prepareStackTrace = (_, stack) => stack; |
| 21 | + const stack = new Error().stack; |
| 22 | + Error.prepareStackTrace = originalPrepareStackTrace; |
| 23 | + |
| 24 | + // Find the first call site that's NOT in @sourcemeta/registry |
| 25 | + for (const callSite of stack) { |
| 26 | + const fileName = callSite.getFileName(); |
| 27 | + if (fileName && !fileName.includes('@sourcemeta/registry') && !fileName.startsWith('node:')) { |
| 28 | + // Convert file:// URL to path if needed |
| 29 | + let filePath = fileName; |
| 30 | + if (filePath.startsWith('file://')) { |
| 31 | + filePath = fileURLToPath(filePath); |
| 32 | + } |
| 33 | + |
| 34 | + // Walk up from this file to find its package.json |
| 35 | + let currentPath = dirname(filePath); |
| 36 | + while (currentPath !== dirname(currentPath)) { |
| 37 | + const packageJsonPath = join(currentPath, 'package.json'); |
| 38 | + try { |
| 39 | + const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf8')); |
| 40 | + return { name: packageJson.name, version: packageJson.version, path: currentPath }; |
| 41 | + } catch (error) { |
| 42 | + // Continue searching |
| 43 | + } |
| 44 | + currentPath = dirname(currentPath); |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + // Fallback to the old method |
| 50 | + let currentPath = dirname(dirname(__dirname)); // Start from node_modules parent |
| 51 | + while (currentPath !== dirname(currentPath)) { |
| 52 | + const packageJsonPath = join(currentPath, 'package.json'); |
| 53 | + try { |
| 54 | + const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf8')); |
| 55 | + if (packageJson.name !== '@sourcemeta/registry') { |
| 56 | + return { name: packageJson.name, version: packageJson.version, path: currentPath }; |
| 57 | + } |
| 58 | + } catch (error) { |
| 59 | + // Continue searching |
| 60 | + } |
| 61 | + currentPath = dirname(currentPath); |
| 62 | + } |
| 63 | + |
| 64 | + throw new Error('Could not find consumer package.json'); |
| 65 | +} |
| 66 | + |
| 67 | +// Cache schemas per consumer package |
| 68 | +const schemasCache = new Map(); |
| 69 | + |
| 70 | +function getSchemas() { |
| 71 | + const consumer = findConsumerPackageFromCaller(); |
| 72 | + const cacheKey = `${consumer.name}@${consumer.version}`; |
| 73 | + |
| 74 | + if (!schemasCache.has(cacheKey)) { |
| 75 | + // Find the root node_modules directory by walking up from @sourcemeta/registry's location |
| 76 | + // The cache is always in the root node_modules, not in the package's own node_modules |
| 77 | + let rootNodeModules = dirname(__dirname); // node_modules/@sourcemeta |
| 78 | + while (basename(rootNodeModules) !== 'node_modules' && rootNodeModules !== dirname(rootNodeModules)) { |
| 79 | + rootNodeModules = dirname(rootNodeModules); |
| 80 | + } |
| 81 | + |
| 82 | + const cacheIndexPath = join( |
| 83 | + rootNodeModules, |
| 84 | + '.cache', |
| 85 | + '@sourcemeta', |
| 86 | + 'registry', |
| 87 | + consumer.name, |
| 88 | + consumer.version, |
| 89 | + 'index.cjs' |
| 90 | + ); |
| 91 | + schemasCache.set(cacheKey, require(cacheIndexPath)); |
| 92 | + } |
| 93 | + |
| 94 | + return schemasCache.get(cacheKey); |
| 95 | +} |
| 96 | + |
| 97 | +export const schemas = new Proxy({}, { |
| 98 | + get(target, prop) { |
| 99 | + return getSchemas()[prop]; |
| 100 | + }, |
| 101 | + has(target, prop) { |
| 102 | + return prop in getSchemas(); |
| 103 | + }, |
| 104 | + ownKeys() { |
| 105 | + return Object.keys(getSchemas()); |
| 106 | + }, |
| 107 | + getOwnPropertyDescriptor(target, prop) { |
| 108 | + const schemas = getSchemas(); |
| 109 | + if (prop in schemas) { |
| 110 | + return { |
| 111 | + enumerable: true, |
| 112 | + configurable: true, |
| 113 | + value: schemas[prop] |
| 114 | + }; |
| 115 | + } |
| 116 | + } |
| 117 | +}); |
0 commit comments