Skip to content
Myr

remote.ts

This file

Path

apps/cli/src/remote.ts

163 lines · 5.0 KiB · TypeScript

Last changed by

Merge build/comments-cli-protocol-checks into mainoscabrielbefore adoptionc86204f

History of this file

Take it

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

Merge build/comments-cli-protocol-checks into mainoscabrielbefore adoptionc86204f

line 1why, one step back
1// The token rides as the HTTP `authorization` header: Rpc headers would ride in the body, where the gate never looks.

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 2–48why, one step back
2import { 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`;3031export class NoPlatform extends Schema.TaggedError<NoPlatform>()("NoPlatform", {32 variable: Schema.String,33}) {34 override get message() {35 return `no platform to call: run ${Brand.cli} login, or set ${this.variable} to call one as anyone`;36 }37}3839const isClientError = Schema.is(RpcClientError.RpcClientError);4041const isDefect = Schema.is(RpcClientError.RpcClientDefect);4243const isNoPlatformError = Schema.is(NoPlatform);4445const isHttpFailure = Schema.is(HttpClientError.HttpClientErrorSchema);4647export const isTransportFailure = isClientError;48

Merge build/comments-cli-protocol-checks into mainoscabrielbefore adoptionc86204f

line 49why, one step back
49/** An HTTP failure's own message is empty: its cause says what failed. */

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 50–68why, one step back
50export const transportMessage = (failure: RpcClientError.RpcClientError) =>
51 isHttpFailure(failure.reason) &&52 Predicate.hasProperty(failure.reason.cause, "message") &&53 Predicate.isString(failure.reason.cause.message)54 ? failure.reason.cause.message55 : failure.message;5657export const noPlatformOf = (58 failure: RpcClientError.RpcClientError59): Option.Option<NoPlatform> =>60 isDefect(failure.reason) && isNoPlatformError(failure.reason.cause)61 ? Option.some(failure.reason.cause)62 : Option.none();6364const isUnauthorized = (failure: RpcClientError.RpcClientError) =>65 Predicate.hasProperty(failure.reason, "cause") &&66 failure.reason.cause instanceof HttpClientError.StatusCodeError &&67 failure.reason.cause.response.status === 401;68

Merge build/comments-cli-protocol-checks into mainoscabrielbefore adoptionc86204f

line 69why, one step back
69/** A 401 from the platform's gate is the same `Unauthenticated` a contract gives a call without a token. */

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 70–163why, one step back
70const SignedIn = RpcMiddleware.layerClient(Authenticated, ({ next, request }) =>
71 next(request).pipe(72 Effect.catchIf(73 (failure) => isClientError(failure) && isUnauthorized(failure),74 () => Effect.fail(new Unauthenticated())75 )76 )77);7879const over = (origin: string, token: Option.Option<Redacted.Redacted>) =>80 Layer.effect(Client)(RpcClient.make(Contracts)).pipe(81 Layer.provide(SignedIn),82 Layer.provide(83 Layer.effect(RpcClient.Protocol)(84 Effect.flatMap(HttpClient.HttpClient, (client) =>85 RpcClient.makeProtocolHttp(86 client.pipe(87 HttpClient.mapRequest(88 HttpClientRequest.prependUrl(89 `${origin.replace(/\/+$/u, "")}/rpc`90 )91 ),92 HttpClient.mapRequest((request) =>93 Option.match(token, {94 onNone: () => request,95 onSome: (bearer) =>96 HttpClientRequest.bearerToken(97 request,98 Redacted.value(bearer)99 ),100 })101 ),102 // Any status but 2xx fails as itself, so a 401 is told apart.103 HttpClient.filterStatusOk104 )105 )106 )107 )108 ),109 Layer.provide([FetchHttpClient.layer, RpcSerialization.layerJson])110 );111112const noPlatform = new NoPlatform({ variable: URL_VARIABLE });113114const nowhere = Layer.effect(Client)(RpcClient.make(Contracts)).pipe(115 Layer.provide(116 Layer.effect(RpcClient.Protocol)(117 Effect.flatMap(118 Effect.service(RpcSerialization.RpcSerialization),119 (serialization) =>120 RpcClient.Protocol.make(() =>121 Effect.succeed({122 codecFor: serialization.codecFor,123 send: () =>124 Effect.fail(125 new RpcClientError.RpcClientError({126 reason: new RpcClientError.RpcClientDefect({127 cause: noPlatform,128 message: noPlatform.message,129 }),130 })131 ),132 supportsAck: false,133 supportsTransferables: false,134 })135 )136 )137 )138 ),139 Layer.provide(RpcSerialization.layerJson)140);141142export const RemoteClient: Layer.Layer<Client, never, Credentials> =143 Layer.unwrap(144 Effect.gen(function* () {145 const profile = yield* Effect.flatMap(146 Effect.service(Credentials),147 (credentials) => credentials.current148 );149150 if (Option.isSome(profile)) {151 return over(profile.value.url, Option.some(profile.value.token));152 }153154 const url = yield* Config.option(155 Config.NonEmptyString(URL_VARIABLE)156 ).pipe(Effect.orElseSucceed(() => Option.none()));157158 return Option.match(url, {159 onNone: () => nowhere,160 onSome: (origin) => over(origin, Option.none()),161 });162 })163 );