Open History to see the last commit
1import { sha256 } from "@noble/hashes/sha2";
2
3export interface DesktopAppControlAddress {
4 readonly address: string;
5 readonly directory: string | null;
6}
7
8function shortHash(value: string): string {
9 return Array.from(sha256(new TextEncoder().encode(value)).slice(0, 12), (byte) =>
10 byte.toString(16).padStart(2, "0"),
11 ).join("");
12}
13
14/**
15 * The state directory is hashed so custom T3 homes cannot exceed Unix socket
16 * path limits.
17 */
18export function resolveDesktopAppControlAddress(input: {
19 readonly stateDir: string;
20 readonly platform: NodeJS.Platform;
21 readonly tempDir: string;
22 readonly userId: number | undefined;
23 readonly joinPath: (...segments: readonly string[]) => string;
24}): DesktopAppControlAddress {
25 const stateHash = shortHash(input.stateDir);
26 if (input.platform === "win32") {
27 return {
28 address: `\\\\.\\pipe\\t3code-app-${stateHash}`,
29 directory: null,
30 };
31 }
32
33 const userKey =
34 input.userId === undefined ? shortHash(input.stateDir).slice(0, 12) : input.userId;
35 const directory = input.joinPath(input.tempDir, `t3code-${userKey}`);
36 return {
37 address: input.joinPath(directory, `${stateHash}.sock`),
38 directory,
39 };
40}