(root: string, files: AnswerFile[])
| 16 | const MAX_MS = 250; |
| 17 | |
| 18 | export async function validateAnswerFiles(root: string, files: AnswerFile[]): Promise<{ |
| 19 | stale: string[]; |
| 20 | unchecked: string[]; |
| 21 | }> { |
| 22 | const stale: string[] = []; |
| 23 | const unchecked: string[] = []; |
| 24 | const signal = AbortSignal.timeout(MAX_MS); |
| 25 | let bytes = 0; |
| 26 | for (let i = 0; i < files.length; i++) { |
| 27 | const file = files[i]!; |
| 28 | if (i >= MAX_FILES || bytes >= MAX_BYTES || signal.aborted) { |
| 29 | unchecked.push(...files.slice(i).map(f => f.path)); |
| 30 | break; |
| 31 | } |
| 32 | if (!file.contentHash) { |
| 33 | unchecked.push(file.path); |
| 34 | continue; |
| 35 | } |
| 36 | try { |
| 37 | const hash = createHash('sha256'); |
| 38 | const absolute = validatePathWithinRoot(root, file.path); |
| 39 | if (!absolute) { stale.push(file.path); continue; } |
| 40 | // A file over the index's size limit is stored as its size stamp |
| 41 | // (#1910): when the stamp still matches, the file is current without |
| 42 | // being read. Anything else falls through to the bounded read below. |
| 43 | const { size } = await stat(absolute); |
| 44 | if (size > MAX_SOURCE_FILE_SIZE_BYTES && |
| 45 | createHash('sha256').update(oversizeStamp(size)).digest('hex') === file.contentHash) { |
| 46 | continue; |
| 47 | } |
| 48 | const stream = createReadStream(absolute, { |
| 49 | encoding: 'utf8', highWaterMark: 64 * 1024, signal, |
| 50 | }); |
| 51 | let complete = true; |
| 52 | for await (const chunk of stream) { |
| 53 | bytes += Buffer.byteLength(chunk); |
| 54 | if (bytes > MAX_BYTES) { complete = false; break; } |
| 55 | hash.update(chunk); |
| 56 | } |
| 57 | if (!complete) unchecked.push(file.path); |
| 58 | else if (hash.digest('hex') !== file.contentHash) stale.push(file.path); |
| 59 | } catch { |
| 60 | // Timeout is unknown, never a clean bill of health. A missing or |
| 61 | // unreadable contributing file cannot support the indexed answer. |
| 62 | (signal.aborted ? unchecked : stale).push(file.path); |
| 63 | } |
| 64 | } |
| 65 | return { stale, unchecked }; |
| 66 | } |
no test coverage detected