aboutsummaryrefslogtreecommitdiffstats
path: root/packages/meshbay-hub/src/meshbay_hub/static/sw.js
diff options
context:
space:
mode:
authorChristophe Besson <cbesson@gmail.com>2026-08-15 17:23:10 +0200
committerChristophe Besson <cbesson@gmail.com>2026-08-15 17:23:10 +0200
commitdd3927a661273734493f65a593755b95aecf5f09 (patch)
treeb40b9a0939e79c65990f4664211025b032c9bafa /packages/meshbay-hub/src/meshbay_hub/static/sw.js
parent4066c754a613deb965472853fe69727d68be593e (diff)
downloadmeshbay-dd3927a661273734493f65a593755b95aecf5f09.tar.gz
feat(groups): remove a member, and keep gigabytes out of the tab
**Removing a member.** The owner can do it from the Members tab, and it is two halves in the order that fails safe: the node stops serving the group key first (an operator-signed request, so a paired browser only), then the hub drops the membership row. The other order would leave someone able to reach a node that still serves them. It is a membership, not an account. The user row is never written: their other groups, their files and their pinned identity survive, because one group's owner must not be able to erase someone from the hub. It is also per group — a node hosting two loses them from one — and it does not take back the key they already unwrapped, which is what rotating the GEK is for. The confirmation and the panel both say so. **Downloads and streaming through the disk, in both browsers.** The audit this started as found two ways to put gigabytes in a tab. Firefox and Safari have no File System Access API, so every download there was collected in memory. A service worker fixes it: the page keeps the writable half of a transferred stream, the worker answers a made-up URL with the readable half and a Content-Disposition header, and the browser writes it to disk as it arrives, with real backpressure. The worker caches nothing and falls through on every request that is not one of these downloads. A zip announces no Content-Length, since the archive is larger than the files in it and a length we miss truncates the file. Video was worse and affected both browsers. The node pushed ffmpeg's whole output as fast as it was produced while the player consumed a segment at a time, so the queue held the film — and appending all of it hit the SourceBuffer's cap, where the handler logged the error and dropped the segment, leaving a hole in the middle of the film with nothing to show for it. Streaming is credit-based now, 24 segments of 256 KB in flight, verified against the live node: three credits, three segments, then silence until more are granted. The player evicts what is more than a minute behind the playhead and retries a refused segment rather than dropping it. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Diffstat (limited to 'packages/meshbay-hub/src/meshbay_hub/static/sw.js')
-rw-r--r--packages/meshbay-hub/src/meshbay_hub/static/sw.js60
1 files changed, 60 insertions, 0 deletions
diff --git a/packages/meshbay-hub/src/meshbay_hub/static/sw.js b/packages/meshbay-hub/src/meshbay_hub/static/sw.js
new file mode 100644
index 0000000..d0a8805
--- /dev/null
+++ b/packages/meshbay-hub/src/meshbay_hub/static/sw.js
@@ -0,0 +1,60 @@
+/**
+ * Service worker: the only way to stream a download to disk in Firefox.
+ *
+ * Chrome and Edge have the File System Access API — the page opens a file and
+ * writes to it. Firefox and Safari do not, and the alternative there was to
+ * collect the whole download in memory and hand the browser a blob, which is
+ * not an option for a file measured in gigabytes.
+ *
+ * So the page makes up a URL, tells this worker what stream answers it, and
+ * navigates a hidden iframe there. The worker replies with the stream and a
+ * Content-Disposition header, and the browser does what it does with any
+ * download: writes it to disk as it arrives, showing its own progress, with
+ * nothing buffered in the tab.
+ *
+ * It caches nothing and intercepts nothing else. Every request that is not one
+ * of these downloads falls through untouched.
+ */
+
+const PREFIX = '/_mbdl/';
+const pending = new Map();
+
+self.addEventListener('install', () => self.skipWaiting());
+self.addEventListener('activate', (event) => event.waitUntil(self.clients.claim()));
+
+self.addEventListener('message', (event) => {
+ const data = event.data || {};
+ if (data.type !== 'mbdl' || !data.id || !data.readable) return;
+ pending.set(data.id, {
+ readable: data.readable,
+ filename: data.filename || 'download',
+ size: Number(data.size) || 0,
+ });
+ // A tab that is closed before it navigates would leave a stream here for the
+ // life of the worker.
+ setTimeout(() => pending.delete(data.id), 60000);
+});
+
+self.addEventListener('fetch', (event) => {
+ const url = new URL(event.request.url);
+ if (url.origin !== self.location.origin || !url.pathname.startsWith(PREFIX)) {
+ return; // not ours — the network handles it
+ }
+
+ const entry = pending.get(url.pathname.slice(PREFIX.length));
+ if (!entry) return;
+ pending.delete(url.pathname.slice(PREFIX.length));
+
+ const headers = {
+ 'Content-Type': 'application/octet-stream',
+ // filename* so a name with accents or spaces survives the trip.
+ 'Content-Disposition':
+ `attachment; filename*=UTF-8''${encodeURIComponent(entry.filename)}`,
+ 'Cache-Control': 'no-store',
+ };
+ // Only when it is known. A zip is assembled as it goes and announcing a
+ // length we then miss would truncate the file.
+ if (entry.size > 0) headers['Content-Length'] = String(entry.size);
+
+ event.respondWith(new Response(entry.readable, { headers }));
+});