@a0n/aeon 5.0.1

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.
Files changed (73) hide show
  1. package/LICENSE +15 -0
  2. package/README.md +199 -0
  3. package/dist/CryptoProvider-SLWjqByk.d.cts +407 -0
  4. package/dist/CryptoProvider-SLWjqByk.d.ts +407 -0
  5. package/dist/compression/index.cjs +1445 -0
  6. package/dist/compression/index.cjs.map +1 -0
  7. package/dist/compression/index.d.cts +451 -0
  8. package/dist/compression/index.d.ts +451 -0
  9. package/dist/compression/index.js +1426 -0
  10. package/dist/compression/index.js.map +1 -0
  11. package/dist/core/index.cjs +4 -0
  12. package/dist/core/index.cjs.map +1 -0
  13. package/dist/core/index.d.cts +212 -0
  14. package/dist/core/index.d.ts +212 -0
  15. package/dist/core/index.js +3 -0
  16. package/dist/core/index.js.map +1 -0
  17. package/dist/crypto/index.cjs +130 -0
  18. package/dist/crypto/index.cjs.map +1 -0
  19. package/dist/crypto/index.d.cts +56 -0
  20. package/dist/crypto/index.d.ts +56 -0
  21. package/dist/crypto/index.js +124 -0
  22. package/dist/crypto/index.js.map +1 -0
  23. package/dist/distributed/index.cjs +2586 -0
  24. package/dist/distributed/index.cjs.map +1 -0
  25. package/dist/distributed/index.d.cts +1005 -0
  26. package/dist/distributed/index.d.ts +1005 -0
  27. package/dist/distributed/index.js +2580 -0
  28. package/dist/distributed/index.js.map +1 -0
  29. package/dist/index.cjs +10953 -0
  30. package/dist/index.cjs.map +1 -0
  31. package/dist/index.d.cts +1953 -0
  32. package/dist/index.d.ts +1953 -0
  33. package/dist/index.js +10828 -0
  34. package/dist/index.js.map +1 -0
  35. package/dist/offline/index.cjs +419 -0
  36. package/dist/offline/index.cjs.map +1 -0
  37. package/dist/offline/index.d.cts +148 -0
  38. package/dist/offline/index.d.ts +148 -0
  39. package/dist/offline/index.js +415 -0
  40. package/dist/offline/index.js.map +1 -0
  41. package/dist/optimization/index.cjs +800 -0
  42. package/dist/optimization/index.cjs.map +1 -0
  43. package/dist/optimization/index.d.cts +347 -0
  44. package/dist/optimization/index.d.ts +347 -0
  45. package/dist/optimization/index.js +790 -0
  46. package/dist/optimization/index.js.map +1 -0
  47. package/dist/persistence/index.cjs +207 -0
  48. package/dist/persistence/index.cjs.map +1 -0
  49. package/dist/persistence/index.d.cts +95 -0
  50. package/dist/persistence/index.d.ts +95 -0
  51. package/dist/persistence/index.js +204 -0
  52. package/dist/persistence/index.js.map +1 -0
  53. package/dist/presence/index.cjs +489 -0
  54. package/dist/presence/index.cjs.map +1 -0
  55. package/dist/presence/index.d.cts +283 -0
  56. package/dist/presence/index.d.ts +283 -0
  57. package/dist/presence/index.js +485 -0
  58. package/dist/presence/index.js.map +1 -0
  59. package/dist/types-CMxO7QF0.d.cts +33 -0
  60. package/dist/types-CMxO7QF0.d.ts +33 -0
  61. package/dist/utils/index.cjs +64 -0
  62. package/dist/utils/index.cjs.map +1 -0
  63. package/dist/utils/index.d.cts +38 -0
  64. package/dist/utils/index.d.ts +38 -0
  65. package/dist/utils/index.js +57 -0
  66. package/dist/utils/index.js.map +1 -0
  67. package/dist/versioning/index.cjs +1164 -0
  68. package/dist/versioning/index.cjs.map +1 -0
  69. package/dist/versioning/index.d.cts +537 -0
  70. package/dist/versioning/index.d.ts +537 -0
  71. package/dist/versioning/index.js +1159 -0
  72. package/dist/versioning/index.js.map +1 -0
  73. package/package.json +194 -0
@@ -0,0 +1,1159 @@
1
+ // src/utils/logger.ts
2
+ var consoleLogger = {
3
+ debug: (...args) => {
4
+ console.debug("[AEON:DEBUG]", ...args);
5
+ },
6
+ info: (...args) => {
7
+ console.info("[AEON:INFO]", ...args);
8
+ },
9
+ warn: (...args) => {
10
+ console.warn("[AEON:WARN]", ...args);
11
+ },
12
+ error: (...args) => {
13
+ console.error("[AEON:ERROR]", ...args);
14
+ }
15
+ };
16
+ var currentLogger = consoleLogger;
17
+ function getLogger() {
18
+ return currentLogger;
19
+ }
20
+ var logger = {
21
+ debug: (...args) => getLogger().debug(...args),
22
+ info: (...args) => getLogger().info(...args),
23
+ warn: (...args) => getLogger().warn(...args),
24
+ error: (...args) => getLogger().error(...args)
25
+ };
26
+
27
+ // src/versioning/SchemaVersionManager.ts
28
+ var SchemaVersionManager = class {
29
+ versions = /* @__PURE__ */ new Map();
30
+ versionHistory = [];
31
+ compatibilityMatrix = /* @__PURE__ */ new Map();
32
+ currentVersion = null;
33
+ constructor() {
34
+ this.initializeDefaultVersions();
35
+ }
36
+ /**
37
+ * Initialize default versions
38
+ */
39
+ initializeDefaultVersions() {
40
+ const v1_0_0 = {
41
+ major: 1,
42
+ minor: 0,
43
+ patch: 0,
44
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
45
+ description: "Initial schema version",
46
+ breaking: false
47
+ };
48
+ this.registerVersion(v1_0_0);
49
+ this.currentVersion = v1_0_0;
50
+ }
51
+ /**
52
+ * Register a new schema version
53
+ */
54
+ registerVersion(version) {
55
+ const versionString = this.versionToString(version);
56
+ this.versions.set(versionString, version);
57
+ this.versionHistory.push(version);
58
+ logger.debug("[SchemaVersionManager] Version registered", {
59
+ version: versionString,
60
+ breaking: version.breaking,
61
+ description: version.description
62
+ });
63
+ }
64
+ /**
65
+ * Get current version
66
+ */
67
+ getCurrentVersion() {
68
+ if (!this.currentVersion) {
69
+ throw new Error("No current version set");
70
+ }
71
+ return this.currentVersion;
72
+ }
73
+ /**
74
+ * Set current version
75
+ */
76
+ setCurrentVersion(version) {
77
+ if (!this.versions.has(this.versionToString(version))) {
78
+ throw new Error(
79
+ `Version ${this.versionToString(version)} not registered`
80
+ );
81
+ }
82
+ this.currentVersion = version;
83
+ logger.debug("[SchemaVersionManager] Current version set", {
84
+ version: this.versionToString(version)
85
+ });
86
+ }
87
+ /**
88
+ * Get version history
89
+ */
90
+ getVersionHistory() {
91
+ return [...this.versionHistory];
92
+ }
93
+ /**
94
+ * Check if version exists
95
+ */
96
+ hasVersion(version) {
97
+ return this.versions.has(this.versionToString(version));
98
+ }
99
+ /**
100
+ * Get version by string (e.g., "1.2.3")
101
+ */
102
+ getVersion(versionString) {
103
+ return this.versions.get(versionString);
104
+ }
105
+ /**
106
+ * Register compatibility rule
107
+ */
108
+ registerCompatibility(rule) {
109
+ if (!this.compatibilityMatrix.has(rule.from)) {
110
+ this.compatibilityMatrix.set(rule.from, []);
111
+ }
112
+ const rules = this.compatibilityMatrix.get(rule.from);
113
+ if (rules) {
114
+ rules.push(rule);
115
+ }
116
+ logger.debug("[SchemaVersionManager] Compatibility rule registered", {
117
+ from: rule.from,
118
+ to: rule.to,
119
+ compatible: rule.compatible,
120
+ requiresMigration: rule.requiresMigration
121
+ });
122
+ }
123
+ /**
124
+ * Check if migration path exists
125
+ */
126
+ canMigrate(fromVersion, toVersion) {
127
+ const fromStr = typeof fromVersion === "string" ? fromVersion : this.versionToString(fromVersion);
128
+ const toStr = typeof toVersion === "string" ? toVersion : this.versionToString(toVersion);
129
+ const rules = this.compatibilityMatrix.get(fromStr) || [];
130
+ return rules.some((r) => r.to === toStr && r.requiresMigration);
131
+ }
132
+ /**
133
+ * Get migration path
134
+ */
135
+ getMigrationPath(fromVersion, toVersion) {
136
+ const path = [];
137
+ let current = fromVersion;
138
+ const maxSteps = 100;
139
+ let steps = 0;
140
+ while (this.compareVersions(current, toVersion) !== 0 && steps < maxSteps) {
141
+ const fromStr = this.versionToString(current);
142
+ const rules = this.compatibilityMatrix.get(fromStr) || [];
143
+ let found = false;
144
+ for (const rule of rules) {
145
+ const nextVersion = this.getVersion(rule.to);
146
+ if (nextVersion) {
147
+ if (this.compareVersions(nextVersion, toVersion) <= 0 || this.compareVersions(current, nextVersion) < this.compareVersions(current, toVersion)) {
148
+ current = nextVersion;
149
+ path.push(current);
150
+ found = true;
151
+ break;
152
+ }
153
+ }
154
+ }
155
+ if (!found) {
156
+ break;
157
+ }
158
+ steps++;
159
+ }
160
+ return path;
161
+ }
162
+ /**
163
+ * Compare two versions
164
+ * Returns: -1 if v1 < v2, 0 if equal, 1 if v1 > v2
165
+ */
166
+ compareVersions(v1, v2) {
167
+ const ver1 = typeof v1 === "string" ? this.parseVersion(v1) : v1;
168
+ const ver2 = typeof v2 === "string" ? this.parseVersion(v2) : v2;
169
+ if (ver1.major !== ver2.major) {
170
+ return ver1.major < ver2.major ? -1 : 1;
171
+ }
172
+ if (ver1.minor !== ver2.minor) {
173
+ return ver1.minor < ver2.minor ? -1 : 1;
174
+ }
175
+ if (ver1.patch !== ver2.patch) {
176
+ return ver1.patch < ver2.patch ? -1 : 1;
177
+ }
178
+ return 0;
179
+ }
180
+ /**
181
+ * Parse version string to SchemaVersion
182
+ */
183
+ parseVersion(versionString) {
184
+ const parts = versionString.split(".").map(Number);
185
+ const safeInt = (v) => v !== void 0 && Number.isFinite(v) ? v : 0;
186
+ return {
187
+ major: safeInt(parts[0]),
188
+ minor: safeInt(parts[1]),
189
+ patch: safeInt(parts[2]),
190
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
191
+ description: "",
192
+ breaking: false
193
+ };
194
+ }
195
+ /**
196
+ * Create new version
197
+ */
198
+ createVersion(major, minor, patch, description, breaking = false) {
199
+ return {
200
+ major,
201
+ minor,
202
+ patch,
203
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
204
+ description,
205
+ breaking
206
+ };
207
+ }
208
+ /**
209
+ * Convert version to string
210
+ */
211
+ versionToString(version) {
212
+ return `${version.major}.${version.minor}.${version.patch}`;
213
+ }
214
+ /**
215
+ * Get version metadata
216
+ */
217
+ getVersionMetadata(version) {
218
+ const history = this.versionHistory;
219
+ const currentIndex = history.findIndex(
220
+ (v) => this.versionToString(v) === this.versionToString(version)
221
+ );
222
+ return {
223
+ version,
224
+ previousVersion: currentIndex > 0 ? history[currentIndex - 1] : void 0,
225
+ changes: [version.description],
226
+ migrationsRequired: this.canMigrate(
227
+ this.currentVersion || version,
228
+ version
229
+ ) ? [this.versionToString(version)] : [],
230
+ rollbackPossible: currentIndex > 0
231
+ };
232
+ }
233
+ /**
234
+ * Get all registered versions
235
+ */
236
+ getAllVersions() {
237
+ return Array.from(this.versions.values()).sort(
238
+ (a, b) => this.compareVersions(a, b)
239
+ );
240
+ }
241
+ /**
242
+ * Clear all versions (for testing)
243
+ */
244
+ clear() {
245
+ this.versions.clear();
246
+ this.versionHistory = [];
247
+ this.compatibilityMatrix.clear();
248
+ this.currentVersion = null;
249
+ }
250
+ };
251
+
252
+ // src/versioning/MigrationEngine.ts
253
+ var MigrationEngine = class {
254
+ migrations = /* @__PURE__ */ new Map();
255
+ executedMigrations = [];
256
+ state = {
257
+ currentVersion: "1.0.0",
258
+ appliedMigrations: [],
259
+ failedMigrations: [],
260
+ lastMigrationTime: (/* @__PURE__ */ new Date()).toISOString(),
261
+ totalMigrationsRun: 0
262
+ };
263
+ /**
264
+ * Register a migration
265
+ */
266
+ registerMigration(migration) {
267
+ this.migrations.set(migration.id, migration);
268
+ logger.debug("[MigrationEngine] Migration registered", {
269
+ id: migration.id,
270
+ version: migration.version,
271
+ name: migration.name
272
+ });
273
+ }
274
+ /**
275
+ * Execute a migration
276
+ */
277
+ async executeMigration(migrationId, data) {
278
+ const migration = this.migrations.get(migrationId);
279
+ if (!migration) {
280
+ throw new Error(`Migration ${migrationId} not found`);
281
+ }
282
+ const startTime = Date.now();
283
+ const result = {
284
+ migrationId,
285
+ success: false,
286
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
287
+ duration: 0,
288
+ itemsAffected: 0,
289
+ errors: []
290
+ };
291
+ try {
292
+ logger.debug("[MigrationEngine] Executing migration", {
293
+ id: migrationId,
294
+ version: migration.version
295
+ });
296
+ migration.up(data);
297
+ result.success = true;
298
+ result.itemsAffected = Array.isArray(data) ? data.length : 1;
299
+ result.duration = Date.now() - startTime;
300
+ this.state.appliedMigrations.push(migrationId);
301
+ this.state.currentVersion = migration.version;
302
+ this.state.totalMigrationsRun++;
303
+ this.state.lastMigrationTime = result.timestamp;
304
+ this.executedMigrations.push(result);
305
+ logger.debug("[MigrationEngine] Migration executed successfully", {
306
+ id: migrationId,
307
+ duration: result.duration,
308
+ itemsAffected: result.itemsAffected
309
+ });
310
+ return result;
311
+ } catch (error) {
312
+ result.errors = [error instanceof Error ? error.message : String(error)];
313
+ this.state.failedMigrations.push(migrationId);
314
+ this.executedMigrations.push(result);
315
+ logger.error("[MigrationEngine] Migration failed", {
316
+ id: migrationId,
317
+ error: result.errors[0]
318
+ });
319
+ throw new Error(`Migration ${migrationId} failed: ${result.errors[0]}`);
320
+ }
321
+ }
322
+ /**
323
+ * Rollback a migration
324
+ */
325
+ async rollbackMigration(migrationId, data) {
326
+ const migration = this.migrations.get(migrationId);
327
+ if (!migration) {
328
+ throw new Error(`Migration ${migrationId} not found`);
329
+ }
330
+ if (!migration.down) {
331
+ throw new Error(`Migration ${migrationId} does not support rollback`);
332
+ }
333
+ const startTime = Date.now();
334
+ const result = {
335
+ migrationId,
336
+ success: false,
337
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
338
+ duration: 0,
339
+ itemsAffected: 0,
340
+ errors: []
341
+ };
342
+ try {
343
+ logger.debug("[MigrationEngine] Rolling back migration", {
344
+ id: migrationId,
345
+ version: migration.version
346
+ });
347
+ migration.down(data);
348
+ result.success = true;
349
+ result.itemsAffected = Array.isArray(data) ? data.length : 1;
350
+ result.duration = Date.now() - startTime;
351
+ this.state.appliedMigrations = this.state.appliedMigrations.filter(
352
+ (id) => id !== migrationId
353
+ );
354
+ this.executedMigrations.push(result);
355
+ logger.debug("[MigrationEngine] Migration rolled back", {
356
+ id: migrationId,
357
+ duration: result.duration
358
+ });
359
+ return result;
360
+ } catch (error) {
361
+ result.errors = [error instanceof Error ? error.message : String(error)];
362
+ this.executedMigrations.push(result);
363
+ logger.error("[MigrationEngine] Rollback failed", {
364
+ id: migrationId,
365
+ error: result.errors[0]
366
+ });
367
+ throw new Error(
368
+ `Rollback for ${migrationId} failed: ${result.errors[0]}`
369
+ );
370
+ }
371
+ }
372
+ /**
373
+ * Get migration state
374
+ */
375
+ getState() {
376
+ return { ...this.state };
377
+ }
378
+ /**
379
+ * Get migration execution history
380
+ */
381
+ getExecutionHistory() {
382
+ return [...this.executedMigrations];
383
+ }
384
+ /**
385
+ * Get migration by ID
386
+ */
387
+ getMigration(migrationId) {
388
+ return this.migrations.get(migrationId);
389
+ }
390
+ /**
391
+ * Get all registered migrations
392
+ */
393
+ getAllMigrations() {
394
+ return Array.from(this.migrations.values());
395
+ }
396
+ /**
397
+ * Get applied migrations
398
+ */
399
+ getAppliedMigrations() {
400
+ return [...this.state.appliedMigrations];
401
+ }
402
+ /**
403
+ * Get failed migrations
404
+ */
405
+ getFailedMigrations() {
406
+ return [...this.state.failedMigrations];
407
+ }
408
+ /**
409
+ * Get pending migrations
410
+ */
411
+ getPendingMigrations() {
412
+ return this.getAllMigrations().filter(
413
+ (m) => !this.state.appliedMigrations.includes(m.id)
414
+ );
415
+ }
416
+ /**
417
+ * Get migration statistics
418
+ */
419
+ getStatistics() {
420
+ const successful = this.executedMigrations.filter((m) => m.success).length;
421
+ const failed = this.executedMigrations.filter((m) => !m.success).length;
422
+ const totalDuration = this.executedMigrations.reduce(
423
+ (sum, m) => sum + m.duration,
424
+ 0
425
+ );
426
+ const totalAffected = this.executedMigrations.reduce(
427
+ (sum, m) => sum + m.itemsAffected,
428
+ 0
429
+ );
430
+ return {
431
+ totalExecuted: this.executedMigrations.length,
432
+ successful,
433
+ failed,
434
+ successRate: this.executedMigrations.length > 0 ? successful / this.executedMigrations.length * 100 : 0,
435
+ totalDurationMs: totalDuration,
436
+ averageDurationMs: this.executedMigrations.length > 0 ? totalDuration / this.executedMigrations.length : 0,
437
+ totalAffected
438
+ };
439
+ }
440
+ /**
441
+ * Clear history (for testing)
442
+ */
443
+ clear() {
444
+ this.migrations.clear();
445
+ this.executedMigrations = [];
446
+ this.state = {
447
+ currentVersion: "1.0.0",
448
+ appliedMigrations: [],
449
+ failedMigrations: [],
450
+ lastMigrationTime: (/* @__PURE__ */ new Date()).toISOString(),
451
+ totalMigrationsRun: 0
452
+ };
453
+ }
454
+ };
455
+
456
+ // src/versioning/DataTransformer.ts
457
+ var DataTransformer = class {
458
+ rules = /* @__PURE__ */ new Map();
459
+ transformationHistory = [];
460
+ /**
461
+ * Register a transformation rule
462
+ */
463
+ registerRule(rule) {
464
+ this.rules.set(rule.field, rule);
465
+ logger.debug("[DataTransformer] Rule registered", {
466
+ field: rule.field,
467
+ required: rule.required,
468
+ hasDefault: rule.defaultValue !== void 0
469
+ });
470
+ }
471
+ /**
472
+ * Transform a single field value
473
+ */
474
+ transformField(field, value) {
475
+ const rule = this.rules.get(field);
476
+ if (!rule) {
477
+ return value;
478
+ }
479
+ try {
480
+ return rule.transformer(value);
481
+ } catch (error) {
482
+ if (rule.required) {
483
+ throw new Error(
484
+ `Failed to transform required field ${field}: ${error instanceof Error ? error.message : String(error)}`
485
+ );
486
+ }
487
+ return rule.defaultValue !== void 0 ? rule.defaultValue : value;
488
+ }
489
+ }
490
+ /**
491
+ * Transform a single object
492
+ */
493
+ transformObject(data) {
494
+ const transformed = {};
495
+ for (const [key, value] of Object.entries(data)) {
496
+ try {
497
+ transformed[key] = this.transformField(key, value);
498
+ } catch (error) {
499
+ logger.warn("[DataTransformer] Field transformation failed", {
500
+ field: key,
501
+ error: error instanceof Error ? error.message : String(error)
502
+ });
503
+ const rule = this.rules.get(key);
504
+ if (!rule || !rule.required) {
505
+ transformed[key] = value;
506
+ }
507
+ }
508
+ }
509
+ return transformed;
510
+ }
511
+ /**
512
+ * Transform a collection of items
513
+ */
514
+ transformCollection(items) {
515
+ const startTime = Date.now();
516
+ const result = {
517
+ success: true,
518
+ itemsTransformed: 0,
519
+ itemsFailed: 0,
520
+ errors: [],
521
+ warnings: [],
522
+ duration: 0
523
+ };
524
+ for (let i = 0; i < items.length; i++) {
525
+ const item = items[i];
526
+ try {
527
+ if (typeof item === "object" && item !== null && !Array.isArray(item)) {
528
+ this.transformObject(item);
529
+ result.itemsTransformed++;
530
+ } else {
531
+ result.warnings.push(`Item ${i} is not a transformable object`);
532
+ }
533
+ } catch (error) {
534
+ result.errors.push({
535
+ item,
536
+ error: error instanceof Error ? error.message : String(error)
537
+ });
538
+ result.itemsFailed++;
539
+ }
540
+ }
541
+ result.duration = Date.now() - startTime;
542
+ result.success = result.itemsFailed === 0;
543
+ this.transformationHistory.push(result);
544
+ logger.debug("[DataTransformer] Collection transformed", {
545
+ total: items.length,
546
+ transformed: result.itemsTransformed,
547
+ failed: result.itemsFailed,
548
+ duration: result.duration
549
+ });
550
+ return result;
551
+ }
552
+ /**
553
+ * Validate transformed data
554
+ */
555
+ validateTransformation(original, transformed) {
556
+ const issues = [];
557
+ if (original.length !== transformed.length) {
558
+ issues.push(
559
+ `Item count mismatch: ${original.length} -> ${transformed.length}`
560
+ );
561
+ }
562
+ for (let i = 0; i < Math.min(original.length, transformed.length); i++) {
563
+ const orig = original[i];
564
+ const trans = transformed[i];
565
+ if (!this.validateItem(orig, trans)) {
566
+ issues.push(`Item ${i} validation failed`);
567
+ }
568
+ }
569
+ return {
570
+ valid: issues.length === 0,
571
+ issues
572
+ };
573
+ }
574
+ /**
575
+ * Validate a single item transformation
576
+ */
577
+ validateItem(original, transformed) {
578
+ if (original === null || original === void 0) {
579
+ return true;
580
+ }
581
+ if (typeof original === "object" && typeof transformed !== "object") {
582
+ return false;
583
+ }
584
+ return true;
585
+ }
586
+ /**
587
+ * Get transformation history
588
+ */
589
+ getTransformationHistory() {
590
+ return [...this.transformationHistory];
591
+ }
592
+ /**
593
+ * Get transformation statistics
594
+ */
595
+ getStatistics() {
596
+ const totalTransformed = this.transformationHistory.reduce(
597
+ (sum, r) => sum + r.itemsTransformed,
598
+ 0
599
+ );
600
+ const totalFailed = this.transformationHistory.reduce(
601
+ (sum, r) => sum + r.itemsFailed,
602
+ 0
603
+ );
604
+ const totalDuration = this.transformationHistory.reduce(
605
+ (sum, r) => sum + r.duration,
606
+ 0
607
+ );
608
+ return {
609
+ totalBatches: this.transformationHistory.length,
610
+ totalTransformed,
611
+ totalFailed,
612
+ successRate: totalTransformed + totalFailed > 0 ? totalTransformed / (totalTransformed + totalFailed) * 100 : 0,
613
+ totalDurationMs: totalDuration,
614
+ averageBatchDurationMs: this.transformationHistory.length > 0 ? totalDuration / this.transformationHistory.length : 0
615
+ };
616
+ }
617
+ /**
618
+ * Get registered rules
619
+ */
620
+ getRules() {
621
+ return Array.from(this.rules.values());
622
+ }
623
+ /**
624
+ * Get rule for field
625
+ */
626
+ getRule(field) {
627
+ return this.rules.get(field);
628
+ }
629
+ /**
630
+ * Clear all rules (for testing)
631
+ */
632
+ clearRules() {
633
+ this.rules.clear();
634
+ }
635
+ /**
636
+ * Clear history (for testing)
637
+ */
638
+ clearHistory() {
639
+ this.transformationHistory = [];
640
+ }
641
+ /**
642
+ * Clear all state (for testing)
643
+ */
644
+ clear() {
645
+ this.clearRules();
646
+ this.clearHistory();
647
+ }
648
+ };
649
+
650
+ // src/versioning/MigrationTracker.ts
651
+ var MigrationTracker = class _MigrationTracker {
652
+ static DEFAULT_PERSIST_KEY = "aeon:migration-tracker:v1";
653
+ static INTEGRITY_ROOT = "aeon:migration-integrity-root:v1";
654
+ migrations = [];
655
+ snapshots = /* @__PURE__ */ new Map();
656
+ persistence = null;
657
+ persistTimer = null;
658
+ persistInFlight = false;
659
+ persistPending = false;
660
+ constructor(options) {
661
+ if (options?.persistence) {
662
+ this.persistence = {
663
+ ...options.persistence,
664
+ key: options.persistence.key ?? _MigrationTracker.DEFAULT_PERSIST_KEY,
665
+ autoPersist: options.persistence.autoPersist ?? true,
666
+ autoLoad: options.persistence.autoLoad ?? false,
667
+ persistDebounceMs: options.persistence.persistDebounceMs ?? 25
668
+ };
669
+ }
670
+ if (this.persistence?.autoLoad) {
671
+ void this.loadFromPersistence().catch((error) => {
672
+ logger.error("[MigrationTracker] Failed to load persistence", {
673
+ key: this.persistence?.key,
674
+ error: error instanceof Error ? error.message : String(error)
675
+ });
676
+ });
677
+ }
678
+ }
679
+ /**
680
+ * Track a new migration
681
+ */
682
+ recordMigration(record) {
683
+ this.migrations.push({ ...record });
684
+ this.schedulePersist();
685
+ logger.debug("[MigrationTracker] Migration recorded", {
686
+ id: record.id,
687
+ migrationId: record.migrationId,
688
+ version: record.version,
689
+ status: record.status
690
+ });
691
+ }
692
+ /**
693
+ * Track migration with snapshot
694
+ */
695
+ trackMigration(migrationId, version, beforeHash, afterHash, itemCount, duration, itemsAffected, appliedBy = "system") {
696
+ const record = {
697
+ id: `${migrationId}-${Date.now()}`,
698
+ migrationId,
699
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
700
+ version,
701
+ direction: "up",
702
+ status: "applied",
703
+ duration,
704
+ itemsAffected,
705
+ dataSnapshot: {
706
+ beforeHash,
707
+ afterHash,
708
+ itemCount
709
+ },
710
+ appliedBy
711
+ };
712
+ this.recordMigration(record);
713
+ this.snapshots.set(record.id, {
714
+ beforeHash,
715
+ afterHash,
716
+ itemCount
717
+ });
718
+ }
719
+ /**
720
+ * Get all migration records
721
+ */
722
+ getMigrations() {
723
+ return this.migrations.map((m) => ({ ...m }));
724
+ }
725
+ /**
726
+ * Get migrations for a specific version
727
+ */
728
+ getMigrationsForVersion(version) {
729
+ return this.migrations.filter((m) => m.version === version);
730
+ }
731
+ /**
732
+ * Get migration by ID
733
+ */
734
+ getMigration(id) {
735
+ return this.migrations.find((m) => m.id === id);
736
+ }
737
+ /**
738
+ * Check if can rollback
739
+ */
740
+ canRollback(fromVersion, toVersion) {
741
+ const fromIndex = this.migrations.findIndex(
742
+ (m) => m.version === fromVersion
743
+ );
744
+ const toIndex = this.migrations.findIndex((m) => m.version === toVersion);
745
+ if (fromIndex === -1 || toIndex === -1) {
746
+ return false;
747
+ }
748
+ if (toIndex >= fromIndex) {
749
+ return false;
750
+ }
751
+ for (let i = fromIndex; i > toIndex; i--) {
752
+ if (!this.migrations[i]?.dataSnapshot) {
753
+ return false;
754
+ }
755
+ }
756
+ return true;
757
+ }
758
+ /**
759
+ * Get rollback path
760
+ */
761
+ getRollbackPath(fromVersion, toVersion) {
762
+ const canRollback = this.canRollback(fromVersion, toVersion);
763
+ const path = [];
764
+ const affectedVersions = [];
765
+ let estimatedDuration = 0;
766
+ if (canRollback) {
767
+ const fromIndex = this.migrations.findIndex(
768
+ (m) => m.version === fromVersion
769
+ );
770
+ const toIndex = this.migrations.findIndex((m) => m.version === toVersion);
771
+ for (let i = fromIndex; i > toIndex; i--) {
772
+ const migration = this.migrations[i];
773
+ if (migration) {
774
+ path.push(migration.migrationId);
775
+ affectedVersions.push(migration.version);
776
+ estimatedDuration += migration.duration;
777
+ }
778
+ }
779
+ }
780
+ return {
781
+ path,
782
+ canRollback,
783
+ affectedVersions,
784
+ estimatedDuration
785
+ };
786
+ }
787
+ /**
788
+ * Get applied migrations
789
+ */
790
+ getAppliedMigrations() {
791
+ return this.migrations.filter((m) => m.status === "applied");
792
+ }
793
+ /**
794
+ * Get failed migrations
795
+ */
796
+ getFailedMigrations() {
797
+ return this.migrations.filter((m) => m.status === "failed");
798
+ }
799
+ /**
800
+ * Get pending migrations
801
+ */
802
+ getPendingMigrations() {
803
+ return this.migrations.filter((m) => m.status === "pending");
804
+ }
805
+ /**
806
+ * Get latest migration
807
+ */
808
+ getLatestMigration() {
809
+ return this.migrations[this.migrations.length - 1];
810
+ }
811
+ /**
812
+ * Get migration timeline
813
+ */
814
+ getTimeline() {
815
+ return this.migrations.map((m) => ({
816
+ timestamp: m.timestamp,
817
+ version: m.version,
818
+ status: m.status
819
+ }));
820
+ }
821
+ /**
822
+ * Get migration statistics
823
+ */
824
+ getStatistics() {
825
+ const applied = this.migrations.filter(
826
+ (m) => m.status === "applied"
827
+ ).length;
828
+ const failed = this.migrations.filter((m) => m.status === "failed").length;
829
+ const pending = this.migrations.filter(
830
+ (m) => m.status === "pending"
831
+ ).length;
832
+ const rolledBack = this.migrations.filter(
833
+ (m) => m.status === "rolled-back"
834
+ ).length;
835
+ const totalDuration = this.migrations.reduce(
836
+ (sum, m) => sum + m.duration,
837
+ 0
838
+ );
839
+ const totalAffected = this.migrations.reduce(
840
+ (sum, m) => sum + m.itemsAffected,
841
+ 0
842
+ );
843
+ return {
844
+ total: this.migrations.length,
845
+ applied,
846
+ failed,
847
+ pending,
848
+ rolledBack,
849
+ successRate: this.migrations.length > 0 ? applied / this.migrations.length * 100 : 0,
850
+ totalDurationMs: totalDuration,
851
+ averageDurationMs: this.migrations.length > 0 ? totalDuration / this.migrations.length : 0,
852
+ totalItemsAffected: totalAffected
853
+ };
854
+ }
855
+ /**
856
+ * Get audit trail
857
+ */
858
+ getAuditTrail(migrationId) {
859
+ const filtered = migrationId ? this.migrations.filter((m) => m.migrationId === migrationId) : this.migrations;
860
+ return filtered.map((m) => ({
861
+ id: m.id,
862
+ timestamp: m.timestamp,
863
+ migrationId: m.migrationId,
864
+ version: m.version,
865
+ status: m.status,
866
+ appliedBy: m.appliedBy,
867
+ duration: m.duration,
868
+ itemsAffected: m.itemsAffected,
869
+ error: m.errorMessage
870
+ }));
871
+ }
872
+ /**
873
+ * Get data snapshot for recovery
874
+ */
875
+ getSnapshot(recordId) {
876
+ return this.snapshots.get(recordId);
877
+ }
878
+ /**
879
+ * Update migration status
880
+ */
881
+ updateMigrationStatus(recordId, status, error) {
882
+ const migration = this.migrations.find((m) => m.id === recordId);
883
+ if (migration) {
884
+ migration.status = status;
885
+ if (error) {
886
+ migration.errorMessage = error;
887
+ }
888
+ logger.debug("[MigrationTracker] Migration status updated", {
889
+ recordId,
890
+ status,
891
+ hasError: !!error
892
+ });
893
+ this.schedulePersist();
894
+ }
895
+ }
896
+ /**
897
+ * Persist tracker state with integrity chain verification metadata.
898
+ */
899
+ async saveToPersistence() {
900
+ if (!this.persistence) {
901
+ return;
902
+ }
903
+ const normalizedMigrations = this.migrations.map((migration) => ({
904
+ ...migration,
905
+ previousHash: void 0,
906
+ integrityHash: void 0
907
+ }));
908
+ const integrityEntries = [];
909
+ let previousHash = _MigrationTracker.INTEGRITY_ROOT;
910
+ for (const migration of normalizedMigrations) {
911
+ const hash = await this.computeDigestHex(
912
+ `${previousHash}|${this.stableStringify(migration)}`
913
+ );
914
+ integrityEntries.push({
915
+ recordId: migration.id,
916
+ previousHash,
917
+ hash
918
+ });
919
+ previousHash = hash;
920
+ }
921
+ const persistedMigrations = normalizedMigrations.map(
922
+ (migration, index) => ({
923
+ ...migration,
924
+ previousHash: integrityEntries[index]?.previousHash,
925
+ integrityHash: integrityEntries[index]?.hash
926
+ })
927
+ );
928
+ const data = {
929
+ migrations: persistedMigrations,
930
+ snapshots: Array.from(this.snapshots.entries()).map(
931
+ ([recordId, snapshot]) => ({
932
+ recordId,
933
+ beforeHash: snapshot.beforeHash,
934
+ afterHash: snapshot.afterHash,
935
+ itemCount: snapshot.itemCount
936
+ })
937
+ ),
938
+ integrity: {
939
+ algorithm: "sha256-chain-v1",
940
+ entries: integrityEntries,
941
+ rootHash: previousHash
942
+ }
943
+ };
944
+ const envelope = {
945
+ version: 1,
946
+ updatedAt: Date.now(),
947
+ data
948
+ };
949
+ const serialize = this.persistence.serializer ?? ((value) => JSON.stringify(value));
950
+ await this.persistence.adapter.setItem(
951
+ this.persistence.key,
952
+ serialize(envelope)
953
+ );
954
+ }
955
+ /**
956
+ * Load tracker state and verify integrity chain.
957
+ */
958
+ async loadFromPersistence() {
959
+ if (!this.persistence) {
960
+ return { migrations: 0, snapshots: 0 };
961
+ }
962
+ const raw = await this.persistence.adapter.getItem(this.persistence.key);
963
+ if (!raw) {
964
+ return { migrations: 0, snapshots: 0 };
965
+ }
966
+ const deserialize = this.persistence.deserializer ?? ((value) => JSON.parse(
967
+ value
968
+ ));
969
+ const envelope = deserialize(raw);
970
+ if (envelope.version !== 1 || !envelope.data) {
971
+ throw new Error("Invalid migration tracker persistence payload");
972
+ }
973
+ if (!Array.isArray(envelope.data.migrations) || !Array.isArray(envelope.data.snapshots) || !envelope.data.integrity || !Array.isArray(envelope.data.integrity.entries) || typeof envelope.data.integrity.rootHash !== "string") {
974
+ throw new Error("Invalid migration tracker persistence structure");
975
+ }
976
+ if (envelope.data.integrity.algorithm !== "sha256-chain-v1") {
977
+ throw new Error("Unsupported migration integrity algorithm");
978
+ }
979
+ if (envelope.data.integrity.entries.length !== envelope.data.migrations.length) {
980
+ throw new Error("Migration integrity entry count mismatch");
981
+ }
982
+ const validatedMigrations = [];
983
+ let previousHash = _MigrationTracker.INTEGRITY_ROOT;
984
+ for (let i = 0; i < envelope.data.migrations.length; i++) {
985
+ const migration = envelope.data.migrations[i];
986
+ const integrity = envelope.data.integrity.entries[i];
987
+ if (!this.isValidMigrationRecord(migration)) {
988
+ throw new Error("Invalid persisted migration record");
989
+ }
990
+ if (!integrity || integrity.recordId !== migration.id || integrity.previousHash !== previousHash) {
991
+ throw new Error("Migration integrity chain mismatch");
992
+ }
993
+ const expectedHash = await this.computeDigestHex(
994
+ `${previousHash}|${this.stableStringify({
995
+ ...migration,
996
+ previousHash: void 0,
997
+ integrityHash: void 0
998
+ })}`
999
+ );
1000
+ if (expectedHash !== integrity.hash) {
1001
+ throw new Error("Migration integrity verification failed");
1002
+ }
1003
+ validatedMigrations.push({
1004
+ ...migration,
1005
+ previousHash: integrity.previousHash,
1006
+ integrityHash: integrity.hash
1007
+ });
1008
+ previousHash = expectedHash;
1009
+ }
1010
+ if (previousHash !== envelope.data.integrity.rootHash) {
1011
+ throw new Error("Migration integrity root hash mismatch");
1012
+ }
1013
+ const validatedSnapshots = /* @__PURE__ */ new Map();
1014
+ for (const snapshot of envelope.data.snapshots) {
1015
+ if (typeof snapshot.recordId !== "string" || typeof snapshot.beforeHash !== "string" || typeof snapshot.afterHash !== "string" || typeof snapshot.itemCount !== "number") {
1016
+ throw new Error("Invalid persisted migration snapshot");
1017
+ }
1018
+ validatedSnapshots.set(snapshot.recordId, {
1019
+ beforeHash: snapshot.beforeHash,
1020
+ afterHash: snapshot.afterHash,
1021
+ itemCount: snapshot.itemCount
1022
+ });
1023
+ }
1024
+ this.migrations = validatedMigrations;
1025
+ this.snapshots = validatedSnapshots;
1026
+ logger.debug("[MigrationTracker] Loaded from persistence", {
1027
+ key: this.persistence.key,
1028
+ migrations: this.migrations.length,
1029
+ snapshots: this.snapshots.size
1030
+ });
1031
+ return {
1032
+ migrations: this.migrations.length,
1033
+ snapshots: this.snapshots.size
1034
+ };
1035
+ }
1036
+ /**
1037
+ * Remove persisted migration tracker state.
1038
+ */
1039
+ async clearPersistence() {
1040
+ if (!this.persistence) {
1041
+ return;
1042
+ }
1043
+ await this.persistence.adapter.removeItem(this.persistence.key);
1044
+ }
1045
+ /**
1046
+ * Clear history (for testing)
1047
+ */
1048
+ clear() {
1049
+ this.migrations = [];
1050
+ this.snapshots.clear();
1051
+ this.schedulePersist();
1052
+ }
1053
+ /**
1054
+ * Get total migrations tracked
1055
+ */
1056
+ getTotalMigrations() {
1057
+ return this.migrations.length;
1058
+ }
1059
+ /**
1060
+ * Find migrations by time range
1061
+ */
1062
+ getMigrationsByTimeRange(startTime, endTime) {
1063
+ const start = new Date(startTime).getTime();
1064
+ const end = new Date(endTime).getTime();
1065
+ return this.migrations.filter((m) => {
1066
+ const time = new Date(m.timestamp).getTime();
1067
+ return time >= start && time <= end;
1068
+ });
1069
+ }
1070
+ schedulePersist() {
1071
+ if (!this.persistence || this.persistence.autoPersist === false) {
1072
+ return;
1073
+ }
1074
+ if (this.persistTimer) {
1075
+ clearTimeout(this.persistTimer);
1076
+ }
1077
+ this.persistTimer = setTimeout(() => {
1078
+ void this.persistSafely();
1079
+ }, this.persistence.persistDebounceMs ?? 25);
1080
+ }
1081
+ async persistSafely() {
1082
+ if (!this.persistence) {
1083
+ return;
1084
+ }
1085
+ if (this.persistInFlight) {
1086
+ this.persistPending = true;
1087
+ return;
1088
+ }
1089
+ this.persistInFlight = true;
1090
+ try {
1091
+ await this.saveToPersistence();
1092
+ } catch (error) {
1093
+ logger.error("[MigrationTracker] Persistence write failed", {
1094
+ key: this.persistence.key,
1095
+ error: error instanceof Error ? error.message : String(error)
1096
+ });
1097
+ } finally {
1098
+ this.persistInFlight = false;
1099
+ const shouldRunAgain = this.persistPending;
1100
+ this.persistPending = false;
1101
+ if (shouldRunAgain) {
1102
+ void this.persistSafely();
1103
+ }
1104
+ }
1105
+ }
1106
+ isValidMigrationRecord(value) {
1107
+ if (typeof value !== "object" || value === null) {
1108
+ return false;
1109
+ }
1110
+ const record = value;
1111
+ const validDirection = record.direction === "up" || record.direction === "down";
1112
+ const validStatus = record.status === "pending" || record.status === "applied" || record.status === "failed" || record.status === "rolled-back";
1113
+ return typeof record.id === "string" && typeof record.migrationId === "string" && typeof record.timestamp === "string" && typeof record.version === "string" && validDirection && validStatus && typeof record.duration === "number" && typeof record.itemsAffected === "number" && typeof record.appliedBy === "string";
1114
+ }
1115
+ stableStringify(value) {
1116
+ if (value === null || typeof value !== "object") {
1117
+ return JSON.stringify(value);
1118
+ }
1119
+ if (Array.isArray(value)) {
1120
+ return `[${value.map((item) => this.stableStringify(item)).join(",")}]`;
1121
+ }
1122
+ const entries = Object.entries(value).sort(
1123
+ ([a], [b]) => a.localeCompare(b)
1124
+ );
1125
+ return `{${entries.map(
1126
+ ([key, entryValue]) => `${JSON.stringify(key)}:${this.stableStringify(entryValue)}`
1127
+ ).join(",")}}`;
1128
+ }
1129
+ async computeDigestHex(value) {
1130
+ if (globalThis.crypto?.subtle) {
1131
+ const bytes = new TextEncoder().encode(value);
1132
+ const normalized = bytes.buffer.slice(
1133
+ bytes.byteOffset,
1134
+ bytes.byteOffset + bytes.byteLength
1135
+ );
1136
+ const digest = await globalThis.crypto.subtle.digest(
1137
+ "SHA-256",
1138
+ normalized
1139
+ );
1140
+ return this.toHex(new Uint8Array(digest));
1141
+ }
1142
+ return this.fallbackDigestHex(value);
1143
+ }
1144
+ toHex(bytes) {
1145
+ return Array.from(bytes).map((byte) => byte.toString(16).padStart(2, "0")).join("");
1146
+ }
1147
+ fallbackDigestHex(value) {
1148
+ let hash = 2166136261;
1149
+ for (let i = 0; i < value.length; i++) {
1150
+ hash ^= value.charCodeAt(i);
1151
+ hash = Math.imul(hash, 16777619);
1152
+ }
1153
+ return (hash >>> 0).toString(16).padStart(8, "0");
1154
+ }
1155
+ };
1156
+
1157
+ export { DataTransformer, MigrationEngine, MigrationTracker, SchemaVersionManager };
1158
+ //# sourceMappingURL=index.js.map
1159
+ //# sourceMappingURL=index.js.map