Files
my_site/node_modules/@dotenvx/dotenvx/src/lib/helpers/detectEncoding.js
2026-03-24 14:30:59 +08:00

23 lines
492 B
JavaScript

const fs = require('fs')
function detectEncoding (filepath) {
const buffer = fs.readFileSync(filepath)
// check for UTF-16LE BOM (Byte Order Mark)
if (buffer.length >= 2 && buffer[0] === 0xFF && buffer[1] === 0xFE) {
return 'utf16le'
}
/* c8 ignore start */
// check for UTF-8 BOM
if (buffer.length >= 3 && buffer[0] === 0xEF && buffer[1] === 0xBB && buffer[2] === 0xBF) {
return 'utf8'
}
/* c8 ignore stop */
return 'utf8'
}
module.exports = detectEncoding