Skip to content

Commit

Permalink
#198 - removed multi-message request model
Browse files Browse the repository at this point in the history
  • Loading branch information
thehenrytsai authored Jan 26, 2023
1 parent ba253a4 commit 985c4b1
Show file tree
Hide file tree
Showing 9 changed files with 15 additions and 157 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Decentralized Web Node (DWN) SDK

Code Coverage
![Statements](https://img.shields.io/badge/statements-92.65%25-brightgreen.svg?style=flat) ![Branches](https://img.shields.io/badge/branches-91.98%25-brightgreen.svg?style=flat) ![Functions](https://img.shields.io/badge/functions-89.3%25-yellow.svg?style=flat) ![Lines](https://img.shields.io/badge/lines-92.65%25-brightgreen.svg?style=flat)
![Statements](https://img.shields.io/badge/statements-93.6%25-brightgreen.svg?style=flat) ![Branches](https://img.shields.io/badge/branches-91.96%25-brightgreen.svg?style=flat) ![Functions](https://img.shields.io/badge/functions-90.38%25-brightgreen.svg?style=flat) ![Lines](https://img.shields.io/badge/lines-93.6%25-brightgreen.svg?style=flat)

## Introduction

Expand Down
4 changes: 1 addition & 3 deletions build/compile-validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import ProtocolsQuery from '../json-schemas/protocols/protocols-query.json' asse
import PublicJwk from '../json-schemas/jwk/public-jwk.json' assert { type: 'json' };
import RecordsQuery from '../json-schemas/records/records-query.json' assert { type: 'json' };
import RecordsWrite from '../json-schemas/records/records-write.json' assert { type: 'json' };
import Request from '../json-schemas/request.json' assert { type: 'json' };

const schemas = {
RecordsQuery,
Expand All @@ -49,8 +48,7 @@ const schemas = {
ProtocolRuleSet,
ProtocolsConfigure,
ProtocolsQuery,
PublicJwk,
Request
PublicJwk
};

const ajv = new Ajv({ code: { source: true, esm: true } });
Expand Down
18 changes: 0 additions & 18 deletions json-schemas/request.json

This file was deleted.

14 changes: 0 additions & 14 deletions src/core/request.ts

This file was deleted.

44 changes: 0 additions & 44 deletions src/core/response.ts

This file was deleted.

4 changes: 0 additions & 4 deletions src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,3 @@ export type DataReferencingMessage = {
export type AuthCreateOptions = {
signatureInput: SignatureInput
};

export type RequestSchema = {
messages: BaseMessage[]
};
44 changes: 11 additions & 33 deletions src/dwn.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import type { BaseMessage } from './core/types.js';
import type { DidMethodResolver } from './did/did-resolver.js';
import type { MessageStore } from './store/message-store.js';
import type { BaseMessage, RequestSchema } from './core/types.js';
import type { Interface, MethodHandler } from './interfaces/types.js';

import { DidResolver } from './did/did-resolver.js';
import { Encoder } from './utils/encoder.js';
import { Message } from './core/message.js';
import { MessageReply } from './core/message-reply.js';
import { MessageStoreLevel } from './store/message-store-level.js';
import { Request } from './core/request.js';
import { Response } from './core/response.js';

import { PermissionsInterface } from './interfaces/permissions/permissions-interface.js';
import { ProtocolsInterface } from './interfaces/protocols/protocols-interface.js';
Expand Down Expand Up @@ -60,42 +58,22 @@ export class Dwn {
}

/**
* Processes the given DWN request and returns with a DWN response.
* @param tenant The tenant DID to route the given request to.
* Processes the given DWN message given as raw bytes and returns with a message reply.
* @param tenant The tenant DID to route the given message to.
*/
async processRequest(tenant: string, rawRequest: Uint8Array): Promise<Response> {
let request: RequestSchema;
async processRequest(tenant: string, rawRequest: Uint8Array): Promise<MessageReply> {
let message: BaseMessage;
try {
const requestString = Encoder.bytesToString(rawRequest);
request = JSON.parse(requestString);
} catch {
throw new Error('expected request to be valid JSON');
}

try {
request = Request.parse(request);
} catch (e) {
return new Response({
status: { code: 400, message: e.message }
message = JSON.parse(requestString);
} catch (error) {
return new MessageReply({
status: { code: 400, detail: error.message }
});
}

const response = new Response();

for (const message of request.messages) {
let result;
try {
result = await this.processMessage(tenant, message);
} catch (error) {
result = new MessageReply({
status: { code: 500, detail: error.message }
});
}

response.addMessageResult(result);
}

return response;
const messageReply = await this.processMessage(tenant, message);
return messageReply;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ export { DidResolver, DidMethodResolver } from './did/did-resolver.js';
export { Dwn } from './dwn.js';
export { Encoder } from './utils/encoder.js';
export { HooksWrite, HooksWriteOptions } from './interfaces/hooks/messages/hooks-write.js';
export { MessageReply } from './core/message-reply.js';
export { MessageStore } from './store/message-store.js';
export { MessageStoreLevel } from './store/message-store-level.js';
export { PrivateJwk, PublicJwk } from './jose/types.js';
export { ProtocolsConfigure, ProtocolsConfigureOptions } from './interfaces/protocols/messages/protocols-configure.js';
export { ProtocolsQuery, ProtocolsQueryOptions } from './interfaces/protocols/messages/protocols-query.js';
export { Response } from './core/response.js';
export { ProtocolsQuery, ProtocolsQueryOptions } from './interfaces/protocols/messages/protocols-query.js';
38 changes: 0 additions & 38 deletions tests/core/request.spec.ts

This file was deleted.

0 comments on commit 985c4b1

Please sign in to comment.