mirror of
https://github.com/ReVanced/revanced-bots.git
synced 2026-01-11 13:56:15 +00:00
chore: apply code fixes with biome
This commit is contained in:
@@ -1,27 +1,27 @@
|
||||
/**
|
||||
* Disconnect reasons for clients
|
||||
*/
|
||||
enum DisconnectReason {
|
||||
/**
|
||||
* Unknown reason
|
||||
*/
|
||||
Generic = 1,
|
||||
/**
|
||||
* The client did not respond in time
|
||||
*/
|
||||
TimedOut,
|
||||
/**
|
||||
* The client sent an invalid packet (unserializable or invalid JSON)
|
||||
*/
|
||||
InvalidPacket,
|
||||
/**
|
||||
* The server has encountered an internal error
|
||||
*/
|
||||
ServerError,
|
||||
/**
|
||||
* The client had never connected to the server (**CLIENT-ONLY**)
|
||||
*/
|
||||
NeverConnected,
|
||||
}
|
||||
|
||||
export default DisconnectReason
|
||||
/**
|
||||
* Disconnect reasons for clients
|
||||
*/
|
||||
enum DisconnectReason {
|
||||
/**
|
||||
* Unknown reason
|
||||
*/
|
||||
Generic = 1,
|
||||
/**
|
||||
* The client did not respond in time
|
||||
*/
|
||||
TimedOut = 2,
|
||||
/**
|
||||
* The client sent an invalid packet (unserializable or invalid JSON)
|
||||
*/
|
||||
InvalidPacket = 3,
|
||||
/**
|
||||
* The server has encountered an internal error
|
||||
*/
|
||||
ServerError = 4,
|
||||
/**
|
||||
* The client had never connected to the server (**CLIENT-ONLY**)
|
||||
*/
|
||||
NeverConnected = 5,
|
||||
}
|
||||
|
||||
export default DisconnectReason
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
import DisconnectReason from './DisconnectReason.js'
|
||||
|
||||
/**
|
||||
* Humanized disconnect reasons for logs
|
||||
*/
|
||||
const HumanizedDisconnectReason = {
|
||||
[DisconnectReason.InvalidPacket]: 'has sent invalid packet',
|
||||
[DisconnectReason.Generic]: 'has been disconnected for unknown reasons',
|
||||
[DisconnectReason.TimedOut]: 'has timed out',
|
||||
[DisconnectReason.ServerError]:
|
||||
'has been disconnected due to an internal server error',
|
||||
[DisconnectReason.NeverConnected]: 'had never connected to the server',
|
||||
} as const satisfies Record<DisconnectReason, string>
|
||||
|
||||
export default HumanizedDisconnectReason
|
||||
import DisconnectReason from './DisconnectReason.js'
|
||||
|
||||
/**
|
||||
* Humanized disconnect reasons for logs
|
||||
*/
|
||||
const HumanizedDisconnectReason = {
|
||||
[DisconnectReason.InvalidPacket]: 'has sent invalid packet',
|
||||
[DisconnectReason.Generic]: 'has been disconnected for unknown reasons',
|
||||
[DisconnectReason.TimedOut]: 'has timed out',
|
||||
[DisconnectReason.ServerError]:
|
||||
'has been disconnected due to an internal server error',
|
||||
[DisconnectReason.NeverConnected]: 'had never connected to the server',
|
||||
} as const satisfies Record<DisconnectReason, string>
|
||||
|
||||
export default HumanizedDisconnectReason
|
||||
|
||||
@@ -1,57 +1,57 @@
|
||||
/**
|
||||
* Client operation codes for the gateway
|
||||
*/
|
||||
export enum ClientOperation {
|
||||
/**
|
||||
* Client's heartbeat (to check if the connection is dead or not)
|
||||
*/
|
||||
Heartbeat = 100,
|
||||
|
||||
/**
|
||||
* Client's request to parse text
|
||||
*/
|
||||
ParseText = 110,
|
||||
/**
|
||||
* Client's request to parse image
|
||||
*/
|
||||
ParseImage,
|
||||
}
|
||||
|
||||
/**
|
||||
* Server operation codes for the gateway
|
||||
*/
|
||||
export enum ServerOperation {
|
||||
/**
|
||||
* Server's acknowledgement of a client's heartbeat
|
||||
*/
|
||||
HeartbeatAck = 1,
|
||||
/**
|
||||
* Server's initial response to a client's connection
|
||||
*/
|
||||
Hello,
|
||||
|
||||
/**
|
||||
* Server's response to client's request to parse text
|
||||
*/
|
||||
ParsedText = 10,
|
||||
/**
|
||||
* Server's response to client's request to parse image
|
||||
*/
|
||||
ParsedImage,
|
||||
/**
|
||||
* Server's failure response to client's request to parse text
|
||||
*/
|
||||
ParseTextFailed,
|
||||
/**
|
||||
* Server's failure response to client's request to parse image
|
||||
*/
|
||||
ParseImageFailed,
|
||||
|
||||
/**
|
||||
* Server's disconnect message
|
||||
*/
|
||||
Disconnect = 20,
|
||||
}
|
||||
|
||||
export const Operation = { ...ClientOperation, ...ServerOperation } as const
|
||||
export type Operation = ClientOperation | ServerOperation
|
||||
/**
|
||||
* Client operation codes for the gateway
|
||||
*/
|
||||
export enum ClientOperation {
|
||||
/**
|
||||
* Client's heartbeat (to check if the connection is dead or not)
|
||||
*/
|
||||
Heartbeat = 100,
|
||||
|
||||
/**
|
||||
* Client's request to parse text
|
||||
*/
|
||||
ParseText = 110,
|
||||
/**
|
||||
* Client's request to parse image
|
||||
*/
|
||||
ParseImage = 111,
|
||||
}
|
||||
|
||||
/**
|
||||
* Server operation codes for the gateway
|
||||
*/
|
||||
export enum ServerOperation {
|
||||
/**
|
||||
* Server's acknowledgement of a client's heartbeat
|
||||
*/
|
||||
HeartbeatAck = 1,
|
||||
/**
|
||||
* Server's initial response to a client's connection
|
||||
*/
|
||||
Hello = 2,
|
||||
|
||||
/**
|
||||
* Server's response to client's request to parse text
|
||||
*/
|
||||
ParsedText = 10,
|
||||
/**
|
||||
* Server's response to client's request to parse image
|
||||
*/
|
||||
ParsedImage = 11,
|
||||
/**
|
||||
* Server's failure response to client's request to parse text
|
||||
*/
|
||||
ParseTextFailed = 12,
|
||||
/**
|
||||
* Server's failure response to client's request to parse image
|
||||
*/
|
||||
ParseImageFailed = 13,
|
||||
|
||||
/**
|
||||
* Server's disconnect message
|
||||
*/
|
||||
Disconnect = 20,
|
||||
}
|
||||
|
||||
export const Operation = { ...ClientOperation, ...ServerOperation } as const
|
||||
export type Operation = ClientOperation | ServerOperation
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
export { default as DisconnectReason } from './DisconnectReason.js'
|
||||
export { default as HumanizedDisconnectReason } from './HumanizedDisconnectReason.js'
|
||||
export * from './Operation.js'
|
||||
export { default as DisconnectReason } from './DisconnectReason.js'
|
||||
export { default as HumanizedDisconnectReason } from './HumanizedDisconnectReason.js'
|
||||
export * from './Operation.js'
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
export * from './constants/index.js'
|
||||
export * from './schemas/index.js'
|
||||
export * from './utils/index.js'
|
||||
export * from './constants/index.js'
|
||||
export * from './schemas/index.js'
|
||||
export * from './utils/index.js'
|
||||
|
||||
@@ -1,101 +1,101 @@
|
||||
import DisconnectReason from '../constants/DisconnectReason.js'
|
||||
import {
|
||||
ClientOperation,
|
||||
Operation,
|
||||
ServerOperation,
|
||||
} from '../constants/Operation.js'
|
||||
import {
|
||||
object,
|
||||
enum_,
|
||||
special,
|
||||
ObjectSchema,
|
||||
number,
|
||||
string,
|
||||
Output,
|
||||
AnySchema,
|
||||
null_,
|
||||
NullSchema,
|
||||
array,
|
||||
url,
|
||||
parse,
|
||||
// merge
|
||||
} from 'valibot'
|
||||
|
||||
/**
|
||||
* Schema to validate packets
|
||||
*/
|
||||
export const PacketSchema = special<Packet>(input => {
|
||||
if (
|
||||
typeof input === 'object' &&
|
||||
input &&
|
||||
'op' in input &&
|
||||
typeof input.op === 'number' &&
|
||||
input.op in Operation &&
|
||||
'd' in input &&
|
||||
typeof input.d === 'object'
|
||||
) {
|
||||
try {
|
||||
parse(PacketDataSchemas[input.op as Operation], input.d)
|
||||
return true
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return false
|
||||
}, 'Invalid packet data')
|
||||
|
||||
/**
|
||||
* Schema to validate packet data for each possible operations
|
||||
*/
|
||||
export const PacketDataSchemas = {
|
||||
[ServerOperation.Hello]: object({
|
||||
heartbeatInterval: number(),
|
||||
}),
|
||||
[ServerOperation.HeartbeatAck]: object({
|
||||
nextHeartbeat: number(),
|
||||
}),
|
||||
[ServerOperation.ParsedText]: object({
|
||||
id: string(),
|
||||
labels: array(
|
||||
object({
|
||||
name: string(),
|
||||
confidence: special<number>(
|
||||
input =>
|
||||
typeof input === 'number' && input >= 0 && input <= 1
|
||||
),
|
||||
})
|
||||
),
|
||||
}),
|
||||
[ServerOperation.ParsedImage]: object({
|
||||
id: string(),
|
||||
text: string(),
|
||||
}),
|
||||
[ServerOperation.ParseTextFailed]: object({
|
||||
id: string(),
|
||||
}),
|
||||
[ServerOperation.ParseImageFailed]: object({
|
||||
id: string(),
|
||||
}),
|
||||
[ServerOperation.Disconnect]: object({
|
||||
reason: enum_(DisconnectReason),
|
||||
}),
|
||||
|
||||
[ClientOperation.Heartbeat]: null_(),
|
||||
[ClientOperation.ParseText]: object({
|
||||
id: string(),
|
||||
text: string(),
|
||||
}),
|
||||
[ClientOperation.ParseImage]: object({
|
||||
id: string(),
|
||||
image_url: string([url()]),
|
||||
}),
|
||||
} as const satisfies Record<
|
||||
Operation,
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
ObjectSchema<any> | AnySchema | NullSchema
|
||||
>
|
||||
|
||||
export type Packet<TOp extends Operation = Operation> = {
|
||||
op: TOp
|
||||
d: Output<(typeof PacketDataSchemas)[TOp]>
|
||||
}
|
||||
import {
|
||||
url,
|
||||
AnySchema,
|
||||
NullSchema,
|
||||
ObjectSchema,
|
||||
Output,
|
||||
array,
|
||||
enum_,
|
||||
null_,
|
||||
number,
|
||||
object,
|
||||
parse,
|
||||
special,
|
||||
string,
|
||||
// merge
|
||||
} from 'valibot'
|
||||
import DisconnectReason from '../constants/DisconnectReason.js'
|
||||
import {
|
||||
ClientOperation,
|
||||
Operation,
|
||||
ServerOperation,
|
||||
} from '../constants/Operation.js'
|
||||
|
||||
/**
|
||||
* Schema to validate packets
|
||||
*/
|
||||
export const PacketSchema = special<Packet>(input => {
|
||||
if (
|
||||
typeof input === 'object' &&
|
||||
input &&
|
||||
'op' in input &&
|
||||
typeof input.op === 'number' &&
|
||||
input.op in Operation &&
|
||||
'd' in input &&
|
||||
typeof input.d === 'object'
|
||||
) {
|
||||
try {
|
||||
parse(PacketDataSchemas[input.op as Operation], input.d)
|
||||
return true
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return false
|
||||
}, 'Invalid packet data')
|
||||
|
||||
/**
|
||||
* Schema to validate packet data for each possible operations
|
||||
*/
|
||||
export const PacketDataSchemas = {
|
||||
[ServerOperation.Hello]: object({
|
||||
heartbeatInterval: number(),
|
||||
}),
|
||||
[ServerOperation.HeartbeatAck]: object({
|
||||
nextHeartbeat: number(),
|
||||
}),
|
||||
[ServerOperation.ParsedText]: object({
|
||||
id: string(),
|
||||
labels: array(
|
||||
object({
|
||||
name: string(),
|
||||
confidence: special<number>(
|
||||
input =>
|
||||
typeof input === 'number' && input >= 0 && input <= 1,
|
||||
),
|
||||
}),
|
||||
),
|
||||
}),
|
||||
[ServerOperation.ParsedImage]: object({
|
||||
id: string(),
|
||||
text: string(),
|
||||
}),
|
||||
[ServerOperation.ParseTextFailed]: object({
|
||||
id: string(),
|
||||
}),
|
||||
[ServerOperation.ParseImageFailed]: object({
|
||||
id: string(),
|
||||
}),
|
||||
[ServerOperation.Disconnect]: object({
|
||||
reason: enum_(DisconnectReason),
|
||||
}),
|
||||
|
||||
[ClientOperation.Heartbeat]: null_(),
|
||||
[ClientOperation.ParseText]: object({
|
||||
id: string(),
|
||||
text: string(),
|
||||
}),
|
||||
[ClientOperation.ParseImage]: object({
|
||||
id: string(),
|
||||
image_url: string([url()]),
|
||||
}),
|
||||
} as const satisfies Record<
|
||||
Operation,
|
||||
// biome-ignore lint/suspicious/noExplicitAny: This is a schema, it's not possible to type it
|
||||
ObjectSchema<any> | AnySchema | NullSchema
|
||||
>
|
||||
|
||||
export type Packet<TOp extends Operation = Operation> = {
|
||||
op: TOp
|
||||
d: Output<typeof PacketDataSchemas[TOp]>
|
||||
}
|
||||
|
||||
@@ -1 +1 @@
|
||||
export * from './Packet.js'
|
||||
export * from './Packet.js'
|
||||
|
||||
@@ -1,41 +1,41 @@
|
||||
import { Packet } from '../schemas/Packet.js'
|
||||
import {
|
||||
ClientOperation,
|
||||
Operation,
|
||||
ServerOperation,
|
||||
} from '../constants/Operation.js'
|
||||
|
||||
/**
|
||||
* Checks whether a packet is trying to do the given operation
|
||||
* @param op Operation code to check
|
||||
* @param packet A packet
|
||||
* @returns Whether this packet is trying to do the operation given
|
||||
*/
|
||||
export function packetMatchesOperation<TOp extends Operation>(
|
||||
op: TOp,
|
||||
packet: Packet
|
||||
): packet is Packet<TOp> {
|
||||
return packet.op === op
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether this packet is a client packet **(this does NOT validate the data)**
|
||||
* @param packet A packet
|
||||
* @returns Whether this packet is a client packet
|
||||
*/
|
||||
export function isClientPacket(
|
||||
packet: Packet
|
||||
): packet is Packet<ClientOperation> {
|
||||
return packet.op in ClientOperation
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether this packet is a server packet **(this does NOT validate the data)**
|
||||
* @param packet A packet
|
||||
* @returns Whether this packet is a server packet
|
||||
*/
|
||||
export function isServerPacket(
|
||||
packet: Packet
|
||||
): packet is Packet<ServerOperation> {
|
||||
return packet.op in ServerOperation
|
||||
}
|
||||
import {
|
||||
ClientOperation,
|
||||
Operation,
|
||||
ServerOperation,
|
||||
} from '../constants/Operation.js'
|
||||
import { Packet } from '../schemas/Packet.js'
|
||||
|
||||
/**
|
||||
* Checks whether a packet is trying to do the given operation
|
||||
* @param op Operation code to check
|
||||
* @param packet A packet
|
||||
* @returns Whether this packet is trying to do the operation given
|
||||
*/
|
||||
export function packetMatchesOperation<TOp extends Operation>(
|
||||
op: TOp,
|
||||
packet: Packet,
|
||||
): packet is Packet<TOp> {
|
||||
return packet.op === op
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether this packet is a client packet **(this does NOT validate the data)**
|
||||
* @param packet A packet
|
||||
* @returns Whether this packet is a client packet
|
||||
*/
|
||||
export function isClientPacket(
|
||||
packet: Packet,
|
||||
): packet is Packet<ClientOperation> {
|
||||
return packet.op in ClientOperation
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether this packet is a server packet **(this does NOT validate the data)**
|
||||
* @param packet A packet
|
||||
* @returns Whether this packet is a server packet
|
||||
*/
|
||||
export function isServerPacket(
|
||||
packet: Packet,
|
||||
): packet is Packet<ServerOperation> {
|
||||
return packet.op in ServerOperation
|
||||
}
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
export * from './guard.js'
|
||||
export * from './serialization.js'
|
||||
export * from './string.js'
|
||||
export * from './guard.js'
|
||||
export * from './serialization.js'
|
||||
export * from './string.js'
|
||||
|
||||
@@ -1,23 +1,23 @@
|
||||
import * as BSON from 'bson'
|
||||
import { Packet, PacketSchema } from '../schemas/index.js'
|
||||
import { Operation } from '../constants/index.js'
|
||||
import { parse } from 'valibot'
|
||||
|
||||
/**
|
||||
* Compresses a packet into a buffer
|
||||
* @param packet The packet to compress
|
||||
* @returns A buffer of the compressed packet
|
||||
*/
|
||||
export function serializePacket<TOp extends Operation>(packet: Packet<TOp>) {
|
||||
return BSON.serialize(packet)
|
||||
}
|
||||
|
||||
/**
|
||||
* Decompresses a buffer into a packet
|
||||
* @param buffer The buffer to decompress
|
||||
* @returns A packet
|
||||
*/
|
||||
export function deserializePacket(buffer: Buffer) {
|
||||
const data = BSON.deserialize(buffer)
|
||||
return parse(PacketSchema, data) as Packet
|
||||
}
|
||||
import * as BSON from 'bson'
|
||||
import { parse } from 'valibot'
|
||||
import { Operation } from '../constants/index.js'
|
||||
import { Packet, PacketSchema } from '../schemas/index.js'
|
||||
|
||||
/**
|
||||
* Compresses a packet into a buffer
|
||||
* @param packet The packet to compress
|
||||
* @returns A buffer of the compressed packet
|
||||
*/
|
||||
export function serializePacket<TOp extends Operation>(packet: Packet<TOp>) {
|
||||
return BSON.serialize(packet)
|
||||
}
|
||||
|
||||
/**
|
||||
* Decompresses a buffer into a packet
|
||||
* @param buffer The buffer to decompress
|
||||
* @returns A packet
|
||||
*/
|
||||
export function deserializePacket(buffer: Buffer) {
|
||||
const data = BSON.deserialize(buffer)
|
||||
return parse(PacketSchema, data) as Packet
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
/**
|
||||
* Uncapitalizes the first letter of a string
|
||||
* @param str The string to uncapitalize
|
||||
* @returns The uncapitalized string
|
||||
*/
|
||||
export function uncapitalize<T extends string>(str: T): Uncapitalize<T> {
|
||||
return (str.charAt(0).toLowerCase() + str.slice(1)) as Uncapitalize<T>
|
||||
}
|
||||
/**
|
||||
* Uncapitalizes the first letter of a string
|
||||
* @param str The string to uncapitalize
|
||||
* @returns The uncapitalized string
|
||||
*/
|
||||
export function uncapitalize<T extends string>(str: T): Uncapitalize<T> {
|
||||
return (str.charAt(0).toLowerCase() + str.slice(1)) as Uncapitalize<T>
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user