longtable 0.0.0-stage → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/store.mjs ADDED
@@ -0,0 +1,104 @@
1
+ // Where the connection keeps its settings and secrets, on this computer only.
2
+ // Secrets are encrypted for this Windows user with DPAPI; elsewhere they are files only this user can read.
3
+
4
+ import { spawnSync } from "node:child_process";
5
+ import { appendFileSync, chmodSync, cpSync, existsSync, mkdirSync, readFileSync, renameSync, rmSync, writeFileSync } from "node:fs";
6
+ import { homedir } from "node:os";
7
+ import { join } from "node:path";
8
+
9
+ const isWindows = process.platform === "win32";
10
+ // ~/.longtable on every system, never AppData. On Windows an app installed as a package (Claude Desktop is one)
11
+ // has the new folders it makes under AppData quietly kept in its own private folder, so a connection set up from
12
+ // inside it was invisible to everything else on the computer, start-up included. Folders in the user profile itself
13
+ // are not redirected (both checked 9 Oct 2026).
14
+ export const defaultHome = join(homedir(), ".longtable");
15
+ const legacyHome = isWindows ? join(process.env.LOCALAPPDATA || homedir(), "Longtable") : null;
16
+ // LONGTABLE_HOME keeps a separate set of settings, for tests.
17
+ export const home = process.env.LONGTABLE_HOME || defaultHome;
18
+
19
+ // A connection set up before the move keeps working: its settings, sign-ins and log come across once.
20
+ if (!process.env.LONGTABLE_HOME && legacyHome && existsSync(legacyHome) && !existsSync(join(defaultHome, "config.json"))
21
+ && !existsSync(join(defaultHome, "secrets"))) {
22
+ try {
23
+ cpSync(legacyHome, defaultHome, { recursive: true, errorOnExist: false, force: false });
24
+ } catch {}
25
+ }
26
+ const secretsDir = join(home, "secrets");
27
+ const configFile = join(home, "config.json");
28
+ const logFile = join(home, "runner.log");
29
+
30
+ mkdirSync(secretsDir, { recursive: true });
31
+
32
+ export function hideSecrets(text) {
33
+ return String(text)
34
+ .replace(/sk-ant-[A-Za-z0-9_-]{8,}/g, "[token]")
35
+ .replace(/\b[0-9a-f]{64}\b/gi, "[device token]");
36
+ }
37
+
38
+ export function log(line) {
39
+ appendFileSync(logFile, `${new Date().toISOString()} ${hideSecrets(line)}\n`);
40
+ }
41
+
42
+ export function readConfig() {
43
+ if (!existsSync(configFile)) return { connections: [] };
44
+ return JSON.parse(readFileSync(configFile, "utf8"));
45
+ }
46
+
47
+ export function writeConfig(config) {
48
+ writeFileSync(configFile, JSON.stringify(config, null, 2));
49
+ }
50
+
51
+ function powershell(script, input, file) {
52
+ const r = spawnSync("powershell.exe", ["-NoProfile", "-NonInteractive", "-Command", script], {
53
+ input,
54
+ env: { ...process.env, LT_SECRET_FILE: file },
55
+ windowsHide: true,
56
+ });
57
+ if (r.status !== 0) throw new Error("Could not use Windows' secret store: " + r.stderr.toString().slice(0, 200));
58
+ return r.stdout.toString();
59
+ }
60
+
61
+ const secretPath = (name) => join(secretsDir, isWindows ? `${name}.bin` : name);
62
+
63
+ export function saveSecret(name, value) {
64
+ const file = secretPath(name);
65
+ if (isWindows) {
66
+ powershell(
67
+ "Add-Type -AssemblyName System.Security; $t = [Console]::In.ReadToEnd(); " +
68
+ "$p = [Security.Cryptography.ProtectedData]::Protect([Text.Encoding]::UTF8.GetBytes($t), $null, 'CurrentUser'); " +
69
+ "[IO.File]::WriteAllBytes($env:LT_SECRET_FILE, $p)",
70
+ value,
71
+ file,
72
+ );
73
+ } else {
74
+ writeFileSync(file, value, { mode: 0o600 });
75
+ chmodSync(file, 0o600);
76
+ }
77
+ }
78
+
79
+ export function loadSecret(name) {
80
+ const file = secretPath(name);
81
+ if (!existsSync(file)) return null;
82
+ if (isWindows) {
83
+ return powershell(
84
+ "Add-Type -AssemblyName System.Security; $p = [IO.File]::ReadAllBytes($env:LT_SECRET_FILE); " +
85
+ "$b = [Security.Cryptography.ProtectedData]::Unprotect($p, $null, 'CurrentUser'); " +
86
+ "[Console]::Out.Write([Text.Encoding]::UTF8.GetString($b))",
87
+ undefined,
88
+ file,
89
+ ).trim();
90
+ }
91
+ return readFileSync(file, "utf8").trim();
92
+ }
93
+
94
+ export function forgetSecret(name) {
95
+ rmSync(secretPath(name), { force: true });
96
+ }
97
+
98
+ // The feasibility probe saved the Claude sign-in one folder up; adopt it instead of asking for a new approval.
99
+ const oldClaudeToken = join(defaultHome, isWindows ? "claude-token.bin" : "claude-token");
100
+ const defaultClaudeToken = join(defaultHome, "secrets", isWindows ? "claude-token.bin" : "claude-token");
101
+ if (existsSync(oldClaudeToken) && !existsSync(defaultClaudeToken)) {
102
+ mkdirSync(join(defaultHome, "secrets"), { recursive: true });
103
+ renameSync(oldClaudeToken, defaultClaudeToken);
104
+ }