96 lines · 3.7 KB · TypeScript
RawDownload
import { bootstrapRemoteBearerSession } from "@t3tools/client-runtime/authorization";import { PRIMARY_LOCAL_ENVIRONMENT_ID } from "@t3tools/contracts";import * as Context from "effect/Context";import * as Effect from "effect/Effect";import * as Layer from "effect/Layer";import * as Option from "effect/Option";import * as Ref from "effect/Ref";import * as Schema from "effect/Schema";import * as Semaphore from "effect/Semaphore";import * as HttpClient from "effect/unstable/http/HttpClient"; import * as DesktopBackendPool from "./DesktopBackendPool.ts"; export class DesktopLocalEnvironmentAuthBackendNotConfiguredError extends Schema.TaggedError<DesktopLocalEnvironmentAuthBackendNotConfiguredError>()(  "DesktopLocalEnvironmentAuthBackendNotConfiguredError",  {},) {  override get message(): string {    return "Local backend is not configured.";  }} export class DesktopLocalEnvironmentAuthSessionBootstrapError extends Schema.TaggedError<DesktopLocalEnvironmentAuthSessionBootstrapError>()(  "DesktopLocalEnvironmentAuthSessionBootstrapError",  { cause: Schema.Defect() },) {  override get message(): string {    return "Failed to create the local desktop bearer session.";  }} export const DesktopLocalEnvironmentAuthError = Schema.Union([  DesktopLocalEnvironmentAuthBackendNotConfiguredError,  DesktopLocalEnvironmentAuthSessionBootstrapError,]);export type DesktopLocalEnvironmentAuthError = typeof DesktopLocalEnvironmentAuthError.Type; export class DesktopLocalEnvironmentAuth extends Context.Service<  DesktopLocalEnvironmentAuth,  {    readonly getBearerToken: Effect.Effect<string, DesktopLocalEnvironmentAuthError>;  }>()("@t3tools/desktop/backend/DesktopLocalEnvironmentAuth") {} /** @public Service construction is part of the canonical Effect module API. */export const make = Effect.gen(function* () {  const pool = yield* DesktopBackendPool.DesktopBackendPool;  const httpClient = yield* HttpClient.HttpClient;  const tokenRef = yield* Ref.make(Option.none<string>());  const mutex = yield* Semaphore.make(1);   const getBearerToken = mutex    .withPermits(1)(      Effect.gen(function* () {        const cached = yield* Ref.get(tokenRef);        if (Option.isSome(cached)) {          return cached.value;        }         const instances = yield* pool.list;        const primary = instances.find((instance) => instance.id === PRIMARY_LOCAL_ENVIRONMENT_ID);        const configOption = primary === undefined ? Option.none() : yield* primary.currentConfig;        if (Option.isNone(configOption)) {          return yield* new DesktopLocalEnvironmentAuthBackendNotConfiguredError();        }        const config = configOption.value;        const credential = config.bootstrap.desktopBootstrapToken;        if (!credential) {          return yield* new DesktopLocalEnvironmentAuthBackendNotConfiguredError();        }        const session = yield* bootstrapRemoteBearerSession({          httpBaseUrl: config.httpBaseUrl.href,          credential,          clientMetadata: {            label: "T3 Code Desktop",            deviceType: "desktop",          },        }).pipe(          Effect.provideService(HttpClient.HttpClient, httpClient),          Effect.mapError(            (cause) =>              new DesktopLocalEnvironmentAuthSessionBootstrapError({                cause,              }),          ),        );        yield* Ref.set(tokenRef, Option.some(session.access_token));        return session.access_token;      }),    )    .pipe(Effect.withSpan("desktop.localEnvironmentAuth.getBearerToken"));   return DesktopLocalEnvironmentAuth.of({ getBearerToken });}); export const layer = Layer.effect(DesktopLocalEnvironmentAuth, make);