UNPKG

3.55 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.parseHost = exports.validateUrl = exports.hasText = exports.decode = exports.encode = exports.fullHostName = void 0;
4var types_1 = require("./types");
5function 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}
16exports.fullHostName = fullHostName;
17function 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}
24exports.encode = encode;
25function decode(text) {
26 return decodeURIComponent(text.replace(/\+/g, '%20'));
27}
28exports.decode = decode;
29function hasText(txt) {
30 return typeof txt === 'string' && /\S/.test(txt);
31}
32exports.hasText = hasText;
33function validateUrl(url) {
34 var idx = url.search(/[^a-z0-9-._:\/?[\]@!$&'()*+,;=%]/i);
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}
40exports.validateUrl = validateUrl;
41function 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 // This is IPv6, with [::] being the shortest possible
51 m = host.match(/((\[[0-9a-z:%]{2,45}])(?::(-?[0-9a-z]+))?)/i);
52 isIPv6 = true;
53 }
54 else {
55 // It is either IPv4 or domain/socket
56 if (direct) {
57 // Allowed directly: ForwardSlash + Space
58 m = host.match(/(([a-z0-9.$/\- ]*)(?::(-?[0-9a-z]+))?)/i);
59 }
60 else {
61 // Allow when indirectly: + and %
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 enumerable: false
96 });
97 return h_1;
98 }
99 }
100 return null;
101}
102exports.parseHost = parseHost;