|
| 1 | +// Contract-freshness guard + fixer. The committed embed-api.json is a PIN of the |
| 2 | +// manifest served at /embed/json; everything in src/generated/ derives from it. The pin |
| 3 | +// must strictly equal the live manifest (editor_version excluded — injected per deploy |
| 4 | +// at the serve boundary): a drifted pin means the SDK is generated from a contract the |
| 5 | +// editor no longer serves. |
| 6 | +// |
| 7 | +// check (default): `npm run check:contract` — fails when the pin is out of date. |
| 8 | +// fix: `npm run fix:contract` — re-syncs the pin from the live manifest |
| 9 | +// (prettier-formatted to keep diffs minimal) and regenerates |
| 10 | +// src/generated/; review the diff and commit both. |
| 11 | + |
| 12 | +import { execFileSync } from 'node:child_process' |
| 13 | +import { readFileSync, writeFileSync } from 'node:fs' |
| 14 | +import { dirname, join } from 'node:path' |
| 15 | +import { fileURLToPath } from 'node:url' |
| 16 | +import { isDeepStrictEqual } from 'node:util' |
| 17 | + |
| 18 | +const PKG_ROOT = join(dirname(fileURLToPath(import.meta.url)), '..') |
| 19 | +const PIN_PATH = join(PKG_ROOT, 'embed-api.json') |
| 20 | + |
| 21 | +// Overridable so the pin can be checked / synced against a staging / local editor. |
| 22 | +const CONTRACT_URL = process.env.EMBED_CONTRACT_URL ?? 'https://simplepdf.com/embed/json' |
| 23 | + |
| 24 | +const EXIT_CODES = { |
| 25 | + stale_pin: 1, |
| 26 | + manifest_fetch_failed: 2, |
| 27 | +} |
| 28 | + |
| 29 | +const isFixMode = process.argv.includes('--fix') |
| 30 | + |
| 31 | +const withoutEditorVersion = ({ editor_version: _editorVersion, ...manifest }) => manifest |
| 32 | + |
| 33 | +const fetchLiveManifest = async () => { |
| 34 | + try { |
| 35 | + const manifestResponse = await fetch(CONTRACT_URL) |
| 36 | + if (!manifestResponse.ok) { |
| 37 | + throw new Error(`unexpected response status ${manifestResponse.status}`) |
| 38 | + } |
| 39 | + return await manifestResponse.json() |
| 40 | + } catch (error) { |
| 41 | + console.error(`contract: failed to fetch the live manifest from ${CONTRACT_URL}: ${error.message}`) |
| 42 | + process.exit(EXIT_CODES.manifest_fetch_failed) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +const liveManifest = await fetchLiveManifest() |
| 47 | + |
| 48 | +if (isFixMode) { |
| 49 | + writeFileSync(PIN_PATH, `${JSON.stringify(liveManifest, null, 2)}\n`) |
| 50 | + // The repo-pinned prettier, so the committed pin formatting stays byte-stable |
| 51 | + // across syncs and the next diff shows only real contract changes. |
| 52 | + execFileSync(join(PKG_ROOT, '..', 'node_modules', '.bin', 'prettier'), ['--write', PIN_PATH], { stdio: 'inherit' }) |
| 53 | + execFileSync('node', [join(PKG_ROOT, 'scripts', 'generate.mjs')], { cwd: PKG_ROOT, stdio: 'inherit' }) |
| 54 | + console.log( |
| 55 | + `fix:contract: pin re-synced to ${CONTRACT_URL} (editor_version: ${liveManifest.editor_version}) — review the diff and commit embed-api.json + src/generated/`, |
| 56 | + ) |
| 57 | + process.exit(0) |
| 58 | +} |
| 59 | + |
| 60 | +const pinnedManifest = withoutEditorVersion(JSON.parse(readFileSync(PIN_PATH, 'utf8'))) |
| 61 | +const comparableLiveManifest = withoutEditorVersion(liveManifest) |
| 62 | + |
| 63 | +if (isDeepStrictEqual(pinnedManifest, comparableLiveManifest)) { |
| 64 | + console.log(`check:contract: pin matches the live manifest at ${CONTRACT_URL}`) |
| 65 | + process.exit(0) |
| 66 | +} |
| 67 | + |
| 68 | +// Section-level hints so the failure is actionable without eyeballing two full JSONs. |
| 69 | +const driftedSections = Object.keys({ ...pinnedManifest, ...comparableLiveManifest }).filter( |
| 70 | + (section) => !isDeepStrictEqual(pinnedManifest[section], comparableLiveManifest[section]), |
| 71 | +) |
| 72 | + |
| 73 | +const operationHints = (() => { |
| 74 | + const pinnedOperations = new Map( |
| 75 | + (pinnedManifest.operations ?? []).map((operation) => [operation.request_type, operation]), |
| 76 | + ) |
| 77 | + const liveOperations = new Map( |
| 78 | + (comparableLiveManifest.operations ?? []).map((operation) => [operation.request_type, operation]), |
| 79 | + ) |
| 80 | + const requestTypes = new Set([...pinnedOperations.keys(), ...liveOperations.keys()]) |
| 81 | + return [...requestTypes].flatMap((requestType) => { |
| 82 | + const pinnedOperation = pinnedOperations.get(requestType) |
| 83 | + const liveOperation = liveOperations.get(requestType) |
| 84 | + if (pinnedOperation === undefined) return [`operation ${requestType}: live only (missing from the pin)`] |
| 85 | + if (liveOperation === undefined) return [`operation ${requestType}: pin only (no longer served)`] |
| 86 | + if (isDeepStrictEqual(pinnedOperation, liveOperation)) return [] |
| 87 | + return [`operation ${requestType}: differs`] |
| 88 | + }) |
| 89 | +})() |
| 90 | + |
| 91 | +console.error(`check:contract: the committed embed-api.json pin is out of date with ${CONTRACT_URL}`) |
| 92 | +console.error(` drifted sections: ${driftedSections.join(', ')}`) |
| 93 | +for (const hint of operationHints) { |
| 94 | + console.error(` - ${hint}`) |
| 95 | +} |
| 96 | +console.error('Run `npm run fix:contract` to re-sync, then commit embed-api.json + src/generated/.') |
| 97 | +process.exit(EXIT_CODES.stale_pin) |
0 commit comments