55 cliReleaseIndexPageUrl ,
66 cliReleaseChannelOf ,
77 newestCliReleaseVersion ,
8+ releaseAssetUrl ,
9+ updateFeedFileName ,
810 type CliReleaseChannel ,
911} from "@t3tools/shared/cliRelease" ;
1012import * as Console from "effect/Console" ;
@@ -24,6 +26,7 @@ import {
2426 ensurePinnedRuntimeInstalled ,
2527 launcherOwnsVersionsDir ,
2628 pinnedRuntimeCommand ,
29+ isUnpublishedRelease ,
2730 PinnedRuntimeInstallError ,
2831 pinnedRuntimePaths ,
2932 pinnedRuntimeVersionsDir ,
@@ -33,7 +36,7 @@ import {
3336} from "../cloud/pinnedRuntime.ts" ;
3437import { compareExactServiceVersions , isExactServiceVersion } from "../cloud/serviceProtocol.ts" ;
3538import * as ManagedPolicy from "../policy/ManagedPolicy.ts" ;
36- import { managedUpdateRefusal } from "../policy/managedUpdates.ts" ;
39+ import { managedUpdateMirror , managedUpdateRefusal } from "../policy/managedUpdates.ts" ;
3740import * as ProcessRunner from "../processRunner.ts" ;
3841import { isProcessAlive , readPersistedServerRuntimeState } from "../serverRuntimeState.ts" ;
3942import { projectLocationFlags , resolveCliAuthConfig } from "./config.ts" ;
@@ -94,6 +97,47 @@ export const resolveNewestVersion = Effect.fn("cli.update.resolve_newest")(funct
9497 return yield * new CliUpdateError ( { reason : `No published ${ channel } release was found.` } ) ;
9598} ) ;
9699
100+ /** The version an update mirror's feed file for this platform offers on `channel`. */
101+ export const resolveMirrorVersion = Effect . fn ( "cli.update.resolve_mirror" ) ( function * (
102+ mirror : { readonly organization : string ; readonly feedUrl : string } ,
103+ channel : CliReleaseChannel ,
104+ ) {
105+ if ( channel === "preview" ) {
106+ return yield * new CliUpdateError ( {
107+ reason : `${ mirror . organization } 's update mirror has no preview builds.` ,
108+ } ) ;
109+ }
110+ const httpClient = yield * HttpClient . HttpClient ;
111+ const fileName = updateFeedFileName (
112+ channel === "nightly" ? "nightly" : "latest" ,
113+ yield * HostProcess . Platform ,
114+ yield * HostProcess . Architecture ,
115+ ) ;
116+ const url = releaseAssetUrl ( mirror . feedUrl , fileName ) ;
117+ // Not the URL, whose query can hold the mirror's access token.
118+ const unreadable = new CliUpdateError ( {
119+ reason : `Could not read ${ fileName } from ${ mirror . organization } 's update mirror.` ,
120+ } ) ;
121+ const body = yield * httpClient . get ( url ) . pipe (
122+ Effect . flatMap ( HttpClientResponse . filterStatusOk ) ,
123+ Effect . flatMap ( ( response ) => response . text ) ,
124+ Effect . mapError ( ( ) => unreadable ) ,
125+ Effect . timeoutOrElse ( {
126+ duration : RELEASE_INDEX_TIMEOUT ,
127+ orElse : ( ) => Effect . fail ( unreadable ) ,
128+ } ) ,
129+ ) ;
130+ const version = / ^ v e r s i o n : \s * [ " ' ] ? ( [ ^ " ' \s ] + ) / m. exec ( body ) ?. [ 1 ] ;
131+ if (
132+ version === undefined ||
133+ ! isExactServiceVersion ( version ) ||
134+ cliReleaseChannelOf ( version ) !== channel
135+ ) {
136+ return yield * unreadable ;
137+ }
138+ return version ;
139+ } ) ;
140+
97141/**
98142 * Only a launcher that already points into this home's `runtime/versions` tree is touched; a
99143 * plain copy of the executable, or a launcher for some other install, is left alone. It is
@@ -320,6 +364,7 @@ export const runUpdate = Effect.fn("cli.update.run")(function* (input: {
320364 const httpClient = yield * HttpClient . HttpClient ;
321365 const service = yield * BootService . BootService ;
322366 const policy = yield * ManagedPolicy . ManagedPolicy ;
367+ const mirror = managedUpdateMirror ( policy . document ) ;
323368
324369 const currentVersion = packageJson . version ;
325370 const channel = input . channel ?? cliReleaseChannelOf ( currentVersion ) ;
@@ -338,9 +383,11 @@ export const runUpdate = Effect.fn("cli.update.run")(function* (input: {
338383 const progress = createUpdateProgress ( ) ;
339384 progress . status ( "Checking for updates..." ) ;
340385 const targetVersion = yield * (
341- input . requestedVersion === undefined
342- ? resolveNewestVersion ( channel )
343- : Effect . succeed ( input . requestedVersion )
386+ input . requestedVersion !== undefined
387+ ? Effect . succeed ( input . requestedVersion )
388+ : mirror === undefined
389+ ? resolveNewestVersion ( channel )
390+ : resolveMirrorVersion ( mirror , channel )
344391 ) . pipe ( Effect . ensuring ( Effect . sync ( progress . finish ) ) ) ;
345392 const targetChannel = cliReleaseChannelOf ( targetVersion ) ;
346393
@@ -455,7 +502,8 @@ export const runUpdate = Effect.fn("cli.update.run")(function* (input: {
455502 httpClient,
456503 platform,
457504 arch,
458- releaseBaseUrl : environment [ CLI_RELEASE_BASE_URL_ENV ] ?. trim ( ) || undefined ,
505+ releaseBaseUrl : mirror ?. feedUrl ?? ( environment [ CLI_RELEASE_BASE_URL_ENV ] ?. trim ( ) || undefined ) ,
506+ requirePublishedRelease : mirror !== undefined ,
459507 validate : ( paths ) =>
460508 runner
461509 . run ( {
@@ -481,17 +529,15 @@ export const runUpdate = Effect.fn("cli.update.run")(function* (input: {
481529 ) ,
482530 } ) . pipe (
483531 Effect . ensuring ( Effect . sync ( progress . finish ) ) ,
484- Effect . catchIf (
485- ( error ) : error is PinnedRuntimeInstallError =>
486- error . _tag === "PinnedRuntimeInstallError" &&
487- error . step . startsWith ( "downloading the t3 release checksums" ) &&
488- String ( error . cause ) . includes ( "404" ) ,
489- ( ) =>
490- Effect . fail (
491- new CliUpdateError ( {
492- reason : `No release archive was published for t3@${ targetVersion } .` ,
493- } ) ,
494- ) ,
532+ Effect . catchIf ( isUnpublishedRelease , ( ) =>
533+ Effect . fail (
534+ new CliUpdateError ( {
535+ reason :
536+ mirror === undefined
537+ ? `No release archive was published for t3@${ targetVersion } .`
538+ : `${ mirror . organization } 's update mirror has no t3@${ targetVersion } for this machine.` ,
539+ } ) ,
540+ ) ,
495541 ) ,
496542 ) ;
497543
0 commit comments