1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | const chalk_1 = require("chalk");
|
10 | const url = require("url");
|
11 | const address = require("address");
|
12 | module.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 |
|
34 | lanUrlForConfig = address.ip();
|
35 | if (lanUrlForConfig) {
|
36 |
|
37 |
|
38 | if (/^10[.]|^30[.]|^172[.](1[6-9]|2[0-9]|3[0-1])[.]|^192[.]168[.]/.test(lanUrlForConfig)) {
|
39 |
|
40 | lanUrlForTerminal = prettyPrintUrl(lanUrlForConfig);
|
41 | lanUrlForBrowser = formatUrl(lanUrlForConfig);
|
42 | }
|
43 | else {
|
44 |
|
45 | lanUrlForConfig = undefined;
|
46 | }
|
47 | }
|
48 | }
|
49 | catch (_e) {
|
50 |
|
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 | };
|