-
-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathua-parser.ts
More file actions
215 lines (191 loc) · 6.83 KB
/
ua-parser.ts
File metadata and controls
215 lines (191 loc) · 6.83 KB
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
//
// mdn-bcd-collector: lib/ua-parser.ts
// Module to parse user agent strings and compare them with BCD browser data
//
// © Gooborg Studios, Google LLC
// See the LICENSE file for copyright details
//
import {Browsers} from "@mdn/browser-compat-data";
import {
compare as compareVersions,
compareVersions as compareVersionsSort,
} from "compare-versions";
import {UAParser} from "ua-parser-js";
import type {ParsedUserAgent} from "../types/types.d.ts";
import {RUNTIME_IDS_WITH_PATCH_VERSIONING} from "./constants.js";
/**
* Returns the major version from a given version string.
* @param version - The version string.
* @returns The major version.
*/
const getMajorVersion = (version: string): string => {
return version.split(".")[0];
};
/**
* Returns the major and minor version of a given version string.
* If the version string does not contain a minor version, it defaults to 0.
* @param version - The version string.
* @returns The major and minor version.
*/
const getMajorMinorVersion = (version: string): string => {
const [major, minor] = version.split(".");
return `${major}.${minor || 0}`;
};
/**
* Parses the user agent string and extracts relevant information about the browser and operating system.
* @param userAgent - The user agent string.
* @param browsers - An object containing browser data.
* @returns An object containing the parsed browser and operating system information.
*/
const parseUA = (userAgent: string, browsers: Browsers): ParsedUserAgent => {
const ua = UAParser(userAgent);
const data: ParsedUserAgent = {
browser: {id: "", name: ""},
version: "",
fullVersion: "",
os: {name: "", version: ""},
inBcd: undefined,
};
if (userAgent.startsWith("!! ")) {
// UA strings in unjs/runtime-compat are prepended with this string to prevent incorrect parsing by standard UA libs
const [runtime, runtimeVersion] = userAgent.replace("!! ", "").split("/");
data.browser.id = runtime;
data.fullVersion = runtimeVersion;
} else if (userAgent.includes("Servo/")) {
// Servo browser detection
const servoMatch = userAgent.match(/Servo\/([\d.]+)/);
if (servoMatch) {
data.browser.id = "servo";
data.browser.name = "Servo";
data.fullVersion = servoMatch[1];
}
data.os.name = ua.os.name || "";
data.os.version = ua.os.version || "";
} else {
if (!ua.browser.name) {
return data;
}
data.browser.id = ua.browser.name.toLowerCase().replace(/ /g, "_");
data.browser.name = ua.browser.name;
data.os.name = ua.os.name || "";
data.os.version = ua.os.version || "";
}
data.browser.id = data.browser.id.replace("mobile_", "");
data.browser.name = data.browser.name.replace("Mobile ", "");
switch (data.browser.id) {
case "oculus_browser":
data.browser.id = "oculus";
break;
case "samsung_internet":
data.browser.id = "samsunginternet";
break;
case "android_browser":
case "chrome_webview":
data.browser.id = "webview";
break;
case "node":
data.browser.id = "nodejs";
break;
}
const os = data.os.name.toLowerCase();
if (os === "android" && data.browser.id !== "oculus") {
data.browser.id += "_android";
data.browser.name += " Android";
if (ua.browser.name === "Android Browser") {
// For early WebView Android, use the OS version
data.fullVersion =
(compareVersions(ua.os.version || "0", "5.0", "<")
? ua.os.version
: ua.engine.version) || "0";
}
} else if (os === "ios") {
if (data.browser.id === "webkit") {
data.browser = {id: "webview", name: "WebView"};
}
data.browser.id += "_ios";
data.browser.name += " iOS";
// https://github.com/mdn/browser-compat-data/blob/main/docs/data-guidelines.md#safari-for-ios-versioning
data.fullVersion = ua.os.version || "0";
}
data.fullVersion = data.fullVersion || ua.browser.version || "0";
if (RUNTIME_IDS_WITH_PATCH_VERSIONING.has(data.browser.id)) {
data.version = data.fullVersion;
} else {
data.version = getMajorMinorVersion(data.fullVersion);
}
if (!(data.browser.id in browsers)) {
return data;
}
data.browser.name = browsers[data.browser.id].name;
data.inBcd = false;
const versions = Object.keys(browsers[data.browser.id].releases);
versions.sort(compareVersionsSort);
// Android 4.4.3 needs to be handled as a special case, because its data
// differs from 4.4, and the code below will strip out the patch versions from
// our version numbers.
if (
data.browser.id === "webview_android" &&
compareVersions(data.fullVersion, "4.4.3", ">=") &&
compareVersions(data.fullVersion, "5.0", "<")
) {
data.version = "4.4.3";
data.inBcd = true;
return data;
}
// Certain Safari versions are backports of newer versions, but contain fewer
// features, particularly ones involving OS integration. We are explicitly
// marking these versions as "not in BCD" to avoid confusion.
if (
data.browser.id === "safari" &&
["4.1", "6.1", "6.2", "7.1"].includes(data.version)
) {
return data;
}
// The |version| from the UA string is typically more precise than |versions|
// from BCD, and some "uninteresting" releases are missing from BCD. To deal
// with this, find the pair of versions in |versions| that sandwiches
// |version|, and use the first of this pair. For example, given |version|
// "10.1" and |versions| entries "10.0" and "10.2", return "10.0".
// However, for Bun, we need exact version matches because patch versions can add features.
if (RUNTIME_IDS_WITH_PATCH_VERSIONING.has(data.browser.id)) {
// For Bun, only mark as inBcd if exact version exists
if (versions.includes(data.version)) {
data.inBcd = true;
}
} else {
for (let i = 0; i < versions.length - 1; i++) {
const current = versions[i];
const next = versions[i + 1];
if (
compareVersions(data.version, current, ">=") &&
compareVersions(data.version, next, "<")
) {
data.inBcd = true;
data.version = current;
break;
}
}
}
// We reached the last entry in |versions|. With no |next| to compare against
// we have to check if it looks like a significant release or not. By default
// that means a new major version, but for Safari and Samsung Internet the
// major and minor version are significant.
if (!RUNTIME_IDS_WITH_PATCH_VERSIONING.has(data.browser.id)) {
let normalize = getMajorVersion;
if (
data.browser.id.startsWith("safari") ||
data.browser.id === "samsunginternet_android"
) {
normalize = getMajorMinorVersion;
}
if (
data.inBcd == false &&
normalize(data.version) === normalize(versions[versions.length - 1])
) {
data.inBcd = true;
data.version = versions[versions.length - 1];
}
}
return data;
};
export {getMajorMinorVersion, parseUA};