Skip to content

Commit

Permalink
Merge branch 'lirancohen/subscriptions-union-reply' into lirancohen/s…
Browse files Browse the repository at this point in the history
…ubscriptions
  • Loading branch information
LiranCohen committed Dec 21, 2023
2 parents 6a76871 + 0874763 commit 4f8a615
Show file tree
Hide file tree
Showing 25 changed files with 196 additions and 200 deletions.
27 changes: 10 additions & 17 deletions src/core/message-reply.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
import type { Readable } from 'readable-stream';
import type { GenericMessageSubscription, QueryResultEntry } from '../types/message-types.js';

type Status = {
code: number
detail: string
};

export type GenericMessageReply = {
status: Status;
};
import type { MessagesGetReplyEntry } from '../types/messages-types.js';
import type { ProtocolsConfigureMessage } from '../types/protocols-types.js';
import type { RecordsWriteReply } from '../types/records-types.js';
import type { GenericMessageReply, GenericMessageSubscription, QueryResultEntry } from '../types/message-types.js';

export function messageReplyFromError(e: unknown, code: number): GenericMessageReply {

Expand All @@ -23,20 +16,20 @@ export function messageReplyFromError(e: unknown, code: number): GenericMessageR
export type UnionMessageReply = GenericMessageReply & {
/**
* Resulting message entries or events returned from the invocation of the corresponding message.
* e.g. the resulting messages from a RecordsQuery
* Mutually exclusive with `data`.
* e.g. the resulting messages from a RecordsQuery, or array of messageCid strings for EventsGet or EventsQuery
* Mutually exclusive with `record`.
*/
entries?: QueryResultEntry[];
entries?: QueryResultEntry[] | ProtocolsConfigureMessage[] | MessagesGetReplyEntry[] | string[];

/**
* Data corresponding to the message received if applicable (e.g. RecordsRead).
* Record corresponding to the message received if applicable (e.g. RecordsRead).
* Mutually exclusive with `entries` and `cursor`.
*/
data?: Readable;
record?: RecordsWriteReply;

/**
* A cursor for pagination if applicable (e.g. RecordsQuery).
* Mutually exclusive with `data`.
* Mutually exclusive with `record`.
*/
cursor?: string;

Expand Down
4 changes: 2 additions & 2 deletions src/dwn.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import type { DataStore } from './types/data-store.js';
import type { EventLog } from './types/event-log.js';
import type { EventStream } from './types/subscriptions.js';
import type { GenericMessage } from './types/message-types.js';
import type { MessageStore } from './types/message-store.js';
import type { MethodHandler } from './types/method-handler.js';
import type { Readable } from 'readable-stream';
import type { TenantGate } from './core/tenant-gate.js';
import type { UnionMessageReply } from './core/message-reply.js';
import type { EventsGetMessage, EventsGetReply, EventsQueryMessage, EventsQueryReply, EventsSubscribeMessage, EventsSubscribeReply } from './types/events-types.js';
import type { GenericMessageReply, UnionMessageReply } from './core/message-reply.js';
import type { GenericMessage, GenericMessageReply } from './types/message-types.js';
import type { MessagesGetMessage, MessagesGetReply } from './types/messages-types.js';
import type { PermissionsGrantMessage, PermissionsRequestMessage, PermissionsRevokeMessage } from './types/permissions-types.js';
import type { ProtocolsConfigureMessage, ProtocolsQueryMessage, ProtocolsQueryReply } from './types/protocols-types.js';
Expand Down
4 changes: 2 additions & 2 deletions src/handlers/events-get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ export class EventsGetHandler implements MethodHandler {
const events = await this.eventLog.getEvents(tenant, options);

return {
status: { code: 200, detail: 'OK' },
events
status : { code: 200, detail: 'OK' },
entries : events
};
}
}
4 changes: 2 additions & 2 deletions src/handlers/events-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ export class EventsQueryHandler implements MethodHandler {
const events = await this.eventLog.queryEvents(tenant, logFilters, cursor);

return {
status: { code: 200, detail: 'OK' },
events
status : { code: 200, detail: 'OK' },
entries : events
};
}
}
4 changes: 2 additions & 2 deletions src/handlers/messages-get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ export class MessagesGetHandler implements MethodHandler {
}

return {
status: { code: 200, detail: 'OK' },
messages
status : { code: 200, detail: 'OK' },
entries : messages
};
}
}
2 changes: 1 addition & 1 deletion src/handlers/permissions-grant.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { DidResolver } from '../did/did-resolver.js';
import type { EventLog } from '../types//event-log.js';
import type { EventStream } from '../types/subscriptions.js';
import type { GenericMessageReply } from '../core/message-reply.js';
import type { GenericMessageReply } from '../types/message-types.js';
import type { KeyValues } from '../types/query-types.js';
import type { MessageStore } from '../types//message-store.js';
import type { MethodHandler } from '../types/method-handler.js';
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/permissions-request.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { DidResolver } from '../did/did-resolver.js';
import type { EventLog } from '../types//event-log.js';
import type { EventStream } from '../types/subscriptions.js';
import type { GenericMessageReply } from '../core/message-reply.js';
import type { GenericMessageReply } from '../types/message-types.js';
import type { MessageStore } from '../types//message-store.js';
import type { MethodHandler } from '../types/method-handler.js';
import type { PermissionsRequestMessage } from '../types/permissions-types.js';
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/permissions-revoke.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { DidResolver } from '../did/did-resolver.js';
import type { EventLog } from '../types/event-log.js';
import type { EventStream } from '../types/subscriptions.js';
import type { GenericMessageReply } from '../core/message-reply.js';
import type { GenericMessageReply } from '../types/message-types.js';
import type { KeyValues } from '../types/query-types.js';
import type { MessageStore } from '../types/message-store.js';
import type { MethodHandler } from '../types/method-handler.js';
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/protocols-configure.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { DidResolver } from '../did/did-resolver.js';
import type { EventLog } from '../types/event-log.js';
import type { EventStream } from '../types/subscriptions.js';
import type { GenericMessageReply } from '../core/message-reply.js';
import type { GenericMessageReply } from '../types/message-types.js';
import type { MessageStore } from '../types//message-store.js';
import type { MethodHandler } from '../types/method-handler.js';
import type { ProtocolsConfigureMessage } from '../types/protocols-types.js';
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/records-delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { DataStore } from '../types/data-store.js';
import type { DidResolver } from '../did/did-resolver.js';
import type { EventLog } from '../types/event-log.js';
import type { EventStream } from '../types/subscriptions.js';
import type { GenericMessageReply } from '../core/message-reply.js';
import type { GenericMessageReply } from '../types/message-types.js';
import type { MessageStore } from '../types//message-store.js';
import type { MethodHandler } from '../types/method-handler.js';
import type { RecordsDeleteMessage, RecordsWriteMessage } from '../types/records-types.js';
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/records-write.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { DataStore } from '../types/data-store.js';
import type { DidResolver } from '../did/did-resolver.js';
import type { EventLog } from '../types/event-log.js';
import type { EventStream } from '../types/subscriptions.js';
import type { GenericMessageReply } from '../core/message-reply.js';
import type { GenericMessageReply } from '../types/message-types.js';
import type { MessageStore } from '../types//message-store.js';
import type { MethodHandler } from '../types/method-handler.js';
import type { RecordsQueryReplyEntry, RecordsWriteMessage } from '../types/records-types.js';
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ export type { EventLog, GetEventsOptions } from './types/event-log.js';
export type { EventStream, SubscriptionReply } from './types/subscriptions.js';
export type { EventsGetMessage, EventsGetReply, EventsSubscribeDescriptor, EventsSubscribeMessage, EventsSubscribeReply, EventsSubscription } from './types/events-types.js';
export type { Filter } from './types/query-types.js';
export type { GenericMessage, MessageSort, Pagination } from './types/message-types.js';
export type { MessagesGetMessage, MessagesGetReply } from './types/messages-types.js';
export type { GenericMessage, GenericMessageReply, MessageSort, Pagination, QueryResultEntry } from './types/message-types.js';
export type { MessagesGetMessage, MessagesGetReply, MessagesGetReplyEntry } from './types/messages-types.js';
export type { PermissionConditions, PermissionScope, PermissionsGrantDescriptor } from './types/permissions-grant-descriptor.js';
export type { PermissionsGrantMessage, PermissionsRequestDescriptor, PermissionsRequestMessage, PermissionsRevokeDescriptor, PermissionsRevokeMessage } from './types/permissions-types.js';
export type { ProtocolsConfigureDescriptor, ProtocolDefinition, ProtocolTypes, ProtocolRuleSet, ProtocolsQueryFilter, ProtocolsConfigureMessage, ProtocolsQueryMessage, ProtocolsQueryReply } from './types/protocols-types.js';
Expand Down
7 changes: 3 additions & 4 deletions src/types/events-types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { GenericMessageReply } from '../core/message-reply.js';
import type { ProtocolsQueryFilter } from './protocols-types.js';
import type { AuthorizationModel, GenericMessage } from './message-types.js';
import type { AuthorizationModel, GenericMessage, GenericMessageReply } from './message-types.js';
import type { DwnInterfaceName, DwnMethodName } from '../enums/dwn-interface-method.js';
import type { RangeCriterion, RangeFilter } from './query-types.js';

Expand Down Expand Up @@ -39,7 +38,7 @@ export type EventsGetMessage = GenericMessage & {
};

export type EventsGetReply = GenericMessageReply & {
events?: string[];
entries?: string[];
};

export type EventsSubscribeMessage = {
Expand Down Expand Up @@ -80,5 +79,5 @@ export type EventsQueryMessage = GenericMessage & {
};

export type EventsQueryReply = GenericMessageReply & {
events?: string[];
entries?: string[];
};
9 changes: 9 additions & 0 deletions src/types/message-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,15 @@ export type Pagination = {
limit?: number;
};

type Status = {
code: number
detail: string
};

export type GenericMessageReply = {
status: Status;
};

export type MessageSort = {
dateCreated?: SortDirection;
datePublished?: SortDirection;
Expand Down
7 changes: 3 additions & 4 deletions src/types/messages-types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { GenericMessageReply } from '../core/message-reply.js';
import type { AuthorizationModel, GenericMessage } from './message-types.js';
import type { AuthorizationModel, GenericMessage, GenericMessageReply } from './message-types.js';
import type { DwnInterfaceName, DwnMethodName } from '../enums/dwn-interface-method.js';

export type MessagesGetDescriptor = {
Expand All @@ -22,5 +21,5 @@ export type MessagesGetReplyEntry = {
};

export type MessagesGetReply = GenericMessageReply & {
messages?: MessagesGetReplyEntry[];
};
entries?: MessagesGetReplyEntry[];
};
3 changes: 1 addition & 2 deletions src/types/method-handler.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { GenericMessage } from './message-types.js';
import type { GenericMessageReply } from '../core/message-reply.js';
import type { Readable } from 'readable-stream';
import type { GenericMessage, GenericMessageReply } from './message-types.js';

/**
* Interface that defines a message handler of a specific method.
Expand Down
3 changes: 1 addition & 2 deletions src/types/protocols-types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { GenericMessageReply } from '../core/message-reply.js';
import type { PublicJwk } from './jose-types.js';
import type { AuthorizationModel, GenericMessage } from './message-types.js';
import type { AuthorizationModel, GenericMessage, GenericMessageReply } from './message-types.js';
import type { DwnInterfaceName, DwnMethodName } from '../enums/dwn-interface-method.js';

export type ProtocolsConfigureDescriptor = {
Expand Down
19 changes: 9 additions & 10 deletions src/types/records-types.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import type { EncryptionAlgorithm } from '../utils/encryption.js';
import type { GeneralJws } from './jws-types.js';
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 { AuthorizationModel, GenericMessage, GenericSignaturePayload, Pagination } from './message-types.js';
import type { AuthorizationModel, GenericMessage, GenericMessageReply, GenericSignaturePayload, Pagination } from './message-types.js';
import type { DwnInterfaceName, DwnMethodName } from '../enums/dwn-interface-method.js';
import type { RangeCriterion, RangeFilter } from './query-types.js';

Expand Down Expand Up @@ -32,7 +31,13 @@ export type RecordsWriteDescriptor = {
dataFormat: string;
};

export type RecordsWriteReply = GenericMessageReply;
export type RecordsWriteReply = RecordsWriteMessage & {
/**
* The initial write of the record if the returned RecordsWrite message itself is not the initial write.
*/
initialWrite?: RecordsWriteMessage;
data: Readable;
};

/**
* Internal RecordsWrite message representation that can be in an incomplete state.
Expand Down Expand Up @@ -166,13 +171,7 @@ export type RecordsReadMessage = {
};

export type RecordsReadReply = GenericMessageReply & {
record?: RecordsWriteMessage & {
/**
* The initial write of the record if the returned RecordsWrite message itself is not the initial write.
*/
initialWrite?: RecordsWriteMessage;
data: Readable;
}
record?: RecordsWriteReply
};

export type RecordsReadDescriptor = {
Expand Down
3 changes: 1 addition & 2 deletions src/types/subscriptions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { GenericMessageReply } from '../core/message-reply.js';
import type { EventsSubscribeMessage, EventsSubscription } from './events-types.js';
import type { Filter, KeyValues } from './query-types.js';
import type { GenericMessage, GenericMessageHandler, GenericMessageSubscription } from './message-types.js';
import type { GenericMessage, GenericMessageHandler, GenericMessageReply, GenericMessageSubscription } from './message-types.js';
import type { RecordsSubscribeMessage, RecordsSubscription } from './records-types.js';

export interface EventStream {
Expand Down
2 changes: 1 addition & 1 deletion tests/core/message-reply.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { GenericMessageReply } from '../../src/core/message-reply.js';
import type { GenericMessageReply } from '../../src/types/message-types.js';

import { expect } from 'chai';
import { messageReplyFromError } from '../../src/core/message-reply.js';
Expand Down
2 changes: 1 addition & 1 deletion tests/dwn.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export function testDwnClass(): void {
const reply: EventsGetReply = await dwn.processMessage(alice.did, message);

expect(reply.status.code).to.equal(200);
expect(reply.events).to.be.empty;
expect(reply.entries).to.be.empty;
expect((reply as any).data).to.not.exist;
});

Expand Down
18 changes: 9 additions & 9 deletions tests/handlers/events-get.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export function testEventsGetHandler(): void {
const reply = await eventsGetHandler.handle({ tenant: bob.did, message });

expect(reply.status.code).to.equal(401);
expect(reply.events).to.not.exist;
expect(reply.entries).to.not.exist;
});

it('returns a 400 if message is invalid', async () => {
Expand All @@ -74,7 +74,7 @@ export function testEventsGetHandler(): void {
const reply = await eventsGetHandler.handle({ tenant: alice.did, message });

expect(reply.status.code).to.equal(400);
expect(reply.events).to.not.exist;
expect(reply.entries).to.not.exist;
});

it('returns all events for a tenant if cursor is not provided', async () => {
Expand All @@ -96,10 +96,10 @@ export function testEventsGetHandler(): void {

expect(reply.status.code).to.equal(200);
expect((reply as any).data).to.not.exist;
expect(reply.events?.length).to.equal(expectedCids.length);
expect(reply.entries?.length).to.equal(expectedCids.length);

for (let i = 0; i < reply.events!.length; i += 1) {
expect(reply.events![i]).to.equal(expectedCids[i]);
for (let i = 0; i < reply.entries!.length; i += 1) {
expect(reply.entries![i]).to.equal(expectedCids[i]);
}
});

Expand All @@ -118,7 +118,7 @@ export function testEventsGetHandler(): void {

expect(reply.status.code).to.equal(200);

const cursor = reply.events![reply.events!.length - 1];
const cursor = reply.entries![reply.entries!.length - 1];
const expectedCids: string[] = [];

for (let i = 0; i < 3; i += 1) {
Expand All @@ -135,10 +135,10 @@ export function testEventsGetHandler(): void {

expect(reply.status.code).to.equal(200);
expect((reply as any).data).to.not.exist;
expect(reply.events!.length).to.equal(expectedCids.length);
expect(reply.entries!.length).to.equal(expectedCids.length);

for (let i = 0; i < reply.events!.length; i += 1) {
expect(reply.events![i]).to.equal(expectedCids[i]);
for (let i = 0; i < reply.entries!.length; i += 1) {
expect(reply.entries![i]).to.equal(expectedCids[i]);
}
});
});
Expand Down
8 changes: 4 additions & 4 deletions tests/handlers/events-query.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export function testEventsQueryHandler(): void {
const reply = await eventsQueryHandler.handle({ tenant: bob.did, message });

expect(reply.status.code).to.equal(401);
expect(reply.events).to.not.exist;
expect(reply.entries).to.not.exist;
});

it('returns a 400 if message is invalid', async () => {
Expand All @@ -79,7 +79,7 @@ export function testEventsQueryHandler(): void {
const reply = await eventsQueryHandler.handle({ tenant: alice.did, message });

expect(reply.status.code).to.equal(400);
expect(reply.events).to.not.exist;
expect(reply.entries).to.not.exist;
});

it('returns 400 if no filters are provided', async () => {
Expand All @@ -94,7 +94,7 @@ export function testEventsQueryHandler(): void {
const reply = await eventsQueryHandler.handle({ tenant: alice.did, message });

expect(reply.status.code).to.equal(400);
expect(reply.events).to.not.exist;
expect(reply.entries).to.not.exist;
});

it('returns 400 if an empty filter without properties is provided', async () => {
Expand All @@ -109,7 +109,7 @@ export function testEventsQueryHandler(): void {
const reply = await eventsQueryHandler.handle({ tenant: alice.did, message });

expect(reply.status.code).to.equal(400);
expect(reply.events).to.not.exist;
expect(reply.entries).to.not.exist;
});
});
}
Loading

0 comments on commit 4f8a615

Please sign in to comment.