@cardonjs/backups 0.0.0-stage → 0.3.2
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.
- package/FORMAT.md +147 -0
- package/LICENSE +21 -0
- package/README.md +1237 -2
- package/dist/copy.d.ts +175 -0
- package/dist/copy.d.ts.map +1 -0
- package/dist/copy.js +863 -0
- package/dist/copy.js.map +1 -0
- package/dist/encryption.d.ts +203 -0
- package/dist/encryption.d.ts.map +1 -0
- package/dist/encryption.js +609 -0
- package/dist/encryption.js.map +1 -0
- package/dist/export.d.ts +201 -0
- package/dist/export.d.ts.map +1 -0
- package/dist/export.js +447 -0
- package/dist/export.js.map +1 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +14 -0
- package/dist/index.js.map +1 -0
- package/dist/keys.d.ts +28 -0
- package/dist/keys.d.ts.map +1 -0
- package/dist/keys.js +97 -0
- package/dist/keys.js.map +1 -0
- package/dist/metadata.d.ts +49 -0
- package/dist/metadata.d.ts.map +1 -0
- package/dist/metadata.js +51 -0
- package/dist/metadata.js.map +1 -0
- package/dist/restore.d.ts +97 -0
- package/dist/restore.d.ts.map +1 -0
- package/dist/restore.js +276 -0
- package/dist/restore.js.map +1 -0
- package/dist/retention.d.ts +153 -0
- package/dist/retention.d.ts.map +1 -0
- package/dist/retention.js +295 -0
- package/dist/retention.js.map +1 -0
- package/dist/run.d.ts +94 -0
- package/dist/run.d.ts.map +1 -0
- package/dist/run.js +129 -0
- package/dist/run.js.map +1 -0
- package/dist/sniff.d.ts +15 -0
- package/dist/sniff.d.ts.map +1 -0
- package/dist/sniff.js +47 -0
- package/dist/sniff.js.map +1 -0
- package/dist/workflow.d.ts +102 -0
- package/dist/workflow.d.ts.map +1 -0
- package/dist/workflow.js +298 -0
- package/dist/workflow.js.map +1 -0
- package/llms.txt +249 -0
- package/package.json +76 -3
package/dist/restore.js
ADDED
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@cardonjs/backups/restore`: the restore check, for Node (tests and scripts). It splits a dump
|
|
3
|
+
* with `statementsOf` from `@cardonjs/testing/miniflare` (an optional peer, which loads
|
|
4
|
+
* Miniflare), loads it into a new, empty D1 database and compares what was loaded with what the
|
|
5
|
+
* dump holds. Never point it at a database with data: it refuses one.
|
|
6
|
+
*/
|
|
7
|
+
import { statementsOf } from "@cardonjs/testing/miniflare";
|
|
8
|
+
import { BackupDecryptError, decryptBackup, ENCRYPTION_SCHEME, startsEncrypted, } from "./encryption.js";
|
|
9
|
+
/**
|
|
10
|
+
* The SQLite result code that ends a D1 error message: `...: SQLITE_ERROR` or
|
|
11
|
+
* `...: SQLITE_CONSTRAINT (extended: SQLITE_CONSTRAINT_UNIQUE)` (the extended one is taken).
|
|
12
|
+
*/
|
|
13
|
+
const SQLITE_CODE = /\b(SQLITE_[A-Z_]{1,40})\)?$/;
|
|
14
|
+
/**
|
|
15
|
+
* The SQLite result code of a D1 error, or `"unknown error"`. Nothing else of the message,
|
|
16
|
+
* which can quote the statement.
|
|
17
|
+
*/
|
|
18
|
+
function sqliteCode(error) {
|
|
19
|
+
const message = error instanceof Error && typeof error.message === "string" ? error.message : "";
|
|
20
|
+
return SQLITE_CODE.exec(message)?.[1] ?? "unknown error";
|
|
21
|
+
}
|
|
22
|
+
/** `BEGIN TRANSACTION` / `COMMIT`: D1 refuses them, and a batch is a transaction already. */
|
|
23
|
+
const TRANSACTION = /^(?:BEGIN(?:\s+(?:DEFERRED|IMMEDIATE|EXCLUSIVE))?(?:\s+TRANSACTION)?|COMMIT|END(?:\s+TRANSACTION)?)\s*;?$/i;
|
|
24
|
+
/** How the `sqlite3` CLI writes virtual tables: D1 refuses it. */
|
|
25
|
+
const WRITABLE_SCHEMA = /\bPRAGMA\s+writable_schema\b/i;
|
|
26
|
+
/** The table an `INSERT` writes to, quoted or not. */
|
|
27
|
+
const INSERT_INTO = /^INSERT\s+(?:OR\s+\w+\s+)?INTO\s+("(?:[^"]|"")+"|`(?:[^`]|``)+`|\[[^\]]+\]|[A-Za-z_][A-Za-z0-9_$]*)/i;
|
|
28
|
+
/** The table a `CREATE TABLE` creates. */
|
|
29
|
+
const CREATE_TABLE = /^CREATE\s+TABLE\s+(?:IF\s+NOT\s+EXISTS\s+)?("(?:[^"]|"")+"|`(?:[^`]|``)+`|\[[^\]]+\]|[A-Za-z_][A-Za-z0-9_$]*)/i;
|
|
30
|
+
const STATEMENTS_PER_BATCH = 500;
|
|
31
|
+
function unquote(name) {
|
|
32
|
+
const first = name[0];
|
|
33
|
+
if (first === '"')
|
|
34
|
+
return name.slice(1, -1).replaceAll('""', '"');
|
|
35
|
+
if (first === "`")
|
|
36
|
+
return name.slice(1, -1).replaceAll("``", "`");
|
|
37
|
+
if (first === "[")
|
|
38
|
+
return name.slice(1, -1);
|
|
39
|
+
return name;
|
|
40
|
+
}
|
|
41
|
+
const quote = (name) => `"${name.replaceAll('"', '""')}"`;
|
|
42
|
+
/** Tables SQLite and D1 manage themselves: never compared. */
|
|
43
|
+
function isInternal(name) {
|
|
44
|
+
const lower = name.toLowerCase();
|
|
45
|
+
return lower.startsWith("sqlite_") || lower.startsWith("_cf_");
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* A dump's statements, ready for `db.batch`: split as Wrangler splits SQL files (comments
|
|
49
|
+
* dropped, strings and trigger bodies kept whole), without `BEGIN TRANSACTION` / `COMMIT`.
|
|
50
|
+
* Throws for a dump made with `format: "sqlite3"` (it writes `sqlite_schema` directly, which D1
|
|
51
|
+
* refuses).
|
|
52
|
+
*/
|
|
53
|
+
export function restoreStatements(sql) {
|
|
54
|
+
if (typeof sql !== "string")
|
|
55
|
+
throw new TypeError("restoreStatements: sql must be a string.");
|
|
56
|
+
const statements = statementsOf(sql).filter((statement) => !TRANSACTION.test(statement));
|
|
57
|
+
if (statements.some((statement) => WRITABLE_SCHEMA.test(statement))) {
|
|
58
|
+
throw new Error("restoreStatements: this dump was made for the sqlite3 CLI (virtual tables) and cannot be loaded into D1.");
|
|
59
|
+
}
|
|
60
|
+
return statements;
|
|
61
|
+
}
|
|
62
|
+
async function userTables(db) {
|
|
63
|
+
const { results } = await db
|
|
64
|
+
.prepare("SELECT name FROM sqlite_schema WHERE type = 'table' ORDER BY name")
|
|
65
|
+
.all();
|
|
66
|
+
return results.map((row) => row.name).filter((name) => !isInternal(name));
|
|
67
|
+
}
|
|
68
|
+
async function sha256Of(bytes) {
|
|
69
|
+
const digest = new Uint8Array(await crypto.subtle.digest("SHA-256", bytes));
|
|
70
|
+
return Array.from(digest, (b) => b.toString(16).padStart(2, "0")).join("");
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* `bytes` decrypted (through `decryptBackup`, chunk by chunk, each released after its tag
|
|
74
|
+
* verified). Throws the stream's `BackupDecryptError` as it is.
|
|
75
|
+
*/
|
|
76
|
+
async function decrypted(bytes, encryption, expected) {
|
|
77
|
+
const transform = decryptBackup(encryption, { expected });
|
|
78
|
+
const source = new ReadableStream({
|
|
79
|
+
start(controller) {
|
|
80
|
+
// Fed in pieces, as a file would be: nothing depends on the input's chunking.
|
|
81
|
+
for (let offset = 0; offset < bytes.byteLength; offset += 1024 * 1024) {
|
|
82
|
+
controller.enqueue(bytes.subarray(offset, offset + 1024 * 1024));
|
|
83
|
+
}
|
|
84
|
+
controller.close();
|
|
85
|
+
},
|
|
86
|
+
});
|
|
87
|
+
const reader = source.pipeThrough(transform).getReader();
|
|
88
|
+
const parts = [];
|
|
89
|
+
let size = 0;
|
|
90
|
+
for (;;) {
|
|
91
|
+
const { done, value } = await reader.read();
|
|
92
|
+
if (done)
|
|
93
|
+
break;
|
|
94
|
+
parts.push(value);
|
|
95
|
+
size += value.byteLength;
|
|
96
|
+
}
|
|
97
|
+
const out = new Uint8Array(size);
|
|
98
|
+
let offset = 0;
|
|
99
|
+
for (const part of parts) {
|
|
100
|
+
out.set(part, offset);
|
|
101
|
+
offset += part.byteLength;
|
|
102
|
+
}
|
|
103
|
+
// Resolved once the first chunk authenticated: settled by now.
|
|
104
|
+
return { plain: out, header: await transform.authenticated };
|
|
105
|
+
}
|
|
106
|
+
/** The dump as bytes and as text: a string is encoded as UTF-8; bytes are decoded leniently. */
|
|
107
|
+
function dumpOf(dump) {
|
|
108
|
+
if (typeof dump === "string")
|
|
109
|
+
return { bytes: new TextEncoder().encode(dump), text: dump };
|
|
110
|
+
const bytes = dump instanceof Uint8Array
|
|
111
|
+
? dump
|
|
112
|
+
: dump instanceof ArrayBuffer
|
|
113
|
+
? new Uint8Array(dump)
|
|
114
|
+
: undefined;
|
|
115
|
+
if (bytes === undefined)
|
|
116
|
+
return undefined;
|
|
117
|
+
// Invalid UTF-8 (possible in a TEXT value) becomes U+FFFD for loading; the size and digest are
|
|
118
|
+
// checked on the bytes themselves.
|
|
119
|
+
return {
|
|
120
|
+
bytes,
|
|
121
|
+
get text() {
|
|
122
|
+
return new TextDecoder().decode(bytes);
|
|
123
|
+
},
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Loads `dump` (a dump from this package) into `db`, a **new, empty, local** D1 database (from
|
|
128
|
+
* `startBindings` in a test, say), then compares each table's rows with the dump's `INSERT`s,
|
|
129
|
+
* the dump's size with `bytes` and its SHA-256 with `sha256` when given.
|
|
130
|
+
*
|
|
131
|
+
* Pass the file's **bytes** (`await readFile(path)`, a `Uint8Array` or an `ArrayBuffer`): the
|
|
132
|
+
* size and digest are computed on them. A string is encoded as UTF-8 first, so it cannot prove
|
|
133
|
+
* byte equality when the dump holds invalid UTF-8 (a TEXT value can): reading the file as text
|
|
134
|
+
* replaces those bytes, and the digest then differs from the recorded one.
|
|
135
|
+
*
|
|
136
|
+
* What it proves: the dump loads, every table it creates has the rows it inserts, and (with
|
|
137
|
+
* `bytes` and `sha256`) the file is the one that was copied. What it does not: that Cloudflare's
|
|
138
|
+
* dump was complete. A dump cut at a statement boundary loads cleanly and its counts agree with
|
|
139
|
+
* themselves; only a digest recorded at copy time shows it changed since, and nothing here can
|
|
140
|
+
* show it was short when Cloudflare produced it.
|
|
141
|
+
*
|
|
142
|
+
* An encrypted backup: pass its bytes and `encryption` (the keys). It is decrypted first, failing
|
|
143
|
+
* closed with a `BackupDecryptError` (a fixed message and a `reason`: `authentication_failed`,
|
|
144
|
+
* `unknown_key`, `truncated`, `not_encrypted`…) before the database is touched; `bytes` and
|
|
145
|
+
* `sha256` are compared with the encrypted bytes given, which is what the copy recorded. Without
|
|
146
|
+
* `encryption`, an encrypted backup is refused (`reason: "encrypted"`). The decrypted dump is
|
|
147
|
+
* held in memory, as a plain one is. Pass `key` (the R2 key you downloaded) and `databaseId`:
|
|
148
|
+
* the authenticated header must name them (`wrong_backup` otherwise), and `report.identity`
|
|
149
|
+
* says what the header names and which of the two were checked.
|
|
150
|
+
*
|
|
151
|
+
* Throws when `db` already has tables (an empty production database would pass this guard: use
|
|
152
|
+
* a local one only), or when the dump does not load. That error names the statements and SQLite's
|
|
153
|
+
* result code only, and has no `cause`: the database's own error may quote SQL from the dump (row
|
|
154
|
+
* data).
|
|
155
|
+
*/
|
|
156
|
+
export async function checkRestore(db, dump, options = {}) {
|
|
157
|
+
if (typeof db !== "object" || db === null || typeof db.batch !== "function") {
|
|
158
|
+
throw new TypeError("checkRestore: db must be a D1 binding.");
|
|
159
|
+
}
|
|
160
|
+
if (options.bytes !== undefined && !(Number.isSafeInteger(options.bytes) && options.bytes >= 0)) {
|
|
161
|
+
throw new TypeError("checkRestore: bytes must be a whole number of bytes.");
|
|
162
|
+
}
|
|
163
|
+
if (options.sha256 !== undefined && !/^[0-9a-f]{64}$/.test(options.sha256)) {
|
|
164
|
+
throw new TypeError("checkRestore: sha256 must be 64 lowercase hex characters.");
|
|
165
|
+
}
|
|
166
|
+
const given = dumpOf(dump);
|
|
167
|
+
if (given === undefined) {
|
|
168
|
+
throw new TypeError("checkRestore: the dump must be a string, a Uint8Array or an ArrayBuffer.");
|
|
169
|
+
}
|
|
170
|
+
for (const field of ["key", "databaseId"]) {
|
|
171
|
+
if (options[field] !== undefined && typeof options[field] !== "string") {
|
|
172
|
+
throw new TypeError(`checkRestore: ${field} must be a string.`);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
let sql;
|
|
176
|
+
let encryption;
|
|
177
|
+
let identity;
|
|
178
|
+
if ("encryption" in options) {
|
|
179
|
+
if (options.encryption === undefined || options.encryption === null) {
|
|
180
|
+
throw new TypeError("checkRestore: encryption is empty; pass the keys, or leave the property out for a plain dump.");
|
|
181
|
+
}
|
|
182
|
+
if (typeof dump === "string") {
|
|
183
|
+
throw new TypeError("checkRestore: an encrypted backup must be given as bytes (readFile).");
|
|
184
|
+
}
|
|
185
|
+
const expected = {
|
|
186
|
+
...(options.key === undefined ? {} : { key: options.key }),
|
|
187
|
+
...(options.databaseId === undefined ? {} : { databaseId: options.databaseId }),
|
|
188
|
+
};
|
|
189
|
+
// Fails closed, before the database is touched; the keys are checked by decryptBackup.
|
|
190
|
+
const { plain, header } = await decrypted(given.bytes, options.encryption, expected);
|
|
191
|
+
sql = new TextDecoder().decode(plain);
|
|
192
|
+
encryption = { scheme: ENCRYPTION_SCHEME, keyId: header.keyId };
|
|
193
|
+
identity = {
|
|
194
|
+
databaseId: header.databaseId,
|
|
195
|
+
key: header.key,
|
|
196
|
+
checked: { databaseId: options.databaseId !== undefined, key: options.key !== undefined },
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
else {
|
|
200
|
+
if (options.key !== undefined || options.databaseId !== undefined) {
|
|
201
|
+
throw new TypeError("checkRestore: key and databaseId can be checked on an encrypted backup only (pass encryption).");
|
|
202
|
+
}
|
|
203
|
+
if (startsEncrypted(given.bytes))
|
|
204
|
+
throw new BackupDecryptError("encrypted");
|
|
205
|
+
sql = given.text;
|
|
206
|
+
}
|
|
207
|
+
if ((await userTables(db)).length > 0) {
|
|
208
|
+
throw new Error("checkRestore: the database has tables; restore checks need a new, empty one.");
|
|
209
|
+
}
|
|
210
|
+
const statements = restoreStatements(sql);
|
|
211
|
+
const expected = new Map();
|
|
212
|
+
for (const statement of statements) {
|
|
213
|
+
const created = CREATE_TABLE.exec(statement);
|
|
214
|
+
if (created !== null) {
|
|
215
|
+
const name = unquote(created[1]);
|
|
216
|
+
if (!expected.has(name))
|
|
217
|
+
expected.set(name, 0);
|
|
218
|
+
continue;
|
|
219
|
+
}
|
|
220
|
+
const inserted = INSERT_INTO.exec(statement);
|
|
221
|
+
if (inserted !== null) {
|
|
222
|
+
const name = unquote(inserted[1]);
|
|
223
|
+
expected.set(name, (expected.get(name) ?? 0) + 1);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
for (let i = 0; i < statements.length; i += STATEMENTS_PER_BATCH) {
|
|
227
|
+
const batch = statements.slice(i, i + STATEMENTS_PER_BATCH);
|
|
228
|
+
try {
|
|
229
|
+
await db.batch(batch.map((statement) => db.prepare(statement)));
|
|
230
|
+
}
|
|
231
|
+
catch (error) {
|
|
232
|
+
// No cause: D1's message can quote the failing statement (row data), and a logged error
|
|
233
|
+
// has its cause chain written. Only SQLite's result code is kept.
|
|
234
|
+
throw new Error(`checkRestore: the dump did not load (statements ${i + 1} to ${i + batch.length}; ${sqliteCode(error)}).`);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
const present = new Set(await userTables(db));
|
|
238
|
+
const problems = [];
|
|
239
|
+
const tables = [];
|
|
240
|
+
for (const [name, rows] of [...expected].sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))) {
|
|
241
|
+
if (isInternal(name))
|
|
242
|
+
continue;
|
|
243
|
+
if (!present.has(name)) {
|
|
244
|
+
tables.push({ name, expectedRows: rows, loadedRows: null });
|
|
245
|
+
problems.push(`table ${name} is missing`);
|
|
246
|
+
continue;
|
|
247
|
+
}
|
|
248
|
+
const row = await db.prepare(`SELECT count(*) AS n FROM ${quote(name)}`).first();
|
|
249
|
+
const loaded = Number(row?.n ?? 0);
|
|
250
|
+
tables.push({ name, expectedRows: rows, loadedRows: loaded });
|
|
251
|
+
if (loaded !== rows)
|
|
252
|
+
problems.push(`table ${name}: ${loaded} rows loaded, ${rows} in the dump`);
|
|
253
|
+
}
|
|
254
|
+
for (const name of present) {
|
|
255
|
+
if (!expected.has(name))
|
|
256
|
+
problems.push(`table ${name} is not in the dump`);
|
|
257
|
+
}
|
|
258
|
+
const encoded = given.bytes;
|
|
259
|
+
if (options.bytes !== undefined && encoded.byteLength !== options.bytes) {
|
|
260
|
+
problems.push(`the dump has ${encoded.byteLength} bytes, ${options.bytes} were recorded`);
|
|
261
|
+
}
|
|
262
|
+
const sha256 = await sha256Of(encoded);
|
|
263
|
+
if (options.sha256 !== undefined && sha256 !== options.sha256) {
|
|
264
|
+
problems.push("the dump's SHA-256 differs from the one recorded");
|
|
265
|
+
}
|
|
266
|
+
return {
|
|
267
|
+
ok: problems.length === 0,
|
|
268
|
+
sha256,
|
|
269
|
+
statements: statements.length,
|
|
270
|
+
tables,
|
|
271
|
+
problems,
|
|
272
|
+
...(encryption === undefined ? {} : { encryption }),
|
|
273
|
+
...(identity === undefined ? {} : { identity }),
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
//# sourceMappingURL=restore.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"restore.js","sourceRoot":"","sources":["../src/restore.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,OAAO,EAAE,YAAY,EAAE,MAAM,6BAA6B,CAAC;AAC3D,OAAO,EAEL,kBAAkB,EAIlB,aAAa,EACb,iBAAiB,EAEjB,eAAe,GAChB,MAAM,iBAAiB,CAAC;AAEzB;;;GAGG;AACH,MAAM,WAAW,GAAG,6BAA6B,CAAC;AAElD;;;GAGG;AACH,SAAS,UAAU,CAAC,KAAc;IAChC,MAAM,OAAO,GAAG,KAAK,YAAY,KAAK,IAAI,OAAO,KAAK,CAAC,OAAO,KAAK,QAAQ,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,C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AClG,CAAC;IACD,KAAK,MAAM,IAAI,IAAI,OAAO,EAAE,CAAC;QAC3B,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,IAAI,CAAC;YAAE,QAAQ,CAAC,IAAI,CAAC,SAAS,IAAI,qBAAqB,CAAC,CAAC;IAC7E,CAAC;IACD,MAAM,OAAO,GAAG,KAAK,CAAC,KAAK,CAAC;IAC5B,IAAI,OAAO,CAAC,KAAK,KAAK,SAAS,IAAI,OAAO,CAAC,UAAU,KAAK,OAAO,CAAC,KAAK,EAAE,CAAC;QACxE,QAAQ,CAAC,IAAI,CAAC,gBAAgB,OAAO,CAAC,UAAU,WAAW,OAAO,CAAC,KAAK,gBAAgB,CAAC,CAAC;IAC5F,CAAC;IACD,MAAM,MAAM,GAAG,MAAM,QAAQ,CAAC,OAAO,CAAC,CAAC;IACvC,IAAI,OAAO,CAAC,MAAM,KAAK,SAAS,IAAI,MAAM,KAAK,OAAO,CAAC,MAAM,EAAE,CAAC;QAC9D,QAAQ,CAAC,IAAI,CAAC,kDAAkD,CAAC,CAAC;IACpE,CAAC;IACD,OAAO;QACL,EAAE,EAAE,QAAQ,CAAC,MAAM,KAAK,CAAC;QACzB,MAAM;QACN,UAAU,EAAE,UAAU,CAAC,MAAM;QAC7B,MAAM;QACN,QAAQ;QACR,GAAG,CAAC,UAAU,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,UAAU,EAAE,CAAC;QACnD,GAAG,CAAC,QAAQ,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,QAAQ,EAAE,CAAC;KAChD,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Retention: which backups to keep (pure, `planRetention`) and the I/O around it (`listBackups`,
|
|
3
|
+
* `pruneBackups`). Periods are calendar days, ISO weeks (Monday to Sunday, ISO week-year) and
|
|
4
|
+
* calendar months, all in UTC, read from the key. The counts are of periods **that have a
|
|
5
|
+
* backup**, newest first, as in restic and Borg: `daily: 7` keeps the newest backup of each of the
|
|
6
|
+
* 7 most recent days with a backup, whatever today is. So backups that stopped a month ago are
|
|
7
|
+
* not pruned away by the passing of time.
|
|
8
|
+
*/
|
|
9
|
+
export interface Retention {
|
|
10
|
+
/** Newest backup of each of the N most recent days that have one. */
|
|
11
|
+
readonly daily?: number;
|
|
12
|
+
/** Newest backup of each of the N most recent ISO weeks (UTC) that have one. */
|
|
13
|
+
readonly weekly?: number;
|
|
14
|
+
/** Newest backup of each of the N most recent months (UTC) that have one. */
|
|
15
|
+
readonly monthly?: number;
|
|
16
|
+
}
|
|
17
|
+
export interface BackupEntry {
|
|
18
|
+
readonly key: string;
|
|
19
|
+
/** The moment the key stands for. */
|
|
20
|
+
readonly at: Date;
|
|
21
|
+
}
|
|
22
|
+
export interface RetentionPlan<E extends BackupEntry = BackupEntry> {
|
|
23
|
+
/** Newest first. */
|
|
24
|
+
readonly keep: readonly E[];
|
|
25
|
+
/** Newest first. */
|
|
26
|
+
readonly remove: readonly E[];
|
|
27
|
+
}
|
|
28
|
+
/** Throws a `TypeError` unless `keep` has whole counts from 0 to 10 000, at least one above 0. */
|
|
29
|
+
export declare function assertValidRetention(keep: unknown, where?: string): asserts keep is Retention;
|
|
30
|
+
/** `2026-11-20` (UTC). */
|
|
31
|
+
export declare function dayOf(at: Date): string;
|
|
32
|
+
/** `2026-11` (UTC). */
|
|
33
|
+
export declare function monthOf(at: Date): string;
|
|
34
|
+
/**
|
|
35
|
+
* The ISO 8601 week of `at` in UTC, `2026-W47`: weeks start on Monday, and week 1 is the one with
|
|
36
|
+
* the year's first Thursday, so `2027-01-01` (a Friday) is in `2026-W53`.
|
|
37
|
+
*/
|
|
38
|
+
export declare function isoWeekOf(at: Date): string;
|
|
39
|
+
/**
|
|
40
|
+
* Which entries to keep and which to remove. Pure: no clock, no I/O. Newest first by `at` (then
|
|
41
|
+
* key); for each rule, walks the entries and keeps the first (newest) of each new period until
|
|
42
|
+
* the count is reached. The newest entry is always kept, whatever the policy. Throws a
|
|
43
|
+
* `TypeError` for an invalid policy.
|
|
44
|
+
*/
|
|
45
|
+
export declare function planRetention<E extends BackupEntry>(entries: readonly E[], keep: Retention): RetentionPlan<E>;
|
|
46
|
+
export interface ListedBackup extends BackupEntry {
|
|
47
|
+
readonly size: number;
|
|
48
|
+
/** The object's custom metadata (bookmark, database id, created at, bytes, format). */
|
|
49
|
+
readonly metadata: Readonly<Record<string, string>>;
|
|
50
|
+
/** The key of its digest sidecar (`<key>.sha256`) when one is listed, else `null`. */
|
|
51
|
+
readonly digestKey: string | null;
|
|
52
|
+
/** The sidecar's custom metadata when one is listed, else `null`. */
|
|
53
|
+
readonly digestMetadata: Readonly<Record<string, string>> | null;
|
|
54
|
+
}
|
|
55
|
+
/** A digest sidecar whose dump is not under the prefix any more. */
|
|
56
|
+
export interface OrphanDigest {
|
|
57
|
+
readonly key: string;
|
|
58
|
+
/** The moment its dump's key stands for. */
|
|
59
|
+
readonly at: Date;
|
|
60
|
+
readonly metadata: Readonly<Record<string, string>>;
|
|
61
|
+
}
|
|
62
|
+
export interface ListBackupsResult {
|
|
63
|
+
/** Backups this package wrote under the prefix, newest first. */
|
|
64
|
+
readonly backups: readonly ListedBackup[];
|
|
65
|
+
/** Digest sidecars this package wrote whose dump is gone. */
|
|
66
|
+
readonly orphanDigests: readonly OrphanDigest[];
|
|
67
|
+
/** Objects under the prefix that are not this package's (never touched). */
|
|
68
|
+
readonly ignored: number;
|
|
69
|
+
/**
|
|
70
|
+
* The encryption key ids the listed backups name (their `key-id` metadata), sorted, without
|
|
71
|
+
* repeats; plain backups name none. A hint, not proof: metadata can be stripped or forged by
|
|
72
|
+
* whoever writes the bucket, and objects without the marker, under another prefix or of
|
|
73
|
+
* another database are not counted. Before destroying a key, confirm from the objects'
|
|
74
|
+
* headers (`claimedKeyId`, a range read of their first bytes).
|
|
75
|
+
*/
|
|
76
|
+
readonly keyIds: readonly string[];
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Every backup under `prefix`: objects whose key matches the package's pattern exactly **and**
|
|
80
|
+
* that carry its metadata marker, with their digest sidecars (`<key>.sha256`, also marked),
|
|
81
|
+
* which are never backups themselves. Lists every page; throws when the prefix holds more than
|
|
82
|
+
* 100 000 objects rather than act on a partial list.
|
|
83
|
+
*/
|
|
84
|
+
export declare function listBackups(bucket: R2Bucket, prefix: string): Promise<ListBackupsResult>;
|
|
85
|
+
export interface PruneOptions {
|
|
86
|
+
/** The prefix the backups were written under, e.g. `"d1/main"`: one prefix per database. */
|
|
87
|
+
readonly prefix: string;
|
|
88
|
+
/**
|
|
89
|
+
* The id of the database whose backups these are (`exporter.databaseId`). Pruning is refused
|
|
90
|
+
* while the prefix holds backups of any other database.
|
|
91
|
+
*/
|
|
92
|
+
readonly databaseId: string;
|
|
93
|
+
readonly keep: Retention;
|
|
94
|
+
/** `true`: decide and report, delete nothing. */
|
|
95
|
+
readonly dryRun?: boolean;
|
|
96
|
+
/**
|
|
97
|
+
* The clock future-dated backups are judged by. Default `() => new Date()`. A backup dated
|
|
98
|
+
* more than a day after it makes pruning refuse.
|
|
99
|
+
*/
|
|
100
|
+
readonly now?: () => Date;
|
|
101
|
+
}
|
|
102
|
+
export interface PruneResult {
|
|
103
|
+
readonly dryRun: boolean;
|
|
104
|
+
/** Keys kept, newest first. */
|
|
105
|
+
readonly kept: readonly string[];
|
|
106
|
+
/** Keys deleted (or, in a dry run, that would be), newest first. */
|
|
107
|
+
readonly deleted: readonly string[];
|
|
108
|
+
/**
|
|
109
|
+
* Digest sidecars deleted (or that would be): those of the deleted backups, and sidecars of
|
|
110
|
+
* this database whose dump is gone, unless the dump's key is dated less than a day before
|
|
111
|
+
* `now()` (or later): a run writing that dump may still be in progress.
|
|
112
|
+
*/
|
|
113
|
+
readonly deletedDigests: readonly string[];
|
|
114
|
+
/** Objects under the prefix that are not this package's backups: never touched. */
|
|
115
|
+
readonly ignored: number;
|
|
116
|
+
/**
|
|
117
|
+
* The encryption key ids the **kept** backups name, sorted (see `ListBackupsResult.keyIds`):
|
|
118
|
+
* the keys still needed to restore this prefix's backups.
|
|
119
|
+
*/
|
|
120
|
+
readonly keyIds: readonly string[];
|
|
121
|
+
}
|
|
122
|
+
/** Why `pruneBackups` refused to delete anything. */
|
|
123
|
+
export type PruneRefusal =
|
|
124
|
+
/** The prefix holds backups (or sidecars) of another database: one prefix per database. */
|
|
125
|
+
"other_database"
|
|
126
|
+
/** A backup is dated more than a day in the future: it would take a slot from real ones. */
|
|
127
|
+
| "future_backups";
|
|
128
|
+
/**
|
|
129
|
+
* `pruneBackups` refused: nothing was deleted. The message names the reason and how many
|
|
130
|
+
* objects; never a key, an id or a value.
|
|
131
|
+
*/
|
|
132
|
+
export declare class PruneRefusedError extends Error {
|
|
133
|
+
readonly reason: PruneRefusal;
|
|
134
|
+
readonly count: number;
|
|
135
|
+
constructor(reason: PruneRefusal, count: number);
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Deletes the backups of `databaseId` under `prefix` that `keep` does not keep (see
|
|
139
|
+
* `planRetention`), with their digest sidecars, and orphan sidecars dated more than a day before
|
|
140
|
+
* `now()`. Never deletes the newest backup, objects whose key does not match the package's
|
|
141
|
+
* pattern exactly, or objects without its marker. Refuses (throws a `PruneRefusedError`,
|
|
142
|
+
* deleting nothing) while the prefix holds a backup or sidecar of another database (or one with
|
|
143
|
+
* no database id) or a backup dated more than a day after `now()`; throws a `TypeError` for invalid
|
|
144
|
+
* options and an `Error` for a plan that would delete every backup. Deletes in batches of 1000
|
|
145
|
+
* keys; a failed batch throws (keys of earlier batches stay deleted; a rerun continues).
|
|
146
|
+
*/
|
|
147
|
+
export declare function pruneBackups(bucket: R2Bucket, options: PruneOptions): Promise<PruneResult>;
|
|
148
|
+
/**
|
|
149
|
+
* The last guard before deleting: a plan that keeps nothing of a non-empty list is refused.
|
|
150
|
+
* `planRetention` always keeps the newest, so this only fires if that ever changes.
|
|
151
|
+
*/
|
|
152
|
+
export declare function assertKeepsSomething(total: number, kept: number): void;
|
|
153
|
+
//# sourceMappingURL=retention.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"retention.d.ts","sourceRoot":"","sources":["../src/retention.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAMH,MAAM,WAAW,SAAS;IACxB,qEAAqE;IACrE,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC;IACxB,gFAAgF;IAChF,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;IACzB,6EAA6E;IAC7E,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,CAAC;CAC3B;AAED,MAAM,WAAW,WAAW;IAC1B,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,qCAAqC;IACrC,QAAQ,CAAC,EAAE,EAAE,IAAI,CAAC;CACnB;AAED,MAAM,WAAW,aAAa,CAAC,CAAC,SAAS,WAAW,GAAG,WAAW;IAChE,oBAAoB;IACpB,QAAQ,CAAC,IAAI,EAAE,SAAS,CAAC,EAAE,CAAC;IAC5B,oBAAoB;IACpB,QAAQ,CAAC,MAAM,EAAE,SAAS,CAAC,EAAE,CAAC;CAC/B;AAKD,kGAAkG;AAClG,wBAAgB,oBAAoB,CAAC,IAAI,EAAE,OAAO,EAAE,KAAK,SAAY,GAAG,OAAO,CAAC,IAAI,IAAI,SAAS,CA6BhG;AAID,0BAA0B;AAC1B,wBAAgB,KAAK,CAAC,EAAE,EAAE,IAAI,GAAG,MAAM,CAEtC;AAED,uBAAuB;AACvB,wBAAgB,OAAO,CAAC,EAAE,EAAE,IAAI,GAAG,MAAM,CAExC;AAED;;;GAGG;AACH,wBAAgB,SAAS,CAAC,EAAE,EAAE,IAAI,GAAG,MAAM,CAS1C;AAQD;;;;;GAKG;AACH,wBAAgB,aAAa,CAAC,CAAC,SAAS,WAAW,EACjD,OAAO,EAAE,SAAS,CAAC,EAAE,EACrB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