express-useragent
Advanced tools
Comparing version 0.0.1 to 0.0.3
@@ -7,3 +7,3 @@ /** | ||
* @package express-useragent | ||
* @version 0.0.1 | ||
* @version 0.0.3 | ||
* @copyright Copyright (c) 2009-2012 - All rights reserved. | ||
@@ -10,0 +10,0 @@ * @license MIT License |
/** | ||
* | ||
* @revision $Id: user-agent.js 2012-03-24 16:21:10 Aleksey $ | ||
* @created 2012-03-24 16:21:10 | ||
* @created 2011-12-10 17:19:10 | ||
* @category Express Helpers | ||
* @package express-useragent | ||
* @version 0.0.1 | ||
* @copyright Copyright (c) 2009-2012 - All rights reserved. | ||
* @version 0.0.3 | ||
* @copyright Copyright (c) 2009-2011 - All rights reserved. | ||
* @license MIT License | ||
@@ -15,5 +15,5 @@ * @author Alexey Gordeyev IK <aleksej@gordejev.lv> | ||
var UserAgent = { | ||
version : '0.0.1', | ||
_Versions : { | ||
var UserAgent = function() { | ||
this.version = '0.0.3'; | ||
this._Versions = { | ||
Firefox: /firefox\/([\d\w\.\-]+)/i, | ||
@@ -23,6 +23,7 @@ IE: /msie\s([\d\.]+[\d])/i, | ||
Safari: /version\/([\d\w\.\-]+)/i, | ||
Opera: /version\/([\d\w\.\-]+)/i, | ||
Ps3: /([\d\w\.\-]+)\)\s*$/i, | ||
Psp: /([\d\w\.\-]+)\)?\s*$/i | ||
}, | ||
_Browsers : { | ||
}; | ||
this._Browsers = { | ||
Konqueror: /konqueror/i, | ||
@@ -36,4 +37,4 @@ Chrome: /chrome/i, | ||
Firefox: /firefox/i | ||
}, | ||
_OS : { | ||
}; | ||
this._OS = { | ||
WindowsVista: /windows nt 6\.0/i, | ||
@@ -51,4 +52,4 @@ Windows7: /windows nt 6\.\d+/i, | ||
iPhone: /\(iPhone.*os (\d+)[._](\d+)/i | ||
}, | ||
_Platform : { | ||
}; | ||
this._Platform = { | ||
Windows: /windows/i, | ||
@@ -64,4 +65,5 @@ Mac: /macintosh/i, | ||
Blackberry: /blackberry/i | ||
}, | ||
Agent : { | ||
}; | ||
this.Agent = {}; | ||
this.DefaultAgent = { | ||
isMobile : false, | ||
@@ -89,161 +91,158 @@ isiPad : false, | ||
source:'' | ||
} | ||
}; | ||
}; | ||
this.getBrowser = function(string) { | ||
switch (true) { | ||
case this._Browsers.Konqueror.test(string): | ||
this.Agent.isKonqueror = true; | ||
return 'Konqueror'; | ||
case this._Browsers.Chrome.test(string): | ||
this.Agent.isChrome = true; | ||
return 'Chrome'; | ||
case this._Browsers.Safari.test(string): | ||
this.Agent.isSafari = true; | ||
return 'Safari'; | ||
case this._Browsers.IE.test(string): | ||
this.Agent.isIE = true; | ||
return 'IE'; | ||
case this._Browsers.Opera.test(string): | ||
this.Agent.isOpera = true; | ||
return 'Opera'; | ||
case this._Browsers.PS3.test(string): | ||
return 'ps3'; | ||
case this._Browsers.PSP.test(string): | ||
return 'psp'; | ||
case this._Browsers.Firefox.test(string): | ||
this.Agent.isFirefox = true; | ||
return 'Firefox'; | ||
default: | ||
return 'unknown'; | ||
} | ||
}; | ||
this.getBrowserVersion = function(string) { | ||
var regex; | ||
UserAgent.getBrowser = function(string) { | ||
switch (true) { | ||
case this._Browsers.Konqueror.test(string): | ||
this.Agent.isKonqueror = true; | ||
return 'Konqueror'; | ||
case this._Browsers.Chrome.test(string): | ||
this.Agent.isChrome = true; | ||
return 'Chrome'; | ||
case this._Browsers.Safari.test(string): | ||
this.Agent.isSafari = true; | ||
return 'Safari'; | ||
case this._Browsers.IE.test(string): | ||
this.Agent.isIE = true; | ||
return 'IE'; | ||
case this._Browsers.Opera.test(string): | ||
this.Agent.isOpera = true; | ||
return 'Opera'; | ||
case this._Browsers.PS3.test(string): | ||
return 'ps3'; | ||
case this._Browsers.PSP.test(string): | ||
return 'psp'; | ||
case this._Browsers.Firefox.test(string): | ||
this.Agent.isFirefox = true; | ||
return 'Firefox'; | ||
default: | ||
return 'unknown'; | ||
} | ||
}; | ||
switch (this.Agent.Browser) { | ||
case 'Chrome': | ||
if (this._Versions.Chrome.test(string)) { | ||
return RegExp.$1; | ||
} | ||
break; | ||
case 'Safari': | ||
if (this._Versions.Safari.test(string)) { | ||
return RegExp.$1; | ||
} | ||
break; | ||
case 'Opera': | ||
if (this._Versions.Opera.test(string)) { | ||
return RegExp.$1; | ||
} | ||
break; | ||
case 'Firefox': | ||
if (this._Versions.Firefox.test(string)) { | ||
return RegExp.$1; | ||
} | ||
break; | ||
case 'IE': | ||
if (this._Versions.IE.test(string)) { | ||
return RegExp.$1; | ||
} | ||
break; | ||
case 'ps3': | ||
if (this._Versions.Ps3.test(string)) { | ||
return RegExp.$1; | ||
} | ||
break; | ||
case 'psp': | ||
if (this._Versions.Psp.test(string)) { | ||
return RegExp.$1; | ||
} | ||
break; | ||
default: | ||
regex = /#{name}[\/ ]([\d\w\.\-]+)/i; | ||
if (regex.test(string)) { | ||
return RegExp.$1; | ||
} | ||
} | ||
}; | ||
UserAgent.getBrowserVersion = function(string) { | ||
var regex; | ||
this.getOS = function(string) { | ||
switch (true) { | ||
case this._OS.WindowsVista.test(string): | ||
this.Agent.isWindows = true; | ||
this.Agent.isDesktop = true; | ||
return 'Windows Vista'; | ||
case this._OS.Windows7.test(string): | ||
this.Agent.isWindows = true; | ||
this.Agent.isDesktop = true; | ||
return 'Windows 7'; | ||
case this._OS.Windows2003.test(string): | ||
this.Agent.isWindows = true; | ||
return 'Windows 2003'; | ||
case this._OS.WindowsXP.test(string): | ||
this.Agent.isWindows = true; | ||
this.Agent.isDesktop = true; | ||
return 'Windows XP'; | ||
case this._OS.Windows2000.test(string): | ||
this.Agent.isWindows = true; | ||
return 'Windows 2000'; | ||
case this._OS.Linux.test(string): | ||
this.Agent.isLinux = true; | ||
this.Agent.isDesktop = true; | ||
return 'Linux'; | ||
case this._OS.Wii.test(string): | ||
return 'Wii'; | ||
case this._OS.PS3.test(string): | ||
return 'Playstation'; | ||
case this._OS.PSP.test(string): | ||
return 'Playstation'; | ||
case this._OS.OSX.test(string): | ||
this.Agent.isMac = true; | ||
return string.match(this._OS.OSX)[0].replace('_', '.'); | ||
case this._OS.iPad.test(string): | ||
this.Agent.isiPad = true; | ||
return string.match(this._OS.iPad)[0].replace('_', '.'); | ||
case this._OS.iPhone.test(string): | ||
this.Agent.isiPhone = true; | ||
return string.match(this._OS.iPhone)[0].replace('_', '.'); | ||
default: | ||
return 'unknown'; | ||
} | ||
}; | ||
this.getPlatform = function(string) { | ||
switch (true) { | ||
case this._Platform.Windows.test(string): | ||
return "Microsoft Windows"; | ||
case this._Platform.Mac.test(string): | ||
return "Apple Mac"; | ||
case this._Platform.Android.test(string): | ||
return "Android"; | ||
case this._Platform.Blackberry.test(string): | ||
return "Blackberry"; | ||
case this._Platform.Linux.test(string): | ||
return "Linux"; | ||
case this._Platform.Wii.test(string): | ||
return "Wii"; | ||
case this._Platform.Playstation.test(string): | ||
return "Playstation"; | ||
case this._Platform.iPad.test(string): | ||
return "iPad"; | ||
case this._Platform.iPod.test(string): | ||
return "iPod"; | ||
case this._Platform.iPhone.test(string): | ||
return "iPhone"; | ||
default: | ||
return 'unknown'; | ||
} | ||
}; | ||
switch (this.Agent.Browser) { | ||
case 'Chrome': | ||
if (this._Versions.Chrome.test(string)) { | ||
return RegExp.$1; | ||
} | ||
break; | ||
case 'Safari': | ||
if (this._Versions.Safari.test(string)) { | ||
return RegExp.$1; | ||
} | ||
break; | ||
case 'Firefox': | ||
if (this._Versions.Firefox.test(string)) { | ||
return RegExp.$1; | ||
} | ||
break; | ||
case 'IE': | ||
if (this._Versions.IE.test(string)) { | ||
return RegExp.$1; | ||
} | ||
break; | ||
case 'ps3': | ||
if (this._Versions.Ps3.test(string)) { | ||
return RegExp.$1; | ||
} | ||
break; | ||
case 'psp': | ||
if (this._Versions.Psp.test(string)) { | ||
return RegExp.$1; | ||
} | ||
break; | ||
default: | ||
regex = /#{name}[\/ ]([\d\w\.\-]+)/i; | ||
if (regex.test(string)) { | ||
return RegExp.$1; | ||
} | ||
} | ||
}; | ||
this.reset = function reset() { | ||
var ua = this; | ||
for( var key in ua.DefaultAgent) { | ||
ua.Agent[key] = ua.DefaultAgent[key]; | ||
} | ||
return ua; | ||
}; | ||
UserAgent.getOS = function(string) { | ||
switch (true) { | ||
case this._OS.WindowsVista.test(string): | ||
this.Agent.isWindows = true; | ||
this.Agent.isDesktop = true; | ||
return 'Windows Vista'; | ||
case this._OS.Windows7.test(string): | ||
this.Agent.isWindows = true; | ||
this.Agent.isDesktop = true; | ||
return 'Windows 7'; | ||
case this._OS.Windows2003.test(string): | ||
this.Agent.isWindows = true; | ||
return 'Windows 2003'; | ||
case this._OS.WindowsXP.test(string): | ||
this.Agent.isWindows = true; | ||
this.Agent.isDesktop = true; | ||
return 'Windows XP'; | ||
case this._OS.Windows2000.test(string): | ||
this.Agent.isWindows = true; | ||
return 'Windows 2000'; | ||
case this._OS.Linux.test(string): | ||
this.Agent.isLinux = true; | ||
this.Agent.isDesktop = true; | ||
return 'Linux'; | ||
case this._OS.Wii.test(string): | ||
return 'Wii'; | ||
case this._OS.PS3.test(string): | ||
return 'Playstation'; | ||
case this._OS.PSP.test(string): | ||
return 'Playstation'; | ||
case this._OS.OSX.test(string): | ||
this.Agent.isMac = true; | ||
return string.match(this._OS.OSX)[0].replace('_', '.'); | ||
case this._OS.iPad.test(string): | ||
this.Agent.isiPad = true; | ||
return string.match(this._OS.iPad)[0].replace('_', '.'); | ||
case this._OS.iPhone.test(string): | ||
this.Agent.isiPhone = true; | ||
return string.match(this._OS.iPhone)[0].replace('_', '.'); | ||
default: | ||
return 'unknown'; | ||
} | ||
}; | ||
UserAgent.getPlatform = function(string) { | ||
switch (true) { | ||
case this._Platform.Windows.test(string): | ||
return "Microsoft Windows"; | ||
case this._Platform.Mac.test(string): | ||
return "Apple Mac"; | ||
case this._Platform.Android.test(string): | ||
return "Android"; | ||
case this._Platform.Blackberry.test(string): | ||
return "Blackberry"; | ||
case this._Platform.Linux.test(string): | ||
return "Linux"; | ||
case this._Platform.Wii.test(string): | ||
return "Wii"; | ||
case this._Platform.Playstation.test(string): | ||
return "Playstation"; | ||
case this._Platform.iPad.test(string): | ||
return "iPad"; | ||
case this._Platform.iPod.test(string): | ||
return "iPod"; | ||
case this._Platform.iPhone.test(string): | ||
return "iPhone"; | ||
default: | ||
return 'unknown'; | ||
} | ||
}; | ||
UserAgent.parse = function parse(source) { | ||
this.Agent.source = source.replace(/^\s*/, '').replace(/\s*$/, ''); | ||
this.Agent.Browser = this.getBrowser(this.Agent.source); | ||
this.Agent.Version = this.getBrowserVersion(this.Agent.source); | ||
this.Agent.OS = this.getOS(this.Agent.source); | ||
this.Agent.Platform = this.getPlatform(this.Agent.source); | ||
return this.Agent; | ||
}; | ||
UserAgent.express = function() { | ||
return function(req, res, next) { | ||
var source = req.headers['user-agent'], | ||
ua = UserAgent; | ||
this.parse = function parse(source) { | ||
var ua = new UserAgent(); | ||
ua.Agent.source = source.replace(/^\s*/, '').replace(/\s*$/, ''); | ||
@@ -254,11 +253,24 @@ ua.Agent.Browser = ua.getBrowser(ua.Agent.source); | ||
ua.Agent.Platform = ua.getPlatform(ua.Agent.source); | ||
req.useragent = ua.Agent; | ||
res.local("useragent", ua.Agent); | ||
next(); | ||
} | ||
}; | ||
return ua.Agent; | ||
}; | ||
module.exports = UserAgent; | ||
this.express = function() { | ||
return function(req, res, next) { | ||
var source = req.headers['user-agent'], | ||
ua = new UserAgent(); | ||
ua.Agent.source = source.replace(/^\s*/, '').replace(/\s*$/, ''); | ||
ua.Agent.Browser = ua.getBrowser(ua.Agent.source); | ||
ua.Agent.Version = ua.getBrowserVersion(ua.Agent.source); | ||
ua.Agent.OS = ua.getOS(ua.Agent.source); | ||
ua.Agent.Platform = ua.getPlatform(ua.Agent.source); | ||
req.useragent = ua.Agent; | ||
res.locals("useragent", ua.Agent); | ||
next(); | ||
} | ||
}; | ||
this.Agent = this.DefaultAgent; | ||
}; | ||
exports = module.exports = new UserAgent(); |
{ | ||
"name": "express-useragent", | ||
"description": "Express user-agent middleware", | ||
"version": "0.0.1", | ||
"description": "ExpressJS user-agent middleware", | ||
"version": "0.0.3", | ||
"homepage": "https://github.com/biggora/express-useragent/", | ||
@@ -28,4 +28,4 @@ "repository": { | ||
"connect": "1.7.1", | ||
"express": "2.0.0" | ||
"express": "2.5.0" | ||
} | ||
} |
@@ -1,7 +0,10 @@ | ||
# express-useragent | ||
# Middleware exposing user-agent for [ExpressJS](http://expressjs.com/) | ||
A tiny ExpressJS user-agent middleware exposing user-agent details to your application and views. | ||
express-useragent is a simple ExpressJS user-agent middleware exposing user-agent details to your application and views. | ||
## Installation | ||
Installation is done using the Node Package Manager (npm). If you don't have npm installed on your system you can download it from [npmjs.org](http://npmjs.org/) | ||
To install express-useragent: | ||
$ npm install -g express-useragent | ||
@@ -27,3 +30,3 @@ | ||
### Example for Express | ||
### for Express | ||
@@ -58,2 +61,29 @@ ```js | ||
## Accessing the User-Agent | ||
If you are using `express` or `connect`, then `express-useragent` | ||
provides an easy way to access the user-agent as: | ||
- `req.useragent` from your app server | ||
- `useragent` helper accessible from your `express` views. | ||
## In the Wild | ||
The following projects use express-useragent. | ||
If you are using express-useragent in a project, app, or module, get on the list below | ||
by getting in touch or submitting a pull request with changes to the README. | ||
### Startups & Apps | ||
- [TViMama](http://tvimama.com/) | ||
- [GorkaTV](https://gorkatv.com/) | ||
## Author | ||
Aleksej Gordejev (aleksej@gordejev.lv). | ||
## License | ||
@@ -82,2 +112,9 @@ | ||
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | ||
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
## Resources | ||
- Visit the [author website](http://www.gordejev.lv). | ||
- Follow [@biggora](https://twitter.com/#!/biggora) on Twitter for updates. | ||
- Report issues on the [github issues](https://github.com/biggora/express-useragent/issues) page. |
@@ -7,3 +7,3 @@ /** | ||
* @package express-useragent | ||
* @version 0.0.1 | ||
* @version 0.0.3 | ||
* @copyright Copyright (c) 2009-2012 - All rights reserved. | ||
@@ -16,10 +16,16 @@ * @license MIT License | ||
var express = require('express') | ||
, app = express.createServer() | ||
, useragent = require('./../lib/express-useragent'); | ||
var Express = require('express') | ||
, App = Express.createServer() | ||
, Useragent = require('./../lib/express-useragent'); | ||
app.use(useragent.express()); | ||
app.get('/', function(req, res){ | ||
App.use(Useragent.express()); | ||
App.get('/', function(req, res){ | ||
res.send(req.useragent); | ||
}); | ||
app.listen(3000); | ||
// To test it try http://localhost:3000/ | ||
App.listen(3000); | ||
console.log('App started on port 3000'); |
@@ -7,3 +7,3 @@ /** | ||
* @package express-useragent | ||
* @version 0.0.1 | ||
* @version 0.0.3 | ||
* @copyright Copyright (c) 2009-2012 - All rights reserved. | ||
@@ -16,13 +16,40 @@ * @license MIT License | ||
var http = require('http') | ||
, useragent = require('./../lib/express-useragent'); | ||
var srv = http.createServer(function (req, res) { | ||
var source = req.headers['user-agent'], | ||
ua = useragent.parse(source); | ||
res.writeHead(200, {'Content-Type': 'text/plain'}); | ||
res.end(JSON.stringify(ua)); | ||
}); | ||
srv.listen(3000); | ||
var Ua = require('./../lib/express-useragent'); | ||
var ua1 = Ua.parse('Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.16) Gecko/20111108 Iceweasel/3.5.16 (like Firefox/3.5.16)'); | ||
console.log('Ua 1: ' + ua1.Browser); | ||
var ua2 = Ua.parse('Mozilla/5.0 (iPad; CPU OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A334 Safari/7534.48.3'); | ||
console.log('Ua 2: ' + ua2.Browser); | ||
var ua3 = Ua.parse('Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.16) Gecko/20111108 Iceweasel/3.5.16 (like Firefox/3.5.16)'); | ||
console.log('Ua 3: ' + ua3.Browser); | ||
var ua4 = Ua.parse('"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.162 Safari/535.19'); | ||
console.log('Ua 4: ' + ua4.Browser); | ||
console.log('Ua 1 is Safari? ' + ua1.isSafari); | ||
console.log('Ua 2 is Safari? ' + ua2.isSafari); | ||
console.log('Ua 3 is Safari? ' + ua3.isSafari); | ||
console.log('Ua 4 is Safari? ' + ua4.isSafari); | ||
/* | ||
* Results | ||
* Ua 1: Firefox | ||
* Ua 2: Safari | ||
* Ua 3: Firefox | ||
* Ua 4: Chrome | ||
* Ua 1 is Safari? false | ||
* Ua 2 is Safari? true | ||
* Ua 3 is Safari? false | ||
* Ua 4 is Safari? false | ||
*/ | ||
16696
7
355
117