Skip to content

Commit 11e31bc

Browse files
committed
refactor: group related modules into subpackages
1 parent e26ac73 commit 11e31bc

File tree

10 files changed

+75
-72
lines changed

10 files changed

+75
-72
lines changed

scripts/sign.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { sign } from "../src/crypto.ts";
1+
import { sign } from "../src/server/crypto.ts";
22

33
if (import.meta.main) {
44
const arg = Deno.args[0] ?? "";

src/webhook.ts renamed to src/discord/webhook.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import config from "./config.ts";
2-
import { getRequestLog } from "./context.ts";
3-
import { sleep } from "./util.ts";
1+
import config from "../config.ts";
2+
import { getRequestLog } from "../server/context.ts";
3+
import { sleep } from "../util.ts";
44

55
export async function sendWebhook(
66
id: string,

src/filter.ts renamed to src/filter/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { WebhookEvent, WebhookEventMap, WebhookEventName } from "@octokit/webhooks-types";
22

3-
import { getRequestLog } from "./context.ts";
43
import { getAndIncrementKV } from "./kv.ts";
5-
import { UrlConfig } from "./types.d.ts";
6-
import { wildcardMatch } from "./util.ts";
4+
import { getRequestLog } from "../server/context.ts";
5+
import { UrlConfig } from "../types.d.ts";
6+
import { wildcardMatch } from "../util.ts";
77

88
const COMMON_CI_BOTS = ["coveralls[bot]", "netlify[bot]", "pre-commit-ci[bot]", "dependabot[bot]"];
99

src/kv.ts renamed to src/filter/kv.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import config from "./config.ts";
2-
import type { RequestLog } from "./util.ts";
1+
import config from "../config.ts";
2+
import type { RequestLog } from "../util.ts";
33

44
const KEY_EXPIRY = 3; // seconds
55
const MAX_RETRIES = 50;

src/handler.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { HTTPException } from "@hono/hono/http-exception";
22
import { WebhookEvent } from "@octokit/webhooks-types";
33

4-
import { getRequestLog } from "./context.ts";
5-
import filterWebhook from "./filter.ts";
6-
import fixupEmbeds from "./formatter.ts";
4+
import fixupEmbeds from "./discord/formatter.ts";
5+
import { sendWebhook } from "./discord/webhook.ts";
6+
import filterWebhook from "./filter/index.ts";
7+
import { getRequestLog } from "./server/context.ts";
78
import { UrlConfig } from "./types.d.ts";
89
import { parseBool } from "./util.ts";
9-
import { sendWebhook } from "./webhook.ts";
1010

1111
export default async function handle(
1212
json: Record<string, any>,

src/main.ts

Lines changed: 1 addition & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,5 @@
1-
import { Hono } from "@hono/hono";
2-
import { HTTPException } from "@hono/hono/http-exception";
3-
import { logger } from "@hono/hono/logger";
4-
51
import config from "./config.ts";
6-
import { contextMiddleware, getRequestLog } from "./context.ts";
7-
import { hasKey, verify } from "./crypto.ts";
8-
import handler from "./handler.ts";
9-
import { setMetaHeader } from "./util.ts";
10-
11-
const app = new Hono();
12-
13-
app.use(contextMiddleware);
14-
15-
// use context's requestlog for server logs
16-
app.use(logger((...args) => getRequestLog().log(...args)));
17-
18-
// add deploy header to responses
19-
app.use(async (c, next) => {
20-
await next();
21-
setMetaHeader(c, "deploy", config.deployId);
22-
});
23-
24-
if (config.mainRedirect) {
25-
app.get("/", (c) => {
26-
return c.redirect(config.mainRedirect!);
27-
});
28-
}
29-
30-
app.post("/:id/:token", async (c) => {
31-
const { id, token } = c.req.param();
32-
33-
// verify signature
34-
if (hasKey) {
35-
const signature = c.req.query("sig");
36-
if (!signature || !(await verify(`${id}/${token}`, signature))) {
37-
throw new HTTPException(403);
38-
}
39-
}
40-
41-
const data = await c.req.json();
42-
let [res, meta] = await handler(data, c.req.header(), c.req.query(), id, token);
43-
44-
// clone response to make headers mutable
45-
res = new Response(res.body, res);
46-
47-
// set metadata headers
48-
for (const [key, value] of Object.entries(meta)) {
49-
setMetaHeader(c, key, value);
50-
}
51-
52-
// remove other headers that don't make sense here
53-
for (const header of ["set-cookie", "alt-svc"]) {
54-
res.headers.delete(header);
55-
}
56-
57-
return res;
58-
});
2+
import app from "./server/index.ts";
593

604
if (import.meta.main) {
615
if (config.signKey) {

src/context.ts renamed to src/server/context.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { createMiddleware } from "@hono/hono/factory";
22
import { AsyncLocalStorage } from "node:async_hooks";
33

4-
import { RequestLog, requestLog } from "./util.ts";
4+
import { RequestLog, requestLog } from "../util.ts";
55

66
interface ContextData {
77
requestLog: RequestLog;

src/crypto.ts renamed to src/server/crypto.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { decodeHex, encodeHex } from "@std/encoding";
22

3-
import config from "./config.ts";
3+
import config from "../config.ts";
44

55
export const hasKey = !!config.signKey;
66

src/server/index.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { Hono } from "@hono/hono";
2+
import { HTTPException } from "@hono/hono/http-exception";
3+
import { logger } from "@hono/hono/logger";
4+
5+
import config from "../config.ts";
6+
import { contextMiddleware, getRequestLog } from "./context.ts";
7+
import { hasKey, verify } from "./crypto.ts";
8+
import handler from "../handler.ts";
9+
import { setMetaHeader } from "../util.ts";
10+
11+
const app = new Hono();
12+
export default app;
13+
14+
app.use(contextMiddleware);
15+
16+
// use context's requestlog for server logs
17+
app.use(logger((...args) => getRequestLog().log(...args)));
18+
19+
// add deploy header to responses
20+
app.use(async (c, next) => {
21+
await next();
22+
setMetaHeader(c, "deploy", config.deployId);
23+
});
24+
25+
if (config.mainRedirect) {
26+
app.get("/", (c) => {
27+
return c.redirect(config.mainRedirect!);
28+
});
29+
}
30+
31+
app.post("/:id/:token", async (c) => {
32+
const { id, token } = c.req.param();
33+
34+
// verify signature
35+
if (hasKey) {
36+
const signature = c.req.query("sig");
37+
if (!signature || !(await verify(`${id}/${token}`, signature))) {
38+
throw new HTTPException(403);
39+
}
40+
}
41+
42+
const data = await c.req.json();
43+
let [res, meta] = await handler(data, c.req.header(), c.req.query(), id, token);
44+
45+
// clone response to make headers mutable
46+
res = new Response(res.body, res);
47+
48+
// set metadata headers
49+
for (const [key, value] of Object.entries(meta)) {
50+
setMetaHeader(c, key, value);
51+
}
52+
53+
// remove other headers that don't make sense here
54+
for (const header of ["set-cookie", "alt-svc"]) {
55+
res.headers.delete(header);
56+
}
57+
58+
return res;
59+
});

0 commit comments

Comments
 (0)