Skip to content

Commit 149c713

Browse files
authored
EventsGet (#305)
Implement `EventsGet` interface
1 parent c6733d6 commit 149c713

File tree

16 files changed

+417
-7
lines changed

16 files changed

+417
-7
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# Decentralized Web Node (DWN) SDK
44

55
Code Coverage
6-
![Statements](https://img.shields.io/badge/statements-93.62%25-brightgreen.svg?style=flat) ![Branches](https://img.shields.io/badge/branches-93.2%25-brightgreen.svg?style=flat) ![Functions](https://img.shields.io/badge/functions-91.24%25-brightgreen.svg?style=flat) ![Lines](https://img.shields.io/badge/lines-93.62%25-brightgreen.svg?style=flat)
6+
![Statements](https://img.shields.io/badge/statements-93.69%25-brightgreen.svg?style=flat) ![Branches](https://img.shields.io/badge/branches-93.17%25-brightgreen.svg?style=flat) ![Functions](https://img.shields.io/badge/functions-91.36%25-brightgreen.svg?style=flat) ![Lines](https://img.shields.io/badge/lines-93.69%25-brightgreen.svg?style=flat)
77

88
## Introduction
99

build/compile-validators.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import mkdirp from 'mkdirp';
1818
import standaloneCode from 'ajv/dist/standalone/index.js';
1919

2020
import Definitions from '../json-schemas/definitions.json' assert { type: 'json' };
21+
import EventsGet from '../json-schemas/events/events-get.json' assert { type: 'json' };
2122
import GeneralJwk from '../json-schemas/jwk/general-jwk.json' assert { type: 'json' };
2223
import GeneralJws from '../json-schemas/general-jws.json' assert { type: 'json' };
2324
import HooksWrite from '../json-schemas/hooks/hooks-write.json' assert { type: 'json' };
@@ -39,6 +40,7 @@ const schemas = {
3940
RecordsDelete,
4041
RecordsQuery,
4142
RecordsWrite,
43+
EventsGet,
4244
Definitions,
4345
GeneralJwk,
4446
GeneralJws,
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema#",
3+
"$id": "https://identity.foundation/dwn/json-schemas/events-get.json",
4+
"type": "object",
5+
"additionalProperties": false,
6+
"required": [
7+
"authorization",
8+
"descriptor"
9+
],
10+
"properties": {
11+
"authorization": {
12+
"$ref": "https://identity.foundation/dwn/json-schemas/general-jws.json"
13+
},
14+
"descriptor": {
15+
"type": "object",
16+
"additionalProperties": false,
17+
"required": [
18+
"interface",
19+
"method"
20+
],
21+
"properties": {
22+
"interface": {
23+
"enum": [
24+
"Events"
25+
],
26+
"type": "string"
27+
},
28+
"method": {
29+
"enum": [
30+
"Get"
31+
],
32+
"type": "string"
33+
},
34+
"watermark": {
35+
"type": "string"
36+
}
37+
}
38+
}
39+
}
40+
}

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@tbd54566975/dwn-sdk-js",
3-
"version": "0.0.29",
3+
"version": "0.0.30",
44
"description": "A reference implementation of https://identity.foundation/decentralized-web-node/spec/",
55
"type": "module",
66
"types": "./dist/esm/src/index.d.ts",
@@ -142,4 +142,4 @@
142142
"url": "https://github.com/TBD54566975/dwn-sdk-js/issues"
143143
},
144144
"homepage": "https://github.com/TBD54566975/dwn-sdk-js#readme"
145-
}
145+
}

src/core/message-reply.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export class MessageReply {
2020
status: Status;
2121

2222
/**
23-
* Resulting message entries returned from the invocation of the corresponding message.
23+
* Resulting message entries or events returned from the invocation of the corresponding message.
2424
* e.g. the resulting messages from a RecordsQuery
2525
* Mutually exclusive with `data`.
2626
*/

src/core/message.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { lexicographicalCompare } from '../utils/string.js';
99
import { validateJsonSchema } from '../schema-validator.js';
1010

1111
export enum DwnInterfaceName {
12+
Events = 'Events',
1213
Hooks = 'Hooks',
1314
Permissions = 'Permissions',
1415
Protocols = 'Protocols',
@@ -17,6 +18,7 @@ export enum DwnInterfaceName {
1718

1819
export enum DwnMethodName {
1920
Configure = 'Configure',
21+
Get = 'Get',
2022
Grant = 'Grant',
2123
Query = 'Query',
2224
Read = 'Read',

src/dwn.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { AllowAllTenantGate } from './core/tenant-gate.js';
1111
import { DataStoreLevel } from './store/data-store-level.js';
1212
import { DidResolver } from './did/did-resolver.js';
1313
import { EventLogLevel } from './event-log/event-log-level.js';
14+
import { EventsGetHandler } from './interfaces/events/handlers/events-get.js';
1415
import { MessageReply } from './core/message-reply.js';
1516
import { MessageStoreLevel } from './store/message-store-level.js';
1617
import { PermissionsRequestHandler } from './interfaces/permissions/handlers/permissions-request.js';
@@ -38,6 +39,7 @@ export class Dwn {
3839
this.tenantGate = config.tenantGate!;
3940

4041
this.methodHandlers = {
42+
[DwnInterfaceName.Events + DwnMethodName.Get] : new EventsGetHandler(this.didResolver, this.eventLog),
4143
[DwnInterfaceName.Permissions + DwnMethodName.Request] : new PermissionsRequestHandler(this.didResolver, this.messageStore, this.dataStore),
4244
[DwnInterfaceName.Protocols + DwnMethodName.Configure] : new ProtocolsConfigureHandler(
4345
this.didResolver, this.messageStore, this.dataStore, this.eventLog),

src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
// export everything that we want to be consumable
55
export type { DwnConfig } from './dwn.js';
66
export type { DwnServiceEndpoint, ServiceEndpoint, DidDocument, DidResolutionResult, DidResolutionMetadata, DidDocumentMetadata, VerificationMethod } from './did/did-resolver.js';
7+
export type { EventLog, Event } from './event-log/event-log.js';
8+
export type { EventsGetMessage, EventsGetReply } from './interfaces/events/types.js';
79
export type { HooksWriteMessage } from './interfaces/hooks/types.js';
810
export type { ProtocolDefinition, ProtocolRuleSet, ProtocolsConfigureMessage, ProtocolsQueryMessage } from './interfaces/protocols/types.js';
911
export type { RecordsDeleteMessage, RecordsQueryMessage, RecordsWriteMessage } from './interfaces/records/types.js';
@@ -22,6 +24,8 @@ export { DwnConstant } from './core/dwn-constant.js';
2224
export { DwnError, DwnErrorCode } from './core/dwn-error.js';
2325
export { DwnInterfaceName, DwnMethodName } from './core/message.js';
2426
export { Encoder } from './utils/encoder.js';
27+
export { EventLogLevel } from './event-log/event-log-level.js';
28+
export { EventsGet, EventsGetOptions } from './interfaces/events/messages/events-get.js';
2529
export { Encryption, EncryptionAlgorithm } from './utils/encryption.js';
2630
export { EncryptionInput, KeyEncryptionInput, RecordsWrite, RecordsWriteOptions, CreateFromOptions } from './interfaces/records/messages/records-write.js';
2731
export { HooksWrite, HooksWriteOptions } from './interfaces/hooks/messages/hooks-write.js';
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import type { DidResolver } from '../../../index.js';
2+
import type { EventLog } from '../../../event-log/event-log.js';
3+
import type { GetEventsOptions } from '../../../event-log/event-log.js';
4+
import type { MethodHandler } from '../../types.js';
5+
import type { EventsGetMessage, EventsGetReply } from '../types.js';
6+
7+
import { EventsGet } from '../messages/events-get.js';
8+
import { MessageReply } from '../../../core/message-reply.js';
9+
import { authenticate, authorize } from '../../../core/auth.js';
10+
11+
type HandleArgs = {tenant: string, message: EventsGetMessage};
12+
13+
export class EventsGetHandler implements MethodHandler {
14+
constructor(private didResolver: DidResolver, private eventLog: EventLog) {}
15+
16+
public async handle({ tenant, message }: HandleArgs): Promise<EventsGetReply> {
17+
let eventsGet: EventsGet;
18+
19+
try {
20+
eventsGet = await EventsGet.parse(message);
21+
} catch (e) {
22+
return MessageReply.fromError(e, 400);
23+
}
24+
25+
try {
26+
await authenticate(message.authorization, this.didResolver);
27+
await authorize(tenant, eventsGet);
28+
} catch (e) {
29+
return MessageReply.fromError(e, 401);
30+
}
31+
32+
// if watermark was provided in message, get all events _after_ the watermark.
33+
// Otherwise, get all events.
34+
let options: GetEventsOptions | undefined;
35+
if (message.descriptor.watermark) {
36+
options = { gt: message.descriptor.watermark };
37+
}
38+
39+
const events = await this.eventLog.getEvents(tenant, options);
40+
41+
return {
42+
status: { code: 200, detail: 'OK' },
43+
events
44+
};
45+
}
46+
}

0 commit comments

Comments
 (0)