From 8e94251188a83bc692d4091f7fdcaa1cdfb67c3d Mon Sep 17 00:00:00 2001 From: Liran Cohen Date: Wed, 13 Sep 2023 13:32:27 -0400 Subject: [PATCH] pull messageCid cursor out of the entries array and into a metadata property --- README.md | 6 +++++- src/core/message-reply.ts | 7 ++++++- src/handlers/records-query.ts | 15 +++++++++------ src/types/message-types.ts | 4 ++++ src/types/records-types.ts | 4 ++-- tests/handlers/records-query.spec.ts | 8 +++++--- 6 files changed, 31 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 58de8a754..fd6dd9ef6 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,11 @@ # Decentralized Web Node (DWN) SDK Code Coverage -![Statements](https://img.shields.io/badge/statements-97.12%25-brightgreen.svg?style=flat) ![Branches](https://img.shields.io/badge/branches-94.48%25-brightgreen.svg?style=flat) ![Functions](https://img.shields.io/badge/functions-93.78%25-brightgreen.svg?style=flat) ![Lines](https://img.shields.io/badge/lines-97.12%25-brightgreen.svg?style=flat) +<<<<<<< Updated upstream +![Statements](https://img.shields.io/badge/statements-97.16%25-brightgreen.svg?style=flat) ![Branches](https://img.shields.io/badge/branches-94.59%25-brightgreen.svg?style=flat) ![Functions](https://img.shields.io/badge/functions-93.84%25-brightgreen.svg?style=flat) ![Lines](https://img.shields.io/badge/lines-97.16%25-brightgreen.svg?style=flat) +======= +![Statements](https://img.shields.io/badge/statements-97.15%25-brightgreen.svg?style=flat) ![Branches](https://img.shields.io/badge/branches-94.58%25-brightgreen.svg?style=flat) ![Functions](https://img.shields.io/badge/functions-93.84%25-brightgreen.svg?style=flat) ![Lines](https://img.shields.io/badge/lines-97.15%25-brightgreen.svg?style=flat) +>>>>>>> Stashed changes - [Introduction](#introduction) - [Installation](#installation) diff --git a/src/core/message-reply.ts b/src/core/message-reply.ts index 2528d1670..1887095a1 100644 --- a/src/core/message-reply.ts +++ b/src/core/message-reply.ts @@ -1,5 +1,5 @@ -import type { QueryResultEntry } from '../types/message-types.js'; import type { Readable } from 'readable-stream'; +import type { QueryReplyMetadata, QueryResultEntry } from '../types/message-types.js'; type Status = { code: number @@ -33,4 +33,9 @@ export type UnionMessageReply = GenericMessageReply & { * Mutually exclusive with `entries`. */ data?: Readable; + + /** + * Metadata associated with the reply, currently only used within `RecordsQuery` replies + */ + metadata?: QueryReplyMetadata; }; \ No newline at end of file diff --git a/src/handlers/records-query.ts b/src/handlers/records-query.ts index 27a350b0e..97006c3a1 100644 --- a/src/handlers/records-query.ts +++ b/src/handlers/records-query.ts @@ -47,26 +47,29 @@ export class RecordsQueryHandler implements MethodHandler { } } + let lastRecordMessageCid: string|undefined; + const lastRecord = recordsWrites.at(-1); + if (lastRecord) { + lastRecordMessageCid = await Message.getCid(lastRecord); + } + const entries = await RecordsQueryHandler.removeAuthorization(recordsWrites); return { - status: { code: 200, detail: 'OK' }, + status : { code: 200, detail: 'OK' }, + metadata : { messageCid: lastRecordMessageCid }, entries, }; } /** * Removes `authorization` property from each and every `RecordsWrite` message given and returns the result as a different array. - * Adds `messageCid` as a cursor pointer for pagination as it can no longer be computed without the `authorization` property. */ private static async removeAuthorization(recordsWriteMessages: RecordsWriteMessageWithOptionalEncodedData[]): Promise { const recordsQueryReplyEntries: RecordsQueryReplyEntry[] = []; for (const record of recordsWriteMessages) { const { authorization: _, ...objectWithRemainingProperties } = record; // a trick to stripping away `authorization` - recordsQueryReplyEntries.push({ - ...objectWithRemainingProperties, - messageCid: await Message.getCid(record) - }); + recordsQueryReplyEntries.push(objectWithRemainingProperties); } return recordsQueryReplyEntries; diff --git a/src/types/message-types.ts b/src/types/message-types.ts index 269f1c52e..d04d7d93c 100644 --- a/src/types/message-types.ts +++ b/src/types/message-types.ts @@ -46,6 +46,10 @@ export type QueryResultEntry = { encodedData?: string; }; +export type QueryReplyMetadata = { + messageCid?: string; +}; + export type EqualFilter = string | number | boolean; export type OneOfFilter = EqualFilter[]; diff --git a/src/types/records-types.ts b/src/types/records-types.ts index 1af4ae67c..e090ca6bd 100644 --- a/src/types/records-types.ts +++ b/src/types/records-types.ts @@ -5,7 +5,7 @@ import type { GenericMessageReply } from '../core/message-reply.js'; import type { KeyDerivationScheme } from '../utils/hd-key.js'; import type { PublicJwk } from './jose-types.js'; import type { Readable } from 'readable-stream'; -import type { BaseAuthorizationPayload, GenericMessage, Pagination } from './message-types.js'; +import type { BaseAuthorizationPayload, GenericMessage, Pagination, QueryReplyMetadata } from './message-types.js'; import type { DwnInterfaceName, DwnMethodName } from '../core/message.js'; export type RecordsWriteDescriptor = { @@ -83,7 +83,6 @@ export type UnsignedRecordsWriteMessage = { */ export type RecordsQueryReplyEntry = RecordsWriteMessage & { encodedData?: string; - messageCid: string; }; export type RecordsQueryDescriptor = { @@ -137,6 +136,7 @@ export type RecordsQueryMessage = GenericMessage & { export type RecordsQueryReply = GenericMessageReply & { entries?: RecordsQueryReplyEntry[]; + metadata?: QueryReplyMetadata; }; export type RecordsReadMessage = { diff --git a/tests/handlers/records-query.spec.ts b/tests/handlers/records-query.spec.ts index bbe9829db..7d1b37f6c 100644 --- a/tests/handlers/records-query.spec.ts +++ b/tests/handlers/records-query.spec.ts @@ -471,20 +471,22 @@ export function testRecordsQueryHandler(): void { const limit = 5; const results: RecordsQueryReplyEntry[] = []; + let messageCid; while (true) { - const cursor = results.length > 0 ? results[results.length - 1].messageCid : undefined; const pageQuery = await TestDataGenerator.generateRecordsQuery({ author : alice, filter : { schema: 'https://schema' }, pagination: { - limit : limit, - messageCid : cursor, + limit: limit, + messageCid, }, }); + const pageReply = await dwn.handleRecordsQuery(alice.did, pageQuery.message); expect(pageReply.status.code).to.equal(200); + messageCid = pageReply.metadata?.messageCid; if (pageReply.entries?.length === 0) { break; }