mirror of
https://github.com/ReVanced/revanced-bots.git
synced 2026-01-11 13:56:15 +00:00
feat(packages/api): allow listening for invalid packets
This commit is contained in:
@@ -127,15 +127,13 @@ export default class ClientGateway {
|
|||||||
#listen() {
|
#listen() {
|
||||||
this.#socket.on('message', data => {
|
this.#socket.on('message', data => {
|
||||||
const packet = deserializePacket(this._toBuffer(data))
|
const packet = deserializePacket(this._toBuffer(data))
|
||||||
// TODO: maybe log this?
|
|
||||||
// Just ignore the invalid packet, we don't have to disconnect
|
if (!isServerPacket(packet)) return this.#emitter.emit('invalidPacket', packet)
|
||||||
if (!isServerPacket(packet)) return
|
|
||||||
|
|
||||||
this.#emitter.emit('packet', packet)
|
this.#emitter.emit('packet', packet)
|
||||||
|
|
||||||
switch (packet.op) {
|
switch (packet.op) {
|
||||||
case ServerOperation.Hello: {
|
case ServerOperation.Hello: {
|
||||||
// eslint-disable-next-line no-case-declarations
|
|
||||||
const data = Object.freeze((packet as Packet<ServerOperation.Hello>).d)
|
const data = Object.freeze((packet as Packet<ServerOperation.Hello>).d)
|
||||||
this.config = data
|
this.config = data
|
||||||
this.#emitter.emit('hello', data)
|
this.#emitter.emit('hello', data)
|
||||||
@@ -186,8 +184,8 @@ export default class ClientGateway {
|
|||||||
|
|
||||||
protected _toBuffer(data: RawData) {
|
protected _toBuffer(data: RawData) {
|
||||||
if (data instanceof Buffer) return data
|
if (data instanceof Buffer) return data
|
||||||
else if (data instanceof ArrayBuffer) return Buffer.from(data)
|
if (data instanceof ArrayBuffer) return Buffer.from(data)
|
||||||
else return Buffer.concat(data)
|
return Buffer.concat(data)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -202,12 +200,13 @@ export type ClientGatewayServerEventName = keyof typeof ServerOperation
|
|||||||
|
|
||||||
export type ClientGatewayEventHandlers = {
|
export type ClientGatewayEventHandlers = {
|
||||||
[K in Uncapitalize<ClientGatewayServerEventName>]: (
|
[K in Uncapitalize<ClientGatewayServerEventName>]: (
|
||||||
packet: Packet<typeof ServerOperation[Capitalize<K>]>,
|
packet: Packet<(typeof ServerOperation)[Capitalize<K>]>,
|
||||||
) => Promise<void> | void
|
) => Promise<void> | void
|
||||||
} & {
|
} & {
|
||||||
hello: (config: NonNullable<ClientGateway['config']>) => Promise<void> | void
|
hello: (config: NonNullable<ClientGateway['config']>) => Promise<void> | void
|
||||||
ready: () => Promise<void> | void
|
ready: () => Promise<void> | void
|
||||||
packet: (packet: Packet<ServerOperation>) => Promise<void> | void
|
packet: (packet: Packet<ServerOperation>) => Promise<void> | void
|
||||||
|
invalidPacket: (packet: Packet) => Promise<void> | void
|
||||||
disconnect: (reason: DisconnectReason) => Promise<void> | void
|
disconnect: (reason: DisconnectReason) => Promise<void> | void
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user