diff --git a/src/client/base.ts b/src/client/base.ts index 0b35ae5..d01fcbd 100644 --- a/src/client/base.ts +++ b/src/client/base.ts @@ -404,7 +404,11 @@ export interface StartOptions { commandsDir: string; componentsDir: string; connection: { intents: number }; - httpConnection: { publicKey: string; port: number }; + httpConnection: { + publicKey: string; + port: number; + useUWS: boolean; + }; token: string; } diff --git a/src/client/httpclient.ts b/src/client/httpclient.ts index a5d7dd4..fc7ea9b 100644 --- a/src/client/httpclient.ts +++ b/src/client/httpclient.ts @@ -8,7 +8,7 @@ import { filetypeinfo } from 'magic-bytes.js'; import type { HttpRequest, HttpResponse } from 'uWebSockets.js'; import { OverwrittenMimeTypes } from '../api'; import { isBufferLike } from '../api/utils/utils'; -import { isCloudfareWorker, type DeepPartial } from '../common'; +import { MergeOptions, isCloudfareWorker, type DeepPartial } from '../common'; import type { BaseClientOptions, InternalRuntimeConfigHTTP, StartOptions } from './base'; import { BaseClient } from './base'; import { onInteractionCreate } from './oninteractioncreate'; @@ -29,7 +29,7 @@ try { } export class HttpClient extends BaseClient { - app!: ReturnType; + app?: ReturnType; publicKey!: string; publicKeyHex!: Buffer; @@ -66,7 +66,7 @@ export class HttpClient extends BaseClient { }); } - protected async execute(options?: { publicKey?: string; port?: number }) { + protected async execute(options: DeepPartial) { await super.execute(); const { publicKey: publicKeyRC, @@ -74,8 +74,8 @@ export class HttpClient extends BaseClient { applicationId: applicationIdRC, } = await this.getRC(); - const publicKey = options?.publicKey ?? publicKeyRC; - const port = options?.port ?? portRC; + const publicKey = options.publicKey ?? publicKeyRC; + const port = options.port ?? portRC; if (!publicKey) { throw new Error('Expected a publicKey, check your config file'); @@ -89,7 +89,7 @@ export class HttpClient extends BaseClient { this.publicKey = publicKey; this.publicKeyHex = Buffer.from(this.publicKey, 'hex'); - if (UWS) { + if (UWS && options.useUWS) { this.app = UWS.App(); this.app.post('/interactions', (res, req) => { return this.onPacket(res, req); @@ -98,13 +98,16 @@ export class HttpClient extends BaseClient { this.logger.info(`Listening to :${port}/interactions`); }); } else { - this.logger.warn('No UWS installed.'); + if (options.useUWS) return this.logger.warn('No uWebSockets installed.'); + this.logger.info('Use your preferred http server and invoke .fetch() to get started'); } } async start(options: DeepPartial> = {}) { await super.start(options); - return this.execute(options.httpConnection); + return this.execute( + MergeOptions>({ useUWS: true }, options.httpConnection), + ); } protected async verifySignatureGenericRequest(req: Request) { diff --git a/src/common/it/logger.ts b/src/common/it/logger.ts index 74b49c3..0f4385d 100644 --- a/src/common/it/logger.ts +++ b/src/common/it/logger.ts @@ -17,7 +17,7 @@ export type LoggerOptions = { saveOnFile?: boolean; }; -export type CustomCallback = (self: Logger, level: LogLevels, args: unknown[]) => unknown[]; +export type CustomCallback = (self: Logger, level: LogLevels, args: unknown[]) => unknown[] | undefined; /** * Represents a logger utility for logging messages with various log levels. @@ -142,6 +142,7 @@ export class Logger { } else { log = Logger.__callback(this, level, args); } + if (!log) return; this.__write(log); return console.log(...log); }