Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .github/workflows/dsh-plugin-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
29 changes: 22 additions & 7 deletions packages/dsh-loopx-plugin/scripts/verify-distribution.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,38 @@ 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)), '..')
const manifest = JSON.parse(await readFile(join(packageRoot, 'package.json'), 'utf8'))
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()
}
Expand All @@ -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')
Expand Down Expand Up @@ -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)
}
Expand Down
15 changes: 13 additions & 2 deletions packages/dsh-loopx-plugin/smoke/dsh-registry-smoke.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down Expand Up @@ -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'),
Expand Down
Loading