From 2af0c1813d04c7e8952d094a23be1ec7c9c9debf Mon Sep 17 00:00:00 2001 From: Aleksandr Shevchuk Date: Fri, 20 Nov 2026 09:08:54 +0200 Subject: [PATCH 1/3] ci: publish waits for npm to serve what a package depends on (#253) npm processes a new version before it serves it: 1 to 4 minutes for most packages and 14 for the 31 MB pack in release 1.7.0, which took four runs. scripts/published.mjs asks again every 30 s for up to H5P_PUBLISH_WAIT minutes, which publish.yml sets to 30 (0 by hand and in a dry run). It runs in the prepublishOnly of every package that names another by workspace: (verify, embed, author, mcp), and the three pin checks use it. A request is cut at 30 s. --- .github/workflows/publish.yml | 5 + design/build.md | 16 ++- design/layout.md | 2 + packages/author/package.json | 2 +- .../author/scripts/check-pack-published.mjs | 10 +- packages/author/tests/published.test.ts | 112 ++++++++++++++++++ packages/embed/package.json | 2 +- packages/embed/scripts/check-published.mjs | 4 +- packages/mcp/package.json | 2 +- .../player/scripts/check-library-pack.mjs | 6 +- packages/verify/package.json | 2 +- scripts/published.mjs | 107 +++++++++++++++++ 12 files changed, 258 insertions(+), 12 deletions(-) create mode 100644 packages/author/tests/published.test.ts create mode 100644 scripts/published.mjs diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index a8cce92a..59bffac5 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -63,7 +63,12 @@ jobs: # --workspace-concurrency: given it, pnpm stopped on the unknown option before publishing # anything (the first run). Without git checks: prepack writes files into the tree (types/, # the licence, the skill), and the environment already holds the run to main. + # Each package's check that what it depends on is on npm waits up to H5P_PUBLISH_WAIT + # minutes for npm to serve a version published earlier in this run: npm processes a new one + # first, 1 to 4 minutes for most packages and 14 for the 31 MB library pack in release + # 1.7.0 (#253). A dry run publishes nothing, so there it fails at once, as by hand. - run: pnpm -r publish --access public --no-git-checks $DRY_RUN env: NPM_CONFIG_PROVENANCE: true DRY_RUN: ${{ inputs.dry-run && '--dry-run' || '' }} + H5P_PUBLISH_WAIT: ${{ inputs.dry-run && '0' || '30' }} diff --git a/design/build.md b/design/build.md index c95f3513..ab859047 100644 --- a/design/build.md +++ b/design/build.md @@ -147,7 +147,21 @@ the commit its tarball came from. Each package's trusted publisher on npmjs.com the environment, and ticks `npm publish` under Allowed actions: one created after 2026-09-03 allows only `npm stage publish` until it is ticked, and the registry refuses a publish with "403 OIDC permission denied for this action" after the token exchange has worked (the second run, on -2026-11-19). A new one expires unless a publish through it succeeds within 2 days. pnpm publishes through npm, which takes that token from 11.5.1, so the job runs +2026-11-19). A new one expires unless a publish through it succeeds within 2 days. + +**A package is published only once npm serves what it depends on** (#253). npm takes a version in, +then processes it before it serves it. In release 1.7.0 that took 1 to 4 minutes for most packages +and 14 for the 31 MB library pack. The release took four runs, since each check that a dependency +is on npm failed on one published moments before. Two kinds of check now wait while npm processes: +- `scripts/published.mjs`, in the `prepublishOnly` of every package that names another by + `workspace:` (verify, embed, author and mcp), checks each at the version the workspace has; +- the three checks of what a package pins, the player's pack and embed's and the author's, use its + `served()`. + +Both ask again every 30 s for up to `H5P_PUBLISH_WAIT` minutes, which `publish.yml` sets to 30, +twice the longest seen. By hand and in a dry run it is 0, where a 404 means the dependency was not +published, and the check says so at once. Each request is cut at 30 s: one to npm hung for over 5 +minutes. pnpm publishes through npm, which takes that token from 11.5.1, so the job runs Node 24. A publish by hand, with a one-time password, still works and records no provenance. A publish of the player runs its `prepublishOnly` (a check that the pack it names is on npm, diff --git a/design/layout.md b/design/layout.md index 782cf047..689f732d 100644 --- a/design/layout.md +++ b/design/layout.md @@ -48,6 +48,8 @@ apps/skills/ the skills' test project: author-verify, author-playback, front-matter skills/ the agent skills, at the root, where `npx skills add` looks docs/ the README's longer sections, for integrators +scripts/ published.mjs, the check before a package is published that npm serves what + it depends on (design/build.md) api/, vercel.json the hosted demo's Vercel function and deployment; at the root because that is the Vercel project's root, which only the dashboard can move ``` diff --git a/packages/author/package.json b/packages/author/package.json index 46b2afd9..68f2539e 100644 --- a/packages/author/package.json +++ b/packages/author/package.json @@ -66,7 +66,7 @@ "build:tool": "pnpm --filter @missing-elements/h5p-offline-player build:plan && node scripts/build-tool.mjs", "sync:skill": "pnpm --filter @missing-elements/h5p-offline-player build:plan && node scripts/sync-skill.mjs", "prepack": "node -e \"require('node:fs').copyFileSync('../../LICENSE', 'LICENSE')\" && node scripts/copy-skill.mjs && pnpm types && pnpm build:tool", - "prepublishOnly": "node -e \"if (!/^pnpm\\//.test(process.env.npm_config_user_agent || '')) { console.error('Publish with pnpm publish: npm would ship the workspace: dependency ranges as they are, and the package would not install.'); process.exit(1) }\" && node scripts/check-pack-published.mjs && pnpm typecheck && pnpm test", + "prepublishOnly": "node -e \"if (!/^pnpm\\//.test(process.env.npm_config_user_agent || '')) { console.error('Publish with pnpm publish: npm would ship the workspace: dependency ranges as they are, and the package would not install.'); process.exit(1) }\" && node ../../scripts/published.mjs && node scripts/check-pack-published.mjs && pnpm typecheck && pnpm test", "types": "pnpm --filter @missing-elements/h5p-offline-player exec tsc -b && node -e \"require('node:fs').rmSync('types', { recursive: true, force: true })\" && tsc -p tsconfig.types.json" }, "dependencies": { diff --git a/packages/author/scripts/check-pack-published.mjs b/packages/author/scripts/check-pack-published.mjs index f0869ea6..2bff0ef8 100644 --- a/packages/author/scripts/check-pack-published.mjs +++ b/packages/author/scripts/check-pack-published.mjs @@ -11,18 +11,20 @@ */ import { readFile } from 'node:fs/promises' import { resolve } from 'node:path' +import { served } from '../../../scripts/published.mjs' const packages = resolve(import.meta.dirname, '..', '..') /** @param {string} directory */ const versionOf = async (directory) => JSON.parse(await readFile(resolve(packages, directory, 'package.json'), 'utf8')).version +// Each waited for in CI, where it was published moments before in the same run (#253). const version = await versionOf('libraries') -const response = await fetch(`https://registry.npmjs.org/@missing-elements%2fh5p-libraries/${version}`) +const response = await served(`https://registry.npmjs.org/@missing-elements%2fh5p-libraries/${version}`, `@missing-elements/h5p-libraries@${version}`) if (!response.ok) { console.error(`[library-pack] @missing-elements/h5p-libraries@${version} is not on npm (${response.status}). Publish it from packages/libraries first: the author package pins it.`) process.exit(1) } -const index = await fetch(`https://cdn.jsdelivr.net/npm/@missing-elements/h5p-libraries@${version}/parts/index.json`) +const index = await served(`https://cdn.jsdelivr.net/npm/@missing-elements/h5p-libraries@${version}/parts/index.json`, `h5p-libraries@${version}/parts/index.json on jsDelivr`) const local = await readFile(resolve(packages, 'libraries', 'parts', 'index.json'), 'utf8') const same = index.ok && JSON.stringify((await index.json()).libraries) === JSON.stringify(JSON.parse(local).libraries) if (!same) { @@ -32,7 +34,7 @@ if (!same) { console.log(`[library-pack] @missing-elements/h5p-libraries@${version} is published, with this checkout's index`) const player = await versionOf('player') -const published = await fetch(`https://registry.npmjs.org/@missing-elements%2fh5p-offline-player/${player}`) +const published = await served(`https://registry.npmjs.org/@missing-elements%2fh5p-offline-player/${player}`, `@missing-elements/h5p-offline-player@${player}`) if (!published.ok) { console.error(`[player] @missing-elements/h5p-offline-player@${player} is not on npm (${published.status}). Publish it from packages/player first: the author package pins it, for its ./plan.`) process.exit(1) @@ -44,7 +46,7 @@ if (!(await published.json()).exports?.['./plan']) { console.log(`[player] @missing-elements/h5p-offline-player@${player} is published, and exports ./plan`) const normalize = await versionOf('normalize') -const normalizer = await fetch(`https://registry.npmjs.org/@missing-elements%2fh5p-normalize/${normalize}`) +const normalizer = await served(`https://registry.npmjs.org/@missing-elements%2fh5p-normalize/${normalize}`, `@missing-elements/h5p-normalize@${normalize}`) if (!normalizer.ok) { console.error(`[normalize] @missing-elements/h5p-normalize@${normalize} is not on npm (${normalizer.status}). Publish it from packages/normalize first: the author package depends on it, for its ./crc32.`) process.exit(1) diff --git a/packages/author/tests/published.test.ts b/packages/author/tests/published.test.ts new file mode 100644 index 00000000..7503f779 --- /dev/null +++ b/packages/author/tests/published.test.ts @@ -0,0 +1,112 @@ +import { mkdir, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises' +import { tmpdir } from 'node:os' +import { join, resolve } from 'node:path' +import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest' +import { dependenciesServed, served } from '../../../scripts/published.mjs' + +/* + * The check, before a package is published, that npm serves what it depends on (#253): in CI it + * waits for a version published moments before in the same run, which npm processes first. + */ + +let dir: string +beforeAll(async () => { + dir = await mkdtemp(join(tmpdir(), 'h5p-published-')) + vi.spyOn(console, 'log').mockImplementation(() => {}) +}) +afterAll(async () => { + vi.restoreAllMocks() + vi.unstubAllEnvs() + await rm(dir, { recursive: true, force: true }) +}) + +/** A fetch that answers with the statuses given, in turn, and with the last one after them. */ +function answers(...statuses: Array) { + const asked: string[] = [] + const fetch = async (url: string) => { + asked.push(url) + const next = statuses.length > 1 ? statuses.shift()! : statuses[0] + if (next instanceof Error) throw next + return { ok: next >= 200 && next < 300, status: next, json: async () => ({}) } + } + return { fetch, asked } +} + +const version = async (folder: string) => JSON.parse(await readFile(resolve(import.meta.dirname, '../..', folder, 'package.json'), 'utf8')).version as string + +describe('waiting for npm to serve a version', () => { + it('asks again after a 404, a server error or a failed request, until it is served', async () => { + const { fetch, asked } = answers(404, 503, new TypeError('fetch failed'), 200) + const response = await served('https://registry.npmjs.org/x/1.0.0', 'x@1.0.0', { minutes: 1, poll: 1, fetch }) + expect(response.ok).toBe(true) + expect(asked).toHaveLength(4) + }) + + it('asks once with no wait, as by hand and in a dry run, and takes any other refusal as the answer', async () => { + const once = answers(404, 200) + expect((await served('u', 'x', { minutes: 0, poll: 1, fetch: once.fetch })).status).toBe(404) + expect(once.asked).toHaveLength(1) + const refused = answers(403, 200) + expect((await served('u', 'x', { minutes: 1, poll: 1, fetch: refused.fetch })).status).toBe(403) + expect(refused.asked).toHaveLength(1) + }) + + it('cuts a request that gets no answer, and asks again within the wait', async () => { + let calls = 0 + const stalling = (_url: string, init: { signal: AbortSignal }) => { + calls++ + if (calls > 1) return Promise.resolve({ ok: true, status: 200, json: async () => ({}) }) + return new Promise((_, reject) => init.signal.addEventListener('abort', () => reject(init.signal.reason))) + } + expect((await served('u', 'x', { minutes: 0, poll: 1, timeout: 10, fetch: stalling })).status).toBe('no answer in time') + calls = 0 + expect((await served('u', 'x', { minutes: 1, poll: 1, timeout: 10, fetch: stalling })).ok).toBe(true) + expect(calls).toBe(2) + }) + + it('waits as long as H5P_PUBLISH_WAIT says, and no longer', async () => { + vi.stubEnv('H5P_PUBLISH_WAIT', '1') + const late = answers(404, 404, 200) + expect((await served('u', 'x', { poll: 1, fetch: late.fetch })).ok).toBe(true) + // The last answer once the wait has run out: the poll is longer than the minute left. + const never = answers(404) + expect((await served('u', 'x', { poll: 61_000, fetch: never.fetch })).status).toBe(404) + expect(never.asked).toHaveLength(1) + vi.unstubAllEnvs() + }) +}) + +describe('the dependencies a package names by workspace:', () => { + it('are each asked for at the version the workspace has, and only those', async () => { + const packages = join(dir, 'packages') + const write = async (folder: string, manifest: object) => { + await mkdir(join(packages, folder), { recursive: true }) + await writeFile(join(packages, folder, 'package.json'), JSON.stringify(manifest)) + } + await write('a', { + name: '@scope/a', + version: '1.0.0', + dependencies: { '@scope/b': 'workspace:^', zod: '^4.0.0' }, + peerDependencies: { '@scope/c': 'workspace:*' }, + devDependencies: { '@scope/d': 'workspace:*' } + }) + await write('b', { name: '@scope/b', version: '2.3.4' }) + await write('c', { name: '@scope/c', version: '0.5.0' }) + await write('d', { name: '@scope/d', version: '9.9.9' }) + const { fetch, asked } = answers(200, 404) + expect(await dependenciesServed(join(packages, 'a'), { minutes: 0, fetch })).toEqual([ + { name: '@scope/c', version: '0.5.0', status: 200, ok: true }, + { name: '@scope/b', version: '2.3.4', status: 404, ok: false } + ]) + expect(asked).toEqual(['https://registry.npmjs.org/@scope%2fc/0.5.0', 'https://registry.npmjs.org/@scope%2fb/2.3.4']) + }) + + it('for the MCP server, are the author and the verifier at their versions here', async () => { + const { fetch, asked } = answers(200) + await dependenciesServed(resolve(import.meta.dirname, '../../mcp'), { minutes: 0, fetch }) + expect(asked).toEqual([ + `https://registry.npmjs.org/@missing-elements%2fh5p-author/${await version('author')}`, + `https://registry.npmjs.org/@missing-elements%2fh5p-verify/${await version('verify')}` + ]) + }) +}) diff --git a/packages/embed/package.json b/packages/embed/package.json index d6f61dff..b9a29814 100644 --- a/packages/embed/package.json +++ b/packages/embed/package.json @@ -75,7 +75,7 @@ "test": "pnpm --filter @missing-elements/h5p-offline-player build && pnpm build && vitest run", "typecheck": "pnpm types && tsc -p tsconfig.json", "prepack": "node -e \"require('node:fs').copyFileSync('../../LICENSE', 'LICENSE')\" && pnpm types && pnpm build", - "prepublishOnly": "node -e \"if (!/^pnpm\\//.test(process.env.npm_config_user_agent || '')) { console.error('Publish with pnpm publish: npm would ship the workspace: dependency ranges as they are, and the package would not install.'); process.exit(1) }\" && node scripts/check-published.mjs && pnpm typecheck && pnpm test", + "prepublishOnly": "node -e \"if (!/^pnpm\\//.test(process.env.npm_config_user_agent || '')) { console.error('Publish with pnpm publish: npm would ship the workspace: dependency ranges as they are, and the package would not install.'); process.exit(1) }\" && node ../../scripts/published.mjs && node scripts/check-published.mjs && pnpm typecheck && pnpm test", "types": "pnpm --filter @missing-elements/h5p-offline-player exec tsc -b && node -e \"require('node:fs').rmSync('types', { recursive: true, force: true })\" && tsc -p tsconfig.types.json" }, "dependencies": { diff --git a/packages/embed/scripts/check-published.mjs b/packages/embed/scripts/check-published.mjs index 739ae261..f60ca72d 100644 --- a/packages/embed/scripts/check-published.mjs +++ b/packages/embed/scripts/check-published.mjs @@ -9,6 +9,7 @@ */ import { readFile } from 'node:fs/promises' import { resolve } from 'node:path' +import { served } from '../../../scripts/published.mjs' /** * @param {string} folder the package's folder under packages/ @@ -18,7 +19,8 @@ import { resolve } from 'node:path' */ async function published(folder, name, subpath, why) { const { version } = JSON.parse(await readFile(resolve(import.meta.dirname, '..', '..', folder, 'package.json'), 'utf8')) - const response = await fetch(`https://registry.npmjs.org/${name.replaceAll('/', '%2f')}/${version}`) + // Waited for in CI, where it was published moments before in the same run (#253). + const response = await served(`https://registry.npmjs.org/${name.replaceAll('/', '%2f')}/${version}`, `${name}@${version}`) if (!response.ok) { console.error(`[${folder}] ${name}@${version} is not on npm (${response.status}). Publish it from packages/${folder} first: the embed package depends on it, for its ${subpath}.`) process.exit(1) diff --git a/packages/mcp/package.json b/packages/mcp/package.json index e92b0029..69c10073 100644 --- a/packages/mcp/package.json +++ b/packages/mcp/package.json @@ -39,7 +39,7 @@ "test": "pnpm --filter @missing-elements/h5p-offline-player build && pnpm --filter @missing-elements/h5p-author exec node scripts/copy-skill.mjs && vitest run", "typecheck": "tsc -p tsconfig.json", "prepack": "node -e \"require('node:fs').copyFileSync('../../LICENSE', 'LICENSE')\"", - "prepublishOnly": "node -e \"if (!/^pnpm\\//.test(process.env.npm_config_user_agent || '')) { console.error('Publish with pnpm publish: npm would ship the workspace:^ dependency ranges as they are, and the package would not install.'); process.exit(1) }\" && pnpm typecheck && pnpm test" + "prepublishOnly": "node -e \"if (!/^pnpm\\//.test(process.env.npm_config_user_agent || '')) { console.error('Publish with pnpm publish: npm would ship the workspace:^ dependency ranges as they are, and the package would not install.'); process.exit(1) }\" && node ../../scripts/published.mjs && pnpm typecheck && pnpm test" }, "dependencies": { "@missing-elements/h5p-author": "workspace:^", diff --git a/packages/player/scripts/check-library-pack.mjs b/packages/player/scripts/check-library-pack.mjs index 8049575e..c00d4879 100644 --- a/packages/player/scripts/check-library-pack.mjs +++ b/packages/player/scripts/check-library-pack.mjs @@ -8,6 +8,7 @@ */ import { readFile } from 'node:fs/promises' import { resolve } from 'node:path' +import { served } from '../../../scripts/published.mjs' const constants = await readFile(resolve(import.meta.dirname, '..', 'src', 'shared', 'constants.ts'), 'utf8') const version = /export const LIBRARY_PACK_VERSION = '([^']+)'/.exec(constants)?.[1] @@ -16,7 +17,8 @@ if (!version) { process.exit(1) } -const response = await fetch(`https://registry.npmjs.org/@missing-elements%2fh5p-libraries/${version}`) +// Waited for in CI, where the pack was published moments before in the same run (#253). +const response = await served(`https://registry.npmjs.org/@missing-elements%2fh5p-libraries/${version}`, `@missing-elements/h5p-libraries@${version}`) if (!response.ok) { console.error( `[library-pack] @missing-elements/h5p-libraries@${version} is not on npm (${response.status}). ` + @@ -26,7 +28,7 @@ if (!response.ok) { } // From jsDelivr, where `pack` fetches the parts from. -const index = await fetch(`https://cdn.jsdelivr.net/npm/@missing-elements/h5p-libraries@${version}/parts/index.json`) +const index = await served(`https://cdn.jsdelivr.net/npm/@missing-elements/h5p-libraries@${version}/parts/index.json`, `h5p-libraries@${version}/parts/index.json on jsDelivr`) const local = await readFile(resolve(import.meta.dirname, '..', '..', 'libraries', 'parts', 'index.json'), 'utf8') const same = index.ok && JSON.stringify((await index.json()).libraries) === JSON.stringify(JSON.parse(local).libraries) if (!same) { diff --git a/packages/verify/package.json b/packages/verify/package.json index 9719ade8..960cf910 100644 --- a/packages/verify/package.json +++ b/packages/verify/package.json @@ -45,7 +45,7 @@ "test": "pnpm --filter @missing-elements/h5p-offline-player build && vitest run", "typecheck": "pnpm types && tsc -p tsconfig.json", "prepack": "node -e \"require('node:fs').copyFileSync('../../LICENSE', 'LICENSE')\" && pnpm types", - "prepublishOnly": "node -e \"if (!/^pnpm\\//.test(process.env.npm_config_user_agent || '')) { console.error('Publish with pnpm publish: npm would ship the workspace:^ dependency ranges as they are, and the package would not install.'); process.exit(1) }\" && pnpm typecheck && pnpm test", + "prepublishOnly": "node -e \"if (!/^pnpm\\//.test(process.env.npm_config_user_agent || '')) { console.error('Publish with pnpm publish: npm would ship the workspace:^ dependency ranges as they are, and the package would not install.'); process.exit(1) }\" && node ../../scripts/published.mjs && pnpm typecheck && pnpm test", "types": "node -e \"require('node:fs').rmSync('types', { recursive: true, force: true })\" && tsc -p tsconfig.types.json" }, "dependencies": { diff --git a/scripts/published.mjs b/scripts/published.mjs new file mode 100644 index 00000000..6faa0567 --- /dev/null +++ b/scripts/published.mjs @@ -0,0 +1,107 @@ +import { readdir, readFile } from 'node:fs/promises' +import { join, resolve } from 'node:path' +import { pathToFileURL } from 'node:url' + +/** + * Whether npm, and jsDelivr behind it, serve the versions a package of this repository is about to + * be published against (#253). npm takes a version in, then processes it before it serves it: in + * release 1.7.0 (2026-11-19) that took 1 to 4 minutes for most packages and 14 for the 31 MB + * library pack, and a package published in that time names a version nobody can install yet. + * `publish.yml` publishes a release in dependency order in one run and sets `H5P_PUBLISH_WAIT`, in + * minutes: there a 404 means the version is still being processed, and the check waits for it. By + * hand it is 0: a 404 means the dependency was not published, and the check says so at once. + * + * `node ../../scripts/published.mjs`, in a package's `prepublishOnly`, checks every dependency the + * package names by `workspace:`, at the version the workspace has. + */ + +/** How often a check asks again while it waits. */ +const POLL = 30_000 + +/** + * How long one request may take. Node's fetch has no limit of its own beyond undici's five minutes + * for the headers, and one request to npm hung that long while this was being written. + */ +const REQUEST = 30_000 + +/** @typedef {{ ok: boolean, status: number | string, json(): Promise, body?: { cancel(): Promise } | null }} Served */ + +/** + * The response for `url` once it is not a 404, a server error or a failed request, or the last + * one when the wait runs out. + * @param {string} url + * @param {string} label what is waited for, as the messages name it + * @param {{ minutes?: number, poll?: number, timeout?: number, fetch?: (url: string, init: { signal: AbortSignal }) => Promise }} [options] + * `minutes`, by default `H5P_PUBLISH_WAIT`, else 0; `timeout`, a request's, by default 30 s + * @returns {Promise} + */ +export async function served(url, label, options = {}) { + const minutes = options.minutes ?? (Number(process.env.H5P_PUBLISH_WAIT) || 0) + const poll = options.poll ?? POLL + const get = options.fetch ?? fetch + const started = Date.now() + const deadline = started + minutes * 60_000 + for (let attempt = 0; ; attempt++) { + /** @type {Served} */ + const response = await get(url, { signal: AbortSignal.timeout(options.timeout ?? REQUEST) }).catch((error) => ({ + ok: false, + status: String(error?.cause?.code ?? (error?.name === 'TimeoutError' ? 'no answer in time' : error?.message) ?? error), + json: async () => undefined + })) + const retry = !response.ok && (typeof response.status !== 'number' || response.status === 404 || response.status >= 500) + if (!retry || Date.now() + poll > deadline) return response + await response.body?.cancel().catch(() => {}) + if (attempt % 10 === 0) { + const waited = Math.round((Date.now() - started) / 60_000) + console.log(`[published] ${label}: ${response.status}${waited ? ` after ${waited} min` : ''}; waiting up to ${minutes} min for npm to serve it`) + } + await new Promise((done) => setTimeout(done, poll)) + } +} + +/** + * The dependencies the package in `folder` names by `workspace:` (in `dependencies`, + * `optionalDependencies` and `peerDependencies`, not `devDependencies`, which nobody installing it + * gets), each at the version the workspace has, and whether npm serves it. + * @param {string} folder a package's folder, beside the others under packages/ + * @param {Parameters[2]} [options] + * @returns {Promise<{ name: string, version: string, status: number | string, ok: boolean }[]>} + */ +export async function dependenciesServed(folder, options) { + const manifest = JSON.parse(await readFile(join(folder, 'package.json'), 'utf8')) + const named = Object.entries({ ...manifest.peerDependencies, ...manifest.optionalDependencies, ...manifest.dependencies }) + .filter(([, range]) => String(range).startsWith('workspace:')) + .map(([name]) => name) + if (!named.length) return [] + /** @type {Map} */ + const versions = new Map() + const packages = resolve(folder, '..') + for (const entry of await readdir(packages, { withFileTypes: true })) { + if (!entry.isDirectory()) continue + const other = await readFile(join(packages, entry.name, 'package.json'), 'utf8').then(JSON.parse, () => null) + if (other?.name) versions.set(other.name, other.version) + } + const results = [] + for (const name of named) { + const version = versions.get(name) + if (!version) throw new Error(`${name} is named by workspace: in ${join(folder, 'package.json')}, but no package beside it has that name`) + const response = await served(`https://registry.npmjs.org/${name.replace('/', '%2f')}/${version}`, `${name}@${version}`, options) + // Only the status is read: the body is let go, so its connection does not keep the process up. + await response.body?.cancel().catch(() => {}) + results.push({ name, version, status: response.status, ok: response.ok }) + } + return results +} + +if (import.meta.url === pathToFileURL(process.argv[1] ?? '').href) { + const folder = process.cwd() + const results = await dependenciesServed(folder) + const missing = results.filter((result) => !result.ok) + for (const { name, version } of results.filter((result) => result.ok)) console.log(`[published] ${name}@${version} is on npm`) + if (missing.length) { + for (const { name, version, status } of missing) { + console.error(`[published] ${name}@${version} is not on npm (${status}). Publish it first: ${JSON.parse(await readFile(join(folder, 'package.json'), 'utf8')).name} depends on it, at that version.`) + } + process.exit(1) + } +} From 83222feb42eabab0dddd92253e1f2a901dd23114 Mon Sep 17 00:00:00 2001 From: Aleksandr Shevchuk Date: Fri, 20 Nov 2026 09:22:24 +0200 Subject: [PATCH 2/3] ci: publish checks share one wait, and tell a failed request from a missing version (#253) From the review of #254: - served() reads a success's JSON body inside its retry loop, so a body cut or stalled after the headers is asked again instead of thrown; - waitUntil() takes H5P_PUBLISH_WAIT once per script and every check in it shares that wait, which was each request's; - stopUnlessAnswered() stops a check on any failure but a 404 with "could not be checked", where a DNS failure or a timeout said "not on npm, publish it first"; - the author's pack check says when jsDelivr has no parts/index.json, as the player's does; - the npm URL escapes every "/" (CodeQL); - the docs name only non-dev workspace dependencies, say a 404 by hand may be a version still being processed, and give the trusted-publishing paragraph back its last two sentences, which the new one had split off. --- .github/workflows/publish.yml | 9 +- design/build.md | 20 ++-- design/layout.md | 4 +- .../author/scripts/check-pack-published.mjs | 23 ++-- packages/author/tests/published.test.ts | 56 ++++++++- packages/embed/scripts/check-published.mjs | 9 +- .../player/scripts/check-library-pack.mjs | 11 +- scripts/published.mjs | 110 +++++++++++++----- 8 files changed, 179 insertions(+), 63 deletions(-) diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 59bffac5..776c3384 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -63,10 +63,11 @@ jobs: # --workspace-concurrency: given it, pnpm stopped on the unknown option before publishing # anything (the first run). Without git checks: prepack writes files into the tree (types/, # the licence, the skill), and the environment already holds the run to main. - # Each package's check that what it depends on is on npm waits up to H5P_PUBLISH_WAIT - # minutes for npm to serve a version published earlier in this run: npm processes a new one - # first, 1 to 4 minutes for most packages and 14 for the 31 MB library pack in release - # 1.7.0 (#253). A dry run publishes nothing, so there it fails at once, as by hand. + # Each package's checks that what it depends on is on npm wait up to H5P_PUBLISH_WAIT + # minutes, one wait per script, for npm to serve a version published earlier in this run: + # npm processes a new one first, 1 to 4 minutes for most packages and 14 for the 31 MB + # library pack in release 1.7.0 (#253). A dry run publishes nothing, so there it fails at + # once, as by hand. - run: pnpm -r publish --access public --no-git-checks $DRY_RUN env: NPM_CONFIG_PROVENANCE: true diff --git a/design/build.md b/design/build.md index ab859047..431d36df 100644 --- a/design/build.md +++ b/design/build.md @@ -147,22 +147,26 @@ the commit its tarball came from. Each package's trusted publisher on npmjs.com the environment, and ticks `npm publish` under Allowed actions: one created after 2026-09-03 allows only `npm stage publish` until it is ticked, and the registry refuses a publish with "403 OIDC permission denied for this action" after the token exchange has worked (the second run, on -2026-11-19). A new one expires unless a publish through it succeeds within 2 days. +2026-11-19). A new one expires unless a publish through it succeeds within 2 days. pnpm publishes +through npm, which takes that token from 11.5.1, so the job runs Node 24. A publish by hand, with a +one-time password, still works and records no provenance. **A package is published only once npm serves what it depends on** (#253). npm takes a version in, then processes it before it serves it. In release 1.7.0 that took 1 to 4 minutes for most packages and 14 for the 31 MB library pack. The release took four runs, since each check that a dependency is on npm failed on one published moments before. Two kinds of check now wait while npm processes: -- `scripts/published.mjs`, in the `prepublishOnly` of every package that names another by - `workspace:` (verify, embed, author and mcp), checks each at the version the workspace has; +- `scripts/published.mjs`, in the `prepublishOnly` of every published package that names another + by `workspace:` outside `devDependencies` (verify, embed, author and mcp), checks each at the + version the workspace has; - the three checks of what a package pins, the player's pack and embed's and the author's, use its `served()`. -Both ask again every 30 s for up to `H5P_PUBLISH_WAIT` minutes, which `publish.yml` sets to 30, -twice the longest seen. By hand and in a dry run it is 0, where a 404 means the dependency was not -published, and the check says so at once. Each request is cut at 30 s: one to npm hung for over 5 -minutes. pnpm publishes through npm, which takes that token from 11.5.1, so the job runs -Node 24. A publish by hand, with a one-time password, still works and records no provenance. +Both ask again every 30 s after a 404, a server error, or a request that failed, timed out or +could not be read. The checks in one script share one wait of `H5P_PUBLISH_WAIT` minutes, which +`publish.yml` sets to 30, twice the longest seen. By hand and in a dry run it is 0: a 404 means npm +does not serve the dependency, not published or still being processed, and the check says so at +once. Any other failure stops the check, which says it could not tell rather than to publish. Each +request, its body included, is cut at 30 s: one to npm hung for over 5 minutes. A publish of the player runs its `prepublishOnly` (a check that the pack it names is on npm, typecheck, the unit suite and the build) so a broken tree cannot ship; the browser suite is left out because it needs an installed diff --git a/design/layout.md b/design/layout.md index 689f732d..8cb5e1e2 100644 --- a/design/layout.md +++ b/design/layout.md @@ -48,8 +48,8 @@ apps/skills/ the skills' test project: author-verify, author-playback, front-matter skills/ the agent skills, at the root, where `npx skills add` looks docs/ the README's longer sections, for integrators -scripts/ published.mjs, the check before a package is published that npm serves what - it depends on (design/build.md) +scripts/ published.mjs, the check before a package is published that npm serves the + workspace packages it names outside devDependencies (design/build.md) api/, vercel.json the hosted demo's Vercel function and deployment; at the root because that is the Vercel project's root, which only the dashboard can move ``` diff --git a/packages/author/scripts/check-pack-published.mjs b/packages/author/scripts/check-pack-published.mjs index 2bff0ef8..46dc6184 100644 --- a/packages/author/scripts/check-pack-published.mjs +++ b/packages/author/scripts/check-pack-published.mjs @@ -11,30 +11,38 @@ */ import { readFile } from 'node:fs/promises' import { resolve } from 'node:path' -import { served } from '../../../scripts/published.mjs' +import { served, stopUnlessAnswered, waitUntil } from '../../../scripts/published.mjs' const packages = resolve(import.meta.dirname, '..', '..') /** @param {string} directory */ const versionOf = async (directory) => JSON.parse(await readFile(resolve(packages, directory, 'package.json'), 'utf8')).version -// Each waited for in CI, where it was published moments before in the same run (#253). +// One wait for all four, in CI, where each was published moments before in the same run (#253). +const until = waitUntil() + const version = await versionOf('libraries') -const response = await served(`https://registry.npmjs.org/@missing-elements%2fh5p-libraries/${version}`, `@missing-elements/h5p-libraries@${version}`) +const response = await served(`https://registry.npmjs.org/@missing-elements%2fh5p-libraries/${version}`, `@missing-elements/h5p-libraries@${version}`, { until }) +stopUnlessAnswered(response, `@missing-elements/h5p-libraries@${version} on npm`) if (!response.ok) { console.error(`[library-pack] @missing-elements/h5p-libraries@${version} is not on npm (${response.status}). Publish it from packages/libraries first: the author package pins it.`) process.exit(1) } -const index = await served(`https://cdn.jsdelivr.net/npm/@missing-elements/h5p-libraries@${version}/parts/index.json`, `h5p-libraries@${version}/parts/index.json on jsDelivr`) +const index = await served(`https://cdn.jsdelivr.net/npm/@missing-elements/h5p-libraries@${version}/parts/index.json`, `h5p-libraries@${version}/parts/index.json on jsDelivr`, { until }) +stopUnlessAnswered(index, `h5p-libraries@${version}/parts/index.json on jsDelivr`) const local = await readFile(resolve(packages, 'libraries', 'parts', 'index.json'), 'utf8') const same = index.ok && JSON.stringify((await index.json()).libraries) === JSON.stringify(JSON.parse(local).libraries) if (!same) { - console.error(`[library-pack] @missing-elements/h5p-libraries@${version} on npm has another parts/index.json than this checkout: publish the pack from this checkout, under a new version.`) + console.error( + `[library-pack] @missing-elements/h5p-libraries@${version} on jsDelivr ${index.ok ? 'has another parts/index.json than' : 'has no parts/index.json, unlike'} this checkout: ` + + 'publish the pack from this checkout, under a new version.' + ) process.exit(1) } console.log(`[library-pack] @missing-elements/h5p-libraries@${version} is published, with this checkout's index`) const player = await versionOf('player') -const published = await served(`https://registry.npmjs.org/@missing-elements%2fh5p-offline-player/${player}`, `@missing-elements/h5p-offline-player@${player}`) +const published = await served(`https://registry.npmjs.org/@missing-elements%2fh5p-offline-player/${player}`, `@missing-elements/h5p-offline-player@${player}`, { until }) +stopUnlessAnswered(published, `@missing-elements/h5p-offline-player@${player} on npm`) if (!published.ok) { console.error(`[player] @missing-elements/h5p-offline-player@${player} is not on npm (${published.status}). Publish it from packages/player first: the author package pins it, for its ./plan.`) process.exit(1) @@ -46,7 +54,8 @@ if (!(await published.json()).exports?.['./plan']) { console.log(`[player] @missing-elements/h5p-offline-player@${player} is published, and exports ./plan`) const normalize = await versionOf('normalize') -const normalizer = await served(`https://registry.npmjs.org/@missing-elements%2fh5p-normalize/${normalize}`, `@missing-elements/h5p-normalize@${normalize}`) +const normalizer = await served(`https://registry.npmjs.org/@missing-elements%2fh5p-normalize/${normalize}`, `@missing-elements/h5p-normalize@${normalize}`, { until }) +stopUnlessAnswered(normalizer, `@missing-elements/h5p-normalize@${normalize} on npm`) if (!normalizer.ok) { console.error(`[normalize] @missing-elements/h5p-normalize@${normalize} is not on npm (${normalizer.status}). Publish it from packages/normalize first: the author package depends on it, for its ./crc32.`) process.exit(1) diff --git a/packages/author/tests/published.test.ts b/packages/author/tests/published.test.ts index 7503f779..af8702ba 100644 --- a/packages/author/tests/published.test.ts +++ b/packages/author/tests/published.test.ts @@ -2,7 +2,7 @@ import { mkdir, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises' import { tmpdir } from 'node:os' import { join, resolve } from 'node:path' import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest' -import { dependenciesServed, served } from '../../../scripts/published.mjs' +import { dependenciesServed, served, stopUnlessAnswered } from '../../../scripts/published.mjs' /* * The check, before a package is published, that npm serves what it depends on (#253): in CI it @@ -64,6 +64,20 @@ describe('waiting for npm to serve a version', () => { expect(calls).toBe(2) }) + it('reads the body of a success, and asks again when it cannot be read', async () => { + let calls = 0 + const cut = async () => { + calls++ + const body = calls > 1 ? { exports: { './plan': './plan.js' } } : undefined + return { ok: true, status: 200, json: async () => body ?? Promise.reject(new TypeError('terminated')) } + } + expect(await served('u', 'x', { minutes: 0, poll: 1, fetch: cut })).toMatchObject({ ok: false, status: 'terminated' }) + calls = 0 + const response = await served('u', 'x', { minutes: 1, poll: 1, fetch: cut }) + expect(await response.json()).toEqual({ exports: { './plan': './plan.js' } }) + expect(calls).toBe(2) + }) + it('waits as long as H5P_PUBLISH_WAIT says, and no longer', async () => { vi.stubEnv('H5P_PUBLISH_WAIT', '1') const late = answers(404, 404, 200) @@ -76,9 +90,34 @@ describe('waiting for npm to serve a version', () => { }) }) +describe('a check whose request failed', () => { + it('goes on after a success or a 404, and stops on anything else, which says nothing about the version', () => { + const exit = vi.spyOn(process, 'exit').mockImplementation(((code?: number) => { + throw new Error(`exit ${code}`) + }) as never) + const error = vi.spyOn(console, 'error').mockImplementation(() => {}) + const response = (ok: boolean, status: number | string) => ({ ok, status, json: async () => undefined }) + stopUnlessAnswered(response(true, 200), 'x@1.0.0 on npm') + stopUnlessAnswered(response(false, 404), 'x@1.0.0 on npm') + for (const status of [403, 503, 'ENOTFOUND', 'no answer in time']) { + expect(() => stopUnlessAnswered(response(false, status), 'x@1.0.0 on npm')).toThrow('exit 1') + } + expect(error.mock.calls.map(([line]) => line)).toEqual([ + expect.stringContaining('x@1.0.0 on npm could not be checked (403)'), + expect.stringContaining('(503)'), + expect.stringContaining('(ENOTFOUND)'), + expect.stringContaining('(no answer in time)') + ]) + expect(error.mock.calls[0][0]).not.toMatch(/publish it/i) + exit.mockRestore() + error.mockRestore() + }) +}) + describe('the dependencies a package names by workspace:', () => { - it('are each asked for at the version the workspace has, and only those', async () => { - const packages = join(dir, 'packages') + let packages: string + beforeAll(async () => { + packages = join(dir, 'packages') const write = async (folder: string, manifest: object) => { await mkdir(join(packages, folder), { recursive: true }) await writeFile(join(packages, folder, 'package.json'), JSON.stringify(manifest)) @@ -93,6 +132,9 @@ describe('the dependencies a package names by workspace:', () => { await write('b', { name: '@scope/b', version: '2.3.4' }) await write('c', { name: '@scope/c', version: '0.5.0' }) await write('d', { name: '@scope/d', version: '9.9.9' }) + }) + + it('are each asked for at the version the workspace has, and only those', async () => { const { fetch, asked } = answers(200, 404) expect(await dependenciesServed(join(packages, 'a'), { minutes: 0, fetch })).toEqual([ { name: '@scope/c', version: '0.5.0', status: 200, ok: true }, @@ -101,6 +143,14 @@ describe('the dependencies a package names by workspace:', () => { expect(asked).toEqual(['https://registry.npmjs.org/@scope%2fc/0.5.0', 'https://registry.npmjs.org/@scope%2fb/2.3.4']) }) + it('share one wait: one that is missing leaves the next none of its own', async () => { + const { fetch, asked } = answers(404) + const results = await dependenciesServed(join(packages, 'a'), { until: Date.now() + 30, poll: 1, fetch }) + expect(results.map(({ ok }) => ok)).toEqual([false, false]) + expect(asked.filter((url) => url.includes('%2fc/')).length).toBeGreaterThan(1) + expect(asked.filter((url) => url.includes('%2fb/'))).toHaveLength(1) + }) + it('for the MCP server, are the author and the verifier at their versions here', async () => { const { fetch, asked } = answers(200) await dependenciesServed(resolve(import.meta.dirname, '../../mcp'), { minutes: 0, fetch }) diff --git a/packages/embed/scripts/check-published.mjs b/packages/embed/scripts/check-published.mjs index f60ca72d..e1ba2dc5 100644 --- a/packages/embed/scripts/check-published.mjs +++ b/packages/embed/scripts/check-published.mjs @@ -9,7 +9,10 @@ */ import { readFile } from 'node:fs/promises' import { resolve } from 'node:path' -import { served } from '../../../scripts/published.mjs' +import { served, stopUnlessAnswered, waitUntil } from '../../../scripts/published.mjs' + +// One wait for both, in CI, where each was published moments before in the same run (#253). +const until = waitUntil() /** * @param {string} folder the package's folder under packages/ @@ -19,8 +22,8 @@ import { served } from '../../../scripts/published.mjs' */ async function published(folder, name, subpath, why) { const { version } = JSON.parse(await readFile(resolve(import.meta.dirname, '..', '..', folder, 'package.json'), 'utf8')) - // Waited for in CI, where it was published moments before in the same run (#253). - const response = await served(`https://registry.npmjs.org/${name.replaceAll('/', '%2f')}/${version}`, `${name}@${version}`) + const response = await served(`https://registry.npmjs.org/${name.replaceAll('/', '%2f')}/${version}`, `${name}@${version}`, { until }) + stopUnlessAnswered(response, `${name}@${version} on npm`) if (!response.ok) { console.error(`[${folder}] ${name}@${version} is not on npm (${response.status}). Publish it from packages/${folder} first: the embed package depends on it, for its ${subpath}.`) process.exit(1) diff --git a/packages/player/scripts/check-library-pack.mjs b/packages/player/scripts/check-library-pack.mjs index c00d4879..09b06e89 100644 --- a/packages/player/scripts/check-library-pack.mjs +++ b/packages/player/scripts/check-library-pack.mjs @@ -8,7 +8,7 @@ */ import { readFile } from 'node:fs/promises' import { resolve } from 'node:path' -import { served } from '../../../scripts/published.mjs' +import { served, stopUnlessAnswered, waitUntil } from '../../../scripts/published.mjs' const constants = await readFile(resolve(import.meta.dirname, '..', 'src', 'shared', 'constants.ts'), 'utf8') const version = /export const LIBRARY_PACK_VERSION = '([^']+)'/.exec(constants)?.[1] @@ -17,8 +17,10 @@ if (!version) { process.exit(1) } -// Waited for in CI, where the pack was published moments before in the same run (#253). -const response = await served(`https://registry.npmjs.org/@missing-elements%2fh5p-libraries/${version}`, `@missing-elements/h5p-libraries@${version}`) +// One wait for both, in CI, where the pack was published moments before in the same run (#253). +const until = waitUntil() +const response = await served(`https://registry.npmjs.org/@missing-elements%2fh5p-libraries/${version}`, `@missing-elements/h5p-libraries@${version}`, { until }) +stopUnlessAnswered(response, `@missing-elements/h5p-libraries@${version} on npm`) if (!response.ok) { console.error( `[library-pack] @missing-elements/h5p-libraries@${version} is not on npm (${response.status}). ` + @@ -28,7 +30,8 @@ if (!response.ok) { } // From jsDelivr, where `pack` fetches the parts from. -const index = await served(`https://cdn.jsdelivr.net/npm/@missing-elements/h5p-libraries@${version}/parts/index.json`, `h5p-libraries@${version}/parts/index.json on jsDelivr`) +const index = await served(`https://cdn.jsdelivr.net/npm/@missing-elements/h5p-libraries@${version}/parts/index.json`, `h5p-libraries@${version}/parts/index.json on jsDelivr`, { until }) +stopUnlessAnswered(index, `h5p-libraries@${version}/parts/index.json on jsDelivr`) const local = await readFile(resolve(import.meta.dirname, '..', '..', 'libraries', 'parts', 'index.json'), 'utf8') const same = index.ok && JSON.stringify((await index.json()).libraries) === JSON.stringify(JSON.parse(local).libraries) if (!same) { diff --git a/scripts/published.mjs b/scripts/published.mjs index 6faa0567..e1894012 100644 --- a/scripts/published.mjs +++ b/scripts/published.mjs @@ -9,65 +9,113 @@ import { pathToFileURL } from 'node:url' * library pack, and a package published in that time names a version nobody can install yet. * `publish.yml` publishes a release in dependency order in one run and sets `H5P_PUBLISH_WAIT`, in * minutes: there a 404 means the version is still being processed, and the check waits for it. By - * hand it is 0: a 404 means the dependency was not published, and the check says so at once. + * hand it is 0: a 404 means npm does not serve it, not published or still being processed, and the + * check says so at once. * - * `node ../../scripts/published.mjs`, in a package's `prepublishOnly`, checks every dependency the - * package names by `workspace:`, at the version the workspace has. + * `node ../../scripts/published.mjs`, in a package's `prepublishOnly`, checks every package of this + * repository it names by `workspace:` outside `devDependencies`, at the version the workspace has. */ /** How often a check asks again while it waits. */ const POLL = 30_000 /** - * How long one request may take. Node's fetch has no limit of its own beyond undici's five minutes - * for the headers, and one request to npm hung that long while this was being written. + * How long one request may take, its body included. Node's fetch has no limit of its own beyond + * undici's five minutes for the headers, and one request to npm hung that long while this was + * being written. */ const REQUEST = 30_000 -/** @typedef {{ ok: boolean, status: number | string, json(): Promise, body?: { cancel(): Promise } | null }} Served */ +/** @typedef {{ ok: boolean, status: number, json(): Promise, body?: { cancel(): Promise } | null }} Answer what a request gives */ +/** @typedef {{ ok: boolean, status: number | string, json(): Promise }} Served a response, its body read */ /** - * The response for `url` once it is not a 404, a server error or a failed request, or the last - * one when the wait runs out. + * When the checks that begin now stop waiting. A script takes it once for all its checks, so it + * waits `H5P_PUBLISH_WAIT` minutes at most however many versions it asks for. + * @param {number} [minutes] by default `H5P_PUBLISH_WAIT`, else 0 + */ +export const waitUntil = (minutes = Number(process.env.H5P_PUBLISH_WAIT) || 0) => Date.now() + minutes * 60_000 + +/** + * The response for `url`, its JSON body read, once it is not a 404, a server error or a request + * that failed, timed out or could not be read, or the last one when the wait runs out. * @param {string} url * @param {string} label what is waited for, as the messages name it - * @param {{ minutes?: number, poll?: number, timeout?: number, fetch?: (url: string, init: { signal: AbortSignal }) => Promise }} [options] - * `minutes`, by default `H5P_PUBLISH_WAIT`, else 0; `timeout`, a request's, by default 30 s + * @param {{ until?: number, minutes?: number, poll?: number, timeout?: number, fetch?: (url: string, init: { signal: AbortSignal }) => Promise }} [options] + * `until`, when the wait runs out, by default `waitUntil(minutes)`; `timeout`, a request's, by default 30 s * @returns {Promise} */ export async function served(url, label, options = {}) { - const minutes = options.minutes ?? (Number(process.env.H5P_PUBLISH_WAIT) || 0) const poll = options.poll ?? POLL const get = options.fetch ?? fetch const started = Date.now() - const deadline = started + minutes * 60_000 + const deadline = options.until ?? waitUntil(options.minutes) for (let attempt = 0; ; attempt++) { - /** @type {Served} */ - const response = await get(url, { signal: AbortSignal.timeout(options.timeout ?? REQUEST) }).catch((error) => ({ - ok: false, - status: String(error?.cause?.code ?? (error?.name === 'TimeoutError' ? 'no answer in time' : error?.message) ?? error), - json: async () => undefined - })) + const response = await get(url, { signal: AbortSignal.timeout(options.timeout ?? REQUEST) }).then(read).catch(failed) const retry = !response.ok && (typeof response.status !== 'number' || response.status === 404 || response.status >= 500) if (!retry || Date.now() + poll > deadline) return response - await response.body?.cancel().catch(() => {}) if (attempt % 10 === 0) { const waited = Math.round((Date.now() - started) / 60_000) - console.log(`[published] ${label}: ${response.status}${waited ? ` after ${waited} min` : ''}; waiting up to ${minutes} min for npm to serve it`) + const left = Math.ceil((deadline - Date.now()) / 60_000) + console.log(`[published] ${label}: ${response.status}${waited ? ` after ${waited} min` : ''}; waiting up to ${left} min more for it to be served`) } await new Promise((done) => setTimeout(done, poll)) } } +/** + * A success with its body read as JSON, so a body that fails, stalls past the request's limit or + * is not JSON is a failed request, asked again; any other answer with its body let go, so its + * connection does not keep the process up. + * @param {Answer} response + * @returns {Promise} + */ +async function read(response) { + if (!response.ok) { + await response.body?.cancel().catch(() => {}) + return { ok: false, status: response.status, json: async () => undefined } + } + const data = await response.json() + return { ok: true, status: response.status, json: async () => data } +} + +/** + * A request that failed, timed out or could not be read, as a response whose status says why. + * @param {any} error + * @returns {Served} + */ +const failed = (error) => ({ + ok: false, + status: String(error?.cause?.code ?? (error?.name === 'TimeoutError' ? 'no answer in time' : error?.message) ?? error), + json: async () => undefined +}) + +/** @param {string} what @param {number | string} status */ +const unchecked = (what, status) => + `[published] ${what} could not be checked (${status}), which says nothing about whether it is there: run the publish again.` + +/** + * Stops a check whose request failed with anything but a 404, the one answer that says a version + * is not served: after a server error, a refusal, or a request that failed, timed out or could not + * be read, the check says it could not tell, rather than to publish what may be there. + * @param {Served} response + * @param {string} what what was asked for, as the message names it + */ +export function stopUnlessAnswered(response, what) { + if (response.ok || response.status === 404) return + console.error(unchecked(what, response.status)) + process.exit(1) +} + /** * The dependencies the package in `folder` names by `workspace:` (in `dependencies`, * `optionalDependencies` and `peerDependencies`, not `devDependencies`, which nobody installing it - * gets), each at the version the workspace has, and whether npm serves it. + * gets), each at the version the workspace has, and whether npm serves it. They share one wait. * @param {string} folder a package's folder, beside the others under packages/ * @param {Parameters[2]} [options] * @returns {Promise<{ name: string, version: string, status: number | string, ok: boolean }[]>} */ -export async function dependenciesServed(folder, options) { +export async function dependenciesServed(folder, options = {}) { const manifest = JSON.parse(await readFile(join(folder, 'package.json'), 'utf8')) const named = Object.entries({ ...manifest.peerDependencies, ...manifest.optionalDependencies, ...manifest.dependencies }) .filter(([, range]) => String(range).startsWith('workspace:')) @@ -81,13 +129,12 @@ export async function dependenciesServed(folder, options) { const other = await readFile(join(packages, entry.name, 'package.json'), 'utf8').then(JSON.parse, () => null) if (other?.name) versions.set(other.name, other.version) } + const until = options.until ?? waitUntil(options.minutes) const results = [] for (const name of named) { const version = versions.get(name) if (!version) throw new Error(`${name} is named by workspace: in ${join(folder, 'package.json')}, but no package beside it has that name`) - const response = await served(`https://registry.npmjs.org/${name.replace('/', '%2f')}/${version}`, `${name}@${version}`, options) - // Only the status is read: the body is let go, so its connection does not keep the process up. - await response.body?.cancel().catch(() => {}) + const response = await served(`https://registry.npmjs.org/${name.replaceAll('/', '%2f')}/${version}`, `${name}@${version}`, { ...options, until }) results.push({ name, version, status: response.status, ok: response.ok }) } return results @@ -95,13 +142,12 @@ export async function dependenciesServed(folder, options) { if (import.meta.url === pathToFileURL(process.argv[1] ?? '').href) { const folder = process.cwd() + const { name: dependent } = JSON.parse(await readFile(join(folder, 'package.json'), 'utf8')) const results = await dependenciesServed(folder) - const missing = results.filter((result) => !result.ok) - for (const { name, version } of results.filter((result) => result.ok)) console.log(`[published] ${name}@${version} is on npm`) - if (missing.length) { - for (const { name, version, status } of missing) { - console.error(`[published] ${name}@${version} is not on npm (${status}). Publish it first: ${JSON.parse(await readFile(join(folder, 'package.json'), 'utf8')).name} depends on it, at that version.`) - } - process.exit(1) + for (const { name, version, status, ok } of results) { + if (ok) console.log(`[published] ${name}@${version} is on npm`) + else if (status === 404) console.error(`[published] ${name}@${version} is not on npm (404). Publish it first: ${dependent} depends on it, at that version.`) + else console.error(unchecked(`${name}@${version} on npm`, status)) } + if (results.some((result) => !result.ok)) process.exit(1) } From 6be200f5bf4c4ec1035fff32365e375d73dd0b28 Mon Sep 17 00:00:00 2001 From: Aleksandr Shevchuk Date: Fri, 20 Nov 2026 09:31:28 +0200 Subject: [PATCH 3/3] ci: a clock the shared-wait test controls, and the 404 said for npm and for jsDelivr (#253) From the second review of #254: - the test that dependencies share one wait moved a 30 ms real-time window, which a busy runner could miss; its requests now move a clock of their own, so the wait runs out at the fourth request every time; - design/build.md says what a 404 means from npm and from jsDelivr, and what the check prints after any other failure. --- design/build.md | 10 ++++++---- packages/author/tests/published.test.ts | 20 ++++++++++++++++---- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/design/build.md b/design/build.md index 431d36df..791ec5a4 100644 --- a/design/build.md +++ b/design/build.md @@ -163,10 +163,12 @@ is on npm failed on one published moments before. Two kinds of check now wait wh Both ask again every 30 s after a 404, a server error, or a request that failed, timed out or could not be read. The checks in one script share one wait of `H5P_PUBLISH_WAIT` minutes, which -`publish.yml` sets to 30, twice the longest seen. By hand and in a dry run it is 0: a 404 means npm -does not serve the dependency, not published or still being processed, and the check says so at -once. Any other failure stops the check, which says it could not tell rather than to publish. Each -request, its body included, is cut at 30 s: one to npm hung for over 5 minutes. +`publish.yml` sets to 30, twice the longest seen. By hand and in a dry run it is 0, and the check +says at once what a 404 means. From npm, it does not serve the version: not published, or still +being processed. From jsDelivr, the pack there has no `parts/index.json`. After any other failure +the check stops and prints that the version could not be checked, which says nothing about whether +it is there, and to run the publish again. Each request, its body included, is cut at 30 s: one to +npm hung for over 5 minutes. A publish of the player runs its `prepublishOnly` (a check that the pack it names is on npm, typecheck, the unit suite and the build) so a broken tree cannot ship; the browser suite is left out because it needs an installed diff --git a/packages/author/tests/published.test.ts b/packages/author/tests/published.test.ts index af8702ba..0644a32b 100644 --- a/packages/author/tests/published.test.ts +++ b/packages/author/tests/published.test.ts @@ -144,10 +144,22 @@ describe('the dependencies a package names by workspace:', () => { }) it('share one wait: one that is missing leaves the next none of its own', async () => { - const { fetch, asked } = answers(404) - const results = await dependenciesServed(join(packages, 'a'), { until: Date.now() + 30, poll: 1, fetch }) - expect(results.map(({ ok }) => ok)).toEqual([false, false]) - expect(asked.filter((url) => url.includes('%2fc/')).length).toBeGreaterThan(1) + // A clock the requests move, 10 ms each, so the wait runs out at the fourth however busy the machine is. + let now = 1_000 + const clock = vi.spyOn(Date, 'now').mockImplementation(() => now) + const asked: string[] = [] + const fetch = async (url: string) => { + asked.push(url) + now += 10 + return { ok: false, status: 404, json: async () => ({}) } + } + try { + const results = await dependenciesServed(join(packages, 'a'), { until: 1_035, poll: 1, fetch }) + expect(results.map(({ ok }) => ok)).toEqual([false, false]) + } finally { + clock.mockRestore() + } + expect(asked.filter((url) => url.includes('%2fc/'))).toHaveLength(4) expect(asked.filter((url) => url.includes('%2fb/'))).toHaveLength(1) })