11import type { Response , NextFunction } from 'express'
2- import got from 'got '
2+ import { fetchWithRetry } from '@/frame/lib/fetch-utils '
33
44import statsd from '@/observability/lib/statsd'
55import {
@@ -190,33 +190,38 @@ export default async function archivedEnterpriseVersions(
190190 }
191191 // Retrieve the page from the archived repo
192192 const doGet = ( ) =>
193- got ( getProxyPath ( req . path , requestedVersion ) , {
194- throwHttpErrors : false ,
195- retry : retryConfiguration ,
196- timeout : timeoutConfiguration ,
197- } )
193+ fetchWithRetry (
194+ getProxyPath ( req . path , requestedVersion ) ,
195+ { } ,
196+ {
197+ retries : retryConfiguration . limit ,
198+ timeout : timeoutConfiguration . response ,
199+ throwHttpErrors : false ,
200+ } ,
201+ )
198202
199203 const statsdTags = [ `version:${ requestedVersion } ` ]
200204 const r = await statsd . asyncTimer ( doGet , 'archive_enterprise_proxy' , [
201205 ...statsdTags ,
202206 `path:${ req . path } ` ,
203207 ] ) ( )
204208
205- if ( r . statusCode === 200 ) {
209+ if ( r . status === 200 ) {
210+ const body = await r . text ( )
206211 const [ , withoutLanguagePath ] = splitByLanguage ( req . path )
207212 const isDeveloperPage = withoutLanguagePath ?. startsWith (
208213 `/enterprise/${ requestedVersion } /developer` ,
209214 )
210215 res . set ( 'x-robots-tag' , 'noindex' )
211216
212217 // make stubbed redirect files (which exist in versions <2.13) redirect with a 301
213- const staticRedirect = r . body . match ( patterns . staticRedirect )
218+ const staticRedirect = body . match ( patterns . staticRedirect )
214219 if ( staticRedirect ) {
215220 cacheAggressively ( res )
216221 return res . redirect ( redirectCode , staticRedirect [ 1 ] )
217222 }
218223
219- res . set ( 'content-type' , r . headers [ 'content-type' ] )
224+ res . set ( 'content-type' , r . headers . get ( 'content-type' ) || '' )
220225
221226 cacheAggressively ( res )
222227
@@ -230,7 +235,7 @@ export default async function archivedEnterpriseVersions(
230235 // `x-host` is a custom header set by Fastly.
231236 // GLB automatically deletes the `x-forwarded-host` header.
232237 const host = req . get ( 'x-host' ) || req . get ( 'x-forwarded-host' ) || req . get ( 'host' )
233- r . body = r . body
238+ let modifiedBody = body
234239 . replaceAll (
235240 `${ OLD_AZURE_BLOB_ENTERPRISE_DIR } /${ requestedVersion } /assets/cb-` ,
236241 `${ ENTERPRISE_GH_PAGES_URL_PREFIX } ${ requestedVersion } /assets/cb-` ,
@@ -239,6 +244,8 @@ export default async function archivedEnterpriseVersions(
239244 `${ OLD_AZURE_BLOB_ENTERPRISE_DIR } /${ requestedVersion } /` ,
240245 `${ req . protocol } ://${ host } /enterprise-server@${ requestedVersion } /` ,
241246 )
247+
248+ return res . send ( modifiedBody )
242249 }
243250
244251 // Releases 3.1 and lower were previously hosted in the
@@ -247,23 +254,42 @@ export default async function archivedEnterpriseVersions(
247254 // The image paths all need to be updated to reference the images in the
248255 // new archived enterprise repo's root assets directory.
249256 if ( versionSatisfiesRange ( requestedVersion , `<${ firstReleaseStoredInBlobStorage } ` ) ) {
250- r . body = r . body . replaceAll (
257+ let modifiedBody = body . replaceAll (
251258 `${ OLD_GITHUB_IMAGES_ENTERPRISE_DIR } /${ requestedVersion } ` ,
252259 `${ ENTERPRISE_GH_PAGES_URL_PREFIX } ${ requestedVersion } ` ,
253260 )
254261 if ( versionSatisfiesRange ( requestedVersion , '<=2.18' ) && isDeveloperPage ) {
255- r . body = r . body . replaceAll (
262+ modifiedBody = modifiedBody . replaceAll (
256263 `${ OLD_DEVELOPER_SITE_CONTAINER } /${ requestedVersion } ` ,
257264 `${ ENTERPRISE_GH_PAGES_URL_PREFIX } ${ requestedVersion } /developer` ,
258265 )
259266 // Update all hrefs to add /developer to the path
260- r . body = r . body . replaceAll (
267+ modifiedBody = modifiedBody . replaceAll (
261268 `="/enterprise/${ requestedVersion } ` ,
262269 `="/enterprise/${ requestedVersion } /developer` ,
263270 )
264271 // The changelog is the only thing remaining on developer.github.com
265- r . body = r . body . replaceAll ( 'href="/changes' , 'href="https://developer.github.com/changes' )
272+ modifiedBody = modifiedBody . replaceAll (
273+ 'href="/changes' ,
274+ 'href="https://developer.github.com/changes' ,
275+ )
266276 }
277+
278+ // Continue with remaining replacements
279+ modifiedBody = modifiedBody . replaceAll (
280+ / = " ( \. \. \/ ) * a s s e t s / g,
281+ `="${ ENTERPRISE_GH_PAGES_URL_PREFIX } ${ requestedVersion } /assets` ,
282+ )
283+
284+ // Fix broken hrefs on the 2.16 landing page
285+ if ( requestedVersion === '2.16' && req . path === '/en/enterprise/2.16' ) {
286+ modifiedBody = modifiedBody . replaceAll ( 'ref="/en/enterprise' , 'ref="/en/enterprise/2.16' )
287+ }
288+
289+ // Remove the search results container from the page
290+ modifiedBody = modifiedBody . replaceAll ( '<div id="search-results-container"></div>' , '' )
291+
292+ return res . send ( modifiedBody )
267293 }
268294
269295 // In all releases, some assets were incorrectly scraped and contain
@@ -275,21 +301,21 @@ export default async function archivedEnterpriseVersions(
275301 // We want to update the URLs in the format
276302 // "../../../../../../assets/" to prefix the assets directory with the
277303 // new archived enterprise repo URL.
278- r . body = r . body . replaceAll (
304+ let modifiedBody = body . replaceAll (
279305 / = " ( \. \. \/ ) * a s s e t s / g,
280306 `="${ ENTERPRISE_GH_PAGES_URL_PREFIX } ${ requestedVersion } /assets` ,
281307 )
282308
283309 // Fix broken hrefs on the 2.16 landing page
284310 if ( requestedVersion === '2.16' && req . path === '/en/enterprise/2.16' ) {
285- r . body = r . body . replaceAll ( 'ref="/en/enterprise' , 'ref="/en/enterprise/2.16' )
311+ modifiedBody = modifiedBody . replaceAll ( 'ref="/en/enterprise' , 'ref="/en/enterprise/2.16' )
286312 }
287313
288314 // Remove the search results container from the page, which removes a white
289315 // box that prevents clicking on page links
290- r . body = r . body . replaceAll ( '<div id="search-results-container"></div>' , '' )
316+ modifiedBody = modifiedBody . replaceAll ( '<div id="search-results-container"></div>' , '' )
291317
292- return res . send ( r . body )
318+ return res . send ( modifiedBody )
293319 }
294320 // In releases 2.13 - 2.17, we lost access to frontmatter redirects
295321 // during the archival process. This workaround finds potentially
0 commit comments