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

http.ts

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