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