Skip to content

Commit

Permalink
Replace the media_types dependency with new Deno std APIs.
Browse files Browse the repository at this point in the history
Fixes #5 .
  • Loading branch information
jaydenseric committed Jun 6, 2022
1 parent 37e5428 commit 0f7cb60
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 10 deletions.
3 changes: 3 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

### Patch

- Replaced the [`media_types`](https://deno.land/x/media_types) dependency with
new Deno `std` APIs, fixing
[#5](https://github.com/jaydenseric/ruck/issues/5).
- Fixed the test script not exiting with an error status when tests fail.
- Added a script for finding Ruck’s minimum compatible Deno version.
- Use a more specific Deno version for the setup Deno step in the GitHub Actions
Expand Down
1 change: 0 additions & 1 deletion importMap.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"imports": {
"free_port/": "https://deno.land/x/[email protected]/",
"graphql-react/": "https://unpkg.com/[email protected]/",
"media_types/": "https://deno.land/x/[email protected]/",
"puppeteer": "https://deno.land/x/[email protected]/mod.ts",
"react": "https://esm.sh/[email protected]?dev",
"react-dom": "https://esm.sh/[email protected]?dev",
Expand Down
9 changes: 5 additions & 4 deletions publicFileResponse.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// @ts-check

import { contentType, lookup } from "media_types/mod.ts";
import { Status, STATUS_TEXT } from "std/http/http_status.ts";
import { contentType } from "std/media_types/mod.ts";
import { extname } from "std/path/mod.ts";
import { readableStreamFromReader } from "std/streams/conversion.ts";

/**
Expand Down Expand Up @@ -62,10 +63,10 @@ export default async function publicFileResponse(
headers: new Headers(),
};

const mimeType = lookup(requestUrl.pathname);
const fileExtension = extname(fileUrl.pathname);

if (mimeType) {
const contentTypeHeader = contentType(mimeType);
if (fileExtension) {
const contentTypeHeader = contentType(fileExtension);

if (contentTypeHeader) {
responseInit.headers.set("content-type", contentTypeHeader);
Expand Down
42 changes: 39 additions & 3 deletions publicFileResponse.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,42 @@ Deno.test("`publicFileResponse` with a missing file.", async () => {
);
});

Deno.test("`publicFileResponse` with an extensionless file.", async () => {
const publicDir = new URL(
"./test/fixtures/publicFileResponse/public/",
import.meta.url,
);
const response = await publicFileResponse(
new Request("http://localhost/A"),
publicDir,
);

assert(response.headers instanceof Headers);
assertEquals(Array.from(response.headers.entries()), []);
assertStrictEquals(
await response.text(),
await Deno.readTextFile(new URL("./A", publicDir)),
);
});

Deno.test("`publicFileResponse` with an unknown file extension.", async () => {
const publicDir = new URL(
"./test/fixtures/publicFileResponse/public/",
import.meta.url,
);
const response = await publicFileResponse(
new Request("http://localhost/A.abcdefg"),
publicDir,
);

assert(response.headers instanceof Headers);
assertEquals(Array.from(response.headers.entries()), []);
assertStrictEquals(
await response.text(),
await Deno.readTextFile(new URL("./A.abcdefg", publicDir)),
);
});

Deno.test("`publicFileResponse` with a CSS file.", async () => {
const publicDir = new URL(
"./test/fixtures/publicFileResponse/public/",
Expand All @@ -109,7 +145,7 @@ Deno.test("`publicFileResponse` with a CSS file.", async () => {

assert(response.headers instanceof Headers);
assertEquals(Array.from(response.headers.entries()), [
["content-type", "text/css; charset=utf-8"],
["content-type", "text/css; charset=UTF-8"],
]);
assertStrictEquals(
await response.text(),
Expand All @@ -129,7 +165,7 @@ Deno.test("`publicFileResponse` with a JavaScript module.", async () => {

assert(response.headers instanceof Headers);
assertEquals(Array.from(response.headers.entries()), [
["content-type", "application/javascript; charset=utf-8"],
["content-type", "application/javascript; charset=UTF-8"],
]);
assertStrictEquals(
await response.text(),
Expand Down Expand Up @@ -181,7 +217,7 @@ Deno.test(
assert(response.headers instanceof Headers);
assertEquals(Array.from(response.headers.entries()), [
addedHeadersEntry,
["content-type", "application/javascript; charset=utf-8"],
["content-type", "application/javascript; charset=UTF-8"],
]);
assertStrictEquals(
await response.text(),
Expand Down
2 changes: 0 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ A Ruck project contains:
{
"imports": {
"graphql-react/": "https://unpkg.com/[email protected]/",
"media_types/": "https://deno.land/x/[email protected]/",
"react": "https://esm.sh/[email protected]?dev",
"react-dom/server": "https://esm.sh/[email protected]/server?dev",
"react-waterfall-render/": "https://unpkg.com/[email protected]/",
Expand All @@ -100,7 +99,6 @@ A Ruck project contains:
{
"imports": {
"graphql-react/": "https://unpkg.com/[email protected]/",
"media_types/": "https://deno.land/x/[email protected]/",
"react": "https://esm.sh/[email protected]",
"react-dom/server": "https://esm.sh/[email protected]/server",
"react-waterfall-render/": "https://unpkg.com/[email protected]/",
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/publicFileResponse/public/A
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
abc
1 change: 1 addition & 0 deletions test/fixtures/publicFileResponse/public/A.abcdefg
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
abc

0 comments on commit 0f7cb60

Please sign in to comment.