@@ -309,6 +309,33 @@ function requestNotMatches(request: Request, urlOrPredicate: UrlOrPredicate): bo
309309 return ! requestMatches ( request , urlOrPredicate ) ;
310310}
311311
312+ // Node 18 does not support URL.canParse()
313+ export function canParseURL ( url : string ) : boolean {
314+ try {
315+ new URL ( url ) ;
316+ return true ;
317+ } catch ( err ) {
318+ return false ;
319+ }
320+ }
321+
322+ // Node Requests cannot be relative
323+ function resolveInput ( input : string ) : string {
324+ if ( canParseURL ( input ) ) return input ;
325+
326+ // Window context
327+ if ( typeof window . document !== 'undefined' ) {
328+ return new URL ( input , window . document . baseURI ) . toString ( ) ;
329+ }
330+
331+ // Worker context
332+ if ( typeof location !== 'undefined' ) {
333+ return new URL ( input , location . origin ) . toString ( ) ;
334+ }
335+
336+ return input ;
337+ }
338+
312339function normalizeRequest ( input : RequestInput , requestInit ?: RequestInit ) : Request {
313340 if ( input instanceof Request ) {
314341 if ( input . signal && input . signal . aborted ) {
@@ -319,12 +346,12 @@ function normalizeRequest(input: RequestInput, requestInit?: RequestInit): Reque
319346 if ( requestInit && requestInit . signal && requestInit . signal . aborted ) {
320347 abort ( ) ;
321348 }
322- return new Request ( input , requestInit ) ;
349+ return new Request ( resolveInput ( input ) , requestInit ) ;
323350 } else {
324351 if ( requestInit && requestInit . signal && requestInit . signal . aborted ) {
325352 abort ( ) ;
326353 }
327- return new Request ( input . toString ( ) , requestInit ) ;
354+ return new Request ( resolveInput ( input . toString ( ) ) , requestInit ) ;
328355 }
329356}
330357
0 commit comments