@devries-cameron20 / t3code Mirror · read-only
Fork 0

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, 07:15 UTC

main
ts 61 lines 2.4 KB
Raw
1 import * as acm from "@distilled.cloud/aws/acm";
2 import * as Data from "effect/Data";
3 import * as Effect from "effect/Effect";
4 import * as Schedule from "effect/Schedule";
5 import * as Stream from "effect/Stream";
6
7 class CertificateImportFailed extends Data.TaggedError("CertificateImportFailed")<{
8 readonly domainName: string;
9 }> {}
10
11 /**
12 * Find an existing imported test certificate by domain (a previous run may
13 * have left one behind) or import the checked-in self-signed fixture. Import
14 * without a CertificateArn mints a new certificate each call, so reuse keeps
15 * repeated runs deterministic and leak-free.
16 */
17 export const ensureImportedCert = Effect.fn(function* (
18 domainName: string,
19 certPem: string,
20 keyPem: string,
21 ) {
22 const pages = yield* acm.listCertificates.pages({ CertificateStatuses: ["ISSUED"] }).pipe(
23 Stream.runCollect,
24 Effect.map((chunk) => Array.from(chunk).flatMap((page) => page.CertificateSummaryList ?? [])),
25 );
26 const existing = pages.find((c) => c.DomainName === domainName);
27 if (existing?.CertificateArn) {
28 return existing.CertificateArn;
29 }
30 const encode = (pem: string) => Effect.sync(() => new TextEncoder().encode(pem));
31 const imported = yield* acm.importCertificate({
32 Certificate: yield* encode(certPem),
33 PrivateKey: yield* encode(keyPem),
34 });
35 if (!imported.CertificateArn) {
36 return yield* Effect.fail(new CertificateImportFailed({ domainName }));
37 }
38 return imported.CertificateArn;
39 });
40
41 /**
42 * Deleting right after the listener detaches can race eventual consistency;
43 * retry briefly, then leave the certificate behind (the next run's
44 * `ensureImportedCert` reclaims it by domain). Not-found is success
45 * (idempotent), still-in-use is the documented leave-for-reuse path, and any
46 * other error is a defect — so the error channel is `never` and this is a
47 * valid `Effect.ensuring` finalizer.
48 */
49 export const deleteCertBestEffort = Effect.fn(function* (certificateArn: string) {
50 yield* acm.deleteCertificate({ CertificateArn: certificateArn }).pipe(
51 Effect.retry({
52 while: (e) => e._tag === "ResourceInUseException",
53 schedule: Schedule.max([Schedule.spaced("3 seconds"), Schedule.recurs(10)]),
54 }),
55 Effect.catchTag("ResourceInUseException", () =>
56 Effect.logWarning(`certificate ${certificateArn} still in use; leaving it for reuse`),
57 ),
58 Effect.catchTag("ResourceNotFoundException", () => Effect.void),
59 Effect.orDie,
60 );
61 });