npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@creative-web-solution/client-device-detection

v0.0.3

Published

Try to guess client device. It uses UserAgent, so don't rely on this for critical stuff.

Downloads

3

Readme

Client device detection

Try to guess client device.

/!\ It uses UserAgent, so don't rely on this for critical stuff

This autoload bundle only works with the website socle.

Dependencies

  • detect-browser

Request

It will add a new property on the request object: request.clientDevice.

It contains (example with Chrome 89):

{
    "userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36",
    "browser": "chrome",
    "features": {},
    "version": {
        "text": "89.0.4389.90",
        "major": 89,
        "minor": 0,
        "patch": 4389
    },
    "type": "browser",
    "isUnknown": false,
    "isBrowser": true,
    "isBot": false,
    "isiOS": false,
    "isAndroid": false,
    "isWindows": false,
    "isMacOs": true
}

Autoload configuration

The package will be automatically added to to the autoload array in the config/app/bundles.json file.

If not, do it manually:

{
    "autoload": [
        ...,
        {
            "name":   "@creative-web-solution/client-device-detection"
        },
        ...
    ]
}

You can set a configuration if needed:

{
    "autoload": [
        ...,
        {
            "name":   "@creative-web-solution/client-device-detection",
            "config": {
                "configName": "clientDeviceDetection"
            }
        },
        ...
    ]
}

Add a clientDeviceDetection.json file in config/packages/ looking like this:

{
    "clientDeviceDetection": {
        // Optional: See below for feature detection
        "builtIn": [ "builInFeatureName" ],
        // Optional
        "features": {
            // See below for feature detection
            ...
        }
    }
}

It will be executed on every route except the static ones.

Configuration as middleware

export default [
    {
        "name":   "some.route",
        "route":  AppPath.getUrlWithBasePath( '/path' ),
        "method": "get",
        "middlewares": [
            addMiddleware({
                "middlewarePath": "@creative-web-solution/client-device-detection",
                // Optional
                "data": {
                    // Optional: See below for feature detection
                    "builtIn": [ "builInFeatureName" ],
                    "features": {
                        // See below for feature detection
                        ...
                    }
                }
            }),
            ...
        ]
    }
]

See "Custom middlewares" in 7-middlewares.md to access the result in the controller.

Device feature support

Add a features property the the data object sent to the middleware. it's contain the name of the feature and the browser version it works on:

{
    "features": {
        "featureName": {
            "browserName1": [ major [, minor [, patch ]] ],
            "browserName2": [ major [, minor [, patch ]] ]
        }
    }
}

major version is mandatory

For example with webp images support:

export default [
    {
        "name":   "some.route",
        "route":  AppPath.getUrlWithBasePath( '/path' ),
        "method": "get",
        "middlewares": [
            addMiddleware({
                "middlewarePath": "@core/Middleware/ClientDeviceDetection",
                "data": {
                    "features": {
                        "webp": {
                            // Optional
                            "header": [ "accept", "image/webp" ],
                            "deviceVersion": {
                                "chrome":   [ 32 ],
                                "firefox":  [ 65 ],
                                "edge": [ 18 ],
                                "edge-chromium": [ 88 ],
                                "opera": [ 19 ],
                                "samsung": [ 4 ],
                                "ios": [ 14, 5 ]
                            }
                        }
                    }
                }
            }),
            ...
        ]
    }
]

header is optional and has 2 versions:

  • With 1 parameter like header": [ "some-header" ]: check the header existence
  • With 2 parameters like header": [ "some-header", "some-value" ]: check the header value (lowerCase the header value and use indexOf on it to check)

deviceVersion is optional. Here the list of available browsers name:

aol, edge, edge-ios, yandexbrowser, kakaotalk, samsung, silk, miui, beaker, edge-chromium, chrome, chromium-webview, phantomjs, crios, firefox, fxios, opera-mini, opera, ie, bb10, android, ios, safari, facebook, instagram, ios-webview, curl, searchbot

If both header and deviceVersion are set, the header will be check first. If the feature is not detected with the header part, the deviceVersion check will be use.

It will update the features property of the result object, with the name of the feature and true or false

{
    "userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36",
    "browser": "chrome",
    "features": {
        "webp": true
    },
    ...
}

BuiltIn feature support

There are 3 features test:

  • lazyLoading
  • webp
  • avif

To use it, insert their name in the builtIn array:

{
    "clientDeviceDetection": {
        "builtIn": [ "lazyLoading", ... ],
        // Optional
        "features": {
            ...
        }
    }
}