Skip to content

Commit 3d9ab44

Browse files
committed
feat: add WORKER_PUBLIC_ORIGIN for WSS terminal connections
- Introduced WORKER_PUBLIC_ORIGIN constant for browser WSS terminal URL. - Updated resolveTerminalConnection to utilize WORKER_PUBLIC_ORIGIN for production environments. - Enhanced type definitions to include WORKER_PUBLIC_ORIGIN in global app context. - Adjusted local development handling for terminal connections.
1 parent 29ab112 commit 3d9ab44

3 files changed

Lines changed: 29 additions & 10 deletions

File tree

alchemy.run.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ import { deployEnv } from './alchemy.env';
1313
const projectName = 'cloudshell';
1414
const workerName = `${projectName}-worker`;
1515

16+
/** Browser WSS terminal URL (must match worker HTTPS custom domain). */
17+
const WORKER_PUBLIC_ORIGIN = 'https://cloudshell-api.coey.dev';
18+
19+
const isLocalDevHostname =
20+
deployEnv.portForwardBaseDomain === 'localhost' ||
21+
deployEnv.portForwardBaseDomain === '127.0.0.1';
22+
1623
const project = await alchemy(projectName, {
1724
password: deployEnv.password,
1825
});
@@ -62,6 +69,11 @@ export const WORKER = await Worker(workerName, {
6269
PORT_FORWARD_BASE_DOMAIN: deployEnv.portForwardBaseDomain,
6370
},
6471
url: false,
72+
...(isLocalDevHostname
73+
? {}
74+
: {
75+
domains: [new URL(WORKER_PUBLIC_ORIGIN).hostname],
76+
}),
6577
});
6678

6779
export const APP = await SvelteKit(`${projectName}-app`, {
@@ -79,6 +91,7 @@ export const APP = await SvelteKit(`${projectName}-app`, {
7991
BETTER_AUTH_TRUSTED_ORIGINS: ['http://localhost:5173', deployEnv.betterAuthUrl].join(','),
8092
TERMINAL_TICKET_SECRET: deployEnv.terminalSecret,
8193
WORKER_DEV_ORIGIN: WORKER.url || 'http://localhost:1337',
94+
WORKER_PUBLIC_ORIGIN,
8295
},
8396
});
8497

src/app.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ declare global {
1818
BETTER_AUTH_TRUSTED_ORIGINS?: string;
1919
TERMINAL_TICKET_SECRET?: string;
2020
WORKER_DEV_ORIGIN?: string;
21+
/** Base URL for WSS terminal (worker custom domain), e.g. https://api.example.com */
22+
WORKER_PUBLIC_ORIGIN?: string;
2123
};
2224
}
2325
}

src/lib/server/worker.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,6 @@ export async function resolveTerminalConnection(
100100
sessionId: string,
101101
tabId: string
102102
): Promise<{ url: string; mode: 'proxy' | 'direct' }> {
103-
if (!dev) {
104-
const url = new URL('/ws/terminal', event.url.origin);
105-
url.protocol = url.protocol === 'https:' ? 'wss:' : 'ws:';
106-
url.searchParams.set('sessionId', sessionId);
107-
url.searchParams.set('tabId', tabId);
108-
return { url: url.toString(), mode: 'proxy' };
109-
}
110-
111103
const identity = getAuthenticatedIdentity(event);
112104
const secret =
113105
event.platform?.env?.TERMINAL_TICKET_SECRET || event.platform?.env?.BETTER_AUTH_SECRET;
@@ -127,8 +119,20 @@ export async function resolveTerminalConnection(
127119
secret
128120
);
129121

130-
const url = new URL('/ws/terminal', getWorkerOrigin(event));
131-
url.protocol = url.protocol === 'https:' ? 'wss:' : 'ws:';
122+
if (dev) {
123+
const url = new URL('/ws/terminal', getWorkerOrigin(event));
124+
url.protocol = url.protocol === 'https:' ? 'wss:' : 'ws:';
125+
url.searchParams.set('ticket', ticket);
126+
return { url: url.toString(), mode: 'direct' };
127+
}
128+
129+
const publicOrigin = event.platform?.env?.WORKER_PUBLIC_ORIGIN?.replace(/\/$/, '');
130+
if (!publicOrigin) {
131+
throw error(500, 'WORKER_PUBLIC_ORIGIN is not configured');
132+
}
133+
134+
const url = new URL('/ws/terminal', publicOrigin);
135+
url.protocol = 'wss:';
132136
url.searchParams.set('ticket', ticket);
133137

134138
return { url: url.toString(), mode: 'direct' };

0 commit comments

Comments
 (0)