@@ -5,7 +5,7 @@ export default {
55 async fetch ( request , env ) {
66 const url = new URL ( request . url ) ;
77
8- if ( ! url . pathname . startsWith ( "/latest/" ) ) {
8+ if ( ! isAllowedLatestPath ( url . pathname ) ) {
99 return new Response ( "Not found" , { status : 404 } ) ;
1010 }
1111
@@ -18,35 +18,65 @@ export default {
1818 ) ;
1919
2020 if ( secondaryCountryCodes . has ( country . toUpperCase ( ) ) && hasSecondaryS3Config ( env ) ) {
21- const objectKey = objectKeyForPath ( url . pathname , env . SECONDARY_S3_PREFIX || "" ) ;
22- const downloadMetadata = downloadMetadataForPath ( url . pathname ) ;
23- const signedUrl = await presignS3GetUrl ( {
24- endpoint : env . SECONDARY_S3_ENDPOINT ,
25- bucket : env . SECONDARY_S3_BUCKET ,
26- key : objectKey ,
27- region : env . SECONDARY_S3_REGION || "auto" ,
28- accessKeyId : env . SECONDARY_S3_ACCESS_KEY_ID ,
29- secretAccessKey : env . SECONDARY_S3_SECRET_ACCESS_KEY ,
30- expiresInSeconds : ttlSeconds ( env . SECONDARY_S3_SIGNED_URL_TTL_SECONDS ) ,
31- responseHeaders : downloadMetadata
32- ? {
33- "response-content-disposition" : `attachment; filename="${ downloadMetadata . filename } "` ,
34- "response-content-type" : downloadMetadata . contentType ,
35- }
36- : { } ,
37- } ) ;
38-
39- return redirect ( signedUrl ) ;
21+ try {
22+ const objectKey = objectKeyForPath ( url . pathname , env . SECONDARY_S3_PREFIX || "" ) ;
23+ const downloadMetadata = downloadMetadataForPath ( url . pathname ) ;
24+ const signedUrl = await presignS3GetUrl ( {
25+ endpoint : env . SECONDARY_S3_ENDPOINT ,
26+ bucket : env . SECONDARY_S3_BUCKET ,
27+ key : objectKey ,
28+ region : env . SECONDARY_S3_REGION || "auto" ,
29+ accessKeyId : env . SECONDARY_S3_ACCESS_KEY_ID ,
30+ secretAccessKey : env . SECONDARY_S3_SECRET_ACCESS_KEY ,
31+ expiresInSeconds : ttlSeconds ( env . SECONDARY_S3_SIGNED_URL_TTL_SECONDS ) ,
32+ responseHeaders : downloadMetadata
33+ ? {
34+ "response-content-disposition" : `attachment; filename="${ downloadMetadata . filename } "` ,
35+ "response-content-type" : downloadMetadata . contentType ,
36+ }
37+ : { } ,
38+ } ) ;
39+
40+ return redirect ( signedUrl ) ;
41+ } catch ( error ) {
42+ console . error (
43+ JSON . stringify ( {
44+ event : "secondary_s3_presign_failed" ,
45+ path : url . pathname ,
46+ country,
47+ error : error instanceof Error ? error . message : String ( error ) ,
48+ } ) ,
49+ ) ;
50+ }
4051 }
4152
4253 if ( ! env . GLOBAL_MIRROR_BASE_URL ) {
4354 return new Response ( "Missing GLOBAL_MIRROR_BASE_URL" , { status : 500 } ) ;
4455 }
4556
46- return redirect ( withPathAndSearch ( env . GLOBAL_MIRROR_BASE_URL , url . pathname , url . search ) . toString ( ) ) ;
57+ return redirect ( withPathAndSearch ( env . GLOBAL_MIRROR_BASE_URL , url . pathname , url . search ) . toString ( ) , {
58+ "X-Mirror-Fallback" : "global" ,
59+ } ) ;
4760 } ,
4861} ;
4962
63+ function isAllowedLatestPath ( pathname ) {
64+ const aliases = new Set ( [
65+ "/latest/win" ,
66+ "/latest/mac-arm64" ,
67+ "/latest/mac-intel" ,
68+ "/latest/checksums" ,
69+ "/latest/manifest" ,
70+ "/latest/appcast.xml" ,
71+ "/latest/appcast-x64.xml" ,
72+ ] ) ;
73+ if ( aliases . has ( pathname ) ) {
74+ return true ;
75+ }
76+
77+ return / ^ \/ l a t e s t \/ m a c \/ ( a r m 6 4 | i n t e l ) \/ [ ^ / ] + \. ( z i p | d e l t a ) $ / . test ( pathname ) ;
78+ }
79+
5080function hasSecondaryS3Config ( env ) {
5181 return Boolean (
5282 env . SECONDARY_S3_ENDPOINT &&
@@ -64,12 +94,13 @@ function ttlSeconds(value) {
6494 return Math . min ( Math . max ( parsed , 1 ) , 604800 ) ;
6595}
6696
67- function redirect ( location ) {
97+ function redirect ( location , extraHeaders = { } ) {
6898 return new Response ( null , {
6999 status : 302 ,
70100 headers : {
71101 Location : location ,
72102 "Cache-Control" : "private, no-store" ,
103+ ...extraHeaders ,
73104 } ,
74105 } ) ;
75106}
@@ -96,7 +127,7 @@ function downloadMetadataForPath(pathname) {
96127 contentType : "application/x-apple-diskimage" ,
97128 } ,
98129 "mac-intel" : {
99- filename : "Codex-mac-intel .dmg" ,
130+ filename : "Codex-mac-x64 .dmg" ,
100131 contentType : "application/x-apple-diskimage" ,
101132 } ,
102133 win : {
0 commit comments