From 8e7c8adab85ab8f98b76aeed681cd6dbea185091 Mon Sep 17 00:00:00 2001 From: Tobias Fried Date: Wed, 18 Sep 2024 22:01:26 -0600 Subject: [PATCH] feat(app): add connection details headers (#335) --- app/rooms/[roomName]/PageClientImpl.tsx | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/app/rooms/[roomName]/PageClientImpl.tsx b/app/rooms/[roomName]/PageClientImpl.tsx index 1bf4761..465c50c 100644 --- a/app/rooms/[roomName]/PageClientImpl.tsx +++ b/app/rooms/[roomName]/PageClientImpl.tsx @@ -24,10 +24,21 @@ import { import { useRouter } from 'next/navigation'; import React from 'react'; +const DEFAULT_CONN_DETAILS_ENDPOINT = '/api/connection-details'; const CONN_DETAILS_ENDPOINT = - process.env.NEXT_PUBLIC_CONN_DETAILS_ENDPOINT ?? '/api/connection-details'; + process.env.NEXT_PUBLIC_CONN_DETAILS_ENDPOINT ?? DEFAULT_CONN_DETAILS_ENDPOINT; const SHOW_SETTINGS_MENU = process.env.NEXT_PUBLIC_SHOW_SETTINGS_MENU == 'true'; +function getConnectionDetailsHeaders(): HeadersInit | undefined { + const hostnameRegex = /^(?[a-z0-9][a-z0-9-]*[a-z0-9])(\.sandbox\.livekit\.io|\.sandbox\.staging\.livekit\.io)$/; + const match = window.location.hostname.match(hostnameRegex); + const sandbox_id = match?.groups?.sandbox_id; + if (!sandbox_id) { + return; + } + return { 'X-Sandbox-ID': sandbox_id }; +} + export function PageClientImpl(props: { roomName: string; region?: string; @@ -56,7 +67,10 @@ export function PageClientImpl(props: { if (props.region) { url.searchParams.append('region', props.region); } - const connectionDetailsResp = await fetch(url.toString()); + const connectionDetailsResp = await fetch( + url.toString(), + { headers: getConnectionDetailsHeaders() }, + ); const connectionDetailsData = await connectionDetailsResp.json(); setConnectionDetails(connectionDetailsData); }, []); @@ -126,9 +140,9 @@ function VideoConferenceComponent(props: { dynacast: true, e2ee: e2eeEnabled ? { - keyProvider, - worker, - } + keyProvider, + worker, + } : undefined, }; // @ts-ignore