|
1 | 1 | import url from 'url'; |
2 | | -import querystring from 'querystring'; |
3 | | -import getCurrentScriptSource from './getCurrentScriptSource'; |
| 2 | +import getUrlParts from './getUrlParts'; |
4 | 3 |
|
5 | 4 | function createSocketUrl(resourceQuery) { |
6 | | - let urlParts; |
7 | | - |
8 | | - if (typeof resourceQuery === 'string' && resourceQuery !== '') { |
9 | | - // If this bundle is inlined, use the resource query to get the correct url. |
10 | | - urlParts = url.parse(resourceQuery.substr(1)); |
11 | | - } else { |
12 | | - // Else, get the url from the <script> this file was called with. |
13 | | - const scriptHost = getCurrentScriptSource(); |
14 | | - |
15 | | - // it seems the reg exp below was intended to cut off the path of a script source, |
16 | | - // so if you have my.host/long/path as the script source, it would become my.host. |
17 | | - // however, it did not work, because if you had http://my.host/long/path it would |
18 | | - // turn it into http:/. Cutting off the path is no longer needed, since we |
19 | | - // simply do url.parse and pull the host from there. |
20 | | - |
21 | | - // eslint-disable-next-line no-useless-escape |
22 | | - // scriptHost = scriptHost.replace(/\/[^\/]+$/, ''); |
23 | | - urlParts = url.parse(scriptHost || '/', false, true); |
24 | | - } |
25 | | - |
26 | | - if (!urlParts.port || urlParts.port === '0') { |
27 | | - urlParts.port = self.location.port; |
28 | | - } |
29 | | - |
30 | | - const { auth, path } = urlParts; |
31 | | - let { hostname, protocol } = urlParts; |
32 | | - |
33 | | - // check ipv4 and ipv6 `all hostname` |
34 | | - // why do we need this check? |
35 | | - // hostname n/a for file protocol (example, when using electron, ionic) |
36 | | - // see: https://github.com/webpack/webpack-dev-server/pull/384 |
37 | | - const isAnyHostname = |
38 | | - (hostname === '0.0.0.0' || hostname === '::') && |
39 | | - self.location.hostname && |
40 | | - // eslint-disable-next-line no-bitwise |
41 | | - !!~self.location.protocol.indexOf('http'); |
42 | | - |
43 | | - if (isAnyHostname) { |
44 | | - hostname = self.location.hostname; |
45 | | - } |
46 | | - |
47 | | - // `hostname` can be empty when the script path is relative. In that case, specifying |
48 | | - // a protocol would result in an invalid URL. |
49 | | - // When https is used in the app, secure websockets are always necessary |
50 | | - // because the browser doesn't accept non-secure websockets. |
51 | | - if ( |
52 | | - hostname && |
53 | | - (self.location.protocol === 'https:' || urlParts.hostname === '0.0.0.0') |
54 | | - ) { |
55 | | - protocol = self.location.protocol; |
56 | | - } |
57 | | - |
58 | | - // default values of the sock url if they are not provided |
59 | | - const defaultHost = hostname; |
60 | | - const defaultSockPath = '/sockjs-node'; |
61 | | - const defaultPort = urlParts.port; |
62 | | - const defaultPublicPath = '/'; |
63 | | - |
64 | | - let sockHost = defaultHost; |
65 | | - let sockPath = defaultSockPath; |
66 | | - let sockPort = defaultPort; |
67 | | - let publicPath = defaultPublicPath; |
68 | | - |
69 | | - // eslint-disable-next-line no-undefined |
70 | | - const shouldParsePath = path !== null && path !== undefined && path !== '/'; |
71 | | - if (shouldParsePath) { |
72 | | - const parsedQuery = querystring.parse(path); |
73 | | - // all of these sock url params are optionally passed in through |
74 | | - // resourceQuery, so we need to fall back to the default if |
75 | | - // they are not provided |
76 | | - sockHost = parsedQuery.sockHost || sockHost; |
77 | | - sockPath = parsedQuery.sockPath || sockPath; |
78 | | - sockPort = parsedQuery.sockPort || sockPort; |
79 | | - publicPath = parsedQuery.publicPath || publicPath; |
80 | | - } |
| 5 | + const urlParts = getUrlParts(resourceQuery); |
81 | 6 |
|
82 | 7 | return url.format({ |
83 | | - protocol, |
84 | | - auth, |
85 | | - hostname: sockHost, |
86 | | - port: sockPort, |
| 8 | + protocol: urlParts.protocol, |
| 9 | + auth: urlParts.auth, |
| 10 | + hostname: urlParts.sockHost, |
| 11 | + port: urlParts.sockPort, |
87 | 12 | // If sockPath is provided it'll be passed in via the resourceQuery as a |
88 | 13 | // query param so it has to be parsed out of the querystring in order for the |
89 | 14 | // client to open the socket to the correct location. |
90 | | - pathname: sockPath, |
| 15 | + pathname: urlParts.sockPath, |
91 | 16 | }); |
92 | 17 | } |
93 | 18 |
|
|
0 commit comments