@stream-io/node-sdk 0.7.56 → 0.7.57
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs.js +221 -1
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.es.mjs +220 -2
- package/dist/index.es.mjs.map +1 -1
- package/dist/src/StreamClient.d.ts +32 -0
- package/dist/src/gen/feeds/FeedApi.d.ts +1 -0
- package/dist/src/gen/feeds/FeedsApi.d.ts +1 -0
- package/dist/src/gen/models/index.d.ts +7 -2
- package/dist/src/utils/webhook.d.ts +98 -0
- package/index.ts +4 -0
- package/package.json +1 -1
- package/src/StreamClient.ts +48 -0
- package/src/gen/feeds/FeedApi.ts +1 -0
- package/src/gen/feeds/FeedsApi.ts +3 -0
- package/src/gen/models/index.ts +9 -2
- package/src/utils/webhook.ts +206 -0
|
@@ -103,4 +103,36 @@ export declare class StreamClient extends CommonApi {
|
|
|
103
103
|
role?: string;
|
|
104
104
|
}, call_cids: string[], exp?: number, iat?: number) => string;
|
|
105
105
|
verifyWebhook: (requestBody: string | Buffer, xSignature: string) => boolean;
|
|
106
|
+
/**
|
|
107
|
+
* Verify and parse an HTTP webhook event.
|
|
108
|
+
*
|
|
109
|
+
* Decompresses `rawBody` when gzipped (detected from the body bytes),
|
|
110
|
+
* verifies the `X-Signature` header against the app's API secret, and
|
|
111
|
+
* returns the parsed typed event. Works whether or not Stream is
|
|
112
|
+
* currently compressing payloads for this app, and stays correct behind
|
|
113
|
+
* middleware that auto-decompresses the request.
|
|
114
|
+
*
|
|
115
|
+
* @param rawBody Raw HTTP request body bytes Stream signed
|
|
116
|
+
* @param signature Value of the `X-Signature` header
|
|
117
|
+
* @throws {InvalidWebhookError} When the signature does not match or
|
|
118
|
+
* the gzip / JSON envelope is malformed.
|
|
119
|
+
*/
|
|
120
|
+
verifyAndParseWebhook: (rawBody: string | Buffer, signature: string) => import("./gen/models").WHEvent;
|
|
121
|
+
/**
|
|
122
|
+
* Parse an SQS firehose event: decodes the message `Body` (base64 +
|
|
123
|
+
* optional gzip) and returns the parsed typed event. No HMAC
|
|
124
|
+
* verification (Stream does not sign SQS bodies).
|
|
125
|
+
*
|
|
126
|
+
* @param messageBody SQS message `Body` string
|
|
127
|
+
* @throws {InvalidWebhookError} When the base64 / gzip envelope is malformed.
|
|
128
|
+
*/
|
|
129
|
+
parseSqs: (messageBody: string) => import("./gen/models").WHEvent;
|
|
130
|
+
/**
|
|
131
|
+
* Parse an SNS-delivered event (unwraps envelope JSON when needed, then
|
|
132
|
+
* same decode path as SQS). No HMAC verification.
|
|
133
|
+
*
|
|
134
|
+
* @param notificationBody Raw SNS POST body or pre-extracted `Message` string
|
|
135
|
+
* @throws {InvalidWebhookError} When the envelope cannot be decoded.
|
|
136
|
+
*/
|
|
137
|
+
parseSns: (notificationBody: string) => import("./gen/models").WHEvent;
|
|
106
138
|
}
|
|
@@ -7,6 +7,7 @@ export declare class FeedApi {
|
|
|
7
7
|
constructor(feedsApi: FeedsApi, group: string, id: string);
|
|
8
8
|
delete(request?: {
|
|
9
9
|
hard_delete?: boolean;
|
|
10
|
+
purge_user_activities?: boolean;
|
|
10
11
|
}): Promise<StreamResponse<DeleteFeedResponse>>;
|
|
11
12
|
getOrCreate(request?: GetOrCreateFeedRequest): Promise<StreamResponse<GetOrCreateFeedResponse>>;
|
|
12
13
|
update(request?: UpdateFeedRequest): Promise<StreamResponse<UpdateFeedResponse>>;
|
|
@@ -160,6 +160,7 @@ export declare class FeedsApi {
|
|
|
160
160
|
feed_group_id: string;
|
|
161
161
|
feed_id: string;
|
|
162
162
|
hard_delete?: boolean;
|
|
163
|
+
purge_user_activities?: boolean;
|
|
163
164
|
}): Promise<StreamResponse<DeleteFeedResponse>>;
|
|
164
165
|
getOrCreateFeed(request: GetOrCreateFeedRequest & {
|
|
165
166
|
feed_group_id: string;
|
|
@@ -734,7 +734,7 @@ export interface ActivitySelectorConfig {
|
|
|
734
734
|
*/
|
|
735
735
|
cutoff_window?: string;
|
|
736
736
|
/**
|
|
737
|
-
* Minimum popularity threshold
|
|
737
|
+
* Minimum popularity threshold. For the 'popular' selector, omit to use the default (5); values below 1 are rejected
|
|
738
738
|
*/
|
|
739
739
|
min_popularity?: number;
|
|
740
740
|
/**
|
|
@@ -761,7 +761,7 @@ export interface ActivitySelectorConfigResponse {
|
|
|
761
761
|
*/
|
|
762
762
|
cutoff_window?: string;
|
|
763
763
|
/**
|
|
764
|
-
* Minimum popularity threshold
|
|
764
|
+
* Minimum popularity threshold. For the 'popular' selector, values below 1 are normalized to the default (5) at read time.
|
|
765
765
|
*/
|
|
766
766
|
min_popularity?: number;
|
|
767
767
|
/**
|
|
@@ -1192,6 +1192,7 @@ export interface AppResponseFields {
|
|
|
1192
1192
|
disable_auth_checks: boolean;
|
|
1193
1193
|
disable_permissions_checks: boolean;
|
|
1194
1194
|
enforce_unique_usernames: string;
|
|
1195
|
+
feed_audit_logs_enabled: boolean;
|
|
1195
1196
|
guest_user_creation_disabled: boolean;
|
|
1196
1197
|
id: number;
|
|
1197
1198
|
image_moderation_enabled: boolean;
|
|
@@ -5492,6 +5493,10 @@ export interface DeleteFeedsBatchRequest {
|
|
|
5492
5493
|
* Whether to permanently delete the feeds instead of soft delete
|
|
5493
5494
|
*/
|
|
5494
5495
|
hard_delete?: boolean;
|
|
5496
|
+
/**
|
|
5497
|
+
* When hard-deleting, also fully delete activities authored by each feed's owner from every other feed those activities were fanned out to. Default false preserves existing fan-out. Requires 'hard_delete' to be true; the request is rejected otherwise. Feeds with no recorded owner (created_by_id is empty) are silently skipped for the purge step — owner-matching against an empty string is a safety guard, not a wildcard.
|
|
5498
|
+
*/
|
|
5499
|
+
purge_user_activities?: boolean;
|
|
5495
5500
|
}
|
|
5496
5501
|
export interface DeleteFeedsBatchResponse {
|
|
5497
5502
|
duration: string;
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import type { WHEvent } from '../gen/models';
|
|
2
|
+
/**
|
|
3
|
+
* Canonical failure-mode messages for {@link InvalidWebhookError}.
|
|
4
|
+
*
|
|
5
|
+
* Callers that prefer exact-match filtering (security logging, retry
|
|
6
|
+
* policy) over substring matches can compare `err.message` to these
|
|
7
|
+
* constants instead of pattern-matching free-form text.
|
|
8
|
+
*/
|
|
9
|
+
export declare const InvalidWebhookErrorMessages: {
|
|
10
|
+
readonly signatureMismatch: "signature mismatch";
|
|
11
|
+
readonly invalidBase64: "invalid base64 encoding";
|
|
12
|
+
readonly gzipFailed: "gzip decompression failed";
|
|
13
|
+
readonly invalidJson: "invalid JSON payload";
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Thrown by {@link verifyAndParseWebhook} when the supplied `x-signature`
|
|
17
|
+
* does not match the HMAC of the uncompressed payload, and by all webhook
|
|
18
|
+
* helpers (including {@link parseSqs} / {@link parseSns}) when a
|
|
19
|
+
* gzip / base64 / JSON envelope is malformed.
|
|
20
|
+
*
|
|
21
|
+
* The message identifies which failure mode fired. See
|
|
22
|
+
* {@link InvalidWebhookErrorMessages} for the canonical strings.
|
|
23
|
+
*/
|
|
24
|
+
export declare class InvalidWebhookError extends Error {
|
|
25
|
+
name: string;
|
|
26
|
+
constructor(message?: string);
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Returns `body` as a `Buffer`, gzip-decompressed when its first two
|
|
30
|
+
* bytes match the gzip magic (`1f 8b`, per RFC 1952). When the body is
|
|
31
|
+
* plain JSON (no compression, or middleware already decompressed), the
|
|
32
|
+
* bytes are returned unchanged.
|
|
33
|
+
*
|
|
34
|
+
* Magic-byte detection (rather than relying on a header) keeps the same
|
|
35
|
+
* handler correct when middleware - Express, Next.js, AWS Lambda -
|
|
36
|
+
* auto-decompresses the request before your code sees it.
|
|
37
|
+
*/
|
|
38
|
+
export declare function gunzipPayload(rawBody: string | Buffer): Buffer;
|
|
39
|
+
/**
|
|
40
|
+
* Reverses the SQS firehose envelope: the message `Body` is
|
|
41
|
+
* base64-decoded, then the result is gzip-decompressed when it begins
|
|
42
|
+
* with the gzip magic. Returns the raw JSON `Buffer` Stream signed.
|
|
43
|
+
*
|
|
44
|
+
* SQS bodies are always base64-encoded so they remain valid UTF-8 over
|
|
45
|
+
* the queue. The same call works whether or not Stream is currently
|
|
46
|
+
* compressing payloads for this app.
|
|
47
|
+
*/
|
|
48
|
+
export declare function decodeSqsPayload(body: string): Buffer;
|
|
49
|
+
/**
|
|
50
|
+
* Reverses an SNS HTTP notification envelope. When `notificationBody`
|
|
51
|
+
* is a JSON envelope (`{"Type":"Notification","Message":"..."}`), the
|
|
52
|
+
* inner `Message` field is extracted and run through the SQS pipeline
|
|
53
|
+
* (base64-decode, then gzip-if-magic). When the input is not a JSON
|
|
54
|
+
* envelope it is treated as the already-extracted `Message` string,
|
|
55
|
+
* so call sites that pre-unwrap continue to work.
|
|
56
|
+
*/
|
|
57
|
+
export declare function decodeSnsPayload(notificationBody: string): Buffer;
|
|
58
|
+
/**
|
|
59
|
+
* Constant-time HMAC-SHA256 verification of `signature` against the
|
|
60
|
+
* digest of `body` using `secret` as the key. The signature is always
|
|
61
|
+
* computed over the **uncompressed** JSON bytes, so callers that
|
|
62
|
+
* decoded a gzipped or base64-wrapped payload must pass the inflated
|
|
63
|
+
* bytes here.
|
|
64
|
+
*
|
|
65
|
+
* The legacy `client.verifyWebhook` helper wraps this function, so
|
|
66
|
+
* callers that have already migrated to `verifyAndParseWebhook`,
|
|
67
|
+
* `parseSqs`, or `parseSns` rarely need to invoke this directly.
|
|
68
|
+
*/
|
|
69
|
+
export declare function verifySignature(body: string | Buffer, signature: string, secret: string): boolean;
|
|
70
|
+
/**
|
|
71
|
+
* Parse a JSON-encoded webhook event into a typed {@link WHEvent}. New
|
|
72
|
+
* event types Stream introduces still parse successfully — the runtime
|
|
73
|
+
* shape is the JSON Stream sent and the `type` field stays preserved.
|
|
74
|
+
*/
|
|
75
|
+
export declare function parseEvent(payload: Buffer | string): WHEvent;
|
|
76
|
+
/**
|
|
77
|
+
* Decompress (when gzipped), verify the HMAC `signature`, and return
|
|
78
|
+
* the parsed {@link WHEvent}.
|
|
79
|
+
*
|
|
80
|
+
* @param rawBody Raw HTTP request body bytes Stream signed
|
|
81
|
+
* @param signature Value of the `X-Signature` header
|
|
82
|
+
* @param secret Your app's API secret
|
|
83
|
+
* @throws {InvalidWebhookError} When the signature does not match or
|
|
84
|
+
* the gzip / base64 / JSON envelope is malformed.
|
|
85
|
+
*/
|
|
86
|
+
export declare function verifyAndParseWebhook(rawBody: string | Buffer, signature: string, secret: string): WHEvent;
|
|
87
|
+
/**
|
|
88
|
+
* Decode the SQS message `Body` (base64, then gzip-if-magic) and return
|
|
89
|
+
* the parsed {@link WHEvent}. Stream does not attach an application-level
|
|
90
|
+
* HMAC to SQS deliveries — use {@link verifyAndParseWebhook} for HTTP
|
|
91
|
+
* webhooks.
|
|
92
|
+
*/
|
|
93
|
+
export declare function parseSqs(messageBody: string): WHEvent;
|
|
94
|
+
/**
|
|
95
|
+
* Decode an SNS notification (unwrap the JSON envelope when needed; same
|
|
96
|
+
* inner format as SQS). No application-level HMAC verification.
|
|
97
|
+
*/
|
|
98
|
+
export declare function parseSns(notificationBody: string): WHEvent;
|
package/index.ts
CHANGED
package/package.json
CHANGED
package/src/StreamClient.ts
CHANGED
|
@@ -1,4 +1,10 @@
|
|
|
1
1
|
import { JWTServerToken, JWTUserToken } from './utils/create-token';
|
|
2
|
+
import {
|
|
3
|
+
InvalidWebhookError,
|
|
4
|
+
parseSns as parseSnsHelper,
|
|
5
|
+
parseSqs as parseSqsHelper,
|
|
6
|
+
verifyAndParseWebhook as verifyAndParseWebhookHelper,
|
|
7
|
+
} from './utils/webhook';
|
|
2
8
|
import { CommonApi } from './gen/common/CommonApi';
|
|
3
9
|
import { StreamVideoClient } from './StreamVideoClient';
|
|
4
10
|
import crypto from 'crypto';
|
|
@@ -253,4 +259,46 @@ export class StreamClient extends CommonApi {
|
|
|
253
259
|
return false;
|
|
254
260
|
}
|
|
255
261
|
};
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* Verify and parse an HTTP webhook event.
|
|
265
|
+
*
|
|
266
|
+
* Decompresses `rawBody` when gzipped (detected from the body bytes),
|
|
267
|
+
* verifies the `X-Signature` header against the app's API secret, and
|
|
268
|
+
* returns the parsed typed event. Works whether or not Stream is
|
|
269
|
+
* currently compressing payloads for this app, and stays correct behind
|
|
270
|
+
* middleware that auto-decompresses the request.
|
|
271
|
+
*
|
|
272
|
+
* @param rawBody Raw HTTP request body bytes Stream signed
|
|
273
|
+
* @param signature Value of the `X-Signature` header
|
|
274
|
+
* @throws {InvalidWebhookError} When the signature does not match or
|
|
275
|
+
* the gzip / JSON envelope is malformed.
|
|
276
|
+
*/
|
|
277
|
+
verifyAndParseWebhook = (rawBody: string | Buffer, signature: string) => {
|
|
278
|
+
if (!this.secret) {
|
|
279
|
+
throw new InvalidWebhookError(
|
|
280
|
+
'cannot verify webhook signature without an API secret on the client',
|
|
281
|
+
);
|
|
282
|
+
}
|
|
283
|
+
return verifyAndParseWebhookHelper(rawBody, signature, this.secret);
|
|
284
|
+
};
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* Parse an SQS firehose event: decodes the message `Body` (base64 +
|
|
288
|
+
* optional gzip) and returns the parsed typed event. No HMAC
|
|
289
|
+
* verification (Stream does not sign SQS bodies).
|
|
290
|
+
*
|
|
291
|
+
* @param messageBody SQS message `Body` string
|
|
292
|
+
* @throws {InvalidWebhookError} When the base64 / gzip envelope is malformed.
|
|
293
|
+
*/
|
|
294
|
+
parseSqs = (messageBody: string) => parseSqsHelper(messageBody);
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* Parse an SNS-delivered event (unwraps envelope JSON when needed, then
|
|
298
|
+
* same decode path as SQS). No HMAC verification.
|
|
299
|
+
*
|
|
300
|
+
* @param notificationBody Raw SNS POST body or pre-extracted `Message` string
|
|
301
|
+
* @throws {InvalidWebhookError} When the envelope cannot be decoded.
|
|
302
|
+
*/
|
|
303
|
+
parseSns = (notificationBody: string) => parseSnsHelper(notificationBody);
|
|
256
304
|
}
|
package/src/gen/feeds/FeedApi.ts
CHANGED
|
@@ -1545,9 +1545,11 @@ export class FeedsApi {
|
|
|
1545
1545
|
feed_group_id: string;
|
|
1546
1546
|
feed_id: string;
|
|
1547
1547
|
hard_delete?: boolean;
|
|
1548
|
+
purge_user_activities?: boolean;
|
|
1548
1549
|
}): Promise<StreamResponse<DeleteFeedResponse>> {
|
|
1549
1550
|
const queryParams = {
|
|
1550
1551
|
hard_delete: request?.hard_delete,
|
|
1552
|
+
purge_user_activities: request?.purge_user_activities,
|
|
1551
1553
|
};
|
|
1552
1554
|
const pathParams = {
|
|
1553
1555
|
feed_group_id: request?.feed_group_id,
|
|
@@ -2275,6 +2277,7 @@ export class FeedsApi {
|
|
|
2275
2277
|
const body = {
|
|
2276
2278
|
feeds: request?.feeds,
|
|
2277
2279
|
hard_delete: request?.hard_delete,
|
|
2280
|
+
purge_user_activities: request?.purge_user_activities,
|
|
2278
2281
|
};
|
|
2279
2282
|
|
|
2280
2283
|
const response = await this.apiClient.sendRequest<
|
package/src/gen/models/index.ts
CHANGED
|
@@ -1041,7 +1041,7 @@ export interface ActivitySelectorConfig {
|
|
|
1041
1041
|
cutoff_window?: string;
|
|
1042
1042
|
|
|
1043
1043
|
/**
|
|
1044
|
-
* Minimum popularity threshold
|
|
1044
|
+
* Minimum popularity threshold. For the 'popular' selector, omit to use the default (5); values below 1 are rejected
|
|
1045
1045
|
*/
|
|
1046
1046
|
min_popularity?: number;
|
|
1047
1047
|
|
|
@@ -1083,7 +1083,7 @@ export interface ActivitySelectorConfigResponse {
|
|
|
1083
1083
|
cutoff_window?: string;
|
|
1084
1084
|
|
|
1085
1085
|
/**
|
|
1086
|
-
* Minimum popularity threshold
|
|
1086
|
+
* Minimum popularity threshold. For the 'popular' selector, values below 1 are normalized to the default (5) at read time.
|
|
1087
1087
|
*/
|
|
1088
1088
|
min_popularity?: number;
|
|
1089
1089
|
|
|
@@ -1656,6 +1656,8 @@ export interface AppResponseFields {
|
|
|
1656
1656
|
|
|
1657
1657
|
enforce_unique_usernames: string;
|
|
1658
1658
|
|
|
1659
|
+
feed_audit_logs_enabled: boolean;
|
|
1660
|
+
|
|
1659
1661
|
guest_user_creation_disabled: boolean;
|
|
1660
1662
|
|
|
1661
1663
|
id: number;
|
|
@@ -7896,6 +7898,11 @@ export interface DeleteFeedsBatchRequest {
|
|
|
7896
7898
|
* Whether to permanently delete the feeds instead of soft delete
|
|
7897
7899
|
*/
|
|
7898
7900
|
hard_delete?: boolean;
|
|
7901
|
+
|
|
7902
|
+
/**
|
|
7903
|
+
* When hard-deleting, also fully delete activities authored by each feed's owner from every other feed those activities were fanned out to. Default false preserves existing fan-out. Requires 'hard_delete' to be true; the request is rejected otherwise. Feeds with no recorded owner (created_by_id is empty) are silently skipped for the purge step — owner-matching against an empty string is a safety guard, not a wildcard.
|
|
7904
|
+
*/
|
|
7905
|
+
purge_user_activities?: boolean;
|
|
7899
7906
|
}
|
|
7900
7907
|
|
|
7901
7908
|
export interface DeleteFeedsBatchResponse {
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
import crypto from 'crypto';
|
|
2
|
+
import zlib from 'zlib';
|
|
3
|
+
import type { WHEvent } from '../gen/models';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Canonical failure-mode messages for {@link InvalidWebhookError}.
|
|
7
|
+
*
|
|
8
|
+
* Callers that prefer exact-match filtering (security logging, retry
|
|
9
|
+
* policy) over substring matches can compare `err.message` to these
|
|
10
|
+
* constants instead of pattern-matching free-form text.
|
|
11
|
+
*/
|
|
12
|
+
export const InvalidWebhookErrorMessages = {
|
|
13
|
+
signatureMismatch: 'signature mismatch',
|
|
14
|
+
invalidBase64: 'invalid base64 encoding',
|
|
15
|
+
gzipFailed: 'gzip decompression failed',
|
|
16
|
+
invalidJson: 'invalid JSON payload',
|
|
17
|
+
} as const;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Thrown by {@link verifyAndParseWebhook} when the supplied `x-signature`
|
|
21
|
+
* does not match the HMAC of the uncompressed payload, and by all webhook
|
|
22
|
+
* helpers (including {@link parseSqs} / {@link parseSns}) when a
|
|
23
|
+
* gzip / base64 / JSON envelope is malformed.
|
|
24
|
+
*
|
|
25
|
+
* The message identifies which failure mode fired. See
|
|
26
|
+
* {@link InvalidWebhookErrorMessages} for the canonical strings.
|
|
27
|
+
*/
|
|
28
|
+
export class InvalidWebhookError extends Error {
|
|
29
|
+
public name = 'InvalidWebhookError';
|
|
30
|
+
|
|
31
|
+
constructor(message: string = InvalidWebhookErrorMessages.signatureMismatch) {
|
|
32
|
+
super(message);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const GZIP_MAGIC = Buffer.from([0x1f, 0x8b]);
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Returns `body` as a `Buffer`, gzip-decompressed when its first two
|
|
40
|
+
* bytes match the gzip magic (`1f 8b`, per RFC 1952). When the body is
|
|
41
|
+
* plain JSON (no compression, or middleware already decompressed), the
|
|
42
|
+
* bytes are returned unchanged.
|
|
43
|
+
*
|
|
44
|
+
* Magic-byte detection (rather than relying on a header) keeps the same
|
|
45
|
+
* handler correct when middleware - Express, Next.js, AWS Lambda -
|
|
46
|
+
* auto-decompresses the request before your code sees it.
|
|
47
|
+
*/
|
|
48
|
+
export function gunzipPayload(rawBody: string | Buffer): Buffer {
|
|
49
|
+
const body = Buffer.isBuffer(rawBody) ? rawBody : Buffer.from(rawBody);
|
|
50
|
+
if (body.length >= 2 && body.subarray(0, 2).equals(GZIP_MAGIC)) {
|
|
51
|
+
try {
|
|
52
|
+
return zlib.gunzipSync(body);
|
|
53
|
+
} catch {
|
|
54
|
+
throw new InvalidWebhookError(InvalidWebhookErrorMessages.gzipFailed);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return body;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Reverses the SQS firehose envelope: the message `Body` is
|
|
62
|
+
* base64-decoded, then the result is gzip-decompressed when it begins
|
|
63
|
+
* with the gzip magic. Returns the raw JSON `Buffer` Stream signed.
|
|
64
|
+
*
|
|
65
|
+
* SQS bodies are always base64-encoded so they remain valid UTF-8 over
|
|
66
|
+
* the queue. The same call works whether or not Stream is currently
|
|
67
|
+
* compressing payloads for this app.
|
|
68
|
+
*/
|
|
69
|
+
export function decodeSqsPayload(body: string): Buffer {
|
|
70
|
+
// Reject anything that isn't canonical base64 up front. Node's base64
|
|
71
|
+
// decoder is permissive (silently strips characters outside the
|
|
72
|
+
// alphabet, accepts both standard and URL-safe variants), so we have
|
|
73
|
+
// to be strict here to avoid silently corrupting the body before the
|
|
74
|
+
// signature check runs.
|
|
75
|
+
if (!/^[A-Za-z0-9+/]*={0,2}$/.test(body) || body.length % 4 !== 0) {
|
|
76
|
+
throw new InvalidWebhookError(InvalidWebhookErrorMessages.invalidBase64);
|
|
77
|
+
}
|
|
78
|
+
const decoded = Buffer.from(body, 'base64');
|
|
79
|
+
if (decoded.toString('base64').length !== body.length) {
|
|
80
|
+
throw new InvalidWebhookError(InvalidWebhookErrorMessages.invalidBase64);
|
|
81
|
+
}
|
|
82
|
+
return gunzipPayload(decoded);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Reverses an SNS HTTP notification envelope. When `notificationBody`
|
|
87
|
+
* is a JSON envelope (`{"Type":"Notification","Message":"..."}`), the
|
|
88
|
+
* inner `Message` field is extracted and run through the SQS pipeline
|
|
89
|
+
* (base64-decode, then gzip-if-magic). When the input is not a JSON
|
|
90
|
+
* envelope it is treated as the already-extracted `Message` string,
|
|
91
|
+
* so call sites that pre-unwrap continue to work.
|
|
92
|
+
*/
|
|
93
|
+
export function decodeSnsPayload(notificationBody: string): Buffer {
|
|
94
|
+
const inner = extractSnsMessage(notificationBody);
|
|
95
|
+
return decodeSqsPayload(inner ?? notificationBody);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function extractSnsMessage(notificationBody: string): string | null {
|
|
99
|
+
const trimmed = notificationBody.replace(/^[\s\uFEFF]+/, '');
|
|
100
|
+
if (!trimmed.startsWith('{')) {
|
|
101
|
+
return null;
|
|
102
|
+
}
|
|
103
|
+
let parsed: unknown;
|
|
104
|
+
try {
|
|
105
|
+
parsed = JSON.parse(trimmed);
|
|
106
|
+
} catch {
|
|
107
|
+
return null;
|
|
108
|
+
}
|
|
109
|
+
if (
|
|
110
|
+
parsed === null ||
|
|
111
|
+
typeof parsed !== 'object' ||
|
|
112
|
+
Array.isArray(parsed) ||
|
|
113
|
+
typeof (parsed as { Message?: unknown }).Message !== 'string'
|
|
114
|
+
) {
|
|
115
|
+
return null;
|
|
116
|
+
}
|
|
117
|
+
return (parsed as { Message: string }).Message;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Constant-time HMAC-SHA256 verification of `signature` against the
|
|
122
|
+
* digest of `body` using `secret` as the key. The signature is always
|
|
123
|
+
* computed over the **uncompressed** JSON bytes, so callers that
|
|
124
|
+
* decoded a gzipped or base64-wrapped payload must pass the inflated
|
|
125
|
+
* bytes here.
|
|
126
|
+
*
|
|
127
|
+
* The legacy `client.verifyWebhook` helper wraps this function, so
|
|
128
|
+
* callers that have already migrated to `verifyAndParseWebhook`,
|
|
129
|
+
* `parseSqs`, or `parseSns` rarely need to invoke this directly.
|
|
130
|
+
*/
|
|
131
|
+
export function verifySignature(
|
|
132
|
+
body: string | Buffer,
|
|
133
|
+
signature: string,
|
|
134
|
+
secret: string,
|
|
135
|
+
): boolean {
|
|
136
|
+
const key = Buffer.from(secret, 'utf8');
|
|
137
|
+
const hash = crypto.createHmac('sha256', key).update(body).digest('hex');
|
|
138
|
+
try {
|
|
139
|
+
return crypto.timingSafeEqual(Buffer.from(hash), Buffer.from(signature));
|
|
140
|
+
} catch {
|
|
141
|
+
return false;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Parse a JSON-encoded webhook event into a typed {@link WHEvent}. New
|
|
147
|
+
* event types Stream introduces still parse successfully — the runtime
|
|
148
|
+
* shape is the JSON Stream sent and the `type` field stays preserved.
|
|
149
|
+
*/
|
|
150
|
+
export function parseEvent(payload: Buffer | string): WHEvent {
|
|
151
|
+
const text = Buffer.isBuffer(payload) ? payload.toString('utf8') : payload;
|
|
152
|
+
try {
|
|
153
|
+
return JSON.parse(text) as WHEvent;
|
|
154
|
+
} catch {
|
|
155
|
+
throw new InvalidWebhookError(InvalidWebhookErrorMessages.invalidJson);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function verifyAndParse(
|
|
160
|
+
payload: Buffer,
|
|
161
|
+
signature: string,
|
|
162
|
+
secret: string,
|
|
163
|
+
): WHEvent {
|
|
164
|
+
if (!verifySignature(payload, signature, secret)) {
|
|
165
|
+
throw new InvalidWebhookError(
|
|
166
|
+
InvalidWebhookErrorMessages.signatureMismatch,
|
|
167
|
+
);
|
|
168
|
+
}
|
|
169
|
+
return parseEvent(payload);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Decompress (when gzipped), verify the HMAC `signature`, and return
|
|
174
|
+
* the parsed {@link WHEvent}.
|
|
175
|
+
*
|
|
176
|
+
* @param rawBody Raw HTTP request body bytes Stream signed
|
|
177
|
+
* @param signature Value of the `X-Signature` header
|
|
178
|
+
* @param secret Your app's API secret
|
|
179
|
+
* @throws {InvalidWebhookError} When the signature does not match or
|
|
180
|
+
* the gzip / base64 / JSON envelope is malformed.
|
|
181
|
+
*/
|
|
182
|
+
export function verifyAndParseWebhook(
|
|
183
|
+
rawBody: string | Buffer,
|
|
184
|
+
signature: string,
|
|
185
|
+
secret: string,
|
|
186
|
+
): WHEvent {
|
|
187
|
+
return verifyAndParse(gunzipPayload(rawBody), signature, secret);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Decode the SQS message `Body` (base64, then gzip-if-magic) and return
|
|
192
|
+
* the parsed {@link WHEvent}. Stream does not attach an application-level
|
|
193
|
+
* HMAC to SQS deliveries — use {@link verifyAndParseWebhook} for HTTP
|
|
194
|
+
* webhooks.
|
|
195
|
+
*/
|
|
196
|
+
export function parseSqs(messageBody: string): WHEvent {
|
|
197
|
+
return parseEvent(decodeSqsPayload(messageBody));
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Decode an SNS notification (unwrap the JSON envelope when needed; same
|
|
202
|
+
* inner format as SQS). No application-level HMAC verification.
|
|
203
|
+
*/
|
|
204
|
+
export function parseSns(notificationBody: string): WHEvent {
|
|
205
|
+
return parseEvent(decodeSnsPayload(notificationBody));
|
|
206
|
+
}
|