@davaux/user-agent 0.8.0 → 0.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,56 @@
1
+ import type { MiddlewareFn } from 'davaux';
2
+ export interface UserAgent {
3
+ /** Raw `User-Agent` header value. Empty string when the header is absent. */
4
+ raw: string;
5
+ /** `true` when the UA contains mobile device indicators (excludes tablets). */
6
+ isMobile: boolean;
7
+ /** `true` when the UA matches a tablet (iPad, Android without Mobile token). */
8
+ isTablet: boolean;
9
+ /** `true` when the client is a desktop/laptop (not bot, not mobile, not tablet). */
10
+ isDesktop: boolean;
11
+ /** `true` when the UA matches a known bot, crawler, or non-browser client. */
12
+ isBot: boolean;
13
+ /** Bot name when `isBot` is true, e.g. `'Googlebot'`, `'curl'`. */
14
+ botName?: string;
15
+ /** Normalized device category. */
16
+ deviceType: 'mobile' | 'tablet' | 'desktop' | 'bot' | 'unknown';
17
+ /** Browser family, e.g. `'Chrome'`, `'Firefox'`, `'Safari'`, `'Edge'`. */
18
+ browserName?: string;
19
+ /** Browser version string, e.g. `'120.0.6099.130'`. */
20
+ browserVersion?: string;
21
+ /** OS family, e.g. `'Windows'`, `'macOS'`, `'Android'`, `'iOS'`, `'Linux'`. */
22
+ osName?: string;
23
+ /** OS version string, e.g. `'10'`, `'15.7'`, `'13'`. */
24
+ osVersion?: string;
25
+ /** Rendering engine, e.g. `'Blink'`, `'Gecko'`, `'WebKit'`, `'Trident'`. */
26
+ engine?: string;
27
+ /**
28
+ * `true` when signals are conflicting or insufficient to fully trust the result.
29
+ * Examples: a non-browser runtime running on a mobile/tablet OS (e.g. Dalvik on
30
+ * Android), or a Mozilla-prefixed UA with no detectable browser or OS (e.g. a
31
+ * gaming console with an unrecognised browser engine).
32
+ */
33
+ partial: boolean;
34
+ }
35
+ declare module 'davaux' {
36
+ interface State {
37
+ userAgent: UserAgent;
38
+ }
39
+ }
40
+ /**
41
+ * Parses the `User-Agent` request header and exposes structured data as
42
+ * `ctx.state.userAgent`. When the header is absent, `raw` is an empty string
43
+ * and all other fields are `undefined` / `false`.
44
+ *
45
+ * ```ts
46
+ * import { userAgent } from '@davaux/user-agent'
47
+ *
48
+ * // _global.ts or _middleware.ts
49
+ * export default userAgent()
50
+ *
51
+ * // A route handler:
52
+ * const { isMobile, isBot, browserName, osName } = ctx.state.userAgent
53
+ * ```
54
+ */
55
+ export declare function userAgent(): MiddlewareFn;
56
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAA;AAI1C,MAAM,WAAW,SAAS;IACxB,6EAA6E;IAC7E,GAAG,EAAE,MAAM,CAAA;IACX,+EAA+E;IAC/E,QAAQ,EAAE,OAAO,CAAA;IACjB,gFAAgF;IAChF,QAAQ,EAAE,OAAO,CAAA;IACjB,oFAAoF;IACpF,SAAS,EAAE,OAAO,CAAA;IAClB,8EAA8E;IAC9E,KAAK,EAAE,OAAO,CAAA;IACd,mEAAmE;IACnE,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,kCAAkC;IAClC,UAAU,EAAE,QAAQ,GAAG,QAAQ,GAAG,SAAS,GAAG,KAAK,GAAG,SAAS,CAAA;IAC/D,0EAA0E;IAC1E,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB,uDAAuD;IACvD,cAAc,CAAC,EAAE,MAAM,CAAA;IACvB,+EAA+E;IAC/E,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,wDAAwD;IACxD,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,4EAA4E;IAC5E,MAAM,CAAC,EAAE,MAAM,CAAA;IACf;;;;;OAKG;IACH,OAAO,EAAE,OAAO,CAAA;CACjB;AAED,OAAO,QAAQ,QAAQ,CAAC;IACtB,UAAU,KAAK;QACb,SAAS,EAAE,SAAS,CAAA;KACrB;CACF;AAkPD;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,SAAS,IAAI,YAAY,CAMxC"}
package/dist/index.js ADDED
@@ -0,0 +1,240 @@
1
+ // ─── Bot detection ────────────────────────────────────────────────────────────
2
+ // Named patterns match in any UA format, including browser-disguised bots.
3
+ const BOT_PATTERNS = [
4
+ [/Googlebot/i, 'Googlebot'],
5
+ [/Bingbot/i, 'Bingbot'],
6
+ [/DuckDuckBot/i, 'DuckDuckBot'],
7
+ [/Yahoo! Slurp/i, 'Yahoo Slurp'],
8
+ [/Baiduspider/i, 'Baiduspider'],
9
+ [/YandexBot/i, 'YandexBot'],
10
+ [/Sogou/i, 'Sogou'],
11
+ [/facebookexternalhit/i, 'FacebookBot'],
12
+ [/Twitterbot/i, 'Twitterbot'],
13
+ [/Applebot/i, 'Applebot'],
14
+ [/AdsBot-Google/i, 'AdsBot-Google'],
15
+ [/SemrushBot/i, 'SemrushBot'],
16
+ [/AhrefsBot/i, 'AhrefsBot'],
17
+ [/MJ12bot/i, 'MJ12bot'],
18
+ [/DotBot/i, 'DotBot'],
19
+ [/Screaming Frog/i, 'Screaming Frog'],
20
+ [/Scrapy/i, 'Scrapy'],
21
+ [/curl\//i, 'curl'],
22
+ [/wget\//i, 'wget'],
23
+ [/python-requests/i, 'python-requests'],
24
+ [/Python-urllib/i, 'Python-urllib'],
25
+ [/Go-http-client/i, 'Go HTTP'],
26
+ [/Java\/\d/i, 'Java'],
27
+ [/libwww-perl/i, 'libwww-perl'],
28
+ ];
29
+ function detectBot(ua) {
30
+ // Named patterns first — work regardless of Mozilla prefix.
31
+ for (const [pattern, name] of BOT_PATTERNS) {
32
+ if (pattern.test(ua))
33
+ return { isBot: true, botName: name };
34
+ }
35
+ // Non-browser clients never start with Mozilla/.
36
+ if (ua && !/^Mozilla\//i.test(ua)) {
37
+ // Try Name/Version token (covers most HTTP libs, CMSes, app runtimes).
38
+ // Allow commas so tokens like "AppleTV14,1" are captured whole.
39
+ const m = /^([\w][\w.,+-]*)\//.exec(ua);
40
+ if (m)
41
+ return { isBot: true, botName: m[1] };
42
+ // No version token — extract service name before a generic bot keyword.
43
+ // e.g. "Traackr.com Bot" → "Traackr.com"
44
+ const k = /^(.*?)\s+(?:bot|crawler|spider)\b/i.exec(ua);
45
+ if (k)
46
+ return { isBot: true, botName: k[1].trim() || 'Unknown' };
47
+ return { isBot: true, botName: 'Unknown' };
48
+ }
49
+ // Mozilla-prefixed bots: URL reference gives a better name than generic keywords.
50
+ // Real browsers never embed a URL in their UA string.
51
+ if (/https?:\/\//i.test(ua)) {
52
+ const m = /\(compatible;\s*([\w][\w. +-]*)\//.exec(ua);
53
+ return { isBot: true, botName: m ? m[1].trim() : 'Unknown' };
54
+ }
55
+ // Last resort: generic keyword match for Mozilla-prefixed unknown bots.
56
+ if (/\b(?:bot|crawler|spider)\b/i.test(ua))
57
+ return { isBot: true, botName: 'Unknown' };
58
+ return { isBot: false };
59
+ }
60
+ // ─── Device detection ─────────────────────────────────────────────────────────
61
+ function detectTablet(ua) {
62
+ if (/iPad/i.test(ua))
63
+ return true;
64
+ // Android tablets omit "Mobile" from their UA string.
65
+ if (/Android/i.test(ua) && !/Mobile/i.test(ua))
66
+ return true;
67
+ return false;
68
+ }
69
+ function detectMobile(ua) {
70
+ return /Mobi|Android|iPhone|iPod|Windows Phone|BlackBerry|IEMobile/i.test(ua);
71
+ }
72
+ // ─── Browser detection ────────────────────────────────────────────────────────
73
+ function extractVersion(ua, token) {
74
+ const m = new RegExp(`${token}/([\\d._]+)`, 'i').exec(ua);
75
+ return m ? m[1].replace(/_/g, '.') : undefined;
76
+ }
77
+ function detectBrowser(ua) {
78
+ // Samsung Browser embeds Chrome in its UA — check first.
79
+ if (/SamsungBrowser\//i.test(ua)) {
80
+ return {
81
+ browserName: 'Samsung Browser',
82
+ browserVersion: extractVersion(ua, 'SamsungBrowser'),
83
+ engine: 'Blink',
84
+ };
85
+ }
86
+ // Chromium-based Edge ("Edg/") ships alongside Chrome token — check before Chrome.
87
+ if (/Edg\/\d/i.test(ua)) {
88
+ return { browserName: 'Edge', browserVersion: extractVersion(ua, 'Edg'), engine: 'Blink' };
89
+ }
90
+ // Legacy EdgeHTML-based Edge.
91
+ if (/Edge\/\d/i.test(ua)) {
92
+ return { browserName: 'Edge', browserVersion: extractVersion(ua, 'Edge'), engine: 'EdgeHTML' };
93
+ }
94
+ // Opera (Chromium-based, "OPR/").
95
+ if (/OPR\/\d/i.test(ua)) {
96
+ return { browserName: 'Opera', browserVersion: extractVersion(ua, 'OPR'), engine: 'Blink' };
97
+ }
98
+ // Chromium (open-source build, distinct from Google Chrome).
99
+ if (/Chromium\/\d/i.test(ua)) {
100
+ return {
101
+ browserName: 'Chromium',
102
+ browserVersion: extractVersion(ua, 'Chromium'),
103
+ engine: 'Blink',
104
+ };
105
+ }
106
+ // Google Chrome.
107
+ if (/Chrome\/\d/i.test(ua)) {
108
+ return { browserName: 'Chrome', browserVersion: extractVersion(ua, 'Chrome'), engine: 'Blink' };
109
+ }
110
+ // Firefox.
111
+ if (/Firefox\/\d/i.test(ua)) {
112
+ return {
113
+ browserName: 'Firefox',
114
+ browserVersion: extractVersion(ua, 'Firefox'),
115
+ engine: 'Gecko',
116
+ };
117
+ }
118
+ // Safari — must come after Chrome/Chromium since their UAs also contain "Safari".
119
+ if (/Safari\/\d/i.test(ua)) {
120
+ return {
121
+ browserName: 'Safari',
122
+ // "Version/x.x" carries the human-readable Safari version; fall back to Safari token.
123
+ browserVersion: extractVersion(ua, 'Version') ?? extractVersion(ua, 'Safari'),
124
+ engine: 'WebKit',
125
+ };
126
+ }
127
+ // Internet Explorer 11 (Trident/7 with rv: version).
128
+ if (/Trident\/\d/i.test(ua)) {
129
+ const m = /rv:([\d.]+)/.exec(ua);
130
+ return { browserName: 'Internet Explorer', browserVersion: m?.[1], engine: 'Trident' };
131
+ }
132
+ // Internet Explorer ≤ 10.
133
+ const ieMatch = /MSIE\s([\d.]+)/i.exec(ua);
134
+ if (ieMatch) {
135
+ return { browserName: 'Internet Explorer', browserVersion: ieMatch[1], engine: 'Trident' };
136
+ }
137
+ return {};
138
+ }
139
+ // ─── OS detection ─────────────────────────────────────────────────────────────
140
+ const WINDOWS_NT_VERSIONS = {
141
+ '10.0': '10',
142
+ '6.3': '8.1',
143
+ '6.2': '8',
144
+ '6.1': '7',
145
+ '6.0': 'Vista',
146
+ '5.2': 'XP',
147
+ '5.1': 'XP',
148
+ '5.0': '2000',
149
+ };
150
+ function detectOS(ua) {
151
+ // Windows Phone must be checked before Windows NT.
152
+ let m = /Windows Phone(?:\s+OS)?\s+([\d.]+)/i.exec(ua);
153
+ if (m)
154
+ return { osName: 'Windows Phone', osVersion: m[1] };
155
+ // Android (appears on both mobile and tablet UAs).
156
+ m = /Android\s+([\d.]+)/i.exec(ua);
157
+ if (m)
158
+ return { osName: 'Android', osVersion: m[1] };
159
+ // iOS — covers iPhone, iPad (iPadOS ≥ 13 drops "iPhone" token), and iPod.
160
+ m = /(?:iPhone|iPad|iPod)[^)]*OS\s+([\d_]+)/i.exec(ua);
161
+ if (m)
162
+ return { osName: 'iOS', osVersion: m[1].replace(/_/g, '.') };
163
+ // macOS / OS X.
164
+ m = /Mac OS X\s+([\d_.]+)/i.exec(ua);
165
+ if (m)
166
+ return { osName: 'macOS', osVersion: m[1].replace(/_/g, '.') };
167
+ // Windows NT.
168
+ m = /Windows NT\s+([\d.]+)/i.exec(ua);
169
+ if (m)
170
+ return { osName: 'Windows', osVersion: WINDOWS_NT_VERSIONS[m[1]] ?? m[1] };
171
+ if (/CrOS/i.test(ua))
172
+ return { osName: 'Chrome OS' };
173
+ if (/Linux/i.test(ua))
174
+ return { osName: 'Linux' };
175
+ return {};
176
+ }
177
+ // ─── Parser ───────────────────────────────────────────────────────────────────
178
+ function parse(raw) {
179
+ const { isBot, botName } = detectBot(raw);
180
+ // Suppress device-type signals when isBot — they become noise, not signal.
181
+ const isTablet = !isBot && detectTablet(raw);
182
+ const isMobile = !isBot && !isTablet && detectMobile(raw);
183
+ const isDesktop = !isBot && !isTablet && !isMobile;
184
+ let deviceType;
185
+ if (isBot)
186
+ deviceType = 'bot';
187
+ else if (isTablet)
188
+ deviceType = 'tablet';
189
+ else if (isMobile)
190
+ deviceType = 'mobile';
191
+ else if (raw)
192
+ deviceType = 'desktop';
193
+ else
194
+ deviceType = 'unknown';
195
+ const { browserName, browserVersion, engine } = detectBrowser(raw);
196
+ const { osName, osVersion } = detectOS(raw);
197
+ // Partial: non-browser runtime on a device we can identify (conflicting signals),
198
+ // or a browser UA with no detectable browser or OS (unrecognised device).
199
+ const partial = (isBot && /Android|iPhone|iPad|iPod/i.test(raw)) ||
200
+ (!isBot && !browserName && !osName && raw.length > 0);
201
+ return {
202
+ raw,
203
+ isMobile,
204
+ isTablet,
205
+ isDesktop,
206
+ isBot,
207
+ botName,
208
+ deviceType,
209
+ browserName,
210
+ browserVersion,
211
+ engine,
212
+ osName,
213
+ osVersion,
214
+ partial,
215
+ };
216
+ }
217
+ // ─── Middleware ───────────────────────────────────────────────────────────────
218
+ /**
219
+ * Parses the `User-Agent` request header and exposes structured data as
220
+ * `ctx.state.userAgent`. When the header is absent, `raw` is an empty string
221
+ * and all other fields are `undefined` / `false`.
222
+ *
223
+ * ```ts
224
+ * import { userAgent } from '@davaux/user-agent'
225
+ *
226
+ * // _global.ts or _middleware.ts
227
+ * export default userAgent()
228
+ *
229
+ * // A route handler:
230
+ * const { isMobile, isBot, browserName, osName } = ctx.state.userAgent
231
+ * ```
232
+ */
233
+ export function userAgent() {
234
+ return async (ctx, next) => {
235
+ const raw = ctx.req.headers['user-agent'] ?? '';
236
+ ctx.state.userAgent = parse(raw);
237
+ await next();
238
+ };
239
+ }
240
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AA4CA,iFAAiF;AAEjF,2EAA2E;AAC3E,MAAM,YAAY,GAA4B;IAC5C,CAAC,YAAY,EAA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QAAE,UAAU,GAAG,QAAQ,CAAA;SACnC,IAAI,QAAQ;QAAE,UAAU,GAAG,QAAQ,CAAA;SACnC,IAAI,GAAG;QAAE,UAAU,GAAG,SAAS,CAAA;;QAC/B,UAAU,GAAG,SAAS,CAAA;IAE3B,MAAM,EAAE,WAAW,EAAE,cAAc,EAAE,MAAM,EAAE,GAAG,aAAa,CAAC,GAAG,CAAC,CAAA;IAClE,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,GAAG,QAAQ,CAAC,GAAG,CAAC,CAAA;IAE3C,kFAAkF;IAClF,0EAA0E;IAC1E,MAAM,OAAO,GACX,CAAC,KAAK,IAAI,2BAA2B,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QAChD,CAAC,CAAC,KAAK,IAAI,CAAC,WAAW,IAAI,CAAC,MAAM,IAAI,GAAG,CAAC,MAAM,GAAG,CAAC,CAAC,CAAA;IAEvD,OAAO;QACL,GAAG;QACH,QAAQ;QACR,QAAQ;QACR,SAAS;QACT,KAAK;QACL,OAAO;QACP,UAAU;QACV,WAAW;QACX,cAAc;QACd,MAAM;QACN,MAAM;QACN,SAAS;QACT,OAAO;KACR,CAAA;AACH,CAAC;AAED,iFAAiF;AAEjF;;;;;;;;;;;;;;GAcG;AACH,MAAM,UAAU,SAAS;IACvB,OAAO,KAAK,EAAE,GAAG,EAAE,IAAI,EAAE,EAAE;QACzB,MAAM,GAAG,GAAI,GAAG,CAAC,GAAG,CAAC,OAAO,CAAC,YAAY,CAAwB,IAAI,EAAE,CAAA;QACvE,GAAG,CAAC,KAAK,CAAC,SAAS,GAAG,KAAK,CAAC,GAAG,CAAC,CAAA;QAChC,MAAM,IAAI,EAAE,CAAA;IACd,CAAC,CAAA;AACH,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@davaux/user-agent",
3
- "version": "0.8.0",
3
+ "version": "0.8.1",
4
4
  "description": "User-Agent parsing middleware for Davaux",
5
5
  "type": "module",
6
6
  "author": "David L Dyess II",
@@ -13,6 +13,9 @@
13
13
  "url": "https://codeberg.org/davaux/davaux/issues"
14
14
  },
15
15
  "homepage": "https://codeberg.org/davaux/davaux#readme",
16
+ "files": [
17
+ "dist"
18
+ ],
16
19
  "exports": {
17
20
  ".": {
18
21
  "import": "./dist/index.js",
@@ -24,11 +27,11 @@
24
27
  "typecheck": "tsc --noEmit"
25
28
  },
26
29
  "peerDependencies": {
27
- "davaux": ">=0.8.0"
30
+ "davaux": ">=0.8.1"
28
31
  },
29
32
  "devDependencies": {
30
33
  "@types/node": "^25.0.0",
31
34
  "davaux": "*",
32
35
  "typescript": "^6.0.3"
33
36
  }
34
- }
37
+ }
package/CLAUDE.md DELETED
@@ -1,95 +0,0 @@
1
- <!-- pka-generated -->
2
- # @davaux/user-agent
3
-
4
- > Generated by Project Knowledge Analyzer on 2026-06-06T21:53:10.521Z
5
-
6
- ## Overview
7
-
8
- User-Agent parsing middleware for Davaux
9
-
10
- **Version**: 0.8.0
11
- **Author**: David L Dyess II
12
- **License**: MIT
13
- **Repository**: https://codeberg.org/davaux/davaux#readme
14
-
15
- ## Tech Stack
16
-
17
- - **Language**: TypeScript
18
- - **Module System**: ESM (`type: module`)
19
-
20
- ## Commands
21
-
22
- - `npm run build` — tsc
23
- - `npm run typecheck` — tsc --noEmit
24
-
25
- ## Project Structure
26
-
27
- ```
28
- ├── CLAUDE.md
29
- ├── README.md
30
- ├── package.json
31
- ├── tsconfig.json
32
- └── src/
33
- └── index.ts
34
-
35
- ```
36
-
37
- ## Entry Points
38
-
39
- - `src/index.ts`
40
-
41
- ## Files by Type
42
-
43
- ### Documentation (2)
44
- - `CLAUDE.md`
45
- - `README.md`
46
-
47
- ### Config (2)
48
- - `package.json`
49
- - `tsconfig.json`
50
-
51
- ### Module (1)
52
- - `src/index.ts`
53
-
54
-
55
- ## Git
56
- - **Branch**: main
57
- - **Last Commit**: chore: Add alpha status note to README
58
- - **Author**: David Dyess II
59
- - **Date**: 2026-06-06 15:52:27 -0600
60
- - **Remote**: https://codeberg.org/davaux/davaux.git
61
-
62
- ### Recent Commits
63
- ```
64
- f527031 chore: Add alpha status note to README
65
- 90c819e chore: Add repo info to package.json files
66
- b200d9d feat(davaux)!: Add OmlCacheConfig - opt-in with includes option or opt-out with excludes option; Fix OML implementation to follow OML spec - use output instead of return
67
- 3bce0c2 chore: Update and add READMEs to packages; update ROADMAP
68
- fc27c20 fix(davaux): Remove old dist folder on new builds; fix server port per DavauxConfig
69
- c760659 feat(davaux): Add minify CSS in production builds
70
- c899ab8 fix(davaux): Added method JS and JSX extenstion to scanner
71
- 7d99f04 feat(davaux): Add support for declarative partial updates
72
- 3b47f37 chore: Bump package versions to 0.8.0
73
- aa0460f chore: Add CHANGELOG.md
74
- ```
75
-
76
- ## Dependencies
77
-
78
-
79
- ### Development
80
- @types/node, davaux, typescript
81
-
82
-
83
-
84
-
85
-
86
-
87
-
88
-
89
-
90
- ## Exported Symbols
91
-
92
- **`src/index.ts`**
93
- `userAgent`, `UserAgent`
94
-
95
-
package/src/index.ts DELETED
@@ -1,306 +0,0 @@
1
- import type { MiddlewareFn } from 'davaux'
2
-
3
- // ─── Types ────────────────────────────────────────────────────────────────────
4
-
5
- export interface UserAgent {
6
- /** Raw `User-Agent` header value. Empty string when the header is absent. */
7
- raw: string
8
- /** `true` when the UA contains mobile device indicators (excludes tablets). */
9
- isMobile: boolean
10
- /** `true` when the UA matches a tablet (iPad, Android without Mobile token). */
11
- isTablet: boolean
12
- /** `true` when the client is a desktop/laptop (not bot, not mobile, not tablet). */
13
- isDesktop: boolean
14
- /** `true` when the UA matches a known bot, crawler, or non-browser client. */
15
- isBot: boolean
16
- /** Bot name when `isBot` is true, e.g. `'Googlebot'`, `'curl'`. */
17
- botName?: string
18
- /** Normalized device category. */
19
- deviceType: 'mobile' | 'tablet' | 'desktop' | 'bot' | 'unknown'
20
- /** Browser family, e.g. `'Chrome'`, `'Firefox'`, `'Safari'`, `'Edge'`. */
21
- browserName?: string
22
- /** Browser version string, e.g. `'120.0.6099.130'`. */
23
- browserVersion?: string
24
- /** OS family, e.g. `'Windows'`, `'macOS'`, `'Android'`, `'iOS'`, `'Linux'`. */
25
- osName?: string
26
- /** OS version string, e.g. `'10'`, `'15.7'`, `'13'`. */
27
- osVersion?: string
28
- /** Rendering engine, e.g. `'Blink'`, `'Gecko'`, `'WebKit'`, `'Trident'`. */
29
- engine?: string
30
- /**
31
- * `true` when signals are conflicting or insufficient to fully trust the result.
32
- * Examples: a non-browser runtime running on a mobile/tablet OS (e.g. Dalvik on
33
- * Android), or a Mozilla-prefixed UA with no detectable browser or OS (e.g. a
34
- * gaming console with an unrecognised browser engine).
35
- */
36
- partial: boolean
37
- }
38
-
39
- declare module 'davaux' {
40
- interface State {
41
- userAgent: UserAgent
42
- }
43
- }
44
-
45
- // ─── Bot detection ────────────────────────────────────────────────────────────
46
-
47
- // Named patterns match in any UA format, including browser-disguised bots.
48
- const BOT_PATTERNS: Array<[RegExp, string]> = [
49
- [/Googlebot/i, 'Googlebot'],
50
- [/Bingbot/i, 'Bingbot'],
51
- [/DuckDuckBot/i, 'DuckDuckBot'],
52
- [/Yahoo! Slurp/i, 'Yahoo Slurp'],
53
- [/Baiduspider/i, 'Baiduspider'],
54
- [/YandexBot/i, 'YandexBot'],
55
- [/Sogou/i, 'Sogou'],
56
- [/facebookexternalhit/i, 'FacebookBot'],
57
- [/Twitterbot/i, 'Twitterbot'],
58
- [/Applebot/i, 'Applebot'],
59
- [/AdsBot-Google/i, 'AdsBot-Google'],
60
- [/SemrushBot/i, 'SemrushBot'],
61
- [/AhrefsBot/i, 'AhrefsBot'],
62
- [/MJ12bot/i, 'MJ12bot'],
63
- [/DotBot/i, 'DotBot'],
64
- [/Screaming Frog/i, 'Screaming Frog'],
65
- [/Scrapy/i, 'Scrapy'],
66
- [/curl\//i, 'curl'],
67
- [/wget\//i, 'wget'],
68
- [/python-requests/i, 'python-requests'],
69
- [/Python-urllib/i, 'Python-urllib'],
70
- [/Go-http-client/i, 'Go HTTP'],
71
- [/Java\/\d/i, 'Java'],
72
- [/libwww-perl/i, 'libwww-perl'],
73
- ]
74
-
75
- function detectBot(ua: string): { isBot: boolean; botName?: string } {
76
- // Named patterns first — work regardless of Mozilla prefix.
77
- for (const [pattern, name] of BOT_PATTERNS) {
78
- if (pattern.test(ua)) return { isBot: true, botName: name }
79
- }
80
-
81
- // Non-browser clients never start with Mozilla/.
82
- if (ua && !/^Mozilla\//i.test(ua)) {
83
- // Try Name/Version token (covers most HTTP libs, CMSes, app runtimes).
84
- // Allow commas so tokens like "AppleTV14,1" are captured whole.
85
- const m = /^([\w][\w.,+-]*)\//.exec(ua)
86
- if (m) return { isBot: true, botName: m[1] }
87
- // No version token — extract service name before a generic bot keyword.
88
- // e.g. "Traackr.com Bot" → "Traackr.com"
89
- const k = /^(.*?)\s+(?:bot|crawler|spider)\b/i.exec(ua)
90
- if (k) return { isBot: true, botName: k[1].trim() || 'Unknown' }
91
- return { isBot: true, botName: 'Unknown' }
92
- }
93
-
94
- // Mozilla-prefixed bots: URL reference gives a better name than generic keywords.
95
- // Real browsers never embed a URL in their UA string.
96
- if (/https?:\/\//i.test(ua)) {
97
- const m = /\(compatible;\s*([\w][\w. +-]*)\//.exec(ua)
98
- return { isBot: true, botName: m ? m[1].trim() : 'Unknown' }
99
- }
100
-
101
- // Last resort: generic keyword match for Mozilla-prefixed unknown bots.
102
- if (/\b(?:bot|crawler|spider)\b/i.test(ua)) return { isBot: true, botName: 'Unknown' }
103
-
104
- return { isBot: false }
105
- }
106
-
107
- // ─── Device detection ─────────────────────────────────────────────────────────
108
-
109
- function detectTablet(ua: string): boolean {
110
- if (/iPad/i.test(ua)) return true
111
- // Android tablets omit "Mobile" from their UA string.
112
- if (/Android/i.test(ua) && !/Mobile/i.test(ua)) return true
113
- return false
114
- }
115
-
116
- function detectMobile(ua: string): boolean {
117
- return /Mobi|Android|iPhone|iPod|Windows Phone|BlackBerry|IEMobile/i.test(ua)
118
- }
119
-
120
- // ─── Browser detection ────────────────────────────────────────────────────────
121
-
122
- function extractVersion(ua: string, token: string): string | undefined {
123
- const m = new RegExp(`${token}/([\\d._]+)`, 'i').exec(ua)
124
- return m ? m[1].replace(/_/g, '.') : undefined
125
- }
126
-
127
- interface BrowserResult {
128
- browserName?: string
129
- browserVersion?: string
130
- engine?: string
131
- }
132
-
133
- function detectBrowser(ua: string): BrowserResult {
134
- // Samsung Browser embeds Chrome in its UA — check first.
135
- if (/SamsungBrowser\//i.test(ua)) {
136
- return {
137
- browserName: 'Samsung Browser',
138
- browserVersion: extractVersion(ua, 'SamsungBrowser'),
139
- engine: 'Blink',
140
- }
141
- }
142
- // Chromium-based Edge ("Edg/") ships alongside Chrome token — check before Chrome.
143
- if (/Edg\/\d/i.test(ua)) {
144
- return { browserName: 'Edge', browserVersion: extractVersion(ua, 'Edg'), engine: 'Blink' }
145
- }
146
- // Legacy EdgeHTML-based Edge.
147
- if (/Edge\/\d/i.test(ua)) {
148
- return { browserName: 'Edge', browserVersion: extractVersion(ua, 'Edge'), engine: 'EdgeHTML' }
149
- }
150
- // Opera (Chromium-based, "OPR/").
151
- if (/OPR\/\d/i.test(ua)) {
152
- return { browserName: 'Opera', browserVersion: extractVersion(ua, 'OPR'), engine: 'Blink' }
153
- }
154
- // Chromium (open-source build, distinct from Google Chrome).
155
- if (/Chromium\/\d/i.test(ua)) {
156
- return {
157
- browserName: 'Chromium',
158
- browserVersion: extractVersion(ua, 'Chromium'),
159
- engine: 'Blink',
160
- }
161
- }
162
- // Google Chrome.
163
- if (/Chrome\/\d/i.test(ua)) {
164
- return { browserName: 'Chrome', browserVersion: extractVersion(ua, 'Chrome'), engine: 'Blink' }
165
- }
166
- // Firefox.
167
- if (/Firefox\/\d/i.test(ua)) {
168
- return {
169
- browserName: 'Firefox',
170
- browserVersion: extractVersion(ua, 'Firefox'),
171
- engine: 'Gecko',
172
- }
173
- }
174
- // Safari — must come after Chrome/Chromium since their UAs also contain "Safari".
175
- if (/Safari\/\d/i.test(ua)) {
176
- return {
177
- browserName: 'Safari',
178
- // "Version/x.x" carries the human-readable Safari version; fall back to Safari token.
179
- browserVersion: extractVersion(ua, 'Version') ?? extractVersion(ua, 'Safari'),
180
- engine: 'WebKit',
181
- }
182
- }
183
- // Internet Explorer 11 (Trident/7 with rv: version).
184
- if (/Trident\/\d/i.test(ua)) {
185
- const m = /rv:([\d.]+)/.exec(ua)
186
- return { browserName: 'Internet Explorer', browserVersion: m?.[1], engine: 'Trident' }
187
- }
188
- // Internet Explorer ≤ 10.
189
- const ieMatch = /MSIE\s([\d.]+)/i.exec(ua)
190
- if (ieMatch) {
191
- return { browserName: 'Internet Explorer', browserVersion: ieMatch[1], engine: 'Trident' }
192
- }
193
- return {}
194
- }
195
-
196
- // ─── OS detection ─────────────────────────────────────────────────────────────
197
-
198
- const WINDOWS_NT_VERSIONS: Record<string, string> = {
199
- '10.0': '10',
200
- '6.3': '8.1',
201
- '6.2': '8',
202
- '6.1': '7',
203
- '6.0': 'Vista',
204
- '5.2': 'XP',
205
- '5.1': 'XP',
206
- '5.0': '2000',
207
- }
208
-
209
- interface OSResult {
210
- osName?: string
211
- osVersion?: string
212
- }
213
-
214
- function detectOS(ua: string): OSResult {
215
- // Windows Phone must be checked before Windows NT.
216
- let m = /Windows Phone(?:\s+OS)?\s+([\d.]+)/i.exec(ua)
217
- if (m) return { osName: 'Windows Phone', osVersion: m[1] }
218
-
219
- // Android (appears on both mobile and tablet UAs).
220
- m = /Android\s+([\d.]+)/i.exec(ua)
221
- if (m) return { osName: 'Android', osVersion: m[1] }
222
-
223
- // iOS — covers iPhone, iPad (iPadOS ≥ 13 drops "iPhone" token), and iPod.
224
- m = /(?:iPhone|iPad|iPod)[^)]*OS\s+([\d_]+)/i.exec(ua)
225
- if (m) return { osName: 'iOS', osVersion: m[1].replace(/_/g, '.') }
226
-
227
- // macOS / OS X.
228
- m = /Mac OS X\s+([\d_.]+)/i.exec(ua)
229
- if (m) return { osName: 'macOS', osVersion: m[1].replace(/_/g, '.') }
230
-
231
- // Windows NT.
232
- m = /Windows NT\s+([\d.]+)/i.exec(ua)
233
- if (m) return { osName: 'Windows', osVersion: WINDOWS_NT_VERSIONS[m[1]] ?? m[1] }
234
-
235
- if (/CrOS/i.test(ua)) return { osName: 'Chrome OS' }
236
- if (/Linux/i.test(ua)) return { osName: 'Linux' }
237
-
238
- return {}
239
- }
240
-
241
- // ─── Parser ───────────────────────────────────────────────────────────────────
242
-
243
- function parse(raw: string): UserAgent {
244
- const { isBot, botName } = detectBot(raw)
245
- // Suppress device-type signals when isBot — they become noise, not signal.
246
- const isTablet = !isBot && detectTablet(raw)
247
- const isMobile = !isBot && !isTablet && detectMobile(raw)
248
- const isDesktop = !isBot && !isTablet && !isMobile
249
-
250
- let deviceType: UserAgent['deviceType']
251
- if (isBot) deviceType = 'bot'
252
- else if (isTablet) deviceType = 'tablet'
253
- else if (isMobile) deviceType = 'mobile'
254
- else if (raw) deviceType = 'desktop'
255
- else deviceType = 'unknown'
256
-
257
- const { browserName, browserVersion, engine } = detectBrowser(raw)
258
- const { osName, osVersion } = detectOS(raw)
259
-
260
- // Partial: non-browser runtime on a device we can identify (conflicting signals),
261
- // or a browser UA with no detectable browser or OS (unrecognised device).
262
- const partial =
263
- (isBot && /Android|iPhone|iPad|iPod/i.test(raw)) ||
264
- (!isBot && !browserName && !osName && raw.length > 0)
265
-
266
- return {
267
- raw,
268
- isMobile,
269
- isTablet,
270
- isDesktop,
271
- isBot,
272
- botName,
273
- deviceType,
274
- browserName,
275
- browserVersion,
276
- engine,
277
- osName,
278
- osVersion,
279
- partial,
280
- }
281
- }
282
-
283
- // ─── Middleware ───────────────────────────────────────────────────────────────
284
-
285
- /**
286
- * Parses the `User-Agent` request header and exposes structured data as
287
- * `ctx.state.userAgent`. When the header is absent, `raw` is an empty string
288
- * and all other fields are `undefined` / `false`.
289
- *
290
- * ```ts
291
- * import { userAgent } from '@davaux/user-agent'
292
- *
293
- * // _global.ts or _middleware.ts
294
- * export default userAgent()
295
- *
296
- * // A route handler:
297
- * const { isMobile, isBot, browserName, osName } = ctx.state.userAgent
298
- * ```
299
- */
300
- export function userAgent(): MiddlewareFn {
301
- return async (ctx, next) => {
302
- const raw = (ctx.req.headers['user-agent'] as string | undefined) ?? ''
303
- ctx.state.userAgent = parse(raw)
304
- await next()
305
- }
306
- }
package/tsconfig.json DELETED
@@ -1,17 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "target": "ESNext",
4
- "module": "NodeNext",
5
- "moduleResolution": "NodeNext",
6
- "strict": true,
7
- "declaration": true,
8
- "declarationMap": true,
9
- "sourceMap": true,
10
- "outDir": "./dist",
11
- "rootDir": "./src",
12
- "skipLibCheck": true,
13
- "lib": ["ESNext"],
14
- "types": ["node"]
15
- },
16
- "include": ["src/**/*"]
17
- }