diff --git a/.changeset/old-seahorses-fold.md b/.changeset/old-seahorses-fold.md new file mode 100644 index 000000000..f5d6cad63 --- /dev/null +++ b/.changeset/old-seahorses-fold.md @@ -0,0 +1,6 @@ +--- +'astro': patch +'create-astro': patch +--- + +Update registry logic, improving edge cases (http support, redirects, registries ending with '/') diff --git a/packages/astro/src/core/add/index.ts b/packages/astro/src/core/add/index.ts index 12554409f..9c7c701af 100644 --- a/packages/astro/src/core/add/index.ts +++ b/packages/astro/src/core/add/index.ts @@ -79,7 +79,7 @@ async function getRegistry(): Promise { const packageManager = (await preferredPM(process.cwd()))?.name || 'npm'; try { const { stdout } = await execa(packageManager, ['config', 'get', 'registry']); - return stdout || 'https://registry.npmjs.org'; + return stdout?.trim()?.replace(/\/$/, '') || 'https://registry.npmjs.org'; } catch (e) { return 'https://registry.npmjs.org'; } diff --git a/packages/create-astro/package.json b/packages/create-astro/package.json index a3f1d6052..4ce3cf383 100644 --- a/packages/create-astro/package.json +++ b/packages/create-astro/package.json @@ -36,6 +36,7 @@ "execa": "^6.1.0", "giget": "1.0.0", "mocha": "^9.2.2", + "node-fetch-native": "^1.2.0", "which-pm-runs": "^1.1.0" }, "devDependencies": { diff --git a/packages/create-astro/src/messages.ts b/packages/create-astro/src/messages.ts index 4a51e0f80..edf22f5f6 100644 --- a/packages/create-astro/src/messages.ts +++ b/packages/create-astro/src/messages.ts @@ -3,7 +3,7 @@ import { color, label, say as houston, spinner as load } from '@astrojs/cli-kit' import { align, sleep } from '@astrojs/cli-kit/utils'; import { execa } from 'execa'; import { exec } from 'node:child_process'; -import { get } from 'node:https'; +import fetch from 'node-fetch-native'; import stripAnsi from 'strip-ansi'; import detectPackageManager from 'which-pm-runs'; @@ -15,7 +15,7 @@ async function getRegistry(): Promise { const packageManager = detectPackageManager()?.name || 'npm'; try { const { stdout } = await execa(packageManager, ['config', 'get', 'registry']); - return stdout || 'https://registry.npmjs.org'; + return stdout?.trim()?.replace(/\/$/, '') || 'https://registry.npmjs.org'; } catch (e) { return 'https://registry.npmjs.org'; } @@ -81,16 +81,10 @@ let v: string; export const getVersion = () => new Promise(async (resolve) => { if (v) return resolve(v); - const registry = await getRegistry(); - get(`${registry}/astro/latest`, (res) => { - let body = ''; - res.on('data', (chunk) => (body += chunk)); - res.on('end', () => { - const { version } = JSON.parse(body); - v = version; - resolve(version); - }); - }); + let registry = await getRegistry(); + const { version } = await fetch(`${registry}/astro/latest`, { redirect: 'follow' }).then(res => res.json()); + v = version; + resolve(version); }); export const log = (message: string) => stdout.write(message + '\n'); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index ee0f3c091..0611eadc1 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -3574,6 +3574,9 @@ importers: mocha: specifier: ^9.2.2 version: 9.2.2 + node-fetch-native: + specifier: ^1.2.0 + version: 1.2.0 which-pm-runs: specifier: ^1.1.0 version: 1.1.0 @@ -11921,7 +11924,7 @@ packages: defu: 6.1.2 https-proxy-agent: 5.0.1 mri: 1.2.0 - node-fetch-native: 1.1.1 + node-fetch-native: 1.2.0 pathe: 1.1.0 tar: 6.1.14 transitivePeerDependencies: @@ -14162,8 +14165,8 @@ packages: engines: {node: '>=10.5.0'} dev: false - /node-fetch-native@1.1.1: - resolution: {integrity: sha512-9VvspTSUp2Sxbl+9vbZTlFGq9lHwE8GDVVekxx6YsNd1YH59sb3Ba8v3Y3cD8PkLNcileGGcA21PFjVl0jzDaw==} + /node-fetch-native@1.2.0: + resolution: {integrity: sha512-5IAMBTl9p6PaAjYCnMv5FmqIF6GcZnawAVnzaCG0rX2aYZJ4CxEkZNtVPuTRug7fL7wyM5BQYTlAzcyMPi6oTQ==} dev: false /node-fetch@2.6.11: