@delt/claude-alarm 0.8.0 → 0.8.1
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/cli.js +108 -31
- package/dist/cli.js.map +1 -1
- package/dist/hub/server.js +74 -10
- package/dist/hub/server.js.map +1 -1
- package/dist/index.js +74 -10
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/hub/server.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
// src/hub/server.ts
|
|
4
4
|
import http from "http";
|
|
5
|
-
import
|
|
5
|
+
import fs4 from "fs";
|
|
6
6
|
import path4 from "path";
|
|
7
7
|
import { fileURLToPath } from "url";
|
|
8
8
|
import { WebSocketServer, WebSocket } from "ws";
|
|
@@ -653,6 +653,54 @@ function saveConfig(config) {
|
|
|
653
653
|
fs2.writeFileSync(CONFIG_FILE, JSON.stringify(config, null, 2), { encoding: "utf-8", mode: 384 });
|
|
654
654
|
}
|
|
655
655
|
|
|
656
|
+
// src/shared/crash-guard.ts
|
|
657
|
+
import fs3 from "fs";
|
|
658
|
+
var BURST_WINDOW_MS = 6e4;
|
|
659
|
+
var BURST_LIMIT = 10;
|
|
660
|
+
function appendToLog(text) {
|
|
661
|
+
try {
|
|
662
|
+
fs3.mkdirSync(CONFIG_DIR, { recursive: true });
|
|
663
|
+
fs3.appendFileSync(LOG_FILE, text, "utf-8");
|
|
664
|
+
} catch {
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
function describe(err) {
|
|
668
|
+
if (err instanceof Error) return err.stack ?? `${err.name}: ${err.message}`;
|
|
669
|
+
try {
|
|
670
|
+
return JSON.stringify(err);
|
|
671
|
+
} catch {
|
|
672
|
+
return String(err);
|
|
673
|
+
}
|
|
674
|
+
}
|
|
675
|
+
function installCrashGuard(context) {
|
|
676
|
+
let recent = [];
|
|
677
|
+
const handle = (kind, err) => {
|
|
678
|
+
const stack = describe(err);
|
|
679
|
+
appendToLog(`
|
|
680
|
+
[${(/* @__PURE__ */ new Date()).toISOString()}] ${kind} (${context})
|
|
681
|
+
${stack}
|
|
682
|
+
`);
|
|
683
|
+
logger.error(`${kind} caught by crash guard: ${stack}`);
|
|
684
|
+
const now = Date.now();
|
|
685
|
+
recent = recent.filter((t) => now - t < BURST_WINDOW_MS);
|
|
686
|
+
recent.push(now);
|
|
687
|
+
if (recent.length > BURST_LIMIT) {
|
|
688
|
+
const msg = `${recent.length} unhandled errors within ${BURST_WINDOW_MS / 1e3}s \u2014 exiting`;
|
|
689
|
+
appendToLog(`[${(/* @__PURE__ */ new Date()).toISOString()}] ${msg}
|
|
690
|
+
`);
|
|
691
|
+
logger.error(msg);
|
|
692
|
+
process.exit(1);
|
|
693
|
+
}
|
|
694
|
+
};
|
|
695
|
+
process.on("uncaughtException", (err) => handle("uncaughtException", err));
|
|
696
|
+
process.on("unhandledRejection", (reason) => handle("unhandledRejection", reason));
|
|
697
|
+
}
|
|
698
|
+
function logStartup(context) {
|
|
699
|
+
appendToLog(`
|
|
700
|
+
[${(/* @__PURE__ */ new Date()).toISOString()}] ${context} started (pid ${process.pid})
|
|
701
|
+
`);
|
|
702
|
+
}
|
|
703
|
+
|
|
656
704
|
// src/hub/server.ts
|
|
657
705
|
var __dirname = path4.dirname(fileURLToPath(import.meta.url));
|
|
658
706
|
var HubServer = class {
|
|
@@ -696,9 +744,12 @@ var HubServer = class {
|
|
|
696
744
|
this.httpServer = http.createServer((req, res) => this.handleHttp(req, res));
|
|
697
745
|
this.wssChannel = new WebSocketServer({ noServer: true });
|
|
698
746
|
this.wssChannel.on("connection", (ws, req) => this.handleChannelConnection(ws, req));
|
|
747
|
+
this.wssChannel.on("error", (err) => logger.warn(`Channel WebSocket server error: ${err.message}`));
|
|
699
748
|
this.wssDashboard = new WebSocketServer({ noServer: true });
|
|
700
749
|
this.wssDashboard.on("connection", (ws) => this.handleDashboardConnection(ws));
|
|
750
|
+
this.wssDashboard.on("error", (err) => logger.warn(`Dashboard WebSocket server error: ${err.message}`));
|
|
701
751
|
this.httpServer.on("upgrade", (req, socket, head) => {
|
|
752
|
+
socket.on("error", (err) => logger.debug(`Upgrade socket error: ${err.message}`));
|
|
702
753
|
const url = new URL(req.url, `http://${req.headers.host}`);
|
|
703
754
|
const pathname = url.pathname;
|
|
704
755
|
if (this.token && !this.isLocalRequest(req)) {
|
|
@@ -725,8 +776,11 @@ var HubServer = class {
|
|
|
725
776
|
this.cleanupUploads();
|
|
726
777
|
this.startHeartbeat();
|
|
727
778
|
return new Promise((resolve, reject) => {
|
|
728
|
-
|
|
779
|
+
const onStartupError = (err) => reject(err);
|
|
780
|
+
this.httpServer.once("error", onStartupError);
|
|
729
781
|
this.httpServer.listen(this.port, this.host, () => {
|
|
782
|
+
this.httpServer.removeListener("error", onStartupError);
|
|
783
|
+
this.httpServer.on("error", (err) => logger.error(`HTTP server error: ${err.message}`));
|
|
730
784
|
const displayHost = this.host === "0.0.0.0" ? "127.0.0.1" : this.host;
|
|
731
785
|
logger.info(`Hub server listening on http://${displayHost}:${this.port}`);
|
|
732
786
|
resolve();
|
|
@@ -847,8 +901,8 @@ var HubServer = class {
|
|
|
847
901
|
];
|
|
848
902
|
logger.debug(`Dashboard candidates: ${JSON.stringify(candidates)}`);
|
|
849
903
|
for (const candidate of candidates) {
|
|
850
|
-
if (
|
|
851
|
-
const html =
|
|
904
|
+
if (fs4.existsSync(candidate)) {
|
|
905
|
+
const html = fs4.readFileSync(candidate, "utf-8");
|
|
852
906
|
res.writeHead(200, { "Content-Type": "text/html; charset=utf-8" });
|
|
853
907
|
res.end(html);
|
|
854
908
|
return;
|
|
@@ -895,6 +949,10 @@ var HubServer = class {
|
|
|
895
949
|
handleChannelConnection(ws, req) {
|
|
896
950
|
const isLocal = this.isLocalRequest(req);
|
|
897
951
|
logger.info(`Channel server connected (local: ${isLocal})`);
|
|
952
|
+
ws.on("error", (err) => {
|
|
953
|
+
logger.warn(`Channel WebSocket error: ${err.message}`);
|
|
954
|
+
ws.terminate();
|
|
955
|
+
});
|
|
898
956
|
ws.on("pong", () => {
|
|
899
957
|
for (const [sessionId, sock] of this.channelSockets) {
|
|
900
958
|
if (sock === ws) {
|
|
@@ -1004,6 +1062,10 @@ var HubServer = class {
|
|
|
1004
1062
|
handleDashboardConnection(ws) {
|
|
1005
1063
|
this.dashboardSockets.add(ws);
|
|
1006
1064
|
logger.info(`Dashboard connected (total: ${this.dashboardSockets.size})`);
|
|
1065
|
+
ws.on("error", (err) => {
|
|
1066
|
+
logger.warn(`Dashboard WebSocket error: ${err.message}`);
|
|
1067
|
+
ws.terminate();
|
|
1068
|
+
});
|
|
1007
1069
|
const sessionsMsg = {
|
|
1008
1070
|
type: "sessions_list",
|
|
1009
1071
|
sessions: this.sessions.getAll()
|
|
@@ -1060,11 +1122,11 @@ var HubServer = class {
|
|
|
1060
1122
|
logger.warn("Image upload rejected: exceeds 10MB");
|
|
1061
1123
|
return;
|
|
1062
1124
|
}
|
|
1063
|
-
|
|
1125
|
+
fs4.mkdirSync(UPLOADS_DIR, { recursive: true });
|
|
1064
1126
|
const ext = mimeType.split("/")[1] === "jpeg" ? "jpg" : mimeType.split("/")[1];
|
|
1065
1127
|
const filename = `${randomUUID3()}.${ext}`;
|
|
1066
1128
|
const filePath = path4.join(UPLOADS_DIR, filename);
|
|
1067
|
-
|
|
1129
|
+
fs4.writeFileSync(filePath, buffer);
|
|
1068
1130
|
const forwardMsg = {
|
|
1069
1131
|
type: "image_to_session",
|
|
1070
1132
|
sessionId,
|
|
@@ -1077,7 +1139,7 @@ var HubServer = class {
|
|
|
1077
1139
|
logger.info(`Image saved and forwarded: ${filename} (${buffer.length} bytes)`);
|
|
1078
1140
|
setTimeout(() => {
|
|
1079
1141
|
try {
|
|
1080
|
-
|
|
1142
|
+
fs4.unlinkSync(filePath);
|
|
1081
1143
|
} catch {
|
|
1082
1144
|
}
|
|
1083
1145
|
}, 5 * 60 * 1e3);
|
|
@@ -1247,11 +1309,11 @@ var HubServer = class {
|
|
|
1247
1309
|
}
|
|
1248
1310
|
cleanupUploads() {
|
|
1249
1311
|
try {
|
|
1250
|
-
if (!
|
|
1251
|
-
const files =
|
|
1312
|
+
if (!fs4.existsSync(UPLOADS_DIR)) return;
|
|
1313
|
+
const files = fs4.readdirSync(UPLOADS_DIR);
|
|
1252
1314
|
for (const file of files) {
|
|
1253
1315
|
try {
|
|
1254
|
-
|
|
1316
|
+
fs4.unlinkSync(path4.join(UPLOADS_DIR, file));
|
|
1255
1317
|
} catch {
|
|
1256
1318
|
}
|
|
1257
1319
|
}
|
|
@@ -1295,6 +1357,8 @@ var HubServer = class {
|
|
|
1295
1357
|
}
|
|
1296
1358
|
};
|
|
1297
1359
|
if (process.argv[1] && (process.argv[1].endsWith("hub/server.js") || process.argv[1].endsWith("hub/server.ts"))) {
|
|
1360
|
+
installCrashGuard("hub daemon");
|
|
1361
|
+
logStartup("Hub daemon");
|
|
1298
1362
|
const config = loadConfig();
|
|
1299
1363
|
const hub = new HubServer(config);
|
|
1300
1364
|
hub.start().catch((err) => {
|