responder
Version:
Responsive/Adaptive utilities for your Node.js application
166 lines (138 loc) • 6.68 kB
JavaScript
/*
* responder
* https://github.com/jessecravens/responderjs
*
* Copyright (c) 2012 Jesse Cravens
* Licensed under the MIT license.
*/
Responder = {};
// "Xda IIs"
// Responder.userAgent = 'Mozilla/4.0 (compatible; MSIE 4.01; Windows CE; O2 Xda 2s;PPC;240x320; PPC; 240x320)';
// iPhone v4.0
// Responder.userAgent = 'Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Mobile/7D11'
// iPad
// Responder.userAgent = 'Mozilla/5.0 (iPad; U; CPU iPhone OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Mobile/7D11'
// MSIE 9 Windows Vista
// Responder.userAgent = exports.userAgent = 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.0; Trident/5.0)';
// Google Chrome 17.0.963.79 on Mac OSX Lion
// Responder.userAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.79 Safari/535.11'
exports.profile = profile = function(ua){
var nconf = require('nconf');
// Setup nconf to use (in-order):
// 1. Command-line arguments
// 2. Environment variables
// 3. A file located at 'path/to/config.json'
// the config file path should change, to the root of your application
nconf.argv()
.env()
.file({ file: './lib/config.json' });
//.file({ file: './node_modules/responder/lib/config.json' });
console.log("Using " + nconf.get('ddr') + " DDR (Device Description Repository)"); // reads from config.json
switch (nconf.get('ddr')) {
case "custom":
var custom = require('./useragents');
console.log("ua: " + ua);
// hardcoded for now
// to be replaced by your custom DDR specification
// If you run the tests you will see how poorly this detection performs
if (ua === undefined || ua === '') {
// No user agent.
return 'desktop';
}
if (ua.match(/GoogleTV|SmartTV|Internet TV|NetCast|NETTV|AppleTV|boxee|Kylo|Roku|DLNADOC|CE\-HTML/i)) {
// if user agent is a smart TV - http://goo.gl/FocDk
return 'largeScreen';
} else if (ua.match(/Xbox|PLAYSTATION 3|Wii/i)) {
// if user agent is a TV Based Gaming Console
return 'largeScreen';
} else if (ua.match(/iP(a|ro)d/i) || (ua.match(/tablet/i) && !ua.match(/RX-34/i)) || ua.match(/FOLIO/i)) {
// if user agent is a Tablet
return 'tablet';
} else if (ua.match(/Linux/i) && ua.match(/Android/i) && !ua.match(/Fennec|mobi|HTC Magic|HTCX06HT|Nexus One|SC-02B|fone 945/i)) {
// if user agent is an Android Tablet
return 'tablet';
} else if (ua.match(/Kindle/i) || (ua.match(/Mac OS/i) && ua.match(/Silk/i))) {
// if user agent is a Kindle or Kindle Fire
return 'tablet';
} else if (ua.match(/GT-P10|SC-01C|SHW-M180S|SGH-T849|SCH-I800|SHW-M180L|SPH-P100|SGH-I987|zt180|HTC( Flyer|_Flyer)|Sprint ATP51|ViewPad7|pandigital(sprnova|nova)|Ideos S7|Dell Streak 7|Advent Vega|A101IT|A70BHT|MID7015|Next2|nook/i) || (ua.match(/MB511/i) && ua.match(/RUTEM/i))) {
// if user agent is a pre Android 3.0 Tablet
return 'tablet';
} else if (ua.match(/BOLT|Fennec|Iris|Maemo|Minimo|Mobi|mowser|NetFront|Novarra|Prism|RX-34|Skyfire|Tear|XV6875|XV6975|Google Wireless Transcoder/i)) {
// if user agent is unique phone User Agent
return 'smartPhone';
} else if (ua.match(/Opera/i) && ua.match(/Windows NT 5/i) && ua.match(/HTC|Xda|Mini|Vario|SAMSUNG\-GT\-i8000|SAMSUNG\-SGH\-i9/i)) {
// if user agent is an odd Opera User Agent - http://goo.gl/nK90K
return 'smartPhone';
} else if ((ua.match(/Windows (NT|XP|ME|9)/) && !ua.match(/Phone/i)) && !ua.match(/Bot|Spider|ia_archiver|NewsGator/i) || ua.match(/Win( ?9|NT)/i)) {
// if user agent is Windows Desktop
return 'desktop';
} else if (ua.match(/Macintosh|PowerPC/i) && !ua.match(/Silk/i)) {
// if agent is Mac Desktop
return 'desktop';
} else if (ua.match(/Linux/i) && ua.match(/X11/i) && !ua.match(/Charlotte/i)) {
// if user agent is a Linux Desktop
return 'desktop';
} else if (ua.match(/Solaris|SunOS|BSD/i)) {
// if user agent is a Solaris, SunOS, BSD Desktop
return 'desktop';
} else if (ua.match(/Bot|B-O-T|Crawler|Spider|Spyder|Yahoo|ia_archiver|Covario-IDS|findlinks|DataparkSearch|larbin|Mediapartners-Google|NG-Search|Snappy|Teoma|Jeeves|Charlotte|NewsGator|TinEye|Cerberian|SearchSight|Zao|Scrubby|Qseero|PycURL|Pompos|oegp|SBIder|yoogliFetchAgent|yacy|webcollage|VYU2|voyager|updated|truwoGPS|StackRambler|Sqworm|silk|semanticdiscovery|ScoutJet|Nymesis|NetResearchServer|MVAClient|mogimogi|Mnogosearch|Arachmo|Accoona|holmes|htdig|ichiro|webis|LinkWalker|lwp-trivial/i) && !ua.match(/phone|Playstation/i)) {
// if user agent is a BOT/Crawler/Spider
return 'bot';
} else {
// Otherwise assume it is a phone Device
return 'smartPhone';
}
case "wurfl":
var wurfl = require('wurfl');
// block while reading WURFL
wurfl.loadSync();
// wurfl xml file is finished loading into a memory-efficient structure
var deviceInfo = wurfl.get(Responder.userAgent);
if (deviceInfo){
if (deviceInfo.product_info.is_tablet){
console.log('is tablet')
return 'tablet';
}
else if (deviceInfo.product_info.is_wireless_device){
console.log('is smartPhone')
return 'smartPhone';
}
else {
console.log('is desktop')
return 'desktop';
}
}
else{
console.log('ua not supported - so default to desktop');
return 'desktop';
};
case "openddr":
//
break;
case "deviceatlas":
//
break;
case "apachemap":
//
break;
case "51degrees":
//
break;
default:
//
}
};
// the middleware function
exports.init = init = function() {
return function(req, res, next) {
if (req.url === "/"){
var ua = req.headers['user-agent'];
var p = profile(ua);
res['profile'] = p;
next();
}
else {
next();
}
}
};