* Handle native fetch for esm.sh URLs.
( urlStr: string, init: RequestInit | undefined, storage: ReturnType<typeof useStorage>, )
| 774 | * Handle native fetch for esm.sh URLs. |
| 775 | */ |
| 776 | async function handleEsmShFetch( |
| 777 | urlStr: string, |
| 778 | init: RequestInit | undefined, |
| 779 | storage: ReturnType<typeof useStorage>, |
| 780 | ): Promise<Response> { |
| 781 | const method = init?.method?.toUpperCase() || 'GET' |
| 782 | const urlObj = new URL(urlStr) |
| 783 | const pathname = urlObj.pathname.slice(1) // Remove leading / |
| 784 | |
| 785 | // HEAD request - return headers with x-typescript-types if fixture exists |
| 786 | if (method === 'HEAD') { |
| 787 | // Extract package@version from pathname |
| 788 | let pkgVersion = pathname |
| 789 | const slashIndex = pkgVersion.indexOf( |
| 790 | '/', |
| 791 | pkgVersion.includes('@') ? pkgVersion.lastIndexOf('@') + 1 : 0, |
| 792 | ) |
| 793 | if (slashIndex !== -1) { |
| 794 | pkgVersion = pkgVersion.slice(0, slashIndex) |
| 795 | } |
| 796 | |
| 797 | const fixturePath = `${FIXTURE_PATHS.esmHeaders}:${pkgVersion.replace(/\//g, ':')}.json` |
| 798 | const headerData = await storage.getItem<{ 'x-typescript-types': string }>(fixturePath) |
| 799 | |
| 800 | if (headerData) { |
| 801 | if (VERBOSE) process.stdout.write(`[test-fixtures] fetch HEAD esm.sh: ${pkgVersion}\n`) |
| 802 | return new Response(null, { |
| 803 | status: 200, |
| 804 | headers: { |
| 805 | 'x-typescript-types': headerData['x-typescript-types'], |
| 806 | 'content-type': 'application/javascript', |
| 807 | }, |
| 808 | }) |
| 809 | } |
| 810 | |
| 811 | // No fixture - return 200 without x-typescript-types header (types not available) |
| 812 | if (VERBOSE) |
| 813 | process.stdout.write(`[test-fixtures] fetch HEAD esm.sh (no fixture): ${pkgVersion}\n`) |
| 814 | return new Response(null, { |
| 815 | status: 200, |
| 816 | headers: { 'content-type': 'application/javascript' }, |
| 817 | }) |
| 818 | } |
| 819 | |
| 820 | // GET request - return .d.ts content if fixture exists |
| 821 | if (method === 'GET' && pathname.endsWith('.d.ts')) { |
| 822 | const fixturePath = `${FIXTURE_PATHS.esmTypes}:${pathname.replace(/\//g, ':')}` |
| 823 | const content = await storage.getItem<string>(fixturePath) |
| 824 | |
| 825 | if (content) { |
| 826 | if (VERBOSE) process.stdout.write(`[test-fixtures] fetch GET esm.sh: ${pathname}\n`) |
| 827 | return new Response(content, { |
| 828 | status: 200, |
| 829 | headers: { 'content-type': 'application/typescript' }, |
| 830 | }) |
| 831 | } |
| 832 | |
| 833 | // Return a minimal stub .d.ts file instead of 404 |