mime-types

Lookup helpers and read-only data accessors ported from npm mime-types 3.0.2 with data generated from mime-db 1.54.0. Every function is stateless — the 2,601-entry database is compiled in as constexpr data, so there is no startup cost and no concurrency concern.

Lookup misses use C++ absence instead of JavaScript false: polycpp::mime::lookup(), polycpp::mime::contentType(), polycpp::mime::extension(), and polycpp::mime::charset() return std::optional<std::string>. The generated map surfaces are exposed as read-only accessors rather than mutable JavaScript objects.

struct TypeEntry

Read-only entry from the extension-to-MIME-type map.

This is the C++ equivalent of one entry in upstream mime.types.

Public Members

std::string_view extension

File extension without a leading dot.

std::string_view type

Preferred MIME type for the extension.

struct ExtensionConflict

Diagnostic record for an extension whose preferred MIME type changed under upstream mimeScore conflict resolution.

This mirrors the upstream _extensionConflicts diagnostic export without exposing mutable global maps.

Public Members

std::string_view extension

File extension without a leading dot.

std::string_view legacyType

MIME type selected by legacy scoring.

std::string_view preferredType

MIME type selected by mimeScore.

std::optional<std::string> polycpp::mime::lookup(const std::string &pathOrExt)

Look up the MIME type for a file path or extension.

Accepts a file path, extension with dot (.html), or bare extension (html). The lookup is case-insensitive.

Example
mime::lookup("file.html");       // "text/html"
mime::lookup(".json");           // "application/json"
mime::lookup("txt");             // "text/plain"
mime::lookup("/path/to/f.js");   // "text/javascript"
mime::lookup("bogus");           // std::nullopt

Parameters:

pathOrExt – A file path, .ext, or bare extension.

Returns:

The MIME type string, or std::nullopt if not found.

std::optional<std::string> polycpp::mime::contentType(const std::string &typeOrExt)

Create a full Content-Type header value.

If the input has no /, it is treated as an extension and looked up. If the MIME type has a known charset and the input does not already contain a charset parameter, ; charset=... is appended.

Example
mime::contentType("html");
// "text/html; charset=utf-8"
mime::contentType("application/json");
// "application/json; charset=utf-8"
mime::contentType("text/html; charset=iso-8859-1");
// "text/html; charset=iso-8859-1"  (unchanged)

Parameters:

typeOrExt – A MIME type or file extension.

Returns:

The full content-type string, or std::nullopt if the extension is unknown.

std::optional<std::string> polycpp::mime::extension(const std::string &mimeType)

Get the default file extension for a MIME type.

Strips any parameters (e.g., ; charset=utf-8) before lookup.

Example
mime::extension("text/html");                // "html"
mime::extension("text/html; charset=UTF-8"); // "html"
mime::extension("application/x-bogus");      // std::nullopt

Parameters:

mimeType – A MIME type string, optionally with parameters.

Returns:

The default extension (without dot), or std::nullopt.

std::optional<std::string> polycpp::mime::charset(const std::string &mimeType)

Get the default charset for a MIME type.

Returns the charset from the mime-db if one is defined. Falls back to "UTF-8" for all text/ types. Returns std::nullopt for types with no known charset.

Example
mime::charset("text/html");          // "UTF-8"
mime::charset("application/json");   // "UTF-8"
mime::charset("image/png");          // std::nullopt

Parameters:

mimeType – A MIME type string, optionally with parameters.

Returns:

The charset string (e.g., "UTF-8"), or std::nullopt.

std::span<const TypeEntry> polycpp::mime::types()

Return the read-only extension-to-MIME-type map.

The returned span points at static generated data and remains valid for the lifetime of the program.

Returns:

Extension-to-type entries sorted by extension.

std::vector<std::string> polycpp::mime::extensions(const std::string &mimeType)

Return all known extensions for a MIME type.

This is the read-only C++ equivalent of upstream mime.extensions[type]. Parameters are stripped and lookup is case-insensitive.

Parameters:

mimeType – A MIME type string, optionally with parameters.

Returns:

Extensions without leading dots; empty when the type is unknown or has no extensions.

std::span<const ExtensionConflict> polycpp::mime::extensionConflicts()

Return extension conflict diagnostics from upstream mimeScore.

The returned span points at static generated data and remains valid for the lifetime of the program.

Returns:

Extension conflict records sorted by generation order.