@cardonjs/backups 0.0.0-stage → 0.3.3
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/copy.d.ts
ADDED
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Streams a ready export into R2. The copy is given the `ready` answer itself, never a bookmark
|
|
3
|
+
* to poll: Cloudflare hands out a completed export's signed URL once (observed on 2026-11-20; a
|
|
4
|
+
* second poll answers "Not currently exporting anything."), so the code that receives `ready` is
|
|
5
|
+
* the code that downloads. Two paths, both atomic (the object appears only when the whole dump is
|
|
6
|
+
* in and checked):
|
|
7
|
+
*
|
|
8
|
+
* - The answer states its length, the length is at most `singlePutLimit` (4 GiB by default) and
|
|
9
|
+
* the runtime has `FixedLengthStream` (Workers): one `put` of a stream of that length, through
|
|
10
|
+
* a check that counts the bytes and reads the start. R2 refuses a stream of unknown length, and
|
|
11
|
+
* a `put` whose stream errors stores nothing.
|
|
12
|
+
* - Otherwise (no `Content-Length`, a compressed answer, a larger dump, or Node): the dump is read
|
|
13
|
+
* in parts of `partSize` bytes. A dump that fits in one part is one `put` of those bytes; a
|
|
14
|
+
* larger one is a multipart upload, completed only after the last part and aborted on any
|
|
15
|
+
* failure.
|
|
16
|
+
*
|
|
17
|
+
* Where the runtime has `crypto.DigestStream` (Workers), the dump's SHA-256 is computed on the way
|
|
18
|
+
* and written after the dump, once it is checked, to a sidecar object `<key>.sha256` (a multipart
|
|
19
|
+
* upload's metadata is fixed when it starts, before the digest is known). The sidecar is also
|
|
20
|
+
* the proof that a copy finished: a retried copy reuses an object only with it, only when it
|
|
21
|
+
* names the very object at the key (its R2 `version`), and only after reading the object back
|
|
22
|
+
* and finding the sidecar's digest (so a reused result's digest is the stored bytes'). It detects
|
|
23
|
+
* corruption, not tampering (whoever can rewrite the dump can rewrite the sidecar). Node has no
|
|
24
|
+
* incremental digest of a web stream (`crypto.subtle.digest` takes the whole input), so there
|
|
25
|
+
* the copy writes no sidecar and reuse rests on the size check alone.
|
|
26
|
+
*
|
|
27
|
+
* One deadline (`timeoutMs`) bounds the whole copy: every R2 call (`head`, `get`, `put`,
|
|
28
|
+
* `createMultipartUpload`, `uploadPart`, `complete`, the sidecar), the download and every read of
|
|
29
|
+
* its body race it. Past it the copy answers `timeout` and gives an open multipart
|
|
30
|
+
* upload a few seconds to abort. It issues no delete after the deadline (one landing late could
|
|
31
|
+
* remove a retry's object). A write that was already in flight may still land: it holds this
|
|
32
|
+
* export's complete, checked dump (or its digest), without a sidecar until a retry writes one.
|
|
33
|
+
*
|
|
34
|
+
* With `encryption` (opt-in), the dump is encrypted on the way (see `encryption.ts` and
|
|
35
|
+
* `FORMAT.md`): the plaintext is counted and checked as SQL, then encrypted, and what R2 stores,
|
|
36
|
+
* what the digest covers and what `size` reports are the encrypted bytes. The encrypted length
|
|
37
|
+
* is a function of the plaintext length, so a dump with `Content-Length` still goes as one
|
|
38
|
+
* known-length `put`; the path in parts encrypts before splitting into parts. Reuse also requires
|
|
39
|
+
* the object's encryption (none, or the scheme and key id) to be this call's; a copy made in
|
|
40
|
+
* plain or with another key id is written again under the current settings.
|
|
41
|
+
*/
|
|
42
|
+
import { type BackupEncryption, type BackupEncryptionInfo, type BackupEncryptionOptions } from "./encryption.js";
|
|
43
|
+
import { type D1Exporter, type DownloadFailureReason, type ExportReady } from "./export.js";
|
|
44
|
+
/** Whether `value` is a valid `run` for `copyExport`. */
|
|
45
|
+
export declare function isValidRun(value: unknown): value is string;
|
|
46
|
+
export interface CopyExportOptions {
|
|
47
|
+
/** The client the export was started with (its database id and format go in the metadata). */
|
|
48
|
+
readonly exporter: D1Exporter;
|
|
49
|
+
/**
|
|
50
|
+
* The answer of the `start` or `poll` that reported the export complete. Cloudflare hands its
|
|
51
|
+
* signed URL out once: download from this answer, never poll again for it. A copy that fails
|
|
52
|
+
* after the download started cannot be retried with the same export; start a new one.
|
|
53
|
+
*/
|
|
54
|
+
readonly ready: ExportReady;
|
|
55
|
+
/** The R2 binding of the backups bucket. */
|
|
56
|
+
readonly bucket: R2Bucket;
|
|
57
|
+
/** Built with `backupKey(prefix, at)`; any other key is refused. */
|
|
58
|
+
readonly key: string;
|
|
59
|
+
/**
|
|
60
|
+
* The backup run's identity, recorded as the `run` metadata: fixed before the run's first
|
|
61
|
+
* export starts (`crypto.randomUUID()`) and passed unchanged to every retry, including after a
|
|
62
|
+
* restarted export, so a retry finds the object its run already finished. 1 to 128 letters,
|
|
63
|
+
* digits, `.`, `_`, `:` or `-`.
|
|
64
|
+
*/
|
|
65
|
+
readonly run: string;
|
|
66
|
+
/** Bytes per part when the dump goes in parts: 5 MiB to 32 MiB, default 10 MiB. */
|
|
67
|
+
readonly partSize?: number;
|
|
68
|
+
/**
|
|
69
|
+
* The whole copy (looking at the key, the download, every R2 write and the sidecar) must
|
|
70
|
+
* finish within this: 1000 ms to 6 hours, default 25 minutes (under the Workflow steps'
|
|
71
|
+
* 30-minute timeout). Past it the copy answers `timeout`, after at most a few more seconds
|
|
72
|
+
* spent aborting an open multipart upload (see the module's header for a write that was
|
|
73
|
+
* already in flight).
|
|
74
|
+
*/
|
|
75
|
+
readonly timeoutMs?: number;
|
|
76
|
+
/**
|
|
77
|
+
* The largest dump of known length sent as one `put`; larger ones go in parts. 1 byte to
|
|
78
|
+
* 4 GiB, default 4 GiB. Lower it only to test the multipart path.
|
|
79
|
+
*/
|
|
80
|
+
readonly singlePutLimit?: number;
|
|
81
|
+
/**
|
|
82
|
+
* Encrypt the dump with the `current` key (`backupEncryption(…)`, or its options, validated
|
|
83
|
+
* here). Without it the dump is stored as it came (plain SQL).
|
|
84
|
+
*/
|
|
85
|
+
readonly encryption?: BackupEncryption | BackupEncryptionOptions;
|
|
86
|
+
}
|
|
87
|
+
export type CopyFailureReason = DownloadFailureReason
|
|
88
|
+
/** The key already holds another backup (another run or database). */
|
|
89
|
+
| "exists"
|
|
90
|
+
/** The dump is empty. */
|
|
91
|
+
| "empty"
|
|
92
|
+
/** The dump does not start like SQL (an error page, say). */
|
|
93
|
+
| "not_sql"
|
|
94
|
+
/** Fewer or more bytes than the answer stated, or than R2 stored. */
|
|
95
|
+
| "size_mismatch"
|
|
96
|
+
/** More than `partSize` × 10 000 bytes. */
|
|
97
|
+
| "too_large"
|
|
98
|
+
/** The dump's stream failed while reading. */
|
|
99
|
+
| "read_failed"
|
|
100
|
+
/** R2 refused or failed the write. */
|
|
101
|
+
| "write_failed";
|
|
102
|
+
export type CopyResult = {
|
|
103
|
+
readonly ok: true;
|
|
104
|
+
readonly key: string;
|
|
105
|
+
/** The stored object's size (encrypted, for an encrypted backup). */
|
|
106
|
+
readonly size: number;
|
|
107
|
+
/** The bookmark the export reported for the dump (the `bookmark` metadata). */
|
|
108
|
+
readonly bookmark: string;
|
|
109
|
+
readonly databaseId: string;
|
|
110
|
+
readonly createdAt: string;
|
|
111
|
+
/**
|
|
112
|
+
* The dump's SHA-256 (64 hex characters), also stored in `<key>.sha256`; `null` where the
|
|
113
|
+
* runtime cannot compute it incrementally (Node). On reuse it is computed again from the
|
|
114
|
+
* stored object (read back from R2) and equals the sidecar's, or nothing is reused.
|
|
115
|
+
*/
|
|
116
|
+
readonly sha256: string | null;
|
|
117
|
+
/**
|
|
118
|
+
* `true` when the key already held this run's finished backup (a retry, possibly after a
|
|
119
|
+
* restarted export): nothing was downloaded or written (on Workers the stored dump was read
|
|
120
|
+
* back to check its digest), and `bookmark` is the stored one.
|
|
121
|
+
*/
|
|
122
|
+
readonly reused: boolean;
|
|
123
|
+
/**
|
|
124
|
+
* How the stored object is encrypted (scheme and key id, never the key): present only for
|
|
125
|
+
* an encrypted backup. `sha256` and `size` are then of the encrypted bytes.
|
|
126
|
+
*/
|
|
127
|
+
readonly encryption?: BackupEncryptionInfo;
|
|
128
|
+
} | {
|
|
129
|
+
readonly ok: false;
|
|
130
|
+
readonly reason: CopyFailureReason;
|
|
131
|
+
readonly retryable: boolean;
|
|
132
|
+
};
|
|
133
|
+
/**
|
|
134
|
+
* Internal: with encryption on, the database id and key go into the encrypted header, so they
|
|
135
|
+
* must fit its patterns. `d1Export` only builds UUID ids; a hand-built exporter is refused here,
|
|
136
|
+
* before anything is read or downloaded, rather than failing late. A fixed message, no value.
|
|
137
|
+
*/
|
|
138
|
+
export declare function assertEncryptable(encryption: BackupEncryption | null, databaseId: string, key: string, who: string): void;
|
|
139
|
+
/**
|
|
140
|
+
* Streams the dump of a `ready` export into `bucket` at `key`. Results, not exceptions, for every
|
|
141
|
+
* expected outcome; a `TypeError` for invalid options. It never polls: `ready` is the one answer
|
|
142
|
+
* that carries the signed URL, and a failed copy needs a new export (`start`), since polling the
|
|
143
|
+
* same bookmark again answers `not_exporting`. `retryable` says whether that new attempt may
|
|
144
|
+
* succeed.
|
|
145
|
+
*
|
|
146
|
+
* A key that already holds an object is reused only when that object is this very backup: the
|
|
147
|
+
* marker, the same database id, **the same `run`** (see `inspectExisting`), the same encryption
|
|
148
|
+
* (none, or the scheme and the `current` key id), the stored size equal to the recorded one, and
|
|
149
|
+
* (where digests are computed) its digest sidecar, naming this very object and equal to the
|
|
150
|
+
* digest of the object read back. Then nothing is downloaded. An object of this run that is not
|
|
151
|
+
* proven finished, or not encrypted as asked, is written again; any other object is `exists`. A
|
|
152
|
+
* failure leaves no object and no open multipart upload (past the deadline, see the module's
|
|
153
|
+
* header). The copy does not look at other keys: two databases sharing a prefix both get their
|
|
154
|
+
* backups written, and `pruneBackups` refuses to prune it.
|
|
155
|
+
*/
|
|
156
|
+
export declare function copyExport(options: CopyExportOptions): Promise<CopyResult>;
|
|
157
|
+
/**
|
|
158
|
+
* Internal (the Workflow's start steps; not exported from the package): whether `key` already
|
|
159
|
+
* holds the finished backup of `run`, by the same rule as `copyExport`'s reuse (encryption
|
|
160
|
+
* included). A finished one is returned as a reused result; another run's object is `exists`;
|
|
161
|
+
* nothing, or an object of this run that is not proven finished, is `null` (an export is needed).
|
|
162
|
+
* Asked before starting an export, so a retry whose run already finished its copy starts none,
|
|
163
|
+
* and by a poll told no export is running, so a finished copy whose step result was lost is
|
|
164
|
+
* found. On Workers a finished object is read back whole (its digest), hence the copy's default
|
|
165
|
+
* deadline.
|
|
166
|
+
*/
|
|
167
|
+
export declare function findCopy(options: {
|
|
168
|
+
readonly exporter: D1Exporter;
|
|
169
|
+
readonly bucket: R2Bucket;
|
|
170
|
+
readonly key: string;
|
|
171
|
+
readonly run: string;
|
|
172
|
+
readonly encryption?: BackupEncryption | BackupEncryptionOptions;
|
|
173
|
+
readonly timeoutMs?: number;
|
|
174
|
+
}): Promise<CopyResult | null>;
|
|
175
|
+
//# sourceMappingURL=copy.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"copy.d.ts","sourceRoot":"","sources":["../src/copy.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwCG;AACH,OAAO,EACL,KAAK,gBAAgB,EACrB,KAAK,oBAAoB,EACzB,KAAK,uBAAuB,EAS7B,MAAM,iBAAiB,CAAC;AACzB,OAAO,EACL,KAAK,UAAU,EACf,KAAK,qBAAqB,EAC1B,KAAK,WAAW,EAEjB,MAAM,aAAa,CAAC;AAmCrB,yDAAyD;AACzD,wBAAgB,UAAU,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,MAAM,CAE1D;AAED,MAAM,WAAW,iBAAiB;IAChC,8FAA8F;IAC9F,QAAQ,CAAC,QAAQ,EAAE,UAAU,CAAC;IAC9B;;;;OAIG;IACH,QAAQ,CAAC,KAAK,EAAE,WAAW,CAAC;IAC5B,4CAA4C;IAC5C,QAAQ,CAAC,MAAM,EAAE,QAAQ,CAAC;IAC1B,oEAAoE;IACpE,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB;;;;;OAKG;IACH,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,mFAAmF;IACnF,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAC3B;;;;;;OAMG;IACH,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC;IAC5B;;;OAGG;IACH,QAAQ,CAAC,cAAc,CAAC,EAAE,MAAM,CAAC;IACjC;;;OAGG;IACH,QAAQ,CAAC,UAAU,CAAC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