@wider/utils_where-am-i
Version:
Determine where your code is running
241 lines (194 loc) • 8.71 kB
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Source: index.js | Wider</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/bootstrap.min.css">
<link type="text/css" rel="stylesheet" href="styles/prettify-jsdoc.css">
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/tui-doc.css">
</head>
<body>
<nav class="lnb" id="lnb">
<div class="logo" style="width: 50px">
<a href="https://y-d-r.co.uk" rel="noopener noreferrer" target="_blank">
<img src="https://y-d-r.co.uk/favicon.ico" width="100%" height="100%">
</a>
</div>
<div class="title">
<h1><a href="index.html" class="link">Wider</a></h1>
<span class="version">v1.1.21</span>
</div>
<div class="search-container" id="search-container">
<input type="text" placeholder="Search">
<ul></ul>
</div>
<ol class="lnb-tab">
<li id="api-tab">
<a href="#"><h4>API</h4></a>
</li>
<li id="examples-tab">
<a href="#"><h4>Examples</h4></a>
</li>
</ol>
<div class="lnb-examples hidden"><h3>Examples</h3><ul><li><a href="tutorial-Examples.html">Examples</a><button type="button" class="hidden toggle-subnav btn btn-link"> <span class="glyphicon glyphicon-plus"></span></button><div class="hidden" id="Examples_sub"></div></li></ul></div><div class="lnb-api hidden"><h3>Modules</h3><ul><li><a href="module-@wider_utils_where-am-i.html">@wider/utils_where-am-i</a><button type="button" class="hidden toggle-subnav btn btn-link"> <span class="glyphicon glyphicon-plus"></span></button><div class="hidden" id="module:@wider/utils_where-am-i_sub"><div class="member-type">Methods</div><ul class="inner"><li><a href="module-@wider_utils_where-am-i.html#~whereAmI">whereAmI</a></li></ul><div class="member-type">Typedef</div><ul class="inner"><li><a href="module-@wider_utils_where-am-i.html#~whereAmI_result">whereAmI_result</a></li></ul></div></li></ul></div>
</nav>
<div id="resizer"></div>
<div class="main" id="main">
<section>
<article>
<pre class="prettyprint source linenums"><code>'use strict';
/**
* @module @wider/utils_where-am-i
*
* @copyright Copyright (C) 1985..2021 Martin Baker -YDR. {@link https://y-d-r.co.uk}
* @author Martin W Baker
* @license ISC Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*/
const $moduleName = "@wider/utils_where-am-i";
const $objectName = "whereAmI";
/* jshint -W117*/
/**
* The structure of the object returned by this module.
*
* In the event that any of these properties cannot be determined, they will contain the value `unknown`
*
* Please feed back to us additional environments or values we might also detect
* @typedef {Object} whereAmI_result
* @property {RegExp} _genres a regular expression that returns true when testing against a string that contains any one of the possible values of `genre`
* @property {NCName} genre the genre or general category of processor engine of the environment on which the code is running. This version will contain one of the following `["unknown", "server", "browser", "script"]`
* @property {NCName} engine the engine that is running the code at the moment. This may contain various values as reported by the engine about itself. Some values include `["unknown", "node", "Firefox", "Edg", "Chrome", "Trident","WScript", "aspClassic"]`
* @property {String} version the version of the engine in the format that is used by that engine
* @property {NCName} [os the name of the operating system coerced to lower case. Some values include `["unknown", "linux", "sunos", "openbsd", "freebsd", "darwin", "aix", "win16", "win32", "win64"]`.
* Important - nodejs reports `win32` even when on a 64 bit machine with 64 bit os. The value responded here is the true version of the operating system so on node on a 64 window machine AND with 64 bit os you get ***win64*** just as you would on a windows script running on the same machine
* @property {String} computerName the name of the computer on which your code is running. This is not discoverable in all environments
*/
/**
* Provide a standard json object response that indicates to the caller the nature of the environment on which it is running
*
* This can be required or imported and the result is a plain javascript object.
*
* See [Examples](/./tutorial-Examples.html) for the JSON objects returned.
* @returns {whereAmI_result}
*/
function whereAmI() {
function wshell() {
var hell = new ActiveXObject("WScript.Shell");
var sys = hell.Environment("SYSTEM");
return {
os: "win" + sys("PROCESSOR_ARCHITECTURE").replace(/[a-z]+/i, ""),
computerName: hell.Environment("COMPUTERNAME")
};
}
var where;
if ((typeof process === "object") && (process.release && process.release.name === "node")) {
var platform = process.platform.toLowerCase();
where = {
genre: "server",
engine: "node",
version: process.version,
computerName: process.env.COMPUTERNAME,
os: /^win/.test(platform) && process.env.ProgramW6432 ? "win64" : platform
};
}
/* jshint -W117*/
else if (typeof Window === "function" && window.document) {
var engine;
var version;
try {
for (var key in $.browser) {
if (Object.hasOwnProperty.call($.browser, key)) {
var element = $.browser[key];
if (typeof element === "boolean" && key != "webkit") {
engine = key;
version = $.browser.version;
break;
}
}
}
} catch (err) {}
if (!engine) {
engine = (navigator.mozGetUserMedia) ?
"Firefox" :
window.Bing ?
"Edg" :
/Google/.test(navigator.vendor) ?
"Chrome" :
/Trident/.test(navigator.userAGent) ?
"Trident" : navigator.vendor.split(/ /)[0];
var items = window.navigator.userAgent.replace(/(\w+\/)/g, "\u400d$1").split(/\u400d/);
for (var index = 0; index < items.length; index++) {
var item = items[index];
details = items.split(/[ \/]/);
if (details[0] === engine) {
version = item.split(/[ \/]/)[1];
return true;
}
}
}
where = {
genre: "browser",
os: (window.navigator.platform || "").toLowerCase(),
engine: engine || window.navigator.userAgent.toLowerCase(),
version: version || "unknown"
};
} else if (typeof WScript === "object") {
wshShell = new ActiveXObject("WScript.Shell");
where = Object.assign(
wshell(), {
genre: "script",
engine: "WScript",
version: WScript.Version
});
} else if (typeof Server === "object") {
var serverSoftware = Request.ServerVariables("SERVER_SOFTWARE") + "";
where = Object.assign(
wshell(), {
genre: "server",
engine: serverSoftware.split(/\//)[0].split(/-/)[1] + "/aspClassic",
version: serverSoftware.split(/\//)[1]
});
}
var unknown = "unknown";
return Object.assign({
$moduleName: $moduleName,
$objectName: $objectName,
"_genres": /unknown|server|browser|script/,
"genre": unknown,
"engine": unknown,
"version": unknown,
"computerName": unknown,
"os": unknown
}, where);
}
module.exports = whereAmI();
/* jshint -W030 */
</code></pre>
</article>
</section>
</div>
<footer>
<img class="logo" src="https://y-d-r.co.uk/favicon.ico" style="width: 50px">
<div class="footer-text">Copyright Martin Baker 1985 onwards - ISC License</div>
</footer>
<script>prettyPrint();</script>
<script src="scripts/jquery.min.js"></script>
<script src="scripts/tui-doc.js"></script>
<script src="scripts/linenumber.js"></script>
<script>
var id = '_sub'.replace(/"/g, '_');
var selectedApi = document.getElementById(id); // do not use jquery selector
var $selectedApi = $(selectedApi);
$selectedApi.removeClass('hidden');
$selectedApi.parent().find('.glyphicon').removeClass('glyphicon-plus').addClass('glyphicon-minus');
showLnbApi();
</script>
</body>
</html>