UNPKG

2.44 kBJavaScriptView Raw
1import process from 'process';
2import { isNode, getNodeVersion, isReactNative, getWindow } from './utils.js';
3function getBrowserOS() {
4 const win = getWindow();
5 if (!win) {
6 return null;
7 }
8 const userAgent = win.navigator.userAgent;
9 // TODO: platform is deprecated.
10 const platform = win.navigator.platform;
11 const macosPlatforms = ['Macintosh', 'MacIntel', 'MacPPC', 'Mac68K'];
12 const windowsPlatforms = ['Win32', 'Win64', 'Windows', 'WinCE'];
13 const iosPlatforms = ['iPhone', 'iPad', 'iPod'];
14 if (macosPlatforms.indexOf(platform) !== -1) {
15 return 'macOS';
16 }
17 else if (iosPlatforms.indexOf(platform) !== -1) {
18 return 'iOS';
19 }
20 else if (windowsPlatforms.indexOf(platform) !== -1) {
21 return 'Windows';
22 }
23 else if (/Android/.test(userAgent)) {
24 return 'Android';
25 }
26 else if (/Linux/.test(platform)) {
27 return 'Linux';
28 }
29 return null;
30}
31function getNodeOS() {
32 const platform = process.platform || 'linux';
33 const version = process.version || '0.0.0';
34 const platformMap = {
35 android: 'Android',
36 aix: 'Linux',
37 darwin: 'macOS',
38 freebsd: 'Linux',
39 linux: 'Linux',
40 openbsd: 'Linux',
41 sunos: 'Linux',
42 win32: 'Windows',
43 };
44 if (platform in platformMap) {
45 return `${platformMap[platform] || 'Linux'}/${version}`;
46 }
47 return null;
48}
49export default function getUserAgentHeader(sdk, application, integration, feature) {
50 const headerParts = [];
51 if (application) {
52 headerParts.push(`app ${application}`);
53 }
54 if (integration) {
55 headerParts.push(`integration ${integration}`);
56 }
57 if (feature) {
58 headerParts.push('feature ' + feature);
59 }
60 headerParts.push(`sdk ${sdk}`);
61 let platform = null;
62 try {
63 if (isReactNative()) {
64 platform = getBrowserOS();
65 headerParts.push('platform ReactNative');
66 }
67 else if (isNode()) {
68 platform = getNodeOS();
69 headerParts.push(`platform node.js/${getNodeVersion()}`);
70 }
71 else {
72 platform = getBrowserOS();
73 headerParts.push('platform browser');
74 }
75 }
76 catch (e) {
77 platform = null;
78 }
79 if (platform) {
80 headerParts.push(`os ${platform}`);
81 }
82 return `${headerParts.filter((item) => item !== '').join('; ')};`;
83}