-
-
Notifications
You must be signed in to change notification settings - Fork 424
Expand file tree
/
Copy path[...pkg].get.ts
More file actions
52 lines (48 loc) · 1.71 KB
/
[...pkg].get.ts
File metadata and controls
52 lines (48 loc) · 1.71 KB
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
import * as v from 'valibot'
import { PackageRouteParamsSchema } from '#shared/schemas/package'
import { CACHE_MAX_AGE_ONE_HOUR } from '#shared/utils/constants'
/**
* GET /api/registry/vulnerabilities/:name or /api/registry/vulnerabilities/:name/v/:version
*
* Analyze entire dependency tree for vulnerabilities and deprecated dependencies.
* I does not rename this endpoint for backward compatibility.
*/
export default defineBypassableCachedEventHandler(
async event => {
const pkgParamSegments = getRouterParam(event, 'pkg')?.split('/') ?? []
const { rawPackageName, rawVersion } = parsePackageParams(pkgParamSegments)
try {
const { packageName, version: requestedVersion } = v.parse(PackageRouteParamsSchema, {
packageName: decodeURIComponent(rawPackageName),
version: rawVersion,
})
// If no version specified, resolve to latest using fast-npm-meta (lightweight)
let version: string | undefined = requestedVersion
if (!version) {
const latestVersion = await fetchLatestVersionWithFallback(packageName)
if (!latestVersion) {
throw createError({
statusCode: 404,
message: 'No latest version found',
})
}
version = latestVersion
}
return await analyzeDependencyTree(packageName, version)
} catch (error: unknown) {
handleApiError(error, {
statusCode: 502,
message: 'Failed to analyze vulnerabilities',
})
}
},
{
maxAge: CACHE_MAX_AGE_ONE_HOUR,
swr: true,
bypassKey: 'vulnerabilities',
getKey: event => {
const pkg = getRouterParam(event, 'pkg') ?? ''
return `vulnerabilities:v1:${pkg.replace(/\/+$/, '').trim()}`
},
},
)