Skip to content

Commit

Permalink
feat: upgrade ua-parser to v2
Browse files Browse the repository at this point in the history
  • Loading branch information
zdm committed Nov 20, 2024
1 parent fc59e71 commit fc1b0d6
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions lib/user-agent.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { UAParser } from "ua-parser-js";
import * as extensions from "ua-parser-js/extensions";
import * as defaultExtensions from "ua-parser-js/extensions";
import { isAIBot, isBot, isChromeFamily } from "ua-parser-js/helpers";

export default class UserAgent {
Expand All @@ -12,16 +12,29 @@ export default class UserAgent {
#isAiBot;
#isChromeFamily;

constructor ( ua ) {
constructor ( ua, { extensions = true, withClientHints } = {} ) {
if ( extensions === true ) {
extensions = defaultExtensions;
}

// from string
if ( !ua || typeof ua === "string" ) {
this.#ua = UAParser( ua, extensions );
}

// from headers
else if ( ua[ "user-agent" ] ) {
this.#ua = UAParser( extensions, ua ).withClientHints();
this.#ua = UAParser( extensions, ua );
}

// from json
else {
this.#ua = UAParser( ua.ua, extensions );
}

if ( withClientHints && !this.#ua.browser.type ) {
this.#ua = this.#ua.withClientHints();
}
}

// static
Expand Down

0 comments on commit fc1b0d6

Please sign in to comment.