From 2b96361613bbc9663a4adeaa433f2a5c41a2385e Mon Sep 17 00:00:00 2001 From: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> Date: Wed, 25 Nov 2026 00:35:28 +0800 Subject: [PATCH] Wait for npm metadata after trusted plugin publication Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --- .github/workflows/dsh-plugin-publish.yml | 6 +++- .../scripts/verify-distribution.mjs | 29 ++++++++++++++----- .../smoke/dsh-registry-smoke.mjs | 15 ++++++++-- 3 files changed, 40 insertions(+), 10 deletions(-) diff --git a/.github/workflows/dsh-plugin-publish.yml b/.github/workflows/dsh-plugin-publish.yml index 5bdf46e144..cf3b9fe5c0 100644 --- a/.github/workflows/dsh-plugin-publish.yml +++ b/.github/workflows/dsh-plugin-publish.yml @@ -121,4 +121,8 @@ jobs: if git cat-file -e origin/main:packages/dsh-loopx-plugin/scripts/verify-distribution.mjs 2>/dev/null; then git show origin/main:packages/dsh-loopx-plugin/scripts/verify-distribution.mjs > scripts/verify-distribution.mjs fi - node scripts/verify-distribution.mjs --tarball "$DSH_ARTIFACT" + propagation_timeout=0 + if [ "$DSH_ALREADY_PUBLISHED" = false ]; then + propagation_timeout=180000 + fi + node scripts/verify-distribution.mjs --tarball "$DSH_ARTIFACT" --registry-propagation-timeout-ms "$propagation_timeout" diff --git a/packages/dsh-loopx-plugin/scripts/verify-distribution.mjs b/packages/dsh-loopx-plugin/scripts/verify-distribution.mjs index affdf37bcf..2a2fb88980 100644 --- a/packages/dsh-loopx-plugin/scripts/verify-distribution.mjs +++ b/packages/dsh-loopx-plugin/scripts/verify-distribution.mjs @@ -6,6 +6,7 @@ import assert from 'node:assert/strict' import { createHash } from 'node:crypto' import { readFile } from 'node:fs/promises' import { dirname, join, resolve } from 'node:path' +import { setTimeout as delay } from 'node:timers/promises' import { fileURLToPath } from 'node:url' const packageRoot = resolve(dirname(fileURLToPath(import.meta.url)), '..') @@ -13,27 +14,30 @@ const manifest = JSON.parse(await readFile(join(packageRoot, 'package.json'), 'u const args = process.argv.slice(2) let tarball let registry = 'https://registry.npmjs.org/' +let registryPropagationTimeoutMs = 0 let searchTimeoutMs = 45_000 let searchPollMs = 3_000 for (let index = 0; index < args.length; index += 2) { const [flag, value] = args.slice(index, index + 2) assert( - value && (flag === '--tarball' || flag === '--registry' || flag === '--search-timeout-ms' || flag === '--search-poll-ms'), - 'usage: verify-distribution.mjs --tarball PATH [--registry URL] [--search-timeout-ms MS] [--search-poll-ms MS]', + value && ['--tarball', '--registry', '--registry-propagation-timeout-ms', '--search-timeout-ms', '--search-poll-ms'].includes(flag), + 'usage: verify-distribution.mjs --tarball PATH [--registry URL] [--registry-propagation-timeout-ms MS] [--search-timeout-ms MS] [--search-poll-ms MS]', ) if (flag === '--tarball') tarball = resolve(value) else if (flag === '--registry') registry = `${value.replace(/\/+$/u, '')}/` else { const parsed = Number(value) assert(Number.isFinite(parsed) && parsed >= 0, `${flag} must be a non-negative number`) - if (flag === '--search-timeout-ms') searchTimeoutMs = parsed + if (flag === '--registry-propagation-timeout-ms') registryPropagationTimeoutMs = parsed + else if (flag === '--search-timeout-ms') searchTimeoutMs = parsed else searchPollMs = parsed } } assert(tarball, '--tarball is required: compare the exact package approved for publication') -async function json(path) { +async function json(path, { allowMissing = false } = {}) { const response = await fetch(new URL(path, registry), { signal: AbortSignal.timeout(15_000) }) + if (allowMissing && response.status === 404) return null assert(response.ok, `registry readback failed: HTTP ${response.status}`) return response.json() } @@ -42,8 +46,19 @@ function repoSlug(url) { return String(url ?? '').replace(/^git\+/u, '').replace(/\.git$/u, '').toLowerCase() } -const published = await json(encodeURIComponent(manifest.name)) -const version = published.versions?.[manifest.version] +// npm can accept a publish before the public package metadata and latest tag +// reflect it. Only the publishing workflow opts into waiting for that state. +const packagePath = encodeURIComponent(manifest.name) +const allowMissing = registryPropagationTimeoutMs > 0 +const registryDeadline = Date.now() + registryPropagationTimeoutMs +let published = await json(packagePath, { allowMissing }) +while (!published?.versions?.[manifest.version] || published['dist-tags']?.latest !== manifest.version) { + const remainingMs = registryDeadline - Date.now() + if (remainingMs <= 0) break + await delay(Math.min(3_000, remainingMs)) + published = await json(packagePath, { allowMissing }) +} +const version = published?.versions?.[manifest.version] assert(version, `registry has no ${manifest.name}@${manifest.version}`) assert.equal(published['dist-tags']?.latest, manifest.version, 'DSH Hub installs the unversioned package: latest must name the qualified release') @@ -98,7 +113,7 @@ let candidates = await searchRepositoryCandidates() let selected = selectPluginCandidate(candidates) while (selected?.package.name !== manifest.name && Date.now() < deadline) { const delayMs = Math.min(searchPollMs, Math.max(0, deadline - Date.now())) - if (delayMs > 0) await new Promise(resolveDelay => setTimeout(resolveDelay, delayMs)) + if (delayMs > 0) await delay(delayMs) candidates = await searchRepositoryCandidates() selected = selectPluginCandidate(candidates) } diff --git a/packages/dsh-loopx-plugin/smoke/dsh-registry-smoke.mjs b/packages/dsh-loopx-plugin/smoke/dsh-registry-smoke.mjs index b7cbba2e76..15359a018c 100644 --- a/packages/dsh-loopx-plugin/smoke/dsh-registry-smoke.mjs +++ b/packages/dsh-loopx-plugin/smoke/dsh-registry-smoke.mjs @@ -41,13 +41,16 @@ const metadata = { let repositorySearchObjects = searchObjects let maintainerSearchObjects = [] let delayedRepoSearchAttemptsRemaining = 0 +const previousRegistrySnapshots = [] const server = createServer((request, response) => { const url = new URL(request.url, registry) requested.add(url.pathname) if (url.pathname === `/${manifest.name}`) { response.setHeader('Content-Type', 'application/json') - response.end(JSON.stringify({ name: manifest.name, maintainers: [{ name: 'fixture-maintainer' }], - 'dist-tags': distTags, versions: { [manifest.version]: metadata } })) + response.end(JSON.stringify(previousRegistrySnapshots.shift() ?? { + name: manifest.name, maintainers: [{ name: 'fixture-maintainer' }], + 'dist-tags': distTags, versions: { [manifest.version]: metadata }, + })) } else if (url.pathname === '/artifact.tgz') { response.end(bytes) } else if (url.pathname === '/-/v1/search') { @@ -132,6 +135,14 @@ try { repositorySearchObjects = searchObjects await run(process.execPath, [join(packageRoot, 'scripts', 'verify-distribution.mjs'), '--tarball', tarball, '--registry', registry, '--search-timeout-ms', '2000', '--search-poll-ms', '50']) + // A successful publish can become visible before its latest tag propagates. + previousRegistrySnapshots.push( + { 'dist-tags': { latest: '0.0.0' }, versions: {} }, + { 'dist-tags': { latest: '0.0.0' }, versions: { [manifest.version]: metadata } }, + ) + await run(process.execPath, [join(packageRoot, 'scripts', 'verify-distribution.mjs'), + '--tarball', tarball, '--registry', registry, '--registry-propagation-timeout-ms', '12000']) + assert.equal(previousRegistrySnapshots.length, 0) // Existing publication metadata cannot hide a wrong tag or discovery target. distTags.latest = '0.0.0' await assert.rejects(run(process.execPath, [join(packageRoot, 'scripts', 'verify-distribution.mjs'),