@xmtp/browser-sdk 0.0.1

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.
@@ -0,0 +1,689 @@
1
+ import type {
2
+ ClientEventsActions,
3
+ ClientEventsClientMessageData,
4
+ ClientEventsErrorData,
5
+ ClientEventsWorkerPostMessageData,
6
+ } from "@/types";
7
+ import {
8
+ fromEncodedContent,
9
+ fromSafeEncodedContent,
10
+ toSafeConversation,
11
+ toSafeGroupMember,
12
+ toSafeInboxState,
13
+ toSafeMessage,
14
+ } from "@/utils/conversions";
15
+ import { WorkerClient } from "@/WorkerClient";
16
+
17
+ let client: WorkerClient;
18
+ let enableLogging = false;
19
+
20
+ /**
21
+ * Type-safe postMessage
22
+ */
23
+ const postMessage = <A extends ClientEventsActions>(
24
+ data: ClientEventsWorkerPostMessageData<A>,
25
+ ) => {
26
+ self.postMessage(data);
27
+ };
28
+
29
+ /**
30
+ * Type-safe postMessage for errors
31
+ */
32
+ const postMessageError = (data: ClientEventsErrorData) => {
33
+ self.postMessage(data);
34
+ };
35
+
36
+ self.onmessage = async (event: MessageEvent<ClientEventsClientMessageData>) => {
37
+ const { action, id, data } = event.data;
38
+
39
+ if (enableLogging) {
40
+ console.log("client worker received event data", event.data);
41
+ }
42
+
43
+ // a client is required for all actions except init
44
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
45
+ if (action !== "init" && !client) {
46
+ postMessageError({
47
+ id,
48
+ action,
49
+ error: "Client not initialized",
50
+ });
51
+ return;
52
+ }
53
+
54
+ try {
55
+ switch (action) {
56
+ /**
57
+ * Client actions
58
+ */
59
+ case "init":
60
+ client = await WorkerClient.create(data.address, data.options);
61
+ enableLogging = data.options?.enableLogging ?? false;
62
+ postMessage({
63
+ id,
64
+ action,
65
+ result: {
66
+ inboxId: client.inboxId,
67
+ installationId: client.installationId,
68
+ },
69
+ });
70
+ break;
71
+ case "getCreateInboxSignatureText": {
72
+ const result = await client.getCreateInboxSignatureText();
73
+ postMessage({
74
+ id,
75
+ action,
76
+ result,
77
+ });
78
+ break;
79
+ }
80
+ case "getAddWalletSignatureText": {
81
+ const result = await client.getAddWalletSignatureText(
82
+ data.accountAddress,
83
+ );
84
+ postMessage({
85
+ id,
86
+ action,
87
+ result,
88
+ });
89
+ break;
90
+ }
91
+ case "getRevokeWalletSignatureText": {
92
+ const result = await client.getRevokeWalletSignatureText(
93
+ data.accountAddress,
94
+ );
95
+ postMessage({
96
+ id,
97
+ action,
98
+ result,
99
+ });
100
+ break;
101
+ }
102
+ case "addSignature":
103
+ await client.addSignature(data.type, data.bytes);
104
+ postMessage({
105
+ id,
106
+ action,
107
+ result: undefined,
108
+ });
109
+ break;
110
+ case "applySignaturesRequests":
111
+ await client.applySignaturesRequests();
112
+ postMessage({
113
+ id,
114
+ action,
115
+ result: undefined,
116
+ });
117
+ break;
118
+ case "registerIdentity":
119
+ await client.registerIdentity();
120
+ postMessage({
121
+ id,
122
+ action,
123
+ result: undefined,
124
+ });
125
+ break;
126
+ case "isRegistered": {
127
+ const result = client.isRegistered;
128
+ postMessage({
129
+ id,
130
+ action,
131
+ result,
132
+ });
133
+ break;
134
+ }
135
+ case "canMessage": {
136
+ const result = await client.canMessage(data.accountAddresses);
137
+ postMessage({
138
+ id,
139
+ action,
140
+ result,
141
+ });
142
+ break;
143
+ }
144
+ case "inboxState": {
145
+ const result = await client.inboxState(data.refreshFromNetwork);
146
+ postMessage({
147
+ id,
148
+ action,
149
+ result: toSafeInboxState(result),
150
+ });
151
+ break;
152
+ }
153
+ case "getLatestInboxState": {
154
+ const result = await client.getLatestInboxState(data.inboxId);
155
+ postMessage({
156
+ id,
157
+ action,
158
+ result: toSafeInboxState(result),
159
+ });
160
+ break;
161
+ }
162
+ case "setConsentStates": {
163
+ await client.setConsentStates(data.records);
164
+ postMessage({
165
+ id,
166
+ action,
167
+ result: undefined,
168
+ });
169
+ break;
170
+ }
171
+ case "getConsentState": {
172
+ const result = await client.getConsentState(
173
+ data.entityType,
174
+ data.entity,
175
+ );
176
+ postMessage({
177
+ id,
178
+ action,
179
+ result,
180
+ });
181
+ break;
182
+ }
183
+ case "findInboxIdByAddress": {
184
+ const result = await client.findInboxIdByAddress(data.address);
185
+ postMessage({
186
+ id,
187
+ action,
188
+ result,
189
+ });
190
+ break;
191
+ }
192
+ /**
193
+ * Conversations actions
194
+ */
195
+ case "getConversations": {
196
+ const conversations = await client.conversations.list(data.options);
197
+ postMessage({
198
+ id,
199
+ action,
200
+ result: conversations.map((conversation) =>
201
+ toSafeConversation(conversation),
202
+ ),
203
+ });
204
+ break;
205
+ }
206
+ case "newGroup": {
207
+ const conversation = await client.conversations.newGroup(
208
+ data.accountAddresses,
209
+ data.options,
210
+ );
211
+ postMessage({
212
+ id,
213
+ action,
214
+ result: toSafeConversation(conversation),
215
+ });
216
+ break;
217
+ }
218
+ case "syncConversations": {
219
+ await client.conversations.sync();
220
+ postMessage({
221
+ id,
222
+ action,
223
+ result: undefined,
224
+ });
225
+ break;
226
+ }
227
+ case "getConversationById": {
228
+ const conversation = client.conversations.getConversationById(data.id);
229
+ postMessage({
230
+ id,
231
+ action,
232
+ result: conversation ? toSafeConversation(conversation) : undefined,
233
+ });
234
+ break;
235
+ }
236
+ case "getMessageById": {
237
+ const message = client.conversations.getMessageById(data.id);
238
+ postMessage({
239
+ id,
240
+ action,
241
+ result: message,
242
+ });
243
+ break;
244
+ }
245
+ /**
246
+ * Group actions
247
+ */
248
+ case "syncGroup": {
249
+ const group = client.conversations.getConversationById(data.id);
250
+ if (group) {
251
+ await group.sync();
252
+ postMessage({
253
+ id,
254
+ action,
255
+ result: toSafeConversation(group),
256
+ });
257
+ } else {
258
+ postMessageError({
259
+ id,
260
+ action,
261
+ error: "Group not found",
262
+ });
263
+ }
264
+ break;
265
+ }
266
+ case "updateGroupName": {
267
+ const group = client.conversations.getConversationById(data.id);
268
+ if (group) {
269
+ await group.updateName(data.name);
270
+ postMessage({
271
+ id,
272
+ action,
273
+ result: undefined,
274
+ });
275
+ } else {
276
+ postMessageError({
277
+ id,
278
+ action,
279
+ error: "Group not found",
280
+ });
281
+ }
282
+ break;
283
+ }
284
+ case "updateGroupDescription": {
285
+ const group = client.conversations.getConversationById(data.id);
286
+ if (group) {
287
+ await group.updateDescription(data.description);
288
+ postMessage({
289
+ id,
290
+ action,
291
+ result: undefined,
292
+ });
293
+ } else {
294
+ postMessageError({
295
+ id,
296
+ action,
297
+ error: "Group not found",
298
+ });
299
+ }
300
+ break;
301
+ }
302
+ case "updateGroupImageUrlSquare": {
303
+ const group = client.conversations.getConversationById(data.id);
304
+ if (group) {
305
+ await group.updateImageUrl(data.imageUrl);
306
+ postMessage({
307
+ id,
308
+ action,
309
+ result: undefined,
310
+ });
311
+ } else {
312
+ postMessageError({
313
+ id,
314
+ action,
315
+ error: "Group not found",
316
+ });
317
+ }
318
+ break;
319
+ }
320
+ case "updateGroupPinnedFrameUrl": {
321
+ const group = client.conversations.getConversationById(data.id);
322
+ if (group) {
323
+ await group.updatePinnedFrameUrl(data.pinnedFrameUrl);
324
+ postMessage({
325
+ id,
326
+ action,
327
+ result: undefined,
328
+ });
329
+ } else {
330
+ postMessageError({
331
+ id,
332
+ action,
333
+ error: "Group not found",
334
+ });
335
+ }
336
+ break;
337
+ }
338
+ case "sendGroupMessage": {
339
+ const group = client.conversations.getConversationById(data.id);
340
+ if (group) {
341
+ const result = await group.send(
342
+ fromEncodedContent(fromSafeEncodedContent(data.content)),
343
+ );
344
+ postMessage({
345
+ id,
346
+ action,
347
+ result,
348
+ });
349
+ } else {
350
+ postMessageError({
351
+ id,
352
+ action,
353
+ error: "Group not found",
354
+ });
355
+ }
356
+ break;
357
+ }
358
+ case "sendOptimisticGroupMessage": {
359
+ const group = client.conversations.getConversationById(data.id);
360
+ if (group) {
361
+ const result = group.sendOptimistic(
362
+ fromEncodedContent(fromSafeEncodedContent(data.content)),
363
+ );
364
+ postMessage({
365
+ id,
366
+ action,
367
+ result,
368
+ });
369
+ } else {
370
+ postMessageError({
371
+ id,
372
+ action,
373
+ error: "Group not found",
374
+ });
375
+ }
376
+ break;
377
+ }
378
+ case "publishGroupMessages": {
379
+ const group = client.conversations.getConversationById(data.id);
380
+ if (group) {
381
+ await group.publishMessages();
382
+ postMessage({
383
+ id,
384
+ action,
385
+ result: undefined,
386
+ });
387
+ } else {
388
+ postMessageError({
389
+ id,
390
+ action,
391
+ error: "Group not found",
392
+ });
393
+ }
394
+ break;
395
+ }
396
+ case "getGroupMessages": {
397
+ const group = client.conversations.getConversationById(data.id);
398
+ if (group) {
399
+ const messages = group.messages(data.options);
400
+ postMessage({
401
+ id,
402
+ action,
403
+ result: messages.map((message) => toSafeMessage(message)),
404
+ });
405
+ } else {
406
+ postMessageError({
407
+ id,
408
+ action,
409
+ error: "Group not found",
410
+ });
411
+ }
412
+ break;
413
+ }
414
+ case "getGroupMembers": {
415
+ const group = client.conversations.getConversationById(data.id);
416
+ if (group) {
417
+ const members = await group.members();
418
+ postMessage({
419
+ id,
420
+ action,
421
+ result: members.map((member) => toSafeGroupMember(member)),
422
+ });
423
+ } else {
424
+ postMessageError({
425
+ id,
426
+ action,
427
+ error: "Group not found",
428
+ });
429
+ }
430
+ break;
431
+ }
432
+ case "getGroupAdmins": {
433
+ const group = client.conversations.getConversationById(data.id);
434
+ if (group) {
435
+ postMessage({
436
+ id,
437
+ action,
438
+ result: group.admins,
439
+ });
440
+ } else {
441
+ postMessageError({
442
+ id,
443
+ action,
444
+ error: "Group not found",
445
+ });
446
+ }
447
+ break;
448
+ }
449
+ case "getGroupSuperAdmins": {
450
+ const group = client.conversations.getConversationById(data.id);
451
+ if (group) {
452
+ postMessage({
453
+ id,
454
+ action,
455
+ result: group.superAdmins,
456
+ });
457
+ } else {
458
+ postMessageError({
459
+ id,
460
+ action,
461
+ error: "Group not found",
462
+ });
463
+ }
464
+ break;
465
+ }
466
+ case "getGroupConsentState": {
467
+ const group = client.conversations.getConversationById(data.id);
468
+ if (group) {
469
+ postMessage({
470
+ id,
471
+ action,
472
+ result: group.consentState,
473
+ });
474
+ } else {
475
+ postMessageError({
476
+ id,
477
+ action,
478
+ error: "Group not found",
479
+ });
480
+ }
481
+ break;
482
+ }
483
+ case "updateGroupConsentState": {
484
+ const group = client.conversations.getConversationById(data.id);
485
+ if (group) {
486
+ group.updateConsentState(data.state);
487
+ postMessage({
488
+ id,
489
+ action,
490
+ result: undefined,
491
+ });
492
+ } else {
493
+ postMessageError({
494
+ id,
495
+ action,
496
+ error: "Group not found",
497
+ });
498
+ }
499
+ break;
500
+ }
501
+ case "addGroupAdmin": {
502
+ const group = client.conversations.getConversationById(data.id);
503
+ if (group) {
504
+ await group.addAdmin(data.inboxId);
505
+ postMessage({
506
+ id,
507
+ action,
508
+ result: undefined,
509
+ });
510
+ } else {
511
+ postMessageError({
512
+ id,
513
+ action,
514
+ error: "Group not found",
515
+ });
516
+ }
517
+ break;
518
+ }
519
+ case "removeGroupAdmin": {
520
+ const group = client.conversations.getConversationById(data.id);
521
+ if (group) {
522
+ await group.removeAdmin(data.inboxId);
523
+ postMessage({
524
+ id,
525
+ action,
526
+ result: undefined,
527
+ });
528
+ } else {
529
+ postMessageError({
530
+ id,
531
+ action,
532
+ error: "Group not found",
533
+ });
534
+ }
535
+ break;
536
+ }
537
+ case "addGroupSuperAdmin": {
538
+ const group = client.conversations.getConversationById(data.id);
539
+ if (group) {
540
+ await group.addSuperAdmin(data.inboxId);
541
+ postMessage({
542
+ id,
543
+ action,
544
+ result: undefined,
545
+ });
546
+ } else {
547
+ postMessageError({
548
+ id,
549
+ action,
550
+ error: "Group not found",
551
+ });
552
+ }
553
+ break;
554
+ }
555
+ case "removeGroupSuperAdmin": {
556
+ const group = client.conversations.getConversationById(data.id);
557
+ if (group) {
558
+ await group.removeSuperAdmin(data.inboxId);
559
+ postMessage({
560
+ id,
561
+ action,
562
+ result: undefined,
563
+ });
564
+ } else {
565
+ postMessageError({
566
+ id,
567
+ action,
568
+ error: "Group not found",
569
+ });
570
+ }
571
+ break;
572
+ }
573
+ case "addGroupMembers": {
574
+ const group = client.conversations.getConversationById(data.id);
575
+ if (group) {
576
+ await group.addMembers(data.accountAddresses);
577
+ postMessage({
578
+ id,
579
+ action,
580
+ result: undefined,
581
+ });
582
+ } else {
583
+ postMessageError({
584
+ id,
585
+ action,
586
+ error: "Group not found",
587
+ });
588
+ }
589
+ break;
590
+ }
591
+ case "removeGroupMembers": {
592
+ const group = client.conversations.getConversationById(data.id);
593
+ if (group) {
594
+ await group.removeMembers(data.accountAddresses);
595
+ postMessage({
596
+ id,
597
+ action,
598
+ result: undefined,
599
+ });
600
+ } else {
601
+ postMessageError({
602
+ id,
603
+ action,
604
+ error: "Group not found",
605
+ });
606
+ }
607
+ break;
608
+ }
609
+ case "addGroupMembersByInboxId": {
610
+ const group = client.conversations.getConversationById(data.id);
611
+ if (group) {
612
+ await group.addMembersByInboxId(data.inboxIds);
613
+ postMessage({
614
+ id,
615
+ action,
616
+ result: undefined,
617
+ });
618
+ } else {
619
+ postMessageError({
620
+ id,
621
+ action,
622
+ error: "Group not found",
623
+ });
624
+ }
625
+ break;
626
+ }
627
+ case "removeGroupMembersByInboxId": {
628
+ const group = client.conversations.getConversationById(data.id);
629
+ if (group) {
630
+ await group.removeMembersByInboxId(data.inboxIds);
631
+ postMessage({
632
+ id,
633
+ action,
634
+ result: undefined,
635
+ });
636
+ } else {
637
+ postMessageError({
638
+ id,
639
+ action,
640
+ error: "Group not found",
641
+ });
642
+ }
643
+ break;
644
+ }
645
+ case "isGroupAdmin": {
646
+ const group = client.conversations.getConversationById(data.id);
647
+ if (group) {
648
+ const result = group.isAdmin(data.inboxId);
649
+ postMessage({
650
+ id,
651
+ action,
652
+ result,
653
+ });
654
+ } else {
655
+ postMessageError({
656
+ id,
657
+ action,
658
+ error: "Group not found",
659
+ });
660
+ }
661
+ break;
662
+ }
663
+ case "isGroupSuperAdmin": {
664
+ const group = client.conversations.getConversationById(data.id);
665
+ if (group) {
666
+ const result = group.isSuperAdmin(data.inboxId);
667
+ postMessage({
668
+ id,
669
+ action,
670
+ result,
671
+ });
672
+ } else {
673
+ postMessageError({
674
+ id,
675
+ action,
676
+ error: "Group not found",
677
+ });
678
+ }
679
+ break;
680
+ }
681
+ }
682
+ } catch (e) {
683
+ postMessageError({
684
+ id,
685
+ action,
686
+ error: (e as Error).message,
687
+ });
688
+ }
689
+ };