Skip to content

Commit aaaca05

Browse files
authored
#198 - added size check when returning encodedData
* added size check when returning encodedData * added typing for entries returned in query results
1 parent 28bbf5e commit aaaca05

File tree

15 files changed

+120
-70
lines changed

15 files changed

+120
-70
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# Decentralized Web Node (DWN) SDK
44

55
Code Coverage
6-
![Statements](https://img.shields.io/badge/statements-94.91%25-brightgreen.svg?style=flat) ![Branches](https://img.shields.io/badge/branches-92.93%25-brightgreen.svg?style=flat) ![Functions](https://img.shields.io/badge/functions-93.27%25-brightgreen.svg?style=flat) ![Lines](https://img.shields.io/badge/lines-94.91%25-brightgreen.svg?style=flat)
6+
![Statements](https://img.shields.io/badge/statements-94.93%25-brightgreen.svg?style=flat) ![Branches](https://img.shields.io/badge/branches-92.92%25-brightgreen.svg?style=flat) ![Functions](https://img.shields.io/badge/functions-93.27%25-brightgreen.svg?style=flat) ![Lines](https://img.shields.io/badge/lines-94.93%25-brightgreen.svg?style=flat)
77

88
## Introduction
99

src/core/dwn-constant.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export class DwnConstant {
2+
/**
3+
* The maximum size of raw data that will be returned as `encodedData`.
4+
*/
5+
public static readonly maxDataSizeAllowedToBeEncoded = 10_000;
6+
}

src/core/message-reply.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { Descriptor } from './types.js';
1+
import type { QueryResultEntry } from './types.js';
2+
23
import { Readable } from 'readable-stream';
34

45
type Status = {
@@ -8,7 +9,7 @@ type Status = {
89

910
type MessageReplyOptions = {
1011
status: Status,
11-
entries?: { descriptor: Descriptor }[];
12+
entries?: QueryResultEntry[];
1213
data? : Readable;
1314
};
1415

@@ -20,7 +21,7 @@ export class MessageReply {
2021
* e.g. the resulting messages from a RecordsQuery
2122
* Mutually exclusive with `data`.
2223
*/
23-
entries?: { descriptor: Descriptor }[];
24+
entries?: QueryResultEntry[];
2425

2526
/**
2627
* Data corresponding to the message received if applicable (e.g. RecordsRead).

src/core/message.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,11 @@ export abstract class Message {
7171
* Gets the CID of the given message.
7272
*/
7373
public static async getCid(message: BaseMessage): Promise<string> {
74-
const messageCopy = { ...message };
75-
76-
// TODO: Once #219 (https://github.com/TBD54566975/dwn-sdk-js/issues/219) is implemented,
77-
// `encodedData` will likely not exist directly as part of the message
78-
if (messageCopy['encodedData'] !== undefined) {
79-
delete (messageCopy as any).encodedData;
80-
}
81-
82-
const cid = await computeCid(messageCopy);
74+
// NOTE: we wrap the `computeCid()` here in case that
75+
// the message will contain properties that should not be part of the CID computation
76+
// and we need to strip them out (like `encodedData` that we historically had for a long time),
77+
// but we can remove this method entirely if the code becomes stable and it is apparent that the wrapper is not needed
78+
const cid = await computeCid(message);
8379
return cid;
8480
}
8581

src/core/types.ts

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,34 @@ export type TimestampedMessage = BaseMessage & {
3535
};
3636

3737
/**
38-
* Message that references `dataCid`.
38+
* Message returned in a query result.
39+
* NOTE: the message structure is a modified version of the message received, the most notable differences are:
40+
* 1. does not contain `authorization`
41+
* 2. may include encoded data
3942
*/
40-
export type DataReferencingMessage = {
41-
descriptor: {
42-
dataCid: string;
43-
};
44-
45-
encodedData: string;
43+
export type QueryResultEntry = {
44+
descriptor: Descriptor;
45+
encodedData?: string;
4646
};
4747

4848
export type EqualFilter = string | number | boolean;
4949

5050
export type OneOfFilter = EqualFilter[];
5151

52-
type GT = ({ gt: string } & { gte?: never }) | ({ gt?: never } & { gte: string });
53-
type LT = ({ lt: string } & { lte?: never }) | ({ lt?: never } & { lte: string });
52+
53+
/**
54+
* "greater than" or "greater than or equal to" range condition. `gt` and `gte` are mutually exclusive.
55+
*/
56+
export type GT = ({ gt: string } & { gte?: never }) | ({ gt?: never } & { gte: string });
57+
58+
/**
59+
* "less than" or "less than or equal to" range condition. `lt`, `lte` are mutually exclusive.
60+
*/
61+
export type LT = ({ lt: string } & { lte?: never }) | ({ lt?: never } & { lte: string });
62+
63+
/**
64+
* Ranger filter. 1 condition is required.
65+
*/
5466
export type RangeFilter = (GT | LT) & Partial<GT> & Partial<LT>;
5567

5668
export type Filter = {

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export { DidKeyResolver } from './did/did-key-resolver.js';
2424
export { DidIonResolver } from './did/did-ion-resolver.js';
2525
export { DidResolver, DidMethodResolver } from './did/did-resolver.js';
2626
export { Dwn } from './dwn.js';
27+
export { DwnConstant } from './core/dwn-constant.js';
2728
export { DwnInterfaceName, DwnMethodName } from './core/message.js';
2829
export { Encoder } from './utils/encoder.js';
2930
export { HooksWrite, HooksWriteOptions } from './interfaces/hooks/messages/hooks-write.js';

src/interfaces/protocols/handlers/protocols-query.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { MethodHandler } from '../../types.js';
22
import type { ProtocolsQueryMessage } from '../types.js';
3+
import type { QueryResultEntry } from '../../../core/types.js';
34

45
import { canonicalAuth } from '../../../core/auth.js';
56
import { MessageReply } from '../../../core/message-reply.js';
@@ -43,7 +44,14 @@ export class ProtocolsQueryHandler implements MethodHandler {
4344
};
4445
removeUndefinedProperties(query);
4546

46-
const entries = await this.messageStore.query(tenant, query);
47+
const records = await this.messageStore.query(tenant, query);
48+
49+
// strip away `authorization` property for each record before responding
50+
const entries: QueryResultEntry[] = [];
51+
for (const record of records) {
52+
const { authorization: _, ...objectWithRemainingProperties } = record; // a trick to stripping away `authorization`
53+
entries.push(objectWithRemainingProperties);
54+
}
4755

4856
return new MessageReply({
4957
status: { code: 200, detail: 'OK' },

src/interfaces/records/handlers/records-query.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import type { MethodHandler } from '../../types.js';
2+
import type { BaseMessage, QueryResultEntry } from '../../../core/types.js';
23
import type { RecordsQueryMessage, RecordsWriteMessage } from '../types.js';
34

45
import { authenticate } from '../../../core/auth.js';
5-
import { BaseMessage } from '../../../core/types.js';
66
import { lexicographicalCompare } from '../../../utils/string.js';
77
import { MessageReply } from '../../../core/message-reply.js';
88
import { StorageController } from '../../../store/storage-controller.js';
@@ -50,11 +50,10 @@ export class RecordsQueryHandler implements MethodHandler {
5050
}
5151

5252
// strip away `authorization` property for each record before responding
53-
const entries = [];
53+
const entries: QueryResultEntry[] = [];
5454
for (const record of records) {
55-
const recordDuplicate = { ...record };
56-
delete recordDuplicate.authorization;
57-
entries.push(recordDuplicate);
55+
const { authorization: _, ...objectWithRemainingProperties } = record; // a trick to stripping away `authorization`
56+
entries.push(objectWithRemainingProperties);
5857
}
5958

6059
return new MessageReply({

src/store/blockstore-level.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ import { sleep } from '../utils/time.js';
1111
// FreeBSD, including any future Node.js and Electron release thanks to Node-API, including ARM
1212
// platforms like Raspberry Pi and Android, as well as in Chrome, Firefox, Edge, Safari, iOS Safari
1313
// and Chrome for Android.
14+
15+
/**
16+
* Blockstore implementation using LevelDB for storing the actual messages (in the case of MessageStore)
17+
* or the data associated with messages (in the case of a DataStore).
18+
*/
1419
export class BlockstoreLevel implements Blockstore {
1520
config: BlockstoreLevelConfig;
1621

src/store/index-level.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ export interface IndexLevelOptions {
1515
signal?: AbortSignal;
1616
}
1717

18+
/**
19+
* A LevelDB implementation for indexing the messages stored in the DWN.
20+
*/
1821
export class IndexLevel {
1922
config: IndexLevelConfig;
2023

0 commit comments

Comments
 (0)