The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 1 | /** |
| 2 | * DNS-01 solvers: how the ACME flow publishes `_acme-challenge` TXT |
| 3 | * records. |
| 4 | * |
| 5 | * A solver has two forms: |
| 6 | * |
| 7 | * - a **descriptor** — plain, serializable data in a resource's props |
| 8 | * (`{ type: "Cloudflare.DNS", zoneId }`) that survives state and the RPC |
| 9 | * boundary. Each DNS solver has a constructor |
| 10 | * (`Cloudflare.ACME.ZoneSolver(zone)`) and a layer registering the |
| 11 | * matching implementation, so the ACME provider never imports a DNS SDK |
| 12 | * itself. |
| 13 | * - a **runtime solver** — the {@link DnsSolver} interface a running |
| 14 | * Worker or Service hands to `IssueCertificate.issue`, built from a |
| 15 | * runtime DNS write client (`Cloudflare.ACME.zoneSolver(dns)`). |
| 16 | */ |
| 17 | import * as Context from "effect/Context"; |
| 18 | import * as Effect from "effect/Effect"; |
| 19 | import type * as HttpClient from "effect/http/HttpClient"; |
| 20 | import * as Layer from "effect/Layer"; |
| 21 | import * as Option from "effect/Option"; |
| 22 | import type { PropagationOptions } from "./Dns.ts"; |
| 23 | import { |
| 24 | DnsSolverNotRegistered, |
| 25 | type DnsPropagationTimeout, |
| 26 | type DnsSolverError, |
| 27 | } from "./Errors.ts"; |
| 28 | |
| 29 | /** A challenge record to publish: the `_acme-challenge.<name>` FQDN and its TXT value. */ |
| 30 | export interface DnsChallengeRecord { |
| 31 | readonly fqdn: string; |
| 32 | readonly value: string; |
| 33 | } |
| 34 | |
| 35 | /** Runtime DNS-01 solver. `R` is whatever context the underlying client needs. */ |
| 36 | export interface DnsSolver<R = never> { |
| 37 | /** Publish `value` as a TXT record at `fqdn`. Idempotent. */ |
| 38 | readonly present: (record: DnsChallengeRecord) => Effect.Effect<void, DnsSolverError, R>; |
| 39 | /** Remove the record published by `present`. Idempotent; runs in a finalizer. */ |
| 40 | readonly cleanup: (record: DnsChallengeRecord) => Effect.Effect<void, DnsSolverError, R>; |
| 41 | /** |
| 42 | * Wait until the record is visible. Defaults to polling public DNS over |
| 43 | * HTTPS; a solver for a private DNS (tests, split-horizon zones) overrides it. |
| 44 | */ |
| 45 | readonly propagated?: |
| 46 | | (( |
| 47 | record: DnsChallengeRecord, |
| 48 | options: PropagationOptions, |
| 49 | ) => Effect.Effect<void, DnsSolverError | DnsPropagationTimeout, R | HttpClient.HttpClient>) |
| 50 | | undefined; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Serializable solver description stored in `ACME.Certificate` props. |
| 55 | * `type` selects the registered implementation; the rest is provider data. |
| 56 | */ |
| 57 | export interface DnsSolverDescriptor { |
| 58 | readonly type: string; |
| 59 | readonly [key: string]: unknown; |
| 60 | } |
| 61 | |
| 62 | /** Builds a runtime solver from a descriptor at reconcile time. */ |
| 63 | export type DnsSolverFactory = ( |
| 64 | descriptor: DnsSolverDescriptor, |
| 65 | ) => Effect.Effect<DnsSolver, DnsSolverError>; |
| 66 | |
| 67 | const solverService = (type: string) => |
| 68 | Context.Service<DnsSolverFactory>(`alchemy/ACME/DnsSolver/${type}`); |
| 69 | |
| 70 | /** Resolve the registered implementation for a descriptor. */ |
| 71 | export const resolveDnsSolver = ( |
| 72 | descriptor: DnsSolverDescriptor, |
| 73 | ): Effect.Effect<DnsSolver, DnsSolverNotRegistered | DnsSolverError> => |
| 74 | Effect.gen(function* () { |
| 75 | const factory = yield* Effect.serviceOption(solverService(descriptor.type)); |
| 76 | if (Option.isNone(factory)) { |
| 77 | return yield* new DnsSolverNotRegistered({ type: descriptor.type }); |
| 78 | } |
| 79 | return yield* factory.value(descriptor); |
| 80 | }); |
| 81 | |
| 82 | /** A solver whose methods run with `context` provided (drops `R`). */ |
| 83 | export const provideSolver = <R>(solver: DnsSolver<R>, context: Context.Context<R>): DnsSolver => ({ |
| 84 | present: (record) => solver.present(record).pipe(Effect.provideContext(context)), |
| 85 | cleanup: (record) => solver.cleanup(record).pipe(Effect.provideContext(context)), |
| 86 | propagated: |
| 87 | solver.propagated === undefined |
| 88 | ? undefined |
| 89 | : (record, options) => |
| 90 | solver.propagated!(record, options).pipe(Effect.provideContext(context)), |
| 91 | }); |
| 92 | |
| 93 | /** |
| 94 | * Register a solver implementation from a `providers()` layer, capturing |
| 95 | * whatever services `R` the solver needs from that layer's own context so |
| 96 | * the ACME provider can run it without knowing about the DNS provider. |
| 97 | */ |
| 98 | export const dnsSolverLayer = <R>( |
| 99 | type: string, |
| 100 | make: (descriptor: DnsSolverDescriptor) => Effect.Effect<DnsSolver<R>, DnsSolverError>, |
| 101 | ): Layer.Layer<DnsSolverFactory, never, R> => |
| 102 | Layer.effect( |
| 103 | solverService(type), |
| 104 | Effect.gen(function* () { |
| 105 | const context = yield* Effect.context<R>(); |
| 106 | return (descriptor: DnsSolverDescriptor) => |
| 107 | make(descriptor).pipe(Effect.map((solver) => provideSolver(solver, context))); |
| 108 | }), |
| 109 | ); |