Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
shobitb authored Oct 13, 2023
2 parents a286953 + 19d4648 commit d46facc
Show file tree
Hide file tree
Showing 18 changed files with 428 additions and 106 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Here's to a thrilling Hacktoberfest voyage with us! 🎉
# Decentralized Web Node (DWN) SDK <!-- omit in toc -->

Code Coverage
![Statements](https://img.shields.io/badge/statements-98.37%25-brightgreen.svg?style=flat) ![Branches](https://img.shields.io/badge/branches-95.11%25-brightgreen.svg?style=flat) ![Functions](https://img.shields.io/badge/functions-95.7%25-brightgreen.svg?style=flat) ![Lines](https://img.shields.io/badge/lines-98.37%25-brightgreen.svg?style=flat)
![Statements](https://img.shields.io/badge/statements-98.38%25-brightgreen.svg?style=flat) ![Branches](https://img.shields.io/badge/branches-95.12%25-brightgreen.svg?style=flat) ![Functions](https://img.shields.io/badge/functions-95.7%25-brightgreen.svg?style=flat) ![Lines](https://img.shields.io/badge/lines-98.38%25-brightgreen.svg?style=flat)

- [Introduction](#introduction)
- [Installation](#installation)
Expand Down
15 changes: 14 additions & 1 deletion src/dwn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { MethodHandler } from './types/method-handler.js';
import type { Readable } from 'readable-stream';
import type { RecordsWriteHandlerOptions } from './handlers/records-write.js';
import type { TenantGate } from './core/tenant-gate.js';
import type { EventsGetMessage, EventsGetReply, PermissionsGrantMessage, PermissionsRequestMessage, PermissionsRevokeMessage, ProtocolsConfigureMessage, ProtocolsQueryMessage, ProtocolsQueryReply } from './index.js';
import type { GenericMessageReply, UnionMessageReply } from './core/message-reply.js';
import type { MessagesGetMessage, MessagesGetReply } from './types/messages-types.js';
import type { RecordsDeleteMessage, RecordsDeleteReply, RecordsQueryMessage, RecordsQueryReply,
Expand Down Expand Up @@ -91,7 +92,19 @@ export class Dwn {
* Processes the given DWN message and returns with a reply.
* @param tenant The tenant DID to route the given message to.
*/
public async processMessage(tenant: string, rawMessage: any, dataStream?: Readable): Promise<UnionMessageReply> {
public async processMessage(tenant: string, rawMessage: EventsGetMessage): Promise<EventsGetReply>;
public async processMessage(tenant: string, rawMessage: MessagesGetMessage): Promise<MessagesGetReply>;
public async processMessage(tenant: string, rawMessage: ProtocolsConfigureMessage): Promise<GenericMessageReply>;
public async processMessage(tenant: string, rawMessage: ProtocolsQueryMessage): Promise<ProtocolsQueryReply>;
public async processMessage(tenant: string, rawMessage: PermissionsRequestMessage): Promise<GenericMessageReply>;
public async processMessage(tenant: string, rawMessage: PermissionsGrantMessage): Promise<GenericMessageReply>;
public async processMessage(tenant: string, rawMessage: PermissionsRevokeMessage): Promise<GenericMessageReply>;
public async processMessage(tenant: string, rawMessage: RecordsDeleteMessage): Promise<GenericMessageReply>;
public async processMessage(tenant: string, rawMessage: RecordsQueryMessage): Promise<RecordsQueryReply>;
public async processMessage(tenant: string, rawMessage: RecordsReadMessage): Promise<RecordsReadReply>;
public async processMessage(tenant: string, rawMessage: RecordsWriteMessage, dataStream?: Readable): Promise<GenericMessageReply>;
public async processMessage(tenant: string, rawMessage: unknown, dataStream?: Readable): Promise<UnionMessageReply>;
public async processMessage(tenant: string, rawMessage: GenericMessage, dataStream?: Readable): Promise<UnionMessageReply> {
const errorMessageReply = await this.validateTenant(tenant) ?? await this.validateMessageIntegrity(rawMessage);
if (errorMessageReply !== undefined) {
return errorMessageReply;
Expand Down
6 changes: 3 additions & 3 deletions src/handlers/protocols-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ export class ProtocolsQueryHandler implements MethodHandler {
};
removeUndefinedProperties(query);

const { messages: entries } = await this.messageStore.query(tenant, [ query ]);
const { messages } = await this.messageStore.query(tenant, [ query ]);

return {
status: { code: 200, detail: 'OK' },
entries
status : { code: 200, detail: 'OK' },
entries : messages as ProtocolsConfigureMessage[]
};
};

Expand Down
7 changes: 5 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ export type { EventsGetMessage, EventsGetReply } from './types/event-types.js';
export type { Filter, GenericMessage, MessageSort, Pagination } from './types/message-types.js';
export type { MessagesGetMessage, MessagesGetReply } from './types/messages-types.js';
export type { PermissionConditions, PermissionScope, PermissionsGrantDescriptor, PermissionsGrantMessage, PermissionsRequestDescriptor, PermissionsRequestMessage, PermissionsRevokeDescriptor, PermissionsRevokeMessage } from './types/permissions-types.js';
export type { ProtocolsConfigureDescriptor, ProtocolDefinition, ProtocolTypes, ProtocolRuleSet, ProtocolsQueryFilter, ProtocolsConfigureMessage, ProtocolsQueryMessage } from './types/protocols-types.js';
export type { ProtocolsConfigureDescriptor, ProtocolDefinition, ProtocolTypes, ProtocolRuleSet, ProtocolsQueryFilter, ProtocolsConfigureMessage, ProtocolsQueryMessage, ProtocolsQueryReply } from './types/protocols-types.js';
export type { EncryptionProperty, RecordsDeleteMessage, RecordsQueryMessage, RecordsQueryReply, RecordsQueryReplyEntry, RecordsReadReply, RecordsWriteDescriptor, RecordsWriteMessage } from './types/records-types.js';
export { SortOrder } from './types/message-types.js';
export { authenticate } from './core/auth.js';
export { AllowAllTenantGate, TenantGate } from './core/tenant-gate.js';
export { Cid } from './utils/cid.js';
export { DateSort, RecordsQuery, RecordsQueryOptions } from './interfaces/records-query.js';
Expand Down Expand Up @@ -45,6 +45,9 @@ export { RecordsDelete, RecordsDeleteOptions } from './interfaces/records-delete
export { RecordsRead, RecordsReadOptions } from './interfaces/records-read.js';
export { Secp256k1 } from './utils/secp256k1.js';
export { Signer } from './types/signer.js';
export { SortOrder } from './types/message-types.js';

// store interfaces
export { DataStoreLevel } from './store/data-store-level.js';
export { EventLogLevel } from './event-log/event-log-level.js';
export { MessageStoreLevel } from './store/message-store-level.js';
4 changes: 2 additions & 2 deletions src/interfaces/records-read.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { DwnInterfaceName, DwnMethodName } from '../core/message.js';

export type RecordsReadOptions = {
filter: RecordsFilter;
date?: string;
messageTimestamp?: string;
authorizationSigner?: Signer;
permissionsGrantId?: string;
/**
Expand Down Expand Up @@ -50,7 +50,7 @@ export class RecordsRead extends Message<RecordsReadMessage> {
interface : DwnInterfaceName.Records,
method : DwnMethodName.Read,
filter : Records.normalizeFilter(filter),
messageTimestamp : options.date ?? currentTime,
messageTimestamp : options.messageTimestamp ?? currentTime,
};

removeUndefinedProperties(descriptor);
Expand Down
4 changes: 2 additions & 2 deletions src/types/protocols-types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { GenericMessageReply } from '../core/message-reply.js';
import type { PublicJwk } from './jose-types.js';
import type { AuthorizationModel, GenericMessage, QueryResultEntry } from './message-types.js';
import type { AuthorizationModel, GenericMessage } from './message-types.js';
import type { DwnInterfaceName, DwnMethodName } from '../core/message.js';

export type ProtocolsConfigureDescriptor = {
Expand Down Expand Up @@ -146,5 +146,5 @@ export type ProtocolsQueryMessage = GenericMessage & {
};

export type ProtocolsQueryReply = GenericMessageReply & {
entries?: QueryResultEntry[];
entries?: ProtocolsConfigureMessage[];
};
9 changes: 9 additions & 0 deletions src/utils/data-stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ export class DataStream {
});
}

/**
* Reads the entire readable stream and JSON parses it into an object.
*/
public static async toObject(readableStream: Readable): Promise<object> {
const contentBytes = await DataStream.toBytes(readableStream);
const contentObject = Encoder.bytesToObject(contentBytes);
return contentObject;
}

/**
* Concatenates the array of bytes given into one Uint8Array.
*/
Expand Down
9 changes: 7 additions & 2 deletions src/utils/encoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ export class Encoder {

public static base64UrlToObject(base64urlString: string): any {
const payloadBytes = base64url.baseDecode(base64urlString);
const payloadString = Encoder.bytesToString(payloadBytes);
const payloadObject = JSON.parse(payloadString);
const payloadObject = Encoder.bytesToObject(payloadBytes);
return payloadObject;
}

Expand All @@ -30,6 +29,12 @@ export class Encoder {
return bytes;
}

public static bytesToObject(content: Uint8Array): object {
const contentString = Encoder.bytesToString(content);
const contentObject = JSON.parse(contentString);
return contentObject;
}

public static objectToBytes(obj: Record<string, any>): Uint8Array {
const objectString = JSON.stringify(obj);
const objectBytes = textEncoder.encode(objectString);
Expand Down
Loading

0 comments on commit d46facc

Please sign in to comment.