diff --git a/.changeset/22420-console-drift-responder.md b/.changeset/22420-console-drift-responder.md new file mode 100644 index 00000000000..b3b2b7eb4a6 --- /dev/null +++ b/.changeset/22420-console-drift-responder.md @@ -0,0 +1,18 @@ +--- +'@objectstack/cli': patch +--- + +fix(cli): a dev server that refuses a drifted Console answers `503` at `/_console/` naming the rebuild, instead of a bare `404` (#22420) + +Clause-②: no + +A refused Console mount answers `503` naming the remedy instead of the router's bare `404` on the same routes, as the not-built position already does. No accepted input, key or export changes. + +`os dev` refuses to mount a Console whose built `dist/.objectui-sha` differs from the repository's `.objectui-sha` pin. The terminal printed the refusal, but nothing was mounted, so `GET /_console/` and `GET /` answered `404 ENDPOINT_NOT_FOUND`, the same answer as a wrong URL. That boot now mounts the same routes as the not-built answer: + +- `GET /_console/` and every path under it answer `503` (`text/plain`, `no-store`). The body is the refusal's own text: both objectui SHAs, why the stale build is refused, `pnpm objectui:build`, and the `OS_ALLOW_CONSOLE_DRIFT=1` escape hatch. The pin file is named without its absolute path. +- `GET /` and `GET /_console` redirect to `/_console/`, as they do when the Console is served. + +The same answer now covers an install where `@objectstack/console` does not resolve at all. That boot used to print nothing and mount nothing. It now prints ``Console package `@objectstack/console` not found``, followed by the reinstall sentence an install with no `dist/` already gets. `/_console/` answers `503` saying the Console is not installed. + +`--no-ui` and `--no-console` still mount nothing, so those routes answer `404`. diff --git a/packages/cli/src/commands/serve.ts b/packages/cli/src/commands/serve.ts index ca7e232ce45..1b9618448ed 100644 --- a/packages/cli/src/commands/serve.ts +++ b/packages/cli/src/commands/serve.ts @@ -174,6 +174,7 @@ import { formatConsoleDistMissingWarning, createConsoleStaticPlugin, createConsoleNotBuiltPlugin, + createConsoleShaDriftRefusalPlugin, createRuntimeAssetsPlugin, type ConsoleShaDrift, } from '../utils/console.js'; @@ -4954,25 +4955,27 @@ export default class Serve extends Command { // present, it owns the root `/` redirect (preferred default UI). It is // optional — we only mount it when the package resolves and a // pre-built `dist/` is present, and — in dev — only when that build - // matches the repo's objectui pin (#7752). A package that resolves - // with no `dist/` gets the not-built answer on those same routes, so - // `/_console/` names the remedy the boot warning prints instead of - // answering a bare 404. - if (consolePath) { - if (consoleWillMount) { - if (consoleDrift) { - console.warn(chalk.yellow(formatConsoleShaDriftWarning(consoleDrift))); - } - const consoleDistPath = path.join(consolePath, 'dist'); - await kernel.use(createConsoleStaticPlugin(consoleDistPath, { isDev })); - trackPlugin('ConsoleUI'); - } else if (refusedForDrift && consoleDrift) { - console.error(chalk.red(formatConsoleShaDriftRefusal(consoleDrift))); - } else { - console.warn(chalk.yellow(formatConsoleDistMissingWarning(consolePath))); - await kernel.use(createConsoleNotBuiltPlugin(consolePath)); - trackPlugin('ConsoleNotBuilt'); + // matches the repo's objectui pin (#7752). Every other boot that wants + // a Console gets a responder on those same routes instead, so + // `/_console/` answers `503` naming the remedy its boot message prints + // rather than a bare 404 (#22420): the drift refusal; a package with no + // `dist/`; no package at all (`consolePath` null). Only `--no-ui` / + // `--no-console` mount nothing, by the operator's own choice. + if (consolePath && consoleWillMount) { + if (consoleDrift) { + console.warn(chalk.yellow(formatConsoleShaDriftWarning(consoleDrift))); } + const consoleDistPath = path.join(consolePath, 'dist'); + await kernel.use(createConsoleStaticPlugin(consoleDistPath, { isDev })); + trackPlugin('ConsoleUI'); + } else if (refusedForDrift && consoleDrift) { + console.error(chalk.red(formatConsoleShaDriftRefusal(consoleDrift))); + await kernel.use(createConsoleShaDriftRefusalPlugin(consoleDrift)); + trackPlugin('ConsoleShaDriftRefused'); + } else if (consoleEnabled) { + console.warn(chalk.yellow(formatConsoleDistMissingWarning(consolePath))); + await kernel.use(createConsoleNotBuiltPlugin(consolePath)); + trackPlugin('ConsoleNotBuilt'); } } diff --git a/packages/cli/src/utils/console-route-ledger.ts b/packages/cli/src/utils/console-route-ledger.ts index 50df99aca32..a4fbe48d5cd 100644 --- a/packages/cli/src/utils/console-route-ledger.ts +++ b/packages/cli/src/utils/console-route-ledger.ts @@ -113,10 +113,12 @@ export interface ConsoleRouteLedgerEntry { } export const CONSOLE_ROUTE_LEDGER: readonly ConsoleRouteLedgerEntry[] = [ - // ── console SPA static serving (createConsoleStaticPlugin) ───────── + // ── console SPA static serving (createConsoleStaticPlugin), and the ──── + // ── responders mounted on the same routes when the Console is refused ── + // ── (createConsoleNotBuiltPlugin, createConsoleShaDriftRefusalPlugin) ── { route: 'GET /', - family: 'console-static', + family: 'console-static | console-unavailable', mountedIn: 'utils/console.ts', disposition: 'static-asset', conditional: 'options.rootRedirect !== false (default: mounted)', @@ -124,17 +126,23 @@ export const CONSOLE_ROUTE_LEDGER: readonly ConsoleRouteLedgerEntry[] = [ 'redirects the site root to `/_console/`. The Console is the default end-user surface, so claiming `/` is the ' + 'intended behaviour in both dev and production once the Console is mounted at all; `os serve` gates whether ' + 'it mounts via `--no-console` / `OS_DISABLE_CONSOLE=1`. Not API surface — a redirect to a static bundle. ' - + 'CONDITIONAL, and the ledger says so because the census reads the mount call and cannot see the branch.', + + 'CONDITIONAL, and the ledger says so because the census reads the mount call and cannot see the branch. ' + + 'Also mounted, unconditionally, by the `console-unavailable` responders `os serve` mounts on a boot that ' + + 'wants a Console and refuses it — `createConsoleNotBuiltPlugin` (no built `dist/`, or no console package) ' + + 'and `createConsoleShaDriftRefusalPlugin` (a dev build that is not the repo\'s objectui pin): the same ' + + 'redirect, to the `503` that `GET /_console/*` answers there. One mount per boot; the boots are exclusive.', }, { route: 'GET /_console', - family: 'console-static', + family: 'console-static | console-unavailable', mountedIn: 'utils/console.ts', disposition: 'static-asset', note: 'redirects the bare mount path to its trailing-slash form, the ordinary SPA convention — the Console is built ' + 'with `base: \'/_console/\'`, so relative asset URLs only resolve from the slashed path. Pure navigation ' - + 'plumbing for a static bundle; there is nothing here for an SDK to express.', + + 'plumbing for a static bundle; there is nothing here for an SDK to express. Also mounted by the ' + + '`console-unavailable` responders (`createConsoleNotBuiltPlugin`, `createConsoleShaDriftRefusalPlugin`) on a ' + + 'boot that refuses the Console: the same redirect.', }, { route: 'GET /forms/:slug', @@ -151,14 +159,18 @@ export const CONSOLE_ROUTE_LEDGER: readonly ConsoleRouteLedgerEntry[] = [ }, { route: 'GET /_console/*', - family: 'console-static', + family: 'console-static | console-unavailable', mountedIn: 'utils/console.ts', disposition: 'static-asset', note: 'serves the pre-built Console SPA verbatim from `dist/`, with HTML entry points routed through base-tag ' + 'injection and an SPA fallback for client-side routes. Reads files off disk behind a path-traversal guard ' + '(any resolved path escaping `dist/` is refused 403). A file server, not an endpoint: its peer group is a ' - + 'CDN origin, so no client method builds these URLs and none should.', + + 'CDN origin, so no client method builds these URLs and none should. On a boot that refuses the Console, ' + + 'the `console-unavailable` responders mount it instead and answer `503` with a `no-store` plain-text body ' + + 'naming the remedy the boot message prints, without host paths: `createConsoleNotBuiltPlugin` (build it, ' + + 'or reinstall `@objectstack/console`) and `createConsoleShaDriftRefusalPlugin` (rebuild the dist at the ' + + 'repo\'s objectui pin). Still the bundle\'s address, answering why there is no bundle to serve.', }, // ── runtime asset serving (second factory in the same module) ────── diff --git a/packages/cli/src/utils/console.not-built.test.ts b/packages/cli/src/utils/console.not-built.test.ts index 9fca308ec75..5608497b321 100644 --- a/packages/cli/src/utils/console.not-built.test.ts +++ b/packages/cli/src/utils/console.not-built.test.ts @@ -13,6 +13,10 @@ * - `GET /_console/` (and every path under it) answers `503` with the remedy * the boot warning names — the same sentence from the same picker, with * the host's absolute paths left out; + * - the same holds when the console package does not resolve at all + * (`consolePath` is `null`), which used to print nothing and mount nothing: + * the answer says the Console is not installed and names the reinstall + * sentence the picker already gives an install with no `dist/`; * - `GET /` and `GET /_console` redirect to `/_console/`, exactly as they do * once the Console is built (the control below), so neither route changes * owner with the build. @@ -38,6 +42,10 @@ const ORIGIN = 'http://console.example.test'; /** The first line of the not-built answer, which says what is wrong before saying how to fix it. */ const NOT_BUILT_HEADLINE = 'The ObjectStack Console is not built, so this server has no Console to serve at /_console/.'; +/** The same line when the console package did not resolve at all. */ +const NOT_INSTALLED_HEADLINE = + 'The ObjectStack Console is not installed, so this server has no Console to serve at /_console/.'; + /** The framework repo's remedy. */ const BUILD_REMEDY = 'pnpm objectui:build'; @@ -176,6 +184,44 @@ describe('an installed CLI whose `@objectstack/console` has no `dist/`', () => { }); }); +describe('an install where `@objectstack/console` does not resolve at all', () => { + let request: Request; + beforeAll(async () => { + request = await serve(createConsoleNotBuiltPlugin(null)); + }); + + it.each(CONSOLE_REQUESTS)('GET %s answers 503 saying the Console is not installed', async (p) => { + const res = await request(p); + expect(res.status).toBe(503); + expect(res.headers.get('content-type')).toMatch(/^text\/plain/); + expect(res.headers.get('cache-control')).toBe('no-store'); + const body = await res.text(); + expect(body.split('\n')[0]).toBe(NOT_INSTALLED_HEADLINE); + expect(body).toContain('@objectstack/console'); + expect(body).not.toContain(BUILD_REMEDY); + }); + + it('is the boot warning\'s sentence verbatim — it names no host path to leave out', async () => { + const warning = formatConsoleDistMissingWarning(null); + expect(warning.startsWith(WARNING_PREFIX)).toBe(true); + const body = await (await request('/_console/')).text(); + expect(body).toContain(warning.slice(WARNING_PREFIX.length)); + expect(body).not.toContain(scratch); + }); + + it('names the same reinstall sentence as an install whose package has no `dist/`', () => { + // One picker: the unresolved arm differs from the installed-without-dist + // arm only in what it says is missing, never in the fix. + const { consoleDir } = installTree(); + const tail = (s: string) => s.slice(s.indexOf(' — ')); + expect(tail(formatConsoleDistMissingWarning(null))).toBe(tail(formatConsoleDistMissingWarning(consoleDir))); + }); + + it('redirects `/` and `/_console` to `/_console/`', async () => { + await expectRedirectsToConsole(request); + }); +}); + describe('controls', () => { it('a BUILT Console serves its SPA shell at `/_console/` and the same redirects', async () => { const dist = path.join(scratch, 'built', 'dist'); diff --git a/packages/cli/src/utils/console.sha-drift.test.ts b/packages/cli/src/utils/console.sha-drift.test.ts index f518619541b..7bf5a50e697 100644 --- a/packages/cli/src/utils/console.sha-drift.test.ts +++ b/packages/cli/src/utils/console.sha-drift.test.ts @@ -15,13 +15,29 @@ * server does not mount the Console at all, so the stale bundle is * unreachable rather than silently authoritative. Detection semantics * (`detectConsoleShaDrift`) are pinned in `test/console-resolve.test.ts`. + * + * Unreachable must not mean silent. A refused boot used to mount nothing, so + * `GET /_console/` and `GET /` answered the router's bare 404, the same answer + * a wrong URL gets, and only the terminal said why. `os serve` now mounts + * `createConsoleShaDriftRefusalPlugin` on that boot — the not-built answer's + * routes (`console.not-built.test.ts`) with the refusal's own text — and the + * last block below pins what it answers, on a REAL Hono app, from a fixture + * tree whose pin and stamp are made up here (never the repo's live pin). */ -import { describe, it, expect } from 'vitest'; +import { describe, it, expect, beforeAll, afterAll } from 'vitest'; +import fs from 'node:fs'; +import os from 'node:os'; +import path from 'node:path'; +import { HonoHttpServer } from '@objectstack/plugin-hono-server'; import { + createConsoleShaDriftRefusalPlugin, + createConsoleStaticPlugin, decideConsoleMount, + detectConsoleShaDrift, formatConsoleShaDriftRefusal, formatConsoleShaDriftWarning, DRIFT_OVERRIDE_ENV, + type ConsoleShaDrift, } from './console.js'; /** The exact gap #7752 measured: pin two days ahead of the served bundle. */ @@ -111,3 +127,118 @@ describe('drift messages', () => { expect(message).toContain(DRIFT_OVERRIDE_ENV); }); }); + +describe('what a dev server that refused a drifted Console answers', () => { + const ORIGIN = 'http://console.example.test'; + const PIN = 'aaaaaaaaaaaa1111111111111111111111111111'; + const STAMP = 'bbbbbbbbbbbb2222222222222222222222222222'; + const HEADLINE = + 'The ObjectStack Console here was built from a different objectui commit than this checkout pins, ' + + 'so this dev server refuses to serve it at /_console/.'; + /** The shell, a client-side route and a hashed asset: the refusal covers every path under the mount. */ + const CONSOLE_REQUESTS = ['/_console/', '/_console/apps/crm/records/42', '/_console/assets/index-ocmkyCt6.js']; + + type Request = (p: string) => Promise; + let scratch: string; + + beforeAll(() => { + scratch = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), 'os-test-console-drift-refusal-'))); + }); + + afterAll(() => { + fs.rmSync(scratch, { recursive: true, force: true }); + }); + + /** A checkout pinned at `PIN` whose console dist is stamped `stamp` — what `os dev` resolves. */ + function pinnedTree(tag: string, stamp: string): { root: string; consoleDir: string } { + const root = path.join(scratch, tag); + const consoleDir = path.join(root, 'packages', 'console'); + fs.mkdirSync(path.join(consoleDir, 'dist'), { recursive: true }); + fs.writeFileSync(path.join(root, '.objectui-sha'), `${PIN}\n`); + fs.writeFileSync(path.join(consoleDir, 'package.json'), JSON.stringify({ name: '@objectstack/console' })); + fs.writeFileSync( + path.join(consoleDir, 'dist', 'index.html'), + 'console shell', + ); + fs.writeFileSync(path.join(consoleDir, 'dist', '.objectui-sha'), `${stamp}\n`); + return { root, consoleDir }; + } + + async function serve(plugin: { start: (ctx: any) => Promise }): Promise { + const server = new HonoHttpServer(0); + await plugin.start({ getServiceAsync: async () => server, logger: { warn: () => {} } }); + const app = server.getRawApp(); + return async (p) => app.request(`${ORIGIN}${p}`); + } + + describe('the build is not the pin', () => { + let root: string; + let drift: ConsoleShaDrift; + let request: Request; + beforeAll(async () => { + const tree = pinnedTree('drifted', STAMP); + root = tree.root; + const found = detectConsoleShaDrift(tree.consoleDir); + expect(found, 'the fixture must be a drift detectConsoleShaDrift can prove').not.toBeNull(); + drift = found!; + // The premise: this is the boot `os dev` refuses, not one it serves. + expect(decideConsoleMount({ hasDist: true, drift, isDev: true, env: {} })).toEqual({ + mount: false, + refusedForDrift: true, + }); + request = await serve(createConsoleShaDriftRefusalPlugin(drift)); + }); + + it.each(CONSOLE_REQUESTS)('GET %s answers 503 naming `pnpm objectui:build`, not the SPA', async (p) => { + const res = await request(p); + expect(res.status).toBe(503); + expect(res.headers.get('content-type')).toMatch(/^text\/plain/); + expect(res.headers.get('cache-control')).toBe('no-store'); + const body = await res.text(); + expect(body.split('\n')[0]).toBe(HEADLINE); + expect(body).toContain('pnpm objectui:build'); + expect(body).toContain(PIN.slice(0, 12)); + expect(body).toContain(STAMP.slice(0, 12)); + expect(body).toContain(DRIFT_OVERRIDE_ENV); + expect(body).not.toContain('console shell'); + }); + + it('is the terminal refusal\'s text, with only the absolute pin-file path left out', async () => { + const refusal = formatConsoleShaDriftRefusal(drift); + expect(drift.pinFile.startsWith(root)).toBe(true); + // The refusal block names the pin file by its path on this host; the + // HTTP answer says where it is in words. Everything below the block's + // headline, either side of that path, is the same text. + const [before, after] = refusal.slice(refusal.indexOf('pinned')).split(drift.pinFile); + expect(after, 'the refusal no longer names the pin file this test splits on').toBeDefined(); + + const body = await (await request('/_console/')).text(); + expect(body).toContain(before); + expect(body).toContain(after); + expect(body).not.toContain(root); + }); + + it('redirects `/` and `/_console` to `/_console/`, as a served Console does', async () => { + for (const p of ['/', '/_console']) { + const res = await request(p); + expect(res.status, `GET ${p}`).toBe(302); + expect(res.headers.get('location'), `GET ${p}`).toBe('/_console/'); + } + }); + }); + + it('control: a build that matches the pin is no drift, mounts, and serves the SPA shell', async () => { + const { consoleDir } = pinnedTree('matching', PIN); + const drift = detectConsoleShaDrift(consoleDir); + expect(drift).toBeNull(); + expect(decideConsoleMount({ hasDist: true, drift, isDev: true, env: {} })).toEqual({ + mount: true, + refusedForDrift: false, + }); + const request = await serve(createConsoleStaticPlugin(path.join(consoleDir, 'dist'))); + const res = await request('/_console/'); + expect(res.status).toBe(200); + expect(res.headers.get('content-type')).toMatch(/^text\/html/); + expect(await res.text()).toContain('console shell'); + }); +}); diff --git a/packages/cli/src/utils/console.ts b/packages/cli/src/utils/console.ts index 0a8b6a55bd4..e8dd212b0e7 100644 --- a/packages/cli/src/utils/console.ts +++ b/packages/cli/src/utils/console.ts @@ -334,12 +334,12 @@ function findConsoleBuildRoot(consolePath: string): string | null { } /** - * What is missing and what fixes it, for a resolved console package with no - * built `dist/` — the ONE remedy picker, which both the boot warning and the - * HTTP answer render ({@link formatConsoleDistMissingWarning}, - * {@link createConsoleNotBuiltPlugin}). + * What is missing and what fixes it, for a console package with no built + * `dist/` — or no console package at all (`consolePath` is `null`) — the ONE + * remedy picker, which both the boot warning and the HTTP answer render + * ({@link formatConsoleDistMissingWarning}, {@link createConsoleNotBuiltPlugin}). * - * Two places reach it, and each gets the remedy that works there: + * Three places reach it, and each gets the remedy that works there: * * - the framework repo, where `packages/console/dist` is a gitignored build * that `pnpm build` never produces — `pnpm objectui:build`, run at the @@ -350,6 +350,12 @@ function findConsoleBuildRoot(consolePath: string): string | null { * remedy is that package install. (An installed `@object-ui/console`, * which this text used to name, is never consulted: that name is matched * only as the workspace package of a sibling `../objectui` checkout.) + * - an install where `@objectstack/console` did not resolve at all, or only + * at a major version this CLI ignores ({@link resolveConsolePath} returned + * `null`). That is the same broken install as the arm above, one step + * further gone, so it gets the same reinstall sentence; only what is + * missing differs. Inside the framework repo the CLI always resolves its + * own workspace `packages/console`, so this arm is never reached there. * * `hostPaths` decides whether the text names absolute paths on this host. The * terminal's reader is the operator, who is told exactly where to look. The @@ -358,7 +364,11 @@ function findConsoleBuildRoot(consolePath: string): string | null { * reinstall) is the same sentence either way, and the repository-relative * `packages/console/dist` stays because it is the repository's public layout. */ -function describeConsoleDistMissing(consolePath: string, hostPaths: boolean): string { +function describeConsoleDistMissing(consolePath: string | null, hostPaths: boolean): string { + const reinstall = + `the Console ships prebuilt in \`${CONSOLE_PACKAGE}\`, ` + + `a dependency of \`@objectstack/cli\`; reinstall it at the same version as the CLI.`; + if (consolePath === null) return `Console package \`${CONSOLE_PACKAGE}\` not found — ${reinstall}`; const dist = path.join(consolePath, 'dist'); const buildRoot = findConsoleBuildRoot(consolePath); if (buildRoot) { @@ -368,14 +378,14 @@ function describeConsoleDistMissing(consolePath: string, hostPaths: boolean): st `(uses a ../objectui checkout if present, else clones objectui at the pinned commit — needs network).` ); } - return ( - `Console dist not found${hostPaths ? ` at ${dist}` : ''} — the Console ships prebuilt in \`${CONSOLE_PACKAGE}\`, ` + - `a dependency of \`@objectstack/cli\`; reinstall it at the same version as the CLI.` - ); + return `Console dist not found${hostPaths ? ` at ${dist}` : ''} — ${reinstall}`; } -/** The boot warning for a resolved console package with no built `dist/`. */ -export function formatConsoleDistMissingWarning(consolePath: string): string { +/** + * The boot warning for a console package with no built `dist/`, or for no + * console package at all (`null`, see {@link describeConsoleDistMissing}). + */ +export function formatConsoleDistMissingWarning(consolePath: string | null): string { return ` ⚠ ${describeConsoleDistMissing(consolePath, true)}`; } @@ -461,15 +471,22 @@ export function formatConsoleShaDriftWarning(drift: ConsoleShaDrift): string { } /** - * The refusal block — `os dev` declines to mount a drifted console. Mirrors - * the remediation of `scripts/check-console-sha.mjs` verbatim (rebuild at the - * *pinned* SHA with `pnpm objectui:build`; `objectui:refresh` would re-bump - * the pin to the local ../objectui HEAD, which is the opposite of the fix). + * What the drift refusal says below its headline — the two SHAs, why the stale + * build is refused, the remedy and the escape hatch. The ONE text both the + * refusal block ({@link formatConsoleShaDriftRefusal}) and the HTTP answer + * ({@link createConsoleShaDriftRefusalPlugin}) render, so the two cannot name + * different fixes. + * + * `hostPaths` decides whether the pin file is named by its absolute path on + * this host, by the rule {@link describeConsoleDistMissing} states: the + * terminal's reader is the operator, the HTTP answer's reader is whoever + * requests `/_console/`, with no credential. The SHAs stay in both: they name + * commits of the public objectui repository, not anything about this host. */ -export function formatConsoleShaDriftRefusal(drift: ConsoleShaDrift): string { +function describeConsoleShaDrift(drift: ConsoleShaDrift, hostPaths: boolean): string { + const pinFile = hostPaths ? drift.pinFile : '.objectui-sha at the checkout root'; return ( - `\n ✗ Console version drift — refusing to serve /_console in dev.\n\n` + - ` pinned (${drift.pinFile}): objectui@${drift.pin.slice(0, 12)}\n` + + ` pinned (${pinFile}): objectui@${drift.pin.slice(0, 12)}\n` + ` built (console/dist/.objectui-sha): objectui@${drift.stamp.slice(0, 12)}\n\n` + ` packages/console/dist is a gitignored local build that 'turbo run build' does NOT refresh,\n` + ` so this server would serve a Console SPA the repo no longer pins — and anything you\n` + @@ -481,6 +498,16 @@ export function formatConsoleShaDriftRefusal(drift: ConsoleShaDrift): string { ); } +/** + * The refusal block — `os dev` declines to mount a drifted console. Mirrors + * the remediation of `scripts/check-console-sha.mjs` verbatim (rebuild at the + * *pinned* SHA with `pnpm objectui:build`; `objectui:refresh` would re-bump + * the pin to the local ../objectui HEAD, which is the opposite of the fix). + */ +export function formatConsoleShaDriftRefusal(drift: ConsoleShaDrift): string { + return `\n ✗ Console version drift — refusing to serve /_console in dev.\n\n${describeConsoleShaDrift(drift, true)}`; +} + /** Env switch that downgrades the dev refusal back to a warning. */ export const DRIFT_OVERRIDE_ENV = 'OS_ALLOW_CONSOLE_DRIFT'; @@ -808,44 +835,46 @@ export function createConsoleStaticPlugin(distPath: string, options?: { isDev?: } /** - * What `/_console/` answers when the console package resolved but has no built - * `dist/` — mounted by `os serve` in place of {@link createConsoleStaticPlugin}, - * on the boot that prints {@link formatConsoleDistMissingWarning}. + * The ONE route-mounting body for a Console that `os serve` decided not to + * serve: what `/_console/` answers instead of the router's bare 404. Mounted in + * place of {@link createConsoleStaticPlugin} by the responder for each refused + * position — {@link createConsoleNotBuiltPlugin} (no built `dist/`, or no + * console package) and {@link createConsoleShaDriftRefusalPlugin} (a dev build + * that is not the repo's pin). Each responder brings its `answer`, rendered from + * the same text its boot message prints; this body decides nothing about it. * - * Without it nothing was mounted, so `GET /_console/` got the router's bare - * `ENDPOINT_NOT_FOUND` and `GET /` an empty 404: a missing build read exactly - * like a wrong URL, and only the terminal said why (AGENTS.md, Route & surface - * ownership rule 3, "Absence must be loud"). It mounts the same three routes the - * static plugin would, so who owns each path does not change with the build: + * Without a responder nothing was mounted, so `GET /_console/` got the router's + * bare `ENDPOINT_NOT_FOUND` and `GET /` an empty 404: a refused Console read + * exactly like a wrong URL, and only the terminal said why (AGENTS.md, Route & + * surface ownership rule 3, "Absence must be loud"). It mounts the same three + * routes the static plugin would, so who owns each path does not change with + * the build: * * - `GET /` and `GET /_console` redirect to `/_console/`, as they do once the - * Console is built — so `/` keeps one owner and one answer, the Console's; - * - `GET /_console/*` answers `503` with a short `text/plain` body saying the - * Console is not built, then the remedy {@link describeConsoleDistMissing} - * picked for the warning, rendered without host paths. + * Console is served — so `/` keeps one owner and one answer, the Console's; + * - `GET /_console/*` answers `503` with the responder's `text/plain` answer. + * + * `503`, not `404`: the address is right and the server is missing a Console + * build it can serve, which the operator fixes and a caller cannot — and a + * caller reading only the status still tells it apart from a wrong URL. Plain + * text rather than a JSON envelope: these are the Console's static-asset + * routes, not API surface (`console-route-ledger.ts`), read by a browser or by + * `curl`. `no-store`, so a browser never keeps the answer past the restart that + * serves the real Console. * - * `503`, not `404`: the address is right and the server is missing a build it - * needs, which the operator fixes and a caller cannot — and a caller reading - * only the status still tells it apart from a wrong URL. Plain text rather than - * a JSON envelope: these are the Console's static-asset routes, not API - * surface (`console-route-ledger.ts`), read by a browser or by `curl`. - * `no-store`, so a browser never keeps the answer past the restart that serves - * the real Console. + * `--no-ui` and `--no-console` mount none of this: the operator chose to serve + * no Console, and those routes stay the router's 404. */ -export function createConsoleNotBuiltPlugin(consolePath: string) { - const answer = - `The ObjectStack Console is not built, so this server has no Console to serve at ${CONSOLE_PATH}/.\n\n` + - `${describeConsoleDistMissing(consolePath, false)}\n\n` + - 'Then restart the server. The API is served either way.\n'; +function createConsoleUnavailablePlugin(name: string, what: string, answer: string) { return { - name: 'com.objectstack.console-not-built', + name, init: async () => {}, start: async (ctx: any) => { const httpServer = await resolveHttpServer(ctx); if (!httpServer?.getRawApp) { - ctx.logger?.warn?.('Console not-built answer: http.server service not found — skipping'); + ctx.logger?.warn?.(`Console ${what} answer: http.server service not found — skipping`); return; } @@ -857,6 +886,40 @@ export function createConsoleNotBuiltPlugin(consolePath: string) { }; } +/** + * The responder for a console package with no built `dist/`, or with no + * console package at all (`consolePath` is `null`): mounted by `os serve` on + * the boot that prints {@link formatConsoleDistMissingWarning}. Its answer says + * the Console is not built (or not installed), then the remedy + * {@link describeConsoleDistMissing} picked for the warning, rendered without + * host paths. The routes are {@link createConsoleUnavailablePlugin}'s. + */ +export function createConsoleNotBuiltPlugin(consolePath: string | null) { + const answer = + `The ObjectStack Console is ${consolePath === null ? 'not installed' : 'not built'}, ` + + `so this server has no Console to serve at ${CONSOLE_PATH}/.\n\n` + + `${describeConsoleDistMissing(consolePath, false)}\n\n` + + 'Then restart the server. The API is served either way.\n'; + return createConsoleUnavailablePlugin('com.objectstack.console-not-built', 'not-built', answer); +} + +/** + * The responder for a dev boot that refused a drifted Console + * ({@link decideConsoleMount} answered `refusedForDrift`): mounted by `os serve` + * on the boot that prints {@link formatConsoleShaDriftRefusal}. Its answer says + * the Console's build is not the repo's pin, then the refusal's own text + * ({@link describeConsoleShaDrift}) without the pin file's absolute path. The + * routes are {@link createConsoleUnavailablePlugin}'s. + */ +export function createConsoleShaDriftRefusalPlugin(drift: ConsoleShaDrift) { + const answer = + `The ObjectStack Console here was built from a different objectui commit than this checkout pins, ` + + `so this dev server refuses to serve it at ${CONSOLE_PATH}/.\n\n` + + `${describeConsoleShaDrift(drift, false)}\n` + + 'Then restart the server.\n'; + return createConsoleUnavailablePlugin('com.objectstack.console-sha-drift-refused', 'drift-refusal', answer); +} + // ─── Runtime Assets Plugin ────────────────────────────────────────── /** The URL prefix the runtime assets route is mounted under. */ diff --git a/packages/cli/test/published-subpath-console.pin.test.ts b/packages/cli/test/published-subpath-console.pin.test.ts index b36ca275d6d..b05a64d54a0 100644 --- a/packages/cli/test/published-subpath-console.pin.test.ts +++ b/packages/cli/test/published-subpath-console.pin.test.ts @@ -175,7 +175,11 @@ const RETIRED_RUNTIME_NAMES = [ * them off the published subpath. Publishing one is the same deliberate act as * re-admitting a retired name. */ -const INTERNAL_SINCE_RETIREMENT = ['createConsoleNotBuiltPlugin', 'formatConsoleDistMissingWarning']; +const INTERNAL_SINCE_RETIREMENT = [ + 'createConsoleNotBuiltPlugin', + 'createConsoleShaDriftRefusalPlugin', + 'formatConsoleDistMissingWarning', +]; /** * Every subpath the published package resolves, in full. A subpath removed here