Skip to content

Commit

Permalink
fix: change server info (#9)
Browse files Browse the repository at this point in the history
* fix: change server info

* chore: update

* chore: remove version from server info (included todo)
  • Loading branch information
userquin committed Oct 13, 2023
1 parent b3dc5a8 commit b0b0673
Show file tree
Hide file tree
Showing 9 changed files with 182 additions and 133 deletions.
10 changes: 4 additions & 6 deletions README.md
Expand Up @@ -30,16 +30,14 @@ import { detect } from 'detect-browser-es'
const { detect } = require('detect-browser-es')
```

## Breaking Changes
## Deprecations

**TODO**: maybe we can revert these changes before releasing the first version.

[NodeInfo](https://github.com/DamonOehlman/detect-browser/blob/master/src/index.ts#L30) and [getNodeVersion](https://github.com/DamonOehlman/detect-browser/blob/master/src/index.ts#L306C17-L306C31) have been renamed to [ServerInfo](https://github.com/userquin/detect-browser-es/blob/main/src/index.ts#L47) and [getServerVersion](https://github.com/userquin/detect-browser-es/blob/main/src/index.ts#L366) respectively.
[NodeInfo](https://github.com/DamonOehlman/detect-browser/blob/master/src/index.ts#L30) and [getNodeVersion](https://github.com/DamonOehlman/detect-browser/blob/master/src/index.ts#L306C17-L306C31) have been deprecated and replaced with [ServerInfo](https://github.com/userquin/detect-browser-es/blob/main/src/index.ts#L47) and [getServerVersion](https://github.com/userquin/detect-browser-es/blob/main/src/index.ts#L366) respectively.

## New Features

- Detect [Happy DOM](https://github.com/capricorn86/happy-dom) and [jsdom](https://github.com/jsdom/jsdom) when using test environments like [Vitest](https://github.com/vitest-dev/vitest) (check the [test](https://github.com/userquin/detect-browser-es/tree/main/test) folder).
- Detect [WebdriverIO](https://github.com/webdriverio/webdriverio) when using WebdriverIO tests or test environments like [Vitest](https://github.com/vitest-dev/vitest) with `@vitest/browser`.
- Detect [WebdriverIO](https://github.com/webdriverio/webdriverio) when using WebdriverIO tests.
- ServerInfo via [std-env](https://github.com/unjs/std-env) with [provider](https://github.com/unjs/std-env#provider-detection) and [runtime](https://github.com/unjs/std-env#runtime-detection) detection.

## Testing
Expand All @@ -48,7 +46,7 @@ To run the tests, from root folder run `nr test`, the script will run:
- the original tests from `detect-browser`
- Happy DOM and jsdom tests, except WebdriverIO detection

To test WebdriverIO detection, run one of the following commands (requires Vitest v1.0.0-beta.2, not yet released, tests will not work):
To test WebdriverIO detection, run one of the following commands:
- `nr wdio-chrome`: Chrome must be installed
- `nr wdio-edge`: Edge must be installed
- `nr wdio-firefox`: Firefox must be installed
Expand Down
4 changes: 4 additions & 0 deletions browser-test/browser.test.ts
Expand Up @@ -10,6 +10,10 @@ describe('Browser Detection test', () => {
expect(typeof navigator).toBeDefined()
expect(typeof navigator?.userAgent).toBeDefined()
})
// TODO: missing __wdioSpec__ and cookie
test.skip('WebdriverIO Detection', () => {
expect(detect()?.type).toBe('webdriverio')
})
test.skipIf(browser !== 'chrome')('Chrome', () => {
expect(detect()?.name).toBe('chrome')
})
Expand Down
7 changes: 3 additions & 4 deletions package.json
Expand Up @@ -37,8 +37,7 @@
"wdio-chrome": "BROWSER=chrome vitest --c vitest-browser.config.mts",
"wdio-edge": "BROWSER=edge vitest --c vitest-browser.config.mts",
"wdio-firefox": "BROWSER=firefox vitest --c vitest-browser.config.mts",
"wdio-safari": "BROWSER=safari vitest --c vitest-browser.config.mts",
"wdio-test": "pnpm run wdio-chrome && pnpm run wdio-edge && pnpm run wdio-firefox"
"wdio-safari": "BROWSER=safari vitest --c vitest-browser.config.mts"
},
"dependencies": {
"std-env": "^3.4.3"
Expand All @@ -48,7 +47,7 @@
"@antfu/ni": "^0.21.8",
"@types/node": "^18.18.4",
"@typescript-eslint/eslint-plugin": "^6.6.0",
"@vitest/browser": "^1.0.0-beta.1",
"@vitest/browser": "^1.0.0-beta.2",
"@vitest/coverage-v8": "^0.34.6",
"@vitest/utils": "^0.34.6",
"bumpp": "^9.2.0",
Expand All @@ -57,7 +56,7 @@
"jsdom": "^22.1.0",
"typescript": "^5.2.2",
"unbuild": "^1.2.1",
"vitest": "^1.0.0-beta.1",
"vitest": "^1.0.0-beta.2",
"webdriverio": "^8.18.0"
}
}
68 changes: 34 additions & 34 deletions pnpm-lock.yaml

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

63 changes: 51 additions & 12 deletions src/index.ts
@@ -1,22 +1,25 @@
import {
type ProviderInfo,
type RuntimeInfo,
nodeMajorVersion,
nodeVersion,
platform,
providerInfo,
runtimeInfo,
runtime, runtimeInfo,
} from 'std-env'
import type {
ProviderInfo,
RuntimeInfo,
RuntimeName,
} from 'std-env'

export type DetectedInfoType =
| 'browser'
| 'node'
| 'jsdom'
| 'happy-dom'
| 'webdriverio'
| 'bot-device'
| 'bot'
| 'react-native'
| RuntimeName

interface DetectedInfo<
T extends DetectedInfoType, N extends string, O, V = null,
Expand Down Expand Up @@ -45,17 +48,35 @@ implements DetectedInfo<'browser', Browser, OperatingSystem | null, string> {
) {}
}

export class ServerInfo
/**
* @deprecated Use `ServerInfo` instead
*/
export class NodeInfo
implements DetectedInfo<'node', 'node', NodeJS.Platform, string> {
public readonly type = 'node'
public readonly name: 'node' = 'node' as const
public readonly os: NodeJS.Platform = platform

constructor(public readonly version: string) {}
}

// TODO: include version if possible
export class ServerInfo
implements DetectedInfo<RuntimeName, RuntimeName, ProviderInfo/* , string */> {
public readonly nodeVersion: string | null = nodeVersion
public readonly nodeMajorVersion: number | null = nodeMajorVersion
public readonly provider: ProviderInfo | undefined = providerInfo
public readonly runtime: RuntimeInfo | undefined = runtimeInfo
// TODO: include version if possible
public readonly version: null = null

constructor(public readonly version: string) {}
constructor(
public readonly name: RuntimeName,
public readonly type: RuntimeName,
public readonly os: ProviderInfo,
// TODO: include version if possible
// public readonly version: string,
) {}
}

export class SearchBotDeviceInfo
Expand All @@ -70,7 +91,7 @@ implements
) {}
}

export class BotInfo implements DetectedInfo<'bot', 'bot', null, null> {
export class BotInfo implements DetectedInfo<'bot', 'bot', null> {
public readonly type = 'bot'
public readonly bot: true = true as const // NOTE: deprecated test name instead
public readonly name: 'bot' = 'bot' as const
Expand All @@ -79,7 +100,7 @@ export class BotInfo implements DetectedInfo<'bot', 'bot', null, null> {
}

export class ReactNativeInfo
implements DetectedInfo<'react-native', 'react-native', null, null> {
implements DetectedInfo<'react-native', 'react-native', null> {
public readonly type = 'react-native'
public readonly name: 'react-native' = 'react-native' as const
public readonly version: null = null
Expand All @@ -106,7 +127,7 @@ implements DetectedInfo<'happy-dom', Browser, OperatingSystem | null, string> {
) {}
}

export class WebDriverIOInfo
export class WebdriverIOInfo
implements DetectedInfo<'webdriverio', Browser, OperatingSystem | null, string> {
public readonly type = 'webdriverio'
constructor(
Expand Down Expand Up @@ -149,7 +170,6 @@ export type Browser =
| 'searchbot'
| 'jsdom'
| 'happy-dom'
| 'webdriverio'
export type OperatingSystem =
| 'iOS'
| 'Android OS'
Expand Down Expand Up @@ -290,7 +310,7 @@ export function detect(userAgent?: string) {
if ('__wdioSpec__' in window) {
const browser = parseUserAgent(navigator.userAgent)
if (browser instanceof BrowserInfo)
return new WebDriverIOInfo(browser.name, browser.version, browser.os)
return new WebdriverIOInfo(browser.name, browser.version, browser.os)
}
}

Expand Down Expand Up @@ -371,11 +391,30 @@ export function detectOS(ua: string) {
return null
}

/**
* @deprecated Use `getServerVersion` instead
*/
export function getNodeVersion() {
if (runtime !== 'node' || !nodeVersion)
return null

return new NodeInfo(nodeVersion)
}

export function getServerVersion() {
return nodeVersion ? new ServerInfo(`${nodeVersion}`) : null
// TODO: how to extract version?
return runtimeInfo
? new ServerInfo(
runtimeInfo.name,
runtimeInfo.name,
providerInfo,
// TODO: include version if possible
)
: null
}

function createVersionParts(count: number) {
// return Array.from({ length: count }, () => '0')
const output: string[] = []
for (let ii = 0; ii < count; ii++)
output.push('0')
Expand Down

0 comments on commit b0b0673

Please sign in to comment.