Open History to see the last commit
1// @effect-diagnostics-next-line nodeBuiltinImport:off -- FileSystem.remove cannot remove an empty directory nonrecursively.
2import * as NodeFSP from "node:fs/promises";
3import * as Cache from "effect/Cache";
4import * as Clock from "effect/Clock";
5import type {
6 RelayClientInstallProgressEvent,
7 RelayClientInstallProgressStage,
8} from "@t3tools/contracts";
9import * as Config from "effect/Config";
10import * as Context from "effect/Context";
11import * as Digest from "effect/crypto/Digest";
12import * as SecureRandom from "effect/crypto/SecureRandom";
13import * as Data from "effect/Data";
14import * as Duration from "effect/Duration";
15import * as Effect from "effect/Effect";
16import * as Exit from "effect/Exit";
17import * as Hex from "effect/encoding/Hex";
18import * as FileSystem from "effect/FileSystem";
19import * as Layer from "effect/Layer";
20import * as Option from "effect/Option";
21import * as Path from "effect/Path";
22import type * as PlatformError from "effect/PlatformError";
23import * as Schedule from "effect/Schedule";
24import * as Schema from "effect/Schema";
25import * as Semaphore from "effect/Semaphore";
26import { HttpClient, HttpClientRequest, HttpClientResponse } from "effect/http";
27import { ChildProcess, ChildProcessSpawner } from "effect/process";
28import { isWindowsRenameLock } from "./atomicWrite.ts";
29import * as HostProcess from "./HostProcess.ts";
30
31export const CLOUDFLARED_VERSION = "2026.10.4";
32// Minimum for --output support and the eMudhra fix, which 2026.10.2 macOS builds lack.
33export const CLOUDFLARED_MIN_VERSION = "2026.10.3";
34const CLOUDFLARED_PATH_ENV_NAME = "T3CODE_CLOUDFLARED_PATH";
35
36const CLOUDFLARED_DOWNLOAD_TIMEOUT = "10 minutes";
37
38export type RelayClientExecutableSource = "override" | "managed" | "path";
39
40export type RelayClientStatus =
41 | {
42 readonly status: "available";
43 readonly executablePath: string;
44 readonly source: RelayClientExecutableSource;
45 readonly version: string;
46 }
47 | {
48 readonly status: "missing";
49 readonly version: string;
50 }
51 | {
52 readonly status: "unsupported";
53 readonly platform: NodeJS.Platform;
54 readonly arch: string;
55 readonly version: string;
56 };
57
58export type AvailableRelayClient = Extract<RelayClientStatus, { readonly status: "available" }>;
59
60class RelayClientLockRemoveError extends Schema.TaggedError<RelayClientLockRemoveError>()(
61 "RelayClientLockRemoveError",
62 { lockPath: Schema.String, cause: Schema.Defect() },
63) {
64 override get message(): string {
65 return `Could not remove the relay client lock ${this.lockPath}.`;
66 }
67}
68
69export class RelayClientInstallError extends Schema.TaggedError<RelayClientInstallError>()(
70 "RelayClientInstallError",
71 {
72 reason: Schema.Literals([
73 "download_failed",
74 "invalid_checksum",
75 "install_locked",
76 "managed_by_policy",
77 "override_missing",
78 "unsupported_platform",
79 "validation_failed",
80 "write_failed",
81 ]),
82 message: Schema.String,
83 cause: Schema.optional(Schema.Defect()),
84 },
85) {}
86
87const isRelayClientInstallError = Schema.is(RelayClientInstallError);
88
89class CloudflaredCommandError extends Schema.TaggedError<CloudflaredCommandError>()(
90 "CloudflaredCommandError",
91 {
92 command: Schema.String,
93 exitCode: Schema.Number,
94 },
95) {}
96
97export interface CloudflaredReleaseAsset {
98 readonly url: string;
99 readonly sha256: string;
100 readonly archive: "binary" | "tgz";
101}
102
103const CLOUDFLARED_RELEASE_ASSETS: Readonly<
104 Partial<Record<`${NodeJS.Platform}-${string}`, CloudflaredReleaseAsset>>
105> = {
106 "darwin-arm64": {
107 url: "https://github.com/cloudflare/cloudflared/releases/download/2026.10.4/cloudflared-darwin-arm64.tgz",
108 sha256: "68b6bc4080ac1330234db2a9002d07f98b000537a1bd531a687b660d4160fa7f",
109 archive: "tgz",
110 },
111 "darwin-x64": {
112 url: "https://github.com/cloudflare/cloudflared/releases/download/2026.10.4/cloudflared-darwin-amd64.tgz",
113 sha256: "f11fbef4207d715892331917dafd437f032cf4b7137ed64dea3008338c1c613d",
114 archive: "tgz",
115 },
116 "linux-arm64": {
117 url: "https://github.com/cloudflare/cloudflared/releases/download/2026.10.4/cloudflared-linux-arm64",
118 sha256: "2b7203b4fb0ba28a0c1e12f8855ce2c25f76d266a8d5edafac5540b6812acff6",
119 archive: "binary",
120 },
121 "linux-x64": {
122 url: "https://github.com/cloudflare/cloudflared/releases/download/2026.10.4/cloudflared-linux-amd64",
123 sha256: "ae5e8add7884b0531d66bbca4570d1de491ff6da51e4ebe7eb160cdf241d9e42",
124 archive: "binary",
125 },
126 "win32-x64": {
127 url: "https://github.com/cloudflare/cloudflared/releases/download/2026.10.4/cloudflared-windows-amd64.exe",
128 sha256: "411b16d40c8d872243efeb52c550053d00af0a8a97fbb32e730fb8aba429eaa8",
129 archive: "binary",
130 },
131 // Cloudflare publishes no Windows ARM64 build; Windows 11 on ARM runs x64 under emulation.
132 "win32-arm64": {
133 url: "https://github.com/cloudflare/cloudflared/releases/download/2026.10.4/cloudflared-windows-amd64.exe",
134 sha256: "411b16d40c8d872243efeb52c550053d00af0a8a97fbb32e730fb8aba429eaa8",
135 archive: "binary",
136 },
137};
138
139const INSTALL_LOCK_RETRY_COUNT = 100;
140const INSTALL_LOCK_RETRY_DELAY = "100 millis";
141// Legacy locks have no owner; allow both ten-minute download stages and the install tail.
142const INSTALL_LOCK_LEGACY_STALE_MS = 30 * 60 * 1_000;
143const VERSION_PROBE_TIMEOUT = "10 seconds";
144// Antivirus scans can hold the first launch of a freshly downloaded binary for 20-30 s.
145const INSTALL_VALIDATION_TIMEOUT = "2 minutes";
146
147const ACTIVATE_RETRY_COUNT = 40;
148const ACTIVATE_RETRY_DELAY = "250 millis";
149
150const trimmedString = (name: string) =>
151 Config.String(name).pipe(
152 Config.option,
153 Config.map(
154 Option.flatMap((value) => {
155 const trimmed = value.trim();
156 return trimmed.length > 0 ? Option.some(trimmed) : Option.none();
157 }),
158 ),
159 );
160
161const CloudflaredConfig = Config.all({
162 executableOverride: trimmedString(CLOUDFLARED_PATH_ENV_NAME),
163});
164
165export interface CloudflaredRelayClientOptions {
166 readonly baseDir: string;
167 readonly releaseAsset?: CloudflaredReleaseAsset;
168}
169
170export interface RelayClientShape {
171 readonly resolve: Effect.Effect<RelayClientStatus>;
172 readonly install: Effect.Effect<AvailableRelayClient, RelayClientInstallError>;
173 readonly installWithProgress: (
174 report: (event: RelayClientInstallProgressEvent) => Effect.Effect<void>,
175 ) => Effect.Effect<AvailableRelayClient, RelayClientInstallError>;
176 /** Removes managed releases older than the pin, except the newest one. Call once the pin has connected. */
177 readonly pruneManagedVersions: Effect.Effect<void>;
178}
179
180export function isPinnedManagedRelayClient(client: AvailableRelayClient): boolean {
181 return client.source === "managed" && client.version === CLOUDFLARED_VERSION;
182}
183
184export class RelayClient extends Context.Service<RelayClient, RelayClientShape>()(
185 "@t3tools/shared/relayClient",
186) {}
187
188function executableFileName(platform: NodeJS.Platform): string {
189 return platform === "win32" ? "cloudflared.exe" : "cloudflared";
190}
191
192function resolveReleaseAsset(
193 platform: NodeJS.Platform,
194 arch: string,
195): CloudflaredReleaseAsset | null {
196 return CLOUDFLARED_RELEASE_ASSETS[`${platform}-${arch}`] ?? null;
197}
198
199const VERSION_PATTERN = /\b(\d{4})\.(\d{1,2})\.(\d+)\b/u;
200
201function parseVersion(value: string): readonly [number, number, number] | null {
202 const match = VERSION_PATTERN.exec(value);
203 return match ? [Number(match[1]), Number(match[2]), Number(match[3])] : null;
204}
205
206export function compareCloudflaredVersions(left: string, right: string): number {
207 const a = parseVersion(left) ?? [0, 0, 0];
208 const b = parseVersion(right) ?? [0, 0, 0];
209 return a[0] - b[0] || a[1] - b[1] || a[2] - b[2];
210}
211
212export function parseCloudflaredVersionOutput(output: string): string | null {
213 const version = parseVersion(output);
214 return version ? version.join(".") : null;
215}
216
217class VersionProbeKey extends Data.Class<{
218 readonly executablePath: string;
219 readonly size: number;
220 readonly mtimeMillis: number;
221}> {}
222
223function isLockContention(error: PlatformError.PlatformError): boolean {
224 const code = (error.reason.cause as NodeJS.ErrnoException | undefined)?.code;
225 return (
226 error.reason._tag === "AlreadyExists" ||
227 error.reason._tag === "BadResource" ||
228 code === "ENOTEMPTY" ||
229 isWindowsRenameLock(error)
230 );
231}
232
233function parseLockOwnerPid(owner: string): number | null {
234 const separator = owner.indexOf("-");
235 if (separator <= 0) return null;
236 const pid = Number(owner.slice(0, separator));
237 return Number.isSafeInteger(pid) && pid > 0 ? pid : null;
238}
239
240function processIsAlive(pid: number): boolean {
241 try {
242 process.kill(pid, 0);
243 return true;
244 } catch (error) {
245 return (error as { readonly code?: unknown }).code !== "ESRCH";
246 }
247}
248
249const wrapInstallFailure =
250 (
251 reason: RelayClientInstallError["reason"],
252 message: string,
253 ): (<E, R>(
254 effect: Effect.Effect<void, E, R>,
255 ) => Effect.Effect<void, RelayClientInstallError, R>) =>
256 (effect) =>
257 effect.pipe(
258 Effect.mapError(
259 (cause) =>
260 new RelayClientInstallError({
261 reason,
262 message,
263 cause,
264 }),
265 ),
266 );
267
268export const makeCloudflaredRelayClient = Effect.fn("cloudflared.make")(function* (
269 options: CloudflaredRelayClientOptions,
270): Effect.fn.Return<
271 RelayClientShape,
272 never,
273 | ChildProcessSpawner.ChildProcessSpawner
274 | SecureRandom.SecureRandom
275 | Digest.Digest
276 | FileSystem.FileSystem
277 | HttpClient.HttpClient
278 | Path.Path
279> {
280 const random = yield* SecureRandom.SecureRandom;
281 const digest = yield* Digest.Digest;
282 const fileSystem = yield* FileSystem.FileSystem;
283 const httpClient = yield* HttpClient.HttpClient;
284 const path = yield* Path.Path;
285 const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
286 const installSemaphore = yield* Semaphore.make(1);
287 const platform = yield* HostProcess.Platform;
288 const arch = yield* HostProcess.Architecture;
289 const releaseAsset = options.releaseAsset ?? resolveReleaseAsset(platform, arch);
290 const loadCloudflaredConfig = Effect.suspend(() => CloudflaredConfig).pipe(Effect.orDie);
291 const managedRoot = path.join(options.baseDir, "tools", "cloudflared");
292 const managedPathFor = (version: string) =>
293 path.join(managedRoot, version, `${platform}-${arch}`, executableFileName(platform));
294 const managedPath = managedPathFor(CLOUDFLARED_VERSION);
295
296 const isExecutableFile = Effect.fn("cloudflared.isExecutableFile")(function* (
297 executablePath: string,
298 ) {
299 const info = yield* fileSystem.stat(executablePath).pipe(Effect.option);
300 if (Option.isNone(info) || info.value.type !== "File") return false;
301 return platform === "win32" || (info.value.mode & 0o111) !== 0;
302 });
303
304 const runVersionProbe = Effect.fn("cloudflared.runVersionProbe")(function* (
305 executablePath: string,
306 timeout: Duration.Input,
307 ) {
308 return yield* spawner
309 .string(ChildProcess.make(executablePath, ["version"], { stdin: "ignore", stderr: "ignore" }))
310 .pipe(
311 Effect.map(parseCloudflaredVersionOutput),
312 Effect.timeoutOption(timeout),
313 Effect.map(Option.getOrNull),
314 Effect.orElseSucceed(() => null),
315 );
316 });
317
318 // Cache successful version probes per file revision; do not let transient failures hide valid binaries.
319 const versionCache = yield* Cache.makeWith(
320 (key: VersionProbeKey) => runVersionProbe(key.executablePath, VERSION_PROBE_TIMEOUT),
321 {
322 capacity: 16,
323 timeToLive: (exit) =>
324 Exit.isSuccess(exit) && exit.value !== null ? Duration.infinity : Duration.zero,
325 },
326 );
327 const probeVersion = Effect.fn("cloudflared.probeVersion")(function* (executablePath: string) {
328 const info = yield* fileSystem.stat(executablePath).pipe(Effect.option);
329 if (Option.isNone(info) || info.value.type !== "File") return null;
330 if (platform !== "win32" && (info.value.mode & 0o111) === 0) return null;
331 return yield* Cache.get(
332 versionCache,
333 new VersionProbeKey({
334 executablePath,
335 size: Number(info.value.size),
336 mtimeMillis: Option.getOrUndefined(info.value.mtime)?.getTime() ?? 0,
337 }),
338 );
339 });
340
341 const compatibleCandidate = Effect.fn("cloudflared.compatibleCandidate")(function* (
342 executablePath: string,
343 source: RelayClientExecutableSource,
344 expectedVersion?: string,
345 ) {
346 const version = yield* probeVersion(executablePath);
347 if (version === null || compareCloudflaredVersions(version, CLOUDFLARED_MIN_VERSION) < 0) {
348 return null;
349 }
350 if (expectedVersion !== undefined && version !== expectedVersion) {
351 yield* Effect.logWarning("Ignoring a managed relay client that reports another version", {
352 executablePath,
353 expectedVersion,
354 version,
355 });
356 return null;
357 }
358 return { status: "available", executablePath, source, version } satisfies AvailableRelayClient;
359 });
360
361 // Newer managed folders may belong to another server; never use or prune them.
362 const olderManagedVersions = Effect.gen(function* () {
363 const entries = yield* fileSystem
364 .readDirectory(managedRoot)
365 .pipe(Effect.orElseSucceed(() => []));
366 return entries
367 .filter(
368 (entry) =>
369 parseVersion(entry) !== null &&
370 compareCloudflaredVersions(entry, CLOUDFLARED_VERSION) < 0,
371 )
372 .sort((left, right) => compareCloudflaredVersions(right, left));
373 });
374
375 const resolvePathExecutable = Effect.gen(function* () {
376 // Read hydrated PATH directly; ConfigProvider may have cached it before fixPath.
377 const rawPathValue = (yield* HostProcess.Environment).PATH;
378 // POSIX keeps the raw value, so trimming never turns " :/bin" into an empty entry.
379 const pathValue = platform === "win32" ? rawPathValue?.trim() : rawPathValue;
380 if (!pathValue) return null;
381 const delimiter = platform === "win32" ? ";" : ":";
382 const cwd = yield* HostProcess.WorkingDirectory;
383 for (const directory of pathValue.split(delimiter)) {
384 // POSIX shells search the working directory for an empty entry. Resolve it now: the
385 // connector may be launched from elsewhere.
386 const workingDirectoryEntry = directory === "" && platform !== "win32";
387 const trimmed = directory.trim().replace(/^"|"$/gu, "");
388 if (trimmed.length === 0 && !workingDirectoryEntry) continue;
389 const candidate = workingDirectoryEntry
390 ? path.resolve(cwd, executableFileName(platform))
391 : path.join(trimmed, executableFileName(platform));
392 if (!(yield* isExecutableFile(candidate))) continue;
393 const available = yield* compatibleCandidate(candidate, "path");
394 if (available) return available;
395 yield* Effect.logWarning("Skipping an incompatible relay client on PATH", {
396 executablePath: candidate,
397 minimumVersion: CLOUDFLARED_MIN_VERSION,
398 });
399 }
400 return null;
401 });
402
403 const missingStatus: RelayClientStatus = { status: "missing", version: CLOUDFLARED_VERSION };
404 const unsupportedStatus: RelayClientStatus = {
405 status: "unsupported",
406 platform,
407 arch,
408 version: CLOUDFLARED_VERSION,
409 };
410 const resolve: RelayClientShape["resolve"] = Effect.gen(function* () {
411 const config = yield* loadCloudflaredConfig;
412 if (Option.isSome(config.executableOverride)) {
413 const override = yield* compatibleCandidate(config.executableOverride.value, "override");
414 if (override) return override;
415 yield* Effect.logWarning(
416 `${CLOUDFLARED_PATH_ENV_NAME} must point to cloudflared ${CLOUDFLARED_MIN_VERSION} or newer`,
417 { executablePath: config.executableOverride.value },
418 );
419 return missingStatus;
420 }
421 const pinned = yield* compatibleCandidate(managedPath, "managed");
422 if (pinned) return pinned;
423 for (const version of yield* olderManagedVersions) {
424 const older = yield* compatibleCandidate(managedPathFor(version), "managed");
425 if (older) return older;
426 }
427 const pathExecutable = yield* resolvePathExecutable;
428 if (pathExecutable) return pathExecutable;
429 return releaseAsset ? missingStatus : unsupportedStatus;
430 }).pipe(Effect.withSpan("cloudflared.resolve"));
431
432 const pruneManagedVersions: RelayClientShape["pruneManagedVersions"] = Effect.gen(function* () {
433 // Retain the newest older release for rollback or older servers that cannot redownload it.
434 for (const version of (yield* olderManagedVersions).slice(1)) {
435 // Windows cannot delete another server's running connector; retry next prune.
436 yield* fileSystem.remove(path.join(managedRoot, version), { recursive: true }).pipe(
437 Effect.tap(() => Effect.logInfo("Removed an older managed relay client", { version })),
438 Effect.catch((cause) =>
439 Effect.logDebug("Could not remove an older managed relay client", { version, cause }),
440 ),
441 );
442 }
443 }).pipe(Effect.withSpan("cloudflared.pruneManagedVersions"));
444
445 const renameWhenUnlocked = (from: string, to: string) =>
446 fileSystem.rename(from, to).pipe(
447 Effect.retry({
448 times: ACTIVATE_RETRY_COUNT,
449 schedule: Schedule.spaced(ACTIVATE_RETRY_DELAY),
450 while: (error) => platform === "win32" && isWindowsRenameLock(error),
451 }),
452 );
453
454 const runCommand = Effect.fn("cloudflared.runCommand")(function* (
455 command: string,
456 args: ReadonlyArray<string>,
457 ) {
458 const child = yield* spawner.spawn(
459 ChildProcess.make(command, args, {
460 shell: false,
461 stdout: "ignore",
462 stderr: "ignore",
463 }),
464 );
465 const exitCode = Number(yield* child.exitCode);
466 if (exitCode !== 0) {
467 return yield* new CloudflaredCommandError({ command, exitCode });
468 }
469 });
470
471 const downloadAsset = Effect.fn("cloudflared.downloadAsset")(function* (
472 asset: CloudflaredReleaseAsset,
473 report: (stage: RelayClientInstallProgressStage) => Effect.Effect<void>,
474 ) {
475 yield* report("downloading");
476 const response = yield* httpClient.execute(HttpClientRequest.get(asset.url)).pipe(
477 Effect.timeout(CLOUDFLARED_DOWNLOAD_TIMEOUT),
478 Effect.flatMap(HttpClientResponse.filterStatusOk),
479 Effect.mapError(
480 (cause) =>
481 new RelayClientInstallError({
482 reason: "download_failed",
483 message: "Could not download the relay client.",
484 cause,
485 }),
486 ),
487 );
488 const bytes = new Uint8Array(
489 yield* response.arrayBuffer.pipe(
490 Effect.timeout(CLOUDFLARED_DOWNLOAD_TIMEOUT),
491 Effect.mapError(
492 (cause) =>
493 new RelayClientInstallError({
494 reason: "download_failed",
495 message: "Could not read the downloaded relay client binary.",
496 cause,
497 }),
498 ),
499 ),
500 );
501 yield* report("verifying");
502 const checksum = yield* digest.digest(Digest.sha256, bytes).pipe(
503 Effect.mapError(
504 (cause) =>
505 new RelayClientInstallError({
506 reason: "validation_failed",
507 message: "Could not verify the downloaded relay client checksum.",
508 cause,
509 }),
510 ),
511 );
512 if (Hex.encode(checksum) !== asset.sha256) {
513 return yield* new RelayClientInstallError({
514 reason: "invalid_checksum",
515 message: "Downloaded relay client checksum did not match the pinned release.",
516 });
517 }
518 return bytes;
519 });
520
521 const removeEmptyLock = (lockPath: string) =>
522 Effect.tryPromise({
523 try: () => NodeFSP.rmdir(lockPath),
524 catch: (cause) => new RelayClientLockRemoveError({ lockPath, cause }),
525 }).pipe(Effect.ignore);
526
527 const releaseOwnedLock = Effect.fn("cloudflared.releaseOwnedLock")(function* (
528 lockPath: string,
529 owner: string,
530 ) {
531 yield* fileSystem.remove(path.join(lockPath, owner), { force: true }).pipe(Effect.ignore);
532 // A replacement contains a different owner entry, so rmdir cannot remove it.
533 yield* removeEmptyLock(lockPath);
534 });
535
536 const acquireInstallLock = Effect.fn("cloudflared.acquireInstallLock")(function* (
537 lockPath: string,
538 ) {
539 const owner = `${process.pid}-${yield* random.uuidV4()}`;
540 const candidate = yield* Effect.acquireRelease(
541 fileSystem.makeTempDirectory({ directory: path.dirname(lockPath), prefix: ".lock-" }),
542 (directory) =>
543 fileSystem.remove(directory, { recursive: true, force: true }).pipe(Effect.ignore),
544 );
545 yield* fileSystem.writeFileString(path.join(candidate, owner), "");
546 for (let attempt = 0; attempt < INSTALL_LOCK_RETRY_COUNT; attempt += 1) {
547 const acquired = yield* Effect.acquireRelease(
548 // Publish a populated directory atomically, with no ownerless acquisition window.
549 fileSystem.rename(candidate, lockPath),
550 () => releaseOwnedLock(lockPath, owner),
551 ).pipe(
552 Effect.as(true),
553 Effect.catchIf(isLockContention, () => Effect.succeed(false)),
554 );
555 if (acquired) return;
556
557 const lockInfo = yield* fileSystem.stat(lockPath).pipe(Effect.option);
558 if (Option.isNone(lockInfo)) continue;
559 if (lockInfo.value.type === "Directory") {
560 const owners = yield* fileSystem
561 .readDirectory(lockPath)
562 .pipe(Effect.orElseSucceed(() => []));
563 let recovered = owners.length === 0;
564 for (const entry of owners) {
565 const pid = parseLockOwnerPid(entry);
566 if (pid !== null && !processIsAlive(pid)) {
567 yield* releaseOwnedLock(lockPath, entry);
568 recovered = true;
569 }
570 }
571 if (recovered) {
572 yield* removeEmptyLock(lockPath);
573 continue;
574 }
575 } else {
576 const now = yield* Clock.currentTimeMillis;
577 const mtime = lockInfo.value.mtime;
578 if (Option.isSome(mtime) && now - mtime.value.getTime() > INSTALL_LOCK_LEGACY_STALE_MS) {
579 // Nonrecursive removal also refuses a populated directory that replaced this file.
580 yield* fileSystem.remove(lockPath, { force: true }).pipe(Effect.ignore);
581 continue;
582 }
583 }
584 yield* Effect.sleep(INSTALL_LOCK_RETRY_DELAY);
585 }
586 return yield* new RelayClientInstallError({
587 reason: "install_locked",
588 message: "Another relay client installation is still in progress.",
589 });
590 });
591
592 const installUnlocked = Effect.fn("cloudflared.installUnlocked")(function* (
593 report: (stage: RelayClientInstallProgressStage) => Effect.Effect<void>,
594 ) {
595 yield* report("checking");
596 const config = yield* loadCloudflaredConfig;
597 if (Option.isSome(config.executableOverride)) {
598 const override = yield* compatibleCandidate(config.executableOverride.value, "override");
599 if (override) return override;
600 return yield* new RelayClientInstallError({
601 reason: "override_missing",
602 message: `${CLOUDFLARED_PATH_ENV_NAME} must point to cloudflared ${CLOUDFLARED_MIN_VERSION} or newer.`,
603 });
604 }
605 const existing = yield* compatibleCandidate(managedPath, "managed", CLOUDFLARED_VERSION);
606 if (existing) return existing;
607 if (!releaseAsset) {
608 return yield* new RelayClientInstallError({
609 reason: "unsupported_platform",
610 message: `T3 Code does not provide a managed relay client binary for ${platform}-${arch}.`,
611 });
612 }
613
614 const managedDirectory = path.dirname(managedPath);
615 const lockPath = `${managedPath}.lock`;
616 yield* fileSystem
617 .makeDirectory(managedDirectory, { recursive: true })
618 .pipe(
619 wrapInstallFailure("write_failed", "Could not create the relay client tool directory."),
620 );
621 yield* report("waiting_for_lock");
622 yield* acquireInstallLock(lockPath).pipe(
623 Effect.catchTags({
624 PlatformError: (cause) =>
625 Effect.fail(
626 new RelayClientInstallError({
627 reason: "write_failed",
628 message: "Could not acquire the relay client installation lock.",
629 cause,
630 }),
631 ),
632 }),
633 );
634 return yield* Effect.gen(function* () {
635 const afterLock = yield* compatibleCandidate(managedPath, "managed", CLOUDFLARED_VERSION);
636 if (afterLock) return afterLock;
637
638 const tempDirectory = yield* fileSystem.makeTempDirectoryScoped({
639 directory: managedDirectory,
640 prefix: ".install-",
641 });
642 const archivePath = path.join(
643 tempDirectory,
644 releaseAsset.archive === "tgz" ? "cloudflared.tgz" : executableFileName(platform),
645 );
646 const download = yield* downloadAsset(releaseAsset, report);
647 yield* report("installing");
648 yield* fileSystem
649 .writeFile(archivePath, download)
650 .pipe(wrapInstallFailure("write_failed", "Could not write the relay client download."));
651
652 const executablePath = path.join(tempDirectory, executableFileName(platform));
653 if (releaseAsset.archive === "tgz") {
654 yield* runCommand("tar", ["-xzf", archivePath, "-C", tempDirectory]).pipe(
655 wrapInstallFailure("write_failed", "Could not extract the relay client."),
656 );
657 }
658 if (platform !== "win32") {
659 yield* fileSystem
660 .chmod(executablePath, 0o755)
661 .pipe(wrapInstallFailure("write_failed", "Could not make the relay client executable."));
662 }
663 yield* report("validating");
664 // Verify the pin so mislabeled assets cannot cause repeated redownloads.
665 const installedVersion = yield* runVersionProbe(executablePath, INSTALL_VALIDATION_TIMEOUT);
666 if (installedVersion !== CLOUDFLARED_VERSION) {
667 return yield* new RelayClientInstallError({
668 reason: "validation_failed",
669 message:
670 installedVersion === null
671 ? "The downloaded relay client binary did not run."
672 : `The downloaded relay client reports version ${installedVersion}, not ${CLOUDFLARED_VERSION}.`,
673 });
674 }
675
676 const stagedPath = `${managedPath}.${yield* random.uuidV4()}.tmp`;
677 yield* report("activating");
678 yield* renameWhenUnlocked(executablePath, stagedPath).pipe(
679 wrapInstallFailure("write_failed", "Could not stage the relay client."),
680 );
681 yield* renameWhenUnlocked(stagedPath, managedPath).pipe(
682 wrapInstallFailure("write_failed", "Could not activate the relay client."),
683 Effect.ensuring(fileSystem.remove(stagedPath, { force: true }).pipe(Effect.ignore)),
684 );
685 return {
686 status: "available",
687 executablePath: managedPath,
688 source: "managed",
689 version: CLOUDFLARED_VERSION,
690 } satisfies AvailableRelayClient;
691 }).pipe(
692 Effect.scoped,
693 Effect.catchIf(
694 (cause) => !isRelayClientInstallError(cause),
695 (cause) =>
696 Effect.fail(
697 new RelayClientInstallError({
698 reason: "write_failed",
699 message: "Could not install the relay client.",
700 cause,
701 }),
702 ),
703 ),
704 );
705 });
706 const installWithProgress: RelayClientShape["installWithProgress"] = (report) =>
707 installSemaphore.withPermit(
708 installUnlocked((stage) =>
709 report({
710 type: "progress",
711 stage,
712 }),
713 ).pipe(Effect.scoped),
714 );
715 const install = installWithProgress(() => Effect.void);
716
717 return RelayClient.of({ resolve, install, installWithProgress, pruneManagedVersions });
718});
719
720export const layerCloudflared = (options: CloudflaredRelayClientOptions) =>
721 Layer.effect(RelayClient, makeCloudflaredRelayClient(options));