@minesa-org/mini-interaction 0.4.10 → 0.4.12

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.
@@ -80,6 +80,7 @@ export declare class MiniInteraction {
80
80
  private normalizeModuleExports;
81
81
  private normalizeExportValue;
82
82
  private walkFiles;
83
+ private scheduleBackgroundTask;
83
84
  private getDefaultInitialResponse;
84
85
  private isDeferredResponse;
85
86
  private isImportableModule;
@@ -1,6 +1,7 @@
1
1
  import { readFile, readdir } from "node:fs/promises";
2
2
  import path from "node:path";
3
3
  import { pathToFileURL } from "node:url";
4
+ import { waitUntil as vercelWaitUntil } from "@vercel/functions";
4
5
  import { ApplicationCommandType, InteractionResponseType, InteractionType, } from "discord-api-types/v10";
5
6
  import { createCommandInteraction } from "../utils/CommandInteractionOptions.js";
6
7
  import { createAppCommandInteraction, createMessageContextMenuInteraction, createUserContextMenuInteraction, } from "../utils/ContextMenuInteraction.js";
@@ -32,11 +33,16 @@ export class MiniInteraction {
32
33
  createNodeHandler() {
33
34
  return async (req, res) => {
34
35
  let responseSent = false;
36
+ let resolveInitialCommit;
37
+ const initialCommitPromise = new Promise((resolve) => {
38
+ resolveInitialCommit = resolve;
39
+ });
35
40
  const commitInitialResponse = (response) => {
36
41
  if (responseSent)
37
42
  return false;
38
43
  this.sendJson(res, 200, response);
39
44
  responseSent = true;
45
+ resolveInitialCommit?.();
40
46
  return true;
41
47
  };
42
48
  try {
@@ -66,7 +72,24 @@ export class MiniInteraction {
66
72
  this.sendJson(res, 200, { type: InteractionResponseType.Pong });
67
73
  return;
68
74
  }
69
- const response = await this.dispatch(interaction, commitInitialResponse);
75
+ const dispatchPromise = this.dispatch(interaction, commitInitialResponse);
76
+ const backgroundPromise = dispatchPromise.catch((error) => {
77
+ if (this.options.debug) {
78
+ console.error("[MiniInteraction] Background interaction processing failed", error);
79
+ }
80
+ });
81
+ const settled = await Promise.race([
82
+ dispatchPromise.then((response) => ({ kind: "result", response }), (error) => ({ kind: "error", error })),
83
+ initialCommitPromise.then(() => ({ kind: "committed" })),
84
+ ]);
85
+ if (settled.kind === "committed") {
86
+ this.scheduleBackgroundTask(backgroundPromise);
87
+ return;
88
+ }
89
+ if (settled.kind === "error") {
90
+ throw settled.error;
91
+ }
92
+ const response = settled.response;
70
93
  if (!responseSent) {
71
94
  this.sendJson(res, 200, response ?? this.getDefaultInitialResponse(interaction));
72
95
  responseSent = true;
@@ -255,6 +278,10 @@ export class MiniInteraction {
255
278
  const autoDeferMs = Math.min(2500, this.options.timeoutConfig?.initialResponseTimeout ?? 2500);
256
279
  const autoDeferTimer = this.options.timeoutConfig?.autoDeferSlowOperations === true
257
280
  ? setTimeout(() => {
281
+ if (initialResponseCommitted)
282
+ return;
283
+ if (this.responseStates.get(interaction.id))
284
+ return;
258
285
  if (!helpers.canRespond(interaction.id))
259
286
  return;
260
287
  if (this.options.debug || this.options.timeoutConfig?.enableResponseDebugLogging) {
@@ -385,6 +412,14 @@ export class MiniInteraction {
385
412
  }));
386
413
  return results.flat();
387
414
  }
415
+ scheduleBackgroundTask(promise) {
416
+ try {
417
+ vercelWaitUntil(promise);
418
+ }
419
+ catch {
420
+ void promise;
421
+ }
422
+ }
388
423
  getDefaultInitialResponse(interaction) {
389
424
  if (interaction.type === InteractionType.MessageComponent) {
390
425
  return { type: InteractionResponseType.DeferredMessageUpdate };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@minesa-org/mini-interaction",
3
- "version": "0.4.10",
3
+ "version": "0.4.12",
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",