Repository navigation
Expand file tree
/
Copy pathalchemy.run.ts
More file actions
177 lines (160 loc) · 7.52 KB
/
Copy pathalchemy.run.ts
File metadata and controls
177 lines (160 loc) · 7.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import { mainDb } from "@internal/db/alchemy";
import alchemy from "alchemy";
import { ReactRouter } from "alchemy/cloudflare";
import { requireAlchemyPassword, requireEnv } from "alchemy-utils";
import { alchemyCiCloudStateStoreOptions } from "alchemy-utils/alchemy-cloud-state-store";
import {
CI_WEB_DEPLOY_URL_RELPATH,
resolvePublicWebUrlForCi,
writeCiDeployUrlIfGithubActions,
} from "alchemy-utils/ci-deploy-web-url";
import { isPrStage, resolveStageFromEnv } from "alchemy-utils/deployment-stage";
import { readProcessEnvTrimmed } from "alchemy-utils/env-requirements";
import {
isPortlessLocalDevEnabled,
localWebPortlessHostname,
localWebPortlessRouteName,
portlessRunShellEnvPrefix,
} from "alchemy-utils/local-portless-dev";
import { pickListenPort } from "alchemy-utils/pick-listen-port";
import { isPosthogAnalyticsEnabled, POSTHOG_BROWSER_API_PATH } from "alchemy-utils/posthog-host";
import {
reactRouterDomainsFromProcessEnv,
reactRouterRoutesFromProcessEnv,
} from "alchemy-utils/web-deploy-hostnames";
import { workerObservabilityWithTraces } from "alchemy-utils/worker-observability";
import {
ALCHEMY_APP_IDS,
DEFAULT_REACT_ROUTER_WEB_RESOURCE_ID,
} from "alchemy-utils/worker-peer-scripts";
import { authWorker } from "auth-worker/alchemy";
import { chatroomWorker } from "chatroom-do/alchemy";
import { posthogProxyWorker } from "posthog-proxy/alchemy";
import { logPosthogSourcemapsAlchemyPlan } from "./posthog/log-sourcemaps-plan";
import { defaultPosthogReleaseName, resolvePosthogReleaseBuild } from "./posthog/release-names";
import { resolvePosthogReleaseVersion } from "./posthog/release-version";
const stage = resolveStageFromEnv();
/** One id per **`alchemy deploy`** so bindings match **`posthog-cli --build`** in the build subprocess (local uses a timestamp). */
const posthogReleaseBuild =
process.env["POSTHOG_RELEASE_BUILD"]?.trim() ||
resolvePosthogReleaseBuild(process.env, stage) ||
"";
if (posthogReleaseBuild) {
process.env["POSTHOG_RELEASE_BUILD"] = posthogReleaseBuild;
}
const app = await alchemy(ALCHEMY_APP_IDS.frontend, {
stage,
...alchemyCiCloudStateStoreOptions(stage),
});
requireAlchemyPassword(app);
const chatroomInternalSecretRaw = requireEnv(
"CHATROOM_INTERNAL_SECRET",
"Shared secret used when the web worker forwards /api/ws/* to the chatroom DO",
app,
);
const authAdminSecretRaw = requireEnv(
"AUTH_ADMIN_SECRET",
"Machine admin API secret (bootstrap sync and auth-worker admin routes)",
app,
);
const chatroomInternalSecret = alchemy.secret(chatroomInternalSecretRaw);
const authAdminSecret = alchemy.secret(authAdminSecretRaw);
const skipWebCustomHostnames = isPrStage(stage);
const webDomains = skipWebCustomHostnames ? [] : [...reactRouterDomainsFromProcessEnv()];
const webRoutes = skipWebCustomHostnames ? [] : [...reactRouterRoutesFromProcessEnv()];
const portlessEnabled = isPortlessLocalDevEnabled(stage);
const localWebPortlessName = localWebPortlessRouteName();
/** `PORT` when set and positive; otherwise `undefined`. */
function explicitPortFromEnv(env: NodeJS.ProcessEnv): number | undefined {
const raw = Number(env["PORT"]?.trim());
return Number.isFinite(raw) && raw > 0 ? Math.floor(raw) : undefined;
}
const explicitListenPort = explicitPortFromEnv(process.env);
/** Portless: honor `PORT`, else `get-port` from 5173 on 127.0.0.1. Otherwise unused for `dev` command — `5173` matches Vite default. */
const localDevListenPort = portlessEnabled
? (explicitListenPort ?? (await pickListenPort({ preferred: 5173, host: "127.0.0.1" })))
: (explicitListenPort ?? 5173);
/** `portless run` then `react-router dev` (Turbo `dev` already runs `typegen:local` where configured). */
const portlessWrappedLocalDev = portlessEnabled
? `${portlessRunShellEnvPrefix(process.env)}portless run --name ${localWebPortlessName} --app-port ${localDevListenPort} bunx react-router dev --port ${localDevListenPort}`
: undefined;
/** Turbo **`deploy:*`** depends on **`typegen`** (see **`turbo.json`**) — this is **`react-router build`** only. */
const reactRouterProductionBuild = "bun run react-router build";
/** Only deployed stages: inject/upload maps when CLI env is set (same signals as **`vite.config.ts`** hidden maps). */
const posthogSourcemapUploadAfterBuild =
stage !== "local" &&
Boolean(process.env["POSTHOG_CLI_TOKEN"]?.trim() || process.env["POSTHOG_CLI_API_KEY"]?.trim()) &&
Boolean(
process.env["POSTHOG_CLI_ENV_ID"]?.trim() || process.env["POSTHOG_CLI_PROJECT_ID"]?.trim(),
);
logPosthogSourcemapsAlchemyPlan({
stage,
runUploadAfterClientBuild: posthogSourcemapUploadAfterBuild,
});
const posthogKey = readProcessEnvTrimmed("POSTHOG_KEY");
export const web = await ReactRouter(DEFAULT_REACT_ROUTER_WEB_RESOURCE_ID, {
main: "workers/app.ts",
compatibility: "node",
placement: { mode: "smart" },
observability: workerObservabilityWithTraces,
build: posthogSourcemapUploadAfterBuild
? `${reactRouterProductionBuild} && bun posthog/upload-sourcemaps.ts`
: reactRouterProductionBuild,
url: true,
adopt: true,
routes: webRoutes,
domains: webDomains,
bindings: {
DB: mainDb,
CHATROOM_INTERNAL_SECRET: chatroomInternalSecret,
AUTH_ADMIN_SECRET: authAdminSecret,
AUTH: authWorker,
CHATROOM: chatroomWorker,
STAGE: stage,
PORT: readProcessEnvTrimmed("PORT"),
LOCAL_PORTLESS: readProcessEnvTrimmed("LOCAL_PORTLESS"),
PORTLESS_TLD: readProcessEnvTrimmed("PORTLESS_TLD"),
POSTHOG: posthogProxyWorker,
POSTHOG_KEY: posthogKey,
POSTHOG_UI_HOST: readProcessEnvTrimmed("POSTHOG_UI_HOST"),
POSTHOG_REGION: readProcessEnvTrimmed("POSTHOG_REGION"),
POSTHOG_SITE: readProcessEnvTrimmed("POSTHOG_SITE"),
/** Symbol-set name — see **`posthog/release-names.ts`**. */
POSTHOG_RELEASE_NAME: defaultPosthogReleaseName(stage, process.env),
/** Git / workflow commit id — **`posthog-cli` `--release-version`**; browser combines with **`POSTHOG_RELEASE_BUILD`** for **`logs.serviceVersion`**. */
POSTHOG_RELEASE_VERSION: resolvePosthogReleaseVersion(process.env),
/** **`posthog-cli` `--build`** (CI run number or **`local-<ms>`**) — packed into **`logs.serviceVersion`** as **`version+build`**. */
POSTHOG_RELEASE_BUILD: posthogReleaseBuild,
},
...(portlessWrappedLocalDev ? { dev: portlessWrappedLocalDev } : {}),
});
const portlessRaw = process.env["PORTLESS_URL"]?.trim();
/**
* Prefer env when Portless exposes it (`PORTLESS_URL` is injected on the `react-router dev`
* child process, but often absent in the `alchemy` process that evaluates this file after `web` updates —
* hence the `https://<name>.localhost` fallback matching `portless --name`).
*/
const portlessDerivedPublicBase =
stage === "local" && portlessWrappedLocalDev
? `https://${localWebPortlessHostname(process.env)}`
: undefined;
const portlessDevPublicUrl = (() => {
if (stage !== "local") {
return undefined;
}
const fromEnv = portlessRaw ? `${portlessRaw.replace(/\/$/, "")}/` : undefined;
const fromFlag = portlessDerivedPublicBase ? `${portlessDerivedPublicBase}/` : undefined;
return fromEnv ?? fromFlag;
})();
const ciWebPublicUrl = resolvePublicWebUrlForCi({ stage, workersDevUrl: web.url });
console.log({ webUrl: portlessDevPublicUrl ?? ciWebPublicUrl ?? web.url });
writeCiDeployUrlIfGithubActions(CI_WEB_DEPLOY_URL_RELPATH, ciWebPublicUrl ?? web.url);
// PR preview comments belong in `.github/workflows/pr-deploy.yml`. Avoid `alchemy/github`
// `GitHubComment` here on CI + `STAGE=pr-*` — `verifyGitHubAuth` often 404s for fork/private PRs.
await app.finalize();
if (isPosthogAnalyticsEnabled(process.env)) {
console.log({
posthogBrowserApiPath: POSTHOG_BROWSER_API_PATH,
posthogProxyWorker: posthogProxyWorker.name,
});
}