Skip to content

Commit

Permalink
Simplify and comment shouldDiscoverRoute
Browse files Browse the repository at this point in the history
  • Loading branch information
timokoessler committed Feb 10, 2025
1 parent d51a88e commit 28fdeea
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 14 deletions.
15 changes: 15 additions & 0 deletions library/helpers/getFileExtension.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import * as t from "tap";
import { getFileExtension } from "./getFileExtension";

t.test("it works", async () => {
t.equal(getFileExtension("file.txt"), "txt");
t.equal(getFileExtension("file.tar.gz"), "gz");
t.equal(getFileExtension(".file.txt"), "txt");
t.equal(getFileExtension("font.woff2"), "woff2");
t.equal(getFileExtension("/path/to/file.txt"), "txt");

t.equal(getFileExtension("file"), "");
t.equal(getFileExtension("file."), "");
t.equal(getFileExtension("file.."), "");
t.equal(getFileExtension("file.txt."), "");
});
13 changes: 13 additions & 0 deletions library/helpers/getFileExtension.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { extname } from "path";

/**
* Get the file extension from a path / path segment without the dot
*/
export function getFileExtension(segment: string) {
const extension = extname(segment);
if (extension && extension.startsWith(".")) {
// Remove the dot from the extension
return extension.slice(1);
}
return extension;
}
27 changes: 27 additions & 0 deletions library/sources/http-server/shouldDiscoverRoute.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,3 +314,30 @@ t.test("it allows redirects", async () => {
true
);
});

t.test("it does not ignore normal routes", async () => {
t.same(
shouldDiscoverRoute({
statusCode: 200,
route: "/api/v1/users",
method: "GET",
}),
true
);
t.same(
shouldDiscoverRoute({
statusCode: 200,
route: "/api/v1/users/1",
method: "GET",
}),
true
);
t.same(
shouldDiscoverRoute({
statusCode: 204,
route: "/api/v1/users/1/friends",
method: "POST",
}),
true
);
});
32 changes: 18 additions & 14 deletions library/sources/http-server/shouldDiscoverRoute.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { extname } from "path";
import { getFileExtension } from "../../helpers/getFileExtension";

const EXCLUDED_METHODS = ["OPTIONS", "HEAD"];
const IGNORE_EXTENSIONS = ["properties", "php", "asp", "aspx", "jsp", "config"];
const IGNORE_EXTENSIONS = ["properties", "config"];
const IGNORE_STRINGS = ["cgi-bin"];

export function shouldDiscoverRoute({
Expand Down Expand Up @@ -34,23 +34,27 @@ export function shouldDiscoverRoute({
return false;
}

return segments.every(isAllowedExtension);
// Check for every file segment if it contains an file extension and if it should be discovered or ignored
return segments.every(shouldDiscoverExtension);
}

function isAllowedExtension(segment: string) {
let extension = extname(segment);
// Ignore routes which contain file extensions
function shouldDiscoverExtension(segment: string) {
const extension = getFileExtension(segment);

if (extension && extension.startsWith(".")) {
// Remove the dot from the extension
extension = extension.slice(1);
// No file extension, allow discovery
if (!extension) {
return true;
}

if (extension.length >= 2 && extension.length <= 5) {
return false;
}
// Do not discover files with extensions of 1 to 5 characters, e.g. file.css, file.js, file.woff2
if (extension.length > 1 && extension.length < 6) {
return false;
}

if (IGNORE_EXTENSIONS.includes(extension)) {
return false;
}
// Ignore some file extensions that are longer than 5 characters or shorter than 2 chars
if (IGNORE_EXTENSIONS.includes(extension)) {
return false;
}

return true;
Expand Down

0 comments on commit 28fdeea

Please sign in to comment.