Skip to content

Commit

Permalink
feat: miniflare
Browse files Browse the repository at this point in the history
  • Loading branch information
Venoox committed Jan 4, 2024
1 parent c673016 commit cfdbab0
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 624 deletions.
48 changes: 24 additions & 24 deletions build/esbuild-build.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
import esbuild from "esbuild";
const typescriptEntries = ["static/main.ts"];
const typescriptEntries = ["src/worker.ts"];
// const cssEntries = ["static/style.css"];
const entries = [
...typescriptEntries,
// ...cssEntries
...typescriptEntries,
// ...cssEntries
];

export const esBuildContext: esbuild.BuildOptions = {
sourcemap: true,
entryPoints: entries,
bundle: true,
minify: false,
loader: {
".png": "dataurl",
".woff": "dataurl",
".woff2": "dataurl",
".eot": "dataurl",
".ttf": "dataurl",
".svg": "dataurl",
},
outdir: "dist",
sourcemap: true,
entryPoints: entries,
bundle: true,
minify: false,
loader: {
".png": "dataurl",
".woff": "dataurl",
".woff2": "dataurl",
".eot": "dataurl",
".ttf": "dataurl",
".svg": "dataurl",
},
outdir: "dist",
};

esbuild
.build(esBuildContext)
.then(() => {
console.log("\tesbuild complete");
})
.catch((err) => {
console.error(err);
process.exit(1);
});
.build(esBuildContext)
.then(() => {
console.log("\tesbuild complete");
})
.catch((err) => {
console.error(err);
process.exit(1);
});
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@
"@octokit/webhooks": "^12.0.10",
"create-cloudflare": "^2.8.3",
"dotenv": "^16.3.1",
"miniflare": "^3.20231030.4",
"smee-client": "^2.0.0"
},
"devDependencies": {
"@cloudflare/workers-types": "^4.20231121.0",
"@commitlint/cli": "^18.4.3",
"@commitlint/config-conventional": "^18.4.3",
"@types/node": "^20.10.5",
"@typescript-eslint/eslint-plugin": "^6.13.1",
"@typescript-eslint/parser": "^6.13.1",
"esbuild": "^0.19.10",
Expand All @@ -49,8 +49,7 @@
"npm-run-all": "^4.1.5",
"prettier": "^3.1.0",
"tsx": "^4.6.2",
"typescript": "^5.0.4",
"wrangler": "^3.0.0"
"typescript": "^5.0.4"
},
"lint-staged": {
"*.ts": [
Expand Down
20 changes: 0 additions & 20 deletions src/cloudflare-worker/work-in-progress-worker.ts

This file was deleted.

28 changes: 28 additions & 0 deletions src/local-server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import SmeeClient from "smee-client";
import { Miniflare } from "miniflare";

const path = "/events";

const mf = new Miniflare({
modules: true,
scriptPath: "./worker.js",
});

mf.ready
.then(async (url) => {
const env = await mf.getBindings();
if (env.WEBHOOK_PROXY_URL && typeof env.WEBHOOK_PROXY_URL === "string") {
url.pathname = path;
const smee = new SmeeClient({
source: env.WEBHOOK_PROXY_URL,
target: url.toString(),
logger: console,
});

smee.start();
}
console.log(`Listening on ${url}`);
})
.catch((err) => {
console.error(err);
});
30 changes: 0 additions & 30 deletions src/research/minimal-github-webhook-handler.ts

This file was deleted.

24 changes: 0 additions & 24 deletions src/research/smee-client.ts

This file was deleted.

File renamed without changes.
3 changes: 3 additions & 0 deletions src/webhooks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export async function handleEvent(event: unknown) {
console.trace(event);
}
18 changes: 16 additions & 2 deletions src/cloudflare-worker/index.ts → src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@
* Learn more at https://developers.cloudflare.com/workers/
*/

import { Webhooks, createNodeMiddleware } from "@octokit/webhooks";
import { handleEvent } from "./webhooks";
import { GitHubEvent } from "./types/github-events";

export interface Env {
WEBHOOK_SECRET: string;
// Example binding to KV. Learn more at https://developers.cloudflare.com/workers/runtime-apis/kv/
// MY_KV_NAMESPACE: KVNamespace;
//
Expand All @@ -26,7 +31,16 @@ export interface Env {
}

export default {
async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
return new Response('Hello World!');
async fetch(request: Request, env: Env): Promise<Response> {
const webhooks = new Webhooks({
secret: env.WEBHOOK_SECRET,
});

webhooks.on(Object.values(GitHubEvent), handleEvent);

const middleware = createNodeMiddleware(webhooks, { path: "/events" });
const response = new Response();
const hasResponse: boolean = await middleware(request, response);
return hasResponse ? response : new Response("Not found", { status: 404 });
},
};
Loading

0 comments on commit cfdbab0

Please sign in to comment.