* Read the code body with a `stat`-first size guard so an oversize * artifact is rejected BEFORE we load + decode the whole thing into * memory. `readCodeFile` was the original sole-source — now wraps the * guard so the same VALIDATION_ERROR / PAYLOAD_TOO_LARGE shapes the * tests assert on still
(path: string)
| 848 | * tests assert on still flow through. |
| 849 | */ |
| 850 | function readCodeFileGuarded(path: string): string { |
| 851 | const absolute = resolveAbsolute(path); |
| 852 | let stat; |
| 853 | try { |
| 854 | stat = statSync(absolute); |
| 855 | } catch (err) { |
| 856 | const code = (err as NodeJS.ErrnoException).code; |
| 857 | if (code === 'ENOENT') { |
| 858 | throw localValidationError('code-file', `file does not exist: ${path}`); |
| 859 | } |
| 860 | if (code === 'EACCES') { |
| 861 | throw localValidationError('code-file', `permission denied reading ${path}`); |
| 862 | } |
| 863 | const reason = err instanceof Error ? err.message : 'unknown error'; |
| 864 | throw localValidationError('code-file', `cannot stat ${path}: ${reason}`); |
| 865 | } |
| 866 | if (stat.size > MAX_INLINE_CODE_BYTES) { |
| 867 | throw ApiError.fromEnvelope({ |
| 868 | error: { |
| 869 | code: 'PAYLOAD_TOO_LARGE', |
| 870 | message: `Inline code exceeds the 350 KB CLI cap (${stat.size} bytes).`, |
| 871 | nextAction: 'Upload via the Portal, or split into smaller tests.', |
| 872 | requestId: 'local', |
| 873 | details: { field: 'code-file', sizeBytes: stat.size, maxBytes: MAX_INLINE_CODE_BYTES }, |
| 874 | }, |
| 875 | }); |
| 876 | } |
| 877 | return readCodeFile(absolute); |
| 878 | } |
| 879 | |
| 880 | function readCodeFile(path: string): string { |
| 881 | try { |
no test coverage detected