Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
5ouma committed Jun 12, 2024
2 parents 93edb75 + 4cdb488 commit ad32909
Show file tree
Hide file tree
Showing 13 changed files with 116 additions and 91 deletions.
1 change: 1 addition & 0 deletions .github/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<br />
[![Test](https://img.shields.io/github/actions/workflow/status/5ouma/reproxy/test.yml?label=test&style=flat-square)](https://github.com/5ouma/reproxy/actions/workflows/test.yml)
[![pre-commit.ci status](https://results.pre-commit.ci/badge/github/5ouma/reproxy/main.svg?style=flat-square)](https://results.pre-commit.ci/latest/github/5ouma/reproxy/main)
[![codecov](https://codecov.io/github/5ouma/reproxy/graph/badge.svg?token=OQB55KXJIL)](https://codecov.io/github/5ouma/reproxy)

</div>

Expand Down
18 changes: 16 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,19 @@ jobs:
with:
deno-version: v1.x

- name: 🧪 Test Libraries
run: deno task test
- name: 🧪 Run Tests
run: deno task cov

- name: 🧹 Lint Check
run: deno lint

- name: 📝 Format Check
run: deno fmt --check

- name: 🔍 Type Check
run: deno check ./**/*.ts

- name: ☂️ Upload Coverage
uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
/.env
/coverage.lcov
/coverage
11 changes: 6 additions & 5 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@
"run": "deno run --env='.env' --allow-env='REPOSITORY_OWNER,REPOSITORY_NAME,REPOSITORY_PATH,GITHUB_TOKEN' --allow-net='0.0.0.0,api.github.com'",
"start": "deno task run ./src/server.ts",
"dev": "deno task run --watch ./src/server.ts",
"test": "deno test --allow-env='REPOSITORY_OWNER,REPOSITORY_NAME,REPOSITORY_PATH,GITHUB_TOKEN' --allow-net='api.github.com' --parallel --shuffle"
"test": "deno test --allow-env='REPOSITORY_OWNER,REPOSITORY_NAME,REPOSITORY_PATH,GITHUB_TOKEN' --allow-net='api.github.com' --parallel --shuffle",
"cov": "deno task test --coverage && deno coverage --lcov > coverage.lcov"
},
"imports": {
"@oak/oak": "jsr:@oak/oak@16.0.0",
"@oak/oak": "jsr:@oak/oak@16.1.0",
"@octokit/rest": "https://esm.sh/@octokit/[email protected]",
"@std/assert": "jsr:@std/[email protected]",
"@std/fmt": "jsr:@std/[email protected].3",
"@std/http": "jsr:@std/[email protected].3",
"@std/fmt": "jsr:@std/[email protected].4",
"@std/http": "jsr:@std/[email protected].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
115 changes: 60 additions & 55 deletions deno.lock

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

2 changes: 1 addition & 1 deletion src/libs/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { getRepository, githubToken } from "./env.ts";

export async function getContent<R extends string>(
ctx: RouterContext<R>,
ref: string | undefined = undefined
ref: string | undefined = undefined,
): Promise<void> {
const octokit = new Octokit({ auth: githubToken });
const repository = getRepository();
Expand Down
6 changes: 4 additions & 2 deletions src/libs/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ export function getRepository(): Repository {
path: Deno.env.get("REPOSITORY_PATH") ?? "",
};

for (const key in repository)
if (!repository[key as keyof Repository])
for (const key in repository) {
if (!repository[key as keyof Repository]) {
console.error(`🚨 missing: $REPOSITORY_${key.toUpperCase()}`);
}
}

return repository;
}
Expand Down
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
10 changes: 6 additions & 4 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ app.addEventListener(
"listen",
({ secure, hostname, port }: ApplicationListenEvent) => {
console.log(
`🔔 listening: ${yellow(
`${secure ? "https" : "http"}://${hostname ?? "localhost"}:${port}`
)}`
`🔔 listening: ${
yellow(
`${secure ? "https" : "http"}://${hostname ?? "localhost"}:${port}`,
)
}`,
);
}
},
);

await app.listen({ port: 8080 });
6 changes: 3 additions & 3 deletions test/libs/content_test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { testing, type RouterContext } from "@oak/oak";
import { type RouterContext, testing } from "@oak/oak";
import { assertEquals, assertStringIncludes } from "@std/assert";
import { STATUS_CODE } from "@std/http/status";
import { getContent } from "../../src/libs/content.ts";
import { testRepo, unknownRepo, testRef, exportRepo } from "../utils.ts";
import { exportRepo, testRef, testRepo, unknownRepo } from "../utils.ts";

Deno.test("Get Content", async <R extends string>() => {
const ctx: RouterContext<R> = testing.createMockContext();
Expand All @@ -28,6 +28,6 @@ Deno.test("Get Content (Not found)", async <R extends string>() => {
assertEquals(ctx.response.status, STATUS_CODE.NotFound);
assertStringIncludes(
ctx.response.body!.toString(),
`⚠️ ${STATUS_CODE.NotFound}:`
`⚠️ ${STATUS_CODE.NotFound}:`,
);
});
2 changes: 1 addition & 1 deletion test/libs/env_test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { assertEquals } from "@std/assert";
import { getRepository } from "../../src/libs/env.ts";
import { testRepo, exportRepo, clearRepo } from "../utils.ts";
import { clearRepo, exportRepo, testRepo } from "../utils.ts";

Deno.test("Get Repository Env", () => {
exportRepo(testRepo);
Expand Down
8 changes: 4 additions & 4 deletions test/libs/redirect_test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { testing, type RouterContext } from "@oak/oak";
import { type RouterContext, testing } from "@oak/oak";
import { assertEquals } from "@std/assert";
import { STATUS_CODE } from "@std/http/status";
import { UserAgent } from "@std/http/user-agent";
import { redirect } from "../../src/libs/redirect.ts";
import { testRepo, testRef, exportRepo } from "../utils.ts";
import { exportRepo, testRef, testRepo } from "../utils.ts";

Deno.test("Redirect Detection", <R extends string>() => {
const ctx: RouterContext<R> = testing.createMockContext();
Expand All @@ -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 All @@ -25,6 +25,6 @@ Deno.test("Redirect Detection (With ref)", <R extends string>() => {
assertEquals(ctx.response.status, STATUS_CODE.PermanentRedirect);
assertEquals(
ctx.response.headers.get("location"),
`https://github.com/${testRepo.owner}/${testRepo.name}/blob/${testRef}/${testRepo.path}`
`https://github.com/${testRepo.owner}/${testRepo.name}/blob/${testRef}/${testRepo.path}`,
);
});
Loading

0 comments on commit ad32909

Please sign in to comment.