UNPKG

pipebomb.js

Version:

Library for interacting with Pipe Bomb servers

136 lines 4.4 kB
import Request from "./Request.js"; import PipeBomb from "./index.js"; export default class Context { constructor(serverURL, instance, options) { this.serverURL = serverURL; this.instance = instance; this.options = options; this.instances = new Map(); if (this.serverURL.toLowerCase().startsWith("http://")) { this.serverAddress = this.serverURL.substring(7); } else if (this.serverURL.toLowerCase().startsWith("https://")) { this.serverAddress = this.serverURL.substring(8); } else { this.serverAddress = this.serverURL; } this.token = options?.token || null; this.privateKey = options?.privateKey || null; this.playlistUpdateFrequency = options?.playlistUpdateFrequency ?? 10; } setHost(host) { this.instances.clear(); this.serverURL = host; if (this.serverURL.toLowerCase().startsWith("http://")) { this.serverAddress = this.serverURL.substring(7); } else if (this.serverURL.toLowerCase().startsWith("https://")) { this.serverAddress = this.serverURL.substring(8); } else { this.serverAddress = this.serverURL; } } getHost() { return this.serverURL; } getAddress() { return this.serverAddress; } setToken(token) { this.token = token; } getToken() { return this.token; } setUsername(username) { this.username = username; for (let instance of this.instances.values()) { instance.context.setUsername(username); } } getUsername() { return this.username; } setPrivateKey(privateKey) { this.privateKey = privateKey; this.instances.forEach(instance => { instance.context.setPrivateKey(privateKey); }); } getPrivateKey() { return this.privateKey; } makeRequest(method, endpoint, body) { return new Promise((resolve, reject) => { try { const request = new Request(method, `${this.serverURL}/${endpoint}`, this.token, body || {}); request.addSuccessHandler(response => { resolve(response); }); } catch (e) { reject(e); } }); } async getInstanceForURI(uri) { if (!this.isAuthenticated()) throw "Instance is not authenticated"; if (typeof uri == "number" || !uri.includes("@")) return { ownInstance: true, instance: this.instance, id: uri }; const parts = uri.split("@", 2); if (parts[0] == this.serverAddress) return { ownInstance: true, instance: this.instance, id: parts[1] }; const existingInstance = this.instances.get(parts[0]); if (existingInstance) return { ownInstance: false, instance: existingInstance, id: parts[1] }; const hostInfo = await PipeBomb.checkHost(parts[0]); if (!hostInfo) return { ownInstance: false, instance: null, id: parts[1] }; const serverUrl = `http${hostInfo.https ? "s" : ""}://${parts[0]}`; const instance = new PipeBomb(serverUrl, { ...this.options, includeAddressInIds: true }); await instance.authenticate(this.username, { privateKey: this.privateKey, createIfMissing: true // todo, use callback to make this optional }); this.instances.set(parts[0], instance); return { ownInstance: false, instance: instance, id: parts[1] }; } isAuthenticated() { return !!this.privateKey && !!this.token && !!this.username; } getOptions() { return { ...this.options }; } prefixAddress(id, ignoreOptions) { if ((!ignoreOptions && !this.options?.includeAddressInIds) || (typeof id == "string" && id.includes("@"))) return id; return this.serverAddress + "@" + id; } } //# sourceMappingURL=Context.js.map