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

fix: set up events before calling Zniffer.init() #3745

Merged
merged 1 commit into from
Jun 4, 2024
Merged
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
44 changes: 19 additions & 25 deletions api/lib/ZnifferManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,25 @@ export default class ZnifferManager extends TypedEventEmitter<ZnifferManagerEven
parseSecurityKeys(config, znifferOptions)

this.zniffer = new Zniffer(config.port, znifferOptions)
this.zniffer.on('error', (error) => {
this.onError(error)
})

this.zniffer.on('frame', (frame, rawData) => {
const socketFrame = this.parseFrame(frame, rawData)

this.socket.emit(socketEvents.znifferFrame, socketFrame)
})

this.zniffer.on('corrupted frame', (frame, rawData) => {
const socketFrame = this.parseFrame(frame, rawData)

this.socket.emit(socketEvents.znifferFrame, socketFrame)
})

this.zniffer.on('ready', () => {
logger.info('Zniffer ready')
})

logger.info('Initing Zniffer...')
this.init().catch(() => {})
Expand All @@ -113,33 +132,8 @@ export default class ZnifferManager extends TypedEventEmitter<ZnifferManagerEven
private async init() {
try {
await this.zniffer.init()

this.zniffer.on('frame', (frame, rawData) => {
const socketFrame = this.parseFrame(frame, rawData)

this.socket.emit(socketEvents.znifferFrame, socketFrame)
})

this.zniffer.on('corrupted frame', (frame, rawData) => {
const socketFrame = this.parseFrame(frame, rawData)

this.socket.emit(socketEvents.znifferFrame, socketFrame)
})

this.zniffer.on('error', (error) => {
this.onError(error)
})

this.zniffer.on('ready', () => {
logger.info('Zniffer ready')
})
} catch (error) {
this.onError(error)

Comment on lines -137 to -138
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is being called anyways in the error event, so this would just log twice

logger.info('Retrying in 5s...')

await this.close()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This prevents reusing the Zniffer instance. Alternatively, constructing the Zniffer instance would have to be done in this.init() too.


this.restartTimeout = setTimeout(() => {
this.init().catch(() => {})
}, 5000)
Expand Down
Loading