index.d.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /// <reference types="node"/>
  2. import {Readable as ReadableStream} from 'stream';
  3. declare namespace fileType {
  4. type FileType =
  5. | 'jpg'
  6. | 'png'
  7. | 'apng'
  8. | 'gif'
  9. | 'webp'
  10. | 'flif'
  11. | 'cr2'
  12. | 'orf'
  13. | 'arw'
  14. | 'dng'
  15. | 'nef'
  16. | 'rw2'
  17. | 'raf'
  18. | 'tif'
  19. | 'bmp'
  20. | 'jxr'
  21. | 'psd'
  22. | 'zip'
  23. | 'tar'
  24. | 'rar'
  25. | 'gz'
  26. | 'bz2'
  27. | '7z'
  28. | 'dmg'
  29. | 'mp4'
  30. | 'mid'
  31. | 'mkv'
  32. | 'webm'
  33. | 'mov'
  34. | 'avi'
  35. | 'wmv'
  36. | 'mpg'
  37. | 'mp2'
  38. | 'mp3'
  39. | 'm4a'
  40. | 'ogg'
  41. | 'opus'
  42. | 'flac'
  43. | 'wav'
  44. | 'qcp'
  45. | 'amr'
  46. | 'pdf'
  47. | 'epub'
  48. | 'mobi'
  49. | 'exe'
  50. | 'swf'
  51. | 'rtf'
  52. | 'woff'
  53. | 'woff2'
  54. | 'eot'
  55. | 'ttf'
  56. | 'otf'
  57. | 'ico'
  58. | 'flv'
  59. | 'ps'
  60. | 'xz'
  61. | 'sqlite'
  62. | 'nes'
  63. | 'crx'
  64. | 'xpi'
  65. | 'cab'
  66. | 'deb'
  67. | 'ar'
  68. | 'rpm'
  69. | 'Z'
  70. | 'lz'
  71. | 'msi'
  72. | 'mxf'
  73. | 'mts'
  74. | 'wasm'
  75. | 'blend'
  76. | 'bpg'
  77. | 'docx'
  78. | 'pptx'
  79. | 'xlsx'
  80. | '3gp'
  81. | '3g2'
  82. | 'jp2'
  83. | 'jpm'
  84. | 'jpx'
  85. | 'mj2'
  86. | 'aif'
  87. | 'odt'
  88. | 'ods'
  89. | 'odp'
  90. | 'xml'
  91. | 'heic'
  92. | 'cur'
  93. | 'ktx'
  94. | 'ape'
  95. | 'wv'
  96. | 'asf'
  97. | 'wma'
  98. | 'dcm'
  99. | 'mpc'
  100. | 'ics'
  101. | 'glb'
  102. | 'pcap'
  103. | 'dsf'
  104. | 'lnk'
  105. | 'alias'
  106. | 'voc'
  107. | 'ac3'
  108. | 'm4b'
  109. | 'm4p'
  110. | 'm4v'
  111. | 'f4a'
  112. | 'f4b'
  113. | 'f4p'
  114. | 'f4v'
  115. | 'mie'
  116. | 'ogv'
  117. | 'ogm'
  118. | 'oga'
  119. | 'spx'
  120. | 'ogx'
  121. | 'arrow'
  122. | 'shp';
  123. type MimeType =
  124. | 'image/jpeg'
  125. | 'image/png'
  126. | 'image/gif'
  127. | 'image/webp'
  128. | 'image/flif'
  129. | 'image/x-canon-cr2'
  130. | 'image/tiff'
  131. | 'image/bmp'
  132. | 'image/vnd.ms-photo'
  133. | 'image/vnd.adobe.photoshop'
  134. | 'application/epub+zip'
  135. | 'application/x-xpinstall'
  136. | 'application/vnd.oasis.opendocument.text'
  137. | 'application/vnd.oasis.opendocument.spreadsheet'
  138. | 'application/vnd.oasis.opendocument.presentation'
  139. | 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
  140. | 'application/vnd.openxmlformats-officedocument.presentationml.presentation'
  141. | 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
  142. | 'application/zip'
  143. | 'application/x-tar'
  144. | 'application/x-rar-compressed'
  145. | 'application/gzip'
  146. | 'application/x-bzip2'
  147. | 'application/x-7z-compressed'
  148. | 'application/x-apple-diskimage'
  149. | 'video/mp4'
  150. | 'audio/midi'
  151. | 'video/x-matroska'
  152. | 'video/webm'
  153. | 'video/quicktime'
  154. | 'video/vnd.avi'
  155. | 'audio/vnd.wave'
  156. | 'audio/qcelp'
  157. | 'audio/x-ms-wma'
  158. | 'video/x-ms-asf'
  159. | 'application/vnd.ms-asf'
  160. | 'video/mpeg'
  161. | 'video/3gpp'
  162. | 'audio/mpeg'
  163. | 'audio/mp4' // RFC 4337
  164. | 'audio/opus'
  165. | 'video/ogg'
  166. | 'audio/ogg'
  167. | 'application/ogg'
  168. | 'audio/x-flac'
  169. | 'audio/ape'
  170. | 'audio/wavpack'
  171. | 'audio/amr'
  172. | 'application/pdf'
  173. | 'application/x-msdownload'
  174. | 'application/x-shockwave-flash'
  175. | 'application/rtf'
  176. | 'application/wasm'
  177. | 'font/woff'
  178. | 'font/woff2'
  179. | 'application/vnd.ms-fontobject'
  180. | 'font/ttf'
  181. | 'font/otf'
  182. | 'image/x-icon'
  183. | 'video/x-flv'
  184. | 'application/postscript'
  185. | 'application/x-xz'
  186. | 'application/x-sqlite3'
  187. | 'application/x-nintendo-nes-rom'
  188. | 'application/x-google-chrome-extension'
  189. | 'application/vnd.ms-cab-compressed'
  190. | 'application/x-deb'
  191. | 'application/x-unix-archive'
  192. | 'application/x-rpm'
  193. | 'application/x-compress'
  194. | 'application/x-lzip'
  195. | 'application/x-msi'
  196. | 'application/x-mie'
  197. | 'application/x-apache-arrow'
  198. | 'application/mxf'
  199. | 'video/mp2t'
  200. | 'application/x-blender'
  201. | 'image/bpg'
  202. | 'image/jp2'
  203. | 'image/jpx'
  204. | 'image/jpm'
  205. | 'image/mj2'
  206. | 'audio/aiff'
  207. | 'application/xml'
  208. | 'application/x-mobipocket-ebook'
  209. | 'image/heif'
  210. | 'image/heif-sequence'
  211. | 'image/heic'
  212. | 'image/heic-sequence'
  213. | 'image/ktx'
  214. | 'application/dicom'
  215. | 'audio/x-musepack'
  216. | 'text/calendar'
  217. | 'model/gltf-binary'
  218. | 'application/vnd.tcpdump.pcap'
  219. | 'audio/x-dsf' // Non-standard
  220. | 'application/x.ms.shortcut' // Invented by us
  221. | 'application/x.apple.alias' // Invented by us
  222. | 'audio/x-voc'
  223. | 'audio/vnd.dolby.dd-raw'
  224. | 'audio/x-m4a'
  225. | 'image/apng'
  226. | 'image/x-olympus-orf'
  227. | 'image/x-sony-arw'
  228. | 'image/x-adobe-dng'
  229. | 'image/x-nikon-nef'
  230. | 'image/x-panasonic-rw2'
  231. | 'image/x-fujifilm-raf'
  232. | 'video/x-m4v'
  233. | 'video/3gpp2'
  234. | 'application/x-esri-shape';
  235. interface FileTypeResult {
  236. /**
  237. One of the supported [file types](https://github.com/sindresorhus/file-type#supported-file-types).
  238. */
  239. ext: FileType;
  240. /**
  241. The detected [MIME type](https://en.wikipedia.org/wiki/Internet_media_type).
  242. */
  243. mime: MimeType;
  244. }
  245. type ReadableStreamWithFileType = ReadableStream & {
  246. readonly fileType?: FileTypeResult;
  247. };
  248. }
  249. declare const fileType: {
  250. /**
  251. Detect the file type of a `Buffer`/`Uint8Array`/`ArrayBuffer`. The file type is detected by checking the [magic number](https://en.wikipedia.org/wiki/Magic_number_(programming)#Magic_numbers_in_files) of the buffer.
  252. @param buffer - It only needs the first `.minimumBytes` bytes. The exception is detection of `docx`, `pptx`, and `xlsx` which potentially requires reading the whole file.
  253. @returns The detected file type and MIME type or `undefined` when there was no match.
  254. @example
  255. ```
  256. import readChunk = require('read-chunk');
  257. import fileType = require('file-type');
  258. const buffer = readChunk.sync('unicorn.png', 0, fileType.minimumBytes);
  259. fileType(buffer);
  260. //=> {ext: 'png', mime: 'image/png'}
  261. // Or from a remote location:
  262. import * as http from 'http';
  263. const url = 'https://assets-cdn.github.com/images/spinners/octocat-spinner-32.gif';
  264. http.get(url, response => {
  265. response.on('readable', () => {
  266. const chunk = response.read(fileType.minimumBytes);
  267. response.destroy();
  268. console.log(fileType(chunk));
  269. //=> {ext: 'gif', mime: 'image/gif'}
  270. });
  271. });
  272. ```
  273. */
  274. (buffer: Buffer | Uint8Array | ArrayBuffer): fileType.FileTypeResult | undefined;
  275. /**
  276. The minimum amount of bytes needed to detect a file type. Currently, it's 4100 bytes, but it can change, so don't hard-code it.
  277. */
  278. readonly minimumBytes: number;
  279. /**
  280. Supported file extensions.
  281. */
  282. readonly extensions: readonly fileType.FileType[];
  283. /**
  284. Supported MIME types.
  285. */
  286. readonly mimeTypes: readonly fileType.MimeType[];
  287. /**
  288. Detect the file type of a readable stream.
  289. @param readableStream - A readable stream containing a file to examine, see: [`stream.Readable`](https://nodejs.org/api/stream.html#stream_class_stream_readable).
  290. @returns A `Promise` which resolves to the original readable stream argument, but with an added `fileType` property, which is an object like the one returned from `fileType()`.
  291. @example
  292. ```
  293. import * as fs from 'fs';
  294. import * as crypto from 'crypto';
  295. import fileType = require('file-type');
  296. (async () => {
  297. const read = fs.createReadStream('encrypted.enc');
  298. const decipher = crypto.createDecipheriv(alg, key, iv);
  299. const stream = await fileType.stream(read.pipe(decipher));
  300. console.log(stream.fileType);
  301. //=> {ext: 'mov', mime: 'video/quicktime'}
  302. const write = fs.createWriteStream(`decrypted.${stream.fileType.ext}`);
  303. stream.pipe(write);
  304. })();
  305. ```
  306. */
  307. readonly stream: (
  308. readableStream: ReadableStream
  309. ) => Promise<fileType.ReadableStreamWithFileType>;
  310. };
  311. export = fileType;