( data: Buffer | null | undefined, sampleSize = 512, )
| 13 | * @returns True if a NULL byte is found, false otherwise. |
| 14 | */ |
| 15 | export function isBinary( |
| 16 | data: Buffer | null | undefined, |
| 17 | sampleSize = 512, |
| 18 | ): boolean { |
| 19 | if (!data) { |
| 20 | return false; |
| 21 | } |
| 22 | |
| 23 | const sample = data.length > sampleSize ? data.subarray(0, sampleSize) : data; |
| 24 | |
| 25 | for (const byte of sample) { |
| 26 | // The presence of a NULL byte (0x00) is one of the most reliable |
| 27 | // indicators of a binary file. Text files should not contain them. |
| 28 | if (byte === 0) { |
| 29 | return true; |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | // If no NULL bytes were found in the sample, we assume it's text. |
| 34 | return false; |
| 35 | } |
no outgoing calls
no test coverage detected