sitegen/framework/lib/mime.ts

33 lines
1.1 KiB
TypeScript
Raw Permalink Normal View History

2025-06-21 16:04:57 -07:00
declare const MIME_INLINE_DATA: never;
const entries = typeof MIME_INLINE_DATA !== "undefined"
? MIME_INLINE_DATA
: fs.readFileSync(path.join(import.meta.dirname, "mime.txt"), "utf8")
.split("\n")
.map((line) => line.trim())
.filter((line) => line && !line.startsWith("#"))
.map((line) => line.split(/\s+/, 2) as [string, string]);
export const rawEntriesText = entries;
2025-06-08 12:38:25 -07:00
const extensions = new Map(entries.filter((x) => x[0].startsWith(".")));
const fullNames = new Map(entries.filter((x) => !x[0].startsWith(".")));
/**
* Accepts:
2025-06-08 12:38:25 -07:00
* - Full file path or basename
* - Extension (with or without dot)
*/
export function contentTypeFor(file: string) {
2025-06-08 12:38:25 -07:00
const slash = file.indexOf("/");
if (slash !== -1) file = file.slice(slash + 1);
const dot = file.indexOf(".");
if (dot === -1) file = "." + file;
2025-06-08 12:38:25 -07:00
else if (dot > 0) {
let entry = fullNames.get(file);
if (entry) return entry;
file = file.slice(dot);
}
return extensions.get(file) ?? "application/octet-stream";
}
2025-06-08 17:31:03 -07:00
import * as fs from "#sitegen/fs";
import * as path from "node:path";