@profullstack/threatcrush 0.11.4 → 0.11.6

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/dist/index.js CHANGED
@@ -12337,7 +12337,7 @@ var source_default = chalk;
12337
12337
 
12338
12338
  // src/index.ts
12339
12339
  var import_readline = __toESM(require("readline"));
12340
- var import_node_child_process10 = require("child_process");
12340
+ var import_node_child_process11 = require("child_process");
12341
12341
  var import_node_fs31 = require("fs");
12342
12342
  var import_node_path21 = require("path");
12343
12343
  var import_node_os8 = require("os");
@@ -18273,7 +18273,7 @@ function printTrustRequired(name) {
18273
18273
  function notifyDaemonIfRunning() {
18274
18274
  if (!findRunningDaemon()) return;
18275
18275
  console.log(source_default.dim(` \u2139 threatcrushd is running \u2014 restart it to load/unload modules:`));
18276
- console.log(source_default.dim(` ${source_default.white("threatcrush stop && threatcrush start")}`));
18276
+ console.log(source_default.dim(` ${source_default.white("threatcrush restart")}`));
18277
18277
  }
18278
18278
  async function modulesListCommand() {
18279
18279
  banner();
@@ -21822,7 +21822,7 @@ async function daemonStop() {
21822
21822
  const pid = findRunningDaemon();
21823
21823
  if (!pid) {
21824
21824
  console.log(source_default.dim(" No running daemon found."));
21825
- return;
21825
+ return true;
21826
21826
  }
21827
21827
  try {
21828
21828
  const client = new IpcClient();
@@ -21836,28 +21836,48 @@ async function daemonStop() {
21836
21836
  } catch {
21837
21837
  }
21838
21838
  }
21839
- const deadline = Date.now() + 3e3;
21840
- while (Date.now() < deadline) {
21841
- try {
21842
- process.kill(pid, 0);
21843
- } catch {
21844
- break;
21845
- }
21846
- await new Promise((r) => setTimeout(r, 100));
21847
- }
21848
- try {
21849
- process.kill(pid, 0);
21850
- } catch {
21839
+ if (await waitForExit(pid, 3e3)) {
21851
21840
  removePidFile();
21852
21841
  console.log(source_default.green(` \u2713 threatcrushd stopped.`));
21853
- return;
21842
+ return true;
21854
21843
  }
21855
21844
  try {
21856
21845
  process.kill(pid, "SIGKILL");
21857
21846
  } catch {
21858
21847
  }
21848
+ if (!await waitForExit(pid, 1e3)) {
21849
+ console.log(source_default.red(` \u2717 threatcrushd (pid ${pid}) is still running and could not be stopped.`));
21850
+ console.log(source_default.dim(" It may be owned by another user \u2014 try `sudo threatcrush stop`.\n"));
21851
+ return false;
21852
+ }
21859
21853
  removePidFile();
21860
21854
  console.log(source_default.yellow(` ! threatcrushd was killed (SIGKILL).`));
21855
+ return true;
21856
+ }
21857
+ async function daemonRestart() {
21858
+ if (!await daemonStop()) return;
21859
+ await releaseStaleSocket();
21860
+ await daemonStart();
21861
+ }
21862
+ async function waitForExit(pid, timeoutMs) {
21863
+ const deadline = Date.now() + timeoutMs;
21864
+ while (isProcessAlive(pid)) {
21865
+ if (Date.now() >= deadline) return false;
21866
+ await new Promise((r) => setTimeout(r, 100));
21867
+ }
21868
+ return true;
21869
+ }
21870
+ async function releaseStaleSocket() {
21871
+ const deadline = Date.now() + 2e3;
21872
+ while ((0, import_node_fs26.existsSync)(PATHS.socket) && Date.now() < deadline) {
21873
+ await new Promise((r) => setTimeout(r, 50));
21874
+ }
21875
+ if ((0, import_node_fs26.existsSync)(PATHS.socket)) {
21876
+ try {
21877
+ (0, import_node_fs26.unlinkSync)(PATHS.socket);
21878
+ } catch {
21879
+ }
21880
+ }
21861
21881
  }
21862
21882
 
21863
21883
  // src/commands/service.ts
@@ -23048,6 +23068,90 @@ async function allowlistCommand(opts) {
23048
23068
 
23049
23069
  // src/index.ts
23050
23070
  init_paths();
23071
+
23072
+ // src/upgrade.ts
23073
+ var import_node_child_process10 = require("child_process");
23074
+ var PACKAGE_MANAGERS = ["npm", "pnpm", "yarn", "bun"];
23075
+ var defaultRunner = (cmd) => {
23076
+ try {
23077
+ return (0, import_node_child_process10.execSync)(cmd, { encoding: "utf-8", stdio: ["pipe", "pipe", "pipe"] });
23078
+ } catch {
23079
+ return null;
23080
+ }
23081
+ };
23082
+ function globalInstallCommand(pm, pkgName, action) {
23083
+ if (action === "remove") {
23084
+ const removals = {
23085
+ npm: `npm uninstall -g ${pkgName}`,
23086
+ pnpm: `pnpm remove -g ${pkgName}`,
23087
+ yarn: `yarn global remove ${pkgName}`,
23088
+ bun: `bun remove -g ${pkgName}`
23089
+ };
23090
+ return removals[pm] ?? removals.npm;
23091
+ }
23092
+ const target = `${pkgName}@latest`;
23093
+ const installs = {
23094
+ npm: `npm i -g ${target}`,
23095
+ pnpm: `pnpm add -g ${target}`,
23096
+ yarn: `yarn global add ${target}`,
23097
+ bun: `bun add -g ${target}`
23098
+ };
23099
+ return installs[pm] ?? installs.npm;
23100
+ }
23101
+ function globalListCommand(pm, pkgName) {
23102
+ const lists = {
23103
+ npm: `npm ls -g --depth=0 ${pkgName}`,
23104
+ pnpm: `pnpm list -g --depth=0 ${pkgName}`,
23105
+ yarn: `yarn global list --pattern ${pkgName}`,
23106
+ bun: `bun pm ls -g`
23107
+ };
23108
+ return lists[pm] ?? lists.npm;
23109
+ }
23110
+ function globalHolders(pkgName, run = defaultRunner) {
23111
+ return PACKAGE_MANAGERS.filter((pm) => {
23112
+ const out = run(globalListCommand(pm, pkgName));
23113
+ return typeof out === "string" && out.includes(pkgName);
23114
+ });
23115
+ }
23116
+ function parseVersionOutput(output) {
23117
+ if (!output) return null;
23118
+ const match = output.match(/\d+\.\d+\.\d+[^\s]*/);
23119
+ return match ? match[0] : null;
23120
+ }
23121
+ function activeCliVersion(bin = "threatcrush", run = defaultRunner) {
23122
+ return parseVersionOutput(run(`${bin} --version`));
23123
+ }
23124
+ function activeCliPath(bin = "threatcrush", run = defaultRunner) {
23125
+ const out = run(`command -v ${bin}`);
23126
+ return out ? out.trim().split("\n")[0] || null : null;
23127
+ }
23128
+ async function latestPublishedVersion(pkgName, timeoutMs = 8e3) {
23129
+ try {
23130
+ const res = await fetch(`https://registry.npmjs.org/${pkgName}/latest`, {
23131
+ signal: AbortSignal.timeout(timeoutMs)
23132
+ });
23133
+ if (!res.ok) return null;
23134
+ const body = await res.json();
23135
+ return body.version ?? null;
23136
+ } catch {
23137
+ return null;
23138
+ }
23139
+ }
23140
+ function shadowedUpdateWarning(latest, active, activePath) {
23141
+ if (!latest || !active || latest === active) return null;
23142
+ return [
23143
+ "\u26A0 The update ran, but PATH still resolves to an older ThreatCrush.",
23144
+ ` Latest published: ${latest}`,
23145
+ ` Running from PATH: ${active} (${activePath || "unknown"})`,
23146
+ "",
23147
+ " Usually a stale shell hash. Try first:",
23148
+ " hash -r # or open a new terminal",
23149
+ " If it persists, that path is a second copy \u2014 remove it:",
23150
+ ` rm ${activePath || "<path>"}`
23151
+ ];
23152
+ }
23153
+
23154
+ // src/index.ts
23051
23155
  var PKG_VERSION2 = "0.1.8";
23052
23156
  try {
23053
23157
  const pkg = JSON.parse((0, import_node_fs31.readFileSync)((0, import_node_path21.join)(__dirname, "..", "package.json"), "utf-8"));
@@ -23069,22 +23173,22 @@ var DESKTOP_PKG_NAME = "@profullstack/threatcrush-desktop";
23069
23173
  var INSTALL_CONFIG_PATH = (0, import_node_path21.join)((0, import_node_os8.homedir)(), ".threatcrush", "install.json");
23070
23174
  function detectPackageManager() {
23071
23175
  try {
23072
- const npmGlobal = (0, import_node_child_process10.execSync)("npm ls -g --depth=0 --json 2>/dev/null", { encoding: "utf-8" });
23176
+ const npmGlobal = (0, import_node_child_process11.execSync)("npm ls -g --depth=0 --json 2>/dev/null", { encoding: "utf-8" });
23073
23177
  if (npmGlobal.includes(PKG_NAME)) return "npm";
23074
23178
  } catch {
23075
23179
  }
23076
23180
  try {
23077
- (0, import_node_child_process10.execSync)("pnpm --version", { stdio: "pipe" });
23181
+ (0, import_node_child_process11.execSync)("pnpm --version", { stdio: "pipe" });
23078
23182
  return "pnpm";
23079
23183
  } catch {
23080
23184
  }
23081
23185
  try {
23082
- (0, import_node_child_process10.execSync)("yarn --version", { stdio: "pipe" });
23186
+ (0, import_node_child_process11.execSync)("yarn --version", { stdio: "pipe" });
23083
23187
  return "yarn";
23084
23188
  } catch {
23085
23189
  }
23086
23190
  try {
23087
- (0, import_node_child_process10.execSync)("bun --version", { stdio: "pipe" });
23191
+ (0, import_node_child_process11.execSync)("bun --version", { stdio: "pipe" });
23088
23192
  return "bun";
23089
23193
  } catch {
23090
23194
  }
@@ -23097,40 +23201,9 @@ function readInstallConfig() {
23097
23201
  return {};
23098
23202
  }
23099
23203
  }
23100
- function getGlobalInstallCommand(pm, pkgName, action) {
23101
- const commands = {
23102
- npm: {
23103
- install: `npm i -g ${pkgName}`,
23104
- update: `npm update -g ${pkgName}`,
23105
- remove: `npm uninstall -g ${pkgName}`
23106
- },
23107
- pnpm: {
23108
- install: `pnpm add -g ${pkgName}`,
23109
- update: `pnpm update -g ${pkgName}`,
23110
- remove: `pnpm remove -g ${pkgName}`
23111
- },
23112
- yarn: {
23113
- install: `yarn global add ${pkgName}`,
23114
- update: `yarn global upgrade ${pkgName}`,
23115
- remove: `yarn global remove ${pkgName}`
23116
- },
23117
- bun: {
23118
- install: `bun add -g ${pkgName}`,
23119
- update: `bun update -g ${pkgName}`,
23120
- remove: `bun remove -g ${pkgName}`
23121
- }
23122
- };
23123
- return commands[pm]?.[action] || commands.npm[action];
23124
- }
23125
23204
  function packageLooksInstalled(pm, pkgName) {
23126
23205
  try {
23127
- const listCommands = {
23128
- npm: `npm ls -g ${pkgName} --depth=0`,
23129
- pnpm: `pnpm list -g ${pkgName} --depth=0`,
23130
- yarn: `yarn global list --pattern ${pkgName}`,
23131
- bun: `bun pm ls -g`
23132
- };
23133
- const output = (0, import_node_child_process10.execSync)(listCommands[pm] || listCommands.npm, {
23206
+ const output = (0, import_node_child_process11.execSync)(globalListCommand(pm, pkgName), {
23134
23207
  encoding: "utf-8",
23135
23208
  stdio: ["pipe", "pipe", "pipe"]
23136
23209
  });
@@ -23139,6 +23212,24 @@ function packageLooksInstalled(pm, pkgName) {
23139
23212
  return false;
23140
23213
  }
23141
23214
  }
23215
+ function upgradeCommands(pkgName) {
23216
+ const holders = globalHolders(pkgName);
23217
+ const targets = holders.length > 0 ? holders : [detectPackageManager()];
23218
+ return targets.map((pm) => globalInstallCommand(pm, pkgName, "update"));
23219
+ }
23220
+ async function reportIfStillStale() {
23221
+ const warning = shadowedUpdateWarning(
23222
+ await latestPublishedVersion(PKG_NAME),
23223
+ activeCliVersion(),
23224
+ activeCliPath()
23225
+ );
23226
+ if (!warning) return;
23227
+ console.log(source_default.yellow(` ${warning[0]}`));
23228
+ for (const line of warning.slice(1)) {
23229
+ console.log(source_default.dim(` ${line}`));
23230
+ }
23231
+ console.log();
23232
+ }
23142
23233
  var program2 = new Command();
23143
23234
  program2.name("threatcrush").description(
23144
23235
  `${source_default.green("\u26A1 ThreatCrush")} \u2014 All-in-one security agent
@@ -23266,6 +23357,10 @@ program2.command("stop").description("Stop the ThreatCrush daemon").action(async
23266
23357
  console.log(LOGO2);
23267
23358
  await daemonStop();
23268
23359
  });
23360
+ program2.command("restart").description("Restart the ThreatCrush daemon (stop, then start)").action(async () => {
23361
+ console.log(LOGO2);
23362
+ await daemonRestart();
23363
+ });
23269
23364
  program2.command("daemon").description("Run threatcrushd in the foreground (systemd / Docker ExecStart)").action(async () => {
23270
23365
  await daemonForeground();
23271
23366
  });
@@ -23286,7 +23381,7 @@ program2.command("logs").description("Tail daemon logs").action(async () => {
23286
23381
  console.log(source_default.green(` Tailing ${logPath}...
23287
23382
  `));
23288
23383
  console.log(source_default.gray(" Press Ctrl+C to stop\n"));
23289
- (0, import_node_child_process10.execSync)(`tail -f ${logPath}`, { stdio: "inherit" });
23384
+ (0, import_node_child_process11.execSync)(`tail -f ${logPath}`, { stdio: "inherit" });
23290
23385
  });
23291
23386
  program2.command("activate").description("Activate your license key").action(async () => {
23292
23387
  console.log(LOGO2);
@@ -23328,17 +23423,15 @@ program2.command("update").description("Update ThreatCrush CLI and installed bun
23328
23423
  if (opts.modules) {
23329
23424
  console.log(LOGO2);
23330
23425
  console.log(source_default.yellow(" Updating modules...\n"));
23331
- const pm2 = detectPackageManager();
23332
- const commands2 = [
23333
- getGlobalInstallCommand(pm2, PKG_NAME, "update")
23334
- ];
23426
+ const commands2 = upgradeCommands(PKG_NAME);
23335
23427
  try {
23336
23428
  for (const cmd of commands2) {
23337
23429
  console.log(source_default.green(` \u2192 ${cmd}
23338
23430
  `));
23339
- (0, import_node_child_process10.execSync)(cmd, { stdio: "inherit" });
23431
+ (0, import_node_child_process11.execSync)(cmd, { stdio: "inherit" });
23340
23432
  }
23341
23433
  console.log(source_default.green("\n \u2713 Modules updated successfully!\n"));
23434
+ await reportIfStillStale();
23342
23435
  } catch (err) {
23343
23436
  console.log(source_default.red("\n \u2717 Module update failed. Try manually:\n"));
23344
23437
  for (const cmd of commands2) {
@@ -23348,21 +23441,19 @@ program2.command("update").description("Update ThreatCrush CLI and installed bun
23348
23441
  }
23349
23442
  return;
23350
23443
  }
23351
- const pm = detectPackageManager();
23444
+ const holders = globalHolders(PKG_NAME);
23352
23445
  const installConfig = readInstallConfig();
23353
23446
  const installMode = opts.cli ? "server" : opts.desktop ? "desktop" : installConfig.installMode || "server";
23354
- console.log(source_default.dim(` Detected package manager: ${pm}`));
23355
- console.log(source_default.dim(` Install mode: ${installMode}
23447
+ console.log(source_default.dim(` Detected package manager: ${holders.join(", ") || detectPackageManager()}`));
23448
+ console.log(source_default.dim(` Install mode: ${installMode}`));
23449
+ console.log(source_default.dim(` Current version: ${PKG_VERSION2}
23356
23450
  `));
23357
- const commands = [getGlobalInstallCommand(pm, PKG_NAME, "update")];
23358
- if (installMode === "desktop") {
23359
- commands.push(getGlobalInstallCommand(pm, DESKTOP_PKG_NAME, "update"));
23360
- }
23451
+ const commands = upgradeCommands(PKG_NAME);
23361
23452
  try {
23362
23453
  for (const cmd of commands) {
23363
23454
  console.log(source_default.green(` \u2192 ${cmd}
23364
23455
  `));
23365
- (0, import_node_child_process10.execSync)(cmd, { stdio: "inherit" });
23456
+ (0, import_node_child_process11.execSync)(cmd, { stdio: "inherit" });
23366
23457
  }
23367
23458
  console.log(source_default.green("\n \u2713 ThreatCrush updated successfully!\n"));
23368
23459
  if (installMode === "desktop") {
@@ -23370,6 +23461,7 @@ program2.command("update").description("Update ThreatCrush CLI and installed bun
23370
23461
  } else {
23371
23462
  console.log(source_default.dim(" Updated bundle: CLI only\n"));
23372
23463
  }
23464
+ await reportIfStillStale();
23373
23465
  } catch (err) {
23374
23466
  console.log(source_default.red("\n \u2717 Update failed. Try manually:\n"));
23375
23467
  for (const cmd of commands) {
@@ -23391,22 +23483,25 @@ program2.command("remove").description("Uninstall ThreatCrush and the installed
23391
23483
  console.log(source_default.dim("\n Cancelled.\n"));
23392
23484
  return;
23393
23485
  }
23394
- const pm = detectPackageManager();
23395
23486
  const installConfig = readInstallConfig();
23396
23487
  const installMode = installConfig.installMode || "server";
23397
- const commands = [getGlobalInstallCommand(pm, PKG_NAME, "remove")];
23398
- if (installMode === "desktop" && packageLooksInstalled(pm, DESKTOP_PKG_NAME)) {
23399
- commands.push(getGlobalInstallCommand(pm, DESKTOP_PKG_NAME, "remove"));
23488
+ const holders = globalHolders(PKG_NAME);
23489
+ const pms = holders.length > 0 ? holders : [detectPackageManager()];
23490
+ const commands = pms.map((pm) => globalInstallCommand(pm, PKG_NAME, "remove"));
23491
+ for (const pm of pms) {
23492
+ if (installMode === "desktop" && packageLooksInstalled(pm, DESKTOP_PKG_NAME)) {
23493
+ commands.push(globalInstallCommand(pm, DESKTOP_PKG_NAME, "remove"));
23494
+ }
23400
23495
  }
23401
23496
  console.log(source_default.dim(`
23402
- Detected package manager: ${pm}`));
23497
+ Detected package manager: ${pms.join(", ")}`));
23403
23498
  console.log(source_default.dim(` Install mode: ${installMode}
23404
23499
  `));
23405
23500
  try {
23406
23501
  for (const cmd of commands) {
23407
23502
  console.log(source_default.green(` \u2192 ${cmd}
23408
23503
  `));
23409
- (0, import_node_child_process10.execSync)(cmd, { stdio: "inherit" });
23504
+ (0, import_node_child_process11.execSync)(cmd, { stdio: "inherit" });
23410
23505
  }
23411
23506
  console.log(source_default.green("\n \u2713 ThreatCrush has been uninstalled.\n"));
23412
23507
  console.log(source_default.dim(" We're sorry to see you go! \u{1F44B}\n"));