Update relay for Alchemy beta.48 and Cloudflare bindings
This commit is contained in:
1 parent
02353974fb
commit
f5f700f006
13 files changed
+323
-366
No files matched your search
@@ -9,6 +9,7 @@ import * as Planetscale from "alchemy/Planetscale";
|
||||
import * as FetchHttpClient from "effect/unstable/http/FetchHttpClient";
|
||||
|
||||
import { PlanetscaleDatabase, RelayHyperdrive } from "./src/db.ts";
|
||||
import { ManagedEndpointZone } from "./src/managedEndpointStack.ts";
|
||||
import Api from "./src/worker.ts";
|
||||
|
||||
export default Alchemy.Stack(
|
||||
@@ -26,6 +27,7 @@ export default Alchemy.Stack(
|
||||
Effect.gen(function* () {
|
||||
const db = yield* PlanetscaleDatabase;
|
||||
const hyperdrive = yield* RelayHyperdrive;
|
||||
const zone = yield* ManagedEndpointZone;
|
||||
const api = yield* Api;
|
||||
|
||||
return {
|
||||
@@ -33,6 +35,7 @@ export default Alchemy.Stack(
|
||||
hyperdriveName: hyperdrive.name,
|
||||
workerName: api.workerName,
|
||||
url: api.url,
|
||||
managedEndpointZoneId: zone.zoneId,
|
||||
};
|
||||
}),
|
||||
);
|
||||
@@ -14,7 +14,7 @@
|
||||
"@t3tools/client-runtime": "workspace:*",
|
||||
"@t3tools/contracts": "workspace:*",
|
||||
"@t3tools/shared": "workspace:*",
|
||||
"alchemy": "2.0.0-beta.47",
|
||||
"alchemy": "2.0.0-beta.48",
|
||||
"drizzle-orm": "1.0.0-rc.3",
|
||||
"effect": "catalog:"
|
||||
},
|
||||
|
||||
@@ -21,7 +21,6 @@ export interface RelayConfigurationShape {
|
||||
readonly cloudMintPrivateKey: Redacted.Redacted<string>;
|
||||
readonly cloudMintPublicKey: string;
|
||||
readonly managedEndpointBaseDomain: string | undefined;
|
||||
readonly cloudflareAccountId: string | undefined;
|
||||
readonly cloudflareZoneId: string | undefined;
|
||||
readonly cloudflareApiToken: Redacted.Redacted<string> | undefined;
|
||||
}
|
||||
|
||||
@@ -41,7 +41,6 @@ const config = RelayConfiguration.RelayConfiguration.of({
|
||||
cloudMintPrivateKey: Redacted.make("cloud-private-key"),
|
||||
cloudMintPublicKey: "cloud-public-key",
|
||||
managedEndpointBaseDomain: undefined,
|
||||
cloudflareAccountId: undefined,
|
||||
cloudflareZoneId: undefined,
|
||||
cloudflareApiToken: undefined,
|
||||
});
|
||||
|
||||
@@ -130,7 +130,6 @@ const config = RelayConfiguration.RelayConfiguration.of({
|
||||
cloudMintPrivateKey: Redacted.make("cloud-private-key"),
|
||||
cloudMintPublicKey: "cloud-public-key",
|
||||
managedEndpointBaseDomain: undefined,
|
||||
cloudflareAccountId: undefined,
|
||||
cloudflareZoneId: undefined,
|
||||
cloudflareApiToken: undefined,
|
||||
});
|
||||
|
||||
@@ -28,7 +28,6 @@ const config = RelayConfiguration.RelayConfiguration.of({
|
||||
cloudMintPrivateKey: Redacted.make(keyPair.privateKey),
|
||||
cloudMintPublicKey: keyPair.publicKey,
|
||||
managedEndpointBaseDomain: undefined,
|
||||
cloudflareAccountId: undefined,
|
||||
cloudflareZoneId: undefined,
|
||||
cloudflareApiToken: undefined,
|
||||
});
|
||||
|
||||
@@ -68,7 +68,6 @@ const settings = RelayConfiguration.RelayConfiguration.of({
|
||||
cloudMintPrivateKey: Redacted.make(cloudKeyPair.privateKey),
|
||||
cloudMintPublicKey: cloudKeyPair.publicKey,
|
||||
managedEndpointBaseDomain: undefined,
|
||||
cloudflareAccountId: undefined,
|
||||
cloudflareZoneId: undefined,
|
||||
cloudflareApiToken: undefined,
|
||||
});
|
||||
|
||||
@@ -41,7 +41,6 @@ const config = RelayConfiguration.RelayConfiguration.of({
|
||||
cloudMintPrivateKey: Redacted.make(relayKeyPair.privateKey),
|
||||
cloudMintPublicKey: relayKeyPair.publicKey,
|
||||
managedEndpointBaseDomain: undefined,
|
||||
cloudflareAccountId: undefined,
|
||||
cloudflareZoneId: undefined,
|
||||
cloudflareApiToken: undefined,
|
||||
});
|
||||
|
||||
@@ -36,7 +36,6 @@ const config = RelayConfiguration.RelayConfiguration.of({
|
||||
cloudMintPrivateKey: Redacted.make(keyPair.privateKey),
|
||||
cloudMintPublicKey: keyPair.publicKey,
|
||||
managedEndpointBaseDomain: undefined,
|
||||
cloudflareAccountId: undefined,
|
||||
cloudflareZoneId: undefined,
|
||||
cloudflareApiToken: undefined,
|
||||
});
|
||||
|
||||
@@ -24,11 +24,53 @@ const config = RelayConfiguration.RelayConfiguration.of({
|
||||
cloudMintPrivateKey: Redacted.make("cloud-private-key"),
|
||||
cloudMintPublicKey: "cloud-public-key",
|
||||
managedEndpointBaseDomain: "t3code.test",
|
||||
cloudflareAccountId: "account-id",
|
||||
cloudflareZoneId: "zone-id",
|
||||
cloudflareApiToken: Redacted.make("api-token"),
|
||||
});
|
||||
|
||||
interface TunnelCall {
|
||||
readonly operation: "list" | "create" | "putConfiguration" | "getToken";
|
||||
readonly input: unknown;
|
||||
}
|
||||
|
||||
function makeTunnelClient(calls: TunnelCall[] = []) {
|
||||
return ManagedEndpointProvider.ManagedEndpointTunnelClient.of({
|
||||
list: (request) =>
|
||||
Effect.sync(() => {
|
||||
calls.push({ operation: "list", input: request });
|
||||
return { result: [] };
|
||||
}),
|
||||
create: (request) =>
|
||||
Effect.sync(() => {
|
||||
calls.push({ operation: "create", input: request });
|
||||
return { id: "tunnel-id", name: request.name };
|
||||
}),
|
||||
putConfiguration: (tunnelId, tunnelConfig) =>
|
||||
Effect.sync(() => {
|
||||
calls.push({ operation: "putConfiguration", input: { tunnelId, tunnelConfig } });
|
||||
}),
|
||||
getToken: (tunnelId) =>
|
||||
Effect.sync(() => {
|
||||
calls.push({ operation: "getToken", input: tunnelId });
|
||||
return "connector-token";
|
||||
}),
|
||||
});
|
||||
}
|
||||
|
||||
function providerLayer(
|
||||
execute: (
|
||||
request: HttpClientRequest.HttpClientRequest,
|
||||
) => Effect.Effect<HttpClientResponse.HttpClientResponse>,
|
||||
tunnelClient = makeTunnelClient(),
|
||||
) {
|
||||
return ManagedEndpointProvider.layer.pipe(
|
||||
Layer.provideMerge(NodeServices.layer),
|
||||
Layer.provide(Layer.succeed(RelayConfiguration.RelayConfiguration, config)),
|
||||
Layer.provide(Layer.succeed(HttpClient.HttpClient, HttpClient.make(execute))),
|
||||
Layer.provide(Layer.succeed(ManagedEndpointProvider.ManagedEndpointTunnelClient, tunnelClient)),
|
||||
);
|
||||
}
|
||||
|
||||
function decodeBody(request: HttpClientRequest.HttpClientRequest): unknown {
|
||||
return request.body._tag === "Uint8Array"
|
||||
? JSON.parse(new TextDecoder().decode(request.body.body))
|
||||
@@ -45,8 +87,31 @@ function expectedManagedTunnelName(environmentId: string): string {
|
||||
return `t3-code-env-abc-${hash}`;
|
||||
}
|
||||
|
||||
function cloudflareApplicationErrorResponse(request: HttpClientRequest.HttpClientRequest) {
|
||||
return Effect.succeed(
|
||||
HttpClientResponse.fromWeb(
|
||||
request,
|
||||
Response.json(
|
||||
{
|
||||
success: false,
|
||||
result: [],
|
||||
errors: [{ code: 10_000, message: "Cloudflare application failure" }],
|
||||
},
|
||||
{ status: 200 },
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
function cloudflareNonSuccessHttpResponse(request: HttpClientRequest.HttpClientRequest) {
|
||||
return Effect.succeed(
|
||||
HttpClientResponse.fromWeb(request, Response.json({ success: false }, { status: 503 })),
|
||||
);
|
||||
}
|
||||
|
||||
describe("ManagedEndpointProvider", () => {
|
||||
it.effect("provisions a Cloudflare tunnel endpoint and connector token", () => {
|
||||
const tunnelCalls: TunnelCall[] = [];
|
||||
const calls: Array<{
|
||||
readonly method: string;
|
||||
readonly url: string;
|
||||
@@ -61,18 +126,6 @@ describe("ManagedEndpointProvider", () => {
|
||||
body: decodeBody(request),
|
||||
authorization: request.headers.authorization,
|
||||
});
|
||||
if (request.url.includes("/cfd_tunnel?")) {
|
||||
return HttpClientResponse.fromWeb(
|
||||
request,
|
||||
Response.json({ success: true, result: [] }, { status: 200 }),
|
||||
);
|
||||
}
|
||||
if (request.url.endsWith("/token")) {
|
||||
return HttpClientResponse.fromWeb(
|
||||
request,
|
||||
Response.json({ success: true, result: "connector-token" }, { status: 200 }),
|
||||
);
|
||||
}
|
||||
if (request.url.includes("/dns_records?")) {
|
||||
return HttpClientResponse.fromWeb(
|
||||
request,
|
||||
@@ -85,19 +138,7 @@ describe("ManagedEndpointProvider", () => {
|
||||
Response.json({ success: true }, { status: 200 }),
|
||||
);
|
||||
}
|
||||
if (request.url.endsWith("/configurations")) {
|
||||
return HttpClientResponse.fromWeb(
|
||||
request,
|
||||
Response.json({ success: true }, { status: 200 }),
|
||||
);
|
||||
}
|
||||
return HttpClientResponse.fromWeb(
|
||||
request,
|
||||
Response.json(
|
||||
{ success: true, result: { id: "tunnel-id", name: "tunnel-name" } },
|
||||
{ status: 200 },
|
||||
),
|
||||
);
|
||||
throw new Error(`Unexpected DNS request: ${request.method} ${request.url}`);
|
||||
});
|
||||
|
||||
return Effect.gen(function* () {
|
||||
@@ -118,20 +159,19 @@ describe("ManagedEndpointProvider", () => {
|
||||
providerKind: "cloudflare_tunnel",
|
||||
connectorToken: "connector-token",
|
||||
tunnelId: "tunnel-id",
|
||||
tunnelName: "tunnel-name",
|
||||
tunnelName: expectedManagedTunnelName("env_ABC"),
|
||||
},
|
||||
});
|
||||
expect(calls.map((call) => call.method)).toEqual([
|
||||
"GET",
|
||||
"POST",
|
||||
"PUT",
|
||||
"GET",
|
||||
"POST",
|
||||
"GET",
|
||||
]);
|
||||
expect(calls.map((call) => call.method)).toEqual(["GET", "POST"]);
|
||||
expect(calls.every((call) => call.authorization === "Bearer api-token")).toBe(true);
|
||||
expect(calls[2]?.body).toMatchObject({
|
||||
config: {
|
||||
expect(tunnelCalls.map((call) => call.operation)).toEqual([
|
||||
"list",
|
||||
"create",
|
||||
"putConfiguration",
|
||||
"getToken",
|
||||
]);
|
||||
expect(tunnelCalls[2]?.input).toMatchObject({
|
||||
tunnelConfig: {
|
||||
ingress: [
|
||||
{
|
||||
hostname,
|
||||
@@ -141,23 +181,17 @@ describe("ManagedEndpointProvider", () => {
|
||||
],
|
||||
},
|
||||
});
|
||||
expect(calls[0]?.url).toContain(
|
||||
`name=${expectedManagedTunnelName("env_ABC")}&is_deleted=false`,
|
||||
);
|
||||
}).pipe(
|
||||
Effect.provide(
|
||||
ManagedEndpointProvider.layer.pipe(
|
||||
Layer.provideMerge(NodeServices.layer),
|
||||
Layer.provide(Layer.succeed(RelayConfiguration.RelayConfiguration, config)),
|
||||
Layer.provide(Layer.succeed(HttpClient.HttpClient, HttpClient.make(execute))),
|
||||
),
|
||||
),
|
||||
);
|
||||
expect(tunnelCalls[0]?.input).toEqual({
|
||||
name: expectedManagedTunnelName("env_ABC"),
|
||||
isDeleted: false,
|
||||
});
|
||||
}).pipe(Effect.provide(providerLayer(execute, makeTunnelClient(tunnelCalls))));
|
||||
});
|
||||
|
||||
it.effect(
|
||||
"normalizes unusual environment ids before using them in Cloudflare tunnel names",
|
||||
() => {
|
||||
const tunnelCalls: TunnelCall[] = [];
|
||||
const calls: Array<{
|
||||
readonly method: string;
|
||||
readonly url: string;
|
||||
@@ -170,37 +204,19 @@ describe("ManagedEndpointProvider", () => {
|
||||
url: request.url,
|
||||
body: decodeBody(request),
|
||||
});
|
||||
if (request.url.includes("/cfd_tunnel?")) {
|
||||
return HttpClientResponse.fromWeb(
|
||||
request,
|
||||
Response.json({ success: true, result: [] }, { status: 200 }),
|
||||
);
|
||||
}
|
||||
if (request.url.endsWith("/token")) {
|
||||
return HttpClientResponse.fromWeb(
|
||||
request,
|
||||
Response.json({ success: true, result: "connector-token" }, { status: 200 }),
|
||||
);
|
||||
}
|
||||
if (request.url.includes("/dns_records?")) {
|
||||
return HttpClientResponse.fromWeb(
|
||||
request,
|
||||
Response.json({ success: true, result: [] }, { status: 200 }),
|
||||
);
|
||||
}
|
||||
if (request.url.endsWith("/dns_records") || request.url.endsWith("/configurations")) {
|
||||
if (request.url.endsWith("/dns_records")) {
|
||||
return HttpClientResponse.fromWeb(
|
||||
request,
|
||||
Response.json({ success: true }, { status: 200 }),
|
||||
);
|
||||
}
|
||||
return HttpClientResponse.fromWeb(
|
||||
request,
|
||||
Response.json(
|
||||
{ success: true, result: { id: "tunnel-id", name: "normalized-name" } },
|
||||
{ status: 200 },
|
||||
),
|
||||
);
|
||||
throw new Error(`Unexpected DNS request: ${request.method} ${request.url}`);
|
||||
});
|
||||
|
||||
return Effect.gen(function* () {
|
||||
@@ -211,53 +227,48 @@ describe("ManagedEndpointProvider", () => {
|
||||
origin: { localHttpHost: "127.0.0.1", localHttpPort: 3773 },
|
||||
});
|
||||
|
||||
const listUrl = calls[0]?.url ?? "";
|
||||
const createBody = calls[1]?.body;
|
||||
const requestedName = new URL(listUrl).searchParams.get("name");
|
||||
const requestedName = (
|
||||
tunnelCalls.find((call) => call.operation === "list")?.input as
|
||||
| { readonly name?: string }
|
||||
| undefined
|
||||
)?.name;
|
||||
expect(requestedName).toMatch(/^t3-code-env-with-spaces-symbols-x+-[a-f0-9]{16}$/);
|
||||
expect(requestedName?.length).toBeLessThanOrEqual(89);
|
||||
const configBody = calls.find((call) => call.url.endsWith("/configurations"))?.body;
|
||||
const configBody = (
|
||||
tunnelCalls.find((call) => call.operation === "putConfiguration")?.input as
|
||||
| { readonly tunnelConfig?: unknown }
|
||||
| undefined
|
||||
)?.tunnelConfig;
|
||||
expect(configBody).toMatchObject({
|
||||
config: {
|
||||
ingress: [
|
||||
{
|
||||
hostname: expect.stringMatching(
|
||||
/^tunnels-env-with-spaces-symbols-x+-[a-f0-9]{16}\.t3code\.test$/,
|
||||
),
|
||||
},
|
||||
{ service: "http_status:404" },
|
||||
],
|
||||
},
|
||||
ingress: [
|
||||
{
|
||||
hostname: expect.stringMatching(
|
||||
/^tunnels-env-with-spaces-symbols-x+-[a-f0-9]{16}\.t3code\.test$/,
|
||||
),
|
||||
},
|
||||
{ service: "http_status:404" },
|
||||
],
|
||||
});
|
||||
const hostname = (
|
||||
configBody as
|
||||
| {
|
||||
readonly config?: {
|
||||
readonly ingress?: readonly [{ readonly hostname?: unknown }, unknown];
|
||||
};
|
||||
readonly ingress?: readonly [{ readonly hostname?: unknown }, unknown];
|
||||
}
|
||||
| undefined
|
||||
)?.config?.ingress?.[0]?.hostname;
|
||||
)?.ingress?.[0]?.hostname;
|
||||
expect(
|
||||
typeof hostname === "string" ? hostname.split(".")[0]?.length : 0,
|
||||
).toBeLessThanOrEqual(63);
|
||||
expect(createBody).toMatchObject({
|
||||
expect(tunnelCalls.find((call) => call.operation === "create")?.input).toMatchObject({
|
||||
name: requestedName,
|
||||
config_src: "cloudflare",
|
||||
configSrc: "cloudflare",
|
||||
});
|
||||
}).pipe(
|
||||
Effect.provide(
|
||||
ManagedEndpointProvider.layer.pipe(
|
||||
Layer.provideMerge(NodeServices.layer),
|
||||
Layer.provide(Layer.succeed(RelayConfiguration.RelayConfiguration, config)),
|
||||
Layer.provide(Layer.succeed(HttpClient.HttpClient, HttpClient.make(execute))),
|
||||
),
|
||||
),
|
||||
);
|
||||
}).pipe(Effect.provide(providerLayer(execute, makeTunnelClient(tunnelCalls))));
|
||||
},
|
||||
);
|
||||
|
||||
it.effect("formats IPv6 loopback origins as valid Cloudflare ingress service URLs", () => {
|
||||
const tunnelCalls: TunnelCall[] = [];
|
||||
const calls: Array<{
|
||||
readonly method: string;
|
||||
readonly url: string;
|
||||
@@ -270,37 +281,19 @@ describe("ManagedEndpointProvider", () => {
|
||||
url: request.url,
|
||||
body: decodeBody(request),
|
||||
});
|
||||
if (request.url.includes("/cfd_tunnel?")) {
|
||||
return HttpClientResponse.fromWeb(
|
||||
request,
|
||||
Response.json({ success: true, result: [] }, { status: 200 }),
|
||||
);
|
||||
}
|
||||
if (request.url.endsWith("/token")) {
|
||||
return HttpClientResponse.fromWeb(
|
||||
request,
|
||||
Response.json({ success: true, result: "connector-token" }, { status: 200 }),
|
||||
);
|
||||
}
|
||||
if (request.url.includes("/dns_records?")) {
|
||||
return HttpClientResponse.fromWeb(
|
||||
request,
|
||||
Response.json({ success: true, result: [] }, { status: 200 }),
|
||||
);
|
||||
}
|
||||
if (request.url.endsWith("/dns_records") || request.url.endsWith("/configurations")) {
|
||||
if (request.url.endsWith("/dns_records")) {
|
||||
return HttpClientResponse.fromWeb(
|
||||
request,
|
||||
Response.json({ success: true }, { status: 200 }),
|
||||
);
|
||||
}
|
||||
return HttpClientResponse.fromWeb(
|
||||
request,
|
||||
Response.json(
|
||||
{ success: true, result: { id: "tunnel-id", name: "normalized-name" } },
|
||||
{ status: 200 },
|
||||
),
|
||||
);
|
||||
throw new Error(`Unexpected DNS request: ${request.method} ${request.url}`);
|
||||
});
|
||||
|
||||
return Effect.gen(function* () {
|
||||
@@ -310,8 +303,10 @@ describe("ManagedEndpointProvider", () => {
|
||||
origin: { localHttpHost: "::1", localHttpPort: 3773 },
|
||||
});
|
||||
|
||||
expect(calls[2]?.body).toMatchObject({
|
||||
config: {
|
||||
expect(
|
||||
tunnelCalls.find((call) => call.operation === "putConfiguration")?.input,
|
||||
).toMatchObject({
|
||||
tunnelConfig: {
|
||||
ingress: [
|
||||
{
|
||||
service: "http://[::1]:3773",
|
||||
@@ -320,15 +315,7 @@ describe("ManagedEndpointProvider", () => {
|
||||
],
|
||||
},
|
||||
});
|
||||
}).pipe(
|
||||
Effect.provide(
|
||||
ManagedEndpointProvider.layer.pipe(
|
||||
Layer.provideMerge(NodeServices.layer),
|
||||
Layer.provide(Layer.succeed(RelayConfiguration.RelayConfiguration, config)),
|
||||
Layer.provide(Layer.succeed(HttpClient.HttpClient, HttpClient.make(execute))),
|
||||
),
|
||||
),
|
||||
);
|
||||
}).pipe(Effect.provide(providerLayer(execute, makeTunnelClient(tunnelCalls))));
|
||||
});
|
||||
|
||||
it.effect("rejects non-loopback managed endpoint origins before calling Cloudflare", () => {
|
||||
@@ -356,15 +343,7 @@ describe("ManagedEndpointProvider", () => {
|
||||
if (result._tag === "Failure") {
|
||||
expect(result.failure._tag).toBe("ManagedEndpointOriginNotAllowed");
|
||||
}
|
||||
}).pipe(
|
||||
Effect.provide(
|
||||
ManagedEndpointProvider.layer.pipe(
|
||||
Layer.provideMerge(NodeServices.layer),
|
||||
Layer.provide(Layer.succeed(RelayConfiguration.RelayConfiguration, config)),
|
||||
Layer.provide(Layer.succeed(HttpClient.HttpClient, HttpClient.make(execute))),
|
||||
),
|
||||
),
|
||||
);
|
||||
}).pipe(Effect.provide(providerLayer(execute)));
|
||||
});
|
||||
|
||||
it.effect("rejects invalid managed endpoint origin ports before calling Cloudflare", () => {
|
||||
@@ -392,33 +371,10 @@ describe("ManagedEndpointProvider", () => {
|
||||
if (result._tag === "Failure") {
|
||||
expect(result.failure._tag).toBe("ManagedEndpointOriginNotAllowed");
|
||||
}
|
||||
}).pipe(
|
||||
Effect.provide(
|
||||
ManagedEndpointProvider.layer.pipe(
|
||||
Layer.provideMerge(NodeServices.layer),
|
||||
Layer.provide(Layer.succeed(RelayConfiguration.RelayConfiguration, config)),
|
||||
Layer.provide(Layer.succeed(HttpClient.HttpClient, HttpClient.make(execute))),
|
||||
),
|
||||
),
|
||||
);
|
||||
}).pipe(Effect.provide(providerLayer(execute)));
|
||||
});
|
||||
|
||||
it.effect("fails provisioning when Cloudflare returns a 2xx application error", () => {
|
||||
const execute = (request: HttpClientRequest.HttpClientRequest) =>
|
||||
Effect.sync(() =>
|
||||
HttpClientResponse.fromWeb(
|
||||
request,
|
||||
Response.json(
|
||||
{
|
||||
success: false,
|
||||
result: [],
|
||||
errors: [{ code: 10_000, message: "Cloudflare application failure" }],
|
||||
},
|
||||
{ status: 200 },
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
return Effect.gen(function* () {
|
||||
const provider = yield* ManagedEndpointProvider.ManagedEndpointProvider;
|
||||
const error = yield* Effect.flip(
|
||||
@@ -433,14 +389,20 @@ describe("ManagedEndpointProvider", () => {
|
||||
success: false,
|
||||
errors: [{ message: "Cloudflare application failure" }],
|
||||
});
|
||||
}).pipe(
|
||||
Effect.provide(
|
||||
ManagedEndpointProvider.layer.pipe(
|
||||
Layer.provideMerge(NodeServices.layer),
|
||||
Layer.provide(Layer.succeed(RelayConfiguration.RelayConfiguration, config)),
|
||||
Layer.provide(Layer.succeed(HttpClient.HttpClient, HttpClient.make(execute))),
|
||||
),
|
||||
),
|
||||
);
|
||||
}).pipe(Effect.provide(providerLayer(cloudflareApplicationErrorResponse)));
|
||||
});
|
||||
|
||||
it.effect("fails provisioning when Cloudflare returns a non-success HTTP response", () => {
|
||||
return Effect.gen(function* () {
|
||||
const provider = yield* ManagedEndpointProvider.ManagedEndpointProvider;
|
||||
const error = yield* Effect.flip(
|
||||
provider.provision({
|
||||
environmentId: "env_ABC",
|
||||
origin: { localHttpHost: "127.0.0.1", localHttpPort: 3773 },
|
||||
}),
|
||||
);
|
||||
|
||||
expect(error._tag).toBe("ManagedEndpointProvisioningFailed");
|
||||
}).pipe(Effect.provide(providerLayer(cloudflareNonSuccessHttpResponse)));
|
||||
});
|
||||
});
|
||||
@@ -1,19 +1,20 @@
|
||||
// @effect-diagnostics nodeBuiltinImport:off
|
||||
|
||||
import type {
|
||||
RelayManagedEndpoint,
|
||||
RelayManagedEndpointOrigin,
|
||||
RelayManagedEndpointRuntimeConfig,
|
||||
} from "@t3tools/contracts/relay";
|
||||
import * as Arr from "effect/Array";
|
||||
import * as Context from "effect/Context";
|
||||
import * as Crypto from "effect/Crypto";
|
||||
import * as Data from "effect/Data";
|
||||
import * as Effect from "effect/Effect";
|
||||
import * as Encoding from "effect/Encoding";
|
||||
import * as Layer from "effect/Layer";
|
||||
import * as Option from "effect/Option";
|
||||
import * as Redacted from "effect/Redacted";
|
||||
import * as Result from "effect/Result";
|
||||
import * as Schema from "effect/Schema";
|
||||
import { HttpClient, HttpClientRequest } from "effect/unstable/http";
|
||||
import { HttpClient, HttpClientRequest, HttpClientResponse } from "effect/unstable/http";
|
||||
|
||||
import * as RelayConfiguration from "../Config.ts";
|
||||
|
||||
@@ -56,35 +57,54 @@ export class ManagedEndpointProvider extends Context.Service<
|
||||
ManagedEndpointProviderShape
|
||||
>()("t3code-relay/environments/ManagedEndpointProvider") {}
|
||||
|
||||
const CloudflareTunnelCreateResponse = Schema.Struct({
|
||||
success: Schema.Boolean,
|
||||
result: Schema.Struct({
|
||||
id: Schema.String,
|
||||
name: Schema.String,
|
||||
}),
|
||||
});
|
||||
interface ManagedEndpointTunnel {
|
||||
readonly id?: string | null;
|
||||
readonly name?: string | null;
|
||||
}
|
||||
|
||||
const CloudflareTunnelListResponse = Schema.Struct({
|
||||
success: Schema.Boolean,
|
||||
result: Schema.Array(
|
||||
Schema.Struct({
|
||||
id: Schema.String,
|
||||
name: Schema.String,
|
||||
}),
|
||||
),
|
||||
});
|
||||
export class ManagedEndpointTunnelClientError extends Data.TaggedError(
|
||||
"ManagedEndpointTunnelClientError",
|
||||
)<{
|
||||
readonly cause: unknown;
|
||||
}> {}
|
||||
|
||||
const CloudflareTunnelTokenResponse = Schema.Struct({
|
||||
success: Schema.Boolean,
|
||||
result: Schema.String,
|
||||
});
|
||||
export interface ManagedEndpointTunnelClientShape {
|
||||
readonly list: (request: {
|
||||
readonly name: string;
|
||||
readonly isDeleted: false;
|
||||
}) => Effect.Effect<
|
||||
{ readonly result: ReadonlyArray<ManagedEndpointTunnel> },
|
||||
ManagedEndpointTunnelClientError
|
||||
>;
|
||||
readonly create: (request: {
|
||||
readonly name: string;
|
||||
readonly configSrc: "cloudflare";
|
||||
}) => Effect.Effect<ManagedEndpointTunnel, ManagedEndpointTunnelClientError>;
|
||||
readonly putConfiguration: (
|
||||
tunnelId: string,
|
||||
config: {
|
||||
readonly ingress: Array<{
|
||||
readonly hostname?: string;
|
||||
readonly service: string;
|
||||
}>;
|
||||
},
|
||||
) => Effect.Effect<unknown, ManagedEndpointTunnelClientError>;
|
||||
readonly getToken: (tunnelId: string) => Effect.Effect<string, ManagedEndpointTunnelClientError>;
|
||||
}
|
||||
|
||||
export class ManagedEndpointTunnelClient extends Context.Service<
|
||||
ManagedEndpointTunnelClient,
|
||||
ManagedEndpointTunnelClientShape
|
||||
>()("t3code-relay/environments/ManagedEndpointProvider/ManagedEndpointTunnelClient") {}
|
||||
|
||||
const CloudflareDnsRecordResponse = Schema.Struct({
|
||||
success: Schema.Boolean,
|
||||
errors: Schema.optionalKey(Schema.Unknown),
|
||||
});
|
||||
|
||||
const CloudflareDnsRecordListResponse = Schema.Struct({
|
||||
success: Schema.Boolean,
|
||||
errors: Schema.optionalKey(Schema.Unknown),
|
||||
result: Schema.Array(
|
||||
Schema.Struct({
|
||||
id: Schema.String,
|
||||
@@ -97,45 +117,18 @@ const requireCloudflareSettings = Effect.fnUntraced(function* (
|
||||
) {
|
||||
if (
|
||||
!settings.managedEndpointBaseDomain ||
|
||||
!settings.cloudflareAccountId ||
|
||||
!settings.cloudflareZoneId ||
|
||||
!settings.cloudflareApiToken
|
||||
) {
|
||||
return yield* new ManagedEndpointProvisioningNotConfigured();
|
||||
}
|
||||
return {
|
||||
accountId: settings.cloudflareAccountId,
|
||||
zoneId: settings.cloudflareZoneId,
|
||||
apiToken: Redacted.value(settings.cloudflareApiToken),
|
||||
baseDomain: settings.managedEndpointBaseDomain,
|
||||
};
|
||||
});
|
||||
|
||||
function cloudflareRequest(input: {
|
||||
readonly method: "GET" | "POST" | "PUT";
|
||||
readonly url: string;
|
||||
readonly apiToken: string;
|
||||
readonly body?: unknown;
|
||||
}): Effect.Effect<HttpClientRequest.HttpClientRequest, ManagedEndpointProvisioningFailed> {
|
||||
const base =
|
||||
input.method === "GET"
|
||||
? HttpClientRequest.get(input.url)
|
||||
: input.method === "POST"
|
||||
? HttpClientRequest.post(input.url)
|
||||
: HttpClientRequest.put(input.url);
|
||||
|
||||
const request = base.pipe(
|
||||
HttpClientRequest.setHeader("authorization", `Bearer ${input.apiToken}`),
|
||||
HttpClientRequest.setHeader("content-type", "application/json"),
|
||||
);
|
||||
return input.body === undefined
|
||||
? Effect.succeed(request)
|
||||
: request.pipe(
|
||||
HttpClientRequest.bodyJson(input.body),
|
||||
Effect.mapError((cause) => new ManagedEndpointProvisioningFailed({ cause })),
|
||||
);
|
||||
}
|
||||
|
||||
const MANAGED_ENDPOINT_HOST_PREFIX = "tunnels";
|
||||
const DNS_LABEL_MAX_LENGTH = 63;
|
||||
const MANAGED_ENDPOINT_HASH_LENGTH = 16;
|
||||
@@ -191,37 +184,20 @@ const make = Effect.gen(function* () {
|
||||
const config = yield* RelayConfiguration.RelayConfiguration;
|
||||
const httpClient = yield* HttpClient.HttpClient;
|
||||
const crypto = yield* Crypto.Crypto;
|
||||
const tunnels = yield* ManagedEndpointTunnelClient;
|
||||
|
||||
const requireCloudflareSuccess = (
|
||||
json: unknown,
|
||||
): Effect.Effect<void, ManagedEndpointProvisioningFailed> =>
|
||||
typeof json === "object" &&
|
||||
json !== null &&
|
||||
"success" in json &&
|
||||
(json as { readonly success: unknown }).success === false
|
||||
? Effect.fail(new ManagedEndpointProvisioningFailed({ cause: json }))
|
||||
: Effect.void;
|
||||
|
||||
const executeJson = Effect.fnUntraced(function* <A>(
|
||||
request: HttpClientRequest.HttpClientRequest,
|
||||
schema: Schema.Schema<A>,
|
||||
) {
|
||||
const response = yield* httpClient
|
||||
.execute(request)
|
||||
.pipe(Effect.mapError((cause) => new ManagedEndpointProvisioningFailed({ cause })));
|
||||
if (response.status < 200 || response.status >= 300) {
|
||||
return yield* new ManagedEndpointProvisioningFailed({ cause: response.status });
|
||||
}
|
||||
const json = yield* response.json.pipe(
|
||||
Effect.mapError((cause) => new ManagedEndpointProvisioningFailed({ cause })),
|
||||
);
|
||||
const isSchema = Schema.is(schema);
|
||||
if (!isSchema(json)) {
|
||||
return yield* new ManagedEndpointProvisioningFailed({ cause: json });
|
||||
}
|
||||
yield* requireCloudflareSuccess(json);
|
||||
return json;
|
||||
});
|
||||
const executeJson =
|
||||
<A extends { readonly success: boolean }>(schema: Schema.Codec<A, unknown, never, unknown>) =>
|
||||
(request: HttpClientRequest.HttpClientRequest) =>
|
||||
httpClient.execute(request).pipe(
|
||||
Effect.flatMap(HttpClientResponse.filterStatusOk),
|
||||
Effect.flatMap(HttpClientResponse.schemaBodyJson(schema)),
|
||||
Effect.mapError((cause) => new ManagedEndpointProvisioningFailed({ cause })),
|
||||
Effect.filterMapOrFail(
|
||||
(body) => (body.success ? Result.succeed(body) : Result.fail(body)),
|
||||
(cause) => new ManagedEndpointProvisioningFailed({ cause }),
|
||||
),
|
||||
);
|
||||
|
||||
return ManagedEndpointProvider.of({
|
||||
provision: Effect.fn("relay.managed_endpoint_provider.provision")(function* (input) {
|
||||
@@ -245,83 +221,70 @@ const make = Effect.gen(function* () {
|
||||
);
|
||||
const hostname = managedHostname(input.environmentId, cf.baseDomain, environmentHash);
|
||||
const tunnelName = managedTunnelName(input.environmentId, environmentHash);
|
||||
const existingTunnels = yield* cloudflareRequest({
|
||||
method: "GET",
|
||||
url: `https://api.cloudflare.com/client/v4/accounts/${cf.accountId}/cfd_tunnel?${new URLSearchParams(
|
||||
[
|
||||
["name", tunnelName],
|
||||
["is_deleted", "false"],
|
||||
],
|
||||
).toString()}`,
|
||||
apiToken: cf.apiToken,
|
||||
}).pipe(Effect.flatMap((request) => executeJson(request, CloudflareTunnelListResponse)));
|
||||
const existingTunnel = existingTunnels.result.find((tunnel) => tunnel.name === tunnelName);
|
||||
const tunnel =
|
||||
existingTunnel ??
|
||||
(yield* cloudflareRequest({
|
||||
method: "POST",
|
||||
url: `https://api.cloudflare.com/client/v4/accounts/${cf.accountId}/cfd_tunnel`,
|
||||
apiToken: cf.apiToken,
|
||||
body: {
|
||||
name: tunnelName,
|
||||
config_src: "cloudflare",
|
||||
},
|
||||
}).pipe(
|
||||
Effect.flatMap((request) => executeJson(request, CloudflareTunnelCreateResponse)),
|
||||
Effect.map((response) => response.result),
|
||||
));
|
||||
|
||||
yield* cloudflareRequest({
|
||||
method: "PUT",
|
||||
url: `https://api.cloudflare.com/client/v4/accounts/${cf.accountId}/cfd_tunnel/${tunnel.id}/configurations`,
|
||||
apiToken: cf.apiToken,
|
||||
body: {
|
||||
config: {
|
||||
ingress: [
|
||||
{
|
||||
hostname,
|
||||
service: formatOriginService(input.origin),
|
||||
},
|
||||
{ service: "http_status:404" },
|
||||
],
|
||||
},
|
||||
},
|
||||
}).pipe(
|
||||
Effect.flatMap((request) =>
|
||||
executeJson(request, Schema.Struct({ success: Schema.Boolean })),
|
||||
const tunnel = yield* tunnels.list({ name: tunnelName, isDeleted: false }).pipe(
|
||||
Effect.map((tunnels) => tunnels.result),
|
||||
Effect.map(Arr.findFirst((tunnel) => tunnel.name === tunnelName)),
|
||||
Effect.flatMap(
|
||||
Option.match({
|
||||
onSome: (tunnel) => Effect.succeed(tunnel),
|
||||
onNone: () => tunnels.create({ name: tunnelName, configSrc: "cloudflare" }),
|
||||
}),
|
||||
),
|
||||
Effect.filterMapOrFail((tunnel) =>
|
||||
tunnel.id && tunnel.name
|
||||
? Result.succeed({ id: tunnel.id, name: tunnel.name })
|
||||
: Result.fail(new ManagedEndpointProvisioningFailed({ cause: tunnel })),
|
||||
),
|
||||
Effect.mapError((cause) => new ManagedEndpointProvisioningFailed({ cause })),
|
||||
);
|
||||
|
||||
const dnsRecords = yield* cloudflareRequest({
|
||||
method: "GET",
|
||||
url: `https://api.cloudflare.com/client/v4/zones/${cf.zoneId}/dns_records?${new URLSearchParams(
|
||||
[
|
||||
["type", "CNAME"],
|
||||
["name", hostname],
|
||||
yield* tunnels
|
||||
.putConfiguration(tunnel.id, {
|
||||
ingress: [
|
||||
{
|
||||
hostname,
|
||||
service: formatOriginService(input.origin),
|
||||
},
|
||||
{ service: "http_status:404" },
|
||||
],
|
||||
).toString()}`,
|
||||
apiToken: cf.apiToken,
|
||||
}).pipe(Effect.flatMap((request) => executeJson(request, CloudflareDnsRecordListResponse)));
|
||||
const existingDnsRecordId = dnsRecords.result[0]?.id;
|
||||
yield* cloudflareRequest({
|
||||
method: existingDnsRecordId ? "PUT" : "POST",
|
||||
url: existingDnsRecordId
|
||||
? `https://api.cloudflare.com/client/v4/zones/${cf.zoneId}/dns_records/${existingDnsRecordId}`
|
||||
: `https://api.cloudflare.com/client/v4/zones/${cf.zoneId}/dns_records`,
|
||||
apiToken: cf.apiToken,
|
||||
body: {
|
||||
})
|
||||
.pipe(Effect.mapError((cause) => new ManagedEndpointProvisioningFailed({ cause })));
|
||||
|
||||
const existingDnsRecordId = yield* HttpClientRequest.make("GET")(
|
||||
"/zones/${cf.zoneId}/dns_records",
|
||||
).pipe(
|
||||
HttpClientRequest.prependUrl("https://api.cloudflare.com/client/v4"),
|
||||
HttpClientRequest.bearerToken(cf.apiToken),
|
||||
HttpClientRequest.setUrlParams({
|
||||
type: "CNAME",
|
||||
name: hostname,
|
||||
}),
|
||||
executeJson(CloudflareDnsRecordListResponse),
|
||||
Effect.map((body) => Arr.head(body.result)),
|
||||
Effect.map(Option.map((record) => record.id)),
|
||||
);
|
||||
|
||||
const upsertDnsRequest = Option.match(existingDnsRecordId, {
|
||||
onSome: (id) => HttpClientRequest.make("PUT")(`/zones/${cf.zoneId}/dns_records/${id}`),
|
||||
onNone: () => HttpClientRequest.make("POST")(`/zones/${cf.zoneId}/dns_records`),
|
||||
});
|
||||
|
||||
yield* upsertDnsRequest.pipe(
|
||||
HttpClientRequest.prependUrl("https://api.cloudflare.com/client/v4"),
|
||||
HttpClientRequest.bearerToken(cf.apiToken),
|
||||
HttpClientRequest.bodyJsonUnsafe({
|
||||
type: "CNAME",
|
||||
name: hostname,
|
||||
content: `${tunnel.id}.cfargotunnel.com`,
|
||||
proxied: true,
|
||||
},
|
||||
}).pipe(Effect.flatMap((request) => executeJson(request, CloudflareDnsRecordResponse)));
|
||||
}),
|
||||
executeJson(CloudflareDnsRecordResponse),
|
||||
);
|
||||
|
||||
const token = yield* cloudflareRequest({
|
||||
method: "GET",
|
||||
url: `https://api.cloudflare.com/client/v4/accounts/${cf.accountId}/cfd_tunnel/${tunnel.id}/token`,
|
||||
apiToken: cf.apiToken,
|
||||
}).pipe(Effect.flatMap((request) => executeJson(request, CloudflareTunnelTokenResponse)));
|
||||
const connectorToken = yield* tunnels
|
||||
.getToken(tunnel.id)
|
||||
.pipe(Effect.mapError((cause) => new ManagedEndpointProvisioningFailed({ cause })));
|
||||
|
||||
return {
|
||||
endpoint: {
|
||||
@@ -331,7 +294,7 @@ const make = Effect.gen(function* () {
|
||||
},
|
||||
runtime: {
|
||||
providerKind: "cloudflare_tunnel",
|
||||
connectorToken: token.result,
|
||||
connectorToken,
|
||||
tunnelId: tunnel.id,
|
||||
tunnelName: tunnel.name,
|
||||
},
|
||||
|
||||
@@ -1,34 +1,32 @@
|
||||
import { adopt } from "alchemy/AdoptPolicy";
|
||||
import * as Cloudflare from "alchemy/Cloudflare";
|
||||
import * as Output from "alchemy/Output";
|
||||
import * as Effect from "effect/Effect";
|
||||
|
||||
// This should be pulled from the Alchemy authenticated cloudflare account
|
||||
export const CLOUDFLARE_ACCOUNT_ID = "1468bbd99811cdaccfbb707dc725421a";
|
||||
|
||||
// We should only need to specify one of these after Alchemy have a CloudflareZone resource: https://github.com/alchemy-run/alchemy-effect/pull/493
|
||||
export const MANAGED_ENDPOINT_ZONE_ID = "fcea40a6915723b0f5c4a9480eb3507b";
|
||||
export const MANAGED_ENDPOINT_ZONE_NAME = "ineededadomain.com";
|
||||
|
||||
export const RELAY_PUBLIC_DOMAIN = `t3code-relay.${MANAGED_ENDPOINT_ZONE_NAME}`;
|
||||
export const RELAY_PUBLIC_DOMAIN = "t3code-relay.ineededadomain.com";
|
||||
export const RELAY_PUBLIC_ORIGIN = `https://${RELAY_PUBLIC_DOMAIN}`;
|
||||
|
||||
export const ManagedEndpointProvisionerToken = Cloudflare.AccountApiToken(
|
||||
"ManagedEndpointProvisionerToken",
|
||||
{
|
||||
name: "t3-code-relay-managed-endpoint-provisioner",
|
||||
export const ManagedEndpointZone = Cloudflare.Zone("ManagedEndpointZone", {
|
||||
name: "ineededadomain.com",
|
||||
}).pipe(adopt(true));
|
||||
|
||||
export const ManagedEndpointDNSToken = Effect.gen(function* () {
|
||||
const zoneId = yield* ManagedEndpointZone.pipe(Effect.map((zone) => zone.zoneId));
|
||||
|
||||
const dnsToken = yield* Cloudflare.AccountApiToken("ManagedEndpointDNSToken", {
|
||||
name: "t3-code-relay-managed-endpoint-dns-token",
|
||||
policies: [
|
||||
{
|
||||
effect: "allow" as const,
|
||||
permissionGroups: ["Cloudflare Tunnel Read" as const, "Cloudflare Tunnel Write" as const],
|
||||
resources: {
|
||||
[`com.cloudflare.api.account.${CLOUDFLARE_ACCOUNT_ID}`]: "*",
|
||||
},
|
||||
},
|
||||
{
|
||||
effect: "allow" as const,
|
||||
permissionGroups: ["DNS Read" as const, "DNS Write" as const],
|
||||
resources: {
|
||||
[`com.cloudflare.api.account.zone.${MANAGED_ENDPOINT_ZONE_ID}`]: "*",
|
||||
},
|
||||
effect: "allow",
|
||||
permissionGroups: ["DNS Read", "DNS Write"],
|
||||
resources: zoneId.pipe(
|
||||
Output.map((id) => ({
|
||||
[`com.cloudflare.api.account.zone.${id}`]: "*",
|
||||
})),
|
||||
),
|
||||
},
|
||||
],
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
return dnsToken;
|
||||
});
|
||||
+51
-13
@@ -30,10 +30,8 @@ import {
|
||||
withoutCapturedParentSpan,
|
||||
} from "./http/Api.ts";
|
||||
import {
|
||||
CLOUDFLARE_ACCOUNT_ID,
|
||||
MANAGED_ENDPOINT_ZONE_ID,
|
||||
MANAGED_ENDPOINT_ZONE_NAME,
|
||||
ManagedEndpointProvisionerToken,
|
||||
ManagedEndpointDNSToken,
|
||||
ManagedEndpointZone,
|
||||
RELAY_PUBLIC_DOMAIN,
|
||||
RELAY_PUBLIC_ORIGIN,
|
||||
} from "./managedEndpointStack.ts";
|
||||
@@ -112,8 +110,12 @@ export default class Api extends Cloudflare.Worker<Api>()(
|
||||
const alchemyRuntimeContext = yield* Alchemy.RuntimeContext;
|
||||
const cloudMintKeyPair = yield* CloudMintKeyPair;
|
||||
const hyperdrive = yield* Cloudflare.Hyperdrive.bind(yield* RelayHyperdrive);
|
||||
const managedEndpointProvisionerToken = yield* ManagedEndpointProvisionerToken;
|
||||
const managedEndpointZone = yield* ManagedEndpointZone;
|
||||
const managedEndpointZoneId = yield* managedEndpointZone.zoneId;
|
||||
const managedEndpointZoneName = yield* managedEndpointZone.name;
|
||||
const managedEndpointProvisionerToken = yield* ManagedEndpointDNSToken;
|
||||
const managedEndpointCloudflareApiToken = yield* managedEndpointProvisionerToken.value;
|
||||
const managedEndpointTunnelBinding = yield* Cloudflare.TunnelReadWrite.bind();
|
||||
const randomApnsDeliveryJobSigningSecret = yield* ApnsDeliveryJobSigningSecret;
|
||||
const observability = yield* RelayObservability;
|
||||
|
||||
@@ -146,6 +148,36 @@ export default class Api extends Cloudflare.Worker<Api>()(
|
||||
Effect.provideService(Alchemy.RuntimeContext, alchemyRuntimeContext),
|
||||
),
|
||||
});
|
||||
const managedEndpointTunnelClient = ManagedEndpointProvider.ManagedEndpointTunnelClient.of({
|
||||
list: (request) =>
|
||||
managedEndpointTunnelBinding.list(request).pipe(
|
||||
Effect.mapError(
|
||||
(cause) => new ManagedEndpointProvider.ManagedEndpointTunnelClientError({ cause }),
|
||||
),
|
||||
Effect.provideService(Alchemy.RuntimeContext, alchemyRuntimeContext),
|
||||
),
|
||||
create: (request) =>
|
||||
managedEndpointTunnelBinding.create(request).pipe(
|
||||
Effect.mapError(
|
||||
(cause) => new ManagedEndpointProvider.ManagedEndpointTunnelClientError({ cause }),
|
||||
),
|
||||
Effect.provideService(Alchemy.RuntimeContext, alchemyRuntimeContext),
|
||||
),
|
||||
putConfiguration: (tunnelId, config) =>
|
||||
managedEndpointTunnelBinding.putConfiguration(tunnelId, config).pipe(
|
||||
Effect.mapError(
|
||||
(cause) => new ManagedEndpointProvider.ManagedEndpointTunnelClientError({ cause }),
|
||||
),
|
||||
Effect.provideService(Alchemy.RuntimeContext, alchemyRuntimeContext),
|
||||
),
|
||||
getToken: (tunnelId) =>
|
||||
managedEndpointTunnelBinding.getToken(tunnelId).pipe(
|
||||
Effect.mapError(
|
||||
(cause) => new ManagedEndpointProvider.ManagedEndpointTunnelClientError({ cause }),
|
||||
),
|
||||
Effect.provideService(Alchemy.RuntimeContext, alchemyRuntimeContext),
|
||||
),
|
||||
});
|
||||
|
||||
//
|
||||
// 3. Runtime layers and app construction
|
||||
@@ -165,9 +197,8 @@ export default class Api extends Cloudflare.Worker<Api>()(
|
||||
clerkSecretKey,
|
||||
cloudMintPrivateKey: yield* cloudMintPrivateKey,
|
||||
cloudMintPublicKey: yield* cloudMintPublicKey,
|
||||
managedEndpointBaseDomain: MANAGED_ENDPOINT_ZONE_NAME,
|
||||
cloudflareAccountId: CLOUDFLARE_ACCOUNT_ID,
|
||||
cloudflareZoneId: MANAGED_ENDPOINT_ZONE_ID,
|
||||
managedEndpointBaseDomain: yield* managedEndpointZoneName,
|
||||
cloudflareZoneId: yield* managedEndpointZoneId,
|
||||
cloudflareApiToken: yield* managedEndpointCloudflareApiToken,
|
||||
});
|
||||
});
|
||||
@@ -205,6 +236,12 @@ export default class Api extends Cloudflare.Worker<Api>()(
|
||||
Layer.provide(RelayTokens.layer),
|
||||
Layer.provide(Layer.succeed(RelayDb, db)),
|
||||
Layer.provide(Layer.succeed(ApnsDeliveryQueue.ApnsDeliveryQueueSender, queueSender)),
|
||||
Layer.provide(
|
||||
Layer.succeed(
|
||||
ManagedEndpointProvider.ManagedEndpointTunnelClient,
|
||||
managedEndpointTunnelClient,
|
||||
),
|
||||
),
|
||||
Layer.provide(Layer.succeed(RelayConfiguration.RelayConfiguration, settings)),
|
||||
Layer.provide(webcryptoLayer),
|
||||
);
|
||||
@@ -273,11 +310,12 @@ export default class Api extends Cloudflare.Worker<Api>()(
|
||||
return { fetch };
|
||||
}).pipe(
|
||||
Effect.provide(
|
||||
Layer.mergeAll(
|
||||
Cloudflare.HyperdriveBindingLive,
|
||||
Cloudflare.CronEventSourceLive,
|
||||
Cloudflare.QueueBindingLive,
|
||||
Cloudflare.QueueEventSourceLive,
|
||||
Layer.empty.pipe(
|
||||
Layer.provideMerge(Cloudflare.HyperdriveBindingLive),
|
||||
Layer.provideMerge(Cloudflare.CronEventSourceLive),
|
||||
Layer.provideMerge(Cloudflare.QueueBindingLive),
|
||||
Layer.provideMerge(Cloudflare.QueueEventSourceLive),
|
||||
Layer.provideMerge(Cloudflare.TunnelReadWriteLive),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
Reference in new issue
Block a user