Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

hostProcess.ts

118 lines3.8 KB

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 HostProcessPlatform = Context.Reference<NodeJS.Platform>(
8 "@t3tools/shared/hostProcess/HostProcessPlatform",
9 {
10 defaultValue: () => process.platform,
11 },
12);
13​
14export const HostProcessArchitecture = Context.Reference<NodeJS.Architecture>(
15 "@t3tools/shared/hostProcess/HostProcessArchitecture",
16 {
17 defaultValue: () => process.arch,
18 },
19);
20​
21export const HostProcessHostname = Context.Reference<string>(
22 "@t3tools/shared/hostProcess/HostProcessHostname",
23 {
24 defaultValue: () => NodeOS.hostname(),
25 },
26);
27​
28export const HostProcessEnvironment = Context.Reference<NodeJS.ProcessEnv>(
29 "@t3tools/shared/hostProcess/HostProcessEnvironment",
30 {
31 defaultValue: () => process.env,
32 },
33);
34​
35export const HostProcessWorkingDirectory = Context.Reference<string>(
36 "@t3tools/shared/hostProcess/HostProcessWorkingDirectory",
37 {
38 defaultValue: () => process.cwd(),
39 },
40);
41​
42export const HostProcessExecutablePath = Context.Reference<string>(
43 "@t3tools/shared/hostProcess/HostProcessExecutablePath",
44 {
45 defaultValue: () => process.execPath,
46 },
47);
48​
49export const HostProcessArguments = Context.Reference<ReadonlyArray<string>>(
50 "@t3tools/shared/hostProcess/HostProcessArguments",
51 {
52 defaultValue: () => process.argv,
53 },
54);
55​
56/**
57 * The command the shell was given, before Node resolved it to the binary:
58 * `t3` for a PATH lookup, `./t3` or the launcher symlink for an explicit
59 * path. `process.argv[0]` and `execPath` are always the resolved binary.
60 */
61export const HostProcessInvokedAs = Context.Reference<string>(
62 "@t3tools/shared/hostProcess/HostProcessInvokedAs",
63 {
64 defaultValue: () => process.argv0,
65 },
66);
67​
68/**
69 * Whether this process is a Node single-executable rather than a script run
70 * by a Node on the machine. Code that needs a sibling file or a Node to run
71 * one branches on this: an executable hosts such things as hidden
72 * subcommands of itself.
73 */
74export const HostProcessIsExecutable = Context.Reference<boolean>(
75 "@t3tools/shared/hostProcess/HostProcessIsExecutable",
76 {
77 defaultValue: () => NodeSea.isSea(),
78 },
79);
80​
81/**
82 * Every IP address this machine answers to: the interface addresses, plus
83 * whatever the resolver returns for the machine's own hostname. The latter
84 * matters because a hostname can map to an address no interface carries —
85 * Debian-style hosts put `127.0.1.1` in `/etc/hosts` — and a program that
86 * records "its" address by resolving its hostname (Firefox's profile lock
87 * does) will write that one. "Is this address ours" has to accept both.
88 *
89 * Best effort: a failed lookup just leaves the interface set.
90 */
91export const HostProcessAddresses = Context.Reference<Effect.Effect<ReadonlySet<string>>>(
92 "@t3tools/shared/hostProcess/HostProcessAddresses",
93 {
94 defaultValue: () =>
95 Effect.gen(function* () {
96 const interfaces = Object.values(NodeOS.networkInterfaces())
97 .flat()
98 .flatMap((entry) => (entry ? [entry.address] : []));
99 const resolved = yield* Effect.tryPromise(() =>
100 NodeDns.promises.lookup(NodeOS.hostname(), { all: true }),
101 ).pipe(
102 Effect.map((entries) => entries.map((entry) => entry.address)),
103 Effect.orElseSucceed(() => [] as ReadonlyArray<string>),
104 );
105 return new Set([...interfaces, ...resolved]);
106 }),
107 },
108);
109​
110/** Undefined on platforms without POSIX uids (Windows). */
111export const HostProcessUserId = Context.Reference<number | undefined>(
112 "@t3tools/shared/hostProcess/HostProcessUserId",
113 {
114 defaultValue: () => process.getuid?.(),
115 },
116);
117​
118export const isHostWindows = Effect.map(HostProcessPlatform, (platform) => platform === "win32");