tero 0.0.5 → 0.0.7

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.
@@ -4,6 +4,9 @@ export interface RecoveryConfig {
4
4
  localPath: string;
5
5
  autoRecover?: boolean;
6
6
  recoveryTimeout?: number;
7
+ mode?: 'all' | 'missing' | 'none';
8
+ continueOnError?: boolean;
9
+ concurrency?: number;
7
10
  }
8
11
  export interface RecoveryResult {
9
12
  success: boolean;
@@ -25,12 +28,23 @@ export declare class DataRecovery {
25
28
  constructor(config: RecoveryConfig);
26
29
  private initializeCloudStorage;
27
30
  private getCloudKey;
31
+ private isNotFound;
28
32
  checkFileInCloud(key: string): Promise<FileRecoveryInfo>;
29
33
  recoverSingleFile(key: string): Promise<boolean>;
30
34
  recoverFromArchive(archiveName?: string): Promise<RecoveryResult>;
31
35
  recoverIndividualFiles(keys?: string[]): Promise<RecoveryResult>;
32
36
  listAvailableArchives(): Promise<string[]>;
33
37
  listAvailableFiles(): Promise<string[]>;
38
+ /**
39
+ * List files that exist in the cloud bucket but are missing from the local directory.
40
+ * This is the v2 fast path used during hydrate-on-startup with mode='missing'.
41
+ */
42
+ listMissingLocally(): Promise<string[]>;
43
+ /**
44
+ * Recover only files that exist in the cloud but are missing locally.
45
+ * Returns the standard RecoveryResult. Used by v2 hydrate-on-startup (mode='missing').
46
+ */
47
+ recoverMissingFiles(): Promise<RecoveryResult>;
34
48
  private downloadFile;
35
49
  private extractArchive;
36
50
  testCloudConnection(): Promise<{
package/dist/recovery.js CHANGED
@@ -34,6 +34,12 @@ export class DataRecovery {
34
34
  const dbName = this.config.localPath.split('/').pop() || 'database';
35
35
  return `${prefix}/${dbName}/${filename}`;
36
36
  }
37
+ isNotFound(error) {
38
+ return (error?.name === 'NotFound' ||
39
+ error?.name === 'NoSuchKey' ||
40
+ error?.$metadata?.httpStatusCode === 404 ||
41
+ (error?.Code && (error.Code === 'NotFound' || error.Code === 'NoSuchKey')));
42
+ }
37
43
  async checkFileInCloud(key) {
38
44
  try {
39
45
  const cloudKey = this.getCloudKey(`${key}.json`);
@@ -50,13 +56,12 @@ export class DataRecovery {
50
56
  };
51
57
  }
52
58
  catch (error) {
53
- if (error.name === 'NotFound' || error.$metadata?.httpStatusCode === 404) {
54
- return {
55
- key,
56
- exists: false
57
- };
59
+ if (this.isNotFound(error)) {
60
+ return { key, exists: false };
58
61
  }
59
- throw new Error(`Failed to check file in cloud: ${error.message}`);
62
+ // Re-surface real errors (auth/network/permission) so callers can distinguish
63
+ // "not in cloud" from "couldn't reach cloud".
64
+ throw new Error(`Failed to check file in cloud: ${error?.message || 'Unknown error'}`);
60
65
  }
61
66
  }
62
67
  async recoverSingleFile(key) {
@@ -82,10 +87,11 @@ export class DataRecovery {
82
87
  return true;
83
88
  }
84
89
  catch (error) {
85
- if (error.name === 'NoSuchKey' || error.$metadata?.httpStatusCode === 404) {
90
+ if (this.isNotFound(error)) {
86
91
  return false;
87
92
  }
88
- return false;
93
+ // Re-surface real errors so callers don't mistake them for "not in cloud".
94
+ throw new Error(`Failed to recover file from cloud: ${error?.message || 'Unknown error'}`);
89
95
  }
90
96
  }
91
97
  async recoverFromArchive(archiveName) {
@@ -149,14 +155,37 @@ export class DataRecovery {
149
155
  throw new Error('No backup files found in cloud storage');
150
156
  }
151
157
  }
152
- // Recover each file
153
- for (const key of keys) {
154
- const success = await this.recoverSingleFile(key);
155
- if (success) {
156
- recovered.push(key);
157
- }
158
- else {
159
- failed.push(key);
158
+ // Recover each file. We catch real (non-404) errors here so a single
159
+ // auth/network hiccup doesn't abort the whole batch — they go into `failed`
160
+ // and the caller can decide whether to retry.
161
+ //
162
+ // Parallel S3 GETs in batches of `concurrency` (default 10). A single-key
163
+ // sequential download takes ~30ms HTTP RTT to S3/R2; 50,000 keys sequentially
164
+ // takes 25 minutes (1,500 seconds). Batched parallelism reduces this to
165
+ // ~25ms × (50,000 / 10) ≈ 125 seconds (~2 min) and can be tuned higher.
166
+ const concurrency = this.config.concurrency ?? 10;
167
+ for (let i = 0; i < keys.length; i += concurrency) {
168
+ const batch = keys.slice(i, i + concurrency);
169
+ const results = await Promise.allSettled(batch.map(async (key) => {
170
+ try {
171
+ const success = await this.recoverSingleFile(key);
172
+ return { key, success };
173
+ }
174
+ catch (error) {
175
+ return { key, success: false, error };
176
+ }
177
+ }));
178
+ for (const r of results) {
179
+ if (r.status === 'fulfilled') {
180
+ const { key, success } = r.value;
181
+ if (success)
182
+ recovered.push(key);
183
+ else
184
+ failed.push(key);
185
+ }
186
+ else {
187
+ failed.push(r.reason?.key || 'unknown');
188
+ }
160
189
  }
161
190
  }
162
191
  const duration = Date.now() - startTime;
@@ -233,6 +262,57 @@ export class DataRecovery {
233
262
  return [];
234
263
  }
235
264
  }
265
+ /**
266
+ * List files that exist in the cloud bucket but are missing from the local directory.
267
+ * This is the v2 fast path used during hydrate-on-startup with mode='missing'.
268
+ */
269
+ async listMissingLocally() {
270
+ try {
271
+ const cloudFiles = await this.listAvailableFiles();
272
+ const missing = [];
273
+ for (const key of cloudFiles) {
274
+ const localPath = join(this.config.localPath, `${key}.json`);
275
+ if (!existsSync(localPath)) {
276
+ missing.push(key);
277
+ }
278
+ }
279
+ return missing;
280
+ }
281
+ catch (error) {
282
+ return [];
283
+ }
284
+ }
285
+ /**
286
+ * Recover only files that exist in the cloud but are missing locally.
287
+ * Returns the standard RecoveryResult. Used by v2 hydrate-on-startup (mode='missing').
288
+ */
289
+ async recoverMissingFiles() {
290
+ const startTime = Date.now();
291
+ try {
292
+ const missing = await this.listMissingLocally();
293
+ if (missing.length === 0) {
294
+ return {
295
+ success: true,
296
+ recovered: [],
297
+ failed: [],
298
+ totalFiles: 0,
299
+ duration: Date.now() - startTime,
300
+ };
301
+ }
302
+ const result = await this.recoverIndividualFiles(missing);
303
+ result.duration = Date.now() - startTime;
304
+ return result;
305
+ }
306
+ catch (error) {
307
+ return {
308
+ success: false,
309
+ recovered: [],
310
+ failed: [],
311
+ totalFiles: 0,
312
+ duration: Date.now() - startTime,
313
+ };
314
+ }
315
+ }
236
316
  async downloadFile(cloudKey, localPath) {
237
317
  try {
238
318
  const getCommand = new GetObjectCommand({
@@ -253,7 +333,10 @@ export class DataRecovery {
253
333
  return true;
254
334
  }
255
335
  catch (error) {
256
- return false;
336
+ if (this.isNotFound(error)) {
337
+ return false;
338
+ }
339
+ throw new Error(`Failed to download file from cloud: ${error?.message || 'Unknown error'}`);
257
340
  }
258
341
  }
259
342
  async extractArchive(archivePath) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tero",
3
- "version": "0.0.5",
3
+ "version": "0.0.7",
4
4
  "description": "Tero is a JSON database that provides ACID transactions, schema validation, automated cloud backup, and automatic cloud recovery",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",
@@ -29,7 +29,10 @@
29
29
  "pretest": "echo 'Running Tero test suite...'",
30
30
  "posttest": "echo 'Test suite completed!'",
31
31
  "verify": "node verify-build.js && npm run test",
32
- "ci": "npm run verify"
32
+ "ci": "npm run verify",
33
+ "bench": "node bench/run-bench.js --synchronous normal",
34
+ "bench:full": "node bench/run-bench.js --synchronous full",
35
+ "bench:quick": "node bench/run-bench.js --quick --synchronous normal"
33
36
  },
34
37
  "files": [
35
38
  "dist/**/*"