@@ -80,6 +80,7 @@ module.exports = ({
8080 url, headers, strictSSL, httpAgent, httpsAgent
8181 } ) ;
8282 doc = { contextUrl : null , documentUrl : url , document : body || null } ;
83+
8384 // handle error
8485 const statusText = http . STATUS_CODES [ res . status ] ;
8586 if ( res . status >= 400 ) {
@@ -92,7 +93,9 @@ module.exports = ({
9293 } ) ;
9394 }
9495 const link = res . headers . get ( 'link' ) ;
96+ let location = res . headers . get ( 'location' ) ;
9597 const contentType = res . headers . get ( 'content-type' ) ;
98+
9699 // handle Link Header
97100 if ( link && contentType !== 'application/ld+json' ) {
98101 // only 1 related link header permitted
@@ -110,15 +113,15 @@ module.exports = ({
110113 }
111114
112115 // "alternate" link header is a redirect
113- alternate = linkHeaders [ ' alternate' ] ;
116+ alternate = linkHeaders . alternate ;
114117 if ( alternate &&
115118 alternate . type == 'application/ld+json' &&
116119 ! ( contentType || '' )
117120 . match ( / ^ a p p l i c a t i o n \/ ( \w * \+ ) ? j s o n $ / ) ) {
118- res . headers . set ( ' location' , prependBase ( url , alternate . target ) ) ;
121+ location = prependBase ( url , alternate . target ) ;
119122 }
120123 }
121- const location = res . headers . get ( 'location' ) ;
124+
122125 // handle redirect
123126 if ( ( alternate ||
124127 res . status >= 300 && res . status < 400 ) && location ) {
@@ -143,7 +146,9 @@ module.exports = ({
143146 } ) ;
144147 }
145148 redirects . push ( url ) ;
146- return loadDocument ( location , redirects ) ;
149+ // location can be relative, turn into full url
150+ const nextUrl = new URL ( location , url ) . href ;
151+ return loadDocument ( nextUrl , redirects ) ;
147152 }
148153
149154 // cache for each redirected URL
@@ -163,7 +168,12 @@ module.exports = ({
163168
164169async function _fetch ( { url, headers, strictSSL, httpAgent, httpsAgent} ) {
165170 try {
166- const options = { headers, redirect : 'manual' } ;
171+ const options = {
172+ headers,
173+ redirect : 'manual' ,
174+ // ky specific to avoid redirects throwing
175+ throwHttpErrors : false
176+ } ;
167177 const isHttps = url . startsWith ( 'https:' ) ;
168178 if ( isHttps ) {
169179 options . agent =
0 commit comments