mirror of
https://github.com/ReVanced/revanced-bots.git
synced 2026-01-11 13:56:15 +00:00
feat(packages/shared): add logger factory
- @revanced/websocket-api now also utilizes the new logger from the shared package - @revanced/websocket-api/utils/checkEnv has been renamed to its full form - It also no longer returns anything as it's no longer needed
This commit is contained in:
@@ -5,5 +5,5 @@
|
||||
"port": 3000,
|
||||
"ocrConcurrentQueues": 1,
|
||||
"clientHeartbeatInterval": 5000,
|
||||
"debugLogsInProduction": false
|
||||
"consoleLogLevel": "silly"
|
||||
}
|
||||
|
||||
@@ -22,10 +22,11 @@
|
||||
"type": "integer",
|
||||
"default": 60000
|
||||
},
|
||||
"debugLogsInProduction": {
|
||||
"description": "Whether to print debug logs in production",
|
||||
"type": "boolean",
|
||||
"default": false
|
||||
"consoleLogLevel": {
|
||||
"description": "The log level to print to console",
|
||||
"type": "string",
|
||||
"enum": ["error", "warn", "info", "verbose", "debug", "silly", "none"],
|
||||
"default": "info"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,43 +47,29 @@ export default class Client {
|
||||
this.#emitter.emit('ready')
|
||||
})
|
||||
.catch(() => {
|
||||
if (this.disconnected === false)
|
||||
this.disconnect(DisconnectReason.ServerError)
|
||||
if (this.disconnected === false) this.disconnect(DisconnectReason.ServerError)
|
||||
else this.forceDisconnect(DisconnectReason.ServerError)
|
||||
})
|
||||
}
|
||||
|
||||
on<TOpName extends keyof ClientEventHandlers>(
|
||||
name: TOpName,
|
||||
handler: ClientEventHandlers[typeof name],
|
||||
) {
|
||||
on<TOpName extends keyof ClientEventHandlers>(name: TOpName, handler: ClientEventHandlers[typeof name]) {
|
||||
this.#emitter.on(name, handler)
|
||||
}
|
||||
|
||||
once<TOpName extends keyof ClientEventHandlers>(
|
||||
name: TOpName,
|
||||
handler: ClientEventHandlers[typeof name],
|
||||
) {
|
||||
once<TOpName extends keyof ClientEventHandlers>(name: TOpName, handler: ClientEventHandlers[typeof name]) {
|
||||
this.#emitter.once(name, handler)
|
||||
}
|
||||
|
||||
off<TOpName extends keyof ClientEventHandlers>(
|
||||
name: TOpName,
|
||||
handler: ClientEventHandlers[typeof name],
|
||||
) {
|
||||
off<TOpName extends keyof ClientEventHandlers>(name: TOpName, handler: ClientEventHandlers[typeof name]) {
|
||||
this.#emitter.off(name, handler)
|
||||
}
|
||||
|
||||
send<TOp extends ServerOperation>(packet: Packet<TOp>) {
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
try {
|
||||
this.#throwIfDisconnected(
|
||||
'Cannot send packet to client that has already disconnected',
|
||||
)
|
||||
this.#throwIfDisconnected('Cannot send packet to client that has already disconnected')
|
||||
|
||||
this.#socket.send(serializePacket(packet), err =>
|
||||
err ? reject(err) : resolve(),
|
||||
)
|
||||
this.#socket.send(serializePacket(packet), err => (err ? reject(err) : resolve()))
|
||||
} catch (e) {
|
||||
reject(e)
|
||||
}
|
||||
@@ -91,16 +77,12 @@ export default class Client {
|
||||
}
|
||||
|
||||
async disconnect(reason: DisconnectReason = DisconnectReason.Generic) {
|
||||
this.#throwIfDisconnected(
|
||||
'Cannot disconnect client that has already disconnected',
|
||||
)
|
||||
this.#throwIfDisconnected('Cannot disconnect client that has already disconnected')
|
||||
|
||||
try {
|
||||
await this.send({ op: ServerOperation.Disconnect, d: { reason } })
|
||||
} catch (err) {
|
||||
throw new Error(
|
||||
`Cannot send disconnect reason to client ${this.id}: ${err}`,
|
||||
)
|
||||
throw new Error(`Cannot send disconnect reason to client ${this.id}: ${err}`)
|
||||
} finally {
|
||||
this.forceDisconnect(reason)
|
||||
}
|
||||
@@ -173,10 +155,7 @@ export default class Client {
|
||||
if (Date.now() - this.lastHeartbeat > 0) {
|
||||
// TODO: put into config
|
||||
// 5000 is extra time to account for latency
|
||||
const interval = setTimeout(
|
||||
() => this.disconnect(DisconnectReason.TimedOut),
|
||||
5000,
|
||||
)
|
||||
const interval = setTimeout(() => this.disconnect(DisconnectReason.TimedOut), 5000)
|
||||
|
||||
this.once('heartbeat', () => clearTimeout(interval))
|
||||
// This should never happen but it did in my testing so I'm adding this just in case
|
||||
@@ -208,11 +187,9 @@ export type ClientEventName = keyof typeof ClientOperation
|
||||
export type ClientEventHandlers = {
|
||||
[K in Uncapitalize<ClientEventName>]: (
|
||||
packet: ClientPacketObject<typeof ClientOperation[Capitalize<K>]>,
|
||||
) => Promise<void> | void
|
||||
) => Promise<unknown> | unknown
|
||||
} & {
|
||||
ready: () => Promise<void> | void
|
||||
packet: (
|
||||
packet: ClientPacketObject<ClientOperation>,
|
||||
) => Promise<void> | void
|
||||
disconnect: (reason: DisconnectReason) => Promise<void> | void
|
||||
ready: () => Promise<unknown> | unknown
|
||||
packet: (packet: ClientPacketObject<ClientOperation>) => Promise<unknown> | unknown
|
||||
disconnect: (reason: DisconnectReason) => Promise<unknown> | unknown
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ import type { Wit } from 'node-wit'
|
||||
import type { Worker as TesseractWorker } from 'tesseract.js'
|
||||
import { ClientPacketObject } from '../classes/Client.js'
|
||||
import type { Config } from '../utils/getConfig.js'
|
||||
import type { Logger } from '../utils/logger.js'
|
||||
import type { Logger } from '@revanced/bot-shared'
|
||||
|
||||
export { default as parseTextEventHandler } from './parseText.js'
|
||||
export { default as parseImageEventHandler } from './parseImage.js'
|
||||
|
||||
@@ -14,13 +14,8 @@ const parseImageEventHandler: EventHandler<ClientOperation.ParseImage> = async (
|
||||
d: { image_url: imageUrl, id },
|
||||
} = packet
|
||||
|
||||
logger.debug(
|
||||
`Client ${client.id} requested to parse image from URL:`,
|
||||
imageUrl,
|
||||
)
|
||||
logger.debug(
|
||||
`Queue currently has ${queue.remaining}/${config.ocrConcurrentQueues} items in it`,
|
||||
)
|
||||
logger.debug(`Client ${client.id} requested to parse image from URL:`, imageUrl)
|
||||
logger.debug(`Queue currently has ${queue.remaining}/${config.ocrConcurrentQueues} items in it`)
|
||||
|
||||
if (queue.remaining < config.ocrConcurrentQueues) queue.shift()
|
||||
await queue.wait()
|
||||
@@ -30,10 +25,7 @@ const parseImageEventHandler: EventHandler<ClientOperation.ParseImage> = async (
|
||||
|
||||
const { data, jobId } = await tesseractWorker.recognize(imageUrl)
|
||||
|
||||
logger.debug(
|
||||
`Recognized image from URL for client ${client.id} (job ${jobId}):`,
|
||||
data.text,
|
||||
)
|
||||
logger.debug(`Recognized image from URL for client ${client.id} (job ${jobId}):`, data.text)
|
||||
await client.send({
|
||||
op: ServerOperation.ParsedImage,
|
||||
d: {
|
||||
@@ -42,10 +34,7 @@ const parseImageEventHandler: EventHandler<ClientOperation.ParseImage> = async (
|
||||
},
|
||||
})
|
||||
} catch {
|
||||
logger.error(
|
||||
`Failed to parse image from URL for client ${client.id}:`,
|
||||
imageUrl,
|
||||
)
|
||||
logger.error(`Failed to parse image from URL for client ${client.id}:`, imageUrl)
|
||||
await client.send({
|
||||
op: ServerOperation.ParseImageFailed,
|
||||
d: {
|
||||
|
||||
@@ -4,10 +4,7 @@ import { inspect as inspectObject } from 'node:util'
|
||||
|
||||
import type { EventHandler } from './index.js'
|
||||
|
||||
const parseTextEventHandler: EventHandler<ClientOperation.ParseText> = async (
|
||||
packet,
|
||||
{ witClient, logger },
|
||||
) => {
|
||||
const parseTextEventHandler: EventHandler<ClientOperation.ParseText> = async (packet, { witClient, logger }) => {
|
||||
const {
|
||||
client,
|
||||
d: { text, id },
|
||||
|
||||
@@ -9,25 +9,21 @@ import { inspect as inspectObject } from 'node:util'
|
||||
|
||||
import Client from './classes/Client.js'
|
||||
|
||||
import {
|
||||
EventContext,
|
||||
parseImageEventHandler,
|
||||
parseTextEventHandler,
|
||||
} from './events/index.js'
|
||||
import { EventContext, parseImageEventHandler, parseTextEventHandler } from './events/index.js'
|
||||
|
||||
import {
|
||||
DisconnectReason,
|
||||
HumanizedDisconnectReason,
|
||||
} from '@revanced/bot-shared'
|
||||
import { DisconnectReason, HumanizedDisconnectReason, createLogger } from '@revanced/bot-shared'
|
||||
import { WebSocket } from 'ws'
|
||||
import { checkEnv, getConfig, logger } from './utils/index.js'
|
||||
import { checkEnvironment, getConfig } from './utils/index.js'
|
||||
|
||||
// Load config, init logger, check environment
|
||||
|
||||
// Check environment variables and load config
|
||||
const environment = checkEnv(logger)
|
||||
const config = getConfig()
|
||||
const logger = createLogger('websocket-api', {
|
||||
level: config.consoleLogLevel === 'none' ? 'error' : config.consoleLogLevel,
|
||||
silent: config.consoleLogLevel === 'none',
|
||||
})
|
||||
|
||||
if (!config.debugLogsInProduction && environment === 'production')
|
||||
logger.debug = () => {}
|
||||
checkEnvironment(logger)
|
||||
|
||||
// Workers and API clients
|
||||
|
||||
@@ -71,46 +67,25 @@ const server = fastify()
|
||||
clients.add(client)
|
||||
|
||||
logger.debug(`Client ${client.id}'s instance has been added`)
|
||||
logger.info(
|
||||
`New client connected (now ${clients.size} clients) with ID:`,
|
||||
client.id,
|
||||
)
|
||||
logger.info(`New client connected (now ${clients.size} clients) with ID:`, client.id)
|
||||
|
||||
client.on('disconnect', reason => {
|
||||
clients.delete(client)
|
||||
logger.info(
|
||||
`Client ${client.id} disconnected because client ${HumanizedDisconnectReason[reason]}`,
|
||||
)
|
||||
logger.info(`Client ${client.id} disconnected because client ${HumanizedDisconnectReason[reason]}`)
|
||||
})
|
||||
|
||||
client.on('parseText', async packet =>
|
||||
parseTextEventHandler(packet, eventContext),
|
||||
)
|
||||
client.on('parseText', async packet => parseTextEventHandler(packet, eventContext))
|
||||
|
||||
client.on('parseImage', async packet =>
|
||||
parseImageEventHandler(packet, eventContext),
|
||||
)
|
||||
client.on('parseImage', async packet => parseImageEventHandler(packet, eventContext))
|
||||
|
||||
if (['debug', 'silly'].includes(config.consoleLogLevel)) {
|
||||
logger.debug('Debug logs enabled, attaching debug events...')
|
||||
|
||||
if (
|
||||
environment === 'development' &&
|
||||
!config.debugLogsInProduction
|
||||
) {
|
||||
logger.debug(
|
||||
'Running development mode or debug logs in production is enabled, attaching debug events...',
|
||||
)
|
||||
client.on('packet', ({ client, ...rawPacket }) =>
|
||||
logger.debug(
|
||||
`Packet received from client ${client.id}:`,
|
||||
inspectObject(rawPacket),
|
||||
),
|
||||
logger.debug(`Packet received from client ${client.id}: ${inspectObject(rawPacket)}`),
|
||||
)
|
||||
|
||||
client.on('heartbeat', () =>
|
||||
logger.debug(
|
||||
'Heartbeat received from client',
|
||||
client.id,
|
||||
),
|
||||
)
|
||||
client.on('heartbeat', () => logger.debug('Heartbeat received from client', client.id))
|
||||
}
|
||||
} catch (e) {
|
||||
if (e instanceof Error) logger.error(e.stack ?? e.message)
|
||||
@@ -125,22 +100,19 @@ const server = fastify()
|
||||
return connection.socket.terminate()
|
||||
}
|
||||
|
||||
if (client.disconnected === false)
|
||||
client.disconnect(DisconnectReason.ServerError)
|
||||
if (client.disconnected === false) client.disconnect(DisconnectReason.ServerError)
|
||||
else client.forceDisconnect()
|
||||
|
||||
clients.delete(client)
|
||||
|
||||
logger.debug(
|
||||
`Client ${client.id} disconnected because of an internal error`,
|
||||
)
|
||||
logger.debug(`Client ${client.id} disconnected because of an internal error`)
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
// Start the server
|
||||
|
||||
logger.debug('Starting with these configurations:', inspectObject(config))
|
||||
logger.debug(`Starting with these configurations: ${inspectObject(config)}`, )
|
||||
|
||||
await server.listen({
|
||||
host: config.address ?? '0.0.0.0',
|
||||
@@ -150,8 +122,4 @@ await server.listen({
|
||||
const addressInfo = server.server.address()
|
||||
if (!addressInfo || typeof addressInfo !== 'object')
|
||||
logger.debug('Server started, but cannot determine address information')
|
||||
else
|
||||
logger.info(
|
||||
'Server started at:',
|
||||
`${addressInfo.address}:${addressInfo.port}`,
|
||||
)
|
||||
else logger.info(`Server started at: ${addressInfo.address}:${addressInfo.port}`)
|
||||
|
||||
@@ -1,31 +0,0 @@
|
||||
import type { Logger } from './logger.js'
|
||||
|
||||
export default function checkEnv(logger: Logger) {
|
||||
if (!process.env['NODE_ENV'])
|
||||
logger.warn('NODE_ENV not set, defaulting to `development`')
|
||||
const environment = (process.env['NODE_ENV'] ??
|
||||
'development') as NodeEnvironment
|
||||
|
||||
if (!['development', 'production'].includes(environment)) {
|
||||
logger.error(
|
||||
'NODE_ENV is neither `development` nor `production`, unable to determine environment',
|
||||
)
|
||||
logger.info('Set NODE_ENV to blank to use `development` mode')
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
logger.info(`Running in ${environment} mode...`)
|
||||
|
||||
if (environment === 'production' && process.env['IS_USING_DOT_ENV']) {
|
||||
logger.warn(
|
||||
'You seem to be using .env files, this is generally not a good idea in production...',
|
||||
)
|
||||
}
|
||||
|
||||
if (!process.env['WIT_AI_TOKEN']) {
|
||||
logger.error('WIT_AI_TOKEN is not defined in the environment variables')
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
return environment
|
||||
}
|
||||
23
apis/websocket/src/utils/checkEnvironment.ts
Executable file
23
apis/websocket/src/utils/checkEnvironment.ts
Executable file
@@ -0,0 +1,23 @@
|
||||
import type { Logger } from '@revanced/bot-shared'
|
||||
|
||||
export default function checkEnvironment(logger: Logger) {
|
||||
if (!process.env['NODE_ENV']) logger.warn('NODE_ENV not set, defaulting to `development`')
|
||||
const environment = (process.env['NODE_ENV'] ?? 'development') as NodeEnvironment
|
||||
|
||||
if (!['development', 'production'].includes(environment)) {
|
||||
logger.error('NODE_ENV is neither `development` nor `production`, unable to determine environment')
|
||||
logger.info('Set NODE_ENV to blank to use `development` mode')
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
logger.info(`Running in ${environment} mode...`)
|
||||
|
||||
if (environment === 'production' && process.env['IS_USING_DOT_ENV']) {
|
||||
logger.warn('You seem to be using .env files, this is generally not a good idea in production...')
|
||||
}
|
||||
|
||||
if (!process.env['WIT_AI_TOKEN']) {
|
||||
logger.error('WIT_AI_TOKEN is not defined in the environment variables')
|
||||
process.exit(1)
|
||||
}
|
||||
}
|
||||
@@ -22,17 +22,14 @@ type BaseTypeOf<T> = T extends (infer U)[]
|
||||
? { [K in keyof T]: T[K] }
|
||||
: T
|
||||
|
||||
export type Config = Omit<
|
||||
BaseTypeOf<typeof import('../../config.json')>,
|
||||
'$schema'
|
||||
>
|
||||
export type Config = Omit<BaseTypeOf<typeof import('../../config.json')>, '$schema'>
|
||||
|
||||
export const defaultConfig: Config = {
|
||||
address: '127.0.0.1',
|
||||
port: 80,
|
||||
ocrConcurrentQueues: 1,
|
||||
clientHeartbeatInterval: 60000,
|
||||
debugLogsInProduction: false,
|
||||
consoleLogLevel: 'info',
|
||||
}
|
||||
|
||||
export default function getConfig() {
|
||||
|
||||
@@ -1,3 +1,2 @@
|
||||
export { default as getConfig } from './getConfig.js'
|
||||
export { default as checkEnv } from './checkEnv.js'
|
||||
export { default as logger } from './logger.js'
|
||||
export { default as checkEnvironment } from './checkEnvironment.js'
|
||||
|
||||
@@ -1,25 +0,0 @@
|
||||
import { Chalk } from 'chalk'
|
||||
|
||||
const chalk = new Chalk()
|
||||
const logger = {
|
||||
debug: (...args) => console.debug(chalk.gray('DEBUG:', ...args)),
|
||||
info: (...args) =>
|
||||
console.info(chalk.bgBlue.whiteBright(' INFO '), ...args),
|
||||
warn: (...args) =>
|
||||
console.warn(
|
||||
chalk.bgYellow.blackBright.bold(' WARN '),
|
||||
chalk.yellowBright(...args),
|
||||
),
|
||||
error: (...args) =>
|
||||
console.error(
|
||||
chalk.bgRed.whiteBright.bold(' ERROR '),
|
||||
chalk.redBright(...args),
|
||||
),
|
||||
log: console.log,
|
||||
} satisfies Logger
|
||||
|
||||
export type LogLevel = 'debug' | 'info' | 'warn' | 'error' | 'log'
|
||||
export type LogFunction = (...x: unknown[]) => void
|
||||
export type Logger = Record<LogLevel, LogFunction>
|
||||
|
||||
export default logger
|
||||
Reference in New Issue
Block a user