|
1 | 1 | import { execFileSync, spawnSync } from "node:child_process"; |
| 2 | +import { createHash } from "node:crypto"; |
2 | 3 | import { readFileSync } from "node:fs"; |
3 | | -import { join, resolve } from "node:path"; |
| 4 | +import { basename, dirname, join, resolve } from "node:path"; |
| 5 | +import { pathToFileURL } from "node:url"; |
4 | 6 |
|
5 | | -const packageIndex = process.argv.indexOf("--package"); |
6 | | -const artifactsIndex = process.argv.indexOf("--artifacts"); |
7 | | -const packageName = packageIndex >= 0 ? process.argv[packageIndex + 1] : undefined; |
8 | | -const artifactsDir = resolve(artifactsIndex >= 0 ? process.argv[artifactsIndex + 1] : "release-artifacts"); |
9 | | -if (!packageName) throw new Error("--package is required"); |
| 7 | +export const REGISTRY_RETRY_DELAYS_MS = [ |
| 8 | + 5_000, |
| 9 | + 10_000, |
| 10 | + 20_000, |
| 11 | + 30_000, |
| 12 | + 45_000, |
| 13 | + 60_000, |
| 14 | + 60_000, |
| 15 | + 60_000, |
| 16 | + 60_000, |
| 17 | + 60_000, |
| 18 | + 60_000, |
| 19 | + 60_000, |
| 20 | +]; |
10 | 21 |
|
11 | | -const manifest = JSON.parse(readFileSync(join(artifactsDir, "manifest.json"), "utf8")); |
12 | | -const artifact = manifest.find((entry) => entry.name === packageName); |
13 | | -if (!artifact) throw new Error(`release manifest has no entry for ${packageName}`); |
14 | | -const spec = `${artifact.name}@${artifact.version}`; |
| 22 | +function sha512Integrity(bytes) { |
| 23 | + return `sha512-${createHash("sha512").update(bytes).digest("base64")}`; |
| 24 | +} |
| 25 | + |
| 26 | +export function loadReleaseArtifact(packageName, artifactsDirectory) { |
| 27 | + const artifactsDir = resolve(artifactsDirectory); |
| 28 | + const manifest = JSON.parse(readFileSync(join(artifactsDir, "manifest.json"), "utf8")); |
| 29 | + if (!Array.isArray(manifest)) throw new Error("release manifest must be an array"); |
| 30 | + |
| 31 | + const artifact = manifest.find((entry) => entry?.name === packageName); |
| 32 | + if (!artifact) throw new Error(`release manifest has no entry for ${packageName}`); |
| 33 | + if ( |
| 34 | + typeof artifact.version !== "string" || |
| 35 | + typeof artifact.filename !== "string" || |
| 36 | + basename(artifact.filename) !== artifact.filename |
| 37 | + ) { |
| 38 | + throw new Error(`release manifest has invalid artifact metadata for ${packageName}`); |
| 39 | + } |
| 40 | + |
| 41 | + const artifactPath = resolve(artifactsDir, artifact.filename); |
| 42 | + if (dirname(artifactPath) !== artifactsDir) { |
| 43 | + throw new Error(`release artifact escapes artifacts directory: ${artifact.filename}`); |
| 44 | + } |
15 | 45 |
|
16 | | -function registryState() { |
| 46 | + const bytes = readFileSync(artifactPath); |
| 47 | + const integrity = sha512Integrity(bytes); |
| 48 | + if (integrity !== artifact.integrity) { |
| 49 | + throw new Error(`${packageName}@${artifact.version} artifact bytes do not match release manifest`); |
| 50 | + } |
| 51 | + if (Number.isSafeInteger(artifact.size) && bytes.length !== artifact.size) { |
| 52 | + throw new Error(`${packageName}@${artifact.version} artifact size does not match release manifest`); |
| 53 | + } |
| 54 | + |
| 55 | + return { ...artifact, path: artifactPath, integrity }; |
| 56 | +} |
| 57 | + |
| 58 | +function registryState(artifact, execNpm) { |
| 59 | + const spec = `${artifact.name}@${artifact.version}`; |
17 | 60 | try { |
18 | | - const raw = execFileSync("npm", ["view", spec, "dist", "--json"], { |
| 61 | + const raw = execNpm("npm", ["view", spec, "dist", "--json", "--prefer-online"], { |
19 | 62 | encoding: "utf8", |
20 | 63 | stdio: ["ignore", "pipe", "pipe"], |
21 | 64 | }); |
22 | 65 | const dist = JSON.parse(raw); |
23 | 66 | if (dist.integrity !== artifact.integrity) { |
24 | | - throw new Error(`${spec} exists with different package bytes`); |
| 67 | + return { state: "pending", reason: "registry reports different package bytes" }; |
25 | 68 | } |
26 | 69 | if (!dist.attestations?.url) { |
27 | | - throw new Error(`${spec} exists without npm provenance`); |
| 70 | + return { state: "pending", reason: "registry has not exposed npm provenance" }; |
28 | 71 | } |
29 | | - return "verified"; |
| 72 | + return { state: "verified" }; |
30 | 73 | } catch (error) { |
31 | 74 | const stderr = String(error?.stderr ?? ""); |
32 | | - if (stderr.includes("E404")) return "missing"; |
33 | | - throw error; |
| 75 | + if (stderr.includes("E404")) return { state: "missing", reason: "version is not visible" }; |
| 76 | + return { state: "pending", reason: error instanceof Error ? error.message : String(error) }; |
34 | 77 | } |
35 | 78 | } |
36 | 79 |
|
37 | | -if (registryState() === "verified") { |
38 | | - console.log(`verified ${spec} integrity and provenance`); |
39 | | - process.exit(0); |
| 80 | +function sleep(milliseconds) { |
| 81 | + return new Promise((resolvePromise) => setTimeout(resolvePromise, milliseconds)); |
40 | 82 | } |
41 | 83 |
|
42 | | -const published = spawnSync( |
43 | | - "npm", |
44 | | - ["publish", join(artifactsDir, artifact.filename), "--access", "public", "--provenance"], |
45 | | - { stdio: "inherit" }, |
46 | | -); |
47 | | -for (let attempt = 1; attempt <= 12; attempt += 1) { |
48 | | - try { |
49 | | - if (registryState() === "verified") { |
50 | | - console.log(`verified ${spec} integrity and provenance`); |
51 | | - process.exit(0); |
| 84 | +export async function publishOrVerify({ |
| 85 | + packageName, |
| 86 | + artifactsDir, |
| 87 | + execNpm = execFileSync, |
| 88 | + spawnNpm = spawnSync, |
| 89 | + retryDelaysMs = REGISTRY_RETRY_DELAYS_MS, |
| 90 | + wait = sleep, |
| 91 | + log = console.log, |
| 92 | +}) { |
| 93 | + if (!packageName) throw new Error("--package is required"); |
| 94 | + const artifact = loadReleaseArtifact(packageName, artifactsDir); |
| 95 | + const spec = `${artifact.name}@${artifact.version}`; |
| 96 | + let publishResult; |
| 97 | + |
| 98 | + for (let attempt = 0; attempt <= retryDelaysMs.length; attempt += 1) { |
| 99 | + const registry = registryState(artifact, execNpm); |
| 100 | + if (registry.state === "verified") { |
| 101 | + log(`verified ${spec} byte identity and provenance`); |
| 102 | + return; |
52 | 103 | } |
53 | | - } catch (error) { |
54 | | - if (attempt === 12) throw error; |
| 104 | + |
| 105 | + if (registry.state === "missing" && publishResult === undefined) { |
| 106 | + publishResult = spawnNpm( |
| 107 | + "npm", |
| 108 | + ["publish", artifact.path, "--access", "public", "--provenance"], |
| 109 | + { stdio: "inherit" }, |
| 110 | + ); |
| 111 | + if (publishResult.error) { |
| 112 | + log(`npm publish could not start: ${publishResult.error.message}`); |
| 113 | + } else if (publishResult.status !== 0) { |
| 114 | + log(`npm publish exited ${publishResult.status}; checking whether the registry committed it`); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + if (attempt === retryDelaysMs.length) { |
| 119 | + const publishSummary = |
| 120 | + publishResult === undefined ? "npm publish was not attempted" : `npm publish exited ${publishResult.status}`; |
| 121 | + throw new Error(`${publishSummary}; registry never confirmed ${spec}: ${registry.reason}`); |
| 122 | + } |
| 123 | + |
| 124 | + const delayMs = retryDelaysMs[attempt]; |
| 125 | + log( |
| 126 | + `registry verification attempt ${attempt + 1}/${retryDelaysMs.length + 1} has not confirmed ${spec}` + |
| 127 | + ` (${registry.reason}); retrying in ${delayMs / 1_000}s`, |
| 128 | + ); |
| 129 | + await wait(delayMs); |
55 | 130 | } |
56 | | - console.log(`registry verification attempt ${attempt}/12 has not confirmed ${spec}`); |
57 | | - Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 5_000); |
58 | 131 | } |
59 | | -throw new Error(`npm publish exited ${published.status}; registry never confirmed ${spec}`); |
| 132 | + |
| 133 | +function parseArguments(argv) { |
| 134 | + const packageIndex = argv.indexOf("--package"); |
| 135 | + const artifactsIndex = argv.indexOf("--artifacts"); |
| 136 | + return { |
| 137 | + packageName: packageIndex >= 0 ? argv[packageIndex + 1] : undefined, |
| 138 | + artifactsDir: resolve(artifactsIndex >= 0 ? argv[artifactsIndex + 1] : "release-artifacts"), |
| 139 | + }; |
| 140 | +} |
| 141 | + |
| 142 | +const entryPath = process.argv[1] ? pathToFileURL(resolve(process.argv[1])).href : undefined; |
| 143 | +if (entryPath === import.meta.url) { |
| 144 | + await publishOrVerify(parseArguments(process.argv.slice(2))); |
| 145 | +} |
0 commit comments