phirepass-widgets 0.0.116 → 0.0.118

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.
Files changed (88) hide show
  1. package/README.md +10 -20
  2. package/dist/cjs/{index-BCgmMoIq.js → index-C3v5xmUo.js} +3 -75
  3. package/dist/cjs/loader.cjs.js +2 -2
  4. package/dist/cjs/phirepass-rdp.cjs.entry.js +5 -16
  5. package/dist/cjs/phirepass-sftp-client.cjs.entry.js +1470 -0
  6. package/dist/cjs/phirepass-terminal.cjs.entry.js +4 -4
  7. package/dist/cjs/phirepass-widgets.cjs.js +3 -3
  8. package/dist/cjs/{protocol-BjOgJZ9-.js → protocol-DCBMfc2u.js} +21 -277
  9. package/dist/collection/collection-manifest.json +1 -3
  10. package/dist/collection/common/channel.js +1 -21
  11. package/dist/collection/components/phirepass-rdp/phirepass-rdp.js +13 -36
  12. package/dist/collection/components/phirepass-sftp-client/phirepass-sftp-client.css +0 -122
  13. package/dist/collection/components/phirepass-sftp-client/phirepass-sftp-client.js +31 -181
  14. package/dist/collection/components/phirepass-terminal/phirepass-terminal.js +2 -2
  15. package/dist/collection/phirepass-channel_bg.wasm +0 -0
  16. package/dist/components/index.js +1 -1
  17. package/dist/components/p-CsYQJXY2.js +1 -0
  18. package/dist/components/phirepass-rdp.js +1 -1
  19. package/dist/components/phirepass-sftp-client.js +1 -1
  20. package/dist/components/phirepass-terminal.js +5 -5
  21. package/dist/esm/{index-Dd9o0CbU.js → index-BVOulYtW.js} +3 -75
  22. package/dist/esm/loader.js +3 -3
  23. package/dist/esm/phirepass-rdp.entry.js +5 -16
  24. package/dist/esm/phirepass-sftp-client.entry.js +1468 -0
  25. package/dist/esm/phirepass-terminal.entry.js +4 -4
  26. package/dist/esm/phirepass-widgets.js +4 -4
  27. package/dist/esm/{protocol-BekUePpu.js → protocol-CsYQJXY2.js} +20 -276
  28. package/dist/phirepass-widgets/{p-c28a9257.entry.js → p-7ea2fdbc.entry.js} +2 -2
  29. package/dist/phirepass-widgets/p-BVOulYtW.js +3 -0
  30. package/dist/phirepass-widgets/p-CsYQJXY2.js +1 -0
  31. package/dist/phirepass-widgets/p-c1c616ca.entry.js +1 -0
  32. package/dist/phirepass-widgets/p-f1bc04a8.entry.js +1 -0
  33. package/dist/phirepass-widgets/phirepass-channel_bg.wasm +0 -0
  34. package/dist/phirepass-widgets/phirepass-widgets.esm.js +1 -1
  35. package/dist/types/common/channel.d.ts +0 -30
  36. package/dist/types/components/phirepass-rdp/phirepass-rdp.d.ts +0 -10
  37. package/dist/types/components/phirepass-sftp-client/phirepass-sftp-client.d.ts +0 -30
  38. package/dist/types/components.d.ts +34 -434
  39. package/package.json +5 -19
  40. package/dist/cjs/phirepass-downloader_3.cjs.entry.js +0 -3812
  41. package/dist/collection/common/fetch.js +0 -44
  42. package/dist/collection/components/phirepass-downloader/form.js +0 -69
  43. package/dist/collection/components/phirepass-downloader/format.js +0 -105
  44. package/dist/collection/components/phirepass-downloader/jobs.js +0 -393
  45. package/dist/collection/components/phirepass-downloader/phirepass-downloader.css +0 -1109
  46. package/dist/collection/components/phirepass-downloader/phirepass-downloader.js +0 -1455
  47. package/dist/collection/components/phirepass-downloader/source.js +0 -120
  48. package/dist/collection/components/phirepass-downloader/views/dialogs.js +0 -34
  49. package/dist/collection/components/phirepass-downloader/views/fetch-form.js +0 -65
  50. package/dist/collection/components/phirepass-downloader/views/icons.js +0 -34
  51. package/dist/collection/components/phirepass-downloader/views/job-row.js +0 -91
  52. package/dist/collection/components/phirepass-downloader/views/notices.js +0 -15
  53. package/dist/collection/components/phirepass-downloader/views/states.js +0 -19
  54. package/dist/collection/components/phirepass-media-player/phirepass-media-player.css +0 -225
  55. package/dist/collection/components/phirepass-media-player/phirepass-media-player.js +0 -549
  56. package/dist/collection/components/phirepass-media-player/player.js +0 -118
  57. package/dist/collection/components/phirepass-sftp-client/phirepass-sftp-client.fetch.js +0 -35
  58. package/dist/collection/testing/fixtures.js +0 -219
  59. package/dist/collection/testing/mock-channel.js +0 -410
  60. package/dist/components/p-5QoAejYG.js +0 -1
  61. package/dist/components/p-BekUePpu.js +0 -1
  62. package/dist/components/p-DIf-Ru26.js +0 -1
  63. package/dist/components/phirepass-downloader.d.ts +0 -11
  64. package/dist/components/phirepass-downloader.js +0 -1
  65. package/dist/components/phirepass-media-player.d.ts +0 -11
  66. package/dist/components/phirepass-media-player.js +0 -1
  67. package/dist/esm/phirepass-downloader_3.entry.js +0 -3808
  68. package/dist/phirepass-widgets/p-70a67e4d.entry.js +0 -1
  69. package/dist/phirepass-widgets/p-BekUePpu.js +0 -1
  70. package/dist/phirepass-widgets/p-Dd9o0CbU.js +0 -3
  71. package/dist/phirepass-widgets/p-ab33f71f.entry.js +0 -1
  72. package/dist/types/common/fetch.d.ts +0 -167
  73. package/dist/types/components/phirepass-downloader/form.d.ts +0 -54
  74. package/dist/types/components/phirepass-downloader/format.d.ts +0 -17
  75. package/dist/types/components/phirepass-downloader/jobs.d.ts +0 -123
  76. package/dist/types/components/phirepass-downloader/phirepass-downloader.d.ts +0 -231
  77. package/dist/types/components/phirepass-downloader/source.d.ts +0 -61
  78. package/dist/types/components/phirepass-downloader/views/dialogs.d.ts +0 -52
  79. package/dist/types/components/phirepass-downloader/views/fetch-form.d.ts +0 -31
  80. package/dist/types/components/phirepass-downloader/views/icons.d.ts +0 -5
  81. package/dist/types/components/phirepass-downloader/views/job-row.d.ts +0 -21
  82. package/dist/types/components/phirepass-downloader/views/notices.d.ts +0 -18
  83. package/dist/types/components/phirepass-downloader/views/states.d.ts +0 -38
  84. package/dist/types/components/phirepass-media-player/phirepass-media-player.d.ts +0 -82
  85. package/dist/types/components/phirepass-media-player/player.d.ts +0 -53
  86. package/dist/types/components/phirepass-sftp-client/phirepass-sftp-client.fetch.d.ts +0 -40
  87. package/dist/types/testing/fixtures.d.ts +0 -25
  88. package/dist/types/testing/mock-channel.d.ts +0 -132
@@ -1,35 +0,0 @@
1
- import { h } from "@stencil/core";
2
- import { Icon } from "../phirepass-downloader/views/icons";
3
- /**
4
- * The SFTP browser's side of Remote Fetch, kept out of the component so it
5
- * stays a host and nothing more: the downloads themselves are the
6
- * `<phirepass-downloader>`'s, and nothing here goes over SFTP.
7
- */
8
- export const PART_SUFFIX = '.phirepass-part';
9
- export function isPartFile(name) {
10
- return name.endsWith(PART_SUFFIX);
11
- }
12
- const PLAYABLE = /\.(mp4|m4v|webm|mkv|mov|mp3|m4a|flac|ogg|oga|opus|wav)$/i;
13
- export function isPlayableFile(name) {
14
- return PLAYABLE.test(name) && !isPartFile(name);
15
- }
16
- function normalize(path) {
17
- const p = path.replace(/\/{2,}/g, '/');
18
- return p.length > 1 ? p.replace(/\/+$/, '') : p;
19
- }
20
- /** Whether a finished download's folder is the one on screen. */
21
- export function sameFolder(a, b) {
22
- return normalize(a) === normalize(b);
23
- }
24
- export function folderOfCompleted(detail) {
25
- return normalize(detail.sub_dir ? `${detail.dest_root}/${detail.sub_dir}` : detail.dest_root);
26
- }
27
- export const FetchButton = ({ count, onClick }) => (h("button", { type: "button", class: "action fetch", onClick: onClick, title: "Fetch from a link, magnet or torrent", "aria-label": "Fetch from a link, magnet or torrent" }, h(Icon, { name: "fetch", size: 14 }), count > 0 && (h("span", { class: "badge", "aria-label": `${count} active` }, count))));
28
- /** K3: replaces the plain delete prompt for a part file that belongs to a running download. */
29
- export const PartDeleteDialog = ({ name, busy, onCancel, onConfirm }) => (h("section", { class: "delete-modal visible" }, h("div", { class: "delete-dialog", role: "dialog", "aria-modal": "true", "aria-label": "This file is still downloading", tabindex: -1, onKeyDown: e => {
30
- if (e.key === 'Escape') {
31
- onCancel();
32
- }
33
- } }, h("div", { class: "title" }, "This file is still downloading"), h("div", { class: "message" }, "Deleting ", h("span", { class: "file-name inline" }, name), " deletes its download."), h("div", { class: "modal-actions" }, h("button", { type: "button", class: "btn secondary", onClick: onCancel, disabled: busy }, "Keep it"), h("button", { type: "button", class: "btn destructive", onClick: onConfirm, disabled: busy }, "Delete download")))));
34
- /** C: right-click a `.torrent` that lies inside the node's download folder. */
35
- export const TorrentContextMenu = ({ menu, onFetch, onDownload, onRename, onDelete, onClose, }) => (h("div", { class: "context-backdrop", onClick: onClose, onContextMenu: e => (e.preventDefault(), onClose()) }, h("div", { class: "context-menu", role: "menu", "aria-label": `Actions for ${menu.name}`, style: { left: `${menu.x}px`, top: `${menu.y}px` }, onClick: e => e.stopPropagation() }, h("button", { type: "button", role: "menuitem", class: "primary", onClick: onFetch }, h(Icon, { name: "fetch", size: 13 }), " Download torrent contents here"), h("hr", null), h("button", { type: "button", role: "menuitem", onClick: onDownload }, "Download"), h("button", { type: "button", role: "menuitem", onClick: onRename }, "Rename"), h("button", { type: "button", role: "menuitem", class: "danger", onClick: onDelete }, "Delete"))));
@@ -1,219 +0,0 @@
1
- /**
2
- * Data for stories and tests: the nodes, folders and downloads the Remote
3
- * Fetch mockups draw, so every screen can be shown with the real widgets.
4
- */
5
- export const NODE_ID = '7b0c3c1e-6f0a-4f43-9d1e-0c5b8f1a2d10';
6
- export const NODE_LABEL = 'raspberrypi';
7
- export const DOWNLOAD_FOLDER = '/home/pi/Downloads';
8
- export const MEDIA_FOLDER = '/home/pi/media';
9
- const MB = 1_000_000;
10
- const GB = 1_000_000_000;
11
- export function caps(over = {}) {
12
- return {
13
- http: true,
14
- torrents: true,
15
- private_targets: false,
16
- max_active: 3,
17
- lock: 'None',
18
- folder: DOWNLOAD_FOLDER,
19
- folder_free_bytes: 41 * GB,
20
- agent_supported: true,
21
- ...over,
22
- };
23
- }
24
- let seq = 0;
25
- export function job(over = {}) {
26
- seq++;
27
- const now = Date.now();
28
- return {
29
- download_id: `00000000-0000-4000-8000-${String(seq).padStart(12, '0')}`,
30
- kind: 'Http',
31
- name: `file-${seq}.bin`,
32
- dest_root: DOWNLOAD_FOLDER,
33
- sub_dir: null,
34
- state: 'Downloading',
35
- done_bytes: 0,
36
- total_bytes: null,
37
- down_bps: 0,
38
- up_bps: 0,
39
- peers: 0,
40
- in_order: true,
41
- started_at: now - 60_000,
42
- finished_at: null,
43
- reason: null,
44
- requested_by: null,
45
- files: [],
46
- up_total: 0,
47
- pause_reason: null,
48
- seeding_since: null,
49
- info_hash: null,
50
- retry_reason: null,
51
- attempt: 0,
52
- next_attempt_at: null,
53
- restarted_from_zero: false,
54
- ...over,
55
- };
56
- }
57
- function media(name, size, done, ready = done) {
58
- return [{ index: 0, name, size, done_bytes: done, media: true, ready_from_start: ready }];
59
- }
60
- /** Rows from mockup A: three active, one finished, one retrying. */
61
- export function drawerJobs() {
62
- const now = Date.now();
63
- return [
64
- job({
65
- name: 'big_buck_bunny_1080p.mp4',
66
- dest_root: MEDIA_FOLDER,
67
- state: 'Downloading',
68
- done_bytes: 356 * MB,
69
- total_bytes: 920 * MB,
70
- down_bps: 11.8 * MB,
71
- files: media('big_buck_bunny_1080p.mp4', 920 * MB, 356 * MB),
72
- }),
73
- job({
74
- kind: 'Torrent',
75
- name: 'ubuntu-26.04-desktop-amd64.iso',
76
- dest_root: MEDIA_FOLDER,
77
- state: 'Downloading',
78
- in_order: false,
79
- done_bytes: 4.31 * GB,
80
- total_bytes: 6.05 * GB,
81
- down_bps: 2.4 * MB,
82
- up_bps: 0.6 * MB,
83
- peers: 42,
84
- }),
85
- job({ kind: 'Torrent', name: 'magnet:?xt=urn:btih:9f2c…a41e', dest_root: MEDIA_FOLDER, state: 'FetchingMetadata', peers: 3, started_at: now - 41_000 }),
86
- job({
87
- kind: 'Torrent',
88
- name: 'debian-13.2.0-amd64-netinst',
89
- dest_root: MEDIA_FOLDER,
90
- state: 'Completed',
91
- done_bytes: 784 * MB,
92
- total_bytes: 784 * MB,
93
- started_at: now - 600_000,
94
- finished_at: now - 348_000,
95
- files: [
96
- { index: 0, name: 'debian-13.2.0-amd64-netinst.iso', size: 783 * MB, done_bytes: 783 * MB, media: false, ready_from_start: 783 * MB },
97
- { index: 1, name: 'SHA256SUMS', size: 1200, done_bytes: 1200, media: false, ready_from_start: 1200 },
98
- { index: 2, name: 'SHA256SUMS.sign', size: 800, done_bytes: 800, media: false, ready_from_start: 800 },
99
- ],
100
- }),
101
- job({
102
- name: 'nightly-build.zip',
103
- dest_root: MEDIA_FOLDER,
104
- state: 'Retrying',
105
- done_bytes: 98 * MB,
106
- retry_reason: 'Server answered 403 Forbidden',
107
- attempt: 6,
108
- next_attempt_at: now + 8 * 60_000,
109
- }),
110
- ];
111
- }
112
- /** Mockup R: one row per state, top to bottom. */
113
- export function everyState() {
114
- const now = Date.now();
115
- return [
116
- job({ name: 'nightly-build-4412.zip', state: 'Queued' }),
117
- job({ name: 'bbb_sunflower_1080p_60fps_normal.mp4', state: 'Resolving' }),
118
- job({ kind: 'Torrent', name: 'magnet:?xt=urn:btih:9f2c…a41e', state: 'FetchingMetadata', peers: 3, started_at: now - 41_000 }),
119
- job({ name: 'big_buck_bunny_1080p.mp4', state: 'Downloading', done_bytes: 356 * MB, total_bytes: 920 * MB, down_bps: 11.8 * MB, files: media('big_buck_bunny_1080p.mp4', 920 * MB, 356 * MB) }),
120
- job({
121
- kind: 'Torrent',
122
- name: 'Sintel.2010.1080p.mkv',
123
- state: 'Downloading',
124
- in_order: true,
125
- done_bytes: 263 * MB,
126
- total_bytes: 1.12 * GB,
127
- down_bps: 3.9 * MB,
128
- peers: 28,
129
- files: media('Sintel.2010.1080p.mkv', 1.12 * GB, 263 * MB, 240 * MB),
130
- }),
131
- job({
132
- kind: 'Torrent',
133
- name: 'ubuntu-26.04-desktop-amd64.iso',
134
- state: 'Downloading',
135
- in_order: false,
136
- done_bytes: 4.31 * GB,
137
- total_bytes: 6.05 * GB,
138
- down_bps: 2.4 * MB,
139
- peers: 42,
140
- files: media('ubuntu-26.04-desktop-amd64.mp4', 6.05 * GB, 4.31 * GB, 10 * MB),
141
- }),
142
- job({ name: 'dataset-2026.tar.zst', state: 'Stopped', done_bytes: 38 * MB, total_bytes: 100 * MB }),
143
- job({ name: 'nightly-build.zip', state: 'Retrying', done_bytes: 98 * MB, retry_reason: 'Server answered 403 Forbidden', attempt: 6, next_attempt_at: now + 8 * 60_000 }),
144
- job({ kind: 'Torrent', name: 'debian-13.2.0-amd64-netinst', state: 'Completed', done_bytes: 784 * MB, total_bytes: 784 * MB, started_at: now - 600_000, finished_at: now - 348_000 }),
145
- job({ name: 'old-iso.img', state: 'Cancelled', reason: 'Stopped with “also delete the part”' }),
146
- ];
147
- }
148
- /** Mockup R2: torrent, schedule, disk and login states. */
149
- export function moreStates() {
150
- const now = Date.now();
151
- return [
152
- job({ kind: 'Torrent', name: 'ubuntu-26.04-desktop-amd64.iso', state: 'Verifying', done_bytes: 4.31 * GB, total_bytes: 10.5 * GB }),
153
- job({ kind: 'Torrent', name: 'debian-13.2.0-amd64-netinst', state: 'Seeding', done_bytes: 784 * MB, total_bytes: 784 * MB, up_bps: 1.4 * MB, up_total: 2.31 * GB, peers: 11, seeding_since: now - 3 * 86_400_000 }),
154
- job({ name: 'dataset-2026.tar.zst', state: 'Scheduled', next_attempt_at: now + (3 * 60 + 12) * 60_000 }),
155
- job({ name: 'big_buck_bunny_1080p.mp4', state: 'Paused', pause_reason: 'DiskFull', reason: '1.6 GB free on /srv/downloads, the floor is 2 GB', done_bytes: 600 * MB, total_bytes: 920 * MB }),
156
- job({ name: 'firmware-v2.4.bin', state: 'Downloading', done_bytes: 57 * MB, total_bytes: 100 * MB, down_bps: 6.2 * MB }),
157
- ];
158
- }
159
- /** H4: the Finished tab. */
160
- export function finishedJobs() {
161
- const today = new Date();
162
- const at = (h, m) => new Date(today.getFullYear(), today.getMonth(), today.getDate(), h, m).getTime();
163
- return [
164
- job({ kind: 'Torrent', name: 'Sintel.2010.1080p.mkv', state: 'Completed', done_bytes: 1.12 * GB, total_bytes: 1.12 * GB, started_at: at(21, 40), finished_at: at(21, 52), files: media('Sintel.2010.1080p.mkv', 1.12 * GB, 1.12 * GB) }),
165
- job({ name: 'home-assistant-backup.tar', state: 'Completed', done_bytes: 812 * MB, total_bytes: 812 * MB, started_at: at(21, 42), finished_at: at(21, 44) }),
166
- job({ kind: 'Torrent', name: 'debian-13.2.0-amd64-netinst', dest_root: MEDIA_FOLDER, state: 'Completed', done_bytes: 784 * MB, total_bytes: 784 * MB, started_at: at(21, 37), finished_at: at(21, 41) }),
167
- job({ name: 'raspios-bookworm.img.xz', state: 'Completed', done_bytes: 436 * MB, total_bytes: 436 * MB, started_at: at(20, 1), finished_at: at(20, 3) }),
168
- job({ kind: 'Torrent', name: 'archlinux-2026.11.01-x86_64.iso', state: 'Seeding', done_bytes: 1.2 * GB, total_bytes: 1.2 * GB, up_bps: 0.3 * MB, up_total: 400 * MB, peers: 4, seeding_since: Date.now() - 86_400_000 }),
169
- ];
170
- }
171
- /** H: the standalone desktop. */
172
- export function standaloneJobs() {
173
- return [
174
- job({
175
- kind: 'Torrent',
176
- name: 'Sintel.2010.1080p.mkv',
177
- state: 'Downloading',
178
- in_order: true,
179
- done_bytes: 263 * MB,
180
- total_bytes: 1.12 * GB,
181
- down_bps: 3.9 * MB,
182
- up_bps: 0.4 * MB,
183
- peers: 28,
184
- files: media('Sintel.2010.1080p.mkv', 1.12 * GB, 263 * MB, 240 * MB),
185
- }),
186
- job({ name: 'raspios-bookworm-arm64-lite.img.xz', state: 'Downloading', done_bytes: 279 * MB, total_bytes: 436 * MB, down_bps: 9.1 * MB }),
187
- ...finishedJobs().slice(0, 3),
188
- job({ name: 'nightly-build.zip', state: 'Retrying', done_bytes: 98 * MB, retry_reason: 'Server answered 403 Forbidden', attempt: 6, next_attempt_at: Date.now() + 8 * 60_000 }),
189
- ];
190
- }
191
- export function entry(name, kind, over = {}, path = MEDIA_FOLDER) {
192
- return {
193
- name,
194
- path,
195
- kind,
196
- items: [],
197
- attributes: { size: 0, uid: 1000, user: 'pi', gid: 1000, group: 'pi', permissions: kind === 'Folder' ? 0o40755 : 0o100644, atime: 0, modified: 1_763_000_000, ...over },
198
- };
199
- }
200
- /** The folders mockups A and C browse. */
201
- export function mediaListing() {
202
- return {
203
- [MEDIA_FOLDER]: [
204
- entry('Shows', 'Folder'),
205
- entry('debian-13.2.0-amd64-netinst', 'Folder'),
206
- entry('home-assistant-backup.tar', 'File', { size: 812 * MB }),
207
- entry('big_buck_bunny_1080p.mp4.phirepass-part', 'File', { size: 356 * MB }),
208
- entry('holiday-2025.mp4', 'File', { size: 1.4 * GB }),
209
- entry('podcast-ep-212.mp3', 'File', { size: 52 * MB }),
210
- entry('notes.txt', 'File', { size: 2000 }),
211
- ],
212
- [`${MEDIA_FOLDER}/Shows`]: [entry('Season 1', 'Folder', {}, `${MEDIA_FOLDER}/Shows`), entry('trailer.webm', 'File', { size: 48 * MB }, `${MEDIA_FOLDER}/Shows`)],
213
- [DOWNLOAD_FOLDER]: [
214
- entry('archlinux-2026.11.01-x86_64.iso.torrent', 'File', { size: 48_000 }, DOWNLOAD_FOLDER),
215
- entry('movies', 'Folder', {}, DOWNLOAD_FOLDER),
216
- entry('report.pdf', 'File', { size: 300_000 }, DOWNLOAD_FOLDER),
217
- ],
218
- };
219
- }
@@ -1,410 +0,0 @@
1
- const ErrorKind = { Generic: 0, Authentication: 10, RequiresUsername: 100, RequiresPassword: 110, RequiresUsernamePassword: 120 };
2
- let uuidSeq = 0x1000;
3
- function uuid() {
4
- uuidSeq++;
5
- return `11111111-2222-4333-8444-${uuidSeq.toString(16).padStart(12, '0')}`;
6
- }
7
- function nameFromSource(source, savedAs) {
8
- if ('Http' in source) {
9
- const fromUrl = (() => {
10
- try {
11
- const last = new URL(source.Http.url).pathname.split('/').filter(Boolean).pop();
12
- return last ? decodeURIComponent(last) : 'download';
13
- }
14
- catch {
15
- return 'download';
16
- }
17
- })();
18
- return { name: savedAs ?? source.Http.save_as ?? fromUrl, kind: 'Http' };
19
- }
20
- if ('Magnet' in source) {
21
- const dn = new URLSearchParams(source.Magnet.uri.split('?')[1] ?? '').get('dn');
22
- return { name: dn ?? source.Magnet.uri.slice(0, 40), kind: 'Torrent' };
23
- }
24
- if ('TorrentFile' in source) {
25
- return { name: source.TorrentFile.name.replace(/\.torrent$/i, ''), kind: 'Torrent' };
26
- }
27
- if ('TorrentPath' in source) {
28
- return { name: source.TorrentPath.path.split('/').pop().replace(/\.torrent$/i, ''), kind: 'Torrent' };
29
- }
30
- return { name: source.Node.name, kind: 'Node' };
31
- }
32
- export class MockChannel {
33
- endpoint;
34
- nodeId;
35
- options;
36
- calls = [];
37
- jobs = new Map();
38
- cb = {};
39
- connected = false;
40
- timers = new Set();
41
- ticker;
42
- constructor(endpoint, nodeId, options = {}) {
43
- this.endpoint = endpoint;
44
- this.nodeId = nodeId;
45
- this.options = options;
46
- for (const j of options.downloads?.jobs ?? []) {
47
- this.jobs.set(j.download_id, { ...j });
48
- }
49
- if (!options.downloads) {
50
- // A channel from before Remote Fetch: the methods simply are not there.
51
- for (const m of Object.getOwnPropertyNames(MockChannel.prototype)) {
52
- if (m.startsWith('send_download_')) {
53
- this[m] = undefined;
54
- }
55
- }
56
- }
57
- if (!options.sftp?.streams) {
58
- this.send_sftp_open_stream = undefined;
59
- }
60
- }
61
- /** A `channelFactory` that builds one of these and keeps the last one built in `ref.current`. */
62
- static factory(options = {}, ref = {}) {
63
- return (endpoint, nodeId) => (ref.current = new MockChannel(endpoint, nodeId, options));
64
- }
65
- record(method, args) {
66
- this.calls.push({ method, args });
67
- }
68
- callsTo(method) {
69
- return this.calls.filter(c => c.method === method).map(c => c.args);
70
- }
71
- later(fn, extra = 0) {
72
- const t = setTimeout(() => {
73
- this.timers.delete(t);
74
- fn();
75
- }, (this.options.latency ?? 0) + extra);
76
- this.timers.add(t);
77
- }
78
- /** Pushes a frame at the widget, as the server would. */
79
- emit(web) {
80
- this.cb.protocol?.({ version: 1, encoding: 'JSON', data: { web } });
81
- }
82
- /** Sends the node's latest word on a download to the widget. */
83
- progress(job) {
84
- if (job.state === 'Deleted') {
85
- this.jobs.delete(job.download_id);
86
- }
87
- else {
88
- this.jobs.set(job.download_id, job);
89
- }
90
- this.emit({ type: 'DownloadProgress', node_id: this.nodeId, job });
91
- }
92
- update(downloadId, patch) {
93
- const current = this.jobs.get(downloadId);
94
- if (current) {
95
- this.progress({ ...current, ...patch });
96
- }
97
- }
98
- /** Drops the connection, as a network failure would. */
99
- drop() {
100
- this.connected = false;
101
- this.cb.close?.();
102
- }
103
- // ---- connection ----
104
- connect() {
105
- this.record('connect', []);
106
- const mode = this.options.connect ?? 'ok';
107
- if (mode === 'never') {
108
- return;
109
- }
110
- this.later(() => {
111
- if (mode === 'refuse') {
112
- this.cb.error?.(new Error('Connection refused'));
113
- this.cb.close?.();
114
- return;
115
- }
116
- this.connected = true;
117
- this.cb.open?.();
118
- });
119
- }
120
- disconnect() {
121
- this.record('disconnect', []);
122
- this.connected = false;
123
- this.timers.forEach(t => clearTimeout(t));
124
- this.timers.clear();
125
- if (this.ticker) {
126
- clearInterval(this.ticker);
127
- this.ticker = undefined;
128
- }
129
- }
130
- is_connected() {
131
- return this.connected;
132
- }
133
- authenticate(token, nodeId) {
134
- this.record('authenticate', [token, nodeId]);
135
- const mode = this.options.auth ?? 'ok';
136
- if (mode === 'never') {
137
- return;
138
- }
139
- this.later(() => {
140
- if (mode === 'fail') {
141
- this.emit({ type: 'Error', kind: ErrorKind.Authentication, message: 'Your session has expired.' });
142
- return;
143
- }
144
- this.emit({ type: 'AuthSuccess', cid: 'cid-mock', version: this.options.version ?? '0.1.356' });
145
- });
146
- }
147
- start_heartbeat(ms) {
148
- this.record('start_heartbeat', [ms]);
149
- }
150
- stop_heartbeat() {
151
- this.record('stop_heartbeat', []);
152
- }
153
- on_connection_open(cb) {
154
- this.cb.open = cb;
155
- }
156
- on_connection_close(cb) {
157
- this.cb.close = cb;
158
- }
159
- on_connection_error(cb) {
160
- this.cb.error = cb;
161
- }
162
- on_connection_message(cb) {
163
- this.cb.message = cb;
164
- }
165
- on_protocol_message(cb) {
166
- this.cb.protocol = cb;
167
- }
168
- // ---- SSH ----
169
- open_ssh_tunnel(nodeId, serviceId, username, password) {
170
- this.record('open_ssh_tunnel', [nodeId, serviceId, username, password]);
171
- const req = this.options.ssh?.requires;
172
- if (req === 'username' && !username) {
173
- this.later(() => this.emit({ type: 'Error', kind: ErrorKind.RequiresUsername, message: 'Username required' }));
174
- return;
175
- }
176
- if (req === 'password' && !password) {
177
- this.later(() => this.emit({ type: 'Error', kind: ErrorKind.RequiresPassword, message: 'Password required' }));
178
- return;
179
- }
180
- this.later(() => {
181
- this.emit({ type: 'TunnelOpened', sid: 1 });
182
- const banner = this.options.ssh?.banner;
183
- if (banner) {
184
- this.emit({ type: 'TunnelData', node_id: nodeId, sid: 1, data: new TextEncoder().encode(banner) });
185
- }
186
- });
187
- }
188
- send_ssh_tunnel_data(nodeId, sid, data) {
189
- this.record('send_ssh_tunnel_data', [nodeId, sid, data]);
190
- if (!this.options.ssh?.echo) {
191
- return;
192
- }
193
- const out = data.replace(/\r/g, '\r\n');
194
- this.later(() => this.emit({ type: 'TunnelData', node_id: nodeId, sid, data: new TextEncoder().encode(out) }));
195
- }
196
- send_ssh_terminal_resize(...args) {
197
- this.record('send_ssh_terminal_resize', args);
198
- }
199
- // ---- RDP ----
200
- open_rdp_tunnel(...args) {
201
- this.record('open_rdp_tunnel', args);
202
- const rdp = this.options.rdp ?? {};
203
- this.later(() => {
204
- if (rdp.error) {
205
- this.emit({ type: 'Error', kind: ErrorKind.Generic, message: rdp.error });
206
- }
207
- else if (rdp.ticket) {
208
- this.emit({ type: 'RDPAuthorized', sid: 1, ticket: 'mock-ticket' });
209
- }
210
- });
211
- }
212
- // ---- SFTP ----
213
- open_sftp_tunnel(nodeId, serviceId, username, password) {
214
- this.record('open_sftp_tunnel', [nodeId, serviceId, username, password]);
215
- const req = this.options.sftp?.requires;
216
- const kind = req === 'both' && !username && !password
217
- ? ErrorKind.RequiresUsernamePassword
218
- : req === 'username' && !username
219
- ? ErrorKind.RequiresUsername
220
- : req === 'password' && !password
221
- ? ErrorKind.RequiresPassword
222
- : null;
223
- this.later(() => {
224
- if (kind !== null) {
225
- this.emit({ type: 'Error', kind, message: 'Credentials required' });
226
- return;
227
- }
228
- this.emit({ type: 'TunnelOpened', sid: 1 });
229
- });
230
- }
231
- send_sftp_list_data(nodeId, sid, path, msgId) {
232
- this.record('send_sftp_list_data', [nodeId, sid, path, msgId]);
233
- const sftp = this.options.sftp;
234
- const resolved = path === '.' ? (sftp?.cwd ?? '/') : path;
235
- const items = sftp?.tree[resolved] ?? [];
236
- this.later(() => this.emit({
237
- type: 'SFTPListItems',
238
- path: resolved,
239
- sid,
240
- msg_id: msgId,
241
- dir: { name: resolved.split('/').pop() || '/', path: resolved, kind: 'Folder', items, attributes: { size: 0, uid: 0, user: 'pi', gid: 0, group: 'pi', permissions: 0o40755, atime: 0, modified: 0 } },
242
- }));
243
- }
244
- opDone(sid, msgId) {
245
- this.later(() => this.emit({ type: 'SFTPOpResult', sid, msg_id: msgId, result: { result: 'Done' } }));
246
- }
247
- send_sftp_mkdir(_n, sid, path, msgId) {
248
- this.record('send_sftp_mkdir', [_n, sid, path, msgId]);
249
- this.opDone(sid, msgId);
250
- }
251
- send_sftp_rename(_n, sid, from, to, msgId) {
252
- this.record('send_sftp_rename', [_n, sid, from, to, msgId]);
253
- this.opDone(sid, msgId);
254
- }
255
- send_sftp_chmod(_n, sid, path, mode, msgId) {
256
- this.record('send_sftp_chmod', [_n, sid, path, mode, msgId]);
257
- this.opDone(sid, msgId);
258
- }
259
- send_sftp_remove(_n, sid, path, recursive, msgId) {
260
- this.record('send_sftp_remove', [_n, sid, path, recursive, msgId]);
261
- this.opDone(sid, msgId);
262
- }
263
- send_sftp_read_file(_n, sid, path, msgId) {
264
- this.record('send_sftp_read_file', [_n, sid, path, msgId]);
265
- const contents = new TextEncoder().encode(`# ${path}\nlisten: 0.0.0.0:8080\nlog_level: info\n`);
266
- this.later(() => this.emit({ type: 'SFTPOpResult', sid, msg_id: msgId, result: { result: 'FileContents', path, contents } }));
267
- }
268
- send_sftp_write_file(_n, sid, path, _contents, msgId) {
269
- this.record('send_sftp_write_file', [_n, sid, path, msgId]);
270
- this.opDone(sid, msgId);
271
- }
272
- send_sftp_open_stream(_n, sid, path, msgId) {
273
- this.record('send_sftp_open_stream', [_n, sid, path, msgId]);
274
- this.later(() => this.emit({ type: 'SFTPOpResult', sid, msg_id: msgId, result: { result: 'Stream', token: 'tok-mock', size: 1000, mime: 'video/mp4' } }));
275
- }
276
- send_sftp_download_start(...args) {
277
- this.record('send_sftp_download_start', args);
278
- }
279
- send_sftp_download_ack(...args) {
280
- this.record('send_sftp_download_ack', args);
281
- }
282
- send_sftp_upload_start(...args) {
283
- this.record('send_sftp_upload_start', args);
284
- }
285
- send_sftp_upload_chunk(...args) {
286
- this.record('send_sftp_upload_chunk', args);
287
- }
288
- // ---- Remote Fetch ----
289
- send_download_subscribe(scope) {
290
- this.record('send_download_subscribe', [scope]);
291
- const d = this.options.downloads;
292
- if (!d) {
293
- return;
294
- }
295
- this.later(() => {
296
- if (d.caps) {
297
- this.emit({ type: 'DownloadCaps', node_id: this.nodeId, caps: d.caps });
298
- }
299
- this.jobs.forEach(j => this.emit({ type: 'DownloadProgress', node_id: this.nodeId, job: j }));
300
- });
301
- if (d.live && !this.ticker) {
302
- this.ticker = setInterval(() => this.tick(), 1000);
303
- }
304
- }
305
- tick() {
306
- this.jobs.forEach(j => {
307
- if (j.state !== 'Downloading' || !j.total_bytes || !j.down_bps) {
308
- return;
309
- }
310
- const done = Math.min(j.total_bytes, j.done_bytes + j.down_bps);
311
- const finished = done >= j.total_bytes;
312
- const files = j.files.map(f => ({ ...f, done_bytes: Math.min(f.size, f.done_bytes + j.down_bps), ready_from_start: j.in_order ? Math.min(f.size, f.ready_from_start + j.down_bps) : f.ready_from_start }));
313
- this.progress({
314
- ...j,
315
- done_bytes: done,
316
- files,
317
- state: finished ? (j.kind === 'Torrent' ? 'Seeding' : 'Completed') : 'Downloading',
318
- finished_at: finished ? Date.now() : null,
319
- seeding_since: finished && j.kind === 'Torrent' ? Date.now() : j.seeding_since,
320
- down_bps: finished ? 0 : j.down_bps,
321
- });
322
- });
323
- }
324
- send_download_where(nodeId, browsing, msgId) {
325
- this.record('send_download_where', [nodeId, browsing, msgId]);
326
- const d = this.options.downloads;
327
- const answer = d?.where?.(browsing) ?? { saves_to: browsing.path, mapped: true };
328
- this.later(() => this.emit({ type: 'DownloadWhereAnswer', msg_id: msgId, ...answer }));
329
- }
330
- send_download_folders(nodeId, subDir, msgId) {
331
- this.record('send_download_folders', [nodeId, subDir, msgId]);
332
- const folders = this.options.downloads?.folders?.[subDir ?? ''] ?? [];
333
- this.later(() => this.emit({ type: 'DownloadFoldersAnswer', msg_id: msgId, folders }));
334
- }
335
- send_download_start(nodeId, source, subDir, browsing, inOrder, options, msgId) {
336
- this.record('send_download_start', [nodeId, source, subDir, browsing, inOrder, options, msgId]);
337
- const d = this.options.downloads ?? {};
338
- const mode = d.onStart ?? 'progress';
339
- if (mode === 'hang') {
340
- return;
341
- }
342
- if (mode === 'error') {
343
- this.later(() => this.emit({ type: 'Error', kind: ErrorKind.Generic, message: d.refuseReason ?? 'You cannot download to this node.', msg_id: msgId }));
344
- return;
345
- }
346
- const id = uuid();
347
- const { name, kind } = nameFromSource(source, d.savedAs);
348
- const root = d.caps?.folder ?? '/srv/downloads';
349
- const dest = browsing ? (d.where?.(browsing) ?? { saves_to: browsing.path }).saves_to : root;
350
- this.later(() => {
351
- this.emit({ type: 'DownloadAccepted', msg_id: msgId, download_id: id });
352
- if (mode === 'silent') {
353
- return;
354
- }
355
- const base = {
356
- download_id: id,
357
- kind,
358
- name,
359
- dest_root: browsing ? dest : root,
360
- sub_dir: browsing ? null : subDir,
361
- state: mode === 'queued' ? 'Queued' : mode === 'refuse' ? 'Cancelled' : 'Downloading',
362
- done_bytes: 0,
363
- total_bytes: kind === 'Http' ? 120_000_000 : null,
364
- down_bps: mode === 'progress' ? 8_000_000 : 0,
365
- up_bps: 0,
366
- peers: 0,
367
- in_order: inOrder,
368
- started_at: Date.now(),
369
- finished_at: null,
370
- reason: mode === 'refuse' ? (d.refuseReason ?? 'This node does not download from private network addresses.') : null,
371
- requested_by: null,
372
- files: [],
373
- up_total: 0,
374
- pause_reason: null,
375
- seeding_since: null,
376
- info_hash: null,
377
- retry_reason: null,
378
- attempt: 0,
379
- next_attempt_at: null,
380
- restarted_from_zero: false,
381
- };
382
- this.progress(base);
383
- }, 5);
384
- }
385
- send_download_stop(downloadId, discard, msgId) {
386
- this.record('send_download_stop', [downloadId, discard, msgId]);
387
- this.later(() => this.update(downloadId, discard ? { state: 'Cancelled', done_bytes: 0, down_bps: 0, reason: null } : { state: 'Stopped', down_bps: 0, up_bps: 0 }));
388
- }
389
- send_download_resume(downloadId, ignoreWindow, msgId) {
390
- this.record('send_download_resume', [downloadId, ignoreWindow, msgId]);
391
- this.later(() => {
392
- const j = this.jobs.get(downloadId);
393
- this.update(downloadId, { state: j?.state === 'Completed' ? 'Completed' : j?.done_bytes && j.total_bytes && j.done_bytes >= j.total_bytes ? 'Seeding' : 'Downloading', attempt: 0, retry_reason: null, next_attempt_at: null });
394
- });
395
- }
396
- send_download_set_order(downloadId, inOrder, msgId) {
397
- this.record('send_download_set_order', [downloadId, inOrder, msgId]);
398
- this.later(() => this.update(downloadId, { in_order: inOrder }));
399
- }
400
- send_download_delete(downloadId, deleteFile, msgId) {
401
- this.record('send_download_delete', [downloadId, deleteFile, msgId]);
402
- this.later(() => {
403
- const j = this.jobs.get(downloadId);
404
- if (j) {
405
- this.progress({ ...j, state: 'Deleted' });
406
- }
407
- // The node takes a moment to delete files: long enough to see “Deleting…”.
408
- }, 300);
409
- }
410
- }