index.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. var fs = require('fs');
  2. var utf8 = require('./encoding/utf8'),
  3. unicode = require('./encoding/unicode'),
  4. mbcs = require('./encoding/mbcs'),
  5. sbcs = require('./encoding/sbcs'),
  6. iso2022 = require('./encoding/iso2022');
  7. var self = this;
  8. var recognisers = [
  9. new utf8,
  10. new unicode.UTF_16BE,
  11. new unicode.UTF_16LE,
  12. new unicode.UTF_32BE,
  13. new unicode.UTF_32LE,
  14. new mbcs.sjis,
  15. new mbcs.big5,
  16. new mbcs.euc_jp,
  17. new mbcs.euc_kr,
  18. new mbcs.gb_18030,
  19. new iso2022.ISO_2022_JP,
  20. new iso2022.ISO_2022_KR,
  21. new iso2022.ISO_2022_CN,
  22. new sbcs.ISO_8859_1,
  23. new sbcs.ISO_8859_2,
  24. new sbcs.ISO_8859_5,
  25. new sbcs.ISO_8859_6,
  26. new sbcs.ISO_8859_7,
  27. new sbcs.ISO_8859_8,
  28. new sbcs.ISO_8859_9,
  29. new sbcs.windows_1251,
  30. new sbcs.windows_1256,
  31. new sbcs.KOI8_R
  32. ];
  33. module.exports.detect = function(buffer) {
  34. // Tally up the byte occurence statistics.
  35. var fByteStats = [];
  36. for (var i = 0; i < 256; i++)
  37. fByteStats[i] = 0;
  38. for (var i = buffer.length - 1; i >= 0; i--)
  39. fByteStats[buffer[i] & 0x00ff]++;
  40. var fC1Bytes = false;
  41. for (var i = 0x80; i <= 0x9F; i += 1) {
  42. if (fByteStats[i] != 0) {
  43. fC1Bytes = true;
  44. break;
  45. }
  46. }
  47. var context = {
  48. fByteStats: fByteStats,
  49. fC1Bytes: fC1Bytes,
  50. fRawInput: buffer,
  51. fRawLength: buffer.length,
  52. fInputBytes: buffer,
  53. fInputLen: buffer.length
  54. };
  55. var match = recognisers.map(function(rec) {
  56. return rec.match(context);
  57. }).filter(function(match) {
  58. return !!match;
  59. }).sort(function(a, b) {
  60. return a.confidence - b.confidence;
  61. }).pop();
  62. return match ? match.name : null;
  63. };
  64. module.exports.detectFile = function(filepath, opts, cb) {
  65. if (typeof opts === 'function') {
  66. cb = opts;
  67. opts = undefined;
  68. }
  69. var fd;
  70. var handler = function(err, buffer) {
  71. if (fd) {
  72. fs.closeSync(fd);
  73. }
  74. if (err) return cb(err, null);
  75. cb(null, self.detect(buffer));
  76. };
  77. if (opts && opts.sampleSize) {
  78. fd = fs.openSync(filepath, 'r'),
  79. sample = new Buffer(opts.sampleSize);
  80. fs.read(fd, sample, 0, opts.sampleSize, null, function(err) {
  81. handler(err, sample);
  82. });
  83. return;
  84. }
  85. fs.readFile(filepath, handler);
  86. };
  87. module.exports.detectFileSync = function(filepath, opts) {
  88. if (opts && opts.sampleSize) {
  89. var fd = fs.openSync(filepath, 'r'),
  90. sample = new Buffer(opts.sampleSize);
  91. fs.readSync(fd, sample, 0, opts.sampleSize);
  92. fs.closeSync(fd);
  93. return self.detect(sample);
  94. }
  95. return self.detect(fs.readFileSync(filepath));
  96. };