All files / media_types / _db.ts

100.00% Branches 13/13
100.00% Lines 29/29
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
 
x14
 
 
 
 
 
x14
 
 
x14
 
 
x84
 
x14
x34258
x34258
 
x34258
x54600
x54600
 
 
x48160
 
x34258
x51870
x51870
x52570
x52570
 
x52570
x52570
x52570
x52570
 
x52570
x52570
x52948
x52948
x52570
 
x69104
x69104
x48160
 
x14














































// Copyright 2018-2025 the Deno authors. MIT license.
import db from "./vendor/db.ts";
import type { DBEntry } from "./_util.ts";

export type KeyOfDb = keyof typeof db;

/** A map of the media type for a given extension */
export const types = new Map<string, KeyOfDb>();

/** A map of extensions for a given media type. */
const extensions: Map<string, string[]> = new Map();

/** Internal function to populate the maps based on the Mime DB. */
const preference = ["nginx", "apache", undefined, "iana"];

for (const type of Object.keys(db) as KeyOfDb[]) {
  const mime = db[type] as DBEntry;
  const exts = mime.extensions;

  if (!exts || !exts.length) {
    continue;
  }

  // @ts-ignore Work around https://github.com/denoland/dnt/issues/148
  extensions.set(type, exts);

  for (const ext of exts) {
    const current = types.get(ext);
    if (current) {
      const from = preference.indexOf((db[current] as DBEntry).source);
      const to = preference.indexOf(mime.source);

      if (
        current !== "application/octet-stream" &&
        current !== "application/mp4" &&
        (from > to ||
          // @ts-ignore work around https://github.com/denoland/dnt/issues/148
          (from === to && current.startsWith("application/")))
      ) {
        continue;
      }
    }

    types.set(ext, type);
  }
}

export { db, extensions };