feat(webhooks): verifyAndParse* API for compressed payloads (CHA-3071)#294
Merged
nijeesh-stream merged 3 commits intoMay 12, 2026
Merged
Conversation
Adds first-class support for gzip-compressed webhook payloads (HTTP
webhooks, SQS, SNS) and exposes the cross-SDK verifyAndParse* / parseSqs
/ parseSns contract that the other Stream SDKs ship in the CHA-3071
rollout.
New module: src/utils/webhook.ts
Primitives:
- gunzipPayload(body) - gzip-magic-byte detection (RFC 1952 1f 8b),
no-op when not compressed
- decodeSqsPayload(body) - strict base64 decode then gunzip-if-magic
- decodeSnsPayload(envelope) - JSON-parse the SNS HTTP notification,
extract Message, run the SQS pipeline;
falls through to a pre-extracted Message
string for backward compatibility
- verifySignature(...) - constant-time HMAC-SHA256 over the
uncompressed body
- parseEvent(payload) - JSON -> typed WHEvent
Composites (return typed WHEvent):
- verifyAndParseWebhook(rawBody, signature, secret)
- parseSqs(messageBody)
- parseSns(notificationBody)
StreamClient#verifyAndParseWebhook / parseSqs / parseSns use the
configured client secret automatically.
Backward compatibility
----------------------
StreamClient#verifyWebhook (boolean) is unchanged.
Unified error handling
----------------------
Every failure path terminates at a single class - InvalidWebhookError -
with one of four canonical messages exported as InvalidWebhookErrorMessages:
signatureMismatch / invalidBase64 / gzipFailed / invalidJson. Customers
only need one catch arm; the message identifies which mode fired.
Tests
-----
__tests__/webhook-compression.test.ts covers plain / gzip /
base64+gzip payloads, signature mismatches, malformed bytes, SNS
envelope unwrapping with and without leading whitespace, and JSON
parsing into a typed WHEvent.
szuperaz
approved these changes
May 12, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds first-class support for gzip-compressed webhook payloads (HTTP webhooks, SQS, SNS) and exposes the stable
verifyAndParse*/parseSqs/parseSnsAPI that the other Stream SDKs ship as part of the CHA-3071 rollout. Linear ticket: CHA-3071.New public API (
src/utils/webhook.ts)Primitives:
gunzipPayload(body)— gzip-magic-byte detection (1f 8b, per RFC 1952), no-op when not compresseddecodeSqsPayload(body)— strict base64 decode thengunzip-if-magicdecodeSnsPayload(notificationBody)— JSON-parse the SNS HTTP notification envelope, extract the innerMessage, then run the SQS pipeline. Falls through to a pre-extractedMessagestring when the input is not a JSON envelopeverifySignature(body, signature, secret)— HMAC-SHA256 over the uncompressed body, with a constant-time comparisonparseEvent(payload)— JSON → typedWHEventComposites (return typed
WHEvent):verifyAndParseWebhook(rawBody, signature, secret)parseSqs(messageBody)parseSns(notificationBody)StreamClient#verifyAndParseWebhook/parseSqs/parseSnsuse the configured client secret automatically.Backwards compatibility
StreamClient#verifyWebhook(boolean) is unchanged.Unified error handling
Every webhook failure path now terminates at a single error class —
InvalidWebhookError— so customers only need onecatcharm. The message identifies which failure mode fired; theInvalidWebhookErrorMessagesconstants are exported for callers that prefer exact-match filtering:signature mismatchinvalid base64 encodinggzip decompression failedinvalid JSON payloadCross-references
Companion SDK PRs that ship the same contract:
Test plan
npx vitest run __tests__/webhook-compression.test.ts— 35 passedyarn lint— cleanyarn build— clean