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

@divvit/user-agent-device-mapper

Package Overview
Dependencies
Maintainers
3
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@divvit/user-agent-device-mapper - npm Package Compare versions

Comparing version 1.0.1 to 2.0.0

test-results/cobertura-coverage.xml

47

index.js

@@ -0,1 +1,7 @@

const crypto = require('crypto');
const LRU = require('lru-cache');
// Max number of elements allowed in the cache.
const CACHE_SIZE = 10000;
var tabletUserAgents = {

@@ -119,4 +125,3 @@ "iPad": "iPad|iPad.*Mobile",

var userAgentCache = {};
var userAgentCache = new LRU({max: CACHE_SIZE});
module.exports = {

@@ -126,4 +131,8 @@

getDeviceType: function(userAgent) {
if (!userAgentCache[userAgent]) {
if(!userAgent || userAgent === ''){
return 'desktop';
}
var uaHash = getUserAgentHash(userAgent);
var value = userAgentCache.get(uaHash);
if (!value) {
// check for tablet first

@@ -137,15 +146,35 @@ var bTabletFound = false;

if (bTabletFound) {
userAgentCache[userAgent] = 'tablet'
value = 'tablet'
} else {
// keep searching for more
if (/mobi/i.test(userAgent)) {
userAgentCache[userAgent] = 'mobile';
value = 'mobile';
} else {
userAgentCache[userAgent] = 'desktop';
value = 'desktop';
}
}
//Update cache.
userAgentCache.set(uaHash, value);
}
return userAgentCache[userAgent];
}
return value;
},
//Used only for test purposes
_getCacheSize: function(){
return userAgentCache.length;
},
//Used only for test purposes
_getElemFromCache: function(userAgent){
return userAgentCache.get(getUserAgentHash(userAgent));
},
_getCacheMaxSize: function(userAgent){
return CACHE_SIZE;
},
};
function getUserAgentHash(userAgent) {
return crypto.createHash('md5').update(userAgent).digest("hex");
}

5

package.json
{
"name": "@divvit/user-agent-device-mapper",
"version": "1.0.1",
"version": "2.0.0",
"description": "Map user agents to desktop, mobile or tablet.",

@@ -24,4 +24,5 @@ "main": "index.js",

"chai": "^3.4.1",
"mocha": "^2.3.4"
"mocha": "^2.3.4",
"lru-cache":"4.0.1"
}
}

@@ -45,2 +45,42 @@ var should = require('chai').should(),

});
it('Ensure cache is being used for tablet', function() {
const ua = 'mozilla/5.0_(linux;_u;_android_3.1;_en-us;_gt-p7510_build/hmj37)_applewebkit/534.13_(khtml,_like_gecko)_version/4.0_safari/534.13';
getDeviceType(ua).should.equal('tablet');
userAgentMapper._getElemFromCache(ua).should.equal('tablet');
});
it('Ensure cache is being used for mobile', function() {
const ua = 'Mozilla/5.0 (BlackBerry; U; BlackBerry 9850; en-US) AppleWebKit/534.11+ (KHTML, like Gecko) Version/7.0.0.254 Mobile Safari/534.11+';
getDeviceType(ua).should.equal('mobile');
userAgentMapper._getElemFromCache(ua).should.equal('mobile');
});
it('Ensure cache is being used for desktop', function() {
const ua = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.73 Safari/537.36';
getDeviceType(ua).should.equal('desktop');
userAgentMapper._getElemFromCache(ua).should.equal('desktop');
});
it('Ensure cache is size is limited', function () {
this.timeout(5000);
const cacheMaxSize = userAgentMapper._getCacheMaxSize()
for (var i = 0; i < cacheMaxSize * 2; i++) {
getDeviceType(i + ' Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.73 Safari/537.36');
}
userAgentMapper._getCacheSize().should.equal(cacheMaxSize);
});
it('Undefined UA should return desktop.', function() {
getDeviceType(undefined)
.should.equal('desktop');
});
it('Null UA should return desktop.', function() {
getDeviceType(null)
.should.equal('desktop');
});
it('Empty UA should return desktop.', function() {
getDeviceType('')
.should.equal('desktop');
});
});
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