diff options
author | Ryan Dahl <ry@tinyclouds.org> | 2020-04-03 12:11:52 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-03 12:11:52 -0400 |
commit | 13db64fbc6a8da68e5f6c1da59e058a1a3146054 (patch) | |
tree | b8f91e48196ed3c230162da8972f0560d3420050 /std/http | |
parent | e99374a0a304bc3bd11ee55274fb7f4bcf902da6 (diff) |
Remove /std/media_types (#4594)
Diffstat (limited to 'std/http')
-rwxr-xr-x | std/http/file_server.ts | 22 | ||||
-rw-r--r-- | std/http/file_server_test.ts | 5 |
2 files changed, 22 insertions, 5 deletions
diff --git a/std/http/file_server.ts b/std/http/file_server.ts index 4ddec7000..79f3e5081 100755 --- a/std/http/file_server.ts +++ b/std/http/file_server.ts @@ -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"; @@ -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. @@ -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); } diff --git a/std/http/file_server_test.ts b/std/http/file_server_test.ts index 3cb59e67f..404d133ae 100644 --- a/std/http/file_server_test.ts +++ b/std/http/file_server_test.ts @@ -39,8 +39,7 @@ test("file_server serveFile", async (): Promise<void> => { const res = await fetch("http://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") @@ -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(); }); |