Fastify-Bowser
The plugin adds the "request.useragent" property, which returns the parsed data.
Tested on Fastify v4.14+ and Node.JS v19+!
https://github.com/bsnext/fastify-bowser/actions/workflows/build_n_test.yml
Why "bowser", not "ua-parser-js" or other library?
Bowser it's a zero-dependency package with MIT license, unlike "ua-parser-js" under AGPL-3.0. Both of these libraries are good, but on top of that, bowser is about x4 times more faster.
Why not those package? Under hood it have a "useragent" - library with 2 dependencies. One of that is "LRU-Cache". But without cache it have a performance less than both previously mentioned libraries. Also, this library excludes parsing of user-agent until you call this property in request.
Installing:
npm install @bsnext/fastify-bowser
Usage:
import FastifyBowser from '@bsnext/fastify-bowser';
import { default as FastifyBowser } from "@bsnext/fastify-bowser";
const { default: FastifyBowser } = require(`@bsnext/fastify-bowser`);
const server = Fastify();
await server.register(FastifyBowser, {
cache: boolean = false;
cacheLimit?: number = 100;
cachePurgeTime?: number;
});
Example
import Fastify from 'fastify';
import FastifyBowser from '@bsnext/fastify-bowser';
const server = Fastify(...);
await server.register(FastifyBowser, { cache: true });
server.get(`/test`, function(request, response) {
response.send(request.useragent);
})
server.listen({ port: 8080 });