npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

guess-file-type

v1.0.1

Published

Detect the file type by doing various checks. Returns the mime type.

Downloads

302

Readme

Guess File Type

Detect the file type by doing various checks. Returns the mime type.

How it Works

  • First it tries to use the mimetype command. See Requirements > Optional.
  • If mimetype is not available, it uses the file signature (magic numbers).
  • If the type could not be determined, it falls back to using the file extension.
  • It returns the mime type of the file on success, and a string 'unknown' on failure.

Changing the Behavior

You can avoid the default behavior by using the individual functions. See Usage.

Installation

In a node.js app:

npm install guess-file-type

Optional

In Ubuntu, install the mimetype command:

sudo apt install libfile-mimeinfo-perl

The module will use it if the command is available.

Its results are much more accurate than the file command in Linux.

Usage

const fileGuesser = require('guess-file-type');

Auto guess the type

Async:

try {
    let type = await fileGuesser.guess('./test/files/gif.gif');
    console.log(type); // 'image/gif' or 'unknown' on fail
} catch (err) {
    
}

Promise:

fileGuesser.guess('./test/files/gif.gif').then( (type) => {
    console.log(type); // 'image/gif' or 'unknown' on fail
}).catch((err)=>{

});

Manual Checking

You can call the individual functions separately and compose them if neccessary.

Guess using the file signature only

Async:

try {
    let type = await fileGuesser.guessByFileSignature('./test/files/png.png');
    console.log(type);  // 'image/png' or 'unknown' on fail
} catch(err){}

Promise:

fileGuesser.guessByFileSignature('./test/files/png.png').then((type)=>{
    console.log(type);  // 'image/png' or 'unknown' on fail
}).catch((err)=>{});

Guess using the file extension only

Async:

try {
    let type = await fileGuesser.guessByExtension('./test/files/png.png');
    console.log(type);  // 'image/png' or 'unknown' on fail
} catch(err){}

Promise:

fileGuesser.guessByExtension('./test/files/png.png').then((type)=>{
    console.log(type);  // 'image/png' or 'unknown' on fail
}).catch((err)=>{});

Get file extension from mime type

let ext = guessFileType.getExtensionFromMime('image/png'); // png

Development

git clone https://github.com/kosinix/guess-file-type.git
cd [/path/to/]guess-file-type
npm install

Test

Install mocha globally

npm install mocha -g

Run test

npm test

References

  • https://www.iana.org/assignments/media-types/media-types.xhtml
  • http://svn.apache.org/viewvc/httpd/httpd/trunk/docs/conf/mime.types?view=markup
  • https://www.garykessler.net/library/file_sigs.html
  • https://en.wikipedia.org/wiki/List_of_file_signatures

Supported Types

  1. 123
  2. 3dml
  3. 3ds
  4. 3g2
  5. 3gp
  6. 7z
  7. aab
  8. aac
  9. aam
  10. aas
  11. abw
  12. ac
  13. acc
  14. ace
  15. acu
  16. acutc
  17. adp
  18. aep
  19. afm
  20. afp
  21. ahead
  22. ai
  23. aif
  24. aifc
  25. aiff
  26. air
  27. ait
  28. ami
  29. apk
  30. appcache
  31. application
  32. apr
  33. arc
  34. asc
  35. asf
  36. asm
  37. aso
  38. asx
  39. atc
  40. atom
  41. atomcat
  42. atomsvc
  43. atx
  44. au
  45. avi
  46. aw
  47. azf
  48. azs
  49. azw
  50. bat
  51. bcpio
  52. bdf
  53. bdm
  54. bed
  55. bh2
  56. bin
  57. blb
  58. blorb
  59. bmi
  60. bmp
  61. book
  62. box
  63. boz
  64. bpk
  65. btif
  66. bz
  67. bz2
  68. c
  69. c11amc
  70. c11amz
  71. c4d
  72. c4f
  73. c4g
  74. c4p
  75. c4u
  76. cab
  77. caf
  78. cap
  79. car
  80. cat
  81. cb7
  82. cba
  83. cbr
  84. cbt
  85. cbz
  86. cc
  87. cct
  88. ccxml
  89. cdbcmsg
  90. cdf
  91. cdkey
  92. cdmia
  93. cdmic
  94. cdmid
  95. cdmio
  96. cdmiq
  97. cdx
  98. cdxml
  99. cdy
  100. cer
  101. cfs
  102. cgm
  103. chat
  104. chm
  105. chrt
  106. cif
  107. cii
  108. cil
  109. cla
  110. class
  111. clkk
  112. clkp
  113. clkt
  114. clkw
  115. clkx
  116. clp
  117. cmc
  118. cmdf
  119. cml
  120. cmp
  121. cmx
  122. cod
  123. com
  124. conf
  125. cpio
  126. cpp
  127. cpt
  128. crd
  129. crl
  130. crt
  131. cryptonote
  132. csh
  133. csml
  134. csp
  135. css
  136. cst
  137. csv
  138. cu
  139. curl
  140. cww
  141. cxt
  142. cxx
  143. dae
  144. daf
  145. dart
  146. dataless
  147. davmount
  148. dbk
  149. dcr
  150. dcurl
  151. dd2
  152. ddd
  153. deb
  154. def
  155. deploy
  156. der
  157. dfac
  158. dgc
  159. dic
  160. dir
  161. dis
  162. dist
  163. distz
  164. djv
  165. djvu
  166. dll
  167. dmg
  168. dmp
  169. dms
  170. dna
  171. doc
  172. docm
  173. docx
  174. dot
  175. dotm
  176. dotx
  177. dp
  178. dpg
  179. dra
  180. dsc
  181. dssc
  182. dtb
  183. dtd
  184. dts
  185. dtshd
  186. dump
  187. dvb
  188. dvi
  189. dwf
  190. dwg
  191. dxf
  192. dxp
  193. dxr
  194. ecelp4800
  195. ecelp7470
  196. ecelp9600
  197. ecma
  198. edm
  199. edx
  200. efif
  201. ei6
  202. elc
  203. emf
  204. eml
  205. emma
  206. emz
  207. eol
  208. eot
  209. eps
  210. epub
  211. es3
  212. esa
  213. esf
  214. et3
  215. etx
  216. eva
  217. evy
  218. exe
  219. exi
  220. ext
  221. ez
  222. ez2
  223. ez3
  224. f
  225. f4v
  226. f77
  227. f90
  228. fbs
  229. fcdt
  230. fcs
  231. fdf
  232. fe_launch
  233. fg5
  234. fgd
  235. fh
  236. fh4
  237. fh5
  238. fh7
  239. fhc
  240. fig
  241. flac
  242. fli
  243. flo
  244. flv
  245. flw
  246. flx
  247. fly
  248. fm
  249. fnc
  250. for
  251. fpx
  252. frame
  253. fsc
  254. fst
  255. ftc
  256. fti
  257. fvt
  258. fxp
  259. fxpl
  260. fzs
  261. g2w
  262. g3
  263. g3w
  264. gac
  265. gam
  266. gbr
  267. gca
  268. gdl
  269. geo
  270. gex
  271. ggb
  272. ggt
  273. ghf
  274. gif
  275. gim
  276. gml
  277. gmx
  278. gnumeric
  279. gph
  280. gpx
  281. gqf
  282. gqs
  283. gram
  284. gramps
  285. gre
  286. grv
  287. grxml
  288. gsf
  289. gtar
  290. gtm
  291. gtw
  292. gv
  293. gxf
  294. gxt
  295. h
  296. h261
  297. h263
  298. h264
  299. hal
  300. hbci
  301. hdf
  302. hh
  303. hlp
  304. hpgl
  305. hpid
  306. hps
  307. hqx
  308. htke
  309. htm
  310. html
  311. hvd
  312. hvp
  313. hvs
  314. i2g
  315. icc
  316. ice
  317. icm
  318. ico
  319. ics
  320. ief
  321. ifb
  322. ifm
  323. iges
  324. igl
  325. igm
  326. igs
  327. igx
  328. iif
  329. imp
  330. ims
  331. in
  332. ink
  333. inkml
  334. install
  335. iota
  336. ipfix
  337. ipk
  338. irm
  339. irp
  340. iso
  341. itp
  342. ivp
  343. ivu
  344. jad
  345. jam
  346. jar
  347. java
  348. jisp
  349. jlt
  350. jnlp
  351. joda
  352. jpe
  353. jpeg
  354. jpg
  355. jpgm
  356. jpgv
  357. jpm
  358. js
  359. json
  360. jsonml
  361. kar
  362. karbon
  363. kfo
  364. kia
  365. kml
  366. kmz
  367. kne
  368. knp
  369. kon
  370. kpr
  371. kpt
  372. kpxx
  373. ksp
  374. ktr
  375. ktx
  376. ktz
  377. kwd
  378. kwt
  379. lasxml
  380. latex
  381. lbd
  382. lbe
  383. les
  384. lha
  385. link66
  386. list
  387. list3820
  388. listafp
  389. lnk
  390. log
  391. lostxml
  392. lrf
  393. lrm
  394. ltf
  395. lvp
  396. lwp
  397. lzh
  398. m13
  399. m14
  400. m1v
  401. m21
  402. m2a
  403. m2v
  404. m3a
  405. m3u
  406. m3u8
  407. m4a
  408. m4u
  409. m4v
  410. ma
  411. mads
  412. mag
  413. maker
  414. man
  415. mar
  416. mathml
  417. mb
  418. mbk
  419. mbox
  420. mc1
  421. mcd
  422. mcurl
  423. mdb
  424. mdi
  425. me
  426. mesh
  427. meta4
  428. metalink
  429. mets
  430. mfm
  431. mft
  432. mgp
  433. mgz
  434. mid
  435. midi
  436. mie
  437. mif
  438. mime
  439. mj2
  440. mjp2
  441. mk3d
  442. mka
  443. mks
  444. mkv
  445. mlp
  446. mmd
  447. mmf
  448. mmr
  449. mng
  450. mny
  451. mobi
  452. mods
  453. mov
  454. movie
  455. mp2
  456. mp21
  457. mp2a
  458. mp3
  459. mp4
  460. mp4a
  461. mp4s
  462. mp4v
  463. mpc
  464. mpe
  465. mpeg
  466. mpg
  467. mpg4
  468. mpga
  469. mpkg
  470. mpm
  471. mpn
  472. mpp
  473. mpt
  474. mpy
  475. mqy
  476. mrc
  477. mrcx
  478. ms
  479. mscml
  480. mseed
  481. mseq
  482. msf
  483. msh
  484. msi
  485. msl
  486. msty
  487. mts
  488. mus
  489. musicxml
  490. mvb
  491. mwf
  492. mxf
  493. mxl
  494. mxml
  495. mxs
  496. mxu
  497. n-gage
  498. n3
  499. nb
  500. nbp
  501. nc
  502. ncx
  503. nfo
  504. ngdat
  505. nitf
  506. nlu
  507. nml
  508. nnd
  509. nns
  510. nnw
  511. npx
  512. nsc
  513. nsf
  514. ntf
  515. nzb
  516. oa2
  517. oa3
  518. oas
  519. obd
  520. obj
  521. oda
  522. odb
  523. odc
  524. odf
  525. odft
  526. odg
  527. odi
  528. odm
  529. odp
  530. ods
  531. odt
  532. oga
  533. ogg
  534. ogv
  535. ogx
  536. omdoc
  537. onepkg
  538. onetmp
  539. onetoc
  540. onetoc2
  541. opf
  542. opml
  543. oprc
  544. org
  545. osf
  546. osfpvg
  547. otc
  548. otf
  549. otg
  550. oth
  551. oti
  552. otp
  553. ots
  554. ott
  555. oxps
  556. oxt
  557. p
  558. p10
  559. p12
  560. p7b
  561. p7c
  562. p7m
  563. p7r
  564. p7s
  565. p8
  566. pas
  567. paw
  568. pbd
  569. pbm
  570. pcap
  571. pcf
  572. pcl
  573. pclxl
  574. pct
  575. pcurl
  576. pcx
  577. pdb
  578. pdf
  579. pfa
  580. pfb
  581. pfm
  582. pfr
  583. pfx
  584. pgm
  585. pgn
  586. pgp
  587. pic
  588. pkg
  589. pki
  590. pkipath
  591. plb
  592. plc
  593. plf
  594. pls
  595. pml
  596. png
  597. pnm
  598. portpkg
  599. pot
  600. potm
  601. potx
  602. ppam
  603. ppd
  604. ppm
  605. pps
  606. ppsm
  607. ppsx
  608. ppt
  609. pptm
  610. pptx
  611. pqa
  612. prc
  613. pre
  614. prf
  615. ps
  616. psb
  617. psd
  618. psf
  619. pskcxml
  620. ptid
  621. pub
  622. pvb
  623. pwn
  624. pya
  625. pyv
  626. qam
  627. qbo
  628. qfx
  629. qps
  630. qt
  631. qwd
  632. qwt
  633. qxb
  634. qxd
  635. qxl
  636. qxt
  637. ra
  638. ram
  639. rar
  640. ras
  641. rcprofile
  642. rdf
  643. rdz
  644. rep
  645. res
  646. rgb
  647. rif
  648. rip
  649. ris
  650. rl
  651. rlc
  652. rld
  653. rm
  654. rmi
  655. rmp
  656. rms
  657. rmvb
  658. rnc
  659. roa
  660. roff
  661. rp9
  662. rpss
  663. rpst
  664. rq
  665. rs
  666. rsd
  667. rss
  668. rtf
  669. rtx
  670. s
  671. s3m
  672. saf
  673. sbml
  674. sc
  675. scd
  676. scm
  677. scq
  678. scs
  679. scurl
  680. sda
  681. sdc
  682. sdd
  683. sdkd
  684. sdkm
  685. sdp
  686. sdw
  687. see
  688. seed
  689. sema
  690. semd
  691. semf
  692. ser
  693. setpay
  694. setreg
  695. sfd-hdstx
  696. sfs
  697. sfv
  698. sgi
  699. sgl
  700. sgm
  701. sgml
  702. sh
  703. shar
  704. shf
  705. sid
  706. sig
  707. sil
  708. silo
  709. sis
  710. sisx
  711. sit
  712. sitx
  713. skd
  714. skm
  715. skp
  716. skt
  717. sldm
  718. sldx
  719. slt
  720. sm
  721. smf
  722. smi
  723. smil
  724. smv
  725. smzip
  726. snd
  727. snf
  728. so
  729. spc
  730. spf
  731. spl
  732. spot
  733. spp
  734. spq
  735. spx
  736. sql
  737. src
  738. srt
  739. sru
  740. srx
  741. ssdl
  742. sse
  743. ssf
  744. ssml
  745. st
  746. stc
  747. std
  748. stf
  749. sti
  750. stk
  751. stl
  752. str
  753. stw
  754. sub
  755. sub
  756. sus
  757. susp
  758. sv4cpio
  759. sv4crc
  760. svc
  761. svd
  762. svg
  763. svgz
  764. swa
  765. swf
  766. swi
  767. sxc
  768. sxd
  769. sxg
  770. sxi
  771. sxm
  772. sxw
  773. t
  774. t3
  775. taglet
  776. tao
  777. tar
  778. tcap
  779. tcl
  780. teacher
  781. tei
  782. teicorpus
  783. tex
  784. texi
  785. texinfo
  786. text
  787. tfi
  788. tfm
  789. tga
  790. thmx
  791. tif
  792. tiff
  793. tmo
  794. torrent
  795. tpl
  796. tpt
  797. tr
  798. tra
  799. trm
  800. tsd
  801. tsv
  802. ttc
  803. ttf
  804. ttl
  805. twd
  806. twds
  807. txd
  808. txf
  809. txt
  810. u32
  811. udeb
  812. ufd
  813. ufdl
  814. ulx
  815. umj
  816. unityweb
  817. uoml
  818. uri
  819. uris
  820. urls
  821. ustar
  822. utz
  823. uu
  824. uva
  825. uvd
  826. uvf
  827. uvg
  828. uvh
  829. uvi
  830. uvm
  831. uvp
  832. uvs
  833. uvt
  834. uvu
  835. uvv
  836. uvva
  837. uvvd
  838. uvvf
  839. uvvg
  840. uvvh
  841. uvvi
  842. uvvm
  843. uvvp
  844. uvvs
  845. uvvt
  846. uvvu
  847. uvvv
  848. uvvx
  849. uvvz
  850. uvx
  851. uvz
  852. vcard
  853. vcd
  854. vcf
  855. vcg
  856. vcs
  857. vcx
  858. vis
  859. viv
  860. vob
  861. vor
  862. vox
  863. vrml
  864. vsd
  865. vsf
  866. vss
  867. vst
  868. vsw
  869. vtu
  870. vxml
  871. w3d
  872. wad
  873. wav
  874. wax
  875. wbmp
  876. wbs
  877. wbxml
  878. wcm
  879. wdb
  880. wdp
  881. weba
  882. webm
  883. webp
  884. wg
  885. wgt
  886. wks
  887. wm
  888. wma
  889. wmd
  890. wmf
  891. wml
  892. wmlc
  893. wmls
  894. wmlsc
  895. wmv
  896. wmx
  897. wmz
  898. wmz
  899. woff
  900. woff2
  901. wpd
  902. wpl
  903. wps
  904. wqd
  905. wri
  906. wrl
  907. wsdl
  908. wspolicy
  909. wtb
  910. wvx
  911. x32
  912. x3d
  913. x3db
  914. x3dbz
  915. x3dv
  916. x3dvz
  917. x3dz
  918. xaml
  919. xap
  920. xar
  921. xbap
  922. xbd
  923. xbm
  924. xdf
  925. xdm
  926. xdp
  927. xdssc
  928. xdw
  929. xenc
  930. xer
  931. xfdf
  932. xfdl
  933. xht
  934. xhtml
  935. xhvml
  936. xif
  937. xla
  938. xlam
  939. xlc
  940. xlf
  941. xlm
  942. xls
  943. xlsb
  944. xlsm
  945. xlsx
  946. xlt
  947. xltm
  948. xltx
  949. xlw
  950. xm
  951. xml
  952. xo
  953. xop
  954. xpi
  955. xpl
  956. xpm
  957. xpr
  958. xps
  959. xpw
  960. xpx
  961. xsl
  962. xslt
  963. xsm
  964. xspf
  965. xul
  966. xvm
  967. xvml
  968. xwd
  969. xyz
  970. xz
  971. yang
  972. yin
  973. z1
  974. z2
  975. z3
  976. z4
  977. z5
  978. z6
  979. z7
  980. z8
  981. zaz
  982. zip
  983. zir
  984. zirz
  985. zmm

When using mimetype, supported types will depend on your mime database: cat /usr/share/mime/types. See: http://manpages.ubuntu.com/manpages/xenial/en/man1/mimetype.1p.html