https://github.com/bsnext/fastify-bowser
A plugin for Fastify that adds the 'request.useragent' property to get header 'user-agent' parsed data.
https://github.com/bsnext/fastify-bowser
bowser fastify parser plugin typescript useragent
Last synced: 3 months ago
JSON representation
A plugin for Fastify that adds the 'request.useragent' property to get header 'user-agent' parsed data.
- Host: GitHub
- URL: https://github.com/bsnext/fastify-bowser
- Owner: bsnext
- License: other
- Created: 2024-10-19T08:49:10.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-06-10T04:10:21.000Z (about 1 year ago)
- Last Synced: 2025-06-27T03:50:53.421Z (12 months ago)
- Topics: bowser, fastify, parser, plugin, typescript, useragent
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/@bsnext/fastify-bowser
- Size: 21.5 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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](https://www.npmjs.com/package/bowser)", not "[ua-parser-js](https://www.npmjs.com/package/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](https://github.com/bsnext/fastify-bowser/blob/master/info/benchmark.md).
Why not [those package](https://github.com/Eomm/fastify-user-agent)? Under hood it have a "[useragent](https://www.npmjs.com/package/useragent)" - library with 2 dependencies. One of that is "LRU-Cache". But without cache it [have a performance less than both previously](https://github.com/bsnext/fastify-bowser/blob/master/info/benchmark.md) mentioned libraries. Also, **this library excludes parsing of user-agent until you call this property in request**.
## Installing:
```bash
npm install @bsnext/fastify-bowser
```
## Usage:
```ts
import FastifyBowser from '@bsnext/fastify-bowser'; // TS
import { default as FastifyBowser } from "@bsnext/fastify-bowser"; // MJS
const { default: FastifyBowser } = require(`@bsnext/fastify-bowser`); // CJS
const server = Fastify();
await server.register(FastifyBowser, {
// Use parsed user-agent LRU cache
cache: boolean = false;
// Cache LRU max size.
cacheLimit?: number = 100;
// Automatically cache purge interval in seconds.
// Disabled by default
cachePurgeTime?: number;
});
```
## Example
```ts
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);
/* {
browser: { name: 'Chrome', version: '129.0.0.0' },
os: { name: 'Windows', version: 'NT 10.0', versionName: '10' },
platform: { type: 'desktop' },
engine: { name: 'Blink' }
} */
})
server.listen({ port: 8080 });
```