Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 1 addition & 60 deletions apps/server/src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,71 +37,12 @@ import {
} from "./auth/http.ts";
import * as ServerEnvironment from "./environment/ServerEnvironment.ts";
import { browserApiCorsAllowedHeaders, browserApiCorsAllowedMethods } from "./httpCors.ts";
import { parkedPageHtml } from "./say-to-me/parked.ts";

const OTLP_TRACES_PROXY_PATH = "/api/observability/v1/traces";
const LOOPBACK_HOSTNAMES = new Set(["127.0.0.1", "::1", "localhost"]);
const DESKTOP_RENDERER_ORIGINS = ["t3code://app", "t3code-dev://app"];

function escapeHtml(value: string): string {
return value.replace(
/[&<>\"']/g,
(character) =>
({ "&": "&amp;", "<": "&lt;", ">": "&gt;", '\"': "&quot;", "'": "&#39;" })[character] ??
character,
);
}

function parkedPageField(label: string, value: string | null): string {
return value ? `<div><dt>${label}</dt><dd>${escapeHtml(value)}</dd></div>` : "";
}

function parkedPageHtml(url: URL): string {
const environmentId = url.searchParams.get("environmentId");
const threadId = url.searchParams.get("threadId");
const title = url.searchParams.get("title");
const project = url.searchParams.get("project");
const cwd = url.searchParams.get("cwd");
const branch = url.searchParams.get("branch");
const fields = [
parkedPageField("Title", title),
parkedPageField("Project", project),
parkedPageField("Directory", cwd),
parkedPageField("Branch", branch),
].join("");
const returnHref =
environmentId && threadId
? `/${encodeURIComponent(environmentId)}/${encodeURIComponent(threadId)}`
: null;

return `<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Session parked</title>
<style>
:root { color-scheme: light dark; font-family: system-ui, sans-serif; }
body { margin: 0; min-height: 100vh; display: grid; place-items: center; background: Canvas; color: CanvasText; }
main { width: min(36rem, calc(100% - 3rem)); text-align: center; }
img { width: 4rem; height: 4rem; border-radius: 1rem; box-shadow: 0 1px 4px #0002; }
p { font-size: 1.125rem; font-weight: 600; }
dl { margin: 1.5rem 0 0; text-align: left; color: GrayText; font-size: .875rem; }
dl div { display: flex; gap: .75rem; margin: .5rem 0; }
dt { width: 5rem; flex: none; color: CanvasText; font-weight: 600; }
dd { margin: 0; overflow-wrap: anywhere; }
</style>
</head>
<body>
<main>
<img src="/apple-touch-icon.png" alt="T3 Code">
<p>session parked</p>
${fields ? `<dl>${fields}</dl>` : ""}
${returnHref ? `<a href="${escapeHtml(returnHref)}">Return to session</a>` : ""}
</main>
</body>
</html>`;
}

export const browserApiCorsLayer = Layer.unwrap(
Effect.gen(function* () {
const config = yield* ServerConfig.ServerConfig;
Expand Down
61 changes: 61 additions & 0 deletions apps/server/src/say-to-me/parked.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
function escapeHtml(value: string): string {
return value.replace(
/[&<>\"']/g,
(character) =>
({ "&": "&amp;", "<": "&lt;", ">": "&gt;", '\"': "&quot;", "'": "&#39;" })[character] ??
character,
);
}

function parkedPageField(label: string, value: string | null): string {
return value ? `<div><dt>${label}</dt><dd>${escapeHtml(value)}</dd></div>` : "";
}

export function parkedPageHtml(url: URL): string {
const environmentId = url.searchParams.get("environmentId");
const threadId = url.searchParams.get("threadId");
const title = url.searchParams.get("title");
const project = url.searchParams.get("project");
const cwd = url.searchParams.get("cwd");
const branch = url.searchParams.get("branch");
const sessionId = threadId ? `say-to-me(t3_${threadId})` : null;
const fields = [
parkedPageField("Title", title),
parkedPageField("Session ID", sessionId),
parkedPageField("Project", project),
parkedPageField("Directory", cwd),
parkedPageField("Branch", branch),
].join("");
const returnHref =
environmentId && threadId
? `/${encodeURIComponent(environmentId)}/${encodeURIComponent(threadId)}`
: null;

return `<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Session parked</title>
<style>
:root { color-scheme: light dark; font-family: system-ui, sans-serif; }
body { margin: 0; min-height: 100vh; display: grid; place-items: center; background: Canvas; color: CanvasText; }
main { width: min(36rem, calc(100% - 3rem)); text-align: center; }
img { width: 4rem; height: 4rem; border-radius: 1rem; box-shadow: 0 1px 4px #0002; }
p { font-size: 1.125rem; font-weight: 600; }
dl { margin: 1.5rem 0 0; text-align: left; color: GrayText; font-size: .875rem; }
dl div { display: flex; gap: .75rem; margin: .5rem 0; }
dt { width: 5rem; flex: none; color: CanvasText; font-weight: 600; }
dd { margin: 0; overflow-wrap: anywhere; }
</style>
</head>
<body>
<main>
<img src="/apple-touch-icon.png" alt="T3 Code">
<p>session parked</p>
${fields ? `<dl>${fields}</dl>` : ""}
${returnHref ? `<a href="${escapeHtml(returnHref)}">Return to session</a>` : ""}
</main>
</body>
</html>`;
}