Open History to see the last commit
1import * as Context from "effect/Context";
2import * as Effect from "effect/Effect";
3import * as Layer from "effect/Layer";
4import * as Result from "effect/Result";
5
6import * as RelayConfiguration from "../Config.ts";
7import {
8 MANAGED_ENDPOINT_KEY_PATTERN,
9 managedEndpointTunnelNameForKey,
10} from "../deploymentConfig.ts";
11import { validateManagedEndpoint } from "../environments/EnvironmentConnector.ts";
12import * as EnvironmentLinks from "../environments/EnvironmentLinks.ts";
13import * as ManagedEndpointAllocations from "../environments/ManagedEndpointAllocations.ts";
14import * as HookInbox from "./HookInbox.ts";
15
16export const endpointKeyForTunnelName = (namespace: string, tunnelName: string): string | null => {
17 const prefix = managedEndpointTunnelNameForKey(namespace, "");
18 const key = tunnelName.startsWith(prefix) ? tunnelName.slice(prefix.length) : "";
19 return MANAGED_ENDPOINT_KEY_PATTERN.test(key) ? key : null;
20};
21
22type PersistenceError =
23 | EnvironmentLinks.EnvironmentLinkEnvironmentLookupPersistenceError
24 | ManagedEndpointAllocations.ManagedEndpointAllocationPersistenceError;
25
26interface HookEndpoint {
27 readonly httpBaseUrl: string;
28 readonly environmentId: string;
29 readonly holdWhileOffline: boolean;
30}
31
32export class HeldHooks extends Context.Service<
33 HeldHooks,
34 {
35 /** Hash user and environment so endpoint keys cannot be claimed by another account. */
36 readonly resolveEndpoint: (
37 endpointKey: string,
38 ) => Effect.Effect<HookEndpoint | null, PersistenceError>;
39 readonly setHoldWhileOffline: (input: {
40 readonly environmentId: string;
41 readonly environmentPublicKey: string;
42 readonly holdWebhooksWhileOffline: boolean;
43 }) => Effect.Effect<void, PersistenceError | HookInbox.HookInboxError>;
44 readonly wake: (input: {
45 readonly environmentId: string;
46 readonly environmentPublicKey: string;
47 }) => Effect.Effect<boolean, PersistenceError | HookInbox.HookInboxError>;
48 }
49>()("t3code-relay/hooks/HeldHooks") {}
50
51const make = Effect.gen(function* () {
52 const links = yield* EnvironmentLinks.EnvironmentLinks;
53 const allocations = yield* ManagedEndpointAllocations.ManagedEndpointAllocations;
54 const settings = yield* RelayConfiguration.RelayConfiguration;
55 const inbox = yield* HookInbox.HookInbox;
56
57 const resolveEndpoint: HeldHooks["Service"]["resolveEndpoint"] = Effect.fn(
58 "relay.hooks.resolve_endpoint",
59 )(function* (endpointKey) {
60 if (!settings.managedEndpointNamespace) return null;
61 const allocation = yield* allocations.getByTunnelName(
62 managedEndpointTunnelNameForKey(settings.managedEndpointNamespace, endpointKey),
63 );
64 if (allocation === null) return null;
65 const [link] = yield* links.findActiveManagedForEnvironment({
66 environmentId: allocation.environmentId,
67 userId: allocation.userId,
68 });
69 if (!link) return null;
70 const result = validateManagedEndpoint({
71 link,
72 allocation,
73 baseDomain: settings.managedEndpointBaseDomain,
74 });
75 if (Result.isFailure(result)) return null;
76 return {
77 ...result.success,
78 environmentId: allocation.environmentId,
79 holdWhileOffline: link.holdWebhooksWhileOffline,
80 };
81 });
82
83 /** Only links proven by this environment key count; other accounts can claim the same environment id. */
84 const ownEndpointKeys = Effect.fn("relay.hooks.own_endpoint_keys")(function* (input: {
85 readonly environmentId: string;
86 readonly environmentPublicKey: string;
87 }) {
88 const namespace = settings.managedEndpointNamespace;
89 if (!namespace) return [];
90 const ownLinks = yield* links.findActiveManagedForEnvironment(input);
91 const keys: Array<string> = [];
92 for (const link of ownLinks) {
93 const allocation = yield* allocations.get({
94 userId: link.userId,
95 environmentId: input.environmentId,
96 });
97 const key = allocation ? endpointKeyForTunnelName(namespace, allocation.tunnelName) : null;
98 if (key !== null) keys.push(key);
99 }
100 return keys;
101 });
102
103 const setHoldWhileOffline: HeldHooks["Service"]["setHoldWhileOffline"] = Effect.fn(
104 "relay.hooks.set_hold_while_offline",
105 )(function* (input) {
106 yield* links.setHoldWebhooksWhileOffline(input);
107 if (input.holdWebhooksWhileOffline) return;
108 for (const endpointKey of yield* ownEndpointKeys(input)) {
109 yield* inbox.clear({ endpointKey });
110 }
111 });
112
113 const wake: HeldHooks["Service"]["wake"] = Effect.fn("relay.hooks.wake")(function* (input) {
114 let pending = false;
115 for (const endpointKey of yield* ownEndpointKeys(input)) {
116 const endpoint = yield* resolveEndpoint(endpointKey);
117 if (endpoint === null) continue;
118 if (yield* inbox.wake({ endpointKey, baseUrl: endpoint.httpBaseUrl })) {
119 pending = true;
120 }
121 }
122 return pending;
123 });
124
125 return HeldHooks.of({ resolveEndpoint, setHoldWhileOffline, wake });
126});
127
128export const layer = Layer.effect(HeldHooks, make);