feat: useUWS httpConnection option

This commit is contained in:
MARCROCK22 2024-05-12 16:07:26 -04:00
parent b5bb4ed45d
commit 52e03c2cb0
3 changed files with 18 additions and 10 deletions

View File

@ -404,7 +404,11 @@ export interface StartOptions {
commandsDir: string; commandsDir: string;
componentsDir: string; componentsDir: string;
connection: { intents: number }; connection: { intents: number };
httpConnection: { publicKey: string; port: number }; httpConnection: {
publicKey: string;
port: number;
useUWS: boolean;
};
token: string; token: string;
} }

View File

@ -8,7 +8,7 @@ import { filetypeinfo } from 'magic-bytes.js';
import type { HttpRequest, HttpResponse } from 'uWebSockets.js'; import type { HttpRequest, HttpResponse } from 'uWebSockets.js';
import { OverwrittenMimeTypes } from '../api'; import { OverwrittenMimeTypes } from '../api';
import { isBufferLike } from '../api/utils/utils'; 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 type { BaseClientOptions, InternalRuntimeConfigHTTP, StartOptions } from './base';
import { BaseClient } from './base'; import { BaseClient } from './base';
import { onInteractionCreate } from './oninteractioncreate'; import { onInteractionCreate } from './oninteractioncreate';
@ -29,7 +29,7 @@ try {
} }
export class HttpClient extends BaseClient { export class HttpClient extends BaseClient {
app!: ReturnType<typeof import('uWebSockets.js').App>; app?: ReturnType<typeof import('uWebSockets.js').App>;
publicKey!: string; publicKey!: string;
publicKeyHex!: Buffer; publicKeyHex!: Buffer;
@ -66,7 +66,7 @@ export class HttpClient extends BaseClient {
}); });
} }
protected async execute(options?: { publicKey?: string; port?: number }) { protected async execute(options: DeepPartial<StartOptions['httpConnection']>) {
await super.execute(); await super.execute();
const { const {
publicKey: publicKeyRC, publicKey: publicKeyRC,
@ -74,8 +74,8 @@ export class HttpClient extends BaseClient {
applicationId: applicationIdRC, applicationId: applicationIdRC,
} = await this.getRC<InternalRuntimeConfigHTTP>(); } = await this.getRC<InternalRuntimeConfigHTTP>();
const publicKey = options?.publicKey ?? publicKeyRC; const publicKey = options.publicKey ?? publicKeyRC;
const port = options?.port ?? portRC; const port = options.port ?? portRC;
if (!publicKey) { if (!publicKey) {
throw new Error('Expected a publicKey, check your config file'); throw new Error('Expected a publicKey, check your config file');
@ -89,7 +89,7 @@ export class HttpClient extends BaseClient {
this.publicKey = publicKey; this.publicKey = publicKey;
this.publicKeyHex = Buffer.from(this.publicKey, 'hex'); this.publicKeyHex = Buffer.from(this.publicKey, 'hex');
if (UWS) { if (UWS && options.useUWS) {
this.app = UWS.App(); this.app = UWS.App();
this.app.post('/interactions', (res, req) => { this.app.post('/interactions', (res, req) => {
return this.onPacket(res, req); return this.onPacket(res, req);
@ -98,13 +98,16 @@ export class HttpClient extends BaseClient {
this.logger.info(`Listening to <url>:${port}/interactions`); this.logger.info(`Listening to <url>:${port}/interactions`);
}); });
} else { } 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 <HttpClient>.fetch(<Request>) to get started');
} }
} }
async start(options: DeepPartial<Omit<StartOptions, 'connection' | 'eventsDir'>> = {}) { async start(options: DeepPartial<Omit<StartOptions, 'connection' | 'eventsDir'>> = {}) {
await super.start(options); await super.start(options);
return this.execute(options.httpConnection); return this.execute(
MergeOptions<DeepPartial<StartOptions['httpConnection']>>({ useUWS: true }, options.httpConnection),
);
} }
protected async verifySignatureGenericRequest(req: Request) { protected async verifySignatureGenericRequest(req: Request) {

View File

@ -17,7 +17,7 @@ export type LoggerOptions = {
saveOnFile?: boolean; 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. * Represents a logger utility for logging messages with various log levels.
@ -142,6 +142,7 @@ export class Logger {
} else { } else {
log = Logger.__callback(this, level, args); log = Logger.__callback(this, level, args);
} }
if (!log) return;
this.__write(log); this.__write(log);
return console.log(...log); return console.log(...log);
} }