UNPKG

2.15 kBJavaScriptView Raw
1"use strict";
2/**
3 * Copyright (c) 2015-present, Facebook, Inc.
4 *
5 * This source code is licensed under the MIT license found in the
6 * LICENSE file at
7 * https://github.com/facebookincubator/create-react-app/blob/master/LICENSE
8 */
9const chalk_1 = require("chalk");
10const url = require("url");
11const address = require("address");
12module.exports = function prepareUrls(protocol, host, port, pathname = '/') {
13 const formatUrl = (hostname) => url.format({
14 protocol,
15 hostname,
16 port,
17 pathname,
18 });
19 const prettyPrintUrl = (hostname) => url.format({
20 protocol,
21 hostname,
22 port: chalk_1.default.bold(port.toString()),
23 pathname,
24 });
25 const isUnspecifiedHost = host === '0.0.0.0' || host === '::';
26 let prettyHost;
27 let lanUrlForConfig;
28 let lanUrlForTerminal;
29 let lanUrlForBrowser;
30 if (isUnspecifiedHost) {
31 prettyHost = 'localhost';
32 try {
33 // This can only return an IPv4 address
34 lanUrlForConfig = address.ip();
35 if (lanUrlForConfig) {
36 // Check if the address is a private ip
37 // https://en.wikipedia.org/wiki/Private_network#Private_IPv4_address_spaces
38 if (/^10[.]|^30[.]|^172[.](1[6-9]|2[0-9]|3[0-1])[.]|^192[.]168[.]/.test(lanUrlForConfig) || process.env.USE_PUBLIC_IP) {
39 // Address is private, format it for later use
40 lanUrlForTerminal = prettyPrintUrl(lanUrlForConfig);
41 lanUrlForBrowser = formatUrl(lanUrlForConfig);
42 }
43 else {
44 // Address is not private, so we will discard it
45 lanUrlForConfig = undefined;
46 }
47 }
48 }
49 catch (_e) {
50 // ignored
51 }
52 }
53 else {
54 prettyHost = host;
55 }
56 const localUrlForTerminal = prettyPrintUrl(prettyHost);
57 const localUrlForBrowser = formatUrl(prettyHost);
58 return {
59 lanUrlForConfig,
60 lanUrlForTerminal,
61 lanUrlForBrowser,
62 localUrlForTerminal,
63 localUrlForBrowser,
64 };
65};