Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add unit tests for SubscribeMessageHandler #17

Merged
merged 1 commit into from
Oct 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/@types/adapters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ export interface IWebServerAdapter extends EventEmitter {


export type IWebSocketAdapter = EventEmitter & {
getSubscriptions(): Map<string, Set<SubscriptionFilter>>
getSubscriptions(): Map<string, SubscriptionFilter[]>
}
1 change: 1 addition & 0 deletions src/@types/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export type SubscribeMessage = {
} & Array<SubscriptionFilter>

export type IncomingEventMessage = EventMessage & [MessageType.EVENT, Event]

export interface EventMessage {
0: MessageType.EVENT
1: Event
Expand Down
8 changes: 4 additions & 4 deletions src/adapters/web-socket-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class WebSocketAdapter extends EventEmitter implements IWebSocketAdapter
private id: string
private clientAddress: string
private alive: boolean
private subscriptions: Map<SubscriptionId, Set<SubscriptionFilter>>
private subscriptions: Map<SubscriptionId, SubscriptionFilter[]>

private sent = 0
private received = 0
Expand Down Expand Up @@ -54,7 +54,7 @@ export class WebSocketAdapter extends EventEmitter implements IWebSocketAdapter
this.subscriptions.delete(subscriptionId)
}

public onSubscribed(subscriptionId: string, filters: Set<SubscriptionFilter>): void {
public onSubscribed(subscriptionId: string, filters: SubscriptionFilter[]): void {
this.subscriptions.set(subscriptionId, filters)
}

Expand All @@ -69,7 +69,7 @@ export class WebSocketAdapter extends EventEmitter implements IWebSocketAdapter
public onSendEvent(event: Event): void {
this.subscriptions.forEach((filters, subscriptionId) => {
if (
Array.from(filters).map(isEventMatchingFilter).some((Matches) => Matches(event))
filters.map(isEventMatchingFilter).some((Matches) => Matches(event))
) {
this.sendMessage(createOutgoingEventMessage(subscriptionId, event))
}
Expand All @@ -95,7 +95,7 @@ export class WebSocketAdapter extends EventEmitter implements IWebSocketAdapter
this.client.ping()
}

public getSubscriptions(): Map<string, Set<SubscriptionFilter>> {
public getSubscriptions(): Map<string, SubscriptionFilter[]> {
return new Map(this.subscriptions)
}

Expand Down
25 changes: 17 additions & 8 deletions src/handlers/subscribe-message-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,47 +38,56 @@ export class SubscribeMessageHandler implements IMessageHandler, IAbortable {
return
}

this.webSocket.emit(WebSocketAdapterEvent.Subscribe, subscriptionId, new Set(filters))
this.webSocket.emit(WebSocketAdapterEvent.Subscribe, subscriptionId, filters)

return this.fetchAndSend(subscriptionId, filters)
}

private async fetchAndSend(subscriptionId: string, filters: SubscriptionFilter[]): Promise<void> {
const sendEvent = (event: Event) =>
this.webSocket.emit(WebSocketAdapterEvent.Message, createOutgoingEventMessage(subscriptionId, event))
const sendEOSE = () =>
this.webSocket.emit(WebSocketAdapterEvent.Message, createEndOfStoredEventsNoticeMessage(subscriptionId))
const isSubscribedToEvent = SubscribeMessageHandler.isClientSubscribedToEvent(filters)

const findEvents = this.eventRepository.findByFilters(filters).stream()

try {
await pipeline(
findEvents,
streamMap(toNostrEvent),
streamFilter(anyPass(map(isEventMatchingFilter)(filters))),
streamFilter(isSubscribedToEvent),
streamEach(sendEvent),
streamEnd(sendEOSE), // NIP-15: End of Stored Events Notice
streamEnd(sendEOSE),
{
signal: this.abortController.signal,
},
}
)
} catch (error) {
if (error instanceof Error && error.name === 'AbortError') {
console.log('AbortError when finding events')
findEvents.end()
}
throw error
}
}

private canSubscribe(subscriptionId: string, filters: SubscriptionFilter[]): string | undefined {
private static isClientSubscribedToEvent(filters: SubscriptionFilter[]): (event: Event) => boolean {
return anyPass(map(isEventMatchingFilter)(filters))
}

private canSubscribe(subscriptionId: SubscriptionId, filters: SubscriptionFilter[]): string | undefined {
const maxSubscriptions = this.settings().limits.client.subscription.maxSubscriptions
if (maxSubscriptions > 0) {
const subscriptions = this.webSocket.getSubscriptions()
if (!subscriptions.has(subscriptionId) && subscriptions.size + 1 > maxSubscriptions) {
return `Too many subscriptions: Number of subscriptions must be less than ${maxSubscriptions}`
return `Too many subscriptions: Number of subscriptions must be less than or equal to ${maxSubscriptions}`
}
}

const maxFilters = this.settings().limits.client.subscription.maxFilters
if (maxFilters > 0) {
if (filters.length > maxFilters) {
return `Too many filters: Number of filters per susbscription must be less or equal to ${maxFilters}`
return `Too many filters: Number of filters per susbscription must be less then or equal to ${maxFilters}`
}
}
}
Expand Down
Loading