ua-browser 1.1.0 → 1.3.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.
package/README.md CHANGED
@@ -5,13 +5,15 @@
5
5
  [![license](https://img.shields.io/npm/l/ua-browser)](./LICENSE)
6
6
  [![TypeScript](https://img.shields.io/badge/TypeScript-ready-3178c6)](https://www.typescriptlang.org/)
7
7
 
8
- Detect browser, OS, device type, rendering engine, CPU architecture, bots, and headless browsers from User Agent strings. Zero dependencies. Works in both browser and Node.js environments.
8
+ Detect browser, OS, device type, rendering engine, CPU architecture, bots, headless browsers, and Mini Programs from User Agent strings. Zero dependencies. Works in both browser and Node.js environments.
9
9
 
10
10
  **[📖 Documentation](https://yangtianxia.github.io/ua-browser/)** · **[🎮 Playground](https://yangtianxia.github.io/ua-browser/playground)** · **[中文](./README.zh-CN.md)**
11
11
 
12
12
  ## Features
13
13
 
14
- - **Comprehensive detection** — browser, OS, engine, device type (Mobile / Tablet / TV / PC), CPU arch, bots, headless browsers
14
+ - **Comprehensive UA detection** — browser, OS, engine, device type (Mobile / Tablet / TV / PC), CPU arch, bots, headless browsers
15
+ - **Multi-signal arch detection** — `getEnvContext()` collects Client Hints, WebGL renderer, and font probes to accurately distinguish Apple Silicon from Intel Mac
16
+ - **SSR Client Hints** — `parseHeaders()` + `ACCEPT_CH` for precise server-side detection (CPU arch, platform) in Chrome / Edge 90+
15
17
  - **AI bot recognition** — built-in support for GPTBot, ClaudeBot, PerplexityBot, CCBot and more
16
18
  - **Zero dependencies** — no runtime dependencies, tiny bundle size after gzip
17
19
  - **Pure function** — `parseUA()` has no global state, works seamlessly with SSR / Node.js
@@ -67,16 +69,10 @@ const { browser, os, device } = uaBrowser()
67
69
  if (device === 'Mobile') {
68
70
  // redirect to mobile version
69
71
  }
70
-
71
- if (browser === 'Wechat') {
72
- // WeChat in-app browser logic
73
- }
74
72
  ```
75
73
 
76
74
  ### Node.js / SSR
77
75
 
78
- Use the pure `parseUA` function with the UA string from the request header:
79
-
80
76
  ```typescript
81
77
  import { parseUA } from 'ua-browser'
82
78
 
@@ -97,6 +93,34 @@ if (isBot) {
97
93
  </script>
98
94
  ```
99
95
 
96
+ ### Multi-signal Architecture Detection
97
+
98
+ `getEnvContext()` collects Client Hints, WebGL renderer, and other browser signals in one async call — enough to distinguish Apple Silicon from Intel Mac:
99
+
100
+ ```typescript
101
+ import { getEnvContext, parseUA } from 'ua-browser'
102
+
103
+ const ctx = await getEnvContext()
104
+ const result = parseUA(navigator.userAgent, { ctx })
105
+
106
+ console.log(result.arch) // 'arm64' or 'x86_64'
107
+ ```
108
+
109
+ ### SSR Client Hints
110
+
111
+ Set the `ACCEPT_CH` response header so Chrome / Edge 90+ browsers send Client Hints on subsequent requests, then use `parseHeaders` for precise server-side detection:
112
+
113
+ ```typescript
114
+ import { parseHeaders, ACCEPT_CH } from 'ua-browser'
115
+
116
+ // First response — tell the browser to send Client Hints
117
+ res.setHeader('Accept-CH', ACCEPT_CH)
118
+
119
+ // Subsequent requests — accurate arch / OS detection
120
+ const result = parseHeaders(req.headers)
121
+ console.log(result.arch) // 'x86_64' (from Sec-CH-UA-Arch)
122
+ ```
123
+
100
124
  ### Accurate Windows 10 / 11 Detection
101
125
 
102
126
  Windows 10 and 11 share the same UA string. Use the Client Hints API to distinguish them:
@@ -118,8 +142,6 @@ console.log(result.osVersion) // '10' or '11'
118
142
  Automatically injects the `navigator` context (language, platform, MIME types, etc.) in browser environments.
119
143
 
120
144
  ```typescript
121
- import uaBrowser from 'ua-browser'
122
-
123
145
  uaBrowser() // reads navigator.userAgent automatically
124
146
  uaBrowser(customUA) // custom UA string, still injects browser context
125
147
  ```
@@ -128,16 +150,18 @@ uaBrowser(customUA) // custom UA string, still injects browser context
128
150
 
129
151
  ```typescript
130
152
  import {
131
- parseUA, // pure function, ideal for SSR / Node.js
132
- getNavContext, // read current browser navigator context
133
- getWindowsVersion, // async: accurately distinguish Windows 10 / 11
134
- getLanguage, // extract browser language from NavContext
135
- isWebview, // detect Android Webview (UA contains "; wv")
136
- isWechatMiniapp, // detect WeChat Mini Program environment
137
- detectBot, // standalone bot detection
138
- detectArch, // standalone CPU architecture detection
139
- detectHeadless, // standalone headless browser detection
140
- VERSION, // current library version
153
+ parseUA, // pure function, ideal for SSR / Node.js
154
+ getNavContext, // read current browser navigator context
155
+ getWindowsVersion, // async: accurately distinguish Windows 10 / 11
156
+ getLanguage, // extract browser language from NavContext
157
+ getEnvContext, // collect all browser signals (Client Hints, WebGL, etc.)
158
+ parseHeaders, // parse UA and Client Hints from HTTP headers (SSR)
159
+ ACCEPT_CH, // response header constant to request Client Hints
160
+ isWebview, // detect Android Webview / iOS WKWebView
161
+ detectBot, // standalone bot detection
162
+ detectArch, // standalone CPU architecture detection
163
+ detectHeadless, // standalone headless browser detection
164
+ VERSION, // current library version
141
165
  } from 'ua-browser'
142
166
  ```
143
167
 
@@ -152,7 +176,7 @@ import {
152
176
  | `osVersion` | `string` | OS version |
153
177
  | `device` | `DeviceName` | Device type: `Mobile` \| `Tablet` \| `TV` \| `PC` |
154
178
  | `arch` | `ArchName` | CPU architecture |
155
- | `isWebview` | `boolean` | Whether running in Android Webview |
179
+ | `isWebview` | `boolean` | Whether running in Android Webview or iOS WKWebView |
156
180
  | `isHeadless` | `boolean` | Whether running in a headless / automated browser |
157
181
  | `isBot` | `boolean` | Whether the UA belongs to a bot / crawler |
158
182
  | `botName` | `BotName` | Bot name |
@@ -163,11 +187,11 @@ import {
163
187
 
164
188
  ## Supported
165
189
 
166
- Over 60 browsers, 17 operating systems, and 19 bot rules built in. See the **[full support list](https://yangtianxia.github.io/ua-browser/guide/support-list)**.
190
+ Over 70 browsers, 17 operating systems, and 19 bot rules built in. See the **[full support list](https://yangtianxia.github.io/ua-browser/guide/support-list)**.
167
191
 
168
192
  Highlights:
169
- - **Browsers** — Chrome, Safari, Firefox, Edge, Samsung Internet, UC, WeChat, DingTalk, TikTok and more
170
- - **OS** — Windows, macOS, Android, iOS, HarmonyOS, Tizen, KaiOS and more
193
+ - **Browsers** — Chrome, Safari, Firefox, Edge, Samsung Internet, UC, WeChat, DingTalk, TikTok, Bilibili, Kuaishou, Xiaohongshu, Feishu and more
194
+ - **OS** — Windows, macOS, Android, iOS, HarmonyOS, OpenHarmony, Tizen, KaiOS and more
171
195
  - **AI bots** — GPTBot, ClaudeBot, PerplexityBot, CCBot and more
172
196
  - **Devices** — Mobile, Tablet, TV (Samsung Smart TV, HbbTV), PC
173
197
 
package/README.zh-CN.md CHANGED
@@ -5,14 +5,16 @@
5
5
  [![license](https://img.shields.io/npm/l/ua-browser)](./LICENSE)
6
6
  [![TypeScript](https://img.shields.io/badge/TypeScript-ready-3178c6)](https://www.typescriptlang.org/)
7
7
 
8
- 通过 User Agent 检测浏览器、操作系统、设备类型、渲染内核、CPU 架构、爬虫及无头浏览器。零依赖,支持浏览器与 Node.js 双环境。
8
+ 通过 User Agent 检测浏览器、操作系统、设备类型、渲染内核、CPU 架构、爬虫、无头浏览器及小程序运行环境。零依赖,支持浏览器与 Node.js 双环境。
9
9
 
10
10
  **[📖 文档](https://yangtianxia.github.io/ua-browser/)** · **[🎮 Playground](https://yangtianxia.github.io/ua-browser/playground)** · **[English](./README.md)**
11
11
 
12
12
  ## 特性
13
13
 
14
- - **全面检测** — 浏览器、OS、渲染内核、设备类型(Mobile / Tablet / TV / PC)、CPU 架构、爬虫、无头浏览器
15
- - **AI 爬虫识别** 内置 GPTBotClaudeBot、PerplexityBot 等主流 AI 抓取机器人
14
+ - **全面 UA 检测** — 浏览器、OS、渲染内核、设备类型(Mobile / Tablet / TV / PC)、CPU 架构、爬虫、无头浏览器
15
+ - **多信号架构检测**`getEnvContext()` 采集 Client HintsWebGL 渲染器、字体探针,精确区分 Apple Silicon 与 Intel Mac
16
+ - **SSR Client Hints** — `parseHeaders()` + `ACCEPT_CH`,在 Chrome / Edge 90+ 中实现服务端精准检测(CPU 架构、平台等)
17
+ - **AI 爬虫识别** — 内置 GPTBot、ClaudeBot、PerplexityBot、CCBot 等主流 AI 抓取机器人
16
18
  - **零依赖** — 无任何运行时依赖,gzip 后体积极小
17
19
  - **纯函数** — `parseUA()` 无全局状态,天然支持 SSR / Node.js
18
20
  - **TypeScript** — 完整类型定义,`BrowserName`、`OsName` 等均为精确字面量联合类型
@@ -67,16 +69,10 @@ const { browser, os, device } = uaBrowser()
67
69
  if (device === 'Mobile') {
68
70
  // 跳转移动版
69
71
  }
70
-
71
- if (browser === 'Wechat') {
72
- // 微信内置浏览器逻辑
73
- }
74
72
  ```
75
73
 
76
74
  ### Node.js / SSR
77
75
 
78
- 使用 `parseUA` 纯函数,传入请求头中的 UA 字符串:
79
-
80
76
  ```typescript
81
77
  import { parseUA } from 'ua-browser'
82
78
 
@@ -97,6 +93,34 @@ if (isBot) {
97
93
  </script>
98
94
  ```
99
95
 
96
+ ### 多信号架构检测
97
+
98
+ `getEnvContext()` 一次性采集 Client Hints、WebGL 渲染器等多维信号,可区分 Apple Silicon 与 Intel Mac:
99
+
100
+ ```typescript
101
+ import { getEnvContext, parseUA } from 'ua-browser'
102
+
103
+ const ctx = await getEnvContext()
104
+ const result = parseUA(navigator.userAgent, { ctx })
105
+
106
+ console.log(result.arch) // 'arm64' 或 'x86_64'
107
+ ```
108
+
109
+ ### SSR Client Hints
110
+
111
+ 通过响应头告知 Chrome / Edge 90+ 上报 Client Hints,再用 `parseHeaders` 在服务端精准解析:
112
+
113
+ ```typescript
114
+ import { parseHeaders, ACCEPT_CH } from 'ua-browser'
115
+
116
+ // 第一次响应时写入 Accept-CH
117
+ res.setHeader('Accept-CH', ACCEPT_CH)
118
+
119
+ // 后续请求携带 Client Hints 后,精准识别架构等信息
120
+ const result = parseHeaders(req.headers)
121
+ console.log(result.arch) // 'x86_64'(来自 Sec-CH-UA-Arch)
122
+ ```
123
+
100
124
  ### 精确区分 Windows 10 / 11
101
125
 
102
126
  Windows 10 和 11 的 UA 字符串相同,需借助 Client Hints API 异步获取:
@@ -118,8 +142,6 @@ console.log(result.osVersion) // '10' 或 '11'
118
142
  在浏览器环境中自动注入 `navigator` 上下文(语言、平台、MIME 类型等)。
119
143
 
120
144
  ```typescript
121
- import uaBrowser from 'ua-browser'
122
-
123
145
  uaBrowser() // 自动读取 navigator.userAgent
124
146
  uaBrowser(customUA) // 传入自定义 UA,仍注入当前浏览器上下文
125
147
  ```
@@ -128,16 +150,18 @@ uaBrowser(customUA) // 传入自定义 UA,仍注入当前浏览器上下文
128
150
 
129
151
  ```typescript
130
152
  import {
131
- parseUA, // 纯函数,适合 SSR / Node.js
132
- getNavContext, // 读取当前浏览器 navigator 上下文
133
- getWindowsVersion, // 异步精确区分 Windows 10 / 11
134
- getLanguage, // 从 NavContext 获取浏览器语言
135
- isWebview, // 检测 Android Webview(UA 含 "; wv")
136
- isWechatMiniapp, // 检测微信小程序运行环境
137
- detectBot, // 独立爬虫检测
138
- detectArch, // 独立 CPU 架构检测
139
- detectHeadless, // 独立无头浏览器检测
140
- VERSION, // 当前版本号
153
+ parseUA, // 纯函数,适合 SSR / Node.js
154
+ getNavContext, // 读取当前浏览器 navigator 上下文
155
+ getWindowsVersion, // 异步精确区分 Windows 10 / 11
156
+ getLanguage, // 从 NavContext 获取浏览器语言
157
+ getEnvContext, // 采集所有浏览器环境信号(Client Hints、WebGL 等)
158
+ parseHeaders, // 从 HTTP 请求头解析 UA 及 Client Hints(SSR)
159
+ ACCEPT_CH, // 响应头常量,告知浏览器上报 Client Hints
160
+ isWebview, // 检测 Android Webview / iOS WKWebView
161
+ detectBot, // 独立爬虫检测
162
+ detectArch, // 独立 CPU 架构检测
163
+ detectHeadless, // 独立无头浏览器检测
164
+ VERSION, // 当前版本号
141
165
  } from 'ua-browser'
142
166
  ```
143
167
 
@@ -152,7 +176,7 @@ import {
152
176
  | `osVersion` | `string` | 系统版本 |
153
177
  | `device` | `DeviceName` | 设备类型:`Mobile` \| `Tablet` \| `TV` \| `PC` |
154
178
  | `arch` | `ArchName` | CPU 架构 |
155
- | `isWebview` | `boolean` | 是否为 Android Webview |
179
+ | `isWebview` | `boolean` | 是否为 Android Webview / iOS WKWebView |
156
180
  | `isHeadless` | `boolean` | 是否为无头 / 自动化浏览器 |
157
181
  | `isBot` | `boolean` | 是否为爬虫 / 机器人 |
158
182
  | `botName` | `BotName` | 爬虫名称 |
@@ -163,11 +187,11 @@ import {
163
187
 
164
188
  ## 支持范围
165
189
 
166
- 内置超过 60 种浏览器、17 种操作系统、19 种爬虫规则,详见 **[内置支持列表](https://yangtianxia.github.io/ua-browser/guide/support-list)**。
190
+ 内置超过 70 种浏览器、17 种操作系统、19 种爬虫规则,详见 **[内置支持列表](https://yangtianxia.github.io/ua-browser/guide/support-list)**。
167
191
 
168
192
  部分覆盖:
169
- - **浏览器** — Chrome、Safari、Firefox、Edge、Samsung Internet、UC、微信、钉钉、抖音等
170
- - **操作系统** — Windows、macOS、Android、iOS、HarmonyOS、Tizen、KaiOS 等
193
+ - **浏览器** — Chrome、Safari、Firefox、Edge、Samsung Internet、UC、微信、钉钉、抖音、哔哩哔哩、快手、小红书、飞书等
194
+ - **操作系统** — Windows、macOS、Android、iOS、HarmonyOS、OpenHarmony、Tizen、KaiOS 等
171
195
  - **AI 爬虫** — GPTBot、ClaudeBot、PerplexityBot、CCBot 等
172
196
  - **设备** — Mobile、Tablet、TV(含三星 Smart TV、HbbTV 标准)、PC
173
197
 
package/dist/index.cjs CHANGED
@@ -4,7 +4,7 @@ Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  // package.json
6
6
  var package_default = {
7
- version: "1.1.0"};
7
+ version: "1.3.0"};
8
8
 
9
9
  // src/constants/browsers.ts
10
10
  var BROWSER_DEFS = [
@@ -22,14 +22,9 @@ var BROWSER_DEFS = [
22
22
  { name: "Samsung Internet", priority: 92, detect: /SamsungBrowser/, versionPattern: /SamsungBrowser\/([\d.]+)/ },
23
23
  { name: "DuckDuckGo", priority: 94, detect: /DuckDuckGo\//, versionPattern: /DuckDuckGo\/([\d.]+)/ },
24
24
  { name: "Puffin", priority: 96, detect: /Puffin\//, versionPattern: /Puffin\/([\d.]+)/ },
25
- { name: "Arora", priority: 100, detect: /Arora/, versionPattern: /Arora\/([\d.]+)/ },
26
- { name: "Lunascape", priority: 110, detect: /Lunascape/, versionPattern: /Lunascape[\s]([\d.]+)/ },
27
- { name: "QupZilla", priority: 120, detect: /QupZilla/, versionPattern: /QupZilla[\s]([\d.]+)/ },
28
25
  { name: "Coc Coc", priority: 130, detect: /coc_coc_browser/, versionPattern: /coc_coc_browser\/([\d.]+)/ },
29
26
  { name: "Kindle", priority: 140, detect: /(Kindle|Silk\/)/, versionPattern: /Version\/([\d.]+)/ },
30
- { name: "Iceweasel", priority: 150, detect: /Iceweasel/, versionPattern: /Iceweasel\/([\d.]+)/ },
31
27
  { name: "Konqueror", priority: 160, detect: /Konqueror/, versionPattern: /Konqueror\/([\d.]+)/ },
32
- { name: "Iceape", priority: 170, detect: /Iceape/, versionPattern: /Iceape\/([\d.]+)/ },
33
28
  { name: "SeaMonkey", priority: 180, detect: /SeaMonkey/, versionPattern: /SeaMonkey\/([\d.]+)/ },
34
29
  { name: "Epiphany", priority: 190, detect: /Epiphany/, versionPattern: /Epiphany\/([\d.]+)/ },
35
30
  { name: "Maxthon", priority: 200, detect: /Maxthon/, versionPattern: /Maxthon\/([\d.]+)/ },
@@ -114,7 +109,14 @@ var BROWSER_DEFS = [
114
109
  { name: "Suning", priority: 560, detect: /SNEBUY-APP/, versionPattern: /SNEBUY-APP([\d.]+)/ },
115
110
  { name: "iQiYi", priority: 570, detect: /IqiyiApp/, versionPattern: /IqiyiVersion\/([\d.]+)/ },
116
111
  { name: "DingTalk", priority: 580, detect: /DingTalk/, versionPattern: /DingTalk\/([\d.]+)/ },
117
- { name: "Douyin", priority: 590, detect: /aweme/, versionPattern: /app_version\/([\d.]+)/ }
112
+ { name: "Douyin", priority: 590, detect: /aweme/, versionPattern: /app_version\/([\d.]+)/ },
113
+ { name: "Bilibili", priority: 592, detect: /BiliBili\//, versionPattern: /BiliBili\/([\d.]+)/ },
114
+ { name: "Kuaishou", priority: 594, detect: /Kwai\//, versionPattern: /Kwai\/([\d.]+)/ },
115
+ { name: "Xiaohongshu", priority: 596, detect: /Xiaohongshu\//, versionPattern: /Xiaohongshu\/([\d.]+)/ },
116
+ { name: "Feishu", priority: 597, detect: /(Lark|Feishu)\//, versionPattern: [/Lark\/([\d.]+)/, /Feishu\/([\d.]+)/] },
117
+ { name: "Toutiao", priority: 598, detect: /NewsArticle\//, versionPattern: /NewsArticle\/([\d.]+)/ },
118
+ { name: "JD", priority: 599, detect: /jdpingou\//, versionPattern: /jdpingou\/([\d.]+)/ },
119
+ { name: "Meituan", priority: 600, detect: /MeituanHybrid\//, versionPattern: /MeituanHybrid\/([\d.]+)/ }
118
120
  ];
119
121
 
120
122
  // src/utils/extract.ts
@@ -495,14 +497,6 @@ function parseUA(ua, options = {}) {
495
497
  } catch (e) {
496
498
  }
497
499
  }
498
- if (browser === "Wechat") {
499
- try {
500
- if (typeof __wxjs_environment !== "undefined" && __wxjs_environment === "miniprogram") {
501
- browser = "Wechat Miniapp";
502
- }
503
- } catch (e) {
504
- }
505
- }
506
500
  if (os === "iOS" && browser === "Safari") {
507
501
  const m = /Version\/([\d.]+)/.exec(ua);
508
502
  if (m && parseInt(m[1], 10) > parseInt(osVersion, 10)) {
@@ -701,19 +695,11 @@ function parseHeaders(headers) {
701
695
 
702
696
  // src/index.ts
703
697
  var { version: VERSION } = package_default;
704
- var isWechatMiniapp = () => {
705
- try {
706
- return typeof __wxjs_environment !== "undefined" && __wxjs_environment === "miniprogram";
707
- } catch (e) {
708
- return false;
709
- }
710
- };
711
698
  function uaBrowser(ua) {
712
699
  const nav = getNavContext();
713
700
  return parseUA(ua != null ? ua : nav.userAgent, { nav });
714
701
  }
715
702
  uaBrowser.isWebview = isWebview;
716
- uaBrowser.isWechatMiniapp = isWechatMiniapp;
717
703
  uaBrowser.getLanguage = () => getLanguage(getNavContext());
718
704
  uaBrowser.VERSION = VERSION;
719
705
  var src_default = uaBrowser;
@@ -729,7 +715,6 @@ exports.getLanguage = getLanguage;
729
715
  exports.getNavContext = getNavContext;
730
716
  exports.getWindowsVersion = getWindowsVersion;
731
717
  exports.isWebview = isWebview;
732
- exports.isWechatMiniapp = isWechatMiniapp;
733
718
  exports.parseHeaders = parseHeaders;
734
719
  exports.parseUA = parseUA;
735
720
  //# sourceMappingURL=index.cjs.map