@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 +14 -0
- package/dist/index.cjs +40 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +12 -1
- package/dist/index.d.ts +12 -1
- package/dist/index.js +40 -2
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.cts
CHANGED
|
@@ -1159,6 +1159,12 @@ interface ChannelConfigReaders {
|
|
|
1159
1159
|
requiredString(config: ChannelConfigInput, key: string): string;
|
|
1160
1160
|
/** An optional boolean; absent gives `undefined`, a non-boolean throws. */
|
|
1161
1161
|
readBoolean(config: ChannelConfigInput, key: string): boolean | undefined;
|
|
1162
|
+
/** An optional number: a finite number or a digit string; anything else throws. */
|
|
1163
|
+
readNumber(config: ChannelConfigInput, key: string): number | undefined;
|
|
1164
|
+
/** An optional boolean as a boolean or the string `"true"`/`"false"`; anything else throws. */
|
|
1165
|
+
readBooleanString(config: ChannelConfigInput, key: string): boolean | undefined;
|
|
1166
|
+
/** An optional list of strings: an array or a comma-separated string (items trimmed). */
|
|
1167
|
+
readList(config: ChannelConfigInput, key: string): string[] | undefined;
|
|
1162
1168
|
/**
|
|
1163
1169
|
* An optional function; absent gives `undefined`, a non-function throws. Only callability is
|
|
1164
1170
|
* checked: the caller names the signature `T` and is responsible for it.
|
|
@@ -1240,11 +1246,16 @@ type IngestResult = "delivered" | "malformed" | "no_handler";
|
|
|
1240
1246
|
*/
|
|
1241
1247
|
interface ChannelWebhook<TAdapter extends BasePlatformAdapter> {
|
|
1242
1248
|
/** `true` when the request carries a valid signature for this adapter's credentials. */
|
|
1243
|
-
verify(adapter: TAdapter, request: WebhookRequest): boolean
|
|
1249
|
+
verify(adapter: TAdapter, request: WebhookRequest): boolean | Promise<boolean>;
|
|
1244
1250
|
/** Dispatch a parsed, verified payload, or report it malformed without dispatching. */
|
|
1245
1251
|
ingest(adapter: TAdapter, payload: unknown): Promise<IngestResult>;
|
|
1246
1252
|
/** Methods the route takes. Default `["POST"]`. */
|
|
1247
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;
|
|
1248
1259
|
/** Full control of one request. When absent, the handler runs verify, JSON.parse and ingest. */
|
|
1249
1260
|
handle?(adapter: TAdapter, request: WebhookRequest): Promise<WebhookAnswer>;
|
|
1250
1261
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1159,6 +1159,12 @@ interface ChannelConfigReaders {
|
|
|
1159
1159
|
requiredString(config: ChannelConfigInput, key: string): string;
|
|
1160
1160
|
/** An optional boolean; absent gives `undefined`, a non-boolean throws. */
|
|
1161
1161
|
readBoolean(config: ChannelConfigInput, key: string): boolean | undefined;
|
|
1162
|
+
/** An optional number: a finite number or a digit string; anything else throws. */
|
|
1163
|
+
readNumber(config: ChannelConfigInput, key: string): number | undefined;
|
|
1164
|
+
/** An optional boolean as a boolean or the string `"true"`/`"false"`; anything else throws. */
|
|
1165
|
+
readBooleanString(config: ChannelConfigInput, key: string): boolean | undefined;
|
|
1166
|
+
/** An optional list of strings: an array or a comma-separated string (items trimmed). */
|
|
1167
|
+
readList(config: ChannelConfigInput, key: string): string[] | undefined;
|
|
1162
1168
|
/**
|
|
1163
1169
|
* An optional function; absent gives `undefined`, a non-function throws. Only callability is
|
|
1164
1170
|
* checked: the caller names the signature `T` and is responsible for it.
|
|
@@ -1240,11 +1246,16 @@ type IngestResult = "delivered" | "malformed" | "no_handler";
|
|
|
1240
1246
|
*/
|
|
1241
1247
|
interface ChannelWebhook<TAdapter extends BasePlatformAdapter> {
|
|
1242
1248
|
/** `true` when the request carries a valid signature for this adapter's credentials. */
|
|
1243
|
-
verify(adapter: TAdapter, request: WebhookRequest): boolean
|
|
1249
|
+
verify(adapter: TAdapter, request: WebhookRequest): boolean | Promise<boolean>;
|
|
1244
1250
|
/** Dispatch a parsed, verified payload, or report it malformed without dispatching. */
|
|
1245
1251
|
ingest(adapter: TAdapter, payload: unknown): Promise<IngestResult>;
|
|
1246
1252
|
/** Methods the route takes. Default `["POST"]`. */
|
|
1247
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;
|
|
1248
1259
|
/** Full control of one request. When absent, the handler runs verify, JSON.parse and ingest. */
|
|
1249
1260
|
handle?(adapter: TAdapter, request: WebhookRequest): Promise<WebhookAnswer>;
|
|
1250
1261
|
}
|
package/dist/index.js
CHANGED
|
@@ -601,6 +601,32 @@ function channelConfigReaders(prefix, createError = (options) => new GatewayConf
|
|
|
601
601
|
if (typeof value !== "boolean") throw invalid2(key, "a boolean");
|
|
602
602
|
return value;
|
|
603
603
|
},
|
|
604
|
+
readNumber(config, key) {
|
|
605
|
+
const value = config[key];
|
|
606
|
+
if (isAbsent2(value)) return void 0;
|
|
607
|
+
if (typeof value === "number" && Number.isFinite(value)) return value;
|
|
608
|
+
if (typeof value === "string" && /^\d+$/.test(value)) return Number(value);
|
|
609
|
+
throw invalid2(key, "a number or a digit string");
|
|
610
|
+
},
|
|
611
|
+
readBooleanString(config, key) {
|
|
612
|
+
const value = config[key];
|
|
613
|
+
if (isAbsent2(value)) return void 0;
|
|
614
|
+
if (typeof value === "boolean") return value;
|
|
615
|
+
if (value === "true") return true;
|
|
616
|
+
if (value === "false") return false;
|
|
617
|
+
throw invalid2(key, 'a boolean or "true"/"false"');
|
|
618
|
+
},
|
|
619
|
+
readList(config, key) {
|
|
620
|
+
const value = config[key];
|
|
621
|
+
if (isAbsent2(value)) return void 0;
|
|
622
|
+
if (typeof value === "string") {
|
|
623
|
+
return value.split(",").map((item) => item.trim()).filter((item) => item !== "");
|
|
624
|
+
}
|
|
625
|
+
if (!Array.isArray(value) || !value.every((item) => typeof item === "string")) {
|
|
626
|
+
throw invalid2(key, "an array of strings or a comma-separated string");
|
|
627
|
+
}
|
|
628
|
+
return value;
|
|
629
|
+
},
|
|
604
630
|
readFunction(config, key) {
|
|
605
631
|
const value = config[key];
|
|
606
632
|
if (isAbsent2(value)) return void 0;
|
|
@@ -2320,7 +2346,7 @@ function answerResponse(answer) {
|
|
|
2320
2346
|
return new Response(answer.body ?? null, { status: answer.status, headers });
|
|
2321
2347
|
}
|
|
2322
2348
|
async function runDefault(webhook, adapter, request) {
|
|
2323
|
-
if (!webhook.verify(adapter, request)) return plain(401);
|
|
2349
|
+
if (!await webhook.verify(adapter, request)) return plain(401);
|
|
2324
2350
|
let payload;
|
|
2325
2351
|
try {
|
|
2326
2352
|
payload = JSON.parse(request.rawBody);
|
|
@@ -2365,7 +2391,7 @@ function createWebhookHandler(options) {
|
|
|
2365
2391
|
const platform = channel.descriptor.platform;
|
|
2366
2392
|
if (!options.isStarted(platform)) return plain(503);
|
|
2367
2393
|
const webhook = channel.descriptor.webhook;
|
|
2368
|
-
const refused2 =
|
|
2394
|
+
const refused2 = refuseEarly(webhook, channel.adapter, request);
|
|
2369
2395
|
if (refused2 !== void 0) return refused2;
|
|
2370
2396
|
const rawBody = await readBody(request, max);
|
|
2371
2397
|
if (typeof rawBody !== "string") return rawBody;
|
|
@@ -2382,6 +2408,18 @@ function createWebhookHandler(options) {
|
|
|
2382
2408
|
}
|
|
2383
2409
|
};
|
|
2384
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
|
+
}
|
|
2385
2423
|
function refuseMethod(webhook, request) {
|
|
2386
2424
|
const methods = webhook.methods ?? ["POST"];
|
|
2387
2425
|
const method = request.method.toUpperCase();
|