Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function isTextSync(filename, buffer) {
// Test extensions
if (filename) {
// Extract filename
const parts = pathUtil
.basename(filename)
.split('.')
.reverse()
// Cycle extensions
for (const extension of parts) {
if (textExtensions.indexOf(extension) !== -1) {
return true
}
if (binaryExtensions.indexOf(extension) !== -1) {
return false
}
}
}
// Fallback to encoding if extension check was not enough
if (buffer) {
return getEncodingSync(buffer) === 'utf8'
}
// No buffer was provided
return null
}
const isBinaryFile = async filePath => {
const ext = path.extname(filePath).slice(1);
if (ext) {
if (textExtensions.includes(ext)) { return false; }
if (binaryExtensions.includes(ext)) { return true; }
}
return _isBinaryFile(filePath);
}