Skip to content
This repository has been archived by the owner on Apr 19, 2024. It is now read-only.

Commit

Permalink
Merge pull request #40 from PHS-TSA/fix-content-type
Browse files Browse the repository at this point in the history
Fix `Content-Type`
  • Loading branch information
lishaduck authored Feb 5, 2024
2 parents 75ccdd4 + 39b821b commit e77cc99
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 20 deletions.
11 changes: 3 additions & 8 deletions server/server.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { assertEquals } from "$std/assert/assert_equals.ts";
import { assertStringIncludes } from "$std/assert/assert_string_includes.ts";
import { assertThrows } from "$std/assert/assert_throws.ts";
import { join } from "$std/path/join.ts";
import { respond } from "./server.ts";

Expand Down Expand Up @@ -65,13 +64,9 @@ Deno.test(
await res.body?.cancel();

assertEquals(res.status, 200);
assertThrows(() =>
assertStringIncludes(
res.headers.get("Content-Type") ?? "",
// Currently, the .pkg file isn't served with the correct content type.
// However, this isn't a big deal, so I'm ignoring the failure.
"application/octet-stream",
),
assertStringIncludes(
res.headers.get("Content-Type") ?? "",
"application/octet-stream",
);
},
);
24 changes: 12 additions & 12 deletions server/server.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { serveDir } from "$std/http/file_server.ts";

function serve(): void {
Deno.serve(async (req: Request): Promise<Response> => await respond(req));
}

const headers = [
"Cross-Origin-Opener-Policy: same-origin",
"Cross-Origin-Embedder-Policy: require-corp",
];

export async function respond(req: Request, dir = "build"): Promise<Response> {
return await serveDir(req, {
const path = new URL(req.url).pathname;
const res = await serveDir(req, {
fsRoot: dir,
headers: headers,
enableCors: true,
});

res.headers.set("Cross-Origin-Opener-Policy", "same-origin");
res.headers.set("Cross-Origin-Embedder-Policy", " require-corp");
if (path.endsWith(".pck")) {
res.headers.set("Content-Type", "application/octet-stream");
}

return res;
}

serve();
Deno.serve(async (req: Request): Promise<Response> => await respond(req));

0 comments on commit e77cc99

Please sign in to comment.