Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add default agent #50

Merged
merged 3 commits into from
Apr 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,32 @@ Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)
* **< Windows 7**
* **< iOS Unknown**


```js
import getAgent from "@egjs/agent";

const agent = getAgent();

// Check iOS
agent.os.name === "ios"
agent.os.majorVersion === 9
// Check Android
agent.os.name === "android"
agent.os.majorVersion === 4
parseFloat(agent.os.version) <= 4.4
// Check browser name
agent.browser.name === "safari"
// Check webkit
agent.browser.webkit
// Check chromium
agent.browser.chromium
```

### If you want to use only the method using navigator.userAgent
```js
import { getLegacyAgent } from "@egjs/agent";

const agent = getLegacyAgent();
// Check iOS
agent.os.name === "ios"
agent.os.majorVersion === 9
Expand All @@ -100,7 +121,7 @@ agent.browser.webkit
agent.browser.chromium
```
### Not Possible (If synchronous)
* Check OS (Mac with browsers except Safari, Windows, Linux), OS version
* Check OS version.
* Check Browser full version.

```js
Expand Down
13 changes: 8 additions & 5 deletions src/agent.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { AgentInfo } from "./types";
import { hasUserAgentData } from "./utils";
import { parseUserAgentData } from "./userAgentData";
import { parseUserAgent } from "./userAgent";
import { getClientHintsAgent } from "./userAgentData";
import { getLegacyAgent } from "./userAgent";

/**
* @namespace eg.agent
*/
Expand Down Expand Up @@ -31,7 +32,7 @@ export function getAccurateAgent(callback?: (result: AgentInfo) => void): Promis
"platformVersion",
"uaFullVersion",
]).then(info => {
const agentInfo = parseUserAgentData(info);
const agentInfo = getClientHintsAgent(info);

callback && callback(agentInfo);
return agentInfo;
Expand All @@ -45,6 +46,7 @@ export function getAccurateAgent(callback?: (result: AgentInfo) => void): Promis
}



/**
* Extracts browser and operating system information from the user agent string.
* @ko 유저 에이전트 문자열에서 브라우저와 운영체제 정보를 추출한다.
Expand All @@ -58,10 +60,11 @@ const { os, browser, isMobile } = agent();
*/
function agent(userAgent?: string): AgentInfo {
if (typeof userAgent === "undefined" && hasUserAgentData()) {
return parseUserAgentData();
return getClientHintsAgent();
} else {
return parseUserAgent(userAgent);
return getLegacyAgent(userAgent);
}
}
export { getLegacyAgent };

export default agent;
6 changes: 3 additions & 3 deletions src/userAgent.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { AgentInfo } from "./types";
import { getUserAgent, findPreset } from "./utils";
import { getUserAgentString, findPreset } from "./utils";
import { WEBVIEW_PRESETS, CHROMIUM_PRESETS, BROWSER_PRESETS, OS_PRESETS, WEBKIT_PRESETS } from "./presets";

export function parseUserAgent(userAgent?: string): AgentInfo {
const nextAgent = getUserAgent(userAgent);
export function getLegacyAgent(userAgent?: string): AgentInfo {
const nextAgent = getUserAgentString(userAgent);
const isMobile = !!/mobi/g.exec(nextAgent);
const browser = {
name: "unknown",
Expand Down
2 changes: 1 addition & 1 deletion src/userAgentData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { UADataValues, AgentInfo, AgentBrowserInfo, AgentOSInfo } from "./types"
import { some, find, findBrand, convertVersion, findPresetBrand } from "./utils";
import { BROWSER_PRESETS, OS_PRESETS, CHROMIUM_PRESETS, WEBKIT_PRESETS, WEBVIEW_PRESETS } from "./presets";

export function parseUserAgentData(osData?: UADataValues): AgentInfo {
export function getClientHintsAgent(osData?: UADataValues): AgentInfo {
const userAgentData = navigator.userAgentData;
const brands = [...(userAgentData.uaList || userAgentData.brands)!];
const isMobile = userAgentData.mobile || false;
Expand Down
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export function find<T>(arr: T[], callback: (value: T, index: number) => any): T
}
return null;
}
export function getUserAgent(agent?: string): string {
export function getUserAgentString(agent?: string): string {
let userAgent = agent;
if (typeof userAgent === "undefined") {
if (typeof navigator === "undefined" || !navigator) {
Expand Down
29 changes: 27 additions & 2 deletions test/unit/userAgent.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import AGENT_LIST from "./userAgentConsts";
import getAgent, { getAccurateAgent } from "../../src/agent";
import getAgent, { getAccurateAgent, getLegacyAgent } from "../../src/agent";

describe("test userAgent", () => {
describe("test getAgent function with custom userAgent", () => {
AGENT_LIST.forEach(uaInfo => {
it(`test ${uaInfo.name} - ${uaInfo.ua}`, () => {
it(`test getAgent ${uaInfo.name} - ${uaInfo.ua}`, () => {
// Given, When
const agent = getAgent(uaInfo.ua);

Expand All @@ -27,6 +27,31 @@ describe("test userAgent", () => {
expect(agent.os.majorVersion).toBe(parseInt(uaInfo.os.version, 10));
expect(agent.browser.majorVersion).toBe(parseInt(uaInfo.browser.version, 10));

expect(agent.isMobile).toBe(uaInfo.isMobile);
});
it(`test getLegacyAgent ${uaInfo.name} - ${uaInfo.ua}`, () => {
// Given, When
const agent = getLegacyAgent(uaInfo.ua);

// Then
// name
expect(agent.os.name).toBe(uaInfo.os.name);
expect(agent.browser.name).toBe(uaInfo.browser.name);

// engine
expect(agent.browser.webview).toBe(uaInfo.browser.webview);
expect(agent.browser.webkit).toBe(uaInfo.browser.webkit);
expect(agent.browser.webkitVersion).toBe(uaInfo.browser.webkitVersion);
expect(agent.browser.chromium).toBe(uaInfo.browser.chromium);
expect(agent.browser.chromiumVersion).toBe(uaInfo.browser.chromiumVersion);

// version
expect(agent.browser.version).toBe(uaInfo.browser.version);
expect(agent.os.version).toBe(uaInfo.os.version);

expect(agent.os.majorVersion).toBe(parseInt(uaInfo.os.version, 10));
expect(agent.browser.majorVersion).toBe(parseInt(uaInfo.browser.version, 10));

expect(agent.isMobile).toBe(uaInfo.isMobile);
});
});
Expand Down