Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(release): publish each package at most once, retrying a rejected signature while npm lacks the version (#9346)

Cindy Zhangd96d470e38e3
diff --git a/.github/scripts/release-publish-workflow.test.mjs b/.github/scripts/release-publish-workflow.test.mjs
index 6a0103632b..c85017d983 100644
--- a/.github/scripts/release-publish-workflow.test.mjs
+++ b/.github/scripts/release-publish-workflow.test.mjs
@@ -35,10 +35,37 @@ function writeExecutable(root, name, contents) {
fs.chmodSync(file, 0o755);
}
+/**
+ * Copies the real per-package publisher into a scratch root and returns the
+ * environment that runs it quickly: an unreachable registry (every read is
+ * unknown, so nothing is skipped) and no waits between attempts.
+ */
+function withPublisher(root) {
+ for (const file of ['publish-package.mjs', 'registry.mjs']) {
+ const target = path.join(root, 'scripts/npm', file);
+ fs.mkdirSync(path.dirname(target), {recursive: true});
+ fs.copyFileSync(path.join(ROOT, 'scripts/npm', file), target);
+ }
+ return {
+ ASTRYX_PUBLISH_REGISTRY: 'http://127.0.0.1:9',
+ ASTRYX_PUBLISH_WAITS_MS: '0',
+ };
+}
+
+// A pnpm stand-in: logs each call; FAIL_PACKAGE always fails, FAIL_ONCE
+// fails its first publish only (the transparency log's 409, then success).
+const PNPM_STUB =
+ '#!/bin/sh\nprintf "%s\\n" "$*" >> "$PUBLISH_LOG"\n' +
+ '[ -z "$FAIL_PACKAGE" ] || case "$*" in *"$FAIL_PACKAGE"*) exit 1;; esac\n' +
+ 'if [ -n "$FAIL_ONCE" ]; then case "$*" in *"$FAIL_ONCE"*)\n' +
+ ' [ -f "$PUBLISH_LOG.once" ] || { : > "$PUBLISH_LOG.once"; echo "(409) an equivalent entry already exists in the transparency log" >&2; exit 1; };;\n' +
+ 'esac; fi\n';
+
function runCanaryPublish({
httpStatus,
curlExitCode = '0',
failingPackage = '',
+ failOnce = '',
}) {
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'astryx-canary-publish-'));
const publishLog = path.join(root, 'publish.log');
@@ -62,12 +89,8 @@ function runCanaryPublish({
'curl',
'#!/bin/sh\nprintf %s "$NATIVE_HTTP_STATUS"\nexit "$CURL_EXIT_CODE"\n',
);
- writeExecutable(
- root,
- 'pnpm',
- '#!/bin/sh\nprintf "%s\\n" "$*" >> "$PUBLISH_LOG"\n' +
- '[ -z "$FAIL_PACKAGE" ] || case "$*" in *"$FAIL_PACKAGE"*) exit 1;; esac\n',
- );
+ writeExecutable(root, 'pnpm', PNPM_STUB);
+ const publisherEnv = withPublisher(root);
const runnerTemp = path.join(root, 'runner-temp');
fs.mkdirSync(runnerTemp, {recursive: true});
@@ -79,7 +102,9 @@ function runCanaryPublish({
encoding: 'utf8',
env: {
...process.env,
+ ...publisherEnv,
CURL_EXIT_CODE: curlExitCode,
+ FAIL_ONCE: failOnce,
FAIL_PACKAGE: failingPackage,
GITHUB_SHA: 'abcdef1234567890',
NATIVE_HTTP_STATUS: httpStatus,
@@ -100,6 +125,58 @@ function runCanaryPublish({
}
}
+/** Runs the stable publish step over two packages with stubbed npm and pnpm. */
+function runStablePublish({failingPackage = '', failOnce = ''} = {}) {
+ const root = fs.mkdtempSync(path.join(os.tmpdir(), 'astryx-stable-publish-'));
+ const publishLog = path.join(root, 'publish.log');
+ try {
+ writeJson(root, 'packages/core/package.json', {
+ name: '@astryxdesign/core',
+ version: '0.7.7',
+ });
+ writeJson(root, 'packages/themes/neutral/package.json', {
+ name: '@astryxdesign/theme-neutral',
+ version: '0.7.7',
+ });
+ // Publishes after core in the loop and peers on it.
+ writeJson(root, 'packages/themes/stone/package.json', {
+ name: '@astryxdesign/theme-stone',
+ version: '0.7.7',
+ peerDependencies: {'@astryxdesign/core': '0.7.7'},
+ });
+ // `npm view` finds nothing on the registry, so every package publishes.
+ writeExecutable(root, 'npm', '#!/bin/sh\nexit 1\n');
+ writeExecutable(root, 'pnpm', PNPM_STUB);
+ const publisherEnv = withPublisher(root);
+ const result = spawnSync(
+ 'bash',
+ ['-c', step(stable, 'Publish changed packages').run],
+ {
+ cwd: root,
+ encoding: 'utf8',
+ env: {
+ ...process.env,
+ ...publisherEnv,
+ DRY_RUN: 'false',
+ FAIL_ONCE: failOnce,
+ FAIL_PACKAGE: failingPackage,
+ PATH: `${path.join(root, 'bin')}:${process.env.PATH}`,
+ PUBLISH_LOG: publishLog,
+ RELEASE_VERSION: '0.7.7',
+ },
+ },
+ );
+ return {
+ ...result,
+ published: fs.existsSync(publishLog)
+ ? fs.readFileSync(publishLog, 'utf8')
+ : '',
+ };
+ } finally {
+ fs.rmSync(root, {recursive: true, force: true});
+ }
+}
+
describe('stable and canary publication authority', () => {
it('keeps stable explicit and canary limited to main pushes', () => {
expect(stable.if).toBe("github.event_name == 'workflow_dispatch'");
@@ -273,6 +350,73 @@ describe('stable and canary publication authority', () => {
expect(result.published).toContain('publish ./packages/native');
});
+ it('publishes every package through the at-most-once publisher, never a bare pnpm publish', () => {
+ for (const job of [stable, canary]) {
+ const text = job.steps.map(candidate => candidate.run ?? '').join('\n');
+ expect(text).not.toMatch(/pnpm publish/u);
+ }
+ expect(step(stable, 'Publish changed packages').run).toContain(
+ 'node scripts/npm/publish-package.mjs --dir "./$pkg_dir" --tag latest',
+ );
+ expect(step(canary, 'Publish canary versions').run).toContain(
+ 'node scripts/npm/publish-package.mjs --dir "./$pkg_dir" --tag canary',
+ );
+ });
+
+ it('retries a canary whose first signature is rejected, and passes', () => {
+ const result = runCanaryPublish({
+ failOnce: 'packages/themes/neutral',
+ httpStatus: '200',
+ });
+
+ expect(result.status).toBe(0);
+ expect(
+ result.published
+ .split('\n')
+ .filter(line => line.startsWith('publish ./packages/themes/neutral')),
+ ).toHaveLength(2);
+ });
+
+ it('keeps publishing stable packages after one fails, then fails the step', () => {
+ const result = runStablePublish({failingPackage: 'packages/core'});
+
+ expect(result.status).toBe(1);
+ expect(result.stdout).toContain(
+ 'Stable publish failed for @astryxdesign/[email protected]',
+ );
+ expect(result.stdout).toContain(
+ 'Re-running this workflow on the same tag publishes only what is missing.',
+ );
+ expect(result.published).toContain('publish ./packages/themes/neutral');
+ });
+
+ it('holds back a stable package whose dependency failed, as failed too', () => {
+ const result = runStablePublish({failingPackage: 'packages/core'});
+
+ expect(result.status).toBe(1);
+ expect(result.stdout).toContain(
+ 'Not publishing @astryxdesign/[email protected] because @astryxdesign/core failed',
+ );
+ expect(result.stdout).toMatch(
+ /Stable publish failed for: @astryxdesign\/core @astryxdesign\/theme-stone\./u,
+ );
+ expect(result.published).not.toContain('publish ./packages/themes/stone');
+ expect(result.published).toContain('publish ./packages/themes/neutral');
+ });
+
+ it('retries a stable package whose first signature is rejected', () => {
+ // Both publish; the step then reaches registry verification, which this
+ // scratch root does not carry, so only the publish calls are asserted.
+ const result = runStablePublish({failOnce: 'packages/core'});
+
+ expect(
+ result.published
+ .split('\n')
+ .filter(line => line.startsWith('publish ./packages/core')),
+ ).toHaveLength(2);
+ expect(result.stdout).not.toContain('Stable publish failed');
+ });
+
it('validates every main merge-back against trusted policy and the immutable tag', () => {
const mergeBack = ci.jobs['release-merge-back'];
expect(mergeBack.if).toContain("github.base_ref == 'main'");
diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml
index 0da917c6f2..1a180d19f7 100644
--- a/.github/workflows/release.yml
+++ b/.github/workflows/release.yml
@@ -178,6 +178,7 @@ jobs:
run: |
set -eu
published=0
+ FAILED=""
for pkg_dir in packages/* packages/themes/*; do
[ -f "$pkg_dir/package.json" ] || continue
# Stable `latest` never includes canaryOnly packages (e.g. lab):
@@ -195,10 +196,35 @@ jobs:
echo "[dry-run] Would publish $NAME@$VERSION"
continue
fi
+ # A package that depends on (or peers on) a package that failed
+ # here would reach `latest` uninstallable: hold it back as failed
+ # too, so the re-run publishes both together.
+ BLOCKED_BY=$(node -e "
+ const p = require('./$pkg_dir/package.json');
+ const failed = new Set(process.argv[1].split(' ').filter(Boolean));
+ const deps = Object.keys({...p.dependencies, ...p.peerDependencies});
+ process.stdout.write(deps.filter(d => failed.has(d)).join(' '));
+ " "$FAILED")
+ if [ -n "$BLOCKED_BY" ]; then
+ echo "::error::Not publishing $NAME@$VERSION because $BLOCKED_BY failed"
+ FAILED="$FAILED $NAME"
+ continue
+ fi
echo "Publishing $NAME@$VERSION"
- pnpm publish "./$pkg_dir" --tag latest --provenance --access public --no-git-checks
- published=$((published + 1))
+ # At most once, retried while the registry lacks the version (see
+ # scripts/npm/publish-package.mjs). One package that cannot
+ # publish does not stop the rest; the step fails at the end.
+ if node scripts/npm/publish-package.mjs --dir "./$pkg_dir" --tag latest; then
+ published=$((published + 1))
+ else
+ echo "::error::Stable publish failed for $NAME@$VERSION"
+ FAILED="$FAILED $NAME"
+ fi
done
+ if [ -n "$FAILED" ]; then
+ echo "::error::Stable publish failed for:$FAILED. Re-running this workflow on the same tag publishes only what is missing."
+ exit 1
+ fi
if [ "$DRY_RUN" = "true" ]; then
echo "Dry run complete; no packages were published."
else
@@ -330,7 +356,12 @@ jobs:
esac
fi
echo "Publishing ${NAME}@${CANARY_VERSION}"
- if ! pnpm publish "./$pkg_dir" --tag canary --provenance --access public --no-git-checks; then
+ # publish-package.mjs publishes at most once and lets the
+ # registry decide: a version already there is skipped, and a
+ # failed attempt (npm's provenance signing can answer "409 an
+ # equivalent entry already exists") is retried with a new
+ # signature only while the registry does not have the version.
+ if ! node scripts/npm/publish-package.mjs --dir "./$pkg_dir" --tag canary; then
echo "::error::Canary publish failed for ${NAME}@${CANARY_VERSION}"
FAILED="${FAILED} ${NAME}"
fi
diff --git a/scripts/npm/publish-package.mjs b/scripts/npm/publish-package.mjs
new file mode 100644
index 0000000000..78ec72e76c
--- /dev/null
+++ b/scripts/npm/publish-package.mjs
@@ -0,0 +1,201 @@
+#!/usr/bin/env node
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+/**
+ * @file Publish one package to npm, at most once, with retries the registry
+ * decides.
+ * @input `--dir <package dir> --tag <dist-tag>`; the package's own
+ * package.json supplies its name and version. `ASTRYX_PUBLISH_REGISTRY`
+ * (default https://registry.npmjs.org), `ASTRYX_PUBLISH_ATTEMPTS` (3) and
+ * `ASTRYX_PUBLISH_WAITS_MS` (milliseconds to wait before each retry, default
+ * "20000,60000"; its first value is also the wait between registry reads,
+ * default 5000) exist so tests can run the real script quickly.
+ * @output Exit 0 when the registry has `name@version` (already there, just
+ * published, or published by an attempt whose client reported a failure);
+ * exit 1 when every attempt failed and the registry still does not have it.
+ * @position Both publish jobs in .github/workflows/release.yml call it once
+ * per package. The registry, not a client's exit code, decides whether a
+ * package is published:
+ * - A version already on the registry is not published again (a re-run
+ * after a partial publish skips what already landed).
+ * - A failed publish is checked against the registry before a retry. npm's
+ * provenance signing can fail with "409 an equivalent entry already
+ * exists in the transparency log" when its client re-sends the same
+ * signed entry after a lost reply; the package is then not published,
+ * and a new attempt signs a new entry, so it is retried.
+ * - npm never accepts a second publish of a version, so a retry cannot
+ * publish twice even when a registry read is stale.
+ */
+
+import {spawnSync} from 'node:child_process';
+import fs from 'node:fs';
+import path from 'node:path';
+import {pathToFileURL} from 'node:url';
+
+import {DEFAULT_REGISTRY, fetchVersionDocument} from './registry.mjs';
+
+const DEFAULT_ATTEMPTS = 3;
+const DEFAULT_RETRY_WAITS_MS = [20_000, 60_000];
+const DEFAULT_READ_INTERVAL_MS = 5_000;
+// Registry reads after a failed attempt: a version npm accepted can take a
+// few seconds to appear.
+const READS_AFTER_FAILURE = 4;
+
+/**
+ * Whether the registry has `name@version`: `true`, `false`, or `null` when
+ * the read failed (a network error or any status but 200 and 404).
+ */
+export async function registryHas({
+ name,
+ version,
+ registry = DEFAULT_REGISTRY,
+ fetchImpl = fetch,
+}) {
+ try {
+ const response = await fetchVersionDocument({
+ name,
+ version,
+ registry,
+ fetchImpl,
+ });
+ await response.body?.cancel();
+ if (response.status === 200) return true;
+ if (response.status === 404) return false;
+ return null;
+ } catch {
+ return null;
+ }
+}
+
+/**
+ * Publishes `name@version` at most once. `publish()` runs one publish and
+ * returns whether its process succeeded; `isPublished()` reads the registry
+ * (`true`, `false`, or `null` when unknown). Returns `{published, outcome,
+ * attempts}`, where `outcome` is `already-published`, `published`,
+ * `published-despite-error`, or `failed`.
+ */
+export async function publishOnce({
+ name,
+ version,
+ publish,
+ isPublished,
+ attempts = DEFAULT_ATTEMPTS,
+ retryWaitsMs = DEFAULT_RETRY_WAITS_MS,
+ readIntervalMs = DEFAULT_READ_INTERVAL_MS,
+ sleep = ms => new Promise(resolve => setTimeout(resolve, ms)),
+ log = () => {},
+}) {
+ if ((await isPublished()) === true) {
+ log(
+ `${name}@${version} is already on the registry; not publishing it again.`,
+ );
+ return {published: true, outcome: 'already-published', attempts: 0};
+ }
+ for (let attempt = 1; attempt <= attempts; attempt += 1) {
+ if (await publish()) {
+ return {published: true, outcome: 'published', attempts: attempt};
+ }
+ for (let read = 1; read <= READS_AFTER_FAILURE; read += 1) {
+ if ((await isPublished()) === true) {
+ log(
+ `${name}@${version} is on the registry although attempt ${attempt} reported a failure; not publishing it again.`,
+ );
+ return {
+ published: true,
+ outcome: 'published-despite-error',
+ attempts: attempt,
+ };
+ }
+ if (read < READS_AFTER_FAILURE) await sleep(readIntervalMs);
+ }
+ if (attempt < attempts) {
+ const wait =
+ retryWaitsMs[Math.min(attempt, retryWaitsMs.length) - 1] ?? 0;
+ log(
+ `${name}@${version} is not on the registry after attempt ${attempt} of ${attempts}; retrying in ${Math.round(wait / 1000)} s with a new signature.`,
+ );
+ await sleep(wait);
+ }
+ }
+ return {published: false, outcome: 'failed', attempts};
+}
+
+function parseArgs(argv) {
+ const args = {};
+ for (let i = 0; i < argv.length; i += 2) {
+ const flag = argv[i];
+ if (!flag?.startsWith('--') || argv[i + 1] == null) {
+ throw new Error(`expected --flag value pairs, got ${argv.join(' ')}`);
+ }
+ args[flag.slice(2)] = argv[i + 1];
+ }
+ for (const required of ['dir', 'tag']) {
+ if (!args[required]) throw new Error(`--${required} is required`);
+ }
+ return args;
+}
+
+function numberList(raw, fallback) {
+ if (raw == null || raw === '') return fallback;
+ const values = raw.split(',').map(Number);
+ if (values.some(value => !Number.isFinite(value) || value < 0)) {
+ throw new Error(`expected comma-separated milliseconds, got "${raw}"`);
+ }
+ return values;
+}
+
+async function main() {
+ const {dir, tag} = parseArgs(process.argv.slice(2));
+ const manifest = JSON.parse(
+ fs.readFileSync(path.join(dir, 'package.json'), 'utf8'),
+ );
+ const {name, version} = manifest;
+ const registry = process.env.ASTRYX_PUBLISH_REGISTRY || DEFAULT_REGISTRY;
+ const attempts = Number(
+ process.env.ASTRYX_PUBLISH_ATTEMPTS || DEFAULT_ATTEMPTS,
+ );
+ if (!Number.isInteger(attempts) || attempts < 1) {
+ throw new Error('ASTRYX_PUBLISH_ATTEMPTS must be a positive integer');
+ }
+ const waits = numberList(process.env.ASTRYX_PUBLISH_WAITS_MS, null);
+ const result = await publishOnce({
+ name,
+ version,
+ attempts,
+ retryWaitsMs: waits ?? DEFAULT_RETRY_WAITS_MS,
+ readIntervalMs: waits ? waits[0] : DEFAULT_READ_INTERVAL_MS,
+ isPublished: () => registryHas({name, version, registry}),
+ publish: () =>
+ spawnSync(
+ 'pnpm',
+ [
+ 'publish',
+ dir,
+ '--tag',
+ tag,
+ '--provenance',
+ '--access',
+ 'public',
+ '--no-git-checks',
+ ],
+ {stdio: 'inherit'},
+ ).status === 0,
+ log: message => console.log(message),
+ });
+ if (!result.published) {
+ console.error(
+ `${name}@${version} is not on the registry after ${result.attempts} attempt(s).`,
+ );
+ process.exit(1);
+ }
+}
+
+if (
+ process.argv[1] &&
+ import.meta.url === pathToFileURL(process.argv[1]).href
+) {
+ main().catch(error => {
+ console.error(error.message);
+ process.exit(1);
+ });
+}
diff --git a/scripts/npm/publish-package.test.mjs b/scripts/npm/publish-package.test.mjs
new file mode 100644
index 0000000000..7b50314b6a
--- /dev/null
+++ b/scripts/npm/publish-package.test.mjs
@@ -0,0 +1,170 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+/**
+ * @file Publishing one package at most once, with retries the registry
+ * decides (scripts/npm/publish-package.mjs).
+ */
+
+import {describe, expect, it} from 'vitest';
+
+import {publishOnce, registryHas} from './publish-package.mjs';
+
+/**
+ * A fake registry and publisher. `publishes` lists each attempt's outcome:
+ * `ok` (accepted), `lost` (accepted, but the client reported a failure, as
+ * when the reply is lost), or `fail` (rejected, e.g. the transparency log's
+ * 409). `present` is whether the version is already on the registry, and
+ * `reads` overrides what each registry read returns.
+ */
+function fake({present = false, publishes = ['ok'], reads} = {}) {
+ let onRegistry = present;
+ const calls = {publish: 0, read: 0, sleeps: []};
+ return {
+ calls,
+ options: {
+ name: '@astryxdesign/vega',
+ version: '0.7.6-canary.abc1234',
+ retryWaitsMs: [20_000, 60_000],
+ readIntervalMs: 5_000,
+ sleep: async ms => {
+ calls.sleeps.push(ms);
+ },
+ isPublished: async () => {
+ const read = reads?.[calls.read];
+ calls.read += 1;
+ return read === undefined ? onRegistry : read;
+ },
+ publish: async () => {
+ const outcome = publishes[calls.publish] ?? 'fail';
+ calls.publish += 1;
+ if (outcome === 'ok' || outcome === 'lost') onRegistry = true;
+ return outcome === 'ok';
+ },
+ },
+ };
+}
+
+describe('publishOnce', () => {
+ it('does not publish a version already on the registry', async () => {
+ const {calls, options} = fake({present: true});
+ expect(await publishOnce(options)).toEqual({
+ published: true,
+ outcome: 'already-published',
+ attempts: 0,
+ });
+ expect(calls.publish).toBe(0);
+ });
+
+ it('publishes once when the first attempt succeeds', async () => {
+ const {calls, options} = fake({publishes: ['ok']});
+ expect(await publishOnce(options)).toMatchObject({
+ published: true,
+ outcome: 'published',
+ attempts: 1,
+ });
+ expect(calls.publish).toBe(1);
+ expect(calls.sleeps).toEqual([]);
+ });
+
+ it('retries a rejected attempt (the transparency log 409) while the registry lacks the version', async () => {
+ const {calls, options} = fake({publishes: ['fail', 'ok']});
+ expect(await publishOnce(options)).toMatchObject({
+ published: true,
+ outcome: 'published',
+ attempts: 2,
+ });
+ expect(calls.publish).toBe(2);
+ // Four registry reads after the failure, then the first retry wait.
+ expect(calls.sleeps).toEqual([5_000, 5_000, 5_000, 20_000]);
+ });
+
+ it('does not publish again when the registry has the version after a reported failure', async () => {
+ const {calls, options} = fake({publishes: ['lost']});
+ expect(await publishOnce(options)).toMatchObject({
+ published: true,
+ outcome: 'published-despite-error',
+ attempts: 1,
+ });
+ expect(calls.publish).toBe(1);
+ });
+
+ it('waits for a version that appears on the registry a few seconds after a reported failure', async () => {
+ // Pre-check, then three reads that still miss it, then it appears.
+ const {calls, options} = fake({
+ publishes: ['lost'],
+ reads: [false, false, false, false],
+ });
+ expect(await publishOnce(options)).toMatchObject({
+ outcome: 'published-despite-error',
+ });
+ expect(calls.publish).toBe(1);
+ expect(calls.read).toBe(5);
+ });
+
+ it('fails a package that never reaches the registry, after every attempt', async () => {
+ const {calls, options} = fake({publishes: ['fail', 'fail', 'fail']});
+ expect(await publishOnce(options)).toEqual({
+ published: false,
+ outcome: 'failed',
+ attempts: 3,
+ });
+ expect(calls.publish).toBe(3);
+ expect(calls.sleeps.filter(ms => ms > 5_000)).toEqual([20_000, 60_000]);
+ });
+
+ it('never skips a package on an unknown registry read', async () => {
+ // The pre-check cannot read the registry: it publishes (npm refuses a
+ // second publish of a version, so this cannot publish twice).
+ const unknown = fake({reads: [null], publishes: ['ok']});
+ expect(await publishOnce(unknown.options)).toMatchObject({
+ outcome: 'published',
+ });
+ expect(unknown.calls.publish).toBe(1);
+
+ // After a failure, unknown reads do not count as published.
+ const failing = fake({
+ publishes: ['fail'],
+ reads: [false, null, null, null, null],
+ });
+ expect(await publishOnce({...failing.options, attempts: 1})).toMatchObject({
+ published: false,
+ outcome: 'failed',
+ });
+ });
+});
+
+describe('registryHas', () => {
+ const response = status => new Response('{}', {status});
+
+ it('reads the exact version document', async () => {
+ const urls = [];
+ const fetchImpl = async url => {
+ urls.push(url);
+ return response(200);
+ };
+ expect(
+ await registryHas({
+ name: '@astryxdesign/vega',
+ version: '0.7.6-canary.abc1234',
+ registry: 'https://registry.example/',
+ fetchImpl,
+ }),
+ ).toBe(true);
+ expect(urls).toEqual([
+ 'https://registry.example/%40astryxdesign%2Fvega/0.7.6-canary.abc1234',
+ ]);
+ });
+
+ it('answers false for 404 and unknown for any other status or a network error', async () => {
+ const read = fetchImpl =>
+ registryHas({name: '@astryxdesign/vega', version: '1.0.0', fetchImpl});
+ expect(await read(async () => response(404))).toBe(false);
+ expect(await read(async () => response(503))).toBe(null);
+ expect(await read(async () => response(401))).toBe(null);
+ expect(
+ await read(async () => {
+ throw new TypeError('fetch failed');
+ }),
+ ).toBe(null);
+ });
+});
diff --git a/scripts/npm/registry.mjs b/scripts/npm/registry.mjs
new file mode 100644
index 0000000000..dbbb40fca2
--- /dev/null
+++ b/scripts/npm/registry.mjs
@@ -0,0 +1,41 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+/**
+ * @file registry.mjs
+ * @input A package name and version, an npm registry URL, and a fetch
+ * @output `fetchVersionDocument`: the registry's response for one exact
+ * version's document
+ * @position The one way the release scripts read a version from npm:
+ * publish-package.mjs asks whether a version exists before and after each
+ * publish, and verify-published.mjs confirms a release propagated. Keeping
+ * the URL and fetch options here stops the two from drifting apart.
+ */
+
+export const DEFAULT_REGISTRY = 'https://registry.npmjs.org';
+
+/** A registry path for a package name, scoped names included. */
+export function packageKey(name) {
+ return encodeURIComponent(name);
+}
+
+/** `pathname` resolved against the registry root. */
+export function registryUrl(registry, pathname) {
+ return new URL(pathname, `${registry.replace(/\/+$/u, '')}/`).href;
+}
+
+/** Fetches the document for exactly `name@version`, never from a cache. */
+export function fetchVersionDocument({
+ name,
+ version,
+ registry = DEFAULT_REGISTRY,
+ fetchImpl = fetch,
+}) {
+ return fetchImpl(
+ registryUrl(registry, `${packageKey(name)}/${encodeURIComponent(version)}`),
+ {
+ cache: 'no-store',
+ headers: {accept: 'application/json'},
+ signal: AbortSignal.timeout(30_000),
+ },
+ );
+}
diff --git a/scripts/npm/verify-published.mjs b/scripts/npm/verify-published.mjs
index a8436c2570..fd67755e7f 100644
--- a/scripts/npm/verify-published.mjs
+++ b/scripts/npm/verify-published.mjs
@@ -15,19 +15,16 @@ import path from 'node:path';
import {fileURLToPath} from 'node:url';
import {isStableReleasePackage} from '../release/active-release.mjs';
+import {
+ DEFAULT_REGISTRY,
+ fetchVersionDocument,
+ packageKey,
+ registryUrl,
+} from './registry.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}`);
@@ -47,16 +44,8 @@ export async function verifyRegistryEntry({
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),
- }),
+ await fetchVersionDocument({name, version, registry, fetchImpl}),
`${name}@${version} version document`,
);
if (versionDocument.version !== version) {