Open-source gym & body-weight tracker, now in the App Store (iPhone + Apple Watch) and as an Android APK. Plan routines, log workouts, see which muscles are trained, import from Strong/Hevy/FitNotes. Self-host it or use it on the phone alone.
JavaScript apple-watchbodyweightdockerfitnessfitness-trackergymhealthiosmcpnodejspasskeysprogressive-web-applicationspwareactself-hostedself-hostingvitewebauthnweightliftingworkout-tracker
API: state, db.json and media writes survive a host reset, and a damaged upload is replaced, not vouched for
Duarte Santos0503834afa57
diff --git a/api/Dockerfile b/api/Dockerfileindex 93834a929..597c84fcc 100644--- a/api/Dockerfile+++ b/api/Dockerfile@@ -53,7 +53,7 @@ RUN npm ci --omit=dev --omit=optional && npm cache clean --force# Every module server.js imports from the api root has to be named here: the image carries nothing# else, and one that is missing only shows at boot, as ERR_MODULE_NOT_FOUND.-COPY server.js push-messages.js nudge.js nudge-copy.js verify-error.js password.js rate-limit.js passkeys-store.js device-link.js media.js queue.js sync-stamps.js ./+COPY server.js push-messages.js nudge.js nudge-copy.js verify-error.js password.js rate-limit.js passkeys-store.js device-link.js media.js queue.js sync-stamps.js durable.js ./COPY coach ./coachENV NODE_ENV=productionENV PORT=3000diff --git a/api/durable.js b/api/durable.jsnew file mode 100644index 000000000..ff22120ad--- /dev/null+++ b/api/durable.js@@ -0,0 +1,38 @@+// Writes that survive a power cut or a host reset, not only a killed process.+//+// Every file the API keeps is written to a temporary name and renamed over the real one, so a+// crash never leaves half a file. But without an fsync the rename and the bytes sit in the page+// cache: the 200 (and the revision it names) went out first, and a reset in the next seconds+// brought the previous file back — or, on filesystems without ext4's rename heuristic, an empty+// one. The device that had been told "saved" then took the older copy and deleted the workout+// locally too (QA 2026-10-06, on a host known to hard-reset). So the bytes are flushed before the+// rename, and the directory after it. A few milliseconds per write.+import fs from 'node:fs';+import path from 'node:path';++/** fsync of a directory, so a rename inside it is on disk. Best effort: some filesystems refuse. */+export function syncDir(dir) {+ let fd = null;+ try { fd = fs.openSync(dir, 'r'); fs.fsyncSync(fd); }+ catch { /* a filesystem that cannot fsync a directory */ }+ finally { if (fd != null) try { fs.closeSync(fd); } catch { /* closed */ } }+}++/** fsync of a file that is already written (an upload streamed to disk). */+export function syncFile(file) {+ const fd = fs.openSync(file, 'r+');+ try { fs.fsyncSync(fd); } finally { fs.closeSync(fd); }+}++/** `content` at `file`, durably: temporary file, fsync, rename, fsync of the directory. */+export function atomicWrite(file, content, mode) {+ const tmp = file + '.tmp';+ const fd = fs.openSync(tmp, 'w', mode);+ try {+ fs.writeFileSync(fd, content);+ fs.fsyncSync(fd);+ } finally { fs.closeSync(fd); }+ if (mode != null) { try { fs.chmodSync(tmp, mode); } catch { /* a host filesystem that refuses chmod */ } }+ fs.renameSync(tmp, file);+ syncDir(path.dirname(file));+}diff --git a/api/media.js b/api/media.jsindex 1233a296c..cb2674d94 100644--- a/api/media.js+++ b/api/media.js@@ -26,6 +26,7 @@import crypto from 'node:crypto';import fs from 'node:fs';import path from 'node:path';+import { syncDir, syncFile } from './durable.js';export const HASH_RE = /^[0-9a-f]{64}$/;const MB = 1024 * 1024; // every *_MB setting means MiB; the client uses the same unit@@ -441,6 +442,9 @@ export function createMediaStore({ dir, limits, now = Date.now, readState = () =if (!m || !d.isFile() || e.hashes.has(m[1])) continue;try {const size = fs.statSync(path.join(e.dir, d.name)).size;+ // An empty file is what a reset right after an upload can leave behind: not a file this+ // profile has. Left out, it is reported missing and the next upload replaces it.+ if (!size) continue;e.hashes.set(m[1], { ext: m[2], size });e.bytes += size;e.count++;@@ -449,6 +453,16 @@ export function createMediaStore({ dir, limits, now = Date.now, readState = () =users.set(id, e);return e;}+ // The cache entry of `hash` when its file is whole: on disk, not empty, the size the cache+ // knows and, given one, the size an upload of the same bytes has. null otherwise.+ function intact(e, hash, size) {+ const f = e.hashes.get(hash);+ if (!f) return null;+ let st;+ try { st = fs.statSync(path.join(e.dir, `${hash}.${f.ext}`)); } catch { return null; }+ if (!st.size || st.size !== f.size || (size != null && size !== f.size)) return null;+ return f;+ }function forget(e, hash) {const f = e.hashes.get(hash);if (!f) return;@@ -474,8 +488,10 @@ export function createMediaStore({ dir, limits, now = Date.now, readState = () =function writeMarks(e, marks) {const f = gcFile(e), tmp = f + '.tmp';try {- fs.writeFileSync(tmp, JSON.stringify(marks), { mode: 0o600 });+ const fd = fs.openSync(tmp, 'w', 0o600);+ try { fs.writeFileSync(fd, JSON.stringify(marks)); fs.fsyncSync(fd); } finally { fs.closeSync(fd); }fs.renameSync(tmp, f);+ syncDir(path.dirname(f));} catch (err) {try { fs.unlinkSync(tmp); } catch { /* never written */ }throw err;@@ -610,7 +626,11 @@ export function createMediaStore({ dir, limits, now = Date.now, readState = () =cap = maxCap;throw new MediaError(413, 'media-too-large', { maxMB: capMB(declared.kind) });}- const have = e.hashes.get(hash);+ // A file this profile already has is taken as it is, unless it is not whole: its size on+ // disk differs from what the cache knows or from what this upload declares (a reset right+ // after an upload, a damaged restore). Then these bytes, checked against the hash below,+ // replace it, instead of the damaged file being vouched for forever.+ const have = intact(e, hash, len);if (have) { drain(req, 2 * cap); touch(e, hash); return existed(hash, have); }// Reserved before a byte is read, so two uploads at once cannot both fit into the space@@ -653,10 +673,18 @@ export function createMediaStore({ dir, limits, now = Date.now, readState = () =// The same bytes from another device may have landed while these were streaming. The// rename and the cache update below are synchronous, so this check cannot be overtaken.- const now2 = e.hashes.get(hash);+ const now2 = intact(e, hash, got.size);if (now2) { touch(e, hash); return existed(hash, now2); }+ // The bytes are on disk before the name is, and the name before the answer (durable.js).+ syncFile(tmp);+ const damaged = e.hashes.get(hash);+ if (damaged) {+ if (damaged.ext !== sn.ext) { try { fs.unlinkSync(path.join(e.dir, `${hash}.${damaged.ext}`)); } catch { /* gone */ } }+ forget(e, hash);+ }fs.renameSync(tmp, path.join(e.dir, `${hash}.${sn.ext}`));tmp = null;+ syncDir(e.dir);e.hashes.set(hash, { ext: sn.ext, size: got.size });e.bytes += got.size;e.count++;@@ -695,13 +723,15 @@ export function createMediaStore({ dir, limits, now = Date.now, readState = () =let st;try { st = fs.statSync(p); } catch { forget(e, hash); return null; }if (!st.isFile()) return null;+ if (!st.size) { forget(e, hash); return null; } // not whole: missing, for a device to send againif (st.size !== f.size) { e.bytes += st.size - f.size; f.size = st.size; }return { path: p, ext: f.ext, mime: EXT_MIME[f.ext], size: st.size };},receive,missing(uid, hashes) {const e = entry(uid);- return { missing: [...new Set(hashes)].filter(h => !e.hashes.has(h)), usage: usageOf(e) };+ // A file that is not whole (intact) counts as missing: the device that has it sends it again.+ return { missing: [...new Set(hashes)].filter(h => !intact(e, h)), usage: usageOf(e) };},noteState,sweep,diff --git a/api/server.js b/api/server.jsindex f8669a8bd..6d7535dae 100644--- a/api/server.js+++ b/api/server.js@@ -33,6 +33,7 @@ import { createDeviceLink, findDeviceLink, burnDeviceLink, dropDeviceLinks } froimport { createMediaStore, mediaLimits, mediaConfig, MediaError, HASH_RE } from './media.js';import { effectiveRoutineId } from './queue.js';import { stampPut } from './sync-stamps.js';+import { atomicWrite as durableWrite } from './durable.js';import { nudgeFor, nudgeWindowOpen, toneOf } from './nudge.js';const PORT = +(process.env.PORT || 3000);@@ -123,11 +124,9 @@ const isAdmin = user => !!user && (user.admin === true || ADMIN_UIDS.includes(us// 0600: db.json holds passkey credential material. It used to be covered by a blanket 0700 on// the whole directory; now that the directory stays traversable, the file carries its own mode.function saveDb() { atomicWrite(dbFile, JSON.stringify(db, null, 2), 0o600); }-function atomicWrite(file, content, mode) {- const tmp = file + '.tmp';- fs.writeFileSync(tmp, content, mode ? { mode } : undefined);- fs.renameSync(tmp, file);-}+// Flushed before and after the rename (durable.js): a host reset must not bring back the file+// the client was told had been replaced.+function atomicWrite(file, content, mode) { durableWrite(file, content, mode); }// How many earlier write ids a document keeps (`_wids`, see PUT /api/data): a device that last// synced more writes ago than this merges on its next pull instead of taking the server's copy.const WID_KEEP = 50;diff --git a/api/test/durable.test.js b/api/test/durable.test.jsnew file mode 100644index 000000000..a81f870bb--- /dev/null+++ b/api/test/durable.test.js@@ -0,0 +1,33 @@+/* Every state, db.json and media write is flushed before its rename and the directory after it+ (durable.js). Without the fsync the 200 went out while the rename sat in the page cache, and a+ host reset brought the previous file back: the device that had been told "saved" then took the+ older copy and deleted the workout locally too. (QA 2026-10-06: strace of a PUT showed no fsync.) */+import { test } from 'node:test';+import assert from 'node:assert/strict';+import fs from 'node:fs';+import os from 'node:os';+import path from 'node:path';+import { atomicWrite } from '../durable.js';++test('atomicWrite: bytes flushed, then the rename, then the directory', t => {+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'durable-'));+ t.after(() => fs.rmSync(dir, { recursive: true, force: true }));+ const file = path.join(dir, 'state-u1.json');+ const calls = [];+ const fdPath = new Map();+ const open = fs.openSync, fsync = fs.fsyncSync, rename = fs.renameSync;+ t.mock.method(fs, 'openSync', (p, ...rest) => { const fd = open(p, ...rest); fdPath.set(fd, p); return fd; });+ t.mock.method(fs, 'fsyncSync', fd => { calls.push(['fsync', fdPath.get(fd)]); return fsync(fd); });+ t.mock.method(fs, 'renameSync', (a, b) => { calls.push(['rename', b]); return rename(a, b); });+ atomicWrite(file, '{"_rev":1}', 0o600);+ assert.deepEqual(calls, [['fsync', file + '.tmp'], ['rename', file], ['fsync', dir]]);+ assert.equal(fs.readFileSync(file, 'utf8'), '{"_rev":1}');+ assert.equal(fs.statSync(file).mode & 0o777, 0o600);+});++test('server.js and media.js write through it', () => {+ const server = fs.readFileSync(new URL('../server.js', import.meta.url), 'utf8');+ assert.match(server, /function atomicWrite\(file, content, mode\) \{ durableWrite\(file, content, mode\); \}/);+ const media = fs.readFileSync(new URL('../media.js', import.meta.url), 'utf8');+ assert.match(media, /syncFile\(tmp\);[\s\S]*fs\.renameSync\(tmp, path\.join\(e\.dir/);+});diff --git a/api/test/media-damaged.test.js b/api/test/media-damaged.test.jsnew file mode 100644index 000000000..5c7878f8f--- /dev/null+++ b/api/test/media-damaged.test.js@@ -0,0 +1,54 @@+/* A stored upload that is not whole — an empty or truncated file left by a host reset right after+ the upload (the blob is renamed to a new name, which ext4's rename heuristic does not cover), or+ by a damaged restore — used to be vouched for forever: /api/media/missing said "not missing",+ GET served 0 bytes, and a re-upload of the good bytes answered "existed" and kept the damage.+ Once the original phone was gone, so was the photo. (QA 2026-10-06.) */+import { test } from 'node:test';+import assert from 'node:assert/strict';+import fs from 'node:fs';+import os from 'node:os';+import path from 'node:path';+import { createMediaStore, mediaLimits } from '../media.js';+import * as M from './media-samples.mjs';++const quiet = { warn() {}, error() {}, log() {} };+function setup(t) {+ const root = fs.mkdtempSync(path.join(os.tmpdir(), 'media-dmg-'));+ t.after(() => fs.rmSync(root, { recursive: true, force: true }));+ const dir = path.join(root, 'uploads');+ const make = () => createMediaStore({ dir, limits: { ...mediaLimits({}), minFreeMB: 0 }, log: quiet });+ return { dir, make };+}++for (const damage of ['empty', 'truncated']) {+ test(`a ${damage} stored file is missing, not served, and replaced by the next upload`, async t => {+ const h = setup(t);+ const bytes = M.png(4000), hash = M.sha(bytes);+ let store = h.make();+ assert.equal((await store.receive('u1', hash, M.fakeReq(bytes, { declare: 'image/png' }))).status, 201);+ const file = path.join(h.dir, 'u1', `${hash}.png`);+ fs.truncateSync(file, damage === 'empty' ? 0 : 1000);+ store = h.make(); // the server starts again after the reset+ // an empty file is missing; a truncated one is only found out when its bytes come again+ if (damage === 'empty') {+ assert.deepEqual(store.missing('u1', [hash]).missing, [hash]);+ assert.equal(store.file('u1', hash), null);+ }+ const r = await store.receive('u1', hash, M.fakeReq(bytes, { declare: 'image/png' }));+ assert.equal(r.status, 201);+ assert.equal(fs.statSync(file).size, 4000);+ assert.deepEqual(store.missing('u1', [hash]).missing, []);+ assert.equal(store.usage('u1').bytes, 4000);+ assert.equal(store.usage('u1').count, 1);+ });+}++test('a whole stored file is still taken as it is', async t => {+ const h = setup(t);+ const bytes = M.png(4000), hash = M.sha(bytes);+ const store = h.make();+ assert.equal((await store.receive('u1', hash, M.fakeReq(bytes, { declare: 'image/png' }))).status, 201);+ const again = await store.receive('u1', hash, M.fakeReq(bytes, { declare: 'image/png' }));+ assert.equal(again.status, 200);+ assert.equal(again.body.existed, true);+});