You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953
  1. 'use strict';
  2. const toBytes = s => [...s].map(c => c.charCodeAt(0));
  3. const xpiZipFilename = toBytes('META-INF/mozilla.rsa');
  4. const oxmlContentTypes = toBytes('[Content_Types].xml');
  5. const oxmlRels = toBytes('_rels/.rels');
  6. function readUInt64LE(buf, offset = 0) {
  7. let n = buf[offset];
  8. let mul = 1;
  9. let i = 0;
  10. while (++i < 8) {
  11. mul *= 0x100;
  12. n += buf[offset + i] * mul;
  13. }
  14. return n;
  15. }
  16. const fileType = input => {
  17. if (!(input instanceof Uint8Array || input instanceof ArrayBuffer || Buffer.isBuffer(input))) {
  18. throw new TypeError(`Expected the \`input\` argument to be of type \`Uint8Array\` or \`Buffer\` or \`ArrayBuffer\`, got \`${typeof input}\``);
  19. }
  20. const buf = input instanceof Uint8Array ? input : new Uint8Array(input);
  21. if (!(buf && buf.length > 1)) {
  22. return null;
  23. }
  24. const check = (header, options) => {
  25. options = Object.assign({
  26. offset: 0
  27. }, options);
  28. for (let i = 0; i < header.length; i++) {
  29. // If a bitmask is set
  30. if (options.mask) {
  31. // If header doesn't equal `buf` with bits masked off
  32. if (header[i] !== (options.mask[i] & buf[i + options.offset])) {
  33. return false;
  34. }
  35. } else if (header[i] !== buf[i + options.offset]) {
  36. return false;
  37. }
  38. }
  39. return true;
  40. };
  41. const checkString = (header, options) => check(toBytes(header), options);
  42. if (check([0xFF, 0xD8, 0xFF])) {
  43. return {
  44. ext: 'jpg',
  45. mime: 'image/jpeg'
  46. };
  47. }
  48. if (check([0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A])) {
  49. return {
  50. ext: 'png',
  51. mime: 'image/png'
  52. };
  53. }
  54. if (check([0x47, 0x49, 0x46])) {
  55. return {
  56. ext: 'gif',
  57. mime: 'image/gif'
  58. };
  59. }
  60. if (check([0x57, 0x45, 0x42, 0x50], {offset: 8})) {
  61. return {
  62. ext: 'webp',
  63. mime: 'image/webp'
  64. };
  65. }
  66. if (check([0x46, 0x4C, 0x49, 0x46])) {
  67. return {
  68. ext: 'flif',
  69. mime: 'image/flif'
  70. };
  71. }
  72. // Needs to be before `tif` check
  73. if (
  74. (check([0x49, 0x49, 0x2A, 0x0]) || check([0x4D, 0x4D, 0x0, 0x2A])) &&
  75. check([0x43, 0x52], {offset: 8})
  76. ) {
  77. return {
  78. ext: 'cr2',
  79. mime: 'image/x-canon-cr2'
  80. };
  81. }
  82. if (
  83. check([0x49, 0x49, 0x2A, 0x0]) ||
  84. check([0x4D, 0x4D, 0x0, 0x2A])
  85. ) {
  86. return {
  87. ext: 'tif',
  88. mime: 'image/tiff'
  89. };
  90. }
  91. if (check([0x42, 0x4D])) {
  92. return {
  93. ext: 'bmp',
  94. mime: 'image/bmp'
  95. };
  96. }
  97. if (check([0x49, 0x49, 0xBC])) {
  98. return {
  99. ext: 'jxr',
  100. mime: 'image/vnd.ms-photo'
  101. };
  102. }
  103. if (check([0x38, 0x42, 0x50, 0x53])) {
  104. return {
  105. ext: 'psd',
  106. mime: 'image/vnd.adobe.photoshop'
  107. };
  108. }
  109. // Zip-based file formats
  110. // Need to be before the `zip` check
  111. if (check([0x50, 0x4B, 0x3, 0x4])) {
  112. if (
  113. check([0x6D, 0x69, 0x6D, 0x65, 0x74, 0x79, 0x70, 0x65, 0x61, 0x70, 0x70, 0x6C, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6F, 0x6E, 0x2F, 0x65, 0x70, 0x75, 0x62, 0x2B, 0x7A, 0x69, 0x70], {offset: 30})
  114. ) {
  115. return {
  116. ext: 'epub',
  117. mime: 'application/epub+zip'
  118. };
  119. }
  120. // Assumes signed `.xpi` from addons.mozilla.org
  121. if (check(xpiZipFilename, {offset: 30})) {
  122. return {
  123. ext: 'xpi',
  124. mime: 'application/x-xpinstall'
  125. };
  126. }
  127. if (checkString('mimetypeapplication/vnd.oasis.opendocument.text', {offset: 30})) {
  128. return {
  129. ext: 'odt',
  130. mime: 'application/vnd.oasis.opendocument.text'
  131. };
  132. }
  133. if (checkString('mimetypeapplication/vnd.oasis.opendocument.spreadsheet', {offset: 30})) {
  134. return {
  135. ext: 'ods',
  136. mime: 'application/vnd.oasis.opendocument.spreadsheet'
  137. };
  138. }
  139. if (checkString('mimetypeapplication/vnd.oasis.opendocument.presentation', {offset: 30})) {
  140. return {
  141. ext: 'odp',
  142. mime: 'application/vnd.oasis.opendocument.presentation'
  143. };
  144. }
  145. // The docx, xlsx and pptx file types extend the Office Open XML file format:
  146. // https://en.wikipedia.org/wiki/Office_Open_XML_file_formats
  147. // We look for:
  148. // - one entry named '[Content_Types].xml' or '_rels/.rels',
  149. // - one entry indicating specific type of file.
  150. // MS Office, OpenOffice and LibreOffice may put the parts in different order, so the check should not rely on it.
  151. const findNextZipHeaderIndex = (arr, startAt = 0) => arr.findIndex((el, i, arr) => i >= startAt && arr[i] === 0x50 && arr[i + 1] === 0x4B && arr[i + 2] === 0x3 && arr[i + 3] === 0x4);
  152. let zipHeaderIndex = 0; // The first zip header was already found at index 0
  153. let oxmlFound = false;
  154. let type = null;
  155. do {
  156. const offset = zipHeaderIndex + 30;
  157. if (!oxmlFound) {
  158. oxmlFound = (check(oxmlContentTypes, {offset}) || check(oxmlRels, {offset}));
  159. }
  160. if (!type) {
  161. if (checkString('word/', {offset})) {
  162. type = {
  163. ext: 'docx',
  164. mime: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
  165. };
  166. } else if (checkString('ppt/', {offset})) {
  167. type = {
  168. ext: 'pptx',
  169. mime: 'application/vnd.openxmlformats-officedocument.presentationml.presentation'
  170. };
  171. } else if (checkString('xl/', {offset})) {
  172. type = {
  173. ext: 'xlsx',
  174. mime: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
  175. };
  176. }
  177. }
  178. if (oxmlFound && type) {
  179. return type;
  180. }
  181. zipHeaderIndex = findNextZipHeaderIndex(buf, offset);
  182. } while (zipHeaderIndex >= 0);
  183. // No more zip parts available in the buffer, but maybe we are almost certain about the type?
  184. if (type) {
  185. return type;
  186. }
  187. }
  188. if (
  189. check([0x50, 0x4B]) &&
  190. (buf[2] === 0x3 || buf[2] === 0x5 || buf[2] === 0x7) &&
  191. (buf[3] === 0x4 || buf[3] === 0x6 || buf[3] === 0x8)
  192. ) {
  193. return {
  194. ext: 'zip',
  195. mime: 'application/zip'
  196. };
  197. }
  198. if (check([0x75, 0x73, 0x74, 0x61, 0x72], {offset: 257})) {
  199. return {
  200. ext: 'tar',
  201. mime: 'application/x-tar'
  202. };
  203. }
  204. if (
  205. check([0x52, 0x61, 0x72, 0x21, 0x1A, 0x7]) &&
  206. (buf[6] === 0x0 || buf[6] === 0x1)
  207. ) {
  208. return {
  209. ext: 'rar',
  210. mime: 'application/x-rar-compressed'
  211. };
  212. }
  213. if (check([0x1F, 0x8B, 0x8])) {
  214. return {
  215. ext: 'gz',
  216. mime: 'application/gzip'
  217. };
  218. }
  219. if (check([0x42, 0x5A, 0x68])) {
  220. return {
  221. ext: 'bz2',
  222. mime: 'application/x-bzip2'
  223. };
  224. }
  225. if (check([0x37, 0x7A, 0xBC, 0xAF, 0x27, 0x1C])) {
  226. return {
  227. ext: '7z',
  228. mime: 'application/x-7z-compressed'
  229. };
  230. }
  231. if (check([0x78, 0x01])) {
  232. return {
  233. ext: 'dmg',
  234. mime: 'application/x-apple-diskimage'
  235. };
  236. }
  237. if (check([0x33, 0x67, 0x70, 0x35]) || // 3gp5
  238. (
  239. check([0x0, 0x0, 0x0]) && check([0x66, 0x74, 0x79, 0x70], {offset: 4}) &&
  240. (
  241. check([0x6D, 0x70, 0x34, 0x31], {offset: 8}) || // MP41
  242. check([0x6D, 0x70, 0x34, 0x32], {offset: 8}) || // MP42
  243. check([0x69, 0x73, 0x6F, 0x6D], {offset: 8}) || // ISOM
  244. check([0x69, 0x73, 0x6F, 0x32], {offset: 8}) || // ISO2
  245. check([0x6D, 0x6D, 0x70, 0x34], {offset: 8}) || // MMP4
  246. check([0x4D, 0x34, 0x56], {offset: 8}) || // M4V
  247. check([0x64, 0x61, 0x73, 0x68], {offset: 8}) // DASH
  248. )
  249. )) {
  250. return {
  251. ext: 'mp4',
  252. mime: 'video/mp4'
  253. };
  254. }
  255. if (check([0x4D, 0x54, 0x68, 0x64])) {
  256. return {
  257. ext: 'mid',
  258. mime: 'audio/midi'
  259. };
  260. }
  261. // https://github.com/threatstack/libmagic/blob/master/magic/Magdir/matroska
  262. if (check([0x1A, 0x45, 0xDF, 0xA3])) {
  263. const sliced = buf.subarray(4, 4 + 4096);
  264. const idPos = sliced.findIndex((el, i, arr) => arr[i] === 0x42 && arr[i + 1] === 0x82);
  265. if (idPos !== -1) {
  266. const docTypePos = idPos + 3;
  267. const findDocType = type => [...type].every((c, i) => sliced[docTypePos + i] === c.charCodeAt(0));
  268. if (findDocType('matroska')) {
  269. return {
  270. ext: 'mkv',
  271. mime: 'video/x-matroska'
  272. };
  273. }
  274. if (findDocType('webm')) {
  275. return {
  276. ext: 'webm',
  277. mime: 'video/webm'
  278. };
  279. }
  280. }
  281. }
  282. if (check([0x0, 0x0, 0x0, 0x14, 0x66, 0x74, 0x79, 0x70, 0x71, 0x74, 0x20, 0x20]) ||
  283. check([0x66, 0x72, 0x65, 0x65], {offset: 4}) || // Type: `free`
  284. check([0x66, 0x74, 0x79, 0x70, 0x71, 0x74, 0x20, 0x20], {offset: 4}) ||
  285. check([0x6D, 0x64, 0x61, 0x74], {offset: 4}) || // MJPEG
  286. check([0x6D, 0x6F, 0x6F, 0x76], {offset: 4}) || // Type: `moov`
  287. check([0x77, 0x69, 0x64, 0x65], {offset: 4})) {
  288. return {
  289. ext: 'mov',
  290. mime: 'video/quicktime'
  291. };
  292. }
  293. // RIFF file format which might be AVI, WAV, QCP, etc
  294. if (check([0x52, 0x49, 0x46, 0x46])) {
  295. if (check([0x41, 0x56, 0x49], {offset: 8})) {
  296. return {
  297. ext: 'avi',
  298. mime: 'video/vnd.avi'
  299. };
  300. }
  301. if (check([0x57, 0x41, 0x56, 0x45], {offset: 8})) {
  302. return {
  303. ext: 'wav',
  304. mime: 'audio/vnd.wave'
  305. };
  306. }
  307. // QLCM, QCP file
  308. if (check([0x51, 0x4C, 0x43, 0x4D], {offset: 8})) {
  309. return {
  310. ext: 'qcp',
  311. mime: 'audio/qcelp'
  312. };
  313. }
  314. }
  315. // ASF_Header_Object first 80 bytes
  316. if (check([0x30, 0x26, 0xB2, 0x75, 0x8E, 0x66, 0xCF, 0x11, 0xA6, 0xD9])) {
  317. // Search for header should be in first 1KB of file.
  318. let offset = 30;
  319. do {
  320. const objectSize = readUInt64LE(buf, offset + 16);
  321. if (check([0x91, 0x07, 0xDC, 0xB7, 0xB7, 0xA9, 0xCF, 0x11, 0x8E, 0xE6, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65], {offset})) {
  322. // Sync on Stream-Properties-Object (B7DC0791-A9B7-11CF-8EE6-00C00C205365)
  323. if (check([0x40, 0x9E, 0x69, 0xF8, 0x4D, 0x5B, 0xCF, 0x11, 0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B], {offset: offset + 24})) {
  324. // Found audio:
  325. return {
  326. ext: 'wma',
  327. mime: 'audio/x-ms-wma'
  328. };
  329. }
  330. if (check([0xC0, 0xEF, 0x19, 0xBC, 0x4D, 0x5B, 0xCF, 0x11, 0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B], {offset: offset + 24})) {
  331. // Found video:
  332. return {
  333. ext: 'wmv',
  334. mime: 'video/x-ms-asf'
  335. };
  336. }
  337. break;
  338. }
  339. offset += objectSize;
  340. } while (offset + 24 <= buf.length);
  341. // Default to ASF generic extension
  342. return {
  343. ext: 'asf',
  344. mime: 'application/vnd.ms-asf'
  345. };
  346. }
  347. if (
  348. check([0x0, 0x0, 0x1, 0xBA]) ||
  349. check([0x0, 0x0, 0x1, 0xB3])
  350. ) {
  351. return {
  352. ext: 'mpg',
  353. mime: 'video/mpeg'
  354. };
  355. }
  356. if (check([0x66, 0x74, 0x79, 0x70, 0x33, 0x67], {offset: 4})) {
  357. return {
  358. ext: '3gp',
  359. mime: 'video/3gpp'
  360. };
  361. }
  362. // Check for MPEG header at different starting offsets
  363. for (let start = 0; start < 2 && start < (buf.length - 16); start++) {
  364. if (
  365. check([0x49, 0x44, 0x33], {offset: start}) || // ID3 header
  366. check([0xFF, 0xE2], {offset: start, mask: [0xFF, 0xE2]}) // MPEG 1 or 2 Layer 3 header
  367. ) {
  368. return {
  369. ext: 'mp3',
  370. mime: 'audio/mpeg'
  371. };
  372. }
  373. if (
  374. check([0xFF, 0xE4], {offset: start, mask: [0xFF, 0xE4]}) // MPEG 1 or 2 Layer 2 header
  375. ) {
  376. return {
  377. ext: 'mp2',
  378. mime: 'audio/mpeg'
  379. };
  380. }
  381. if (
  382. check([0xFF, 0xF8], {offset: start, mask: [0xFF, 0xFC]}) // MPEG 2 layer 0 using ADTS
  383. ) {
  384. return {
  385. ext: 'mp2',
  386. mime: 'audio/mpeg'
  387. };
  388. }
  389. if (
  390. check([0xFF, 0xF0], {offset: start, mask: [0xFF, 0xFC]}) // MPEG 4 layer 0 using ADTS
  391. ) {
  392. return {
  393. ext: 'mp4',
  394. mime: 'audio/mpeg'
  395. };
  396. }
  397. }
  398. if (
  399. check([0x66, 0x74, 0x79, 0x70, 0x4D, 0x34, 0x41], {offset: 4})
  400. ) {
  401. return { // MPEG-4 layer 3 (audio)
  402. ext: 'm4a',
  403. mime: 'audio/mp4' // RFC 4337
  404. };
  405. }
  406. // Needs to be before `ogg` check
  407. if (check([0x4F, 0x70, 0x75, 0x73, 0x48, 0x65, 0x61, 0x64], {offset: 28})) {
  408. return {
  409. ext: 'opus',
  410. mime: 'audio/opus'
  411. };
  412. }
  413. // If 'OggS' in first bytes, then OGG container
  414. if (check([0x4F, 0x67, 0x67, 0x53])) {
  415. // This is a OGG container
  416. // If ' theora' in header.
  417. if (check([0x80, 0x74, 0x68, 0x65, 0x6F, 0x72, 0x61], {offset: 28})) {
  418. return {
  419. ext: 'ogv',
  420. mime: 'video/ogg'
  421. };
  422. }
  423. // If '\x01video' in header.
  424. if (check([0x01, 0x76, 0x69, 0x64, 0x65, 0x6F, 0x00], {offset: 28})) {
  425. return {
  426. ext: 'ogm',
  427. mime: 'video/ogg'
  428. };
  429. }
  430. // If ' FLAC' in header https://xiph.org/flac/faq.html
  431. if (check([0x7F, 0x46, 0x4C, 0x41, 0x43], {offset: 28})) {
  432. return {
  433. ext: 'oga',
  434. mime: 'audio/ogg'
  435. };
  436. }
  437. // 'Speex ' in header https://en.wikipedia.org/wiki/Speex
  438. if (check([0x53, 0x70, 0x65, 0x65, 0x78, 0x20, 0x20], {offset: 28})) {
  439. return {
  440. ext: 'spx',
  441. mime: 'audio/ogg'
  442. };
  443. }
  444. // If '\x01vorbis' in header
  445. if (check([0x01, 0x76, 0x6F, 0x72, 0x62, 0x69, 0x73], {offset: 28})) {
  446. return {
  447. ext: 'ogg',
  448. mime: 'audio/ogg'
  449. };
  450. }
  451. // Default OGG container https://www.iana.org/assignments/media-types/application/ogg
  452. return {
  453. ext: 'ogx',
  454. mime: 'application/ogg'
  455. };
  456. }
  457. if (check([0x66, 0x4C, 0x61, 0x43])) {
  458. return {
  459. ext: 'flac',
  460. mime: 'audio/x-flac'
  461. };
  462. }
  463. if (check([0x4D, 0x41, 0x43, 0x20])) { // 'MAC '
  464. return {
  465. ext: 'ape',
  466. mime: 'audio/ape'
  467. };
  468. }
  469. if (check([0x77, 0x76, 0x70, 0x6B])) { // 'wvpk'
  470. return {
  471. ext: 'wv',
  472. mime: 'audio/wavpack'
  473. };
  474. }
  475. if (check([0x23, 0x21, 0x41, 0x4D, 0x52, 0x0A])) {
  476. return {
  477. ext: 'amr',
  478. mime: 'audio/amr'
  479. };
  480. }
  481. if (check([0x25, 0x50, 0x44, 0x46])) {
  482. return {
  483. ext: 'pdf',
  484. mime: 'application/pdf'
  485. };
  486. }
  487. if (check([0x4D, 0x5A])) {
  488. return {
  489. ext: 'exe',
  490. mime: 'application/x-msdownload'
  491. };
  492. }
  493. if (
  494. (buf[0] === 0x43 || buf[0] === 0x46) &&
  495. check([0x57, 0x53], {offset: 1})
  496. ) {
  497. return {
  498. ext: 'swf',
  499. mime: 'application/x-shockwave-flash'
  500. };
  501. }
  502. if (check([0x7B, 0x5C, 0x72, 0x74, 0x66])) {
  503. return {
  504. ext: 'rtf',
  505. mime: 'application/rtf'
  506. };
  507. }
  508. if (check([0x00, 0x61, 0x73, 0x6D])) {
  509. return {
  510. ext: 'wasm',
  511. mime: 'application/wasm'
  512. };
  513. }
  514. if (
  515. check([0x77, 0x4F, 0x46, 0x46]) &&
  516. (
  517. check([0x00, 0x01, 0x00, 0x00], {offset: 4}) ||
  518. check([0x4F, 0x54, 0x54, 0x4F], {offset: 4})
  519. )
  520. ) {
  521. return {
  522. ext: 'woff',
  523. mime: 'font/woff'
  524. };
  525. }
  526. if (
  527. check([0x77, 0x4F, 0x46, 0x32]) &&
  528. (
  529. check([0x00, 0x01, 0x00, 0x00], {offset: 4}) ||
  530. check([0x4F, 0x54, 0x54, 0x4F], {offset: 4})
  531. )
  532. ) {
  533. return {
  534. ext: 'woff2',
  535. mime: 'font/woff2'
  536. };
  537. }
  538. if (
  539. check([0x4C, 0x50], {offset: 34}) &&
  540. (
  541. check([0x00, 0x00, 0x01], {offset: 8}) ||
  542. check([0x01, 0x00, 0x02], {offset: 8}) ||
  543. check([0x02, 0x00, 0x02], {offset: 8})
  544. )
  545. ) {
  546. return {
  547. ext: 'eot',
  548. mime: 'application/vnd.ms-fontobject'
  549. };
  550. }
  551. if (check([0x00, 0x01, 0x00, 0x00, 0x00])) {
  552. return {
  553. ext: 'ttf',
  554. mime: 'font/ttf'
  555. };
  556. }
  557. if (check([0x4F, 0x54, 0x54, 0x4F, 0x00])) {
  558. return {
  559. ext: 'otf',
  560. mime: 'font/otf'
  561. };
  562. }
  563. if (check([0x00, 0x00, 0x01, 0x00])) {
  564. return {
  565. ext: 'ico',
  566. mime: 'image/x-icon'
  567. };
  568. }
  569. if (check([0x00, 0x00, 0x02, 0x00])) {
  570. return {
  571. ext: 'cur',
  572. mime: 'image/x-icon'
  573. };
  574. }
  575. if (check([0x46, 0x4C, 0x56, 0x01])) {
  576. return {
  577. ext: 'flv',
  578. mime: 'video/x-flv'
  579. };
  580. }
  581. if (check([0x25, 0x21])) {
  582. return {
  583. ext: 'ps',
  584. mime: 'application/postscript'
  585. };
  586. }
  587. if (check([0xFD, 0x37, 0x7A, 0x58, 0x5A, 0x00])) {
  588. return {
  589. ext: 'xz',
  590. mime: 'application/x-xz'
  591. };
  592. }
  593. if (check([0x53, 0x51, 0x4C, 0x69])) {
  594. return {
  595. ext: 'sqlite',
  596. mime: 'application/x-sqlite3'
  597. };
  598. }
  599. if (check([0x4E, 0x45, 0x53, 0x1A])) {
  600. return {
  601. ext: 'nes',
  602. mime: 'application/x-nintendo-nes-rom'
  603. };
  604. }
  605. if (check([0x43, 0x72, 0x32, 0x34])) {
  606. return {
  607. ext: 'crx',
  608. mime: 'application/x-google-chrome-extension'
  609. };
  610. }
  611. if (
  612. check([0x4D, 0x53, 0x43, 0x46]) ||
  613. check([0x49, 0x53, 0x63, 0x28])
  614. ) {
  615. return {
  616. ext: 'cab',
  617. mime: 'application/vnd.ms-cab-compressed'
  618. };
  619. }
  620. // Needs to be before `ar` check
  621. if (check([0x21, 0x3C, 0x61, 0x72, 0x63, 0x68, 0x3E, 0x0A, 0x64, 0x65, 0x62, 0x69, 0x61, 0x6E, 0x2D, 0x62, 0x69, 0x6E, 0x61, 0x72, 0x79])) {
  622. return {
  623. ext: 'deb',
  624. mime: 'application/x-deb'
  625. };
  626. }
  627. if (check([0x21, 0x3C, 0x61, 0x72, 0x63, 0x68, 0x3E])) {
  628. return {
  629. ext: 'ar',
  630. mime: 'application/x-unix-archive'
  631. };
  632. }
  633. if (check([0xED, 0xAB, 0xEE, 0xDB])) {
  634. return {
  635. ext: 'rpm',
  636. mime: 'application/x-rpm'
  637. };
  638. }
  639. if (
  640. check([0x1F, 0xA0]) ||
  641. check([0x1F, 0x9D])
  642. ) {
  643. return {
  644. ext: 'Z',
  645. mime: 'application/x-compress'
  646. };
  647. }
  648. if (check([0x4C, 0x5A, 0x49, 0x50])) {
  649. return {
  650. ext: 'lz',
  651. mime: 'application/x-lzip'
  652. };
  653. }
  654. if (check([0xD0, 0xCF, 0x11, 0xE0, 0xA1, 0xB1, 0x1A, 0xE1])) {
  655. return {
  656. ext: 'msi',
  657. mime: 'application/x-msi'
  658. };
  659. }
  660. if (check([0x06, 0x0E, 0x2B, 0x34, 0x02, 0x05, 0x01, 0x01, 0x0D, 0x01, 0x02, 0x01, 0x01, 0x02])) {
  661. return {
  662. ext: 'mxf',
  663. mime: 'application/mxf'
  664. };
  665. }
  666. if (check([0x47], {offset: 4}) && (check([0x47], {offset: 192}) || check([0x47], {offset: 196}))) {
  667. return {
  668. ext: 'mts',
  669. mime: 'video/mp2t'
  670. };
  671. }
  672. if (check([0x42, 0x4C, 0x45, 0x4E, 0x44, 0x45, 0x52])) {
  673. return {
  674. ext: 'blend',
  675. mime: 'application/x-blender'
  676. };
  677. }
  678. if (check([0x42, 0x50, 0x47, 0xFB])) {
  679. return {
  680. ext: 'bpg',
  681. mime: 'image/bpg'
  682. };
  683. }
  684. if (check([0x00, 0x00, 0x00, 0x0C, 0x6A, 0x50, 0x20, 0x20, 0x0D, 0x0A, 0x87, 0x0A])) {
  685. // JPEG-2000 family
  686. if (check([0x6A, 0x70, 0x32, 0x20], {offset: 20})) {
  687. return {
  688. ext: 'jp2',
  689. mime: 'image/jp2'
  690. };
  691. }
  692. if (check([0x6A, 0x70, 0x78, 0x20], {offset: 20})) {
  693. return {
  694. ext: 'jpx',
  695. mime: 'image/jpx'
  696. };
  697. }
  698. if (check([0x6A, 0x70, 0x6D, 0x20], {offset: 20})) {
  699. return {
  700. ext: 'jpm',
  701. mime: 'image/jpm'
  702. };
  703. }
  704. if (check([0x6D, 0x6A, 0x70, 0x32], {offset: 20})) {
  705. return {
  706. ext: 'mj2',
  707. mime: 'image/mj2'
  708. };
  709. }
  710. }
  711. if (check([0x46, 0x4F, 0x52, 0x4D])) {
  712. return {
  713. ext: 'aif',
  714. mime: 'audio/aiff'
  715. };
  716. }
  717. if (checkString('<?xml ')) {
  718. return {
  719. ext: 'xml',
  720. mime: 'application/xml'
  721. };
  722. }
  723. if (check([0x42, 0x4F, 0x4F, 0x4B, 0x4D, 0x4F, 0x42, 0x49], {offset: 60})) {
  724. return {
  725. ext: 'mobi',
  726. mime: 'application/x-mobipocket-ebook'
  727. };
  728. }
  729. // File Type Box (https://en.wikipedia.org/wiki/ISO_base_media_file_format)
  730. if (check([0x66, 0x74, 0x79, 0x70], {offset: 4})) {
  731. if (check([0x6D, 0x69, 0x66, 0x31], {offset: 8})) {
  732. return {
  733. ext: 'heic',
  734. mime: 'image/heif'
  735. };
  736. }
  737. if (check([0x6D, 0x73, 0x66, 0x31], {offset: 8})) {
  738. return {
  739. ext: 'heic',
  740. mime: 'image/heif-sequence'
  741. };
  742. }
  743. if (check([0x68, 0x65, 0x69, 0x63], {offset: 8}) || check([0x68, 0x65, 0x69, 0x78], {offset: 8})) {
  744. return {
  745. ext: 'heic',
  746. mime: 'image/heic'
  747. };
  748. }
  749. if (check([0x68, 0x65, 0x76, 0x63], {offset: 8}) || check([0x68, 0x65, 0x76, 0x78], {offset: 8})) {
  750. return {
  751. ext: 'heic',
  752. mime: 'image/heic-sequence'
  753. };
  754. }
  755. }
  756. if (check([0xAB, 0x4B, 0x54, 0x58, 0x20, 0x31, 0x31, 0xBB, 0x0D, 0x0A, 0x1A, 0x0A])) {
  757. return {
  758. ext: 'ktx',
  759. mime: 'image/ktx'
  760. };
  761. }
  762. if (check([0x44, 0x49, 0x43, 0x4D], {offset: 128})) {
  763. return {
  764. ext: 'dcm',
  765. mime: 'application/dicom'
  766. };
  767. }
  768. // Musepack, SV7
  769. if (check([0x4D, 0x50, 0x2B])) {
  770. return {
  771. ext: 'mpc',
  772. mime: 'audio/x-musepack'
  773. };
  774. }
  775. // Musepack, SV8
  776. if (check([0x4D, 0x50, 0x43, 0x4B])) {
  777. return {
  778. ext: 'mpc',
  779. mime: 'audio/x-musepack'
  780. };
  781. }
  782. if (check([0x42, 0x45, 0x47, 0x49, 0x4E, 0x3A])) {
  783. return {
  784. ext: 'ics',
  785. mime: 'text/calendar'
  786. };
  787. }
  788. if (check([0x67, 0x6C, 0x54, 0x46, 0x02, 0x00, 0x00, 0x00])) {
  789. return {
  790. ext: 'glb',
  791. mime: 'model/gltf-binary'
  792. };
  793. }
  794. if (check([0xD4, 0xC3, 0xB2, 0xA1]) || check([0xA1, 0xB2, 0xC3, 0xD4])) {
  795. return {
  796. ext: 'pcap',
  797. mime: 'application/vnd.tcpdump.pcap'
  798. };
  799. }
  800. return null;
  801. };
  802. module.exports = fileType;
  803. // TODO: Remove this for the next major release
  804. module.exports.default = fileType;
  805. Object.defineProperty(fileType, 'minimumBytes', {value: 4100});
  806. module.exports.stream = readableStream => new Promise((resolve, reject) => {
  807. // Using `eval` to work around issues when bundling with Webpack
  808. const stream = eval('require')('stream'); // eslint-disable-line no-eval
  809. readableStream.once('readable', () => {
  810. const pass = new stream.PassThrough();
  811. const chunk = readableStream.read(module.exports.minimumBytes) || readableStream.read();
  812. try {
  813. pass.fileType = fileType(chunk);
  814. } catch (error) {
  815. reject(error);
  816. }
  817. readableStream.unshift(chunk);
  818. if (stream.pipeline) {
  819. resolve(stream.pipeline(readableStream, pass, () => {}));
  820. } else {
  821. resolve(readableStream.pipe(pass));
  822. }
  823. });
  824. });