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 cf958c0 commit ebba850
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
2 changes: 2 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
0.0.7
========================

- improved user-agent parser (added support for new headers `Sec-CH-UA`)

========================
0.0.6
========================
Expand Down
3 changes: 1 addition & 2 deletions controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,7 @@ Controller.prototype = {
get ua() {
if (this.$ua != null)
return this.$ua;
let ua = this.headers['user-agent'];
this.$ua = ua ? ua.parseUA() : '';
this.$ua = F.TUtils.parseUA(this.headers);
return this.$ua;
},

Expand Down
14 changes: 14 additions & 0 deletions utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -1584,6 +1584,20 @@ exports.filestreamer = function(filename, onbuffer, onend, size) {

};

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);
return structured ? { os: platform, browser: browser, device: mobile ? 'mobile' : 'desktop' } : (platfrom + ' ' + browser + (mobile ? ' Mobile' : '')).trim();
} else {
ua = (headers['user-agent'] || '');
return ua ? ua.parseUA() : ua;
}
};

exports.parseInt = function(obj, def) {
if (obj == null || obj === '')
return def === undefined ? 0 : def;
Expand Down
3 changes: 1 addition & 2 deletions websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ Controller.prototype = {
get ua() {
if (this.$ua != null)
return this.$ua;
let ua = this.headers['user-agent'];
this.$ua = ua ? ua.parseUA() : '';
this.$ua = F.TUtils.parseUA(this.headers);
return this.$ua;
},

Expand Down

0 comments on commit ebba850

Please sign in to comment.