@theokit/gateway 0.16.0 → 0.17.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
@@ -1677,8 +1677,8 @@ declare function redactSecrets(value: unknown): string;
1677
1677
  /** Strategy function: pure mapping from event to agentId string. */
1678
1678
  type AgentIdStrategy = (event: MessageEvent) => string;
1679
1679
  /**
1680
- * Default strategy. Sanitizes `-` to `_` inside ids so the format
1681
- * remains parseable by `-` segments.
1680
+ * Default strategy. Escapes ids (`_` to `__`, `-` to `_d`) so the format
1681
+ * remains splittable on `-` and distinct ids never share a key.
1682
1682
  */
1683
1683
  declare function defaultStrategy(event: MessageEvent): string;
1684
1684
  /**
@@ -1949,10 +1949,11 @@ declare function loadPeer<T>(load: () => Promise<unknown>, onMissing: () => neve
1949
1949
  * Every provider signs the exact bytes, so a body that was parsed and re-serialised hashes
1950
1950
  * differently and fails verification for a correct request. The bytes are attached as `rawBody`.
1951
1951
  *
1952
- * With `maxBytes`, a body larger than that many bytes is refused before it is held. The rest of
1953
- * the stream is read and dropped, and once it ends the middleware answers 413 with the JSON body
1954
- * `{ "error": "request body too large" }` itself; `next` is not called. A route that must stay
1955
- * public (a provider dials in) sets it, or any client can stream a body until the heap runs out.
1952
+ * A body larger than `maxBytes` is refused before it is held. `maxBytes` defaults to
1953
+ * {@link DEFAULT_WEBHOOK_MAX_BYTES} (1 MiB); pass `Number.POSITIVE_INFINITY` for no limit. The rest
1954
+ * of the stream is read and dropped, and once it ends the middleware answers 413 with the JSON body
1955
+ * `{ "error": "request body too large" }` itself; `next` is not called. The capture runs before
1956
+ * signature verification, so without a limit any client could stream a body until the heap runs out.
1956
1957
  * The refusal is answered here rather than handed to `next` as an error because Express's final
1957
1958
  * handler writes an error as an HTML page carrying its stack whenever `NODE_ENV` is not
1958
1959
  * `production`, and these routes answer anyone.
@@ -1961,13 +1962,16 @@ declare function loadPeer<T>(load: () => Promise<unknown>, onMissing: () => neve
1961
1962
  * error do nothing, and after a stream error the end of the stream does nothing.
1962
1963
  *
1963
1964
  * @param tag the package name for the diagnostic, so a reader knows which router is complaining
1964
- * @param options `maxBytes`, the largest body accepted; no limit when absent
1965
- * @throws RangeError when `maxBytes` is not a finite number of zero or more
1965
+ * @param options `maxBytes`, the largest body accepted; 1 MiB when absent, `Infinity` for no limit
1966
+ * @throws RangeError when `maxBytes` is NaN or negative
1966
1967
  */
1967
1968
  declare function rawBodyCapture(tag: string, options?: RawBodyCaptureOptions): (rawReq: unknown, rawRes: unknown, next: (err?: unknown) => void) => void;
1969
+ /** The body size {@link rawBodyCapture} accepts when `maxBytes` is not given: 1 MiB. */
1970
+ declare const DEFAULT_WEBHOOK_MAX_BYTES: number;
1968
1971
  /** Options for {@link rawBodyCapture}. */
1969
1972
  interface RawBodyCaptureOptions {
1970
- /** The largest body accepted, in bytes. A larger one is refused with 413. No limit when absent. */
1973
+ /** The largest body accepted, in bytes. A larger one is refused with 413. Defaults to
1974
+ * {@link DEFAULT_WEBHOOK_MAX_BYTES}; `Number.POSITIVE_INFINITY` removes the limit. */
1971
1975
  readonly maxBytes?: number;
1972
1976
  }
1973
1977
  /**
@@ -1993,4 +1997,4 @@ declare function listenerLifecycle(opts: {
1993
1997
  stop(): Promise<void>;
1994
1998
  };
1995
1999
 
1996
- export { type AgentIdStrategy, type BaseMessageEvent, BasePlatformAdapter, type BuiltChannel, type BuzzMessageEvent, CREDENTIAL_SHAPES, type ChannelConfigField, type ChannelDescriptor, type ChannelEntry, type ChannelRejection, type ChannelRendering, type ChannelWebhook, type ChunkByGraphemeOptions, type ChunkTextOptions, type ClickClackMessageEvent, type ConnectFailure, type CoveredShape, type CreateChannelsResult, type DeliveryRequest, DeliveryRouter, type DeliveryTarget, type DiscordMessageEvent, type EmailMessageEvent, type FeishuLarkMessageEvent, type FenceRegion, GatewayConfigurationError, type GatewayConfigurationErrorOptions, type GatewayContext, type GatewayHandler, type GatewayHook, GatewayLifecycleError, type GatewayLifecycleErrorOptions, GatewayRunner, type GatewayRunnerOptions, type GoogleChatMessageEvent, type HookDecision, HookExecutor, type HookName, type IMessageMessageEvent, type InboundAudio, type IngestResult, type IrcMessageEvent, type LineMessageEvent, type ListenableApp, type ListenerLike, type MatrixMessageEvent, type MattermostMessageEvent, type MessageEvent, type NextcloudTalkMessageEvent, type NostrMessageEvent, type OnErrorContext, type OutboundAttachment, type OutboundMessage, type PlatformEventRegistry, type PlatformName, type PostOutboundContext, type PreInboundContext, type QQMessageEvent, type RaftMessageEvent, type RawBodyCaptureOptions, type RawBodyRequest, type ReconnectPolicy, type SMSMessageEvent, SeenIds, type SendResult, SessionRouter, type SignalMessageEvent, type SlackMessageEvent, type StartResult, type SynologyChatMessageEvent, type TeamsMessageEvent, type TelegramMessageEvent, type TlonUrbitMessageEvent, type TwitchMessageEvent, type UncoveredShape, type WeChatMessageEvent, type WeComMessageEvent, type WebhookRequest, WeixinAdapterBase, type WeixinAdapterConfig, type WeixinFetch, type WeixinSendRequest, type WeixinWebhookAnswer, type WhatsAppMessageEvent, type XMessageEvent, type XThreadTweet, type YuanbaoMessageEvent, type ZaloMessageEvent, type ZaloPersonalMessageEvent, channelRendering, chunkByGrapheme, chunkText, createChannels, defaultStrategy, listenerLifecycle, loadPeer, maskShapes, parseFlatXml, rawBodyCapture, redactSecrets, scanFences, splitKeepingRows, toDialect };
2000
+ export { type AgentIdStrategy, type BaseMessageEvent, BasePlatformAdapter, type BuiltChannel, type BuzzMessageEvent, CREDENTIAL_SHAPES, type ChannelConfigField, type ChannelDescriptor, type ChannelEntry, type ChannelRejection, type ChannelRendering, type ChannelWebhook, type ChunkByGraphemeOptions, type ChunkTextOptions, type ClickClackMessageEvent, type ConnectFailure, type CoveredShape, type CreateChannelsResult, DEFAULT_WEBHOOK_MAX_BYTES, type DeliveryRequest, DeliveryRouter, type DeliveryTarget, type DiscordMessageEvent, type EmailMessageEvent, type FeishuLarkMessageEvent, type FenceRegion, GatewayConfigurationError, type GatewayConfigurationErrorOptions, type GatewayContext, type GatewayHandler, type GatewayHook, GatewayLifecycleError, type GatewayLifecycleErrorOptions, GatewayRunner, type GatewayRunnerOptions, type GoogleChatMessageEvent, type HookDecision, HookExecutor, type HookName, type IMessageMessageEvent, type InboundAudio, type IngestResult, type IrcMessageEvent, type LineMessageEvent, type ListenableApp, type ListenerLike, type MatrixMessageEvent, type MattermostMessageEvent, type MessageEvent, type NextcloudTalkMessageEvent, type NostrMessageEvent, type OnErrorContext, type OutboundAttachment, type OutboundMessage, type PlatformEventRegistry, type PlatformName, type PostOutboundContext, type PreInboundContext, type QQMessageEvent, type RaftMessageEvent, type RawBodyCaptureOptions, type RawBodyRequest, type ReconnectPolicy, type SMSMessageEvent, SeenIds, type SendResult, SessionRouter, type SignalMessageEvent, type SlackMessageEvent, type StartResult, type SynologyChatMessageEvent, type TeamsMessageEvent, type TelegramMessageEvent, type TlonUrbitMessageEvent, type TwitchMessageEvent, type UncoveredShape, type WeChatMessageEvent, type WeComMessageEvent, type WebhookRequest, WeixinAdapterBase, type WeixinAdapterConfig, type WeixinFetch, type WeixinSendRequest, type WeixinWebhookAnswer, type WhatsAppMessageEvent, type XMessageEvent, type XThreadTweet, type YuanbaoMessageEvent, type ZaloMessageEvent, type ZaloPersonalMessageEvent, channelRendering, chunkByGrapheme, chunkText, createChannels, defaultStrategy, listenerLifecycle, loadPeer, maskShapes, parseFlatXml, rawBodyCapture, redactSecrets, scanFences, splitKeepingRows, toDialect };
package/dist/index.d.ts CHANGED
@@ -1677,8 +1677,8 @@ declare function redactSecrets(value: unknown): string;
1677
1677
  /** Strategy function: pure mapping from event to agentId string. */
1678
1678
  type AgentIdStrategy = (event: MessageEvent) => string;
1679
1679
  /**
1680
- * Default strategy. Sanitizes `-` to `_` inside ids so the format
1681
- * remains parseable by `-` segments.
1680
+ * Default strategy. Escapes ids (`_` to `__`, `-` to `_d`) so the format
1681
+ * remains splittable on `-` and distinct ids never share a key.
1682
1682
  */
1683
1683
  declare function defaultStrategy(event: MessageEvent): string;
1684
1684
  /**
@@ -1949,10 +1949,11 @@ declare function loadPeer<T>(load: () => Promise<unknown>, onMissing: () => neve
1949
1949
  * Every provider signs the exact bytes, so a body that was parsed and re-serialised hashes
1950
1950
  * differently and fails verification for a correct request. The bytes are attached as `rawBody`.
1951
1951
  *
1952
- * With `maxBytes`, a body larger than that many bytes is refused before it is held. The rest of
1953
- * the stream is read and dropped, and once it ends the middleware answers 413 with the JSON body
1954
- * `{ "error": "request body too large" }` itself; `next` is not called. A route that must stay
1955
- * public (a provider dials in) sets it, or any client can stream a body until the heap runs out.
1952
+ * A body larger than `maxBytes` is refused before it is held. `maxBytes` defaults to
1953
+ * {@link DEFAULT_WEBHOOK_MAX_BYTES} (1 MiB); pass `Number.POSITIVE_INFINITY` for no limit. The rest
1954
+ * of the stream is read and dropped, and once it ends the middleware answers 413 with the JSON body
1955
+ * `{ "error": "request body too large" }` itself; `next` is not called. The capture runs before
1956
+ * signature verification, so without a limit any client could stream a body until the heap runs out.
1956
1957
  * The refusal is answered here rather than handed to `next` as an error because Express's final
1957
1958
  * handler writes an error as an HTML page carrying its stack whenever `NODE_ENV` is not
1958
1959
  * `production`, and these routes answer anyone.
@@ -1961,13 +1962,16 @@ declare function loadPeer<T>(load: () => Promise<unknown>, onMissing: () => neve
1961
1962
  * error do nothing, and after a stream error the end of the stream does nothing.
1962
1963
  *
1963
1964
  * @param tag the package name for the diagnostic, so a reader knows which router is complaining
1964
- * @param options `maxBytes`, the largest body accepted; no limit when absent
1965
- * @throws RangeError when `maxBytes` is not a finite number of zero or more
1965
+ * @param options `maxBytes`, the largest body accepted; 1 MiB when absent, `Infinity` for no limit
1966
+ * @throws RangeError when `maxBytes` is NaN or negative
1966
1967
  */
1967
1968
  declare function rawBodyCapture(tag: string, options?: RawBodyCaptureOptions): (rawReq: unknown, rawRes: unknown, next: (err?: unknown) => void) => void;
1969
+ /** The body size {@link rawBodyCapture} accepts when `maxBytes` is not given: 1 MiB. */
1970
+ declare const DEFAULT_WEBHOOK_MAX_BYTES: number;
1968
1971
  /** Options for {@link rawBodyCapture}. */
1969
1972
  interface RawBodyCaptureOptions {
1970
- /** The largest body accepted, in bytes. A larger one is refused with 413. No limit when absent. */
1973
+ /** The largest body accepted, in bytes. A larger one is refused with 413. Defaults to
1974
+ * {@link DEFAULT_WEBHOOK_MAX_BYTES}; `Number.POSITIVE_INFINITY` removes the limit. */
1971
1975
  readonly maxBytes?: number;
1972
1976
  }
1973
1977
  /**
@@ -1993,4 +1997,4 @@ declare function listenerLifecycle(opts: {
1993
1997
  stop(): Promise<void>;
1994
1998
  };
1995
1999
 
1996
- export { type AgentIdStrategy, type BaseMessageEvent, BasePlatformAdapter, type BuiltChannel, type BuzzMessageEvent, CREDENTIAL_SHAPES, type ChannelConfigField, type ChannelDescriptor, type ChannelEntry, type ChannelRejection, type ChannelRendering, type ChannelWebhook, type ChunkByGraphemeOptions, type ChunkTextOptions, type ClickClackMessageEvent, type ConnectFailure, type CoveredShape, type CreateChannelsResult, type DeliveryRequest, DeliveryRouter, type DeliveryTarget, type DiscordMessageEvent, type EmailMessageEvent, type FeishuLarkMessageEvent, type FenceRegion, GatewayConfigurationError, type GatewayConfigurationErrorOptions, type GatewayContext, type GatewayHandler, type GatewayHook, GatewayLifecycleError, type GatewayLifecycleErrorOptions, GatewayRunner, type GatewayRunnerOptions, type GoogleChatMessageEvent, type HookDecision, HookExecutor, type HookName, type IMessageMessageEvent, type InboundAudio, type IngestResult, type IrcMessageEvent, type LineMessageEvent, type ListenableApp, type ListenerLike, type MatrixMessageEvent, type MattermostMessageEvent, type MessageEvent, type NextcloudTalkMessageEvent, type NostrMessageEvent, type OnErrorContext, type OutboundAttachment, type OutboundMessage, type PlatformEventRegistry, type PlatformName, type PostOutboundContext, type PreInboundContext, type QQMessageEvent, type RaftMessageEvent, type RawBodyCaptureOptions, type RawBodyRequest, type ReconnectPolicy, type SMSMessageEvent, SeenIds, type SendResult, SessionRouter, type SignalMessageEvent, type SlackMessageEvent, type StartResult, type SynologyChatMessageEvent, type TeamsMessageEvent, type TelegramMessageEvent, type TlonUrbitMessageEvent, type TwitchMessageEvent, type UncoveredShape, type WeChatMessageEvent, type WeComMessageEvent, type WebhookRequest, WeixinAdapterBase, type WeixinAdapterConfig, type WeixinFetch, type WeixinSendRequest, type WeixinWebhookAnswer, type WhatsAppMessageEvent, type XMessageEvent, type XThreadTweet, type YuanbaoMessageEvent, type ZaloMessageEvent, type ZaloPersonalMessageEvent, channelRendering, chunkByGrapheme, chunkText, createChannels, defaultStrategy, listenerLifecycle, loadPeer, maskShapes, parseFlatXml, rawBodyCapture, redactSecrets, scanFences, splitKeepingRows, toDialect };
2000
+ export { type AgentIdStrategy, type BaseMessageEvent, BasePlatformAdapter, type BuiltChannel, type BuzzMessageEvent, CREDENTIAL_SHAPES, type ChannelConfigField, type ChannelDescriptor, type ChannelEntry, type ChannelRejection, type ChannelRendering, type ChannelWebhook, type ChunkByGraphemeOptions, type ChunkTextOptions, type ClickClackMessageEvent, type ConnectFailure, type CoveredShape, type CreateChannelsResult, DEFAULT_WEBHOOK_MAX_BYTES, type DeliveryRequest, DeliveryRouter, type DeliveryTarget, type DiscordMessageEvent, type EmailMessageEvent, type FeishuLarkMessageEvent, type FenceRegion, GatewayConfigurationError, type GatewayConfigurationErrorOptions, type GatewayContext, type GatewayHandler, type GatewayHook, GatewayLifecycleError, type GatewayLifecycleErrorOptions, GatewayRunner, type GatewayRunnerOptions, type GoogleChatMessageEvent, type HookDecision, HookExecutor, type HookName, type IMessageMessageEvent, type InboundAudio, type IngestResult, type IrcMessageEvent, type LineMessageEvent, type ListenableApp, type ListenerLike, type MatrixMessageEvent, type MattermostMessageEvent, type MessageEvent, type NextcloudTalkMessageEvent, type NostrMessageEvent, type OnErrorContext, type OutboundAttachment, type OutboundMessage, type PlatformEventRegistry, type PlatformName, type PostOutboundContext, type PreInboundContext, type QQMessageEvent, type RaftMessageEvent, type RawBodyCaptureOptions, type RawBodyRequest, type ReconnectPolicy, type SMSMessageEvent, SeenIds, type SendResult, SessionRouter, type SignalMessageEvent, type SlackMessageEvent, type StartResult, type SynologyChatMessageEvent, type TeamsMessageEvent, type TelegramMessageEvent, type TlonUrbitMessageEvent, type TwitchMessageEvent, type UncoveredShape, type WeChatMessageEvent, type WeComMessageEvent, type WebhookRequest, WeixinAdapterBase, type WeixinAdapterConfig, type WeixinFetch, type WeixinSendRequest, type WeixinWebhookAnswer, type WhatsAppMessageEvent, type XMessageEvent, type XThreadTweet, type YuanbaoMessageEvent, type ZaloMessageEvent, type ZaloPersonalMessageEvent, channelRendering, chunkByGrapheme, chunkText, createChannels, defaultStrategy, listenerLifecycle, loadPeer, maskShapes, parseFlatXml, rawBodyCapture, redactSecrets, scanFences, splitKeepingRows, toDialect };
package/dist/index.js CHANGED
@@ -1266,7 +1266,7 @@ function defaultStrategy(event) {
1266
1266
  }
1267
1267
  }
1268
1268
  function sanitize(s) {
1269
- return s.replace(/-/g, "_");
1269
+ return s.replace(/_/g, "__").replace(/-/g, "_d");
1270
1270
  }
1271
1271
  var SessionRouter = class {
1272
1272
  constructor(strategy = defaultStrategy) {
@@ -1522,10 +1522,10 @@ async function loadPeer(load, onMissing) {
1522
1522
  }
1523
1523
  }
1524
1524
  function rawBodyCapture(tag, options = {}) {
1525
- const { maxBytes } = options;
1526
- if (maxBytes !== void 0 && !(Number.isFinite(maxBytes) && maxBytes >= 0)) {
1525
+ const maxBytes = options.maxBytes ?? DEFAULT_WEBHOOK_MAX_BYTES;
1526
+ if (!(maxBytes >= 0)) {
1527
1527
  throw new RangeError(
1528
- `[${tag}] rawBodyCapture: maxBytes must be a finite number of zero or more, got ${maxBytes}`
1528
+ `[${tag}] rawBodyCapture: maxBytes must be a number of zero or more (Infinity allowed), got ${maxBytes}`
1529
1529
  );
1530
1530
  }
1531
1531
  return (rawReq, rawRes, next) => {
@@ -1541,13 +1541,13 @@ function rawBodyCapture(tag, options = {}) {
1541
1541
  }
1542
1542
  let buf = "";
1543
1543
  let bytes = 0;
1544
- let refused2 = maxBytes !== void 0 && declaredLength(req) > maxBytes;
1544
+ let refused2 = declaredLength(req) > maxBytes;
1545
1545
  let settled = false;
1546
1546
  req.setEncoding("utf8");
1547
1547
  req.on("data", (chunk) => {
1548
1548
  if (refused2 || settled) return;
1549
1549
  bytes += Buffer.byteLength(chunk);
1550
- if (maxBytes !== void 0 && bytes > maxBytes) {
1550
+ if (bytes > maxBytes) {
1551
1551
  refused2 = true;
1552
1552
  buf = "";
1553
1553
  return;
@@ -1558,7 +1558,7 @@ function rawBodyCapture(tag, options = {}) {
1558
1558
  if (settled) return;
1559
1559
  settled = true;
1560
1560
  if (refused2) {
1561
- answerTooLarge(rawRes, tag, maxBytes ?? 0);
1561
+ answerTooLarge(rawRes, tag, maxBytes);
1562
1562
  return;
1563
1563
  }
1564
1564
  req.rawBody = buf;
@@ -1572,6 +1572,7 @@ function rawBodyCapture(tag, options = {}) {
1572
1572
  });
1573
1573
  };
1574
1574
  }
1575
+ var DEFAULT_WEBHOOK_MAX_BYTES = 1024 * 1024;
1575
1576
  function declaredLength(req) {
1576
1577
  const value = req.headers?.["content-length"];
1577
1578
  const declared = Number(Array.isArray(value) ? value[0] : value);
@@ -1621,6 +1622,6 @@ function listenerLifecycle(opts) {
1621
1622
  };
1622
1623
  }
1623
1624
 
1624
- export { BasePlatformAdapter, CREDENTIAL_SHAPES, DeliveryRouter, GatewayConfigurationError, GatewayLifecycleError, GatewayRunner, HookExecutor, SeenIds, SessionRouter, WeixinAdapterBase, channelRendering, chunkByGrapheme, chunkText, createChannels, defaultStrategy, listenerLifecycle, loadPeer, maskShapes, parseFlatXml, rawBodyCapture, redactSecrets, scanFences, splitKeepingRows, toDialect };
1625
+ export { BasePlatformAdapter, CREDENTIAL_SHAPES, DEFAULT_WEBHOOK_MAX_BYTES, DeliveryRouter, GatewayConfigurationError, GatewayLifecycleError, GatewayRunner, HookExecutor, SeenIds, SessionRouter, WeixinAdapterBase, channelRendering, chunkByGrapheme, chunkText, createChannels, defaultStrategy, listenerLifecycle, loadPeer, maskShapes, parseFlatXml, rawBodyCapture, redactSecrets, scanFences, splitKeepingRows, toDialect };
1625
1626
  //# sourceMappingURL=index.js.map
1626
1627
  //# sourceMappingURL=index.js.map