Skip to content

Commit 9b79e58

Browse files
committed
secret can be undefined
1 parent 9e88224 commit 9b79e58

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

worker/src/index.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,8 @@ function getTunnelId(path: string): string {
162162
export default {
163163
async fetch(request, env, ctx): Promise<Response> {
164164
try {
165-
const isSecretSet = isValidSecret(env.WEBHOOKS_PROXY_TUNNEL_SECRET);
165+
const secret = env.WEBHOOKS_PROXY_TUNNEL_SECRET;
166+
const secretIsValid = isValidSecret(secret);
166167
const url = new URL(request.url);
167168
if (url.pathname.startsWith("/tunnel/")) {
168169
const tunnelId = getTunnelId(url.pathname);
@@ -171,15 +172,14 @@ export default {
171172
const stats = await stub.stats();
172173

173174
return new Response(
174-
tunnelPage(url.origin, tunnelId, stats, isSecretSet),
175+
tunnelPage(url.origin, tunnelId, stats, secretIsValid),
175176
{
176177
headers: { "content-type": "text/html" },
177178
},
178179
);
179180
} else if (url.pathname.startsWith("/connect/")) {
180181
const tunnelId = getTunnelId(url.pathname);
181-
if (isSecretSet) {
182-
const secret = env.WEBHOOKS_PROXY_TUNNEL_SECRET;
182+
if (secretIsValid) {
183183
const swspHeader = request.headers.get("Sec-WebSocket-Protocol");
184184
if (!swspHeader) {
185185
return new Response(
@@ -240,7 +240,7 @@ export default {
240240
} else if (url.pathname == "/") {
241241
return new Response(
242242
homePage({
243-
isSecretSet,
243+
isSecretSet: secretIsValid,
244244
}),
245245
{
246246
headers: { "content-type": "text/html" },
@@ -271,7 +271,7 @@ function withTimeout<T>(promise: Promise<T>, ms: number): Promise<T> {
271271
});
272272
}
273273

274-
function isValidSecret(secret: unknown): boolean {
274+
function isValidSecret(secret: unknown): secret is string {
275275
// A valid secret is a string of 40 characters (30 bytes in Base64).
276276
return typeof secret === "string" && secret.length === 40;
277277
}

worker/worker-configuration.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// Runtime types generated with [email protected] 2025-04-04
44
declare namespace Cloudflare {
55
interface Env {
6-
WEBHOOKS_PROXY_TUNNEL_SECRET: string;
6+
WEBHOOKS_PROXY_TUNNEL_SECRET: string | undefined;
77
MY_DURABLE_OBJECT: DurableObjectNamespace<import("./src/index").MyDurableObject>;
88
ASSETS: Fetcher;
99
}

0 commit comments

Comments
 (0)