longtable 0.1.16 → 0.1.18

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/signin.mjs CHANGED
@@ -72,6 +72,9 @@ export function signIn(claude, { title = "Connect your Claude" } = {}) {
72
72
  let state = { state: "starting", message: "Starting Claude's sign-in…" };
73
73
  let finished = false;
74
74
  let pressedEnter = false;
75
+ // Tokens already checked. One read while Claude was still printing it is only part of it: that one is set aside
76
+ // and the screen watched for the rest, where before the whole sign-in ended on the first bad read.
77
+ const tried = new Set();
75
78
  const screen = new Terminal({ cols: 400, rows: 200, allowProposedApi: true, scrollback: 2000 });
76
79
  const screenText = () => {
77
80
  const b = screen.buffer.active;
@@ -83,9 +86,13 @@ export function signIn(claude, { title = "Connect your Claude" } = {}) {
83
86
  name: "xterm-256color", cols: 400, rows: 200, cwd: process.env.USERPROFILE || process.env.HOME || process.cwd(), env: childEnv(null),
84
87
  });
85
88
 
89
+ // The ten-minute limit below. Cleared when the sign-in ends, or `connect` waited out all ten minutes before
90
+ // exiting even after a sign-in that worked first time.
91
+ let deadline = null;
86
92
  function finish(ok, message) {
87
93
  if (finished) return;
88
94
  finished = true;
95
+ clearTimeout(deadline);
89
96
  state = { state: ok ? "connected" : "failed", message };
90
97
  try { term.kill(); } catch {}
91
98
  setTimeout(() => { server.close(); resolve(ok); }, 3000);
@@ -94,17 +101,24 @@ export function signIn(claude, { title = "Connect your Claude" } = {}) {
94
101
  function look() {
95
102
  if (finished) return;
96
103
  const text = screenText();
97
- const token = text.match(/sk-ant-oat\d{2}-[A-Za-z0-9_-]{20,}/);
98
- if (token) {
104
+ // A token counts once something follows it on the screen: until then Claude may still be printing it.
105
+ const token = text.match(/sk-ant-oat\d{2}-[A-Za-z0-9_-]{20,}(?=\s)/);
106
+ if (token && !tried.has(token[0])) {
107
+ tried.add(token[0]);
108
+ const was = state;
99
109
  state = { state: "working", url: state.url, message: "Approved. Checking the sign-in works…" };
100
- try { term.kill(); } catch {}
101
110
  const good = pickWorkingToken(claude, token[0]);
102
111
  if (good) {
112
+ try { term.kill(); } catch {}
103
113
  saveSecret("claude-token", good);
104
114
  log(`saved a working Claude sign-in of ${good.length} characters`);
105
115
  finish(true, "Connected. Your Claude can now answer at the table, quietly, on your own plan.");
106
- } else {
116
+ } else if (tried.size >= 3) {
117
+ try { term.kill(); } catch {}
107
118
  finish(false, "Claude approved, but the sign-in didn't work. Close this page and press Connect again.");
119
+ } else {
120
+ log(`a sign-in read from the screen did not work (${tried.size} of 3); watching for the rest of it`);
121
+ state = was;
108
122
  }
109
123
  return;
110
124
  }
@@ -147,6 +161,6 @@ export function signIn(claude, { title = "Connect your Claude" } = {}) {
147
161
  res.end();
148
162
  });
149
163
  server.listen(0, "127.0.0.1", () => openInBrowser(`http://127.0.0.1:${server.address().port}/${secret}`));
150
- setTimeout(() => finish(false, "This took too long, so it stopped. Close this page and try again."), 10 * 60 * 1000);
164
+ deadline = setTimeout(() => finish(false, "This took too long, so it stopped. Close this page and try again."), 10 * 60 * 1000);
151
165
  });
152
166
  }
package/src/store.mjs CHANGED
@@ -2,7 +2,7 @@
2
2
  // Secrets are encrypted for this Windows user with DPAPI; elsewhere they are files only this user can read.
3
3
 
4
4
  import { spawnSync } from "node:child_process";
5
- import { appendFileSync, chmodSync, cpSync, existsSync, mkdirSync, readFileSync, renameSync, rmSync, writeFileSync } from "node:fs";
5
+ import { appendFileSync, chmodSync, cpSync, existsSync, mkdirSync, readFileSync, renameSync, rmSync, statSync, writeFileSync } from "node:fs";
6
6
  import { homedir } from "node:os";
7
7
  import { join } from "node:path";
8
8
 
@@ -35,7 +35,29 @@ export function hideSecrets(text) {
35
35
  .replace(/\b[0-9a-f]{64}\b/gi, "[device token]");
36
36
  }
37
37
 
38
+ // The log keeps to a size. Offline, a connection writes a line every few seconds, so it once grew without end; past
39
+ // 5 MB it starts again and keeps only the file before it (runner.log.1).
40
+ const LOG_MAX = 5 * 1024 * 1024;
41
+ let logWrites = 0;
42
+ // Text cut to n, never through the middle of a character that takes two (most emoji).
43
+ export const cut = (s, n) => {
44
+ const t = String(s ?? "");
45
+ if (t.length <= n) return t;
46
+ const x = t.slice(0, n);
47
+ return /[\uD800-\uDBFF]$/.test(x) ? x.slice(0, -1) : x;
48
+ };
49
+ // A file written whole or not at all: to a file beside it, then put in its place. A stop halfway through a write
50
+ // used to leave settings or notes cut short, and the next start could not read them.
51
+ export function writeWhole(file, text) {
52
+ const tmp = `${file}.${process.pid}.tmp`;
53
+ writeFileSync(tmp, text);
54
+ renameSync(tmp, file);
55
+ }
56
+
38
57
  export function log(line) {
58
+ if (logWrites++ % 200 === 0) {
59
+ try { if (statSync(logFile).size > LOG_MAX) renameSync(logFile, `${logFile}.1`); } catch {}
60
+ }
39
61
  appendFileSync(logFile, `${new Date().toISOString()} ${hideSecrets(line)}\n`);
40
62
  }
41
63
 
@@ -45,7 +67,7 @@ export function readConfig() {
45
67
  }
46
68
 
47
69
  export function writeConfig(config) {
48
- writeFileSync(configFile, JSON.stringify(config, null, 2));
70
+ writeWhole(configFile, JSON.stringify(config, null, 2));
49
71
  }
50
72
 
51
73
  function powershell(script, input, file) {