New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@realeyes/environment-detector

Package Overview
Dependencies
Maintainers
2
Versions
65
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@realeyes/environment-detector - npm Package Compare versions

Comparing version 1.0.0 to 1.0.1

dist/environment-detector.js

160

index.js

@@ -8,9 +8,3 @@ /**

import swfobject from 'swfobject';
import detect from './lib/index';
import platform from './lib/platform';
import browsers from './lib/browsers';
import capabilities from './lib/capabilities';
import inArray from './lib/utils/inArray';
import compareVersions from './lib/utils/compareVersions';

@@ -23,155 +17,1 @@

window.Realeyesit.EnvironmentDetector = { detect };
/**
* Compatibility wrapper for old EnvironmentalDetectionAPI
*/
window.Realeyesit.EnvironmentalDetectionAPI = {
BLACKLISTED_FLASH_VERSIONS: ['11.4.31.110'],
MIN_FLASH_VERSION: '10.2.0',
BLACKLISTED_BROWSERS: [
[browsers.names.INTERNET_EXPLORER, '0-8'],
[browsers.names.FIREFOX, '0-3'],
[browsers.names.OPERA, '0-11'],
],
checkResults: {
checksPassed: null,
failureReasonCode: null,
failureReasonString: null,
},
failureReasonCodes: {
FLASH_NOT_INSTALLED: 1,
FLASH_TOO_OLD: 2,
FLASH_BLACKLISTED: 3,
BROWSER_NOT_CAPABLE: 4,
MOBILE_BROWSER: 5,
NO_WEBCAMS_DETECTED: 6,
OTHER_ERROR: 7,
},
/**
* Performs a list of checks and calls the callback with the checkResults object.
*
* @param {function()} callback Callback function
* @return {undefined}
*/
start(callback) {
this.clearResults();
this.checkResults.checksPassed = false;
detect({ flash: { path: '/dist/environment-checker.swf' } }).then(result => {
const flashVersion = swfobject.getFlashPlayerVersion();
const flashVersionString = `${flashVersion.major}.${flashVersion.minor}.${flashVersion.release}`;
if (!this.checkBrowser(result)) {
this.checkResults.failureReasonCode = this.failureReasonCodes.BROWSER_NOT_CAPABLE;
this.checkResults.failureReasonString = 'BROWSER_NOT_CAPABLE';
} else if (result.platform.type !== platform.types.DESKTOP) {
this.checkResults.failureReasonCode = this.failureReasonCodes.MOBILE_BROWSER;
this.checkResults.failureReasonString = 'MOBILE_BROWSER';
} else if (result.flash === null) {
this.checkResults.failureReasonCode = this.failureReasonCodes.FLASH_NOT_INSTALLED;
this.checkResults.failureReasonString = 'FLASH_NOT_INSTALLED';
} else if (compareVersions(flashVersionString, this.MIN_FLASH_VERSION) === -1) {
this.checkResults.failureReasonCode = this.failureReasonCodes.FLASH_TOO_OLD;
this.checkResults.failureReasonString = 'FLASH_TOO_OLD';
} else if (this.BLACKLISTED_FLASH_VERSIONS.indexOf(result.flash.version) !== -1) {
this.checkResults.failureReasonCode = this.failureReasonCodes.FLASH_BLACKLISTED;
this.checkResults.failureReasonString = 'FLASH_BLACKLISTED';
} else if (result.flash.webcams.length === 0) {
this.checkResults.failureReasonCode = this.failureReasonCodes.NO_WEBCAMS_DETECTED;
this.checkResults.failureReasonString = 'NO_WEBCAMS_DETECTED';
} else {
this.checkResults.checksPassed = true;
}
return callback(this.checkResults);
}, err => {
this.checkResults.failureReasonCode = this.failureReasonCodes.OTHER_ERROR;
this.checkResults.failureReasonString = err.message;
return callback(this.checkResults);
});
},
/**
* Checks if browser is capable for DC1 recording.
*
* @param {{
* browser: {
* name: string,
* version: string,
* },
* platform: {
* type: string,
* },
* os: {
* name: string,
* version: string,
* },
* flash: {
* version: string,
* webcams: Array.<string>,
* },
* capabilities: Array.<string>,
* }} res Environment check result
* @return {boolean}
*/
checkBrowser(res) {
// TODO: this check is probably better to be performed in "./src/browsers" module
// Detect IE <=7
if (inArray(res.capabilities, capabilities.names.DOCUMENT_ALL)
&& !inArray(res.capabilities, capabilities.names.DOCUMENT_QUERY_SELECTOR)) {
return false;
}
for (let i = 0; i < this.BLACKLISTED_BROWSERS.length; i++) {
const name = this.BLACKLISTED_BROWSERS[i][0];
const [from, to] = this.BLACKLISTED_BROWSERS[i][1].split('-');
if (name === res.browser.name
&& compareVersions(res.browser.version, from) >= 0
&& compareVersions(res.browser.version, to) <= 0) {
return false;
}
}
return true;
},
/**
* Resets the checkResults object.
*
* @return {undefined}
*/
clearResults() {
this.checkResults = {
checksPassed: null,
failureReasonCode: null,
failureReasonString: null,
};
},
};
/* eslint-disable */
/**
* Start detection immediately when window has a env detect callback.
*/
(function () {
if (typeof window._RealeyesitEnvDetectParams === 'object' &&
typeof window._RealeyesitEnvDetectParams._callback === 'function') {
window.Realeyesit.EnvironmentalDetectionAPI.start(window._RealeyesitEnvDetectParams._callback);
} else {
if (typeof window._RealeyesitEnvDetectCallback === 'function') {
window.Realeyesit.EnvironmentalDetectionAPI.start(window._RealeyesitEnvDetectCallback);
}
}
}());
/* eslint-enable */

10

lib/browsers.js

@@ -11,2 +11,7 @@ /**

/**
* Enum for the browsers that we can detect.
*
* @enum {string}
*/
const names = {

@@ -26,2 +31,5 @@ OPERA: 'Opera',

/**
* List of tests.
*/
const tests = [

@@ -85,3 +93,3 @@ {

* name: string,
* version: string
* version: ?string
* }}

@@ -88,0 +96,0 @@ */

@@ -11,2 +11,7 @@ /**

/**
* Enum for capabilities that we can detect.
*
* @enum
*/
const names = {

@@ -19,5 +24,10 @@ FLASH: 'flash',

DOCUMENT_QUERY_SELECTOR: 'document.querySelector',
HTTP: 'http',
HTTPS: 'https',
};
/**
* List of tests.
*/
const tests = [

@@ -65,2 +75,16 @@ {

},
{
capabilityName: names.HTTP,
test() {
return document.location.protocol === 'http:';
},
},
{
capabilityName: names.HTTPS,
test() {
return document.location.protocol === 'https:';
},
},
];

@@ -67,0 +91,0 @@

@@ -15,2 +15,24 @@ /**

/**
* @typedef {?{
* version: string,
* webcams: Array.<string>
* }} FlashResult
*/
/**
* @typedef {{
* getFlashVersion: function(): string,
* getCamerasNames: function(): Array.<string>
* }} FlashEnvironmentDetector
*/
/**
* Caches the flash object element.
*/
let flashEl;
export default {

@@ -23,6 +45,13 @@ /**

* @param {string} minFlashVersion Target player version
* @return {Promise.<*>}
* @return {Promise.<FlashEnvironmentDetector>}
*/
embedSWF(path, containerId, minFlashVersion) {
return new Promise((resolve, reject) => {
// immediately resolve if we have a cached flash element
if (flashEl !== undefined) {
resolve(flashEl);
return;
}
// create a flash placeholder element
const container = document.createElement('div');

@@ -35,4 +64,4 @@ container.id = containerId;

containerId, // placeholder element id
'0', // width
'0', // height
'10', // width
'1', // height
minFlashVersion, // target player version

@@ -48,3 +77,10 @@ null, // express install swf url

window.Realeyesit = window.Realeyesit || {};
window.Realeyesit.FlashEnvironmentCheckerReady = () => resolve(e.ref);
window.Realeyesit.FlashEnvironmentDetectorReady = () => {
flashEl = e.ref;
// push flash element above window top border
flashEl.style.position = 'absolute';
flashEl.style.top = '-10px';
resolve(e.ref);
};
}

@@ -57,16 +93,2 @@ }

/**
* Removes the swf checker object from DOM.
*
* @param {string} containerId Placeholder element id
* @return {undefined}
*/
cleanup(containerId) {
const container = document.getElementById(containerId);
if (container) {
container.parentNode.removeChild(container);
}
},
/**
* Performs a flash detection. Resolves with the version of flash and array of webcam names.

@@ -78,11 +100,8 @@ *

* @param {string} minFlashVersion Target player version
* @return {Promise.<{
* version: string,
* webcams: Array.<string>
* }|null>}
* @return {Promise.<FlashResult>}
*/
detect({
path = `${currentScriptPath}environment-checker.swf`,
path = `${currentScriptPath}environment-detector.swf`,
testTimeout = 3000,
containerId = 'Realeyesit_FlashEnvironmentChecker',
containerId = 'Realeyesit_FlashEnvironmentDetector',
minFlashVersion = '10.2.0',

@@ -94,14 +113,8 @@ } = {}) {

.then(() => this.embedSWF(path, containerId, minFlashVersion))
.then(flashChecker => {
const result = {
version: flashChecker.getFlashVersion().replace(/.*?(\d+),(\d+),(\d+),(\d+)/, '$1.$2.$3.$4'),
webcams: flashChecker.getCamerasNames(),
};
this.cleanup(containerId);
return result;
})
).catch(() => { this.cleanup(containerId); return null; });
.then(flashChecker => ({
version: flashChecker.getFlashVersion().replace(/.*?(\d+),(\d+),(\d+),(\d+)/, '$1.$2.$3.$4'),
webcams: flashChecker.getCamerasNames(),
}))
).catch(() => null);
},
};

@@ -13,7 +13,7 @@ /**

import flash from './flash';
import webcams from './webcam';
/**
* @param {*} options Options
* @return {Promise.<{
* @typedef {{
* browser: {

@@ -30,9 +30,28 @@ * name: string,

* },
* flash: {
* flash: ?{
* version: string,
* webcams: Array.<string>,
* },
* webcams: Array.<string>,
* capabilities: Array.<string>,
* }>}
* }} EnvironmentDetectionResult
*/
/**
* @typedef {{
* flash: {
* path: string,
* testTimeout: number,
* containerId: string,
* minFlashVersion: string,
* }
* }} EnvironmentDetectionOptions
*/
/**
* @param {EnvironmentDetectionOptions} options Options
* @return {Promise.<EnvironmentDetectionResult>}
*/
export default function detect(options = {}) {

@@ -42,4 +61,4 @@ const ua = navigator.userAgent;

return flash.detect(options.flash)
.then(flashResult => {
const result = {
.then(flashResult => webcams.detect(flashResult)
.then(webcamsResult => ({
browser: browsers.detect(ua),

@@ -49,23 +68,5 @@ platform: platform.detect(ua),

flash: flashResult,
webcams: webcamsResult,
capabilities: capabilities.detect(),
};
if (flashResult && flashResult.webcams.length) {
result.capabilities.push(capabilities.names.WEBCAM);
} else if (navigator.mediaDevices && navigator.mediaDevices.enumerateDevices) {
return navigator.mediaDevices.enumerateDevices()
.then(devices => {
for (const device of devices) {
if (device.kind === 'videoinput') {
result.capabilities.push(capabilities.names.WEBCAM);
break;
}
}
return result;
});
}
return result;
});
})));
}

@@ -11,2 +11,7 @@ /**

/**
* Enum for OSs that we can detect.
*
* @enum {string}
*/
const names = {

@@ -21,2 +26,5 @@ ANDROID: 'android',

/**
* List of tests.
*/
const tests = [

@@ -57,3 +65,3 @@ {

* name: string,
* version: string
* version: ?string
* }}

@@ -60,0 +68,0 @@ */

@@ -8,2 +8,7 @@ /**

/**
* Enum for platform types that we can detect.
*
* @enum {string}
*/
const types = {

@@ -16,2 +21,5 @@ MOBILE: 'mobile',

/**
* List of tests.
*/
const tests = [

@@ -18,0 +26,0 @@ {

@@ -15,3 +15,3 @@ /**

*/
export default function compareVersions(v1, v2) {
export default (v1, v2) => {
const s1 = v1.split('.');

@@ -29,2 +29,2 @@ const s2 = v2.split('.');

return 0;
}
};

@@ -18,2 +18,10 @@ /**

// Try to guess the script path in old browsers that do not support "document.currentScript" by parsing the "src"
// property of the last script tag in the document this will only work if the environment-checker.js bundle was
// included in the html document directly not using "async" of "defer" attributes.
// This will also give wrong result if the script tag was
// embedded dynamically and was not inserted after the last script tag in the document.
//
// If the environment-checker.js was embedded using one of the mentioned techniques please use the
// EnvironmentDetectionOptions to pass the flash.path string to the library.
const scripts = document.getElementsByTagName('script');

@@ -20,0 +28,0 @@ const src = scripts[scripts.length - 1].src.split('?')[0];

@@ -15,3 +15,3 @@ /**

*/
export default function inArray(arr, value) {
export default (arr, value) => {
if (typeof Array.prototype.indexOf === 'function') {

@@ -28,2 +28,2 @@ return arr.indexOf(value) !== -1;

return false;
}
};
{
"name": "@realeyes/environment-detector",
"version": "1.0.0",
"version": "1.0.1",
"scripts": {
"lint": "eslint .",
"build:flash": "mxmlc -static-link-runtime-shared-libraries=true -output dist/environment-checker.swf flash/EnvironmentChecker.as",
"build:js": "browserify index.js -d -t [ babelify ] > ./dist/environment-checker.js",
"build:flash": "mxmlc -static-link-runtime-shared-libraries=true -output dist/environment-detector.swf flash/EnvironmentDetector.as",
"build:js": "browserify index.js -d -t [ babelify ] > ./dist/environment-detector.js",
"build": "npm run build:flash && npm run build:js",
"cleanup": "rimraf dist/*",
"all": "npm run cleanup && npm run lint && npm run build"
"all": "npm run cleanup && npm run lint && npm run build",
"prepublish": "npm run all"
},

@@ -15,3 +16,3 @@ "license": "Apache-2.0",

"type": "git",
"url": "git+ssh://git@github.com/Realeyes/Data-Collection-Environment-Checker.git"
"url": "git+ssh://git@github.com/Realeyes/environment-detector.git"
},

@@ -33,7 +34,5 @@ "main": "lib/index.js",

"flex-sdk": "^4.6.0-0",
"minifyify": "^7.3.3",
"rimraf": "^2.5.4"
},
"dependencies": {
"babel-runtime": "^6.11.6",
"native-promise-only": "^0.8.1",

@@ -44,5 +43,5 @@ "swfobject": "^2.2.1"

"bugs": {
"url": "https://github.com/Realeyes/Data-Collection-Environment-Checker/issues"
"url": "https://github.com/Realeyes/environment-detector/issues"
},
"homepage": "https://github.com/Realeyes/Data-Collection-Environment-Checker#readme",
"homepage": "https://github.com/Realeyes/environment-detector#readme",
"directories": {

@@ -49,0 +48,0 @@ "example": "example"

@@ -1,3 +0,73 @@

Environment Detector
# Environment Detector
## Realeyesit.EnvironmentDetector API
`Realeyesit.EnvironmentDetector` namespace exposes one method `detect()`. This method returns a `Promise` object that resolves with EnvironmentDetectionResult object.
This is the type definition for the EnvironmentDetectionResult:
/**
* @typedef {{
* browser: {
* name: string,
* version: string,
* },
* platform: {
* type: string,
* },
* os: {
* name: string,
* version: string,
* },
* flash: ?{
* version: string,
* },
* webcams: Array.<string>,
* capabilities: Array.<string>,
* }} EnvironmentDetectionResult
*/
* **browser.name** is a string like "Chrome" or "Firefox"
* **browser.version** is a version string like "53.0"
* **platform.type** is one of "tablet", "mobile" or "desktop"
* **os.name** is a string like "windows" or "macos"
* **os.version** is a version string like "10.11"
* **flash.version** is a version string "\<major\>.\<minor\>.\<patch\>.\<build\>" format
* **webcams** is an array of strings, for example \["USB Camera", "iSight"\]
* **capabilities** is an array of strings
Here is the list of capabilities we can detect:
{
FLASH: 'flash',
GET_USER_MEDIA: 'navigator.getUserMedia',
MEDIA_RECORDER: 'MediaRecorder',
WEBCAM: 'webcam',
DOCUMENT_ALL: 'document.all',
DOCUMENT_QUERY_SELECTOR: 'document.querySelector',
HTTP: 'http',
HTTPS: 'https',
}
`detect()` can receive an optional `options` object with the following type definition:
/**
* @typedef {{
* flash: {
* path: string,
* testTimeout: number,
* containerId: string,
* minFlashVersion: string,
* }
* }} EnvironmentDetectionOptions
*/
### Example usage
```javascript
Realeyesit.EnvironmentDetector.detect()
.then(function (result) {
// result will be an EnvironmentDetectionResult object
});
```
## License

@@ -4,0 +74,0 @@

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