Skip to content

Commit

Permalink
Improved user-agent parser (added support for new headers Sec-CH-UA).
Browse files Browse the repository at this point in the history
  • Loading branch information
petersirka committed Oct 14, 2024
1 parent 1f3ed69 commit 8857147
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- fixed partial validation
- fix image bug in the `Image.measureWEBP()` method by [Marek Mraz](https://github.com/Mrazbb)
- improved Markdown parser
- improved user-agent parser (added support for new headers `Sec-CH-UA`)

========================
0.0.97
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17343,7 +17343,7 @@ function extend_request(PROTO) {
Object.defineProperty(PROTO, 'ua', {
get: function() {
if (this.$ua === undefined)
this.$ua = (this.headers['user-agent'] || '').parseUA();
this.$ua = U.parseUA(this.headers);
return this.$ua;
}
});
Expand Down
16 changes: 16 additions & 0 deletions utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -1685,6 +1685,22 @@ exports.isStaticFile = function(url) {
return index !== -1;
};

exports.parseUA = function(headers, structured) {
let ua = headers['sec-ch-ua'];
if (ua) {
let platform = headers['sec-ch-ua-platform'] || '';
let mobile = headers['sec-ch-ua-mobile'] === '?1';
let index = ua.indexOf('";v');
let browser = ua.substring(1, index);
if (platform)
platform = platform.substring(1, platform.length - 1);
return structured ? { os: platform, browser: browser, device: mobile ? 'mobile' : 'desktop' } : ((platform ? (platform + ' ') : '') + browser + (mobile ? ' Mobile' : ''));
} else {
ua = (headers['user-agent'] || '');
return ua ? ua.parseUA(structured) : ua;
}
};

exports.parseInt = function(obj, def) {
if (obj == null || obj === '')
return def === undefined ? 0 : def;
Expand Down

0 comments on commit 8857147

Please sign in to comment.