Coder Social home page Coder Social logo

device's Introduction

device NPM version Downloads Build Status

device

Basically the express-device gained life of it's own and I had to refactor the basic functionality (it makes sense). Many were using express-device only to identify the type of device and didn't want all the express stuff.

how to use it?

$ npm install device

Here's an example on how to use it:

var device = require('device');
var mydevice = device('put here user-agent string');

if(mydevice.is('bot'))
	// do something...
else
	// do another thing...

By doing this you're getting an object that have the following properties:

NameField TypeDescriptionPossible Values
type string It gets the device type for the parsed user-agent string desktop, tv, tablet, phone, bot or car
model string It gets the device model name for the parsed user-agent string Example: iPhone. If the option parseUserAgent is set to false, then it will return an empty string

It accepts an object with only the config options you which to override (go here for some examples). The ones you don't override it will use the default values. Here's the list with the available config options:

NameField TypeDescriptionPossible Values
emptyUserAgentDeviceType string Device type to be returned whenever the has an empty user-agent. Defaults to desktop. desktop, tv, tablet, phone, bot, car or console
unknownUserAgentDeviceType string Device type to be returned whenever the user-agent is unknown. Defaults to phone. desktop, tv, tablet, phone, bot, car or console
botUserAgentDeviceType string Device type to be returned whenever the user-agent belongs to a bot. Defaults to bot. desktop, tv, tablet, phone, bot, car or console
carUserAgentDeviceType string Device type to be returned whenever the user-agent belongs to a car. Defaults to car. desktop, tv, tablet, phone, bot, car or console
consoleUserAgentDeviceType string Device type to be returned whenever the user-agent belongs to a console. Defaults to tv. desktop, tv, tablet, phone, bot, car or console
tvUserAgentDeviceType string Device type to be returned whenever the user-agent belongs to a tv. Defaults to tv. desktop, tv, tablet, phone, bot, car or console
parseUserAgent string Configuration to parse the user-agent string using the useragent npm package. It's needed in order to get the device name. Defaults to false. true | false

In case you didn't notice there's also a method is() that will return a boolean (true or false) when you pass the device type that you want validate against (check the initial example).

The device package is supported on node version 10.* upwards.

contributors

Some contributed in the express-device repository.

Special thanks to @jimmybergman that allowed me to use his device package for this refactoring.

where to go from here?

Currently, device is on version 0.3.12. In order to add more features I'm asking anyone to contribute with some ideas. You can do it by making some feature requests on the issues panel, but I prefer that you make your contribution with some pull requests ;)

In case you found a misinterpreted user-agent please open an issue and submit the full string and state what type of device it should return.

license

The MIT License (MIT)

Copyright (c) 2015 Rodrigo Guerreiro

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

device's People

Contributors

boreplusplus avatar cliftonc avatar fmmsilva avatar kelp404 avatar lennym avatar martincad avatar mettin avatar rguerreiro avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

device's Issues

Bug Fix Reporting and QA Guides?

Hi, what is the correct way to report a bug or trigger some QA procedure here? In my instance, the module fails to recognise latest Chrome, Firefox and Edge on Windows 10 Pro 64. What test you like me to run and what report you want me to submit in order to clear this up? Please note that I am aware that it might be my very local issue, hence I am not really pushing it ahead as a confirmed bug. I would rather first run some tests before submitting the bug report, thought I am not sure what the bug fixing policy is optimal for you.

Go-http-client wrong device detect

When I try to request my api with golang (By default, user agent is: "Go-http-client/1.1")
I have device type "phone", because my User-Agent is Unknown. But this is not a phone. I think this behaviour, when you set unknownUserAgentDeviceType: 'phone', is strange and wrong.
Can you add "Go-http-client" to desktop section?

wget shows up as a phone

Submitted by @reality in express-device repo

Thanks for your useful library, however there is one small issue: wget seems to be interpreted as a 'phone,' and I think it should be a 'bot.' Here is what I get when wgetting a simple page which simply res.sends the req.device:

{"parser":{"options":{"emptyUserAgentDeviceType":"desktop","unknownUserAgentDeviceType":"phone","botUserAgentDeviceType":"bot","carUserAgentDeviceType":"car","parseUserAgent":true},"useragent":{"family":"Wget","major":"1","minor":"16","patch":"3","device":{"family":"Other","major":"0","minor":"0","patch":"0"},"os":{"family":"Other","major":"0","minor":"0","patch":"0"}}},"type":"phone","name":"Other"} 

If I perform the same experiment using curl, then it correctly returns as a bot.

This was tested using: [email protected] [email protected] and [email protected]

Externalize list of useragents

I was thinking it might be nice to externalize the list of useragents to a separate module. That way your implementation is more clearly separated from the useragent data and updates should be easier.

Also, it'll be possible for other libraries to consume the list of useragents as well (which should attract more contributors and keep the list more up-to-date). A library like https://www.npmjs.com/package/useragent-list could suit as base and be modified to just json (so get rid of the xml), and to a structure that suits consumption by other libs (like express-useragent, who also mix useragent strings in their code).

What do you think?

Support car device type

Mozilla/5.0 (X11; u; Linux; C) AppleWebKit /533.3 (Khtml, like Gheko) QtCarBrowser Safari /533.3

Upgrade minimist

Upgrade minimist to version 0.2.1 or later. For example:

"dependencies": {
"minimist": ">=0.2.1"
}

or…

"devDependencies": {
"minimist": ">=0.2.1"
}

Desktop user agent without spaces reported as phone

One of our users has this user agent being sent from a desktop device (trying to get more specific information from them): Mozilla/5.0(WindowsNT6.1;rv:31.0)Gecko/20100101Firefox/31.0. Because there are no spaces it is being reported as unknown and set to the default value of phone.

The fix seems as easy as replacing ua.match(/Windows (NT|XP|ME|9)/) with ua.match(/Windows( )?(NT|XP|ME|9)/).

Does Not Declare Dependency `yamlparser` or `request`

This package requires usage of useragent in such a way that it always requires the usage of yamlparser and request too, but that is not declared in the package.json.

You can see instructions for why those packages are necessary in their documentation right here https://github.com/3rd-Eden/useragent#api.

This will async load the database from the server and compile it to a proper JavaScript supported format. If it fails to compile or load it from the remote location it will just fall back silently to the shipped version. If you want to use this feature you need to add yamlparser and request to your package.json
npm install yamlparser --save
npm install request --save

Therefore yamlparser and request should be considered a hard dependency of this library. If I use this library without installing yamlparser I cannot compile my application. It's essentially become an external dependency for this library, even though it really shouldn't be treated as such.

Module not found: Error: Can't resolve 'yamlparser' 
in '/Users/user/Documents/workspace/application/node_modules/useragent/lib

I believe the final json should look like this

  "dependencies": {
    "useragent": "*",
    "yamlparser": "*",
    "request": "*"
  },

AdsBot from Google is detected as "phone"

Can you please add AdsBot also to the botlist?

"Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1 (compatible; AdsBot-Google-Mobile; +http://www.google.com/mobile/adsbot.html)"

CloudFront headers

AWS CloudFront groups incoming user agents to these values:

CloudFront-Is-Desktop-Viewer
CloudFront-Is-Mobile-Viewer
CloudFront-Is-SmartTV-Viewer
CloudFront-Is-Tablet-Viewer

(source)

Maybe we can also add detection support for those in device library?

Error: Cannot find module "../package"

Great work!

Using webpack 2, I'm running into this error where the relative reference to package module in ./node_modules/device/lib/device.js breaks.

Seems normal as it looks for "package" in ./node_modules/device folder while it should be looking for it in ./node_modules.

getting empty response for get_model()!

Hi,
I am getting undefined for get_model().

Ex:
{ parser: DeviceParser { options: { emptyUserAgentDeviceType: 'desktop', unknownUserAgentDeviceType: 'phone', botUserAgentDeviceType: 'bot', carUserAgentDeviceType: 'car', parseUserAgent: false }, make_sure_parser_was_executed: [Function], get_model: [Function], get_type: [Function] }, type: 'phone', model: '', is: [Function: is] }

and my api response is : " "
empty for getmodel().

UserAgent is Mozilla/5.0 (Linux; Android 7.0; Redmi Note 4 Build/NRD90M) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.125 Mobile Safari/537.36

[Bug] Build issues with Angular project (webpack)

Webpack in an angular project seems to have issues with json imports when the extension of the file is not added in the require, as well as the package useragent having issues with yamlparser being in the wrong dependency property (is in dev deps, not in normal ones where it should).

screenshot from 2018-02-15 17-50-31

Googlebot on smartphone classified as "phone" instead of "bot"

@macinjosh32 submitted the following issue on express-device repo:

I'm seeing the following user agents being detected as "phone":

Mozilla/5.0 (iPhone; CPU iPhone OS 8_3 like Mac OS X) AppleWebKit/537.36 (KHTML, like Gecko) Version/8.0 Mobile/12F70 Safari/600.1.4 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)

Mozilla/5.0 (iPhone; CPU iPhone OS 83 like Mac OS X) AppleWebKit/600.1.4 (KHTML like Gecko) Version/8.0 Mobile/12F70 Safari/600.1.4 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)

More complete list here:
https://support.google.com/webmasters/answer/1061943?hl=en

Seems to me they should be categorized as "bot" to be consistent with the fact bot-ness trumps device type for desktop. What do you think?

TrendsmapResolver is not detected as a bot

Could this be added to the bot list?

    const ua = 'Mozilla/5.0 (compatible; TrendsmapResolver/0.1)';
    console.log(device(ua, {parseUserAgent: true} ))

Results in

    { parser: 
       DeviceParser {
         options: 
          { emptyUserAgentDeviceType: 'desktop',
            unknownUserAgentDeviceType: 'phone',
            botUserAgentDeviceType: 'bot',
            carUserAgentDeviceType: 'car',
            consoleUserAgentDeviceType: 'tv',
            tvUserAgentDeviceType: 'tv',
            parseUserAgent: true },
         make_sure_parser_was_executed: [Function],
         get_model: [Function],
         get_type: [Function],
         useragent: 
          Agent {
            family: 'Other',
            major: '0',
            minor: '0',
            patch: '0',
            source: 'Mozilla/5.0 (compatible; TrendsmapResolver/0.1)' } },
      type: 'phone',
      model: 'Other',
      is: [Function: is] }

Return desktop for Ipad Simulator from XCode

Target Device:

XCode Simulator:
IOS 13.3 - Ipad (7th generation)
IOS 13.3 - Ipad Pro (11 -inch)

Code Snippet:

const express = require('express');
const router = express.Router();

router.get('/', function(req, res, next) {

  const ua = req.headers['user-agent'];
  const theDevice = device.useragent_is(ua);
  console.log(theDevice.type);

  res.render('index', { title: 'Express' });
});

Actual Result

desktop

Expected Result

tablet

Universal support?

It would be nice to use the same library to detect devices on the server and on the client to write universal apps :)

No iPhone rule

Here's an iPhone 10 user agent string which is not recognized as "phone" by the library (falling back to the unknown device).

Mozilla/5.0 (iPhone; CPU iPhone OS 12_1_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Snapchat/10.47.5.13 (iPhone10,1; iOS 12.1.2; gzip)

Seems like an iPhone options is missing in the regex on this line:

device/lib/device.js

Lines 80 to 82 in 2a8bd17

} 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 'phone';

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.