@instantdb/platform 1.0.65 → 1.0.66
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/.turbo/turbo-build.log +8 -8
- package/__tests__/src/backupDownload.test.ts +34 -18
- package/dist/commonjs/backupDownload.d.ts +1 -13
- package/dist/commonjs/backupDownload.d.ts.map +1 -1
- package/dist/commonjs/backupDownload.js +8 -84
- package/dist/commonjs/backupDownload.js.map +1 -1
- package/dist/esm/backupDownload.d.ts +1 -13
- package/dist/esm/backupDownload.d.ts.map +1 -1
- package/dist/esm/backupDownload.js +8 -84
- package/dist/esm/backupDownload.js.map +1 -1
- package/dist/standalone/index.js +1114 -1156
- package/dist/standalone/index.umd.cjs +21 -21
- package/package.json +4 -4
- package/src/backupDownload.ts +16 -112
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
|
|
2
|
-
> @instantdb/platform@1.0.
|
|
2
|
+
> @instantdb/platform@1.0.66 build /home/runner/work/instant/instant/client/packages/platform
|
|
3
3
|
> rm -rf dist; npm run build:tshy && npm run build:standalone && npm run check-exports
|
|
4
4
|
|
|
5
5
|
npm warn Unknown env config "verify-deps-before-run". This will stop working in the next major version of npm. See `npm help npmrc` for supported config options.
|
|
6
6
|
npm warn Unknown user config "always-auth". This will stop working in the next major version of npm. See `npm help npmrc` for supported config options.
|
|
7
7
|
|
|
8
|
-
> @instantdb/platform@1.0.
|
|
8
|
+
> @instantdb/platform@1.0.66 build:tshy
|
|
9
9
|
> tshy
|
|
10
10
|
|
|
11
11
|
npm warn Unknown env config "verify-deps-before-run". This will stop working in the next major version of npm. See `npm help npmrc` for supported config options.
|
|
12
12
|
npm warn Unknown user config "always-auth". This will stop working in the next major version of npm. See `npm help npmrc` for supported config options.
|
|
13
13
|
|
|
14
|
-
> @instantdb/platform@1.0.
|
|
14
|
+
> @instantdb/platform@1.0.66 build:standalone
|
|
15
15
|
> vite build
|
|
16
16
|
|
|
17
17
|
[36mvite v5.4.14 [32mbuilding for production...[36m[39m
|
|
@@ -19,17 +19,17 @@ transforming...
|
|
|
19
19
|
[32m✓[39m 88 modules transformed.
|
|
20
20
|
rendering chunks...
|
|
21
21
|
computing gzip size...
|
|
22
|
-
[2mdist/standalone/[22m[36mindex.umd.cjs [39m[1m[
|
|
23
|
-
[2mdist/standalone/[22m[36mindex.js [39m[1m[
|
|
24
|
-
[32m✓ built in
|
|
22
|
+
[2mdist/standalone/[22m[36mindex.umd.cjs [39m[1m[2m371.69 kB[22m[1m[22m[2m │ gzip: 102.99 kB[22m
|
|
23
|
+
[2mdist/standalone/[22m[36mindex.js [39m[1m[2m498.59 kB[22m[1m[22m[2m │ gzip: 120.55 kB[22m
|
|
24
|
+
[32m✓ built in 2.26s[39m
|
|
25
25
|
npm warn Unknown env config "verify-deps-before-run". This will stop working in the next major version of npm. See `npm help npmrc` for supported config options.
|
|
26
26
|
npm warn Unknown user config "always-auth". This will stop working in the next major version of npm. See `npm help npmrc` for supported config options.
|
|
27
27
|
|
|
28
|
-
> @instantdb/platform@1.0.
|
|
28
|
+
> @instantdb/platform@1.0.66 check-exports
|
|
29
29
|
> attw --pack .
|
|
30
30
|
|
|
31
31
|
|
|
32
|
-
@instantdb/platform v1.0.
|
|
32
|
+
@instantdb/platform v1.0.66
|
|
33
33
|
|
|
34
34
|
Build tools:
|
|
35
35
|
- @arethetypeswrong/cli@^0.18.5
|
|
@@ -168,44 +168,49 @@ describe('downloadBackupArchive', () => {
|
|
|
168
168
|
await expect(promise).rejects.toMatchObject({ name: 'AbortError' });
|
|
169
169
|
});
|
|
170
170
|
|
|
171
|
-
test('fetches
|
|
171
|
+
test('fetches each entry body only when the writer reaches it, never ahead', async () => {
|
|
172
172
|
const files = [
|
|
173
173
|
{ name: 'config.json', size: 1 },
|
|
174
174
|
{ name: 'entities/a.jsonl', size: 1 },
|
|
175
175
|
{ name: 'entities/b.jsonl', size: 1 },
|
|
176
176
|
];
|
|
177
177
|
const started: string[] = [];
|
|
178
|
-
let resolveAllStarted!: () => void;
|
|
179
|
-
const allStarted = new Promise<void>((r) => {
|
|
180
|
-
resolveAllStarted = r;
|
|
181
|
-
});
|
|
182
178
|
|
|
183
179
|
const manager = {
|
|
184
180
|
listFiles: async () => files,
|
|
185
181
|
getFileUrl,
|
|
186
|
-
// A fetch records that it started and, once every entry's fetch has
|
|
187
|
-
// begun, releases the writer below.
|
|
188
182
|
streamStorageFiles: async function* () {},
|
|
189
183
|
} as any;
|
|
184
|
+
// Records the moment a body is fetched. Downloads are strictly sequential,
|
|
185
|
+
// so this fires only when the writer reaches the entry — never ahead.
|
|
190
186
|
const trackingFetch = async (url: string) => {
|
|
191
187
|
started.push(url);
|
|
192
|
-
if (started.length === files.length) resolveAllStarted();
|
|
193
188
|
return bodyOf(`body:${url}`);
|
|
194
189
|
};
|
|
195
190
|
|
|
196
|
-
//
|
|
197
|
-
//
|
|
198
|
-
//
|
|
199
|
-
//
|
|
200
|
-
|
|
191
|
+
// A pair of gates per entry: `began` resolves when the encoder starts
|
|
192
|
+
// writing that entry; the encoder then blocks on `release` until the test
|
|
193
|
+
// lets it proceed. This lets us pause on each entry in turn and check that
|
|
194
|
+
// no later body has been pulled ahead — the browser-fetch bug that buffered
|
|
195
|
+
// whole entities before their turn would show a later fetch in `started`
|
|
196
|
+
// while the current write is blocked.
|
|
197
|
+
const deferred = () => {
|
|
198
|
+
let resolve!: () => void;
|
|
199
|
+
const promise = new Promise<void>((r) => {
|
|
200
|
+
resolve = r;
|
|
201
|
+
});
|
|
202
|
+
return { promise, resolve };
|
|
203
|
+
};
|
|
204
|
+
const began = files.map(deferred);
|
|
205
|
+
const release = files.map(deferred);
|
|
206
|
+
let addCount = 0;
|
|
201
207
|
const createWriter = async (sink: WritableStream<Uint8Array>) => {
|
|
202
208
|
const w = sink.getWriter();
|
|
203
209
|
return {
|
|
204
210
|
add: async (_name: string, input: ReadableStream<Uint8Array>) => {
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
}
|
|
211
|
+
const i = addCount++;
|
|
212
|
+
began[i].resolve();
|
|
213
|
+
await release[i].promise;
|
|
209
214
|
for await (const _chunk of input) {
|
|
210
215
|
// drain
|
|
211
216
|
}
|
|
@@ -215,7 +220,7 @@ describe('downloadBackupArchive', () => {
|
|
|
215
220
|
};
|
|
216
221
|
};
|
|
217
222
|
|
|
218
|
-
|
|
223
|
+
const done = downloadBackupArchive({
|
|
219
224
|
manager,
|
|
220
225
|
backup,
|
|
221
226
|
fetchBody: trackingFetch,
|
|
@@ -223,6 +228,17 @@ describe('downloadBackupArchive', () => {
|
|
|
223
228
|
createWriter,
|
|
224
229
|
});
|
|
225
230
|
|
|
231
|
+
// Walk the entries one at a time. When each write begins, exactly the
|
|
232
|
+
// bodies up to and including it have been fetched — nothing ahead. Release
|
|
233
|
+
// it and move to the next; the next body must not have been fetched until
|
|
234
|
+
// this write completed.
|
|
235
|
+
for (let i = 0; i < files.length; i++) {
|
|
236
|
+
await began[i].promise;
|
|
237
|
+
expect(started).toEqual(files.slice(0, i + 1).map((f) => f.name));
|
|
238
|
+
release[i].resolve();
|
|
239
|
+
}
|
|
240
|
+
await done;
|
|
241
|
+
|
|
226
242
|
expect(started).toEqual([
|
|
227
243
|
'config.json',
|
|
228
244
|
'entities/a.jsonl',
|
|
@@ -54,21 +54,9 @@ export type DownloadBackupArchiveOpts = {
|
|
|
54
54
|
createWriter: (sink: WritableStream<Uint8Array>, signal: AbortSignal) => Promise<BackupArchiveWriter>;
|
|
55
55
|
signal?: AbortSignal;
|
|
56
56
|
onProgress?: (progress: BackupDownloadProgress) => void;
|
|
57
|
-
/**
|
|
58
|
-
* How many entries to fetch ahead of the one currently being written into
|
|
59
|
-
* the archive.
|
|
60
|
-
*
|
|
61
|
-
* Only fetch *initiation* is parallelised, the bodies are still written in
|
|
62
|
-
* order and consumed one at a time, so an in-flight prefetched body buffers
|
|
63
|
-
* only to its stream's high-water mark and memory stays bounded. Defaults to
|
|
64
|
-
* {@link DEFAULT_PREFETCH}.
|
|
65
|
-
*/
|
|
66
|
-
prefetch?: number;
|
|
67
57
|
/**
|
|
68
58
|
* Retry policy for *opening* an entry's body (fetching its presigned URL and
|
|
69
|
-
* getting a live response).
|
|
70
|
-
* while earlier entries write, so a queued connection can be reset before we
|
|
71
|
-
* read it.
|
|
59
|
+
* getting a live response).
|
|
72
60
|
* `attempts` is the total number of tries (default {@link DEFAULT_FETCH_ATTEMPTS});
|
|
73
61
|
* `delayMs` is the base for exponential backoff (default {@link DEFAULT_RETRY_DELAY_MS}).
|
|
74
62
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"backupDownload.d.ts","sourceRoot":"","sources":["../../src/backupDownload.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,SAAS,EAET,cAAc,EACf,MAAM,cAAc,CAAC;AAEtB,MAAM,MAAM,sBAAsB,GAAG;IACnC,iBAAiB,EAAE,MAAM,CAAC;IAC1B,aAAa,EAAE,MAAM,GAAG,IAAI,CAAC;IAC7B,cAAc,EAAE,MAAM,CAAC;IACvB,UAAU,EAAE,MAAM,GAAG,IAAI,CAAC;IAE1B,QAAQ,EAAE,MAAM,CAAC;IAIjB,SAAS,EAAE,MAAM,CAAC;IAClB,UAAU,EAAE,MAAM,GAAG,IAAI,CAAC;IAG1B,aAAa,EAAE,MAAM,CAAC;IACtB,WAAW,EAAE,MAAM,CAAC;CACrB,CAAC;AAEF,MAAM,MAAM,oBAAoB,GAAG;IACjC,QAAQ,EAAE,MAAM,CAAC;IACjB,KAAK,EAAE,MAAM,CAAC;IACd,QAAQ,EAAE,MAAM,CAAC;CAClB,CAAC;AAEF;;;;;;;;;GASG;AACH,MAAM,MAAM,mBAAmB,GAAG;IAChC,GAAG,CACD,IAAI,EAAE,MAAM,EACZ,KAAK,EAAE,cAAc,CAAC,UAAU,CAAC,EACjC,IAAI,EAAE;QAAE,WAAW,EAAE,IAAI,CAAA;KAAE,GAC1B,OAAO,C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
3
3
|
exports.downloadBackupArchive = downloadBackupArchive;
|
|
4
|
-
const DEFAULT_PREFETCH = 4;
|
|
5
4
|
const DEFAULT_FETCH_ATTEMPTS = 3;
|
|
6
5
|
const DEFAULT_RETRY_DELAY_MS = 500;
|
|
7
6
|
const MAX_RETRY_DELAY_MS = 5000;
|
|
@@ -59,9 +58,9 @@ function replayFrom(first, reader) {
|
|
|
59
58
|
* naming the entry.
|
|
60
59
|
*
|
|
61
60
|
* The first chunk is read inside the retry scope, so a body that connects but
|
|
62
|
-
* fails on its first read
|
|
63
|
-
*
|
|
64
|
-
*
|
|
61
|
+
* fails on its first read is re-fetched too, since nothing has been written
|
|
62
|
+
* to the archive yet. Only failures once bytes are flowing are treated as
|
|
63
|
+
* unrecoverable.
|
|
65
64
|
*/
|
|
66
65
|
async function openWithRetry(open, opts) {
|
|
67
66
|
let lastError;
|
|
@@ -89,80 +88,6 @@ async function openWithRetry(open, opts) {
|
|
|
89
88
|
}
|
|
90
89
|
throw new Error(opts.describe(lastError));
|
|
91
90
|
}
|
|
92
|
-
/**
|
|
93
|
-
* Wraps an ordered stream of entry thunks, keeping up to `lookahead` fetches
|
|
94
|
-
* in flight while yielding the prepared entries in their original order. A
|
|
95
|
-
* background producer pulls thunks and starts their fetches as space frees up;
|
|
96
|
-
* the consumer awaits each in turn. Preserves order and backpressure: at most
|
|
97
|
-
* `lookahead` bodies are ever in flight, and the producer parks when the
|
|
98
|
-
* pipeline is full or the source is waiting for more work.
|
|
99
|
-
*/
|
|
100
|
-
async function* prefetchEntries(thunks, lookahead) {
|
|
101
|
-
const pipeline = [];
|
|
102
|
-
const state = { done: false, producerError: null, onItem: null, onSpace: null };
|
|
103
|
-
const wakeItem = () => {
|
|
104
|
-
const w = state.onItem;
|
|
105
|
-
state.onItem = null;
|
|
106
|
-
if (w)
|
|
107
|
-
w();
|
|
108
|
-
};
|
|
109
|
-
const wakeSpace = () => {
|
|
110
|
-
const w = state.onSpace;
|
|
111
|
-
state.onSpace = null;
|
|
112
|
-
if (w)
|
|
113
|
-
w();
|
|
114
|
-
};
|
|
115
|
-
// Never rejects: a failure to produce the next thunk (or start its fetch)
|
|
116
|
-
// lands in state.producerError for the consumer to throw in order.
|
|
117
|
-
const producer = (async () => {
|
|
118
|
-
try {
|
|
119
|
-
for await (const thunk of thunks) {
|
|
120
|
-
while (pipeline.length >= lookahead) {
|
|
121
|
-
await new Promise((resolve) => {
|
|
122
|
-
state.onSpace = resolve;
|
|
123
|
-
});
|
|
124
|
-
}
|
|
125
|
-
const started = thunk();
|
|
126
|
-
// The consumer awaits `started` in order; attach a no-op catch so a
|
|
127
|
-
// fetch that rejects before then isn't reported as unhandled.
|
|
128
|
-
started.catch(() => { });
|
|
129
|
-
pipeline.push(started);
|
|
130
|
-
wakeItem();
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
catch (e) {
|
|
134
|
-
state.producerError = e;
|
|
135
|
-
}
|
|
136
|
-
finally {
|
|
137
|
-
state.done = true;
|
|
138
|
-
wakeItem();
|
|
139
|
-
}
|
|
140
|
-
})();
|
|
141
|
-
try {
|
|
142
|
-
while (true) {
|
|
143
|
-
if (pipeline.length === 0) {
|
|
144
|
-
if (state.done) {
|
|
145
|
-
if (state.producerError)
|
|
146
|
-
throw state.producerError;
|
|
147
|
-
break;
|
|
148
|
-
}
|
|
149
|
-
await new Promise((resolve) => {
|
|
150
|
-
state.onItem = resolve;
|
|
151
|
-
});
|
|
152
|
-
continue;
|
|
153
|
-
}
|
|
154
|
-
const entry = await pipeline.shift();
|
|
155
|
-
wakeSpace();
|
|
156
|
-
yield entry;
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
finally {
|
|
160
|
-
// On early exit (abort/error), let the producer unwind — its own signal
|
|
161
|
-
// teardown resolves the source's waits — without blocking here.
|
|
162
|
-
wakeSpace();
|
|
163
|
-
producer.catch(() => { });
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
91
|
/**
|
|
167
92
|
* Downloads a backup into a single archive written to `opts.sink`: entries
|
|
168
93
|
* in the canonical restore order (`config.json`, then the
|
|
@@ -285,9 +210,9 @@ async function downloadBackupArchive(opts) {
|
|
|
285
210
|
// entities/*.jsonl shards, then files/<locationId>. In particular ALL entity
|
|
286
211
|
// files must be written before ANY storage file. listFiles returns the
|
|
287
212
|
// entity files in write order; this generator yields thunks for them in
|
|
288
|
-
// order, then drains the storage queue. The
|
|
289
|
-
//
|
|
290
|
-
//
|
|
213
|
+
// order, then drains the storage queue. The consumer calls each thunk in turn
|
|
214
|
+
// and writes it to completion before the next, so bodies download one at a
|
|
215
|
+
// time and never ahead of the encoder.
|
|
291
216
|
const thunks = (async function* () {
|
|
292
217
|
const files = await manager.listFiles(backup.id, { signal });
|
|
293
218
|
if (files.length === 0) {
|
|
@@ -380,8 +305,6 @@ async function downloadBackupArchive(opts) {
|
|
|
380
305
|
if (storageError)
|
|
381
306
|
throw storageError;
|
|
382
307
|
})();
|
|
383
|
-
const prefetch = finitePositiveInt(opts.prefetch, DEFAULT_PREFETCH);
|
|
384
|
-
const entries = prefetchEntries(thunks, prefetch);
|
|
385
308
|
const sinkWriter = opts.sink.getWriter();
|
|
386
309
|
try {
|
|
387
310
|
// Sink the archive encoder writes into: it tallies the encoded size for
|
|
@@ -403,7 +326,8 @@ async function downloadBackupArchive(opts) {
|
|
|
403
326
|
},
|
|
404
327
|
});
|
|
405
328
|
const writer = await createWriter(countingSink, signal);
|
|
406
|
-
for await (const
|
|
329
|
+
for await (const thunk of thunks) {
|
|
330
|
+
const entry = await thunk();
|
|
407
331
|
entry.onWriting();
|
|
408
332
|
await writer.add(entry.name, entry.input, {
|
|
409
333
|
lastModDate: backup.backupAt,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"backupDownload.js","sourceRoot":"","sources":["../../src/backupDownload.ts"],"names":[],"mappings":";;AAuUA,sDA+RC;AA9fD,MAAM,gBAAgB,GAAG,CAAC,CAAC;AAC3B,MAAM,sBAAsB,GAAG,CAAC,C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UAAU,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,GAAG,EAAE,GAAE,CAAC,CAAC,CAAC;QAC1C,MAAM,CAAC,CAAC;IACV,CAAC;AACH,CAAC","sourcesContent":["import type {\n AppBackup,\n AppBackupStorageFile,\n BackupsManager,\n} from './backups.ts';\n\nexport type BackupDownloadProgress = {\n entitiesCompleted: number;\n entitiesTotal: number | null;\n filesCompleted: number;\n filesTotal: number | null;\n // Compressed bytes written to the sink so far (the zip's on-disk size).\n zipBytes: number;\n // Uncompressed bytes read from source bodies, and the backup's known\n // uncompressed total — the numerator/denominator for a progress bar.\n // bytesTotal is null when the backup row carries no sizes.\n bytesRead: number;\n bytesTotal: number | null;\n // The entry currently being fetched in each phase; empty while that phase\n // isn't actively fetching, so a finished phase stops claiming a file.\n currentEntity: string;\n currentFile: string;\n};\n\nexport type BackupDownloadResult = {\n entities: number;\n files: number;\n zipBytes: number;\n};\n\n/**\n * The archive encoder {@link downloadBackupArchive} writes entries through,\n * supplied by the caller so this package doesn't depend on a zip\n * implementation. zip.js's `ZipWriter` satisfies it structurally, so\n * `new ZipWriter(sink, { zip64: true, signal })` works without an adapter.\n *\n * Implementations must handle archives past 4GB — for zip that means zip64,\n * without which the central-directory offsets wrap and the archive is\n * silently unreadable.\n */\nexport type BackupArchiveWriter = {\n add(\n name: string,\n input: ReadableStream<Uint8Array>,\n opts: { lastModDate: Date },\n ): Promise<unknown>;\n close(): Promise<unknown>;\n};\n\nexport type DownloadBackupArchiveOpts = {\n backup: AppBackup;\n /**\n * Fetches a presigned URL, resolving with the response body and rejecting\n * on a non-200 status. Put the status in the message (e.g. `HTTP 403`) —\n * it's surfaced to the user alongside the failing entry's name. The entity\n * files are served with `Content-Encoding: zstd`; browser fetch decodes\n * that transparently, other runtimes must decompress explicitly.\n */\n fetchBody: (\n url: string,\n signal: AbortSignal,\n ) => Promise<ReadableStream<Uint8Array>>;\n /**\n * Where the archive's bytes go. Closed after the last entry is written;\n * aborted when the download fails or is cancelled, so the caller can\n * discard partial output.\n */\n sink: WritableStream<Uint8Array>;\n /**\n * Builds the archive encoder over a sink that already counts progress and\n * carries the caller's sink's backpressure.\n */\n createWriter: (\n sink: WritableStream<Uint8Array>,\n signal: AbortSignal,\n ) => Promise<BackupArchiveWriter>;\n signal?: AbortSignal;\n onProgress?: (progress: BackupDownloadProgress) => void;\n /**\n * How many entries to fetch ahead of the one currently being written into\n * the archive.\n *\n * Only fetch *initiation* is parallelised, the bodies are still written in\n * order and consumed one at a time, so an in-flight prefetched body buffers\n * only to its stream's high-water mark and memory stays bounded. Defaults to\n * {@link DEFAULT_PREFETCH}.\n */\n prefetch?: number;\n /**\n * Retry policy for *opening* an entry's body (fetching its presigned URL and\n * getting a live response). Prefetching keeps body connections open, paused,\n * while earlier entries write, so a queued connection can be reset before we\n * read it.\n * `attempts` is the total number of tries (default {@link DEFAULT_FETCH_ATTEMPTS});\n * `delayMs` is the base for exponential backoff (default {@link DEFAULT_RETRY_DELAY_MS}).\n *\n * This only covers failures before the writer starts consuming the body.\n * Once bytes have been written into the archive entry there's no way to\n * restart it without HTTP range/resume support, so a mid-stream failure\n * still fails the download.\n */\n retry?: { attempts?: number; delayMs?: number };\n};\n\nconst DEFAULT_PREFETCH = 4;\nconst DEFAULT_FETCH_ATTEMPTS = 3;\nconst DEFAULT_RETRY_DELAY_MS = 500;\nconst MAX_RETRY_DELAY_MS = 5000;\n\n// Normalize caller-supplied numeric options so NaN/Infinity/non-integers can't\n// alter retry counts or pipeline bounds — fall back to the default instead.\nconst finitePositiveInt = (v: number | undefined, fallback: number): number =>\n v != null && Number.isInteger(v) && v > 0 ? v : fallback;\nconst finiteNonNegative = (v: number | undefined, fallback: number): number =>\n v != null && Number.isFinite(v) && v >= 0 ? v : fallback;\n\nconst isAbortError = (e: unknown): boolean =>\n (e as { name?: string })?.name === 'AbortError';\n\nconst errorMessage = (e: unknown): string =>\n e instanceof Error ? e.message : String(e);\n\n// A cancellable sleep: resolves after `ms`, or rejects if the signal aborts\n// first so backoff between retries doesn't outlive a cancelled download.\nconst delay = (ms: number, signal: AbortSignal): Promise<void> =>\n new Promise<void>((resolve, reject) => {\n const onAbort = () => {\n clearTimeout(timer);\n reject(signal.reason ?? new DOMException('Aborted', 'AbortError'));\n };\n const timer = setTimeout(() => {\n signal.removeEventListener('abort', onAbort);\n resolve();\n }, ms);\n signal.addEventListener('abort', onAbort, { once: true });\n });\n\n// Re-emits an already-read first chunk, then streams the rest from `reader`.\n// Past that first chunk read errors propagate to the consumer unchanged — by\n// then bytes are in the archive entry and it can't be restarted.\nfunction replayFrom(\n first: ReadableStreamReadResult<Uint8Array>,\n reader: ReadableStreamDefaultReader<Uint8Array>,\n): ReadableStream<Uint8Array> {\n let replayed = false;\n return new ReadableStream<Uint8Array>({\n async pull(controller) {\n if (!replayed) {\n replayed = true;\n if (first.done) {\n controller.close();\n return;\n }\n controller.enqueue(first.value);\n return;\n }\n const { done, value } = await reader.read();\n if (done) controller.close();\n else controller.enqueue(value);\n },\n async cancel(reason) {\n await reader.cancel(reason);\n },\n });\n}\n\n/**\n * Opens a body via `open`, retrying on transient failure with abortable\n * exponential backoff. `open` is re-invoked from scratch each attempt, so an\n * entity file re-mints its presigned URL. An abort propagates immediately; any\n * other final failure is wrapped by `describe` into a user-facing message\n * naming the entry.\n *\n * The first chunk is read inside the retry scope, so a body that connects but\n * fails on its first read — the shape of a prefetched connection reset while it\n * sat idle — is re-fetched too, since nothing has been written to the archive\n * yet. Only failures once bytes are flowing are treated as unrecoverable.\n */\nasync function openWithRetry(\n open: () => Promise<ReadableStream<Uint8Array>>,\n opts: {\n signal: AbortSignal;\n attempts: number;\n delayMs: number;\n describe: (e: unknown) => string;\n },\n): Promise<ReadableStream<Uint8Array>> {\n let lastError: unknown;\n for (let attempt = 1; attempt <= opts.attempts; attempt++) {\n opts.signal.throwIfAborted();\n let reader: ReadableStreamDefaultReader<Uint8Array> | undefined;\n try {\n const body = await open();\n reader = body.getReader();\n const first = await reader.read();\n return replayFrom(first, reader);\n } catch (e) {\n // Release the failed connection before retrying (or giving up).\n if (reader) reader.cancel().catch(() => {});\n if (isAbortError(e)) throw e;\n lastError = e;\n if (attempt < opts.attempts) {\n const backoff = Math.min(\n opts.delayMs * 2 ** (attempt - 1),\n MAX_RETRY_DELAY_MS,\n );\n await delay(backoff, opts.signal);\n }\n }\n }\n throw new Error(opts.describe(lastError));\n}\n\n/**\n * One archive entry whose body fetch has already been started. `onWriting`\n * runs when the encoder begins consuming it (so progress reflects the entry\n * actually streaming, not one prefetched ahead); `onAdded` runs once it's\n * fully written.\n */\ntype PreparedEntry = {\n name: string;\n input: ReadableStream<Uint8Array>;\n onWriting: () => void;\n onAdded: () => void;\n};\n\n/**\n * A thunk that starts fetching one entry (presigned URL + body) and resolves\n * once the body stream is available — not once it's fully downloaded.\n */\ntype EntryThunk = () => Promise<PreparedEntry>;\n\n/**\n * Wraps an ordered stream of entry thunks, keeping up to `lookahead` fetches\n * in flight while yielding the prepared entries in their original order. A\n * background producer pulls thunks and starts their fetches as space frees up;\n * the consumer awaits each in turn. Preserves order and backpressure: at most\n * `lookahead` bodies are ever in flight, and the producer parks when the\n * pipeline is full or the source is waiting for more work.\n */\nasync function* prefetchEntries(\n thunks: AsyncIterable<EntryThunk>,\n lookahead: number,\n): AsyncGenerator<PreparedEntry> {\n const pipeline: Promise<PreparedEntry>[] = [];\n const state: {\n done: boolean;\n producerError: unknown;\n // Woken when the producer pushes an entry (or finishes).\n onItem: (() => void) | null;\n // Woken when the consumer frees a pipeline slot.\n onSpace: (() => void) | null;\n } = { done: false, producerError: null, onItem: null, onSpace: null };\n\n const wakeItem = () => {\n const w = state.onItem;\n state.onItem = null;\n if (w) w();\n };\n const wakeSpace = () => {\n const w = state.onSpace;\n state.onSpace = null;\n if (w) w();\n };\n\n // Never rejects: a failure to produce the next thunk (or start its fetch)\n // lands in state.producerError for the consumer to throw in order.\n const producer = (async () => {\n try {\n for await (const thunk of thunks) {\n while (pipeline.length >= lookahead) {\n await new Promise<void>((resolve) => {\n state.onSpace = resolve;\n });\n }\n const started = thunk();\n // The consumer awaits `started` in order; attach a no-op catch so a\n // fetch that rejects before then isn't reported as unhandled.\n started.catch(() => {});\n pipeline.push(started);\n wakeItem();\n }\n } catch (e) {\n state.producerError = e;\n } finally {\n state.done = true;\n wakeItem();\n }\n })();\n\n try {\n while (true) {\n if (pipeline.length === 0) {\n if (state.done) {\n if (state.producerError) throw state.producerError;\n break;\n }\n await new Promise<void>((resolve) => {\n state.onItem = resolve;\n });\n continue;\n }\n const entry = await pipeline.shift()!;\n wakeSpace();\n yield entry;\n }\n } finally {\n // On early exit (abort/error), let the producer unwind — its own signal\n // teardown resolves the source's waits — without blocking here.\n wakeSpace();\n producer.catch(() => {});\n }\n}\n\n/**\n * Downloads a backup into a single archive written to `opts.sink`: entries\n * in the canonical restore order (`config.json`, then the\n * `entities/<etype>.jsonl` shards, then `files/<locationId>` storage blobs —\n * all entity files before any storage file), with the encoder writing\n * through a counting sink that awaits the caller's sink, so a fast source\n * can't outrun it and balloon memory.\n *\n * The runtime-specific pieces are injected: how to fetch a presigned URL\n * (`fetchBody`), where the bytes go (`sink`), and the archive encoder\n * (`createWriter`). Most callers reach this via\n * {@link BackupsManager.downloadArchive}.\n */\nexport async function downloadBackupArchive(\n opts: DownloadBackupArchiveOpts & {\n manager: Pick<\n BackupsManager,\n 'listFiles' | 'getFileUrl' | 'streamStorageFiles'\n >;\n },\n): Promise<BackupDownloadResult> {\n const { manager, backup, fetchBody, createWriter, onProgress } = opts;\n\n // Internal controller so a pipeline failure also tears down the\n // storage-files discovery stream and any in-flight body fetches.\n const abortController = new AbortController();\n if (opts.signal?.aborted) {\n abortController.abort();\n } else {\n opts.signal?.addEventListener('abort', () => abortController.abort(), {\n once: true,\n });\n }\n const signal = abortController.signal;\n\n let entitiesCompleted = 0;\n let entitiesTotal: number | null = null;\n let filesCompleted = 0;\n let filesTotal: number | null = null;\n let zipBytes = 0;\n let bytesRead = 0;\n let currentEntity = '';\n let currentFile = '';\n const bytesTotal =\n backup.uncompressedSize != null\n ? backup.uncompressedSize + (backup.filesSize ?? 0)\n : null;\n\n const retryAttempts = finitePositiveInt(\n opts.retry?.attempts,\n DEFAULT_FETCH_ATTEMPTS,\n );\n const retryDelayMs = finiteNonNegative(\n opts.retry?.delayMs,\n DEFAULT_RETRY_DELAY_MS,\n );\n\n const tick = () =>\n onProgress?.({\n entitiesCompleted,\n entitiesTotal,\n filesCompleted,\n filesTotal,\n zipBytes,\n bytesRead,\n bytesTotal,\n currentEntity,\n currentFile,\n });\n\n // Throttle by time: a large backup pushes many small chunks and ticking on\n // every one is wasted work. Phase changes tick() directly so they're still\n // immediate.\n const TICK_INTERVAL_MS = 100;\n let lastTickAt = 0;\n const throttledTick = () => {\n const now = Date.now();\n if (now - lastTickAt >= TICK_INTERVAL_MS) {\n lastTickAt = now;\n tick();\n }\n };\n\n // Count the uncompressed bytes of a source body for progress as it streams\n // into the archive.\n const countBytes = (\n body: ReadableStream<Uint8Array>,\n ): ReadableStream<Uint8Array> =>\n body.pipeThrough(\n new TransformStream<Uint8Array, Uint8Array>({\n transform(chunk, controller) {\n bytesRead += chunk.byteLength;\n throttledTick();\n controller.enqueue(chunk);\n },\n }),\n );\n\n // Storage-files discovery runs concurrently with the entity phase and is\n // drained eagerly into a queue. That isn't just overlap: it closes the\n // NDJSON connection quickly instead of holding it open (and at the mercy of\n // idle timeouts) while multi-GB blobs download. `queueHead` walks the array\n // in place, freeing each slot as it's consumed.\n const queue: (AppBackupStorageFile | undefined)[] = [];\n let queueHead = 0;\n let storageDone = false;\n let storageError: Error | null = null;\n let waitResolve: (() => void) | null = null;\n const notify = () => {\n const w = waitResolve;\n waitResolve = null;\n w?.();\n };\n\n // Never rejects: failures land in storageError for the drain loop to throw.\n const discovery = (async () => {\n let discoveryComplete = false;\n try {\n for await (const file of manager.streamStorageFiles(backup.id, {\n signal,\n })) {\n queue.push(file);\n filesTotal = (filesTotal ?? 0) + 1;\n throttledTick();\n notify();\n }\n discoveryComplete = true;\n } catch (e) {\n // The abort path is expected when the pipeline failed and we tore the\n // discovery down.\n if (!isAbortError(e)) {\n storageError = e as Error;\n }\n } finally {\n // A failed listing keeps the total unknown rather than reading as an\n // empty-but-complete storage phase.\n if (discoveryComplete && filesTotal == null) filesTotal = 0;\n storageDone = true;\n tick();\n notify();\n }\n })();\n\n // Entry write order is significant for restore: config.json first, then the\n // entities/*.jsonl shards, then files/<locationId>. In particular ALL entity\n // files must be written before ANY storage file. listFiles returns the\n // entity files in write order; this generator yields thunks for them in\n // order, then drains the storage queue. The thunks are consumed through\n // prefetchEntries, which starts a bounded number of the fetches ahead of the\n // encoder while keeping this order and one-at-a-time writing.\n const thunks = (async function* (): AsyncGenerator<EntryThunk> {\n const files = await manager.listFiles(backup.id, { signal });\n if (files.length === 0) {\n throw new Error('No files found for this backup.');\n }\n // We write entries in the order the server returns them, and restore\n // requires config.json to be the first entry. Fail loudly rather than\n // build a zip that can't be restored.\n if (files[0].name !== 'config.json') {\n throw new Error(\n `Backup files came back in an unexpected order (expected config.json first, got \"${files[0].name}\").`,\n );\n }\n // config.json isn't a namespace — count only the entities/*.jsonl shards.\n entitiesTotal = files.filter((f) => f.name !== 'config.json').length;\n tick();\n\n for (const f of files) {\n yield async () => {\n const body = await openWithRetry(\n async () => {\n const url = await manager.getFileUrl(backup.id, f.name, { signal });\n return fetchBody(url, signal);\n },\n {\n signal,\n attempts: retryAttempts,\n delayMs: retryDelayMs,\n describe: (e) => `Failed to fetch ${f.name}: ${errorMessage(e)}.`,\n },\n );\n return {\n name: f.name,\n input: countBytes(body),\n onWriting: () => {\n currentEntity = f.name;\n currentFile = '';\n tick();\n },\n onAdded: () => {\n if (f.name !== 'config.json') entitiesCompleted++;\n tick();\n },\n };\n };\n }\n\n while (true) {\n if (storageError) throw storageError;\n // A caller abort while no fetch is in flight surfaces only in the\n // discovery stream, which swallows it as expected teardown — check\n // explicitly so a cancellation can't read as a complete storage phase\n // with files still undiscovered.\n signal.throwIfAborted();\n let file: AppBackupStorageFile | undefined;\n if (queueHead < queue.length) {\n file = queue[queueHead];\n queue[queueHead] = undefined;\n queueHead++;\n }\n if (file) {\n const storageFile = file;\n const label = storageFile.path || storageFile.locationId;\n yield async () => {\n const body = await openWithRetry(\n () => fetchBody(storageFile.url, signal),\n {\n signal,\n attempts: retryAttempts,\n delayMs: retryDelayMs,\n describe: (e) =>\n `Couldn't download storage file \"${label}\" (${errorMessage(e)}).`,\n },\n );\n return {\n name: `files/${storageFile.locationId}`,\n input: countBytes(body),\n onWriting: () => {\n currentEntity = '';\n currentFile = label;\n tick();\n },\n onAdded: () => {\n filesCompleted++;\n tick();\n },\n };\n };\n } else if (storageDone) {\n break;\n } else {\n await new Promise<void>((resolve) => {\n waitResolve = resolve;\n });\n }\n }\n\n if (storageError) throw storageError;\n })();\n\n const prefetch = finitePositiveInt(opts.prefetch, DEFAULT_PREFETCH);\n const entries = prefetchEntries(thunks, prefetch);\n\n const sinkWriter = opts.sink.getWriter();\n try {\n // Sink the archive encoder writes into: it tallies the encoded size for\n // progress, then forwards to the caller's sink. Awaiting the downstream\n // write propagates backpressure up into the encoder, so a fast source\n // can't outrun the sink and balloon memory.\n const countingSink = new WritableStream<Uint8Array>({\n async write(chunk) {\n zipBytes += chunk.byteLength;\n throttledTick();\n await sinkWriter.write(chunk);\n },\n async close() {\n await sinkWriter.close();\n tick();\n },\n async abort(reason) {\n await sinkWriter.abort(reason);\n },\n });\n\n const writer = await createWriter(countingSink, signal);\n for await (const entry of entries) {\n entry.onWriting();\n await writer.add(entry.name, entry.input, {\n lastModDate: backup.backupAt,\n });\n entry.onAdded();\n }\n currentEntity = '';\n currentFile = '';\n tick();\n // A caller abort that lands after the last entry lets the generator\n // finish cleanly; don't close and return a complete-looking archive.\n signal.throwIfAborted();\n await writer.close();\n await discovery;\n tick();\n return { entities: entitiesCompleted, files: filesCompleted, zipBytes };\n } catch (e) {\n // Tear down the discovery stream and any in-flight body fetches so we\n // don't keep pulling from S3, and abort the caller's sink so it can\n // discard whatever it wrote.\n abortController.abort();\n await sinkWriter.abort(e).catch(() => {});\n throw e;\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"backupDownload.js","sourceRoot":"","sources":["../../src/backupDownload.ts"],"names":[],"mappings":";;AAwOA,sDA8RC;AA1aD,MAAM,sBAAsB,GAAG,CAAC,CAAC;AACjC,MAAM,sBAAsB,GAAG,GAAG,CAAC;AACnC,MAAM,kBAAkB,GAAG,IAAI,CAAC;AAEhC,+EAA+E;AAC/E,4EAA4E;AAC5E,MAAM,iBAAiB,GAAG,CAAC,CAAqB,EAAE,QAAgB,EAAU,EAAE,CAC5E,CAAC,IAAI,IAAI,IAAI,MAAM,CAAC,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC;AAC3D,MAAM,iBAAiB,GAAG,CAAC,CAAqB,EAAE,QAAgB,EAAU,EAAE,CAC5E,CAAC,IAAI,IAAI,IAAI,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC;AAE3D,MAAM,YAAY,GAAG,CAAC,CAAU,EAAW,EAAE,CAC1C,CAAuB,EAAE,IAAI,KAAK,YAAY,CAAC;AAElD,MAAM,YAAY,GAAG,CAAC,CAAU,EAAU,EAAE,CAC1C,CAAC,YAAY,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;AAE7C,4EAA4E;AAC5E,yEAAyE;AACzE,MAAM,KAAK,GAAG,CAAC,EAAU,EAAE,MAAmB,EAAiB,EAAE,CAC/D,IAAI,OAAO,CAAO,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;IACpC,MAAM,OAAO,GAAG,GAAG,EAAE;QACnB,YAAY,C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ACH,KAAK,CAAC,OAAO,EAAE,CAAC;QAClB,CAAC;QACD,aAAa,GAAG,EAAE,CAAC;QACnB,WAAW,GAAG,EAAE,CAAC;QACjB,IAAI,EAAE,CAAC;QACP,oEAAoE;QACpE,qEAAqE;QACrE,MAAM,CAAC,cAAc,EAAE,CAAC;QACxB,MAAM,MAAM,CAAC,KAAK,EAAE,CAAC;QACrB,MAAM,SAAS,CAAC;QAChB,IAAI,EAAE,CAAC;QACP,OAAO,EAAE,QAAQ,EAAE,iBAAiB,EAAE,KAAK,EAAE,cAAc,EAAE,QAAQ,EAAE,CAAC;IAC1E,CAAC;IAAC,OAAO,CAAC,EAAE,CAAC;QACX,sEAAsE;QACtE,oEAAoE;QACpE,6BAA6B;QAC7B,eAAe,CAAC,KAAK,EAAE,CAAC;QACxB,MAAM,UAAU,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,GAAG,EAAE,GAAE,CAAC,CAAC,CAAC;QAC1C,MAAM,CAAC,CAAC;IACV,CAAC;AACH,CAAC","sourcesContent":["import type {\n AppBackup,\n AppBackupStorageFile,\n BackupsManager,\n} from './backups.ts';\n\nexport type BackupDownloadProgress = {\n entitiesCompleted: number;\n entitiesTotal: number | null;\n filesCompleted: number;\n filesTotal: number | null;\n // Compressed bytes written to the sink so far (the zip's on-disk size).\n zipBytes: number;\n // Uncompressed bytes read from source bodies, and the backup's known\n // uncompressed total — the numerator/denominator for a progress bar.\n // bytesTotal is null when the backup row carries no sizes.\n bytesRead: number;\n bytesTotal: number | null;\n // The entry currently being fetched in each phase; empty while that phase\n // isn't actively fetching, so a finished phase stops claiming a file.\n currentEntity: string;\n currentFile: string;\n};\n\nexport type BackupDownloadResult = {\n entities: number;\n files: number;\n zipBytes: number;\n};\n\n/**\n * The archive encoder {@link downloadBackupArchive} writes entries through,\n * supplied by the caller so this package doesn't depend on a zip\n * implementation. zip.js's `ZipWriter` satisfies it structurally, so\n * `new ZipWriter(sink, { zip64: true, signal })` works without an adapter.\n *\n * Implementations must handle archives past 4GB — for zip that means zip64,\n * without which the central-directory offsets wrap and the archive is\n * silently unreadable.\n */\nexport type BackupArchiveWriter = {\n add(\n name: string,\n input: ReadableStream<Uint8Array>,\n opts: { lastModDate: Date },\n ): Promise<unknown>;\n close(): Promise<unknown>;\n};\n\nexport type DownloadBackupArchiveOpts = {\n backup: AppBackup;\n /**\n * Fetches a presigned URL, resolving with the response body and rejecting\n * on a non-200 status. Put the status in the message (e.g. `HTTP 403`) —\n * it's surfaced to the user alongside the failing entry's name. The entity\n * files are served with `Content-Encoding: zstd`; browser fetch decodes\n * that transparently, other runtimes must decompress explicitly.\n */\n fetchBody: (\n url: string,\n signal: AbortSignal,\n ) => Promise<ReadableStream<Uint8Array>>;\n /**\n * Where the archive's bytes go. Closed after the last entry is written;\n * aborted when the download fails or is cancelled, so the caller can\n * discard partial output.\n */\n sink: WritableStream<Uint8Array>;\n /**\n * Builds the archive encoder over a sink that already counts progress and\n * carries the caller's sink's backpressure.\n */\n createWriter: (\n sink: WritableStream<Uint8Array>,\n signal: AbortSignal,\n ) => Promise<BackupArchiveWriter>;\n signal?: AbortSignal;\n onProgress?: (progress: BackupDownloadProgress) => void;\n /**\n * Retry policy for *opening* an entry's body (fetching its presigned URL and\n * getting a live response).\n * `attempts` is the total number of tries (default {@link DEFAULT_FETCH_ATTEMPTS});\n * `delayMs` is the base for exponential backoff (default {@link DEFAULT_RETRY_DELAY_MS}).\n *\n * This only covers failures before the writer starts consuming the body.\n * Once bytes have been written into the archive entry there's no way to\n * restart it without HTTP range/resume support, so a mid-stream failure\n * still fails the download.\n */\n retry?: { attempts?: number; delayMs?: number };\n};\n\nconst DEFAULT_FETCH_ATTEMPTS = 3;\nconst DEFAULT_RETRY_DELAY_MS = 500;\nconst MAX_RETRY_DELAY_MS = 5000;\n\n// Normalize caller-supplied numeric options so NaN/Infinity/non-integers can't\n// alter retry counts or pipeline bounds — fall back to the default instead.\nconst finitePositiveInt = (v: number | undefined, fallback: number): number =>\n v != null && Number.isInteger(v) && v > 0 ? v : fallback;\nconst finiteNonNegative = (v: number | undefined, fallback: number): number =>\n v != null && Number.isFinite(v) && v >= 0 ? v : fallback;\n\nconst isAbortError = (e: unknown): boolean =>\n (e as { name?: string })?.name === 'AbortError';\n\nconst errorMessage = (e: unknown): string =>\n e instanceof Error ? e.message : String(e);\n\n// A cancellable sleep: resolves after `ms`, or rejects if the signal aborts\n// first so backoff between retries doesn't outlive a cancelled download.\nconst delay = (ms: number, signal: AbortSignal): Promise<void> =>\n new Promise<void>((resolve, reject) => {\n const onAbort = () => {\n clearTimeout(timer);\n reject(signal.reason ?? new DOMException('Aborted', 'AbortError'));\n };\n const timer = setTimeout(() => {\n signal.removeEventListener('abort', onAbort);\n resolve();\n }, ms);\n signal.addEventListener('abort', onAbort, { once: true });\n });\n\n// Re-emits an already-read first chunk, then streams the rest from `reader`.\n// Past that first chunk read errors propagate to the consumer unchanged — by\n// then bytes are in the archive entry and it can't be restarted.\nfunction replayFrom(\n first: ReadableStreamReadResult<Uint8Array>,\n reader: ReadableStreamDefaultReader<Uint8Array>,\n): ReadableStream<Uint8Array> {\n let replayed = false;\n return new ReadableStream<Uint8Array>({\n async pull(controller) {\n if (!replayed) {\n replayed = true;\n if (first.done) {\n controller.close();\n return;\n }\n controller.enqueue(first.value);\n return;\n }\n const { done, value } = await reader.read();\n if (done) controller.close();\n else controller.enqueue(value);\n },\n async cancel(reason) {\n await reader.cancel(reason);\n },\n });\n}\n\n/**\n * Opens a body via `open`, retrying on transient failure with abortable\n * exponential backoff. `open` is re-invoked from scratch each attempt, so an\n * entity file re-mints its presigned URL. An abort propagates immediately; any\n * other final failure is wrapped by `describe` into a user-facing message\n * naming the entry.\n *\n * The first chunk is read inside the retry scope, so a body that connects but\n * fails on its first read is re-fetched too, since nothing has been written\n * to the archive yet. Only failures once bytes are flowing are treated as\n * unrecoverable.\n */\nasync function openWithRetry(\n open: () => Promise<ReadableStream<Uint8Array>>,\n opts: {\n signal: AbortSignal;\n attempts: number;\n delayMs: number;\n describe: (e: unknown) => string;\n },\n): Promise<ReadableStream<Uint8Array>> {\n let lastError: unknown;\n for (let attempt = 1; attempt <= opts.attempts; attempt++) {\n opts.signal.throwIfAborted();\n let reader: ReadableStreamDefaultReader<Uint8Array> | undefined;\n try {\n const body = await open();\n reader = body.getReader();\n const first = await reader.read();\n return replayFrom(first, reader);\n } catch (e) {\n // Release the failed connection before retrying (or giving up).\n if (reader) reader.cancel().catch(() => {});\n if (isAbortError(e)) throw e;\n lastError = e;\n if (attempt < opts.attempts) {\n const backoff = Math.min(\n opts.delayMs * 2 ** (attempt - 1),\n MAX_RETRY_DELAY_MS,\n );\n await delay(backoff, opts.signal);\n }\n }\n }\n throw new Error(opts.describe(lastError));\n}\n\n/**\n * One archive entry whose body fetch has been started. `onWriting` runs when\n * the encoder begins consuming it (so progress reflects the entry actually\n * streaming); `onAdded` runs once it's fully written.\n */\ntype PreparedEntry = {\n name: string;\n input: ReadableStream<Uint8Array>;\n onWriting: () => void;\n onAdded: () => void;\n};\n\n/**\n * A thunk that fetches one entry (presigned URL + body) and resolves once the\n * body stream is available — not once it's fully downloaded. Called only when\n * the entry is about to be written, so its body never downloads ahead.\n */\ntype EntryThunk = () => Promise<PreparedEntry>;\n\n/**\n * Downloads a backup into a single archive written to `opts.sink`: entries\n * in the canonical restore order (`config.json`, then the\n * `entities/<etype>.jsonl` shards, then `files/<locationId>` storage blobs —\n * all entity files before any storage file), with the encoder writing\n * through a counting sink that awaits the caller's sink, so a fast source\n * can't outrun it and balloon memory.\n *\n * The runtime-specific pieces are injected: how to fetch a presigned URL\n * (`fetchBody`), where the bytes go (`sink`), and the archive encoder\n * (`createWriter`). Most callers reach this via\n * {@link BackupsManager.downloadArchive}.\n */\nexport async function downloadBackupArchive(\n opts: DownloadBackupArchiveOpts & {\n manager: Pick<\n BackupsManager,\n 'listFiles' | 'getFileUrl' | 'streamStorageFiles'\n >;\n },\n): Promise<BackupDownloadResult> {\n const { manager, backup, fetchBody, createWriter, onProgress } = opts;\n\n // Internal controller so a pipeline failure also tears down the\n // storage-files discovery stream and any in-flight body fetches.\n const abortController = new AbortController();\n if (opts.signal?.aborted) {\n abortController.abort();\n } else {\n opts.signal?.addEventListener('abort', () => abortController.abort(), {\n once: true,\n });\n }\n const signal = abortController.signal;\n\n let entitiesCompleted = 0;\n let entitiesTotal: number | null = null;\n let filesCompleted = 0;\n let filesTotal: number | null = null;\n let zipBytes = 0;\n let bytesRead = 0;\n let currentEntity = '';\n let currentFile = '';\n const bytesTotal =\n backup.uncompressedSize != null\n ? backup.uncompressedSize + (backup.filesSize ?? 0)\n : null;\n\n const retryAttempts = finitePositiveInt(\n opts.retry?.attempts,\n DEFAULT_FETCH_ATTEMPTS,\n );\n const retryDelayMs = finiteNonNegative(\n opts.retry?.delayMs,\n DEFAULT_RETRY_DELAY_MS,\n );\n\n const tick = () =>\n onProgress?.({\n entitiesCompleted,\n entitiesTotal,\n filesCompleted,\n filesTotal,\n zipBytes,\n bytesRead,\n bytesTotal,\n currentEntity,\n currentFile,\n });\n\n // Throttle by time: a large backup pushes many small chunks and ticking on\n // every one is wasted work. Phase changes tick() directly so they're still\n // immediate.\n const TICK_INTERVAL_MS = 100;\n let lastTickAt = 0;\n const throttledTick = () => {\n const now = Date.now();\n if (now - lastTickAt >= TICK_INTERVAL_MS) {\n lastTickAt = now;\n tick();\n }\n };\n\n // Count the uncompressed bytes of a source body for progress as it streams\n // into the archive.\n const countBytes = (\n body: ReadableStream<Uint8Array>,\n ): ReadableStream<Uint8Array> =>\n body.pipeThrough(\n new TransformStream<Uint8Array, Uint8Array>({\n transform(chunk, controller) {\n bytesRead += chunk.byteLength;\n throttledTick();\n controller.enqueue(chunk);\n },\n }),\n );\n\n // Storage-files discovery runs concurrently with the entity phase and is\n // drained eagerly into a queue. That isn't just overlap: it closes the\n // NDJSON connection quickly instead of holding it open (and at the mercy of\n // idle timeouts) while multi-GB blobs download. `queueHead` walks the array\n // in place, freeing each slot as it's consumed.\n const queue: (AppBackupStorageFile | undefined)[] = [];\n let queueHead = 0;\n let storageDone = false;\n let storageError: Error | null = null;\n let waitResolve: (() => void) | null = null;\n const notify = () => {\n const w = waitResolve;\n waitResolve = null;\n w?.();\n };\n\n // Never rejects: failures land in storageError for the drain loop to throw.\n const discovery = (async () => {\n let discoveryComplete = false;\n try {\n for await (const file of manager.streamStorageFiles(backup.id, {\n signal,\n })) {\n queue.push(file);\n filesTotal = (filesTotal ?? 0) + 1;\n throttledTick();\n notify();\n }\n discoveryComplete = true;\n } catch (e) {\n // The abort path is expected when the pipeline failed and we tore the\n // discovery down.\n if (!isAbortError(e)) {\n storageError = e as Error;\n }\n } finally {\n // A failed listing keeps the total unknown rather than reading as an\n // empty-but-complete storage phase.\n if (discoveryComplete && filesTotal == null) filesTotal = 0;\n storageDone = true;\n tick();\n notify();\n }\n })();\n\n // Entry write order is significant for restore: config.json first, then the\n // entities/*.jsonl shards, then files/<locationId>. In particular ALL entity\n // files must be written before ANY storage file. listFiles returns the\n // entity files in write order; this generator yields thunks for them in\n // order, then drains the storage queue. The consumer calls each thunk in turn\n // and writes it to completion before the next, so bodies download one at a\n // time and never ahead of the encoder.\n const thunks = (async function* (): AsyncGenerator<EntryThunk> {\n const files = await manager.listFiles(backup.id, { signal });\n if (files.length === 0) {\n throw new Error('No files found for this backup.');\n }\n // We write entries in the order the server returns them, and restore\n // requires config.json to be the first entry. Fail loudly rather than\n // build a zip that can't be restored.\n if (files[0].name !== 'config.json') {\n throw new Error(\n `Backup files came back in an unexpected order (expected config.json first, got \"${files[0].name}\").`,\n );\n }\n // config.json isn't a namespace — count only the entities/*.jsonl shards.\n entitiesTotal = files.filter((f) => f.name !== 'config.json').length;\n tick();\n\n for (const f of files) {\n yield async () => {\n const body = await openWithRetry(\n async () => {\n const url = await manager.getFileUrl(backup.id, f.name, { signal });\n return fetchBody(url, signal);\n },\n {\n signal,\n attempts: retryAttempts,\n delayMs: retryDelayMs,\n describe: (e) => `Failed to fetch ${f.name}: ${errorMessage(e)}.`,\n },\n );\n return {\n name: f.name,\n input: countBytes(body),\n onWriting: () => {\n currentEntity = f.name;\n currentFile = '';\n tick();\n },\n onAdded: () => {\n if (f.name !== 'config.json') entitiesCompleted++;\n tick();\n },\n };\n };\n }\n\n while (true) {\n if (storageError) throw storageError;\n // A caller abort while no fetch is in flight surfaces only in the\n // discovery stream, which swallows it as expected teardown — check\n // explicitly so a cancellation can't read as a complete storage phase\n // with files still undiscovered.\n signal.throwIfAborted();\n let file: AppBackupStorageFile | undefined;\n if (queueHead < queue.length) {\n file = queue[queueHead];\n queue[queueHead] = undefined;\n queueHead++;\n }\n if (file) {\n const storageFile = file;\n const label = storageFile.path || storageFile.locationId;\n yield async () => {\n const body = await openWithRetry(\n () => fetchBody(storageFile.url, signal),\n {\n signal,\n attempts: retryAttempts,\n delayMs: retryDelayMs,\n describe: (e) =>\n `Couldn't download storage file \"${label}\" (${errorMessage(e)}).`,\n },\n );\n return {\n name: `files/${storageFile.locationId}`,\n input: countBytes(body),\n onWriting: () => {\n currentEntity = '';\n currentFile = label;\n tick();\n },\n onAdded: () => {\n filesCompleted++;\n tick();\n },\n };\n };\n } else if (storageDone) {\n break;\n } else {\n await new Promise<void>((resolve) => {\n waitResolve = resolve;\n });\n }\n }\n\n if (storageError) throw storageError;\n })();\n\n const sinkWriter = opts.sink.getWriter();\n try {\n // Sink the archive encoder writes into: it tallies the encoded size for\n // progress, then forwards to the caller's sink. Awaiting the downstream\n // write propagates backpressure up into the encoder, so a fast source\n // can't outrun the sink and balloon memory.\n const countingSink = new WritableStream<Uint8Array>({\n async write(chunk) {\n zipBytes += chunk.byteLength;\n throttledTick();\n await sinkWriter.write(chunk);\n },\n async close() {\n await sinkWriter.close();\n tick();\n },\n async abort(reason) {\n await sinkWriter.abort(reason);\n },\n });\n\n const writer = await createWriter(countingSink, signal);\n\n for await (const thunk of thunks) {\n const entry = await thunk();\n entry.onWriting();\n await writer.add(entry.name, entry.input, {\n lastModDate: backup.backupAt,\n });\n entry.onAdded();\n }\n currentEntity = '';\n currentFile = '';\n tick();\n // A caller abort that lands after the last entry lets the generator\n // finish cleanly; don't close and return a complete-looking archive.\n signal.throwIfAborted();\n await writer.close();\n await discovery;\n tick();\n return { entities: entitiesCompleted, files: filesCompleted, zipBytes };\n } catch (e) {\n // Tear down the discovery stream and any in-flight body fetches so we\n // don't keep pulling from S3, and abort the caller's sink so it can\n // discard whatever it wrote.\n abortController.abort();\n await sinkWriter.abort(e).catch(() => {});\n throw e;\n }\n}\n"]}
|
|
@@ -54,21 +54,9 @@ export type DownloadBackupArchiveOpts = {
|
|
|
54
54
|
createWriter: (sink: WritableStream<Uint8Array>, signal: AbortSignal) => Promise<BackupArchiveWriter>;
|
|
55
55
|
signal?: AbortSignal;
|
|
56
56
|
onProgress?: (progress: BackupDownloadProgress) => void;
|
|
57
|
-
/**
|
|
58
|
-
* How many entries to fetch ahead of the one currently being written into
|
|
59
|
-
* the archive.
|
|
60
|
-
*
|
|
61
|
-
* Only fetch *initiation* is parallelised, the bodies are still written in
|
|
62
|
-
* order and consumed one at a time, so an in-flight prefetched body buffers
|
|
63
|
-
* only to its stream's high-water mark and memory stays bounded. Defaults to
|
|
64
|
-
* {@link DEFAULT_PREFETCH}.
|
|
65
|
-
*/
|
|
66
|
-
prefetch?: number;
|
|
67
57
|
/**
|
|
68
58
|
* Retry policy for *opening* an entry's body (fetching its presigned URL and
|
|
69
|
-
* getting a live response).
|
|
70
|
-
* while earlier entries write, so a queued connection can be reset before we
|
|
71
|
-
* read it.
|
|
59
|
+
* getting a live response).
|
|
72
60
|
* `attempts` is the total number of tries (default {@link DEFAULT_FETCH_ATTEMPTS});
|
|
73
61
|
* `delayMs` is the base for exponential backoff (default {@link DEFAULT_RETRY_DELAY_MS}).
|
|
74
62
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"backupDownload.d.ts","sourceRoot":"","sources":["../../src/backupDownload.ts"],"names":[],"mappings":"AAAA,OA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IACzC;;;;OAIG;IACH,IAAI,EAAE,cAAc,CAAC,UAAU,CAAC,CAAC;IACjC;;;OAGG;IACH,YAAY,EAAE,CACZ,IAAI,EAAE,cAAc,CAAC,UAAU,CAAC,EAChC,MAAM,EAAE,WAAW,KAChB,OAAO,CAAC,mBAAmB,CAAC,CAAC;IAClC,MAAM,CAAC,EAAE,WAAW,CAAC;IACrB,UAAU,CAAC,EAAE,CAAC,QAAQ,EAAE,sBAAsB,KAAK,IAAI,CAAC;IACxD;;;;;;;;;;OAUG;IACH,KAAK,CAAC,EAAE;QAAE,QAAQ,CAAC,EAAE,MAAM,CAAC;QAAC,OAAO,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC;CACjD,CAAC;AAiIF;;;;;;;;;;;;GAYG;AACH,wBAAsB,qBAAqB,CACzC,IAAI,EAAE,yBAAyB,GAAG;IAChC,OAAO,EAAE,IAAI,CACX,cAAc,EACd,WAAW,GAAG,YAAY,GAAG,oBAAoB,CAClD,CAAC;CACH,GACA,OAAO,CAAC,oBAAoB,CAAC,CAuR/B"}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
const DEFAULT_PREFETCH = 4;
|
|
2
1
|
const DEFAULT_FETCH_ATTEMPTS = 3;
|
|
3
2
|
const DEFAULT_RETRY_DELAY_MS = 500;
|
|
4
3
|
const MAX_RETRY_DELAY_MS = 5000;
|
|
@@ -56,9 +55,9 @@ function replayFrom(first, reader) {
|
|
|
56
55
|
* naming the entry.
|
|
57
56
|
*
|
|
58
57
|
* The first chunk is read inside the retry scope, so a body that connects but
|
|
59
|
-
* fails on its first read
|
|
60
|
-
*
|
|
61
|
-
*
|
|
58
|
+
* fails on its first read is re-fetched too, since nothing has been written
|
|
59
|
+
* to the archive yet. Only failures once bytes are flowing are treated as
|
|
60
|
+
* unrecoverable.
|
|
62
61
|
*/
|
|
63
62
|
async function openWithRetry(open, opts) {
|
|
64
63
|
let lastError;
|
|
@@ -86,80 +85,6 @@ async function openWithRetry(open, opts) {
|
|
|
86
85
|
}
|
|
87
86
|
throw new Error(opts.describe(lastError));
|
|
88
87
|
}
|
|
89
|
-
/**
|
|
90
|
-
* Wraps an ordered stream of entry thunks, keeping up to `lookahead` fetches
|
|
91
|
-
* in flight while yielding the prepared entries in their original order. A
|
|
92
|
-
* background producer pulls thunks and starts their fetches as space frees up;
|
|
93
|
-
* the consumer awaits each in turn. Preserves order and backpressure: at most
|
|
94
|
-
* `lookahead` bodies are ever in flight, and the producer parks when the
|
|
95
|
-
* pipeline is full or the source is waiting for more work.
|
|
96
|
-
*/
|
|
97
|
-
async function* prefetchEntries(thunks, lookahead) {
|
|
98
|
-
const pipeline = [];
|
|
99
|
-
const state = { done: false, producerError: null, onItem: null, onSpace: null };
|
|
100
|
-
const wakeItem = () => {
|
|
101
|
-
const w = state.onItem;
|
|
102
|
-
state.onItem = null;
|
|
103
|
-
if (w)
|
|
104
|
-
w();
|
|
105
|
-
};
|
|
106
|
-
const wakeSpace = () => {
|
|
107
|
-
const w = state.onSpace;
|
|
108
|
-
state.onSpace = null;
|
|
109
|
-
if (w)
|
|
110
|
-
w();
|
|
111
|
-
};
|
|
112
|
-
// Never rejects: a failure to produce the next thunk (or start its fetch)
|
|
113
|
-
// lands in state.producerError for the consumer to throw in order.
|
|
114
|
-
const producer = (async () => {
|
|
115
|
-
try {
|
|
116
|
-
for await (const thunk of thunks) {
|
|
117
|
-
while (pipeline.length >= lookahead) {
|
|
118
|
-
await new Promise((resolve) => {
|
|
119
|
-
state.onSpace = resolve;
|
|
120
|
-
});
|
|
121
|
-
}
|
|
122
|
-
const started = thunk();
|
|
123
|
-
// The consumer awaits `started` in order; attach a no-op catch so a
|
|
124
|
-
// fetch that rejects before then isn't reported as unhandled.
|
|
125
|
-
started.catch(() => { });
|
|
126
|
-
pipeline.push(started);
|
|
127
|
-
wakeItem();
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
catch (e) {
|
|
131
|
-
state.producerError = e;
|
|
132
|
-
}
|
|
133
|
-
finally {
|
|
134
|
-
state.done = true;
|
|
135
|
-
wakeItem();
|
|
136
|
-
}
|
|
137
|
-
})();
|
|
138
|
-
try {
|
|
139
|
-
while (true) {
|
|
140
|
-
if (pipeline.length === 0) {
|
|
141
|
-
if (state.done) {
|
|
142
|
-
if (state.producerError)
|
|
143
|
-
throw state.producerError;
|
|
144
|
-
break;
|
|
145
|
-
}
|
|
146
|
-
await new Promise((resolve) => {
|
|
147
|
-
state.onItem = resolve;
|
|
148
|
-
});
|
|
149
|
-
continue;
|
|
150
|
-
}
|
|
151
|
-
const entry = await pipeline.shift();
|
|
152
|
-
wakeSpace();
|
|
153
|
-
yield entry;
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
finally {
|
|
157
|
-
// On early exit (abort/error), let the producer unwind — its own signal
|
|
158
|
-
// teardown resolves the source's waits — without blocking here.
|
|
159
|
-
wakeSpace();
|
|
160
|
-
producer.catch(() => { });
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
88
|
/**
|
|
164
89
|
* Downloads a backup into a single archive written to `opts.sink`: entries
|
|
165
90
|
* in the canonical restore order (`config.json`, then the
|
|
@@ -282,9 +207,9 @@ export async function downloadBackupArchive(opts) {
|
|
|
282
207
|
// entities/*.jsonl shards, then files/<locationId>. In particular ALL entity
|
|
283
208
|
// files must be written before ANY storage file. listFiles returns the
|
|
284
209
|
// entity files in write order; this generator yields thunks for them in
|
|
285
|
-
// order, then drains the storage queue. The
|
|
286
|
-
//
|
|
287
|
-
//
|
|
210
|
+
// order, then drains the storage queue. The consumer calls each thunk in turn
|
|
211
|
+
// and writes it to completion before the next, so bodies download one at a
|
|
212
|
+
// time and never ahead of the encoder.
|
|
288
213
|
const thunks = (async function* () {
|
|
289
214
|
const files = await manager.listFiles(backup.id, { signal });
|
|
290
215
|
if (files.length === 0) {
|
|
@@ -377,8 +302,6 @@ export async function downloadBackupArchive(opts) {
|
|
|
377
302
|
if (storageError)
|
|
378
303
|
throw storageError;
|
|
379
304
|
})();
|
|
380
|
-
const prefetch = finitePositiveInt(opts.prefetch, DEFAULT_PREFETCH);
|
|
381
|
-
const entries = prefetchEntries(thunks, prefetch);
|
|
382
305
|
const sinkWriter = opts.sink.getWriter();
|
|
383
306
|
try {
|
|
384
307
|
// Sink the archive encoder writes into: it tallies the encoded size for
|
|
@@ -400,7 +323,8 @@ export async function downloadBackupArchive(opts) {
|
|
|
400
323
|
},
|
|
401
324
|
});
|
|
402
325
|
const writer = await createWriter(countingSink, signal);
|
|
403
|
-
for await (const
|
|
326
|
+
for await (const thunk of thunks) {
|
|
327
|
+
const entry = await thunk();
|
|
404
328
|
entry.onWriting();
|
|
405
329
|
await writer.add(entry.name, entry.input, {
|
|
406
330
|
lastModDate: backup.backupAt,
|