Skip to content
Myr

backend.ts

This file

Path

apps/web/src/server/backend.ts

65 lines · 2.1 KiB · TypeScript

Last changed by

Merge build/comments-web-rest into mainoscabrielbefore adoption0eb1248

History of this file

Take it

The walk stopped after 200 commits: lines older than 678c8b8 point at it.

Merge build/comments-web-rest into mainoscabrielbefore adoption0eb1248

lines 1–3why, one step back
1// The request travels at the platform's own URL, never the web's: the platform names git remotes from the origin it is handed.
2// The session cookie becomes `Authorization`; the browser's own cookies and any `authorization` it set never leave the web.3// A 401 (a credential the platform cannot verify) is asked again with no credential, and the answer expires the cookie.

Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8

lines 4–7why, one step back
4import { Effect, Option } from "effect";
56import type { Backend } from "./rpc.ts";7import {

Merge build/1213-platform-performance-and-agent-surfacesoscabrielbefore adoption1d75a0d

line 8why, one step back
8 clearedCookies,

Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8

lines 9–21why, one step back
9 credentialFrom,
10 settingCookies,11 withCredential,12} from "./session.ts";1314const onPlatform = (request: Request, platformUrl: string) => {15 const { pathname, search } = new URL(request.url);1617 return new Request(new URL(`${pathname}${search}`, platformUrl), request);18};1920const expiringSession = (response: Response) =>21 new Response(response.body, {

Merge build/1213-platform-performance-and-agent-surfacesoscabrielbefore adoption1d75a0d

line 22why, one step back
22 headers: settingCookies(response.headers, clearedCookies()),

Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8

lines 23–65why, one step back
23 status: response.status,
24 statusText: response.statusText,25 });2627const send = (env: Env, request: Request) =>28 Effect.promise(() => env.PLATFORM.fetch(request));2930const bodyOf = (request: Request) =>31 request.method === "GET" || request.method === "HEAD"32 ? Effect.succeed(null)33 : Effect.promise(() => request.arrayBuffer());3435export const backend: Backend = (request, env) =>36 Effect.runPromise(37 Effect.gen(function* () {38 const platform = onPlatform(request, env.PUBLIC_PLATFORM_URL);39 const credential = credentialFrom(request);4041 if (Option.isNone(credential)) {42 return yield* send(env, withCredential(platform, credential));43 }4445 const body = yield* bodyOf(platform);4647 const asked = (token: Option.Option<string>) =>48 withCredential(49 body === null50 ? platform51 : new Request(platform, { body, method: platform.method }),52 token53 );5455 const answer = yield* send(env, asked(credential));5657 if (answer.status !== 401) {58 return answer;59 }6061 yield* Effect.promise(() => answer.body?.cancel() ?? Promise.resolve());6263 return expiringSession(yield* send(env, asked(Option.none())));64 })65 );