|
| 1 | +/** |
| 2 | + * Local directory storage driver - reads packument JSON files from a directory |
| 3 | + * |
| 4 | + * This is a read-only storage driver that allows mounting existing directories |
| 5 | + * of packument JSON files as a virtual cache. Useful for analyzing local datasets |
| 6 | + * without importing them into the cache. |
| 7 | + */ |
| 8 | +import { readdir, readFile, access } from 'node:fs/promises'; |
| 9 | +import { join } from 'node:path'; |
| 10 | +import { existsSync } from 'node:fs'; |
| 11 | + |
| 12 | +/** |
| 13 | + * Check if an origin string represents a local path |
| 14 | + * @param {string} origin - Origin string (URL, encoded origin, or path) |
| 15 | + * @returns {boolean} True if origin is a local path |
| 16 | + */ |
| 17 | +export function isLocalPath(origin) { |
| 18 | + if (!origin) return false; |
| 19 | + |
| 20 | + // file:// URL |
| 21 | + if (origin.startsWith('file://')) return true; |
| 22 | + |
| 23 | + // Absolute path (Unix) |
| 24 | + if (origin.startsWith('/')) return true; |
| 25 | + |
| 26 | + // Relative path starting with ./ |
| 27 | + if (origin.startsWith('./') || origin.startsWith('../')) return true; |
| 28 | + |
| 29 | + // Windows absolute path (C:\, D:\, etc.) |
| 30 | + if (/^[A-Za-z]:[\\\/]/.test(origin)) return true; |
| 31 | + |
| 32 | + // Path that exists on disk (fallback check) |
| 33 | + if (existsSync(origin)) return true; |
| 34 | + |
| 35 | + return false; |
| 36 | +} |
| 37 | + |
| 38 | +/** |
| 39 | + * Normalize origin to a filesystem path |
| 40 | + * @param {string} origin - Origin string |
| 41 | + * @returns {string} Filesystem path |
| 42 | + */ |
| 43 | +function normalizePath(origin) { |
| 44 | + if (origin.startsWith('file://')) { |
| 45 | + return origin.replace('file://', ''); |
| 46 | + } |
| 47 | + return origin; |
| 48 | +} |
| 49 | + |
| 50 | +/** |
| 51 | + * Read-only storage driver for local directories of JSON files |
| 52 | + */ |
| 53 | +export class LocalDirStorageDriver { |
| 54 | + /** |
| 55 | + * @param {string} dirPath - Path to directory containing packument JSON files |
| 56 | + */ |
| 57 | + constructor(dirPath) { |
| 58 | + this.dirPath = normalizePath(dirPath); |
| 59 | + this.supportsBatch = false; |
| 60 | + this.supportsBloom = false; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Get a packument by key (filename) |
| 65 | + * @param {string} key - Filename (with or without .json extension) |
| 66 | + * @returns {Promise<object>} Parsed JSON content |
| 67 | + * @throws {Error} If file not found or invalid JSON |
| 68 | + */ |
| 69 | + async get(key) { |
| 70 | + const filename = key.endsWith('.json') ? key : `${key}.json`; |
| 71 | + const filePath = join(this.dirPath, filename); |
| 72 | + |
| 73 | + try { |
| 74 | + const content = await readFile(filePath, 'utf8'); |
| 75 | + return JSON.parse(content); |
| 76 | + } catch (error) { |
| 77 | + if (error.code === 'ENOENT') { |
| 78 | + throw new Error(`Key not found: ${key}`); |
| 79 | + } |
| 80 | + throw error; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Check if a key exists |
| 86 | + * @param {string} key - Filename |
| 87 | + * @returns {Promise<boolean>} |
| 88 | + */ |
| 89 | + async has(key) { |
| 90 | + const filename = key.endsWith('.json') ? key : `${key}.json`; |
| 91 | + const filePath = join(this.dirPath, filename); |
| 92 | + |
| 93 | + try { |
| 94 | + await access(filePath); |
| 95 | + return true; |
| 96 | + } catch { |
| 97 | + return false; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * List all JSON files in the directory |
| 103 | + * Note: prefix is ignored for local directories since the directory path |
| 104 | + * itself serves as the namespace isolation. |
| 105 | + * @param {string} [_prefix] - Ignored for local directories |
| 106 | + * @yields {string} Filenames |
| 107 | + */ |
| 108 | + async *list(_prefix) { |
| 109 | + const files = await readdir(this.dirPath); |
| 110 | + for (const file of files) { |
| 111 | + if (file.endsWith('.json')) { |
| 112 | + yield file; |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Put is not supported - this is a read-only driver |
| 119 | + * @throws {Error} Always throws |
| 120 | + */ |
| 121 | + async put(_key, _value) { |
| 122 | + throw new Error('LocalDirStorageDriver is read-only'); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Delete is not supported - this is a read-only driver |
| 127 | + * @throws {Error} Always throws |
| 128 | + */ |
| 129 | + async delete(_key) { |
| 130 | + throw new Error('LocalDirStorageDriver is read-only'); |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Clear is not supported - this is a read-only driver |
| 135 | + * @throws {Error} Always throws |
| 136 | + */ |
| 137 | + async clear() { |
| 138 | + throw new Error('LocalDirStorageDriver is read-only'); |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Batch put is not supported - this is a read-only driver |
| 143 | + * @throws {Error} Always throws |
| 144 | + */ |
| 145 | + async putBatch(_entries) { |
| 146 | + throw new Error('LocalDirStorageDriver is read-only'); |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * Get metadata info for a file (basic implementation) |
| 151 | + * @param {string} key - Filename |
| 152 | + * @returns {Promise<object|null>} Basic info or null if not found |
| 153 | + */ |
| 154 | + async info(key) { |
| 155 | + const exists = await this.has(key); |
| 156 | + if (!exists) return null; |
| 157 | + return { key, path: join(this.dirPath, key) }; |
| 158 | + } |
| 159 | +} |
0 commit comments