| 423 | prefix.every((byte, index) => bytes[index] === byte); |
| 424 | |
| 425 | const isLikelyUtf8Text = (bytes: Uint8Array): boolean => { |
| 426 | if (bytes.length === 0) return false; |
| 427 | let text: string; |
| 428 | // oxlint-disable-next-line executor/no-try-catch-or-throw -- boundary: TextDecoder throws while probing arbitrary binary content |
| 429 | try { |
| 430 | text = new TextDecoder("utf-8", { fatal: true }).decode(bytes); |
| 431 | } catch { |
| 432 | return false; |
| 433 | } |
| 434 | let suspicious = 0; |
| 435 | for (let index = 0; index < text.length; index += 1) { |
| 436 | const code = text.charCodeAt(index); |
| 437 | const allowedControl = code === 0x09 || code === 0x0a || code === 0x0c || code === 0x0d; |
| 438 | if (code === 0x00) return false; |
| 439 | if (code < 0x20 && !allowedControl) suspicious += 1; |
| 440 | } |
| 441 | return suspicious / Math.max(1, text.length) <= 0.02; |
| 442 | }; |
| 443 | |
| 444 | const sniffMimeType = (bytes: Uint8Array): string | null => { |
| 445 | if (startsWithBytes(bytes, [0xff, 0xd8, 0xff])) return "image/jpeg"; |