Skip to content

Commit

Permalink
fix: move env to types, update packages, remove unused functions
Browse files Browse the repository at this point in the history
  • Loading branch information
whilefoo committed Jan 21, 2024
1 parent 7eef4f0 commit 06fc88c
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 35 deletions.
Binary file modified bun.lockb
Binary file not shown.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"smee-client": "^2.0.0"
},
"devDependencies": {
"@cloudflare/workers-types": "^4.20231121.0",
"@cloudflare/workers-types": "^4.20240117.0",
"@commitlint/cli": "^18.4.3",
"@commitlint/config-conventional": "^18.4.3",
"@types/node": "^20.11.1",
Expand All @@ -53,7 +53,7 @@
"prettier": "^3.1.0",
"tsx": "^4.6.2",
"typescript": "^5.0.4",
"wrangler": "^3.22.3"
"wrangler": "^3.23.0"
},
"lint-staged": {
"*.ts": [
Expand Down
4 changes: 4 additions & 0 deletions src/types/env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { Type as T, type Static } from "@sinclair/typebox";

export const envSchema = T.Object({ WEBHOOK_SECRET: T.String() });
export type Env = Static<typeof envSchema>;
29 changes: 0 additions & 29 deletions src/webhooks.ts

This file was deleted.

13 changes: 9 additions & 4 deletions src/worker.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { EmitterWebhookEventName as WebhookEventName, emitterEventNames } from "@octokit/webhooks";
import { Type as T, type Static } from "@sinclair/typebox";
import { Value } from "@sinclair/typebox/value";

import { EventHandler } from "./event-handler";
import { bindHandlers } from "./handlers";
const envSchema = T.Object({ WEBHOOK_SECRET: T.String() });
type Env = Static<typeof envSchema>;
import { envSchema, Env } from "./types/env";

export default {
async fetch(request: Request, env: Env): Promise<Response> {
if (!Value.Check(envSchema, env)) {
Expand All @@ -16,6 +14,7 @@ export default {
headers: { "content-type": "application/json" },
});
}

let pathname;
try {
pathname = new URL(request.url, "http://localhost").pathname;
Expand All @@ -25,39 +24,45 @@ export default {
headers: { "content-type": "application/json" },
});
}

if (pathname !== "/events" || request.method !== "POST") {
return new Response(JSON.stringify({ error: `Unknown route: ${request.method} ${request.url}` }), {
status: 400,
headers: { "content-type": "application/json" },
});
}

if (!request.headers.get("content-type") || !request.headers.get("content-type")?.startsWith("application/json")) {
return new Response(JSON.stringify({ error: `Unsupported "Content-Type" header value. Must be "application/json"` }), {
status: 415,
headers: { "content-type": "application/json" },
});
}

const eventName = request.headers.get("x-github-event");
if (!eventName) {
return new Response(JSON.stringify({ error: `Missing "x-github-event" header` }), {
status: 400,
headers: { "content-type": "application/json" },
});
}

const signatureSHA256 = request.headers.get("x-hub-signature-256");
if (!signatureSHA256) {
return new Response(JSON.stringify({ error: `Missing "x-hub-signature-256" header` }), {
status: 400,
headers: { "content-type": "application/json" },
});
}

const id = request.headers.get("x-github-delivery");
if (!id) {
return new Response(JSON.stringify({ error: `Missing "x-github-delivery" header` }), {
status: 400,
headers: { "content-type": "application/json" },
});
}

if (!emitterEventNames.includes(eventName as WebhookEventName)) {
return new Response(JSON.stringify({ error: `Unsupported "x-github-event" header value: ${eventName}` }), {
status: 400,
Expand Down

0 comments on commit 06fc88c

Please sign in to comment.