File size: 10,770 Bytes
da2e594
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/**
 * Node Version Service
 *
 * Central service for node version discovery, comparison, and upgrade path recommendation.
 * Provides caching for performance and integrates with the database and breaking change detector.
 */

import { NodeRepository } from '../database/node-repository';
import { BreakingChangeDetector } from './breaking-change-detector';

export interface NodeVersion {
  nodeType: string;
  version: string;
  packageName: string;
  displayName: string;
  isCurrentMax: boolean;
  minimumN8nVersion?: string;
  breakingChanges: any[];
  deprecatedProperties: string[];
  addedProperties: string[];
  releasedAt?: Date;
}

export interface VersionComparison {
  nodeType: string;
  currentVersion: string;
  latestVersion: string;
  isOutdated: boolean;
  versionGap: number; // How many versions behind
  hasBreakingChanges: boolean;
  recommendUpgrade: boolean;
  confidence: 'HIGH' | 'MEDIUM' | 'LOW';
  reason: string;
}

export interface UpgradePath {
  nodeType: string;
  fromVersion: string;
  toVersion: string;
  direct: boolean; // Can upgrade directly or needs intermediate steps
  intermediateVersions: string[]; // If multi-step upgrade needed
  totalBreakingChanges: number;
  autoMigratableChanges: number;
  manualRequiredChanges: number;
  estimatedEffort: 'LOW' | 'MEDIUM' | 'HIGH';
  steps: UpgradeStep[];
}

export interface UpgradeStep {
  fromVersion: string;
  toVersion: string;
  breakingChanges: number;
  migrationHints: string[];
}

/**
 * Node Version Service with caching
 */
export class NodeVersionService {
  private versionCache: Map<string, NodeVersion[]> = new Map();
  private cacheTTL: number = 5 * 60 * 1000; // 5 minutes
  private cacheTimestamps: Map<string, number> = new Map();

  constructor(
    private nodeRepository: NodeRepository,
    private breakingChangeDetector: BreakingChangeDetector
  ) {}

  /**
   * Get all available versions for a node type
   */
  getAvailableVersions(nodeType: string): NodeVersion[] {
    // Check cache first
    const cached = this.getCachedVersions(nodeType);
    if (cached) return cached;

    // Query from database
    const versions = this.nodeRepository.getNodeVersions(nodeType);

    // Cache the result
    this.cacheVersions(nodeType, versions);

    return versions;
  }

  /**
   * Get the latest available version for a node type
   */
  getLatestVersion(nodeType: string): string | null {
    const versions = this.getAvailableVersions(nodeType);

    if (versions.length === 0) {
      // Fallback to main nodes table
      const node = this.nodeRepository.getNode(nodeType);
      return node?.version || null;
    }

    // Find version marked as current max
    const maxVersion = versions.find(v => v.isCurrentMax);
    if (maxVersion) return maxVersion.version;

    // Fallback: sort and get highest
    const sorted = versions.sort((a, b) => this.compareVersions(b.version, a.version));
    return sorted[0]?.version || null;
  }

  /**
   * Compare a node's current version against the latest available
   */
  compareVersions(currentVersion: string, latestVersion: string): number {
    const parts1 = currentVersion.split('.').map(Number);
    const parts2 = latestVersion.split('.').map(Number);

    for (let i = 0; i < Math.max(parts1.length, parts2.length); i++) {
      const p1 = parts1[i] || 0;
      const p2 = parts2[i] || 0;

      if (p1 < p2) return -1;
      if (p1 > p2) return 1;
    }

    return 0;
  }

  /**
   * Analyze if a node version is outdated and should be upgraded
   */
  analyzeVersion(nodeType: string, currentVersion: string): VersionComparison {
    const latestVersion = this.getLatestVersion(nodeType);

    if (!latestVersion) {
      return {
        nodeType,
        currentVersion,
        latestVersion: currentVersion,
        isOutdated: false,
        versionGap: 0,
        hasBreakingChanges: false,
        recommendUpgrade: false,
        confidence: 'HIGH',
        reason: 'No version information available. Using current version.'
      };
    }

    const comparison = this.compareVersions(currentVersion, latestVersion);
    const isOutdated = comparison < 0;

    if (!isOutdated) {
      return {
        nodeType,
        currentVersion,
        latestVersion,
        isOutdated: false,
        versionGap: 0,
        hasBreakingChanges: false,
        recommendUpgrade: false,
        confidence: 'HIGH',
        reason: 'Node is already at the latest version.'
      };
    }

    // Calculate version gap
    const versionGap = this.calculateVersionGap(currentVersion, latestVersion);

    // Check for breaking changes
    const hasBreakingChanges = this.breakingChangeDetector.hasBreakingChanges(
      nodeType,
      currentVersion,
      latestVersion
    );

    // Determine upgrade recommendation and confidence
    let recommendUpgrade = true;
    let confidence: 'HIGH' | 'MEDIUM' | 'LOW' = 'HIGH';
    let reason = `Version ${latestVersion} available. `;

    if (hasBreakingChanges) {
      confidence = 'MEDIUM';
      reason += 'Contains breaking changes. Review before upgrading.';
    } else {
      reason += 'Safe to upgrade (no breaking changes detected).';
    }

    if (versionGap > 2) {
      confidence = 'LOW';
      reason += ` Version gap is large (${versionGap} versions). Consider incremental upgrade.`;
    }

    return {
      nodeType,
      currentVersion,
      latestVersion,
      isOutdated,
      versionGap,
      hasBreakingChanges,
      recommendUpgrade,
      confidence,
      reason
    };
  }

  /**
   * Calculate the version gap (number of versions between)
   */
  private calculateVersionGap(fromVersion: string, toVersion: string): number {
    const from = fromVersion.split('.').map(Number);
    const to = toVersion.split('.').map(Number);

    // Simple gap calculation based on version numbers
    let gap = 0;

    for (let i = 0; i < Math.max(from.length, to.length); i++) {
      const f = from[i] || 0;
      const t = to[i] || 0;
      gap += Math.abs(t - f);
    }

    return gap;
  }

  /**
   * Suggest the best upgrade path for a node
   */
  async suggestUpgradePath(nodeType: string, currentVersion: string): Promise<UpgradePath | null> {
    const latestVersion = this.getLatestVersion(nodeType);

    if (!latestVersion) return null;

    const comparison = this.compareVersions(currentVersion, latestVersion);
    if (comparison >= 0) return null; // Already at latest or newer

    // Get all available versions between current and latest
    const allVersions = this.getAvailableVersions(nodeType);
    const intermediateVersions = allVersions
      .filter(v =>
        this.compareVersions(v.version, currentVersion) > 0 &&
        this.compareVersions(v.version, latestVersion) < 0
      )
      .map(v => v.version)
      .sort((a, b) => this.compareVersions(a, b));

    // Analyze the upgrade
    const analysis = await this.breakingChangeDetector.analyzeVersionUpgrade(
      nodeType,
      currentVersion,
      latestVersion
    );

    // Determine if direct upgrade is safe
    const versionGap = this.calculateVersionGap(currentVersion, latestVersion);
    const direct = versionGap <= 1 || !analysis.hasBreakingChanges;

    // Generate upgrade steps
    const steps: UpgradeStep[] = [];

    if (direct || intermediateVersions.length === 0) {
      // Direct upgrade
      steps.push({
        fromVersion: currentVersion,
        toVersion: latestVersion,
        breakingChanges: analysis.changes.filter(c => c.isBreaking).length,
        migrationHints: analysis.recommendations
      });
    } else {
      // Multi-step upgrade through intermediate versions
      let stepFrom = currentVersion;

      for (const intermediateVersion of intermediateVersions) {
        const stepAnalysis = await this.breakingChangeDetector.analyzeVersionUpgrade(
          nodeType,
          stepFrom,
          intermediateVersion
        );

        steps.push({
          fromVersion: stepFrom,
          toVersion: intermediateVersion,
          breakingChanges: stepAnalysis.changes.filter(c => c.isBreaking).length,
          migrationHints: stepAnalysis.recommendations
        });

        stepFrom = intermediateVersion;
      }

      // Final step to latest
      const finalStepAnalysis = await this.breakingChangeDetector.analyzeVersionUpgrade(
        nodeType,
        stepFrom,
        latestVersion
      );

      steps.push({
        fromVersion: stepFrom,
        toVersion: latestVersion,
        breakingChanges: finalStepAnalysis.changes.filter(c => c.isBreaking).length,
        migrationHints: finalStepAnalysis.recommendations
      });
    }

    // Calculate estimated effort
    const totalBreakingChanges = steps.reduce((sum, step) => sum + step.breakingChanges, 0);
    let estimatedEffort: 'LOW' | 'MEDIUM' | 'HIGH' = 'LOW';

    if (totalBreakingChanges > 5 || steps.length > 3) {
      estimatedEffort = 'HIGH';
    } else if (totalBreakingChanges > 2 || steps.length > 1) {
      estimatedEffort = 'MEDIUM';
    }

    return {
      nodeType,
      fromVersion: currentVersion,
      toVersion: latestVersion,
      direct,
      intermediateVersions,
      totalBreakingChanges,
      autoMigratableChanges: analysis.autoMigratableCount,
      manualRequiredChanges: analysis.manualRequiredCount,
      estimatedEffort,
      steps
    };
  }

  /**
   * Check if a specific version exists for a node
   */
  versionExists(nodeType: string, version: string): boolean {
    const versions = this.getAvailableVersions(nodeType);
    return versions.some(v => v.version === version);
  }

  /**
   * Get version metadata (breaking changes, added/deprecated properties)
   */
  getVersionMetadata(nodeType: string, version: string): NodeVersion | null {
    const versionData = this.nodeRepository.getNodeVersion(nodeType, version);
    return versionData;
  }

  /**
   * Clear the version cache
   */
  clearCache(nodeType?: string): void {
    if (nodeType) {
      this.versionCache.delete(nodeType);
      this.cacheTimestamps.delete(nodeType);
    } else {
      this.versionCache.clear();
      this.cacheTimestamps.clear();
    }
  }

  /**
   * Get cached versions if still valid
   */
  private getCachedVersions(nodeType: string): NodeVersion[] | null {
    const cached = this.versionCache.get(nodeType);
    const timestamp = this.cacheTimestamps.get(nodeType);

    if (cached && timestamp) {
      const age = Date.now() - timestamp;
      if (age < this.cacheTTL) {
        return cached;
      }
    }

    return null;
  }

  /**
   * Cache versions with timestamp
   */
  private cacheVersions(nodeType: string, versions: NodeVersion[]): void {
    this.versionCache.set(nodeType, versions);
    this.cacheTimestamps.set(nodeType, Date.now());
  }
}