Open History to see the last commit
1import * as Context from "effect/Context";
2import * as Effect from "effect/Effect";
3import * as NodeDns from "node:dns";
4import * as NodeOS from "node:os";
5import * as NodeSea from "node:sea";
6
7export const Platform = Context.Reference<NodeJS.Platform>("@t3tools/shared/HostProcess/Platform", {
8 defaultValue: () => process.platform,
9});
10
11export const Architecture = Context.Reference<NodeJS.Architecture>(
12 "@t3tools/shared/HostProcess/Architecture",
13 {
14 defaultValue: () => process.arch,
15 },
16);
17
18export const Hostname = Context.Reference<string>("@t3tools/shared/HostProcess/Hostname", {
19 defaultValue: () => NodeOS.hostname(),
20});
21
22export const Username = Context.Reference<string | null>("@t3tools/shared/HostProcess/Username", {
23 defaultValue: () => {
24 try {
25 return NodeOS.userInfo().username.trim() || null;
26 } catch {
27 return null;
28 }
29 },
30});
31
32export const HomeDirectory = Context.Reference<string>(
33 "@t3tools/shared/HostProcess/HomeDirectory",
34 {
35 defaultValue: () => NodeOS.homedir(),
36 },
37);
38
39export const Environment = Context.Reference<NodeJS.ProcessEnv>(
40 "@t3tools/shared/HostProcess/Environment",
41 {
42 defaultValue: () => process.env,
43 },
44);
45
46export const WorkingDirectory = Context.Reference<string>(
47 "@t3tools/shared/HostProcess/WorkingDirectory",
48 {
49 defaultValue: () => process.cwd(),
50 },
51);
52
53export const ExecutablePath = Context.Reference<string>(
54 "@t3tools/shared/HostProcess/ExecutablePath",
55 {
56 defaultValue: () => process.execPath,
57 },
58);
59
60export const Arguments = Context.Reference<ReadonlyArray<string>>(
61 "@t3tools/shared/HostProcess/Arguments",
62 {
63 defaultValue: () => process.argv,
64 },
65);
66
67/**
68 * The command the shell was given, before Node resolved it to the binary:
69 * `t3` for a PATH lookup, `./t3` or the launcher symlink for an explicit
70 * path. `process.argv[0]` and `execPath` are always the resolved binary.
71 */
72export const InvokedAs = Context.Reference<string>("@t3tools/shared/HostProcess/InvokedAs", {
73 defaultValue: () => process.argv0,
74});
75
76export const IsExecutable = Context.Reference<boolean>("@t3tools/shared/HostProcess/IsExecutable", {
77 defaultValue: () => NodeSea.isSea(),
78});
79
80/**
81 * Every IP address this machine answers to: the interface addresses, plus
82 * whatever the resolver returns for the machine's own hostname. The latter
83 * matters because a hostname can map to an address no interface carries —
84 * Debian-style hosts put `127.0.1.1` in `/etc/hosts` — and a program that
85 * records "its" address by resolving its hostname (Firefox's profile lock does)
86 * will write that one.
87 */
88export const Addresses = Context.Reference<Effect.Effect<ReadonlySet<string>>>(
89 "@t3tools/shared/HostProcess/Addresses",
90 {
91 defaultValue: () =>
92 Effect.gen(function* () {
93 const interfaces = Object.values(NodeOS.networkInterfaces())
94 .flat()
95 .flatMap((entry) => (entry ? [entry.address] : []));
96 const resolved = yield* Effect.tryPromise(() =>
97 NodeDns.promises.lookup(NodeOS.hostname(), { all: true }),
98 ).pipe(
99 Effect.map((entries) => entries.map((entry) => entry.address)),
100 Effect.orElseSucceed(() => [] as ReadonlyArray<string>),
101 );
102 return new Set([...interfaces, ...resolved]);
103 }),
104 },
105);
106
107/** Undefined on platforms without POSIX uids (Windows). */
108export const UserId = Context.Reference<number | undefined>("@t3tools/shared/HostProcess/UserId", {
109 defaultValue: () => process.getuid?.(),
110});
111
112export const isWindows = Effect.map(Platform, (platform) => platform === "win32");