(request: Request)
| 67 | }; |
| 68 | |
| 69 | async function handleRequest(request: Request): Promise<Response> { |
| 70 | const url = new URL(request.url); |
| 71 | |
| 72 | if (APPLE_APP_SITE_ASSOCIATION_PATHS.has(url.pathname)) { |
| 73 | return appleAppSiteAssociationResponse(); |
| 74 | } |
| 75 | |
| 76 | if (url.pathname === "/api/session") { |
| 77 | return sessionMetadata(request); |
| 78 | } |
| 79 | |
| 80 | if (url.pathname === "/api/session/auth" && request.method === "POST") { |
| 81 | return authenticateSession(request); |
| 82 | } |
| 83 | |
| 84 | const redirect = url.searchParams.get(REDIRECT_PARAM); |
| 85 | if (redirect) { |
| 86 | const session = parseEncodedSession(redirect); |
| 87 | const device = url.searchParams.get("device"); |
| 88 | if (device) { |
| 89 | session.device = device; |
| 90 | } |
| 91 | if (session.tokenCipher) { |
| 92 | return passwordPage(session, redirect, ""); |
| 93 | } |
| 94 | if (!session.token) { |
| 95 | return passwordPage( |
| 96 | session, |
| 97 | redirect, |
| 98 | "This session link is missing its SimDeck access token.", |
| 99 | ); |
| 100 | } |
| 101 | return establishSession(session, session.token); |
| 102 | } |
| 103 | |
| 104 | const cookieSession = sessionFromCookie(request); |
| 105 | if (!cookieSession) { |
| 106 | return landingPage(); |
| 107 | } |
| 108 | |
| 109 | if (sessionExpired(cookieSession.expiresAt)) { |
| 110 | return clearSessionResponse( |
| 111 | passwordShell( |
| 112 | "Session expired", |
| 113 | "<p>This SimDeck CI session has expired. Re-run the workflow to create a new link.</p>", |
| 114 | ), |
| 115 | 410, |
| 116 | ); |
| 117 | } |
| 118 | |
| 119 | return proxyToSimDeck(request, cookieSession); |
| 120 | } |
| 121 | |
| 122 | function appleAppSiteAssociationResponse(): Response { |
| 123 | return new Response(JSON.stringify(APPLE_APP_SITE_ASSOCIATION), { |
no test coverage detected