| b69ab31 | | | 1 | /** |
| b69ab31 | | | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| b69ab31 | | | 3 | * |
| b69ab31 | | | 4 | * This source code is licensed under the MIT license found in the |
| b69ab31 | | | 5 | * LICENSE file in the root directory of this source tree. |
| b69ab31 | | | 6 | */ |
| b69ab31 | | | 7 | |
| b69ab31 | | | 8 | import type {ApplicationInfo} from './types'; |
| b69ab31 | | | 9 | |
| b69ab31 | | | 10 | import os from 'node:os'; |
| b69ab31 | | | 11 | import {nullthrows, randomId} from 'shared/utils'; |
| b69ab31 | | | 12 | |
| b69ab31 | | | 13 | export function getUsername(): string { |
| b69ab31 | | | 14 | try { |
| b69ab31 | | | 15 | return os.userInfo().username; |
| b69ab31 | | | 16 | } catch (osInfoError) { |
| b69ab31 | | | 17 | try { |
| b69ab31 | | | 18 | const {env} = process; |
| b69ab31 | | | 19 | return nullthrows(env.LOGNAME || env.USER || env.LNAME || env.USERNAME); |
| b69ab31 | | | 20 | } catch (processEnvError) { |
| b69ab31 | | | 21 | throw new Error(String(processEnvError) + String(osInfoError)); |
| b69ab31 | | | 22 | } |
| b69ab31 | | | 23 | } |
| b69ab31 | | | 24 | } |
| b69ab31 | | | 25 | |
| b69ab31 | | | 26 | export function generateAnalyticsInfo( |
| b69ab31 | | | 27 | platformName: string, |
| b69ab31 | | | 28 | version: string, |
| b69ab31 | | | 29 | sessionId?: string, |
| b69ab31 | | | 30 | ): ApplicationInfo { |
| b69ab31 | | | 31 | return { |
| b69ab31 | | | 32 | platform: platformName, |
| b69ab31 | | | 33 | version, |
| b69ab31 | | | 34 | repo: undefined, |
| b69ab31 | | | 35 | /** |
| b69ab31 | | | 36 | * Random id for this ISL session, created at startup. |
| b69ab31 | | | 37 | * Note: this is only generated on the server, so client-logged events share the ID with the server. |
| b69ab31 | | | 38 | * May be manually specified instead of randomly created, e.g. if your platform has a well-defined session ID already. |
| b69ab31 | | | 39 | */ |
| b69ab31 | | | 40 | sessionId: sessionId ?? randomId(), |
| b69ab31 | | | 41 | unixname: getUsername(), |
| b69ab31 | | | 42 | osArch: os.arch(), |
| b69ab31 | | | 43 | osType: os.platform(), |
| b69ab31 | | | 44 | osRelease: os.release(), |
| b69ab31 | | | 45 | hostname: os.hostname(), |
| b69ab31 | | | 46 | }; |
| b69ab31 | | | 47 | } |