@flighthq/useragent 0.5.1-next.973.788d3d7 → 0.6.0

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.
Files changed (42) hide show
  1. package/README.md +2 -2
  2. package/dist/contract.d.ts +4 -2
  3. package/dist/contract.d.ts.map +1 -1
  4. package/dist/contract.js +3 -2
  5. package/dist/contract.js.map +1 -1
  6. package/dist/deviceFormFactor.d.ts +35 -0
  7. package/dist/deviceFormFactor.d.ts.map +1 -0
  8. package/dist/deviceFormFactor.js +31 -0
  9. package/dist/deviceFormFactor.js.map +1 -0
  10. package/dist/index.d.ts +2 -1
  11. package/dist/index.d.ts.map +1 -1
  12. package/dist/index.js +1 -1
  13. package/dist/index.js.map +1 -1
  14. package/dist/jsPattern.d.ts +65 -0
  15. package/dist/jsPattern.d.ts.map +1 -0
  16. package/dist/jsPattern.js +247 -0
  17. package/dist/jsPattern.js.map +1 -0
  18. package/dist/rustTestSupport.d.ts +12 -0
  19. package/dist/rustTestSupport.d.ts.map +1 -0
  20. package/dist/rustTestSupport.js +24 -0
  21. package/dist/rustTestSupport.js.map +1 -0
  22. package/dist/userAgent.d.ts +53 -3
  23. package/dist/userAgent.d.ts.map +1 -1
  24. package/dist/userAgent.js +162 -146
  25. package/dist/userAgent.js.map +1 -1
  26. package/dist/userAgentNative.d.ts +12 -0
  27. package/dist/userAgentNative.d.ts.map +1 -0
  28. package/dist/userAgentNative.js +46 -0
  29. package/dist/userAgentNative.js.map +1 -0
  30. package/dist/userAgentParse.d.ts +17 -1
  31. package/dist/userAgentParse.d.ts.map +1 -1
  32. package/dist/userAgentParse.js +195 -93
  33. package/dist/userAgentParse.js.map +1 -1
  34. package/dist/userAgentTruthy.d.ts +3 -0
  35. package/dist/userAgentTruthy.d.ts.map +1 -0
  36. package/dist/userAgentTruthy.js +64 -0
  37. package/dist/userAgentTruthy.js.map +1 -0
  38. package/package.json +5 -5
  39. package/src/jsPattern.test.ts +136 -0
  40. package/src/userAgent.test.ts +217 -370
  41. package/src/userAgentNative.test.ts +55 -0
  42. package/src/userAgentParse.test.ts +111 -132
@@ -1,4 +1,7 @@
1
- import type { PlatformName } from '@flighthq/types/contract';
1
+ // Generated from crates/flight-useragent/tests/user_agent_test.rs by flight-compiler.
2
+ // Rust is authoritative; run `npm run generate` after changing it.
3
+
4
+ import { describe, expect, it } from 'vitest';
2
5
 
3
6
  import {
4
7
  detectEndianness,
@@ -7,430 +10,274 @@ import {
7
10
  parseUserAgentEngineVersion,
8
11
  parseUserAgentKind,
9
12
  parseUserAgentName,
13
+ parseUserAgentOsVersion,
10
14
  parseUserAgentPointerWidth,
11
15
  parseUserAgentRuntime,
12
16
  parseUserAgentVersion,
13
- } from './userAgent';
14
- import { parseUserAgentOsVersion } from './userAgentParse';
15
-
16
- describe('detectEndianness', () => {
17
- it('returns a known canonical value', () => {
18
- expect(['little', 'big', 'unknown']).toContain(detectEndianness());
19
- });
20
-
21
- it('returns little on Node.js (x64 host)', () => {
22
- // Node.js always runs on x64/arm64 hardware which is little-endian.
23
- expect(detectEndianness()).toBe('little');
24
- });
25
- });
26
-
27
- describe('legacy EdgeHTML', () => {
28
- // Edge only became Chromium at Edg/79. The blink branch's `edg` alternative matches `Edge/18` too, so
29
- // EdgeHTML was reported as blink — and a caller gating a Chromium workaround on that would apply it to
30
- // a browser that never ran Chromium.
31
- const LEGACY_EDGE =
32
- 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/18.17763';
33
-
34
- it('is not reported as blink', () => {
35
- expect(parseUserAgentEngine(LEGACY_EDGE)).toBe('unknown');
36
- });
17
+ } from './contract.ts';
37
18
 
38
- // The realistic UA carries a Chrome token, so before the fix this did not merely come back empty —
39
- // it came back with CHROME's version, which is a wrong answer rather than a missing one.
40
- it('does not report the embedded Chrome version as its engine version', () => {
41
- const engine = parseUserAgentEngine(LEGACY_EDGE);
42
- expect(parseUserAgentEngineVersion(LEGACY_EDGE, engine)).toBe('');
43
- });
44
-
45
- it('is still not blink without a Chrome token', () => {
46
- expect(parseUserAgentEngine('Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 Edge/18.17763')).toBe('unknown');
47
- });
19
+ function iosPrefix(): string {
20
+ return 'Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) ';
21
+ }
48
22
 
49
- // The discriminator is the trailing `e`, so the fix must not catch modern Edge or Edge on iOS.
50
- it('leaves modern Chromium Edge reported as blink', () => {
51
- const modern =
52
- 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0';
53
- expect(parseUserAgentEngine(modern)).toBe('blink');
54
- expect(parseUserAgentEngineVersion(modern, 'blink')).toBe('120.0.0.0');
55
- });
23
+ function legacyEdge(): string {
24
+ return 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/18.17763';
25
+ }
56
26
 
57
- it('leaves Edge on iOS reported as webkit', () => {
58
- expect(parseUserAgentEngine('Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) EdgiOS/120.0')).toBe('webkit');
27
+ describe('detectEndianness', () => {
28
+ it('returns a stable value', () => {
29
+ expect(detectEndianness()).toEqual(detectEndianness());
59
30
  });
60
31
  });
61
32
 
62
33
  describe('parseUserAgentArch', () => {
63
- it('detects arm64 from aarch64 token', () => {
64
- expect(parseUserAgentArch('Mozilla/5.0 (Linux; aarch64) AppleWebKit/537.36')).toBe('arm64');
34
+ it('detects each arch token in source order', () => {
35
+ expect(parseUserAgentArch('Mozilla/5.0 (Linux; aarch64) AppleWebKit/537.36', undefined)).toEqual('arm64');
36
+ expect(parseUserAgentArch('Mozilla/5.0 (iPhone; CPU arm64)', undefined)).toEqual('arm64');
37
+ expect(parseUserAgentArch('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36', undefined)).toEqual(
38
+ 'x64',
39
+ );
40
+ expect(parseUserAgentArch('Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36', undefined)).toEqual('x64');
41
+ expect(parseUserAgentArch('Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36', undefined)).toEqual('x64');
42
+ expect(parseUserAgentArch('Mozilla/5.0 (X11; Linux i686; rv:109.0)', undefined)).toEqual('x86');
43
+ expect(parseUserAgentArch('Mozilla/5.0 (Linux; armv7l)', undefined)).toEqual('arm');
44
+ expect(parseUserAgentArch('Mozilla/5.0 (Linux; riscv64)', undefined)).toEqual('riscv64');
45
+ expect(parseUserAgentArch('Mozilla/5.0 (Linux; mips64)', undefined)).toEqual('mips64');
46
+ expect(parseUserAgentArch('Mozilla/5.0 (Linux; mips)', undefined)).toEqual('mips');
47
+ });
48
+ it('prefers the ua ch platform hint over the ua string', () => {
49
+ expect(parseUserAgentArch('', 'arm')).toEqual('arm64');
50
+ expect(parseUserAgentArch('', 'Windows')).toEqual('x64');
51
+ expect(parseUserAgentArch('', 'macOS')).toEqual('x64');
52
+ expect(parseUserAgentArch('', 'Linux')).toEqual('x64');
53
+ expect(parseUserAgentArch('', 'Chrome OS')).toEqual('x64');
54
+ });
55
+ it('falls back to the ua string when the platform hint is inconclusive', () => {
56
+ expect(parseUserAgentArch('Mozilla/5.0 (X11; Linux x86_64)', 'SomeCustomPlatform')).toEqual('x64');
57
+ });
58
+ it('treats an empty hint as absent as ts truthiness does', () => {
59
+ expect(parseUserAgentArch('Mozilla/5.0 (Linux; mips)', '')).toEqual('mips');
65
60
  });
66
-
67
- it('detects arm64 from arm64 token', () => {
68
- expect(parseUserAgentArch('Mozilla/5.0 (iPhone; CPU arm64)')).toBe('arm64');
69
- });
70
-
71
- it('detects x64 from Win64 token', () => {
72
- expect(parseUserAgentArch('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36')).toBe('x64');
73
- });
74
-
75
- it('detects x64 from WOW64 token', () => {
76
- expect(parseUserAgentArch('Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36')).toBe('x64');
77
- });
78
-
79
- it('detects x64 from x86_64 token', () => {
80
- expect(parseUserAgentArch('Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36')).toBe('x64');
81
- });
82
-
83
- it('detects x86 from i686 token', () => {
84
- expect(parseUserAgentArch('Mozilla/5.0 (X11; Linux i686; rv:109.0)')).toBe('x86');
85
- });
86
-
87
- it('detects arm before arm64 does not fire on plain arm token', () => {
88
- // A UA with only 'arm' (not 'arm64'/'aarch64') should yield 'arm'.
89
- expect(parseUserAgentArch('Mozilla/5.0 (Linux; armv7l)')).toBe('arm');
90
- });
91
-
92
- it('detects riscv64, mips64, and mips tokens', () => {
93
- expect(parseUserAgentArch('Mozilla/5.0 (Linux; riscv64)')).toBe('riscv64');
94
- expect(parseUserAgentArch('Mozilla/5.0 (Linux; mips64)')).toBe('mips64');
95
- expect(parseUserAgentArch('Mozilla/5.0 (Linux; mips)')).toBe('mips');
96
- });
97
-
98
- it('prefers the UA-CH platform hint over the UA string', () => {
99
- expect(parseUserAgentArch('', 'arm')).toBe('arm64');
100
- expect(parseUserAgentArch('', 'Windows')).toBe('x64');
101
- expect(parseUserAgentArch('', 'macOS')).toBe('x64');
102
- expect(parseUserAgentArch('', 'Linux')).toBe('x64');
103
- expect(parseUserAgentArch('', 'Chrome OS')).toBe('x64');
104
- });
105
-
106
- it('falls back to the UA string when the platform hint is inconclusive', () => {
107
- expect(parseUserAgentArch('Mozilla/5.0 (X11; Linux x86_64)', 'SomeCustomPlatform')).toBe('x64');
108
- });
109
-
110
61
  it('returns empty string when arch is undetectable', () => {
111
- expect(parseUserAgentArch('Mozilla/5.0 (X11; Linux) AppleWebKit/537.36')).toBe('');
112
- expect(parseUserAgentArch('SomeCustomBrowser/1.0')).toBe('');
62
+ expect(parseUserAgentArch('Mozilla/5.0 (X11; Linux) AppleWebKit/537.36', undefined)).toEqual('');
63
+ expect(parseUserAgentArch('SomeCustomBrowser/1.0', undefined)).toEqual('');
113
64
  });
114
65
  });
115
66
 
116
67
  describe('parseUserAgentEngine', () => {
117
- it('returns gecko for Firefox', () => {
118
- expect(
119
- parseUserAgentEngine('Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:120.0) Gecko/20100101 Firefox/120.0'),
120
- ).toBe('gecko');
121
- });
122
-
123
- it('returns blink for Chrome', () => {
124
- expect(
125
- parseUserAgentEngine(
126
- 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
127
- ),
128
- ).toBe('blink');
129
- });
130
-
131
- it('returns blink for Edge (Edg/)', () => {
68
+ it('returns each engine', () => {
69
+ const gecko = parseUserAgentEngine(
70
+ 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:120.0) Gecko/20100101 Firefox/120.0',
71
+ );
72
+ const blink = parseUserAgentEngine(
73
+ 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
74
+ );
132
75
  expect(
133
76
  parseUserAgentEngine(
134
77
  'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.2210.133',
135
78
  ),
136
- ).toBe('blink');
137
- });
138
-
139
- it('returns webkit for Safari', () => {
140
- expect(
141
- parseUserAgentEngine(
142
- 'Mozilla/5.0 (Macintosh; Intel Mac OS X 13_0) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.0 Safari/605.1.15',
143
- ),
144
- ).toBe('webkit');
145
- });
146
-
147
- it('returns unknown for unrecognized UA', () => {
148
- expect(parseUserAgentEngine('')).toBe('unknown');
149
- expect(parseUserAgentEngine('CustomBot/1.0')).toBe('unknown');
150
- });
151
- });
152
-
153
- describe('parseUserAgentEngine for Opera on iOS', () => {
154
- const IOS_PREFIX = 'Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) AppleWebKit/605.1.15 ';
155
-
156
- it.each([
157
- ['OPiOS', `${IOS_PREFIX}(KHTML, like Gecko) OPiOS/9.2.0.11256 Mobile/15E148 Safari/9537.53`],
158
- ['OPT', `${IOS_PREFIX}(KHTML, like Gecko) Version/17.0 OPT/3.6.1 Mobile/15E148 Safari/604.1`],
159
- ])('keeps the %s product token on WebKit', (_token, ua) => {
160
- const engine = parseUserAgentEngine(ua);
161
- expect(engine).toBe('webkit');
162
- expect(parseUserAgentEngineVersion(ua, engine)).not.toBe('');
163
- });
164
- });
165
-
166
- describe('parseUserAgentEngine on iOS', () => {
167
- // Every browser on iOS/iPadOS runs on the system WebKit, whatever product token it advertises.
168
- // Deciding the engine from the product token got EdgiOS wrong — it contains `edg`, so it was
169
- // reported as blink, and then no `Edg/` token followed so the version came back empty.
170
- const IOS_PREFIX = 'Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) ';
171
-
172
- it('reports webkit for Edge on iOS, which contains the blink-family token "edg"', () => {
173
- const ua = `${IOS_PREFIX}EdgiOS/120.0.0.0 Mobile/15E148 Safari/605.1.15`;
174
- expect(parseUserAgentEngine(ua)).toBe('webkit');
175
- expect(parseUserAgentEngineVersion(ua, 'webkit')).not.toBe('');
176
- });
177
-
178
- it('reports webkit for Chrome and Firefox on iOS', () => {
179
- expect(parseUserAgentEngine(`${IOS_PREFIX}CriOS/120.0.6099.119 Mobile/15E148 Safari/604.1`)).toBe('webkit');
180
- expect(parseUserAgentEngine(`${IOS_PREFIX}FxiOS/121.0 Mobile/15E148 Safari/605.1.15`)).toBe('webkit');
79
+ ).toEqual(blink);
80
+ const webkit = parseUserAgentEngine(
81
+ 'Mozilla/5.0 (Macintosh; Intel Mac OS X 13_0) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.0 Safari/605.1.15',
82
+ );
83
+ expect(gecko).not.toEqual(blink);
84
+ expect(gecko).not.toEqual(webkit);
85
+ expect(blink).not.toEqual(webkit);
86
+ });
87
+ it('returns unknown for unrecognized ua', () => {
88
+ const unknown = parseUserAgentEngine('');
89
+ expect(parseUserAgentEngine('CustomBot/1.0')).toEqual(unknown);
90
+ expect(parseUserAgentEngine('Chrome/120.0')).not.toEqual(unknown);
91
+ });
92
+ it('legacy edgehtml is not reported as blink', () => {
93
+ const unknown = parseUserAgentEngine('');
94
+ expect(parseUserAgentEngine(legacyEdge())).toEqual(unknown);
95
+ expect(parseUserAgentEngine('Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 Edge/18.17763')).toEqual(unknown);
96
+ const modern =
97
+ 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0';
98
+ const blink = parseUserAgentEngine('Chrome/120.0');
99
+ expect(parseUserAgentEngine(modern)).toEqual(blink);
100
+ const iosEdge = parseUserAgentEngine('Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) EdgiOS/120.0');
101
+ expect(iosEdge).not.toEqual(blink);
102
+ expect(iosEdge).not.toEqual(unknown);
103
+ });
104
+ it('keeps opera on ios product tokens on webkit', () => {
105
+ const webkit = parseUserAgentEngine('Mozilla/5.0 (Macintosh) AppleWebKit/605.1.15 Version/17.0 Safari/605.1.15');
106
+ for (const ua of [
107
+ `${iosPrefix()}OPiOS/9.2.0.11256 Mobile/15E148 Safari/9537.53`,
108
+ `${iosPrefix()}Version/17.0 OPT/3.6.1 Mobile/15E148 Safari/604.1`,
109
+ ]) {
110
+ const engine = parseUserAgentEngine(ua);
111
+ expect(engine).toEqual(webkit);
112
+ expect(parseUserAgentEngineVersion(ua, engine)).not.toEqual('');
113
+ }
181
114
  });
182
-
183
- it('reports webkit for an iPad UA', () => {
184
- const ua =
115
+ it('reports webkit for every browser on ios', () => {
116
+ const webkit = parseUserAgentEngine('Mozilla/5.0 (Macintosh) AppleWebKit/605.1.15 Version/17.0 Safari/605.1.15');
117
+ const edge = `${iosPrefix()}EdgiOS/120.0.0.0 Mobile/15E148 Safari/605.1.15`;
118
+ expect(parseUserAgentEngine(edge)).toEqual(webkit);
119
+ expect(parseUserAgentEngineVersion(edge, webkit)).not.toEqual('');
120
+ expect(parseUserAgentEngine(`${iosPrefix()}CriOS/120.0.6099.119 Mobile/15E148 Safari/604.1`)).toEqual(webkit);
121
+ expect(parseUserAgentEngine(`${iosPrefix()}FxiOS/121.0 Mobile/15E148 Safari/605.1.15`)).toEqual(webkit);
122
+ const ipad =
185
123
  'Mozilla/5.0 (iPad; CPU OS 17_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/120.0.0.0 Mobile/15E148 Safari/604.1';
186
- expect(parseUserAgentEngine(ua)).toBe('webkit');
124
+ expect(parseUserAgentEngine(ipad)).toEqual(webkit);
187
125
  });
188
-
189
- it('still reports blink for desktop Chrome and Edge, which are not on iOS', () => {
126
+ it('still reports blink and gecko off ios', () => {
190
127
  const mac =
191
128
  'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36';
192
129
  const win =
193
130
  'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0';
194
- expect(parseUserAgentEngine(mac)).toBe('blink');
195
- expect(parseUserAgentEngine(win)).toBe('blink');
196
- expect(parseUserAgentEngineVersion(win, 'blink')).toBe('120.0.0.0');
197
- });
198
-
199
- it('still reports gecko for desktop Firefox', () => {
200
- const ua = 'Mozilla/5.0 (X11; Linux x86_64; rv:120.0) Gecko/20100101 Firefox/120.0';
201
- expect(parseUserAgentEngine(ua)).toBe('gecko');
131
+ const blink = parseUserAgentEngine('Chrome/120.0');
132
+ const gecko = parseUserAgentEngine('Firefox/120.0');
133
+ expect(parseUserAgentEngine(mac)).toEqual(blink);
134
+ expect(parseUserAgentEngine(win)).toEqual(blink);
135
+ expect(parseUserAgentEngine('Mozilla/5.0 (X11; Linux x86_64; rv:120.0) Gecko/20100101 Firefox/120.0')).toEqual(
136
+ gecko,
137
+ );
202
138
  });
203
139
  });
204
140
 
205
141
  describe('parseUserAgentEngineVersion', () => {
206
- it('extracts Firefox version', () => {
207
- expect(
208
- parseUserAgentEngineVersion(
209
- 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:120.0) Gecko/20100101 Firefox/120.0',
210
- 'gecko',
211
- ),
212
- ).toBe('120.0');
213
- });
214
-
215
- it('extracts Chrome version', () => {
216
- expect(
217
- parseUserAgentEngineVersion(
218
- 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.6099.109 Safari/537.36',
219
- 'blink',
220
- ),
221
- ).toBe('120.0.6099.109');
222
- });
223
-
224
- it('extracts Edge version (Edg/ token takes priority over Chrome/)', () => {
225
- expect(
226
- parseUserAgentEngineVersion(
227
- 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.2210.133',
228
- 'blink',
229
- ),
230
- ).toBe('120.0.2210.133');
231
- });
232
-
233
- it('extracts Safari Version/ token', () => {
234
- expect(
235
- parseUserAgentEngineVersion(
236
- 'Mozilla/5.0 (Macintosh; Intel Mac OS X 13_0) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.0 Safari/605.1.15',
237
- 'webkit',
238
- ),
239
- ).toBe('16.0');
240
- });
241
-
242
- it('returns empty string for unknown engine', () => {
243
- expect(parseUserAgentEngineVersion('any UA string', 'unknown')).toBe('');
244
- });
245
-
246
- it('returns empty string when version is absent', () => {
247
- expect(parseUserAgentEngineVersion('', 'gecko')).toBe('');
142
+ it('extracts each engine version', () => {
143
+ const geckoUa = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:120.0) Gecko/20100101 Firefox/120.0';
144
+ expect(parseUserAgentEngineVersion(geckoUa, parseUserAgentEngine(geckoUa))).toEqual('120.0');
145
+ const chromeUa =
146
+ 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.6099.109 Safari/537.36';
147
+ expect(parseUserAgentEngineVersion(chromeUa, parseUserAgentEngine(chromeUa))).toEqual('120.0.6099.109');
148
+ const edgeUa =
149
+ 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.2210.133';
150
+ expect(parseUserAgentEngineVersion(edgeUa, parseUserAgentEngine(edgeUa))).toEqual('120.0.2210.133');
151
+ const safariUa =
152
+ 'Mozilla/5.0 (Macintosh; Intel Mac OS X 13_0) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.0 Safari/605.1.15';
153
+ expect(parseUserAgentEngineVersion(safariUa, parseUserAgentEngine(safariUa))).toEqual('16.0');
154
+ });
155
+ it('returns empty for unknown engine or absent version', () => {
156
+ const unknown = parseUserAgentEngine('');
157
+ expect(parseUserAgentEngineVersion('any UA string', unknown)).toEqual('');
158
+ const gecko = parseUserAgentEngine('Firefox/120.0');
159
+ expect(parseUserAgentEngineVersion('', gecko)).toEqual('');
160
+ });
161
+ it('does not report the embedded chrome version for legacy edge', () => {
162
+ const engine = parseUserAgentEngine(legacyEdge());
163
+ expect(parseUserAgentEngineVersion(legacyEdge(), engine)).toEqual('');
164
+ });
165
+ it('skips an earlier token without digits as the regex does', () => {
166
+ const blink = parseUserAgentEngine('Chrome/120.0');
167
+ expect(parseUserAgentEngineVersion('Chrome/x Chrome/99.1', blink)).toEqual('99.1');
168
+ const webkit = parseUserAgentEngine('AppleWebKit/605.1');
169
+ expect(parseUserAgentEngineVersion('AppleWebKit/605.1', webkit)).toEqual('605.1');
248
170
  });
249
171
  });
250
172
 
251
173
  describe('parseUserAgentKind', () => {
252
- it('returns mobile for ios', () => {
253
- expect(parseUserAgentKind('ios')).toBe('mobile');
254
- });
255
-
256
- it('returns mobile for android', () => {
257
- expect(parseUserAgentKind('android')).toBe('mobile');
258
- });
259
-
260
- it('returns web for desktop names', () => {
261
- expect(parseUserAgentKind('windows')).toBe('web');
262
- expect(parseUserAgentKind('macos')).toBe('web');
263
- expect(parseUserAgentKind('linux')).toBe('web');
264
- });
265
-
266
- it('returns web for unknown', () => {
267
- expect(parseUserAgentKind('unknown')).toBe('web');
174
+ it('returns mobile for ios and android and web otherwise', () => {
175
+ const ios = parseUserAgentName('Mozilla/5.0 (iPhone)');
176
+ const android = parseUserAgentName('Mozilla/5.0 (Linux; Android 14)');
177
+ const mobile = parseUserAgentKind(ios);
178
+ expect(parseUserAgentKind(android)).toEqual(mobile);
179
+ for (const ua of [
180
+ 'Mozilla/5.0 (Windows NT 10.0)',
181
+ 'Mozilla/5.0 (Macintosh; Intel Mac OS X 13_0)',
182
+ 'Mozilla/5.0 (X11; Linux x86_64)',
183
+ 'CustomBot/1.0',
184
+ ]) {
185
+ expect(parseUserAgentKind(parseUserAgentName(ua))).not.toEqual(mobile);
186
+ }
268
187
  });
269
188
  });
270
189
 
271
190
  describe('parseUserAgentName', () => {
272
- it('detects android', () => {
273
- expect(parseUserAgentName('Mozilla/5.0 (Linux; Android 14; Pixel 8)')).toBe('android');
274
- });
275
-
276
- it('detects ios from iPhone', () => {
277
- expect(parseUserAgentName('Mozilla/5.0 (iPhone; CPU iPhone OS 17_4_1 like Mac OS X)')).toBe('ios');
278
- });
279
-
280
- it('detects ios from iPad', () => {
281
- expect(parseUserAgentName('Mozilla/5.0 (iPad; CPU OS 17_0 like Mac OS X)')).toBe('ios');
282
- });
283
-
284
- it('detects windows', () => {
285
- expect(parseUserAgentName('Mozilla/5.0 (Windows NT 10.0; Win64; x64)')).toBe('windows');
286
- });
287
-
288
- it('detects macos', () => {
289
- expect(parseUserAgentName('Mozilla/5.0 (Macintosh; Intel Mac OS X 13_0)')).toBe('macos');
290
- });
291
-
292
- it('detects linux', () => {
293
- expect(parseUserAgentName('Mozilla/5.0 (X11; Linux x86_64)')).toBe('linux');
294
- });
295
-
296
- it('returns web when no OS is detected', () => {
297
- expect(parseUserAgentName('')).toBe('web');
298
- expect(parseUserAgentName('CustomBot/1.0')).toBe('web');
191
+ it('detects each name', () => {
192
+ const android = parseUserAgentName('Mozilla/5.0 (Linux; Android 14; Pixel 8)');
193
+ const ios = parseUserAgentName('Mozilla/5.0 (iPhone; CPU iPhone OS 17_4_1 like Mac OS X)');
194
+ expect(parseUserAgentName('Mozilla/5.0 (iPad; CPU OS 17_0 like Mac OS X)')).toEqual(ios);
195
+ const windows = parseUserAgentName('Mozilla/5.0 (Windows NT 10.0; Win64; x64)');
196
+ const mac = parseUserAgentName('Mozilla/5.0 (Macintosh; Intel Mac OS X 13_0)');
197
+ const linux = parseUserAgentName('Mozilla/5.0 (X11; Linux x86_64)');
198
+ const web = parseUserAgentName('');
199
+ expect(android).not.toEqual(ios);
200
+ expect(android).not.toEqual(linux);
201
+ expect(ios).not.toEqual(mac);
202
+ expect(windows).not.toEqual(mac);
203
+ expect(windows).not.toEqual(linux);
204
+ expect(mac).not.toEqual(linux);
205
+ expect(web).not.toEqual(android);
206
+ expect(web).not.toEqual(ios);
207
+ expect(web).not.toEqual(windows);
208
+ expect(web).not.toEqual(mac);
209
+ expect(web).not.toEqual(linux);
210
+ });
211
+ it('returns web when no os is detected', () => {
212
+ const web = parseUserAgentName('');
213
+ expect(parseUserAgentName('CustomBot/1.0')).toEqual(web);
299
214
  });
300
215
  });
301
216
 
302
217
  describe('parseUserAgentPointerWidth', () => {
303
- it('returns 64 for x64', () => {
304
- expect(parseUserAgentPointerWidth('x64')).toBe(64);
305
- });
306
-
307
- it('returns 64 for arm64', () => {
308
- expect(parseUserAgentPointerWidth('arm64')).toBe(64);
309
- });
310
-
311
- it('returns 32 for x86', () => {
312
- expect(parseUserAgentPointerWidth('x86')).toBe(32);
313
- });
314
-
315
- it('returns 32 for arm', () => {
316
- expect(parseUserAgentPointerWidth('arm')).toBe(32);
317
- });
318
-
319
- it('returns -1 for wasm', () => {
320
- expect(parseUserAgentPointerWidth('wasm')).toBe(-1);
321
- });
322
-
323
- it('returns -1 for empty string', () => {
324
- expect(parseUserAgentPointerWidth('')).toBe(-1);
218
+ it('maps arch tokens to widths', () => {
219
+ expect(parseUserAgentPointerWidth('x64')).toEqual(64);
220
+ expect(parseUserAgentPointerWidth('arm64')).toEqual(64);
221
+ expect(parseUserAgentPointerWidth('x86')).toEqual(32);
222
+ expect(parseUserAgentPointerWidth('arm')).toEqual(32);
223
+ expect(parseUserAgentPointerWidth('wasm')).toEqual(-1);
224
+ expect(parseUserAgentPointerWidth('')).toEqual(-1);
325
225
  });
326
226
  });
327
227
 
328
228
  describe('parseUserAgentRuntime', () => {
329
- it('returns unknown when window is null', () => {
330
- expect(parseUserAgentRuntime(null)).toBe('unknown');
331
- });
332
-
333
- it('returns unknown when window is undefined', () => {
334
- expect(parseUserAgentRuntime(undefined)).toBe('unknown');
335
- });
336
-
337
- it('returns web when no host shell globals are present', () => {
338
- expect(parseUserAgentRuntime({})).toBe('web');
339
- });
340
-
341
- it('returns electron when process.versions.electron is present', () => {
342
- expect(parseUserAgentRuntime({ process: { versions: { electron: '28.0.0' } } })).toBe('electron');
343
- });
344
-
345
- it('returns tauri when __TAURI__ is present', () => {
346
- expect(parseUserAgentRuntime({ __TAURI__: {} })).toBe('tauri');
347
- });
348
-
349
- it('returns capacitor when Capacitor is present', () => {
350
- expect(parseUserAgentRuntime({ Capacitor: {} })).toBe('capacitor');
351
- });
352
-
353
- it('prioritizes electron over tauri when both globals are present', () => {
354
- expect(parseUserAgentRuntime({ __TAURI__: {}, process: { versions: { electron: '28.0.0' } } })).toBe('electron');
229
+ it('returns a stable value when window is absent', () => {
230
+ expect(parseUserAgentRuntime(undefined)).toEqual(parseUserAgentRuntime(undefined));
355
231
  });
356
232
  });
357
233
 
358
234
  describe('parseUserAgentVersion', () => {
359
- it('parses Windows NT version', () => {
360
- expect(parseUserAgentVersion('Mozilla/5.0 (Windows NT 10.0; Win64; x64)', 'windows')).toBe('10.0');
361
- });
362
-
363
- it('parses macOS version with underscore separators', () => {
364
- expect(parseUserAgentVersion('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)', 'macos')).toBe('10.15.7');
365
- });
366
-
367
- it('parses macOS version with dot separators', () => {
368
- expect(parseUserAgentVersion('Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15.7)', 'macos')).toBe('10.15.7');
369
- });
370
-
371
- it('parses iOS version', () => {
372
- expect(parseUserAgentVersion('Mozilla/5.0 (iPhone; CPU iPhone OS 17_4_1 like Mac OS X)', 'ios')).toBe('17.4.1');
373
- });
374
-
375
- it('parses iOS version without iPhone token', () => {
376
- expect(parseUserAgentVersion('Mozilla/5.0 (iPad; CPU OS 17_0 like Mac OS X)', 'ios')).toBe('17.0');
377
- });
378
-
379
- it('parses Android version', () => {
380
- expect(parseUserAgentVersion('Mozilla/5.0 (Linux; Android 14; Pixel 8)', 'android')).toBe('14');
381
- });
382
-
383
- it('returns empty string for linux (no kernel in UA)', () => {
384
- expect(parseUserAgentVersion('Mozilla/5.0 (X11; Linux x86_64)', 'linux')).toBe('');
385
- });
386
-
387
- it('returns empty string for unknown name', () => {
388
- expect(parseUserAgentVersion('any UA', 'unknown')).toBe('');
389
- });
390
-
391
- it('returns empty string when version is absent', () => {
392
- expect(parseUserAgentVersion('', 'windows')).toBe('');
393
- });
394
- });
395
-
396
- describe('parseUserAgentVersion shares one extractor with parseUserAgentOsVersion', () => {
397
- // The two families each carried their own copy of the same four patterns, and the copies had
398
- // drifted: this one required exactly one space where the other accepts any whitespace, so it
399
- // returned '' for UAs the other parsed correctly. These pin the agreement, not just the fix.
400
- const CASES: readonly (readonly [string, PlatformName, string])[] = [
401
- ['Mozilla/5.0 (Linux; Android 14; SM-X710) AppleWebKit/537.36', 'android', '14'],
402
- ['Mozilla/5.0 (Windows NT 10.0; Win64; x64)', 'windows', '10.0'],
403
- ['Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)', 'macos', '10.15.7'],
404
- ];
405
-
406
- it('parses the whitespace variants that used to return empty', () => {
407
- for (const [ua, name, expected] of CASES) {
408
- expect(parseUserAgentVersion(ua, name)).toBe(expected);
235
+ it('parses each platform version', () => {
236
+ const windows = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)';
237
+ expect(parseUserAgentVersion(windows, parseUserAgentName(windows))).toEqual('10.0');
238
+ const macUnderscores = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)';
239
+ expect(parseUserAgentVersion(macUnderscores, parseUserAgentName(macUnderscores))).toEqual('10.15.7');
240
+ const macDots = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15.7)';
241
+ expect(parseUserAgentVersion(macDots, parseUserAgentName(macDots))).toEqual('10.15.7');
242
+ const iphone = 'Mozilla/5.0 (iPhone; CPU iPhone OS 17_4_1 like Mac OS X)';
243
+ expect(parseUserAgentVersion(iphone, parseUserAgentName(iphone))).toEqual('17.4.1');
244
+ const ipad = 'Mozilla/5.0 (iPad; CPU OS 17_0 like Mac OS X)';
245
+ expect(parseUserAgentVersion(ipad, parseUserAgentName(ipad))).toEqual('17.0');
246
+ const android = 'Mozilla/5.0 (Linux; Android 14; Pixel 8)';
247
+ expect(parseUserAgentVersion(android, parseUserAgentName(android))).toEqual('14');
248
+ });
249
+ it('returns empty for linux unknown and absent versions', () => {
250
+ const linux = 'Mozilla/5.0 (X11; Linux x86_64)';
251
+ expect(parseUserAgentVersion(linux, parseUserAgentName(linux))).toEqual('');
252
+ const windows = parseUserAgentName('Mozilla/5.0 (Windows NT 10.0)');
253
+ expect(parseUserAgentVersion('', windows)).toEqual('');
254
+ });
255
+ it('shares one extractor with parse user agent os version', () => {
256
+ const cases = [
257
+ ['Mozilla/5.0 (Linux; Android 14; SM-X710) AppleWebKit/537.36', '14'],
258
+ ['Mozilla/5.0 (Windows NT 10.0; Win64; x64)', '10.0'],
259
+ ['Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)', '10.15.7'],
260
+ ];
261
+ for (const [ua, expected] of cases) {
262
+ const name = parseUserAgentName(ua);
263
+ expect(parseUserAgentVersion(ua, name)).toEqual(expected);
264
+ expect(parseUserAgentVersion(ua, name)).toEqual(parseUserAgentOsVersion(ua));
409
265
  }
410
266
  });
411
-
412
- it('agrees with parseUserAgentOsVersion on every case', () => {
413
- for (const [ua, name] of CASES) {
414
- expect(parseUserAgentVersion(ua, name)).toBe(parseUserAgentOsVersion(ua));
415
- }
416
- });
417
-
418
- it('returns empty when the requested platform is not the one the UA describes', () => {
267
+ it('returns empty when the requested platform is not the one the ua describes', () => {
419
268
  const win = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36';
420
- expect(parseUserAgentVersion(win, 'ios')).toBe('');
421
- expect(parseUserAgentVersion(win, 'macos')).toBe('');
422
- expect(parseUserAgentVersion(win, 'windows')).toBe('10.0');
423
- });
424
-
425
- it('does not read a macOS version off an iPad UA that says "like Mac OS X"', () => {
269
+ const iosName = parseUserAgentName('Mozilla/5.0 (iPhone)');
270
+ const macName = parseUserAgentName('Mozilla/5.0 (Macintosh)');
271
+ const windowsName = parseUserAgentName(win);
272
+ expect(parseUserAgentVersion(win, iosName)).toEqual('');
273
+ expect(parseUserAgentVersion(win, macName)).toEqual('');
274
+ expect(parseUserAgentVersion(win, windowsName)).toEqual('10.0');
426
275
  const ipad = 'Mozilla/5.0 (iPad; CPU OS 17_4 like Mac OS X) AppleWebKit/605.1.15';
427
- expect(parseUserAgentVersion(ipad, 'macos')).toBe('');
428
- expect(parseUserAgentVersion(ipad, 'ios')).toBe('17.4');
429
- });
430
-
431
- it('still returns empty for linux and the web fallback', () => {
276
+ expect(parseUserAgentVersion(ipad, macName)).toEqual('');
277
+ expect(parseUserAgentVersion(ipad, iosName)).toEqual('17.4');
432
278
  const linux = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36';
433
- expect(parseUserAgentVersion(linux, 'linux')).toBe('');
434
- expect(parseUserAgentVersion('some-native-host/1.0', 'web')).toBe('');
279
+ expect(parseUserAgentVersion(linux, parseUserAgentName(linux))).toEqual('');
280
+ const webName = parseUserAgentName('');
281
+ expect(parseUserAgentVersion('some-native-host/1.0', webName)).toEqual('');
435
282
  });
436
283
  });