* Index a single file
(relativePath: string)
| 2767 | * Index a single file |
| 2768 | */ |
| 2769 | async indexFile(relativePath: string): Promise<ExtractionResult> { |
| 2770 | // Indexing read: follow in-root symlinks (the `../` guard still applies), #935. |
| 2771 | const fullPath = validatePathWithinRoot(this.rootDir, relativePath, { allowSymlinkEscape: true }); |
| 2772 | |
| 2773 | if (!fullPath) { |
| 2774 | return { |
| 2775 | nodes: [], |
| 2776 | edges: [], |
| 2777 | unresolvedReferences: [], |
| 2778 | errors: [{ message: `Path traversal blocked: ${relativePath}`, filePath: relativePath, severity: 'error', code: 'path_traversal' }], |
| 2779 | durationMs: 0, |
| 2780 | }; |
| 2781 | } |
| 2782 | |
| 2783 | // Read file content and stats |
| 2784 | let content: string; |
| 2785 | let stats: fs.Stats; |
| 2786 | try { |
| 2787 | // An oversize file is stored as skipped; its bytes are never needed (#1910). |
| 2788 | const read = await readBoundedSource(fullPath); |
| 2789 | stats = read.stats; |
| 2790 | // An MPEG transport stream named `.ts` is not TypeScript (#1910). |
| 2791 | if (read.bytes !== null && isMpegTsBytes(relativePath, read.bytes)) { |
| 2792 | return { nodes: [], edges: [], unresolvedReferences: [], errors: [], durationMs: 0 }; |
| 2793 | } |
| 2794 | content = read.bytes === null ? oversizeStamp(stats.size) : read.bytes.toString('utf8'); |
| 2795 | } catch (error) { |
| 2796 | return { |
| 2797 | nodes: [], |
| 2798 | edges: [], |
| 2799 | unresolvedReferences: [], |
| 2800 | errors: [ |
| 2801 | { |
| 2802 | message: `Failed to read file: ${error instanceof Error ? error.message : String(error)}`, |
| 2803 | filePath: relativePath, |
| 2804 | severity: 'error', |
| 2805 | code: 'read_error', |
| 2806 | }, |
| 2807 | ], |
| 2808 | durationMs: 0, |
| 2809 | }; |
| 2810 | } |
| 2811 | |
| 2812 | return this.indexFileWithContent(relativePath, content, stats); |
| 2813 | } |
| 2814 | |
| 2815 | /** |
| 2816 | * Index a single file with pre-read content and stats. |
no test coverage detected