@theokit/gateway 0.24.0 → 0.25.0

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.d.cts CHANGED
@@ -1246,11 +1246,16 @@ type IngestResult = "delivered" | "malformed" | "no_handler";
1246
1246
  */
1247
1247
  interface ChannelWebhook<TAdapter extends BasePlatformAdapter> {
1248
1248
  /** `true` when the request carries a valid signature for this adapter's credentials. */
1249
- verify(adapter: TAdapter, request: WebhookRequest): boolean;
1249
+ verify(adapter: TAdapter, request: WebhookRequest): boolean | Promise<boolean>;
1250
1250
  /** Dispatch a parsed, verified payload, or report it malformed without dispatching. */
1251
1251
  ingest(adapter: TAdapter, payload: unknown): Promise<IngestResult>;
1252
1252
  /** Methods the route takes. Default `["POST"]`. */
1253
1253
  readonly methods?: readonly string[];
1254
+ /**
1255
+ * Called with the method, URL and headers before the body is read. An answer refuses the request
1256
+ * there (an unreadable or oversized body never gets a 400 or 413); `undefined` lets it go on.
1257
+ */
1258
+ precheck?(adapter: TAdapter, request: Omit<WebhookRequest, "rawBody">): WebhookAnswer | undefined;
1254
1259
  /** Full control of one request. When absent, the handler runs verify, JSON.parse and ingest. */
1255
1260
  handle?(adapter: TAdapter, request: WebhookRequest): Promise<WebhookAnswer>;
1256
1261
  }
package/dist/index.d.ts CHANGED
@@ -1246,11 +1246,16 @@ type IngestResult = "delivered" | "malformed" | "no_handler";
1246
1246
  */
1247
1247
  interface ChannelWebhook<TAdapter extends BasePlatformAdapter> {
1248
1248
  /** `true` when the request carries a valid signature for this adapter's credentials. */
1249
- verify(adapter: TAdapter, request: WebhookRequest): boolean;
1249
+ verify(adapter: TAdapter, request: WebhookRequest): boolean | Promise<boolean>;
1250
1250
  /** Dispatch a parsed, verified payload, or report it malformed without dispatching. */
1251
1251
  ingest(adapter: TAdapter, payload: unknown): Promise<IngestResult>;
1252
1252
  /** Methods the route takes. Default `["POST"]`. */
1253
1253
  readonly methods?: readonly string[];
1254
+ /**
1255
+ * Called with the method, URL and headers before the body is read. An answer refuses the request
1256
+ * there (an unreadable or oversized body never gets a 400 or 413); `undefined` lets it go on.
1257
+ */
1258
+ precheck?(adapter: TAdapter, request: Omit<WebhookRequest, "rawBody">): WebhookAnswer | undefined;
1254
1259
  /** Full control of one request. When absent, the handler runs verify, JSON.parse and ingest. */
1255
1260
  handle?(adapter: TAdapter, request: WebhookRequest): Promise<WebhookAnswer>;
1256
1261
  }
package/dist/index.js CHANGED
@@ -2346,7 +2346,7 @@ function answerResponse(answer) {
2346
2346
  return new Response(answer.body ?? null, { status: answer.status, headers });
2347
2347
  }
2348
2348
  async function runDefault(webhook, adapter, request) {
2349
- if (!webhook.verify(adapter, request)) return plain(401);
2349
+ if (!await webhook.verify(adapter, request)) return plain(401);
2350
2350
  let payload;
2351
2351
  try {
2352
2352
  payload = JSON.parse(request.rawBody);
@@ -2391,7 +2391,7 @@ function createWebhookHandler(options) {
2391
2391
  const platform = channel.descriptor.platform;
2392
2392
  if (!options.isStarted(platform)) return plain(503);
2393
2393
  const webhook = channel.descriptor.webhook;
2394
- const refused2 = refuseMethod(webhook, request);
2394
+ const refused2 = refuseEarly(webhook, channel.adapter, request);
2395
2395
  if (refused2 !== void 0) return refused2;
2396
2396
  const rawBody = await readBody(request, max);
2397
2397
  if (typeof rawBody !== "string") return rawBody;
@@ -2408,6 +2408,18 @@ function createWebhookHandler(options) {
2408
2408
  }
2409
2409
  };
2410
2410
  }
2411
+ function refuseEarly(webhook, adapter, request) {
2412
+ return refuseMethod(webhook, request) ?? precheckAnswer(webhook, adapter, request);
2413
+ }
2414
+ function precheckAnswer(webhook, adapter, request) {
2415
+ if (webhook.precheck === void 0) return void 0;
2416
+ const answer = webhook.precheck(adapter, {
2417
+ headers: headersOf(request),
2418
+ method: request.method.toUpperCase(),
2419
+ url: request.url
2420
+ });
2421
+ return answer === void 0 ? void 0 : answerResponse(answer);
2422
+ }
2411
2423
  function refuseMethod(webhook, request) {
2412
2424
  const methods = webhook.methods ?? ["POST"];
2413
2425
  const method = request.method.toUpperCase();