Types
ContentType = string
- A string representing the MIME type, e.g., "text/html".
Mime = ref object
- Represents a MIME type and its associated metadata.
MimeDatabaseStorage = ref object
MimeDBException = object of CatchableError
- Exception type for MIME database errors.
MimeSource = enum mimeSourceNone = "none", mimeSourceApache = "apache", mimeSourceIana = "iana", mimeSourceNginx = "nginx"
- The source of the MIME type information.
Vars
MimeDB: MimeDatabaseStorage
- A global variable that holds the MIME database. It contains mappings of MIME types to their metadata and file extensions to MIME types. Populated lazily on first use (see ensureDatabase): importing this module — and therefore starting any process that links it — parses nothing. The bundled 200KB JSON stays an untouched const until the first query.
Procs
proc ensureDatabase() {....raises: [OpenParserJsonError, ValueError], tags: [], forbids: [].}
- Parses the bundled database on first use (thread-safe, double-checked). Every query proc below calls this first, so callers never need an explicit init. Explicit initDatabase() / initMimeDatabase(custom) calls still work and win if they run first.
proc getExtensions(mime: Option[Mime]): Option[seq[string]] {....raises: [], tags: [], forbids: [].}
- Returns the file extensions associated with a given Mime type.
proc getMimeInfo(mimeType: string): Option[Mime] {. ...raises: [OpenParserJsonError, ValueError, KeyError], tags: [], forbids: [].}
- Returns the Mime information for a given MIME type, if it exists.
proc getMimeType(ext: string): Option[string] {. ...raises: [OpenParserJsonError, ValueError, KeyError], tags: [], forbids: [].}
- Returns the MIME type for a given file extension, if it exists. Accepts extensions with or without a leading dot, e.g. both "html" and ".html" work (case-insensitive).
proc getSource(mime: Option[Mime]): MimeSource {....raises: [], tags: [], forbids: [].}
- Returns the source of the given Mime type.
proc hasExtension(mime: Option[Mime]; ext: string): bool {....raises: [], tags: [], forbids: [].}
- Checks if the given Mime type has the specified file extension. Accepts extensions with or without a leading dot, e.g. both "zip" and ".zip" work (case-insensitive).
proc hasMimeType(mimeType: string): bool {. ...raises: [OpenParserJsonError, ValueError], tags: [], forbids: [].}
- Checks if the MIME database has an entry for a given MIME type.
proc initMimeDatabase(data: sink string) {. ...raises: [OpenParserJsonError, ValueError], tags: [], forbids: [].}
- Initializes the MIME database from a JSON string at runtime.
proc isCompressible(mime: Option[Mime]): bool {....raises: [], tags: [], forbids: [].}
- Checks if the given MIME type is compressible.
proc isCompressibleExt(ext: string): bool {. ...raises: [OpenParserJsonError, ValueError, KeyError], tags: [], forbids: [].}
- Compressibility for a file extension in one lookup pass, with no intermediate MIME-type string: ".HTML" and "html" both work. Already-clean values allocate nothing.
proc isCompressibleType(mimeType: string): bool {. ...raises: [OpenParserJsonError, ValueError, KeyError], tags: [], forbids: [].}
- Compressibility for a Content-Type header value: strips parameters (text/html; charset=utf-8) and matches case-insensitively. The common already-clean value allocates nothing; dirty values cost one small copy.
proc isExtension(ext: string): bool {....raises: [OpenParserJsonError, ValueError], tags: [], forbids: [].}
- Checks if the MIME database has an entry for the given file extension. Accepts extensions with or without a leading dot, e.g. both "html" and ".html" work (case-insensitive).
proc normalizeExtension(ext: string): string {....raises: [], tags: [], forbids: [].}
- Normalizes a file extension for database lookups. Strips whitespace, leading dots and lowercases the result, so "html", ".html" and ".HTML" are all equivalent.
Macros
macro initDatabase()
- Eagerly loads the bundled MIME database (same data the lazy ensureDatabase parses on first query). Only needed when you want to pay the parse cost up front, e.g. before spawning worker threads on a backend without locks.