Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

platform-detect

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

platform-detect - npm Package Compare versions

Comparing version 2.0.3 to 3.0.0

CHANGELOG.md

237

index.js

@@ -1,236 +0,1 @@

(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define('platform-detect', factory) :
(global['platform-detect'] = factory());
}(this, (function () { 'use strict';
// The app has a window to render DOM content in.
var gui = typeof navigator !== 'undefined' && typeof window !== 'undefined';
// NW.JS' background script loads into invisible empty HTML page.
// Therefore NW.JS "main" script has all of window, document, navigator objects
// and window.open() won't be null if App's window is launched from such "main".
if (gui && typeof nw !== 'undefined') {
try {
nw.Window.get();
} catch(err) {
gui = false;
}
}
// The app runs in terminal/console and can only use console.log.
var terminal = !gui;
var ua = gui ? navigator.userAgent : undefined;
function registerQuery(query, handler) {
var mql = window.matchMedia(query);
handler(mql.matches);
var listener = () => handler(mql.matches);
mql.addListener(listener);
return () => mql.removeListener(listener)
}
// The holy grail
var p = {gui, terminal, registerQuery};
// Fully functional Node & core modules.
// it is true for Node.js, Electron, NW.JS
// it is false for browsers with shims or bundles of some Node modules (shimmed process, EventEmitter, etc..)
p.node = typeof process !== 'undefined'
&& !!process.versions
&& !!process.versions.node;
// Detects if the APP is launched as standalone Progressive Web App. Still a website, but looking like OS app, without url bar.
// TODO: WARNING: this returns true even for popup windows created with window.open()
p.pwa = p.gui
&& window.matchMedia('(display-mode: standalone)').matches
&& (document.head.querySelector('[rel="manifest"]') !== null);
// Windows 10 app - Universal Windows Platform.
p.uwp = typeof Windows !== 'undefined' && typeof MSApp !== 'undefined';
// Node + Chromium
p.nwjs = !!(p.node && process.versions.nw);
p.electron = !!(p.node && process.versions.electron);
// Cordova mobile app
p.cordova = !!(p.gui && window.cordova);
// Chrome app (Chrome OS app)
p.chromeapp = undefined; // todo
// The platform requires app to be compiled, bundled or packaged.
p.packaged = p.uwp || p.nwjs || p.electron || p.cordova || p.chromeapp;
// The app runs inside browser and is served from a server or browser cache.
p.web = !p.node && !p.packaged;
// App is a plain old webpage and not a PWA.
p.website = p.web && !p.pwa;
// Script is executed inside Web Worker
p.worker = !p.gui
&& typeof self !== 'undefined'
&& self.importScripts !== undefined;
//&& self.close !== undefined
//p.serviceWorker = p.worker && !!navigator.serviceWorker.controller
// TODO: test user agent availability in worker
// TODO: figure out if these should be mutually exclusive (Tizen and Android are based on linux and have Linux in their user agents)
p.android = p.gui ? ua.includes('Android') : false; // TODO
p.chromeos = p.gui ? ua.includes('CrOS') : false; // TODO
p.tizen = p.gui ? ua.includes('Tizen') : false; // TODO
p.ios = p.gui && /iPad|iPhone|iPod/.test(ua) && !window.MSStream || false;
p.linuxBased = p.android || p.tizen;
p.windows = p.node ? process.platform === 'win32' : ua.includes('Windows');
p.macos = p.node ? process.platform === 'darwin' : ua.includes('Macintosh');
p.linux = p.node ? process.platform === 'linux' : ua.includes('Linux') && !p.linuxBased && !p.macos;
// These return true if their rendering engine is used to render the app.
// I.e: UWP apps are rendered by Edge's EdgeHTML, Electron/NWJS apps are rendered by Chromium.
// https://blogs.windows.com/msedgedev/2017/10/05/microsoft-edge-ios-android-developer/#49Fi4TfpgzHAwuXQ.97
//p.edgeAndroid = p.gui && ua.includes('EdgiOS/')
//p.edgeIos = p.gui && ua.includes('EdgA/')
//p.edgeWin = p.gui && ua.includes('Edge/')
//p.edge = edgeWin
// NOTE: Only returnin true for Edge (EdgeHtml) on windows. Android/iOs versions of Edge are powered by Webkit.
p.edge = p.gui && ua.includes('Edge/');
p.chrome = p.gui && ua.includes('Chrome') && !p.edge;
p.safari = p.gui && ua.includes('Safari') && !p.chrome && !p.edge;
p.opera = p.gui && ua.includes('Opera');
p.firefox = p.gui && ua.includes('Firefox');
// RENDERING & JS ENGINES
// TODO: all other android/ios browsers are webkit based.
p.edgeHtml = p.edge;
p.blink = p.chrome;// || edgeAndroid
p.webkit = p.blink || p.safari;// || edgeIos
p.gecko = p.firefox;
// minimal EventEmitter like API for notifying abou changes
var listeners = {};
p.on = function(name, listener) {
listeners[name] = listeners[name] || new Set;
listeners[name].add(listener);
};
p.removeListener = function(name, listener) {
if (listeners[name])
listeners[name].delete(listener);
};
var emit = p.emit = function(name, value) {
if (listeners[name])
listeners[name].forEach(listener => listener(value));
};
if (p.gui) {
p.pixelRatio = parseFloat(window.devicePixelRatio.toFixed(2));
p.gameconsole = ua.includes('Xbox') || ua.includes('PlayStation');
if (p.gameconsole) {
// WARNING: do not use Gamepad API unless necessary. On Xbox it automatically hides pointer and shows warning that the site uses
// custom gamepad controls. We don't want to trigger that. This library only observes.
p.gamepad = true;
p.mouse = true; // this is controvertial. if app decides to use gamepad controls, then it doesnt have the mouse & hover anymore
p.touch = false;
p.tv = true;
p.battery = false;
p.phone = p.tablet = p.hybrid = p.laptop = p.desktop = false;
// undocumented api used for detecting current use of the site
p.gamepads = 0;
window.addEventListener('gamepadconnected', e => p.gamepads++);
window.addEventListener('gamepaddisconnected', e => p.gamepads--);
} else {
p.touch = navigator.maxTouchPoints > 0;
p.tv = ua.includes('TV');
// TODO: detect when gamepad is custom handled (and used not just used to control the emulated pointer on smart TVs)
// TODO: throw gamepad into the platform.input
var gamepads = navigator.getGamepads();
p.gamepad = !!gamepads && Array.from(gamepads).some(g => g !== null);
registerQuery('(orientation: portrait)', bool => {
p.portrait = bool;
p.landscape = !bool;
p.orientation = bool ? 'portrait' : 'landscape';
emit('portrait', p.portrait);
emit('landscape', p.landscape);
emit('orientation', p.orientation);
});
registerQuery('(any-pointer: coarse)', bool => {
p.touch = bool;
emit('touch', p.touch);
});
registerQuery('(hover: hover)', bool => {
p.mouse = bool;
p.input = bool ? 'mouse' : 'touch';
p.formfactor = getFormfactor();
emit('mouse', p.mouse);
emit('input', p.input);
emit('formfactor', p.formfactor);
});
// TODO: apply some light transpilation with babel because my relatively new smart tv runs tizen
// with 2 years old chromium which doesn't support destructuring syntax.
function getFormfactor() {
var shorterScreenSide = Math.min(window.screen.width, window.screen.height);
// TODO: add 'iot' or some other form of window-less app or monitor-less hardware.
if (p.tv)
return 'tv'
else if (p.touch && shorterScreenSide < 600)
return 'phone'
else if (p.touch && !p.mouse)
return 'tablet'
//else if (p.touch && p.mouse)
// return 'hybrid'
else if (p.battery)
return 'laptop'
else
return 'desktop'
}
}
}
// Detects if the platform is constrained by Cancerous Security Policy.
p.csp = p.uwp || p.chromeapp || false;
if (p.nwjs) {
p.dev = process.versions['nw-flavor'] === 'sdk';
} else if (p.electron) {
p.dev = process.execPath.replace(/\\/g, '/').includes('node_modules/electron/');
} else if (p.uwp) {
p.dev = Windows.ApplicationModel.Package.current.isDevelopmentMode;
} else if (p.web) {
// toString on object is only called in developer tools in console.
// NOTE: Printing extra empty line into console is not ideal but it's the only reliable way.
// Window size checking seemed to be somewhat reliable but was failing on Tizen smart TV in the end.
let temp = /./;
temp.toString = () => p.dev = true;
console.log('%c', temp);
// TODO: doesnt work anymore in latest chrome
} else if (p.node) {
p.dev = process.env.NODE_ENV !== 'production';
} else {
// Detects if NW.JS runs in SDK version (console available) and if Electron is executed from npm/node_modules/electron global.
// TODO: would be nice to detect if UWP is attached to Visual Studio debugger.
p.dev = false;
}
return p;
})));
!function(e,o){"object"==typeof exports&&"undefined"!=typeof module?module.exports=o():"function"==typeof define&&define.amd?define("platform-detect",o):(e=e||self,e["platform-detect"]=o())}(this,function(){"use strict";function e(e,o){var n=window.matchMedia(e);o(n.matches);var i=function(){return o(n.matches)};return n.addListener(i),function(){return n.removeListener(i)}}function o(e,o){return t[e]!==o&&(t[e]=o,s(e,o),!0)}var n="undefined"!=typeof navigator&&"undefined"!=typeof window;if(n&&"undefined"!=typeof nw)try{nw.Window.get()}catch(e){n=!1}var i=!n,r=n?navigator.userAgent:void 0,t={gui:n,terminal:i,registerQuery:e};t.node="undefined"!=typeof process&&!!process.versions&&!!process.versions.node,t.pwa=t.gui&&window.matchMedia("(display-mode: standalone)").matches&&null!==document.head.querySelector('[rel="manifest"]'),t.uwp="undefined"!=typeof Windows&&"undefined"!=typeof MSApp,t.nwjs=!(!t.node||!process.versions.nw),t.electron=!(!t.node||!process.versions.electron),t.cordova=!(!t.gui||!window.cordova),t.packaged=t.uwp||t.nwjs||t.electron||t.cordova,t.web=!t.node&&!t.packaged,t.browser=t.web,t.website=t.web&&!t.pwa,t.worker=!t.gui&&"undefined"!=typeof self&&void 0!==self.importScripts,t.serviceWorker=t.worker&&!!navigator.serviceWorker.controller||!1,t.android=!!t.gui&&r.includes("Android"),t.chromeos=!!t.gui&&r.includes("CrOS"),t.tizen=!!t.gui&&r.includes("Tizen"),t.ios=t.gui&&/iPad|iPhone|iPod/.test(r)&&!window.MSStream||!1,t.linuxBased=t.android||t.tizen,t.windows=t.node?"win32"===process.platform:r.includes("Windows"),t.macos=t.node?"darwin"===process.platform:r.includes("Macintosh"),t.linux=t.node?"linux"===process.platform:r.includes("Linux")&&!t.linuxBased&&!t.macos,t.edgeHtml=t.gui&&r.includes("Edge/"),t.edgeChromium=t.gui&&r.includes("Edg/"),t.edgeAndroid=t.gui&&r.includes("EdgA/"),t.edgeIos=t.gui&&r.includes("EdgiOS/"),t.chromeIos=t.gui&&r.includes("CriOS/"),t.firefoxIos=t.gui&&r.includes("FxiOS/"),t.edge=t.edgeHtml||t.edgeChromium||t.edgeAndroid||t.edgeIos,t.samsungBrowser=t.gui&&r.includes("SamsungBrowser/"),t.opera=t.gui&&(r.includes("Opera")||r.includes("OPR/")),t.firefox=t.gui&&(r.includes("Firefox")||t.firefoxIos),t.chrome=t.gui&&(r.includes("Chrome")||t.chromeIos)&&!t.edge&&!t.opera&&!t.samsungBrowser,t.safari=t.gui&&r.includes("Safari")&&!t.chrome&&!t.edge&&!t.firefox&&!t.opera||t.edgeIos||t.chromeIos||t.firefoxIos,t.ie=t.trident=t.gui&&r.includes("Trident"),t.blink=t.chrome&&!t.chromeIos||t.edgeChromium||t.edgeAndroid||t.samsungBrowser,t.webkit=t.blink||t.safari,t.gecko=t.firefox&&!t.firefoxIos&&!t.webkit&&!t.safari;var d={};t.on=function(e,o){d[e]=d[e]||new Set,d[e].add(o)},t.off=t.removeListener=function(e,o){d[e]&&d[e].delete(o)};var s=t.emit=function(e,o){d[e]&&d[e].forEach(function(e){return e(o)})};if(t.gui){t.pixelRatio=parseFloat(window.devicePixelRatio.toFixed(2)),t.gameconsole=r.includes("Xbox")||r.includes("PlayStation");var a=0;if(window.addEventListener("gamepadconnected",function(e){return a++}),window.addEventListener("gamepaddisconnected",function(e){return a--}),t.gameconsole)t.gamepad=!0,t.mouse=!0,t.touch=!1,t.tv=!0,t.battery=!1,t.phone=t.tablet=t.hybrid=t.laptop=t.desktop=!1,t.formfactor="gameconsole";else{var c=function(){o("tv","tv"===t.formfactor),o("phone","phone"===t.formfactor),o("tablet","tablet"===t.formfactor),o("hybrid","hybrid"===t.formfactor),o("laptop","laptop"===t.formfactor),o("desktop","desktop"===t.formfactor)},u=function(){var e=Math.min(window.screen.width,window.screen.height);return t.tv?"tv":t.touch&&e<600?"phone":t.touch&&!t.mouse?"tablet":t.touch&&t.mouse?"hybrid":t.battery?"laptop":"desktop"};t.touch=navigator.maxTouchPoints>0,t.tv=r.includes("TV"),a=navigator.getGamepads?Array.from(navigator.getGamepads()).filter(function(e){return null!==e&&void 0!==e}).length:0,t.gamepad=a>0,e("(orientation: portrait)",function(e){t.portrait=e,t.landscape=!e,t.orientation=e?"portrait":"landscape",s("portrait",t.portrait),s("landscape",t.landscape),s("orientation",t.orientation)}),e("(any-pointer: coarse)",function(e){o("touch",e),o("formfactor",u())&&c()}),e("(hover: hover)",function(e){o("mouse",e),o("input",e?"mouse":"touch"),o("formfactor",u())&&c()})}}return t.csp=t.uwp||!1,t.nwjs?t.dev="sdk"===process.versions["nw-flavor"]:t.electron?t.dev=process.execPath.replace(/\\/g,"/").includes("node_modules/electron/"):t.uwp?t.dev=Windows.ApplicationModel.Package.current.isDevelopmentMode:t.node?t.dev="production"!==process.env.NODE_ENV:t.dev=void 0,t});
{
"name": "platform-detect",
"version": "2.0.3",
"version": "3.0.0",
"description": "🃏 Minimalistic isomorphic library for detection of platform, runtime, APIs and more.",

@@ -34,3 +34,6 @@ "author": "Mike Kovarik",

"devDependencies": {
"@babel/core": "^7.6.4",
"@babel/preset-env": "^7.6.3",
"rollup": "^1.11.3",
"rollup-plugin-babel": "^4.3.3",
"rollup-plugin-notify": "^1.0.1",

@@ -37,0 +40,0 @@ "rollup-plugin-uglify-es": "0.0.1"

# platform-detect
[![Build Status](https://travis-ci.org/MikeKovarik/platform-detect.svg)](https://travis-ci.org/MikeKovarik/platform-detect)
[![NPM](https://img.shields.io/npm/v/platform-detect.svg)](https://www.npmjs.com/package/platform-detect)
[![License](http://img.shields.io/npm/l/platform-detect.svg?style=flat)](LICENSE)
[![Build Status](https://travis-ci.org/MikeKovarik/platform-detect.svg?branch=master)](https://travis-ci.org/MikeKovarik/platform-detect)
[![Dependency Status](https://david-dm.org/MikeKovarik/platform-detect.svg)](https://david-dm.org/MikeKovarik/platform-detect)
[![devDependency Status](https://david-dm.org/MikeKovarik/platform-detect/dev-status.svg)](https://david-dm.org/MikeKovarik/platform-detect#info=devDependencies)
[![gzip size](http://img.badgesize.io/https://unpkg.com/platform-detect/index.js?compression=gzip)](https://unpkg.com/platform-detect)
[![Known Vulnerabilities](https://snyk.io/test/github/MikeKovarik/platform-detect/badge.svg)](https://snyk.io/test/github/MikeKovarik/platform-detect)
[![Maintainability](https://api.codeclimate.com/v1/badges/f4c0ee405c46126d6325/maintainability)](https://codeclimate.com/github/MikeKovarik/platform-detect/maintainability)
[![Discord](https://img.shields.io/discord/419198557363634178.svg)](https://discord.gg/v2mUmeD)
[![Gitter](https://badges.gitter.im/MikeKovarik/platform-detect.svg)](https://gitter.im/MikeKovarik/platform-detect?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
[![NPM](https://img.shields.io/npm/v/platform-detect.svg?style=flat)](https://www.npmjs.com/package/platform-detect)
[![License](http://img.shields.io/npm/l/platform-detect.svg?style=flat)](LICENSE)
🃏 Minimalistic isomorphic library for detection of platform, runtime, APIs and more.
[Check out the demo](http://htmlpreview.github.io/?https://github.com/MikeKovarik/platform-detect/blob/master/example.html)
[Check out the demo](http://mutiny.cz/platform-detect/)
* No dependencies
* Small size ~3kB
* Small size ~4kB
* Split into multiple files *"import just what you need"*

@@ -36,8 +32,12 @@ * ... but also available as a bundle of all detectors.

```js
```
npm install platform-detect
```
Minified bundle is also available at [unpkg.com/platform-detect](https://unpkg.com/platform-detect)
ur use CDN like [unpkg.com/platform-detect](https://unpkg.com/platform-detect) (comes with)
```js
<script src="https://unpkg.com/platform-detect"></script>
```
## Usage

@@ -120,5 +120,11 @@

### Usage in older browsers
Be advised: The library uses various ES6 and newer features. The syntax is compiled down to ES5 using babel. But you need to polyfill builtin methods in case you target browsers that don't implement them.
Namely `String.prototype.includes` and `Array.from`.
## API
[Check out the demo for full table of APIs](http://htmlpreview.github.io/?https://github.com/MikeKovarik/platform-detect/blob/master/demo.html)
[Check out the demo for full table of APIs](http://mutiny.cz/platform-detect/)

@@ -130,3 +136,3 @@ By importing the `index.js` bundle you get all of the below. Or you can pick only certain file with the checks you need.

* **`gui`** bool
<br>App has a window, access to DOM. Can render GUI
<br>App has a window & access to DOM. Can render GUI

@@ -151,3 +157,2 @@ ### `runtime.mjs`

* **`uwp`** bool
* ~~**`chromeapp`** bool~~

@@ -209,6 +214,6 @@ ### `formfactor.mjs`

platform.on('input', input => {
if (input === 'mouse)
console.log('keyboard attached')
else
console.log('tablet mode')
if (input === 'mouse')
console.log('keyboard attached')
else
console.log('tablet mode')
})

@@ -219,2 +224,3 @@ ```

* Mention .web in readme
* even better documentation

@@ -221,0 +227,0 @@ * fix dev mode detection

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc