u-chan-secrets 5.0.1 → 5.1.0

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.
Files changed (42) hide show
  1. package/README.md +233 -106
  2. package/dist/cli.js +114 -20
  3. package/dist/client/claude-setup.js +97 -0
  4. package/dist/client/codex-setup.js +6 -5
  5. package/dist/client/discord.js +67 -0
  6. package/dist/client/github.js +28 -0
  7. package/dist/client/setup.js +31 -2
  8. package/dist/client/update-package.js +166 -0
  9. package/dist/client/update.js +138 -0
  10. package/dist/core/browser.js +0 -5
  11. package/dist/core/config.js +1 -0
  12. package/dist/core/doctor.js +5 -1
  13. package/dist/core/vault.js +110 -5
  14. package/dist/discord/http.js +84 -0
  15. package/dist/discord/policy.js +47 -0
  16. package/dist/discord/service.js +349 -0
  17. package/dist/discord/store.js +85 -0
  18. package/dist/github/http.js +93 -0
  19. package/dist/github/policy.js +24 -0
  20. package/dist/github/service.js +185 -0
  21. package/dist/github/store.js +54 -0
  22. package/dist/index.js +1 -1
  23. package/dist/ipc/client.js +74 -6
  24. package/dist/ipc/server.js +38 -3
  25. package/dist/mcp/server.js +25 -2
  26. package/dist/platform/windows/assets/prompt-src.js +160 -38
  27. package/dist/platform/windows/doctor.js +2 -2
  28. package/dist/service/control.js +47 -5
  29. package/dist/service/main.js +34 -17
  30. package/dist/service/manage.js +20 -16
  31. package/dist/service/state.js +28 -0
  32. package/dist/service/update/coordinator.js +171 -0
  33. package/dist/service/update/files.js +47 -0
  34. package/dist/service/update/gate.js +21 -0
  35. package/dist/service/update/journal.js +157 -0
  36. package/dist/service/update/native.js +50 -0
  37. package/dist/service/update/network.js +28 -0
  38. package/dist/service/update/protocol.js +28 -0
  39. package/dist/service/update/release.js +102 -0
  40. package/dist/service/update/worker-main.js +116 -0
  41. package/dist/service/update/worker.js +58 -0
  42. package/package.json +1 -1
@@ -0,0 +1,116 @@
1
+ import { readFile, lstat } from 'node:fs/promises';
2
+ import { join } from 'node:path';
3
+ import { performance } from 'node:perf_hooks';
4
+ import { defaultProgramData } from '../../platform/windows/install.js';
5
+ import { ServiceManager } from '../../platform/windows/service-manager.js';
6
+ import { productionJournal, verifyUpdateActive, secureUpdateDirectory } from './native.js';
7
+ import { idSchema, sameIdentity } from './protocol.js';
8
+ import { artifactIntegrity, inspectRelease } from './release.js';
9
+ import { noLinks } from './journal.js';
10
+ import { runUpdateTransaction } from './worker.js';
11
+ import { assertCompatibleState } from '../state.js';
12
+ const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));
13
+ const alive = (pid) => { try {
14
+ process.kill(pid, 0);
15
+ return true;
16
+ }
17
+ catch {
18
+ return false;
19
+ } };
20
+ async function main() {
21
+ if (process.platform !== 'win32' || !process.send)
22
+ throw new Error('unsupported_worker');
23
+ const home = process.env.U_CHAN_HOME ?? defaultProgramData(), id = idSchema.parse(process.argv[2]), journal = productionJournal(home), job = await journal.read(id);
24
+ if (job.parentPid !== process.ppid || job.phase !== 'switching')
25
+ throw new Error('invalid_worker_parent');
26
+ delete process.env.BW_SESSION;
27
+ process.send({ type: 'ready', id, nonce: job.nonce });
28
+ await new Promise((resolve, reject) => {
29
+ const timer = setTimeout(() => reject(new Error('handoff_timeout')), 30000);
30
+ process.on('message', message => { const m = message; if (m?.type === 'start' && m.id === id && m.nonce === job.nonce) {
31
+ clearTimeout(timer);
32
+ resolve();
33
+ } });
34
+ });
35
+ const sm = new ServiceManager();
36
+ const waitStatus = async (status) => { const end = performance.now() + 30000; while (await sm.status('u-chan-secrets') !== status) {
37
+ if (performance.now() > end)
38
+ throw new Error('service_timeout');
39
+ await sleep(200);
40
+ } };
41
+ await runUpdateTransaction(journal, id, {
42
+ verify: async () => {
43
+ await verifyUpdateActive(home);
44
+ await assertCompatibleState(home, Number(job.target.version.split('.')[0]));
45
+ const dir = journal.directory(id), archive = join(dir, 'release.tgz');
46
+ await noLinks(archive);
47
+ if ((await lstat(archive)).size > 32 * 1024 * 1024)
48
+ throw new Error('invalid_release');
49
+ const bytes = await readFile(archive);
50
+ if (artifactIntegrity(bytes) !== job.target.integrity)
51
+ throw new Error('invalid_release');
52
+ const release = inspectRelease(bytes);
53
+ for (const [path, expected] of release.files) {
54
+ const p = join(dir, 'next', path);
55
+ await noLinks(p);
56
+ if (!(await readFile(p)).equals(expected))
57
+ throw new Error('staged_release_changed');
58
+ }
59
+ await verifyUpdateActive(home, join(dir, 'next'));
60
+ const parent = join(home, '#0 backup');
61
+ await noLinks(parent);
62
+ try {
63
+ await lstat(parent);
64
+ }
65
+ catch (e) {
66
+ if (e.code !== 'ENOENT')
67
+ throw e;
68
+ const fs = await import('node:fs/promises');
69
+ await fs.mkdir(parent, { mode: 0o700 });
70
+ await secureUpdateDirectory(parent);
71
+ }
72
+ if (await import('../../platform/windows/doctor.js').then(m => m.checkWindowsPermissions(parent, false)) !== 'pass')
73
+ throw new Error('unsafe_backup_parent');
74
+ },
75
+ handoff: async () => {
76
+ process.send?.({ type: 'activated', id, nonce: job.nonce });
77
+ const end = performance.now() + 30000;
78
+ while (alive(job.parentPid)) {
79
+ if (performance.now() > end)
80
+ throw new Error('parent_exit_timeout');
81
+ await sleep(200);
82
+ }
83
+ if (process.connected)
84
+ process.disconnect?.();
85
+ },
86
+ stopForRollback: async () => { if (!alive(job.parentPid)) {
87
+ await sm.stop('u-chan-secrets');
88
+ await waitStatus('stopped');
89
+ } },
90
+ startSource: async () => { if (!alive(job.parentPid)) {
91
+ await sm.start('u-chan-secrets');
92
+ await waitStatus('running');
93
+ } },
94
+ waitReady: async (kind) => {
95
+ const end = performance.now() + 90000, expected = kind === 'target' ? job.target : job.source;
96
+ let stable = 0, lastPid = 0;
97
+ while (performance.now() < end) {
98
+ const m = await journal.marker(id);
99
+ if (m && m.id === id && m.nonce === job.nonce && m.kind === kind && sameIdentity(m.boot, expected) && alive(m.pid)) {
100
+ stable = m.pid === lastPid ? stable + 1 : 1;
101
+ lastPid = m.pid;
102
+ if (stable >= 2)
103
+ return true;
104
+ }
105
+ else {
106
+ stable = 0;
107
+ lastPid = 0;
108
+ }
109
+ await sleep(500);
110
+ }
111
+ return false;
112
+ },
113
+ });
114
+ }
115
+ main().catch(() => { process.exitCode = 1; }).finally(() => { if (process.connected)
116
+ process.disconnect?.(); });
@@ -0,0 +1,58 @@
1
+ import { mkdir, readFile, readdir, rename, lstat } from 'node:fs/promises';
2
+ import { join } from 'node:path';
3
+ import { noLinks } from './journal.js';
4
+ export async function runUpdateTransaction(journal, id, deps) {
5
+ const job = await journal.read(id);
6
+ if (job.phase !== 'switching')
7
+ throw new Error('invalid_update_transition');
8
+ const program = join(journal.home, 'program'), next = join(journal.directory(id), 'next');
9
+ const parent = join(journal.home, '#0 backup'), backup = join(parent, 'update-' + id), failed = join(journal.directory(id), 'failed-program');
10
+ let backedUp = false, activated = false;
11
+ try {
12
+ await deps.verify();
13
+ for (const path of [program, next, parent, backup, failed])
14
+ await noLinks(path);
15
+ await mkdir(parent, { recursive: true, mode: 0o700 });
16
+ for (const path of [backup, failed]) {
17
+ try {
18
+ await lstat(path);
19
+ throw new Error('recovery_path_exists');
20
+ }
21
+ catch (e) {
22
+ if (e.code !== 'ENOENT')
23
+ throw e;
24
+ }
25
+ }
26
+ const names = await readdir(program), pkg = await readFile(join(program, 'package.json'));
27
+ await rename(program, backup);
28
+ backedUp = true;
29
+ const moved = await readdir(backup);
30
+ if (moved.length !== names.length || names.some(n => !moved.includes(n)) || !(await readFile(join(backup, 'package.json'))).equals(pkg))
31
+ throw new Error('backup_mismatch');
32
+ await rename(next, program);
33
+ activated = true;
34
+ await journal.transition(id, 'waiting_for_service');
35
+ await deps.handoff();
36
+ if (!await deps.waitReady('target'))
37
+ throw new Error('startup_failed');
38
+ return await journal.transition(id, 'service_ready');
39
+ }
40
+ catch {
41
+ if (!backedUp)
42
+ return journal.transition(id, 'failed', 'worker_failed');
43
+ try {
44
+ await journal.transition(id, 'rolling_back', 'startup_failed');
45
+ await deps.stopForRollback();
46
+ if (activated)
47
+ await rename(program, failed);
48
+ await rename(backup, program);
49
+ await deps.startSource();
50
+ if (!await deps.waitReady('source'))
51
+ throw new Error('rollback_start_failed');
52
+ return await journal.transition(id, 'rolled_back', 'startup_failed');
53
+ }
54
+ catch {
55
+ return journal.transition(id, 'recovery_required', 'rollback_failed');
56
+ }
57
+ }
58
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "u-chan-secrets",
3
- "version": "5.0.1",
3
+ "version": "5.1.0",
4
4
  "description": "U-Chan Secrets: local MCP server for Bitwarden-backed logins",
5
5
  "license": "MIT",
6
6
  "type": "module",