An open source design system that's fully customizable and agent ready
TypeScript
fix: verify npm registry before publish success (#9093)
Cindy Zhange6d7449aaa93
diff --git a/.github/scripts/release-publish-workflow.test.mjs b/.github/scripts/release-publish-workflow.test.mjsindex fb2fc11d32..0f521092ff 100644--- a/.github/scripts/release-publish-workflow.test.mjs+++ b/.github/scripts/release-publish-workflow.test.mjs@@ -84,6 +84,23 @@ describe('stable and canary publication authority', () => {expect(publish).not.toContain('--tag canary');});+ it('reports stable success only after registry propagation is verified', () => {+ const publishStep = step(stable, 'Publish changed packages');+ expect(publishStep.env.RELEASE_VERSION).toBe(+ "${{ inputs['release-version'] }}",+ );+ const publish = publishStep.run;+ const verify = publish.indexOf('node scripts/npm/verify-published.mjs');+ const success = publish.indexOf(+ 'Published $published package(s); registry verification passed.',+ );+ expect(verify).toBeGreaterThan(0);+ expect(publish).toContain('--version "$RELEASE_VERSION"');+ expect(publish).toContain('--tag latest');+ expect(success).toBeGreaterThan(verify);+ expect(publish.indexOf('if [ "$DRY_RUN" = "true" ]')).toBeLessThan(verify);+ });+it('publishes canaries only while main is strictly ahead of stable', () => {const eligibility = step(canary,diff --git a/.github/workflows/release.yml b/.github/workflows/release.ymlindex d6d63d22a8..5799d619e8 100644--- a/.github/workflows/release.yml+++ b/.github/workflows/release.yml@@ -157,6 +157,7 @@ jobs:- name: Publish changed packagesenv:DRY_RUN: ${{ inputs.dry-run }}+ RELEASE_VERSION: ${{ inputs['release-version'] }}run: |set -eupublished=0@@ -181,7 +182,14 @@ jobs:pnpm publish "./$pkg_dir" --tag latest --provenance --access public --no-git-checkspublished=$((published + 1))done- echo "Published $published package(s)."+ if [ "$DRY_RUN" = "true" ]; then+ echo "Dry run complete; no packages were published."+ else+ node scripts/npm/verify-published.mjs \+ --version "$RELEASE_VERSION" \+ --tag latest+ echo "Published $published package(s); registry verification passed."+ fi# Canary publish — runs on every push to main. The @canary dist-tag always# points to the latest main commit:diff --git a/scripts/npm/verify-published.mjs b/scripts/npm/verify-published.mjsnew file mode 100644index 0000000000..a8436c2570--- /dev/null+++ b/scripts/npm/verify-published.mjs@@ -0,0 +1,225 @@+#!/usr/bin/env node+// Copyright (c) Meta Platforms, Inc. and affiliates.+/**+ * Wait until every stable package is actually readable from npm.+ *+ * A successful `npm publish` process can precede registry propagation. This+ * verifier checks the small version document, the requested dist-tag, and the+ * tarball response for each package before the release workflow reports+ * success. Packages that are already visible are not fetched again while a+ * slower package continues propagating.+ */++import fs from 'node:fs';+import path from 'node:path';+import {fileURLToPath} from 'node:url';++import {isStableReleasePackage} from '../release/active-release.mjs';++const DEFAULT_REGISTRY = 'https://registry.npmjs.org';+const DEFAULT_ATTEMPTS = 41;+const DEFAULT_INTERVAL_MS = 15_000;++function packageKey(name) {+ return encodeURIComponent(name);+}++function registryUrl(registry, pathname) {+ return new URL(pathname, `${registry.replace(/\/+$/u, '')}/`).href;+}++async function jsonResponse(response, label) {+ if (response.status !== 200) {+ throw new Error(`${label} returned HTTP ${response.status}`);+ }+ try {+ return await response.json();+ } catch {+ throw new Error(`${label} returned invalid JSON`);+ }+}++export async function verifyRegistryEntry({+ name,+ version,+ tag,+ registry = DEFAULT_REGISTRY,+ fetchImpl = fetch,+}) {+ const encoded = packageKey(name);+ const versionUrl = registryUrl(+ registry,+ `${encoded}/${encodeURIComponent(version)}`,+ );+ const versionDocument = await jsonResponse(+ await fetchImpl(versionUrl, {+ cache: 'no-store',+ headers: {accept: 'application/json'},+ signal: AbortSignal.timeout(30_000),+ }),+ `${name}@${version} version document`,+ );+ if (versionDocument.version !== version) {+ throw new Error(+ `${name}@${version} version document reported ${versionDocument.version ?? 'no version'}`,+ );+ }++ const tagsUrl = registryUrl(registry, `-/package/${encoded}/dist-tags`);+ const tags = await jsonResponse(+ await fetchImpl(tagsUrl, {+ cache: 'no-store',+ headers: {accept: 'application/json'},+ signal: AbortSignal.timeout(30_000),+ }),+ `${name} dist-tags`,+ );+ if (tags[tag] !== version) {+ throw new Error(+ `${name} ${tag} dist-tag is ${tags[tag] ?? 'missing'}, expected ${version}`,+ );+ }++ const tarball = versionDocument.dist?.tarball;+ if (typeof tarball !== 'string' || tarball.length === 0) {+ throw new Error(`${name}@${version} version document has no tarball URL`);+ }+ const tarballResponse = await fetchImpl(tarball, {+ cache: 'no-store',+ signal: AbortSignal.timeout(30_000),+ });+ if (tarballResponse.status !== 200) {+ await tarballResponse.body?.cancel();+ throw new Error(+ `${name}@${version} tarball returned HTTP ${tarballResponse.status}`,+ );+ }+ await tarballResponse.body?.cancel();+}++export async function verifyPublishedPackages({+ packages,+ version,+ tag = 'latest',+ registry = DEFAULT_REGISTRY,+ attempts = DEFAULT_ATTEMPTS,+ intervalMs = DEFAULT_INTERVAL_MS,+ fetchImpl = fetch,+ sleep = ms => new Promise(resolve => setTimeout(resolve, ms)),+ onAttempt = () => {},+}) {+ const pending = new Map(packages.map(name => [name, 'not checked']));+ for (let attempt = 1; attempt <= attempts; attempt += 1) {+ const names = [...pending.keys()];+ onAttempt({attempt, attempts, names});+ await Promise.all(+ names.map(async name => {+ try {+ await verifyRegistryEntry({+ name,+ version,+ tag,+ registry,+ fetchImpl,+ });+ pending.delete(name);+ } catch (error) {+ pending.set(name, error.message);+ }+ }),+ );+ if (pending.size === 0) return;+ if (attempt < attempts) await sleep(intervalMs);+ }+ throw new Error(+ [...pending]+ .map(([name, error]) => `${name}@${version}: ${error}`)+ .join('\n'),+ );+}++export function stablePackageNames(root, version) {+ const directories = [+ ...fs+ .readdirSync(path.join(root, 'packages'), {withFileTypes: true})+ .filter(entry => entry.isDirectory())+ .map(entry => path.join(root, 'packages', entry.name)),+ ...fs+ .readdirSync(path.join(root, 'packages', 'themes'), {+ withFileTypes: true,+ })+ .filter(entry => entry.isDirectory())+ .map(entry => path.join(root, 'packages', 'themes', entry.name)),+ ];+ return directories+ .map(directory => path.join(directory, 'package.json'))+ .filter(filename => fs.existsSync(filename))+ .map(filename => JSON.parse(fs.readFileSync(filename, 'utf8')))+ .filter(isStableReleasePackage)+ .map(manifest => {+ if (manifest.version !== version) {+ throw new Error(+ `${manifest.name} declares ${manifest.version}, expected ${version}`,+ );+ }+ return manifest.name;+ })+ .sort();+}++function parseArgs(argv) {+ const values = {};+ for (let index = 0; index < argv.length; index += 2) {+ const key = argv[index];+ const value = argv[index + 1];+ if (!key?.startsWith('--') || value == null) {+ throw new Error(+ 'expected --version X.Y.Z and optional --tag, --registry, --attempts, or --interval-ms',+ );+ }+ values[key.slice(2)] = value;+ }+ return values;+}++async function main() {+ const values = parseArgs(process.argv.slice(2));+ const version = values.version;+ if (!/^\d+\.\d+\.\d+$/u.test(version ?? '')) {+ throw new Error('--version must be X.Y.Z');+ }+ const root = path.resolve(+ path.dirname(fileURLToPath(import.meta.url)),+ '../..',+ );+ const packages = stablePackageNames(root, version);+ if (packages.length === 0) throw new Error('no stable packages found');+ await verifyPublishedPackages({+ packages,+ version,+ tag: values.tag ?? 'latest',+ registry: values.registry ?? DEFAULT_REGISTRY,+ attempts: Number(values.attempts ?? DEFAULT_ATTEMPTS),+ intervalMs: Number(values['interval-ms'] ?? DEFAULT_INTERVAL_MS),+ onAttempt({attempt, attempts, names}) {+ console.log(+ `Registry verification ${attempt}/${attempts}: ${names.length} package(s) pending`,+ );+ },+ });+ console.log(+ `✅ Verified ${packages.length} package(s) at ${version}: version document, tarball HTTP 200, and ${values.tag ?? 'latest'} dist-tag.`,+ );+}++if (+ process.argv[1] &&+ fileURLToPath(import.meta.url) === path.resolve(process.argv[1])+) {+ main().catch(error => {+ console.error(+ `published package registry verification failed:\n${error.message}`,+ );+ process.exit(1);+ });+}diff --git a/scripts/npm/verify-published.test.mjs b/scripts/npm/verify-published.test.mjsnew file mode 100644index 0000000000..faf6282efa--- /dev/null+++ b/scripts/npm/verify-published.test.mjs@@ -0,0 +1,184 @@+// Copyright (c) Meta Platforms, Inc. and affiliates.++import fs from 'node:fs';+import os from 'node:os';+import path from 'node:path';++import {afterEach, describe, expect, it} from 'vitest';++import {+ stablePackageNames,+ verifyPublishedPackages,+ verifyRegistryEntry,+} from './verify-published.mjs';++const roots = [];+afterEach(() => {+ for (const root of roots.splice(0)) {+ fs.rmSync(root, {recursive: true, force: true});+ }+});++function response(body, status = 200) {+ return new Response(typeof body === 'string' ? body : JSON.stringify(body), {+ status,+ headers: {'content-type': 'application/json'},+ });+}++function registryFetch({+ versionStatus = 200,+ tag = '0.7.5',+ tarStatus = 200,+} = {}) {+ const calls = [];+ const fetchImpl = async url => {+ calls.push(url);+ if (url.includes('/dist-tags')) return response({latest: tag});+ if (url.endsWith('.tgz')) return response('tarball', tarStatus);+ return response(+ {+ name: '@astryxdesign/core',+ version: '0.7.5',+ dist: {tarball: 'https://registry.npmjs.org/core-0.7.5.tgz'},+ },+ versionStatus,+ );+ };+ return {calls, fetchImpl};+}++describe('verifyRegistryEntry', () => {+ it('requires the exact version document, dist-tag, and tarball HTTP 200', async () => {+ const {calls, fetchImpl} = registryFetch();+ await verifyRegistryEntry({+ name: '@astryxdesign/core',+ version: '0.7.5',+ tag: 'latest',+ fetchImpl,+ });+ expect(calls).toEqual([+ 'https://registry.npmjs.org/%40astryxdesign%2Fcore/0.7.5',+ 'https://registry.npmjs.org/-/package/%40astryxdesign%2Fcore/dist-tags',+ 'https://registry.npmjs.org/core-0.7.5.tgz',+ ]);+ });++ it('rejects a stale dist-tag', async () => {+ const {fetchImpl} = registryFetch({tag: '0.7.4'});+ await expect(+ verifyRegistryEntry({+ name: '@astryxdesign/core',+ version: '0.7.5',+ tag: 'latest',+ fetchImpl,+ }),+ ).rejects.toThrow('latest dist-tag is 0.7.4, expected 0.7.5');+ });++ it('rejects a tarball that is not readable', async () => {+ const {fetchImpl} = registryFetch({tarStatus: 404});+ await expect(+ verifyRegistryEntry({+ name: '@astryxdesign/core',+ version: '0.7.5',+ tag: 'latest',+ fetchImpl,+ }),+ ).rejects.toThrow('tarball returned HTTP 404');+ });+});++describe('verifyPublishedPackages', () => {+ it('retries registry propagation without rechecking packages already visible', async () => {+ const attempts = new Map();+ const sleeps = [];+ const fetchImpl = async url => {+ const name = url.includes('cli') ? 'cli' : 'core';+ if (!url.includes('/dist-tags') && !url.endsWith('.tgz')) {+ const seen = (attempts.get(name) ?? 0) + 1;+ attempts.set(name, seen);+ if (name === 'core' && seen === 1) return response({}, 404);+ }+ if (url.includes('/dist-tags')) return response({latest: '0.7.5'});+ if (url.endsWith('.tgz')) return response('tarball');+ return response({+ version: '0.7.5',+ dist: {tarball: `https://registry.npmjs.org/${name}-0.7.5.tgz`},+ });+ };+ await verifyPublishedPackages({+ packages: ['@astryxdesign/cli', '@astryxdesign/core'],+ version: '0.7.5',+ attempts: 2,+ intervalMs: 1,+ fetchImpl,+ sleep: async ms => sleeps.push(ms),+ });+ expect(attempts).toEqual(+ new Map([+ ['cli', 1],+ ['core', 2],+ ]),+ );+ expect(sleeps).toEqual([1]);+ });++ it('reports the last registry failure after the retry budget', async () => {+ await expect(+ verifyPublishedPackages({+ packages: ['@astryxdesign/core'],+ version: '0.7.5',+ attempts: 2,+ intervalMs: 0,+ fetchImpl: async () => response({}, 404),+ sleep: async () => {},+ }),+ ).rejects.toThrow(+ '@astryxdesign/[email protected]: @astryxdesign/[email protected] version document returned HTTP 404',+ );+ });+});++describe('stablePackageNames', () => {+ it('matches stable publish scope and refuses a split version', () => {+ const root = fs.mkdtempSync(+ path.join(os.tmpdir(), 'astryx-registry-verify-'),+ );+ roots.push(root);+ for (const [relative, manifest] of [+ ['packages/core', {name: '@astryxdesign/core', version: '0.7.5'}],+ [+ 'packages/themes/neutral',+ {name: '@astryxdesign/theme-neutral', version: '0.7.5'},+ ],+ [+ 'packages/lab',+ {+ name: '@astryxdesign/lab',+ version: '0.7.5',+ private: true,+ astryx: {canaryOnly: true},+ },+ ],+ ]) {+ fs.mkdirSync(path.join(root, relative), {recursive: true});+ fs.writeFileSync(+ path.join(root, relative, 'package.json'),+ JSON.stringify(manifest),+ );+ }+ expect(stablePackageNames(root, '0.7.5')).toEqual([+ '@astryxdesign/core',+ '@astryxdesign/theme-neutral',+ ]);+ const corePath = path.join(root, 'packages/core/package.json');+ fs.writeFileSync(+ corePath,+ JSON.stringify({name: '@astryxdesign/core', version: '0.7.4'}),+ );+ expect(() => stablePackageNames(root, '0.7.5')).toThrow(+ '@astryxdesign/core declares 0.7.4, expected 0.7.5',+ );+ });+});