stream-chat 10.0.1 → 10.1.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.
- package/dist/cjs/index.browser.js +104 -14
- package/dist/cjs/index.browser.js.map +4 -4
- package/dist/cjs/index.node.js +105 -14
- package/dist/cjs/index.node.js.map +4 -4
- package/dist/esm/index.mjs +104 -14
- package/dist/esm/index.mjs.map +4 -4
- package/dist/types/channel.d.ts +10 -5
- package/dist/types/channel_state.d.ts +1 -0
- package/dist/types/client.d.ts +7 -1
- package/dist/types/index.d.ts +1 -0
- package/dist/types/types.d.ts +2 -2
- package/dist/types/utils/activeMemberCount.d.ts +5 -0
- package/dist/types/version.d.ts +60 -0
- package/package.json +1 -1
- package/src/channel.ts +27 -7
- package/src/channel_state.ts +1 -0
- package/src/client.ts +42 -8
- package/src/index.ts +2 -0
- package/src/types.ts +2 -2
- package/src/utils/activeMemberCount.ts +30 -0
- package/src/version.ts +98 -0
package/dist/types/channel.d.ts
CHANGED
|
@@ -522,11 +522,16 @@ export declare class Channel extends ChannelApi {
|
|
|
522
522
|
get offlineMode(): boolean;
|
|
523
523
|
set offlineMode(offlineMode: boolean);
|
|
524
524
|
/**
|
|
525
|
-
* Reads `state.data`. Assigning publishes `data`, `memberCount
|
|
526
|
-
* If the object omits `member_count` or
|
|
527
|
-
* a copy (the assigned object is not
|
|
528
|
-
*
|
|
529
|
-
*
|
|
525
|
+
* Reads `state.data`. Assigning publishes `data`, `memberCount`, `activeMemberCount` and
|
|
526
|
+
* `ownCapabilities` in one update. If the object omits `member_count`, `active_member_count` or
|
|
527
|
+
* `own_capabilities`, the last known values are carried onto a copy (the assigned object is not
|
|
528
|
+
* changed). A carried `active_member_count` moves with `member_count` (shifted by the change in
|
|
529
|
+
* membership, clamped to `member_count`), since only Query Channels returns it; treat it as a
|
|
530
|
+
* last-known estimate, refreshed on the next query — it can lag a (de)activation or an
|
|
531
|
+
* already-deactivated member being added or removed. Editing
|
|
532
|
+
* `channel.data.x` in place does not reach the state. `ownCapabilities` keeps its array while the
|
|
533
|
+
* capabilities stay the same (in any order), so a subscriber to it is notified only when they
|
|
534
|
+
* change.
|
|
530
535
|
*/
|
|
531
536
|
get data(): Partial<ChannelResponse> | undefined;
|
|
532
537
|
set data(next: Partial<ChannelResponse> | undefined);
|
package/dist/types/client.d.ts
CHANGED
|
@@ -522,7 +522,12 @@ export declare class StreamChat extends ChatApi {
|
|
|
522
522
|
*/
|
|
523
523
|
_addChannelConfig({ cid, config }: Pick<ChannelResponse, 'cid' | 'config'>): void;
|
|
524
524
|
/**
|
|
525
|
-
* Revokes a global ban.
|
|
525
|
+
* Revokes a global ban, or a channel ban when `options.type` and `options.id` name a channel.
|
|
526
|
+
*
|
|
527
|
+
* Goes through V2 `client.moderation.unban()`. The one exception is
|
|
528
|
+
* `remove_future_channels_ban`, which that endpoint does not accept, so a call with it set still
|
|
529
|
+
* goes to v1 `DELETE /moderation/ban`. `shadow` is accepted for compatibility but ignored: the
|
|
530
|
+
* server lifts whichever ban is in place, shadow or not, on both endpoints.
|
|
526
531
|
*
|
|
527
532
|
* @param targetUserId - The user to unban.
|
|
528
533
|
* @param options - Unban options (optional).
|
|
@@ -531,6 +536,7 @@ export declare class StreamChat extends ChatApi {
|
|
|
531
536
|
unbanUser(targetUserId: string, options?: UnBanUserOptions): Promise<APIResponse>;
|
|
532
537
|
blockUser(blockedUserId: string, requestOptions?: StreamRequestOptions): Promise<StreamResponse<import(".").BlockUsersResponse>>;
|
|
533
538
|
getBlockedUsers(...args: Parameters<ChatApi['getBlockedUsers']>): Promise<StreamResponse<import(".").GetBlockedUsersResponse>>;
|
|
539
|
+
updatePushNotificationPreferences(...args: Parameters<ChatApi['updatePushNotificationPreferences']>): Promise<StreamResponse<import(".").UpsertPushPreferencesResponse>>;
|
|
534
540
|
unblockUser(blockedUserId: string, requestOptions?: StreamRequestOptions): Promise<StreamResponse<import(".").UnblockUsersResponse>>;
|
|
535
541
|
/**
|
|
536
542
|
* Checks whether a user is muted.
|
package/dist/types/index.d.ts
CHANGED
|
@@ -47,3 +47,4 @@ export { asTimestampNS, convertTimestampToDate, dateToNs, msToNs, nowNs, NS_PER_
|
|
|
47
47
|
export { hasPending, settled, withCancellation, withoutConcurrency, } from './utils/concurrency';
|
|
48
48
|
export * from './ChannelManager';
|
|
49
49
|
export * from './EventHandlerPipeline';
|
|
50
|
+
export { version } from './version';
|
package/dist/types/types.d.ts
CHANGED
|
@@ -9,8 +9,8 @@ export type RequireAtLeastOne<T> = {
|
|
|
9
9
|
[K in keyof T]-?: Required<Pick<T, K>> & Partial<Omit<T, K>>;
|
|
10
10
|
}[keyof T];
|
|
11
11
|
/**
|
|
12
|
-
*
|
|
13
|
-
*
|
|
12
|
+
* The return type of `StreamChat.unbanUser`, kept so its signature is unchanged. Its v1 fallback
|
|
13
|
+
* (`remove_future_channels_ban`) is the one hand-written `/moderation/*` call left.
|
|
14
14
|
*/
|
|
15
15
|
export type APIResponse = {
|
|
16
16
|
duration: string;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The parsed version of this `stream-chat` build.
|
|
3
|
+
*
|
|
4
|
+
* - `prerelease` is the prerelease identifier without its trailing `.N` (`'rc'` for
|
|
5
|
+
* `10.0.0-rc.35`; a numeric-only id such as `1.0.0-1` has no `.N` and stays `'1'`), or `null`
|
|
6
|
+
* for a stable release. It is a plain string because this package has
|
|
7
|
+
* shipped prereleases beyond `alpha` / `beta` / `rc` (`dev`, `insights`, `offline-support-beta`).
|
|
8
|
+
* - `prereleaseNumber` is the number of that trailing `.N` (`35`), or `null` when there is none.
|
|
9
|
+
* - `raw` is the version string exactly as it was given.
|
|
10
|
+
*
|
|
11
|
+
* Outside the esbuild bundles (unit tests, code importing `src/` directly) no version is injected,
|
|
12
|
+
* and the value is that of the `package.json` placeholder, `0.0.0-development`.
|
|
13
|
+
*/
|
|
14
|
+
export type StreamChatVersion = Readonly<{
|
|
15
|
+
major: number;
|
|
16
|
+
minor: number;
|
|
17
|
+
patch: number;
|
|
18
|
+
prerelease: string | null;
|
|
19
|
+
prereleaseNumber: number | null;
|
|
20
|
+
raw: string;
|
|
21
|
+
}>;
|
|
22
|
+
/**
|
|
23
|
+
* Parses a version string into a {@link StreamChatVersion}. Never throws: a missing or empty value
|
|
24
|
+
* parses as the `package.json` placeholder, and an unparseable one gives zeros (the defaults below)
|
|
25
|
+
* with the input kept in `raw`.
|
|
26
|
+
*
|
|
27
|
+
* @internal
|
|
28
|
+
*/
|
|
29
|
+
export declare const parseVersion: (raw: string | undefined) => StreamChatVersion;
|
|
30
|
+
/**
|
|
31
|
+
* The version esbuild's `define` injected, or `undefined` outside the bundles.
|
|
32
|
+
*
|
|
33
|
+
* Keep `process.env.PKG_VERSION` spelled out exactly: `define` replaces that expression and no
|
|
34
|
+
* other (a destructure or `globalThis.process` would ship the fallback). A `typeof process` guard
|
|
35
|
+
* would not survive the replacement either: it compiles to `typeof process !== 'undefined' ?
|
|
36
|
+
* "10.0.1" : undefined`, which is the fallback in every browser. A `try` keeps the literal while
|
|
37
|
+
* still letting an unbundled `src/` import in a browser (no `process`) load.
|
|
38
|
+
*
|
|
39
|
+
* @internal
|
|
40
|
+
*/
|
|
41
|
+
export declare const readInjectedVersion: () => string | undefined;
|
|
42
|
+
/**
|
|
43
|
+
* The bundle esbuild's `define` injected (`node-cjs`, `browser-cjs`, `browser-esm`), or `undefined`
|
|
44
|
+
* outside the bundles. Same spelling rule as {@link readInjectedVersion}.
|
|
45
|
+
*
|
|
46
|
+
* @internal
|
|
47
|
+
*/
|
|
48
|
+
export declare const readInjectedClientBundle: () => string | undefined;
|
|
49
|
+
/**
|
|
50
|
+
* The version of this `stream-chat` build, for feature detection between packages (a UI SDK
|
|
51
|
+
* checking which core it runs against). To find out whether one specific API exists, checking for
|
|
52
|
+
* the API itself (`'method' in client`) is more precise.
|
|
53
|
+
*
|
|
54
|
+
* Only the esbuild bundles carry the real version. Imported any other way (`src/` through an alias,
|
|
55
|
+
* a linked checkout) it is `0.0.0-development`, so `major === 0` means **unknown**, not old: a
|
|
56
|
+
* feature check should treat it as "assume the newest behaviour" rather than take a legacy path.
|
|
57
|
+
*
|
|
58
|
+
* Both calls are annotated: `#__PURE__` covers one call, not its arguments.
|
|
59
|
+
*/
|
|
60
|
+
export declare const version: StreamChatVersion;
|
package/package.json
CHANGED
package/src/channel.ts
CHANGED
|
@@ -19,6 +19,7 @@ import {
|
|
|
19
19
|
logChatPromiseExecution,
|
|
20
20
|
sanitizeOutgoingAttachments,
|
|
21
21
|
} from './utils';
|
|
22
|
+
import { getCarriedActiveMemberCount } from './utils/activeMemberCount';
|
|
22
23
|
import { asTimestampNS, nowNs } from './utils/time';
|
|
23
24
|
import { abortError, waitUnlessAborted } from './utils/abort';
|
|
24
25
|
import { normalizeUploadFile } from './upload-utils';
|
|
@@ -1185,11 +1186,16 @@ export class Channel extends ChannelApi {
|
|
|
1185
1186
|
}
|
|
1186
1187
|
|
|
1187
1188
|
/**
|
|
1188
|
-
* Reads `state.data`. Assigning publishes `data`, `memberCount
|
|
1189
|
-
* If the object omits `member_count` or
|
|
1190
|
-
* a copy (the assigned object is not
|
|
1191
|
-
*
|
|
1192
|
-
*
|
|
1189
|
+
* Reads `state.data`. Assigning publishes `data`, `memberCount`, `activeMemberCount` and
|
|
1190
|
+
* `ownCapabilities` in one update. If the object omits `member_count`, `active_member_count` or
|
|
1191
|
+
* `own_capabilities`, the last known values are carried onto a copy (the assigned object is not
|
|
1192
|
+
* changed). A carried `active_member_count` moves with `member_count` (shifted by the change in
|
|
1193
|
+
* membership, clamped to `member_count`), since only Query Channels returns it; treat it as a
|
|
1194
|
+
* last-known estimate, refreshed on the next query — it can lag a (de)activation or an
|
|
1195
|
+
* already-deactivated member being added or removed. Editing
|
|
1196
|
+
* `channel.data.x` in place does not reach the state. `ownCapabilities` keeps its array while the
|
|
1197
|
+
* capabilities stay the same (in any order), so a subscriber to it is notified only when they
|
|
1198
|
+
* change.
|
|
1193
1199
|
*/
|
|
1194
1200
|
get data(): Partial<ChannelResponse> | undefined {
|
|
1195
1201
|
return this.state.getLatestValue().data;
|
|
@@ -1197,6 +1203,7 @@ export class Channel extends ChannelApi {
|
|
|
1197
1203
|
|
|
1198
1204
|
set data(next: Partial<ChannelResponse> | undefined) {
|
|
1199
1205
|
const {
|
|
1206
|
+
activeMemberCount: previousActiveMemberCount,
|
|
1200
1207
|
data: previous,
|
|
1201
1208
|
memberCount: previousMemberCount,
|
|
1202
1209
|
ownCapabilities: previousOwnCapabilities,
|
|
@@ -1205,27 +1212,40 @@ export class Channel extends ChannelApi {
|
|
|
1205
1212
|
typeof next?.member_count !== 'number' && typeof previous?.member_count === 'number'
|
|
1206
1213
|
? previous.member_count
|
|
1207
1214
|
: undefined;
|
|
1215
|
+
const carriedActiveMemberCount = next
|
|
1216
|
+
? getCarriedActiveMemberCount({ next, previous })
|
|
1217
|
+
: undefined;
|
|
1208
1218
|
// `own_capabilities` stays undefined until known, so "not loaded" isn't read as "none"
|
|
1209
1219
|
const carriedCapabilities =
|
|
1210
1220
|
!Array.isArray(next?.own_capabilities) && Array.isArray(previous?.own_capabilities)
|
|
1211
1221
|
? [...previous.own_capabilities]
|
|
1212
1222
|
: undefined;
|
|
1213
1223
|
|
|
1224
|
+
const carriesMemberCount = carriedMemberCount !== undefined;
|
|
1225
|
+
const carriesActiveMemberCount = carriedActiveMemberCount !== undefined;
|
|
1226
|
+
const carriesValues =
|
|
1227
|
+
carriesMemberCount || carriesActiveMemberCount || !!carriedCapabilities;
|
|
1228
|
+
|
|
1214
1229
|
const data =
|
|
1215
|
-
next &&
|
|
1230
|
+
next && carriesValues
|
|
1216
1231
|
? {
|
|
1217
1232
|
...next,
|
|
1218
|
-
...(
|
|
1233
|
+
...(carriesMemberCount && { member_count: carriedMemberCount }),
|
|
1234
|
+
...(carriesActiveMemberCount && {
|
|
1235
|
+
active_member_count: carriedActiveMemberCount,
|
|
1236
|
+
}),
|
|
1219
1237
|
...(carriedCapabilities && { own_capabilities: carriedCapabilities }),
|
|
1220
1238
|
}
|
|
1221
1239
|
: next;
|
|
1222
1240
|
|
|
1223
1241
|
const memberCount = data?.member_count ?? carriedMemberCount;
|
|
1242
|
+
const activeMemberCount = data ? data.active_member_count : previousActiveMemberCount;
|
|
1224
1243
|
const ownCapabilities = data?.own_capabilities ?? carriedCapabilities ?? [];
|
|
1225
1244
|
const toKey = (capabilities: readonly string[]) => [...capabilities].sort().join();
|
|
1226
1245
|
const haveSameOwnCapabilities =
|
|
1227
1246
|
toKey(previousOwnCapabilities) === toKey(ownCapabilities);
|
|
1228
1247
|
this.state.partialNext({
|
|
1248
|
+
activeMemberCount,
|
|
1229
1249
|
data,
|
|
1230
1250
|
memberCount: memberCount ?? previousMemberCount,
|
|
1231
1251
|
ownCapabilities: haveSameOwnCapabilities
|
package/src/channel_state.ts
CHANGED
package/src/client.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
/* eslint no-unused-vars: "off" */
|
|
2
|
-
/* global process */
|
|
3
2
|
|
|
4
3
|
import type { AxiosInstance, AxiosRequestConfig } from 'axios';
|
|
5
4
|
import axios from 'axios';
|
|
@@ -22,6 +21,7 @@ import {
|
|
|
22
21
|
sanitizeOutgoingAttachments,
|
|
23
22
|
} from './utils';
|
|
24
23
|
import { nowNs } from './utils/time';
|
|
24
|
+
import { readInjectedClientBundle, readInjectedVersion } from './version';
|
|
25
25
|
import { normalizeUploadFile } from './upload-utils';
|
|
26
26
|
|
|
27
27
|
import type {
|
|
@@ -1681,16 +1681,34 @@ export class StreamChat extends ChatApi {
|
|
|
1681
1681
|
}
|
|
1682
1682
|
|
|
1683
1683
|
/**
|
|
1684
|
-
* Revokes a global ban.
|
|
1684
|
+
* Revokes a global ban, or a channel ban when `options.type` and `options.id` name a channel.
|
|
1685
|
+
*
|
|
1686
|
+
* Goes through V2 `client.moderation.unban()`. The one exception is
|
|
1687
|
+
* `remove_future_channels_ban`, which that endpoint does not accept, so a call with it set still
|
|
1688
|
+
* goes to v1 `DELETE /moderation/ban`. `shadow` is accepted for compatibility but ignored: the
|
|
1689
|
+
* server lifts whichever ban is in place, shadow or not, on both endpoints.
|
|
1685
1690
|
*
|
|
1686
1691
|
* @param targetUserId - The user to unban.
|
|
1687
1692
|
* @param options - Unban options (optional).
|
|
1688
1693
|
* @returns The server response.
|
|
1689
1694
|
*/
|
|
1690
|
-
async unbanUser(
|
|
1691
|
-
|
|
1692
|
-
|
|
1693
|
-
|
|
1695
|
+
async unbanUser(
|
|
1696
|
+
targetUserId: string,
|
|
1697
|
+
options?: UnBanUserOptions,
|
|
1698
|
+
): Promise<APIResponse> {
|
|
1699
|
+
const { id, remove_future_channels_ban, type } = options ?? {};
|
|
1700
|
+
const target_user_id = options?.target_user_id ?? targetUserId;
|
|
1701
|
+
|
|
1702
|
+
if (remove_future_channels_ban) {
|
|
1703
|
+
return await this.api.delete<APIResponse>(this.baseURL + '/moderation/ban', {
|
|
1704
|
+
...options,
|
|
1705
|
+
target_user_id,
|
|
1706
|
+
});
|
|
1707
|
+
}
|
|
1708
|
+
|
|
1709
|
+
return await this.moderation.unban({
|
|
1710
|
+
target_user_id,
|
|
1711
|
+
channel_cid: type && id ? `${type}:${id}` : undefined,
|
|
1694
1712
|
});
|
|
1695
1713
|
}
|
|
1696
1714
|
|
|
@@ -1715,6 +1733,22 @@ export class StreamChat extends ChatApi {
|
|
|
1715
1733
|
return result;
|
|
1716
1734
|
}
|
|
1717
1735
|
|
|
1736
|
+
override async updatePushNotificationPreferences(
|
|
1737
|
+
...args: Parameters<ChatApi['updatePushNotificationPreferences']>
|
|
1738
|
+
) {
|
|
1739
|
+
const result = await super.updatePushNotificationPreferences(...args);
|
|
1740
|
+
const { user } = this;
|
|
1741
|
+
if (!user) return result;
|
|
1742
|
+
|
|
1743
|
+
// The full stored preferences after the update, or null when the request only had
|
|
1744
|
+
// channel-level items for this user.
|
|
1745
|
+
const updated = result.user_preferences[user.id];
|
|
1746
|
+
if (updated) {
|
|
1747
|
+
this.user = { ...user, push_preferences: updated };
|
|
1748
|
+
}
|
|
1749
|
+
return result;
|
|
1750
|
+
}
|
|
1751
|
+
|
|
1718
1752
|
async unblockUser(blockedUserId: string, requestOptions?: StreamRequestOptions) {
|
|
1719
1753
|
const result = await this.unblockUsers(
|
|
1720
1754
|
{
|
|
@@ -1933,7 +1967,7 @@ export class StreamChat extends ChatApi {
|
|
|
1933
1967
|
|
|
1934
1968
|
// Memoized on the first call: later changes to the identifiers are not picked up.
|
|
1935
1969
|
if (!this.cachedUserAgent) {
|
|
1936
|
-
const version =
|
|
1970
|
+
const version = readInjectedVersion();
|
|
1937
1971
|
const { name: sdkName, version: sdkVersion } = this.sdkIdentifier ?? {};
|
|
1938
1972
|
const { name: appName, version: appVersion } = this.appIdentifier ?? {};
|
|
1939
1973
|
const { os, model: deviceModel } = this.deviceIdentifier ?? {};
|
|
@@ -1950,7 +1984,7 @@ export class StreamChat extends ChatApi {
|
|
|
1950
1984
|
app_version: appVersion,
|
|
1951
1985
|
os,
|
|
1952
1986
|
device_model: deviceModel,
|
|
1953
|
-
client_bundle:
|
|
1987
|
+
client_bundle: readInjectedClientBundle(),
|
|
1954
1988
|
})
|
|
1955
1989
|
.filter(([, value]) => value && value.length > 0)
|
|
1956
1990
|
.map(([key, value]) => `${key}=${value}`),
|
package/src/index.ts
CHANGED
package/src/types.ts
CHANGED
|
@@ -51,8 +51,8 @@ export type RequireAtLeastOne<T> = {
|
|
|
51
51
|
}[keyof T];
|
|
52
52
|
|
|
53
53
|
/**
|
|
54
|
-
*
|
|
55
|
-
*
|
|
54
|
+
* The return type of `StreamChat.unbanUser`, kept so its signature is unchanged. Its v1 fallback
|
|
55
|
+
* (`remove_future_channels_ban`) is the one hand-written `/moderation/*` call left.
|
|
56
56
|
*/
|
|
57
57
|
export type APIResponse = {
|
|
58
58
|
duration: string;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { ChannelResponse } from '../gen/models';
|
|
2
|
+
|
|
3
|
+
export const getCarriedActiveMemberCount = ({
|
|
4
|
+
next,
|
|
5
|
+
previous,
|
|
6
|
+
}: {
|
|
7
|
+
next: Partial<ChannelResponse>;
|
|
8
|
+
previous: Partial<ChannelResponse> | undefined;
|
|
9
|
+
}) => {
|
|
10
|
+
const { active_member_count: nextActiveMemberCount } = next;
|
|
11
|
+
|
|
12
|
+
const {
|
|
13
|
+
active_member_count: previousActiveMemberCount,
|
|
14
|
+
member_count: previousMemberCount,
|
|
15
|
+
} = previous ?? {};
|
|
16
|
+
|
|
17
|
+
const hasOwnActiveMemberCount = typeof nextActiveMemberCount === 'number';
|
|
18
|
+
const hasPreviousActiveMemberCount = typeof previousActiveMemberCount === 'number';
|
|
19
|
+
if (hasOwnActiveMemberCount || !hasPreviousActiveMemberCount) return undefined;
|
|
20
|
+
|
|
21
|
+
// Only Query Channels returns the count, so between queries shift the last known value
|
|
22
|
+
// by the change in member_count (an estimate, clamped to [0, member_count]).
|
|
23
|
+
const memberCount = next.member_count ?? previousMemberCount;
|
|
24
|
+
const delta =
|
|
25
|
+
typeof next.member_count === 'number' && typeof previousMemberCount === 'number'
|
|
26
|
+
? next.member_count - previousMemberCount
|
|
27
|
+
: 0;
|
|
28
|
+
const shifted = Math.max(0, previousActiveMemberCount + delta);
|
|
29
|
+
return typeof memberCount === 'number' ? Math.min(shifted, memberCount) : shifted;
|
|
30
|
+
};
|
package/src/version.ts
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The parsed version of this `stream-chat` build.
|
|
3
|
+
*
|
|
4
|
+
* - `prerelease` is the prerelease identifier without its trailing `.N` (`'rc'` for
|
|
5
|
+
* `10.0.0-rc.35`; a numeric-only id such as `1.0.0-1` has no `.N` and stays `'1'`), or `null`
|
|
6
|
+
* for a stable release. It is a plain string because this package has
|
|
7
|
+
* shipped prereleases beyond `alpha` / `beta` / `rc` (`dev`, `insights`, `offline-support-beta`).
|
|
8
|
+
* - `prereleaseNumber` is the number of that trailing `.N` (`35`), or `null` when there is none.
|
|
9
|
+
* - `raw` is the version string exactly as it was given.
|
|
10
|
+
*
|
|
11
|
+
* Outside the esbuild bundles (unit tests, code importing `src/` directly) no version is injected,
|
|
12
|
+
* and the value is that of the `package.json` placeholder, `0.0.0-development`.
|
|
13
|
+
*/
|
|
14
|
+
export type StreamChatVersion = Readonly<{
|
|
15
|
+
major: number;
|
|
16
|
+
minor: number;
|
|
17
|
+
patch: number;
|
|
18
|
+
prerelease: string | null;
|
|
19
|
+
prereleaseNumber: number | null;
|
|
20
|
+
raw: string;
|
|
21
|
+
}>;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* `major.minor.patch`, an optional `-<id>` with its trailing `.N` split off (lazy, so `rc.35` gives
|
|
25
|
+
* `rc` and `35`, and `offline-support-beta.2` keeps its whole id), and an ignored `+<build>`.
|
|
26
|
+
*/
|
|
27
|
+
const SEMVER_PATTERN =
|
|
28
|
+
/^v?(\d+)\.(\d+)\.(\d+)(?:-([0-9A-Za-z.-]+?)(?:\.(\d+))?)?(?:\+[0-9A-Za-z.-]+)?$/;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Parses a version string into a {@link StreamChatVersion}. Never throws: a missing or empty value
|
|
32
|
+
* parses as the `package.json` placeholder, and an unparseable one gives zeros (the defaults below)
|
|
33
|
+
* with the input kept in `raw`.
|
|
34
|
+
*
|
|
35
|
+
* @internal
|
|
36
|
+
*/
|
|
37
|
+
export const parseVersion = (raw: string | undefined): StreamChatVersion => {
|
|
38
|
+
const input = raw || '0.0.0-development';
|
|
39
|
+
const [, major = 0, minor = 0, patch = 0, prerelease = null, prereleaseNumber] =
|
|
40
|
+
SEMVER_PATTERN.exec(input) ?? [];
|
|
41
|
+
|
|
42
|
+
return Object.freeze({
|
|
43
|
+
major: Number(major),
|
|
44
|
+
minor: Number(minor),
|
|
45
|
+
patch: Number(patch),
|
|
46
|
+
prerelease,
|
|
47
|
+
prereleaseNumber: prereleaseNumber ? Number(prereleaseNumber) : null,
|
|
48
|
+
raw: input,
|
|
49
|
+
});
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* The version esbuild's `define` injected, or `undefined` outside the bundles.
|
|
54
|
+
*
|
|
55
|
+
* Keep `process.env.PKG_VERSION` spelled out exactly: `define` replaces that expression and no
|
|
56
|
+
* other (a destructure or `globalThis.process` would ship the fallback). A `typeof process` guard
|
|
57
|
+
* would not survive the replacement either: it compiles to `typeof process !== 'undefined' ?
|
|
58
|
+
* "10.0.1" : undefined`, which is the fallback in every browser. A `try` keeps the literal while
|
|
59
|
+
* still letting an unbundled `src/` import in a browser (no `process`) load.
|
|
60
|
+
*
|
|
61
|
+
* @internal
|
|
62
|
+
*/
|
|
63
|
+
export const readInjectedVersion = (): string | undefined => {
|
|
64
|
+
try {
|
|
65
|
+
return process.env.PKG_VERSION;
|
|
66
|
+
} catch {
|
|
67
|
+
return undefined;
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* The bundle esbuild's `define` injected (`node-cjs`, `browser-cjs`, `browser-esm`), or `undefined`
|
|
73
|
+
* outside the bundles. Same spelling rule as {@link readInjectedVersion}.
|
|
74
|
+
*
|
|
75
|
+
* @internal
|
|
76
|
+
*/
|
|
77
|
+
export const readInjectedClientBundle = (): string | undefined => {
|
|
78
|
+
try {
|
|
79
|
+
return process.env.CLIENT_BUNDLE;
|
|
80
|
+
} catch {
|
|
81
|
+
return undefined;
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* The version of this `stream-chat` build, for feature detection between packages (a UI SDK
|
|
87
|
+
* checking which core it runs against). To find out whether one specific API exists, checking for
|
|
88
|
+
* the API itself (`'method' in client`) is more precise.
|
|
89
|
+
*
|
|
90
|
+
* Only the esbuild bundles carry the real version. Imported any other way (`src/` through an alias,
|
|
91
|
+
* a linked checkout) it is `0.0.0-development`, so `major === 0` means **unknown**, not old: a
|
|
92
|
+
* feature check should treat it as "assume the newest behaviour" rather than take a legacy path.
|
|
93
|
+
*
|
|
94
|
+
* Both calls are annotated: `#__PURE__` covers one call, not its arguments.
|
|
95
|
+
*/
|
|
96
|
+
export const version: StreamChatVersion = /* #__PURE__ */ parseVersion(
|
|
97
|
+
/* #__PURE__ */ readInjectedVersion(),
|
|
98
|
+
);
|