isBinary()
isBinary(filePath, sampleSize?, suspiciousThreshold?): Promise<boolean>
Defined in: utils.ts:84
Heuristically determines whether a file should be treated as binary.
Detection strategy:
- Reads only the first
sampleSizebytes (default: 8 KB). - Immediately returns
trueif a NULL byte (0x00) is found. - Computes the ratio of suspicious non-printable bytes.
If the ratio exceeds
suspiciousThreshold, the file is treated as binary.
Suspicious bytes:
- Control characters outside common whitespace
- Bytes < 7 or between 15–31 (excluding \t, \n, \r)
This approach is:
- Fast (does not read entire file)
- Safe for large workspace scans
- Reliable for common binary formats (PNG, JPEG, ZIP, etc.)
⚠️ This is a heuristic, not MIME detection. Some exotic encodings may bypass detection.
Parameters
filePath
string
Absolute path to file.
sampleSize?
number = 8192
Number of bytes to inspect from file start.
suspiciousThreshold?
number = 0.3
Ratio (0–1) above which file is considered binary.
Returns
Promise<boolean>
true if file appears binary; otherwise false.
Example
if (await isBinary("/path/to/image.png")) {
return; // Skip scanning
}