mime-db vs mime-types vs content-type vs mime vs file-type
Web开发中的MIME类型处理库
mime-dbmime-typescontent-typemimefile-type类似的npm包:

Web开发中的MIME类型处理库

这些库用于处理MIME类型,帮助开发者识别和管理文件类型。它们提供了不同的功能和使用场景,适用于各种Web开发需求。通过这些库,开发者可以轻松获取文件的MIME类型,确保在处理文件时遵循正确的格式和标准。

npm下载趋势

3 年

GitHub Stars 排名

统计详情

npm包名称
下载量
Stars
大小
Issues
发布时间
License
mime-db299,163,8421,248226 kB552 年前MIT
mime-types286,918,6441,47822.9 kB271 年前MIT
content-type248,651,33014836.2 kB21 个月前MIT
mime158,382,4862,359161 kB06 天前MIT
file-type04,336142 kB03 天前MIT

功能对比: mime-db vs mime-types vs content-type vs mime vs file-type

功能

  • mime-db:

    mime-db库是一个包含所有MIME类型及其相关信息的数据库,适合需要频繁查询MIME类型的应用。

  • mime-types:

    mime-types库提供了强大的MIME类型处理功能,支持解析、生成和扩展MIME类型,适合复杂的Web应用。

  • content-type:

    content-type库提供了基本的功能来解析和生成HTTP内容类型,适合简单的应用场景。

  • mime:

    mime库提供了全面的MIME类型映射,支持多种文件扩展名与MIME类型的转换,适合需要处理多种文件格式的应用。

  • file-type:

    file-type库能够根据文件的内容来识别文件类型,支持多种文件格式,适合需要准确识别文件类型的应用。

使用场景

  • mime-db:

    mime-db库适合用于需要查询和验证MIME类型的应用,如内容分发网络(CDN)。

  • mime-types:

    mime-types库适合用于需要灵活处理MIME类型的复杂应用,如Web服务器和API开发。

  • content-type:

    content-type库适合用于处理HTTP请求和响应中的内容类型,尤其是在小型项目中。

  • mime:

    mime库适合用于需要处理多种文件格式的Web应用,如文件管理系统。

  • file-type:

    file-type库适合用于文件上传和下载功能中,确保文件类型的正确识别。

维护

  • mime-db:

    mime-db库定期更新,确保MIME类型信息的准确性,适合需要最新数据的应用。

  • mime-types:

    mime-types库维护活跃,提供丰富的功能和灵活性,适合需要长期支持的复杂项目。

  • content-type:

    content-type库维护简单,更新频率较低,适合不需要频繁更新的项目。

  • mime:

    mime库维护良好,拥有广泛的用户基础,适合需要稳定支持的项目。

  • file-type:

    file-type库活跃度较高,定期更新以支持新的文件类型,适合需要最新功能的项目。

学习曲线

  • mime-db:

    mime-db库的使用相对简单,适合需要快速查询MIME类型的开发者。

  • mime-types:

    mime-types库功能丰富,学习曲线稍陡,适合有经验的开发者。

  • content-type:

    content-type库简单易用,学习曲线平缓,适合初学者。

  • mime:

    mime库相对简单,学习曲线适中,适合有一定经验的开发者。

  • file-type:

    file-type库功能明确,易于上手,适合希望快速实现文件类型识别的开发者。

如何选择: mime-db vs mime-types vs content-type vs mime vs file-type

  • mime-db:

    选择mime-db库,如果你需要一个包含所有MIME类型及其相关信息的数据库。它提供了丰富的MIME类型数据,适合需要频繁查询MIME类型的应用。

  • mime-types:

    选择mime-types库,如果你需要一个功能强大的MIME类型处理工具,支持MIME类型的解析、生成和扩展。它适合需要灵活处理MIME类型的复杂应用。

  • content-type:

    选择content-type库,如果你需要一个简单且轻量的解决方案来解析和生成HTTP内容类型。它提供了基本的功能,适合小型项目或简单的应用场景。

  • mime:

    选择mime库,如果你需要一个全面的MIME类型映射,支持多种文件扩展名与MIME类型的转换。它适合需要处理多种文件格式的Web应用。

  • file-type:

    选择file-type库,如果你需要根据文件的内容来检测文件类型,而不仅仅是依赖文件扩展名。它支持多种文件类型的识别,适合需要文件内容分析的应用。

mime-db的README

mime-db

NPM Version NPM Downloads Node.js Version Build Status Coverage Status

This is a large database of mime types and information about them. It consists of a single, public JSON file and does not include any logic, allowing it to remain as un-opinionated as possible with an API. It aggregates data from the following sources:

Installation

npm install mime-db

Database Download

If you intend to use this in a web browser, you can conveniently access the JSON file via jsDelivr, a popular CDN (Content Delivery Network). To ensure stability and compatibility, it is advisable to specify a release tag instead of using the 'master' branch. This is because the JSON file's format might change in future updates, and relying on a specific release tag will prevent potential issues arising from these changes.

https://cdn.jsdelivr.net/gh/jshttp/mime-db@master/db.json

Usage

var db = require('mime-db')

// grab data on .js files
var data = db['application/javascript']

Data Structure

The JSON file is a map lookup for lowercased mime types. Each mime type has the following properties:

  • .source - where the mime type is defined. If not set, it's probably a custom media type.
  • .extensions[] - known extensions associated with this mime type.
  • .compressible - whether a file of this type can be gzipped.
  • .charset - the default charset associated with this type, if any.

If unknown, every property could be undefined.

Note on MIME Type Data and Semver

This package considers the programmatic api as the semver compatibility. This means the MIME type resolution is not considered in the semver bumps. This means that if you want to pin your mime-db data you will need to do it in your application. While this expectation was not set in docs until now, it is how the pacakge operated, so we do not feel this is a breaking change.

Contributing

The primary way to contribute to this database is by updating the data in one of the upstream sources. The database is updated from the upstreams periodically and will pull in any changes.

Registering Media Types

The best way to get new media types included in this library is to register them with the IANA. The community registration procedure is outlined in RFC 6838 section 5. Types registered with the IANA are automatically pulled into this library.

Direct Inclusion

If that is not possible / feasible, they can be added directly here as a "custom" type. To do this, it is required to have a primary source that definitively lists the media type. If an extension is going to be listed as associated with this media type, the source must definitively link the media type and extension as well.

To edit the database, only make PRs against src/custom-types.json or src/custom-suffix.json.

The src/custom-types.json file is a JSON object with the MIME type as the keys and the values being an object with the following keys:

  • compressible - leave out if you don't know, otherwise true/false to indicate whether the data represented by the type is typically compressible.
  • extensions - include an array of file extensions that are associated with the type.
  • notes - human-readable notes about the type, typically what the type is.
  • sources - include an array of URLs of where the MIME type and the associated extensions are sourced from. This needs to be a primary source; links to type aggregating sites and Wikipedia are not acceptable.

To update the build, run npm run build.