From 7f10ab2ab6dc676d73cc24342f19dbe5ad29d754 Mon Sep 17 00:00:00 2001 From: jincheng32537 Date: Fri, 30 Jun 2023 20:00:02 +0800 Subject: [PATCH 1/9] fix: getVersion Error when registry ends with '/' --- .changeset/old-seahorses-fold.md | 5 +++++ packages/create-astro/src/messages.ts | 6 +++++- 2 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 .changeset/old-seahorses-fold.md diff --git a/.changeset/old-seahorses-fold.md b/.changeset/old-seahorses-fold.md new file mode 100644 index 000000000000..6f01d30cd047 --- /dev/null +++ b/.changeset/old-seahorses-fold.md @@ -0,0 +1,5 @@ +--- +'create-astro': major +--- + +fix: getVersion Error when registry ends with '/' diff --git a/packages/create-astro/src/messages.ts b/packages/create-astro/src/messages.ts index 4a51e0f80813..3fe6ce0327d3 100644 --- a/packages/create-astro/src/messages.ts +++ b/packages/create-astro/src/messages.ts @@ -81,7 +81,11 @@ let v: string; export const getVersion = () => new Promise(async (resolve) => { if (v) return resolve(v); - const registry = await getRegistry(); + let registry = await getRegistry(); + // if the registry ends with '/', trim it. + if (registry.lastIndexOf('/') === registry.length - 1) { + registry = registry.substring(0, registry.length - 1); + } get(`${registry}/astro/latest`, (res) => { let body = ''; res.on('data', (chunk) => (body += chunk)); From 6b63f4a7d161f8c74f10fbdfa2e8976db7a2a8cc Mon Sep 17 00:00:00 2001 From: Nate Moore Date: Fri, 30 Jun 2023 16:32:32 -0500 Subject: [PATCH 2/9] Update .changeset/old-seahorses-fold.md --- .changeset/old-seahorses-fold.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/old-seahorses-fold.md b/.changeset/old-seahorses-fold.md index 6f01d30cd047..6711185b8666 100644 --- a/.changeset/old-seahorses-fold.md +++ b/.changeset/old-seahorses-fold.md @@ -1,5 +1,5 @@ --- -'create-astro': major +'create-astro': patch --- fix: getVersion Error when registry ends with '/' From 1a6dd08c56a28fbaf9d1fd24cd42bfd109a32ec3 Mon Sep 17 00:00:00 2001 From: Nate Moore Date: Fri, 30 Jun 2023 16:32:36 -0500 Subject: [PATCH 3/9] Update .changeset/old-seahorses-fold.md --- .changeset/old-seahorses-fold.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/old-seahorses-fold.md b/.changeset/old-seahorses-fold.md index 6711185b8666..8c6eb92c9029 100644 --- a/.changeset/old-seahorses-fold.md +++ b/.changeset/old-seahorses-fold.md @@ -2,4 +2,4 @@ 'create-astro': patch --- -fix: getVersion Error when registry ends with '/' +Update registry logic to handle registry URLs ending with '/' From 59eb571154b6294cf2579b91046d4422717b3373 Mon Sep 17 00:00:00 2001 From: Nate Moore Date: Fri, 30 Jun 2023 16:38:16 -0500 Subject: [PATCH 4/9] refactor: cleanup registry logic --- packages/astro/src/core/add/index.ts | 2 +- packages/create-astro/src/messages.ts | 6 +----- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/packages/astro/src/core/add/index.ts b/packages/astro/src/core/add/index.ts index 12554409feaf..9c7c701afdf6 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/src/messages.ts b/packages/create-astro/src/messages.ts index 3fe6ce0327d3..e75a47a2e062 100644 --- a/packages/create-astro/src/messages.ts +++ b/packages/create-astro/src/messages.ts @@ -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'; } @@ -82,10 +82,6 @@ export const getVersion = () => new Promise(async (resolve) => { if (v) return resolve(v); let registry = await getRegistry(); - // if the registry ends with '/', trim it. - if (registry.lastIndexOf('/') === registry.length - 1) { - registry = registry.substring(0, registry.length - 1); - } get(`${registry}/astro/latest`, (res) => { let body = ''; res.on('data', (chunk) => (body += chunk)); From 7018dfc4cc5f17caa7ebe9acd1439a294733b54b Mon Sep 17 00:00:00 2001 From: Nate Moore Date: Fri, 30 Jun 2023 16:38:46 -0500 Subject: [PATCH 5/9] chore: update changeset --- .changeset/old-seahorses-fold.md | 1 + 1 file changed, 1 insertion(+) diff --git a/.changeset/old-seahorses-fold.md b/.changeset/old-seahorses-fold.md index 8c6eb92c9029..b90b698ea079 100644 --- a/.changeset/old-seahorses-fold.md +++ b/.changeset/old-seahorses-fold.md @@ -1,4 +1,5 @@ --- +'astro': patch 'create-astro': patch --- From e18a0509d3d1347e90a3a31b8b3fe4829b22ede7 Mon Sep 17 00:00:00 2001 From: Nate Moore Date: Fri, 30 Jun 2023 16:44:46 -0500 Subject: [PATCH 6/9] fix(create-astro): update registry logic to rely on `fetch` --- packages/create-astro/package.json | 1 + packages/create-astro/src/messages.ts | 14 ++++---------- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/packages/create-astro/package.json b/packages/create-astro/package.json index a3f1d6052c21..4ce3cf383e58 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 e75a47a2e062..edf22f5f6267 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'; @@ -82,15 +82,9 @@ export const getVersion = () => new Promise(async (resolve) => { if (v) return resolve(v); let 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); - }); - }); + 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'); From d30c06d555e3da71b1de779946d66e5e08e90958 Mon Sep 17 00:00:00 2001 From: Nate Moore Date: Fri, 30 Jun 2023 16:45:26 -0500 Subject: [PATCH 7/9] chore: update changeset --- .changeset/old-seahorses-fold.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/old-seahorses-fold.md b/.changeset/old-seahorses-fold.md index b90b698ea079..0c2fefc24a9f 100644 --- a/.changeset/old-seahorses-fold.md +++ b/.changeset/old-seahorses-fold.md @@ -3,4 +3,4 @@ 'create-astro': patch --- -Update registry logic to handle registry URLs ending with '/' +Update registry logic, improving edge cases (redirects, UTF-8 sequences, registries ending with '/') From 50ca8f51c29d424ab117f6537ab8c568290bbdaa Mon Sep 17 00:00:00 2001 From: Nate Moore Date: Fri, 30 Jun 2023 16:48:26 -0500 Subject: [PATCH 8/9] chore: update lockfile --- pnpm-lock.yaml | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index ee0f3c0911e2..0611eadc1803 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: From 1e954550ce0f7f3db67fdd95a29cba1a0c7a9c38 Mon Sep 17 00:00:00 2001 From: Nate Moore Date: Fri, 30 Jun 2023 16:51:12 -0500 Subject: [PATCH 9/9] Update old-seahorses-fold.md --- .changeset/old-seahorses-fold.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/old-seahorses-fold.md b/.changeset/old-seahorses-fold.md index 0c2fefc24a9f..f5d6cad63c03 100644 --- a/.changeset/old-seahorses-fold.md +++ b/.changeset/old-seahorses-fold.md @@ -3,4 +3,4 @@ 'create-astro': patch --- -Update registry logic, improving edge cases (redirects, UTF-8 sequences, registries ending with '/') +Update registry logic, improving edge cases (http support, redirects, registries ending with '/')