From 92b33fde15829380b7ec1dc55d0944d96e24cc69 Mon Sep 17 00:00:00 2001 From: Daniel Climent Date: Mon, 3 Oct 2022 16:58:55 +0200 Subject: [PATCH] More details about the system in the userAgent --- src/index.js | 3 ++- src/user_agent.js | 56 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 src/user_agent.js diff --git a/src/index.js b/src/index.js index a5788f0..2575533 100644 --- a/src/index.js +++ b/src/index.js @@ -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) { @@ -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, diff --git a/src/user_agent.js b/src/user_agent.js new file mode 100644 index 0000000..6b1a2e3 --- /dev/null +++ b/src/user_agent.js @@ -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}` +}