Modules@robinpath/mime
mime

@robinpath/mime

0.1.2Node.jsPublic

MIME type detection from extensions and file content, type classification, Content-Type building

Mime

MIME type detection from extensions and file content, type classification, Content-Type building

Package: @robinpath/mime | Category: Utility | Type: Utility

Authentication

No authentication required. All functions are available immediately.

Use Cases

Use the mime module when you need to:

  • Get MIME type from file extension -- Use mime.lookup to perform this operation
  • Get extension from MIME type -- Use mime.extension to perform this operation
  • Detect MIME type from file content (magic bytes) -- Use mime.detect to perform this operation
  • Get charset for MIME type -- Use mime.charset to perform this operation
  • Check if MIME type is text-based -- Use mime.isText to perform this operation

Quick Reference

FunctionDescriptionReturns
lookupGet MIME type from file extensionMIME type or null
extensionGet extension from MIME typeExtension or null
detectDetect MIME type from file content (magic bytes)Detected MIME type
charsetGet charset for MIME typeUTF-8 or null
isTextCheck if MIME type is text-basedtrue if text
isImageCheck if MIME type is imagetrue if image
isAudioCheck if MIME type is audiotrue if audio
isVideoCheck if MIME type is videotrue if video
isFontCheck if MIME type is fonttrue if font
isArchiveCheck if MIME type is archivetrue if archive
contentTypeBuild Content-Type header with charsetContent-Type header value
allTypesGet all known MIME type mappingsExtension-to-MIME map

Functions

lookup

Get MIME type from file extension

Module: mime | Returns: string -- MIME type or null

mime.lookup "photo.png"
ParameterTypeRequiredDescription
pathOrExtstringYesFile path or extension

extension

Get extension from MIME type

Module: mime | Returns: string -- Extension or null

mime.extension "image/png"
ParameterTypeRequiredDescription
mimeTypestringYesMIME type

detect

Detect MIME type from file content (magic bytes)

Module: mime | Returns: string -- Detected MIME type

mime.detect "./unknown_file"
ParameterTypeRequiredDescription
filePathstringYesFile path

charset

Get charset for MIME type

Module: mime | Returns: string -- UTF-8 or null

mime.charset "text/html"
ParameterTypeRequiredDescription
mimeTypestringYesMIME type

isText

Check if MIME type is text-based

Module: mime | Returns: boolean -- true if text

mime.isText "application/json"
ParameterTypeRequiredDescription
mimeTypestringYesMIME type

isImage

Check if MIME type is image

Module: mime | Returns: boolean -- true if image

mime.isImage "image/png"
ParameterTypeRequiredDescription
mimeTypestringYesMIME type

isAudio

Check if MIME type is audio

Module: mime | Returns: boolean -- true if audio

mime.isAudio "audio/mpeg"
ParameterTypeRequiredDescription
mimeTypestringYesMIME type

isVideo

Check if MIME type is video

Module: mime | Returns: boolean -- true if video

mime.isVideo "video/mp4"
ParameterTypeRequiredDescription
mimeTypestringYesMIME type

isFont

Check if MIME type is font

Module: mime | Returns: boolean -- true if font

mime.isFont "font/woff2"
ParameterTypeRequiredDescription
mimeTypestringYesMIME type

isArchive

Check if MIME type is archive

Module: mime | Returns: boolean -- true if archive

mime.isArchive "application/zip"
ParameterTypeRequiredDescription
mimeTypestringYesMIME type

contentType

Build Content-Type header with charset

Module: mime | Returns: string -- Content-Type header value

mime.contentType "index.html"
ParameterTypeRequiredDescription
pathOrExtstringYesFile path or extension

allTypes

Get all known MIME type mappings

Module: mime | Returns: object -- Extension-to-MIME map

mime.allTypes
ParameterTypeRequiredDescription
(none)NoCall with no arguments

Error Handling

All functions throw on failure. Common errors:

ErrorCause
(standard errors)Check function parameters and authentication
@desc "Lookup and validate result"
do
  set $result as mime.lookup "photo.png"
  if $result != null
    print "Success"
  else
    print "No result"
  end
enddo

Recipes

1. Multi-step Mime workflow

Chain multiple mime operations together.

@desc "Lookup, extension, and more"
do
  set $r_lookup as mime.lookup "photo.png"
  set $r_extension as mime.extension "image/png"
  set $r_detect as mime.detect "./unknown_file"
  print "All operations complete"
enddo

2. Safe lookup with validation

Check results before proceeding.

@desc "Lookup and validate result"
do
  set $result as mime.lookup "photo.png"
  if $result != null
    print "Success: " + $result
  else
    print "Operation returned no data"
  end
enddo

Related Modules

  • json -- JSON module for complementary functionality

Versions (1)

VersionTagPublished
0.1.2latest1 months ago
Install
$ robinpath add @robinpath/mime

Collaborators

Dumitru Balaban
Dumitru Balaban
@dumitru
View all @robinpath modules
Version0.1.2
LicenseMIT
Unpacked Size5.4 KB
Versions1
Weekly Downloads21
Total Downloads21
Stars0
Last Publish1 months ago
Created1 months ago

Keywords

Category

utilities