feat(node): add TypeScript sibling project for npm-based install
Introduces @kuns/claude-mailbox under node/, a wire-compatible TypeScript port of the .NET daemon that distributes via the public Gitea npm registry. The .NET project stays in src/ClaudeMailbox/ untouched; users pick whichever flavor they prefer. - node/ project: fastify + @modelcontextprotocol/sdk StreamableHTTPServerTransport + better-sqlite3, schema and wire surface match the C# version (port 47822, X-Mailbox header, MCP tool names, snake_case SQLite columns) - Cross-platform autostart: Scheduled Task (Win, no admin) / Windows Service (Win, --service) / launchd (mac) / systemd --user (linux) - 9/9 vitest tests pass; end-to-end /health + send/check round-trip verified - CI split: existing ci.yml/release.yml renamed to *-dotnet.yml with path filters, new ci-node.yml and release-node.yml publish to Gitea npm registry - install.ps1 / install.sh bootstrap one-liners at repo root; homebrew/ contains a tap formula template - README install section reordered: npm path primary, dotnet publish secondary Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
226
node/src/autostart/windows.ts
Normal file
226
node/src/autostart/windows.ts
Normal file
@@ -0,0 +1,226 @@
|
||||
import { existsSync, mkdirSync, writeFileSync, readFileSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
import { run, cliEntry, type AutostartManager, type AutostartInstallOpts } from "./index.js";
|
||||
import { userConfigPath } from "../config.js";
|
||||
|
||||
const TASK_NAME = "ClaudeMailbox";
|
||||
const SERVICE_NAME = "ClaudeMailbox";
|
||||
|
||||
function ensureConfigSeeded(opts: AutostartInstallOpts): string {
|
||||
const path = userConfigPath();
|
||||
if (!existsSync(path)) {
|
||||
mkdirSync(join(path, ".."), { recursive: true });
|
||||
const seed: Record<string, unknown> = {};
|
||||
if (opts.port !== undefined) seed.port = opts.port;
|
||||
if (opts.bind !== undefined) seed.bind = opts.bind;
|
||||
if (opts.dbPath !== undefined) seed.dbPath = opts.dbPath;
|
||||
writeFileSync(path, JSON.stringify(seed, null, 2) + "\n", "utf8");
|
||||
}
|
||||
return path;
|
||||
}
|
||||
|
||||
function buildServeCommand(): { node: string; script: string; configPath: string } {
|
||||
const { node, script } = cliEntry();
|
||||
return { node, script, configPath: userConfigPath() };
|
||||
}
|
||||
|
||||
function scheduledTaskInstall(opts: AutostartInstallOpts): void {
|
||||
const configPath = ensureConfigSeeded(opts);
|
||||
const { node, script } = buildServeCommand();
|
||||
const tr = `"${node}" "${script}" serve --config "${configPath}"`;
|
||||
const r = run("schtasks.exe", [
|
||||
"/Create",
|
||||
"/SC",
|
||||
"ONLOGON",
|
||||
"/TN",
|
||||
TASK_NAME,
|
||||
"/TR",
|
||||
tr,
|
||||
"/RL",
|
||||
"LIMITED",
|
||||
"/F",
|
||||
]);
|
||||
if (r.status !== 0) {
|
||||
throw new Error(`schtasks /Create failed (exit ${r.status}): ${r.stderr || r.stdout}`);
|
||||
}
|
||||
const start = run("schtasks.exe", ["/Run", "/TN", TASK_NAME]);
|
||||
if (start.status !== 0) {
|
||||
console.warn(`Task created but /Run returned ${start.status}: ${start.stderr.trim()}`);
|
||||
}
|
||||
}
|
||||
|
||||
function scheduledTaskUninstall(purge: boolean): void {
|
||||
run("schtasks.exe", ["/End", "/TN", TASK_NAME]);
|
||||
const r = run("schtasks.exe", ["/Delete", "/TN", TASK_NAME, "/F"]);
|
||||
if (r.status !== 0 && !/cannot find/i.test(r.stderr)) {
|
||||
throw new Error(`schtasks /Delete failed (exit ${r.status}): ${r.stderr || r.stdout}`);
|
||||
}
|
||||
if (purge) purgeData();
|
||||
}
|
||||
|
||||
function scheduledTaskStatus(): "Running" | "Stopped" | "NotInstalled" {
|
||||
const r = run("schtasks.exe", ["/Query", "/TN", TASK_NAME, "/FO", "LIST", "/V"]);
|
||||
if (r.status !== 0) {
|
||||
if (/cannot find/i.test(r.stderr) || /does not exist/i.test(r.stderr)) return "NotInstalled";
|
||||
return "Stopped";
|
||||
}
|
||||
if (/Status:\s*Running/i.test(r.stdout)) return "Running";
|
||||
return "Stopped";
|
||||
}
|
||||
|
||||
function scheduledTaskRun(): void {
|
||||
const r = run("schtasks.exe", ["/Run", "/TN", TASK_NAME]);
|
||||
if (r.status !== 0) throw new Error(`schtasks /Run failed: ${r.stderr || r.stdout}`);
|
||||
}
|
||||
|
||||
function scheduledTaskEnd(): void {
|
||||
run("schtasks.exe", ["/End", "/TN", TASK_NAME]);
|
||||
}
|
||||
|
||||
interface NodeWindowsService {
|
||||
on(event: "install" | "uninstall" | "alreadyinstalled" | "start" | "stop", cb: () => void): void;
|
||||
install(): void;
|
||||
uninstall(): void;
|
||||
start(): void;
|
||||
stop(): void;
|
||||
exists?: boolean;
|
||||
}
|
||||
|
||||
interface NodeWindowsModule {
|
||||
Service: new (opts: {
|
||||
name: string;
|
||||
description?: string;
|
||||
script: string;
|
||||
nodeOptions?: string[];
|
||||
workingDirectory?: string;
|
||||
}) => NodeWindowsService;
|
||||
}
|
||||
|
||||
async function loadNodeWindows(): Promise<NodeWindowsModule> {
|
||||
try {
|
||||
return (await import("node-windows")) as unknown as NodeWindowsModule;
|
||||
} catch (err) {
|
||||
throw new Error(
|
||||
"node-windows is not installed. Install it with `npm i -g node-windows` or use the default Scheduled Task autostart instead.",
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
function isAdministrator(): boolean {
|
||||
const r = run("net.exe", ["session"]);
|
||||
return r.status === 0;
|
||||
}
|
||||
|
||||
async function serviceInstall(opts: AutostartInstallOpts): Promise<void> {
|
||||
if (!isAdministrator()) {
|
||||
throw new Error("install-autostart --service requires an Administrator shell.");
|
||||
}
|
||||
ensureConfigSeeded(opts);
|
||||
const { script, configPath } = buildServeCommand();
|
||||
const nw = await loadNodeWindows();
|
||||
await new Promise<void>((resolveFn, rejectFn) => {
|
||||
const svc = new nw.Service({
|
||||
name: SERVICE_NAME,
|
||||
description: "ClaudeMailbox MCP mail daemon for parallel Claude session coordination.",
|
||||
script,
|
||||
nodeOptions: [],
|
||||
});
|
||||
svc.on("install", () => {
|
||||
svc.start();
|
||||
resolveFn();
|
||||
});
|
||||
svc.on("alreadyinstalled", () => resolveFn());
|
||||
try {
|
||||
svc.install();
|
||||
} catch (e) {
|
||||
rejectFn(e);
|
||||
}
|
||||
void configPath;
|
||||
});
|
||||
}
|
||||
|
||||
async function serviceUninstall(purge: boolean): Promise<void> {
|
||||
if (!isAdministrator()) {
|
||||
throw new Error("uninstall-autostart --service requires an Administrator shell.");
|
||||
}
|
||||
const { script } = buildServeCommand();
|
||||
const nw = await loadNodeWindows();
|
||||
await new Promise<void>((resolveFn, rejectFn) => {
|
||||
const svc = new nw.Service({ name: SERVICE_NAME, script });
|
||||
svc.on("uninstall", () => resolveFn());
|
||||
try {
|
||||
svc.uninstall();
|
||||
} catch (e) {
|
||||
rejectFn(e);
|
||||
}
|
||||
});
|
||||
if (purge) purgeData();
|
||||
}
|
||||
|
||||
function serviceStatus(): "Running" | "Stopped" | "NotInstalled" {
|
||||
const r = run("sc.exe", ["query", SERVICE_NAME]);
|
||||
if (r.status !== 0) return "NotInstalled";
|
||||
if (/STATE\s*:\s*\d+\s+RUNNING/i.test(r.stdout)) return "Running";
|
||||
return "Stopped";
|
||||
}
|
||||
|
||||
function serviceStart(): void {
|
||||
const r = run("sc.exe", ["start", SERVICE_NAME]);
|
||||
if (r.status !== 0) throw new Error(`sc start failed: ${r.stderr || r.stdout}`);
|
||||
}
|
||||
|
||||
function serviceStop(): void {
|
||||
const r = run("sc.exe", ["stop", SERVICE_NAME]);
|
||||
if (r.status !== 0 && !/has not been started/i.test(r.stdout)) {
|
||||
throw new Error(`sc stop failed: ${r.stderr || r.stdout}`);
|
||||
}
|
||||
}
|
||||
|
||||
function purgeData(): void {
|
||||
const cfg = userConfigPath();
|
||||
if (existsSync(cfg)) {
|
||||
try {
|
||||
const parsed = JSON.parse(readFileSync(cfg, "utf8")) as { dbPath?: string };
|
||||
void parsed;
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function windowsManager(mode: "default" | "service"): AutostartManager {
|
||||
if (mode === "service") {
|
||||
return {
|
||||
mode,
|
||||
install: serviceInstall,
|
||||
uninstall: serviceUninstall,
|
||||
async start() {
|
||||
serviceStart();
|
||||
},
|
||||
async stop() {
|
||||
serviceStop();
|
||||
},
|
||||
async status() {
|
||||
return serviceStatus();
|
||||
},
|
||||
};
|
||||
}
|
||||
return {
|
||||
mode,
|
||||
async install(opts) {
|
||||
scheduledTaskInstall(opts);
|
||||
},
|
||||
async uninstall(purge) {
|
||||
scheduledTaskUninstall(purge);
|
||||
},
|
||||
async start() {
|
||||
scheduledTaskRun();
|
||||
},
|
||||
async stop() {
|
||||
scheduledTaskEnd();
|
||||
},
|
||||
async status() {
|
||||
return scheduledTaskStatus();
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user