Skip to content

Commit

Permalink
fix: set up events before calling Zniffer.init() (#3745)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlCalzone authored Jun 4, 2024
1 parent 61d406e commit 66ff0e3
Showing 1 changed file with 19 additions and 25 deletions.
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)

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

await this.close()

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

0 comments on commit 66ff0e3

Please sign in to comment.