@16pxh/cli-bridge 1.0.1 → 1.0.2
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/bin/bridge.mjs +8 -2
- package/lib/auth.mjs +29 -2
- package/package.json +1 -1
package/bin/bridge.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import * as readline from 'node:readline';
|
|
3
|
-
import { setToken, clearToken } from '../lib/auth.mjs';
|
|
3
|
+
import { setToken, clearToken, loadPersistedToken } from '../lib/auth.mjs';
|
|
4
4
|
import { checkClaudeInstalled, killActive } from '../lib/claude.mjs';
|
|
5
5
|
import { startServer } from '../lib/server.mjs';
|
|
6
6
|
|
|
@@ -42,7 +42,13 @@ async function promptToken() {
|
|
|
42
42
|
});
|
|
43
43
|
}
|
|
44
44
|
|
|
45
|
-
|
|
45
|
+
// --- Load persisted token or prompt for new one ---
|
|
46
|
+
const loaded = loadPersistedToken();
|
|
47
|
+
if (loaded) {
|
|
48
|
+
console.log('✓ Previous token loaded from ~/.16pxh/bridge-token');
|
|
49
|
+
} else {
|
|
50
|
+
await promptToken();
|
|
51
|
+
}
|
|
46
52
|
|
|
47
53
|
// --- Start server ---
|
|
48
54
|
const server = startServer();
|
package/lib/auth.mjs
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
import { createHmac } from 'node:crypto';
|
|
2
|
+
import { readFileSync, writeFileSync, unlinkSync, mkdirSync } from 'node:fs';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import { homedir } from 'node:os';
|
|
2
5
|
|
|
3
6
|
const SECRET = '16pxh-bridge-v1';
|
|
7
|
+
const CONFIG_DIR = join(homedir(), '.16pxh');
|
|
8
|
+
const TOKEN_FILE = join(CONFIG_DIR, 'bridge-token');
|
|
4
9
|
|
|
5
10
|
/** @type {string | null} */
|
|
6
11
|
let storedHash = null;
|
|
@@ -15,18 +20,40 @@ function hashToken(rawToken) {
|
|
|
15
20
|
}
|
|
16
21
|
|
|
17
22
|
/**
|
|
18
|
-
* Hash rawToken
|
|
23
|
+
* Hash rawToken, store in memory + persist to ~/.16pxh/bridge-token.
|
|
19
24
|
* @param {string} rawToken
|
|
20
25
|
*/
|
|
21
26
|
export function setToken(rawToken) {
|
|
22
27
|
storedHash = hashToken(rawToken);
|
|
28
|
+
try {
|
|
29
|
+
mkdirSync(CONFIG_DIR, { recursive: true });
|
|
30
|
+
writeFileSync(TOKEN_FILE, storedHash, 'utf-8');
|
|
31
|
+
} catch {
|
|
32
|
+
// Non-critical — memory still works
|
|
33
|
+
}
|
|
23
34
|
}
|
|
24
35
|
|
|
25
36
|
/**
|
|
26
|
-
* Clear the stored token hash.
|
|
37
|
+
* Clear the stored token hash + delete persisted file.
|
|
27
38
|
*/
|
|
28
39
|
export function clearToken() {
|
|
29
40
|
storedHash = null;
|
|
41
|
+
try { unlinkSync(TOKEN_FILE); } catch { /* ignore */ }
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Load persisted token hash from disk (if exists).
|
|
46
|
+
* @returns {boolean} true if a persisted token was loaded
|
|
47
|
+
*/
|
|
48
|
+
export function loadPersistedToken() {
|
|
49
|
+
try {
|
|
50
|
+
const hash = readFileSync(TOKEN_FILE, 'utf-8').trim();
|
|
51
|
+
if (hash) {
|
|
52
|
+
storedHash = hash;
|
|
53
|
+
return true;
|
|
54
|
+
}
|
|
55
|
+
} catch { /* no persisted token */ }
|
|
56
|
+
return false;
|
|
30
57
|
}
|
|
31
58
|
|
|
32
59
|
/**
|