👀 Refactoring file watcher code

This commit is contained in:
2025-06-10 13:30:31 -04:00
parent 999af29800
commit a0671ce6d1
6 changed files with 1288 additions and 1191 deletions

View File

@@ -1,17 +1,49 @@
const WebSocket = require("ws");
const { Socket } = require("airdcpp-apisocket");
import WebSocket from "ws";
// const { Socket } = require("airdcpp-apisocket");
import { Socket } from "airdcpp-apisocket";
/**
* Wrapper around the AirDC++ WebSocket API socket.
* Provides methods to connect, disconnect, and interact with the AirDC++ API.
*/
class AirDCPPSocket {
// Explicitly declare properties
options; // Holds configuration options
socketInstance; // Instance of the AirDCPP Socket
/**
* Configuration options for the underlying socket.
* @private
*/
private options: {
url: string;
autoReconnect: boolean;
reconnectInterval: number;
logLevel: string;
ignoredListenerEvents: string[];
username: string;
password: string;
};
constructor(configuration: any) {
let socketProtocol = configuration.protocol === "https" ? "wss" : "ws";
/**
* Instance of the AirDC++ API socket.
* @private
*/
private socketInstance: any;
/**
* Constructs a new AirDCPPSocket wrapper.
* @param {{ protocol: string; hostname: string; username: string; password: string }} configuration
* Connection configuration: protocol (ws or wss), hostname, username, and password.
*/
constructor(configuration: {
protocol: string;
hostname: string;
username: string;
password: string;
}) {
const socketProtocol =
configuration.protocol === "https" ? "wss" : "ws";
this.options = {
url: `${socketProtocol}://${configuration.hostname}/api/v1/`,
autoReconnect: true,
reconnectInterval: 5000, // milliseconds
reconnectInterval: 5000,
logLevel: "verbose",
ignoredListenerEvents: [
"transfer_statistics",
@@ -21,25 +53,33 @@ class AirDCPPSocket {
username: configuration.username,
password: configuration.password,
};
// Initialize the socket instance using the configured options and WebSocket
// Initialize the AirDC++ socket instance
this.socketInstance = Socket(this.options, WebSocket);
}
// Method to ensure the socket connection is established if required by the library or implementation logic
async connect() {
// Here we'll check if a connect method exists and call it
/**
* Establishes a connection to the AirDC++ server.
* @async
* @returns {Promise<any>} Session information returned by the server.
*/
async connect(): Promise<any> {
if (
this.socketInstance &&
typeof this.socketInstance.connect === "function"
) {
const sessionInformation = await this.socketInstance.connect();
return sessionInformation;
return await this.socketInstance.connect();
}
return Promise.reject(
new Error("Connect method not available on socket instance")
);
}
// Method to ensure the socket is disconnected properly if required by the library or implementation logic
async disconnect() {
// Similarly, check if a disconnect method exists and call it
/**
* Disconnects from the AirDC++ server.
* @async
* @returns {Promise<void>}
*/
async disconnect(): Promise<void> {
if (
this.socketInstance &&
typeof this.socketInstance.disconnect === "function"
@@ -48,19 +88,43 @@ class AirDCPPSocket {
}
}
// Method to post data to an endpoint
async post(endpoint: any, data: any = {}) {
// Call post on the socket instance, assuming post is a valid method of the socket instance
/**
* Sends a POST request to a specific AirDC++ endpoint.
* @async
* @param {string} endpoint - API endpoint path (e.g., "search").
* @param {object} [data={}] - Payload to send with the request.
* @returns {Promise<any>} Response from the AirDC++ server.
*/
async post(endpoint: string, data: object = {}): Promise<any> {
return await this.socketInstance.post(endpoint, data);
}
async get(endpoint: any, data: any = {}) {
// Call post on the socket instance, assuming post is a valid method of the socket instance
/**
* Sends a GET request to a specific AirDC++ endpoint.
* @async
* @param {string} endpoint - API endpoint path (e.g., "search/123").
* @param {object} [data={}] - Query parameters to include.
* @returns {Promise<any>} Response from the AirDC++ server.
*/
async get(endpoint: string, data: object = {}): Promise<any> {
return await this.socketInstance.get(endpoint, data);
}
// Method to add listeners to the socket instance for handling real-time updates or events
async addListener(event: any, handlerName: any, callback: any, id?: any) {
// Attach a listener to the socket instance
/**
* Adds an event listener to the AirDC++ socket.
* @async
* @param {string} event - Event group (e.g., "search" or "queue").
* @param {string} handlerName - Specific event within the group (e.g., "search_result_added").
* @param {Function} callback - Callback to invoke when the event occurs.
* @param {string|number} [id] - Optional identifier (e.g., search instance ID).
* @returns {Promise<any>} Listener registration result.
*/
async addListener(
event: string,
handlerName: string,
callback: (...args: any[]) => void,
id?: string | number
): Promise<any> {
return await this.socketInstance.addListener(
event,
handlerName,