99
1010import Url from 'url' ;
1111import Stream from 'stream' ;
12+ import { URL } from 'whatwg-url' ;
1213import Headers , { exportNodeCompatibleHeaders } from './headers.js' ;
1314import Body , { clone , extractContentType , getTotalBytes } from './body' ;
1415
@@ -18,6 +19,39 @@ const INTERNALS = Symbol('Request internals');
1819const parse_url = Url . parse ;
1920const format_url = Url . format ;
2021
22+ /**
23+ * Wrapper around `new URL` to handle arbitrary URLs
24+ *
25+ * @param {string } urlStr
26+ * @return {void }
27+ */
28+ function parseURL ( urlStr ) {
29+ /*
30+ Check whether the URL is absolute or not
31+
32+ Scheme: https://tools.ietf.org/html/rfc3986#section-3.1
33+ Absolute URL: https://tools.ietf.org/html/rfc3986#section-4.3
34+ */
35+ if ( / ^ [ a - z A - Z ] [ a - z A - Z \d + \- . ] * : / . exec ( urlStr ) ) {
36+ const url = new URL ( urlStr ) ;
37+
38+ return {
39+ path : url . pathname ,
40+ pathname : url . pathname ,
41+ hostname : url . hostname ,
42+ protocol : url . protocol ,
43+ port : url . port ,
44+ hash : url . hash ,
45+ search : url . search ,
46+ query : url . query ,
47+ href : url . href ,
48+ }
49+ }
50+
51+ // Fallback to old implementation for arbitrary URLs
52+ return parse_url ( urlStr ) ;
53+ }
54+
2155const streamDestructionSupported = 'destroy' in Stream . Readable . prototype ;
2256
2357/**
@@ -59,14 +93,14 @@ export default class Request {
5993 // in order to support Node.js' Url objects; though WHATWG's URL objects
6094 // will fall into this branch also (since their `toString()` will return
6195 // `href` property anyway)
62- parsedURL = parse_url ( input . href ) ;
96+ parsedURL = parseURL ( input . href ) ;
6397 } else {
6498 // coerce input to a string before attempting to parse
65- parsedURL = parse_url ( `${ input } ` ) ;
99+ parsedURL = parseURL ( `${ input } ` ) ;
66100 }
67101 input = { } ;
68102 } else {
69- parsedURL = parse_url ( input . url ) ;
103+ parsedURL = parseURL ( input . url ) ;
70104 }
71105
72106 let method = init . method || input . method || 'GET' ;
0 commit comments