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