11import { renderHome } from "./home.jsx" ;
22import oldMappings from "./old_redirects.json" with { type : "json" } ;
3- import {
4- tryResolveAssetUrl ,
5- tryResolveLatestJson ,
6- tryResolvePluginUrl ,
7- tryResolveSchemaUrl ,
8- } from "./plugins.js" ;
3+ import { tryResolveAssetUrl , tryResolveLatestJson , tryResolvePluginUrl , tryResolveSchemaUrl } from "./plugins.js" ;
94import { readInfoFile } from "./readInfoFile.js" ;
105import robotsTxt from "./robots.txt" ;
116import styleCSS from "./style.css" ;
7+ import { fetchWithRetries } from "./utils/fetchWithRetries.js" ;
128import { LruCache } from "./utils/LruCache.js" ;
139import { getCliInfo } from "./utils/mod.js" ;
1410import { r2Get , r2Put } from "./utils/r2Cache.js" ;
@@ -94,7 +90,7 @@ export function createRequestHandler() {
9490 }
9591
9692 if ( url . pathname === "/" ) {
97- return renderHome ( ) . then ( ( home ) =>
93+ return renderHome ( url . origin ) . then ( ( home ) =>
9894 new Response ( home , {
9995 headers : {
10096 "content-type" : contentTypes . html ,
@@ -130,14 +126,16 @@ export function createRequestHandler() {
130126 githubUrl : string ,
131127 ctx ?: ExecutionContext ,
132128 ) : Promise < { body : ArrayBuffer | ReadableStream | null ; status : number ; contentType : string } > {
133- // L1: in-memory cache (already rewritten)
129+ // L1: in-memory cache
134130 const cached = memoryCache . get ( githubUrl ) ;
135131 if ( cached != null ) {
136132 return { body : cached . body , status : 200 , contentType : cached . contentType } ;
137133 }
138134
139135 const result = await fetchBody ( githubUrl , ctx ) ;
140- if ( result . status === 200 && result . body instanceof ArrayBuffer && result . body . byteLength <= MAX_MEM_CACHE_BODY_SIZE ) {
136+ if (
137+ result . status === 200 && result . body instanceof ArrayBuffer && result . body . byteLength <= MAX_MEM_CACHE_BODY_SIZE
138+ ) {
141139 memoryCache . set ( githubUrl , { body : result . body , contentType : result . contentType } ) ;
142140 }
143141
@@ -160,7 +158,11 @@ export function createRequestHandler() {
160158 }
161159
162160 // L3: fetch from GitHub
163- const response = await fetchWithRetries ( githubUrl ) ;
161+ const response = await fetchWithRetries ( githubUrl , {
162+ // don't need the github token here because these assets
163+ // are not part of the github api
164+ headers : { "user-agent" : "dprint-plugins" } ,
165+ } ) ;
164166 if ( ! response . ok ) {
165167 if ( response . status !== 404 ) {
166168 console . error ( `GitHub fetch error: ${ response . status } ${ response . statusText } for ${ githubUrl } ` ) ;
@@ -227,27 +229,12 @@ function githubUrlToAssetPath(githubUrl: string) {
227229 return `/${ username } /${ repo } /${ tag } /asset/${ fileName } ` ;
228230}
229231
230-
231232function contentTypeForUrl ( url : string ) {
232233 if ( url . endsWith ( ".wasm" ) ) return contentTypes . wasm ;
233234 if ( url . endsWith ( ".json" ) || url . endsWith ( ".exe-plugin" ) ) return contentTypes . json ;
234235 return contentTypes . octetStream ;
235236}
236237
237- async function fetchWithRetries ( url : string , retries = 3 ) : Promise < Response > {
238- for ( let i = 0 ; i <= retries ; i ++ ) {
239- const response = await fetch ( url , {
240- headers : { "user-agent" : "dprint-plugins" } ,
241- } ) ;
242- if ( response . status < 500 || i === retries ) {
243- return response ;
244- }
245- console . error ( `GitHub fetch attempt ${ i + 1 } failed: ${ response . status } for ${ url } ` ) ;
246- await new Promise ( ( resolve ) => setTimeout ( resolve , Math . min ( 1000 * 2 ** i , 2500 ) ) ) ;
247- }
248- throw new Error ( "unreachable" ) ;
249- }
250-
251238function create404Response ( ) {
252239 return new Response ( null , {
253240 status : 404 ,
0 commit comments