mirror of
https://github.com/waku-org/js-waku.git
synced 2025-02-25 18:45:35 +00:00
fix: cursor
This commit is contained in:
parent
9613e9c41d
commit
5c4118041e
@ -379,18 +379,28 @@ export function isDefined<T>(msg: T | undefined): msg is T {
|
||||
}
|
||||
|
||||
export async function createCursor(
|
||||
message: string,
|
||||
messageTimestamp: bigint,
|
||||
contentTopic: string,
|
||||
message: DecodedMessage,
|
||||
pubsubTopic: string = DefaultPubSubTopic
|
||||
): Promise<proto.Index> {
|
||||
const contentTopicBytes = utf8ToBytes(contentTopic);
|
||||
const messageBytes = utf8ToBytes(message);
|
||||
const digest = sha256(concat([contentTopicBytes, messageBytes]));
|
||||
if (
|
||||
!message ||
|
||||
!message.timestamp ||
|
||||
!message.payload ||
|
||||
!message.contentTopic
|
||||
) {
|
||||
throw new Error("Message is missing timestamp or payload");
|
||||
}
|
||||
|
||||
const contentTopicBytes = utf8ToBytes(message.contentTopic);
|
||||
|
||||
const digest = sha256(concat([contentTopicBytes, message.payload]));
|
||||
|
||||
const messageTime = BigInt(message.timestamp.getTime()) * BigInt(1000000);
|
||||
|
||||
return {
|
||||
digest,
|
||||
pubsubTopic,
|
||||
senderTime: messageTimestamp,
|
||||
senderTime: messageTime,
|
||||
receivedTime: messageTime,
|
||||
};
|
||||
}
|
||||
|
@ -30,7 +30,7 @@ describe("Waku Store", () => {
|
||||
let nwaku: Nwaku;
|
||||
|
||||
beforeEach(async function () {
|
||||
this.timeout(15_000);
|
||||
this.timeout(35_000);
|
||||
nwaku = new Nwaku(makeLogFileName(this));
|
||||
await nwaku.start({ store: true, lightpush: true });
|
||||
});
|
||||
@ -135,30 +135,25 @@ describe("Waku Store", () => {
|
||||
const query = waku.store.queryGenerator([TestDecoder]);
|
||||
|
||||
// messages in reversed order (first message at last index)
|
||||
const messages: Message[] = [];
|
||||
const messages: DecodedMessage[] = [];
|
||||
for await (const page of query) {
|
||||
for await (const msg of page.reverse()) {
|
||||
messages.push(msg as Message);
|
||||
messages.push(msg as DecodedMessage);
|
||||
}
|
||||
}
|
||||
|
||||
// index 2 would mean the third last message sent
|
||||
const cursorIndex = 2;
|
||||
const cursorMessage = messages[cursorIndex];
|
||||
|
||||
// create cursor to extract messages after the 3rd index
|
||||
const cursor = await createCursor(
|
||||
bytesToUtf8(cursorMessage.payload!),
|
||||
BigInt(cursorMessage.timestamp!.getTime()) * BigInt(1000000),
|
||||
TestContentTopic
|
||||
);
|
||||
const cursor = await createCursor(messages[cursorIndex]);
|
||||
|
||||
const messagesAfterCursor: Message[] = [];
|
||||
const messagesAfterCursor: DecodedMessage[] = [];
|
||||
for await (const page of waku.store.queryGenerator([TestDecoder], {
|
||||
cursor,
|
||||
})) {
|
||||
for await (const msg of page.reverse()) {
|
||||
messagesAfterCursor.push(msg as Message);
|
||||
messagesAfterCursor.push(msg as DecodedMessage);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user