Skip to content

Commit

Permalink
pull messageCid cursor out of the entries array and into a metadata p…
Browse files Browse the repository at this point in the history
…roperty
  • Loading branch information
LiranCohen committed Sep 13, 2023
1 parent dec9e4e commit 8e94251
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 13 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
# Decentralized Web Node (DWN) SDK <!-- omit in toc -->

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)
Expand Down
7 changes: 6 additions & 1 deletion src/core/message-reply.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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;
};
15 changes: 9 additions & 6 deletions src/handlers/records-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<RecordsQueryReplyEntry[]> {
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;
Expand Down
4 changes: 4 additions & 0 deletions src/types/message-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ export type QueryResultEntry = {
encodedData?: string;
};

export type QueryReplyMetadata = {
messageCid?: string;
};

export type EqualFilter = string | number | boolean;

export type OneOfFilter = EqualFilter[];
Expand Down
4 changes: 2 additions & 2 deletions src/types/records-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -83,7 +83,6 @@ export type UnsignedRecordsWriteMessage = {
*/
export type RecordsQueryReplyEntry = RecordsWriteMessage & {
encodedData?: string;
messageCid: string;
};

export type RecordsQueryDescriptor = {
Expand Down Expand Up @@ -137,6 +136,7 @@ export type RecordsQueryMessage = GenericMessage & {

export type RecordsQueryReply = GenericMessageReply & {
entries?: RecordsQueryReplyEntry[];
metadata?: QueryReplyMetadata;
};

export type RecordsReadMessage = {
Expand Down
8 changes: 5 additions & 3 deletions tests/handlers/records-query.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 8e94251

Please sign in to comment.