-
Notifications
You must be signed in to change notification settings - Fork 34
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
Add Server Side Events endpoint #75
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { EventsController } from './events.controller'; | ||
import { EventsService } from './events.service'; | ||
import { QueueProvider } from '../../datasources/queue/queue.provider'; | ||
import { WebhookService } from '../webhook/webhook.service'; | ||
import { firstValueFrom } from 'rxjs'; | ||
import { TxServiceEvent, TxServiceEventType } from './event.dto'; | ||
import { BadRequestException } from '@nestjs/common'; | ||
|
||
describe('EventsController', () => { | ||
let controller: EventsController; | ||
let service: EventsService; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
controllers: [EventsController], | ||
providers: [EventsService, QueueProvider, WebhookService], | ||
}) | ||
.overrideProvider(QueueProvider) | ||
.useValue({}) | ||
.overrideProvider(WebhookService) | ||
.useValue({}) | ||
.compile(); | ||
|
||
controller = module.get<EventsController>(EventsController); | ||
service = module.get<EventsService>(EventsService); | ||
}); | ||
|
||
describe('SSE events', () => { | ||
it('should require an EIP55 address', async () => { | ||
const notValidAddress = '0x8618CE407F169ABB1388348A19632AaFA857CCB9'; | ||
const expectedError = new BadRequestException('Not valid EIP55 address', { | ||
description: `${notValidAddress} is not a valid EIP55 Safe address`, | ||
}); | ||
expect(() => { | ||
controller.sse(notValidAddress); | ||
}).toThrow(expectedError); | ||
}); | ||
it('should receive for a Safe', async () => { | ||
const relevantSafeAddress = '0x8618ce407F169ABB1388348A19632AaFA857CCB9'; | ||
const notRelevantSafeAddress = | ||
'0x3F6E283068Ded118459B56fC669A27C3a65e587D'; | ||
const txServiceEvents: Array<TxServiceEvent> = [ | ||
{ | ||
chainId: '1', | ||
type: 'SAFE_CREATED' as TxServiceEventType, | ||
hero: 'Saitama', | ||
address: notRelevantSafeAddress, | ||
}, | ||
{ | ||
chainId: '100', | ||
type: 'MESSAGE_CREATED' as TxServiceEventType, | ||
hero: 'Atomic Samurai', | ||
address: relevantSafeAddress, | ||
}, | ||
]; | ||
const observable = controller.sse(relevantSafeAddress); | ||
const firstValue = firstValueFrom(observable); | ||
txServiceEvents.map((txServiceEvent) => | ||
service.pushEventToEventsObservable(txServiceEvent), | ||
); | ||
|
||
// Not relevant event must be ignored by Safe filter | ||
const event = await firstValue; | ||
expect(event.data).toEqual(txServiceEvents[1]); | ||
expect(event.type).toEqual('message'); | ||
}); | ||
}); | ||
}); |
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,19 @@ | ||
import { BadRequestException, Controller, Param, Sse } from '@nestjs/common'; | ||
import { Observable } from 'rxjs'; | ||
import { EventsService } from './events.service'; | ||
import { getAddress, isAddress } from 'ethers'; | ||
|
||
@Controller('events') | ||
export class EventsController { | ||
constructor(private readonly eventsService: EventsService) {} | ||
|
||
@Sse('/sse/:safe') | ||
sse(@Param('safe') safe: string): Observable<MessageEvent> { | ||
if (isAddress(safe) && getAddress(safe) === safe) | ||
return this.eventsService.getEventsObservableForSafe(safe); | ||
|
||
throw new BadRequestException('Not valid EIP55 address', { | ||
description: `${safe} is not a valid EIP55 Safe address`, | ||
}); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,11 +1,12 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { EventsController } from './events.controller'; | ||
import { EventsService } from './events.service'; | ||
import { WebhookModule } from '../webhook/webhook.module'; | ||
import { QueueModule } from '../../datasources/queue/queue.module'; | ||
import { WebhookModule } from '../webhook/webhook.module'; | ||
|
||
@Module({ | ||
imports: [QueueModule, WebhookModule], | ||
// controllers: [Controller], | ||
controllers: [EventsController], | ||
providers: [EventsService], | ||
}) | ||
export class EventsModule {} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we consider check if the address is a Safe address?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would add too many dependencies for the service, in my opinion
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But it's a good observation, depending on traffic we have to think on how to protect this endpoint, maybe a flag to disable the feature in the future