@@ -13,7 +13,7 @@ export interface FileDownloadInfoOutput {
13
13
/**
14
14
* @returns null when the file doesn't exist
15
15
*/
16
- export function fileDownloadInfo ( params : {
16
+ export async function fileDownloadInfo ( params : {
17
17
repo : RepoId ;
18
18
path : string ;
19
19
revision ?: string ;
@@ -28,43 +28,45 @@ export function fileDownloadInfo(params: {
28
28
params . repo . name
29
29
} /${ params . raw ? "raw" : "resolve" } /${ encodeURIComponent ( params . revision ?? "main" ) } /${ params . path } `;
30
30
31
- return fileDownloadInfoInternal ( url , params . credentials ) ;
32
- }
33
-
34
- async function fileDownloadInfoInternal (
35
- url : string ,
36
- credentials ?: Credentials ,
37
- redirects = 0
38
- ) : Promise < FileDownloadInfoOutput | null > {
39
- if ( redirects >= 20 ) {
40
- throw new Error ( "Too many redirects" ) ;
41
- }
42
-
43
- const resp = await fetch ( url , {
31
+ let resp = await fetch ( url , {
44
32
method : "HEAD" ,
45
- headers : credentials
33
+ headers : params . credentials
46
34
? {
47
- Authorization : `Bearer ${ credentials . accessToken } ` ,
35
+ Authorization : `Bearer ${ params . credentials . accessToken } ` ,
48
36
}
49
37
: { } ,
50
38
redirect : "manual" ,
51
39
} ) ;
52
40
41
+ let redirects = 0 ;
42
+ while ( resp . status >= 300 && resp . status < 400 && new URL ( resp . headers . get ( "Location" ) ! ) . host === new URL ( url ) . host ) {
43
+ if ( ++ redirects >= 20 ) {
44
+ throw new Error ( "Too many redirects" ) ;
45
+ }
46
+
47
+ resp = await fetch ( url , {
48
+ method : "HEAD" ,
49
+ headers : params . credentials
50
+ ? {
51
+ Authorization : `Bearer ${ params . credentials . accessToken } ` ,
52
+ }
53
+ : { } ,
54
+ redirect : "manual" ,
55
+ } ) ;
56
+ }
57
+
53
58
if ( resp . status === 404 && resp . headers . get ( "X-Error-Code" ) === "EntryNotFound" ) {
54
59
return null ;
55
60
}
56
61
57
62
let isLfs = false ;
58
- if ( resp . status >= 300 && resp . status < 400 ) {
59
- if ( resp . headers . get ( "Location" ) ?. startsWith ( HUB_URL ) ) {
60
- // can happen on repo name change
61
- return fileDownloadInfoInternal ( resp . headers . get ( "Location" ) ! , credentials , redirects + 1 ) ;
62
- }
63
63
64
- if ( ! resp . headers . has ( "X-Linked-Size" ) ) {
64
+ if ( resp . status >= 300 && resp . status < 400 ) {
65
+ if ( resp . headers . has ( "X-Linked-Size" ) ) {
66
+ isLfs = true ;
67
+ } else {
65
68
throw new Error ( "Invalid response from server: redirect to external server should have X-Linked-Size header" ) ;
66
69
}
67
- isLfs = true ;
68
70
} else if ( ! resp . ok ) {
69
71
throw await createApiError ( resp ) ;
70
72
}
0 commit comments