@musistudio/claude-code-router 1.0.2 → 1.0.3

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 (2) hide show
  1. package/dist/cli.js +50 -7
  2. package/package.json +1 -1
package/dist/cli.js CHANGED
@@ -37030,6 +37030,29 @@ var rewriteBody = async (req, res, next) => {
37030
37030
 
37031
37031
  // src/utils/processCheck.ts
37032
37032
  var import_fs2 = require("fs");
37033
+ var REFERENCE_COUNT_FILE = "/tmp/claude-code-reference-count.txt";
37034
+ function incrementReferenceCount() {
37035
+ let count = 0;
37036
+ if ((0, import_fs2.existsSync)(REFERENCE_COUNT_FILE)) {
37037
+ count = parseInt((0, import_fs2.readFileSync)(REFERENCE_COUNT_FILE, "utf-8")) || 0;
37038
+ }
37039
+ count++;
37040
+ (0, import_fs2.writeFileSync)(REFERENCE_COUNT_FILE, count.toString());
37041
+ }
37042
+ function decrementReferenceCount() {
37043
+ let count = 0;
37044
+ if ((0, import_fs2.existsSync)(REFERENCE_COUNT_FILE)) {
37045
+ count = parseInt((0, import_fs2.readFileSync)(REFERENCE_COUNT_FILE, "utf-8")) || 0;
37046
+ }
37047
+ count = Math.max(0, count - 1);
37048
+ (0, import_fs2.writeFileSync)(REFERENCE_COUNT_FILE, count.toString());
37049
+ }
37050
+ function getReferenceCount() {
37051
+ if (!(0, import_fs2.existsSync)(REFERENCE_COUNT_FILE)) {
37052
+ return 0;
37053
+ }
37054
+ return parseInt((0, import_fs2.readFileSync)(REFERENCE_COUNT_FILE, "utf-8")) || 0;
37055
+ }
37033
37056
  function isServiceRunning() {
37034
37057
  if (!(0, import_fs2.existsSync)(PID_FILE)) {
37035
37058
  return false;
@@ -37074,7 +37097,8 @@ function getServiceInfo() {
37074
37097
  pid,
37075
37098
  port: 3456,
37076
37099
  endpoint: "http://127.0.0.1:3456",
37077
- pidFile: PID_FILE
37100
+ pidFile: PID_FILE,
37101
+ referenceCount: getReferenceCount()
37078
37102
  };
37079
37103
  }
37080
37104
 
@@ -37108,6 +37132,15 @@ async function run(options = {}) {
37108
37132
  await initDir();
37109
37133
  await initConfig();
37110
37134
  savePid(process.pid);
37135
+ process.on("SIGINT", () => {
37136
+ console.log("Received SIGINT, cleaning up...");
37137
+ cleanupPidFile();
37138
+ process.exit(0);
37139
+ });
37140
+ process.on("SIGTERM", () => {
37141
+ cleanupPidFile();
37142
+ process.exit(0);
37143
+ });
37111
37144
  const servicePort = process.env.SERVICE_PORT ? parseInt(process.env.SERVICE_PORT) : port;
37112
37145
  const server = createServer(servicePort);
37113
37146
  server.useMiddleware(formatRequest);
@@ -37134,19 +37167,21 @@ async function run(options = {}) {
37134
37167
 
37135
37168
  // src/utils/close.ts
37136
37169
  var import_fs4 = require("fs");
37137
- var import_os = require("os");
37138
37170
  var import_path2 = require("path");
37139
37171
  async function closeService() {
37140
- const PID_FILE2 = (0, import_path2.join)((0, import_os.homedir)(), ".claude-code-router.pid");
37172
+ const PID_FILE2 = (0, import_path2.join)(HOME_DIR, ".claude-code-router.pid");
37141
37173
  if (!isServiceRunning()) {
37142
37174
  console.log("No service is currently running.");
37143
37175
  return;
37144
37176
  }
37177
+ if (getReferenceCount() > 0) {
37178
+ return;
37179
+ }
37145
37180
  try {
37146
37181
  const pid = parseInt((0, import_fs4.readFileSync)(PID_FILE2, "utf-8"));
37147
37182
  process.kill(pid);
37148
37183
  cleanupPidFile();
37149
- console.log("Service has been successfully stopped.");
37184
+ console.log("claude code router service has been successfully stopped.");
37150
37185
  } catch (e2) {
37151
37186
  console.log("Failed to stop the service. It may have already been stopped.");
37152
37187
  cleanupPidFile();
@@ -37187,6 +37222,7 @@ async function executeCodeCommand(args = []) {
37187
37222
  ANTHROPIC_BASE_URL: "http://127.0.0.1:3456",
37188
37223
  API_TIMEOUT_MS: "600000"
37189
37224
  };
37225
+ incrementReferenceCount();
37190
37226
  const claudeProcess = (0, import_child_process.spawn)("claude", args, {
37191
37227
  env,
37192
37228
  stdio: "inherit",
@@ -37195,17 +37231,21 @@ async function executeCodeCommand(args = []) {
37195
37231
  claudeProcess.on("error", (error) => {
37196
37232
  console.error("Failed to start claude command:", error.message);
37197
37233
  console.log("Make sure Claude Code is installed: npm install -g @anthropic-ai/claude-code");
37234
+ decrementReferenceCount();
37198
37235
  process.exit(1);
37199
37236
  });
37200
37237
  claudeProcess.on("close", (code) => {
37238
+ decrementReferenceCount();
37239
+ closeService();
37201
37240
  process.exit(code || 0);
37202
37241
  });
37203
37242
  }
37204
37243
 
37205
37244
  // package.json
37206
- var version = "1.0.2";
37245
+ var version = "1.0.1";
37207
37246
 
37208
37247
  // src/cli.ts
37248
+ var import_child_process2 = require("child_process");
37209
37249
  var command = process.argv[2];
37210
37250
  var HELP_TEXT = `
37211
37251
  Usage: claude-code [command]
@@ -37237,7 +37277,7 @@ async function waitForService(timeout = 1e4, initialDelay = 1e3) {
37237
37277
  async function main() {
37238
37278
  switch (command) {
37239
37279
  case "start":
37240
- await run({ daemon: true });
37280
+ run();
37241
37281
  break;
37242
37282
  case "stop":
37243
37283
  await closeService();
@@ -37248,7 +37288,10 @@ async function main() {
37248
37288
  case "code":
37249
37289
  if (!isServiceRunning()) {
37250
37290
  console.log("Service not running, starting service...");
37251
- await run({ daemon: true });
37291
+ (0, import_child_process2.spawn)("ccr", ["start"], {
37292
+ detached: true,
37293
+ stdio: "ignore"
37294
+ }).unref();
37252
37295
  if (await waitForService()) {
37253
37296
  executeCodeCommand(process.argv.slice(3));
37254
37297
  } else {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@musistudio/claude-code-router",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "description": "Use Claude Code without an Anthropics account and route it to another LLM provider",
5
5
  "bin": {
6
6
  "ccr": "./dist/cli.js"