Skip to content

Commit

Permalink
switch user agent parser
Browse files Browse the repository at this point in the history
  • Loading branch information
romainmenke committed Dec 8, 2024
1 parent b3f3f0b commit d425a14
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 6 deletions.
4 changes: 3 additions & 1 deletion eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const ignores = [
"polyfills/__dist/**",
"tasks/**/node_modules",
"test/coverage/**",
"ua-parser/**/*",

// We ignore the polyfill.js files for third-party polyfills
// because we do not control their implementation.
Expand Down Expand Up @@ -89,9 +90,10 @@ export default [
},
{
files: [
"polyfills/**/update.task.js",
"tasks/**/*",
"test/**/*",
"polyfills/**/update.task.js",
"ua-parser/**/*",
],
ignores: [
"tasks/polyfill-templates/polyfill.js",
Expand Down
96 changes: 96 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"semver": "^7.3.7",
"toposort": "^2.0.2",
"typescript": "^5.6.2",
"ua-parser-js": "^2.0.0",
"uglify-js": "^3.17.4",
"webdriverio": "^9.1.4"
},
Expand Down
9 changes: 4 additions & 5 deletions test/polyfills/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ const promisify = require("node:util").promisify;
const readFile = promisify(fs.readFile);
const path = require("node:path");
const handlebars = require("handlebars");
const UA = require("@financial-times/polyfill-useragent-normaliser");
const normalizeUserAgent = UA.normalize;
const ua_parser = require('../../ua-parser/ua-parser');

process.title = "polyfill-library-test-server";

Expand Down Expand Up @@ -57,7 +56,7 @@ const cacheFor1Day = cache("1 day", () => true, {
request.query.always;
if (request.query.always === "no") {
const ua = request.get("User-Agent");
key += normalizeUserAgent(ua);
key += ua_parser(ua).normalize();
}
return key;
}
Expand Down Expand Up @@ -114,7 +113,7 @@ app.get(
),
minify: false,
stream: false,
ua: always === "yes" ? new UA("other/0.0.0") : new UA(request.get("user-agent"))
ua: ua_parser(always === "yes" ? "other/0.0.0" : request.get("user-agent"))
};

const bundle = await polyfillio.getPolyfillString(parameters);
Expand Down Expand Up @@ -246,7 +245,7 @@ function createEndpoint(template) {
}
let polyfills;
if (includePolyfills === 'yes' && always === 'no') {
polyfills = await testablePolyfills(normalizeUserAgent(ua));
polyfills = await testablePolyfills(ua_parser(ua).normalize());
} else {
polyfills = await testablePolyfills();
}
Expand Down
60 changes: 60 additions & 0 deletions ua-parser/ua-parser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
const semver = require('semver');
const uap = require('ua-parser-js');

/**
* @param {string} ua
* @returns {import('../lib/index').UaParser}
*/
function ua_parser(ua) {
const p = uap(ua);

return {
normalize: function normalize() {
return this.getFamily() + "/" + this.getVersion();
},
isUnknown: function isUnknown() {
return !p.browser?.name || !p.browser?.version || this.getFamily() === 'other';
},
getVersion: function getVersion() {
if (this.isUnknown()) return '0.0.0';

if (this.getFamily() === 'ios_saf' && p.os.version) return semver.coerce(p.os.version).toString();

return semver.coerce(p.browser.version).toString();
},
getFamily: function getFamily() {
switch (p.browser?.name) {
case "Android Browser":
return "android"
case "Chrome":
case "Chrome Mobile":
case "Chrome Headless":
case "Chrome WebView":
case "Chromium":
return "chrome"
case "Edge":
return "edge";
case "Firefox":
return "firefox";
case "Firefox Mobile":
return "firefox_mob";
case "IE":
return "ie";
case "IEMobile":
return "ie_mob";
case "Safari":
return "safari";
case "Mobile Safari":
case "Safari Mobile":
return "ios_saf";
default:
return "other";
}
},
satisfies: function satisfies(range) {
return semver.satisfies(this.getVersion(), range);
}
}
}

module.exports = ua_parser;

0 comments on commit d425a14

Please sign in to comment.