* Index a single file
(relativePath: string)
| 3258 | * Index a single file |
| 3259 | */ |
| 3260 | async indexFile(relativePath: string): Promise<ExtractionResult> { |
| 3261 | // Indexing read: follow in-root symlinks (the `../` guard still applies), #935. |
| 3262 | const fullPath = validatePathWithinRoot(this.rootDir, relativePath, { allowSymlinkEscape: true }); |
| 3263 | |
| 3264 | if (!fullPath) { |
| 3265 | return { |
| 3266 | nodes: [], |
| 3267 | edges: [], |
| 3268 | unresolvedReferences: [], |
| 3269 | errors: [{ message: `Path traversal blocked: ${relativePath}`, filePath: relativePath, severity: 'error', code: 'path_traversal' }], |
| 3270 | durationMs: 0, |
| 3271 | }; |
| 3272 | } |
| 3273 | |
| 3274 | // Read file content and stats |
| 3275 | let content: string; |
| 3276 | let stats: fs.Stats; |
| 3277 | try { |
| 3278 | // An oversize file is stored as skipped; its bytes are never needed (#1910). |
| 3279 | const read = await readBoundedSource(fullPath); |
| 3280 | stats = read.stats; |
| 3281 | // An MPEG transport stream named `.ts` is not TypeScript (#1910). |
| 3282 | if (read.bytes !== null && isMpegTsBytes(relativePath, read.bytes)) { |
| 3283 | return { nodes: [], edges: [], unresolvedReferences: [], errors: [], durationMs: 0 }; |
| 3284 | } |
| 3285 | content = read.bytes === null ? oversizeStamp(stats.size) : read.bytes.toString('utf8'); |
| 3286 | } catch (error) { |
| 3287 | // Nothing at the path: nothing to index, and not a read failure (#2904). |
| 3288 | if (isMissingPathError(error)) { |
| 3289 | return { nodes: [], edges: [], unresolvedReferences: [], errors: [], durationMs: 0 }; |
| 3290 | } |
| 3291 | return { |
| 3292 | nodes: [], |
| 3293 | edges: [], |
| 3294 | unresolvedReferences: [], |
| 3295 | errors: [ |
| 3296 | { |
| 3297 | message: `Failed to read file: ${error instanceof Error ? error.message : String(error)}`, |
| 3298 | filePath: relativePath, |
| 3299 | severity: 'error', |
| 3300 | code: 'read_error', |
| 3301 | }, |
| 3302 | ], |
| 3303 | durationMs: 0, |
| 3304 | }; |
| 3305 | } |
| 3306 | |
| 3307 | return this.indexFileWithContent(relativePath, content, stats); |
| 3308 | } |
| 3309 | |
| 3310 | /** |
| 3311 | * Index a single file with pre-read content and stats. |
no test coverage detected