(content: string, limit = 500_000)
| 30 | } |
| 31 | |
| 32 | export function sampledChecksum(content: string, limit = 500_000): string | undefined { |
| 33 | if (!content) return undefined |
| 34 | if (content.length <= limit) return checksum(content) |
| 35 | |
| 36 | const size = 4096 |
| 37 | const points = [ |
| 38 | 0, |
| 39 | Math.floor(content.length * 0.25), |
| 40 | Math.floor(content.length * 0.5), |
| 41 | Math.floor(content.length * 0.75), |
| 42 | content.length - size, |
| 43 | ] |
| 44 | const hashes = points |
| 45 | .map((point) => { |
| 46 | const start = Math.max(0, Math.min(content.length - size, point - Math.floor(size / 2))) |
| 47 | return checksum(content.slice(start, start + size)) ?? "" |
| 48 | }) |
| 49 | .join(":") |
| 50 | return `${content.length}:${hashes}` |
| 51 | } |
no test coverage detected