1- import { request } from ' undici' ;
1+ import { request } from " undici" ;
22const redirectStatuses = new Set ( [ 301 , 302 , 303 , 307 , 308 ] ) ;
33
44export async function getRedirectingURL ( url , dispatcher , headers ) {
@@ -8,18 +8,34 @@ export async function getRedirectingURL(url, dispatcher, headers) {
88 headers,
99 redirect : 'manual'
1010 } ;
11+ const getParams = {
12+ ...params ,
13+ method : 'GET' ,
14+ } ;
1115
12- let location = await request ( url , params ) . then ( r => {
16+ const callback = ( r ) => {
1317 if ( redirectStatuses . has ( r . statusCode ) && r . headers [ 'location' ] ) {
1418 return r . headers [ 'location' ] ;
1519 }
16- } ) . catch ( ( ) => null ) ;
20+ }
1721
18- location ??= await fetch ( url , params ) . then ( r => {
19- if ( redirectStatuses . has ( r . status ) && r . headers . has ( 'location' ) ) {
20- return r . headers . get ( 'location' ) ;
21- }
22- } ) . catch ( ( ) => null ) ;
22+ /*
23+ try request() with HEAD & GET,
24+ then do the same with fetch
25+ (fetch is required for shortened reddit links)
26+ */
27+
28+ let location = await request ( url , params )
29+ . then ( callback ) . catch ( ( ) => null ) ;
30+
31+ location ??= await request ( url , getParams )
32+ . then ( callback ) . catch ( ( ) => null ) ;
33+
34+ location ??= await fetch ( url , params )
35+ . then ( callback ) . catch ( ( ) => null ) ;
36+
37+ location ??= await fetch ( url , getParams )
38+ . then ( callback ) . catch ( ( ) => null ) ;
2339
2440 return location ;
2541}
0 commit comments