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