hybrid 1.2.6 → 1.2.7

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.cjs CHANGED
@@ -120,8 +120,24 @@ function generateXMTPToolsToken(payload) {
120
120
  var BG_STARTED = Symbol("BG_STARTED");
121
121
  var BG_STATE = Symbol("BG_STATE");
122
122
  var BG_STOP = Symbol("BG_STOP");
123
- function sleep(ms) {
124
- return new Promise((r) => setTimeout(r, ms));
123
+ function sleep(ms, signal) {
124
+ return new Promise((resolve, reject) => {
125
+ if (signal?.aborted) {
126
+ reject(new Error("AbortError"));
127
+ return;
128
+ }
129
+ const timeout = setTimeout(resolve, ms);
130
+ if (signal) {
131
+ signal.addEventListener(
132
+ "abort",
133
+ () => {
134
+ clearTimeout(timeout);
135
+ reject(new Error("AbortError"));
136
+ },
137
+ { once: true }
138
+ );
139
+ }
140
+ });
125
141
  }
126
142
  function createBackgroundMessageProcessor(opts) {
127
143
  if (!opts?.agent || !opts?.xmtpClient) {
@@ -294,10 +310,18 @@ function createBackgroundMessageProcessor(opts) {
294
310
  );
295
311
  }
296
312
  }
297
- await sleep(nextDelay);
313
+ try {
314
+ await sleep(nextDelay, signal);
315
+ } catch (error) {
316
+ if (error instanceof Error && error.name === "AbortError") {
317
+ break;
318
+ }
319
+ throw error;
320
+ }
298
321
  }
299
322
  try {
300
323
  if (state.listenerRunning) {
324
+ console.log("[XMTP Background] Stopping message listener...");
301
325
  await listener.stop();
302
326
  }
303
327
  } catch (error) {
@@ -392,11 +416,51 @@ async function listen({
392
416
  app.notFound((c) => {
393
417
  return c.json({ error: "Not found" }, 404);
394
418
  });
395
- (0, import_node_server.serve)({
396
- fetch: app.fetch,
397
- port: Number.parseInt(port || "8454")
419
+ const httpPort = Number.parseInt(port || "8454");
420
+ const shutdown = async () => {
421
+ console.log("Waiting for graceful termination...");
422
+ try {
423
+ stopBackground();
424
+ } catch (error) {
425
+ console.error("Error stopping background processor:", error);
426
+ }
427
+ await new Promise((resolve) => setTimeout(resolve, 1e3));
428
+ };
429
+ process.once("SIGINT", async () => {
430
+ await shutdown();
431
+ process.exit(0);
432
+ });
433
+ process.once("SIGTERM", async () => {
434
+ await shutdown();
435
+ process.exit(0);
436
+ });
437
+ process.on("uncaughtException", async (error) => {
438
+ console.error("Uncaught exception:", error);
439
+ await shutdown();
440
+ process.exit(1);
398
441
  });
399
- console.log(`\u2705 XMTP Tools HTTP Server running`);
442
+ process.on("unhandledRejection", async (reason) => {
443
+ console.error("Unhandled rejection:", reason);
444
+ await shutdown();
445
+ process.exit(1);
446
+ });
447
+ try {
448
+ (0, import_node_server.serve)({
449
+ fetch: app.fetch,
450
+ port: httpPort
451
+ });
452
+ console.log(`\u2705 XMTP Tools HTTP Server running on port ${httpPort}`);
453
+ } catch (error) {
454
+ if (error.code === "EADDRINUSE") {
455
+ console.error(
456
+ `\u274C Port ${httpPort} is already in use. Please stop the existing server or use a different port.`
457
+ );
458
+ process.exit(1);
459
+ } else {
460
+ console.error("Server error:", error);
461
+ process.exit(1);
462
+ }
463
+ }
400
464
  }
401
465
 
402
466
  // src/core/plugin.ts