Skip to content

Commit

Permalink
Deprecate EventsGet in favor of EventsQuery without filters (#764)
Browse files Browse the repository at this point in the history
`EventsGet` was used for sync before `EventsQuery` was made available,
which allows filtering events/messages vs simply getting all of them.

This PR removes the `EventsGet` interface method and removes the
restriction in `EventsQuery` to allow querying without any filters.
  • Loading branch information
LiranCohen authored Jun 20, 2024
1 parent c2bc5c4 commit 8446cdb
Show file tree
Hide file tree
Showing 18 changed files with 97 additions and 448 deletions.
2 changes: 0 additions & 2 deletions build/compile-validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import AuthorizationDelegatedGrant from '../json-schemas/authorization-delegated
import AuthorizationOwner from '../json-schemas/authorization-owner.json' assert { type: 'json' };
import Definitions from '../json-schemas/definitions.json' assert { type: 'json' };
import EventsFilter from '../json-schemas/interface-methods/events-filter.json' assert { type: 'json' };
import EventsGet from '../json-schemas/interface-methods/events-get.json' assert { type: 'json' };
import EventsQuery from '../json-schemas/interface-methods/events-query.json' assert { type: 'json' };
import EventsSubscribe from '../json-schemas/interface-methods/events-subscribe.json' assert { type: 'json' };
import GeneralJwk from '../json-schemas/jwk/general-jwk.json' assert { type: 'json' };
Expand Down Expand Up @@ -64,7 +63,6 @@ const schemas = {
RecordsWriteDataEncoded,
RecordsWriteUnidentified,
EventsFilter,
EventsGet,
EventsQuery,
EventsSubscribe,
Definitions,
Expand Down
44 changes: 0 additions & 44 deletions json-schemas/interface-methods/events-get.json

This file was deleted.

3 changes: 1 addition & 2 deletions json-schemas/interface-methods/events-query.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
"required": [
"interface",
"method",
"messageTimestamp",
"filters"
"messageTimestamp"
],
"properties": {
"interface": {
Expand Down
2 changes: 1 addition & 1 deletion src/core/message-reply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ 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, or array of messageCid strings for EventsGet or EventsQuery
* e.g. the resulting messages from a RecordsQuery, or array of messageCid strings for EventsQuery
* Mutually exclusive with `record`.
*/
entries?: QueryResultEntry[] | ProtocolsConfigureMessage[] | MessagesGetReplyEntry[] | string[];
Expand Down
8 changes: 1 addition & 7 deletions src/dwn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@ import type { Readable } from 'readable-stream';
import type { ResumableTaskStore } from './types/resumable-task-store.js';
import type { TenantGate } from './core/tenant-gate.js';
import type { UnionMessageReply } from './core/message-reply.js';
import type { EventsGetMessage, EventsGetReply, EventsQueryMessage, EventsQueryReply, EventsSubscribeMessage, EventsSubscribeMessageOptions, EventsSubscribeReply, MessageSubscriptionHandler } from './types/events-types.js';
import type { EventsQueryMessage, EventsQueryReply, EventsSubscribeMessage, EventsSubscribeMessageOptions, EventsSubscribeReply, MessageSubscriptionHandler } from './types/events-types.js';
import type { GenericMessage, GenericMessageReply } from './types/message-types.js';
import type { MessagesGetMessage, MessagesGetReply } from './types/messages-types.js';
import type { ProtocolsConfigureMessage, ProtocolsQueryMessage, ProtocolsQueryReply } from './types/protocols-types.js';
import type { RecordsDeleteMessage, RecordsQueryMessage, RecordsQueryReply, RecordsReadMessage, RecordsReadReply, RecordsSubscribeMessage, RecordsSubscribeMessageOptions, RecordsSubscribeReply, RecordSubscriptionHandler, RecordsWriteMessage, RecordsWriteMessageOptions } from './types/records-types.js';

import { AllowAllTenantGate } from './core/tenant-gate.js';
import { EventsGetHandler } from './handlers/events-get.js';
import { EventsQueryHandler } from './handlers/events-query.js';
import { EventsSubscribeHandler } from './handlers/events-subscribe.js';
import { Message } from './core/message.js';
Expand Down Expand Up @@ -67,10 +66,6 @@ export class Dwn {
);

this.methodHandlers = {
[DwnInterfaceName.Events + DwnMethodName.Get]: new EventsGetHandler(
this.didResolver,
this.eventLog,
),
[DwnInterfaceName.Events + DwnMethodName.Query]: new EventsQueryHandler(
this.didResolver,
this.eventLog,
Expand Down Expand Up @@ -165,7 +160,6 @@ 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: EventsGetMessage): Promise<EventsGetReply>;
public async processMessage(tenant: string, rawMessage: EventsQueryMessage): Promise<EventsQueryReply>;
public async processMessage(
tenant: string, rawMessage: EventsSubscribeMessage, options?: EventsSubscribeMessageOptions): Promise<EventsSubscribeReply>;
Expand Down
42 changes: 0 additions & 42 deletions src/handlers/events-get.ts

This file was deleted.

5 changes: 4 additions & 1 deletion src/handlers/events-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ export class EventsQueryHandler implements MethodHandler {
return messageReplyFromError(e, 401);
}

const eventFilters = Events.convertFilters(message.descriptor.filters);
// if no filter is present in the the `EventsQuery` descriptor, we pass an empty array of filters to the `queryEvents` method
// this will return all events in the event log for the given tenant beyond the cursor provided.
// if no cursor is provided, it will return all events
const eventFilters = message.descriptor.filters ? Events.convertFilters(message.descriptor.filters) : [];
const { events, cursor } = await this.eventLog.queryEvents(tenant, eventFilters, message.descriptor.cursor);

return {
Expand Down
3 changes: 1 addition & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// export everything that we want to be consumable
export type { DwnConfig } from './dwn.js';
export type { EventLog } from './types/event-log.js';
export type { EventsGetMessage, EventsGetReply, EventsQueryMessage, EventsQueryReply, EventsSubscribeDescriptor, EventsSubscribeMessage, EventsSubscribeReply, MessageSubscriptionHandler as EventSubscriptionHandler } from './types/events-types.js';
export type { EventsQueryMessage, EventsQueryReply, EventsSubscribeDescriptor, EventsSubscribeMessage, EventsSubscribeReply, MessageSubscriptionHandler } from './types/events-types.js';
export type { EventListener, EventStream, EventSubscription, MessageEvent, SubscriptionReply } from './types/subscriptions.js';
export type { GenericMessage, GenericMessageReply, MessageSort, MessageSubscription, Pagination, QueryResultEntry } from './types/message-types.js';
export type { MessagesGetMessage, MessagesGetReply, MessagesGetReplyEntry } from './types/messages-types.js';
Expand All @@ -23,7 +23,6 @@ export { DwnConstant } from './core/dwn-constant.js';
export { DwnError, DwnErrorCode } from './core/dwn-error.js';
export { DwnInterfaceName, DwnMethodName } from './enums/dwn-interface-method.js';
export { Encoder } from './utils/encoder.js';
export { EventsGet, EventsGetOptions } from './interfaces/events-get.js';
export { EventsQuery, EventsQueryOptions } from './interfaces/events-query.js';
export { EventsSubscribe, EventsSubscribeOptions } from './interfaces/events-subscribe.js';
export { Encryption, EncryptionAlgorithm } from './utils/encryption.js';
Expand Down
44 changes: 0 additions & 44 deletions src/interfaces/events-get.ts

This file was deleted.

6 changes: 3 additions & 3 deletions src/interfaces/events-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { validateProtocolUrlNormalized, validateSchemaUrlNormalized } from '../u

export type EventsQueryOptions = {
signer: Signer;
filters: EventsFilter[];
filters?: EventsFilter[];
cursor?: PaginationCursor;
messageTimestamp?: string;
};
Expand All @@ -23,7 +23,7 @@ export class EventsQuery extends AbstractMessage<EventsQueryMessage>{
Message.validateJsonSchema(message);
await Message.validateSignatureStructure(message.authorization.signature, message.descriptor);

for (const filter of message.descriptor.filters) {
for (const filter of message.descriptor.filters || []) {
if ('protocol' in filter && filter.protocol !== undefined) {
validateProtocolUrlNormalized(filter.protocol);
}
Expand All @@ -39,7 +39,7 @@ export class EventsQuery extends AbstractMessage<EventsQueryMessage>{
const descriptor: EventsQueryDescriptor = {
interface : DwnInterfaceName.Events,
method : DwnMethodName.Query,
filters : Events.normalizeFilters(options.filters),
filters : options.filters ? Events.normalizeFilters(options.filters) : undefined,
messageTimestamp : options.messageTimestamp ?? Time.getCurrentTimestamp(),
cursor : options.cursor,
};
Expand Down
19 changes: 1 addition & 18 deletions src/types/events-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,6 @@ export type EventsRecordsFilter = {
*/
export type EventsFilter = EventsMessageFilter | EventsRecordsFilter;

export type EventsGetDescriptor = {
interface: DwnInterfaceName.Events;
method: DwnMethodName.Get;
cursor?: PaginationCursor;
messageTimestamp: string;
};

export type EventsGetMessage = GenericMessage & {
authorization: AuthorizationModel; // overriding `GenericMessage` with `authorization` being required
descriptor: EventsGetDescriptor;
};

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

export type MessageSubscriptionHandler = (event: MessageEvent) => void;

export type EventsSubscribeMessageOptions = {
Expand All @@ -77,7 +60,7 @@ export type EventsQueryDescriptor = {
interface: DwnInterfaceName.Events;
method: DwnMethodName.Query;
messageTimestamp: string;
filters: EventsFilter[];
filters?: EventsFilter[];
cursor?: PaginationCursor;
};

Expand Down
8 changes: 4 additions & 4 deletions tests/dwn.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { DidResolver } from '@web5/dids';
import type { EventStream } from '../src/types/subscriptions.js';
import type { ActiveTenantCheckResult, EventsGetReply, TenantGate } from '../src/index.js';
import type { ActiveTenantCheckResult, EventsQueryReply, TenantGate } from '../src/index.js';
import type { DataStore, EventLog, MessageStore, ResumableTaskStore } from '../src/index.js';

import chaiAsPromised from 'chai-as-promised';
Expand Down Expand Up @@ -78,11 +78,11 @@ export function testDwnClass(): void {
expect(reply.entries).to.be.empty;
});

it('should process an EventsGet message', async () => {
it('should process an EventsQuery message', async () => {
const alice = await TestDataGenerator.generateDidKeyPersona();
const { message } = await TestDataGenerator.generateEventsGet({ author: alice });
const { message } = await TestDataGenerator.generateEventsQuery({ author: alice });

const reply: EventsGetReply = await dwn.processMessage(alice.did, message);
const reply: EventsQueryReply = await dwn.processMessage(alice.did, message);

expect(reply.status.code).to.equal(200);
expect(reply.entries).to.be.empty;
Expand Down
Loading

0 comments on commit 8446cdb

Please sign in to comment.