@theokit/gateway 0.22.1 → 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/CHANGELOG.md CHANGED
@@ -1,5 +1,19 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.25.0
4
+
5
+ ### Minor Changes
6
+
7
+ - a0b59f4: `ChannelWebhook.verify` may return a `Promise<boolean>`, and `createWebhookHandler` awaits it.
8
+
9
+ `ChannelWebhook.precheck` answers a request from its method, URL and headers before the body is read.
10
+
11
+ ## 0.24.0
12
+
13
+ ### Minor Changes
14
+
15
+ - 35d9d91: `channelConfigReaders` gains `readNumber` (a number or a digit string), `readBooleanString` (a boolean or `"true"`/`"false"`) and `readList` (an array or a comma-separated string), the string forms `configFromEnv` yields.
16
+
3
17
  ## 0.22.1
4
18
 
5
19
  ### Patch Changes
package/dist/index.cjs CHANGED
@@ -603,6 +603,32 @@ function channelConfigReaders(prefix, createError = (options) => new GatewayConf
603
603
  if (typeof value !== "boolean") throw invalid2(key, "a boolean");
604
604
  return value;
605
605
  },
606
+ readNumber(config, key) {
607
+ const value = config[key];
608
+ if (isAbsent2(value)) return void 0;
609
+ if (typeof value === "number" && Number.isFinite(value)) return value;
610
+ if (typeof value === "string" && /^\d+$/.test(value)) return Number(value);
611
+ throw invalid2(key, "a number or a digit string");
612
+ },
613
+ readBooleanString(config, key) {
614
+ const value = config[key];
615
+ if (isAbsent2(value)) return void 0;
616
+ if (typeof value === "boolean") return value;
617
+ if (value === "true") return true;
618
+ if (value === "false") return false;
619
+ throw invalid2(key, 'a boolean or "true"/"false"');
620
+ },
621
+ readList(config, key) {
622
+ const value = config[key];
623
+ if (isAbsent2(value)) return void 0;
624
+ if (typeof value === "string") {
625
+ return value.split(",").map((item) => item.trim()).filter((item) => item !== "");
626
+ }
627
+ if (!Array.isArray(value) || !value.every((item) => typeof item === "string")) {
628
+ throw invalid2(key, "an array of strings or a comma-separated string");
629
+ }
630
+ return value;
631
+ },
606
632
  readFunction(config, key) {
607
633
  const value = config[key];
608
634
  if (isAbsent2(value)) return void 0;
@@ -2322,7 +2348,7 @@ function answerResponse(answer) {
2322
2348
  return new Response(answer.body ?? null, { status: answer.status, headers });
2323
2349
  }
2324
2350
  async function runDefault(webhook, adapter, request) {
2325
- if (!webhook.verify(adapter, request)) return plain(401);
2351
+ if (!await webhook.verify(adapter, request)) return plain(401);
2326
2352
  let payload;
2327
2353
  try {
2328
2354
  payload = JSON.parse(request.rawBody);
@@ -2367,7 +2393,7 @@ function createWebhookHandler(options) {
2367
2393
  const platform = channel.descriptor.platform;
2368
2394
  if (!options.isStarted(platform)) return plain(503);
2369
2395
  const webhook = channel.descriptor.webhook;
2370
- const refused2 = refuseMethod(webhook, request);
2396
+ const refused2 = refuseEarly(webhook, channel.adapter, request);
2371
2397
  if (refused2 !== void 0) return refused2;
2372
2398
  const rawBody = await readBody(request, max);
2373
2399
  if (typeof rawBody !== "string") return rawBody;
@@ -2384,6 +2410,18 @@ function createWebhookHandler(options) {
2384
2410
  }
2385
2411
  };
2386
2412
  }
2413
+ function refuseEarly(webhook, adapter, request) {
2414
+ return refuseMethod(webhook, request) ?? precheckAnswer(webhook, adapter, request);
2415
+ }
2416
+ function precheckAnswer(webhook, adapter, request) {
2417
+ if (webhook.precheck === void 0) return void 0;
2418
+ const answer = webhook.precheck(adapter, {
2419
+ headers: headersOf(request),
2420
+ method: request.method.toUpperCase(),
2421
+ url: request.url
2422
+ });
2423
+ return answer === void 0 ? void 0 : answerResponse(answer);
2424
+ }
2387
2425
  function refuseMethod(webhook, request) {
2388
2426
  const methods = webhook.methods ?? ["POST"];
2389
2427
  const method = request.method.toUpperCase();