/
logic.js
552 lines (464 loc) · 16.1 KB
/
logic.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
var test = require('tape');
var { parseUserAgent } = require('../');
function assertAgentString(t, agentString, expectedResult) {
t.deepEqual(parseUserAgent(agentString), expectedResult);
}
test('detects Chrome', function(t) {
assertAgentString(
t,
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36',
{ type: 'browser', name: 'chrome', version: '50.0.2661', os: 'Linux' },
);
assertAgentString(
t,
'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36',
{ type: 'browser', name: 'chrome', version: '41.0.2228', os: 'Windows 7' },
);
assertAgentString(
t,
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36',
{ type: 'browser', name: 'chrome', version: '72.0.3626', os: 'Windows 10' },
);
t.end();
});
test('detects Chrome for iOS', function(t) {
assertAgentString(
t,
'Mozilla/5.0 (iPhone; U; CPU iPhone OS 5_1_1 like Mac OS X; en) AppleWebKit/534.46.0 (KHTML, like Gecko) CriOS/19.0.1084.60 Mobile/9B206 Safari/7534.48.3',
{ type: 'browser', name: 'crios', version: '19.0.1084', os: 'iOS' },
);
t.end();
});
test('detects Firefox', function(t) {
assertAgentString(
t,
'Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:46.0) Gecko/20100101 Firefox/46.0',
{ type: 'browser', name: 'firefox', version: '46.0.0', os: 'Linux' },
);
assertAgentString(
t,
'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1',
{ type: 'browser', name: 'firefox', version: '40.1.0', os: 'Windows 7' },
);
t.end();
});
test('detects Firefox for iOS', function(t) {
assertAgentString(
t,
'Mozilla/5.0 (iPad; CPU iPhone OS 8_3 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) FxiOS/1.0 Mobile/12F69 Safari/600.1.4',
{ type: 'browser', name: 'fxios', version: '1.0.0', os: 'iOS' },
);
assertAgentString(
t,
'Mozilla/5.0 (iPad; CPU iPhone OS 8_3 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) FxiOS/3.2 Mobile/12F69 Safari/600.1.4',
{ type: 'browser', name: 'fxios', version: '3.2.0', os: 'iOS' },
);
t.end();
});
test('detects Edge', function(t) {
assertAgentString(
t,
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.246',
{ type: 'browser', name: 'edge', version: '12.246.0', os: 'Windows 10' },
);
assertAgentString(
t,
'Mozilla/5.0 (Windows NT 6.3; Win64, x64; Touch) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36 Edge/12.0 (Touch; Trident/7.0; .NET4.0E; .NET4.0C; .NET CLR 3.5.30729; .NET CLR 2.0.50727; .NET CLR 3.0.30729; HPNTDFJS; H9P; InfoPath',
{ type: 'browser', name: 'edge', version: '12.0.0', os: 'Windows 8.1' },
);
t.end();
});
test('detects IE', function(t) {
assertAgentString(
t,
'Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; .NET4.0E; .NET4.0C; rv:11.0) like Gecko',
{ type: 'browser', name: 'ie', version: '11.0.0', os: 'Windows 8.1' },
);
assertAgentString(
t,
'Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0; MSN 11.61; MSNbMSNI; MSNmen-us; MSNcOTH) like Gecko',
{ type: 'browser', name: 'ie', version: '11.0.0', os: 'Windows 10' },
);
assertAgentString(
t,
'Mozilla/5.0 (compatible; MSIE 10.6; Windows NT 6.1; Trident/5.0; InfoPath.2; SLCC1; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET CLR 2.0.50727) 3gpp-gba UNTRUSTED/1.0',
{ type: 'browser', name: 'ie', version: '10.6.0', os: 'Windows 7' },
);
assertAgentString(
t,
'Mozilla/5.0 (compatible; MSIE 7.0; Windows NT 5.2; WOW64; .NET CLR 2.0.50727)',
{
type: 'browser',
name: 'ie',
version: '7.0.0',
os: 'Windows Server 2003',
},
);
t.end();
});
test('detects Opera', function(t) {
assertAgentString(
t,
'Opera/9.80 (J2ME/MIDP; Opera Mini/5.0 (Windows; U; Windows NT 5.1; en) AppleWebKit/886; U; en) Presto/2.4.15',
{ type: 'browser', name: 'opera', version: '9.80.0', os: 'Windows XP' },
);
assertAgentString(t, 'Opera/9.25 (Macintosh; Intel Mac OS X; U; en)', {
type: 'browser',
name: 'opera',
version: '9.25.0',
os: 'Mac OS',
});
assertAgentString(
t,
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.84 Safari/537.36 OPR/38.0.2220.31',
{ type: 'browser', name: 'opera', version: '38.0.2220', os: 'Mac OS' },
);
assertAgentString(
t,
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.28 Safari/537.36 OPR/61.0.3282.0 (Edition developer)',
{ type: 'browser', name: 'opera', version: '61.0.3282', os: 'Mac OS' },
);
t.end();
});
test('detects BlackBerry 10', function(t) {
assertAgentString(
t,
'Mozilla/5.0 (BB10; Touch) AppleWebKit/537.10+ (KHTML, like Gecko) Version/7.2.0.0 Mobile Safari/537.10+',
{ type: 'browser', name: 'bb10', version: '7.2.0', os: 'BlackBerry OS' },
);
t.end();
});
test('detects Android Webkit browser', function(t) {
assertAgentString(
t,
'Mozilla/5.0 (Linux; U; Android 4.0.3; ko-kr; LG-L160L Build/IML74K) AppleWebkit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30',
{ type: 'browser', name: 'android', version: '4.0.3', os: 'Android OS' },
);
t.end();
});
test('detects mobile Safari', function(t) {
assertAgentString(
t,
'Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5355d Safari/8536.25',
{ type: 'browser', name: 'ios', version: '6.0.0', os: 'iOS' },
);
assertAgentString(
t,
'Mozilla/5.0 (iPod; U; CPU iPhone OS 4_3_3 like Mac OS X; ja-jp) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5',
{ type: 'browser', name: 'ios', version: '5.0.2', os: 'iOS' },
);
t.end();
});
test('detects Safari', function(t) {
assertAgentString(
t,
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.75.14 (KHTML, like Gecko) Version/7.0.3 Safari/7046A194A',
{ type: 'browser', name: 'safari', version: '7.0.3', os: 'Mac OS' },
);
t.end();
});
test('detects Yandex Browser', function(t) {
assertAgentString(
t,
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 YaBrowser/16.10.0.2774 Safari/537.36',
{
type: 'browser',
name: 'yandexbrowser',
version: '16.10.0',
os: 'Mac OS',
},
);
t.end();
});
test('detects Kakaotalk Browser', function(t) {
assertAgentString(
t,
'Netscape 5.0 (iPhone; CPU iPhone OS 10_3 1 like Mac OS X) AppleWebKit/603.1.30 (KHTML, like Gecko) Mobile/14E304 KAKAOTALK 6.2.2',
{ type: 'browser', name: 'kakaotalk', version: '6.2.2', os: 'iOS' },
);
assertAgentString(
t,
'Mozilla/5.0 (iPhone; CPU iPhone OS 10_3 1 like Mac OS X) AppleWebKit/ 603.1.30 (KHTML, like Gecko) Mobile/ 14E304 KAKAOTALK 6.2.2',
{ type: 'browser', name: 'kakaotalk', version: '6.2.2', os: 'iOS' },
);
t.end();
});
test('detects PhantomJS Browser', function(t) {
assertAgentString(
t,
'Mozilla/5.0 (Macintosh; Intel Mac OS X) AppleWebKit/538.1 (KHTML, like Gecko) PhantomJS/2.1.1 Safari/538.1',
{ type: 'browser', name: 'phantomjs', version: '2.1.1', os: 'Mac OS' },
);
t.end();
});
test('detects AOLShield Browser', function(t) {
assertAgentString(
t,
'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2841.00 Safari/537.36 AOLShield/54.0.2848.0',
{ type: 'browser', name: 'aol', version: '54.0.2848', os: 'Windows 10' },
);
t.end();
});
test('detects facebook in-app browser', function(t) {
assertAgentString(
t,
'Mozilla/5.0 (iPhone; CPU iPhone OS 11_2_5 like Mac OS X) AppleWebKit/604.5.6 (KHTML, like Gecko) Mobile/15D60 [FBAN/FBIOS;FBAV/157.0.0.42.96;FBBV/90008621;FBDV/iPhone9,1;FBMD/iPhone;FBSN/iOS;FBSV/11.2.5;FBSS/2;FBCR/Verizon;FBID/phone;FBLC/en_US;FBOP/5;FBRV/0]',
{ type: 'browser', name: 'facebook', version: '157.0.0', os: 'iOS' },
);
t.end();
});
test('detects instagram in-app browser', function(t) {
assertAgentString(
t,
'Mozilla/5.0 (iPhone; CPU iPhone OS 9_3_2 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Mobile/13F69 Instagram 8.4.0 (iPhone7,2; iPhone OS 9_3_2; nb_NO; nb-NO; scale=2.00; 750x1334',
{ type: 'browser', name: 'instagram', version: '8.4.0', os: 'iOS' },
);
t.end();
});
test('detects native iOS WebView browser', function(t) {
assertAgentString(
t,
'User-Agent: Mozilla/5.0 (iPad; U; CPU OS 4_3_2 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Mobile',
{ type: 'browser', name: 'ios-webview', version: '533.17.9', os: 'iOS' },
);
assertAgentString(
t,
'Mozilla/5.0 (iPad; CPU OS 11_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E216',
{ type: 'browser', name: 'ios-webview', version: '605.1.15', os: 'iOS' },
);
assertAgentString(
t,
'Mozilla/5.0 (iPhone; CPU iPhone OS 12_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/16B92',
{ type: 'browser', name: 'ios-webview', version: '605.1.15', os: 'iOS' },
);
assertAgentString(
t,
'Mozilla/5.0 (iPhone; CPU iPhone OS 12_1_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko)',
{ type: 'browser', name: 'ios-webview', version: '605.1.15', os: 'iOS' },
);
t.end();
});
test('detects Samsung Internet browser', function(t) {
assertAgentString(
t,
'Mozilla/5.0 (Linux; Android 5.0.2; SAMSUNG SM-G925F Build/LRX22G) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/4.0 Chrome/44.0.2403.133 Mobile Safari/537.36',
{ type: 'browser', name: 'samsung', version: '4.0.0', os: 'Android OS' },
);
t.end();
});
test('detects crawler: AhrefsBot', function(t) {
assertAgentString(
t,
'Mozilla/5.0 (compatible; AhrefsBot/5.2; +http://ahrefs.com/robot/)',
{ type: 'bot', bot: true, name: 'bot', version: null, os: null },
);
t.end();
});
test('detects crawler: GoogleBot', function(t) {
assertAgentString(
t,
'Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; Googlebot/2.1; +http://www.google.com/bot.html) Safari/537.36',
{ type: 'bot', bot: true, name: 'bot', version: null, os: null },
);
t.end();
});
test('detects crawler: YandexBot', function(t) {
assertAgentString(
t,
'Mozilla/5.0 (compatible; YandexBot/3.0; +http://yandex.com/bots)',
{ type: 'bot', bot: true, name: 'bot', version: null, os: null },
);
t.end();
});
test('detects Opera-Mini', function(t) {
assertAgentString(
t,
'Opera/9.80 (Android; Opera Mini/8.0.1807/36.1609; U; en) Presto/2.12.423 Version/12.16',
{
type: 'browser',
name: 'opera-mini',
version: '12.16.0',
os: 'Android OS',
},
);
assertAgentString(
t,
'Opera/9.80 (BlackBerry; Opera Mini/6.5.27548/27.2020; U; en) Presto/2.8.119 Version/11.10',
{
type: 'browser',
name: 'opera-mini',
version: '11.10.0',
os: 'BlackBerry OS',
},
);
t.end();
});
test('detects Silk', function(t) {
assertAgentString(
t,
'Mozilla/5.0 (Linux; Android 4.4.3; KFTHWI Build/KTU84M) AppleWebKit/537.36 (KHTML, like Gecko) Silk/44.1.54 like Chrome/44.0.2403.63 Safari/537.36',
{ type: 'browser', name: 'silk', version: '44.1.54', os: 'Android OS' },
);
assertAgentString(
t,
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Silk/44.1.54 like Chrome/44.0.2403.63 Safari/537.36',
{ type: 'browser', name: 'silk', version: '44.1.54', os: 'Linux' },
);
assertAgentString(
t,
'Mozilla/5.0 (Linux; U; Android 4.4.3; KFTHWI Build/KTU84M) AppleWebKit/537.36 (KHTML, like Gecko) Silk/44.1.54 like Chrome/44.0.2403.63 Mobile Safari/537.36',
{ type: 'browser', name: 'silk', version: '44.1.54', os: 'Android OS' },
);
t.end();
});
test('detects Chrome OS', function(t) {
assertAgentString(
t,
'Mozilla/5.0 (X11; CrOS x86_64 10895.78.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.120 Safari/537.36',
{ type: 'browser', name: 'chrome', version: '69.0.3497', os: 'Chrome OS' },
);
assertAgentString(
t,
'Mozilla/5.0 (X11; U; CrOS i686 9.10.0; en-US) AppleWebKit/532.5 (KHTML, like Gecko) Gecko/20100101 Firefox/29.0',
{ type: 'browser', name: 'firefox', version: '29.0.0', os: 'Chrome OS' },
);
t.end();
});
test('detects miui', function(t) {
assertAgentString(
t,
'Mozilla/5.0 (Linux; U; Android 7.0; en-us; MI 5 Build/NRD90M) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/53.0.2785.146 Mobile Safari/537.36 XiaoMi/MiuiBrowser/9.0.3',
{ type: 'browser', name: 'miui', version: '9.0.3', os: 'Android OS' },
);
t.end();
});
test('detects Beaker Browser', function(t) {
assertAgentString(
t,
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) BeakerBrowser/0.8.7 Chrome/69.0.3497.128 Electron/4.1.3 Safari/537.36',
{ type: 'browser', name: 'beaker', version: '0.8.7', os: 'Windows 10' },
);
t.end();
});
test('detects edge chromium', function(t) {
assertAgentString(
t,
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.48 Safari/537.36 Edg/74.1.96.24',
{
type: 'browser',
name: 'edge-chromium',
version: '74.1.96',
os: 'Windows 10',
},
);
t.end();
});
test('detects edge chromium (android os)', function(t) {
assertAgentString(
t,
'Mozilla/5.0 (Linux; Android 9; SM-N950F) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.116 Mobile Safari/537.36 EdgA/45.08.4.5074',
{
type: 'browser',
name: 'edge-chromium',
version: '45.08.4',
os: 'Android OS',
},
);
t.end();
});
test('detects edge iOS', function(t) {
assertAgentString(
t,
'Mozilla/5.0 (iPhone; CPU iPhone OS 12_1_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0 EdgiOS/44.2.1 Mobile/16D57 Safari/605.1.15',
{ type: 'browser', name: 'edge-ios', version: '44.2.1', os: 'iOS' },
);
t.end();
});
test('handles no browser', function(t) {
assertAgentString(t, null, null);
t.end();
});
/* https://developer.chrome.com/multidevice/user-agent */
test('detects Chromium-based WebView On Android', function(t) {
assertAgentString(
t,
'Mozilla/5.0 (Linux; Android 5.1.1; Nexus 5 Build/LMY48B; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/43.0.2357.65 Mobile Safari/537.36',
{
type: 'browser',
name: 'chromium-webview',
version: '43.0.2357',
os: 'Android OS',
},
);
t.end();
});
test('detects extended bot info', function(t) {
assertAgentString(
t,
'Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5X Build/MMB29P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.96 Mobile Safari/537.36 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)',
{
type: 'bot-device',
name: 'chrome',
version: '41.0.2272',
os: 'Android OS',
bot: 'Googlebot',
},
);
t.end();
});
/** Windows CE Ozone (CE 4.2 P/PC 2003) */
test('detects PocketPC2003', function(t) {
assertAgentString(
t,
'Mozilla/4.0 (compatible; MSIE 4.01; Windows CE; PPC; 240x320)',
{
type: 'browser',
name: 'pie',
version: '4.01.0',
os: 'Windows CE',
},
);
t.end();
});
/** Windows CE Pegasus (CE 1.0x) PIE 1.1 */
test('detects PIE 1.1', function(t) {
assertAgentString(
t,
'Mozilla/1.1 (compatible; MSPIE 1.1; Windows CE)',
{
type: 'browser',
name: 'pie',
version: '1.1.0',
os: 'Windows CE',
},
);
t.end();
});
/** Windows CE Stinger SmartPhone 2003 */
test('detects NetFront', function(t) {
assertAgentString(
t,
'Mozilla/4.0 (PDA; Windows CE;1.0.0) NetFront/3.0',
{
type: 'browser',
name: 'netfront',
version: '3.0.0',
os: 'Windows CE',
},
);
t.end();
});
test('detects extended bot info', function(t) {
assertAgentString(
t,
'curl/7.64.1',
{
type: 'bot-device',
name: 'curl',
version: '7.64.1',
os: null,
bot: 'curl',
},
);
t.end();
});