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