From 6b88be1b181eb1290e0bd3ca280896021dfe0468 Mon Sep 17 00:00:00 2001 From: Fabian Stamm Date: Wed, 21 Oct 2020 02:40:55 +0200 Subject: [PATCH] Add content type to registry using filenames --- registry/src/http/raw.ts | 3 +++ registry/src/utils.ts | 17 ++++++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/registry/src/http/raw.ts b/registry/src/http/raw.ts index a9675f1..7835b5c 100644 --- a/registry/src/http/raw.ts +++ b/registry/src/http/raw.ts @@ -4,6 +4,7 @@ import { extractPackagePath, getAbsolutePackageVersion, getFile, + getContentType, } from "../utils.ts"; import db from "../db.ts"; @@ -54,6 +55,8 @@ export default function raw(g: ABC.Group) { if (!result) return E404(); ctx.response.headers.set("e-tag", result.etag); + let contentType = getContentType(filepath); + if (contentType) ctx.response.headers.set("Content-Type", contentType); return result.data; }); } diff --git a/registry/src/utils.ts b/registry/src/utils.ts index a0090b0..4bce07c 100644 --- a/registry/src/utils.ts +++ b/registry/src/utils.ts @@ -1,4 +1,4 @@ -import { ABC, Base64 } from "./deps.ts"; +import { ABC, Base64, Path } from "./deps.ts"; import config from "./config.ts"; const packageNameRegex = /^[@]?[a-zA-Z][\d\w\-\_]*$/g.compile(); @@ -156,3 +156,18 @@ export async function getFile( throw err; } } + +const exts = new Map(); + +exts.set(".js", "text/javascript"); +exts.set(".mjs", "text/javascript"); +exts.set(".json", "application/json"); +exts.set(".jsonld", "application/ld+json"); +exts.set(".css", "text/css"); +exts.set(".html", "text/html"); +exts.set(".htm", "text/html"); + +export function getContentType(filename: string) { + const ext = Path.extname(filename); + return exts.get(ext); +}