1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.parseHost = exports.validateUrl = exports.hasText = exports.decode = exports.encode = exports.fullHostName = void 0;
|
4 | var types_1 = require("./types");
|
5 | function fullHostName(obj, options) {
|
6 | var a = '';
|
7 | if (obj.name) {
|
8 | var skipEncoding = obj.type === types_1.HostType.IPv4 || obj.type === types_1.HostType.IPv6;
|
9 | a = skipEncoding ? obj.name : encode(obj.name, options !== null && options !== void 0 ? options : {});
|
10 | }
|
11 | if (obj.port) {
|
12 | a += ':' + obj.port;
|
13 | }
|
14 | return a;
|
15 | }
|
16 | exports.fullHostName = fullHostName;
|
17 | function encode(text, options) {
|
18 | text = encodeURIComponent(text);
|
19 | if (options.plusForSpace) {
|
20 | text = text.replace(/%20/g, '+');
|
21 | }
|
22 | return options.encodeDollar ? text : text.replace(/%24/g, '$');
|
23 | }
|
24 | exports.encode = encode;
|
25 | function decode(text) {
|
26 | return decodeURIComponent(text.replace(/\+/g, '%20'));
|
27 | }
|
28 | exports.decode = decode;
|
29 | function hasText(txt) {
|
30 | return typeof txt === 'string' && /\S/.test(txt);
|
31 | }
|
32 | exports.hasText = hasText;
|
33 | function validateUrl(url) {
|
34 | var idx = url.search(/[^A-Za-z0-9-._:/?[\]@!$&'()*+,;=%]/);
|
35 | if (idx >= 0) {
|
36 | var s = JSON.stringify(url[idx]).replace(/^"|"$/g, "'");
|
37 | throw new Error("Invalid URL character " + s + " at position " + idx);
|
38 | }
|
39 | }
|
40 | exports.validateUrl = validateUrl;
|
41 | function parseHost(host, direct) {
|
42 | if (direct) {
|
43 | if (typeof host !== 'string') {
|
44 | throw new TypeError("Invalid \"host\" parameter: " + JSON.stringify(host));
|
45 | }
|
46 | host = host.trim();
|
47 | }
|
48 | var m, isIPv6;
|
49 | if (host[0] === '[') {
|
50 |
|
51 | m = host.match(/((\[[0-9a-z:%]{2,45}])(?::(-?[0-9a-z]+))?)/i);
|
52 | isIPv6 = true;
|
53 | }
|
54 | else {
|
55 |
|
56 | if (direct) {
|
57 |
|
58 | m = host.match(/(([a-z0-9.$/\- ]*)(?::(-?[0-9a-z]+))?)/i);
|
59 | }
|
60 | else {
|
61 |
|
62 | m = host.match(/(([a-z0-9.+$%\-]*)(?::(-?[0-9a-z]+))?)/i);
|
63 | }
|
64 | }
|
65 | if (m) {
|
66 | var h_1 = {};
|
67 | if (m[2]) {
|
68 | if (isIPv6) {
|
69 | h_1.name = m[2];
|
70 | h_1.type = types_1.HostType.IPv6;
|
71 | }
|
72 | else {
|
73 | if (m[2].match(/([0-9]{1,3}\.){3}[0-9]{1,3}/)) {
|
74 | h_1.name = m[2];
|
75 | h_1.type = types_1.HostType.IPv4;
|
76 | }
|
77 | else {
|
78 | h_1.name = direct ? m[2] : decode(m[2]);
|
79 | h_1.type = h_1.name.match(/\/|.*\.sock$/i) ? types_1.HostType.socket : types_1.HostType.domain;
|
80 | }
|
81 | }
|
82 | }
|
83 | if (m[3]) {
|
84 | var p = m[3], port = parseInt(p);
|
85 | if (port > 0 && port < 65536 && port.toString() === p) {
|
86 | h_1.port = port;
|
87 | }
|
88 | else {
|
89 | throw new Error("Invalid port: " + JSON.stringify(p) + ". Valid port range is: [1...65535]");
|
90 | }
|
91 | }
|
92 | if (h_1.name || h_1.port) {
|
93 | Object.defineProperty(h_1, 'toString', {
|
94 | value: function (options) { return fullHostName(h_1, options); }
|
95 | });
|
96 | return h_1;
|
97 | }
|
98 | }
|
99 | return null;
|
100 | }
|
101 | exports.parseHost = parseHost;
|