@minesa-org/mini-interaction 0.2.17 → 0.2.18

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.
@@ -1577,6 +1577,8 @@ function createTimeoutWrapper(handler, timeoutMs, handlerName, enableWarnings =
1577
1577
  return async (...args) => {
1578
1578
  const startTime = Date.now();
1579
1579
  let timeoutId;
1580
+ let ackResult = null;
1581
+ let ackReceived = false;
1580
1582
  const timeoutPromise = new Promise((_, reject) => {
1581
1583
  timeoutId = setTimeout(() => {
1582
1584
  const elapsed = Date.now() - startTime;
@@ -1584,47 +1586,56 @@ function createTimeoutWrapper(handler, timeoutMs, handlerName, enableWarnings =
1584
1586
  reject(new Error(`Handler timeout: ${handlerName} exceeded ${timeoutMs}ms limit`));
1585
1587
  }, timeoutMs);
1586
1588
  });
1587
- // Create the handler promise and handle its potential background errors
1588
- const handlerPromise = (async () => {
1589
- try {
1590
- return await handler(...args);
1591
- }
1592
- catch (error) {
1593
- // If this error happens AFTER an ACK was already sent (via ackPromise)
1594
- // or after a timeout, we MUST still log it because nobody else will catch it.
1595
- console.error(`[MiniInteraction] ${handlerName} background error:`, error);
1596
- throw error;
1597
- }
1598
- })();
1589
+ // If we have an ackPromise, listen for it but DON'T return early
1590
+ // Instead, capture the ACK result and continue waiting for handler
1591
+ if (ackPromise) {
1592
+ ackPromise.then((result) => {
1593
+ ackResult = result;
1594
+ ackReceived = true;
1595
+ // Clear the timeout once we have an ACK - handler can now take longer
1596
+ if (timeoutId) {
1597
+ clearTimeout(timeoutId);
1598
+ timeoutId = undefined;
1599
+ }
1600
+ }).catch(() => {
1601
+ // ACK promise rejection is fine, we'll use handler result
1602
+ });
1603
+ }
1599
1604
  try {
1600
- const promises = [
1601
- handlerPromise,
1605
+ // ALWAYS wait for handler to complete
1606
+ const handlerResult = await Promise.race([
1607
+ handler(...args),
1602
1608
  timeoutPromise,
1603
- ];
1604
- if (ackPromise) {
1605
- promises.push(ackPromise);
1606
- }
1607
- const result = await Promise.race(promises);
1609
+ ]);
1608
1610
  if (timeoutId) {
1609
1611
  clearTimeout(timeoutId);
1610
1612
  }
1611
1613
  const elapsed = Date.now() - startTime;
1612
- if (enableWarnings && elapsed > timeoutMs * 0.8) {
1614
+ if (enableWarnings && elapsed > timeoutMs * 0.8 && !ackReceived) {
1613
1615
  console.warn(`[MiniInteraction] ${handlerName} completed in ${elapsed}ms (${Math.round((elapsed / timeoutMs) * 100)}% of timeout limit)`);
1614
1616
  }
1615
- return result;
1617
+ // If we got an ACK, return that for the HTTP response
1618
+ // The handler has completed at this point so all background work is done
1619
+ if (ackReceived && ackResult !== null) {
1620
+ return ackResult;
1621
+ }
1622
+ return handlerResult;
1616
1623
  }
1617
1624
  catch (error) {
1618
1625
  if (timeoutId) {
1619
1626
  clearTimeout(timeoutId);
1620
1627
  }
1621
- // Background errors are already logged above
1622
- if (error instanceof Error &&
1623
- error.message.includes("Handler timeout")) {
1628
+ // If handler timed out but we have an ACK, return the ACK
1629
+ // This allows deferReply to work even if later operations are slow
1630
+ if (error instanceof Error && error.message.includes("Handler timeout")) {
1631
+ if (ackReceived && ackResult !== null) {
1632
+ console.warn(`[MiniInteraction] ${handlerName} timed out but ACK was already captured. ` +
1633
+ `Background work may not complete in serverless environments.`);
1634
+ return ackResult;
1635
+ }
1624
1636
  throw error;
1625
1637
  }
1626
- // Only log here if it wasn't a background error (which we already caught)
1627
- // But since we catch all in handlerPromise, this is mostly for the timeout/race itself.
1638
+ console.error(`[MiniInteraction] ${handlerName} failed:`, error);
1628
1639
  throw error;
1629
1640
  }
1630
1641
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@minesa-org/mini-interaction",
3
- "version": "0.2.17",
3
+ "version": "0.2.18",
4
4
  "description": "Mini interaction, connecting your app with Discord via HTTP-interaction (Vercel support).",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",