forked from npmx-dev/npmx.dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path[...pkg].get.ts
More file actions
110 lines (97 loc) · 3.19 KB
/
[...pkg].get.ts
File metadata and controls
110 lines (97 loc) · 3.19 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
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
import * as v from 'valibot'
import { PackageRouteParamsSchema } from '#shared/schemas/package'
import {
CACHE_MAX_AGE_ONE_HOUR,
NPM_MISSING_README_SENTINEL,
ERROR_NPM_FETCH_FAILED,
} from '#shared/utils/constants'
/**
* Fetch README from jsdelivr CDN for a specific package version.
* Falls back through common README filenames.
*/
async function fetchReadmeFromJsdelivr(
packageName: string,
version?: string,
): Promise<string | null> {
const filenames = [
'README.md',
'readme.md',
'Readme.md',
'README',
'readme',
'README.markdown',
'readme.markdown',
]
const versionSuffix = version ? `@${version}` : ''
for (const filename of filenames) {
try {
const url = `https://cdn.jsdelivr.net/npm/${packageName}${versionSuffix}/${filename}`
const response = await fetch(url)
if (response.ok) {
return await response.text()
}
} catch {
// Try next filename
}
}
return null
}
/**
* Returns rendered README HTML for a package.
*
* URL patterns:
* - /api/registry/readme/packageName - latest version
* - /api/registry/readme/packageName/v/1.2.3 - specific version
* - /api/registry/readme/@scope/packageName - scoped package, latest
* - /api/registry/readme/@scope/packageName/v/1.2.3 - scoped package, specific version
*/
export default defineCachedEventHandler(
async event => {
// Parse package name and optional version from URL segments
// Patterns: [pkg] or [pkg, 'v', version] or [@scope, pkg] or [@scope, pkg, 'v', version]
const pkgParamSegments = getRouterParam(event, 'pkg')?.split('/') ?? []
const { rawPackageName, rawVersion } = parsePackageParams(pkgParamSegments)
try {
// 1. Validate
const { packageName, version } = v.parse(PackageRouteParamsSchema, {
packageName: rawPackageName,
version: rawVersion,
})
const packageData = await fetchNpmPackage(packageName)
let readmeContent: string | undefined
// If a specific version is requested, get README from that version
if (version) {
const versionData = packageData.versions[version]
if (versionData) {
readmeContent = versionData.readme
}
} else {
// Use the packument-level readme (from latest version)
readmeContent = packageData.readme
}
// If no README in packument, try fetching from jsdelivr (package tarball)
if (!readmeContent || readmeContent === NPM_MISSING_README_SENTINEL) {
readmeContent = (await fetchReadmeFromJsdelivr(packageName, version)) ?? undefined
}
if (!readmeContent) {
return { html: '', playgroundLinks: [] }
}
// Parse repository info for resolving relative URLs to GitHub
const repoInfo = parseRepositoryInfo(packageData.repository)
return await renderReadmeHtml(readmeContent, packageName, repoInfo)
} catch (error: unknown) {
handleApiError(error, {
statusCode: 502,
message: ERROR_NPM_FETCH_FAILED,
})
}
},
{
maxAge: CACHE_MAX_AGE_ONE_HOUR,
swr: true,
getKey: event => {
const pkg = getRouterParam(event, 'pkg') ?? ''
return `readme:v4:${pkg.replace(/\/+$/, '').trim()}`
},
},
)