generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This PR introduces an `EventStream` interface and an implementation based on `EventEmitter` which emits events for any interfaces which are saved as a part of the DWN's message store. (ie. not Query/Read/Subscribe messages). We also introduce an `EventsSubscribe` interface that follows the same authorization model as `EventsGet` and `EventsQuery`, which only allows access to the tenant owner. In a subsequent PR we will introduce `RecordsSubscribe`, as well as some additional enhancements. Co-authored-by: Liran Cohen <[email protected]> Co-authored-by: Andor Kesselman <[email protected]>
- Loading branch information
1 parent
30ba7d5
commit a2f0dbe
Showing
64 changed files
with
2,195 additions
and
283 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
{ | ||
"$schema": "http://json-schema.org/draft-07/schema#", | ||
"$id": "https://identity.foundation/dwn/json-schemas/events-subscribe.json", | ||
"type": "object", | ||
"additionalProperties": false, | ||
"required": [ | ||
"descriptor", | ||
"authorization" | ||
], | ||
"properties": { | ||
"authorization": { | ||
"$ref": "https://identity.foundation/dwn/json-schemas/authorization.json" | ||
}, | ||
"descriptor": { | ||
"type": "object", | ||
"additionalProperties": false, | ||
"required": [ | ||
"interface", | ||
"method", | ||
"messageTimestamp", | ||
"filters" | ||
], | ||
"properties": { | ||
"interface": { | ||
"enum": [ | ||
"Events" | ||
], | ||
"type": "string" | ||
}, | ||
"method": { | ||
"enum": [ | ||
"Subscribe" | ||
], | ||
"type": "string" | ||
}, | ||
"messageTimestamp": { | ||
"type": "string" | ||
}, | ||
"filters": { | ||
"type": "array", | ||
"items": { | ||
"$ref": "https://identity.foundation/dwn/json-schemas/events-filter.json" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import type { GenericMessage } from '../types/message-types.js'; | ||
import type { KeyValues } from '../types/query-types.js'; | ||
import type { EventListener, EventStream, EventSubscription } from '../types/subscriptions.js'; | ||
|
||
import { EventEmitter } from 'events'; | ||
|
||
const EVENTS_LISTENER_CHANNEL = 'events'; | ||
|
||
export class EventEmitterStream implements EventStream { | ||
private eventEmitter: EventEmitter; | ||
private isOpen: boolean = false; | ||
|
||
constructor() { | ||
// we capture the rejections and currently just log the errors that are produced | ||
this.eventEmitter = new EventEmitter({ captureRejections: true }); | ||
this.eventEmitter.on('error', this.eventError); | ||
} | ||
|
||
// we subscribe to the general `EventEmitter` error events with this handler. | ||
// this handler is also called when there is a caught error upon emitting an event from a handler. | ||
private eventError(error: any): void { | ||
console.error('event emitter error', error); | ||
}; | ||
|
||
async subscribe(id: string, listener: EventListener): Promise<EventSubscription> { | ||
this.eventEmitter.on(EVENTS_LISTENER_CHANNEL, listener); | ||
return { | ||
id, | ||
close: async (): Promise<void> => { this.eventEmitter.off(EVENTS_LISTENER_CHANNEL, listener); } | ||
}; | ||
} | ||
|
||
async open(): Promise<void> { | ||
this.isOpen = true; | ||
} | ||
|
||
async close(): Promise<void> { | ||
this.isOpen = false; | ||
this.eventEmitter.removeAllListeners(); | ||
} | ||
|
||
emit(tenant: string, message: GenericMessage, indexes: KeyValues): void { | ||
if (!this.isOpen) { | ||
console.error('message emitted when EventEmitterStream is closed', tenant, message, indexes); | ||
return; | ||
} | ||
this.eventEmitter.emit(EVENTS_LISTENER_CHANNEL, tenant, message, indexes); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.