Skip to main content

Mime Types

MIME-type lookup by file extension, plus the database behind it and its string-literal types. It is a vendored, TypeScript-ported subset of the mime-types package with path.extname removed, so it runs on edge runtimes where node:path is unavailable. MimeType and FileExtension are unions of every value in the database, which turns a typo into a compile error rather than a silent false at runtime.

Example

import { lookup, type MimeType } from '@rtorcato/js-common/mime-types'

// Set Content-Type from an uploaded filename.
res.setHeader('Content-Type', lookup(upload.name) || 'application/octet-stream')

lookup('report.pdf') // 'application/pdf'
lookup('.png') // 'image/png' — bare extensions work too
lookup('README') // false

// A typo in a MimeType is a compile error, not a silent runtime `false`.
const accepted: MimeType[] = ['image/png', 'image/webp']

Import

import { FileExtension, MimeType, MimeValue } from '@rtorcato/js-common/mime-types'

Exports

NameSummary
FileExtensionUnion of every file extension string (e.g. 'pdf', 'png') present in the MIME-type database.
MimeTypeUnion of every MIME type string (e.g. 'application/json') present in the database.
MimeValueMetadata record for a given MimeType — its source and associated extensions.
extensionsMap from MimeType to its associated file extensions, populated from the database.
lookupLookup the MIME type for a file path/extension.
mimeTypesFull MIME-type database keyed by MimeType, with source and extensions metadata per entry.
typesMap from FileExtension to its canonical MimeType, populated from the database.

See also

  • file — read, write and delete files
  • fetch — JSON helpers and fetch with a timeout
  • url — parse, validate and edit URLs and query params
  • node — Node version checks and optional requires