@stream-io/node-sdk 0.7.56 → 0.7.58
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 +263 -1
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.es.mjs +262 -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 +4 -1
- package/dist/src/gen/models/index.d.ts +49 -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 +66 -0
- package/src/gen/model-decoders/decoders.ts +14 -0
- package/src/gen/models/index.ts +63 -2
- package/src/utils/webhook.ts +206 -0
package/dist/index.d.ts
CHANGED
package/dist/index.es.mjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import jwt from 'jsonwebtoken';
|
|
2
2
|
import crypto from 'crypto';
|
|
3
|
+
import zlib from 'zlib';
|
|
3
4
|
import { v4 } from 'uuid';
|
|
4
5
|
|
|
5
6
|
function JWTUserToken(apiSecret, payload) {
|
|
@@ -24,6 +25,184 @@ function JWTServerToken(apiSecret, jwtOptions = {}) {
|
|
|
24
25
|
return jwt.sign(payload, apiSecret, opts);
|
|
25
26
|
}
|
|
26
27
|
|
|
28
|
+
/**
|
|
29
|
+
* Canonical failure-mode messages for {@link InvalidWebhookError}.
|
|
30
|
+
*
|
|
31
|
+
* Callers that prefer exact-match filtering (security logging, retry
|
|
32
|
+
* policy) over substring matches can compare `err.message` to these
|
|
33
|
+
* constants instead of pattern-matching free-form text.
|
|
34
|
+
*/
|
|
35
|
+
const InvalidWebhookErrorMessages = {
|
|
36
|
+
signatureMismatch: 'signature mismatch',
|
|
37
|
+
invalidBase64: 'invalid base64 encoding',
|
|
38
|
+
gzipFailed: 'gzip decompression failed',
|
|
39
|
+
invalidJson: 'invalid JSON payload',
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* Thrown by {@link verifyAndParseWebhook} when the supplied `x-signature`
|
|
43
|
+
* does not match the HMAC of the uncompressed payload, and by all webhook
|
|
44
|
+
* helpers (including {@link parseSqs} / {@link parseSns}) when a
|
|
45
|
+
* gzip / base64 / JSON envelope is malformed.
|
|
46
|
+
*
|
|
47
|
+
* The message identifies which failure mode fired. See
|
|
48
|
+
* {@link InvalidWebhookErrorMessages} for the canonical strings.
|
|
49
|
+
*/
|
|
50
|
+
class InvalidWebhookError extends Error {
|
|
51
|
+
constructor(message = InvalidWebhookErrorMessages.signatureMismatch) {
|
|
52
|
+
super(message);
|
|
53
|
+
this.name = 'InvalidWebhookError';
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
const GZIP_MAGIC = Buffer.from([0x1f, 0x8b]);
|
|
57
|
+
/**
|
|
58
|
+
* Returns `body` as a `Buffer`, gzip-decompressed when its first two
|
|
59
|
+
* bytes match the gzip magic (`1f 8b`, per RFC 1952). When the body is
|
|
60
|
+
* plain JSON (no compression, or middleware already decompressed), the
|
|
61
|
+
* bytes are returned unchanged.
|
|
62
|
+
*
|
|
63
|
+
* Magic-byte detection (rather than relying on a header) keeps the same
|
|
64
|
+
* handler correct when middleware - Express, Next.js, AWS Lambda -
|
|
65
|
+
* auto-decompresses the request before your code sees it.
|
|
66
|
+
*/
|
|
67
|
+
function gunzipPayload(rawBody) {
|
|
68
|
+
const body = Buffer.isBuffer(rawBody) ? rawBody : Buffer.from(rawBody);
|
|
69
|
+
if (body.length >= 2 && body.subarray(0, 2).equals(GZIP_MAGIC)) {
|
|
70
|
+
try {
|
|
71
|
+
return zlib.gunzipSync(body);
|
|
72
|
+
}
|
|
73
|
+
catch {
|
|
74
|
+
throw new InvalidWebhookError(InvalidWebhookErrorMessages.gzipFailed);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return body;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Reverses the SQS firehose envelope: the message `Body` is
|
|
81
|
+
* base64-decoded, then the result is gzip-decompressed when it begins
|
|
82
|
+
* with the gzip magic. Returns the raw JSON `Buffer` Stream signed.
|
|
83
|
+
*
|
|
84
|
+
* SQS bodies are always base64-encoded so they remain valid UTF-8 over
|
|
85
|
+
* the queue. The same call works whether or not Stream is currently
|
|
86
|
+
* compressing payloads for this app.
|
|
87
|
+
*/
|
|
88
|
+
function decodeSqsPayload(body) {
|
|
89
|
+
// Reject anything that isn't canonical base64 up front. Node's base64
|
|
90
|
+
// decoder is permissive (silently strips characters outside the
|
|
91
|
+
// alphabet, accepts both standard and URL-safe variants), so we have
|
|
92
|
+
// to be strict here to avoid silently corrupting the body before the
|
|
93
|
+
// signature check runs.
|
|
94
|
+
if (!/^[A-Za-z0-9+/]*={0,2}$/.test(body) || body.length % 4 !== 0) {
|
|
95
|
+
throw new InvalidWebhookError(InvalidWebhookErrorMessages.invalidBase64);
|
|
96
|
+
}
|
|
97
|
+
const decoded = Buffer.from(body, 'base64');
|
|
98
|
+
if (decoded.toString('base64').length !== body.length) {
|
|
99
|
+
throw new InvalidWebhookError(InvalidWebhookErrorMessages.invalidBase64);
|
|
100
|
+
}
|
|
101
|
+
return gunzipPayload(decoded);
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Reverses an SNS HTTP notification envelope. When `notificationBody`
|
|
105
|
+
* is a JSON envelope (`{"Type":"Notification","Message":"..."}`), the
|
|
106
|
+
* inner `Message` field is extracted and run through the SQS pipeline
|
|
107
|
+
* (base64-decode, then gzip-if-magic). When the input is not a JSON
|
|
108
|
+
* envelope it is treated as the already-extracted `Message` string,
|
|
109
|
+
* so call sites that pre-unwrap continue to work.
|
|
110
|
+
*/
|
|
111
|
+
function decodeSnsPayload(notificationBody) {
|
|
112
|
+
const inner = extractSnsMessage(notificationBody);
|
|
113
|
+
return decodeSqsPayload(inner ?? notificationBody);
|
|
114
|
+
}
|
|
115
|
+
function extractSnsMessage(notificationBody) {
|
|
116
|
+
const trimmed = notificationBody.replace(/^[\s\uFEFF]+/, '');
|
|
117
|
+
if (!trimmed.startsWith('{')) {
|
|
118
|
+
return null;
|
|
119
|
+
}
|
|
120
|
+
let parsed;
|
|
121
|
+
try {
|
|
122
|
+
parsed = JSON.parse(trimmed);
|
|
123
|
+
}
|
|
124
|
+
catch {
|
|
125
|
+
return null;
|
|
126
|
+
}
|
|
127
|
+
if (parsed === null ||
|
|
128
|
+
typeof parsed !== 'object' ||
|
|
129
|
+
Array.isArray(parsed) ||
|
|
130
|
+
typeof parsed.Message !== 'string') {
|
|
131
|
+
return null;
|
|
132
|
+
}
|
|
133
|
+
return parsed.Message;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Constant-time HMAC-SHA256 verification of `signature` against the
|
|
137
|
+
* digest of `body` using `secret` as the key. The signature is always
|
|
138
|
+
* computed over the **uncompressed** JSON bytes, so callers that
|
|
139
|
+
* decoded a gzipped or base64-wrapped payload must pass the inflated
|
|
140
|
+
* bytes here.
|
|
141
|
+
*
|
|
142
|
+
* The legacy `client.verifyWebhook` helper wraps this function, so
|
|
143
|
+
* callers that have already migrated to `verifyAndParseWebhook`,
|
|
144
|
+
* `parseSqs`, or `parseSns` rarely need to invoke this directly.
|
|
145
|
+
*/
|
|
146
|
+
function verifySignature(body, signature, secret) {
|
|
147
|
+
const key = Buffer.from(secret, 'utf8');
|
|
148
|
+
const hash = crypto.createHmac('sha256', key).update(body).digest('hex');
|
|
149
|
+
try {
|
|
150
|
+
return crypto.timingSafeEqual(Buffer.from(hash), Buffer.from(signature));
|
|
151
|
+
}
|
|
152
|
+
catch {
|
|
153
|
+
return false;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Parse a JSON-encoded webhook event into a typed {@link WHEvent}. New
|
|
158
|
+
* event types Stream introduces still parse successfully — the runtime
|
|
159
|
+
* shape is the JSON Stream sent and the `type` field stays preserved.
|
|
160
|
+
*/
|
|
161
|
+
function parseEvent(payload) {
|
|
162
|
+
const text = Buffer.isBuffer(payload) ? payload.toString('utf8') : payload;
|
|
163
|
+
try {
|
|
164
|
+
return JSON.parse(text);
|
|
165
|
+
}
|
|
166
|
+
catch {
|
|
167
|
+
throw new InvalidWebhookError(InvalidWebhookErrorMessages.invalidJson);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
function verifyAndParse(payload, signature, secret) {
|
|
171
|
+
if (!verifySignature(payload, signature, secret)) {
|
|
172
|
+
throw new InvalidWebhookError(InvalidWebhookErrorMessages.signatureMismatch);
|
|
173
|
+
}
|
|
174
|
+
return parseEvent(payload);
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Decompress (when gzipped), verify the HMAC `signature`, and return
|
|
178
|
+
* the parsed {@link WHEvent}.
|
|
179
|
+
*
|
|
180
|
+
* @param rawBody Raw HTTP request body bytes Stream signed
|
|
181
|
+
* @param signature Value of the `X-Signature` header
|
|
182
|
+
* @param secret Your app's API secret
|
|
183
|
+
* @throws {InvalidWebhookError} When the signature does not match or
|
|
184
|
+
* the gzip / base64 / JSON envelope is malformed.
|
|
185
|
+
*/
|
|
186
|
+
function verifyAndParseWebhook(rawBody, signature, secret) {
|
|
187
|
+
return verifyAndParse(gunzipPayload(rawBody), signature, secret);
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Decode the SQS message `Body` (base64, then gzip-if-magic) and return
|
|
191
|
+
* the parsed {@link WHEvent}. Stream does not attach an application-level
|
|
192
|
+
* HMAC to SQS deliveries — use {@link verifyAndParseWebhook} for HTTP
|
|
193
|
+
* webhooks.
|
|
194
|
+
*/
|
|
195
|
+
function parseSqs(messageBody) {
|
|
196
|
+
return parseEvent(decodeSqsPayload(messageBody));
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Decode an SNS notification (unwrap the JSON envelope when needed; same
|
|
200
|
+
* inner format as SQS). No application-level HMAC verification.
|
|
201
|
+
*/
|
|
202
|
+
function parseSns(notificationBody) {
|
|
203
|
+
return parseEvent(decodeSnsPayload(notificationBody));
|
|
204
|
+
}
|
|
205
|
+
|
|
27
206
|
const decoders = {};
|
|
28
207
|
const decodeDatetimeType = (input) => typeof input === 'number'
|
|
29
208
|
? new Date(Math.floor(input / 1000000))
|
|
@@ -2059,6 +2238,18 @@ decoders.GetOrCreateFeedViewResponse = (input) => {
|
|
|
2059
2238
|
};
|
|
2060
2239
|
return decode(typeMappings, input);
|
|
2061
2240
|
};
|
|
2241
|
+
decoders.GetOrCreateFollowResponse = (input) => {
|
|
2242
|
+
const typeMappings = {
|
|
2243
|
+
follow: { type: 'FollowResponse', isSingle: true },
|
|
2244
|
+
};
|
|
2245
|
+
return decode(typeMappings, input);
|
|
2246
|
+
};
|
|
2247
|
+
decoders.GetOrCreateUnfollowResponse = (input) => {
|
|
2248
|
+
const typeMappings = {
|
|
2249
|
+
follow: { type: 'FollowResponse', isSingle: true },
|
|
2250
|
+
};
|
|
2251
|
+
return decode(typeMappings, input);
|
|
2252
|
+
};
|
|
2062
2253
|
decoders.GetPushTemplatesResponse = (input) => {
|
|
2063
2254
|
const typeMappings = {
|
|
2064
2255
|
templates: { type: 'PushTemplateResponse', isSingle: false },
|
|
@@ -7948,7 +8139,7 @@ class ApiClient {
|
|
|
7948
8139
|
const headers = {
|
|
7949
8140
|
Authorization: this.apiConfig.token,
|
|
7950
8141
|
'stream-auth-type': 'jwt',
|
|
7951
|
-
'X-Stream-Client': 'stream-node-' + "0.7.
|
|
8142
|
+
'X-Stream-Client': 'stream-node-' + "0.7.58",
|
|
7952
8143
|
'Accept-Encoding': 'gzip',
|
|
7953
8144
|
'x-client-request-id': clientRequestId,
|
|
7954
8145
|
};
|
|
@@ -8733,6 +8924,7 @@ class FeedsApi {
|
|
|
8733
8924
|
async deleteFeed(request) {
|
|
8734
8925
|
const queryParams = {
|
|
8735
8926
|
hard_delete: request?.hard_delete,
|
|
8927
|
+
purge_user_activities: request?.purge_user_activities,
|
|
8736
8928
|
};
|
|
8737
8929
|
const pathParams = {
|
|
8738
8930
|
feed_group_id: request?.feed_group_id,
|
|
@@ -9088,6 +9280,7 @@ class FeedsApi {
|
|
|
9088
9280
|
const body = {
|
|
9089
9281
|
feeds: request?.feeds,
|
|
9090
9282
|
hard_delete: request?.hard_delete,
|
|
9283
|
+
purge_user_activities: request?.purge_user_activities,
|
|
9091
9284
|
};
|
|
9092
9285
|
const response = await this.apiClient.sendRequest('POST', '/api/v2/feeds/feeds/delete', undefined, undefined, body, 'application/json');
|
|
9093
9286
|
decoders.DeleteFeedsBatchResponse?.(response.body);
|
|
@@ -9218,6 +9411,24 @@ class FeedsApi {
|
|
|
9218
9411
|
decoders.RejectFollowResponse?.(response.body);
|
|
9219
9412
|
return { ...response.body, metadata: response.metadata };
|
|
9220
9413
|
}
|
|
9414
|
+
async getOrCreateFollow(request) {
|
|
9415
|
+
const body = {
|
|
9416
|
+
source: request?.source,
|
|
9417
|
+
target: request?.target,
|
|
9418
|
+
activity_copy_limit: request?.activity_copy_limit,
|
|
9419
|
+
copy_custom_to_notification: request?.copy_custom_to_notification,
|
|
9420
|
+
create_notification_activity: request?.create_notification_activity,
|
|
9421
|
+
create_users: request?.create_users,
|
|
9422
|
+
enrich_own_fields: request?.enrich_own_fields,
|
|
9423
|
+
push_preference: request?.push_preference,
|
|
9424
|
+
skip_push: request?.skip_push,
|
|
9425
|
+
status: request?.status,
|
|
9426
|
+
custom: request?.custom,
|
|
9427
|
+
};
|
|
9428
|
+
const response = await this.apiClient.sendRequest('POST', '/api/v2/feeds/follows/upsert', undefined, undefined, body, 'application/json');
|
|
9429
|
+
decoders.GetOrCreateFollowResponse?.(response.body);
|
|
9430
|
+
return { ...response.body, metadata: response.metadata };
|
|
9431
|
+
}
|
|
9221
9432
|
async unfollow(request) {
|
|
9222
9433
|
const queryParams = {
|
|
9223
9434
|
delete_notification_activity: request?.delete_notification_activity,
|
|
@@ -9321,6 +9532,18 @@ class FeedsApi {
|
|
|
9321
9532
|
decoders.UnfollowBatchResponse?.(response.body);
|
|
9322
9533
|
return { ...response.body, metadata: response.metadata };
|
|
9323
9534
|
}
|
|
9535
|
+
async getOrCreateUnfollow(request) {
|
|
9536
|
+
const body = {
|
|
9537
|
+
source: request?.source,
|
|
9538
|
+
target: request?.target,
|
|
9539
|
+
delete_notification_activity: request?.delete_notification_activity,
|
|
9540
|
+
enrich_own_fields: request?.enrich_own_fields,
|
|
9541
|
+
keep_history: request?.keep_history,
|
|
9542
|
+
};
|
|
9543
|
+
const response = await this.apiClient.sendRequest('POST', '/api/v2/feeds/unfollow/upsert', undefined, undefined, body, 'application/json');
|
|
9544
|
+
decoders.GetOrCreateUnfollowResponse?.(response.body);
|
|
9545
|
+
return { ...response.body, metadata: response.metadata };
|
|
9546
|
+
}
|
|
9324
9547
|
async deleteFeedUserData(request) {
|
|
9325
9548
|
const pathParams = {
|
|
9326
9549
|
user_id: request?.user_id,
|
|
@@ -9623,6 +9846,43 @@ class StreamClient extends CommonApi {
|
|
|
9623
9846
|
return false;
|
|
9624
9847
|
}
|
|
9625
9848
|
};
|
|
9849
|
+
/**
|
|
9850
|
+
* Verify and parse an HTTP webhook event.
|
|
9851
|
+
*
|
|
9852
|
+
* Decompresses `rawBody` when gzipped (detected from the body bytes),
|
|
9853
|
+
* verifies the `X-Signature` header against the app's API secret, and
|
|
9854
|
+
* returns the parsed typed event. Works whether or not Stream is
|
|
9855
|
+
* currently compressing payloads for this app, and stays correct behind
|
|
9856
|
+
* middleware that auto-decompresses the request.
|
|
9857
|
+
*
|
|
9858
|
+
* @param rawBody Raw HTTP request body bytes Stream signed
|
|
9859
|
+
* @param signature Value of the `X-Signature` header
|
|
9860
|
+
* @throws {InvalidWebhookError} When the signature does not match or
|
|
9861
|
+
* the gzip / JSON envelope is malformed.
|
|
9862
|
+
*/
|
|
9863
|
+
this.verifyAndParseWebhook = (rawBody, signature) => {
|
|
9864
|
+
if (!this.secret) {
|
|
9865
|
+
throw new InvalidWebhookError('cannot verify webhook signature without an API secret on the client');
|
|
9866
|
+
}
|
|
9867
|
+
return verifyAndParseWebhook(rawBody, signature, this.secret);
|
|
9868
|
+
};
|
|
9869
|
+
/**
|
|
9870
|
+
* Parse an SQS firehose event: decodes the message `Body` (base64 +
|
|
9871
|
+
* optional gzip) and returns the parsed typed event. No HMAC
|
|
9872
|
+
* verification (Stream does not sign SQS bodies).
|
|
9873
|
+
*
|
|
9874
|
+
* @param messageBody SQS message `Body` string
|
|
9875
|
+
* @throws {InvalidWebhookError} When the base64 / gzip envelope is malformed.
|
|
9876
|
+
*/
|
|
9877
|
+
this.parseSqs = (messageBody) => parseSqs(messageBody);
|
|
9878
|
+
/**
|
|
9879
|
+
* Parse an SNS-delivered event (unwraps envelope JSON when needed, then
|
|
9880
|
+
* same decode path as SQS). No HMAC verification.
|
|
9881
|
+
*
|
|
9882
|
+
* @param notificationBody Raw SNS POST body or pre-extracted `Message` string
|
|
9883
|
+
* @throws {InvalidWebhookError} When the envelope cannot be decoded.
|
|
9884
|
+
*/
|
|
9885
|
+
this.parseSns = (notificationBody) => parseSns(notificationBody);
|
|
9626
9886
|
this.video = new StreamVideoClient({
|
|
9627
9887
|
streamClient: this,
|
|
9628
9888
|
apiClient: videoApiClient,
|
|
@@ -9743,5 +10003,5 @@ const OwnCapability = {
|
|
|
9743
10003
|
UPDATE_CALL_SETTINGS: 'update-call-settings',
|
|
9744
10004
|
};
|
|
9745
10005
|
|
|
9746
|
-
export { ChannelOwnCapability, FeedOwnCapability, OwnCapability, StreamCall, StreamChannel, StreamChatClient, StreamClient, StreamFeed, StreamFeedsClient, StreamVideoClient };
|
|
10006
|
+
export { ChannelOwnCapability, FeedOwnCapability, InvalidWebhookError, InvalidWebhookErrorMessages, OwnCapability, StreamCall, StreamChannel, StreamChatClient, StreamClient, StreamFeed, StreamFeedsClient, StreamVideoClient };
|
|
9747
10007
|
//# sourceMappingURL=index.es.mjs.map
|