@gonzih/cc-tg 0.3.1 → 0.3.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.
- package/dist/claude.js +1 -1
- package/dist/index.js +35 -20
- package/package.json +1 -1
package/dist/claude.js
CHANGED
package/dist/index.js
CHANGED
|
@@ -14,31 +14,46 @@
|
|
|
14
14
|
* GROUP_CHAT_IDS — comma-separated Telegram group/supergroup chat IDs (leave empty to allow all groups)
|
|
15
15
|
* CWD — working directory for Claude Code (default: process.cwd())
|
|
16
16
|
*/
|
|
17
|
-
import {
|
|
17
|
+
import { createServer, createConnection } from "net";
|
|
18
|
+
import { unlinkSync } from "fs";
|
|
18
19
|
import { tmpdir } from "os";
|
|
19
20
|
import { join } from "path";
|
|
20
21
|
import { CcTgBot } from "./bot.js";
|
|
21
|
-
const
|
|
22
|
+
const LOCK_SOCKET = join(tmpdir(), "cc-tg.sock");
|
|
22
23
|
function acquireLock() {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
24
|
+
return new Promise((resolve) => {
|
|
25
|
+
const server = createServer();
|
|
26
|
+
server.listen(LOCK_SOCKET, () => {
|
|
27
|
+
// Bound successfully — we own the lock. Socket auto-released on any exit incl. SIGKILL.
|
|
28
|
+
resolve(true);
|
|
29
|
+
});
|
|
30
|
+
server.on("error", (err) => {
|
|
31
|
+
if (err.code !== "EADDRINUSE") {
|
|
32
|
+
resolve(true); // unrelated error, proceed
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
// Socket path exists — probe if anything is actually listening
|
|
36
|
+
const probe = createConnection(LOCK_SOCKET);
|
|
37
|
+
probe.on("connect", () => {
|
|
38
|
+
probe.destroy();
|
|
39
|
+
console.error("[cc-tg] Another instance is already running. Exiting.");
|
|
40
|
+
resolve(false);
|
|
41
|
+
});
|
|
42
|
+
probe.on("error", () => {
|
|
43
|
+
// Nothing listening — stale socket, remove and retry
|
|
44
|
+
try {
|
|
45
|
+
unlinkSync(LOCK_SOCKET);
|
|
46
|
+
}
|
|
47
|
+
catch { }
|
|
48
|
+
const retry = createServer();
|
|
49
|
+
retry.listen(LOCK_SOCKET, () => resolve(true));
|
|
50
|
+
retry.on("error", () => resolve(true)); // give up on lock, just start
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
});
|
|
40
54
|
}
|
|
41
|
-
|
|
55
|
+
const lockAcquired = await acquireLock();
|
|
56
|
+
if (!lockAcquired) {
|
|
42
57
|
process.exit(1);
|
|
43
58
|
}
|
|
44
59
|
function required(name) {
|