index.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. exports.isBinaryFileSync = exports.isBinaryFile = void 0;
  13. const fs = require("fs");
  14. const util_1 = require("util");
  15. const statAsync = util_1.promisify(fs.stat);
  16. const openAsync = util_1.promisify(fs.open);
  17. const closeAsync = util_1.promisify(fs.close);
  18. const MAX_BYTES = 512;
  19. // A very basic non-exception raising reader. Read bytes and
  20. // at the end use hasError() to check whether this worked.
  21. class Reader {
  22. constructor(fileBuffer, size) {
  23. this.fileBuffer = fileBuffer;
  24. this.size = size;
  25. this.offset = 0;
  26. this.error = false;
  27. }
  28. hasError() {
  29. return this.error;
  30. }
  31. nextByte() {
  32. if (this.offset === this.size || this.hasError()) {
  33. this.error = true;
  34. return 0xff;
  35. }
  36. return this.fileBuffer[this.offset++];
  37. }
  38. next(len) {
  39. const n = new Array();
  40. for (let i = 0; i < len; i++) {
  41. n[i] = this.nextByte();
  42. }
  43. return n;
  44. }
  45. }
  46. // Read a Google Protobuf var(iable)int from the buffer.
  47. function readProtoVarInt(reader) {
  48. let idx = 0;
  49. let varInt = 0;
  50. while (!reader.hasError()) {
  51. const b = reader.nextByte();
  52. varInt = varInt | ((b & 0x7f) << (7 * idx));
  53. if ((b & 0x80) === 0) {
  54. break;
  55. }
  56. idx++;
  57. }
  58. return varInt;
  59. }
  60. // Attempt to taste a full Google Protobuf message.
  61. function readProtoMessage(reader) {
  62. const varInt = readProtoVarInt(reader);
  63. const wireType = varInt & 0x7;
  64. switch (wireType) {
  65. case 0:
  66. readProtoVarInt(reader);
  67. return true;
  68. case 1:
  69. reader.next(8);
  70. return true;
  71. case 2:
  72. const len = readProtoVarInt(reader);
  73. reader.next(len);
  74. return true;
  75. case 5:
  76. reader.next(4);
  77. return true;
  78. }
  79. return false;
  80. }
  81. // Check whether this seems to be a valid protobuf file.
  82. function isBinaryProto(fileBuffer, totalBytes) {
  83. const reader = new Reader(fileBuffer, totalBytes);
  84. let numMessages = 0;
  85. while (true) {
  86. // Definitely not a valid protobuf
  87. if (!readProtoMessage(reader) && !reader.hasError()) {
  88. return false;
  89. }
  90. // Short read?
  91. if (reader.hasError()) {
  92. break;
  93. }
  94. numMessages++;
  95. }
  96. return numMessages > 0;
  97. }
  98. function isBinaryFile(file, size) {
  99. return __awaiter(this, void 0, void 0, function* () {
  100. if (isString(file)) {
  101. const stat = yield statAsync(file);
  102. isStatFile(stat);
  103. const fileDescriptor = yield openAsync(file, 'r');
  104. const allocBuffer = Buffer.alloc(MAX_BYTES);
  105. // Read the file with no encoding for raw buffer access.
  106. // NB: something is severely wrong with promisify, had to construct my own Promise
  107. return new Promise((fulfill, reject) => {
  108. fs.read(fileDescriptor, allocBuffer, 0, MAX_BYTES, 0, (err, bytesRead, _) => {
  109. closeAsync(fileDescriptor);
  110. if (err) {
  111. reject(err);
  112. }
  113. else {
  114. fulfill(isBinaryCheck(allocBuffer, bytesRead));
  115. }
  116. });
  117. });
  118. }
  119. else {
  120. if (size === undefined) {
  121. size = file.length;
  122. }
  123. return isBinaryCheck(file, size);
  124. }
  125. });
  126. }
  127. exports.isBinaryFile = isBinaryFile;
  128. function isBinaryFileSync(file, size) {
  129. if (isString(file)) {
  130. const stat = fs.statSync(file);
  131. isStatFile(stat);
  132. const fileDescriptor = fs.openSync(file, 'r');
  133. const allocBuffer = Buffer.alloc(MAX_BYTES);
  134. const bytesRead = fs.readSync(fileDescriptor, allocBuffer, 0, MAX_BYTES, 0);
  135. fs.closeSync(fileDescriptor);
  136. return isBinaryCheck(allocBuffer, bytesRead);
  137. }
  138. else {
  139. if (size === undefined) {
  140. size = file.length;
  141. }
  142. return isBinaryCheck(file, size);
  143. }
  144. }
  145. exports.isBinaryFileSync = isBinaryFileSync;
  146. function isBinaryCheck(fileBuffer, bytesRead) {
  147. // empty file. no clue what it is.
  148. if (bytesRead === 0) {
  149. return false;
  150. }
  151. let suspiciousBytes = 0;
  152. const totalBytes = Math.min(bytesRead, MAX_BYTES);
  153. // UTF-8 BOM
  154. if (bytesRead >= 3 && fileBuffer[0] === 0xef && fileBuffer[1] === 0xbb && fileBuffer[2] === 0xbf) {
  155. return false;
  156. }
  157. // UTF-32 BOM
  158. if (bytesRead >= 4 &&
  159. fileBuffer[0] === 0x00 &&
  160. fileBuffer[1] === 0x00 &&
  161. fileBuffer[2] === 0xfe &&
  162. fileBuffer[3] === 0xff) {
  163. return false;
  164. }
  165. // UTF-32 LE BOM
  166. if (bytesRead >= 4 &&
  167. fileBuffer[0] === 0xff &&
  168. fileBuffer[1] === 0xfe &&
  169. fileBuffer[2] === 0x00 &&
  170. fileBuffer[3] === 0x00) {
  171. return false;
  172. }
  173. // GB BOM
  174. if (bytesRead >= 4 &&
  175. fileBuffer[0] === 0x84 &&
  176. fileBuffer[1] === 0x31 &&
  177. fileBuffer[2] === 0x95 &&
  178. fileBuffer[3] === 0x33) {
  179. return false;
  180. }
  181. if (totalBytes >= 5 && fileBuffer.slice(0, 5).toString() === '%PDF-') {
  182. /* PDF. This is binary. */
  183. return true;
  184. }
  185. // UTF-16 BE BOM
  186. if (bytesRead >= 2 && fileBuffer[0] === 0xfe && fileBuffer[1] === 0xff) {
  187. return false;
  188. }
  189. // UTF-16 LE BOM
  190. if (bytesRead >= 2 && fileBuffer[0] === 0xff && fileBuffer[1] === 0xfe) {
  191. return false;
  192. }
  193. for (let i = 0; i < totalBytes; i++) {
  194. if (fileBuffer[i] === 0) {
  195. // NULL byte--it's binary!
  196. return true;
  197. }
  198. else if ((fileBuffer[i] < 7 || fileBuffer[i] > 14) && (fileBuffer[i] < 32 || fileBuffer[i] > 127)) {
  199. // UTF-8 detection
  200. if (fileBuffer[i] > 193 && fileBuffer[i] < 224 && i + 1 < totalBytes) {
  201. i++;
  202. if (fileBuffer[i] > 127 && fileBuffer[i] < 192) {
  203. continue;
  204. }
  205. }
  206. else if (fileBuffer[i] > 223 && fileBuffer[i] < 240 && i + 2 < totalBytes) {
  207. i++;
  208. if (fileBuffer[i] > 127 && fileBuffer[i] < 192 && fileBuffer[i + 1] > 127 && fileBuffer[i + 1] < 192) {
  209. i++;
  210. continue;
  211. }
  212. }
  213. suspiciousBytes++;
  214. // Read at least 32 fileBuffer before making a decision
  215. if (i >= 32 && (suspiciousBytes * 100) / totalBytes > 10) {
  216. return true;
  217. }
  218. }
  219. }
  220. if ((suspiciousBytes * 100) / totalBytes > 10) {
  221. return true;
  222. }
  223. if (suspiciousBytes > 1 && isBinaryProto(fileBuffer, totalBytes)) {
  224. return true;
  225. }
  226. return false;
  227. }
  228. function isString(x) {
  229. return typeof x === 'string';
  230. }
  231. function isStatFile(stat) {
  232. if (!stat.isFile()) {
  233. throw new Error(`Path provided was not a file!`);
  234. }
  235. }