phirepass-widgets 0.0.111 → 0.0.113
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +33 -0
- package/dist/cjs/{index-B8w6l0VL.js → index-CurErEK7.js} +101 -30
- package/dist/cjs/loader.cjs.js +2 -2
- package/dist/cjs/phirepass-downloader_3.cjs.entry.js +3696 -0
- package/dist/cjs/phirepass-rdp.cjs.entry.js +18 -6
- package/dist/cjs/phirepass-terminal.cjs.entry.js +6 -5
- package/dist/cjs/phirepass-widgets.cjs.js +3 -3
- package/dist/cjs/{protocol-CCD7cbHu.js → protocol-DCBMfc2u.js} +557 -360
- package/dist/collection/collection-manifest.json +3 -1
- package/dist/collection/common/channel.js +21 -1
- package/dist/collection/common/fetch.js +40 -0
- package/dist/collection/components/phirepass-downloader/form.js +69 -0
- package/dist/collection/components/phirepass-downloader/format.js +105 -0
- package/dist/collection/components/phirepass-downloader/jobs.js +370 -0
- package/dist/collection/components/phirepass-downloader/phirepass-downloader.css +1109 -0
- package/dist/collection/components/phirepass-downloader/phirepass-downloader.js +1455 -0
- package/dist/collection/components/phirepass-downloader/source.js +106 -0
- package/dist/collection/components/phirepass-downloader/views/dialogs.js +34 -0
- package/dist/collection/components/phirepass-downloader/views/fetch-form.js +65 -0
- package/dist/collection/components/phirepass-downloader/views/icons.js +33 -0
- package/dist/collection/components/phirepass-downloader/views/job-row.js +91 -0
- package/dist/collection/components/phirepass-downloader/views/notices.js +15 -0
- package/dist/collection/components/phirepass-downloader/views/states.js +19 -0
- package/dist/collection/components/phirepass-media-player/phirepass-media-player.css +216 -0
- package/dist/collection/components/phirepass-media-player/phirepass-media-player.js +473 -0
- package/dist/collection/components/phirepass-media-player/player.js +94 -0
- package/dist/collection/components/phirepass-rdp/phirepass-rdp.js +36 -13
- package/dist/collection/components/phirepass-sftp-client/phirepass-sftp-client.css +122 -0
- package/dist/collection/components/phirepass-sftp-client/phirepass-sftp-client.fetch.js +35 -0
- package/dist/collection/components/phirepass-sftp-client/phirepass-sftp-client.js +181 -31
- package/dist/collection/components/phirepass-terminal/phirepass-terminal.js +2 -2
- package/dist/collection/phirepass-channel_bg.wasm +0 -0
- package/dist/collection/testing/fixtures.js +219 -0
- package/dist/collection/testing/mock-channel.js +410 -0
- package/dist/components/index.js +1 -1
- package/dist/components/p-C4cmk3bS.js +1 -0
- package/dist/components/p-CsYQJXY2.js +1 -0
- package/dist/components/p-D6Geq8Rn.js +1 -0
- package/dist/components/phirepass-downloader.d.ts +11 -0
- package/dist/components/phirepass-downloader.js +1 -0
- package/dist/components/phirepass-media-player.d.ts +11 -0
- package/dist/components/phirepass-media-player.js +1 -0
- package/dist/components/phirepass-rdp.js +1 -1
- package/dist/components/phirepass-sftp-client.js +1 -1
- package/dist/components/phirepass-terminal.js +2 -2
- package/dist/esm/{index-DGq2b7rM.js → index-B9qUl6FW.js} +101 -30
- package/dist/esm/loader.js +3 -3
- package/dist/esm/phirepass-downloader_3.entry.js +3692 -0
- package/dist/esm/phirepass-rdp.entry.js +18 -6
- package/dist/esm/phirepass-terminal.entry.js +6 -5
- package/dist/esm/phirepass-widgets.js +4 -4
- package/dist/esm/{protocol-D4JeTIHt.js → protocol-CsYQJXY2.js} +556 -359
- package/dist/phirepass-widgets/p-1b84c526.entry.js +1 -0
- package/dist/phirepass-widgets/{p-9eebbf05.entry.js → p-41dd1ac9.entry.js} +2 -2
- package/dist/phirepass-widgets/p-614ab3de.entry.js +1 -0
- package/dist/phirepass-widgets/p-B9qUl6FW.js +3 -0
- package/dist/phirepass-widgets/p-CsYQJXY2.js +1 -0
- package/dist/phirepass-widgets/phirepass-channel_bg.wasm +0 -0
- package/dist/phirepass-widgets/phirepass-widgets.esm.js +1 -1
- package/dist/types/common/channel.d.ts +30 -0
- package/dist/types/common/fetch.d.ts +161 -0
- package/dist/types/components/phirepass-downloader/form.d.ts +54 -0
- package/dist/types/components/phirepass-downloader/format.d.ts +17 -0
- package/dist/types/components/phirepass-downloader/jobs.d.ts +115 -0
- package/dist/types/components/phirepass-downloader/phirepass-downloader.d.ts +231 -0
- package/dist/types/components/phirepass-downloader/source.d.ts +61 -0
- package/dist/types/components/phirepass-downloader/views/dialogs.d.ts +52 -0
- package/dist/types/components/phirepass-downloader/views/fetch-form.d.ts +31 -0
- package/dist/types/components/phirepass-downloader/views/icons.d.ts +5 -0
- package/dist/types/components/phirepass-downloader/views/job-row.d.ts +21 -0
- package/dist/types/components/phirepass-downloader/views/notices.d.ts +18 -0
- package/dist/types/components/phirepass-downloader/views/states.d.ts +38 -0
- package/dist/types/components/phirepass-media-player/phirepass-media-player.d.ts +67 -0
- package/dist/types/components/phirepass-media-player/player.d.ts +43 -0
- package/dist/types/components/phirepass-rdp/phirepass-rdp.d.ts +10 -0
- package/dist/types/components/phirepass-sftp-client/phirepass-sftp-client.d.ts +30 -0
- package/dist/types/components/phirepass-sftp-client/phirepass-sftp-client.fetch.d.ts +40 -0
- package/dist/types/components.d.ts +426 -34
- package/dist/types/stencil-public-runtime.d.ts +1 -1
- package/dist/types/testing/fixtures.d.ts +25 -0
- package/dist/types/testing/mock-channel.d.ts +132 -0
- package/package.json +26 -12
- package/dist/cjs/phirepass-sftp-client.cjs.entry.js +0 -1469
- package/dist/components/p-D4JeTIHt.js +0 -1
- package/dist/esm/phirepass-sftp-client.entry.js +0 -1467
- package/dist/phirepass-widgets/p-401631af.entry.js +0 -1
- package/dist/phirepass-widgets/p-5f27300b.entry.js +0 -1
- package/dist/phirepass-widgets/p-D4JeTIHt.js +0 -1
- package/dist/phirepass-widgets/p-DGq2b7rM.js +0 -3
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"entries": [
|
|
3
|
+
"components/phirepass-downloader/phirepass-downloader.js",
|
|
4
|
+
"components/phirepass-media-player/phirepass-media-player.js",
|
|
3
5
|
"components/phirepass-rdp/phirepass-rdp.js",
|
|
4
6
|
"components/phirepass-sftp-client/phirepass-sftp-client.js",
|
|
5
7
|
"components/phirepass-terminal/phirepass-terminal.js"
|
|
@@ -7,7 +9,7 @@
|
|
|
7
9
|
"mixins": [],
|
|
8
10
|
"compiler": {
|
|
9
11
|
"name": "@stencil/core",
|
|
10
|
-
"version": "4.45.
|
|
12
|
+
"version": "4.45.3",
|
|
11
13
|
"typescriptVersion": "5.8.3"
|
|
12
14
|
},
|
|
13
15
|
"collections": [],
|
|
@@ -1 +1,21 @@
|
|
|
1
|
-
|
|
1
|
+
const DOWNLOAD_METHODS = [
|
|
2
|
+
'send_download_start',
|
|
3
|
+
'send_download_stop',
|
|
4
|
+
'send_download_resume',
|
|
5
|
+
'send_download_set_order',
|
|
6
|
+
'send_download_delete',
|
|
7
|
+
'send_download_subscribe',
|
|
8
|
+
'send_download_where',
|
|
9
|
+
];
|
|
10
|
+
/**
|
|
11
|
+
* Whether this channel can speak Remote Fetch. A channel built from a
|
|
12
|
+
* `phirepass-channel` release older than T1.10 cannot, and the widgets then
|
|
13
|
+
* keep the feature out of sight instead of offering buttons that do nothing.
|
|
14
|
+
* The folder picker's method is optional (phase 4) and checked where used.
|
|
15
|
+
*/
|
|
16
|
+
export function supportsDownloads(channel) {
|
|
17
|
+
if (!channel || typeof channel !== 'object') {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
return DOWNLOAD_METHODS.every(name => typeof channel[name] === 'function');
|
|
21
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Remote Fetch: the browser side of `common/src/protocol/fetch.rs` and the
|
|
3
|
+
* `Download*` web frames (codes 56–68).
|
|
4
|
+
*
|
|
5
|
+
* Shapes follow serde's defaults as the WASM channel hands them to JS: unit
|
|
6
|
+
* enums by name, data-carrying enums externally tagged (`{ Http: { … } }`).
|
|
7
|
+
* Unit enums that may grow end in `Unknown`, as on the Rust side, so a newer
|
|
8
|
+
* server never breaks an older widget.
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
11
|
+
* `msg_id`s `<phirepass-downloader>` mints start here, so a host sharing its
|
|
12
|
+
* channel (the SFTP client) can tell which `Error` frames to hand over
|
|
13
|
+
* without bookkeeping. `msg_id` is a u32 on the wire.
|
|
14
|
+
*/
|
|
15
|
+
export const DOWNLOADER_MSG_ID_BASE = 0x4000_0000;
|
|
16
|
+
/** Largest `.torrent` accepted from the browser. Mirrors `FETCH_TORRENT_MAX_BYTES`. */
|
|
17
|
+
export const FETCH_TORRENT_MAX_BYTES = 2 * 1024 * 1024;
|
|
18
|
+
const DOWNLOAD_FRAME_TYPES = new Set(['DownloadProgress', 'DownloadAccepted', 'DownloadWhereAnswer', 'DownloadCaps', 'DownloadFoldersAnswer']);
|
|
19
|
+
/** True for the frames a host should hand to `<phirepass-downloader>`. */
|
|
20
|
+
export function isDownloadFrame(web) {
|
|
21
|
+
return DOWNLOAD_FRAME_TYPES.has(web.type);
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Header names a link login may not set: they belong to the transfer itself or
|
|
25
|
+
* to the connection, and the agent refuses them too.
|
|
26
|
+
*/
|
|
27
|
+
export const DENIED_AUTH_HEADERS = new Set([
|
|
28
|
+
'host',
|
|
29
|
+
'content-length',
|
|
30
|
+
'range',
|
|
31
|
+
'if-range',
|
|
32
|
+
'transfer-encoding',
|
|
33
|
+
'connection',
|
|
34
|
+
'keep-alive',
|
|
35
|
+
'proxy-authenticate',
|
|
36
|
+
'proxy-authorization',
|
|
37
|
+
'te',
|
|
38
|
+
'trailer',
|
|
39
|
+
'upgrade',
|
|
40
|
+
]);
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { EMPTY_AUTH, buildSourceAuth, checkTorrentFile, detectSource } from "./source";
|
|
2
|
+
export const EMPTY_FORM = { text: '', saveAs: '', torrent: null, order: null, auth: EMPTY_AUTH, authOpen: false };
|
|
3
|
+
export function chooseTorrent(file) {
|
|
4
|
+
const check = checkTorrentFile(file);
|
|
5
|
+
return check.ok ? { name: file.name, size: file.size } : { name: file.name, size: file.size, error: check.reason };
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Everything the Fetch dialog shows that follows from what was typed or
|
|
9
|
+
* chosen: which fields appear, which note (B4–B7), and whether Start works.
|
|
10
|
+
* `ready` is false while the dialog still waits on something outside the form
|
|
11
|
+
* (where the file will land, or the Start already in flight).
|
|
12
|
+
*/
|
|
13
|
+
export function formModel(form, caps, ready) {
|
|
14
|
+
const detected = form.torrent ? { kind: 'empty' } : detectSource(form.text);
|
|
15
|
+
const isTorrent = !!form.torrent || detected.kind === 'magnet';
|
|
16
|
+
const torrentsOff = isTorrent && !!caps && !caps.torrents;
|
|
17
|
+
const isHttp = detected.kind === 'http' || detected.kind === 'torrent-url';
|
|
18
|
+
let note = null;
|
|
19
|
+
if (form.torrent?.error === 'too-large') {
|
|
20
|
+
note = { tone: 'err', title: 'Too large', body: 'A .torrent file can be up to 2 MB. Use its magnet link instead.' };
|
|
21
|
+
}
|
|
22
|
+
else if (form.torrent?.error === 'not-torrent') {
|
|
23
|
+
note = { tone: 'err', title: 'Not a .torrent', body: 'Choose a file that ends in .torrent.' };
|
|
24
|
+
}
|
|
25
|
+
else if (detected.kind === 'invalid') {
|
|
26
|
+
note = { tone: 'err', title: 'Not a link', body: 'Use http://, https:// or magnet:' };
|
|
27
|
+
}
|
|
28
|
+
else if (torrentsOff) {
|
|
29
|
+
note = { tone: 'warn', title: 'Torrents are off', body: "This node only allows ordinary links. Its owner can allow torrents in the node's settings." };
|
|
30
|
+
}
|
|
31
|
+
const auth = buildSourceAuth(form.auth);
|
|
32
|
+
const hasSource = isHttp || detected.kind === 'magnet' || (!!form.torrent && !form.torrent.error && !!form.torrent.contents);
|
|
33
|
+
const needsOrder = isTorrent && !torrentsOff;
|
|
34
|
+
const canStart = ready && !!caps && hasSource && !note && (!needsOrder || form.order !== null) && (!isHttp || !form.authOpen || !auth.error);
|
|
35
|
+
let askedName;
|
|
36
|
+
if (isHttp) {
|
|
37
|
+
askedName = form.saveAs.trim() || detected.name;
|
|
38
|
+
}
|
|
39
|
+
else if (detected.kind === 'magnet') {
|
|
40
|
+
askedName = detected.name;
|
|
41
|
+
}
|
|
42
|
+
const typed = form.auth.username || form.auth.token || form.auth.headerName || form.auth.headerValue;
|
|
43
|
+
const authError = isHttp && form.authOpen && typed ? auth.error : undefined;
|
|
44
|
+
return { detected, isTorrent, needsOrder, showSaveAs: detected.kind === 'http', showAuth: isHttp, note, canStart, askedName, authError };
|
|
45
|
+
}
|
|
46
|
+
/** The `FetchSource`, order and options a valid form starts. */
|
|
47
|
+
export function buildStart(form) {
|
|
48
|
+
const options = { source_auth: null, ignore_window: false };
|
|
49
|
+
if (form.torrent) {
|
|
50
|
+
if (!form.torrent.contents || form.torrent.error) {
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
return { source: { TorrentFile: { name: form.torrent.name, contents: form.torrent.contents } }, inOrder: form.order === 'in', options };
|
|
54
|
+
}
|
|
55
|
+
const detected = detectSource(form.text);
|
|
56
|
+
if (detected.kind === 'magnet') {
|
|
57
|
+
return { source: { Magnet: { uri: detected.uri } }, inOrder: form.order === 'in', options };
|
|
58
|
+
}
|
|
59
|
+
if (detected.kind === 'http' || detected.kind === 'torrent-url') {
|
|
60
|
+
const auth = form.authOpen ? buildSourceAuth(form.auth) : { auth: null };
|
|
61
|
+
if ('error' in auth && auth.error) {
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
const saveAs = form.saveAs.trim();
|
|
65
|
+
// A link always downloads front to back.
|
|
66
|
+
return { source: { Http: { url: detected.url, save_as: saveAs || null } }, inOrder: true, options: { ...options, source_auth: auth.auth } };
|
|
67
|
+
}
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
const UNITS = ['B', 'KB', 'MB', 'GB', 'TB'];
|
|
2
|
+
/** `356 MB`, `4.31 GB`, `2 KB`. Decimal units, as the mockups and disk tools use. */
|
|
3
|
+
export function formatBytes(bytes) {
|
|
4
|
+
if (bytes === null || bytes === undefined || !Number.isFinite(bytes) || bytes < 0) {
|
|
5
|
+
return '—';
|
|
6
|
+
}
|
|
7
|
+
let value = bytes;
|
|
8
|
+
let unit = 0;
|
|
9
|
+
while (value >= 1000 && unit < UNITS.length - 1) {
|
|
10
|
+
value /= 1000;
|
|
11
|
+
unit++;
|
|
12
|
+
}
|
|
13
|
+
if (unit === 0) {
|
|
14
|
+
return `${Math.round(value)} B`;
|
|
15
|
+
}
|
|
16
|
+
// 812 MB, 11.8 MB, 4.31 GB, 1.2 KB: more digits only where a unit is coarse.
|
|
17
|
+
const digits = value >= 100 ? 0 : value >= 10 ? 1 : unit >= 3 ? 2 : 1;
|
|
18
|
+
const text = value.toFixed(digits);
|
|
19
|
+
// Trim trailing zeros: "2.00 GB" reads as "2 GB".
|
|
20
|
+
return `${digits ? text.replace(/\.?0+$/, '') : text} ${UNITS[unit]}`;
|
|
21
|
+
}
|
|
22
|
+
/** `↓ 11.8 MB/s`. A rate of zero shows nothing worth reading, so it returns ''. */
|
|
23
|
+
export function formatRate(bps, arrow = '↓') {
|
|
24
|
+
if (!bps || bps <= 0) {
|
|
25
|
+
return '';
|
|
26
|
+
}
|
|
27
|
+
return `${arrow} ${formatBytes(bps)}/s`;
|
|
28
|
+
}
|
|
29
|
+
/** `48s`, `3m 40s`, `12m`, `2h 5m`. */
|
|
30
|
+
export function formatDuration(totalSeconds) {
|
|
31
|
+
const s = Math.max(0, Math.round(totalSeconds));
|
|
32
|
+
if (s < 60) {
|
|
33
|
+
return `${s}s`;
|
|
34
|
+
}
|
|
35
|
+
const m = Math.floor(s / 60);
|
|
36
|
+
if (m < 10) {
|
|
37
|
+
const rest = s % 60;
|
|
38
|
+
return rest ? `${m}m ${rest}s` : `${m}m`;
|
|
39
|
+
}
|
|
40
|
+
if (m < 60) {
|
|
41
|
+
return `${m}m`;
|
|
42
|
+
}
|
|
43
|
+
const h = Math.floor(m / 60);
|
|
44
|
+
const rm = m % 60;
|
|
45
|
+
if (h < 24) {
|
|
46
|
+
return rm ? `${h}h ${rm}m` : `${h}h`;
|
|
47
|
+
}
|
|
48
|
+
const d = Math.floor(h / 24);
|
|
49
|
+
return `${d}d ${h % 24}h`;
|
|
50
|
+
}
|
|
51
|
+
/** Time left at the current rate, or '' when it cannot be told. */
|
|
52
|
+
export function formatEta(done, total, bps) {
|
|
53
|
+
if (!total || !bps || bps <= 0 || done >= total) {
|
|
54
|
+
return '';
|
|
55
|
+
}
|
|
56
|
+
return `${formatDuration((total - done) / bps)} left`;
|
|
57
|
+
}
|
|
58
|
+
/** `38.7%`. One decimal under 100, none at 100. */
|
|
59
|
+
export function formatPercent(done, total) {
|
|
60
|
+
if (!total || total <= 0) {
|
|
61
|
+
return '';
|
|
62
|
+
}
|
|
63
|
+
const pct = Math.min(100, (done / total) * 100);
|
|
64
|
+
return pct >= 100 ? '100%' : `${pct.toFixed(1)}%`;
|
|
65
|
+
}
|
|
66
|
+
export function percent(done, total) {
|
|
67
|
+
if (!total || total <= 0) {
|
|
68
|
+
return 0;
|
|
69
|
+
}
|
|
70
|
+
return Math.max(0, Math.min(100, (done / total) * 100));
|
|
71
|
+
}
|
|
72
|
+
/** Media clock: `2:31`, `1:02:09`. */
|
|
73
|
+
export function formatClock(seconds) {
|
|
74
|
+
if (!Number.isFinite(seconds) || seconds < 0) {
|
|
75
|
+
return '0:00';
|
|
76
|
+
}
|
|
77
|
+
const s = Math.floor(seconds);
|
|
78
|
+
const h = Math.floor(s / 3600);
|
|
79
|
+
const m = Math.floor((s % 3600) / 60);
|
|
80
|
+
const sec = String(s % 60).padStart(2, '0');
|
|
81
|
+
return h ? `${h}:${String(m).padStart(2, '0')}:${sec}` : `${m}:${sec}`;
|
|
82
|
+
}
|
|
83
|
+
/** `today 21:52`, `yesterday 09:10`, `13 Nov 21:52`. */
|
|
84
|
+
export function formatWhen(ms, now = Date.now()) {
|
|
85
|
+
if (!ms) {
|
|
86
|
+
return '';
|
|
87
|
+
}
|
|
88
|
+
const d = new Date(ms);
|
|
89
|
+
const time = `${String(d.getHours()).padStart(2, '0')}:${String(d.getMinutes()).padStart(2, '0')}`;
|
|
90
|
+
const today = new Date(now);
|
|
91
|
+
const startOfToday = new Date(today.getFullYear(), today.getMonth(), today.getDate()).getTime();
|
|
92
|
+
if (ms >= startOfToday) {
|
|
93
|
+
return `today ${time}`;
|
|
94
|
+
}
|
|
95
|
+
if (ms >= startOfToday - 86_400_000) {
|
|
96
|
+
return `yesterday ${time}`;
|
|
97
|
+
}
|
|
98
|
+
const month = d.toLocaleString('en-GB', { month: 'short' });
|
|
99
|
+
return `${d.getDate()} ${month} ${time}`;
|
|
100
|
+
}
|
|
101
|
+
/** `13 Nov`. */
|
|
102
|
+
export function formatDay(ms) {
|
|
103
|
+
const d = new Date(ms);
|
|
104
|
+
return `${d.getDate()} ${d.toLocaleString('en-GB', { month: 'short' })}`;
|
|
105
|
+
}
|
|
@@ -0,0 +1,370 @@
|
|
|
1
|
+
import { formatBytes, formatClock, formatDuration, formatEta, formatPercent, formatRate, formatWhen } from "./format";
|
|
2
|
+
/** Ends of a download where it stops moving by itself. */
|
|
3
|
+
const FINISHED = new Set(['Completed', 'Seeding']);
|
|
4
|
+
export function isFinished(state) {
|
|
5
|
+
return FINISHED.has(state);
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Folds one `DownloadProgress` into the rows.
|
|
9
|
+
*
|
|
10
|
+
* `completed` is set only the first time a job reaches Completed or Seeding
|
|
11
|
+
* from anything else: a re-sent Completed (after a reconnect, or the snapshot a
|
|
12
|
+
* fresh subscription gets) is not a second completion. A job first seen already
|
|
13
|
+
* finished is not one either. `Deleted` removes the row.
|
|
14
|
+
*/
|
|
15
|
+
export function applyProgress(prev, job) {
|
|
16
|
+
const before = prev[job.download_id];
|
|
17
|
+
if (job.state === 'Deleted') {
|
|
18
|
+
if (!before) {
|
|
19
|
+
return { jobs: prev, first: false };
|
|
20
|
+
}
|
|
21
|
+
const { [job.download_id]: _gone, ...rest } = prev;
|
|
22
|
+
return { jobs: rest, first: false };
|
|
23
|
+
}
|
|
24
|
+
const jobs = { ...prev, [job.download_id]: { ...job, deleting: before?.deleting } };
|
|
25
|
+
const completed = before && !isFinished(before.state) && isFinished(job.state) ? job : undefined;
|
|
26
|
+
return { jobs, completed, first: !before };
|
|
27
|
+
}
|
|
28
|
+
const ACTIVE_STATES = new Set(['Queued', 'Scheduled', 'Resolving', 'FetchingMetadata', 'Downloading', 'Verifying', 'Paused', 'Stopped', 'Unknown']);
|
|
29
|
+
export function tabOf(row) {
|
|
30
|
+
if (row.state === 'Retrying') {
|
|
31
|
+
return 'retrying';
|
|
32
|
+
}
|
|
33
|
+
if (ACTIVE_STATES.has(row.state)) {
|
|
34
|
+
return 'active';
|
|
35
|
+
}
|
|
36
|
+
return 'finished';
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Jobs working towards their bytes: what the badge and “N active” count.
|
|
40
|
+
* A Retrying job is waiting out its backoff, so it is not counted (mockup A
|
|
41
|
+
* shows 3 active beside a retrying row); it has its own tab instead.
|
|
42
|
+
*/
|
|
43
|
+
export function isRunning(row) {
|
|
44
|
+
return ['Queued', 'Scheduled', 'Resolving', 'FetchingMetadata', 'Downloading', 'Verifying', 'Paused'].includes(row.state);
|
|
45
|
+
}
|
|
46
|
+
/** Rows “Clear finished” deletes: Completed and Cancelled, never a seeding torrent. */
|
|
47
|
+
export function isClearable(row) {
|
|
48
|
+
return (row.state === 'Completed' || row.state === 'Cancelled') && !row.deleting;
|
|
49
|
+
}
|
|
50
|
+
const ORDER = {
|
|
51
|
+
Downloading: 0,
|
|
52
|
+
Verifying: 1,
|
|
53
|
+
FetchingMetadata: 2,
|
|
54
|
+
Resolving: 3,
|
|
55
|
+
Paused: 4,
|
|
56
|
+
Queued: 5,
|
|
57
|
+
Scheduled: 6,
|
|
58
|
+
Retrying: 7,
|
|
59
|
+
Stopped: 8,
|
|
60
|
+
Seeding: 9,
|
|
61
|
+
Completed: 10,
|
|
62
|
+
Cancelled: 11,
|
|
63
|
+
Deleted: 12,
|
|
64
|
+
Unknown: 13,
|
|
65
|
+
};
|
|
66
|
+
/** Active work first, newest first within a state; finished rows by when they finished. */
|
|
67
|
+
export function sortRows(rows) {
|
|
68
|
+
return [...rows].sort((a, b) => ORDER[a.state] - ORDER[b.state] || (b.finished_at ?? b.started_at) - (a.finished_at ?? a.started_at));
|
|
69
|
+
}
|
|
70
|
+
export function summarize(rows) {
|
|
71
|
+
const running = rows.filter(isRunning);
|
|
72
|
+
const downBps = running.reduce((n, r) => n + (r.down_bps || 0), 0);
|
|
73
|
+
const sized = running.filter(r => r.total_bytes);
|
|
74
|
+
const done = sized.reduce((n, r) => n + r.done_bytes, 0);
|
|
75
|
+
const total = sized.reduce((n, r) => n + (r.total_bytes ?? 0), 0);
|
|
76
|
+
return { active: running.length, downBps, done, total, percent: total ? (done / total) * 100 : null };
|
|
77
|
+
}
|
|
78
|
+
// ---- playback ----
|
|
79
|
+
const MEDIA_EXT = /\.(mp4|m4v|webm|mkv|mov|mp3|m4a|flac|ogg|oga|opus|wav)$/i;
|
|
80
|
+
const AUDIO_EXT = /\.(mp3|m4a|flac|ogg|oga|opus|wav)$/i;
|
|
81
|
+
export function isMediaName(name) {
|
|
82
|
+
return MEDIA_EXT.test(name);
|
|
83
|
+
}
|
|
84
|
+
export function isAudioName(name) {
|
|
85
|
+
return AUDIO_EXT.test(name);
|
|
86
|
+
}
|
|
87
|
+
/** The file a player opens: the largest media file, which is the one the in-order reader follows. */
|
|
88
|
+
export function mediaFile(row) {
|
|
89
|
+
const media = row.files.filter(f => f.media || isMediaName(f.name));
|
|
90
|
+
if (media.length) {
|
|
91
|
+
return media.reduce((a, b) => (b.size > a.size ? b : a));
|
|
92
|
+
}
|
|
93
|
+
if (!row.files.length && isMediaName(row.name)) {
|
|
94
|
+
return { index: 0, name: row.name, size: row.total_bytes ?? 0, done_bytes: row.done_bytes, media: true, ready_from_start: row.in_order ? row.done_bytes : 0 };
|
|
95
|
+
}
|
|
96
|
+
return null;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Play appears only for video or audio whose size is known and whose download
|
|
100
|
+
* is finished or in order. A Fastest torrent offers “Switch to in order to
|
|
101
|
+
* watch” instead; the server refuses its stream anyway (409).
|
|
102
|
+
*/
|
|
103
|
+
export function playCheck(row) {
|
|
104
|
+
const file = mediaFile(row);
|
|
105
|
+
if (!file || row.deleting) {
|
|
106
|
+
return { play: false, file: null, switchToInOrder: false };
|
|
107
|
+
}
|
|
108
|
+
if (isFinished(row.state)) {
|
|
109
|
+
return { play: true, file, switchToInOrder: false };
|
|
110
|
+
}
|
|
111
|
+
const moving = ['Downloading', 'Verifying', 'Paused', 'Stopped', 'Retrying'].includes(row.state);
|
|
112
|
+
if (!moving || !file.size) {
|
|
113
|
+
return { play: false, file: null, switchToInOrder: false };
|
|
114
|
+
}
|
|
115
|
+
if (row.in_order) {
|
|
116
|
+
return { play: true, file, switchToInOrder: false };
|
|
117
|
+
}
|
|
118
|
+
return { play: false, file: null, switchToInOrder: row.kind === 'Torrent' };
|
|
119
|
+
}
|
|
120
|
+
export function streamUrl(base, nodeId, downloadId, fileIndex) {
|
|
121
|
+
return `${base.replace(/\/+$/, '')}/api/web/stream/${encodeURIComponent(nodeId)}/${encodeURIComponent(downloadId)}/${fileIndex}`;
|
|
122
|
+
}
|
|
123
|
+
// ---- where a job saves ----
|
|
124
|
+
export function joinPath(...parts) {
|
|
125
|
+
const joined = parts
|
|
126
|
+
.filter((p) => !!p)
|
|
127
|
+
.join('/')
|
|
128
|
+
.replace(/\/{2,}/g, '/');
|
|
129
|
+
return joined.length > 1 ? joined.replace(/\/+$/, '') : joined;
|
|
130
|
+
}
|
|
131
|
+
export function folderOf(row) {
|
|
132
|
+
return joinPath(row.dest_root, row.sub_dir);
|
|
133
|
+
}
|
|
134
|
+
/** The path of a running link download's part file, as the SFTP listing shows it. */
|
|
135
|
+
export function partPath(row) {
|
|
136
|
+
return `${joinPath(folderOf(row), row.name)}.phirepass-part`;
|
|
137
|
+
}
|
|
138
|
+
export function statePill(row) {
|
|
139
|
+
if (row.deleting) {
|
|
140
|
+
return { text: 'Deleting…', tone: 'ps' };
|
|
141
|
+
}
|
|
142
|
+
if (row.waitingForNode) {
|
|
143
|
+
return { text: 'Waiting for the node', tone: 'q' };
|
|
144
|
+
}
|
|
145
|
+
switch (row.state) {
|
|
146
|
+
case 'Queued':
|
|
147
|
+
return { text: 'Queued', tone: 'q' };
|
|
148
|
+
case 'Scheduled':
|
|
149
|
+
return { text: 'Scheduled', tone: 'q' };
|
|
150
|
+
case 'Resolving':
|
|
151
|
+
return { text: 'Connecting', tone: 'meta' };
|
|
152
|
+
case 'FetchingMetadata':
|
|
153
|
+
return { text: 'Fetching metadata', tone: 'meta' };
|
|
154
|
+
case 'Downloading':
|
|
155
|
+
return { text: 'Downloading', tone: 'dl' };
|
|
156
|
+
case 'Verifying':
|
|
157
|
+
return { text: 'Checking pieces', tone: 'meta' };
|
|
158
|
+
case 'Paused':
|
|
159
|
+
return { text: row.pause_reason === 'DiskFull' ? 'Paused · disk almost full' : 'Paused', tone: 'mv' };
|
|
160
|
+
case 'Retrying':
|
|
161
|
+
return { text: 'Retrying', tone: 'mv' };
|
|
162
|
+
case 'Seeding':
|
|
163
|
+
return { text: 'Seeding', tone: 'sd' };
|
|
164
|
+
case 'Completed':
|
|
165
|
+
return { text: 'Completed', tone: 'ok' };
|
|
166
|
+
case 'Stopped':
|
|
167
|
+
return { text: 'Stopped', tone: 'ps' };
|
|
168
|
+
case 'Cancelled':
|
|
169
|
+
return { text: 'Cancelled', tone: 'err' };
|
|
170
|
+
default:
|
|
171
|
+
return { text: row.state, tone: 'ps' };
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
/** The second pill on a torrent row: which order it downloads in. */
|
|
175
|
+
export function orderPill(row) {
|
|
176
|
+
if (row.kind !== 'Torrent' || !['Downloading', 'Verifying', 'Paused'].includes(row.state)) {
|
|
177
|
+
return null;
|
|
178
|
+
}
|
|
179
|
+
return row.in_order ? 'In order' : 'Fastest';
|
|
180
|
+
}
|
|
181
|
+
/** The stats line under a row's bar, state by state, as mockups R and R2 draw it. */
|
|
182
|
+
export function statsLine(row, ctx = {}) {
|
|
183
|
+
const now = ctx.now ?? Date.now();
|
|
184
|
+
const pct = formatPercent(row.done_bytes, row.total_bytes);
|
|
185
|
+
const sizes = row.total_bytes ? `${formatBytes(row.done_bytes)} of ${formatBytes(row.total_bytes)}` : row.done_bytes ? formatBytes(row.done_bytes) : '';
|
|
186
|
+
const out = [];
|
|
187
|
+
const push = (text, extra = {}) => text && out.push({ text, ...extra });
|
|
188
|
+
if (row.deleting) {
|
|
189
|
+
push('Waiting for the node to confirm');
|
|
190
|
+
return out;
|
|
191
|
+
}
|
|
192
|
+
if (row.waitingForNode) {
|
|
193
|
+
push('Starts when the node reports in');
|
|
194
|
+
push('It keeps going after you close this tab');
|
|
195
|
+
return out;
|
|
196
|
+
}
|
|
197
|
+
switch (row.state) {
|
|
198
|
+
case 'Queued':
|
|
199
|
+
push('Waiting for a free slot');
|
|
200
|
+
if (ctx.running) {
|
|
201
|
+
push(`${ctx.running} ${ctx.running === 1 ? 'is' : 'are'} running`);
|
|
202
|
+
}
|
|
203
|
+
break;
|
|
204
|
+
case 'Scheduled':
|
|
205
|
+
push('Waits for the download window');
|
|
206
|
+
if (row.next_attempt_at && row.next_attempt_at > now) {
|
|
207
|
+
push(`starts in ${formatDuration((row.next_attempt_at - now) / 1000)}`);
|
|
208
|
+
}
|
|
209
|
+
break;
|
|
210
|
+
case 'Resolving':
|
|
211
|
+
push(ctx.source ? `Contacting ${ctx.source}` : 'Contacting the server');
|
|
212
|
+
break;
|
|
213
|
+
case 'FetchingMetadata':
|
|
214
|
+
push('Asking peers for the torrent info');
|
|
215
|
+
push(`${row.peers} ${row.peers === 1 ? 'peer' : 'peers'}`);
|
|
216
|
+
push(formatClock((now - row.started_at) / 1000));
|
|
217
|
+
break;
|
|
218
|
+
case 'Downloading':
|
|
219
|
+
push(pct, { strong: true });
|
|
220
|
+
push(sizes);
|
|
221
|
+
push(formatRate(row.down_bps));
|
|
222
|
+
if (row.kind === 'Torrent') {
|
|
223
|
+
push(formatRate(row.up_bps, '↑'));
|
|
224
|
+
push(`${row.peers} ${row.peers === 1 ? 'peer' : 'peers'}`);
|
|
225
|
+
}
|
|
226
|
+
push(formatEta(row.done_bytes, row.total_bytes, row.down_bps));
|
|
227
|
+
if (row.restarted_from_zero) {
|
|
228
|
+
push('The file changed on the server, so it started over');
|
|
229
|
+
}
|
|
230
|
+
if (ctx.source) {
|
|
231
|
+
push(ctx.source);
|
|
232
|
+
}
|
|
233
|
+
break;
|
|
234
|
+
case 'Verifying':
|
|
235
|
+
push(pct, { strong: true });
|
|
236
|
+
push('Checking downloaded pieces after a restart');
|
|
237
|
+
push(`${formatBytes(row.done_bytes)} already on disk`);
|
|
238
|
+
break;
|
|
239
|
+
case 'Paused':
|
|
240
|
+
if (row.pause_reason === 'DiskFull') {
|
|
241
|
+
push(row.reason ?? 'Free space is below the floor');
|
|
242
|
+
push('Resumes by itself when there is room');
|
|
243
|
+
}
|
|
244
|
+
else {
|
|
245
|
+
push(pct, { strong: true });
|
|
246
|
+
push('Outside the download window');
|
|
247
|
+
push('Resumes by itself');
|
|
248
|
+
}
|
|
249
|
+
break;
|
|
250
|
+
case 'Retrying':
|
|
251
|
+
push(row.retry_reason ?? row.reason ?? 'Something went wrong', { error: true });
|
|
252
|
+
push(`attempt ${row.attempt}`);
|
|
253
|
+
if (row.next_attempt_at) {
|
|
254
|
+
push(row.next_attempt_at > now ? `Next try in ${formatDuration((row.next_attempt_at - now) / 1000)}` : 'Trying again now');
|
|
255
|
+
}
|
|
256
|
+
if (row.done_bytes) {
|
|
257
|
+
push(`${formatBytes(row.done_bytes)} kept`);
|
|
258
|
+
}
|
|
259
|
+
break;
|
|
260
|
+
case 'Seeding':
|
|
261
|
+
push(formatRate(row.up_bps, '↑'));
|
|
262
|
+
push(`${formatBytes(row.up_total)} uploaded`);
|
|
263
|
+
push(`${row.peers} ${row.peers === 1 ? 'peer' : 'peers'}`);
|
|
264
|
+
if (row.seeding_since) {
|
|
265
|
+
push(`seeding for ${formatDuration((now - row.seeding_since) / 1000).replace(/ \d+[hm]$/, '')}, until you delete it`);
|
|
266
|
+
}
|
|
267
|
+
break;
|
|
268
|
+
case 'Completed': {
|
|
269
|
+
push('100%', { strong: true });
|
|
270
|
+
push(row.files.length > 1 ? `${row.files.length} files · ${formatBytes(row.total_bytes ?? row.done_bytes)}` : formatBytes(row.total_bytes ?? row.done_bytes));
|
|
271
|
+
if (row.finished_at && row.started_at) {
|
|
272
|
+
const secs = (row.finished_at - row.started_at) / 1000;
|
|
273
|
+
push(`took ${formatDuration(secs)}`);
|
|
274
|
+
if (secs > 0 && (row.total_bytes ?? row.done_bytes)) {
|
|
275
|
+
push(`avg ${formatBytes((row.total_bytes ?? row.done_bytes) / secs)}/s`);
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
break;
|
|
279
|
+
}
|
|
280
|
+
case 'Stopped':
|
|
281
|
+
push(pct, { strong: true });
|
|
282
|
+
push(row.reason ?? 'Stopped by you');
|
|
283
|
+
push('Resume continues from here');
|
|
284
|
+
break;
|
|
285
|
+
case 'Cancelled':
|
|
286
|
+
push(row.reason ?? 'Stopped and its part deleted', { error: !!row.reason });
|
|
287
|
+
push('Resume starts from zero');
|
|
288
|
+
break;
|
|
289
|
+
default:
|
|
290
|
+
break;
|
|
291
|
+
}
|
|
292
|
+
return out;
|
|
293
|
+
}
|
|
294
|
+
/** The line under a finished row in the standalone Finished tab (H4). */
|
|
295
|
+
export function finishedLine(row, now = Date.now()) {
|
|
296
|
+
const size = row.files.length > 1 ? `${row.files.length} files · ${formatBytes(row.total_bytes ?? row.done_bytes)}` : formatBytes(row.total_bytes ?? row.done_bytes);
|
|
297
|
+
return [size, folderOf(row)].join(' · ') + (row.finished_at ? ` ${formatWhen(row.finished_at, now)}` : '');
|
|
298
|
+
}
|
|
299
|
+
// ---- notices ----
|
|
300
|
+
/** Default longest retry wait (node setting “Longest retry wait”). */
|
|
301
|
+
export const RETRY_MAX_SECS = 3600;
|
|
302
|
+
/**
|
|
303
|
+
* How long a job has been retrying, from its attempt count: 10 s doubling,
|
|
304
|
+
* capped at the node's longest wait. It ignores jitter and time spent
|
|
305
|
+
* downloading between attempts, which only makes it later, never earlier.
|
|
306
|
+
*/
|
|
307
|
+
export function retryingForSecs(attempt, maxSecs = RETRY_MAX_SECS) {
|
|
308
|
+
let total = 0;
|
|
309
|
+
for (let i = 0; i < attempt; i++) {
|
|
310
|
+
total += Math.min(10 * 2 ** i, maxSecs);
|
|
311
|
+
}
|
|
312
|
+
return total;
|
|
313
|
+
}
|
|
314
|
+
export const STUCK_AFTER_SECS = 24 * 3600;
|
|
315
|
+
export function isStuck(row, maxSecs = RETRY_MAX_SECS) {
|
|
316
|
+
return row.state === 'Retrying' && retryingForSecs(row.attempt, maxSecs) >= STUCK_AFTER_SECS;
|
|
317
|
+
}
|
|
318
|
+
/** True when the node saved under another name than the one asked for (N4). */
|
|
319
|
+
export function renamedByNode(asked, used) {
|
|
320
|
+
return !!asked && asked !== used && / \(\d+\)(\.[^.]*)?$/.test(used);
|
|
321
|
+
}
|
|
322
|
+
/**
|
|
323
|
+
* Why nothing can be started on this node, if anything stops it (B8, B9, L3).
|
|
324
|
+
* Order matters: an agent too old to download is the first thing to fix, an
|
|
325
|
+
* organisation block outranks the node's own switch.
|
|
326
|
+
*/
|
|
327
|
+
export function blockerFor(caps, nodeName, canConfigure) {
|
|
328
|
+
if (!caps.agent_supported) {
|
|
329
|
+
return {
|
|
330
|
+
id: 'too-old',
|
|
331
|
+
title: 'Too old',
|
|
332
|
+
body: `${nodeName} runs an agent that cannot download. Update the agent on that machine, then try again.`,
|
|
333
|
+
};
|
|
334
|
+
}
|
|
335
|
+
if (caps.lock === 'Organisation') {
|
|
336
|
+
return {
|
|
337
|
+
id: 'blocked',
|
|
338
|
+
title: 'Blocked',
|
|
339
|
+
body: `Your organisation does not allow downloads on ${nodeName}.`,
|
|
340
|
+
secondary: 'An owner or admin of the organisation can change this under Organisation → Downloads.',
|
|
341
|
+
};
|
|
342
|
+
}
|
|
343
|
+
if (!caps.http) {
|
|
344
|
+
return {
|
|
345
|
+
id: 'off',
|
|
346
|
+
title: 'Downloads are off',
|
|
347
|
+
body: `Downloads are turned off for ${nodeName}.`,
|
|
348
|
+
secondary: canConfigure ? "You can turn them on in the node's settings." : "Ask the node's owner to turn them on.",
|
|
349
|
+
action: canConfigure ? 'Open node settings' : undefined,
|
|
350
|
+
};
|
|
351
|
+
}
|
|
352
|
+
if (caps.lock === 'NoFolder') {
|
|
353
|
+
return {
|
|
354
|
+
id: 'no-folder',
|
|
355
|
+
title: 'No download folder',
|
|
356
|
+
body: "The download folder on this node is missing or can't be written.",
|
|
357
|
+
secondary: 'Someone who can configure the node can pick another one.',
|
|
358
|
+
action: canConfigure ? 'Open node settings' : undefined,
|
|
359
|
+
};
|
|
360
|
+
}
|
|
361
|
+
return null;
|
|
362
|
+
}
|
|
363
|
+
/**
|
|
364
|
+
* Whether a Cancelled reason is the node's free-space check (K5), which gets
|
|
365
|
+
* its own dialog. The node words it as “needs X … has Y free” or “not enough
|
|
366
|
+
* space”; until the protocol carries a code for it, the wording decides.
|
|
367
|
+
*/
|
|
368
|
+
export function isNoSpace(reason) {
|
|
369
|
+
return !!reason && /not enough (disk )?space|disk (is )?full|needs [\d.]+ ?[kmgt]?b\b.*\bfree\b/i.test(reason);
|
|
370
|
+
}
|