remote.ts
This file
Path
apps/cli/src/remote.ts
131 lines · 4.0 KiB · TypeScript
Last changed by
History of this fileTake it
The walk stopped after 200 commits: lines older than 10c8b54 point at it.
Merge build/comments-cli-protocol-checks into mainoscabrielbefore adoptionc86204f
line 1why, one step back1// The token rides as the HTTP `authorization` header: Rpc headers would ride in the body, where the gate never looks.Merge branch 'build/906-platform-root-page'oscabrielbefore adoption10c8b54
lines 2–36why, one step back2import { Brand } from "@repo/brand";3import { Client, Contracts } from "@repo/protocol/contracts";4import { Authenticated, Unauthenticated } from "@repo/protocol/principal";5import {6 Config,7 Effect,8 Layer,9 Option,10 Predicate,11 Redacted,12 Schema,13} from "effect";14import {15 FetchHttpClient,16 HttpClient,17 HttpClientError,18 HttpClientRequest,19} from "effect/http";20import {21 RpcClient,22 RpcClientError,23 RpcMiddleware,24 RpcSerialization,25} from "effect/rpc";2627import { Credentials } from "./credentials.ts";2829const URL_VARIABLE = `${Brand.slug.toUpperCase()}_URL`;3031const isClientError = Schema.is(RpcClientError.RpcClientError);3233const isHttpFailure = Schema.is(HttpClientError.HttpClientErrorSchema);3435export const isTransportFailure = isClientError;36Merge build/comments-cli-protocol-checks into mainoscabrielbefore adoptionc86204f
line 37why, one step back37/** An HTTP failure's own message is empty: its cause says what failed. */Merge branch 'build/906-platform-root-page'oscabrielbefore adoption10c8b54
lines 38–49why, one step back38export const transportMessage = (failure: RpcClientError.RpcClientError) =>39 isHttpFailure(failure.reason) &&40 Predicate.hasProperty(failure.reason.cause, "message") &&41 Predicate.isString(failure.reason.cause.message)42 ? failure.reason.cause.message43 : failure.message;4445const isUnauthorized = (failure: RpcClientError.RpcClientError) =>46 Predicate.hasProperty(failure.reason, "cause") &&47 failure.reason.cause instanceof HttpClientError.StatusCodeError &&48 failure.reason.cause.response.status === 401;49Merge build/comments-cli-protocol-checks into mainoscabrielbefore adoptionc86204f
line 50why, one step back50/** A 401 from the platform's gate is the same `Unauthenticated` a contract gives a call without a token. */Merge branch 'build/906-platform-root-page'oscabrielbefore adoption10c8b54
lines 51–59why, one step back51const SignedIn = RpcMiddleware.layerClient(Authenticated, ({ next, request }) =>52 next(request).pipe(53 Effect.catchIf(54 (failure) => isClientError(failure) && isUnauthorized(failure),55 () => Effect.fail(new Unauthenticated())56 )57 )58);5960// The token is read for each request, not once: `mcp` serves for hours, and61// the stored token is renewed under it as it nears its lapse.62const over = (63 origin: string,64 token: Effect.Effect<Option.Option<Redacted.Redacted>>65) =>Merge branch 'build/906-platform-root-page'oscabrielbefore adoption10c8b54
lines 66–77why, one step back66 Layer.effect(Client)(RpcClient.make(Contracts)).pipe(67 Layer.provide(SignedIn),68 Layer.provide(69 Layer.effect(RpcClient.Protocol)(70 Effect.flatMap(HttpClient.HttpClient, (client) =>71 RpcClient.makeProtocolHttp(72 client.pipe(73 HttpClient.mapRequest(74 HttpClientRequest.prependUrl(75 `${origin.replace(/\/+$/u, "")}/rpc`76 )77 ),78 HttpClient.mapRequestEffect((request) =>79 Effect.map(token, (bearer) =>80 Option.match(bearer, {81 onNone: () => request,82 onSome: (value) =>83 HttpClientRequest.bearerToken(84 request,85 Redacted.value(value)86 ),87 })88 )Merge branch 'build/906-platform-root-page'oscabrielbefore adoption10c8b54
lines 89–102why, one step back89 ),90 // Any status but 2xx fails as itself, so a 401 is told apart.91 HttpClient.filterStatusOk92 )93 )94 )95 )96 ),97 Layer.provide([FetchHttpClient.layer, RpcSerialization.layerJson])98 );99100export const RemoteClient: Layer.Layer<Client, never, Credentials> =101 Layer.unwrap(102 Effect.gen(function* () {103 const credentials = yield* Credentials;Merge branch 'build/906-platform-root-page'oscabrielbefore adoption10c8b54
line 104why, one step back104105 const profile = yield* credentials.current;106Merge branch 'build/906-platform-root-page'oscabrielbefore adoption10c8b54
line 107why, one step back107 if (Option.isSome(profile)) {108 const { url } = profile.value;109110 return over(111 url,112 Effect.map(credentials.current, (now) =>113 Option.map(114 Option.filter(now, (kept) => kept.url === url),115 ({ token }) => token116 )117 )118 );Merge branch 'build/906-platform-root-page'oscabrielbefore adoption10c8b54
lines 119–124why, one step back119 }120121 const url = yield* Config.option(122 Config.NonEmptyString(URL_VARIABLE)123 ).pipe(Effect.orElseSucceed(() => Option.none()));124Merge cli/default-platform into mainoscabrielbefore adoptionb9fc54b
lines 125–127why, one step back125 // With nothing naming a platform, the platform itself, as anyone.126 return over(127 Option.getOrElse(url, () => Brand.platformUrl),128 Effect.succeedNoneMerge cli/default-platform into mainoscabrielbefore adoptionb9fc54b
line 129why, one step back129 );Merge branch 'build/906-platform-root-page'oscabrielbefore adoption10c8b54
lines 130–131why, one step back130 })131 );