(buffer: Buffer)
| 129 | * or a high proportion of non-printable characters. |
| 130 | */ |
| 131 | export function isBinaryContent(buffer: Buffer): boolean { |
| 132 | // Check first BINARY_CHECK_SIZE bytes (or full buffer if smaller) |
| 133 | const checkSize = Math.min(buffer.length, BINARY_CHECK_SIZE) |
| 134 | |
| 135 | let nonPrintable = 0 |
| 136 | for (let i = 0; i < checkSize; i++) { |
| 137 | const byte = buffer[i]! |
| 138 | // Null byte is a strong indicator of binary |
| 139 | if (byte === 0) { |
| 140 | return true |
| 141 | } |
| 142 | // Count non-printable, non-whitespace bytes |
| 143 | // Printable ASCII is 32-126, plus common whitespace (9, 10, 13) |
| 144 | if ( |
| 145 | byte < 32 && |
| 146 | byte !== 9 && // tab |
| 147 | byte !== 10 && // newline |
| 148 | byte !== 13 // carriage return |
| 149 | ) { |
| 150 | nonPrintable++ |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | // If more than 10% non-printable, likely binary |
| 155 | return nonPrintable / checkSize > 0.1 |
| 156 | } |
| 157 |
no outgoing calls
no test coverage detected