Skip to content

Commit

Permalink
Remove /media_types (denoland/deno#4594)
Browse files Browse the repository at this point in the history
  • Loading branch information
ry authored and denobot committed Feb 1, 2021
1 parent ddd9b90 commit 36bf5ba
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 8,302 deletions.
22 changes: 20 additions & 2 deletions http/file_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
// https://github.com/indexzero/http-server/blob/master/test/http-server-test.js

const { args, stat, readdir, open, exit } = Deno;
import { contentType } from "../media_types/mod.ts";
import { posix, extname } from "../path/mod.ts";
import { listenAndServe, ServerRequest, Response } from "./server.ts";
import { parse } from "../flags/mod.ts";
Expand Down Expand Up @@ -41,6 +40,25 @@ const CORSEnabled = serverArgs.cors ? true : false;
const target = posix.resolve(serverArgs._[1] ?? "");
const addr = `0.0.0.0:${serverArgs.port ?? serverArgs.p ?? 4500}`;

const MEDIA_TYPES: Record<string, string> = {
".md": "text/markdown",
".html": "text/html",
".htm": "text/html",
".json": "application/json",
".map": "application/json",
".txt": "text/plain",
".ts": "application/typescript",
".tsx": "application/typescript",
".js": "application/javascript",
".jsx": "application/jsx",
".gz": "application/gzip",
};

/** Returns the content-type based on the extension of a path. */
function contentType(path: string): string | undefined {
return MEDIA_TYPES[extname(path)];
}

if (serverArgs.h ?? serverArgs.help) {
console.log(`Deno File Server
Serves a local directory in HTTP.
Expand Down Expand Up @@ -104,7 +122,7 @@ export async function serveFile(
const [file, fileInfo] = await Promise.all([open(filePath), stat(filePath)]);
const headers = new Headers();
headers.set("content-length", fileInfo.size.toString());
const contentTypeValue = contentType(extname(filePath));
const contentTypeValue = contentType(filePath);
if (contentTypeValue) {
headers.set("content-type", contentTypeValue);
}
Expand Down
5 changes: 2 additions & 3 deletions http/file_server_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ test("file_server serveFile", async (): Promise<void> => {
const res = await fetch("http:https://localhost:4500/README.md");
assert(res.headers.has("access-control-allow-origin"));
assert(res.headers.has("access-control-allow-headers"));
assert(res.headers.has("content-type"));
assert(res.headers.get("content-type")!.includes("charset=utf-8"));
assertEquals(res.headers.get("content-type"), "text/markdown");
const downloadedFile = await res.text();
const localFile = new TextDecoder().decode(
await Deno.readFile("README.md")
Expand Down Expand Up @@ -148,6 +147,6 @@ test("contentType", async () => {
const request = new ServerRequest();
const response = await serveFile(request, "http/testdata/hello.html");
const contentType = response.headers!.get("content-type");
assertEquals(contentType, "text/html; charset=utf-8");
assertEquals(contentType, "text/html");
(response.body as Deno.File).close();
});
89 changes: 0 additions & 89 deletions media_types/README.md

This file was deleted.

Loading

0 comments on commit 36bf5ba

Please sign in to comment.