|
1 | 1 | /** |
2 | | - * @fileoverview Check npm registry for package updates |
| 2 | + * @fileoverview Check npm registry for package updates with caching |
| 3 | + * |
| 4 | + * Uses a simple file-based cache in the OS temp directory to avoid |
| 5 | + * hitting npm on every CLI invocation. Cache expires after 1 hour. |
3 | 6 | */ |
4 | 7 |
|
| 8 | +import fs from 'node:fs'; |
5 | 9 | import https from 'https'; |
| 10 | +import os from 'node:os'; |
| 11 | +import path from 'node:path'; |
6 | 12 |
|
7 | 13 | import { fetchChangelogHighlights } from './changelog.js'; |
8 | 14 | import type { UpdateInfo } from './types.js'; |
9 | 15 | import { compareVersions, getCurrentVersion } from './version.js'; |
10 | 16 |
|
| 17 | +// ============================================================================ |
| 18 | +// Cache Configuration |
| 19 | +// ============================================================================ |
| 20 | + |
| 21 | +/** Cache TTL: 1 hour in milliseconds */ |
| 22 | +const CACHE_TTL_MS = 60 * 60 * 1000; |
| 23 | + |
| 24 | +/** Cache file name */ |
| 25 | +const CACHE_FILENAME = 'taskmaster-update-cache.json'; |
| 26 | + |
| 27 | +interface UpdateCache { |
| 28 | + timestamp: number; |
| 29 | + latestVersion: string; |
| 30 | + highlights?: string[]; |
| 31 | +} |
| 32 | + |
| 33 | +// ============================================================================ |
| 34 | +// Cache Operations (Single Responsibility: cache I/O) |
| 35 | +// ============================================================================ |
| 36 | + |
11 | 37 | /** |
12 | | - * Check for newer version of task-master-ai |
| 38 | + * Get the path to the update cache file in OS temp directory |
13 | 39 | */ |
14 | | -export async function checkForUpdate( |
15 | | - currentVersionOverride?: string |
16 | | -): Promise<UpdateInfo> { |
17 | | - const currentVersion = currentVersionOverride || getCurrentVersion(); |
| 40 | +const getCachePath = (): string => path.join(os.tmpdir(), CACHE_FILENAME); |
18 | 41 |
|
19 | | - return new Promise((resolve) => { |
20 | | - const options = { |
21 | | - hostname: 'registry.npmjs.org', |
22 | | - path: '/task-master-ai', |
23 | | - method: 'GET', |
24 | | - headers: { |
25 | | - Accept: 'application/vnd.npm.install-v1+json', |
26 | | - 'User-Agent': `task-master-ai/${currentVersion}` |
27 | | - } |
28 | | - }; |
| 42 | +/** |
| 43 | + * Read cached update info if still valid |
| 44 | + * @returns Cached data or null if expired/missing/invalid |
| 45 | + */ |
| 46 | +function readCache(): UpdateCache | null { |
| 47 | + try { |
| 48 | + const cachePath = getCachePath(); |
| 49 | + if (!fs.existsSync(cachePath)) return null; |
29 | 50 |
|
30 | | - const req = https.request(options, (res) => { |
31 | | - let data = ''; |
| 51 | + const data: UpdateCache = JSON.parse(fs.readFileSync(cachePath, 'utf-8')); |
| 52 | + const isExpired = Date.now() - data.timestamp > CACHE_TTL_MS; |
32 | 53 |
|
33 | | - res.on('data', (chunk) => { |
34 | | - data += chunk; |
35 | | - }); |
| 54 | + return isExpired ? null : data; |
| 55 | + } catch { |
| 56 | + return null; |
| 57 | + } |
| 58 | +} |
36 | 59 |
|
37 | | - res.on('end', async () => { |
38 | | - try { |
39 | | - if (res.statusCode !== 200) |
40 | | - throw new Error(`npm registry status ${res.statusCode}`); |
41 | | - const npmData = JSON.parse(data); |
42 | | - const latestVersion = npmData['dist-tags']?.latest || currentVersion; |
| 60 | +/** |
| 61 | + * Write update info to cache |
| 62 | + */ |
| 63 | +function writeCache(latestVersion: string, highlights?: string[]): void { |
| 64 | + try { |
| 65 | + fs.writeFileSync( |
| 66 | + getCachePath(), |
| 67 | + JSON.stringify( |
| 68 | + { |
| 69 | + timestamp: Date.now(), |
| 70 | + latestVersion, |
| 71 | + highlights |
| 72 | + } satisfies UpdateCache, |
| 73 | + null, |
| 74 | + 2 |
| 75 | + ) |
| 76 | + ); |
| 77 | + } catch { |
| 78 | + // Cache write failures are non-critical - silently ignore |
| 79 | + } |
| 80 | +} |
43 | 81 |
|
44 | | - const needsUpdate = |
45 | | - compareVersions(currentVersion, latestVersion) < 0; |
| 82 | +// ============================================================================ |
| 83 | +// NPM Registry Operations (Single Responsibility: npm API) |
| 84 | +// ============================================================================ |
46 | 85 |
|
47 | | - // Fetch highlights if update is needed |
48 | | - let highlights: string[] | undefined; |
49 | | - if (needsUpdate) { |
50 | | - highlights = await fetchChangelogHighlights(latestVersion); |
51 | | - } |
| 86 | +/** Request timeout for npm registry */ |
| 87 | +const NPM_TIMEOUT_MS = 3000; |
52 | 88 |
|
53 | | - resolve({ |
54 | | - currentVersion, |
55 | | - latestVersion, |
56 | | - needsUpdate, |
57 | | - highlights |
58 | | - }); |
59 | | - } catch { |
60 | | - resolve({ |
61 | | - currentVersion, |
62 | | - latestVersion: currentVersion, |
63 | | - needsUpdate: false |
64 | | - }); |
| 89 | +/** |
| 90 | + * Fetch latest version from npm registry |
| 91 | + * @returns Latest version string or null on failure |
| 92 | + */ |
| 93 | +function fetchLatestVersion(currentVersion: string): Promise<string | null> { |
| 94 | + return new Promise((resolve) => { |
| 95 | + const req = https.request( |
| 96 | + { |
| 97 | + hostname: 'registry.npmjs.org', |
| 98 | + path: '/task-master-ai', |
| 99 | + method: 'GET', |
| 100 | + headers: { |
| 101 | + Accept: 'application/vnd.npm.install-v1+json', |
| 102 | + 'User-Agent': `task-master-ai/${currentVersion}` |
65 | 103 | } |
66 | | - }); |
67 | | - }); |
68 | | - |
69 | | - req.on('error', () => { |
70 | | - resolve({ |
71 | | - currentVersion, |
72 | | - latestVersion: currentVersion, |
73 | | - needsUpdate: false |
74 | | - }); |
75 | | - }); |
| 104 | + }, |
| 105 | + (res) => { |
| 106 | + let data = ''; |
| 107 | + res.on('data', (chunk) => (data += chunk)); |
| 108 | + res.on('end', () => { |
| 109 | + try { |
| 110 | + if (res.statusCode !== 200) { |
| 111 | + resolve(null); |
| 112 | + return; |
| 113 | + } |
| 114 | + const npmData = JSON.parse(data); |
| 115 | + resolve(npmData['dist-tags']?.latest || null); |
| 116 | + } catch { |
| 117 | + resolve(null); |
| 118 | + } |
| 119 | + }); |
| 120 | + } |
| 121 | + ); |
76 | 122 |
|
77 | | - req.setTimeout(3000, () => { |
| 123 | + req.on('error', () => resolve(null)); |
| 124 | + req.setTimeout(NPM_TIMEOUT_MS, () => { |
78 | 125 | req.destroy(); |
79 | | - resolve({ |
80 | | - currentVersion, |
81 | | - latestVersion: currentVersion, |
82 | | - needsUpdate: false |
83 | | - }); |
| 126 | + resolve(null); |
84 | 127 | }); |
85 | | - |
86 | 128 | req.end(); |
87 | 129 | }); |
88 | 130 | } |
| 131 | + |
| 132 | +// ============================================================================ |
| 133 | +// Public API |
| 134 | +// ============================================================================ |
| 135 | + |
| 136 | +/** |
| 137 | + * Build UpdateInfo response |
| 138 | + */ |
| 139 | +function buildUpdateInfo( |
| 140 | + currentVersion: string, |
| 141 | + latestVersion: string, |
| 142 | + highlights?: string[] |
| 143 | +): UpdateInfo { |
| 144 | + return { |
| 145 | + currentVersion, |
| 146 | + latestVersion, |
| 147 | + needsUpdate: compareVersions(currentVersion, latestVersion) < 0, |
| 148 | + highlights |
| 149 | + }; |
| 150 | +} |
| 151 | + |
| 152 | +/** |
| 153 | + * Check for newer version of task-master-ai |
| 154 | + * Uses a 1-hour cache to avoid hitting npm on every CLI invocation |
| 155 | + */ |
| 156 | +export async function checkForUpdate( |
| 157 | + currentVersionOverride?: string |
| 158 | +): Promise<UpdateInfo> { |
| 159 | + const currentVersion = currentVersionOverride || getCurrentVersion(); |
| 160 | + |
| 161 | + // Return cached result if valid |
| 162 | + const cached = readCache(); |
| 163 | + if (cached) { |
| 164 | + return buildUpdateInfo( |
| 165 | + currentVersion, |
| 166 | + cached.latestVersion, |
| 167 | + cached.highlights |
| 168 | + ); |
| 169 | + } |
| 170 | + |
| 171 | + // Fetch from npm registry |
| 172 | + const latestVersion = await fetchLatestVersion(currentVersion); |
| 173 | + if (!latestVersion) { |
| 174 | + return buildUpdateInfo(currentVersion, currentVersion); |
| 175 | + } |
| 176 | + |
| 177 | + // Fetch changelog highlights if update available |
| 178 | + const needsUpdate = compareVersions(currentVersion, latestVersion) < 0; |
| 179 | + const highlights = needsUpdate |
| 180 | + ? await fetchChangelogHighlights(latestVersion) |
| 181 | + : undefined; |
| 182 | + |
| 183 | + // Cache result |
| 184 | + writeCache(latestVersion, highlights); |
| 185 | + |
| 186 | + return buildUpdateInfo(currentVersion, latestVersion, highlights); |
| 187 | +} |
0 commit comments