@sidekickdb/platform 0.0.0-stage → 2.0.0
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/.tshy/build.json +8 -0
- package/.tshy/commonjs.json +16 -0
- package/.tshy/esm.json +15 -0
- package/.turbo/turbo-build.log +50 -0
- package/LICENSE.md +202 -0
- package/NOTICE +27 -0
- package/README.md +445 -2
- package/__tests__/src/apiURI.test.ts +58 -0
- package/__tests__/src/backupDownload.test.ts +427 -0
- package/__tests__/src/backups.test.ts +168 -0
- package/__tests__/src/clerk.test.ts +13 -0
- package/__tests__/src/consts.test.ts +11 -0
- package/__tests__/src/migrations.test.ts +885 -0
- package/__tests__/src/perms.test.ts +102 -0
- package/__tests__/src/rename.test.ts +126 -0
- package/__tests__/src/schema.push.e2e.test.ts +186 -0
- package/__tests__/src/schema.test.ts +644 -0
- package/__tests__/src/typescript-schema.test.ts +117 -0
- package/dist/commonjs/ProgressPromise.d.ts +18 -0
- package/dist/commonjs/ProgressPromise.d.ts.map +1 -0
- package/dist/commonjs/ProgressPromise.js +77 -0
- package/dist/commonjs/ProgressPromise.js.map +1 -0
- package/dist/commonjs/api.d.ts +480 -0
- package/dist/commonjs/api.d.ts.map +1 -0
- package/dist/commonjs/api.js +1132 -0
- package/dist/commonjs/api.js.map +1 -0
- package/dist/commonjs/backupDownload.d.ts +89 -0
- package/dist/commonjs/backupDownload.d.ts.map +1 -0
- package/dist/commonjs/backupDownload.js +357 -0
- package/dist/commonjs/backupDownload.js.map +1 -0
- package/dist/commonjs/backups.d.ts +146 -0
- package/dist/commonjs/backups.d.ts.map +1 -0
- package/dist/commonjs/backups.js +248 -0
- package/dist/commonjs/backups.js.map +1 -0
- package/dist/commonjs/clerk.d.ts +2 -0
- package/dist/commonjs/clerk.d.ts.map +1 -0
- package/dist/commonjs/clerk.js +29 -0
- package/dist/commonjs/clerk.js.map +1 -0
- package/dist/commonjs/consts.d.ts +11 -0
- package/dist/commonjs/consts.d.ts.map +1 -0
- package/dist/commonjs/consts.js +17 -0
- package/dist/commonjs/consts.js.map +1 -0
- package/dist/commonjs/crypto.d.ts +4 -0
- package/dist/commonjs/crypto.d.ts.map +1 -0
- package/dist/commonjs/crypto.js +26 -0
- package/dist/commonjs/crypto.js.map +1 -0
- package/dist/commonjs/index.d.ts +18 -0
- package/dist/commonjs/index.d.ts.map +1 -0
- package/dist/commonjs/index.js +61 -0
- package/dist/commonjs/index.js.map +1 -0
- package/dist/commonjs/migrationUtils.d.ts +4 -0
- package/dist/commonjs/migrationUtils.d.ts.map +1 -0
- package/dist/commonjs/migrationUtils.js +52 -0
- package/dist/commonjs/migrationUtils.js.map +1 -0
- package/dist/commonjs/migrations.d.ts +99 -0
- package/dist/commonjs/migrations.d.ts.map +1 -0
- package/dist/commonjs/migrations.js +597 -0
- package/dist/commonjs/migrations.js.map +1 -0
- package/dist/commonjs/oauth.d.ts +98 -0
- package/dist/commonjs/oauth.d.ts.map +1 -0
- package/dist/commonjs/oauth.js +277 -0
- package/dist/commonjs/oauth.js.map +1 -0
- package/dist/commonjs/oauthCommon.d.ts +12 -0
- package/dist/commonjs/oauthCommon.d.ts.map +1 -0
- package/dist/commonjs/oauthCommon.js +26 -0
- package/dist/commonjs/oauthCommon.js.map +1 -0
- package/dist/commonjs/package.json +3 -0
- package/dist/commonjs/perms.d.ts +3 -0
- package/dist/commonjs/perms.d.ts.map +1 -0
- package/dist/commonjs/perms.js +66 -0
- package/dist/commonjs/perms.js.map +1 -0
- package/dist/commonjs/relationships.d.ts +7 -0
- package/dist/commonjs/relationships.d.ts.map +1 -0
- package/dist/commonjs/relationships.js +46 -0
- package/dist/commonjs/relationships.js.map +1 -0
- package/dist/commonjs/schema.d.ts +161 -0
- package/dist/commonjs/schema.d.ts.map +1 -0
- package/dist/commonjs/schema.js +174 -0
- package/dist/commonjs/schema.js.map +1 -0
- package/dist/commonjs/serverOAuth.d.ts +25 -0
- package/dist/commonjs/serverOAuth.d.ts.map +1 -0
- package/dist/commonjs/serverOAuth.js +72 -0
- package/dist/commonjs/serverOAuth.js.map +1 -0
- package/dist/commonjs/typescript-schema.d.ts +17 -0
- package/dist/commonjs/typescript-schema.d.ts.map +1 -0
- package/dist/commonjs/typescript-schema.js +314 -0
- package/dist/commonjs/typescript-schema.js.map +1 -0
- package/dist/commonjs/util.d.ts +13 -0
- package/dist/commonjs/util.d.ts.map +1 -0
- package/dist/commonjs/util.js +55 -0
- package/dist/commonjs/util.js.map +1 -0
- package/dist/commonjs/version.d.ts +3 -0
- package/dist/commonjs/version.d.ts.map +1 -0
- package/dist/commonjs/version.js +5 -0
- package/dist/commonjs/version.js.map +1 -0
- package/dist/esm/ProgressPromise.d.ts +18 -0
- package/dist/esm/ProgressPromise.d.ts.map +1 -0
- package/dist/esm/ProgressPromise.js +73 -0
- package/dist/esm/ProgressPromise.js.map +1 -0
- package/dist/esm/api.d.ts +480 -0
- package/dist/esm/api.d.ts.map +1 -0
- package/dist/esm/api.js +1121 -0
- package/dist/esm/api.js.map +1 -0
- package/dist/esm/backupDownload.d.ts +89 -0
- package/dist/esm/backupDownload.d.ts.map +1 -0
- package/dist/esm/backupDownload.js +354 -0
- package/dist/esm/backupDownload.js.map +1 -0
- package/dist/esm/backups.d.ts +146 -0
- package/dist/esm/backups.d.ts.map +1 -0
- package/dist/esm/backups.js +237 -0
- package/dist/esm/backups.js.map +1 -0
- package/dist/esm/clerk.d.ts +2 -0
- package/dist/esm/clerk.d.ts.map +1 -0
- package/dist/esm/clerk.js +26 -0
- package/dist/esm/clerk.js.map +1 -0
- package/dist/esm/consts.d.ts +11 -0
- package/dist/esm/consts.d.ts.map +1 -0
- package/dist/esm/consts.js +13 -0
- package/dist/esm/consts.js.map +1 -0
- package/dist/esm/crypto.d.ts +4 -0
- package/dist/esm/crypto.d.ts.map +1 -0
- package/dist/esm/crypto.js +21 -0
- package/dist/esm/crypto.js.map +1 -0
- package/dist/esm/index.d.ts +18 -0
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +17 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/migrationUtils.d.ts +4 -0
- package/dist/esm/migrationUtils.d.ts.map +1 -0
- package/dist/esm/migrationUtils.js +47 -0
- package/dist/esm/migrationUtils.js.map +1 -0
- package/dist/esm/migrations.d.ts +99 -0
- package/dist/esm/migrations.d.ts.map +1 -0
- package/dist/esm/migrations.js +588 -0
- package/dist/esm/migrations.js.map +1 -0
- package/dist/esm/oauth.d.ts +98 -0
- package/dist/esm/oauth.d.ts.map +1 -0
- package/dist/esm/oauth.js +268 -0
- package/dist/esm/oauth.js.map +1 -0
- package/dist/esm/oauthCommon.d.ts +12 -0
- package/dist/esm/oauthCommon.d.ts.map +1 -0
- package/dist/esm/oauthCommon.js +22 -0
- package/dist/esm/oauthCommon.js.map +1 -0
- package/dist/esm/package.json +3 -0
- package/dist/esm/perms.d.ts +3 -0
- package/dist/esm/perms.d.ts.map +1 -0
- package/dist/esm/perms.js +62 -0
- package/dist/esm/perms.js.map +1 -0
- package/dist/esm/relationships.d.ts +7 -0
- package/dist/esm/relationships.d.ts.map +1 -0
- package/dist/esm/relationships.js +43 -0
- package/dist/esm/relationships.js.map +1 -0
- package/dist/esm/schema.d.ts +161 -0
- package/dist/esm/schema.d.ts.map +1 -0
- package/dist/esm/schema.js +158 -0
- package/dist/esm/schema.js.map +1 -0
- package/dist/esm/serverOAuth.d.ts +25 -0
- package/dist/esm/serverOAuth.d.ts.map +1 -0
- package/dist/esm/serverOAuth.js +68 -0
- package/dist/esm/serverOAuth.js.map +1 -0
- package/dist/esm/typescript-schema.d.ts +17 -0
- package/dist/esm/typescript-schema.d.ts.map +1 -0
- package/dist/esm/typescript-schema.js +310 -0
- package/dist/esm/typescript-schema.js.map +1 -0
- package/dist/esm/util.d.ts +13 -0
- package/dist/esm/util.d.ts.map +1 -0
- package/dist/esm/util.js +46 -0
- package/dist/esm/util.js.map +1 -0
- package/dist/esm/version.d.ts +3 -0
- package/dist/esm/version.d.ts.map +1 -0
- package/dist/esm/version.js +3 -0
- package/dist/esm/version.js.map +1 -0
- package/dist/standalone/index.js +13747 -0
- package/dist/standalone/index.umd.cjs +72 -0
- package/package.json +74 -3
- package/src/ProgressPromise.ts +97 -0
- package/src/api.ts +1903 -0
- package/src/backupDownload.ts +519 -0
- package/src/backups.ts +342 -0
- package/src/clerk.ts +24 -0
- package/src/consts.ts +22 -0
- package/src/crypto.ts +28 -0
- package/src/index.ts +147 -0
- package/src/migrationUtils.ts +56 -0
- package/src/migrations.ts +920 -0
- package/src/oauth.ts +390 -0
- package/src/oauthCommon.ts +38 -0
- package/src/perms.ts +81 -0
- package/src/relationships.ts +57 -0
- package/src/schema.ts +497 -0
- package/src/serverOAuth.ts +119 -0
- package/src/typescript-schema.ts +439 -0
- package/src/util.ts +77 -0
- package/src/version.ts +3 -0
- package/tsconfig.dev.json +14 -0
- package/tsconfig.json +14 -0
- package/vite.config.ts +19 -0
|
@@ -0,0 +1,427 @@
|
|
|
1
|
+
import { describe, expect, test } from 'vitest';
|
|
2
|
+
import { downloadBackupArchive } from '../../src/backupDownload.ts';
|
|
3
|
+
import type { AppBackup } from '../../src/backups.ts';
|
|
4
|
+
|
|
5
|
+
const backup: AppBackup = {
|
|
6
|
+
id: 'backup-1',
|
|
7
|
+
isn: '1',
|
|
8
|
+
backupAt: new Date('2026-08-01T00:00:00Z'),
|
|
9
|
+
filesSize: 16,
|
|
10
|
+
dbSize: 100,
|
|
11
|
+
uncompressedSize: 40,
|
|
12
|
+
description: null,
|
|
13
|
+
expiresAt: null,
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
const bodyOf = (text: string) =>
|
|
17
|
+
new ReadableStream<Uint8Array>({
|
|
18
|
+
start(controller) {
|
|
19
|
+
controller.enqueue(new TextEncoder().encode(text));
|
|
20
|
+
controller.close();
|
|
21
|
+
},
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
const fetchBody = async (url: string) => bodyOf(`body:${url}`);
|
|
25
|
+
|
|
26
|
+
const listFiles = async () => [
|
|
27
|
+
{ name: 'config.json', size: 1 },
|
|
28
|
+
{ name: 'entities/todos.jsonl', size: 1 },
|
|
29
|
+
];
|
|
30
|
+
const getFileUrl = async (_backupId: string, name: string) => name;
|
|
31
|
+
|
|
32
|
+
// Minimal archive writer: records entry names, drains each input, and writes
|
|
33
|
+
// a byte through the counting sink so zipBytes moves like a real encoder.
|
|
34
|
+
const makeWriter =
|
|
35
|
+
(names: string[]) => async (sink: WritableStream<Uint8Array>) => {
|
|
36
|
+
const w = sink.getWriter();
|
|
37
|
+
return {
|
|
38
|
+
add: async (name: string, input: ReadableStream<Uint8Array>) => {
|
|
39
|
+
names.push(name);
|
|
40
|
+
for await (const _chunk of input) {
|
|
41
|
+
// drain
|
|
42
|
+
}
|
|
43
|
+
await w.write(new Uint8Array([0]));
|
|
44
|
+
},
|
|
45
|
+
close: () => w.close(),
|
|
46
|
+
};
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
const nullSink = () => new WritableStream<Uint8Array>({ write() {} });
|
|
50
|
+
|
|
51
|
+
describe('downloadBackupArchive', () => {
|
|
52
|
+
test('writes entries in canonical order through the injected writer', async () => {
|
|
53
|
+
const names: string[] = [];
|
|
54
|
+
const manager = {
|
|
55
|
+
listFiles,
|
|
56
|
+
getFileUrl,
|
|
57
|
+
streamStorageFiles: async function* () {
|
|
58
|
+
yield { locationId: 'loc-1', path: 'a.png', url: 'loc-1-url' };
|
|
59
|
+
},
|
|
60
|
+
} as any;
|
|
61
|
+
|
|
62
|
+
const result = await downloadBackupArchive({
|
|
63
|
+
manager,
|
|
64
|
+
backup,
|
|
65
|
+
fetchBody,
|
|
66
|
+
sink: nullSink(),
|
|
67
|
+
createWriter: makeWriter(names),
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
expect(names).toEqual([
|
|
71
|
+
'config.json',
|
|
72
|
+
'entities/todos.jsonl',
|
|
73
|
+
'files/loc-1',
|
|
74
|
+
]);
|
|
75
|
+
expect(result.entities).toBe(1);
|
|
76
|
+
expect(result.files).toBe(1);
|
|
77
|
+
expect(result.zipBytes).toBe(3);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
test('counts an entry as completed only after the writer consumed it', async () => {
|
|
81
|
+
const seen: Array<{
|
|
82
|
+
name: string;
|
|
83
|
+
entitiesCompleted: number;
|
|
84
|
+
filesCompleted: number;
|
|
85
|
+
}> = [];
|
|
86
|
+
let last = { entitiesCompleted: 0, filesCompleted: 0 };
|
|
87
|
+
const createWriter = async (sink: WritableStream<Uint8Array>) => {
|
|
88
|
+
const w = sink.getWriter();
|
|
89
|
+
return {
|
|
90
|
+
add: async (name: string, input: ReadableStream<Uint8Array>) => {
|
|
91
|
+
seen.push({ name, ...last });
|
|
92
|
+
for await (const _chunk of input) {
|
|
93
|
+
// drain
|
|
94
|
+
}
|
|
95
|
+
await w.write(new Uint8Array([0]));
|
|
96
|
+
},
|
|
97
|
+
close: () => w.close(),
|
|
98
|
+
};
|
|
99
|
+
};
|
|
100
|
+
const manager = {
|
|
101
|
+
listFiles,
|
|
102
|
+
getFileUrl,
|
|
103
|
+
streamStorageFiles: async function* () {
|
|
104
|
+
yield { locationId: 'loc-1', path: 'a.png', url: 'loc-1-url' };
|
|
105
|
+
},
|
|
106
|
+
} as any;
|
|
107
|
+
|
|
108
|
+
await downloadBackupArchive({
|
|
109
|
+
manager,
|
|
110
|
+
backup,
|
|
111
|
+
fetchBody,
|
|
112
|
+
sink: nullSink(),
|
|
113
|
+
createWriter,
|
|
114
|
+
onProgress: (p) => {
|
|
115
|
+
last = {
|
|
116
|
+
entitiesCompleted: p.entitiesCompleted,
|
|
117
|
+
filesCompleted: p.filesCompleted,
|
|
118
|
+
};
|
|
119
|
+
},
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
// At the moment each add starts, the entry being written isn't counted.
|
|
123
|
+
expect(seen).toEqual([
|
|
124
|
+
{ name: 'config.json', entitiesCompleted: 0, filesCompleted: 0 },
|
|
125
|
+
{ name: 'entities/todos.jsonl', entitiesCompleted: 0, filesCompleted: 0 },
|
|
126
|
+
{ name: 'files/loc-1', entitiesCompleted: 1, filesCompleted: 0 },
|
|
127
|
+
]);
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
test('a cancellation while storage discovery is idle rejects instead of completing', async () => {
|
|
131
|
+
const controller = new AbortController();
|
|
132
|
+
const manager = {
|
|
133
|
+
listFiles,
|
|
134
|
+
getFileUrl,
|
|
135
|
+
// Yields one file, then holds the stream open like a server with more
|
|
136
|
+
// to send, rejecting with AbortError when the download is cancelled —
|
|
137
|
+
// the same shape as a real aborted fetch.
|
|
138
|
+
streamStorageFiles: (
|
|
139
|
+
_backupId: string,
|
|
140
|
+
opts?: { signal?: AbortSignal },
|
|
141
|
+
) =>
|
|
142
|
+
(async function* () {
|
|
143
|
+
yield { locationId: 'loc-1', path: 'a.png', url: 'loc-1-url' };
|
|
144
|
+
yield await new Promise<never>((_resolve, reject) => {
|
|
145
|
+
opts?.signal?.addEventListener('abort', () =>
|
|
146
|
+
reject(
|
|
147
|
+
Object.assign(new Error('aborted'), { name: 'AbortError' }),
|
|
148
|
+
),
|
|
149
|
+
);
|
|
150
|
+
});
|
|
151
|
+
})(),
|
|
152
|
+
} as any;
|
|
153
|
+
|
|
154
|
+
const promise = downloadBackupArchive({
|
|
155
|
+
manager,
|
|
156
|
+
backup,
|
|
157
|
+
fetchBody,
|
|
158
|
+
sink: nullSink(),
|
|
159
|
+
createWriter: makeWriter([]),
|
|
160
|
+
signal: controller.signal,
|
|
161
|
+
onProgress: (p) => {
|
|
162
|
+
// Cancel once the only discovered file is fully written and the
|
|
163
|
+
// drain loop is about to go idle.
|
|
164
|
+
if (p.filesCompleted === 1) controller.abort();
|
|
165
|
+
},
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
await expect(promise).rejects.toMatchObject({ name: 'AbortError' });
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
test('fetches each entry body only when the writer reaches it, never ahead', async () => {
|
|
172
|
+
const files = [
|
|
173
|
+
{ name: 'config.json', size: 1 },
|
|
174
|
+
{ name: 'entities/a.jsonl', size: 1 },
|
|
175
|
+
{ name: 'entities/b.jsonl', size: 1 },
|
|
176
|
+
];
|
|
177
|
+
const started: string[] = [];
|
|
178
|
+
|
|
179
|
+
const manager = {
|
|
180
|
+
listFiles: async () => files,
|
|
181
|
+
getFileUrl,
|
|
182
|
+
streamStorageFiles: async function* () {},
|
|
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.
|
|
186
|
+
const trackingFetch = async (url: string) => {
|
|
187
|
+
started.push(url);
|
|
188
|
+
return bodyOf(`body:${url}`);
|
|
189
|
+
};
|
|
190
|
+
|
|
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;
|
|
207
|
+
const createWriter = async (sink: WritableStream<Uint8Array>) => {
|
|
208
|
+
const w = sink.getWriter();
|
|
209
|
+
return {
|
|
210
|
+
add: async (_name: string, input: ReadableStream<Uint8Array>) => {
|
|
211
|
+
const i = addCount++;
|
|
212
|
+
began[i].resolve();
|
|
213
|
+
await release[i].promise;
|
|
214
|
+
for await (const _chunk of input) {
|
|
215
|
+
// drain
|
|
216
|
+
}
|
|
217
|
+
await w.write(new Uint8Array([0]));
|
|
218
|
+
},
|
|
219
|
+
close: () => w.close(),
|
|
220
|
+
};
|
|
221
|
+
};
|
|
222
|
+
|
|
223
|
+
const done = downloadBackupArchive({
|
|
224
|
+
manager,
|
|
225
|
+
backup,
|
|
226
|
+
fetchBody: trackingFetch,
|
|
227
|
+
sink: nullSink(),
|
|
228
|
+
createWriter,
|
|
229
|
+
});
|
|
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
|
+
|
|
242
|
+
expect(started).toEqual([
|
|
243
|
+
'config.json',
|
|
244
|
+
'entities/a.jsonl',
|
|
245
|
+
'entities/b.jsonl',
|
|
246
|
+
]);
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
test('retries a transient failure opening an entry body', async () => {
|
|
250
|
+
const names: string[] = [];
|
|
251
|
+
const manager = {
|
|
252
|
+
listFiles,
|
|
253
|
+
getFileUrl,
|
|
254
|
+
streamStorageFiles: async function* () {
|
|
255
|
+
yield { locationId: 'loc-1', path: 'a.png', url: 'loc-1-url' };
|
|
256
|
+
},
|
|
257
|
+
} as any;
|
|
258
|
+
|
|
259
|
+
// The storage blob's first open fails once, then succeeds.
|
|
260
|
+
let storageAttempts = 0;
|
|
261
|
+
const flakyFetch = async (url: string) => {
|
|
262
|
+
if (url === 'loc-1-url') {
|
|
263
|
+
storageAttempts++;
|
|
264
|
+
if (storageAttempts === 1) throw new Error('ECONNRESET');
|
|
265
|
+
}
|
|
266
|
+
return bodyOf(`body:${url}`);
|
|
267
|
+
};
|
|
268
|
+
|
|
269
|
+
const result = await downloadBackupArchive({
|
|
270
|
+
manager,
|
|
271
|
+
backup,
|
|
272
|
+
fetchBody: flakyFetch,
|
|
273
|
+
sink: nullSink(),
|
|
274
|
+
createWriter: makeWriter(names),
|
|
275
|
+
retry: { attempts: 3, delayMs: 0 },
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
expect(storageAttempts).toBe(2);
|
|
279
|
+
expect(names).toEqual([
|
|
280
|
+
'config.json',
|
|
281
|
+
'entities/todos.jsonl',
|
|
282
|
+
'files/loc-1',
|
|
283
|
+
]);
|
|
284
|
+
expect(result.files).toBe(1);
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
test('retries a body that connects but errors on its first read', async () => {
|
|
288
|
+
const names: string[] = [];
|
|
289
|
+
const manager = {
|
|
290
|
+
listFiles,
|
|
291
|
+
getFileUrl,
|
|
292
|
+
streamStorageFiles: async function* () {
|
|
293
|
+
yield { locationId: 'loc-1', path: 'a.png', url: 'loc-1-url' };
|
|
294
|
+
},
|
|
295
|
+
} as any;
|
|
296
|
+
|
|
297
|
+
// fetchBody resolves (connection established) but the body errors on its
|
|
298
|
+
// first read the first time — the shape of a reset idle connection.
|
|
299
|
+
let storageAttempts = 0;
|
|
300
|
+
const flakyFetch = async (url: string) => {
|
|
301
|
+
if (url === 'loc-1-url') {
|
|
302
|
+
storageAttempts++;
|
|
303
|
+
if (storageAttempts === 1) {
|
|
304
|
+
return new ReadableStream<Uint8Array>({
|
|
305
|
+
pull(controller) {
|
|
306
|
+
controller.error(new Error('ECONNRESET'));
|
|
307
|
+
},
|
|
308
|
+
});
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
return bodyOf(`body:${url}`);
|
|
312
|
+
};
|
|
313
|
+
|
|
314
|
+
const result = await downloadBackupArchive({
|
|
315
|
+
manager,
|
|
316
|
+
backup,
|
|
317
|
+
fetchBody: flakyFetch,
|
|
318
|
+
sink: nullSink(),
|
|
319
|
+
createWriter: makeWriter(names),
|
|
320
|
+
retry: { attempts: 3, delayMs: 0 },
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
expect(storageAttempts).toBe(2);
|
|
324
|
+
expect(names).toEqual([
|
|
325
|
+
'config.json',
|
|
326
|
+
'entities/todos.jsonl',
|
|
327
|
+
'files/loc-1',
|
|
328
|
+
]);
|
|
329
|
+
expect(result.files).toBe(1);
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
test('does not retry once the body has yielded a chunk', async () => {
|
|
333
|
+
const manager = {
|
|
334
|
+
listFiles,
|
|
335
|
+
getFileUrl,
|
|
336
|
+
streamStorageFiles: async function* () {
|
|
337
|
+
yield { locationId: 'loc-1', path: 'a.png', url: 'loc-1-url' };
|
|
338
|
+
},
|
|
339
|
+
} as any;
|
|
340
|
+
|
|
341
|
+
// The body delivers one chunk on the first read, then errors on the next —
|
|
342
|
+
// a mid-stream failure. Once bytes are flowing the entry can't be
|
|
343
|
+
// restarted, so this must not retry.
|
|
344
|
+
let storageAttempts = 0;
|
|
345
|
+
const fetchBody = async (url: string) => {
|
|
346
|
+
if (url === 'loc-1-url') {
|
|
347
|
+
storageAttempts++;
|
|
348
|
+
let phase = 0;
|
|
349
|
+
return new ReadableStream<Uint8Array>({
|
|
350
|
+
pull(controller) {
|
|
351
|
+
if (phase++ === 0) {
|
|
352
|
+
controller.enqueue(new TextEncoder().encode('partial'));
|
|
353
|
+
} else {
|
|
354
|
+
controller.error(new Error('mid-stream reset'));
|
|
355
|
+
}
|
|
356
|
+
},
|
|
357
|
+
});
|
|
358
|
+
}
|
|
359
|
+
return bodyOf(`body:${url}`);
|
|
360
|
+
};
|
|
361
|
+
|
|
362
|
+
await expect(
|
|
363
|
+
downloadBackupArchive({
|
|
364
|
+
manager,
|
|
365
|
+
backup,
|
|
366
|
+
fetchBody,
|
|
367
|
+
sink: nullSink(),
|
|
368
|
+
createWriter: makeWriter([]),
|
|
369
|
+
retry: { attempts: 3, delayMs: 0 },
|
|
370
|
+
}),
|
|
371
|
+
).rejects.toThrow();
|
|
372
|
+
expect(storageAttempts).toBe(1);
|
|
373
|
+
});
|
|
374
|
+
|
|
375
|
+
test('gives up after exhausting retries and names the failing entry', async () => {
|
|
376
|
+
const manager = {
|
|
377
|
+
listFiles,
|
|
378
|
+
getFileUrl,
|
|
379
|
+
streamStorageFiles: async function* () {
|
|
380
|
+
yield { locationId: 'loc-9', path: null, url: 'bad-url' };
|
|
381
|
+
},
|
|
382
|
+
} as any;
|
|
383
|
+
|
|
384
|
+
let attempts = 0;
|
|
385
|
+
await expect(
|
|
386
|
+
downloadBackupArchive({
|
|
387
|
+
manager,
|
|
388
|
+
backup,
|
|
389
|
+
fetchBody: async (url: string) => {
|
|
390
|
+
if (url === 'bad-url') {
|
|
391
|
+
attempts++;
|
|
392
|
+
throw new Error('HTTP 500');
|
|
393
|
+
}
|
|
394
|
+
return bodyOf('x');
|
|
395
|
+
},
|
|
396
|
+
sink: nullSink(),
|
|
397
|
+
createWriter: makeWriter([]),
|
|
398
|
+
retry: { attempts: 3, delayMs: 0 },
|
|
399
|
+
}),
|
|
400
|
+
).rejects.toThrow('Couldn\'t download storage file "loc-9" (HTTP 500).');
|
|
401
|
+
expect(attempts).toBe(3);
|
|
402
|
+
});
|
|
403
|
+
|
|
404
|
+
test('names a pathless storage file by locationId when its download fails', async () => {
|
|
405
|
+
const manager = {
|
|
406
|
+
listFiles,
|
|
407
|
+
getFileUrl,
|
|
408
|
+
streamStorageFiles: async function* () {
|
|
409
|
+
yield { locationId: 'loc-9', path: null, url: 'bad-url' };
|
|
410
|
+
},
|
|
411
|
+
} as any;
|
|
412
|
+
|
|
413
|
+
await expect(
|
|
414
|
+
downloadBackupArchive({
|
|
415
|
+
manager,
|
|
416
|
+
backup,
|
|
417
|
+
fetchBody: async (url: string) => {
|
|
418
|
+
if (url === 'bad-url') throw new Error('HTTP 500');
|
|
419
|
+
return bodyOf('x');
|
|
420
|
+
},
|
|
421
|
+
sink: nullSink(),
|
|
422
|
+
createWriter: makeWriter([]),
|
|
423
|
+
retry: { attempts: 1 },
|
|
424
|
+
}),
|
|
425
|
+
).rejects.toThrow('Couldn\'t download storage file "loc-9" (HTTP 500).');
|
|
426
|
+
});
|
|
427
|
+
});
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import { afterEach, describe, expect, test, vi } from 'vitest';
|
|
2
|
+
import {
|
|
3
|
+
BackupsManager,
|
|
4
|
+
backupZipName,
|
|
5
|
+
formatFileSize,
|
|
6
|
+
type AppBackupStorageFile,
|
|
7
|
+
} from '../../src/backups.ts';
|
|
8
|
+
|
|
9
|
+
const makeManager = () =>
|
|
10
|
+
new BackupsManager({
|
|
11
|
+
appId: 'app-1',
|
|
12
|
+
apiURI: 'http://api.test',
|
|
13
|
+
withAuth: (operation) => operation('test-token'),
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
const streamOf = (chunks: string[]) =>
|
|
17
|
+
new ReadableStream<Uint8Array>({
|
|
18
|
+
start(controller) {
|
|
19
|
+
const encoder = new TextEncoder();
|
|
20
|
+
for (const chunk of chunks) {
|
|
21
|
+
controller.enqueue(encoder.encode(chunk));
|
|
22
|
+
}
|
|
23
|
+
controller.close();
|
|
24
|
+
},
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
const stubFetchBody = (chunks: string[]) => {
|
|
28
|
+
vi.stubGlobal(
|
|
29
|
+
'fetch',
|
|
30
|
+
vi.fn(async () => new Response(streamOf(chunks), { status: 200 })),
|
|
31
|
+
);
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const collect = async (manager: BackupsManager) => {
|
|
35
|
+
const files: AppBackupStorageFile[] = [];
|
|
36
|
+
for await (const file of manager.streamStorageFiles('backup-1')) {
|
|
37
|
+
files.push(file);
|
|
38
|
+
}
|
|
39
|
+
return files;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
afterEach(() => {
|
|
43
|
+
vi.unstubAllGlobals();
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
describe('streamStorageFiles', () => {
|
|
47
|
+
test('yields files and completes on the done sentinel', async () => {
|
|
48
|
+
stubFetchBody([
|
|
49
|
+
'{"locationId":"loc-1","path":"a.png","url":"http://s3/loc-1"}\n',
|
|
50
|
+
'{"locationId":"loc-2","path":"b.png","url":"http://s3/loc-2"}\n',
|
|
51
|
+
'{"done":true}\n',
|
|
52
|
+
]);
|
|
53
|
+
|
|
54
|
+
const files = await collect(makeManager());
|
|
55
|
+
expect(files).toEqual([
|
|
56
|
+
{ locationId: 'loc-1', path: 'a.png', url: 'http://s3/loc-1' },
|
|
57
|
+
{ locationId: 'loc-2', path: 'b.png', url: 'http://s3/loc-2' },
|
|
58
|
+
]);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
test('handles lines split across chunks', async () => {
|
|
62
|
+
stubFetchBody([
|
|
63
|
+
'{"locationId":"loc-1","pa',
|
|
64
|
+
'th":"a.png","url":"http://s3/loc-1"}\n{"done"',
|
|
65
|
+
':true}\n',
|
|
66
|
+
]);
|
|
67
|
+
|
|
68
|
+
const files = await collect(makeManager());
|
|
69
|
+
expect(files).toEqual([
|
|
70
|
+
{ locationId: 'loc-1', path: 'a.png', url: 'http://s3/loc-1' },
|
|
71
|
+
]);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
test('throws on a record without a locationId or url', async () => {
|
|
75
|
+
stubFetchBody([
|
|
76
|
+
'{"path":"orphan.png"}\n',
|
|
77
|
+
'{"locationId":"loc-1","path":"a.png","url":"http://s3/loc-1"}\n',
|
|
78
|
+
'{"done":true}\n',
|
|
79
|
+
]);
|
|
80
|
+
|
|
81
|
+
await expect(collect(makeManager())).rejects.toThrow(/malformed record/);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
test('only accepts an exact done sentinel', async () => {
|
|
85
|
+
stubFetchBody([
|
|
86
|
+
'{"locationId":"loc-1","path":"a.png","url":"http://s3/loc-1"}\n',
|
|
87
|
+
'{"done":1}\n',
|
|
88
|
+
]);
|
|
89
|
+
|
|
90
|
+
await expect(collect(makeManager())).rejects.toThrow(/malformed record/);
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
test('completes without files when the app has none', async () => {
|
|
94
|
+
stubFetchBody(['{"done":true}\n']);
|
|
95
|
+
expect(await collect(makeManager())).toEqual([]);
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
test('rejects a locationId that could escape the archive path', async () => {
|
|
99
|
+
stubFetchBody([
|
|
100
|
+
'{"locationId":"../evil","path":"a.png","url":"http://s3/loc-1"}\n',
|
|
101
|
+
'{"done":true}\n',
|
|
102
|
+
]);
|
|
103
|
+
|
|
104
|
+
await expect(collect(makeManager())).rejects.toThrow(/malformed record/);
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
test('throws when the stream ends without the done sentinel', async () => {
|
|
108
|
+
stubFetchBody([
|
|
109
|
+
'{"locationId":"loc-1","path":"a.png","url":"http://s3/loc-1"}\n',
|
|
110
|
+
]);
|
|
111
|
+
|
|
112
|
+
await expect(collect(makeManager())).rejects.toThrow(
|
|
113
|
+
/ended before it finished/,
|
|
114
|
+
);
|
|
115
|
+
});
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
describe('list', () => {
|
|
119
|
+
test('coerces the server row into an AppBackup', async () => {
|
|
120
|
+
vi.stubGlobal(
|
|
121
|
+
'fetch',
|
|
122
|
+
vi.fn(async () =>
|
|
123
|
+
Response.json({
|
|
124
|
+
backups: [
|
|
125
|
+
{
|
|
126
|
+
id: 'backup-1',
|
|
127
|
+
isn: '42',
|
|
128
|
+
backup_at: '2026-08-05T07:12:00Z',
|
|
129
|
+
files_size: 100,
|
|
130
|
+
db_size: 200,
|
|
131
|
+
uncompressed_size: 300,
|
|
132
|
+
description: 'Automated Daily Snapshot',
|
|
133
|
+
expires_at: '2026-08-12T07:12:00Z',
|
|
134
|
+
},
|
|
135
|
+
],
|
|
136
|
+
}),
|
|
137
|
+
),
|
|
138
|
+
);
|
|
139
|
+
|
|
140
|
+
const [backup] = await makeManager().list();
|
|
141
|
+
expect(backup).toEqual({
|
|
142
|
+
id: 'backup-1',
|
|
143
|
+
isn: '42',
|
|
144
|
+
backupAt: new Date('2026-08-05T07:12:00Z'),
|
|
145
|
+
filesSize: 100,
|
|
146
|
+
dbSize: 200,
|
|
147
|
+
uncompressedSize: 300,
|
|
148
|
+
description: 'Automated Daily Snapshot',
|
|
149
|
+
expiresAt: new Date('2026-08-12T07:12:00Z'),
|
|
150
|
+
});
|
|
151
|
+
expect(backupZipName(backup)).toBe(
|
|
152
|
+
'instant-backup-2026-08-05T07-12-00-000Z.zip',
|
|
153
|
+
);
|
|
154
|
+
});
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
describe('formatFileSize', () => {
|
|
158
|
+
test('formats with decimal units and adaptive precision', () => {
|
|
159
|
+
expect(formatFileSize(999)).toBe('999 B');
|
|
160
|
+
expect(formatFileSize(3510)).toBe('3.51 KB');
|
|
161
|
+
expect(formatFileSize(1_540_000_000)).toBe('1.54 GB');
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
test('promotes across the unit boundary when rounding', () => {
|
|
165
|
+
expect(formatFileSize(999_500)).toBe('1.00 MB');
|
|
166
|
+
expect(formatFileSize(999_999_500)).toBe('1.00 GB');
|
|
167
|
+
});
|
|
168
|
+
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { describe, expect, test } from 'vitest';
|
|
2
|
+
|
|
3
|
+
import { clerkDomainFromPublishableKey } from '../../src/clerk';
|
|
4
|
+
|
|
5
|
+
describe('clerkDomainFromPublishableKey', () => {
|
|
6
|
+
test('extracts the domain from a Clerk publishable key', () => {
|
|
7
|
+
expect(
|
|
8
|
+
clerkDomainFromPublishableKey(
|
|
9
|
+
'pk_test_Z3VpZGluZy1wZWdhc3VzLTkzLmNsZXJrLmFjY291bnRzLmRldiQ',
|
|
10
|
+
),
|
|
11
|
+
).toBe('guiding-pegasus-93.clerk.accounts.dev');
|
|
12
|
+
});
|
|
13
|
+
});
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { expect, test } from 'vitest';
|
|
2
|
+
import { oauthCallbackURL } from '../../src/consts.ts';
|
|
3
|
+
|
|
4
|
+
test('builds an OAuth callback URL from the API origin', () => {
|
|
5
|
+
expect(oauthCallbackURL('https://api.example.com')).toBe(
|
|
6
|
+
'https://api.example.com/runtime/oauth/callback',
|
|
7
|
+
);
|
|
8
|
+
expect(oauthCallbackURL('https://api.example.com/')).toBe(
|
|
9
|
+
'https://api.example.com/runtime/oauth/callback',
|
|
10
|
+
);
|
|
11
|
+
});
|