From a35afb831874037e0cf843e80b3ed77a962eba2a Mon Sep 17 00:00:00 2001 From: Ry Racherbaumer Date: Wed, 18 Dec 2024 15:28:05 -0600 Subject: [PATCH] Upgrade bindings (#756) * Upgrade bindings * Refactor some methods to be async * Create four-bats-scream.md --- .changeset/four-bats-scream.md | 6 ++++ sdks/browser-sdk/package.json | 2 +- sdks/browser-sdk/src/WorkerConversation.ts | 6 ++-- sdks/browser-sdk/src/utils/conversions.ts | 6 ++-- sdks/browser-sdk/src/workers/client.ts | 34 ++++++++++++++-------- sdks/node-sdk/package.json | 2 +- sdks/node-sdk/src/Conversation.ts | 10 +++---- sdks/node-sdk/test/Conversation.test.ts | 30 ++++++++++--------- sdks/node-sdk/test/Conversations.test.ts | 10 +++---- yarn.lock | 20 ++++++------- 10 files changed, 73 insertions(+), 53 deletions(-) create mode 100644 .changeset/four-bats-scream.md diff --git a/.changeset/four-bats-scream.md b/.changeset/four-bats-scream.md new file mode 100644 index 000000000..653532740 --- /dev/null +++ b/.changeset/four-bats-scream.md @@ -0,0 +1,6 @@ +--- +"@xmtp/browser-sdk": patch +"@xmtp/node-sdk": patch +--- + +Upgrade bindings diff --git a/sdks/browser-sdk/package.json b/sdks/browser-sdk/package.json index 065b24589..17c9a8031 100644 --- a/sdks/browser-sdk/package.json +++ b/sdks/browser-sdk/package.json @@ -64,7 +64,7 @@ "@xmtp/content-type-primitives": "^2.0.0", "@xmtp/content-type-text": "^2.0.0", "@xmtp/proto": "^3.72.3", - "@xmtp/wasm-bindings": "^0.0.8", + "@xmtp/wasm-bindings": "^0.0.9", "uuid": "^11.0.3" }, "devDependencies": { diff --git a/sdks/browser-sdk/src/WorkerConversation.ts b/sdks/browser-sdk/src/WorkerConversation.ts index 4d7309ba7..241dca5c1 100644 --- a/sdks/browser-sdk/src/WorkerConversation.ts +++ b/sdks/browser-sdk/src/WorkerConversation.ts @@ -70,8 +70,8 @@ export class WorkerConversation { return this.#group.createdAtNs(); } - get metadata() { - const metadata = this.#group.groupMetadata(); + async metadata() { + const metadata = await this.#group.groupMetadata(); return { creatorInboxId: metadata.creatorInboxId(), conversationType: metadata.conversationType(), @@ -155,7 +155,7 @@ export class WorkerConversation { return this.#group.send(encodedContent); } - messages(options?: SafeListMessagesOptions) { + async messages(options?: SafeListMessagesOptions) { return this.#group.findMessages( options ? fromSafeListMessagesOptions(options) : undefined, ); diff --git a/sdks/browser-sdk/src/utils/conversions.ts b/sdks/browser-sdk/src/utils/conversions.ts index e0399777e..8f93031d6 100644 --- a/sdks/browser-sdk/src/utils/conversions.ts +++ b/sdks/browser-sdk/src/utils/conversions.ts @@ -305,9 +305,9 @@ export type SafeConversation = { createdAtNs: bigint; }; -export const toSafeConversation = ( +export const toSafeConversation = async ( conversation: WorkerConversation, -): SafeConversation => ({ +): Promise => ({ id: conversation.id, name: conversation.name, imageUrl: conversation.imageUrl, @@ -332,7 +332,7 @@ export const toSafeConversation = ( }, isActive: conversation.isActive, addedByInboxId: conversation.addedByInboxId, - metadata: conversation.metadata, + metadata: await conversation.metadata(), admins: conversation.admins, superAdmins: conversation.superAdmins, createdAtNs: conversation.createdAtNs, diff --git a/sdks/browser-sdk/src/workers/client.ts b/sdks/browser-sdk/src/workers/client.ts index 953f7b9d8..be5c758ad 100644 --- a/sdks/browser-sdk/src/workers/client.ts +++ b/sdks/browser-sdk/src/workers/client.ts @@ -259,8 +259,10 @@ self.onmessage = async (event: MessageEvent) => { postMessage({ id, action, - result: conversations.map((conversation) => - toSafeConversation(conversation), + result: await Promise.all( + conversations.map((conversation) => + toSafeConversation(conversation), + ), ), }); break; @@ -272,8 +274,10 @@ self.onmessage = async (event: MessageEvent) => { postMessage({ id, action, - result: conversations.map((conversation) => - toSafeConversation(conversation), + result: await Promise.all( + conversations.map((conversation) => + toSafeConversation(conversation), + ), ), }); break; @@ -283,8 +287,10 @@ self.onmessage = async (event: MessageEvent) => { postMessage({ id, action, - result: conversations.map((conversation) => - toSafeConversation(conversation), + result: await Promise.all( + conversations.map((conversation) => + toSafeConversation(conversation), + ), ), }); break; @@ -302,7 +308,7 @@ self.onmessage = async (event: MessageEvent) => { postMessage({ id, action, - result: toSafeConversation(conversation), + result: await toSafeConversation(conversation), }); break; } @@ -313,7 +319,7 @@ self.onmessage = async (event: MessageEvent) => { postMessage({ id, action, - result: toSafeConversation(conversation), + result: await toSafeConversation(conversation), }); break; } @@ -340,7 +346,9 @@ self.onmessage = async (event: MessageEvent) => { postMessage({ id, action, - result: conversation ? toSafeConversation(conversation) : undefined, + result: conversation + ? await toSafeConversation(conversation) + : undefined, }); break; } @@ -358,7 +366,9 @@ self.onmessage = async (event: MessageEvent) => { postMessage({ id, action, - result: conversation ? toSafeConversation(conversation) : undefined, + result: conversation + ? await toSafeConversation(conversation) + : undefined, }); break; } @@ -372,7 +382,7 @@ self.onmessage = async (event: MessageEvent) => { postMessage({ id, action, - result: toSafeConversation(group), + result: await toSafeConversation(group), }); } else { postMessageError({ @@ -516,7 +526,7 @@ self.onmessage = async (event: MessageEvent) => { case "getGroupMessages": { const group = client.conversations.getConversationById(data.id); if (group) { - const messages = group.messages(data.options); + const messages = await group.messages(data.options); postMessage({ id, action, diff --git a/sdks/node-sdk/package.json b/sdks/node-sdk/package.json index 11ef4ecd0..169776e5d 100644 --- a/sdks/node-sdk/package.json +++ b/sdks/node-sdk/package.json @@ -51,7 +51,7 @@ "@xmtp/content-type-group-updated": "^2.0.0", "@xmtp/content-type-primitives": "^2.0.0", "@xmtp/content-type-text": "^2.0.0", - "@xmtp/node-bindings": "^0.0.29", + "@xmtp/node-bindings": "^0.0.30", "@xmtp/proto": "^3.72.3" }, "devDependencies": { diff --git a/sdks/node-sdk/src/Conversation.ts b/sdks/node-sdk/src/Conversation.ts index 4d5b1e2fc..241b43a33 100644 --- a/sdks/node-sdk/src/Conversation.ts +++ b/sdks/node-sdk/src/Conversation.ts @@ -71,8 +71,8 @@ export class Conversation { return nsToDate(this.createdAtNs); } - get metadata() { - const metadata = this.#group.groupMetadata(); + async metadata() { + const metadata = await this.#group.groupMetadata(); return { creatorInboxId: metadata.creatorInboxId(), conversationType: metadata.conversationType(), @@ -194,10 +194,10 @@ export class Conversation { return this.#group.send(encodedContent); } - messages(options?: ListMessagesOptions): DecodedMessage[] { + async messages(options?: ListMessagesOptions): Promise { + const messages = await this.#group.findMessages(options); return ( - this.#group - .findMessages(options) + messages .map((message) => new DecodedMessage(this.#client, message)) // filter out messages without content .filter((message) => message.content !== undefined) diff --git a/sdks/node-sdk/test/Conversation.test.ts b/sdks/node-sdk/test/Conversation.test.ts index 63f8c3b90..6f7943b8a 100644 --- a/sdks/node-sdk/test/Conversation.test.ts +++ b/sdks/node-sdk/test/Conversation.test.ts @@ -19,7 +19,7 @@ describe("Conversation", () => { const newName = "foo"; await conversation.updateName(newName); expect(conversation.name).toBe(newName); - const messages = conversation.messages(); + const messages = await conversation.messages(); expect(messages.length).toBe(2); await client2.conversations.sync(); @@ -31,7 +31,8 @@ describe("Conversation", () => { await conversation2.sync(); expect(conversation2.id).toBe(conversation.id); expect(conversation2.name).toBe(newName); - expect(conversation2.messages().length).toBe(1); + const messages2 = await conversation2.messages(); + expect(messages2.length).toBe(1); }); it("should update conversation image URL", async () => { @@ -45,7 +46,7 @@ describe("Conversation", () => { const imageUrl = "https://foo/bar.jpg"; await conversation.updateImageUrl(imageUrl); expect(conversation.imageUrl).toBe(imageUrl); - const messages = conversation.messages(); + const messages = await conversation.messages(); expect(messages.length).toBe(2); await client2.conversations.sync(); @@ -57,7 +58,8 @@ describe("Conversation", () => { await conversation2.sync(); expect(conversation2.id).toBe(conversation.id); expect(conversation2.imageUrl).toBe(imageUrl); - expect(conversation2.messages().length).toBe(1); + const messages2 = await conversation2.messages(); + expect(messages2.length).toBe(1); }); it("should update conversation description", async () => { @@ -71,7 +73,7 @@ describe("Conversation", () => { const newDescription = "foo"; await conversation.updateDescription(newDescription); expect(conversation.description).toBe(newDescription); - const messages = conversation.messages(); + const messages = await conversation.messages(); expect(messages.length).toBe(2); await client2.conversations.sync(); @@ -83,7 +85,8 @@ describe("Conversation", () => { await conversation2.sync(); expect(conversation2.id).toBe(conversation.id); expect(conversation2.description).toBe(newDescription); - expect(conversation2.messages().length).toBe(1); + const messages2 = await conversation2.messages(); + expect(messages2.length).toBe(1); }); it("should update conversation pinned frame URL", async () => { @@ -97,7 +100,7 @@ describe("Conversation", () => { const pinnedFrameUrl = "https://foo/bar"; await conversation.updatePinnedFrameUrl(pinnedFrameUrl); expect(conversation.pinnedFrameUrl).toBe(pinnedFrameUrl); - const messages = conversation.messages(); + const messages = await conversation.messages(); expect(messages.length).toBe(2); await client2.conversations.sync(); @@ -109,7 +112,8 @@ describe("Conversation", () => { await conversation2.sync(); expect(conversation2.id).toBe(conversation.id); expect(conversation2.pinnedFrameUrl).toBe(pinnedFrameUrl); - expect(conversation2.messages().length).toBe(1); + const messages2 = await conversation2.messages(); + expect(messages2.length).toBe(1); }); it("should add and remove members", async () => { @@ -201,7 +205,7 @@ describe("Conversation", () => { const text = "gm"; await conversation.send(text); - const messages = conversation.messages(); + const messages = await conversation.messages(); expect(messages.length).toBe(2); expect(messages[1].content).toBe(text); @@ -214,7 +218,7 @@ describe("Conversation", () => { await conversation2.sync(); expect(conversation2.id).toBe(conversation.id); - const messages2 = conversation2.messages(); + const messages2 = await conversation2.messages(); expect(messages2.length).toBe(1); expect(messages2[0].content).toBe(text); }); @@ -249,7 +253,7 @@ describe("Conversation", () => { const text = "gm"; conversation.sendOptimistic(text); - const messages = conversation.messages(); + const messages = await conversation.messages(); expect(messages.length).toBe(2); expect(messages[1].content).toBe(text); @@ -263,13 +267,13 @@ describe("Conversation", () => { await conversation2.sync(); expect(conversation2.id).toBe(conversation.id); - const messages2 = conversation2.messages(); + const messages2 = await conversation2.messages(); expect(messages2.length).toBe(0); await conversation.publishMessages(); await conversation2.sync(); - const messages4 = conversation2.messages(); + const messages4 = await conversation2.messages(); expect(messages4.length).toBe(1); expect(messages4[0].content).toBe(text); }); diff --git a/sdks/node-sdk/test/Conversations.test.ts b/sdks/node-sdk/test/Conversations.test.ts index f28ddd7db..c881e6625 100644 --- a/sdks/node-sdk/test/Conversations.test.ts +++ b/sdks/node-sdk/test/Conversations.test.ts @@ -43,14 +43,14 @@ describe("Conversations", () => { updateGroupPinnedFrameUrlPolicy: 0, }); expect(conversation.addedByInboxId).toBe(client1.inboxId); - expect(conversation.messages().length).toBe(1); + expect((await conversation.messages()).length).toBe(1); const members = await conversation.members(); expect(members.length).toBe(2); const memberInboxIds = members.map((member) => member.inboxId); expect(memberInboxIds).toContain(client1.inboxId); expect(memberInboxIds).toContain(client2.inboxId); - expect(conversation.metadata).toEqual({ + expect(await conversation.metadata()).toEqual({ conversationType: "group", creatorInboxId: client1.inboxId, }); @@ -97,14 +97,14 @@ describe("Conversations", () => { updateGroupPinnedFrameUrlPolicy: 0, }); expect(group.addedByInboxId).toBe(client1.inboxId); - expect(group.messages().length).toBe(0); + expect((await group.messages()).length).toBe(0); const members = await group.members(); expect(members.length).toBe(2); const memberInboxIds = members.map((member) => member.inboxId); expect(memberInboxIds).toContain(client1.inboxId); expect(memberInboxIds).toContain(client2.inboxId); - expect(group.metadata.conversationType).toBe("dm"); - expect(group.metadata.creatorInboxId).toBe(client1.inboxId); + expect((await group.metadata()).conversationType).toBe("dm"); + expect((await group.metadata()).creatorInboxId).toBe(client1.inboxId); expect(group.consentState).toBe(ConsentState.Allowed); diff --git a/yarn.lock b/yarn.lock index 5030eb607..19947cb1c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4809,7 +4809,7 @@ __metadata: "@xmtp/content-type-primitives": "npm:^2.0.0" "@xmtp/content-type-text": "npm:^2.0.0" "@xmtp/proto": "npm:^3.72.3" - "@xmtp/wasm-bindings": "npm:^0.0.8" + "@xmtp/wasm-bindings": "npm:^0.0.9" playwright: "npm:^1.49.0" rollup: "npm:^4.27.3" rollup-plugin-dts: "npm:^6.1.1" @@ -5114,10 +5114,10 @@ __metadata: languageName: node linkType: hard -"@xmtp/node-bindings@npm:^0.0.29": - version: 0.0.29 - resolution: "@xmtp/node-bindings@npm:0.0.29" - checksum: 10/d6abb86a58517bffff07bda59b387318a9d60b2ba0ca13376ef60fe79aa9afefb8e2b07b218064738b07c4e699eac967f8aff64942017f8872f4fe982ac475ae +"@xmtp/node-bindings@npm:^0.0.30": + version: 0.0.30 + resolution: "@xmtp/node-bindings@npm:0.0.30" + checksum: 10/a7bf581c038f811b38ea53690b1500bd931b3fb8c32798fc96cf852ab5e0fc0b837fdfc2f354943be45095f096c23467e8de5a96235409b2013fc49c27cb2644 languageName: node linkType: hard @@ -5145,7 +5145,7 @@ __metadata: "@xmtp/content-type-group-updated": "npm:^2.0.0" "@xmtp/content-type-primitives": "npm:^2.0.0" "@xmtp/content-type-text": "npm:^2.0.0" - "@xmtp/node-bindings": "npm:^0.0.29" + "@xmtp/node-bindings": "npm:^0.0.30" "@xmtp/proto": "npm:^3.72.3" "@xmtp/xmtp-js": "workspace:^" fast-glob: "npm:^3.3.2" @@ -5256,10 +5256,10 @@ __metadata: languageName: node linkType: hard -"@xmtp/wasm-bindings@npm:^0.0.8": - version: 0.0.8 - resolution: "@xmtp/wasm-bindings@npm:0.0.8" - checksum: 10/6020c15b56d3ac02fc76d86bb176c2504f1976aec7af2ae7f3aea7e159f2692db08c0d5501462f8ea4fb34530cacbd2568a3870d0a7b8189db33e7e7a49a9711 +"@xmtp/wasm-bindings@npm:^0.0.9": + version: 0.0.9 + resolution: "@xmtp/wasm-bindings@npm:0.0.9" + checksum: 10/02cb19cf4b610b040a52e48b324e712a84b6278834c68ce0f0f7d033a00e2188b58cc75c69a30c0b908505921cd9ef354bce51528105267141320378681280d1 languageName: node linkType: hard