ua-parser-js 2.0.0-rc.3 → 2.0.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.
@@ -1,13 +1,14 @@
1
- // Type definitions for Helpers submodule of UAParser.js v2.0.0-rc.3
1
+ // Type definitions for Helpers submodule of UAParser.js v2.0.1
2
2
  // Project: https://github.com/faisalman/ua-parser-js
3
3
  // Definitions by: Faisal Salman <https://github.com/faisalman>
4
4
 
5
- import { IResult } from "../main/ua-parser";
5
+ import type { IResult } from "../main/ua-parser";
6
6
 
7
7
  declare function getDeviceVendor(model: string): string | undefined;
8
- declare function isAppleSilicon(res: IResult, useFeatureDetection?: boolean): boolean;
9
- declare function isBot(res: IResult): boolean;
10
- declare function isChromeFamily(res: IResult): boolean;
8
+ declare function isAppleSilicon(resultOrUA: IResult | string): boolean;
9
+ declare function isAIBot(resultOrUA: IResult | string): boolean;
10
+ declare function isBot(resultOrUA: IResult | string): boolean;
11
+ declare function isChromeFamily(resultOrUA: IResult | string): boolean;
11
12
  declare function isElectron(): boolean;
12
13
  declare function isFromEU(): boolean;
13
14
  declare function isFrozenUA(ua: string): boolean;
@@ -16,6 +17,7 @@ declare function isStandalonePWA(): boolean;
16
17
  export {
17
18
  getDeviceVendor,
18
19
  isAppleSilicon,
20
+ isAIBot,
19
21
  isBot,
20
22
  isChromeFamily,
21
23
  isElectron,
@@ -1,5 +1,5 @@
1
1
  ///////////////////////////////////////////////
2
- /* Helpers for UAParser.js v2.0.0-rc.3
2
+ /* Helpers for UAParser.js v2.0.1
3
3
  https://github.com/faisalman/ua-parser-js
4
4
  Author: Faisal Salman <f@faisalman.com>
5
5
  AGPLv3 License */
@@ -9,18 +9,22 @@
9
9
 
10
10
  const { UAParser } = require('../main/ua-parser');
11
11
  const { CPU, OS, Engine } = require('../enums/ua-parser-enums');
12
+ const { Bots } = require('../extensions/ua-parser-extensions');
12
13
  const { isFromEU } = require('detect-europe-js');
13
14
  const { isFrozenUA } = require('ua-is-frozen');
14
15
  const { isStandalonePWA } = require('is-standalone-pwa');
15
16
 
17
+ const toResult = (value, head, ext) => typeof value === 'string' ? UAParser(value, head, ext) : value;
18
+
16
19
  const getDeviceVendor = (model) => UAParser(`Mozilla/5.0 (Linux; Android 10; ${model}) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.0.0 Safari/537.36`).device.vendor;
17
20
 
18
- const isAppleSilicon = (res, useFeatureDetection) => {
21
+ const isAppleSilicon = (resultOrUA) => {
22
+ const res = toResult(resultOrUA);
19
23
  if (res.os.is(OS.MACOS)) {
20
24
  if (res.cpu.is(CPU.ARM)) {
21
25
  return true;
22
26
  }
23
- if (useFeatureDetection) {
27
+ if (typeof resultOrUA !== 'string' && typeof window !== 'undefined') {
24
28
  try {
25
29
  const canvas = document.createElement('canvas');
26
30
  const webgl = canvas.getContext('webgl2') || canvas.getContext('webgl') || canvas.getContext('experimental-webgl');
@@ -37,9 +41,85 @@ const isAppleSilicon = (res, useFeatureDetection) => {
37
41
  return false;
38
42
  }
39
43
 
40
- const isBot = (res) => ['cli', 'crawler', 'fetcher', 'library'].includes(res.browser.type);
44
+ const isAIBot = (resultOrUA) => [
45
+
46
+ // AI2
47
+ 'ai2bot',
48
+
49
+ // Amazon
50
+ 'amazonbot',
51
+
52
+ // Anthropic
53
+ 'anthropic-ai',
54
+ 'claude-web',
55
+ 'claudebot',
56
+
57
+ // Apple
58
+ 'applebot',
59
+ 'applebot-extended',
60
+
61
+ // ByteDance
62
+ 'bytespider',
63
+
64
+ // Common Crawl
65
+ 'ccbot',
66
+
67
+ // DataForSeo
68
+ 'dataforseobot',
69
+
70
+ // Diffbot
71
+ 'diffbot',
72
+
73
+ // Google
74
+ 'googleother',
75
+ 'googleother-image',
76
+ 'googleother-video',
77
+ 'google-extended',
78
+
79
+ // Hive AI
80
+ 'imagesiftbot',
81
+
82
+ // Huawei
83
+ 'petalbot',
84
+
85
+ // Meta
86
+ 'facebookbot',
87
+ 'meta-externalagent',
88
+
89
+ // OpenAI
90
+ 'gptbot',
91
+ 'oai-searchbot',
92
+
93
+ // Perplexity
94
+ 'perplexitybot',
95
+
96
+ // Timpi
97
+ 'timpibot',
98
+
99
+ // Velen.io
100
+ 'velenpublicwebcrawler',
101
+
102
+ // Webz.io
103
+ 'omgili',
104
+ 'omgilibot',
105
+ 'webzio-extended',
106
+
107
+ // You.com
108
+ 'youbot',
109
+
110
+ // Zyte
111
+ 'scrapy'
112
+
113
+ ].includes(String(toResult(resultOrUA, Bots).browser.name).toLowerCase());
114
+
115
+ const isBot = (resultOrUA) => [
116
+ 'cli',
117
+ 'crawler',
118
+ 'fetcher',
119
+ 'library'
120
+ ].includes(toResult(resultOrUA, Bots).browser.type);
41
121
 
42
- const isChromeFamily = (res) => res.engine.is(Engine.BLINK);
122
+ const isChromeFamily = (resultOrUA) => toResult(resultOrUA).engine.is(Engine.BLINK);
43
123
 
44
124
  const isElectron = () => !!(process?.versions?.hasOwnProperty('electron') || // node.js
45
125
  / electron\//i.test(navigator?.userAgent)); // browser
@@ -47,6 +127,7 @@ const isElectron = () => !!(process?.versions?.hasOwnProperty('electron') ||
47
127
  module.exports = {
48
128
  getDeviceVendor,
49
129
  isAppleSilicon,
130
+ isAIBot,
50
131
  isBot,
51
132
  isChromeFamily,
52
133
  isElectron,
@@ -3,7 +3,7 @@
3
3
  // Source: /src/helpers/ua-parser-helpers.js
4
4
 
5
5
  ///////////////////////////////////////////////
6
- /* Helpers for UAParser.js v2.0.0-rc.3
6
+ /* Helpers for UAParser.js v2.0.1
7
7
  https://github.com/faisalman/ua-parser-js
8
8
  Author: Faisal Salman <f@faisalman.com>
9
9
  AGPLv3 License */
@@ -13,18 +13,22 @@
13
13
 
14
14
  import { UAParser } from '../main/ua-parser.mjs';
15
15
  import { CPU, OS, Engine } from '../enums/ua-parser-enums.mjs';
16
+ import { Bots } from '../extensions/ua-parser-extensions.mjs';
16
17
  import { isFromEU } from 'detect-europe-js';
17
18
  import { isFrozenUA } from 'ua-is-frozen';
18
19
  import { isStandalonePWA } from 'is-standalone-pwa';
19
20
 
21
+ const toResult = (value, head, ext) => typeof value === 'string' ? UAParser(value, head, ext) : value;
22
+
20
23
  const getDeviceVendor = (model) => UAParser(`Mozilla/5.0 (Linux; Android 10; ${model}) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.0.0 Safari/537.36`).device.vendor;
21
24
 
22
- const isAppleSilicon = (res, useFeatureDetection) => {
25
+ const isAppleSilicon = (resultOrUA) => {
26
+ const res = toResult(resultOrUA);
23
27
  if (res.os.is(OS.MACOS)) {
24
28
  if (res.cpu.is(CPU.ARM)) {
25
29
  return true;
26
30
  }
27
- if (useFeatureDetection) {
31
+ if (typeof resultOrUA !== 'string' && typeof window !== 'undefined') {
28
32
  try {
29
33
  const canvas = document.createElement('canvas');
30
34
  const webgl = canvas.getContext('webgl2') || canvas.getContext('webgl') || canvas.getContext('experimental-webgl');
@@ -41,9 +45,85 @@ const isAppleSilicon = (res, useFeatureDetection) => {
41
45
  return false;
42
46
  }
43
47
 
44
- const isBot = (res) => ['cli', 'crawler', 'fetcher', 'library'].includes(res.browser.type);
48
+ const isAIBot = (resultOrUA) => [
49
+
50
+ // AI2
51
+ 'ai2bot',
52
+
53
+ // Amazon
54
+ 'amazonbot',
55
+
56
+ // Anthropic
57
+ 'anthropic-ai',
58
+ 'claude-web',
59
+ 'claudebot',
60
+
61
+ // Apple
62
+ 'applebot',
63
+ 'applebot-extended',
64
+
65
+ // ByteDance
66
+ 'bytespider',
67
+
68
+ // Common Crawl
69
+ 'ccbot',
70
+
71
+ // DataForSeo
72
+ 'dataforseobot',
73
+
74
+ // Diffbot
75
+ 'diffbot',
76
+
77
+ // Google
78
+ 'googleother',
79
+ 'googleother-image',
80
+ 'googleother-video',
81
+ 'google-extended',
82
+
83
+ // Hive AI
84
+ 'imagesiftbot',
85
+
86
+ // Huawei
87
+ 'petalbot',
88
+
89
+ // Meta
90
+ 'facebookbot',
91
+ 'meta-externalagent',
92
+
93
+ // OpenAI
94
+ 'gptbot',
95
+ 'oai-searchbot',
96
+
97
+ // Perplexity
98
+ 'perplexitybot',
99
+
100
+ // Timpi
101
+ 'timpibot',
102
+
103
+ // Velen.io
104
+ 'velenpublicwebcrawler',
105
+
106
+ // Webz.io
107
+ 'omgili',
108
+ 'omgilibot',
109
+ 'webzio-extended',
110
+
111
+ // You.com
112
+ 'youbot',
113
+
114
+ // Zyte
115
+ 'scrapy'
116
+
117
+ ].includes(String(toResult(resultOrUA, Bots).browser.name).toLowerCase());
118
+
119
+ const isBot = (resultOrUA) => [
120
+ 'cli',
121
+ 'crawler',
122
+ 'fetcher',
123
+ 'library'
124
+ ].includes(toResult(resultOrUA, Bots).browser.type);
45
125
 
46
- const isChromeFamily = (res) => res.engine.is(Engine.BLINK);
126
+ const isChromeFamily = (resultOrUA) => toResult(resultOrUA).engine.is(Engine.BLINK);
47
127
 
48
128
  const isElectron = () => !!(process?.versions?.hasOwnProperty('electron') || // node.js
49
129
  / electron\//i.test(navigator?.userAgent)); // browser
@@ -51,6 +131,7 @@ const isElectron = () => !!(process?.versions?.hasOwnProperty('electron') ||
51
131
  export {
52
132
  getDeviceVendor,
53
133
  isAppleSilicon,
134
+ isAIBot,
54
135
  isBot,
55
136
  isChromeFamily,
56
137
  isElectron,
@@ -1,7 +1,10 @@
1
- // Type definitions for UAParser.js v2.0.0-rc.3
1
+ // Type definitions for UAParser.js v2.0.1
2
2
  // Project: https://github.com/faisalman/ua-parser-js
3
3
  // Definitions by: Faisal Salman <https://github.com/faisalman>
4
4
 
5
+ import type { IncomingHttpHeaders } from 'http';
6
+ import type { Headers as FetchAPIHeaders } from 'node-fetch';
7
+
5
8
  declare namespace UAParser {
6
9
 
7
10
  interface IData<T> {
@@ -50,11 +53,12 @@ declare namespace UAParser {
50
53
  type RegexMap = ((RegExp | string | (string | RegExp | Function)[])[])[];
51
54
  type UAParserProps = 'browser' | 'cpu' | 'device' | 'engine' | 'os';
52
55
  type UAParserExt = Partial<Record<UAParserProps, RegexMap>> | Partial<Record<UAParserProps, RegexMap>>[];
56
+ type UAParserHeaders = Record<string, string> | IncomingHttpHeaders | FetchAPIHeaders;
53
57
 
54
- export function UAParser(uastring?: string, extensions?: UAParserExt, headers?: Record<string, string>): IResult;
55
- export function UAParser(uastring?: string, headers?: Record<string, string>): IResult;
56
- export function UAParser(extensions?: UAParserExt, headers?: Record<string, string>): IResult;
57
- export function UAParser(headers?: Record<string, string>): IResult;
58
+ export function UAParser(uastring?: string, extensions?: UAParserExt, headers?: UAParserHeaders): IResult;
59
+ export function UAParser(uastring?: string, headers?: UAParserHeaders): IResult;
60
+ export function UAParser(extensions?: UAParserExt, headers?: UAParserHeaders): IResult;
61
+ export function UAParser(headers?: UAParserHeaders): IResult;
58
62
 
59
63
  export class UAParser {
60
64