@nekzus/mcp-server 1.26.0 → 1.26.1-alpha.2
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.
- package/README.md +35 -27
- package/dist/.tsbuildinfo +1 -1
- package/dist/index.d.ts +52 -15
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +184 -166
- package/dist/index.js.map +1 -1
- package/dist/src/schemas.d.ts +53 -15
- package/dist/src/schemas.d.ts.map +1 -1
- package/dist/src/schemas.js +68 -44
- package/dist/src/schemas.js.map +1 -1
- package/dist/src/utils/normalize-package.d.ts +10 -0
- package/dist/src/utils/normalize-package.d.ts.map +1 -0
- package/dist/src/utils/normalize-package.js +123 -0
- package/dist/src/utils/normalize-package.js.map +1 -0
- package/package.json +12 -25
- package/smithery.yaml +0 -30
package/dist/index.js
CHANGED
|
@@ -6,103 +6,13 @@ import { serveStdio } from '@modelcontextprotocol/server/stdio';
|
|
|
6
6
|
import fetch from 'node-fetch';
|
|
7
7
|
import { z } from 'zod';
|
|
8
8
|
import createServer, { createMcpServer } from './src/server.js';
|
|
9
|
+
import { extractAuthorString, extractBugsUrl, extractLicenseString, extractRepositoryUrl, extractTypesPath, } from './src/utils/normalize-package.js';
|
|
9
10
|
import { needsVersionResolution, resolvePackageVersion } from './src/utils/version-resolver.js';
|
|
10
11
|
// Cache configuration
|
|
11
12
|
export let NPM_REGISTRY_URL = (process.env.NPM_REGISTRY_URL || 'https://registry.npmjs.org').replace(/\/$/, '');
|
|
12
13
|
export function setNpmRegistryUrl(url) {
|
|
13
14
|
NPM_REGISTRY_URL = url.replace(/\/$/, '');
|
|
14
15
|
}
|
|
15
|
-
// Cache configuration
|
|
16
|
-
const CACHE_TTL_SHORT = 5 * 60 * 1000; // 5 minutes
|
|
17
|
-
const CACHE_TTL_MEDIUM = 60 * 60 * 1000; // 1 hour
|
|
18
|
-
const CACHE_TTL_LONG = 6 * 60 * 60 * 1000; // 6 hours
|
|
19
|
-
const CACHE_TTL_VERY_LONG = 24 * 60 * 60 * 1000; // 24 hours
|
|
20
|
-
const MAX_CACHE_SIZE = 500; // Max number of items in cache
|
|
21
|
-
async function resolveVersionIfShorthand(name, version) {
|
|
22
|
-
if (!needsVersionResolution(version)) {
|
|
23
|
-
return version;
|
|
24
|
-
}
|
|
25
|
-
const cacheKey = generateCacheKey('abbreviatedPackument', name);
|
|
26
|
-
let packument = cacheGet(cacheKey);
|
|
27
|
-
if (!packument) {
|
|
28
|
-
try {
|
|
29
|
-
const res = await fetchWithRetry(`${NPM_REGISTRY_URL}/${encodeURIComponent(name)}`, {
|
|
30
|
-
headers: { Accept: 'application/vnd.npm.install-v1+json' },
|
|
31
|
-
});
|
|
32
|
-
if (res.ok) {
|
|
33
|
-
packument = await res.json();
|
|
34
|
-
cacheSet(cacheKey, packument, CACHE_TTL_SHORT);
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
catch {
|
|
38
|
-
// Ignore error and fall back to original version
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
if (packument) {
|
|
42
|
-
const resolved = resolvePackageVersion(packument, version);
|
|
43
|
-
if (resolved)
|
|
44
|
-
return resolved;
|
|
45
|
-
}
|
|
46
|
-
return version;
|
|
47
|
-
}
|
|
48
|
-
const apiCache = new Map();
|
|
49
|
-
let currentLockfileHash = null;
|
|
50
|
-
function getLockfileHash() {
|
|
51
|
-
const lockfiles = ['pnpm-lock.yaml', 'package-lock.json', 'yarn.lock'];
|
|
52
|
-
for (const lockfile of lockfiles) {
|
|
53
|
-
const fullPath = path.join(process.cwd(), lockfile);
|
|
54
|
-
if (fs.existsSync(fullPath)) {
|
|
55
|
-
try {
|
|
56
|
-
const content = fs.readFileSync(fullPath);
|
|
57
|
-
return crypto.createHash('md5').update(content).digest('hex');
|
|
58
|
-
}
|
|
59
|
-
catch (e) {
|
|
60
|
-
console.error(`Error reading lockfile ${lockfile}:`, e);
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
return null;
|
|
65
|
-
}
|
|
66
|
-
// Initialize hash
|
|
67
|
-
currentLockfileHash = getLockfileHash();
|
|
68
|
-
function checkCacheInvalidation() {
|
|
69
|
-
const newHash = getLockfileHash();
|
|
70
|
-
if (newHash !== currentLockfileHash) {
|
|
71
|
-
console.error('[Cache] Lockfile changed, invalidating all cache entries.');
|
|
72
|
-
apiCache.clear();
|
|
73
|
-
currentLockfileHash = newHash;
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
function generateCacheKey(toolName, ...args) {
|
|
77
|
-
// Simple key generation, ensure consistent order and stringification of args
|
|
78
|
-
return `${toolName}:${args.map((arg) => String(arg)).join(':')}`;
|
|
79
|
-
}
|
|
80
|
-
function cacheGet(key) {
|
|
81
|
-
// Check for global invalidation first
|
|
82
|
-
checkCacheInvalidation();
|
|
83
|
-
const entry = apiCache.get(key);
|
|
84
|
-
if (entry && entry.expiresAt > Date.now()) {
|
|
85
|
-
return entry.data;
|
|
86
|
-
}
|
|
87
|
-
if (entry && entry.expiresAt <= Date.now()) {
|
|
88
|
-
apiCache.delete(key); // Remove stale entry
|
|
89
|
-
}
|
|
90
|
-
return undefined;
|
|
91
|
-
}
|
|
92
|
-
function cacheSet(key, value, ttlMilliseconds) {
|
|
93
|
-
if (ttlMilliseconds <= 0)
|
|
94
|
-
return; // Do not cache if TTL is zero or negative
|
|
95
|
-
const expiresAt = Date.now() + ttlMilliseconds;
|
|
96
|
-
apiCache.set(key, { data: value, expiresAt });
|
|
97
|
-
// Basic FIFO eviction strategy if cache exceeds max size
|
|
98
|
-
if (apiCache.size > MAX_CACHE_SIZE) {
|
|
99
|
-
// To make it FIFO, we need to ensure Map iteration order is insertion order (which it is)
|
|
100
|
-
const oldestKey = apiCache.keys().next().value;
|
|
101
|
-
if (oldestKey) {
|
|
102
|
-
apiCache.delete(oldestKey);
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
16
|
// HTTP wrapper configuration
|
|
107
17
|
const HTTP_MAX_RETRIES = 3;
|
|
108
18
|
const HTTP_INITIAL_BACKOFF_MS = 1000;
|
|
@@ -173,6 +83,97 @@ export async function fetchWithRetry(url, options = {}, config = {}) {
|
|
|
173
83
|
}
|
|
174
84
|
}
|
|
175
85
|
}
|
|
86
|
+
// Cache configuration
|
|
87
|
+
const CACHE_TTL_SHORT = 5 * 60 * 1000; // 5 minutes
|
|
88
|
+
const CACHE_TTL_MEDIUM = 60 * 60 * 1000; // 1 hour
|
|
89
|
+
const CACHE_TTL_LONG = 6 * 60 * 60 * 1000; // 6 hours
|
|
90
|
+
const CACHE_TTL_VERY_LONG = 24 * 60 * 60 * 1000; // 24 hours
|
|
91
|
+
const MAX_CACHE_SIZE = 500; // Max number of items in cache
|
|
92
|
+
const apiCache = new Map();
|
|
93
|
+
let currentLockfileHash = null;
|
|
94
|
+
function getLockfileHash() {
|
|
95
|
+
const lockfiles = ['pnpm-lock.yaml', 'package-lock.json', 'yarn.lock'];
|
|
96
|
+
for (const lockfile of lockfiles) {
|
|
97
|
+
const fullPath = path.join(process.cwd(), lockfile);
|
|
98
|
+
if (fs.existsSync(fullPath)) {
|
|
99
|
+
try {
|
|
100
|
+
const content = fs.readFileSync(fullPath);
|
|
101
|
+
return crypto.createHash('md5').update(content).digest('hex');
|
|
102
|
+
}
|
|
103
|
+
catch (e) {
|
|
104
|
+
console.error(`Error reading lockfile ${lockfile}:`, e);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return null;
|
|
109
|
+
}
|
|
110
|
+
// Initialize hash
|
|
111
|
+
currentLockfileHash = getLockfileHash();
|
|
112
|
+
function checkCacheInvalidation() {
|
|
113
|
+
const newHash = getLockfileHash();
|
|
114
|
+
if (newHash !== currentLockfileHash) {
|
|
115
|
+
console.error('[Cache] Lockfile changed, invalidating all cache entries.');
|
|
116
|
+
apiCache.clear();
|
|
117
|
+
currentLockfileHash = newHash;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
function generateCacheKey(toolName, ...args) {
|
|
121
|
+
// Simple key generation, ensure consistent order and stringification of args
|
|
122
|
+
return `${toolName}:${args.map((arg) => String(arg)).join(':')}`;
|
|
123
|
+
}
|
|
124
|
+
function cacheGet(key) {
|
|
125
|
+
// Check for global invalidation first
|
|
126
|
+
checkCacheInvalidation();
|
|
127
|
+
const entry = apiCache.get(key);
|
|
128
|
+
if (entry && entry.expiresAt > Date.now()) {
|
|
129
|
+
return entry.data;
|
|
130
|
+
}
|
|
131
|
+
if (entry && entry.expiresAt <= Date.now()) {
|
|
132
|
+
apiCache.delete(key); // Remove stale entry
|
|
133
|
+
}
|
|
134
|
+
return undefined;
|
|
135
|
+
}
|
|
136
|
+
function cacheSet(key, value, ttlMilliseconds) {
|
|
137
|
+
if (ttlMilliseconds <= 0)
|
|
138
|
+
return; // Do not cache if TTL is zero or negative
|
|
139
|
+
const expiresAt = Date.now() + ttlMilliseconds;
|
|
140
|
+
apiCache.set(key, { data: value, expiresAt });
|
|
141
|
+
// Basic FIFO eviction strategy if cache exceeds max size
|
|
142
|
+
if (apiCache.size > MAX_CACHE_SIZE) {
|
|
143
|
+
// To make it FIFO, we need to ensure Map iteration order is insertion order (which it is)
|
|
144
|
+
const oldestKey = apiCache.keys().next().value;
|
|
145
|
+
if (oldestKey) {
|
|
146
|
+
apiCache.delete(oldestKey);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
async function resolveVersionIfShorthand(name, version) {
|
|
151
|
+
if (!needsVersionResolution(version)) {
|
|
152
|
+
return version;
|
|
153
|
+
}
|
|
154
|
+
const cacheKey = generateCacheKey('abbreviatedPackument', name);
|
|
155
|
+
let packument = cacheGet(cacheKey);
|
|
156
|
+
if (!packument) {
|
|
157
|
+
try {
|
|
158
|
+
const res = await fetchWithRetry(`${NPM_REGISTRY_URL}/${encodeURIComponent(name)}`, {
|
|
159
|
+
headers: { Accept: 'application/vnd.npm.install-v1+json' },
|
|
160
|
+
});
|
|
161
|
+
if (res.ok) {
|
|
162
|
+
packument = await res.json();
|
|
163
|
+
cacheSet(cacheKey, packument, CACHE_TTL_SHORT);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
catch {
|
|
167
|
+
// Ignore error and fall back to original version
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
if (packument) {
|
|
171
|
+
const resolved = resolvePackageVersion(packument, version);
|
|
172
|
+
if (resolved)
|
|
173
|
+
return resolved;
|
|
174
|
+
}
|
|
175
|
+
return version;
|
|
176
|
+
}
|
|
176
177
|
// Zod schemas for npm package data
|
|
177
178
|
export const NpmMaintainerSchema = z
|
|
178
179
|
.object({
|
|
@@ -181,43 +182,69 @@ export const NpmMaintainerSchema = z
|
|
|
181
182
|
url: z.string().optional(),
|
|
182
183
|
})
|
|
183
184
|
.loose();
|
|
184
|
-
export const
|
|
185
|
-
.
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
.
|
|
191
|
-
z.string(),
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
.optional(),
|
|
201
|
-
license: z.string().optional(),
|
|
202
|
-
repository: z
|
|
185
|
+
export const NpmPersonSchema = z
|
|
186
|
+
.union([
|
|
187
|
+
z.string(),
|
|
188
|
+
z
|
|
189
|
+
.object({
|
|
190
|
+
name: z.string().optional(),
|
|
191
|
+
email: z.string().optional(),
|
|
192
|
+
url: z.string().optional(),
|
|
193
|
+
})
|
|
194
|
+
.loose(),
|
|
195
|
+
])
|
|
196
|
+
.optional();
|
|
197
|
+
export const NpmLicenseSchema = z
|
|
198
|
+
.union([
|
|
199
|
+
z.string(),
|
|
200
|
+
z
|
|
203
201
|
.object({
|
|
204
202
|
type: z.string().optional(),
|
|
205
203
|
url: z.string().optional(),
|
|
206
204
|
})
|
|
207
|
-
.loose()
|
|
208
|
-
|
|
209
|
-
|
|
205
|
+
.loose(),
|
|
206
|
+
z.array(z.any()),
|
|
207
|
+
])
|
|
208
|
+
.optional();
|
|
209
|
+
export const NpmRepositorySchema = z
|
|
210
|
+
.union([
|
|
211
|
+
z.string(),
|
|
212
|
+
z
|
|
210
213
|
.object({
|
|
214
|
+
type: z.string().optional(),
|
|
211
215
|
url: z.string().optional(),
|
|
216
|
+
directory: z.string().optional(),
|
|
212
217
|
})
|
|
213
|
-
.loose()
|
|
214
|
-
|
|
218
|
+
.loose(),
|
|
219
|
+
])
|
|
220
|
+
.optional();
|
|
221
|
+
export const NpmBugsSchema = z
|
|
222
|
+
.union([
|
|
223
|
+
z.string(),
|
|
224
|
+
z
|
|
225
|
+
.object({
|
|
226
|
+
url: z.string().optional(),
|
|
227
|
+
email: z.string().optional(),
|
|
228
|
+
})
|
|
229
|
+
.loose(),
|
|
230
|
+
])
|
|
231
|
+
.optional();
|
|
232
|
+
export const NpmPackageVersionSchema = z
|
|
233
|
+
.object({
|
|
234
|
+
name: z.string(),
|
|
235
|
+
version: z.string(),
|
|
236
|
+
description: z.string().optional(),
|
|
237
|
+
author: NpmPersonSchema,
|
|
238
|
+
license: NpmLicenseSchema,
|
|
239
|
+
repository: NpmRepositorySchema,
|
|
240
|
+
bugs: NpmBugsSchema,
|
|
215
241
|
homepage: z.string().optional(),
|
|
216
242
|
dependencies: z.record(z.string(), z.string()).optional(),
|
|
217
243
|
devDependencies: z.record(z.string(), z.string()).optional(),
|
|
218
244
|
peerDependencies: z.record(z.string(), z.string()).optional(),
|
|
219
245
|
types: z.string().optional(),
|
|
220
246
|
typings: z.string().optional(),
|
|
247
|
+
exports: z.any().optional(),
|
|
221
248
|
dist: z
|
|
222
249
|
.object({ shasum: z.string().optional(), tarball: z.string().optional() })
|
|
223
250
|
.loose()
|
|
@@ -230,19 +257,8 @@ export const NpmPackageInfoSchema = z
|
|
|
230
257
|
'dist-tags': z.record(z.string(), z.string()),
|
|
231
258
|
versions: z.record(z.string(), NpmPackageVersionSchema),
|
|
232
259
|
time: z.record(z.string(), z.string()).optional(),
|
|
233
|
-
repository:
|
|
234
|
-
|
|
235
|
-
type: z.string().optional(),
|
|
236
|
-
url: z.string().optional(),
|
|
237
|
-
})
|
|
238
|
-
.loose()
|
|
239
|
-
.optional(),
|
|
240
|
-
bugs: z
|
|
241
|
-
.object({
|
|
242
|
-
url: z.string().optional(),
|
|
243
|
-
})
|
|
244
|
-
.loose()
|
|
245
|
-
.optional(),
|
|
260
|
+
repository: NpmRepositorySchema,
|
|
261
|
+
bugs: NpmBugsSchema,
|
|
246
262
|
homepage: z.string().optional(),
|
|
247
263
|
maintainers: z.array(NpmMaintainerSchema).optional(),
|
|
248
264
|
})
|
|
@@ -252,12 +268,13 @@ export const NpmPackageDataSchema = z
|
|
|
252
268
|
name: z.string(),
|
|
253
269
|
version: z.string(),
|
|
254
270
|
description: z.string().optional(),
|
|
255
|
-
license:
|
|
271
|
+
license: NpmLicenseSchema,
|
|
256
272
|
dependencies: z.record(z.string(), z.string()).optional(),
|
|
257
273
|
devDependencies: z.record(z.string(), z.string()).optional(),
|
|
258
274
|
peerDependencies: z.record(z.string(), z.string()).optional(),
|
|
259
275
|
types: z.string().optional(),
|
|
260
276
|
typings: z.string().optional(),
|
|
277
|
+
exports: z.any().optional(),
|
|
261
278
|
})
|
|
262
279
|
.loose();
|
|
263
280
|
export const BundlephobiaDataSchema = z.object({
|
|
@@ -315,12 +332,7 @@ function isNpmPackageInfo(data) {
|
|
|
315
332
|
data !== null &&
|
|
316
333
|
(!('maintainers' in data) ||
|
|
317
334
|
(Array.isArray(data.maintainers) &&
|
|
318
|
-
(data.maintainers?.every((m) => typeof m === 'object' &&
|
|
319
|
-
m !== null &&
|
|
320
|
-
'name' in m &&
|
|
321
|
-
'email' in m &&
|
|
322
|
-
typeof m.name === 'string' &&
|
|
323
|
-
typeof m.email === 'string') ??
|
|
335
|
+
(data.maintainers?.every((m) => typeof m === 'object' && m !== null && 'name' in m && typeof m.name === 'string') ??
|
|
324
336
|
true))));
|
|
325
337
|
}
|
|
326
338
|
function isNpmPackageData(data) {
|
|
@@ -438,7 +450,9 @@ export async function handleNpmVersions(args) {
|
|
|
438
450
|
};
|
|
439
451
|
}
|
|
440
452
|
try {
|
|
441
|
-
const response = await fetchWithRetry(`${NPM_REGISTRY_URL}/${name}
|
|
453
|
+
const response = await fetchWithRetry(`${NPM_REGISTRY_URL}/${encodeURIComponent(name)}`, {
|
|
454
|
+
headers: { Accept: 'application/vnd.npm.install-v1+json' },
|
|
455
|
+
});
|
|
442
456
|
if (!response.ok) {
|
|
443
457
|
return {
|
|
444
458
|
packageInput: pkgInput,
|
|
@@ -450,7 +464,7 @@ export async function handleNpmVersions(args) {
|
|
|
450
464
|
};
|
|
451
465
|
}
|
|
452
466
|
const data = await response.json();
|
|
453
|
-
if (!
|
|
467
|
+
if (!data || typeof data !== 'object' || (!data.versions && !data['dist-tags'])) {
|
|
454
468
|
return {
|
|
455
469
|
packageInput: pkgInput,
|
|
456
470
|
packageName: name,
|
|
@@ -462,7 +476,7 @@ export async function handleNpmVersions(args) {
|
|
|
462
476
|
}
|
|
463
477
|
const allVersions = Object.keys(data.versions || {});
|
|
464
478
|
const tags = data['dist-tags'] || {};
|
|
465
|
-
const latestVersionTag = tags.latest || null;
|
|
479
|
+
const latestVersionTag = tags.latest || (allVersions.length > 0 ? allVersions[allVersions.length - 1] : null);
|
|
466
480
|
const resultData = {
|
|
467
481
|
allVersions,
|
|
468
482
|
tags,
|
|
@@ -570,7 +584,7 @@ export async function handleNpmLatest(args) {
|
|
|
570
584
|
}
|
|
571
585
|
try {
|
|
572
586
|
const resolvedVersion = await resolveVersionIfShorthand(name, versionTag);
|
|
573
|
-
const response = await fetchWithRetry(`${NPM_REGISTRY_URL}/${name}/${resolvedVersion}`);
|
|
587
|
+
const response = await fetchWithRetry(`${NPM_REGISTRY_URL}/${encodeURIComponent(name)}/${encodeURIComponent(resolvedVersion)}`);
|
|
574
588
|
if (!response.ok) {
|
|
575
589
|
let errorMsg = `Failed to fetch package version: ${response.status} ${response.statusText}`;
|
|
576
590
|
if (response.status === 404) {
|
|
@@ -602,16 +616,16 @@ export async function handleNpmLatest(args) {
|
|
|
602
616
|
name: data.name,
|
|
603
617
|
version: data.version,
|
|
604
618
|
description: data.description || null,
|
|
605
|
-
author: (
|
|
606
|
-
license: data.license
|
|
619
|
+
author: extractAuthorString(data.author),
|
|
620
|
+
license: extractLicenseString(data.license),
|
|
607
621
|
homepage: data.homepage || null,
|
|
608
|
-
repositoryUrl: data.repository
|
|
609
|
-
bugsUrl: data.bugs
|
|
622
|
+
repositoryUrl: extractRepositoryUrl(data.repository),
|
|
623
|
+
bugsUrl: extractBugsUrl(data.bugs),
|
|
610
624
|
dependenciesCount: Object.keys(data.dependencies || {}).length,
|
|
611
625
|
devDependenciesCount: Object.keys(data.devDependencies || {}).length,
|
|
612
626
|
peerDependenciesCount: Object.keys(data.peerDependencies || {}).length,
|
|
613
627
|
dist: data.dist || null,
|
|
614
|
-
types: data
|
|
628
|
+
types: extractTypesPath(data),
|
|
615
629
|
};
|
|
616
630
|
cacheSet(cacheKey, versionData, CACHE_TTL_MEDIUM);
|
|
617
631
|
return {
|
|
@@ -705,7 +719,7 @@ export async function handleNpmDeps(args) {
|
|
|
705
719
|
}
|
|
706
720
|
try {
|
|
707
721
|
const resolvedVersion = await resolveVersionIfShorthand(name, version);
|
|
708
|
-
const response = await fetchWithRetry(`${NPM_REGISTRY_URL}/${name}/${resolvedVersion}`);
|
|
722
|
+
const response = await fetchWithRetry(`${NPM_REGISTRY_URL}/${encodeURIComponent(name)}/${encodeURIComponent(resolvedVersion)}`);
|
|
709
723
|
if (!response.ok) {
|
|
710
724
|
return {
|
|
711
725
|
package: packageNameForOutput,
|
|
@@ -832,7 +846,7 @@ export async function handleNpmTypes(args) {
|
|
|
832
846
|
}
|
|
833
847
|
try {
|
|
834
848
|
const resolvedVersion = await resolveVersionIfShorthand(name, version);
|
|
835
|
-
const response = await fetchWithRetry(`${NPM_REGISTRY_URL}/${name}/${resolvedVersion}`);
|
|
849
|
+
const response = await fetchWithRetry(`${NPM_REGISTRY_URL}/${encodeURIComponent(name)}/${encodeURIComponent(resolvedVersion)}`);
|
|
836
850
|
if (!response.ok) {
|
|
837
851
|
return {
|
|
838
852
|
package: packageNameForOutput,
|
|
@@ -845,8 +859,8 @@ export async function handleNpmTypes(args) {
|
|
|
845
859
|
const mainPackageData = (await response.json());
|
|
846
860
|
const actualVersion = mainPackageData.version || version; // Use version from response
|
|
847
861
|
const finalPackageName = `${name}@${actualVersion}`;
|
|
848
|
-
const
|
|
849
|
-
const
|
|
862
|
+
const typesPath = extractTypesPath(mainPackageData);
|
|
863
|
+
const hasBuiltInTypes = Boolean(typesPath);
|
|
850
864
|
const typesPackageName = `@types/${name.replace('@', '').replace('/', '__')}`;
|
|
851
865
|
let typesPackageInfo = {
|
|
852
866
|
name: typesPackageName,
|
|
@@ -1599,7 +1613,7 @@ export async function handleNpmCompare(args) {
|
|
|
1599
1613
|
try {
|
|
1600
1614
|
const resolvedVersion = await resolveVersionIfShorthand(name, versionTag);
|
|
1601
1615
|
// Fetch package version details from registry
|
|
1602
|
-
const pkgResponse = await fetchWithRetry(`${NPM_REGISTRY_URL}/${name}/${resolvedVersion}`);
|
|
1616
|
+
const pkgResponse = await fetchWithRetry(`${NPM_REGISTRY_URL}/${encodeURIComponent(name)}/${encodeURIComponent(resolvedVersion)}`);
|
|
1603
1617
|
if (!pkgResponse.ok) {
|
|
1604
1618
|
throw new Error(`Failed to fetch package info for ${name}@${versionTag}: ${pkgResponse.status} ${pkgResponse.statusText}`);
|
|
1605
1619
|
}
|
|
@@ -1610,7 +1624,7 @@ export async function handleNpmCompare(args) {
|
|
|
1610
1624
|
// Fetch monthly downloads
|
|
1611
1625
|
let monthlyDownloads = null;
|
|
1612
1626
|
try {
|
|
1613
|
-
const downloadsResponse = await fetchWithRetry(`https://api.npmjs.org/downloads/point/last-month/${name}`);
|
|
1627
|
+
const downloadsResponse = await fetchWithRetry(`https://api.npmjs.org/downloads/point/last-month/${encodeURIComponent(name)}`);
|
|
1614
1628
|
if (downloadsResponse.ok) {
|
|
1615
1629
|
const downloadsData = await downloadsResponse.json();
|
|
1616
1630
|
if (isNpmDownloadsData(downloadsData)) {
|
|
@@ -1625,7 +1639,7 @@ export async function handleNpmCompare(args) {
|
|
|
1625
1639
|
// Need to fetch the full package info to get to the 'time' field for specific version
|
|
1626
1640
|
let publishDate = null;
|
|
1627
1641
|
try {
|
|
1628
|
-
const fullPkgInfoResponse = await fetchWithRetry(`${NPM_REGISTRY_URL}/${name}`);
|
|
1642
|
+
const fullPkgInfoResponse = await fetchWithRetry(`${NPM_REGISTRY_URL}/${encodeURIComponent(name)}`);
|
|
1629
1643
|
if (fullPkgInfoResponse.ok) {
|
|
1630
1644
|
const fullPkgInfo = await fullPkgInfoResponse.json();
|
|
1631
1645
|
if (isNpmPackageInfo(fullPkgInfo) && fullPkgInfo.time) {
|
|
@@ -1640,13 +1654,13 @@ export async function handleNpmCompare(args) {
|
|
|
1640
1654
|
name: pkgData.name,
|
|
1641
1655
|
version: pkgData.version,
|
|
1642
1656
|
description: pkgData.description || null,
|
|
1643
|
-
license: pkgData.license
|
|
1657
|
+
license: extractLicenseString(pkgData.license),
|
|
1644
1658
|
dependenciesCount: Object.keys(pkgData.dependencies || {}).length,
|
|
1645
1659
|
devDependenciesCount: Object.keys(pkgData.devDependencies || {}).length,
|
|
1646
1660
|
peerDependenciesCount: Object.keys(pkgData.peerDependencies || {}).length,
|
|
1647
1661
|
monthlyDownloads: monthlyDownloads,
|
|
1648
1662
|
publishDate: publishDate,
|
|
1649
|
-
repositoryUrl: pkgData.repository
|
|
1663
|
+
repositoryUrl: extractRepositoryUrl(pkgData.repository),
|
|
1650
1664
|
};
|
|
1651
1665
|
cacheSet(cacheKey, comparisonData, CACHE_TTL_MEDIUM);
|
|
1652
1666
|
return {
|
|
@@ -1696,7 +1710,7 @@ export async function getLocalPackageMetrics(name, ignoreCache = false) {
|
|
|
1696
1710
|
if (cached)
|
|
1697
1711
|
return cached;
|
|
1698
1712
|
// 1. Fetch full packument from registry
|
|
1699
|
-
const registryResponse = await fetchWithRetry(`${NPM_REGISTRY_URL}/${name}`);
|
|
1713
|
+
const registryResponse = await fetchWithRetry(`${NPM_REGISTRY_URL}/${encodeURIComponent(name)}`);
|
|
1700
1714
|
if (!registryResponse.ok) {
|
|
1701
1715
|
throw new Error(`Package ${name} not found on registry.`);
|
|
1702
1716
|
}
|
|
@@ -1709,13 +1723,16 @@ export async function getLocalPackageMetrics(name, ignoreCache = false) {
|
|
|
1709
1723
|
throw new Error(`No latest version found for ${name}.`);
|
|
1710
1724
|
}
|
|
1711
1725
|
const versionData = packageInfo.versions[latestVersion];
|
|
1712
|
-
const hasTypes = Boolean(versionData.types ||
|
|
1726
|
+
const hasTypes = Boolean(versionData.types ||
|
|
1727
|
+
versionData.typings ||
|
|
1728
|
+
versionData.dependencies?.[`@types/${name}`] ||
|
|
1729
|
+
extractTypesPath(versionData));
|
|
1713
1730
|
const hasReadme = Boolean(versionData.readme || packageInfo.readme);
|
|
1714
1731
|
const dependencyCount = Object.keys(versionData.dependencies || {}).length;
|
|
1715
1732
|
// 2. Fetch downloads for last month
|
|
1716
1733
|
let downloadsLastMonth = 0;
|
|
1717
1734
|
try {
|
|
1718
|
-
const downloadsResponse = await fetchWithRetry(`https://api.npmjs.org/downloads/point/last-month/${name}`);
|
|
1735
|
+
const downloadsResponse = await fetchWithRetry(`https://api.npmjs.org/downloads/point/last-month/${encodeURIComponent(name)}`);
|
|
1719
1736
|
if (downloadsResponse.ok) {
|
|
1720
1737
|
const dlData = await downloadsResponse.json();
|
|
1721
1738
|
downloadsLastMonth = dlData.downloads || 0;
|
|
@@ -1733,7 +1750,7 @@ export async function getLocalPackageMetrics(name, ignoreCache = false) {
|
|
|
1733
1750
|
hasGitHubData: false,
|
|
1734
1751
|
};
|
|
1735
1752
|
let scorecard;
|
|
1736
|
-
const repoUrl = versionData.repository
|
|
1753
|
+
const repoUrl = extractRepositoryUrl(versionData.repository) || extractRepositoryUrl(packageInfo.repository);
|
|
1737
1754
|
if (repoUrl?.includes('github.com')) {
|
|
1738
1755
|
const githubMatch = repoUrl.match(/github\.com[:/]([^/]+)\/([^/.]+)/);
|
|
1739
1756
|
if (githubMatch) {
|
|
@@ -2063,7 +2080,7 @@ export async function handleNpmMaintainers(args) {
|
|
|
2063
2080
|
};
|
|
2064
2081
|
}
|
|
2065
2082
|
try {
|
|
2066
|
-
const response = await
|
|
2083
|
+
const response = await fetchWithRetry(`${NPM_REGISTRY_URL}/${encodeURIComponent(name)}`);
|
|
2067
2084
|
if (!response.ok) {
|
|
2068
2085
|
let errorMsg = `Failed to fetch package info: ${response.status} ${response.statusText}`;
|
|
2069
2086
|
if (response.status === 404) {
|
|
@@ -2621,7 +2638,7 @@ export async function handleNpmLicenseCompatibility(args) {
|
|
|
2621
2638
|
}
|
|
2622
2639
|
try {
|
|
2623
2640
|
const resolvedVersion = await resolveVersionIfShorthand(name, versionTag);
|
|
2624
|
-
const response = await fetchWithRetry(`${NPM_REGISTRY_URL}/${name}/${resolvedVersion}`);
|
|
2641
|
+
const response = await fetchWithRetry(`${NPM_REGISTRY_URL}/${encodeURIComponent(name)}/${encodeURIComponent(resolvedVersion)}`);
|
|
2625
2642
|
if (!response.ok) {
|
|
2626
2643
|
let errorMsg = `Failed to fetch package info: ${response.status} ${response.statusText}`;
|
|
2627
2644
|
if (response.status === 404) {
|
|
@@ -2649,8 +2666,9 @@ export async function handleNpmLicenseCompatibility(args) {
|
|
|
2649
2666
|
data: null,
|
|
2650
2667
|
};
|
|
2651
2668
|
}
|
|
2669
|
+
const normalizedLicense = extractLicenseString(versionData.license) || 'UNKNOWN';
|
|
2652
2670
|
const licenseInfoToCache = {
|
|
2653
|
-
license:
|
|
2671
|
+
license: normalizedLicense,
|
|
2654
2672
|
versionFetched: versionData.version,
|
|
2655
2673
|
};
|
|
2656
2674
|
cacheSet(cacheKey, licenseInfoToCache, CACHE_TTL_VERY_LONG);
|
|
@@ -2662,7 +2680,7 @@ export async function handleNpmLicenseCompatibility(args) {
|
|
|
2662
2680
|
status: 'success',
|
|
2663
2681
|
error: null,
|
|
2664
2682
|
data: {
|
|
2665
|
-
license:
|
|
2683
|
+
license: normalizedLicense,
|
|
2666
2684
|
},
|
|
2667
2685
|
message: `Successfully fetched license info for ${name}@${versionData.version}.`,
|
|
2668
2686
|
};
|
|
@@ -2796,7 +2814,7 @@ export async function handleNpmRepoStats(args) {
|
|
|
2796
2814
|
};
|
|
2797
2815
|
}
|
|
2798
2816
|
try {
|
|
2799
|
-
const npmResponse = await fetchWithRetry(`${NPM_REGISTRY_URL}/${name}/latest`);
|
|
2817
|
+
const npmResponse = await fetchWithRetry(`${NPM_REGISTRY_URL}/${encodeURIComponent(name)}/latest`);
|
|
2800
2818
|
if (!npmResponse.ok) {
|
|
2801
2819
|
const errorData = {
|
|
2802
2820
|
packageInput: pkgInput,
|
|
@@ -2821,7 +2839,7 @@ export async function handleNpmRepoStats(args) {
|
|
|
2821
2839
|
};
|
|
2822
2840
|
return errorData;
|
|
2823
2841
|
}
|
|
2824
|
-
const repoUrl = npmData.repository
|
|
2842
|
+
const repoUrl = extractRepositoryUrl(npmData.repository);
|
|
2825
2843
|
if (!repoUrl) {
|
|
2826
2844
|
const resultNoRepo = {
|
|
2827
2845
|
packageInput: pkgInput,
|
|
@@ -3187,7 +3205,7 @@ export async function handleNpmChangelogAnalysis(args) {
|
|
|
3187
3205
|
};
|
|
3188
3206
|
}
|
|
3189
3207
|
try {
|
|
3190
|
-
const npmResponse = await fetchWithRetry(`${NPM_REGISTRY_URL}/${name}`);
|
|
3208
|
+
const npmResponse = await fetchWithRetry(`${NPM_REGISTRY_URL}/${encodeURIComponent(name)}`);
|
|
3191
3209
|
if (!npmResponse.ok) {
|
|
3192
3210
|
const errorResult = {
|
|
3193
3211
|
packageInput: pkgInput,
|
|
@@ -3213,7 +3231,7 @@ export async function handleNpmChangelogAnalysis(args) {
|
|
|
3213
3231
|
};
|
|
3214
3232
|
return errorResult; // Do not cache this type of error
|
|
3215
3233
|
}
|
|
3216
|
-
const repositoryUrl = npmData.repository
|
|
3234
|
+
const repositoryUrl = extractRepositoryUrl(npmData.repository);
|
|
3217
3235
|
if (!repositoryUrl) {
|
|
3218
3236
|
const resultNoRepo = {
|
|
3219
3237
|
packageInput: pkgInput,
|
|
@@ -3683,7 +3701,7 @@ async function main() {
|
|
|
3683
3701
|
});
|
|
3684
3702
|
}
|
|
3685
3703
|
// Type guard for NpmPackageVersionSchema
|
|
3686
|
-
function isNpmPackageVersionData(data) {
|
|
3704
|
+
export function isNpmPackageVersionData(data) {
|
|
3687
3705
|
try {
|
|
3688
3706
|
const result = NpmPackageVersionSchema.safeParse(data);
|
|
3689
3707
|
if (!result.success) {
|