serve --stdio: desktop JSON-RPC profile on the MCP core, confirm tokens, --json results, sidecar builds
- cli/serve.ts: session.hello with a per-session secret, typed methods per tool, project.inspect, portfolio.status over many project folders, progress notifications, cancellation - cli/lib/confirm-token.ts: single-use HMAC tokens bound to method + canonical params + expiry; writes are dry runs without one; no submit / release / publish method exists - cli/lib/runner.ts: shared child runner for mcp and serve, works from a compiled binary - --json on check, testflight / ppo status, next-build, next-version-code, analytics compare, prices import; inspect command; confirm() always answers no under serve - tools/build-sidecars.ts: bun build --compile for five targets, ad-hoc re-sign on macOS, path leak check - tests/serve.test.ts: methods, token binding / expiry / replay, shared vector, stdio and compiled smoke
This commit is contained in:
parent
c6360812b8
commit
3454a9aadb
28 changed files with 1048 additions and 42 deletions
79
cli/lib/runner.ts
Normal file
79
cli/lib/runner.ts
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
// Runs one storebound command as a child process: the shared core of `storebound mcp` and `storebound serve --stdio`.
|
||||
// Works from source (`bun cli/index.ts ...`) and from a `bun build --compile` binary (the desktop sidecar), where the
|
||||
// binary re-executes itself. Output is streamed line by line (progress) and the `--json` result line is picked out.
|
||||
import { join } from 'node:path';
|
||||
import { RESULT_MARK } from './log';
|
||||
|
||||
/** True inside a `bun build --compile` executable (the entry module lives in Bun's embedded file system). */
|
||||
export const compiled = (): boolean => /^(\/\$bunfs\/|[A-Z]:[\\/]~BUN[\\/])/.test(Bun.main);
|
||||
|
||||
/** Command prefix that starts this storebound: the compiled binary itself, or bun + cli/index.ts. */
|
||||
export function selfCommand(): string[] {
|
||||
return compiled() ? [process.execPath] : [process.execPath, join(import.meta.dir, '..', 'index.ts')];
|
||||
}
|
||||
|
||||
export interface RunOptions {
|
||||
cwd: string;
|
||||
/** Extra environment for this call only (credentials from the desktop keychain, never written anywhere). */
|
||||
env?: Record<string, string>;
|
||||
/** Every output line as it arrives (stdout and stderr), without the machine result line. */
|
||||
onLine?: (line: string, stream: 'stdout' | 'stderr') => void;
|
||||
/** Ask for the marked `--json` result line (STOREBOUND_MACHINE=1). */
|
||||
machine?: boolean;
|
||||
}
|
||||
|
||||
export interface RunResult { code: number; out: string; data?: unknown; cancelled: boolean }
|
||||
|
||||
export interface Running { result: Promise<RunResult>; cancel(): void }
|
||||
|
||||
/** Env names a caller may pass per call; anything else in `env` is ignored. */
|
||||
export const CALL_ENV = ['PLAY_SERVICE_ACCOUNT', 'ASC_KEY_ID', 'ASC_ISSUER_ID', 'ASC_P8'] as const;
|
||||
|
||||
export function pickCallEnv(env: Record<string, unknown> | undefined): Record<string, string> {
|
||||
const out: Record<string, string> = {};
|
||||
for (const k of CALL_ENV) { const v = env?.[k]; if (typeof v === 'string' && v) out[k] = v; }
|
||||
return out;
|
||||
}
|
||||
|
||||
/** Environment of a child: the parent's minus anything secret to the session, plus the per-call values. */
|
||||
export function childEnv(extra: Record<string, string> = {}, machine = false): Record<string, string> {
|
||||
const base: Record<string, string> = {};
|
||||
for (const [k, v] of Object.entries(process.env)) if (v !== undefined && !k.startsWith('STOREBOUND_SESSION')) base[k] = v;
|
||||
return { ...base, ...extra, NO_COLOR: '1', ...(machine ? { STOREBOUND_MACHINE: '1', STOREBOUND_SERVE: '1' } : {}) };
|
||||
}
|
||||
|
||||
async function pump(stream: ReadableStream<Uint8Array>, kind: 'stdout' | 'stderr', each: (line: string, kind: 'stdout' | 'stderr') => void): Promise<void> {
|
||||
const dec = new TextDecoder();
|
||||
let buf = '';
|
||||
const reader = stream.getReader();
|
||||
for (;;) {
|
||||
const { value, done } = await reader.read();
|
||||
if (done) break;
|
||||
buf += dec.decode(value, { stream: true });
|
||||
let nl: number;
|
||||
while ((nl = buf.indexOf('\n')) >= 0) { each(buf.slice(0, nl).replace(/\r$/, ''), kind); buf = buf.slice(nl + 1); }
|
||||
}
|
||||
buf += dec.decode();
|
||||
if (buf) each(buf, kind);
|
||||
}
|
||||
|
||||
export function runCommand(argv: string[], o: RunOptions): Running {
|
||||
const p = Bun.spawn([...selfCommand(), ...argv], { cwd: o.cwd, stdin: 'ignore', stdout: 'pipe', stderr: 'pipe', env: childEnv(o.env, o.machine) });
|
||||
let cancelled = false;
|
||||
const lines: string[] = [];
|
||||
let data: unknown;
|
||||
const each = (line: string, kind: 'stdout' | 'stderr'): void => {
|
||||
if (kind === 'stdout' && line.startsWith(RESULT_MARK)) {
|
||||
try { data = JSON.parse(line.slice(RESULT_MARK.length)); } catch { lines.push(line); }
|
||||
return;
|
||||
}
|
||||
lines.push(line);
|
||||
o.onLine?.(line, kind);
|
||||
};
|
||||
const result = (async (): Promise<RunResult> => {
|
||||
await Promise.all([pump(p.stdout, 'stdout', each), pump(p.stderr, 'stderr', each)]);
|
||||
const code = await p.exited;
|
||||
return { code, out: lines.join('\n').trim(), ...(data === undefined ? {} : { data }), cancelled };
|
||||
})();
|
||||
return { result, cancel: () => { cancelled = true; p.kill(); } };
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue