Skip to content
This repository was archived by the owner on Oct 10, 2022. It is now read-only.

chore: Add more system details to userAgent #738

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import pWaitFor from 'p-wait-for'
import { getMethods } from './methods/index.js'
import { openApiSpec } from './open_api.js'
import { getOperations } from './operations.js'
import { getUserAgent } from './user_agent.js'

export class NetlifyAPI {
constructor(firstArg, secondArg) {
Expand All @@ -11,7 +12,7 @@ export class NetlifyAPI {

// default opts
const {
userAgent = 'netlify/js-client',
userAgent = getUserAgent(),
scheme = openApiSpec.schemes[0],
host = openApiSpec.host,
pathPrefix = openApiSpec.basePath,
Expand Down
56 changes: 56 additions & 0 deletions src/user_agent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { process } from 'process'

import { version } from '../package.json'

const isNode = function () {
return typeof process === 'object'
}

const getBrowserName = function () {
const { userAgent } = navigator
let browserName = 'Unkown Browser'

if (/chrome|chromium|crios/i.test(userAgent)) browserName = 'chrome'
if (/firefox|fxios/i.test(userAgent)) browserName = 'firefox'
if (/safari/i.test(userAgent)) browserName = 'safari'
if (/opr/i.test(userAgent)) browserName = 'opera'
if (/edg/i.test(userAgent)) browserName = 'edge'

return browserName
}

const getBrowserPlatormData = function () {
const browserName = getBrowserName()
const { appVersion } = navigator

let OSName = 'Unknown OS'

if (appVersion.includes('Win')) OSName = 'Windows'
if (appVersion.includes('Mac')) OSName = 'MacOS'
if (appVersion.includes('X11')) OSName = 'UNIX'
if (appVersion.includes('Linux')) OSName = 'Linux'

return { os: OSName, runtime: browserName, runtimeVersion: appVersion }
}

const getNodePlatformData = function () {
const { platform } = process
const {
versions: { node: runtimeVersion },
} = process

let OSName = platform

if (platform === 'darwin') OSName = 'MacOS'
if (platform.includes('win')) OSName = 'Windows'

return { os: OSName, runtime: 'nodeJS', runtimeVersion }
}

export const getUserAgent = function () {
const packageVersion = version
const platformData = isNode() ? getNodePlatformData() : getBrowserPlatormData()
const { os, runtime, runtimeVersion } = platformData

return `netlify/js-client ${packageVersion}; ${runtime} version ${runtimeVersion}; OS: ${os}`
}