Skip to content

Commit 8000756

Browse files
committed
Update cache.ts
1 parent 1e742e6 commit 8000756

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

modules/runtime/server/cache.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,46 @@ function logUnmockedRequest(type: string, detail: string, url: string): void {
571571
)
572572
}
573573

574+
async function handleJsdelivrDataApi(
575+
url: string,
576+
storage: ReturnType<typeof useStorage>,
577+
): Promise<MockResult | null> {
578+
let urlObj: URL
579+
try {
580+
urlObj = new URL(url)
581+
} catch {
582+
return null
583+
}
584+
585+
if (urlObj.host !== 'data.jsdelivr.com') return null
586+
587+
const packageMatch = decodeURIComponent(urlObj.pathname).match(/^\/v1\/packages\/npm\/(.+)$/)
588+
if (!packageMatch?.[1]) return null
589+
590+
const parsed = parseScopedPackageWithVersion(packageMatch[1])
591+
592+
// Try per-package fixture first
593+
const fixturePath = `jsdelivr:${parsed.name.replace(/\//g, ':')}.json`
594+
const fixture = await storage.getItem<unknown>(fixturePath)
595+
if (fixture) {
596+
return { data: fixture }
597+
}
598+
599+
// Fall back to generic stub (no declaration files)
600+
return {
601+
data: {
602+
type: 'npm',
603+
name: parsed.name,
604+
version: parsed.version || 'latest',
605+
files: [
606+
{ name: 'package.json', hash: 'abc123', size: 1000 },
607+
{ name: 'index.js', hash: 'def456', size: 500 },
608+
{ name: 'README.md', hash: 'ghi789', size: 2000 },
609+
],
610+
},
611+
}
612+
}
613+
574614
/**
575615
* Shared fixture-backed fetch implementation.
576616
* This is used by both cachedFetch and the global $fetch override.
@@ -593,6 +633,12 @@ async function fetchFromFixtures<T>(
593633
return { data: fastNpmMetaResult.data as T, isStale: false, cachedAt: Date.now() }
594634
}
595635

636+
const jsdelivrResult = await handleJsdelivrDataApi(url, storage)
637+
if (jsdelivrResult) {
638+
if (VERBOSE) process.stdout.write(`[test-fixtures] jsDelivr Data API: ${url}\n`)
639+
return { data: jsdelivrResult.data as T, isStale: false, cachedAt: Date.now() }
640+
}
641+
596642
// Check for GitHub API
597643
const githubResult = await handleGitHubApi(url, storage)
598644
if (githubResult) {

0 commit comments

Comments
 (0)