@tangle-network/agent-integrations 0.67.3 → 0.69.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.
Files changed (85) hide show
  1. package/data/bundled-adapter-manifests.json +1 -1
  2. package/dist/{adapters-DwSzcrWJ.js → adapters-JMJ8fU1b.js} +211 -20
  3. package/dist/{adapters-DwSzcrWJ.js.map → adapters-JMJ8fU1b.js.map} +1 -1
  4. package/dist/{bundled-manifest-data-DelvV_Gl.js → bundled-manifest-data-Bv0ejHMQ.js} +128 -18
  5. package/dist/{bundled-manifest-data-DelvV_Gl.js.map → bundled-manifest-data-Bv0ejHMQ.js.map} +1 -1
  6. package/dist/{bundled-manifest-runtime-D1unUvQL.d.ts → bundled-manifest-runtime-G6p2i9W-.d.ts} +2 -2
  7. package/dist/bundled-manifest-runtime-G6p2i9W-.d.ts.map +1 -0
  8. package/dist/{catalog-DgqwU5CZ.d.ts → catalog-DmAjfp4i.d.ts} +3 -3
  9. package/dist/{catalog-DgqwU5CZ.d.ts.map → catalog-DmAjfp4i.d.ts.map} +1 -1
  10. package/dist/catalog.d.ts +1 -1
  11. package/dist/connect/index.d.ts +1 -1
  12. package/dist/connect/index.js +1 -1
  13. package/dist/connectors/adapters/index.d.ts +2 -2
  14. package/dist/connectors/adapters/index.js +2 -2
  15. package/dist/connectors/index.d.ts +7 -7
  16. package/dist/connectors/index.js +4 -4
  17. package/dist/connectors/oauth.d.ts +1 -1
  18. package/dist/{consumer-BtrQO_N5.d.ts → consumer-BvaFhBN_.d.ts} +8 -8
  19. package/dist/{consumer-BtrQO_N5.d.ts.map → consumer-BvaFhBN_.d.ts.map} +1 -1
  20. package/dist/consumer.d.ts +1 -1
  21. package/dist/consumer.js +1 -1
  22. package/dist/conversation-events/index.d.ts +1 -1
  23. package/dist/conversation-events/index.js +135 -1
  24. package/dist/conversation-events/index.js.map +1 -1
  25. package/dist/{core-types-CIez54KT.d.ts → core-types-DfCMwTYt.d.ts} +2 -2
  26. package/dist/{core-types-CIez54KT.d.ts.map → core-types-DfCMwTYt.d.ts.map} +1 -1
  27. package/dist/{coverage-catalog-DRTB89P0.d.ts → coverage-catalog-oQiZ8yot.d.ts} +2 -2
  28. package/dist/{coverage-catalog-DRTB89P0.d.ts.map → coverage-catalog-oQiZ8yot.d.ts.map} +1 -1
  29. package/dist/coverage-catalog.d.ts +1 -1
  30. package/dist/{hub-Cdujm_53.d.ts → hub-fmmnwC2s.d.ts} +3 -3
  31. package/dist/{hub-Cdujm_53.d.ts.map → hub-fmmnwC2s.d.ts.map} +1 -1
  32. package/dist/hub.d.ts +1 -1
  33. package/dist/{index-5hnHPubX.d.ts → index-B-VH5b3E.d.ts} +2 -2
  34. package/dist/{index-5hnHPubX.d.ts.map → index-B-VH5b3E.d.ts.map} +1 -1
  35. package/dist/{index-XYlfJKj-.d.ts → index-CQljvKV2.d.ts} +2 -2
  36. package/dist/{index-XYlfJKj-.d.ts.map → index-CQljvKV2.d.ts.map} +1 -1
  37. package/dist/{index-CtJOt-Va.d.ts → index-CW1fmxQi.d.ts} +3 -3
  38. package/dist/{index-CtJOt-Va.d.ts.map → index-CW1fmxQi.d.ts.map} +1 -1
  39. package/dist/{index-q7Sw2D9T.d.ts → index-D0uvn67q.d.ts} +3 -3
  40. package/dist/{index-q7Sw2D9T.d.ts.map → index-D0uvn67q.d.ts.map} +1 -1
  41. package/dist/{index-BIH6grDK.d.ts → index-DbwNiedC.d.ts} +2 -2
  42. package/dist/{index-BIH6grDK.d.ts.map → index-DbwNiedC.d.ts.map} +1 -1
  43. package/dist/{index-B0ijSDnT.d.ts → index-DkNU5tcV.d.ts} +14 -2
  44. package/dist/{index-B0ijSDnT.d.ts.map → index-DkNU5tcV.d.ts.map} +1 -1
  45. package/dist/index.d.ts +18 -18
  46. package/dist/index.js +6 -6
  47. package/dist/internal/bundled-manifest-runtime.d.ts +1 -1
  48. package/dist/internal/bundled-manifest-runtime.js +1 -1
  49. package/dist/mcp.d.ts +2 -2
  50. package/dist/mcp.js +1 -1
  51. package/dist/middleware/index.d.ts +1 -1
  52. package/dist/middleware/index.js +1 -1
  53. package/dist/{oauth-DNBrs_kX.d.ts → oauth-tzsnaJIb.d.ts} +2 -2
  54. package/dist/{oauth-DNBrs_kX.d.ts.map → oauth-tzsnaJIb.d.ts.map} +1 -1
  55. package/dist/{policy-BFvREV-D.d.ts → policy-C9frNJFH.d.ts} +2 -2
  56. package/dist/{policy-BFvREV-D.d.ts.map → policy-C9frNJFH.d.ts.map} +1 -1
  57. package/dist/{registry-BzTp-XSg.d.ts → registry-CE1iu5zX.d.ts} +3 -3
  58. package/dist/{registry-BzTp-XSg.d.ts.map → registry-CE1iu5zX.d.ts.map} +1 -1
  59. package/dist/{registry-core-DqYvz99E.d.ts → registry-core-DtFI-9kF.d.ts} +2 -2
  60. package/dist/{registry-core-DqYvz99E.d.ts.map → registry-core-DtFI-9kF.d.ts.map} +1 -1
  61. package/dist/registry.d.ts +2 -2
  62. package/dist/registry.js +1 -1
  63. package/dist/runtime.d.ts +1 -1
  64. package/dist/sandbox.d.ts +1 -1
  65. package/dist/{specs-Bosi0i_f.js → specs-BqZdi7Jk.js} +2 -2
  66. package/dist/{specs-Bosi0i_f.js.map → specs-BqZdi7Jk.js.map} +1 -1
  67. package/dist/specs.d.ts +1 -1
  68. package/dist/specs.js +1 -1
  69. package/dist/{src-DOsqUMqy.js → src-BFIGHp-s.js} +4 -4
  70. package/dist/{src-DOsqUMqy.js.map → src-BFIGHp-s.js.map} +1 -1
  71. package/dist/{tangle-id-WKgOA6u9.d.ts → tangle-id-Bq7jkKmE.d.ts} +2 -2
  72. package/dist/{tangle-id-WKgOA6u9.d.ts.map → tangle-id-Bq7jkKmE.d.ts.map} +1 -1
  73. package/dist/{tangle-id-DSH2t9nP.js → tangle-id-D2kPF0rX.js} +18 -2
  74. package/dist/tangle-id-D2kPF0rX.js.map +1 -0
  75. package/dist/{types-BdWDKvd0.d.ts → types-W418mSuf.d.ts} +15 -2
  76. package/dist/{types-BdWDKvd0.d.ts.map → types-W418mSuf.d.ts.map} +1 -1
  77. package/dist/webhooks/index.d.ts +11 -1
  78. package/dist/webhooks/index.d.ts.map +1 -1
  79. package/dist/webhooks/index.js +60 -1
  80. package/dist/webhooks/index.js.map +1 -1
  81. package/dist/worker.d.ts +2 -2
  82. package/docs/whatsapp-cloud-routing.md +11 -2
  83. package/package.json +1 -1
  84. package/dist/bundled-manifest-runtime-D1unUvQL.d.ts.map +0 -1
  85. package/dist/tangle-id-DSH2t9nP.js.map +0 -1
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","names":[],"sources":["../../src/webhooks/router.ts","../../src/webhooks/providers.ts","../../src/webhooks/twilio.ts","../../src/webhooks/messaging.ts"],"sourcesContent":["/**\n * @stable Provider-agnostic inbound webhook router.\n *\n * Consumer hooks a single HTTP handler at `/webhook/:provider/:event`\n * (or whatever pathing they prefer) and forwards the request through\n * `WebhookRouter.handle()`. The router:\n *\n * 1. Resolves the registered provider entry.\n * 2. Calls the provider's `verifySignature(rawBody, headers, secrets)`.\n * Failure → 401 fast, no downstream work.\n * 3. Calls the provider's `parse(rawBody, headers)` to extract zero or\n * more normalized events.\n * 4. Awaits the consumer-supplied `deliver(event)` callback.\n * 5. Returns 2xx only after every accepted event finishes durably. A failed\n * or concurrently active delivery returns 503 so the provider retries.\n *\n * Replay protection: providers that sign timestamps (Stripe, Slack)\n * already reject stale signatures inside `verifySignature`. For providers\n * that don't (DocuSeal, GDrive push), the router atomically claims every\n * parsed event before scheduling `deliver()`. Consumers wire this to a\n * durable SETNX/unique insert (D1, Redis, or Postgres) across workers.\n *\n * Why a router and not a per-provider express app: the runtime contract\n * a product cares about is \"an inbound event came in, here's the\n * normalized envelope\". Verification, parsing, and idempotency-dedup\n * are mechanical and provider-specific — the router owns them. The\n * consumer's `deliver()` is the only place product logic runs.\n *\n * Stability: `@stable` — additions to `WebhookEnvelope` must be\n * additive; the router's HTTP contract (paths, status codes) is frozen\n * at 200 (ok), 400 (bad request), 401 (bad signature), 404 (unknown\n * provider), 405 (provider has no inbound surface), and 503 (delivery\n * failed or another worker still owns the delivery).\n */\n\nimport { createHash } from 'node:crypto'\nimport {\n FileSystemAtomicIdempotencyStore,\n InMemoryAtomicIdempotencyStore,\n resolveAtomicIdempotencyStore,\n type AtomicIdempotencyStore,\n type IdempotencyRuntime,\n} from '../idempotency.js'\n\nexport interface WebhookHeaders {\n [name: string]: string | string[] | undefined\n}\n\n/** Normalized inbound event the router emits after parsing. */\nexport interface WebhookEnvelope<TPayload = unknown> {\n /** Provider id (matches the `:provider` path segment). */\n provider: string\n /** Optional event class — e.g., 'customer.subscription.deleted'. The\n * provider's parser decides. Used for routing inside `deliver()`. */\n eventType: string\n /** Provider-emitted event id, when present. Used for the idempotency\n * short-circuit. */\n providerEventId?: string\n /** Wall-clock receive time. */\n receivedAt: number\n /** Provider payload, normalized to the provider's documented event\n * shape. The router does NOT reshape this — `parse()` is the contract. */\n payload: TPayload\n /** Headers passed through for downstream handlers that want them\n * (e.g., to extract custom routing metadata). Always lowercased keys. */\n headers: Record<string, string>\n}\n\nexport type SignatureVerification =\n | { valid: true }\n | { valid: false; reason: string }\n\n/** One declared triggerable event, in the runtime `eventType` namespace a\n * provider's `parse` emits. */\nexport interface TriggerEventDescriptor {\n /** The `eventType` a workflow `provider_event` trigger matches on\n * (`slack.app_mention`, `charge.succeeded`). */\n id: string\n /** Short human label for authoring surfaces; optional. */\n title?: string\n}\n\n/** The set of events a provider can deliver, for author-time validation of\n * workflow `provider_event` triggers. Co-located with the provider's `parse`\n * (the source of the runtime namespace) so the two cannot drift — see the\n * per-provider drift tests. Consumers validate an authored event with two\n * checks: it must carry `namespace` (a prefix-less event on a namespaced\n * provider can never be delivered), and — only when `closed` — be one of\n * `events` (an exhaustive set makes an unlisted event an authoring error). */\nexport interface TriggerEventCatalog {\n /** Required prefix every `parse`-emitted `eventType` carries (`slack.`,\n * `telegram.`), or `null` when the provider emits raw event names (stripe). */\n namespace: string | null\n /** Declared events. Exhaustive iff `closed`; otherwise authoring hints for an\n * open, provider-owned vocabulary. */\n events: readonly TriggerEventDescriptor[]\n /** True when `events` is the COMPLETE deliverable set, so an event outside it\n * is rejected. False when the provider owns an open-ended vocabulary it keeps\n * adding to (Stripe's ~250 types, Slack's Events API) and only `namespace`\n * can be authoritatively enforced. */\n closed: boolean\n}\n\n/** Per-provider plug-in. Stateless — the router calls `verifySignature`\n * then `parse` on every request. The provider's HTTP-shape concerns\n * (e.g., raw body required) are documented per provider. */\nexport interface WebhookProvider {\n /** Stable provider id (`stripe`, `docuseal`, `gdrive`, ...). */\n id: string\n /** Verify the inbound signature. Receives the EXACT raw body string —\n * consumers MUST preserve raw bytes through their HTTP server (do not\n * parse JSON before forwarding here). */\n verifySignature(input: {\n rawBody: string\n headers: WebhookHeaders\n secret: string\n }): SignatureVerification\n /** Parse the validated raw body into zero or more normalized events.\n * A single push payload may carry multiple events (e.g., Slack bulk\n * delivery). Return [] to ack the push as a no-op. */\n parse(input: {\n rawBody: string\n headers: WebhookHeaders\n now?: number\n }): WebhookEnvelope[] | Promise<WebhookEnvelope[]>\n /** Optional provider-specific 200 ACK the router returns on a verified\n * delivery, in place of the default `{ received, total }` JSON. Dropbox Sign\n * acknowledges a delivery only when the body is exactly\n * 'Hello API Event Received' and retries otherwise; this lets such a\n * provider declare the body/headers it needs. The frozen status contract is\n * unchanged — only the 200 body/headers are customized. */\n successResponse?: { body: string; headers?: Record<string, string> }\n /** Declared triggerable-event catalog, for author-time validation of workflow\n * `provider_event` triggers. Optional: a provider without one is left\n * unvalidated by consumers (the trigger still installs). */\n eventCatalog?: TriggerEventCatalog\n}\n\nexport interface WebhookIdempotencyStore extends AtomicIdempotencyStore {}\n\n/** Process-local atomic store for tests and explicitly single-process apps. */\nexport class InMemoryWebhookIdempotencyStore extends InMemoryAtomicIdempotencyStore implements WebhookIdempotencyStore {}\n\n/** Durable store for workers that share the same filesystem directory. */\nexport class FileSystemWebhookIdempotencyStore extends FileSystemAtomicIdempotencyStore implements WebhookIdempotencyStore {}\n\nexport interface WebhookRouterOptions {\n /** Provider registry. Pass any number of providers; routing is by id. */\n providers: WebhookProvider[]\n /** Async callback invoked with every accepted event. The callback must\n * finish its durable enqueue or processing before it resolves. */\n deliver(event: WebhookEnvelope): Promise<void> | void\n /** Resolve the signing secret for a provider id at request time. The\n * router never holds secrets — the consumer's vault resolves them. */\n resolveSecret(providerId: string, headers: WebhookHeaders): Promise<string | null> | string | null\n /** Atomic idempotency store. Production requires shared atomic storage. */\n idempotency?: WebhookIdempotencyStore\n /** Runtime controls the safe default. Production fails closed without a\n * shared store; test/development use an in-memory store when omitted. */\n runtime?: IdempotencyRuntime\n /** TTL on idempotency entries. Default 7 days — long enough that a\n * provider's normal retry-window can't re-deliver. */\n idempotencyTtlMs?: number\n /** Surface delivery errors. Default: console.error. */\n onError?(err: unknown, context: { provider: string; eventType?: string; providerEventId?: string }): void\n /** Override `now()` for tests. */\n now?(): number\n}\n\nexport interface WebhookRouterRequest {\n providerId: string\n rawBody: string\n headers: WebhookHeaders\n}\n\nexport interface WebhookRouterResponse {\n status: number\n body: unknown\n headers?: Record<string, string>\n}\n\n/**\n * Router instance. Stateless aside from the provider registry — safe to\n * share across requests; build once per process.\n */\nexport class WebhookRouter {\n private readonly providers: Map<string, WebhookProvider>\n private readonly deliver: WebhookRouterOptions['deliver']\n private readonly resolveSecret: WebhookRouterOptions['resolveSecret']\n private readonly idempotency: WebhookIdempotencyStore\n private readonly idempotencyTtlMs: number\n private readonly onError: NonNullable<WebhookRouterOptions['onError']>\n private readonly nowFn: () => number\n\n constructor(opts: WebhookRouterOptions) {\n this.providers = new Map(opts.providers.map((p) => [p.id, p]))\n this.deliver = opts.deliver\n this.resolveSecret = opts.resolveSecret\n this.idempotency = resolveAtomicIdempotencyStore({\n component: 'WebhookRouter',\n store: opts.idempotency,\n runtime: opts.runtime,\n })\n this.idempotencyTtlMs = opts.idempotencyTtlMs ?? 7 * 24 * 60 * 60 * 1000\n this.onError = opts.onError ?? defaultOnError\n this.nowFn = opts.now ?? Date.now\n }\n\n /** Process one inbound webhook request. Pure with respect to side-\n * effects on the router instance — safe to call concurrently. */\n async handle(request: WebhookRouterRequest): Promise<WebhookRouterResponse> {\n const provider = this.providers.get(request.providerId)\n if (!provider) {\n return { status: 404, body: { error: 'unknown_provider', provider: request.providerId } }\n }\n const secret = await this.resolveSecret(provider.id, request.headers)\n if (!secret) {\n return { status: 401, body: { error: 'missing_secret', provider: provider.id } }\n }\n const verification = provider.verifySignature({\n rawBody: request.rawBody,\n headers: request.headers,\n secret,\n })\n if (!verification.valid) {\n return { status: 401, body: { error: 'invalid_signature', reason: verification.reason } }\n }\n\n let events: WebhookEnvelope[]\n try {\n events = await provider.parse({ rawBody: request.rawBody, headers: request.headers, now: this.nowFn() })\n } catch (err) {\n this.onError(err, { provider: provider.id })\n return { status: 400, body: { error: 'parse_error', message: errMessage(err) } }\n }\n\n const accepted: Array<{ event: WebhookEnvelope; key: string }> = []\n let inProgress = 0\n try {\n for (const [index, event] of events.entries()) {\n const key = eventKey(provider.id, event, index, request.rawBody)\n const status = await this.idempotency.claimStatus(key, this.idempotencyTtlMs)\n if (status === 'acquired') accepted.push({ event, key })\n if (status === 'in_progress') inProgress++\n }\n } catch (err) {\n // Do not strand earlier claims when a later claim detects an unavailable\n // or corrupt shared store. The request remains failed closed.\n await Promise.allSettled(accepted.map(({ key }) => this.idempotency.release(key)))\n throw err\n }\n\n const delivery = await this.deliverEach(accepted)\n if (delivery.failed > 0 || inProgress > 0) {\n return {\n status: 503,\n body: {\n error: delivery.failed > 0 ? 'delivery_failed' : 'delivery_in_progress',\n received: delivery.succeeded,\n failed: delivery.failed,\n inProgress,\n total: events.length,\n },\n }\n }\n\n if (provider.successResponse) {\n return {\n status: 200,\n body: provider.successResponse.body,\n headers: provider.successResponse.headers,\n }\n }\n return { status: 200, body: { received: accepted.length, total: events.length } }\n }\n\n private async deliverEach(\n events: Array<{ event: WebhookEnvelope; key: string }>,\n ): Promise<{ succeeded: number; failed: number }> {\n let succeeded = 0\n let failed = 0\n for (const { event, key } of events) {\n try {\n await this.deliver(event)\n } catch (err) {\n try {\n await this.idempotency.release(key)\n } catch (releaseError) {\n this.onError(releaseError, {\n provider: event.provider,\n eventType: event.eventType,\n providerEventId: event.providerEventId,\n })\n }\n this.onError(err, {\n provider: event.provider,\n eventType: event.eventType,\n providerEventId: event.providerEventId,\n })\n failed++\n continue\n }\n try {\n await this.idempotency.complete(key)\n succeeded++\n } catch (err) {\n try {\n await this.idempotency.release(key)\n } catch (releaseError) {\n this.onError(releaseError, {\n provider: event.provider,\n eventType: event.eventType,\n providerEventId: event.providerEventId,\n })\n }\n this.onError(err, {\n provider: event.provider,\n eventType: event.eventType,\n providerEventId: event.providerEventId,\n })\n failed++\n }\n }\n return { succeeded, failed }\n }\n}\n\nfunction defaultOnError(err: unknown, context: { provider: string; eventType?: string; providerEventId?: string }): void {\n // eslint-disable-next-line no-console\n console.error('[WebhookRouter]', context, err)\n}\n\nfunction errMessage(err: unknown): string {\n return err instanceof Error ? err.message : String(err)\n}\n\nfunction eventKey(providerId: string, event: WebhookEnvelope, index: number, rawBody: string): string {\n if (event.providerEventId?.trim()) return `${providerId}:id:${event.providerEventId.trim()}`\n // Providers without stable event ids still need collision-resistant keys.\n // A 32-bit hash could drop a distinct signed event after a birthday collision.\n const bodyHash = createHash('sha256').update(rawBody, 'utf8').digest('hex')\n return `${providerId}:body:${event.eventType}:${index}:${bodyHash}`\n}\n","/**\n * Pre-built `WebhookProvider` implementations for the inbound surfaces\n * the substrate ships first-party verifiers for.\n *\n * Each provider implementation is intentionally thin: it delegates\n * signature verification to the corresponding pure function in\n * `connectors/webhooks.ts` and parses the body into one or more\n * normalized `WebhookEnvelope` rows. Anything provider-specific that\n * doesn't fit cleanly (Slack URL-verification handshake, etc.) is\n * surfaced via the envelope `eventType` so the consumer's `deliver()`\n * can branch.\n */\n\nimport {\n firstHeader,\n verifyEmailSignature,\n verifyHmacSignature,\n verifySlackSignature,\n verifyStripeSignature,\n} from '../connectors/webhooks.js'\nimport type { WebhookEnvelope, WebhookHeaders, WebhookProvider, SignatureVerification } from './router.js'\nimport { createHash, createHmac, timingSafeEqual } from 'node:crypto'\nimport type { InboundEmailPayload } from '../conversation-events/index.js'\n\nexport type { InboundEmailPayload } from '../conversation-events/index.js'\n\n/** Stripe webhook provider. Signature header `Stripe-Signature`. */\nexport const stripeWebhookProvider: WebhookProvider = {\n id: 'stripe',\n verifySignature({ rawBody, headers, secret }): SignatureVerification {\n const sig = firstHeader(headers, 'stripe-signature')\n if (!sig) return { valid: false, reason: 'missing_stripe_signature' }\n return verifyStripeSignature(rawBody, sig, secret)\n ? { valid: true }\n : { valid: false, reason: 'invalid_signature' }\n },\n parse({ rawBody, headers, now }): WebhookEnvelope[] {\n const evt = safeJson(rawBody)\n if (!evt || typeof evt !== 'object') return []\n const e = evt as { id?: unknown; type?: unknown }\n return [\n {\n provider: 'stripe',\n eventType: typeof e.type === 'string' ? e.type : 'stripe.unknown',\n providerEventId: typeof e.id === 'string' ? e.id : undefined,\n receivedAt: now ?? Date.now(),\n payload: evt,\n headers: normalizeHeaders(headers),\n },\n ]\n },\n eventCatalog: {\n // Stripe emits the raw event `type` (`charge.succeeded`) — no namespace, and\n // an OPEN, versioned vocabulary (~250 types), so these are authoring hints,\n // not a gate.\n namespace: null,\n closed: false,\n events: [\n { id: 'checkout.session.completed' },\n { id: 'checkout.session.expired' },\n { id: 'customer.subscription.created' },\n { id: 'customer.subscription.updated' },\n { id: 'customer.subscription.deleted' },\n { id: 'invoice.paid' },\n { id: 'invoice.payment_failed' },\n { id: 'payment_intent.succeeded' },\n { id: 'payment_intent.payment_failed' },\n { id: 'charge.succeeded' },\n { id: 'charge.refunded' },\n ],\n },\n}\n\n/** Slack Events API provider. Handles the `url_verification` handshake\n * by emitting a synthetic event the consumer's `deliver()` can echo. */\n/**\n * Answer for Slack's app-config handshake, or `null` when the body is not one.\n *\n * Slack accepts an app install only when the response body carries the\n * `challenge` value from the request. `WebhookRouter` shapes its own 200 body\n * and `deliver()` returns void, so the handshake cannot be answered through the\n * router — call this in the HTTP handler ahead of `router.handle` and return\n * its result when it is not `null`.\n */\nexport function slackHandshakeResponse(rawBody: string): { challenge: string } | null {\n const parsed = safeJson(rawBody) as { type?: unknown; challenge?: unknown } | null\n if (!parsed || typeof parsed !== 'object') return null\n if (parsed.type !== 'url_verification') return null\n return { challenge: typeof parsed.challenge === 'string' ? parsed.challenge : '' }\n}\n\nexport const slackWebhookProvider: WebhookProvider = {\n id: 'slack',\n verifySignature({ rawBody, headers, secret }): SignatureVerification {\n const sig = firstHeader(headers, 'x-slack-signature')\n const ts = firstHeader(headers, 'x-slack-request-timestamp')\n if (!sig || !ts) return { valid: false, reason: 'missing_slack_signature_or_timestamp' }\n return verifySlackSignature(rawBody, sig, ts, secret)\n ? { valid: true }\n : { valid: false, reason: 'invalid_signature' }\n },\n parse({ rawBody, headers, now }): WebhookEnvelope[] {\n const evt = safeJson(rawBody) as { type?: string; event_id?: string; event?: { type?: string } } | null\n if (!evt || typeof evt !== 'object') return []\n if (evt.type === 'url_verification') {\n return [{\n provider: 'slack',\n eventType: 'slack.url_verification',\n receivedAt: now ?? Date.now(),\n payload: evt,\n headers: normalizeHeaders(headers),\n }]\n }\n const eventType = `slack.${evt.event?.type ?? evt.type ?? 'unknown'}`\n return [{\n provider: 'slack',\n eventType,\n providerEventId: typeof evt.event_id === 'string' ? evt.event_id : undefined,\n receivedAt: now ?? Date.now(),\n payload: evt,\n headers: normalizeHeaders(headers),\n }]\n },\n eventCatalog: {\n // Slack namespaces every event `slack.<events-api-type>`, so a prefix-less\n // event is provably dead. The Events API vocabulary is open (Slack adds\n // types), so `events` are hints, not a gate. `slack.url_verification` (the\n // config handshake) is intentionally excluded — it carries no workflow\n // meaning and is answered before dispatch.\n namespace: 'slack.',\n closed: false,\n events: [\n { id: 'slack.message' },\n { id: 'slack.app_mention' },\n { id: 'slack.reaction_added' },\n { id: 'slack.reaction_removed' },\n { id: 'slack.channel_created' },\n { id: 'slack.member_joined_channel' },\n { id: 'slack.team_join' },\n { id: 'slack.app_home_opened' },\n { id: 'slack.file_shared' },\n ],\n },\n}\n\n/** DocuSeal webhook provider. Signature header `X-Docuseal-Signature`. */\nexport const docusealWebhookProvider: WebhookProvider = {\n id: 'docuseal',\n verifySignature({ rawBody, headers, secret }): SignatureVerification {\n const sig = firstHeader(headers, 'x-docuseal-signature')\n if (!sig) return { valid: false, reason: 'missing_docuseal_signature' }\n const expected = createHmac('sha256', secret).update(rawBody).digest('hex')\n const a = Buffer.from(sig.toLowerCase(), 'utf-8')\n const b = Buffer.from(expected, 'utf-8')\n if (a.length !== b.length) return { valid: false, reason: 'invalid_signature' }\n return timingSafeEqual(a, b) ? { valid: true } : { valid: false, reason: 'invalid_signature' }\n },\n parse({ rawBody, headers, now }): WebhookEnvelope[] {\n const evt = safeJson(rawBody) as { event_type?: string; event_id?: string } | null\n if (!evt || typeof evt !== 'object') return []\n return [{\n provider: 'docuseal',\n eventType: `docuseal.${evt.event_type ?? 'unknown'}`,\n providerEventId: typeof evt.event_id === 'string' ? evt.event_id : undefined,\n receivedAt: now ?? Date.now(),\n payload: evt,\n headers: normalizeHeaders(headers),\n }]\n },\n eventCatalog: {\n // DocuSeal namespaces every event `docuseal.<event_type>`. The documented\n // set is small but not derivable from `parse`, so it stays open (namespace\n // is the authoritative gate) with the documented types as hints.\n namespace: 'docuseal.',\n closed: false,\n events: [\n { id: 'docuseal.form.viewed' },\n { id: 'docuseal.form.started' },\n { id: 'docuseal.form.completed' },\n { id: 'docuseal.form.declined' },\n { id: 'docuseal.submission.created' },\n { id: 'docuseal.submission.completed' },\n { id: 'docuseal.submission.expired' },\n { id: 'docuseal.submission.archived' },\n ],\n },\n}\n\n/** Tangle inbound-email provider. Our own Cloudflare Email Routing worker parses\n * the MIME, JSON-serializes it, timestamped-HMAC signs it with the\n * per-connection secret, and POSTs it here — there is no upstream mail-vendor\n * signature, we are both signer and verifier. One inbound message → one\n * `email.received` envelope; `payload.to` carries the owner-resolution address. */\nexport const emailWebhookProvider: WebhookProvider = {\n id: 'email',\n verifySignature({ rawBody, headers, secret }): SignatureVerification {\n const sig = firstHeader(headers, 'x-tangle-email-signature')\n if (!sig) return { valid: false, reason: 'missing_email_signature' }\n const ts = firstHeader(headers, 'x-tangle-email-timestamp')\n if (!ts) return { valid: false, reason: 'missing_email_timestamp' }\n return verifyEmailSignature(rawBody, sig, ts, secret)\n ? { valid: true }\n : { valid: false, reason: 'invalid_signature' }\n },\n parse({ rawBody, headers, now }): WebhookEnvelope[] {\n const msg = safeJson(rawBody) as Partial<InboundEmailPayload> | null\n if (!msg || typeof msg !== 'object') return []\n // `from` + `to` are load-bearing (owner resolution keys off `to`); a body\n // missing either is a malformed edge post — ack as a no-op rather than\n // dispatch an unroutable event.\n if (typeof msg.from !== 'string' || typeof msg.to !== 'string') return []\n return [{\n provider: 'email',\n eventType: 'email.received',\n providerEventId: typeof msg.messageId === 'string' ? msg.messageId : undefined,\n receivedAt: now ?? Date.now(),\n payload: msg as InboundEmailPayload,\n headers: normalizeHeaders(headers),\n }]\n },\n eventCatalog: {\n // We own the edge, so the inbound surface is exactly one closed event.\n namespace: 'email.',\n closed: true,\n events: [{ id: 'email.received' }],\n },\n}\n\n/** Gmail push provider. Cloud Pub/Sub posts a JWT-signed envelope; the\n * *payload* is base64 JSON describing the changed history range. The\n * signature scheme here is the Pub/Sub JWT auth header — when supplied,\n * consumers SHOULD verify the JWT against Google's well-known\n * certificates. We accept the simpler \"Bearer <pubsub-shared-secret>\"\n * variant by default (matching `verifyHmacSignature`). */\nexport const gmailWebhookProvider: WebhookProvider = {\n id: 'gmail',\n verifySignature({ headers, secret }): SignatureVerification {\n const auth = firstHeader(headers, 'authorization')\n if (!auth) return { valid: false, reason: 'missing_authorization' }\n // Accept either \"Bearer <secret>\" or \"Token <secret>\" formats. This\n // is the simple per-tenant shared-secret path; JWT verification is\n // left to the consumer's `deliver()` when they need full Google JWT\n // chain validation.\n const m = /^(?:Bearer|Token)\\s+(.+)$/i.exec(auth)\n if (!m) return { valid: false, reason: 'invalid_authorization_format' }\n const a = Buffer.from(m[1]!, 'utf-8')\n const b = Buffer.from(secret, 'utf-8')\n if (a.length !== b.length) return { valid: false, reason: 'invalid_signature' }\n return timingSafeEqual(a, b) ? { valid: true } : { valid: false, reason: 'invalid_signature' }\n },\n parse({ rawBody, headers, now }): WebhookEnvelope[] {\n const envelope = safeJson(rawBody) as { message?: { data?: string; messageId?: string; publishTime?: string } } | null\n if (!envelope?.message?.data) return []\n let payload: unknown\n try {\n payload = JSON.parse(Buffer.from(envelope.message.data, 'base64').toString('utf-8'))\n } catch {\n return []\n }\n const inner = payload as { historyId?: number | string; emailAddress?: string }\n return [{\n provider: 'gmail',\n eventType: 'gmail.history_changed',\n providerEventId: envelope.message.messageId,\n receivedAt: now ?? Date.now(),\n payload: { ...inner, publishTime: envelope.message.publishTime },\n headers: normalizeHeaders(headers),\n }]\n },\n}\n\n/** Google Drive push provider. Drive does NOT sign the body — it uses\n * the per-channel token (`X-Goog-Channel-Token`) as the shared secret.\n * The router compares it constant-time against the resolved secret. */\nexport const gdriveWebhookProvider: WebhookProvider = {\n id: 'gdrive',\n verifySignature({ headers, secret }): SignatureVerification {\n const token = firstHeader(headers, 'x-goog-channel-token')\n if (!token) return { valid: false, reason: 'missing_channel_token' }\n const a = Buffer.from(token, 'utf-8')\n const b = Buffer.from(secret, 'utf-8')\n if (a.length !== b.length) return { valid: false, reason: 'invalid_signature' }\n return timingSafeEqual(a, b) ? { valid: true } : { valid: false, reason: 'invalid_signature' }\n },\n parse({ headers, now }): WebhookEnvelope[] {\n const resourceId = firstHeader(headers, 'x-goog-resource-id')\n const resourceState = firstHeader(headers, 'x-goog-resource-state') ?? 'unknown'\n const channelId = firstHeader(headers, 'x-goog-channel-id')\n const messageNumber = firstHeader(headers, 'x-goog-message-number')\n if (resourceState === 'sync') {\n return [{\n provider: 'gdrive',\n eventType: 'gdrive.channel.sync',\n providerEventId: messageNumber ? `${channelId}-${messageNumber}` : undefined,\n receivedAt: now ?? Date.now(),\n payload: { channelId, resourceId, resourceState },\n headers: normalizeHeaders(headers),\n }]\n }\n return [{\n provider: 'gdrive',\n eventType: `gdrive.resource.${resourceState}`,\n providerEventId: messageNumber ? `${channelId}-${messageNumber}` : undefined,\n receivedAt: now ?? Date.now(),\n payload: { channelId, resourceId, resourceState },\n headers: normalizeHeaders(headers),\n }]\n },\n}\n\n/** The Telegram update keys, in resolution order — the first present key names\n * the update kind (`telegram.message`, `telegram.callback_query`, …). */\nconst TELEGRAM_UPDATE_KEYS = [\n 'message',\n 'edited_message',\n 'channel_post',\n 'edited_channel_post',\n 'business_connection',\n 'business_message',\n 'edited_business_message',\n 'deleted_business_messages',\n 'message_reaction',\n 'message_reaction_count',\n 'inline_query',\n 'chosen_inline_result',\n 'callback_query',\n 'shipping_query',\n 'pre_checkout_query',\n 'purchased_paid_media',\n 'poll',\n 'poll_answer',\n 'my_chat_member',\n 'chat_member',\n 'chat_join_request',\n 'chat_boost',\n 'removed_chat_boost',\n] as const\n\n/** Telegram Bot API webhook provider. Telegram does NOT sign the body — it\n * authenticates by echoing the `secret_token` set at `setWebhook` time in the\n * `X-Telegram-Bot-Api-Secret-Token` header. We compare it constant-time to the\n * per-connection secret. `update_id` is the idempotency anchor. */\nexport const telegramWebhookProvider: WebhookProvider = {\n id: 'telegram',\n verifySignature({ headers, secret }): SignatureVerification {\n const token = firstHeader(headers, 'x-telegram-bot-api-secret-token')\n if (!token) return { valid: false, reason: 'missing_telegram_secret_token' }\n const a = Buffer.from(token, 'utf-8')\n const b = Buffer.from(secret, 'utf-8')\n if (a.length !== b.length) return { valid: false, reason: 'invalid_secret_token' }\n return timingSafeEqual(a, b) ? { valid: true } : { valid: false, reason: 'invalid_secret_token' }\n },\n parse({ rawBody, headers, now }): WebhookEnvelope[] {\n const evt = safeJson(rawBody) as ({ update_id?: number } & Record<string, unknown>) | null\n if (!evt || typeof evt !== 'object') return []\n const kind = TELEGRAM_UPDATE_KEYS.find((k) => k in evt) ?? 'unknown'\n return [{\n provider: 'telegram',\n eventType: `telegram.${kind}`,\n providerEventId: typeof evt.update_id === 'number' ? String(evt.update_id) : undefined,\n receivedAt: now ?? Date.now(),\n payload: evt,\n headers: normalizeHeaders(headers),\n }]\n },\n eventCatalog: {\n // Derived from the SAME TELEGRAM_UPDATE_KEYS `parse` matches on, so the\n // catalog is exhaustive and cannot drift — hence `closed`. (`telegram.unknown`,\n // parse's fallback for an update with no recognized key, is intentionally\n // excluded: it is not a triggerable event.)\n namespace: 'telegram.',\n closed: true,\n events: TELEGRAM_UPDATE_KEYS.map((k) => ({ id: `telegram.${k}` })),\n },\n}\n\n/** Dropbox Sign (HelloSign) webhook provider. There is NO signature header:\n * the body carries `event.event_hash = HMAC_SHA256(apiKey, event_time +\n * event_type)` (hex), so the `secret` here is the account's API key (the\n * caller resolves it from the connection credential). The body is JSON, or a\n * `json=`/multipart `name=\"json\"` form field on older apps. */\nexport const hellosignWebhookProvider: WebhookProvider = {\n id: 'hellosign',\n // Dropbox Sign acknowledges a delivery only when the response body is exactly\n // 'Hello API Event Received'; any other body makes it retry the event.\n successResponse: { body: 'Hello API Event Received', headers: { 'content-type': 'text/plain' } },\n verifySignature({ rawBody, secret }): SignatureVerification {\n const body = parseHelloSignBody(rawBody)\n const event = body?.event\n if (\n !event ||\n typeof event.event_time !== 'string' ||\n typeof event.event_type !== 'string' ||\n typeof event.event_hash !== 'string'\n ) {\n return { valid: false, reason: 'missing_event_fields' }\n }\n const expected = createHmac('sha256', secret)\n .update(`${event.event_time}${event.event_type}`)\n .digest('hex')\n const a = Buffer.from(event.event_hash.toLowerCase(), 'utf-8')\n const b = Buffer.from(expected.toLowerCase(), 'utf-8')\n if (a.length !== b.length) return { valid: false, reason: 'invalid_signature' }\n return timingSafeEqual(a, b) ? { valid: true } : { valid: false, reason: 'invalid_signature' }\n },\n parse({ rawBody, headers, now }): WebhookEnvelope[] {\n const body = parseHelloSignBody(rawBody)\n const event = body?.event\n if (!event) return []\n return [{\n provider: 'hellosign',\n eventType: `hellosign.${event.event_type ?? 'unknown'}`,\n // Prefer Dropbox Sign's canonical `event.event_id` (stable across\n // redeliveries, correlates with the provider). Fall back to a digest of\n // the parsed body when it's absent: `event_hash` = HMAC(apiKey,\n // event_time + event_type), so two distinct same-type events in the same\n // second share it — the body digest stays distinct across differing\n // events and identical across redeliveries of one event.\n providerEventId:\n typeof event.event_id === 'string' && event.event_id.length > 0\n ? event.event_id\n : createHash('sha256').update(JSON.stringify(body)).digest('hex'),\n receivedAt: now ?? Date.now(),\n payload: body,\n headers: normalizeHeaders(headers),\n }]\n },\n eventCatalog: {\n // Dropbox Sign namespaces every event `hellosign.<event_type>`. Documented\n // set as hints; namespace is the authoritative gate. `callback_test` (the\n // app-config ping) is intentionally excluded — it is not a workflow event.\n namespace: 'hellosign.',\n closed: false,\n events: [\n { id: 'hellosign.signature_request_sent' },\n { id: 'hellosign.signature_request_viewed' },\n { id: 'hellosign.signature_request_signed' },\n { id: 'hellosign.signature_request_all_signed' },\n { id: 'hellosign.signature_request_declined' },\n { id: 'hellosign.signature_request_reassigned' },\n { id: 'hellosign.signature_request_remind' },\n { id: 'hellosign.signature_request_canceled' },\n { id: 'hellosign.signature_request_expired' },\n ],\n },\n}\n\n/** Keys Clay HTTP-API-action payloads commonly carry that can anchor\n * idempotency, in resolution order. Clay bodies are operator-defined, so we\n * fall back to a digest of the raw body when none is present. */\nconst CLAY_EVENT_ID_KEYS = ['id', 'rowId', 'recordId', 'clayRowId', 'eventId'] as const\n\n/** Clay webhook provider — receives rows Clay delivers via its outbound \"HTTP\n * API\" action. Clay does NOT sign its requests and adds no proprietary\n * headers (the `x-clay-signature: sha256=...` header seen in some third-party\n * code is unverified inference, not in Clay's docs). Authentication is\n * therefore a pre-shared secret the operator adds as a header on the Clay\n * action; we compare it constant-time against the per-connection secret.\n * Header: `x-clay-webhook-secret`. The body is whatever columns the operator\n * mapped (free-form JSON with no canonical event id), so idempotency anchors\n * on a common id field when present, else a sha256 of the raw body. */\nexport const clayWebhookProvider: WebhookProvider = {\n id: 'clay',\n verifySignature({ headers, secret }): SignatureVerification {\n const token = firstHeader(headers, 'x-clay-webhook-secret')\n if (!token) return { valid: false, reason: 'missing_clay_webhook_secret' }\n const a = Buffer.from(token, 'utf-8')\n const b = Buffer.from(secret, 'utf-8')\n if (a.length !== b.length) return { valid: false, reason: 'invalid_signature' }\n return timingSafeEqual(a, b) ? { valid: true } : { valid: false, reason: 'invalid_signature' }\n },\n parse({ rawBody, headers, now }): WebhookEnvelope[] {\n const evt = safeJson(rawBody)\n if (!evt || typeof evt !== 'object') return []\n const record = evt as Record<string, unknown>\n const idKey = CLAY_EVENT_ID_KEYS.find(\n (k) => typeof record[k] === 'string' || typeof record[k] === 'number',\n )\n const providerEventId = idKey\n ? String(record[idKey])\n : createHash('sha256').update(rawBody).digest('hex')\n const eventType =\n typeof record.event === 'string'\n ? `clay.${record.event}`\n : typeof record.type === 'string'\n ? `clay.${record.type}`\n : 'clay.row'\n return [{\n provider: 'clay',\n eventType,\n providerEventId,\n receivedAt: now ?? Date.now(),\n payload: evt,\n headers: normalizeHeaders(headers),\n }]\n },\n}\n\ninterface HelloSignInboundBody {\n event?: {\n event_time?: string\n event_type?: string\n event_hash?: string\n event_id?: string\n }\n}\n\n/** Dropbox Sign posts JSON, a form-urlencoded `json=` field, or a\n * multipart `name=\"json\"` part depending on app vintage. Mirrors the\n * connector adapter's body extraction. */\nfunction parseHelloSignBody(rawBody: string): HelloSignInboundBody | null {\n try {\n const json = JSON.parse(rawBody) as HelloSignInboundBody\n if (json && typeof json === 'object') return json\n } catch {\n // fall through to form-data extraction\n }\n // application/x-www-form-urlencoded `json=` field. URLSearchParams decodes\n // `+` as a space (decodeURIComponent leaves it literal) and resolves\n // percent-escapes, so space-bearing string fields aren't corrupted.\n const formJson = new URLSearchParams(rawBody).get('json')\n if (formJson) {\n try {\n return JSON.parse(formJson) as HelloSignInboundBody\n } catch {\n return null\n }\n }\n // multipart/form-data with a `name=\"json\"` part. Extract linearly (indexOf +\n // fixed-shape separator probes, no unbounded lazy regex) so a hostile body\n // can't drive quadratic backtracking on this public ingress.\n return extractMultipartJsonField(rawBody)\n}\n\n/** Pull the `json` part out of a multipart/form-data body without an\n * unbounded backtracking regex. Returns null if the part isn't present or\n * isn't valid JSON. The separator probes (`\\r?\\n\\r?\\n`, `\\r?\\n--`) are\n * fixed-shape (no `*`/`+`), so each runs in a single linear scan. */\nfunction extractMultipartJsonField(rawBody: string): HelloSignInboundBody | null {\n const nameIdx = rawBody.indexOf('name=\"json\"')\n if (nameIdx === -1) return null\n const afterName = rawBody.slice(nameIdx)\n const sep = /\\r?\\n\\r?\\n/.exec(afterName)\n if (!sep) return null\n const partBody = afterName.slice(sep.index + sep[0].length)\n const end = /\\r?\\n--/.exec(partBody)\n if (!end) return null\n try {\n return JSON.parse(partBody.slice(0, end.index)) as HelloSignInboundBody\n } catch {\n return null\n }\n}\n\n/** Generic HMAC provider — for the long-tail webhook source where the\n * caller has standardised on a single sha256-of-body scheme. Header\n * `X-Signature` by default; override at provider-build time if needed. */\nexport function genericHmacWebhookProvider(options: {\n id: string\n signatureHeader?: string\n algorithm?: 'sha256' | 'sha1' | 'sha512'\n signaturePrefix?: string\n /** Parser to convert the raw body into envelopes. Defaults to\n * \"one event with eventType=<provider>.event and payload=JSON\". */\n parse?: WebhookProvider['parse']\n}): WebhookProvider {\n const header = options.signatureHeader ?? 'x-signature'\n return {\n id: options.id,\n verifySignature({ rawBody, headers, secret }) {\n const sig = firstHeader(headers, header)\n if (!sig) return { valid: false, reason: `missing_${header}` }\n return verifyHmacSignature(rawBody, sig, secret, {\n algorithm: options.algorithm ?? 'sha256',\n signaturePrefix: options.signaturePrefix,\n })\n ? { valid: true }\n : { valid: false, reason: 'invalid_signature' }\n },\n parse:\n options.parse ??\n (({ rawBody, headers, now }) => {\n const evt = safeJson(rawBody) ?? rawBody\n return [{\n provider: options.id,\n eventType: `${options.id}.event`,\n receivedAt: now ?? Date.now(),\n payload: evt,\n headers: normalizeHeaders(headers),\n }]\n }),\n }\n}\n\nfunction safeJson(s: string): unknown {\n try {\n return JSON.parse(s)\n } catch {\n return null\n }\n}\n\nfunction normalizeHeaders(headers: WebhookHeaders): Record<string, string> {\n const out: Record<string, string> = {}\n for (const [k, v] of Object.entries(headers)) {\n if (v === undefined) continue\n const value = Array.isArray(v) ? v[0] : v\n if (typeof value === 'string') out[k.toLowerCase()] = value\n }\n return out\n}\n","import { authenticateTwilioForm } from '../twilio/index.js'\nimport type { WebhookProvider } from './router.js'\n\n/** Bind to a trusted exact external route; forwarded Host headers are not a signing authority. */\nexport function createTwilioWebhookProvider(options: {\n url: string\n accountSid: string\n kind: 'message' | 'status'\n providerId?: 'twilio' | 'twilio-sms'\n}): WebhookProvider {\n if (!['message', 'status'].includes(options.kind)) throw new Error('Choose message or status webhook mode')\n const bound = { ...options }\n const providerId = bound.providerId ?? 'twilio'\n return {\n id: providerId,\n verifySignature({ rawBody, headers, secret }) {\n const key = Object.keys(headers).find(k => k.toLowerCase() === 'x-twilio-signature')\n const signature = key ? headers[key] : undefined\n try {\n authenticateTwilioForm({ url: bound.url, rawBody, signature, authToken: secret, accountSid: bound.accountSid })\n return { valid: true }\n } catch { return { valid: false, reason: 'invalid_twilio_form_or_signature' } }\n },\n parse({ rawBody, now }) {\n const data = Object.fromEntries(new URLSearchParams(rawBody))\n if (!/^(SM|MM)[a-f0-9]{32}$/i.test(data.MessageSid ?? '')) throw new Error('Twilio event has no valid message identity')\n const status = bound.kind === 'status' ? data.MessageStatus : 'received'\n if (!status || !/^[a-z_]+$/.test(status)) throw new Error('Twilio status event has no status')\n return [{ provider: providerId, eventType: `${providerId}.message.${bound.kind === 'status' ? 'status' : 'received'}`,\n // A message SID alone would incorrectly deduplicate all subsequent delivery states.\n providerEventId: `${bound.accountSid}:${data.MessageSid}:${bound.kind}:${status}`,\n receivedAt: now ?? Date.now(), payload: { ...data, __receivedAt: new Date(now ?? Date.now()).toISOString() }, headers: {} }]\n },\n successResponse: { body: '<Response/>', headers: { 'Content-Type': 'text/xml; charset=utf-8' } },\n eventCatalog: { namespace: `${providerId}.`, closed: true,\n events: [{ id: `${providerId}.message.received` }, { id: `${providerId}.message.status` }] },\n }\n}\n\n/** Register this provider for Twilio SMS/MMS Hub lines. The older constructor retains its twilio event IDs. */\nexport function createTwilioSmsWebhookProvider(options: {\n url: string\n accountSid: string\n kind: 'message' | 'status'\n}): WebhookProvider {\n return createTwilioWebhookProvider({ ...options, providerId: 'twilio-sms' })\n}\n","import { createHmac, timingSafeEqual } from 'node:crypto'\nimport { verifyHmacSignature, verifyStripeSignature } from '../connectors/webhooks.js'\nimport type { WebhookEnvelope, WebhookHeaders, WebhookProvider } from './router.js'\n\n/** A repeated authentication header is ambiguous, never an invitation to pick one. */\nfunction header(headers: WebhookHeaders, name: string): string | null {\n const entries = Object.entries(headers).filter(([key]) => key.toLowerCase() === name)\n if (entries.length !== 1) return null\n const value = entries[0]?.[1]\n return typeof value === 'string' && value.length > 0 && value.length <= 4096 ? value : null\n}\n\nfunction fresh(timestamp: string | null, now: number): timestamp is string {\n return timestamp !== null && /^\\d{1,12}$/.test(timestamp)\n && Number.isSafeInteger(Number(timestamp))\n && Number.isFinite(now) && Math.abs(now - Number(timestamp)) <= 300\n}\n\n/** Inkbox signs the literal identity secret and request-id.timestamp.raw-body. */\nexport function verifyInkboxWebhook(rawBody: string, headers: WebhookHeaders, secret: string, now = Date.now() / 1000): boolean {\n const id = header(headers, 'x-inkbox-request-id')\n const timestamp = header(headers, 'x-inkbox-timestamp')\n const signature = header(headers, 'x-inkbox-signature')\n return Boolean(secret && id && signature && fresh(timestamp, now)\n && verifyHmacSignature(`${id}.${timestamp}.${rawBody}`, signature, secret, { signaturePrefix: 'sha256=' }))\n}\n\n/** Standard Webhooks signs the exact request bytes and three bounded headers. */\nfunction verifyStandardWebhook(rawBody: string, headers: WebhookHeaders, secret: string, prefix: 'webhook' | 'svix', now: number): boolean {\n const id = header(headers, `${prefix}-id`)\n const timestamp = header(headers, `${prefix}-timestamp`)\n const signature = header(headers, `${prefix}-signature`)\n if (!id || !signature || !fresh(timestamp, now) || !secret.startsWith('whsec_')) return false\n const encoded = secret.slice(6)\n if (!/^[A-Za-z0-9+/]+={0,2}$/.test(encoded)) return false\n const key = Buffer.from(encoded, 'base64')\n if (key.length < 16 || key.toString('base64') !== encoded) return false\n const expected = createHmac('sha256', key).update(`${id}.${timestamp}.${rawBody}`).digest('base64')\n return signature.split(' ').some((part) => part.startsWith('v1,')\n && part.slice(3).length === expected.length\n && timingSafeEqual(Buffer.from(part.slice(3)), Buffer.from(expected)))\n}\n\n/** Both Linq services use Standard Webhooks with a base64-decoded signing key. */\nexport function verifyLinqWebhook(rawBody: string, headers: WebhookHeaders, secret: string, now = Date.now() / 1000): boolean {\n return verifyStandardWebhook(rawBody, headers, secret, 'webhook', now)\n}\n\n/** Resend uses the Svix header names for the same signed-body protocol. */\nexport function verifyResendWebhook(rawBody: string, headers: WebhookHeaders, secret: string, now = Date.now() / 1000): boolean {\n return verifyStandardWebhook(rawBody, headers, secret, 'svix', now)\n}\n\nfunction object(value: unknown): value is Record<string, unknown> {\n return value !== null && typeof value === 'object' && !Array.isArray(value)\n}\n\nfunction parse(provider: string, typeField: string, idField: string, input: Parameters<WebhookProvider['parse']>[0]): WebhookEnvelope[] {\n if (Buffer.byteLength(input.rawBody, 'utf8') > 1_048_576) throw new Error('Webhook exceeds 1 MiB')\n const value: unknown = JSON.parse(input.rawBody)\n if (!object(value) || typeof value[typeField] !== 'string' || typeof value[idField] !== 'string'\n || !/^[a-z][a-z0-9_.-]{0,100}$/.test(value[typeField] as string)\n || !(value[idField] as string).length || (value[idField] as string).length > 256) {\n throw new Error('Provider event requires a bounded event type and stable id')\n }\n return [{ provider, eventType: `${provider}.${value[typeField]}`, providerEventId: value[idField] as string,\n receivedAt: input.now ?? Date.now(), payload: value, headers: {} }]\n}\n\nexport const inkboxWebhookProvider: WebhookProvider = {\n id: 'inkbox',\n verifySignature: ({ rawBody, headers, secret }) => verifyInkboxWebhook(rawBody, headers, secret)\n ? { valid: true } : { valid: false, reason: 'invalid_signature' },\n parse: (input) => parse('inkbox', 'event_type', 'id', input),\n eventCatalog: { namespace: 'inkbox.', closed: false, events: [\n { id: 'inkbox.imessage.received' }, { id: 'inkbox.text.received' }, { id: 'inkbox.message.received' },\n ] },\n}\n\nexport const linqWebhookProvider: WebhookProvider = {\n id: 'linq',\n verifySignature: ({ rawBody, headers, secret }) => verifyLinqWebhook(rawBody, headers, secret)\n ? { valid: true } : { valid: false, reason: 'invalid_signature' },\n parse: (input) => {\n const events = parse('linq', 'event_type', 'event_id', input)\n const id = header(input.headers, 'webhook-id')\n if (!id) throw new Error('Missing signed event identity')\n return events.map((event) => ({ ...event, providerEventId: id }))\n },\n eventCatalog: { namespace: 'linq.', closed: false, events: [{ id: 'linq.message.received' }] },\n}\n\nexport const contiguityWebhookProvider: WebhookProvider = {\n id: 'contiguity',\n verifySignature: ({ rawBody, headers, secret }) => {\n const signature = header(headers, 'contiguity-signature')\n return secret && signature && verifyStripeSignature(rawBody, signature, secret)\n ? { valid: true } : { valid: false, reason: 'invalid_signature' }\n },\n parse: (input) => parse('contiguity', 'type', 'id', input),\n eventCatalog: { namespace: 'contiguity.', closed: false, events: [\n { id: 'contiguity.imessage.incoming' }, { id: 'contiguity.text.incoming.sms' },\n { id: 'contiguity.text.incoming.mms' },\n ] },\n}\n\nexport const linqWhatsappWebhookProvider: WebhookProvider = {\n id: 'linq-whatsapp',\n verifySignature: ({ rawBody, headers, secret }) => verifyLinqWebhook(rawBody, headers, secret)\n ? { valid: true } : { valid: false, reason: 'invalid_signature' },\n parse: (input) => {\n const events = parse('linq-whatsapp', 'type', 'id', input)\n if (events[0]?.providerEventId !== header(input.headers, 'webhook-id')) throw new Error('Signed event identity mismatch')\n return events\n },\n eventCatalog: { namespace: 'linq-whatsapp.', closed: false, events: [{ id: 'linq-whatsapp.message.received' }] },\n}\n\nexport const resendWebhookProvider: WebhookProvider = {\n id: 'resend',\n verifySignature: ({ rawBody, headers, secret }) => verifyResendWebhook(rawBody, headers, secret)\n ? { valid: true } : { valid: false, reason: 'invalid_signature' },\n parse: (input) => {\n if (Buffer.byteLength(input.rawBody, 'utf8') > 1_048_576) throw new Error('Webhook exceeds 1 MiB')\n const value: unknown = JSON.parse(input.rawBody)\n if (!object(value) || value.type !== 'email.received' || !object(value.data)\n || typeof value.data.email_id !== 'string' || !/^[a-f\\d]{8}(?:-[a-f\\d]{4}){3}-[a-f\\d]{12}$/i.test(value.data.email_id)) {\n throw new Error('Resend event requires a received email id')\n }\n const signedId = header(input.headers, 'svix-id')\n if (!signedId) throw new Error('Missing signed event identity')\n return [{ provider: 'resend', eventType: 'resend.email.received', providerEventId: signedId,\n receivedAt: input.now ?? Date.now(), payload: value, headers: {} }]\n },\n eventCatalog: { namespace: 'resend.', closed: true, events: [{ id: 'resend.email.received' }] },\n}\n\n/** Sendblue sends a shared secret verbatim, with no signed timestamp. Deduplicate by message_handle. */\nexport const sendblueWebhookProvider: WebhookProvider = {\n id: 'sendblue',\n verifySignature: ({ headers, secret }) => {\n const supplied = header(headers, 'sb-signing-secret')\n if (!secret || !supplied) return { valid: false, reason: 'invalid_signature' }\n const actual = Buffer.from(supplied), expected = Buffer.from(secret)\n if (actual.length !== expected.length) return { valid: false, reason: 'invalid_signature' }\n return timingSafeEqual(actual, expected)\n ? { valid: true } : { valid: false, reason: 'invalid_signature' }\n },\n parse: (input) => {\n if (Buffer.byteLength(input.rawBody, 'utf8') > 1_048_576) throw new Error('Webhook exceeds 1 MiB')\n const value: unknown = JSON.parse(input.rawBody)\n if (!object(value)) throw new Error('Sendblue webhook requires an object')\n const messageType = typeof value.message_type === 'string' ? value.message_type.toLowerCase() : null\n if (messageType !== 'message' && messageType !== 'group') return []\n if (typeof value.message_handle !== 'string' || !value.message_handle\n || value.message_handle.length > 256 || typeof value.is_outbound !== 'boolean') {\n throw new Error('Sendblue event requires a stable message handle and direction')\n }\n if (value.is_outbound) return []\n return [{ provider: 'sendblue', eventType: 'sendblue.message.received',\n providerEventId: value.message_handle, receivedAt: input.now ?? Date.now(), payload: value, headers: {} }]\n },\n eventCatalog: { namespace: 'sendblue.', closed: true, events: [{ id: 'sendblue.message.received' }] },\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6IA,IAAa,kCAAb,cAAqD,+BAAkE,CAAC;;AAGxH,IAAa,oCAAb,cAAuD,iCAAoE,CAAC;;;;;AAyC5H,IAAa,gBAAb,MAA2B;CACzB;CACA;CACA;CACA;CACA;CACA;CACA;CAEA,YAAY,MAA4B;EACtC,KAAK,YAAY,IAAI,IAAI,KAAK,UAAU,KAAK,MAAM,CAAC,EAAE,IAAI,CAAC,CAAC,CAAC;EAC7D,KAAK,UAAU,KAAK;EACpB,KAAK,gBAAgB,KAAK;EAC1B,KAAK,cAAc,8BAA8B;GAC/C,WAAW;GACX,OAAO,KAAK;GACZ,SAAS,KAAK;EAChB,CAAC;EACD,KAAK,mBAAmB,KAAK,oBAAoB;EACjD,KAAK,UAAU,KAAK,WAAW;EAC/B,KAAK,QAAQ,KAAK,OAAO,KAAK;CAChC;;;CAIA,MAAM,OAAO,SAA+D;EAC1E,MAAM,WAAW,KAAK,UAAU,IAAI,QAAQ,UAAU;EACtD,IAAI,CAAC,UACH,OAAO;GAAE,QAAQ;GAAK,MAAM;IAAE,OAAO;IAAoB,UAAU,QAAQ;GAAW;EAAE;EAE1F,MAAM,SAAS,MAAM,KAAK,cAAc,SAAS,IAAI,QAAQ,OAAO;EACpE,IAAI,CAAC,QACH,OAAO;GAAE,QAAQ;GAAK,MAAM;IAAE,OAAO;IAAkB,UAAU,SAAS;GAAG;EAAE;EAEjF,MAAM,eAAe,SAAS,gBAAgB;GAC5C,SAAS,QAAQ;GACjB,SAAS,QAAQ;GACjB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AAc;EAAE,WAAW;EAAa,QAAQ;EAAM,QAAQ,CAAC,EAAE,IAAI,4BAA4B,CAAC;CAAE;AACtG"}
1
+ {"version":3,"file":"index.js","names":[],"sources":["../../src/webhooks/router.ts","../../src/webhooks/providers.ts","../../src/webhooks/twilio.ts","../../src/webhooks/messaging.ts","../../src/webhooks/whatsapp.ts"],"sourcesContent":["/**\n * @stable Provider-agnostic inbound webhook router.\n *\n * Consumer hooks a single HTTP handler at `/webhook/:provider/:event`\n * (or whatever pathing they prefer) and forwards the request through\n * `WebhookRouter.handle()`. The router:\n *\n * 1. Resolves the registered provider entry.\n * 2. Calls the provider's `verifySignature(rawBody, headers, secrets)`.\n * Failure → 401 fast, no downstream work.\n * 3. Calls the provider's `parse(rawBody, headers)` to extract zero or\n * more normalized events.\n * 4. Awaits the consumer-supplied `deliver(event)` callback.\n * 5. Returns 2xx only after every accepted event finishes durably. A failed\n * or concurrently active delivery returns 503 so the provider retries.\n *\n * Replay protection: providers that sign timestamps (Stripe, Slack)\n * already reject stale signatures inside `verifySignature`. For providers\n * that don't (DocuSeal, GDrive push), the router atomically claims every\n * parsed event before scheduling `deliver()`. Consumers wire this to a\n * durable SETNX/unique insert (D1, Redis, or Postgres) across workers.\n *\n * Why a router and not a per-provider express app: the runtime contract\n * a product cares about is \"an inbound event came in, here's the\n * normalized envelope\". Verification, parsing, and idempotency-dedup\n * are mechanical and provider-specific — the router owns them. The\n * consumer's `deliver()` is the only place product logic runs.\n *\n * Stability: `@stable` — additions to `WebhookEnvelope` must be\n * additive; the router's HTTP contract (paths, status codes) is frozen\n * at 200 (ok), 400 (bad request), 401 (bad signature), 404 (unknown\n * provider), 405 (provider has no inbound surface), and 503 (delivery\n * failed or another worker still owns the delivery).\n */\n\nimport { createHash } from 'node:crypto'\nimport {\n FileSystemAtomicIdempotencyStore,\n InMemoryAtomicIdempotencyStore,\n resolveAtomicIdempotencyStore,\n type AtomicIdempotencyStore,\n type IdempotencyRuntime,\n} from '../idempotency.js'\n\nexport interface WebhookHeaders {\n [name: string]: string | string[] | undefined\n}\n\n/** Normalized inbound event the router emits after parsing. */\nexport interface WebhookEnvelope<TPayload = unknown> {\n /** Provider id (matches the `:provider` path segment). */\n provider: string\n /** Optional event class — e.g., 'customer.subscription.deleted'. The\n * provider's parser decides. Used for routing inside `deliver()`. */\n eventType: string\n /** Provider-emitted event id, when present. Used for the idempotency\n * short-circuit. */\n providerEventId?: string\n /** Wall-clock receive time. */\n receivedAt: number\n /** Provider payload, normalized to the provider's documented event\n * shape. The router does NOT reshape this — `parse()` is the contract. */\n payload: TPayload\n /** Headers passed through for downstream handlers that want them\n * (e.g., to extract custom routing metadata). Always lowercased keys. */\n headers: Record<string, string>\n}\n\nexport type SignatureVerification =\n | { valid: true }\n | { valid: false; reason: string }\n\n/** One declared triggerable event, in the runtime `eventType` namespace a\n * provider's `parse` emits. */\nexport interface TriggerEventDescriptor {\n /** The `eventType` a workflow `provider_event` trigger matches on\n * (`slack.app_mention`, `charge.succeeded`). */\n id: string\n /** Short human label for authoring surfaces; optional. */\n title?: string\n}\n\n/** The set of events a provider can deliver, for author-time validation of\n * workflow `provider_event` triggers. Co-located with the provider's `parse`\n * (the source of the runtime namespace) so the two cannot drift — see the\n * per-provider drift tests. Consumers validate an authored event with two\n * checks: it must carry `namespace` (a prefix-less event on a namespaced\n * provider can never be delivered), and — only when `closed` — be one of\n * `events` (an exhaustive set makes an unlisted event an authoring error). */\nexport interface TriggerEventCatalog {\n /** Required prefix every `parse`-emitted `eventType` carries (`slack.`,\n * `telegram.`), or `null` when the provider emits raw event names (stripe). */\n namespace: string | null\n /** Declared events. Exhaustive iff `closed`; otherwise authoring hints for an\n * open, provider-owned vocabulary. */\n events: readonly TriggerEventDescriptor[]\n /** True when `events` is the COMPLETE deliverable set, so an event outside it\n * is rejected. False when the provider owns an open-ended vocabulary it keeps\n * adding to (Stripe's ~250 types, Slack's Events API) and only `namespace`\n * can be authoritatively enforced. */\n closed: boolean\n}\n\n/** Per-provider plug-in. Stateless — the router calls `verifySignature`\n * then `parse` on every request. The provider's HTTP-shape concerns\n * (e.g., raw body required) are documented per provider. */\nexport interface WebhookProvider {\n /** Stable provider id (`stripe`, `docuseal`, `gdrive`, ...). */\n id: string\n /** Verify the inbound signature. Receives the EXACT raw body string —\n * consumers MUST preserve raw bytes through their HTTP server (do not\n * parse JSON before forwarding here). */\n verifySignature(input: {\n rawBody: string\n headers: WebhookHeaders\n secret: string\n }): SignatureVerification\n /** Parse the validated raw body into zero or more normalized events.\n * A single push payload may carry multiple events (e.g., Slack bulk\n * delivery). Return [] to ack the push as a no-op. */\n parse(input: {\n rawBody: string\n headers: WebhookHeaders\n now?: number\n }): WebhookEnvelope[] | Promise<WebhookEnvelope[]>\n /** Optional provider-specific 200 ACK the router returns on a verified\n * delivery, in place of the default `{ received, total }` JSON. Dropbox Sign\n * acknowledges a delivery only when the body is exactly\n * 'Hello API Event Received' and retries otherwise; this lets such a\n * provider declare the body/headers it needs. The frozen status contract is\n * unchanged — only the 200 body/headers are customized. */\n successResponse?: { body: string; headers?: Record<string, string> }\n /** Declared triggerable-event catalog, for author-time validation of workflow\n * `provider_event` triggers. Optional: a provider without one is left\n * unvalidated by consumers (the trigger still installs). */\n eventCatalog?: TriggerEventCatalog\n}\n\nexport interface WebhookIdempotencyStore extends AtomicIdempotencyStore {}\n\n/** Process-local atomic store for tests and explicitly single-process apps. */\nexport class InMemoryWebhookIdempotencyStore extends InMemoryAtomicIdempotencyStore implements WebhookIdempotencyStore {}\n\n/** Durable store for workers that share the same filesystem directory. */\nexport class FileSystemWebhookIdempotencyStore extends FileSystemAtomicIdempotencyStore implements WebhookIdempotencyStore {}\n\nexport interface WebhookRouterOptions {\n /** Provider registry. Pass any number of providers; routing is by id. */\n providers: WebhookProvider[]\n /** Async callback invoked with every accepted event. The callback must\n * finish its durable enqueue or processing before it resolves. */\n deliver(event: WebhookEnvelope): Promise<void> | void\n /** Resolve the signing secret for a provider id at request time. The\n * router never holds secrets — the consumer's vault resolves them. */\n resolveSecret(providerId: string, headers: WebhookHeaders): Promise<string | null> | string | null\n /** Atomic idempotency store. Production requires shared atomic storage. */\n idempotency?: WebhookIdempotencyStore\n /** Runtime controls the safe default. Production fails closed without a\n * shared store; test/development use an in-memory store when omitted. */\n runtime?: IdempotencyRuntime\n /** TTL on idempotency entries. Default 7 days — long enough that a\n * provider's normal retry-window can't re-deliver. */\n idempotencyTtlMs?: number\n /** Surface delivery errors. Default: console.error. */\n onError?(err: unknown, context: { provider: string; eventType?: string; providerEventId?: string }): void\n /** Override `now()` for tests. */\n now?(): number\n}\n\nexport interface WebhookRouterRequest {\n providerId: string\n rawBody: string\n headers: WebhookHeaders\n}\n\nexport interface WebhookRouterResponse {\n status: number\n body: unknown\n headers?: Record<string, string>\n}\n\n/**\n * Router instance. Stateless aside from the provider registry — safe to\n * share across requests; build once per process.\n */\nexport class WebhookRouter {\n private readonly providers: Map<string, WebhookProvider>\n private readonly deliver: WebhookRouterOptions['deliver']\n private readonly resolveSecret: WebhookRouterOptions['resolveSecret']\n private readonly idempotency: WebhookIdempotencyStore\n private readonly idempotencyTtlMs: number\n private readonly onError: NonNullable<WebhookRouterOptions['onError']>\n private readonly nowFn: () => number\n\n constructor(opts: WebhookRouterOptions) {\n this.providers = new Map(opts.providers.map((p) => [p.id, p]))\n this.deliver = opts.deliver\n this.resolveSecret = opts.resolveSecret\n this.idempotency = resolveAtomicIdempotencyStore({\n component: 'WebhookRouter',\n store: opts.idempotency,\n runtime: opts.runtime,\n })\n this.idempotencyTtlMs = opts.idempotencyTtlMs ?? 7 * 24 * 60 * 60 * 1000\n this.onError = opts.onError ?? defaultOnError\n this.nowFn = opts.now ?? Date.now\n }\n\n /** Process one inbound webhook request. Pure with respect to side-\n * effects on the router instance — safe to call concurrently. */\n async handle(request: WebhookRouterRequest): Promise<WebhookRouterResponse> {\n const provider = this.providers.get(request.providerId)\n if (!provider) {\n return { status: 404, body: { error: 'unknown_provider', provider: request.providerId } }\n }\n const secret = await this.resolveSecret(provider.id, request.headers)\n if (!secret) {\n return { status: 401, body: { error: 'missing_secret', provider: provider.id } }\n }\n const verification = provider.verifySignature({\n rawBody: request.rawBody,\n headers: request.headers,\n secret,\n })\n if (!verification.valid) {\n return { status: 401, body: { error: 'invalid_signature', reason: verification.reason } }\n }\n\n let events: WebhookEnvelope[]\n try {\n events = await provider.parse({ rawBody: request.rawBody, headers: request.headers, now: this.nowFn() })\n } catch (err) {\n this.onError(err, { provider: provider.id })\n return { status: 400, body: { error: 'parse_error', message: errMessage(err) } }\n }\n\n const accepted: Array<{ event: WebhookEnvelope; key: string }> = []\n let inProgress = 0\n try {\n for (const [index, event] of events.entries()) {\n const key = eventKey(provider.id, event, index, request.rawBody)\n const status = await this.idempotency.claimStatus(key, this.idempotencyTtlMs)\n if (status === 'acquired') accepted.push({ event, key })\n if (status === 'in_progress') inProgress++\n }\n } catch (err) {\n // Do not strand earlier claims when a later claim detects an unavailable\n // or corrupt shared store. The request remains failed closed.\n await Promise.allSettled(accepted.map(({ key }) => this.idempotency.release(key)))\n throw err\n }\n\n const delivery = await this.deliverEach(accepted)\n if (delivery.failed > 0 || inProgress > 0) {\n return {\n status: 503,\n body: {\n error: delivery.failed > 0 ? 'delivery_failed' : 'delivery_in_progress',\n received: delivery.succeeded,\n failed: delivery.failed,\n inProgress,\n total: events.length,\n },\n }\n }\n\n if (provider.successResponse) {\n return {\n status: 200,\n body: provider.successResponse.body,\n headers: provider.successResponse.headers,\n }\n }\n return { status: 200, body: { received: accepted.length, total: events.length } }\n }\n\n private async deliverEach(\n events: Array<{ event: WebhookEnvelope; key: string }>,\n ): Promise<{ succeeded: number; failed: number }> {\n let succeeded = 0\n let failed = 0\n for (const { event, key } of events) {\n try {\n await this.deliver(event)\n } catch (err) {\n try {\n await this.idempotency.release(key)\n } catch (releaseError) {\n this.onError(releaseError, {\n provider: event.provider,\n eventType: event.eventType,\n providerEventId: event.providerEventId,\n })\n }\n this.onError(err, {\n provider: event.provider,\n eventType: event.eventType,\n providerEventId: event.providerEventId,\n })\n failed++\n continue\n }\n try {\n await this.idempotency.complete(key)\n succeeded++\n } catch (err) {\n try {\n await this.idempotency.release(key)\n } catch (releaseError) {\n this.onError(releaseError, {\n provider: event.provider,\n eventType: event.eventType,\n providerEventId: event.providerEventId,\n })\n }\n this.onError(err, {\n provider: event.provider,\n eventType: event.eventType,\n providerEventId: event.providerEventId,\n })\n failed++\n }\n }\n return { succeeded, failed }\n }\n}\n\nfunction defaultOnError(err: unknown, context: { provider: string; eventType?: string; providerEventId?: string }): void {\n // eslint-disable-next-line no-console\n console.error('[WebhookRouter]', context, err)\n}\n\nfunction errMessage(err: unknown): string {\n return err instanceof Error ? err.message : String(err)\n}\n\nfunction eventKey(providerId: string, event: WebhookEnvelope, index: number, rawBody: string): string {\n if (event.providerEventId?.trim()) return `${providerId}:id:${event.providerEventId.trim()}`\n // Providers without stable event ids still need collision-resistant keys.\n // A 32-bit hash could drop a distinct signed event after a birthday collision.\n const bodyHash = createHash('sha256').update(rawBody, 'utf8').digest('hex')\n return `${providerId}:body:${event.eventType}:${index}:${bodyHash}`\n}\n","/**\n * Pre-built `WebhookProvider` implementations for the inbound surfaces\n * the substrate ships first-party verifiers for.\n *\n * Each provider implementation is intentionally thin: it delegates\n * signature verification to the corresponding pure function in\n * `connectors/webhooks.ts` and parses the body into one or more\n * normalized `WebhookEnvelope` rows. Anything provider-specific that\n * doesn't fit cleanly (Slack URL-verification handshake, etc.) is\n * surfaced via the envelope `eventType` so the consumer's `deliver()`\n * can branch.\n */\n\nimport {\n firstHeader,\n verifyEmailSignature,\n verifyHmacSignature,\n verifySlackSignature,\n verifyStripeSignature,\n} from '../connectors/webhooks.js'\nimport type { WebhookEnvelope, WebhookHeaders, WebhookProvider, SignatureVerification } from './router.js'\nimport { createHash, createHmac, timingSafeEqual } from 'node:crypto'\nimport type { InboundEmailPayload } from '../conversation-events/index.js'\n\nexport type { InboundEmailPayload } from '../conversation-events/index.js'\n\n/** Stripe webhook provider. Signature header `Stripe-Signature`. */\nexport const stripeWebhookProvider: WebhookProvider = {\n id: 'stripe',\n verifySignature({ rawBody, headers, secret }): SignatureVerification {\n const sig = firstHeader(headers, 'stripe-signature')\n if (!sig) return { valid: false, reason: 'missing_stripe_signature' }\n return verifyStripeSignature(rawBody, sig, secret)\n ? { valid: true }\n : { valid: false, reason: 'invalid_signature' }\n },\n parse({ rawBody, headers, now }): WebhookEnvelope[] {\n const evt = safeJson(rawBody)\n if (!evt || typeof evt !== 'object') return []\n const e = evt as { id?: unknown; type?: unknown }\n return [\n {\n provider: 'stripe',\n eventType: typeof e.type === 'string' ? e.type : 'stripe.unknown',\n providerEventId: typeof e.id === 'string' ? e.id : undefined,\n receivedAt: now ?? Date.now(),\n payload: evt,\n headers: normalizeHeaders(headers),\n },\n ]\n },\n eventCatalog: {\n // Stripe emits the raw event `type` (`charge.succeeded`) — no namespace, and\n // an OPEN, versioned vocabulary (~250 types), so these are authoring hints,\n // not a gate.\n namespace: null,\n closed: false,\n events: [\n { id: 'checkout.session.completed' },\n { id: 'checkout.session.expired' },\n { id: 'customer.subscription.created' },\n { id: 'customer.subscription.updated' },\n { id: 'customer.subscription.deleted' },\n { id: 'invoice.paid' },\n { id: 'invoice.payment_failed' },\n { id: 'payment_intent.succeeded' },\n { id: 'payment_intent.payment_failed' },\n { id: 'charge.succeeded' },\n { id: 'charge.refunded' },\n ],\n },\n}\n\n/** Slack Events API provider. Handles the `url_verification` handshake\n * by emitting a synthetic event the consumer's `deliver()` can echo. */\n/**\n * Answer for Slack's app-config handshake, or `null` when the body is not one.\n *\n * Slack accepts an app install only when the response body carries the\n * `challenge` value from the request. `WebhookRouter` shapes its own 200 body\n * and `deliver()` returns void, so the handshake cannot be answered through the\n * router — call this in the HTTP handler ahead of `router.handle` and return\n * its result when it is not `null`.\n */\nexport function slackHandshakeResponse(rawBody: string): { challenge: string } | null {\n const parsed = safeJson(rawBody) as { type?: unknown; challenge?: unknown } | null\n if (!parsed || typeof parsed !== 'object') return null\n if (parsed.type !== 'url_verification') return null\n return { challenge: typeof parsed.challenge === 'string' ? parsed.challenge : '' }\n}\n\nexport const slackWebhookProvider: WebhookProvider = {\n id: 'slack',\n verifySignature({ rawBody, headers, secret }): SignatureVerification {\n const sig = firstHeader(headers, 'x-slack-signature')\n const ts = firstHeader(headers, 'x-slack-request-timestamp')\n if (!sig || !ts) return { valid: false, reason: 'missing_slack_signature_or_timestamp' }\n return verifySlackSignature(rawBody, sig, ts, secret)\n ? { valid: true }\n : { valid: false, reason: 'invalid_signature' }\n },\n parse({ rawBody, headers, now }): WebhookEnvelope[] {\n const evt = safeJson(rawBody) as { type?: string; event_id?: string; event?: { type?: string } } | null\n if (!evt || typeof evt !== 'object') return []\n if (evt.type === 'url_verification') {\n return [{\n provider: 'slack',\n eventType: 'slack.url_verification',\n receivedAt: now ?? Date.now(),\n payload: evt,\n headers: normalizeHeaders(headers),\n }]\n }\n const eventType = `slack.${evt.event?.type ?? evt.type ?? 'unknown'}`\n return [{\n provider: 'slack',\n eventType,\n providerEventId: typeof evt.event_id === 'string' ? evt.event_id : undefined,\n receivedAt: now ?? Date.now(),\n payload: evt,\n headers: normalizeHeaders(headers),\n }]\n },\n eventCatalog: {\n // Slack namespaces every event `slack.<events-api-type>`, so a prefix-less\n // event is provably dead. The Events API vocabulary is open (Slack adds\n // types), so `events` are hints, not a gate. `slack.url_verification` (the\n // config handshake) is intentionally excluded — it carries no workflow\n // meaning and is answered before dispatch.\n namespace: 'slack.',\n closed: false,\n events: [\n { id: 'slack.message' },\n { id: 'slack.app_mention' },\n { id: 'slack.reaction_added' },\n { id: 'slack.reaction_removed' },\n { id: 'slack.channel_created' },\n { id: 'slack.member_joined_channel' },\n { id: 'slack.team_join' },\n { id: 'slack.app_home_opened' },\n { id: 'slack.file_shared' },\n ],\n },\n}\n\n/** DocuSeal webhook provider. Signature header `X-Docuseal-Signature`. */\nexport const docusealWebhookProvider: WebhookProvider = {\n id: 'docuseal',\n verifySignature({ rawBody, headers, secret }): SignatureVerification {\n const sig = firstHeader(headers, 'x-docuseal-signature')\n if (!sig) return { valid: false, reason: 'missing_docuseal_signature' }\n const expected = createHmac('sha256', secret).update(rawBody).digest('hex')\n const a = Buffer.from(sig.toLowerCase(), 'utf-8')\n const b = Buffer.from(expected, 'utf-8')\n if (a.length !== b.length) return { valid: false, reason: 'invalid_signature' }\n return timingSafeEqual(a, b) ? { valid: true } : { valid: false, reason: 'invalid_signature' }\n },\n parse({ rawBody, headers, now }): WebhookEnvelope[] {\n const evt = safeJson(rawBody) as { event_type?: string; event_id?: string } | null\n if (!evt || typeof evt !== 'object') return []\n return [{\n provider: 'docuseal',\n eventType: `docuseal.${evt.event_type ?? 'unknown'}`,\n providerEventId: typeof evt.event_id === 'string' ? evt.event_id : undefined,\n receivedAt: now ?? Date.now(),\n payload: evt,\n headers: normalizeHeaders(headers),\n }]\n },\n eventCatalog: {\n // DocuSeal namespaces every event `docuseal.<event_type>`. The documented\n // set is small but not derivable from `parse`, so it stays open (namespace\n // is the authoritative gate) with the documented types as hints.\n namespace: 'docuseal.',\n closed: false,\n events: [\n { id: 'docuseal.form.viewed' },\n { id: 'docuseal.form.started' },\n { id: 'docuseal.form.completed' },\n { id: 'docuseal.form.declined' },\n { id: 'docuseal.submission.created' },\n { id: 'docuseal.submission.completed' },\n { id: 'docuseal.submission.expired' },\n { id: 'docuseal.submission.archived' },\n ],\n },\n}\n\n/** Tangle inbound-email provider. Our own Cloudflare Email Routing worker parses\n * the MIME, JSON-serializes it, timestamped-HMAC signs it with the\n * per-connection secret, and POSTs it here — there is no upstream mail-vendor\n * signature, we are both signer and verifier. One inbound message → one\n * `email.received` envelope; `payload.to` carries the owner-resolution address. */\nexport const emailWebhookProvider: WebhookProvider = {\n id: 'email',\n verifySignature({ rawBody, headers, secret }): SignatureVerification {\n const sig = firstHeader(headers, 'x-tangle-email-signature')\n if (!sig) return { valid: false, reason: 'missing_email_signature' }\n const ts = firstHeader(headers, 'x-tangle-email-timestamp')\n if (!ts) return { valid: false, reason: 'missing_email_timestamp' }\n return verifyEmailSignature(rawBody, sig, ts, secret)\n ? { valid: true }\n : { valid: false, reason: 'invalid_signature' }\n },\n parse({ rawBody, headers, now }): WebhookEnvelope[] {\n const msg = safeJson(rawBody) as Partial<InboundEmailPayload> | null\n if (!msg || typeof msg !== 'object') return []\n // `from` + `to` are load-bearing (owner resolution keys off `to`); a body\n // missing either is a malformed edge post — ack as a no-op rather than\n // dispatch an unroutable event.\n if (typeof msg.from !== 'string' || typeof msg.to !== 'string') return []\n return [{\n provider: 'email',\n eventType: 'email.received',\n providerEventId: typeof msg.messageId === 'string' ? msg.messageId : undefined,\n receivedAt: now ?? Date.now(),\n payload: msg as InboundEmailPayload,\n headers: normalizeHeaders(headers),\n }]\n },\n eventCatalog: {\n // We own the edge, so the inbound surface is exactly one closed event.\n namespace: 'email.',\n closed: true,\n events: [{ id: 'email.received' }],\n },\n}\n\n/** Gmail push provider. Cloud Pub/Sub posts a JWT-signed envelope; the\n * *payload* is base64 JSON describing the changed history range. The\n * signature scheme here is the Pub/Sub JWT auth header — when supplied,\n * consumers SHOULD verify the JWT against Google's well-known\n * certificates. We accept the simpler \"Bearer <pubsub-shared-secret>\"\n * variant by default (matching `verifyHmacSignature`). */\nexport const gmailWebhookProvider: WebhookProvider = {\n id: 'gmail',\n verifySignature({ headers, secret }): SignatureVerification {\n const auth = firstHeader(headers, 'authorization')\n if (!auth) return { valid: false, reason: 'missing_authorization' }\n // Accept either \"Bearer <secret>\" or \"Token <secret>\" formats. This\n // is the simple per-tenant shared-secret path; JWT verification is\n // left to the consumer's `deliver()` when they need full Google JWT\n // chain validation.\n const m = /^(?:Bearer|Token)\\s+(.+)$/i.exec(auth)\n if (!m) return { valid: false, reason: 'invalid_authorization_format' }\n const a = Buffer.from(m[1]!, 'utf-8')\n const b = Buffer.from(secret, 'utf-8')\n if (a.length !== b.length) return { valid: false, reason: 'invalid_signature' }\n return timingSafeEqual(a, b) ? { valid: true } : { valid: false, reason: 'invalid_signature' }\n },\n parse({ rawBody, headers, now }): WebhookEnvelope[] {\n const envelope = safeJson(rawBody) as { message?: { data?: string; messageId?: string; publishTime?: string } } | null\n if (!envelope?.message?.data) return []\n let payload: unknown\n try {\n payload = JSON.parse(Buffer.from(envelope.message.data, 'base64').toString('utf-8'))\n } catch {\n return []\n }\n const inner = payload as { historyId?: number | string; emailAddress?: string }\n return [{\n provider: 'gmail',\n eventType: 'gmail.history_changed',\n providerEventId: envelope.message.messageId,\n receivedAt: now ?? Date.now(),\n payload: { ...inner, publishTime: envelope.message.publishTime },\n headers: normalizeHeaders(headers),\n }]\n },\n}\n\n/** Google Drive push provider. Drive does NOT sign the body — it uses\n * the per-channel token (`X-Goog-Channel-Token`) as the shared secret.\n * The router compares it constant-time against the resolved secret. */\nexport const gdriveWebhookProvider: WebhookProvider = {\n id: 'gdrive',\n verifySignature({ headers, secret }): SignatureVerification {\n const token = firstHeader(headers, 'x-goog-channel-token')\n if (!token) return { valid: false, reason: 'missing_channel_token' }\n const a = Buffer.from(token, 'utf-8')\n const b = Buffer.from(secret, 'utf-8')\n if (a.length !== b.length) return { valid: false, reason: 'invalid_signature' }\n return timingSafeEqual(a, b) ? { valid: true } : { valid: false, reason: 'invalid_signature' }\n },\n parse({ headers, now }): WebhookEnvelope[] {\n const resourceId = firstHeader(headers, 'x-goog-resource-id')\n const resourceState = firstHeader(headers, 'x-goog-resource-state') ?? 'unknown'\n const channelId = firstHeader(headers, 'x-goog-channel-id')\n const messageNumber = firstHeader(headers, 'x-goog-message-number')\n if (resourceState === 'sync') {\n return [{\n provider: 'gdrive',\n eventType: 'gdrive.channel.sync',\n providerEventId: messageNumber ? `${channelId}-${messageNumber}` : undefined,\n receivedAt: now ?? Date.now(),\n payload: { channelId, resourceId, resourceState },\n headers: normalizeHeaders(headers),\n }]\n }\n return [{\n provider: 'gdrive',\n eventType: `gdrive.resource.${resourceState}`,\n providerEventId: messageNumber ? `${channelId}-${messageNumber}` : undefined,\n receivedAt: now ?? Date.now(),\n payload: { channelId, resourceId, resourceState },\n headers: normalizeHeaders(headers),\n }]\n },\n}\n\n/** The Telegram update keys, in resolution order — the first present key names\n * the update kind (`telegram.message`, `telegram.callback_query`, …). */\nconst TELEGRAM_UPDATE_KEYS = [\n 'message',\n 'edited_message',\n 'channel_post',\n 'edited_channel_post',\n 'business_connection',\n 'business_message',\n 'edited_business_message',\n 'deleted_business_messages',\n 'message_reaction',\n 'message_reaction_count',\n 'inline_query',\n 'chosen_inline_result',\n 'callback_query',\n 'shipping_query',\n 'pre_checkout_query',\n 'purchased_paid_media',\n 'poll',\n 'poll_answer',\n 'my_chat_member',\n 'chat_member',\n 'chat_join_request',\n 'chat_boost',\n 'removed_chat_boost',\n] as const\n\n/** Telegram Bot API webhook provider. Telegram does NOT sign the body — it\n * authenticates by echoing the `secret_token` set at `setWebhook` time in the\n * `X-Telegram-Bot-Api-Secret-Token` header. We compare it constant-time to the\n * per-connection secret. `update_id` is the idempotency anchor. */\nexport const telegramWebhookProvider: WebhookProvider = {\n id: 'telegram',\n verifySignature({ headers, secret }): SignatureVerification {\n const token = firstHeader(headers, 'x-telegram-bot-api-secret-token')\n if (!token) return { valid: false, reason: 'missing_telegram_secret_token' }\n const a = Buffer.from(token, 'utf-8')\n const b = Buffer.from(secret, 'utf-8')\n if (a.length !== b.length) return { valid: false, reason: 'invalid_secret_token' }\n return timingSafeEqual(a, b) ? { valid: true } : { valid: false, reason: 'invalid_secret_token' }\n },\n parse({ rawBody, headers, now }): WebhookEnvelope[] {\n const evt = safeJson(rawBody) as ({ update_id?: number } & Record<string, unknown>) | null\n if (!evt || typeof evt !== 'object') return []\n const kind = TELEGRAM_UPDATE_KEYS.find((k) => k in evt) ?? 'unknown'\n return [{\n provider: 'telegram',\n eventType: `telegram.${kind}`,\n providerEventId: typeof evt.update_id === 'number' ? String(evt.update_id) : undefined,\n receivedAt: now ?? Date.now(),\n payload: evt,\n headers: normalizeHeaders(headers),\n }]\n },\n eventCatalog: {\n // Derived from the SAME TELEGRAM_UPDATE_KEYS `parse` matches on, so the\n // catalog is exhaustive and cannot drift — hence `closed`. (`telegram.unknown`,\n // parse's fallback for an update with no recognized key, is intentionally\n // excluded: it is not a triggerable event.)\n namespace: 'telegram.',\n closed: true,\n events: TELEGRAM_UPDATE_KEYS.map((k) => ({ id: `telegram.${k}` })),\n },\n}\n\n/** Dropbox Sign (HelloSign) webhook provider. There is NO signature header:\n * the body carries `event.event_hash = HMAC_SHA256(apiKey, event_time +\n * event_type)` (hex), so the `secret` here is the account's API key (the\n * caller resolves it from the connection credential). The body is JSON, or a\n * `json=`/multipart `name=\"json\"` form field on older apps. */\nexport const hellosignWebhookProvider: WebhookProvider = {\n id: 'hellosign',\n // Dropbox Sign acknowledges a delivery only when the response body is exactly\n // 'Hello API Event Received'; any other body makes it retry the event.\n successResponse: { body: 'Hello API Event Received', headers: { 'content-type': 'text/plain' } },\n verifySignature({ rawBody, secret }): SignatureVerification {\n const body = parseHelloSignBody(rawBody)\n const event = body?.event\n if (\n !event ||\n typeof event.event_time !== 'string' ||\n typeof event.event_type !== 'string' ||\n typeof event.event_hash !== 'string'\n ) {\n return { valid: false, reason: 'missing_event_fields' }\n }\n const expected = createHmac('sha256', secret)\n .update(`${event.event_time}${event.event_type}`)\n .digest('hex')\n const a = Buffer.from(event.event_hash.toLowerCase(), 'utf-8')\n const b = Buffer.from(expected.toLowerCase(), 'utf-8')\n if (a.length !== b.length) return { valid: false, reason: 'invalid_signature' }\n return timingSafeEqual(a, b) ? { valid: true } : { valid: false, reason: 'invalid_signature' }\n },\n parse({ rawBody, headers, now }): WebhookEnvelope[] {\n const body = parseHelloSignBody(rawBody)\n const event = body?.event\n if (!event) return []\n return [{\n provider: 'hellosign',\n eventType: `hellosign.${event.event_type ?? 'unknown'}`,\n // Prefer Dropbox Sign's canonical `event.event_id` (stable across\n // redeliveries, correlates with the provider). Fall back to a digest of\n // the parsed body when it's absent: `event_hash` = HMAC(apiKey,\n // event_time + event_type), so two distinct same-type events in the same\n // second share it — the body digest stays distinct across differing\n // events and identical across redeliveries of one event.\n providerEventId:\n typeof event.event_id === 'string' && event.event_id.length > 0\n ? event.event_id\n : createHash('sha256').update(JSON.stringify(body)).digest('hex'),\n receivedAt: now ?? Date.now(),\n payload: body,\n headers: normalizeHeaders(headers),\n }]\n },\n eventCatalog: {\n // Dropbox Sign namespaces every event `hellosign.<event_type>`. Documented\n // set as hints; namespace is the authoritative gate. `callback_test` (the\n // app-config ping) is intentionally excluded — it is not a workflow event.\n namespace: 'hellosign.',\n closed: false,\n events: [\n { id: 'hellosign.signature_request_sent' },\n { id: 'hellosign.signature_request_viewed' },\n { id: 'hellosign.signature_request_signed' },\n { id: 'hellosign.signature_request_all_signed' },\n { id: 'hellosign.signature_request_declined' },\n { id: 'hellosign.signature_request_reassigned' },\n { id: 'hellosign.signature_request_remind' },\n { id: 'hellosign.signature_request_canceled' },\n { id: 'hellosign.signature_request_expired' },\n ],\n },\n}\n\n/** Keys Clay HTTP-API-action payloads commonly carry that can anchor\n * idempotency, in resolution order. Clay bodies are operator-defined, so we\n * fall back to a digest of the raw body when none is present. */\nconst CLAY_EVENT_ID_KEYS = ['id', 'rowId', 'recordId', 'clayRowId', 'eventId'] as const\n\n/** Clay webhook provider — receives rows Clay delivers via its outbound \"HTTP\n * API\" action. Clay does NOT sign its requests and adds no proprietary\n * headers (the `x-clay-signature: sha256=...` header seen in some third-party\n * code is unverified inference, not in Clay's docs). Authentication is\n * therefore a pre-shared secret the operator adds as a header on the Clay\n * action; we compare it constant-time against the per-connection secret.\n * Header: `x-clay-webhook-secret`. The body is whatever columns the operator\n * mapped (free-form JSON with no canonical event id), so idempotency anchors\n * on a common id field when present, else a sha256 of the raw body. */\nexport const clayWebhookProvider: WebhookProvider = {\n id: 'clay',\n verifySignature({ headers, secret }): SignatureVerification {\n const token = firstHeader(headers, 'x-clay-webhook-secret')\n if (!token) return { valid: false, reason: 'missing_clay_webhook_secret' }\n const a = Buffer.from(token, 'utf-8')\n const b = Buffer.from(secret, 'utf-8')\n if (a.length !== b.length) return { valid: false, reason: 'invalid_signature' }\n return timingSafeEqual(a, b) ? { valid: true } : { valid: false, reason: 'invalid_signature' }\n },\n parse({ rawBody, headers, now }): WebhookEnvelope[] {\n const evt = safeJson(rawBody)\n if (!evt || typeof evt !== 'object') return []\n const record = evt as Record<string, unknown>\n const idKey = CLAY_EVENT_ID_KEYS.find(\n (k) => typeof record[k] === 'string' || typeof record[k] === 'number',\n )\n const providerEventId = idKey\n ? String(record[idKey])\n : createHash('sha256').update(rawBody).digest('hex')\n const eventType =\n typeof record.event === 'string'\n ? `clay.${record.event}`\n : typeof record.type === 'string'\n ? `clay.${record.type}`\n : 'clay.row'\n return [{\n provider: 'clay',\n eventType,\n providerEventId,\n receivedAt: now ?? Date.now(),\n payload: evt,\n headers: normalizeHeaders(headers),\n }]\n },\n}\n\ninterface HelloSignInboundBody {\n event?: {\n event_time?: string\n event_type?: string\n event_hash?: string\n event_id?: string\n }\n}\n\n/** Dropbox Sign posts JSON, a form-urlencoded `json=` field, or a\n * multipart `name=\"json\"` part depending on app vintage. Mirrors the\n * connector adapter's body extraction. */\nfunction parseHelloSignBody(rawBody: string): HelloSignInboundBody | null {\n try {\n const json = JSON.parse(rawBody) as HelloSignInboundBody\n if (json && typeof json === 'object') return json\n } catch {\n // fall through to form-data extraction\n }\n // application/x-www-form-urlencoded `json=` field. URLSearchParams decodes\n // `+` as a space (decodeURIComponent leaves it literal) and resolves\n // percent-escapes, so space-bearing string fields aren't corrupted.\n const formJson = new URLSearchParams(rawBody).get('json')\n if (formJson) {\n try {\n return JSON.parse(formJson) as HelloSignInboundBody\n } catch {\n return null\n }\n }\n // multipart/form-data with a `name=\"json\"` part. Extract linearly (indexOf +\n // fixed-shape separator probes, no unbounded lazy regex) so a hostile body\n // can't drive quadratic backtracking on this public ingress.\n return extractMultipartJsonField(rawBody)\n}\n\n/** Pull the `json` part out of a multipart/form-data body without an\n * unbounded backtracking regex. Returns null if the part isn't present or\n * isn't valid JSON. The separator probes (`\\r?\\n\\r?\\n`, `\\r?\\n--`) are\n * fixed-shape (no `*`/`+`), so each runs in a single linear scan. */\nfunction extractMultipartJsonField(rawBody: string): HelloSignInboundBody | null {\n const nameIdx = rawBody.indexOf('name=\"json\"')\n if (nameIdx === -1) return null\n const afterName = rawBody.slice(nameIdx)\n const sep = /\\r?\\n\\r?\\n/.exec(afterName)\n if (!sep) return null\n const partBody = afterName.slice(sep.index + sep[0].length)\n const end = /\\r?\\n--/.exec(partBody)\n if (!end) return null\n try {\n return JSON.parse(partBody.slice(0, end.index)) as HelloSignInboundBody\n } catch {\n return null\n }\n}\n\n/** Generic HMAC provider — for the long-tail webhook source where the\n * caller has standardised on a single sha256-of-body scheme. Header\n * `X-Signature` by default; override at provider-build time if needed. */\nexport function genericHmacWebhookProvider(options: {\n id: string\n signatureHeader?: string\n algorithm?: 'sha256' | 'sha1' | 'sha512'\n signaturePrefix?: string\n /** Parser to convert the raw body into envelopes. Defaults to\n * \"one event with eventType=<provider>.event and payload=JSON\". */\n parse?: WebhookProvider['parse']\n}): WebhookProvider {\n const header = options.signatureHeader ?? 'x-signature'\n return {\n id: options.id,\n verifySignature({ rawBody, headers, secret }) {\n const sig = firstHeader(headers, header)\n if (!sig) return { valid: false, reason: `missing_${header}` }\n return verifyHmacSignature(rawBody, sig, secret, {\n algorithm: options.algorithm ?? 'sha256',\n signaturePrefix: options.signaturePrefix,\n })\n ? { valid: true }\n : { valid: false, reason: 'invalid_signature' }\n },\n parse:\n options.parse ??\n (({ rawBody, headers, now }) => {\n const evt = safeJson(rawBody) ?? rawBody\n return [{\n provider: options.id,\n eventType: `${options.id}.event`,\n receivedAt: now ?? Date.now(),\n payload: evt,\n headers: normalizeHeaders(headers),\n }]\n }),\n }\n}\n\nfunction safeJson(s: string): unknown {\n try {\n return JSON.parse(s)\n } catch {\n return null\n }\n}\n\nfunction normalizeHeaders(headers: WebhookHeaders): Record<string, string> {\n const out: Record<string, string> = {}\n for (const [k, v] of Object.entries(headers)) {\n if (v === undefined) continue\n const value = Array.isArray(v) ? v[0] : v\n if (typeof value === 'string') out[k.toLowerCase()] = value\n }\n return out\n}\n","import { authenticateTwilioForm } from '../twilio/index.js'\nimport type { WebhookProvider } from './router.js'\n\n/** Bind to a trusted exact external route; forwarded Host headers are not a signing authority. */\nexport function createTwilioWebhookProvider(options: {\n url: string\n accountSid: string\n kind: 'message' | 'status'\n providerId?: 'twilio' | 'twilio-sms'\n}): WebhookProvider {\n if (!['message', 'status'].includes(options.kind)) throw new Error('Choose message or status webhook mode')\n const bound = { ...options }\n const providerId = bound.providerId ?? 'twilio'\n return {\n id: providerId,\n verifySignature({ rawBody, headers, secret }) {\n const key = Object.keys(headers).find(k => k.toLowerCase() === 'x-twilio-signature')\n const signature = key ? headers[key] : undefined\n try {\n authenticateTwilioForm({ url: bound.url, rawBody, signature, authToken: secret, accountSid: bound.accountSid })\n return { valid: true }\n } catch { return { valid: false, reason: 'invalid_twilio_form_or_signature' } }\n },\n parse({ rawBody, now }) {\n const data = Object.fromEntries(new URLSearchParams(rawBody))\n if (!/^(SM|MM)[a-f0-9]{32}$/i.test(data.MessageSid ?? '')) throw new Error('Twilio event has no valid message identity')\n const status = bound.kind === 'status' ? data.MessageStatus : 'received'\n if (!status || !/^[a-z_]+$/.test(status)) throw new Error('Twilio status event has no status')\n return [{ provider: providerId, eventType: `${providerId}.message.${bound.kind === 'status' ? 'status' : 'received'}`,\n // A message SID alone would incorrectly deduplicate all subsequent delivery states.\n providerEventId: `${bound.accountSid}:${data.MessageSid}:${bound.kind}:${status}`,\n receivedAt: now ?? Date.now(), payload: { ...data, __receivedAt: new Date(now ?? Date.now()).toISOString() }, headers: {} }]\n },\n successResponse: { body: '<Response/>', headers: { 'Content-Type': 'text/xml; charset=utf-8' } },\n eventCatalog: { namespace: `${providerId}.`, closed: true,\n events: [{ id: `${providerId}.message.received` }, { id: `${providerId}.message.status` }] },\n }\n}\n\n/** Register this provider for Twilio SMS/MMS Hub lines. The older constructor retains its twilio event IDs. */\nexport function createTwilioSmsWebhookProvider(options: {\n url: string\n accountSid: string\n kind: 'message' | 'status'\n}): WebhookProvider {\n return createTwilioWebhookProvider({ ...options, providerId: 'twilio-sms' })\n}\n","import { createHmac, timingSafeEqual } from 'node:crypto'\nimport { verifyHmacSignature, verifyStripeSignature } from '../connectors/webhooks.js'\nimport type { WebhookEnvelope, WebhookHeaders, WebhookProvider } from './router.js'\n\n/** A repeated authentication header is ambiguous, never an invitation to pick one. */\nfunction header(headers: WebhookHeaders, name: string): string | null {\n const entries = Object.entries(headers).filter(([key]) => key.toLowerCase() === name)\n if (entries.length !== 1) return null\n const value = entries[0]?.[1]\n return typeof value === 'string' && value.length > 0 && value.length <= 4096 ? value : null\n}\n\nfunction fresh(timestamp: string | null, now: number): timestamp is string {\n return timestamp !== null && /^\\d{1,12}$/.test(timestamp)\n && Number.isSafeInteger(Number(timestamp))\n && Number.isFinite(now) && Math.abs(now - Number(timestamp)) <= 300\n}\n\n/** Inkbox signs the literal identity secret and request-id.timestamp.raw-body. */\nexport function verifyInkboxWebhook(rawBody: string, headers: WebhookHeaders, secret: string, now = Date.now() / 1000): boolean {\n const id = header(headers, 'x-inkbox-request-id')\n const timestamp = header(headers, 'x-inkbox-timestamp')\n const signature = header(headers, 'x-inkbox-signature')\n return Boolean(secret && id && signature && fresh(timestamp, now)\n && verifyHmacSignature(`${id}.${timestamp}.${rawBody}`, signature, secret, { signaturePrefix: 'sha256=' }))\n}\n\n/** Standard Webhooks signs the exact request bytes and three bounded headers. */\nfunction verifyStandardWebhook(rawBody: string, headers: WebhookHeaders, secret: string, prefix: 'webhook' | 'svix', now: number): boolean {\n const id = header(headers, `${prefix}-id`)\n const timestamp = header(headers, `${prefix}-timestamp`)\n const signature = header(headers, `${prefix}-signature`)\n if (!id || !signature || !fresh(timestamp, now) || !secret.startsWith('whsec_')) return false\n const encoded = secret.slice(6)\n if (!/^[A-Za-z0-9+/]+={0,2}$/.test(encoded)) return false\n const key = Buffer.from(encoded, 'base64')\n if (key.length < 16 || key.toString('base64') !== encoded) return false\n const expected = createHmac('sha256', key).update(`${id}.${timestamp}.${rawBody}`).digest('base64')\n return signature.split(' ').some((part) => part.startsWith('v1,')\n && part.slice(3).length === expected.length\n && timingSafeEqual(Buffer.from(part.slice(3)), Buffer.from(expected)))\n}\n\n/** Both Linq services use Standard Webhooks with a base64-decoded signing key. */\nexport function verifyLinqWebhook(rawBody: string, headers: WebhookHeaders, secret: string, now = Date.now() / 1000): boolean {\n return verifyStandardWebhook(rawBody, headers, secret, 'webhook', now)\n}\n\n/** Resend uses the Svix header names for the same signed-body protocol. */\nexport function verifyResendWebhook(rawBody: string, headers: WebhookHeaders, secret: string, now = Date.now() / 1000): boolean {\n return verifyStandardWebhook(rawBody, headers, secret, 'svix', now)\n}\n\nfunction object(value: unknown): value is Record<string, unknown> {\n return value !== null && typeof value === 'object' && !Array.isArray(value)\n}\n\nfunction parse(provider: string, typeField: string, idField: string, input: Parameters<WebhookProvider['parse']>[0]): WebhookEnvelope[] {\n if (Buffer.byteLength(input.rawBody, 'utf8') > 1_048_576) throw new Error('Webhook exceeds 1 MiB')\n const value: unknown = JSON.parse(input.rawBody)\n if (!object(value) || typeof value[typeField] !== 'string' || typeof value[idField] !== 'string'\n || !/^[a-z][a-z0-9_.-]{0,100}$/.test(value[typeField] as string)\n || !(value[idField] as string).length || (value[idField] as string).length > 256) {\n throw new Error('Provider event requires a bounded event type and stable id')\n }\n return [{ provider, eventType: `${provider}.${value[typeField]}`, providerEventId: value[idField] as string,\n receivedAt: input.now ?? Date.now(), payload: value, headers: {} }]\n}\n\nexport const inkboxWebhookProvider: WebhookProvider = {\n id: 'inkbox',\n verifySignature: ({ rawBody, headers, secret }) => verifyInkboxWebhook(rawBody, headers, secret)\n ? { valid: true } : { valid: false, reason: 'invalid_signature' },\n parse: (input) => parse('inkbox', 'event_type', 'id', input),\n eventCatalog: { namespace: 'inkbox.', closed: false, events: [\n { id: 'inkbox.imessage.received' }, { id: 'inkbox.text.received' }, { id: 'inkbox.message.received' },\n ] },\n}\n\nexport const linqWebhookProvider: WebhookProvider = {\n id: 'linq',\n verifySignature: ({ rawBody, headers, secret }) => verifyLinqWebhook(rawBody, headers, secret)\n ? { valid: true } : { valid: false, reason: 'invalid_signature' },\n parse: (input) => {\n const events = parse('linq', 'event_type', 'event_id', input)\n const id = header(input.headers, 'webhook-id')\n if (!id) throw new Error('Missing signed event identity')\n return events.map((event) => ({ ...event, providerEventId: id }))\n },\n eventCatalog: { namespace: 'linq.', closed: false, events: [{ id: 'linq.message.received' }] },\n}\n\nexport const contiguityWebhookProvider: WebhookProvider = {\n id: 'contiguity',\n verifySignature: ({ rawBody, headers, secret }) => {\n const signature = header(headers, 'contiguity-signature')\n return secret && signature && verifyStripeSignature(rawBody, signature, secret)\n ? { valid: true } : { valid: false, reason: 'invalid_signature' }\n },\n parse: (input) => parse('contiguity', 'type', 'id', input),\n eventCatalog: { namespace: 'contiguity.', closed: false, events: [\n { id: 'contiguity.imessage.incoming' }, { id: 'contiguity.text.incoming.sms' },\n { id: 'contiguity.text.incoming.mms' },\n ] },\n}\n\nexport const linqWhatsappWebhookProvider: WebhookProvider = {\n id: 'linq-whatsapp',\n verifySignature: ({ rawBody, headers, secret }) => verifyLinqWebhook(rawBody, headers, secret)\n ? { valid: true } : { valid: false, reason: 'invalid_signature' },\n parse: (input) => {\n const events = parse('linq-whatsapp', 'type', 'id', input)\n if (events[0]?.providerEventId !== header(input.headers, 'webhook-id')) throw new Error('Signed event identity mismatch')\n return events\n },\n eventCatalog: { namespace: 'linq-whatsapp.', closed: false, events: [{ id: 'linq-whatsapp.message.received' }] },\n}\n\nexport const resendWebhookProvider: WebhookProvider = {\n id: 'resend',\n verifySignature: ({ rawBody, headers, secret }) => verifyResendWebhook(rawBody, headers, secret)\n ? { valid: true } : { valid: false, reason: 'invalid_signature' },\n parse: (input) => {\n if (Buffer.byteLength(input.rawBody, 'utf8') > 1_048_576) throw new Error('Webhook exceeds 1 MiB')\n const value: unknown = JSON.parse(input.rawBody)\n if (!object(value) || value.type !== 'email.received' || !object(value.data)\n || typeof value.data.email_id !== 'string' || !/^[a-f\\d]{8}(?:-[a-f\\d]{4}){3}-[a-f\\d]{12}$/i.test(value.data.email_id)) {\n throw new Error('Resend event requires a received email id')\n }\n const signedId = header(input.headers, 'svix-id')\n if (!signedId) throw new Error('Missing signed event identity')\n return [{ provider: 'resend', eventType: 'resend.email.received', providerEventId: signedId,\n receivedAt: input.now ?? Date.now(), payload: value, headers: {} }]\n },\n eventCatalog: { namespace: 'resend.', closed: true, events: [{ id: 'resend.email.received' }] },\n}\n\n/** Sendblue sends a shared secret verbatim, with no signed timestamp. Deduplicate by message_handle. */\nexport const sendblueWebhookProvider: WebhookProvider = {\n id: 'sendblue',\n verifySignature: ({ headers, secret }) => {\n const supplied = header(headers, 'sb-signing-secret')\n if (!secret || !supplied) return { valid: false, reason: 'invalid_signature' }\n const actual = Buffer.from(supplied), expected = Buffer.from(secret)\n if (actual.length !== expected.length) return { valid: false, reason: 'invalid_signature' }\n return timingSafeEqual(actual, expected)\n ? { valid: true } : { valid: false, reason: 'invalid_signature' }\n },\n parse: (input) => {\n if (Buffer.byteLength(input.rawBody, 'utf8') > 1_048_576) throw new Error('Webhook exceeds 1 MiB')\n const value: unknown = JSON.parse(input.rawBody)\n if (!object(value)) throw new Error('Sendblue webhook requires an object')\n const messageType = typeof value.message_type === 'string' ? value.message_type.toLowerCase() : null\n if (messageType !== 'message' && messageType !== 'group') return []\n if (typeof value.message_handle !== 'string' || !value.message_handle\n || value.message_handle.length > 256 || typeof value.is_outbound !== 'boolean') {\n throw new Error('Sendblue event requires a stable message handle and direction')\n }\n if (value.is_outbound) return []\n return [{ provider: 'sendblue', eventType: 'sendblue.message.received',\n providerEventId: value.message_handle, receivedAt: input.now ?? Date.now(), payload: value, headers: {} }]\n },\n eventCatalog: { namespace: 'sendblue.', closed: true, events: [{ id: 'sendblue.message.received' }] },\n}\n","import { verifyHmacSignature, firstHeader } from '../connectors/webhooks.js'\nimport type { WebhookProvider, WebhookEnvelope } from './router.js'\n\nexport type MetaWhatsappProvider = 'whatsapp' | 'whatsapp-business'\nconst record = (v: unknown): v is Record<string, unknown> => !!v && typeof v === 'object' && !Array.isArray(v)\nconst graphId = (v: unknown): v is string => typeof v === 'string' && /^[0-9]{1,64}$/.test(v)\n\n/** Bind one callback to one WABA and number. The app secret authenticates Meta,\n * not the tenant: app-wide signatures alone cannot establish account ownership. */\nexport function createWhatsappWebhookProvider(options: {\n providerId: MetaWhatsappProvider\n wabaId: string\n phoneNumberId: string\n}): WebhookProvider {\n if (!graphId(options.wabaId) || !graphId(options.phoneNumberId)) throw new Error('Meta webhook requires a WABA and phone number ID')\n return {\n id: options.providerId,\n verifySignature({ rawBody, headers, secret }) {\n const signature = firstHeader(headers, 'x-hub-signature-256')\n return signature && /^sha256=[a-f0-9]{64}$/.test(signature)\n && verifyHmacSignature(rawBody, signature, secret, { algorithm: 'sha256', signaturePrefix: 'sha256=' })\n ? { valid: true } : { valid: false, reason: 'invalid_signature' }\n },\n parse({ rawBody, headers, now }) {\n const body: unknown = JSON.parse(rawBody)\n if (!record(body) || body.object !== 'whatsapp_business_account' || !Array.isArray(body.entry) || body.entry.length > 100) throw new Error('Invalid WhatsApp webhook envelope')\n const events: WebhookEnvelope[] = []\n for (const entry of body.entry) {\n if (!record(entry) || !Array.isArray(entry.changes) || entry.changes.length > 100) throw new Error('Invalid WhatsApp entry')\n if (entry.id !== options.wabaId) continue\n for (const change of entry.changes) {\n if (!record(change) || change.field !== 'messages' || !record(change.value)) continue\n const value = change.value\n if (value.messaging_product !== 'whatsapp' || !record(value.metadata)\n || value.metadata.phone_number_id !== options.phoneNumberId) continue\n if (value.messages === undefined) continue // Delivery status is not an incoming command.\n if (!Array.isArray(value.messages) || value.messages.length > 100) throw new Error('Invalid WhatsApp messages')\n for (const message of value.messages) {\n if (!record(message) || typeof message.id !== 'string' || !message.id || message.id.length > 256) throw new Error('Invalid WhatsApp message identity')\n events.push({ provider: options.providerId, eventType: `${options.providerId}.message.received`,\n providerEventId: `${options.phoneNumberId}:${message.id}`, receivedAt: now ?? Date.now(),\n payload: { wabaId: options.wabaId, phoneNumberId: options.phoneNumberId, message },\n headers: Object.fromEntries(Object.entries(headers).flatMap(([k,v]) => typeof v === 'string' ? [[k.toLowerCase(), v]] : [])),\n })\n }\n }\n }\n return events\n },\n eventCatalog: { namespace: `${options.providerId}.`, closed: true, events: [{ id: `${options.providerId}.message.received` }] },\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6IA,IAAa,kCAAb,cAAqD,+BAAkE,CAAC;;AAGxH,IAAa,oCAAb,cAAuD,iCAAoE,CAAC;;;;;AAyC5H,IAAa,gBAAb,MAA2B;CACzB;CACA;CACA;CACA;CACA;CACA;CACA;CAEA,YAAY,MAA4B;EACtC,KAAK,YAAY,IAAI,IAAI,KAAK,UAAU,KAAK,MAAM,CAAC,EAAE,IAAI,CAAC,CAAC,CAAC;EAC7D,KAAK,UAAU,KAAK;EACpB,KAAK,gBAAgB,KAAK;EAC1B,KAAK,cAAc,8BAA8B;GAC/C,WAAW;GACX,OAAO,KAAK;GACZ,SAAS,KAAK;EAChB,CAAC;EACD,KAAK,mBAAmB,KAAK,oBAAoB;EACjD,KAAK,UAAU,KAAK,WAAW;EAC/B,KAAK,QAAQ,KAAK,OAAO,KAAK;CAChC;;;CAIA,MAAM,OAAO,SAA+D;EAC1E,MAAM,WAAW,KAAK,UAAU,IAAI,QAAQ,UAAU;EACtD,IAAI,CAAC,UACH,OAAO;GAAE,QAAQ;GAAK,MAAM;IAAE,OAAO;IAAoB,UAAU,QAAQ;GAAW;EAAE;EAE1F,MAAM,SAAS,MAAM,KAAK,cAAc,SAAS,IAAI,QAAQ,OAAO;EACpE,IAAI,CAAC,QACH,OAAO;GAAE,QAAQ;GAAK,MAAM;IAAE,OAAO;IAAkB,U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,UAAU,MAAM,SAAS;KAClC,IAAI,CAAC,OAAO,MAAM,KAAK,OAAO,UAAU,cAAc,CAAC,OAAO,OAAO,KAAK,GAAG;KAC7E,MAAM,QAAQ,OAAO;KACrB,IAAI,MAAM,sBAAsB,cAAc,CAAC,OAAO,MAAM,QAAQ,KAC/D,MAAM,SAAS,oBAAoB,QAAQ,eAAe;KAC/D,IAAI,MAAM,aAAa,KAAA,GAAW;KAClC,IAAI,CAAC,MAAM,QAAQ,MAAM,QAAQ,KAAK,MAAM,SAAS,SAAS,KAAK,MAAM,IAAI,MAAM,2BAA2B;KAC9G,KAAK,MAAM,WAAW,MAAM,UAAU;MACpC,IAAI,CAAC,OAAO,OAAO,KAAK,OAAO,QAAQ,OAAO,YAAY,CAAC,QAAQ,MAAM,QAAQ,GAAG,SAAS,KAAK,MAAM,IAAI,MAAM,mCAAmC;MACrJ,OAAO,KAAK;OAAE,UAAU,QAAQ;OAAY,WAAW,GAAG,QAAQ,WAAW;OAC3E,iBAAiB,GAAG,QAAQ,cAAc,GAAG,QAAQ;OAAM,YAAY,OAAO,KAAK,IAAI;OACvF,SAAS;QAAE,QAAQ,QAAQ;QAAQ,eAAe,QAAQ;QAAe;OAAQ;OACjF,SAAS,OAAO,YAAY,OAAO,QAAQ,OAAO,CAAC,CAAC,SAAS,CAAC,GAAE,OAAO,OAAO,MAAM,WAAW,CAAC,CAAC,EAAE,YAAY,GAAG,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC;MAC7H,CAAC;KACH;IACF;GACF;GACA,OAAO;EACT;EACA,cAAc;GAAE,WAAW,GAAG,QAAQ,WAAW;GAAI,QAAQ;GAAM,QAAQ,CAAC,EAAE,IAAI,GAAG,QAAQ,WAAW,mBAAmB,CAAC;EAAE;CAChI;AACF"}
package/dist/worker.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- import { A as IntegrationTriggerSubscription, C as IntegrationOAuthStateStore, D as IntegrationProviderKind, E as IntegrationProvider, F as StartAuthRequest, I as StartAuthResult, M as IssueCapabilityRequest, N as IssuedIntegrationCapability, O as IntegrationSecretStore, P as SecretRef, S as IntegrationOAuthStateOutcome, T as IntegrationPolicyEngine, _ as IntegrationCredentialsRotatedEvent, a as IntegrationActionResult, b as IntegrationHubOptions, c as IntegrationApprovalRequest, d as IntegrationConnection, f as IntegrationConnectionStore, g as IntegrationConnectorTrigger, h as IntegrationConnectorCategory, i as IntegrationActionRequest, j as InvokeWithCapabilityRequest, k as IntegrationTriggerEvent, l as IntegrationCapability, m as IntegrationConnectorAction, n as HttpIntegrationProviderOptions, o as IntegrationActionRisk, p as IntegrationConnector, r as IntegrationActionGuard, s as IntegrationActor, t as CompleteAuthRequest, u as IntegrationCatalogSource, v as IntegrationDataClass, w as IntegrationPolicyDecision, x as IntegrationOAuthState, y as IntegrationGuardContext } from "./core-types-CIez54KT.js";
2
- import { C as createIntegrationAuditEvent, S as createAuditingActionGuard, _ as IntegrationAuditEvent, a as StaticIntegrationPolicyOptions, b as IntegrationAuditSink, c as redactApprovalRequest, d as IntegrationIdempotencyRecord, f as IntegrationIdempotencyStore, g as InMemoryIntegrationAuditStore, h as createDefaultIntegrationActionGuard, i as StaticIntegrationPolicyEngine, l as DefaultIntegrationActionGuard, m as IntegrationRateLimiter, n as IntegrationPolicyEffect, o as buildApprovalRequest, p as IntegrationRateLimitDecision, r as IntegrationPolicyRule, s as createDefaultIntegrationPolicyEngine, t as IntegrationApprovalResolution, u as InMemoryIntegrationIdempotencyStore, v as IntegrationAuditEventType, w as sanitizeAuditConnection, x as IntegrationAuditStore, y as IntegrationAuditFilter } from "./policy-BFvREV-D.js";
1
+ import { A as IntegrationTriggerSubscription, C as IntegrationOAuthStateStore, D as IntegrationProviderKind, E as IntegrationProvider, F as StartAuthRequest, I as StartAuthResult, M as IssueCapabilityRequest, N as IssuedIntegrationCapability, O as IntegrationSecretStore, P as SecretRef, S as IntegrationOAuthStateOutcome, T as IntegrationPolicyEngine, _ as IntegrationCredentialsRotatedEvent, a as IntegrationActionResult, b as IntegrationHubOptions, c as IntegrationApprovalRequest, d as IntegrationConnection, f as IntegrationConnectionStore, g as IntegrationConnectorTrigger, h as IntegrationConnectorCategory, i as IntegrationActionRequest, j as InvokeWithCapabilityRequest, k as IntegrationTriggerEvent, l as IntegrationCapability, m as IntegrationConnectorAction, n as HttpIntegrationProviderOptions, o as IntegrationActionRisk, p as IntegrationConnector, r as IntegrationActionGuard, s as IntegrationActor, t as CompleteAuthRequest, u as IntegrationCatalogSource, v as IntegrationDataClass, w as IntegrationPolicyDecision, x as IntegrationOAuthState, y as IntegrationGuardContext } from "./core-types-DfCMwTYt.js";
2
+ import { C as createIntegrationAuditEvent, S as createAuditingActionGuard, _ as IntegrationAuditEvent, a as StaticIntegrationPolicyOptions, b as IntegrationAuditSink, c as redactApprovalRequest, d as IntegrationIdempotencyRecord, f as IntegrationIdempotencyStore, g as InMemoryIntegrationAuditStore, h as createDefaultIntegrationActionGuard, i as StaticIntegrationPolicyEngine, l as DefaultIntegrationActionGuard, m as IntegrationRateLimiter, n as IntegrationPolicyEffect, o as buildApprovalRequest, p as IntegrationRateLimitDecision, r as IntegrationPolicyRule, s as createDefaultIntegrationPolicyEngine, t as IntegrationApprovalResolution, u as InMemoryIntegrationIdempotencyStore, v as IntegrationAuditEventType, w as sanitizeAuditConnection, x as IntegrationAuditStore, y as IntegrationAuditFilter } from "./policy-C9frNJFH.js";
3
3
  export { CompleteAuthRequest, DefaultIntegrationActionGuard, HttpIntegrationProviderOptions, InMemoryIntegrationAuditStore, InMemoryIntegrationIdempotencyStore, IntegrationActionGuard, IntegrationActionRequest, IntegrationActionResult, IntegrationActionRisk, IntegrationActor, IntegrationApprovalRequest, IntegrationApprovalResolution, IntegrationAuditEvent, IntegrationAuditEventType, IntegrationAuditFilter, IntegrationAuditSink, IntegrationAuditStore, IntegrationCapability, IntegrationCatalogSource, IntegrationConnection, IntegrationConnectionStore, IntegrationConnector, IntegrationConnectorAction, IntegrationConnectorCategory, IntegrationConnectorTrigger, IntegrationCredentialsRotatedEvent, IntegrationDataClass, IntegrationGuardContext, IntegrationHubOptions, IntegrationIdempotencyRecord, IntegrationIdempotencyStore, IntegrationOAuthState, IntegrationOAuthStateOutcome, IntegrationOAuthStateStore, IntegrationPolicyDecision, IntegrationPolicyEffect, IntegrationPolicyEngine, IntegrationPolicyRule, IntegrationProvider, IntegrationProviderKind, IntegrationRateLimitDecision, IntegrationRateLimiter, IntegrationSecretStore, IntegrationTriggerEvent, IntegrationTriggerSubscription, InvokeWithCapabilityRequest, IssueCapabilityRequest, IssuedIntegrationCapability, SecretRef, StartAuthRequest, StartAuthResult, StaticIntegrationPolicyEngine, StaticIntegrationPolicyOptions, buildApprovalRequest, createAuditingActionGuard, createDefaultIntegrationActionGuard, createDefaultIntegrationPolicyEngine, createIntegrationAuditEvent, redactApprovalRequest, sanitizeAuditConnection };
@@ -13,7 +13,16 @@ prove that a specific sender is registered or that a message will deliver.
13
13
 
14
14
  The separate OAuth `whatsapp-business` adapter already takes its sender from
15
15
  connection metadata `phoneNumberId`, with `wabaId` for account-level template
16
- operations. This change does not migrate those connections or add a Hub Lines
17
- channel.
16
+ operations. An OAuth reply must match that saved sender. Both providers expose owned-number
17
+ inventory and signed incoming message normalization for Hub Lines.
18
18
 
19
19
  API contract: [Meta's WhatsApp Cloud API messages](https://www.postman.com/meta/whatsapp-business-platform/folder/13382743-ba8d099d-007e-4b52-b9f2-3cf3c60e4fbc).
20
+
21
+ Incoming webhooks must be bound to the exact WABA and phone number ID. The app
22
+ secret authenticates the raw body; it does not authorize another account using
23
+ the same Meta app. Status callbacks are not agent commands. Text is bounded to
24
+ 4096 characters; media descriptors require explicit handling by the consumer.
25
+
26
+ Meta does not expose an idempotency key for message sends. API-key and OAuth
27
+ replies declare `cas: none`; consumers must retain an attempted effect before
28
+ calling Meta and must not repeat an ambiguous send automatically.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tangle-network/agent-integrations",
3
- "version": "0.67.3",
3
+ "version": "0.69.0",
4
4
  "description": "Vendor-neutral integration contracts and runtime helpers for sandbox and agent apps.",
5
5
  "homepage": "https://github.com/tangle-network/agent-integrations#readme",
6
6
  "repository": {
@@ -1 +0,0 @@
1
- {"version":3,"file":"bundled-manifest-runtime-D1unUvQL.d.ts","names":[],"sources":["../src/connectors/bundled-manifest-runtime.ts"],"mappings":";;;;;;;;;;iBAqCgB,gCAAgC;;iBAmBhC,uCAAuC"}