or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

cli.mdcustom-instances.mdindex.mdmime-operations.md

mime-operations.mddocs/

0

# MIME Type Operations

1

2

Core functionality for resolving MIME types from file paths and extensions, with robust path handling and comprehensive type database coverage.

3

4

## Capabilities

5

6

### Get MIME Type from Path

7

8

Determines the MIME type for a given file path or extension with intelligent path parsing.

9

10

```typescript { .api }

11

/**

12

* Get mime type associated with an extension or file path

13

* @param path - File path or extension to look up

14

* @returns MIME type string or null if not found

15

*/

16

function getType(path: string): string | null;

17

```

18

19

**Usage Examples:**

20

21

```typescript

22

import mime from "mime";

23

24

// File extensions

25

mime.getType("js"); // "text/javascript"

26

mime.getType("json"); // "application/json"

27

mime.getType("png"); // "image/png"

28

29

// File paths (various formats)

30

mime.getType("script.js"); // "text/javascript"

31

mime.getType("dir/data.json"); // "application/json"

32

mime.getType("dir\\image.png"); // "image/png" (Windows paths)

33

mime.getType(".hidden.txt"); // "text/plain"

34

mime.getType("file.tar.gz"); // "application/gzip"

35

36

// Returns null for unrecognized or invalid inputs

37

mime.getType("unknown_extension"); // null

38

mime.getType("file_without_extension"); // null

39

mime.getType("path/file"); // null (no extension)

40

```

41

42

**Path Processing Rules:**

43

- Removes directory path components, only considers filename

44

- Extracts extension after the last dot

45

- Case-insensitive matching

46

- Handles both Unix (/) and Windows (\\) path separators

47

- Returns null for files without extensions in directory paths

48

49

### Get Default Extension from MIME Type

50

51

Retrieves the default (primary) file extension for a given MIME type.

52

53

```typescript { .api }

54

/**

55

* Get default file extension associated with a mime type

56

* @param type - MIME type to look up

57

* @returns Primary extension string or null if not found

58

*/

59

function getExtension(type: string): string | null;

60

```

61

62

**Usage Examples:**

63

64

```typescript

65

import mime from "mime";

66

67

// Standard MIME types

68

mime.getExtension("text/plain"); // "txt"

69

mime.getExtension("application/json"); // "json"

70

mime.getExtension("image/jpeg"); // "jpeg"

71

mime.getExtension("application/javascript"); // "js"

72

73

// MIME types with parameters (automatically stripped)

74

mime.getExtension("text/html; charset=utf-8"); // "html"

75

mime.getExtension("application/json; charset=utf-8"); // "json"

76

77

// Returns null for unrecognized types

78

mime.getExtension("unknown/type"); // null

79

mime.getExtension("invalid-mime-type"); // null

80

```

81

82

**Parameter Handling:**

83

- Automatically removes HTTP header parameters (charset, boundary, etc.)

84

- Trims whitespace from type string

85

- Case-insensitive type matching

86

87

### Get All Extensions from MIME Type

88

89

Retrieves all file extensions associated with a given MIME type (new in v4.0).

90

91

```typescript { .api }

92

/**

93

* Get all file extensions associated with a mime type

94

* @param type - MIME type to look up

95

* @returns Set of all extensions or null if not found

96

*/

97

function getAllExtensions(type: string): Set<string> | null;

98

```

99

100

**Usage Examples:**

101

102

```typescript

103

import mime from "mime";

104

105

// Multiple extensions for common types

106

mime.getAllExtensions("image/jpeg"); // Set(3) { "jpeg", "jpg", "jpe" }

107

mime.getAllExtensions("text/javascript"); // Set(2) { "js", "mjs" }

108

mime.getAllExtensions("application/zip"); // Set(1) { "zip" }

109

110

// Single extension types

111

mime.getAllExtensions("text/plain"); // Set(1) { "txt" }

112

113

// Returns null for unrecognized types

114

mime.getAllExtensions("unknown/type"); // null

115

```

116

117

**Set Iteration:**

118

119

```typescript

120

const extensions = mime.getAllExtensions("image/jpeg");

121

if (extensions) {

122

for (const ext of extensions) {

123

console.log(ext); // "jpeg", "jpg", "jpe"

124

}

125

126

// Convert to array if needed

127

const extArray = Array.from(extensions); // ["jpeg", "jpg", "jpe"]

128

}

129

```

130

131

## Version Differences

132

133

### Full vs Lite Versions

134

135

```typescript

136

// Full version - includes all MIME types (800+ types, 1000+ extensions)

137

import mime from "mime";

138

139

// Lite version - excludes unofficial types (prs.*, x-*, vnd.*)

140

import mime from "mime/lite";

141

```

142

143

**Type Coverage:**

144

- **Full version**: Standard types + vendor-specific + experimental types

145

- **Lite version**: Only standard, widely-supported MIME types

146

- Both versions have identical API signatures and behavior

147

- Choose lite version for smaller bundle size, full version for comprehensive coverage

148

149

## Error Handling

150

151

All methods handle invalid input gracefully:

152

153

```typescript

154

// Invalid inputs return null, never throw

155

mime.getType(null); // null

156

mime.getType(undefined); // null

157

mime.getType(123); // null

158

mime.getExtension(null); // null

159

mime.getAllExtensions(""); // null

160

```

161

162

## Type Safety

163

164

```typescript

165

// Return types are explicitly nullable

166

const type: string | null = mime.getType("file.js");

167

const ext: string | null = mime.getExtension("text/plain");

168

const exts: Set<string> | null = mime.getAllExtensions("image/png");

169

170

// Always check for null before use

171

if (type !== null) {

172

console.log(`MIME type: ${type}`);

173

}

174

```