@georgemiao/qbit.js
Version:
a qBittorrent library made with typescript
95 lines • 3.03 kB
JavaScript
import EventEmitter from "node:events";
import http from "node:http";
import https from "node:https";
import { URL } from "node:url";
import fetch from "node-fetch";
import { Api } from "./Api.js";
import { Application } from "./Application.js";
export class QBittorrent extends EventEmitter {
/**
* Create a new qBittorrent client
* @param host qBittorrent host
* @param insecure if to allow self signed certs
*/
constructor(host, insecure = false) {
super();
/** @private */
this.destroyed = false;
this.session = "";
this.exp = 0;
const parsedHost = new URL(host);
if (!["http:", "https:"].includes(parsedHost.protocol))
throw new Error(`Invalid protocol "${parsedHost.protocol}"!`);
this.host = parsedHost.href;
this.agent =
parsedHost.protocol === "http:"
? new http.Agent()
: new https.Agent({ rejectUnauthorized: !insecure });
this.api = new Api(this);
this.application = new Application(this);
// TODO: main sync loop and events
// TODO: torrents, rss and settings managers
// TODO: format responses
// TODO: maintain a cache with sync loop
// TODO: abstract objects for things
}
/**
* Function to run before any action.
* Checks if the client is destroyed,
* not logged in yet or session expired
* and logs automaticly back in if is.
* @private
*/
async checkLogin() {
if (this.destroyed)
throw new Error("Client destroyed.");
await this.defer;
if (!this.session || !this.user || !this.password)
throw new Error("Not logged in");
if (Date.now() > this.exp) {
this.defer = this.sessionLogin(this.user, this.password);
await this.defer;
}
}
/**
* Wrapper for node-fetch
* Adds session cookies, base path and http agent
* @private
* @param url api url to fetch
* @param opts fetch options
*/
async fetch(url, opts) {
const res = await fetch(`${this.host}api/v2/${url}`, {
...opts,
headers: {
...opts?.headers,
Cookie: this.session,
Referrer: this.host,
},
agent: this.agent,
});
return res;
}
/**
* Log out and destroy the client
*/
async logout() {
this.destroyed = true;
await this.api.logout();
}
/**
* Login to qBittorrent
*/
async login(username, password) {
this.defer = this.sessionLogin(username, password);
this.user = username;
this.password = password;
await this.defer;
}
async sessionLogin(username, password) {
const session = await this.api.login(username, password);
this.session = session;
this.exp = Date.now() + 55 * 60 * 1000;
}
}
//# sourceMappingURL=QBittorrent.js.map