Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade node bindings #709

Merged
merged 7 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/metal-boxes-marry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@xmtp/node-sdk": patch
---

Upgrade node bindings
2 changes: 1 addition & 1 deletion sdks/node-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"@xmtp/content-type-group-updated": "^1.0.0",
"@xmtp/content-type-primitives": "^1.0.2",
"@xmtp/content-type-text": "^1.0.0",
"@xmtp/node-bindings": "^0.0.16",
"@xmtp/node-bindings": "^0.0.17",
"@xmtp/proto": "^3.62.1"
},
"devDependencies": {
Expand Down
9 changes: 6 additions & 3 deletions sdks/node-sdk/src/AsyncStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ type ResolveValue<T> = {

type ResolveNext<T> = (resolveValue: ResolveValue<T>) => void;

export type StreamCallback<T> = (err: Error | null, value: T) => void;
export type StreamCallback<T> = (
err: Error | null,
value: T | undefined,
) => void;

export class AsyncStream<T> {
#done = false;
#resolveNext: ResolveNext<T> | null;
#queue: T[];
#queue: (T | undefined)[];

onReturn: (() => void) | undefined = undefined;

Expand Down Expand Up @@ -62,7 +65,7 @@ export class AsyncStream<T> {
}
};

return = (value: T) => {
return = (value: T | undefined) => {
this.#done = true;
this.onReturn?.();
return Promise.resolve({
Expand Down
28 changes: 14 additions & 14 deletions sdks/node-sdk/src/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ import {
createClient,
generateInboxId,
getInboxIdForAddress,
NapiGroupMessageKind,
type NapiClient,
type NapiConsent,
type NapiConsentEntityType,
type NapiMessage,
type NapiSignatureRequestType,
GroupMessageKind,
type Consent,
type ConsentEntityType,
type Message,
type Client as NodeClient,
type SignatureRequestType,
} from "@xmtp/node-bindings";
import { Conversations } from "@/Conversations";

Expand Down Expand Up @@ -84,11 +84,11 @@ export type ClientOptions = NetworkOptions &
OtherOptions;

export class Client {
#innerClient: NapiClient;
#innerClient: NodeClient;
#conversations: Conversations;
#codecs: Map<string, ContentCodec>;

constructor(client: NapiClient, codecs: ContentCodec[]) {
constructor(client: NodeClient, codecs: ContentCodec[]) {
this.#innerClient = client;
this.#conversations = new Conversations(this, client.conversations());
this.#codecs = new Map(
Expand Down Expand Up @@ -186,7 +186,7 @@ export class Client {
}

addSignature(
signatureType: NapiSignatureRequestType,
signatureType: SignatureRequestType,
signatureBytes: Uint8Array,
) {
void this.#innerClient.addSignature(signatureType, signatureBytes);
Expand Down Expand Up @@ -221,7 +221,7 @@ export class Client {
return encoded;
}

decodeContent(message: NapiMessage, contentType: ContentTypeId) {
decodeContent(message: Message, contentType: ContentTypeId) {
const codec = this.codecFor(contentType);
if (!codec) {
throw new Error(`no codec for ${contentType.toString()}`);
Expand All @@ -230,7 +230,7 @@ export class Client {
// throw an error if there's an invalid group membership change message
if (
contentType.sameAs(ContentTypeGroupUpdated) &&
message.kind !== NapiGroupMessageKind.MembershipChange
message.kind !== GroupMessageKind.MembershipChange
) {
throw new Error("Error decoding group membership change");
}
Expand All @@ -240,7 +240,7 @@ export class Client {
}

async requestHistorySync() {
return this.#innerClient.requestHistorySync();
return this.#innerClient.sendHistorySyncRequest();
}

async getInboxIdByAddress(accountAddress: string) {
Expand All @@ -265,11 +265,11 @@ export class Client {
);
}

async setConsentStates(consentStates: NapiConsent[]) {
async setConsentStates(consentStates: Consent[]) {
return this.#innerClient.setConsentStates(consentStates);
}

async getConsentState(entityType: NapiConsentEntityType, entity: string) {
async getConsentState(entityType: ConsentEntityType, entity: string) {
return this.#innerClient.getConsentState(entityType, entity);
}
}
24 changes: 13 additions & 11 deletions sdks/node-sdk/src/Conversation.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { ContentTypeId } from "@xmtp/content-type-primitives";
import { ContentTypeText } from "@xmtp/content-type-text";
import type {
NapiConsentState,
NapiGroup,
NapiListMessagesOptions,
ConsentState,
Conversation as Group,
ListMessagesOptions,
} from "@xmtp/node-bindings";
import { AsyncStream, type StreamCallback } from "@/AsyncStream";
import type { Client } from "@/Client";
Expand All @@ -12,9 +12,9 @@ import { nsToDate } from "@/helpers/date";

export class Conversation {
#client: Client;
#group: NapiGroup;
#group: Group;

constructor(client: Client, group: NapiGroup) {
constructor(client: Client, group: Group) {
this.#client = client;
this.#group = group;
}
Expand Down Expand Up @@ -113,10 +113,12 @@ export class Conversation {
stream(callback?: StreamCallback<DecodedMessage>) {
const asyncStream = new AsyncStream<DecodedMessage>();

const stream = this.#group.stream((err, message) => {
const decodedMessage = new DecodedMessage(this.#client, message);
asyncStream.callback(err, decodedMessage);
callback?.(err, decodedMessage);
const stream = this.#group.stream((error, value) => {
const message = value
? new DecodedMessage(this.#client, value)
: undefined;
asyncStream.callback(error, message);
callback?.(error, message);
});

asyncStream.onReturn = stream.end.bind(stream);
Expand Down Expand Up @@ -192,7 +194,7 @@ export class Conversation {
return this.#group.send(encodedContent);
}

messages(options?: NapiListMessagesOptions): DecodedMessage[] {
messages(options?: ListMessagesOptions): DecodedMessage[] {
return (
this.#group
.findMessages(options)
Expand All @@ -206,7 +208,7 @@ export class Conversation {
return this.#group.consentState();
}

updateConsentState(consentState: NapiConsentState) {
updateConsentState(consentState: ConsentState) {
this.#group.updateConsentState(consentState);
}

Expand Down
64 changes: 36 additions & 28 deletions sdks/node-sdk/src/Conversations.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type {
NapiConversations,
NapiCreateGroupOptions,
NapiListConversationsOptions,
CreateGroupOptions,
ListConversationsOptions,
Conversations as NodeConversations,
} from "@xmtp/node-bindings";
import { AsyncStream, type StreamCallback } from "@/AsyncStream";
import type { Client } from "@/Client";
Expand All @@ -10,9 +10,9 @@ import { DecodedMessage } from "@/DecodedMessage";

export class Conversations {
#client: Client;
#conversations: NapiConversations;
#conversations: NodeConversations;

constructor(client: Client, conversations: NapiConversations) {
constructor(client: Client, conversations: NodeConversations) {
this.#client = client;
this.#conversations = conversations;
}
Expand Down Expand Up @@ -49,7 +49,7 @@ export class Conversations {

async newConversation(
accountAddresses: string[],
options?: NapiCreateGroupOptions,
options?: CreateGroupOptions,
) {
const group = await this.#conversations.createGroup(
accountAddresses,
Expand All @@ -65,7 +65,7 @@ export class Conversations {
return conversation;
}

async list(options?: NapiListConversationsOptions) {
async list(options?: ListConversationsOptions) {
const groups = await this.#conversations.list(options);
return groups.map((group) => {
const conversation = new Conversation(this.#client, group);
Expand All @@ -74,7 +74,7 @@ export class Conversations {
}

async listGroups(
options?: Omit<NapiListConversationsOptions, "conversationType">,
options?: Omit<ListConversationsOptions, "conversationType">,
) {
const groups = await this.#conversations.listGroups(options);
return groups.map((group) => {
Expand All @@ -83,9 +83,7 @@ export class Conversations {
});
}

async listDms(
options?: Omit<NapiListConversationsOptions, "conversationType">,
) {
async listDms(options?: Omit<ListConversationsOptions, "conversationType">) {
const groups = await this.#conversations.listDms(options);
return groups.map((group) => {
const conversation = new Conversation(this.#client, group);
Expand All @@ -100,8 +98,10 @@ export class Conversations {
stream(callback?: StreamCallback<Conversation>) {
const asyncStream = new AsyncStream<Conversation>();

const stream = this.#conversations.stream((err, group) => {
const conversation = new Conversation(this.#client, group);
const stream = this.#conversations.stream((err, value) => {
const conversation = value
? new Conversation(this.#client, value)
: undefined;
asyncStream.callback(err, conversation);
callback?.(err, conversation);
});
Expand All @@ -114,8 +114,10 @@ export class Conversations {
streamGroups(callback?: StreamCallback<Conversation>) {
const asyncStream = new AsyncStream<Conversation>();

const stream = this.#conversations.streamGroups((err, group) => {
const conversation = new Conversation(this.#client, group);
const stream = this.#conversations.streamGroups((err, value) => {
const conversation = value
? new Conversation(this.#client, value)
: undefined;
asyncStream.callback(err, conversation);
callback?.(err, conversation);
});
Expand All @@ -128,8 +130,10 @@ export class Conversations {
streamDms(callback?: StreamCallback<Conversation>) {
const asyncStream = new AsyncStream<Conversation>();

const stream = this.#conversations.streamDms((err, group) => {
const conversation = new Conversation(this.#client, group);
const stream = this.#conversations.streamDms((err, value) => {
const conversation = value
? new Conversation(this.#client, value)
: undefined;
asyncStream.callback(err, conversation);
callback?.(err, conversation);
});
Expand All @@ -145,8 +149,10 @@ export class Conversations {

const asyncStream = new AsyncStream<DecodedMessage>();

const stream = this.#conversations.streamAllMessages((err, message) => {
const decodedMessage = new DecodedMessage(this.#client, message);
const stream = this.#conversations.streamAllMessages((err, value) => {
const decodedMessage = value
? new DecodedMessage(this.#client, value)
: undefined;
asyncStream.callback(err, decodedMessage);
callback?.(err, decodedMessage);
});
Expand All @@ -162,13 +168,13 @@ export class Conversations {

const asyncStream = new AsyncStream<DecodedMessage>();

const stream = this.#conversations.streamAllGroupMessages(
(err, message) => {
const decodedMessage = new DecodedMessage(this.#client, message);
asyncStream.callback(err, decodedMessage);
callback?.(err, decodedMessage);
},
);
const stream = this.#conversations.streamAllGroupMessages((err, value) => {
const decodedMessage = value
? new DecodedMessage(this.#client, value)
: undefined;
asyncStream.callback(err, decodedMessage);
callback?.(err, decodedMessage);
});

asyncStream.onReturn = stream.end.bind(stream);

Expand All @@ -181,8 +187,10 @@ export class Conversations {

const asyncStream = new AsyncStream<DecodedMessage>();

const stream = this.#conversations.streamAllDmMessages((err, message) => {
const decodedMessage = new DecodedMessage(this.#client, message);
const stream = this.#conversations.streamAllDmMessages((err, value) => {
const decodedMessage = value
? new DecodedMessage(this.#client, value)
: undefined;
asyncStream.callback(err, decodedMessage);
callback?.(err, decodedMessage);
});
Expand Down
18 changes: 9 additions & 9 deletions sdks/node-sdk/src/DecodedMessage.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ContentTypeId } from "@xmtp/content-type-primitives";
import {
NapiDeliveryStatus,
NapiGroupMessageKind,
type NapiMessage,
DeliveryStatus,
GroupMessageKind,
type Message,
} from "@xmtp/node-bindings";
import type { Client } from "@/Client";
import { nsToDate } from "@/helpers/date";
Expand All @@ -25,7 +25,7 @@ export class DecodedMessage<T = any> {
sentAt: Date;
sentAtNs: number;

constructor(client: Client, message: NapiMessage) {
constructor(client: Client, message: Message) {
this.#client = client;
this.id = message.id;
this.sentAtNs = message.sentAtNs;
Expand All @@ -34,23 +34,23 @@ export class DecodedMessage<T = any> {
this.senderInboxId = message.senderInboxId;

switch (message.kind) {
case NapiGroupMessageKind.Application:
case GroupMessageKind.Application:
this.kind = "application";
break;
case NapiGroupMessageKind.MembershipChange:
case GroupMessageKind.MembershipChange:
this.kind = "membership_change";
break;
// no default
}

switch (message.deliveryStatus) {
case NapiDeliveryStatus.Unpublished:
case DeliveryStatus.Unpublished:
this.deliveryStatus = "unpublished";
break;
case NapiDeliveryStatus.Published:
case DeliveryStatus.Published:
this.deliveryStatus = "published";
break;
case NapiDeliveryStatus.Failed:
case DeliveryStatus.Failed:
this.deliveryStatus = "failed";
break;
// no default
Expand Down
Loading
Loading