@theokit/gateway 0.16.1 → 0.18.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 +12 -0
- package/README.md +27 -0
- package/dist/index.cjs +57 -6
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +32 -8
- package/dist/index.d.ts +32 -8
- package/dist/index.js +56 -7
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.cts
CHANGED
|
@@ -1608,6 +1608,26 @@ declare class GatewayRunner {
|
|
|
1608
1608
|
private buildContext;
|
|
1609
1609
|
}
|
|
1610
1610
|
|
|
1611
|
+
/** Options for `withMediaUnderstanding`: the functions that understand media, a size cap and a failure sink. */
|
|
1612
|
+
interface MediaUnderstandingOptions {
|
|
1613
|
+
/** e.g. plugin-voice's transcribe, bound to its config. */
|
|
1614
|
+
transcribe?(audio: {
|
|
1615
|
+
data: Uint8Array;
|
|
1616
|
+
mediaType: string;
|
|
1617
|
+
}): Promise<string>;
|
|
1618
|
+
/** Called for each image an event carries, once inbound images exist. */
|
|
1619
|
+
describeImage?(image: {
|
|
1620
|
+
data: Uint8Array;
|
|
1621
|
+
mediaType: string;
|
|
1622
|
+
}): Promise<string>;
|
|
1623
|
+
/** Largest payload sent to a function. Default 20 MiB; larger is reported as not understood. */
|
|
1624
|
+
readonly maxBytes?: number;
|
|
1625
|
+
/** Where a failure is reported, with the media type and the error name only. */
|
|
1626
|
+
readonly write?: (line: string) => void;
|
|
1627
|
+
}
|
|
1628
|
+
/** Wraps a handler: the event it receives has the transcript or description appended to text. */
|
|
1629
|
+
declare function withMediaUnderstanding(handler: GatewayHandler, options: MediaUnderstandingOptions): GatewayHandler;
|
|
1630
|
+
|
|
1611
1631
|
/** A credential shape this module masks, and the cost of matching it. */
|
|
1612
1632
|
interface CoveredShape {
|
|
1613
1633
|
/** The platform and field, for the reader of the table. */
|
|
@@ -1949,10 +1969,11 @@ declare function loadPeer<T>(load: () => Promise<unknown>, onMissing: () => neve
|
|
|
1949
1969
|
* Every provider signs the exact bytes, so a body that was parsed and re-serialised hashes
|
|
1950
1970
|
* differently and fails verification for a correct request. The bytes are attached as `rawBody`.
|
|
1951
1971
|
*
|
|
1952
|
-
*
|
|
1953
|
-
*
|
|
1954
|
-
*
|
|
1955
|
-
*
|
|
1972
|
+
* A body larger than `maxBytes` is refused before it is held. `maxBytes` defaults to
|
|
1973
|
+
* {@link DEFAULT_WEBHOOK_MAX_BYTES} (1 MiB); pass `Number.POSITIVE_INFINITY` for no limit. The rest
|
|
1974
|
+
* of the stream is read and dropped, and once it ends the middleware answers 413 with the JSON body
|
|
1975
|
+
* `{ "error": "request body too large" }` itself; `next` is not called. The capture runs before
|
|
1976
|
+
* signature verification, so without a limit any client could stream a body until the heap runs out.
|
|
1956
1977
|
* The refusal is answered here rather than handed to `next` as an error because Express's final
|
|
1957
1978
|
* handler writes an error as an HTML page carrying its stack whenever `NODE_ENV` is not
|
|
1958
1979
|
* `production`, and these routes answer anyone.
|
|
@@ -1961,13 +1982,16 @@ declare function loadPeer<T>(load: () => Promise<unknown>, onMissing: () => neve
|
|
|
1961
1982
|
* error do nothing, and after a stream error the end of the stream does nothing.
|
|
1962
1983
|
*
|
|
1963
1984
|
* @param tag the package name for the diagnostic, so a reader knows which router is complaining
|
|
1964
|
-
* @param options `maxBytes`, the largest body accepted;
|
|
1965
|
-
* @throws RangeError when `maxBytes` is
|
|
1985
|
+
* @param options `maxBytes`, the largest body accepted; 1 MiB when absent, `Infinity` for no limit
|
|
1986
|
+
* @throws RangeError when `maxBytes` is NaN or negative
|
|
1966
1987
|
*/
|
|
1967
1988
|
declare function rawBodyCapture(tag: string, options?: RawBodyCaptureOptions): (rawReq: unknown, rawRes: unknown, next: (err?: unknown) => void) => void;
|
|
1989
|
+
/** The body size {@link rawBodyCapture} accepts when `maxBytes` is not given: 1 MiB. */
|
|
1990
|
+
declare const DEFAULT_WEBHOOK_MAX_BYTES: number;
|
|
1968
1991
|
/** Options for {@link rawBodyCapture}. */
|
|
1969
1992
|
interface RawBodyCaptureOptions {
|
|
1970
|
-
/** The largest body accepted, in bytes. A larger one is refused with 413.
|
|
1993
|
+
/** The largest body accepted, in bytes. A larger one is refused with 413. Defaults to
|
|
1994
|
+
* {@link DEFAULT_WEBHOOK_MAX_BYTES}; `Number.POSITIVE_INFINITY` removes the limit. */
|
|
1971
1995
|
readonly maxBytes?: number;
|
|
1972
1996
|
}
|
|
1973
1997
|
/**
|
|
@@ -1993,4 +2017,4 @@ declare function listenerLifecycle(opts: {
|
|
|
1993
2017
|
stop(): Promise<void>;
|
|
1994
2018
|
};
|
|
1995
2019
|
|
|
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 };
|
|
2020
|
+
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 MediaUnderstandingOptions, 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, withMediaUnderstanding };
|
package/dist/index.d.ts
CHANGED
|
@@ -1608,6 +1608,26 @@ declare class GatewayRunner {
|
|
|
1608
1608
|
private buildContext;
|
|
1609
1609
|
}
|
|
1610
1610
|
|
|
1611
|
+
/** Options for `withMediaUnderstanding`: the functions that understand media, a size cap and a failure sink. */
|
|
1612
|
+
interface MediaUnderstandingOptions {
|
|
1613
|
+
/** e.g. plugin-voice's transcribe, bound to its config. */
|
|
1614
|
+
transcribe?(audio: {
|
|
1615
|
+
data: Uint8Array;
|
|
1616
|
+
mediaType: string;
|
|
1617
|
+
}): Promise<string>;
|
|
1618
|
+
/** Called for each image an event carries, once inbound images exist. */
|
|
1619
|
+
describeImage?(image: {
|
|
1620
|
+
data: Uint8Array;
|
|
1621
|
+
mediaType: string;
|
|
1622
|
+
}): Promise<string>;
|
|
1623
|
+
/** Largest payload sent to a function. Default 20 MiB; larger is reported as not understood. */
|
|
1624
|
+
readonly maxBytes?: number;
|
|
1625
|
+
/** Where a failure is reported, with the media type and the error name only. */
|
|
1626
|
+
readonly write?: (line: string) => void;
|
|
1627
|
+
}
|
|
1628
|
+
/** Wraps a handler: the event it receives has the transcript or description appended to text. */
|
|
1629
|
+
declare function withMediaUnderstanding(handler: GatewayHandler, options: MediaUnderstandingOptions): GatewayHandler;
|
|
1630
|
+
|
|
1611
1631
|
/** A credential shape this module masks, and the cost of matching it. */
|
|
1612
1632
|
interface CoveredShape {
|
|
1613
1633
|
/** The platform and field, for the reader of the table. */
|
|
@@ -1949,10 +1969,11 @@ declare function loadPeer<T>(load: () => Promise<unknown>, onMissing: () => neve
|
|
|
1949
1969
|
* Every provider signs the exact bytes, so a body that was parsed and re-serialised hashes
|
|
1950
1970
|
* differently and fails verification for a correct request. The bytes are attached as `rawBody`.
|
|
1951
1971
|
*
|
|
1952
|
-
*
|
|
1953
|
-
*
|
|
1954
|
-
*
|
|
1955
|
-
*
|
|
1972
|
+
* A body larger than `maxBytes` is refused before it is held. `maxBytes` defaults to
|
|
1973
|
+
* {@link DEFAULT_WEBHOOK_MAX_BYTES} (1 MiB); pass `Number.POSITIVE_INFINITY` for no limit. The rest
|
|
1974
|
+
* of the stream is read and dropped, and once it ends the middleware answers 413 with the JSON body
|
|
1975
|
+
* `{ "error": "request body too large" }` itself; `next` is not called. The capture runs before
|
|
1976
|
+
* signature verification, so without a limit any client could stream a body until the heap runs out.
|
|
1956
1977
|
* The refusal is answered here rather than handed to `next` as an error because Express's final
|
|
1957
1978
|
* handler writes an error as an HTML page carrying its stack whenever `NODE_ENV` is not
|
|
1958
1979
|
* `production`, and these routes answer anyone.
|
|
@@ -1961,13 +1982,16 @@ declare function loadPeer<T>(load: () => Promise<unknown>, onMissing: () => neve
|
|
|
1961
1982
|
* error do nothing, and after a stream error the end of the stream does nothing.
|
|
1962
1983
|
*
|
|
1963
1984
|
* @param tag the package name for the diagnostic, so a reader knows which router is complaining
|
|
1964
|
-
* @param options `maxBytes`, the largest body accepted;
|
|
1965
|
-
* @throws RangeError when `maxBytes` is
|
|
1985
|
+
* @param options `maxBytes`, the largest body accepted; 1 MiB when absent, `Infinity` for no limit
|
|
1986
|
+
* @throws RangeError when `maxBytes` is NaN or negative
|
|
1966
1987
|
*/
|
|
1967
1988
|
declare function rawBodyCapture(tag: string, options?: RawBodyCaptureOptions): (rawReq: unknown, rawRes: unknown, next: (err?: unknown) => void) => void;
|
|
1989
|
+
/** The body size {@link rawBodyCapture} accepts when `maxBytes` is not given: 1 MiB. */
|
|
1990
|
+
declare const DEFAULT_WEBHOOK_MAX_BYTES: number;
|
|
1968
1991
|
/** Options for {@link rawBodyCapture}. */
|
|
1969
1992
|
interface RawBodyCaptureOptions {
|
|
1970
|
-
/** The largest body accepted, in bytes. A larger one is refused with 413.
|
|
1993
|
+
/** The largest body accepted, in bytes. A larger one is refused with 413. Defaults to
|
|
1994
|
+
* {@link DEFAULT_WEBHOOK_MAX_BYTES}; `Number.POSITIVE_INFINITY` removes the limit. */
|
|
1971
1995
|
readonly maxBytes?: number;
|
|
1972
1996
|
}
|
|
1973
1997
|
/**
|
|
@@ -1993,4 +2017,4 @@ declare function listenerLifecycle(opts: {
|
|
|
1993
2017
|
stop(): Promise<void>;
|
|
1994
2018
|
};
|
|
1995
2019
|
|
|
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 };
|
|
2020
|
+
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 MediaUnderstandingOptions, 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, withMediaUnderstanding };
|
package/dist/index.js
CHANGED
|
@@ -1248,6 +1248,54 @@ var GatewayRunner = class {
|
|
|
1248
1248
|
}
|
|
1249
1249
|
};
|
|
1250
1250
|
|
|
1251
|
+
// src/runner/media-understanding.ts
|
|
1252
|
+
var DEFAULT_MAX_BYTES = 20 * 1024 * 1024;
|
|
1253
|
+
function sanitizeType(mediaType) {
|
|
1254
|
+
return mediaType.replace(/[^\w/.+-]/g, "");
|
|
1255
|
+
}
|
|
1256
|
+
function errorName(error) {
|
|
1257
|
+
return error instanceof Error ? error.name : "UnknownError";
|
|
1258
|
+
}
|
|
1259
|
+
function withMediaUnderstanding(handler, options) {
|
|
1260
|
+
const maxBytes = options.maxBytes ?? DEFAULT_MAX_BYTES;
|
|
1261
|
+
const write = options.write ?? ((line) => void process.stderr.write(`${line}
|
|
1262
|
+
`));
|
|
1263
|
+
const understand = async (kind, media, fn) => {
|
|
1264
|
+
const type = sanitizeType(media.mediaType);
|
|
1265
|
+
if (fn === void 0) return `[Attachment of type ${type} not understood]`;
|
|
1266
|
+
const failed = `[Attachment of type ${type} could not be understood]`;
|
|
1267
|
+
if (media.data.byteLength > maxBytes) {
|
|
1268
|
+
report(type, "TooLarge");
|
|
1269
|
+
return failed;
|
|
1270
|
+
}
|
|
1271
|
+
try {
|
|
1272
|
+
const text = await fn({ data: media.data, mediaType: media.mediaType });
|
|
1273
|
+
return `[${kind}: ${text}]`;
|
|
1274
|
+
} catch (error) {
|
|
1275
|
+
report(type, errorName(error));
|
|
1276
|
+
return failed;
|
|
1277
|
+
}
|
|
1278
|
+
};
|
|
1279
|
+
const report = (type, name) => {
|
|
1280
|
+
try {
|
|
1281
|
+
write(`[gateway] media understanding failed for ${type}: ${name}`);
|
|
1282
|
+
} catch {
|
|
1283
|
+
}
|
|
1284
|
+
};
|
|
1285
|
+
return async (event, ctx) => {
|
|
1286
|
+
const lines = [];
|
|
1287
|
+
if (event.audio !== void 0) {
|
|
1288
|
+
lines.push(
|
|
1289
|
+
await understand("Audio transcript", event.audio, options.transcribe?.bind(options))
|
|
1290
|
+
);
|
|
1291
|
+
}
|
|
1292
|
+
if (lines.length === 0) return handler(event, ctx);
|
|
1293
|
+
const text = [event.text, ...lines].filter((part) => part !== "").join("\n");
|
|
1294
|
+
const wrapped = { ...event, text };
|
|
1295
|
+
return handler(wrapped, ctx);
|
|
1296
|
+
};
|
|
1297
|
+
}
|
|
1298
|
+
|
|
1251
1299
|
// src/session/router.ts
|
|
1252
1300
|
function defaultStrategy(event) {
|
|
1253
1301
|
const { platform, channel, sender } = event;
|
|
@@ -1522,10 +1570,10 @@ async function loadPeer(load, onMissing) {
|
|
|
1522
1570
|
}
|
|
1523
1571
|
}
|
|
1524
1572
|
function rawBodyCapture(tag, options = {}) {
|
|
1525
|
-
const
|
|
1526
|
-
if (
|
|
1573
|
+
const maxBytes = options.maxBytes ?? DEFAULT_WEBHOOK_MAX_BYTES;
|
|
1574
|
+
if (!(maxBytes >= 0)) {
|
|
1527
1575
|
throw new RangeError(
|
|
1528
|
-
`[${tag}] rawBodyCapture: maxBytes must be a
|
|
1576
|
+
`[${tag}] rawBodyCapture: maxBytes must be a number of zero or more (Infinity allowed), got ${maxBytes}`
|
|
1529
1577
|
);
|
|
1530
1578
|
}
|
|
1531
1579
|
return (rawReq, rawRes, next) => {
|
|
@@ -1541,13 +1589,13 @@ function rawBodyCapture(tag, options = {}) {
|
|
|
1541
1589
|
}
|
|
1542
1590
|
let buf = "";
|
|
1543
1591
|
let bytes = 0;
|
|
1544
|
-
let refused2 =
|
|
1592
|
+
let refused2 = declaredLength(req) > maxBytes;
|
|
1545
1593
|
let settled = false;
|
|
1546
1594
|
req.setEncoding("utf8");
|
|
1547
1595
|
req.on("data", (chunk) => {
|
|
1548
1596
|
if (refused2 || settled) return;
|
|
1549
1597
|
bytes += Buffer.byteLength(chunk);
|
|
1550
|
-
if (
|
|
1598
|
+
if (bytes > maxBytes) {
|
|
1551
1599
|
refused2 = true;
|
|
1552
1600
|
buf = "";
|
|
1553
1601
|
return;
|
|
@@ -1558,7 +1606,7 @@ function rawBodyCapture(tag, options = {}) {
|
|
|
1558
1606
|
if (settled) return;
|
|
1559
1607
|
settled = true;
|
|
1560
1608
|
if (refused2) {
|
|
1561
|
-
answerTooLarge(rawRes, tag, maxBytes
|
|
1609
|
+
answerTooLarge(rawRes, tag, maxBytes);
|
|
1562
1610
|
return;
|
|
1563
1611
|
}
|
|
1564
1612
|
req.rawBody = buf;
|
|
@@ -1572,6 +1620,7 @@ function rawBodyCapture(tag, options = {}) {
|
|
|
1572
1620
|
});
|
|
1573
1621
|
};
|
|
1574
1622
|
}
|
|
1623
|
+
var DEFAULT_WEBHOOK_MAX_BYTES = 1024 * 1024;
|
|
1575
1624
|
function declaredLength(req) {
|
|
1576
1625
|
const value = req.headers?.["content-length"];
|
|
1577
1626
|
const declared = Number(Array.isArray(value) ? value[0] : value);
|
|
@@ -1621,6 +1670,6 @@ function listenerLifecycle(opts) {
|
|
|
1621
1670
|
};
|
|
1622
1671
|
}
|
|
1623
1672
|
|
|
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 };
|
|
1673
|
+
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, withMediaUnderstanding };
|
|
1625
1674
|
//# sourceMappingURL=index.js.map
|
|
1626
1675
|
//# sourceMappingURL=index.js.map
|