Skip to content

Instantly share code, notes, and snippets.

@0xlkda
Last active February 22, 2023 09:56
Show Gist options
  • Save 0xlkda/e019ccdbda3f9b6a552e3e2bda12e660 to your computer and use it in GitHub Desktop.
Save 0xlkda/e019ccdbda3f9b6a552e3e2bda12e660 to your computer and use it in GitHub Desktop.
// Detects encoding of a buffer
exports.detectEncoding = function (buffer) {
for (var i = 0; i < buffer.length; i++) {
if (buffer[i] === 65533 || buffer[i] <= 8) {
// The character code 65533 is often used as a replacement character for an unknown or unprintable character in a text file.
// The check for values less than or equal to 8 is intended to exclude control characters which are typically not part of human-readable text.
return 'binary'
}
}
return 'utf8'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment