UNPKG

1.94 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.isLocal = exports.isInternalIP = exports.toProtocol = exports.stripSlashEnd = exports.stripSlashStart = exports.stripSlash = exports.stripPort = exports.stripHttp = exports.toPort = void 0;
4var common_1 = require("../common");
5function toPort(input) {
6 var text = common_1.R.pipe(stripHttp, stripSlash)(input || '').split(':')[1];
7 return text === undefined ? undefined : common_1.value.toNumber(text);
8}
9exports.toPort = toPort;
10function stripHttp(input) {
11 return (input || '')
12 .trim()
13 .replace(/^http\:\/\//, '')
14 .replace(/^https\:\/\//, '');
15}
16exports.stripHttp = stripHttp;
17function stripPort(input) {
18 return (input || '').replace(/\:\d*$/, '');
19}
20exports.stripPort = stripPort;
21function stripSlash(input) {
22 return (input || '').replace(/^\/*/, '').replace(/\/*$/, '');
23}
24exports.stripSlash = stripSlash;
25function stripSlashStart(input) {
26 return (input || '').replace(/^\/*/, '');
27}
28exports.stripSlashStart = stripSlashStart;
29function stripSlashEnd(input) {
30 return (input || '').replace(/\/*$/, '');
31}
32exports.stripSlashEnd = stripSlashEnd;
33function toProtocol(input) {
34 input = (input || '').trim();
35 if (input.startsWith('localhost') && !input.includes('.')) {
36 return 'http';
37 }
38 if (isInternalIP(input)) {
39 return 'http';
40 }
41 return 'https';
42}
43exports.toProtocol = toProtocol;
44function isInternalIP(input) {
45 var host = common_1.R.pipe(stripHttp, stripSlash)(input || '').split('/')[0] || '';
46 return host.startsWith('192.168.');
47}
48exports.isInternalIP = isInternalIP;
49function isLocal(input) {
50 if (isInternalIP(input)) {
51 return true;
52 }
53 var host = stripSlash(stripHttp(input)).split('/')[0] || '';
54 if (host === 'localhost' || host.startsWith('localhost:')) {
55 return true;
56 }
57 return false;
58}
59exports.isLocal = isLocal;