Open-source document signature platform. Not affiliated with DocuSign.
TypeScript
Backup: record which version of each file was copied; stop the export when the upload fails; Scorecard read scopes
Claude7c81bdbb2b17
diff --git a/.github/workflows/scorecard.yml b/.github/workflows/scorecard.ymlindex 8ffff28..323c9c7 100644--- a/.github/workflows/scorecard.yml+++ b/.github/workflows/scorecard.yml@@ -22,8 +22,12 @@ jobs:if: github.repository == 'NYLLON-SOFTWARE/SignuDoc'runs-on: ubuntu-latestpermissions:+ actions: read+ checks: readcontents: readid-token: write+ issues: read+ pull-requests: readsecurity-events: writesteps:- uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6.1.0diff --git a/src/lib/backup.dbtest.ts b/src/lib/backup.dbtest.tsindex 50050d1..bfbbbd3 100644--- a/src/lib/backup.dbtest.ts+++ b/src/lib/backup.dbtest.ts@@ -408,3 +408,47 @@ test("copying files back skips the ones a stopped run already copied", async ()assert.deepEqual(result, { copied: 1, failures: [], skipped: 1 });assert.equal(app.objects.get("b.pdf")?.body.toString(), "bbb");});++test("a file written again at the same size is copied again", async () => {+ let now = new Date("2026-11-01T08:00:00Z");+ const clock = () => now;+ const source = memoryStore(clock);+ const target = memoryStore(clock);++ await source.put("a.pdf", Buffer.from("unsigned"), {+ contentType: "application/pdf",+ });+ await runBackup({ database, keepDays: 30, now, source, target });++ // Rewritten in place, the same number of bytes, and the backup's copy is+ // newer than neither: only the recorded version tells them apart.+ now = new Date("2026-11-01T09:00:00Z");+ await source.put("a.pdf", Buffer.from("signed!!"), {+ contentType: "application/pdf",+ });+ target.objects.get("files/a.pdf")!.uploadedAt = new Date(+ "2026-11-01T10:00:00Z",+ );++ const second = await runBackup({+ database,+ keepDays: 30,+ now,+ source,+ target,+ });++ assert.equal(second.filesCopied, 1);+ assert.equal(target.objects.get("files/a.pdf")?.body.toString(), "signed!!");++ const third = await runBackup({+ database,+ keepDays: 30,+ now,+ source,+ target,+ });++ assert.equal(third.filesCopied, 0);+ assert.equal(third.filesUnchanged, 1);+});diff --git a/src/lib/backup.ts b/src/lib/backup.tsindex 210df6d..72b6820 100644--- a/src/lib/backup.ts+++ b/src/lib/backup.ts@@ -309,6 +309,13 @@ function databaseBackupPath(createdAt: Date): string {}type Manifest = {+ /**+ * For each copied file, the original's upload time as listed just before+ * it was read. A copy counts as current only while the original still+ * shows that time and size, so an original overwritten during or after a+ * copy is copied again on the next run.+ */+ copiedFrom: Record<string, string>;/** Backed-up files whose original was found missing, and since when. */missingSince: Record<string, string>;version: 1;@@ -321,13 +328,17 @@ async function readManifest(store: BackupStore): Promise<Manifest> {const found = await store.get(MANIFEST_PATH);if (!found) {- return { missingSince: {}, version: 1 };+ return { copiedFrom: {}, missingSince: {}, version: 1 };}const text = await new Response(found.stream).text();const parsed = JSON.parse(text) as Partial<Manifest>;- return { missingSince: parsed.missingSince ?? {}, version: 1 };+ return {+ copiedFrom: parsed.copiedFrom ?? {},+ missingSince: parsed.missingSince ?? {},+ version: 1,+ };}export type BackupSummary = {@@ -362,12 +373,28 @@ export async function runBackup({// pipeline() passes a failed export on to the upload, so it ends with// that error rather than waiting for data that will not come.+ // If the upload fails first, the export is stopped and waited for, so+ // its snapshot transaction and connection are let go before this returns.const compressed = createGzip();+ const exporting = pipeline(+ Readable.from(exportDatabaseLines(database, now)),+ compressed,+ );- await Promise.all([- pipeline(Readable.from(exportDatabaseLines(database, now)), compressed),- target.put(databaseBackup, compressed, { contentType: "application/gzip" }),- ]);+ try {+ await Promise.all([+ exporting,+ target.put(databaseBackup, compressed, {+ contentType: "application/gzip",+ }),+ ]);+ } catch (error) {+ compressed.destroy(+ error instanceof Error ? error : new Error(String(error)),+ );+ await exporting.catch(() => undefined);+ throw error;+ }const cutoff = now.getTime() - keepDays * DAY_MS;const databaseCopies = (await listAll(target, DATABASE_PREFIX)).sort((a, b) =>@@ -381,7 +408,18 @@ export async function runBackup({await target.delete(expiredCopies);// 2. The files.+ const manifest = await readManifest(target);const originals = await listAll(source);+ // Carried over for files this run does not reach before its deadline.+ const copiedFrom: Record<string, string> = {};++ for (const original of originals) {+ const earlier = manifest.copiedFrom[original.pathname];++ if (earlier) {+ copiedFrom[original.pathname] = earlier;+ }+ }const copies = new Map((await listAll(target, FILES_PREFIX)).map((blob) => [blob.pathname.slice(FILES_PREFIX.length),@@ -400,11 +438,12 @@ export async function runBackup({for (const original of originals) {const copy = copies.get(original.pathname);+ const version = original.uploadedAt.toISOString();if (copy &©.size === original.size &&- copy.uploadedAt.getTime() >= original.uploadedAt.getTime()+ manifest.copiedFrom[original.pathname] === version) {summary.filesUnchanged += 1;continue;@@ -415,6 +454,9 @@ export async function runBackup({break;}+ // Until this copy is done, the one in the backup is not this version.+ delete copiedFrom[original.pathname];+try {const found = await source.get(original.pathname);@@ -425,6 +467,7 @@ export async function runBackup({await target.put(`${FILES_PREFIX}${original.pathname}`, found.stream, {contentType: found.contentType,});+ copiedFrom[original.pathname] = version;summary.filesCopied += 1;} catch (error) {summary.fileFailures.push(original.pathname);@@ -436,7 +479,6 @@ export async function runBackup({}// 3. Copies whose original is gone, kept keepDays days after it went.- const manifest = await readManifest(target);const present = new Set(originals.map((blob) => blob.pathname));const missingSince: Record<string, string> = {};const toRemove: string[] = [];@@ -459,7 +501,7 @@ export async function runBackup({summary.filesRemoved = toRemove.length;await target.put(MANIFEST_PATH,- JSON.stringify({ missingSince, version: 1 } satisfies Manifest),+ JSON.stringify({ copiedFrom, missingSince, version: 1 } satisfies Manifest),{ contentType: "application/json" },);