Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

deploymentConfig.ts

141 lines4.6 KB

1import type { RelayManagedEndpoint } from "@t3tools/contracts/relay";
2import * as Schema from "effect/Schema";
3​
4const DNS_LABEL_MAX_LENGTH = 63;
5const MANAGED_ENDPOINT_HASH_LENGTH = 16;
6const MANAGED_ENDPOINT_TUNNEL_PREFIX = "t3coderelay-managedendpoint";
7export const MANAGED_ENDPOINT_ZONE_OWNER_STAGE = "prod";
8​
9// Workers Logs bills per GB; keep a sample of console output and uncaught exceptions for
10// crashes that never export a span. Invocation logs stay off: they record each request URL
11// as received, and request URLs are recorded only by the relay's own spans, which apply
12// route redaction before export to Axiom.
13export const RELAY_WORKER_OBSERVABILITY = {
14 enabled: true,
15 logs: { enabled: true, invocationLogs: false, headSamplingRate: 0.01 },
16} as const;
17​
18export class RelayPublicDomainLabelTooLongError extends Schema.TaggedError<RelayPublicDomainLabelTooLongError>()(
19 "RelayPublicDomainLabelTooLongError",
20 {
21 stage: Schema.String,
22 label: Schema.String,
23 maxLength: Schema.Number,
24 },
25) {
26 override get message(): string {
27 return `Relay stage '${this.stage}' produces custom domain label '${this.label}' (${this.label.length} characters), exceeding the DNS label limit of ${this.maxLength}.`;
28 }
30​
31function normalizeZoneName(zoneName: string): string {
32 return zoneName
33 .trim()
34 .toLowerCase()
35 .replace(/^\.+|\.+$/g, "");
37​
38function isDnsName(name: string): boolean {
39 return (
40 name.length > 0 &&
41 name.length <= 253 &&
42 name
43 .split(".")
44 .every(
45 (label) =>
46 label.length > 0 &&
47 label.length <= DNS_LABEL_MAX_LENGTH &&
48 /^[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/u.test(label),
49 )
50 );
52​
53function stableSuffix(hash: string): string {
54 return hash.toLowerCase().slice(0, MANAGED_ENDPOINT_HASH_LENGTH);
56​
57function appendDnsSafeSuffix(prefix: string, suffix: string): string {
58 const truncatedPrefix = prefix
59 .slice(0, DNS_LABEL_MAX_LENGTH - suffix.length - 1)
60 .replace(/-+$/g, "");
61 return `${truncatedPrefix}-${suffix}`;
63​
64// Match Alchemy's post-stage name sanitization for domains and runtime-created resources.
65function relayStageSlug(stage: string): string {
66 return stage
67 .toLowerCase()
68 .replaceAll(/[^a-z0-9-]/g, "-")
69 .replaceAll(/-+/g, "-")
70 .replace(/^-+|-+$/g, "");
72​
73export function relayResourceNameForStage(name: string, stage: string): string {
74 return `${name}-${relayStageSlug(stage)}`;
76​
77export function relayOwnsManagedEndpointZone(stage: string): boolean {
78 return stage === MANAGED_ENDPOINT_ZONE_OWNER_STAGE;
80​
81export function relayPublicDomainForStage(stage: string, zoneName: string): string {
82 const stageSlug = relayStageSlug(stage);
83 const relayLabel = stage === "prod" ? "relay" : `relay-${stageSlug}`;
84 if (relayLabel.length > DNS_LABEL_MAX_LENGTH) {
85 throw new RelayPublicDomainLabelTooLongError({
86 stage,
87 label: relayLabel,
88 maxLength: DNS_LABEL_MAX_LENGTH,
89 });
90 }
91 return `${relayLabel}.${normalizeZoneName(zoneName)}`;
93​
94export function managedEndpointDigestInput(
95 stage: string,
96 userId: string,
97 environmentId: string,
98): string {
99 return `${stage}:${userId}:${environmentId}`;
101​
102export function managedEndpointHostname(stage: string, baseDomain: string, hash: string): string {
103 const label = appendDnsSafeSuffix(relayStageSlug(stage), stableSuffix(hash));
104 return `${label}.${normalizeZoneName(baseDomain)}`;
106​
107export function isManagedEndpointHostname(hostname: string, baseDomain: string): boolean {
108 const normalizedHostname = normalizeZoneName(hostname);
109 const normalizedBaseDomain = normalizeZoneName(baseDomain);
110 return (
111 hostname === normalizedHostname &&
112 isDnsName(normalizedHostname) &&
113 isDnsName(normalizedBaseDomain) &&
114 normalizedHostname.endsWith(`.${normalizedBaseDomain}`)
115 );
117​
118export function managedEndpointForHostname(hostname: string): RelayManagedEndpoint {
119 return {
120 httpBaseUrl: `https://${hostname}/`,
121 wsBaseUrl: `wss://${hostname}/ws`,
122 providerKind: "cloudflare_tunnel",
123 };
125​
126export function managedEndpointTunnelNamePrefix(stage: string): string {
127 return `${MANAGED_ENDPOINT_TUNNEL_PREFIX}-${relayStageSlug(stage)}-`;
129​
130export function managedEndpointTunnelName(stage: string, hash: string): string {
131 return `${managedEndpointTunnelNamePrefix(stage)}${stableSuffix(hash)}`;
133​
134/** Tunnel-name hash covers user and environment; public environment ids alone do not identify an authorized link. */
135export const MANAGED_ENDPOINT_KEY_PATTERN = new RegExp(
136 `^[0-9a-f]{${MANAGED_ENDPOINT_HASH_LENGTH}}$`,
137);
138​
139export function managedEndpointTunnelNameForKey(stage: string, endpointKey: string): string {
140 return `${managedEndpointTunnelNamePrefix(stage)}${endpointKey}`;