@@ -2,12 +2,16 @@ import { sha256 } from "@noble/hashes/sha2";
22import * as NodeServices from "@effect/platform-node/NodeServices" ;
33import { describe , expect , it } from "@effect/vitest" ;
44import * as ConfigProvider from "effect/ConfigProvider" ;
5+ import * as Deferred from "effect/Deferred" ;
56import * as Effect from "effect/Effect" ;
67import * as Hex from "effect/encoding/Hex" ;
8+ import * as Fiber from "effect/Fiber" ;
79import * as FileSystem from "effect/FileSystem" ;
810import * as Layer from "effect/Layer" ;
11+ import * as PlatformError from "effect/PlatformError" ;
912import * as Sink from "effect/Sink" ;
1013import * as Stream from "effect/Stream" ;
14+ import * as TestClock from "effect/testing/TestClock" ;
1115import { HttpClient , HttpClientResponse } from "effect/http" ;
1216import { ChildProcess , ChildProcessSpawner } from "effect/process" ;
1317import { HostProcessArchitecture , HostProcessPlatform } from "./hostProcess.ts" ;
@@ -18,9 +22,9 @@ import * as RelayClient from "./relayClient.ts";
1822// POSIX exec bits that NTFS never reports; the win32 branch skips that check.
1923const windowsHost = HostProcessPlatform . defaultValue ( ) === "win32" ;
2024
21- const layerHostRuntime = ( env : Record < string , string > = { } ) =>
25+ const layerHostRuntime = ( env : Record < string , string > = { } , platform : NodeJS . Platform = "linux" ) =>
2226 Layer . mergeAll (
23- Layer . succeed ( HostProcessPlatform , "linux" ) ,
27+ Layer . succeed ( HostProcessPlatform , platform ) ,
2428 Layer . succeed ( HostProcessArchitecture , "x64" ) ,
2529 ConfigProvider . layer ( ConfigProvider . fromEnv ( { env } ) ) ,
2630 ) ;
@@ -65,6 +69,64 @@ const layerSpawner = (commands: Array<string>) =>
6569 ) ,
6670 ) ;
6771
72+ const renameError = ( code : string , path : string ) =>
73+ PlatformError . systemError ( {
74+ _tag : code === "EBUSY" ? "Busy" : code === "EACCES" ? "PermissionDenied" : "Unknown" ,
75+ module : "FileSystem" ,
76+ method : "rename" ,
77+ pathOrDescriptor : path ,
78+ cause : Object . assign ( new Error ( code ) , { code } ) ,
79+ } ) ;
80+
81+ type InstallRename = "staging" | "activation" ;
82+
83+ /**
84+ * Fails the first `failures` staging or activation renames with `code`, as Windows does while
85+ * another process holds the file. Staging moves the binary to a `.tmp` name beside its final path.
86+ */
87+ const makeLockedRenames = (
88+ fileSystem : FileSystem . FileSystem ,
89+ input : { readonly rename : InstallRename ; readonly failures : number ; readonly code : string } ,
90+ ) =>
91+ Effect . gen ( function * ( ) {
92+ const firstAttempt = yield * Deferred . make < void > ( ) ;
93+ const renames = { attempts : 0 } ;
94+ const locked = FileSystem . make ( {
95+ ...fileSystem ,
96+ rename : ( from , to ) =>
97+ Effect . suspend ( ( ) => {
98+ if ( ( to . endsWith ( ".tmp" ) ? "staging" : "activation" ) !== input . rename ) {
99+ return fileSystem . rename ( from , to ) ;
100+ }
101+ renames . attempts += 1 ;
102+ return renames . attempts <= input . failures
103+ ? Deferred . succeed ( firstAttempt , undefined ) . pipe (
104+ Effect . andThen ( Effect . fail ( renameError ( input . code , to ) ) ) ,
105+ )
106+ : fileSystem . rename ( from , to ) ;
107+ } ) ,
108+ } ) ;
109+ return { locked, renames, firstAttempt } ;
110+ } ) ;
111+
112+ const testBinary = new TextEncoder ( ) . encode ( "test-cloudflared-binary" ) ;
113+ const testReleaseAsset = {
114+ url : "https://example.test/cloudflared" ,
115+ sha256 : Hex . encode ( sha256 ( testBinary ) ) ,
116+ archive : "binary" ,
117+ } as const ;
118+
119+ const layerInstallRuntime = ( platform : NodeJS . Platform ) =>
120+ Layer . mergeAll (
121+ NodeServices . layer ,
122+ layerHttpClient ( testBinary ) ,
123+ layerSpawner ( [ ] ) ,
124+ layerHostRuntime ( { PATH : "" } , platform ) ,
125+ ) ;
126+
127+ const managedDirectory = ( baseDir : string , platform : NodeJS . Platform ) =>
128+ `${ baseDir } /tools/cloudflared/${ RelayClient . CLOUDFLARED_VERSION } /${ platform } -x64` ;
129+
68130describe ( "RelayClient" , ( ) => {
69131 it . effect . skipIf ( windowsHost ) (
70132 "resolves explicit overrides before managed and PATH executables" ,
@@ -278,4 +340,95 @@ describe("RelayClient", () => {
278340 ) ;
279341 } ,
280342 ) ;
343+
344+ it . effect . each ( [
345+ [ "staging" , "EBUSY" ] ,
346+ [ "activation" , "EACCES" ] ,
347+ ] as const ) ( "retries %s on Windows while another process holds the binary" , ( [ rename , code ] ) =>
348+ Effect . gen ( function * ( ) {
349+ const fileSystem = yield * FileSystem . FileSystem ;
350+ const baseDir = yield * fileSystem . makeTempDirectoryScoped ( {
351+ prefix : "t3-cloudflared-test-" ,
352+ } ) ;
353+ const { locked, renames, firstAttempt } = yield * makeLockedRenames ( fileSystem , {
354+ rename,
355+ failures : 2 ,
356+ code,
357+ } ) ;
358+ const manager = yield * RelayClient . makeCloudflaredRelayClient ( {
359+ baseDir,
360+ releaseAsset : testReleaseAsset ,
361+ } ) . pipe ( Effect . provideService ( FileSystem . FileSystem , locked ) ) ;
362+
363+ const installing = yield * manager . install . pipe ( Effect . forkChild ) ;
364+ yield * Deferred . await ( firstAttempt ) ;
365+ yield * TestClock . adjust ( "1 second" ) ;
366+ const installed = yield * Fiber . join ( installing ) ;
367+
368+ expect ( renames . attempts ) . toBe ( 3 ) ;
369+ expect ( new TextDecoder ( ) . decode ( yield * fileSystem . readFile ( installed . executablePath ) ) ) . toBe (
370+ "test-cloudflared-binary" ,
371+ ) ;
372+ expect ( yield * fileSystem . readDirectory ( managedDirectory ( baseDir , "win32" ) ) ) . toEqual ( [
373+ "cloudflared.exe" ,
374+ ] ) ;
375+ } ) . pipe ( Effect . scoped , Effect . provide ( layerInstallRuntime ( "win32" ) ) ) ,
376+ ) ;
377+
378+ it . effect . each ( [
379+ [ "staging" , "EPERM" , "Could not stage the relay client." ] ,
380+ [ "activation" , "EBUSY" , "Could not activate the relay client." ] ,
381+ ] as const ) ( "gives up on a Windows %s lock that does not clear" , ( [ rename , code , message ] ) =>
382+ Effect . gen ( function * ( ) {
383+ const fileSystem = yield * FileSystem . FileSystem ;
384+ const baseDir = yield * fileSystem . makeTempDirectoryScoped ( {
385+ prefix : "t3-cloudflared-test-" ,
386+ } ) ;
387+ const { locked, renames, firstAttempt } = yield * makeLockedRenames ( fileSystem , {
388+ rename,
389+ failures : Infinity ,
390+ code,
391+ } ) ;
392+ const manager = yield * RelayClient . makeCloudflaredRelayClient ( {
393+ baseDir,
394+ releaseAsset : testReleaseAsset ,
395+ } ) . pipe ( Effect . provideService ( FileSystem . FileSystem , locked ) ) ;
396+
397+ const installing = yield * manager . install . pipe ( Effect . flip , Effect . forkChild ) ;
398+ yield * Deferred . await ( firstAttempt ) ;
399+ yield * TestClock . adjust ( "1 minute" ) ;
400+ const error = yield * Fiber . join ( installing ) ;
401+
402+ expect ( error . message ) . toBe ( message ) ;
403+ expect ( renames . attempts ) . toBe ( 41 ) ;
404+ // Nothing is installed, and the staged copy, download folder, and install lock are gone.
405+ expect ( yield * fileSystem . readDirectory ( managedDirectory ( baseDir , "win32" ) ) ) . toEqual ( [ ] ) ;
406+ } ) . pipe ( Effect . scoped , Effect . provide ( layerInstallRuntime ( "win32" ) ) ) ,
407+ ) ;
408+
409+ it . effect . each ( [
410+ [ "linux" , "EBUSY" ] ,
411+ [ "win32" , "EXDEV" ] ,
412+ ] as const ) ( "fails staging at once on %s for %s" , ( [ platform , code ] ) =>
413+ Effect . gen ( function * ( ) {
414+ const fileSystem = yield * FileSystem . FileSystem ;
415+ const baseDir = yield * fileSystem . makeTempDirectoryScoped ( {
416+ prefix : "t3-cloudflared-test-" ,
417+ } ) ;
418+ const { locked, renames } = yield * makeLockedRenames ( fileSystem , {
419+ rename : "staging" ,
420+ failures : Infinity ,
421+ code,
422+ } ) ;
423+ const manager = yield * RelayClient . makeCloudflaredRelayClient ( {
424+ baseDir,
425+ releaseAsset : testReleaseAsset ,
426+ } ) . pipe ( Effect . provideService ( FileSystem . FileSystem , locked ) ) ;
427+
428+ const error = yield * manager . install . pipe ( Effect . flip ) ;
429+
430+ expect ( error . message ) . toBe ( "Could not stage the relay client." ) ;
431+ expect ( renames . attempts ) . toBe ( 1 ) ;
432+ } ) . pipe ( Effect . scoped , Effect . provide ( layerInstallRuntime ( platform ) ) ) ,
433+ ) ;
281434} ) ;
0 commit comments