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 | import * as ACME from "@alchemy.run/acme"; |
| 2 | import * as Cloudflare from "@alchemy.run/cloudflare"; |
| 3 | import * as Cause from "effect/Cause"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import { HttpServerRequest } from "effect/http/HttpServerRequest"; |
| 6 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 7 | import * as Redacted from "effect/Redacted"; |
| 8 | import { Staging, Zone } from "./shared.ts"; |
| 9 | |
| 10 | /** |
| 11 | * Effect-native Worker that mints a certificate at runtime: the |
| 12 | * {@link ACME.IssueCertificate} binding signs as the staging account bound |
| 13 | * into the Worker, DNS-01 goes through {@link Cloudflare.DNS.WriteDns}. |
| 14 | */ |
| 15 | export default class AcmeIssueWorker extends Cloudflare.Worker<AcmeIssueWorker>()( |
| 16 | "AcmeIssueWorker", |
| 17 | { |
| 18 | main: import.meta.url, |
| 19 | }, |
| 20 | Effect.gen(function* () { |
| 21 | const acme = yield* ACME.IssueCertificate(Staging); |
| 22 | const dns = yield* Cloudflare.DNS.WriteDns(Zone); |
| 23 | const solver = Cloudflare.ACME.zoneSolver(dns); |
| 24 | |
| 25 | return { |
| 26 | fetch: Effect.gen(function* () { |
| 27 | const request = yield* HttpServerRequest; |
| 28 | const url = new URL(request.url, "http://worker"); |
| 29 | if (url.pathname !== "/issue") { |
| 30 | return yield* HttpServerResponse.json({ ok: true, version: 2 }); |
| 31 | } |
| 32 | const name = url.searchParams.get("name") ?? ""; |
| 33 | return yield* Effect.gen(function* () { |
| 34 | const issued = yield* acme.issue({ identifiers: [name], solver }); |
| 35 | const parsed = yield* ACME.parseCertificate(issued.certificate); |
| 36 | yield* acme.revoke({ |
| 37 | certificate: issued.certificate, |
| 38 | privateKey: issued.privateKey, |
| 39 | }); |
| 40 | return yield* HttpServerResponse.json({ |
| 41 | issuer: issued.issuer, |
| 42 | notAfter: issued.notAfter, |
| 43 | serial: issued.serial, |
| 44 | dnsNames: parsed.dnsNames, |
| 45 | hasKey: Redacted.value(issued.privateKey).includes("BEGIN PRIVATE KEY"), |
| 46 | chainLength: ACME.splitPemChain(issued.chain).length, |
| 47 | }); |
| 48 | }).pipe( |
| 49 | // Failures and defects alike come back as JSON so the test can |
| 50 | // show what went wrong inside the Worker. |
| 51 | Effect.catchCause((cause) => |
| 52 | HttpServerResponse.json({ error: Cause.pretty(cause) }, { status: 500 }), |
| 53 | ), |
| 54 | ); |
| 55 | }), |
| 56 | }; |
| 57 | }).pipe(Effect.provide(ACME.IssueCertificateHttp), Effect.provide(Cloudflare.DNS.WriteDnsHttp)), |
| 58 | ) {} |