forked from decentralized-identity/dwn-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
207 additions
and
8 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
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,174 @@ | ||
import type { JsonRpcSuccessResponse } from './lib/json-rpc.js'; | ||
import { SubscriptionRequestMessage } from '@tbd54566975/dwn-sdk-js'; | ||
import { v4 as uuidv4 } from 'uuid'; | ||
import type WebSocket from 'ws'; | ||
import { WebSocketServer } from 'ws'; | ||
|
||
import type { Dwn, SubscriptionFilter } from '@tbd54566975/dwn-sdk-js'; | ||
import type { EventMessage, PermissionsGrant } from '@tbd54566975/dwn-sdk-js'; | ||
import type { | ||
MessageStore, | ||
SubscriptionRequestReply, | ||
} from '@tbd54566975/dwn-sdk-js'; | ||
|
||
export class Subscription { | ||
from?: string; | ||
subscriptionId: string; | ||
createdAt: string; | ||
description: string; | ||
filters?: SubscriptionFilter[]; | ||
permissionGrant: PermissionsGrant; | ||
connection: WebSocket; | ||
} | ||
|
||
export interface SubscriptionController { | ||
clear(): Promise<void>; | ||
close(): Promise<void>; | ||
start(): Promise<void>; | ||
subscribe( | ||
request: RegisterSubscriptionRequest, | ||
): Promise<RegisterSubscriptionReply>; | ||
} | ||
|
||
export type RegisterSubscriptionRequest = { | ||
from: string; | ||
socket: WebSocket; | ||
filters?: SubscriptionFilter[]; | ||
permissionGrant: PermissionsGrant; | ||
subscriptionRequestMessage: SubscriptionRequestMessage; | ||
}; | ||
|
||
export type RegisterSubscriptionReply = { | ||
reply: SubscriptionRequestReply; | ||
subscriptionId?: string; | ||
}; | ||
|
||
export type defaultSubscriptionChannel = 'event'; | ||
|
||
export type SubscriptionManagerOptions = { | ||
subscriptionChannel: string; | ||
wss: WebSocketServer; | ||
dwn: Dwn; | ||
messageStore: MessageStore; | ||
tenant: string; | ||
}; | ||
|
||
export class SubscriptionManager { | ||
private wss: WebSocketServer; | ||
private dwn: Dwn; | ||
private connections: Map<string, Subscription>; | ||
private messageStore: MessageStore; | ||
private tenant: string; | ||
options: SubscriptionManagerOptions; | ||
#open: boolean; | ||
|
||
constructor(options?: SubscriptionManagerOptions) { | ||
this.wss = options?.wss || new WebSocketServer(); | ||
this.connections = new Map(); | ||
this.messageStore = options?.messageStore; | ||
this.tenant = options?.tenant; | ||
this.dwn = options?.dwn; | ||
this.options = options; | ||
|
||
this.wss.on('connection', (socket: WebSocket) => { | ||
socket.on('subscribe', async (data) => { | ||
await this.handleSubscribe(socket, data); | ||
}); | ||
}); | ||
} | ||
|
||
async clear(): Promise<void> { | ||
this.wss.removeAllListeners(); | ||
this.connections.clear(); | ||
} | ||
|
||
async close(): Promise<void> { | ||
this.#open = false; | ||
this.connections.clear(); | ||
this.wss.close(); | ||
} | ||
|
||
async open(): Promise<void> { | ||
this.#open = true; | ||
} | ||
|
||
async start(): Promise<void> { | ||
this.open(); | ||
} | ||
|
||
private async createSubscription( | ||
from: string, | ||
request: RegisterSubscriptionRequest, | ||
): Promise<Subscription> { | ||
return { | ||
from, | ||
subscriptionId: uuidv4(), | ||
createdAt: new Date().toISOString(), | ||
description: 'subscription', | ||
filters: request.filters, | ||
permissionGrant: request.permissionGrant, | ||
connection: request.socket, | ||
}; | ||
} | ||
|
||
async handleSubscribe( | ||
socket: WebSocket, | ||
data: any, | ||
): Promise<RegisterSubscriptionReply> { | ||
// parse message | ||
const req = SubscriptionRequestMessage.parse(data); | ||
return await this.subscribe(req, socket); | ||
} | ||
|
||
createJSONRPCEvent(e: EventMessage): JsonRpcSuccessResponse { | ||
return { | ||
id: uuidv4(), | ||
jsonrpc: '2.0', | ||
result: e, | ||
}; | ||
} | ||
|
||
async subscribe( | ||
req: RegisterSubscriptionRequest, | ||
socket: WebSocket, | ||
): Promise<RegisterSubscriptionReply> { | ||
const subscriptionReply = await this.dwn.handleSubscriptionRequest( | ||
this.tenant, | ||
req.subscriptionRequestMessage, | ||
); | ||
if (subscriptionReply.status.code !== 200) { | ||
return { reply: subscriptionReply }; | ||
} | ||
const subscription = await this.createSubscription(req.from, req); | ||
this.registerSubscription(subscription); | ||
// set up forwarding. | ||
subscriptionReply.subscription.emitter.on( | ||
async (e: EventMessage): Promise<void> => { | ||
const jsonRpcResponse = this.createJSONRPCEvent(e); | ||
const str = JSON.stringify(jsonRpcResponse); | ||
return socket.send(Buffer.from(str)); | ||
}, | ||
); | ||
} | ||
|
||
private async registerSubscription( | ||
subscription: Subscription, | ||
): Promise<void> { | ||
if (!this.#open) { | ||
throw new Error("Can't register subscription. It's not opened."); | ||
} | ||
if (this.connections.has(subscription.subscriptionId)) { | ||
throw new Error( | ||
'Failed to add connection to controller. ID already exists.', | ||
); | ||
} | ||
this.connections.set(subscription.subscriptionId, subscription); | ||
subscription.connection.on('close', () => { | ||
this.deleteSubscription(subscription.subscriptionId); | ||
}); | ||
} | ||
|
||
private async deleteSubscription(id: string): Promise<void> { | ||
this.connections.delete(id); | ||
} | ||
} |
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