From 985c4b1ec86150fba294ef251f1ab43770681b41 Mon Sep 17 00:00:00 2001 From: Henry Tsai Date: Thu, 26 Jan 2023 13:53:49 -0800 Subject: [PATCH] #198 - removed multi-message request model --- README.md | 2 +- build/compile-validators.js | 4 +--- json-schemas/request.json | 18 --------------- src/core/request.ts | 14 ------------ src/core/response.ts | 44 ------------------------------------- src/core/types.ts | 4 ---- src/dwn.ts | 44 ++++++++++--------------------------- src/index.ts | 4 ++-- tests/core/request.spec.ts | 38 -------------------------------- 9 files changed, 15 insertions(+), 157 deletions(-) delete mode 100644 json-schemas/request.json delete mode 100644 src/core/request.ts delete mode 100644 src/core/response.ts delete mode 100644 tests/core/request.spec.ts diff --git a/README.md b/README.md index 89f25b0be..a54c0be6a 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/build/compile-validators.js b/build/compile-validators.js index b3aac548d..abd30bc28 100644 --- a/build/compile-validators.js +++ b/build/compile-validators.js @@ -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, @@ -49,8 +48,7 @@ const schemas = { ProtocolRuleSet, ProtocolsConfigure, ProtocolsQuery, - PublicJwk, - Request + PublicJwk }; const ajv = new Ajv({ code: { source: true, esm: true } }); diff --git a/json-schemas/request.json b/json-schemas/request.json deleted file mode 100644 index 4740dfca3..000000000 --- a/json-schemas/request.json +++ /dev/null @@ -1,18 +0,0 @@ -{ - "$schema": "http://json-schema.org/draft-07/schema#", - "$id": "https://identity.foundation/dwn/json-schemas/request.json", - "type": "object", - "additionalProperties": false, - "required": [ - "messages" - ], - "properties": { - "messages": { - "type": "array", - "minItems": 1, - "items": { - "type": "object" - } - } - } -} diff --git a/src/core/request.ts b/src/core/request.ts deleted file mode 100644 index b694e8416..000000000 --- a/src/core/request.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { RequestSchema } from './types.js'; -import { validateJsonSchema } from '../validator.js'; - -export class Request { - /** - * parses the provided payload into a `RequestSchema`. - */ - static parse(request: object): RequestSchema { - // throws an error if validation fails - validateJsonSchema('Request', request); - - return request as RequestSchema; - } -} \ No newline at end of file diff --git a/src/core/response.ts b/src/core/response.ts deleted file mode 100644 index f87044f31..000000000 --- a/src/core/response.ts +++ /dev/null @@ -1,44 +0,0 @@ -import { MessageReply } from './message-reply.js'; - -type ResponseStatus = { - code: number - message: string -}; - -type ResponseOptions = { - status?: ResponseStatus - replies?: MessageReply[]; -}; - -/** - * class used to create a response that will be returned for a given request - */ -export class Response { - // present ONLY if there is a general request-related issue - // e.g. malformed request. - // `status` and `replies` are mutually exclusive - status?: ResponseStatus; - // responses to individual messages provided within a request - replies?: MessageReply[]; - - constructor(opts: ResponseOptions = {}) { - const { status, replies } = opts; - if (status && replies) { - throw new Error('status and replies are mutually exclusive'); - } - - if (status) { - this.status = status; - } - - this.replies = replies || []; - } - - addMessageResult(result: MessageReply): void { - if (this.status) { - throw new Error('a response with a status cannot contain any replies'); - } - - this.replies.push(result); - } -} \ No newline at end of file diff --git a/src/core/types.ts b/src/core/types.ts index c793a4c99..99cca662e 100644 --- a/src/core/types.ts +++ b/src/core/types.ts @@ -36,7 +36,3 @@ export type DataReferencingMessage = { export type AuthCreateOptions = { signatureInput: SignatureInput }; - -export type RequestSchema = { - messages: BaseMessage[] -}; \ No newline at end of file diff --git a/src/dwn.ts b/src/dwn.ts index 1c3d4e4d5..1f0421439 100644 --- a/src/dwn.ts +++ b/src/dwn.ts @@ -1,6 +1,6 @@ +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'; @@ -8,8 +8,6 @@ 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'; @@ -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 { - let request: RequestSchema; + async processRequest(tenant: string, rawRequest: Uint8Array): Promise { + 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; } /** diff --git a/src/index.ts b/src/index.ts index 6e7ad833d..7f0256323 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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'; \ No newline at end of file +export { ProtocolsQuery, ProtocolsQueryOptions } from './interfaces/protocols/messages/protocols-query.js'; \ No newline at end of file diff --git a/tests/core/request.spec.ts b/tests/core/request.spec.ts deleted file mode 100644 index 29b5271ed..000000000 --- a/tests/core/request.spec.ts +++ /dev/null @@ -1,38 +0,0 @@ -import { expect } from 'chai'; -import { Request } from '../../src/core/request.js'; - -describe('Request', () => { - describe('parse', () => { - it('throws an exception if messages is missing', () => { - expect(() => { - const req = { }; - Request.parse(req); - }).throws('messages'); - }); - - it('throws an exception if messages is not an array', () => { - const tests = [{}, 'messages', 1, true, null]; - - for (const t of tests) { - expect(() => { - const req = { messages: t }; - Request.parse(req); - }).to.throw('array'); - } - }); - - it('throws an exception if messages is an empty array', () => { - expect(() => { - const req = { messages: [] }; - Request.parse(req); - }).throws('fewer than 1 items'); - }); - - it('returns a Request object if valid', () => { - const request = { messages: [{}] }; - const req = Request.parse(request); - - expect(req.messages.length).to.equal(1); - }); - }); -}); \ No newline at end of file