From a90e59edb8698868bf6d52a33dd0208e70466c48 Mon Sep 17 00:00:00 2001 From: PalmDevs Date: Sun, 14 Jan 2024 21:46:25 +0700 Subject: [PATCH] refactor(apis/websocket): clean up imports, format, and fix issues --- apis/websocket/src/classes/Client.ts | 8 ++++---- apis/websocket/src/events/index.ts | 10 +++++----- apis/websocket/src/events/parseImage.ts | 2 +- apis/websocket/src/events/parseText.ts | 5 ++--- apis/websocket/src/index.ts | 11 ++++------- apis/websocket/src/utils/getConfig.ts | 14 +++++++------- apis/websocket/src/utils/index.ts | 4 ++-- 7 files changed, 25 insertions(+), 29 deletions(-) diff --git a/apis/websocket/src/classes/Client.ts b/apis/websocket/src/classes/Client.ts index 6de63d0..40037c3 100755 --- a/apis/websocket/src/classes/Client.ts +++ b/apis/websocket/src/classes/Client.ts @@ -1,4 +1,4 @@ -import { EventEmitter } from 'node:events' +import { EventEmitter } from 'events' import { ClientOperation, DisconnectReason, @@ -167,8 +167,8 @@ export default class Client { protected _toBuffer(data: RawData) { if (data instanceof Buffer) return data - else if (data instanceof ArrayBuffer) return Buffer.from(data) - else return Buffer.concat(data) + if (data instanceof ArrayBuffer) return Buffer.from(data) + return Buffer.concat(data) } } @@ -186,7 +186,7 @@ export type ClientEventName = keyof typeof ClientOperation export type ClientEventHandlers = { [K in Uncapitalize]: ( - packet: ClientPacketObject]>, + packet: ClientPacketObject<(typeof ClientOperation)[Capitalize]>, ) => Promise | unknown } & { ready: () => Promise | unknown diff --git a/apis/websocket/src/events/index.ts b/apis/websocket/src/events/index.ts index 9f1ed4f..6e81e88 100755 --- a/apis/websocket/src/events/index.ts +++ b/apis/websocket/src/events/index.ts @@ -1,12 +1,12 @@ import type { ClientOperation } from '@revanced/bot-shared' +import type { Logger } from '@revanced/bot-shared' 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 '@revanced/bot-shared' +import { ClientPacketObject } from '../classes/Client' +import type { Config } from '../utils/getConfig' -export { default as parseTextEventHandler } from './parseText.js' -export { default as parseImageEventHandler } from './parseImage.js' +export { default as parseTextEventHandler } from './parseText' +export { default as parseImageEventHandler } from './parseImage' export type EventHandler = ( packet: ClientPacketObject, diff --git a/apis/websocket/src/events/parseImage.ts b/apis/websocket/src/events/parseImage.ts index 61fa14a..b48bf09 100755 --- a/apis/websocket/src/events/parseImage.ts +++ b/apis/websocket/src/events/parseImage.ts @@ -1,7 +1,7 @@ import { ClientOperation, ServerOperation } from '@revanced/bot-shared' import { AsyncQueue } from '@sapphire/async-queue' -import type { EventHandler } from './index.js' +import type { EventHandler } from './index' const queue = new AsyncQueue() diff --git a/apis/websocket/src/events/parseText.ts b/apis/websocket/src/events/parseText.ts index 0d9246f..690dbb5 100755 --- a/apis/websocket/src/events/parseText.ts +++ b/apis/websocket/src/events/parseText.ts @@ -1,8 +1,8 @@ import { ClientOperation, ServerOperation } from '@revanced/bot-shared' -import { inspect as inspectObject } from 'node:util' +import { inspect as inspectObject } from 'util' -import type { EventHandler } from './index.js' +import type { EventHandler } from './index' const parseTextEventHandler: EventHandler = async (packet, { witClient, logger }) => { const { @@ -14,7 +14,6 @@ const parseTextEventHandler: EventHandler = async (pa try { const { intents } = await witClient.message(text, {}) - // eslint-disable-next-line @typescript-eslint/no-unused-vars const intentsWithoutIds = intents.map(({ id, ...rest }) => rest) await client.send({ diff --git a/apis/websocket/src/index.ts b/apis/websocket/src/index.ts index d5e287a..1145d64 100755 --- a/apis/websocket/src/index.ts +++ b/apis/websocket/src/index.ts @@ -5,15 +5,15 @@ import witPkg from 'node-wit' import { createWorker as createTesseractWorker } from 'tesseract.js' const { Wit } = witPkg -import { inspect as inspectObject } from 'node:util' +import { inspect as inspectObject } from 'util' -import Client from './classes/Client.js' +import Client from './classes/Client' -import { EventContext, parseImageEventHandler, parseTextEventHandler } from './events/index.js' +import { EventContext, parseImageEventHandler, parseTextEventHandler } from './events/index' import { DisconnectReason, HumanizedDisconnectReason, createLogger } from '@revanced/bot-shared' import { WebSocket } from 'ws' -import { checkEnvironment, getConfig } from './utils/index.js' +import { checkEnvironment, getConfig } from './utils/index' // Load config, init logger, check environment @@ -31,9 +31,6 @@ const witClient = new Wit({ accessToken: process.env['WIT_AI_TOKEN']!, }) -logger.fatal('test') -logger.error('test') - // Server logic const clients = new Set() diff --git a/apis/websocket/src/utils/getConfig.ts b/apis/websocket/src/utils/getConfig.ts index 841c219..d26042e 100755 --- a/apis/websocket/src/utils/getConfig.ts +++ b/apis/websocket/src/utils/getConfig.ts @@ -1,6 +1,6 @@ -import { existsSync } from 'node:fs' -import { resolve as resolvePath } from 'node:path' -import { pathToFileURL } from 'node:url' +import { existsSync } from 'fs' +import { resolve as resolvePath } from 'path' +import { pathToFileURL } from 'url' const configPath = resolvePath(process.cwd(), 'config.json') @@ -17,10 +17,10 @@ const userConfig: Partial = existsSync(configPath) type BaseTypeOf = T extends (infer U)[] ? U[] : T extends (...args: unknown[]) => infer U - ? (...args: unknown[]) => U - : T extends object - ? { [K in keyof T]: T[K] } - : T + ? (...args: unknown[]) => U + : T extends object + ? { [K in keyof T]: T[K] } + : T export type Config = Omit, '$schema'> diff --git a/apis/websocket/src/utils/index.ts b/apis/websocket/src/utils/index.ts index 4aab21d..24a2c7c 100755 --- a/apis/websocket/src/utils/index.ts +++ b/apis/websocket/src/utils/index.ts @@ -1,2 +1,2 @@ -export { default as getConfig } from './getConfig.js' -export { default as checkEnvironment } from './checkEnvironment.js' +export { default as getConfig } from './getConfig' +export { default as checkEnvironment } from './checkEnvironment'