@lowentry/utils 1.1.2 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LeUtils.js +53 -0
- package/package.json +1 -1
package/LeUtils.js
CHANGED
|
@@ -2263,6 +2263,59 @@ export const LeUtils = {
|
|
|
2263
2263
|
window.localStorage.removeItem('LeUtils_' + id);
|
|
2264
2264
|
},
|
|
2265
2265
|
|
|
2266
|
+
/**
|
|
2267
|
+
* Returns whether the current hostname (window.location.hostname) is private (such as localhost, 192.168.1.1, etc).
|
|
2268
|
+
* This can be used to determine if the app is running in a development environment or not.
|
|
2269
|
+
*
|
|
2270
|
+
* Only "localhost" and IPv4 addresses are supported. IPv6 addresses will always return false.
|
|
2271
|
+
*
|
|
2272
|
+
* @returns {boolean}
|
|
2273
|
+
*/
|
|
2274
|
+
isCurrentHostPrivate:
|
|
2275
|
+
(() =>
|
|
2276
|
+
{
|
|
2277
|
+
let lastHostname = null;
|
|
2278
|
+
let lastResult = false;
|
|
2279
|
+
|
|
2280
|
+
return () =>
|
|
2281
|
+
{
|
|
2282
|
+
const hostname = window.location.hostname;
|
|
2283
|
+
if(lastHostname === hostname)
|
|
2284
|
+
{
|
|
2285
|
+
return lastResult;
|
|
2286
|
+
}
|
|
2287
|
+
lastHostname = hostname;
|
|
2288
|
+
lastResult = LeUtils.isGivenHostPrivate(lastHostname);
|
|
2289
|
+
return lastResult;
|
|
2290
|
+
};
|
|
2291
|
+
})(),
|
|
2292
|
+
|
|
2293
|
+
/**
|
|
2294
|
+
* Returns true if the given hostname is private (such as localhost, 192.168.1.1, etc).
|
|
2295
|
+
*
|
|
2296
|
+
* Only "localhost" and IPv4 addresses are supported. IPv6 addresses will always return false.
|
|
2297
|
+
*
|
|
2298
|
+
* @param {string} host
|
|
2299
|
+
* @returns {boolean}
|
|
2300
|
+
*/
|
|
2301
|
+
isGivenHostPrivate:
|
|
2302
|
+
(host) =>
|
|
2303
|
+
{
|
|
2304
|
+
host = STRING(host).trim().toLowerCase();
|
|
2305
|
+
if((host === 'localhost') || (host === '127.0.0.1'))
|
|
2306
|
+
{
|
|
2307
|
+
return true;
|
|
2308
|
+
}
|
|
2309
|
+
if(!/^(\d{1,3}\.){3}\d{1,3}$/.test(host))
|
|
2310
|
+
{
|
|
2311
|
+
return false;
|
|
2312
|
+
}
|
|
2313
|
+
const parts = host.split('.');
|
|
2314
|
+
return (parts[0] === '10') || // 10.0.0.0 - 10.255.255.255
|
|
2315
|
+
((parts[0] === '172') && ((parseInt(parts[1], 10) >= 16) && (parseInt(parts[1], 10) <= 31))) || // 172.16.0.0 - 172.31.255.255
|
|
2316
|
+
((parts[0] === '192') && (parts[1] === '168')); // 192.168.0.0 - 192.168.255.255
|
|
2317
|
+
},
|
|
2318
|
+
|
|
2266
2319
|
/**
|
|
2267
2320
|
* Creates and returns a new TreeSet.
|
|
2268
2321
|
* A TreeSet is a set of elements, sorted by a comparator.
|