Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(media_types): move extensions utility #4358

Merged
merged 2 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 31 additions & 30 deletions media_types/_db.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,47 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import db from "./vendor/mime-db.v1.52.0.ts";
import { type DBEntry, extensions } from "./_util.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. */
(function populateMaps() {
const preference = ["nginx", "apache", undefined, "iana"];
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;
for (const type of Object.keys(db) as KeyOfDb[]) {
const mime = db[type] as DBEntry;
const exts = mime.extensions;

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

// @ts-ignore work around denoland/dnt#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" &&
(from > to ||
// @ts-ignore work around denoland/dnt#148
(from === to && current.startsWith("application/")))
) {
continue;
}
// @ts-ignore work around denoland/dnt#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" &&
(from > to ||
// @ts-ignore work around denoland/dnt#148
(from === to && current.startsWith("application/")))
) {
continue;
}

types.set(ext, type);
}

types.set(ext, type);
}
})();
}

export { db };
export { db, extensions };
3 changes: 0 additions & 3 deletions media_types/_util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ export interface DBEntry {
extensions?: string[];
}

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

export function consumeToken(v: string): [token: string, rest: string] {
const notPos = indexOf(v, isNotTokenChar);
if (notPos === -1) {
Expand Down
3 changes: 1 addition & 2 deletions media_types/extensions_by_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
// This module is browser compatible.

import { parseMediaType } from "./parse_media_type.ts";
import { extensions } from "./_util.ts";
import "./_db.ts";
import { extensions } from "./_db.ts";

export { extensions };

Expand Down
Loading