@xmtp/browser-sdk 2.0.12 → 2.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.
Files changed (44) hide show
  1. package/dist/index.d.ts +910 -735
  2. package/dist/index.js +1 -1
  3. package/dist/index.js.map +1 -1
  4. package/dist/workers/client.js +1 -1
  5. package/dist/workers/client.js.map +1 -1
  6. package/dist/workers/utils.js +1 -1
  7. package/dist/workers/utils.js.map +1 -1
  8. package/package.json +9 -11
  9. package/src/AsyncStream.ts +3 -1
  10. package/src/Client.ts +71 -31
  11. package/src/ClientWorkerClass.ts +62 -19
  12. package/src/Conversation.ts +60 -33
  13. package/src/Conversations.ts +96 -48
  14. package/src/DecodedMessage.ts +8 -5
  15. package/src/Dm.ts +14 -4
  16. package/src/Group.ts +27 -20
  17. package/src/Preferences.ts +21 -10
  18. package/src/Utils.ts +2 -2
  19. package/src/UtilsWorkerClass.ts +56 -15
  20. package/src/WorkerClient.ts +25 -3
  21. package/src/WorkerConversation.ts +11 -2
  22. package/src/WorkerConversations.ts +19 -4
  23. package/src/WorkerPreferences.ts +4 -0
  24. package/src/index.ts +4 -1
  25. package/src/types/actions/client.ts +181 -0
  26. package/src/types/actions/conversation.ts +146 -0
  27. package/src/types/actions/conversations.ts +146 -0
  28. package/src/types/actions/dm.ts +19 -0
  29. package/src/types/actions/group.ts +161 -0
  30. package/src/types/actions/preferences.ts +68 -0
  31. package/src/types/actions/streams.ts +44 -0
  32. package/src/types/actions/utils.ts +29 -0
  33. package/src/types/actions.ts +75 -0
  34. package/src/types/options.ts +18 -0
  35. package/src/utils/conversions.ts +60 -0
  36. package/src/utils/createClient.ts +6 -1
  37. package/src/utils/errors.ts +3 -1
  38. package/src/workers/client.ts +243 -190
  39. package/src/workers/utils.ts +25 -29
  40. package/src/types/clientEvents.ts +0 -693
  41. package/src/types/clientStreamEvents.ts +0 -45
  42. package/src/types/index.ts +0 -4
  43. package/src/types/utils.ts +0 -72
  44. package/src/types/utilsEvents.ts +0 -60
@@ -0,0 +1,44 @@
1
+ import type { UserPreference } from "@xmtp/wasm-bindings";
2
+ import type {
3
+ SafeConsent,
4
+ SafeConversation,
5
+ SafeMessage,
6
+ } from "@/utils/conversions";
7
+
8
+ export type StreamAction =
9
+ | {
10
+ action: "stream.message";
11
+ streamId: string;
12
+ result: SafeMessage | undefined;
13
+ }
14
+ | {
15
+ action: "stream.conversation";
16
+ streamId: string;
17
+ result: SafeConversation | undefined;
18
+ }
19
+ | {
20
+ action: "stream.consent";
21
+ streamId: string;
22
+ result: SafeConsent[] | undefined;
23
+ }
24
+ | {
25
+ action: "stream.preferences";
26
+ streamId: string;
27
+ result: UserPreference[] | undefined;
28
+ };
29
+
30
+ export type StreamActionName = StreamAction["action"];
31
+
32
+ export type ExtractStreamAction<A extends StreamActionName> = Extract<
33
+ StreamAction,
34
+ { action: A }
35
+ >;
36
+
37
+ export type StreamActionResult<A extends StreamActionName> =
38
+ ExtractStreamAction<A>["result"];
39
+
40
+ export type StreamActionErrorData = {
41
+ action: StreamActionName;
42
+ error: Error;
43
+ streamId: string;
44
+ };
@@ -0,0 +1,29 @@
1
+ import type { Identifier } from "@xmtp/wasm-bindings";
2
+ import type { XmtpEnv } from "@/types/options";
3
+
4
+ export type UtilsWorkerAction =
5
+ | {
6
+ action: "utils.init";
7
+ id: string;
8
+ result: undefined;
9
+ data: {
10
+ enableLogging: boolean;
11
+ };
12
+ }
13
+ | {
14
+ action: "utils.generateInboxId";
15
+ id: string;
16
+ result: string;
17
+ data: {
18
+ identifier: Identifier;
19
+ };
20
+ }
21
+ | {
22
+ action: "utils.getInboxIdForIdentifier";
23
+ id: string;
24
+ result: string | undefined;
25
+ data: {
26
+ identifier: Identifier;
27
+ env?: XmtpEnv;
28
+ };
29
+ };
@@ -0,0 +1,75 @@
1
+ import type { ClientAction } from "@/types/actions/client";
2
+ import type { ConversationAction } from "@/types/actions/conversation";
3
+ import type { ConversationsAction } from "@/types/actions/conversations";
4
+ import type { DmAction } from "@/types/actions/dm";
5
+ import type { GroupAction } from "@/types/actions/group";
6
+ import type { PreferencesAction } from "@/types/actions/preferences";
7
+
8
+ type UnknownAction = {
9
+ action: string;
10
+ id: string;
11
+ result: unknown;
12
+ data: unknown;
13
+ };
14
+
15
+ export type ClientWorkerAction =
16
+ | {
17
+ action: "endStream";
18
+ id: string;
19
+ result: undefined;
20
+ data: {
21
+ streamId: string;
22
+ };
23
+ }
24
+ | ClientAction
25
+ | ConversationAction
26
+ | ConversationsAction
27
+ | DmAction
28
+ | GroupAction
29
+ | PreferencesAction;
30
+
31
+ export type ActionName<T extends UnknownAction> = T["action"];
32
+
33
+ export type ExtractAction<
34
+ T extends UnknownAction,
35
+ A extends ActionName<T>,
36
+ > = Extract<T, { action: A }>;
37
+
38
+ export type ExtractActionWithoutData<
39
+ T extends UnknownAction,
40
+ A extends ActionName<T>,
41
+ > = Omit<ExtractAction<T, A>, "data">;
42
+
43
+ export type ExtractActionWithoutResult<
44
+ T extends UnknownAction,
45
+ A extends ActionName<T>,
46
+ > = Omit<ExtractAction<T, A>, "result">;
47
+
48
+ export type ExtractActionData<
49
+ T extends UnknownAction,
50
+ A extends ActionName<T>,
51
+ > = ExtractAction<T, A>["data"];
52
+
53
+ export type ExtractActionResult<
54
+ T extends UnknownAction,
55
+ A extends ActionName<T>,
56
+ > = ExtractAction<T, A>["result"];
57
+
58
+ export type ActionWithoutData<T extends UnknownAction> = {
59
+ [A in T["action"]]: Omit<Extract<T, { action: A }>, "data">;
60
+ }[T["action"]];
61
+
62
+ export type ActionWithoutResult<T extends UnknownAction> = {
63
+ [A in T["action"]]: Omit<Extract<T, { action: A }>, "result">;
64
+ }[T["action"]];
65
+
66
+ export type ActionErrorData<T extends UnknownAction> = {
67
+ id: string;
68
+ action: ActionName<T>;
69
+ error: Error;
70
+ };
71
+
72
+ export type ExtractActionGroup<
73
+ T extends UnknownAction,
74
+ U extends string,
75
+ > = Extract<T, { action: `${U}.${string}` }>;
@@ -36,6 +36,20 @@ export type ContentOptions = {
36
36
  export type StorageOptions = {
37
37
  /**
38
38
  * Path to the local DB
39
+ *
40
+ * There are 3 value types that can be used to specify the database path:
41
+ *
42
+ * - `undefined` (or excluded from the client options)
43
+ * The database will be created in the current working directory and is based on
44
+ * the XMTP environment and client inbox ID.
45
+ * Example: `xmtp-dev-<inbox-id>.db3`
46
+ *
47
+ * - `null`
48
+ * No database will be created and all data will be lost once the client disconnects.
49
+ *
50
+ * - `string`
51
+ * The given path will be used to create the database.
52
+ * Example: `./my-db.db3`
39
53
  */
40
54
  dbPath?: string | null;
41
55
  /**
@@ -61,6 +75,10 @@ export type OtherOptions = {
61
75
  * Disable automatic registration when creating a client
62
76
  */
63
77
  disableAutoRegister?: boolean;
78
+ /**
79
+ * Disable device sync
80
+ */
81
+ disableDeviceSync?: boolean;
64
82
  };
65
83
 
66
84
  export type ClientOptions = NetworkOptions &
@@ -14,13 +14,17 @@ import {
14
14
  PermissionPolicySet,
15
15
  ContentTypeId as WasmContentTypeId,
16
16
  EncodedContent as WasmEncodedContent,
17
+ type ApiStats,
17
18
  type ConsentEntityType,
18
19
  type ConsentState,
19
20
  type ContentType,
21
+ type ConversationDebugInfo,
22
+ type ConversationType,
20
23
  type DeliveryStatus,
21
24
  type GroupMessageKind,
22
25
  type HmacKey,
23
26
  type Identifier,
27
+ type IdentityStats,
24
28
  type InboxState,
25
29
  type Installation,
26
30
  type KeyPackageStatus,
@@ -181,6 +185,7 @@ export const fromSafeListMessagesOptions = (
181
185
 
182
186
  export type SafeListConversationsOptions = {
183
187
  consentStates?: ConsentState[];
188
+ conversationType?: ConversationType;
184
189
  createdAfterNs?: bigint;
185
190
  createdBeforeNs?: bigint;
186
191
  includeDuplicateDms?: boolean;
@@ -191,6 +196,7 @@ export const toSafeListConversationsOptions = (
191
196
  options: ListConversationsOptions,
192
197
  ): SafeListConversationsOptions => ({
193
198
  consentStates: options.consentStates,
199
+ conversationType: options.conversationType,
194
200
  createdAfterNs: options.createdAfterNs,
195
201
  createdBeforeNs: options.createdBeforeNs,
196
202
  includeDuplicateDms: options.includeDuplicateDms,
@@ -202,6 +208,7 @@ export const fromSafeListConversationsOptions = (
202
208
  ): ListConversationsOptions =>
203
209
  new ListConversationsOptions(
204
210
  options.consentStates,
211
+ options.conversationType,
205
212
  options.createdAfterNs,
206
213
  options.createdBeforeNs,
207
214
  options.includeDuplicateDms ?? false,
@@ -498,3 +505,56 @@ export const toSafeKeyPackageStatus = (
498
505
  : undefined,
499
506
  validationError: status.validationError,
500
507
  });
508
+
509
+ export type SafeConversationDebugInfo = {
510
+ epoch: bigint;
511
+ maybeForked: boolean;
512
+ forkDetails: string;
513
+ };
514
+
515
+ export const toSafeConversationDebugInfo = (
516
+ debugInfo: ConversationDebugInfo,
517
+ ): SafeConversationDebugInfo => ({
518
+ epoch: debugInfo.epoch,
519
+ maybeForked: debugInfo.maybeForked,
520
+ forkDetails: debugInfo.forkDetails,
521
+ });
522
+
523
+ export type SafeApiStats = {
524
+ fetchKeyPackage: bigint;
525
+ queryGroupMessages: bigint;
526
+ queryWelcomeMessages: bigint;
527
+ sendGroupMessages: bigint;
528
+ sendWelcomeMessages: bigint;
529
+ subscribeMessages: bigint;
530
+ subscribeWelcomes: bigint;
531
+ uploadKeyPackage: bigint;
532
+ };
533
+
534
+ export const toSafeApiStats = (stats: ApiStats): SafeApiStats => ({
535
+ uploadKeyPackage: stats.upload_key_package,
536
+ fetchKeyPackage: stats.fetch_key_package,
537
+ sendGroupMessages: stats.send_group_messages,
538
+ sendWelcomeMessages: stats.send_welcome_messages,
539
+ queryGroupMessages: stats.query_group_messages,
540
+ queryWelcomeMessages: stats.query_welcome_messages,
541
+ subscribeMessages: stats.subscribe_messages,
542
+ subscribeWelcomes: stats.subscribe_welcomes,
543
+ });
544
+
545
+ export type SafeIdentityStats = {
546
+ getIdentityUpdatesV2: bigint;
547
+ getInboxIds: bigint;
548
+ publishIdentityUpdate: bigint;
549
+ verifySmartContractWalletSignature: bigint;
550
+ };
551
+
552
+ export const toSafeIdentityStats = (
553
+ stats: IdentityStats,
554
+ ): SafeIdentityStats => ({
555
+ getIdentityUpdatesV2: stats.get_identity_updates_v2,
556
+ getInboxIds: stats.get_inbox_ids,
557
+ publishIdentityUpdate: stats.publish_identity_update,
558
+ verifySmartContractWalletSignature:
559
+ stats.verify_smart_contract_wallet_signature,
560
+ });
@@ -6,7 +6,7 @@ import {
6
6
  type Identifier,
7
7
  } from "@xmtp/wasm-bindings";
8
8
  import { ApiUrls, HistorySyncUrls } from "@/constants";
9
- import type { ClientOptions } from "@/types";
9
+ import type { ClientOptions } from "@/types/options";
10
10
 
11
11
  export const createClient = async (
12
12
  identifier: Identifier,
@@ -32,6 +32,10 @@ export const createClient = async (
32
32
  ? HistorySyncUrls[env]
33
33
  : options.historySyncUrl;
34
34
 
35
+ const deviceSyncWorkerMode = options?.disableDeviceSync
36
+ ? "disabled"
37
+ : "enabled";
38
+
35
39
  return createWasmClient(
36
40
  host,
37
41
  inboxId,
@@ -39,6 +43,7 @@ export const createClient = async (
39
43
  dbPath,
40
44
  options?.dbEncryptionKey,
41
45
  historySyncUrl,
46
+ deviceSyncWorkerMode,
42
47
  isLogging
43
48
  ? new LogOptions(
44
49
  options.structuredLogging ?? false,
@@ -3,7 +3,9 @@ import { SignatureRequestType } from "@xmtp/wasm-bindings";
3
3
 
4
4
  export class ClientNotInitializedError extends Error {
5
5
  constructor() {
6
- super("Client not initialized");
6
+ super(
7
+ "Client not initialized, use Client.create or Client.build to create a client",
8
+ );
7
9
  }
8
10
  }
9
11