Skip to content
microlinkhqPublic

About

Redis primitives to increment and rank top user-agent strings for bot analytics.

Topics

Resources

Stars

2 stars

Watchers

1 watching

Forks

Latest commit

 

History

71 Commits

Folders and files

Repository files navigation

microlink logo microlink logo

Last version Coverage Status NPM Status

A simple redis primitives to incr() and top() user agents.

Install

npm install @microlink/ua

Usage

First, initialize the library passing a Redis instance:

const Redis = require('ioredis')
const redis = new Redis()
const ua = require('@microlink/ua')(redis)

After that, the library offers the following methods:

  • incr(userAgent, [clientId]) — e.g. ua.incr(req.headers['user-agent'], req.ip)
    Increment the counter associated with a user agent. Only user agents with the exact shape a real browser sends are counted: a known platform (Windows, macOS, Linux, ChromeOS, Android, iOS), an engine that runs on it (Blink, WebKit, Gecko) and known browser vendor tokens only (Edge, Opera, Samsung, Yandex, Firefox, etc.). Bots, HTTP libraries, Electron apps, in-app webviews and hand-written fakes such as Chrome/124.0 or Mac OS X 14_6 are skipped without touching Redis.
    When clientId is provided, the same client is counted once per user agent within window seconds, so a single heavy caller cannot dominate the ranking. IP addresses are normalized first: IPv6 clients are grouped by their /64 network and IPv4-mapped IPv6 (::ffff:203.0.113.1) is the same client as its IPv4. Any other string, including an empty one or one that only looks numeric, is used as given; only null or undefined count every request. Each mark is a key holding a SHA-1 of the client id and user agent that expires after window; it is not anonymization, since anyone reading Redis can brute-force the client id back. Passing a clientId requires a Redis client that implements defineCommand, such as ioredis.

  • top(n, { withScore }) — e.g. ua.top(100)
    Returns an array of the N ranked from top.

  • flush() — e.g. ua.flush()
    Delete the counters. Client marks are kept and expire on their own after window, so a client seen just before a flush is counted again once its mark expires.

The factory accepts options as a second argument:

  • window — default: 86400
    Seconds during which a clientId is counted once per user agent.
const ua = require('@microlink/ua')(redis, { window: 60 * 60 })

The browser check is also exported for reuse:

const { isBrowser } = require('@microlink/ua')

isBrowser('Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 Chrome/124 Safari/537.36') // => false

License

microlink/ua © microlink.io, released under the MIT License.
Authored and maintained by Kiko Beats with help from contributors.

microlink.io · GitHub microlink.io · X @microlinkhq

About

Redis primitives to increment and rank top user-agent strings for bot analytics.

Topics

Resources

Stars

2 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages