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

http.ts

162 lines6.1 KB

1import {
2 AuthRelayReadScope,
3 AuthRelayWriteScope,
4 EnvironmentCloudEndpointUnavailableError,
5 EnvironmentHttpApi,
6 EnvironmentHttpBadRequestError,
7 EnvironmentHttpConflictError,
8 EnvironmentHttpForbiddenError,
9 EnvironmentHttpInternalServerError,
10 EnvironmentHttpUnauthorizedError,
11} from "@t3tools/contracts";
12import * as Effect from "effect/Effect";
13import * as HttpEffect from "effect/http/HttpEffect";
14import { HttpServerRequest, HttpServerResponse } from "effect/http";
15import * as HttpApiBuilder from "effect/http-api/HttpApiBuilder";
16​
17import { requireEnvironmentScope } from "../auth/http.ts";
18import * as EnvironmentAuth from "../auth/EnvironmentAuth.ts";
19import * as ManagedLimits from "../policy/managedLimits.ts";
20import * as CloudLink from "./CloudLink.ts";
21import * as ConnectReachability from "./ConnectReachability.ts";
22import type { RelayRequestError } from "./relayResponse.ts";
23import { traceRelayRequest } from "./traceRelayRequest.ts";
24​
25const CLOUD_CREDENTIAL_RESPONSE_HEADERS = {
26 "cache-control": "no-store",
27 pragma: "no-cache",
28} as const;
29​
30const appendCloudCredentialResponseHeaders = HttpEffect.appendPreResponseHandler(
31 (_request, response) =>
32 Effect.succeed(HttpServerResponse.setHeaders(response, CLOUD_CREDENTIAL_RESPONSE_HEADERS)),
33);
34​
35const internalServerError = (error: { readonly message: string }, cause: unknown) =>
36 Effect.logError(error.message, { cause }).pipe(
37 Effect.andThen(Effect.fail(new EnvironmentHttpInternalServerError({ message: error.message }))),
38 );
39​
40const relayFailure = (error: RelayRequestError) => {
41 const message = error.message;
42 switch (error.rejection) {
43 case "unauthorized":
44 return Effect.fail(new EnvironmentHttpUnauthorizedError({ message }));
45 case "forbidden":
46 return Effect.fail(new EnvironmentHttpForbiddenError({ message }));
47 case "rejected":
48 return Effect.fail(new EnvironmentHttpBadRequestError({ message }));
49 case "unavailable":
50 return Effect.fail(new EnvironmentHttpInternalServerError({ message }));
51 }
52};
53​
54const badRequest = (error: { readonly message: string }) =>
55 Effect.fail(new EnvironmentHttpBadRequestError({ message: error.message }));
56const unauthorized = (error: { readonly message: string }) =>
57 Effect.fail(new EnvironmentHttpUnauthorizedError({ message: error.message }));
58const conflict = (error: { readonly message: string }) =>
59 Effect.fail(new EnvironmentHttpConflictError({ message: error.message }));
60​
61const connectErrorCases = {
62 CloudLinkRelayConfigInvalidError: badRequest,
63 CloudLinkOriginInvalidError: badRequest,
64 CloudLinkNotLinkedError: badRequest,
65 CloudLinkAccountMismatchError: conflict,
66 CloudLinkAuthorizationMissingError: unauthorized,
67 CloudLinkProofRejectedError: unauthorized,
68 CloudLinkProofReplayedError: conflict,
69 CloudLinkTunnelSupersededError: conflict,
70 ManagedPolicyRefusedError: (error: ManagedLimits.ManagedPolicyRefusedError) =>
71 Effect.fail(new EnvironmentHttpForbiddenError({ message: error.message })),
72 CloudLinkInternalError: (error: CloudLink.CloudLinkInternalError) =>
73 internalServerError(error, error.cause),
74 RelayRequestError: relayFailure,
75} as const;
76​
77type ConnectFailure =
78 | Exclude<CloudLink.CloudLinkError, CloudLink.CloudLinkEndpointUnavailableError>
79 | EnvironmentAuth.ServerAuthInternalError
80 | ManagedLimits.ManagedPolicyRefusedError
81 | RelayRequestError;
82​
83const toHttpError = <A, R>(effect: Effect.Effect<A, ConnectFailure, R>) =>
84 effect.pipe(
85 Effect.catchTags(connectErrorCases),
86 Effect.catchIf(EnvironmentAuth.isServerAuthInternalError, (error) =>
87 internalServerError(error, error),
88 ),
89 );
90​
91const toHttpErrorOrUnavailable = <A, R>(
92 effect: Effect.Effect<A, ConnectFailure | CloudLink.CloudLinkEndpointUnavailableError, R>,
93) =>
94 effect.pipe(
95 Effect.catchTags({
96 ...connectErrorCases,
97 CloudLinkEndpointUnavailableError: (error) =>
98 Effect.fail(
99 new EnvironmentCloudEndpointUnavailableError({
100 message: error.message,
101 endpointRuntimeStatus: error.endpointRuntimeStatus,
102 }),
103 ),
104 }),
105 Effect.catchIf(EnvironmentAuth.isServerAuthInternalError, (error) =>
106 internalServerError(error, error),
107 ),
108 );
109​
110export const layer = HttpApiBuilder.group(
111 EnvironmentHttpApi,
112 "connect",
113 Effect.fnUntraced(function* (handlers) {
114 const cloudLink = yield* CloudLink.CloudLink;
115 const reachability = yield* ConnectReachability.ConnectReachability;
116 return handlers
117 .handle("linkProof", ({ payload }) =>
118 Effect.gen(function* () {
119 yield* requireEnvironmentScope(AuthRelayWriteScope);
120 const request = yield* HttpServerRequest.HttpServerRequest;
121 const proof = yield* toHttpError(cloudLink.linkProof(payload, request));
122 yield* appendCloudCredentialResponseHeaders;
123 return proof;
124 }),
125 )
126 .handle("relayConfig", ({ payload }) =>
127 requireEnvironmentScope(AuthRelayWriteScope).pipe(
128 Effect.andThen(toHttpErrorOrUnavailable(cloudLink.applyRelayConfig(payload))),
129 ),
130 )
131 .handle("linkState", () =>
132 requireEnvironmentScope(AuthRelayReadScope).pipe(
133 Effect.andThen(toHttpError(cloudLink.linkState())),
134 ),
135 )
136 .handle("checkReachability", () =>
137 requireEnvironmentScope(AuthRelayReadScope).pipe(Effect.andThen(reachability.check)),
138 )
139 .handle("unlink", () =>
140 requireEnvironmentScope(AuthRelayWriteScope).pipe(
141 Effect.andThen(toHttpError(cloudLink.unlink())),
142 ),
143 )
144 .handle("preferences", ({ payload }) =>
145 requireEnvironmentScope(AuthRelayWriteScope).pipe(
146 Effect.andThen(toHttpError(cloudLink.updatePreferences(payload))),
147 ),
148 )
149 .handle("health", ({ payload }) =>
150 toHttpError(cloudLink.answerHealthRequest(payload)).pipe(
151 Effect.tap(() => appendCloudCredentialResponseHeaders),
152 ),
153 )
154 .handle("t3MintCredential", ({ payload }) =>
155 traceRelayRequest(
156 toHttpError(cloudLink.mintCredential(payload)).pipe(
157 Effect.tap(() => appendCloudCredentialResponseHeaders),
158 ),
159 ),
160 );
161 }),
162);