Skip to content

Commit

Permalink
refactor(redirect): Use @std/url and change to "master" (#4)
Browse files Browse the repository at this point in the history
- Because of the processing URL, not the directory path.
- Redirects to the default branch, not head hash.
  • Loading branch information
5ouma authored Jun 12, 2024
1 parent c2a7fb9 commit e1ea1aa
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 20 deletions.
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"@std/fmt": "jsr:@std/fmt@0.225.4",
"@std/http": "jsr:@std/http@0.224.4",
"@std/http/user-agent": "jsr:@std/http@~0.223/user-agent",
"@std/path": "jsr:@std/path@0.225.2"
"@std/url": "jsr:@std/url@0.224.1"
},
"deploy": {
"project": "reproxy",
Expand Down
20 changes: 14 additions & 6 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 10 additions & 12 deletions src/libs/redirect.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
import type { RouterContext } from "@oak/oak";
import { STATUS_CODE } from "@std/http/status";
import { UserAgent } from "@std/http/user-agent";
import { join } from "@std/path";
import { join } from "@std/url";
import { getRepository } from "./env.ts";

export function redirect<R extends string>(
ctx: RouterContext<R>,
userAgent: UserAgent,
ref: string = "HEAD"
ref: string = "master"
): void {
const repository = getRepository();
const url: string =
"https://" +
join(
"github.com",
repository.owner,
repository.name,
"blob",
ref,
repository.path
);
const url = join(
new URL("https://github.com"),
repository.owner,
repository.name,
"blob",
ref,
repository.path
);

if (userAgent?.browser.name) {
ctx.response.status = STATUS_CODE.PermanentRedirect;
Expand Down
2 changes: 1 addition & 1 deletion test/libs/redirect_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Deno.test("Redirect Detection", <R extends string>() => {
assertEquals(ctx.response.status, STATUS_CODE.PermanentRedirect);
assertEquals(
ctx.response.headers.get("location"),
`https://github.com/${testRepo.owner}/${testRepo.name}/blob/HEAD/${testRepo.path}`
`https://github.com/${testRepo.owner}/${testRepo.name}/blob/master/${testRepo.path}`
);
});

Expand Down

0 comments on commit e1ea1aa

Please sign in to comment.