stream-chat 9.50.3 → 10.0.0-rc.2
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/cjs/index.browser.js +19798 -14472
- package/dist/cjs/index.browser.js.map +4 -4
- package/dist/cjs/index.node.js +19817 -14482
- package/dist/cjs/index.node.js.map +4 -4
- package/dist/esm/index.mjs +19795 -14469
- package/dist/esm/index.mjs.map +4 -4
- package/dist/types/ChannelManager.d.ts +221 -0
- package/dist/types/EventHandlerPipeline.d.ts +104 -0
- package/dist/types/api-client.d.ts +26 -0
- package/dist/types/campaign.d.ts +0 -44
- package/dist/types/channel.d.ts +454 -479
- package/dist/types/channel_batch_updater.d.ts +0 -93
- package/dist/types/channel_state.d.ts +53 -201
- package/dist/types/client.d.ts +451 -1920
- package/dist/types/client_state.d.ts +4 -4
- package/dist/types/configuration/InstanceConfigurationService.d.ts +26 -0
- package/dist/types/configuration/index.d.ts +1 -0
- package/dist/types/configuration/types.d.ts +53 -0
- package/dist/types/connection.d.ts +49 -41
- package/dist/types/connection_fallback.d.ts +6 -4
- package/dist/types/constants.d.ts +0 -4
- package/dist/types/custom_types.d.ts +2 -0
- package/dist/types/entityStore/EntityStore.d.ts +111 -0
- package/dist/types/entityStore/StoreBackedItemIndex.d.ts +60 -0
- package/dist/types/entityStore/applyReactionLocally.d.ts +26 -0
- package/dist/types/entityStore/index.d.ts +2 -0
- package/dist/types/errors.d.ts +10 -2
- package/dist/types/gen/chat/ChannelApi.d.ts +45 -0
- package/dist/types/gen/chat/ChatApi.d.ts +334 -0
- package/dist/types/gen/model-decoders/decoders.d.ts +3 -0
- package/dist/types/gen/model-decoders/event-decoder-mapping.d.ts +6 -0
- package/dist/types/gen/models/index.d.ts +8836 -0
- package/dist/types/gen/moderation/ModerationApi.d.ts +50 -0
- package/dist/types/gen-imports.d.ts +3 -0
- package/dist/types/index.d.ts +8 -7
- package/dist/types/insights.d.ts +9 -8
- package/dist/types/logger.d.ts +9 -0
- package/dist/types/messageComposer/LocationComposer.d.ts +7 -4
- package/dist/types/messageComposer/attachmentIdentity.d.ts +2 -2
- package/dist/types/messageComposer/attachmentManager.d.ts +8 -4
- package/dist/types/messageComposer/configuration/types.d.ts +11 -11
- package/dist/types/messageComposer/linkPreviewsManager.d.ts +25 -13
- package/dist/types/messageComposer/messageComposer.d.ts +10 -10
- package/dist/types/messageComposer/middleware/messageComposer/types.d.ts +3 -3
- package/dist/types/messageComposer/middleware/pollComposer/types.d.ts +4 -7
- package/dist/types/messageComposer/middleware/textComposer/commandUtils.d.ts +3 -3
- package/dist/types/messageComposer/middleware/textComposer/commands.d.ts +7 -7
- package/dist/types/messageComposer/middleware/textComposer/mentions.d.ts +218 -4
- package/dist/types/messageComposer/middleware/textComposer/types.d.ts +9 -4
- package/dist/types/messageComposer/pollComposer.d.ts +5 -4
- package/dist/types/messageComposer/textComposer.d.ts +8 -3
- package/dist/types/messageComposer/types.d.ts +6 -28
- package/dist/types/messageDelivery/MessageDeliveryReporter.d.ts +40 -25
- package/dist/types/messageDelivery/MessageReceiptsTracker.d.ts +53 -12
- package/dist/types/messageOperations/MessageOperationStatePolicy.d.ts +19 -0
- package/dist/types/messageOperations/MessageOperations.d.ts +17 -0
- package/dist/types/messageOperations/index.d.ts +3 -0
- package/dist/types/messageOperations/types.d.ts +49 -0
- package/dist/types/moderation.d.ts +21 -203
- package/dist/types/notifications/types.d.ts +8 -7
- package/dist/types/offline-support/offline_support_api.d.ts +111 -87
- package/dist/types/offline-support/offline_sync_manager.d.ts +0 -7
- package/dist/types/offline-support/types.d.ts +19 -23
- package/dist/types/offline-support/util.d.ts +4 -2
- package/dist/types/pagination/FilterBuilder.d.ts +1 -1
- package/dist/types/pagination/ItemIndex.d.ts +25 -0
- package/dist/types/pagination/cursorDerivation/createdAtAroundPaginationFlags.d.ts +9 -0
- package/dist/types/pagination/cursorDerivation/idAroundPaginationFlags.d.ts +6 -0
- package/dist/types/pagination/cursorDerivation/index.d.ts +1 -0
- package/dist/types/pagination/cursorDerivation/linearPaginationFlags.d.ts +6 -0
- package/dist/types/pagination/filterCompiler.d.ts +7 -0
- package/dist/types/pagination/index.d.ts +1 -3
- package/dist/types/pagination/paginators/BasePaginator.d.ts +848 -0
- package/dist/types/pagination/paginators/ChannelPaginator.d.ts +212 -0
- package/dist/types/pagination/paginators/MessageIntervalPaginator.d.ts +282 -0
- package/dist/types/pagination/paginators/MessagePaginator.d.ts +208 -0
- package/dist/types/pagination/paginators/PinnedMessagePaginator.d.ts +39 -0
- package/dist/types/pagination/paginators/ReminderPaginator.d.ts +19 -0
- package/dist/types/pagination/paginators/UserGroupPaginator.d.ts +24 -0
- package/dist/types/pagination/paginators/index.d.ts +7 -0
- package/dist/types/pagination/paginators/stateThrottling.d.ts +2 -0
- package/dist/types/pagination/sortCompiler.d.ts +49 -0
- package/dist/types/pagination/types.normalization.d.ts +11 -0
- package/dist/types/pagination/utility.normalization.d.ts +18 -0
- package/dist/types/pagination/utility.queryChannel.d.ts +26 -0
- package/dist/types/pagination/utility.search.d.ts +13 -0
- package/dist/types/permissions.d.ts +1 -1
- package/dist/types/poll.d.ts +29 -32
- package/dist/types/poll_manager.d.ts +2 -2
- package/dist/types/reminders/Reminder.d.ts +2 -5
- package/dist/types/reminders/ReminderManager.d.ts +2 -9
- package/dist/types/search/MessageSearchSource.d.ts +4 -4
- package/dist/types/search/UserSearchSource.d.ts +1 -1
- package/dist/types/search/types.d.ts +3 -3
- package/dist/types/segment.d.ts +0 -34
- package/dist/types/signing.d.ts +105 -65
- package/dist/types/store.d.ts +1 -0
- package/dist/types/thread.d.ts +70 -34
- package/dist/types/thread_manager.d.ts +3 -3
- package/dist/types/token_manager.d.ts +15 -10
- package/dist/types/types.d.ts +197 -3404
- package/dist/types/utils/FixedSizeQueueCache.d.ts +12 -6
- package/dist/types/utils/WithSubscriptions.d.ts +2 -1
- package/dist/types/utils/concurrency.d.ts +4 -4
- package/dist/types/utils/mergeWith/mergeWith.d.ts +3 -3
- package/dist/types/utils/mergeWith/mergeWithCore.d.ts +8 -3
- package/dist/types/utils/retryable.d.ts +34 -0
- package/dist/types/utils/throttling/throttle.d.ts +36 -0
- package/dist/types/utils.d.ts +100 -194
- package/package.json +7 -3
- package/src/ChannelManager.ts +724 -0
- package/src/CooldownTimer.ts +2 -2
- package/src/EventHandlerPipeline.ts +228 -0
- package/src/LiveLocationManager.ts +12 -11
- package/src/api-client.ts +275 -0
- package/src/campaign.ts +1 -77
- package/src/channel.ts +1344 -1282
- package/src/channel_batch_updater.ts +1 -211
- package/src/channel_state.ts +144 -1013
- package/src/client.ts +936 -3995
- package/src/client_state.ts +4 -4
- package/src/configuration/InstanceConfigurationService.ts +73 -0
- package/src/configuration/index.ts +1 -0
- package/src/configuration/types.ts +81 -0
- package/src/connection.ts +145 -94
- package/src/connection_fallback.ts +29 -27
- package/src/constants.ts +1 -5
- package/src/custom_types.ts +4 -1
- package/src/entityStore/EntityStore.ts +208 -0
- package/src/entityStore/StoreBackedItemIndex.ts +115 -0
- package/src/entityStore/applyReactionLocally.ts +113 -0
- package/src/entityStore/index.ts +2 -0
- package/src/errors.ts +14 -2
- package/src/gen/chat/ChannelApi.ts +275 -0
- package/src/gen/chat/ChatApi.ts +2554 -0
- package/src/gen/model-decoders/decoders.ts +2680 -0
- package/src/gen/model-decoders/event-decoder-mapping.ts +198 -0
- package/src/gen/models/index.ts +12673 -0
- package/src/gen/moderation/ModerationApi.ts +607 -0
- package/src/gen-imports.ts +3 -0
- package/src/index.ts +11 -12
- package/src/insights.ts +6 -5
- package/src/logger.ts +28 -0
- package/src/messageComposer/LocationComposer.ts +9 -5
- package/src/messageComposer/MessageComposerEffectHandlers.ts +1 -0
- package/src/messageComposer/attachmentIdentity.ts +16 -9
- package/src/messageComposer/attachmentManager.ts +13 -8
- package/src/messageComposer/configuration/types.ts +11 -11
- package/src/messageComposer/linkPreviewsManager.ts +9 -10
- package/src/messageComposer/messageComposer.ts +57 -53
- package/src/messageComposer/middleware/messageComposer/attachments.ts +1 -2
- package/src/messageComposer/middleware/messageComposer/cleanData.ts +1 -1
- package/src/messageComposer/middleware/messageComposer/compositionValidation.ts +2 -2
- package/src/messageComposer/middleware/messageComposer/messageComposerState.ts +2 -2
- package/src/messageComposer/middleware/messageComposer/sharedLocation.ts +4 -3
- package/src/messageComposer/middleware/messageComposer/textComposer.ts +2 -2
- package/src/messageComposer/middleware/messageComposer/types.ts +3 -4
- package/src/messageComposer/middleware/messageComposer/userDataInjection.ts +9 -5
- package/src/messageComposer/middleware/pollComposer/state.ts +0 -2
- package/src/messageComposer/middleware/pollComposer/types.ts +4 -7
- package/src/messageComposer/middleware/textComposer/TextComposerMiddlewareExecutor.ts +10 -1
- package/src/messageComposer/middleware/textComposer/commandEffects.ts +4 -4
- package/src/messageComposer/middleware/textComposer/commandUtils.ts +3 -6
- package/src/messageComposer/middleware/textComposer/commands.ts +3 -3
- package/src/messageComposer/middleware/textComposer/mentionUtils.ts +1 -1
- package/src/messageComposer/middleware/textComposer/mentions.ts +31 -15
- package/src/messageComposer/middleware/textComposer/types.ts +9 -4
- package/src/messageComposer/pollComposer.ts +6 -8
- package/src/messageComposer/textComposer.ts +27 -2
- package/src/messageComposer/types.ts +6 -28
- package/src/messageDelivery/MessageDeliveryReporter.ts +101 -45
- package/src/messageDelivery/MessageReceiptsTracker.ts +294 -21
- package/src/messageOperations/MessageOperationStatePolicy.ts +77 -0
- package/src/messageOperations/MessageOperations.ts +212 -0
- package/src/messageOperations/index.ts +10 -0
- package/src/messageOperations/types.ts +64 -0
- package/src/moderation.ts +38 -431
- package/src/notifications/types.ts +8 -7
- package/src/offline-support/offline_support_api.ts +200 -133
- package/src/offline-support/offline_sync_manager.ts +23 -44
- package/src/offline-support/types.ts +23 -26
- package/src/offline-support/util.ts +5 -4
- package/src/pagination/FilterBuilder.ts +4 -1
- package/src/pagination/ItemIndex.ts +25 -0
- package/src/pagination/cursorDerivation/createdAtAroundPaginationFlags.ts +73 -0
- package/src/pagination/cursorDerivation/idAroundPaginationFlags.ts +53 -0
- package/src/pagination/cursorDerivation/index.ts +1 -0
- package/src/pagination/cursorDerivation/linearPaginationFlags.ts +86 -0
- package/src/pagination/filterCompiler.ts +196 -0
- package/src/pagination/index.ts +1 -3
- package/src/pagination/paginators/BasePaginator.ts +2859 -0
- package/src/pagination/paginators/ChannelPaginator.ts +729 -0
- package/src/pagination/paginators/MessageIntervalPaginator.ts +1116 -0
- package/src/pagination/paginators/MessagePaginator.ts +508 -0
- package/src/pagination/paginators/PinnedMessagePaginator.ts +108 -0
- package/src/pagination/paginators/ReminderPaginator.ts +107 -0
- package/src/pagination/paginators/UserGroupPaginator.ts +127 -0
- package/src/pagination/paginators/index.ts +7 -0
- package/src/pagination/paginators/stateThrottling.ts +31 -0
- package/src/pagination/sortCompiler.ts +193 -0
- package/src/pagination/types.normalization.ts +14 -0
- package/src/pagination/utility.normalization.ts +106 -0
- package/src/pagination/utility.queryChannel.ts +84 -0
- package/src/pagination/utility.search.ts +80 -0
- package/src/permissions.ts +7 -8
- package/src/poll.ts +101 -110
- package/src/poll_manager.ts +14 -8
- package/src/reminders/Reminder.ts +2 -5
- package/src/reminders/ReminderManager.ts +19 -21
- package/src/search/BaseSearchSource.ts +0 -3
- package/src/search/ChannelMemberSearchSource.ts +7 -1
- package/src/search/ChannelSearchSource.ts +10 -3
- package/src/search/MessageSearchSource.ts +29 -30
- package/src/search/UserSearchSource.ts +12 -8
- package/src/search/types.ts +3 -4
- package/src/segment.ts +1 -95
- package/src/signing.ts +107 -67
- package/src/store.ts +2 -1
- package/src/thread.ts +562 -238
- package/src/thread_manager.ts +37 -16
- package/src/token_manager.ts +21 -10
- package/src/types.ts +364 -4533
- package/src/uploadManager.ts +8 -0
- package/src/utils/FixedSizeQueueCache.ts +12 -6
- package/src/utils/WithSubscriptions.ts +2 -1
- package/src/utils/concurrency.ts +4 -4
- package/src/utils/mergeWith/mergeWith.ts +3 -3
- package/src/utils/mergeWith/mergeWithCore.ts +140 -164
- package/src/utils/mergeWith/mergeWithDiff.ts +3 -3
- package/src/utils/retryable.ts +117 -0
- package/src/utils/throttling/throttle.ts +130 -0
- package/src/utils.ts +208 -754
- package/dist/types/channel_manager.d.ts +0 -122
- package/dist/types/events.d.ts +0 -72
- package/dist/types/pagination/BasePaginator.d.ts +0 -69
- package/dist/types/pagination/ReminderPaginator.d.ts +0 -16
- package/dist/types/pagination/UserGroupPaginator.d.ts +0 -21
- package/src/channel_manager.ts +0 -830
- package/src/events.ts +0 -75
- package/src/pagination/BasePaginator.ts +0 -184
- package/src/pagination/ReminderPaginator.ts +0 -56
- package/src/pagination/UserGroupPaginator.ts +0 -93
package/src/signing.ts
CHANGED
|
@@ -2,18 +2,17 @@ import jwt from 'jsonwebtoken';
|
|
|
2
2
|
import crypto from 'crypto';
|
|
3
3
|
import zlib from 'zlib';
|
|
4
4
|
import { decodeBase64, encodeBase64 } from './base64';
|
|
5
|
-
import type {
|
|
5
|
+
import type { UR } from './types';
|
|
6
|
+
import type { WSEvent } from './gen/models';
|
|
6
7
|
|
|
7
8
|
/**
|
|
8
|
-
* Creates the JWT token that can be used for a
|
|
9
|
-
*
|
|
10
|
-
* @
|
|
11
|
-
* @
|
|
12
|
-
* @param
|
|
13
|
-
* @param
|
|
14
|
-
* @
|
|
15
|
-
* @param {SignOptions} [jwtOptions] - Options that can be past to jwt.sign
|
|
16
|
-
* @return {string} JWT Token
|
|
9
|
+
* Creates the JWT token that can be used for a user session.
|
|
10
|
+
*
|
|
11
|
+
* @param apiSecret - API secret key.
|
|
12
|
+
* @param userId - The `user_id` key in the JWT payload.
|
|
13
|
+
* @param extraData - Extra data that should be part of the JWT token (optional, defaults to `{}`).
|
|
14
|
+
* @param jwtOptions - Options that can be passed to `jwt.sign` (optional, defaults to `{}`).
|
|
15
|
+
* @returns The signed JWT token.
|
|
17
16
|
*/
|
|
18
17
|
export function JWTUserToken(
|
|
19
18
|
apiSecret: jwt.Secret,
|
|
@@ -30,7 +29,7 @@ export function JWTUserToken(
|
|
|
30
29
|
...extraData,
|
|
31
30
|
};
|
|
32
31
|
|
|
33
|
-
// make sure we return a clear error when
|
|
32
|
+
// make sure we return a clear error when the JWT module is shimmed (i.e. browser build)
|
|
34
33
|
if (jwt == null || jwt.sign == null) {
|
|
35
34
|
throw Error(
|
|
36
35
|
`Unable to find jwt crypto, if you are getting this error is probably because you are trying to generate tokens on browser or React Native (or other environment where crypto functions are not available). Please Note: token should only be generated server-side.`,
|
|
@@ -48,6 +47,13 @@ export function JWTUserToken(
|
|
|
48
47
|
return jwt.sign(payload, apiSecret, opts);
|
|
49
48
|
}
|
|
50
49
|
|
|
50
|
+
/**
|
|
51
|
+
* Creates the JWT token that can be used for a server-side session.
|
|
52
|
+
*
|
|
53
|
+
* @param apiSecret - API secret key.
|
|
54
|
+
* @param jwtOptions - Options that can be passed to `jwt.sign` (optional, defaults to `{}`).
|
|
55
|
+
* @returns The signed JWT token.
|
|
56
|
+
*/
|
|
51
57
|
export function JWTServerToken(apiSecret: jwt.Secret, jwtOptions: jwt.SignOptions = {}) {
|
|
52
58
|
const payload = {
|
|
53
59
|
server: true,
|
|
@@ -60,6 +66,12 @@ export function JWTServerToken(apiSecret: jwt.Secret, jwtOptions: jwt.SignOption
|
|
|
60
66
|
return jwt.sign(payload, apiSecret, opts);
|
|
61
67
|
}
|
|
62
68
|
|
|
69
|
+
/**
|
|
70
|
+
* Decodes a JWT token and returns the embedded `user_id`.
|
|
71
|
+
*
|
|
72
|
+
* @param token - The JWT token to decode.
|
|
73
|
+
* @returns The `user_id` extracted from the token's payload, or an empty string when the token is malformed.
|
|
74
|
+
*/
|
|
63
75
|
export function UserFromToken(token: string) {
|
|
64
76
|
const fragments = token.split('.');
|
|
65
77
|
if (fragments.length !== 3) {
|
|
@@ -72,9 +84,12 @@ export function UserFromToken(token: string) {
|
|
|
72
84
|
}
|
|
73
85
|
|
|
74
86
|
/**
|
|
87
|
+
* Generates a development token for the given user.
|
|
88
|
+
*
|
|
89
|
+
* Development tokens are unsigned and must only be used in environments where token validation is disabled.
|
|
75
90
|
*
|
|
76
|
-
* @param
|
|
77
|
-
* @
|
|
91
|
+
* @param userId - The ID of the user.
|
|
92
|
+
* @returns The development token.
|
|
78
93
|
*/
|
|
79
94
|
export function DevToken(userId: string) {
|
|
80
95
|
return [
|
|
@@ -85,16 +100,18 @@ export function DevToken(userId: string) {
|
|
|
85
100
|
}
|
|
86
101
|
|
|
87
102
|
/**
|
|
88
|
-
* Constant-time HMAC-SHA256 verification of `signature` against the
|
|
89
|
-
*
|
|
90
|
-
*
|
|
91
|
-
* decoded a gzipped or base64-wrapped payload must pass the inflated
|
|
92
|
-
* bytes here.
|
|
103
|
+
* Constant-time HMAC-SHA256 verification of `signature` against the digest of `body` using `secret`
|
|
104
|
+
* as the key. The signature is always computed over the **uncompressed** JSON bytes, so callers that
|
|
105
|
+
* decoded a gzipped or base64-wrapped payload must pass the inflated bytes here.
|
|
93
106
|
*
|
|
94
|
-
* The legacy `client.verifyWebhook` helper wraps this function, so
|
|
95
|
-
*
|
|
96
|
-
*
|
|
97
|
-
*
|
|
107
|
+
* The legacy `client.verifyWebhook` helper wraps this function, so callers that have already
|
|
108
|
+
* migrated to {@link verifyAndParseWebhook}, {@link parseSqs}, or {@link parseSns} rarely need to
|
|
109
|
+
* invoke this directly.
|
|
110
|
+
*
|
|
111
|
+
* @param body - The uncompressed payload bytes that Stream signed.
|
|
112
|
+
* @param signature - The HMAC-SHA256 signature delivered alongside the payload.
|
|
113
|
+
* @param secret - Your app's API secret used as the HMAC key.
|
|
114
|
+
* @returns `true` when the signature matches the digest of `body`, otherwise `false`.
|
|
98
115
|
*/
|
|
99
116
|
export function verifySignature(
|
|
100
117
|
body: string | Buffer,
|
|
@@ -111,9 +128,14 @@ export function verifySignature(
|
|
|
111
128
|
}
|
|
112
129
|
|
|
113
130
|
/**
|
|
114
|
-
*
|
|
115
|
-
*
|
|
116
|
-
*
|
|
131
|
+
* Verifies an HMAC-SHA256 signature with the legacy parameter order.
|
|
132
|
+
*
|
|
133
|
+
* @param body - The uncompressed payload bytes that Stream signed.
|
|
134
|
+
* @param secret - Your app's API secret used as the HMAC key.
|
|
135
|
+
* @param signature - The HMAC-SHA256 signature delivered alongside the payload.
|
|
136
|
+
* @returns `true` when the signature matches the digest of `body`, otherwise `false`.
|
|
137
|
+
* @deprecated Use {@link verifySignature} instead — same logic, parameters reordered to match the
|
|
138
|
+
* cross-SDK contract (`verifySignature(body, signature, secret)`).
|
|
117
139
|
*/
|
|
118
140
|
export function CheckSignature(body: string | Buffer, secret: string, signature: string) {
|
|
119
141
|
return verifySignature(body, signature, secret);
|
|
@@ -150,14 +172,16 @@ export class InvalidWebhookError extends Error {
|
|
|
150
172
|
}
|
|
151
173
|
|
|
152
174
|
/**
|
|
153
|
-
* Returns `body` as a `Buffer`, gzip-decompressed when its first two
|
|
154
|
-
*
|
|
155
|
-
*
|
|
156
|
-
* bytes are returned unchanged.
|
|
175
|
+
* Returns `body` as a `Buffer`, gzip-decompressed when its first two bytes match the gzip magic
|
|
176
|
+
* (`1f 8b`, per RFC 1952). When the body is plain JSON (no compression, or middleware already
|
|
177
|
+
* decompressed), the bytes are returned unchanged.
|
|
157
178
|
*
|
|
158
|
-
* Magic-byte detection (rather than relying on a header) keeps the
|
|
159
|
-
*
|
|
160
|
-
*
|
|
179
|
+
* Magic-byte detection (rather than relying on a header) keeps the same handler correct when
|
|
180
|
+
* middleware — Express, Next.js, AWS Lambda — auto-decompresses the request before your code sees it.
|
|
181
|
+
*
|
|
182
|
+
* @param rawBody - The raw HTTP request body, either as a string or a `Buffer`.
|
|
183
|
+
* @returns The uncompressed payload bytes.
|
|
184
|
+
* @throws {@link InvalidWebhookError} when the gzip envelope is malformed.
|
|
161
185
|
*/
|
|
162
186
|
export function gunzipPayload(rawBody: string | Buffer): Buffer {
|
|
163
187
|
const GZIP_MAGIC = Buffer.from([0x1f, 0x8b]);
|
|
@@ -174,13 +198,15 @@ export function gunzipPayload(rawBody: string | Buffer): Buffer {
|
|
|
174
198
|
}
|
|
175
199
|
|
|
176
200
|
/**
|
|
177
|
-
* Reverses the SQS firehose envelope: the message `Body` is
|
|
178
|
-
*
|
|
179
|
-
*
|
|
201
|
+
* Reverses the SQS firehose envelope: the message `Body` is base64-decoded, then the result is
|
|
202
|
+
* gzip-decompressed when it begins with the gzip magic. Returns the raw JSON `Buffer` Stream signed.
|
|
203
|
+
*
|
|
204
|
+
* SQS bodies are always base64-encoded so they remain valid UTF-8 over the queue. The same call
|
|
205
|
+
* works whether or not Stream is currently compressing payloads for this app.
|
|
180
206
|
*
|
|
181
|
-
*
|
|
182
|
-
*
|
|
183
|
-
*
|
|
207
|
+
* @param body - The base64-encoded SQS message body.
|
|
208
|
+
* @returns The decoded (and decompressed, when gzipped) payload bytes.
|
|
209
|
+
* @throws {@link InvalidWebhookError} when the body is not canonical base64 or the gzip envelope is malformed.
|
|
184
210
|
*/
|
|
185
211
|
export function decodeSqsPayload(body: string): Buffer {
|
|
186
212
|
// Reject anything that isn't canonical base64 up front. Node's base64
|
|
@@ -199,12 +225,15 @@ export function decodeSqsPayload(body: string): Buffer {
|
|
|
199
225
|
}
|
|
200
226
|
|
|
201
227
|
/**
|
|
202
|
-
* Reverses an SNS HTTP notification envelope. When `notificationBody`
|
|
203
|
-
*
|
|
204
|
-
*
|
|
205
|
-
*
|
|
206
|
-
*
|
|
207
|
-
*
|
|
228
|
+
* Reverses an SNS HTTP notification envelope. When `notificationBody` is a JSON envelope
|
|
229
|
+
* (`{"Type":"Notification","MessageRequest":"..."}`), the inner `MessageRequest` field is extracted and run
|
|
230
|
+
* through the SQS pipeline (base64-decode, then gzip-if-magic). When the input is not a JSON
|
|
231
|
+
* envelope it is treated as the already-extracted `MessageRequest` string, so call sites that pre-unwrap
|
|
232
|
+
* continue to work.
|
|
233
|
+
*
|
|
234
|
+
* @param notificationBody - The raw SNS notification body, or a pre-extracted `MessageRequest` string.
|
|
235
|
+
* @returns The decoded (and decompressed, when gzipped) payload bytes.
|
|
236
|
+
* @throws {@link InvalidWebhookError} when the body is not canonical base64 or the gzip envelope is malformed.
|
|
208
237
|
*/
|
|
209
238
|
export function decodeSnsPayload(notificationBody: string): Buffer {
|
|
210
239
|
const inner = extractSnsMessage(notificationBody);
|
|
@@ -226,28 +255,32 @@ function extractSnsMessage(notificationBody: string): string | null {
|
|
|
226
255
|
parsed === null ||
|
|
227
256
|
typeof parsed !== 'object' ||
|
|
228
257
|
Array.isArray(parsed) ||
|
|
229
|
-
typeof (parsed as {
|
|
258
|
+
typeof (parsed as { MessageRequest?: unknown }).MessageRequest !== 'string'
|
|
230
259
|
) {
|
|
231
260
|
return null;
|
|
232
261
|
}
|
|
233
|
-
return (parsed as {
|
|
262
|
+
return (parsed as { MessageRequest: string }).MessageRequest;
|
|
234
263
|
}
|
|
235
264
|
|
|
236
265
|
/**
|
|
237
|
-
*
|
|
238
|
-
*
|
|
239
|
-
*
|
|
266
|
+
* Parses a JSON-encoded webhook event into a typed {@link WSEvent}. New event types Stream
|
|
267
|
+
* introduces still parse successfully — the runtime shape is the JSON Stream sent and the `type`
|
|
268
|
+
* field stays preserved.
|
|
269
|
+
*
|
|
270
|
+
* @param payload - The raw event payload bytes or string.
|
|
271
|
+
* @returns The parsed WebSocket event.
|
|
272
|
+
* @throws {@link InvalidWebhookError} when the payload is not valid JSON.
|
|
240
273
|
*/
|
|
241
|
-
export function parseEvent(payload: Buffer | string):
|
|
274
|
+
export function parseEvent(payload: Buffer | string): WSEvent {
|
|
242
275
|
const text = Buffer.isBuffer(payload) ? payload.toString('utf8') : payload;
|
|
243
276
|
try {
|
|
244
|
-
return JSON.parse(text) as
|
|
277
|
+
return JSON.parse(text) as WSEvent;
|
|
245
278
|
} catch {
|
|
246
279
|
throw new InvalidWebhookError(InvalidWebhookErrorMessages.invalidJson);
|
|
247
280
|
}
|
|
248
281
|
}
|
|
249
282
|
|
|
250
|
-
function verifyAndParse(payload: Buffer, signature: string, secret: string):
|
|
283
|
+
function verifyAndParse(payload: Buffer, signature: string, secret: string): WSEvent {
|
|
251
284
|
if (!verifySignature(payload, signature, secret)) {
|
|
252
285
|
throw new InvalidWebhookError(InvalidWebhookErrorMessages.signatureMismatch);
|
|
253
286
|
}
|
|
@@ -255,36 +288,43 @@ function verifyAndParse(payload: Buffer, signature: string, secret: string): Eve
|
|
|
255
288
|
}
|
|
256
289
|
|
|
257
290
|
/**
|
|
258
|
-
* Decompress (when gzipped), verify the HMAC `signature`, and return
|
|
259
|
-
* the parsed {@link Event}.
|
|
291
|
+
* Decompress (when gzipped), verify the HMAC `signature`, and return the parsed {@link WSEvent}.
|
|
260
292
|
*
|
|
261
|
-
* @param rawBody Raw HTTP request body bytes Stream signed
|
|
262
|
-
* @param signature Value of the `X-Signature` header
|
|
263
|
-
* @param secret Your app's API secret
|
|
264
|
-
* @
|
|
265
|
-
*
|
|
293
|
+
* @param rawBody - Raw HTTP request body bytes Stream signed.
|
|
294
|
+
* @param signature - Value of the `X-Signature` header.
|
|
295
|
+
* @param secret - Your app's API secret.
|
|
296
|
+
* @returns The parsed WebSocket event.
|
|
297
|
+
* @throws {@link InvalidWebhookError} when the signature does not match or the gzip envelope is malformed.
|
|
266
298
|
*/
|
|
267
299
|
export function verifyAndParseWebhook(
|
|
268
300
|
rawBody: string | Buffer,
|
|
269
301
|
signature: string,
|
|
270
302
|
secret: string,
|
|
271
|
-
):
|
|
303
|
+
): WSEvent {
|
|
272
304
|
return verifyAndParse(gunzipPayload(rawBody), signature, secret);
|
|
273
305
|
}
|
|
274
306
|
|
|
275
307
|
/**
|
|
276
|
-
*
|
|
277
|
-
*
|
|
278
|
-
*
|
|
308
|
+
* Decodes the SQS message `Body` (base64, then gzip-if-magic) and returns the parsed {@link WSEvent}.
|
|
309
|
+
* Stream does not attach an application-level HMAC to SQS deliveries — use
|
|
310
|
+
* {@link verifyAndParseWebhook} for HTTP webhooks.
|
|
311
|
+
*
|
|
312
|
+
* @param messageBody - The base64-encoded SQS message body.
|
|
313
|
+
* @returns The parsed WebSocket event.
|
|
314
|
+
* @throws {@link InvalidWebhookError} when the body is malformed.
|
|
279
315
|
*/
|
|
280
|
-
export function parseSqs(messageBody: string):
|
|
316
|
+
export function parseSqs(messageBody: string): WSEvent {
|
|
281
317
|
return parseEvent(decodeSqsPayload(messageBody));
|
|
282
318
|
}
|
|
283
319
|
|
|
284
320
|
/**
|
|
285
|
-
*
|
|
286
|
-
*
|
|
321
|
+
* Decodes an SNS notification (unwraps the JSON envelope when needed; same inner format as SQS).
|
|
322
|
+
* No application-level HMAC verification.
|
|
323
|
+
*
|
|
324
|
+
* @param notificationBody - The raw SNS notification body, or a pre-extracted `MessageRequest` string.
|
|
325
|
+
* @returns The parsed WebSocket event.
|
|
326
|
+
* @throws {@link InvalidWebhookError} when the body is malformed.
|
|
287
327
|
*/
|
|
288
|
-
export function parseSns(notificationBody: string):
|
|
328
|
+
export function parseSns(notificationBody: string): WSEvent {
|
|
289
329
|
return parseEvent(decodeSnsPayload(notificationBody));
|
|
290
330
|
}
|
package/src/store.ts
CHANGED
|
@@ -20,10 +20,11 @@ export class StateStore<T extends Record<string, unknown>> {
|
|
|
20
20
|
|
|
21
21
|
/**
|
|
22
22
|
* Allows merging two stores only if their keys differ otherwise there's no way to ensure the data type stability.
|
|
23
|
+
*
|
|
23
24
|
* @experimental
|
|
24
25
|
* This method is experimental and may change in future versions.
|
|
25
26
|
*/
|
|
26
|
-
|
|
27
|
+
|
|
27
28
|
public merge<Q extends StateStore<any>>(
|
|
28
29
|
stateStore: Q extends StateStore<infer L>
|
|
29
30
|
? Extract<keyof T, keyof L> extends never
|