Skip to content

Commit 926a362

Browse files
committed
make it nice
1 parent b776d8c commit 926a362

File tree

4 files changed

+71
-19
lines changed

4 files changed

+71
-19
lines changed

client/src/index.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
1-
// Creates a new WebSocket connection to the specified URL.
2-
const socket = new WebSocket("ws://localhost:8787/tunnel");
1+
const [_, tunnelURL, targetURL] = process.argv;
2+
3+
function usage() {
4+
console.log("Usage: node client.js <tunnelURL> <targetURL>");
5+
console.log(
6+
"Example: node client.js ws://https://webhooks-proxy-tunnel.YOUR_ORG.workers.dev/tunnel http://localhost:3000"
7+
);
8+
}
9+
10+
if (!tunnelURL) {
11+
console.error("Please provide a tunnel URL.");
12+
process.exit(1);
13+
}
14+
if (!targetURL) {
15+
console.error("Please provide a target URL.");
16+
process.exit(1);
17+
}
18+
19+
const socket = new WebSocket(tunnelURL);
320

421
// Executes when the connection is successfully established.
522
socket.addEventListener("open", (event) => {

worker/public/pico.min.css

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
File renamed without changes.

worker/src/index.ts

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ export class MyDurableObject extends DurableObject {
118118
}
119119
}
120120

121-
const DO_ID = "foo";
121+
const DO_NAME = "foo";
122122

123123
export default {
124124
/**
@@ -144,37 +144,33 @@ export default {
144144
// Create a `DurableObjectId` for an instance of the `MyDurableObject`
145145
// class named "foo". Requests from all Workers to the instance named
146146
// "foo" will go to a single globally unique Durable Object instance.
147-
const id = env.MY_DURABLE_OBJECT.idFromName(DO_ID);
147+
const id = env.MY_DURABLE_OBJECT.idFromName(DO_NAME);
148148

149149
// Create a stub to open a communication channel with the Durable
150150
// Object instance.
151151
const stub = env.MY_DURABLE_OBJECT.get(id);
152152

153153
return stub.fetch(request);
154154
} else if (url.pathname.startsWith("/proxy/")) {
155-
const id = env.MY_DURABLE_OBJECT.idFromName(DO_ID);
155+
const id = env.MY_DURABLE_OBJECT.idFromName(DO_NAME);
156156

157157
// Create a stub to open a communication channel with the Durable
158158
// Object instance.
159159
const stub = env.MY_DURABLE_OBJECT.get(id);
160160
return stub.proxy(request);
161-
} else if (url.pathname.startsWith("/close")) {
162-
const id = env.MY_DURABLE_OBJECT.idFromName(DO_ID);
163-
164-
// Create a stub to open a communication channel with the Durable
165-
// Object instance.
161+
} else if (url.pathname == "/close") {
162+
if (request.method !== "POST") {
163+
return new Response("Method not allowed", { status: 405 });
164+
}
165+
const id = env.MY_DURABLE_OBJECT.idFromName(DO_NAME);
166166
const stub = env.MY_DURABLE_OBJECT.get(id);
167167
return stub.close();
168-
} else {
169-
return new Response(
170-
"Websocket endpoint is listenning on <code>/tunnel</code>",
171-
{
172-
headers: {
173-
"Content-Type": "text/html",
174-
},
175-
},
176-
);
168+
} else if (url.pathname == "/") {
169+
return new Response(indexPage(url.origin), {
170+
headers: { "content-type": "text/html" },
171+
});
177172
}
173+
return new Response("Not found", { status: 404 });
178174
},
179175
} satisfies ExportedHandler<Env>;
180176

@@ -191,3 +187,38 @@ function withTimeout<T>(promise: Promise<T>, ms: number): Promise<T> {
191187
});
192188
});
193189
}
190+
191+
function indexPage(root: string): string {
192+
return `
193+
<!doctype html>
194+
<html lang="en">
195+
<head>
196+
<meta charset="UTF-8" />
197+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
198+
<title>Hello, World!</title>
199+
<link
200+
rel="stylesheet"
201+
href="/pico.min.css"
202+
>
203+
</head>
204+
<body>
205+
<main class="container">
206+
<h1>Webhooks Proxy Tunnel</h1>
207+
<p>Tunnel URL: <code>${root}/tunnel</code></p>
208+
<p>Proxy URL: <code>${root}/proxy/</code></p>
209+
<p>Force <button id="close" class="outline">close</button> the tunnel.</p>
210+
</main>
211+
<script>
212+
async function closeTunnel() {
213+
const response = await fetch("/close", {
214+
method: "POST",
215+
body: "",
216+
})
217+
218+
console.log(response);
219+
}
220+
document.querySelector("#close").addEventListener("click", closeTunnel);
221+
</script>
222+
</body>
223+
</html>`;
224+
}

0 commit comments

Comments
 (0)