| 377 | prefix.every((byte, index) => bytes[index] === byte); |
| 378 | |
| 379 | const isLikelyUtf8Text = (bytes: Uint8Array): boolean => { |
| 380 | if (bytes.length === 0) return false; |
| 381 | let text: string; |
| 382 | // oxlint-disable-next-line executor/no-try-catch-or-throw -- boundary: TextDecoder throws while probing arbitrary binary content |
| 383 | try { |
| 384 | text = new TextDecoder("utf-8", { fatal: true }).decode(bytes); |
| 385 | } catch { |
| 386 | return false; |
| 387 | } |
| 388 | let suspicious = 0; |
| 389 | for (let index = 0; index < text.length; index += 1) { |
| 390 | const code = text.charCodeAt(index); |
| 391 | const allowedControl = code === 0x09 || code === 0x0a || code === 0x0c || code === 0x0d; |
| 392 | if (code === 0x00) return false; |
| 393 | if (code < 0x20 && !allowedControl) suspicious += 1; |
| 394 | } |
| 395 | return suspicious / Math.max(1, text.length) <= 0.02; |
| 396 | }; |
| 397 | |
| 398 | const sniffMimeType = (bytes: Uint8Array): string | null => { |
| 399 | if (startsWithBytes(bytes, [0xff, 0xd8, 0xff])) return "image/jpeg"; |