This commit is contained in:
2024-02-07 15:39:44 +01:00
parent 834664184b
commit e7e4d03baf
2 changed files with 45 additions and 0 deletions

View File

@@ -23,6 +23,9 @@ export class RPCClient {
)
}
/**
* Websocket connection
*/
public get socket() {
return this._socket$
}
@@ -61,6 +64,11 @@ export class RPCClient {
return data
}
/**
* Request a new download. Handles arguments sanitization.
* @param req payload
* @returns
*/
public download(req: DownloadRequestArgs) {
if (!req.url) {
return
@@ -105,6 +113,11 @@ export class RPCClient {
})
}
/**
* Requests the available formats for a given url (-f arg)
* @param url requested url
* @returns
*/
public formats(url: string) {
if (url) {
return this.sendHTTP<DLMetadata>({
@@ -116,6 +129,9 @@ export class RPCClient {
}
}
/**
* Requests all downloads
*/
public running() {
this.send({
method: 'Service.Running',
@@ -123,6 +139,10 @@ export class RPCClient {
})
}
/**
* Stops and removes a download asynchronously
* @param id download id
*/
public kill(id: string) {
this.sendHTTP({
method: 'Service.Kill',
@@ -130,6 +150,9 @@ export class RPCClient {
})
}
/**
* Stops and removes all downloads
*/
public killAll() {
this.sendHTTP({
method: 'Service.KillAll',
@@ -137,6 +160,10 @@ export class RPCClient {
})
}
/**
* Get asynchronously the avaliable space on downloads directory
* @returns free space in bytes
*/
public freeSpace() {
return this.sendHTTP<number>({
method: 'Service.FreeSpace',
@@ -144,6 +171,10 @@ export class RPCClient {
})
}
/**
* Get asynchronously the tree view of the download directory
* @returns free space in bytes
*/
public directoryTree() {
return this.sendHTTP<string[]>({
method: 'Service.DirectoryTree',
@@ -151,6 +182,10 @@ export class RPCClient {
})
}
/**
* Updates synchronously yt-dlp executable
* @returns free space in bytes
*/
public updateExecutable() {
return this.sendHTTP({
method: 'Service.UpdateExecutable',

View File

@@ -6,8 +6,10 @@ import type { RPCResult } from './types'
export const rpcHost = writable<string>(localStorage.getItem('rpcHost') ?? 'localhost')
export const rpcPort = writable<number>(Number(localStorage.getItem('rpcPort')) || 3033)
// if authentication is enabled...
export const rpcWebToken = writable<string>(localStorage.getItem('rpcWebToken') ?? '')
// will be used to access the api and archive endpoints
export const serverApiEndpoint = derived(
[rpcHost, rpcPort],
([$host, $port]) => window.location.port == ''
@@ -15,6 +17,7 @@ export const serverApiEndpoint = derived(
: `${window.location.protocol}//${$host}:${$port}`
)
// access the websocket JSON-RPC 1.0 to gather downloads state
export const websocketRpcEndpoint = derived(
[rpcHost, rpcPort],
([$host, $port]) => window.location.port == ''
@@ -22,14 +25,21 @@ export const websocketRpcEndpoint = derived(
: `${window.location.protocol.startsWith('https') ? 'wss:' : 'ws:'}//${$host}:${$port}/rpc/ws`
)
// same as websocket one but using HTTP-POST mainly used to send commands (download, stop, ...)
export const httpPostRpcEndpoint = derived(
serverApiEndpoint,
$ep => window.location.port == '' ? `${$ep}/rpc/http` : `${$ep}/rpc/http`
)
/**
* Will handle Websocket and HTTP-POST communications based on the requested method
*/
export const rpcClient = derived(
[httpPostRpcEndpoint, websocketRpcEndpoint, rpcWebToken],
([$http, $ws, $token]) => new RPCClient($http, $ws, $token)
)
/**
* Stores all the downloads returned by the rpc
*/
export const downloads = writable<O.Option<RPCResult[]>>(O.none)