phirepass-widgets 0.0.112 → 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.
Files changed (89) hide show
  1. package/README.md +33 -0
  2. package/dist/cjs/{index-B8w6l0VL.js → index-CurErEK7.js} +101 -30
  3. package/dist/cjs/loader.cjs.js +2 -2
  4. package/dist/cjs/phirepass-downloader_3.cjs.entry.js +3696 -0
  5. package/dist/cjs/phirepass-rdp.cjs.entry.js +18 -6
  6. package/dist/cjs/phirepass-terminal.cjs.entry.js +6 -5
  7. package/dist/cjs/phirepass-widgets.cjs.js +3 -3
  8. package/dist/cjs/{protocol-COvteaUJ.js → protocol-DCBMfc2u.js} +557 -360
  9. package/dist/collection/collection-manifest.json +3 -1
  10. package/dist/collection/common/channel.js +21 -1
  11. package/dist/collection/common/fetch.js +40 -0
  12. package/dist/collection/components/phirepass-downloader/form.js +69 -0
  13. package/dist/collection/components/phirepass-downloader/format.js +105 -0
  14. package/dist/collection/components/phirepass-downloader/jobs.js +370 -0
  15. package/dist/collection/components/phirepass-downloader/phirepass-downloader.css +1109 -0
  16. package/dist/collection/components/phirepass-downloader/phirepass-downloader.js +1455 -0
  17. package/dist/collection/components/phirepass-downloader/source.js +106 -0
  18. package/dist/collection/components/phirepass-downloader/views/dialogs.js +34 -0
  19. package/dist/collection/components/phirepass-downloader/views/fetch-form.js +65 -0
  20. package/dist/collection/components/phirepass-downloader/views/icons.js +33 -0
  21. package/dist/collection/components/phirepass-downloader/views/job-row.js +91 -0
  22. package/dist/collection/components/phirepass-downloader/views/notices.js +15 -0
  23. package/dist/collection/components/phirepass-downloader/views/states.js +19 -0
  24. package/dist/collection/components/phirepass-media-player/phirepass-media-player.css +216 -0
  25. package/dist/collection/components/phirepass-media-player/phirepass-media-player.js +473 -0
  26. package/dist/collection/components/phirepass-media-player/player.js +94 -0
  27. package/dist/collection/components/phirepass-rdp/phirepass-rdp.js +36 -13
  28. package/dist/collection/components/phirepass-sftp-client/phirepass-sftp-client.css +122 -0
  29. package/dist/collection/components/phirepass-sftp-client/phirepass-sftp-client.fetch.js +35 -0
  30. package/dist/collection/components/phirepass-sftp-client/phirepass-sftp-client.js +181 -31
  31. package/dist/collection/components/phirepass-terminal/phirepass-terminal.js +2 -2
  32. package/dist/collection/phirepass-channel_bg.wasm +0 -0
  33. package/dist/collection/testing/fixtures.js +219 -0
  34. package/dist/collection/testing/mock-channel.js +410 -0
  35. package/dist/components/index.js +1 -1
  36. package/dist/components/p-C4cmk3bS.js +1 -0
  37. package/dist/components/p-CsYQJXY2.js +1 -0
  38. package/dist/components/p-D6Geq8Rn.js +1 -0
  39. package/dist/components/phirepass-downloader.d.ts +11 -0
  40. package/dist/components/phirepass-downloader.js +1 -0
  41. package/dist/components/phirepass-media-player.d.ts +11 -0
  42. package/dist/components/phirepass-media-player.js +1 -0
  43. package/dist/components/phirepass-rdp.js +1 -1
  44. package/dist/components/phirepass-sftp-client.js +1 -1
  45. package/dist/components/phirepass-terminal.js +2 -2
  46. package/dist/esm/{index-DGq2b7rM.js → index-B9qUl6FW.js} +101 -30
  47. package/dist/esm/loader.js +3 -3
  48. package/dist/esm/phirepass-downloader_3.entry.js +3692 -0
  49. package/dist/esm/phirepass-rdp.entry.js +18 -6
  50. package/dist/esm/phirepass-terminal.entry.js +6 -5
  51. package/dist/esm/phirepass-widgets.js +4 -4
  52. package/dist/esm/{protocol-Dur4cBX7.js → protocol-CsYQJXY2.js} +556 -359
  53. package/dist/phirepass-widgets/p-1b84c526.entry.js +1 -0
  54. package/dist/phirepass-widgets/{p-1fef5b5f.entry.js → p-41dd1ac9.entry.js} +2 -2
  55. package/dist/phirepass-widgets/p-614ab3de.entry.js +1 -0
  56. package/dist/phirepass-widgets/p-B9qUl6FW.js +3 -0
  57. package/dist/phirepass-widgets/p-CsYQJXY2.js +1 -0
  58. package/dist/phirepass-widgets/phirepass-channel_bg.wasm +0 -0
  59. package/dist/phirepass-widgets/phirepass-widgets.esm.js +1 -1
  60. package/dist/types/common/channel.d.ts +30 -0
  61. package/dist/types/common/fetch.d.ts +161 -0
  62. package/dist/types/components/phirepass-downloader/form.d.ts +54 -0
  63. package/dist/types/components/phirepass-downloader/format.d.ts +17 -0
  64. package/dist/types/components/phirepass-downloader/jobs.d.ts +115 -0
  65. package/dist/types/components/phirepass-downloader/phirepass-downloader.d.ts +231 -0
  66. package/dist/types/components/phirepass-downloader/source.d.ts +61 -0
  67. package/dist/types/components/phirepass-downloader/views/dialogs.d.ts +52 -0
  68. package/dist/types/components/phirepass-downloader/views/fetch-form.d.ts +31 -0
  69. package/dist/types/components/phirepass-downloader/views/icons.d.ts +5 -0
  70. package/dist/types/components/phirepass-downloader/views/job-row.d.ts +21 -0
  71. package/dist/types/components/phirepass-downloader/views/notices.d.ts +18 -0
  72. package/dist/types/components/phirepass-downloader/views/states.d.ts +38 -0
  73. package/dist/types/components/phirepass-media-player/phirepass-media-player.d.ts +67 -0
  74. package/dist/types/components/phirepass-media-player/player.d.ts +43 -0
  75. package/dist/types/components/phirepass-rdp/phirepass-rdp.d.ts +10 -0
  76. package/dist/types/components/phirepass-sftp-client/phirepass-sftp-client.d.ts +30 -0
  77. package/dist/types/components/phirepass-sftp-client/phirepass-sftp-client.fetch.d.ts +40 -0
  78. package/dist/types/components.d.ts +426 -34
  79. package/dist/types/stencil-public-runtime.d.ts +1 -1
  80. package/dist/types/testing/fixtures.d.ts +25 -0
  81. package/dist/types/testing/mock-channel.d.ts +132 -0
  82. package/package.json +26 -12
  83. package/dist/cjs/phirepass-sftp-client.cjs.entry.js +0 -1469
  84. package/dist/components/p-Dur4cBX7.js +0 -1
  85. package/dist/esm/phirepass-sftp-client.entry.js +0 -1467
  86. package/dist/phirepass-widgets/p-8eb3ec24.entry.js +0 -1
  87. package/dist/phirepass-widgets/p-9a1f9b9f.entry.js +0 -1
  88. package/dist/phirepass-widgets/p-DGq2b7rM.js +0 -3
  89. package/dist/phirepass-widgets/p-Dur4cBX7.js +0 -1
@@ -0,0 +1,219 @@
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
+ }
@@ -0,0 +1,410 @@
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
+ }