├── .gitignore ├── .npmignore ├── Makefile ├── index.js ├── History.md ├── examples └── index.js ├── package.json ├── lib └── connect-useragent.js └── Readme.md /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules 3 | *.sock 4 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | support 2 | test 3 | examples 4 | *.sock 5 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | 2 | test: 3 | @echo "populate me" 4 | 5 | .PHONY: test -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 2 | module.exports = require('./lib/connect-useragent'); -------------------------------------------------------------------------------- /History.md: -------------------------------------------------------------------------------- 1 | 2 | 0.0.2 / 2012-04-02 3 | ================== 4 | 5 | * Added support for any node 6 | 7 | 0.0.1 / 2011-10-02 8 | ================== 9 | 10 | * Initial release 11 | -------------------------------------------------------------------------------- /examples/index.js: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * Module dependencies. 4 | */ 5 | 6 | var connect = require('connect') 7 | , useragent = require('../'); 8 | 9 | connect() 10 | .use(connect.logger('dev')) 11 | .use(useragent()) 12 | .use(function(req, res){ 13 | console.log(req.agent); 14 | }) 15 | .listen(3000); -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "connect-useragent" 3 | , "version": "0.0.1" 4 | , "description": "Connect user-agent middleware" 5 | , "keywords": ["useragent", "connect", "middleware"] 6 | , "author": "TJ Holowaychuk " 7 | , "dependencies": { 8 | "useragent": "1.0.6" 9 | } 10 | , "devDependencies": { 11 | "connect": "1.7.1" 12 | } 13 | , "main": "index" 14 | , "engines": { "node": "*" } 15 | } -------------------------------------------------------------------------------- /lib/connect-useragent.js: -------------------------------------------------------------------------------- 1 | 2 | /*! 3 | * connect-useragent 4 | * Copyright(c) 2011 TJ Holowaychuk 5 | * MIT Licensed 6 | */ 7 | 8 | /** 9 | * Module dependencies. 10 | */ 11 | 12 | var ua = require('useragent'); 13 | 14 | /** 15 | * Expose the middleware. 16 | */ 17 | 18 | module.exports = useragent; 19 | 20 | /** 21 | * Parse the user-agent string when available and `next()`. 22 | * 23 | * @api public 24 | */ 25 | 26 | function useragent() { 27 | return function(req, res, next){ 28 | req.agent = ua.parse(req.headers['user-agent'] || ''); 29 | var family = req.agent.family 30 | , os = req.agent.os; 31 | req.agent.family = { name: family, machine: machine(family) }; 32 | req.agent.os = { name: os, machine: machine(os) }; 33 | next(); 34 | } 35 | } 36 | 37 | /** 38 | * Convert the given `str` to a machine-readable string 39 | * suitable for use as an html class name etc. 40 | * 41 | * @param {String} str 42 | * @return {String} 43 | * @api private 44 | */ 45 | 46 | function machine(str) { 47 | return str 48 | .toLowerCase() 49 | .replace(/ +/g, '-'); 50 | } -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | 2 | # connect-useragent 3 | 4 | A tiny Connect user-agent middleware exposing user-agent details to your application and views. A good idea by @guille backed by @3rd-Eden's [useragent](https://github.com/3rd-Eden/useragent) module. 5 | 6 | ## Installation 7 | 8 | $ npm install connect-useragent 9 | 10 | ## Example 11 | 12 | ```js 13 | var connect = require('connect') 14 | , useragent = require('connect-useragent'); 15 | 16 | connect() 17 | .use(connect.logger('dev')) 18 | .use(useragent()) 19 | .use(function(req, res){ 20 | console.log(req.agent); 21 | }) 22 | .listen(3000); 23 | ``` 24 | 25 | provides details such as the following: 26 | 27 | ```js 28 | { family: { name: 'Safari', machine: 'safari' }, 29 | major: '5', 30 | minor: '0', 31 | patch: '4', 32 | os: { name: 'Mac OS X', machine: 'mac-os-x' } } 33 | ``` 34 | 35 | ## License 36 | 37 | (The MIT License) 38 | 39 | Copyright (c) 2011 TJ Holowaychuk <tj@vision-media.ca> 40 | 41 | Permission is hereby granted, free of charge, to any person obtaining 42 | a copy of this software and associated documentation files (the 43 | 'Software'), to deal in the Software without restriction, including 44 | without limitation the rights to use, copy, modify, merge, publish, 45 | distribute, sublicense, and/or sell copies of the Software, and to 46 | permit persons to whom the Software is furnished to do so, subject to 47 | the following conditions: 48 | 49 | The above copyright notice and this permission notice shall be 50 | included in all copies or substantial portions of the Software. 51 | 52 | THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, 53 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 54 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 55 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 56 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 57 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 58 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. --------------------------------------------------------------------------------