index.d.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /// <reference types="node"/>
  2. import {Readable as ReadableStream} from 'stream';
  3. declare namespace fileType {
  4. type FileType =
  5. | 'jpg'
  6. | 'png'
  7. | 'gif'
  8. | 'webp'
  9. | 'flif'
  10. | 'cr2'
  11. | 'orf'
  12. | 'arw'
  13. | 'dng'
  14. | 'nef'
  15. | 'tif'
  16. | 'bmp'
  17. | 'jxr'
  18. | 'psd'
  19. | 'zip'
  20. | 'tar'
  21. | 'rar'
  22. | 'gz'
  23. | 'bz2'
  24. | '7z'
  25. | 'dmg'
  26. | 'mp4'
  27. | 'mid'
  28. | 'mkv'
  29. | 'webm'
  30. | 'mov'
  31. | 'avi'
  32. | 'wmv'
  33. | 'mpg'
  34. | 'mp2'
  35. | 'mp3'
  36. | 'm4a'
  37. | 'ogg'
  38. | 'opus'
  39. | 'flac'
  40. | 'wav'
  41. | 'qcp'
  42. | 'amr'
  43. | 'pdf'
  44. | 'epub'
  45. | 'mobi'
  46. | 'exe'
  47. | 'swf'
  48. | 'rtf'
  49. | 'woff'
  50. | 'woff2'
  51. | 'eot'
  52. | 'ttf'
  53. | 'otf'
  54. | 'ico'
  55. | 'flv'
  56. | 'ps'
  57. | 'xz'
  58. | 'sqlite'
  59. | 'nes'
  60. | 'crx'
  61. | 'xpi'
  62. | 'cab'
  63. | 'deb'
  64. | 'ar'
  65. | 'rpm'
  66. | 'Z'
  67. | 'lz'
  68. | 'msi'
  69. | 'mxf'
  70. | 'mts'
  71. | 'wasm'
  72. | 'blend'
  73. | 'bpg'
  74. | 'docx'
  75. | 'pptx'
  76. | 'xlsx'
  77. | '3gp'
  78. | '3g2'
  79. | 'jp2'
  80. | 'jpm'
  81. | 'jpx'
  82. | 'mj2'
  83. | 'aif'
  84. | 'odt'
  85. | 'ods'
  86. | 'odp'
  87. | 'xml'
  88. | 'heic'
  89. | 'cur'
  90. | 'ktx'
  91. | 'ape'
  92. | 'wv'
  93. | 'asf'
  94. | 'wma'
  95. | 'wmv'
  96. | 'dcm'
  97. | 'mpc'
  98. | 'ics'
  99. | 'glb'
  100. | 'pcap'
  101. | 'dsf'
  102. | 'lnk'
  103. | 'alias'
  104. | 'voc'
  105. | 'ac3'
  106. | 'm4a'
  107. | 'm4b'
  108. | 'm4p'
  109. | 'm4v'
  110. | 'f4a'
  111. | 'f4b'
  112. | 'f4p'
  113. | 'f4v';
  114. interface FileTypeResult {
  115. /**
  116. One of the supported [file types](https://github.com/sindresorhus/file-type#supported-file-types).
  117. */
  118. ext: FileType;
  119. /**
  120. The detected [MIME type](https://en.wikipedia.org/wiki/Internet_media_type).
  121. */
  122. mime: string;
  123. }
  124. type ReadableStreamWithFileType = ReadableStream & {
  125. readonly fileType?: FileTypeResult;
  126. };
  127. }
  128. declare const fileType: {
  129. /**
  130. 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.
  131. @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.
  132. @returns The detected file type and MIME type or `undefined` when there was no match.
  133. @example
  134. ```
  135. import readChunk = require('read-chunk');
  136. import fileType = require('file-type');
  137. const buffer = readChunk.sync('unicorn.png', 0, fileType.minimumBytes);
  138. fileType(buffer);
  139. //=> {ext: 'png', mime: 'image/png'}
  140. // Or from a remote location:
  141. import * as http from 'http';
  142. const url = 'https://assets-cdn.github.com/images/spinners/octocat-spinner-32.gif';
  143. http.get(url, response => {
  144. response.on('readable', () => {
  145. const chunk = response.read(fileType.minimumBytes);
  146. response.destroy();
  147. console.log(fileType(chunk));
  148. //=> {ext: 'gif', mime: 'image/gif'}
  149. });
  150. });
  151. ```
  152. */
  153. (buffer: Buffer | Uint8Array | ArrayBuffer): fileType.FileTypeResult | undefined;
  154. /**
  155. 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.
  156. */
  157. readonly minimumBytes: number;
  158. /**
  159. Detect the file type of a readable stream.
  160. @param readableStream - A readable stream containing a file to examine, see: [`stream.Readable`](https://nodejs.org/api/stream.html#stream_class_stream_readable).
  161. @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()`.
  162. @example
  163. ```
  164. import * as fs from 'fs';
  165. import * as crypto from 'crypto';
  166. import fileType = require('file-type');
  167. (async () => {
  168. const read = fs.createReadStream('encrypted.enc');
  169. const decipher = crypto.createDecipheriv(alg, key, iv);
  170. const stream = await fileType.stream(read.pipe(decipher));
  171. console.log(stream.fileType);
  172. //=> {ext: 'mov', mime: 'video/quicktime'}
  173. const write = fs.createWriteStream(`decrypted.${stream.fileType.ext}`);
  174. stream.pipe(write);
  175. })();
  176. ```
  177. */
  178. readonly stream: (
  179. readableStream: ReadableStream
  180. ) => Promise<fileType.ReadableStreamWithFileType>;
  181. };
  182. export = fileType;