index.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. "use strict";
  2. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  3. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  4. return new (P || (P = Promise))(function (resolve, reject) {
  5. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  6. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  7. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  8. step((generator = generator.apply(thisArg, _arguments || [])).next());
  9. });
  10. };
  11. Object.defineProperty(exports, "__esModule", { value: true });
  12. const fs = require("fs");
  13. const util_1 = require("util");
  14. const statAsync = util_1.promisify(fs.stat);
  15. const openAsync = util_1.promisify(fs.open);
  16. const closeAsync = util_1.promisify(fs.close);
  17. const MAX_BYTES = 512;
  18. function isBinaryFile(file, size) {
  19. return __awaiter(this, void 0, void 0, function* () {
  20. if (isString(file)) {
  21. const stat = yield statAsync(file);
  22. isStatFile(stat);
  23. const fileDescriptor = yield openAsync(file, 'r');
  24. const allocBuffer = Buffer.alloc(MAX_BYTES);
  25. // Read the file with no encoding for raw buffer access.
  26. // NB: something is severely wrong with promisify, had to construct my own Promise
  27. return new Promise((fulfill, reject) => {
  28. fs.read(fileDescriptor, allocBuffer, 0, MAX_BYTES, 0, (err, bytesRead, _) => {
  29. closeAsync(fileDescriptor);
  30. if (err) {
  31. reject(err);
  32. }
  33. else {
  34. fulfill(isBinaryCheck(allocBuffer, bytesRead));
  35. }
  36. });
  37. });
  38. }
  39. else {
  40. if (size === undefined) {
  41. size = file.length;
  42. }
  43. return isBinaryCheck(file, size);
  44. }
  45. });
  46. }
  47. exports.isBinaryFile = isBinaryFile;
  48. function isBinaryFileSync(file, size) {
  49. if (isString(file)) {
  50. const stat = fs.statSync(file);
  51. isStatFile(stat);
  52. const fileDescriptor = fs.openSync(file, 'r');
  53. const allocBuffer = Buffer.alloc(MAX_BYTES);
  54. const bytesRead = fs.readSync(fileDescriptor, allocBuffer, 0, MAX_BYTES, 0);
  55. fs.closeSync(fileDescriptor);
  56. return isBinaryCheck(allocBuffer, bytesRead);
  57. }
  58. else {
  59. if (size === undefined) {
  60. size = file.length;
  61. }
  62. return isBinaryCheck(file, size);
  63. }
  64. }
  65. exports.isBinaryFileSync = isBinaryFileSync;
  66. function isBinaryCheck(fileBuffer, bytesRead) {
  67. // empty file. no clue what it is.
  68. if (bytesRead === 0) {
  69. return false;
  70. }
  71. let suspiciousBytes = 0;
  72. const totalBytes = Math.min(bytesRead, MAX_BYTES);
  73. // UTF-8 BOM
  74. if (bytesRead >= 3 && fileBuffer[0] === 0xef && fileBuffer[1] === 0xbb && fileBuffer[2] === 0xbf) {
  75. return false;
  76. }
  77. // UTF-32 BOM
  78. if (bytesRead >= 4 &&
  79. fileBuffer[0] === 0x00 &&
  80. fileBuffer[1] === 0x00 &&
  81. fileBuffer[2] === 0xfe &&
  82. fileBuffer[3] === 0xff) {
  83. return false;
  84. }
  85. // UTF-32 LE BOM
  86. if (bytesRead >= 4 &&
  87. fileBuffer[0] === 0xff &&
  88. fileBuffer[1] === 0xfe &&
  89. fileBuffer[2] === 0x00 &&
  90. fileBuffer[3] === 0x00) {
  91. return false;
  92. }
  93. // GB BOM
  94. if (bytesRead >= 4 &&
  95. fileBuffer[0] === 0x84 &&
  96. fileBuffer[1] === 0x31 &&
  97. fileBuffer[2] === 0x95 &&
  98. fileBuffer[3] === 0x33) {
  99. return false;
  100. }
  101. if (totalBytes >= 5 && fileBuffer.slice(0, 5).toString() === '%PDF-') {
  102. /* PDF. This is binary. */
  103. return true;
  104. }
  105. // UTF-16 BE BOM
  106. if (bytesRead >= 2 && fileBuffer[0] === 0xfe && fileBuffer[1] === 0xff) {
  107. return false;
  108. }
  109. // UTF-16 LE BOM
  110. if (bytesRead >= 2 && fileBuffer[0] === 0xff && fileBuffer[1] === 0xfe) {
  111. return false;
  112. }
  113. for (let i = 0; i < totalBytes; i++) {
  114. if (fileBuffer[i] === 0) {
  115. // NULL byte--it's binary!
  116. return true;
  117. }
  118. else if ((fileBuffer[i] < 7 || fileBuffer[i] > 14) && (fileBuffer[i] < 32 || fileBuffer[i] > 127)) {
  119. // UTF-8 detection
  120. if (fileBuffer[i] > 193 && fileBuffer[i] < 224 && i + 1 < totalBytes) {
  121. i++;
  122. if (fileBuffer[i] > 127 && fileBuffer[i] < 192) {
  123. continue;
  124. }
  125. }
  126. else if (fileBuffer[i] > 223 && fileBuffer[i] < 240 && i + 2 < totalBytes) {
  127. i++;
  128. if (fileBuffer[i] > 127 && fileBuffer[i] < 192 && fileBuffer[i + 1] > 127 && fileBuffer[i + 1] < 192) {
  129. i++;
  130. continue;
  131. }
  132. }
  133. suspiciousBytes++;
  134. // Read at least 32 fileBuffer before making a decision
  135. if (i > 32 && (suspiciousBytes * 100) / totalBytes > 10) {
  136. return true;
  137. }
  138. }
  139. }
  140. if ((suspiciousBytes * 100) / totalBytes > 10) {
  141. return true;
  142. }
  143. return false;
  144. }
  145. function isString(x) {
  146. return typeof x === 'string';
  147. }
  148. function isStatFile(stat) {
  149. if (!stat.isFile()) {
  150. throw new Error(`Path provided was not a file!`);
  151. }
  152. }