diff --git a/.github/workflows/website.yml b/.github/workflows/website.yml index 0dbd53aed5..4d2c0e6eb6 100644 --- a/.github/workflows/website.yml +++ b/.github/workflows/website.yml @@ -5,10 +5,12 @@ on: branches: [main] paths: - "website/**" + - "ui/**" - ".github/workflows/website.yml" pull_request: paths: - "website/**" + - "ui/**" - ".github/workflows/website.yml" workflow_dispatch: @@ -33,7 +35,9 @@ jobs: with: node-version: 22 cache: npm - cache-dependency-path: website/package-lock.json + cache-dependency-path: | + website/package-lock.json + ui/package-lock.json - name: Install dependencies run: npm ci @@ -41,6 +45,14 @@ jobs: - name: Build run: npm run build + # The click-through demo at /demo: the web UI replaying a recording of DuckShop on sqb dev. + - name: Build the UI demo + working-directory: ui + run: | + npm ci + SQB_UI_BASE=/demo VITE_SQB_DEMO=1 npm run build + node scripts/demo-pages.mjs build demo/recording.json.gz ../website/dist/demo + - name: Check links run: python3 scripts/check_links.py dist diff --git a/ui/README.md b/ui/README.md index 5bee1080fa..e2bcbc77ac 100644 --- a/ui/README.md +++ b/ui/README.md @@ -31,3 +31,22 @@ regenerate the extract; `check:command-table` fails when it is stale. npm run gen:command-table npm run check:command-table ``` + +## Static demo + +`sqlbuild.com/demo` is this UI with no server behind it: the server client replays +`demo/recording.json.gz`, a recording of DuckShop on `sqb dev`, and refuses every write. +The website workflow builds it: + +```sh +SQB_UI_BASE=/demo VITE_SQB_DEMO=1 npm run build +node scripts/demo-pages.mjs build demo/recording.json.gz ../website/dist/demo +``` + +To record it again, run DuckShop under `sqb dev`, click through the pages with the +browser saving a HAR, then ask the server for every read the pages made: + +```sh +node scripts/record-demo.mjs demo/recording.json.gz --server http://127.0.0.1: \ + --replace =/home/demo/duckshop --replace =duckshop session.har +``` diff --git a/ui/demo/recording.json.gz b/ui/demo/recording.json.gz new file mode 100644 index 0000000000..a5c6adaa17 Binary files /dev/null and b/ui/demo/recording.json.gz differ diff --git a/ui/scripts/demo-pages.mjs b/ui/scripts/demo-pages.mjs new file mode 100644 index 0000000000..e2cd139fff --- /dev/null +++ b/ui/scripts/demo-pages.mjs @@ -0,0 +1,68 @@ +// Lays out the static demo for a host without an SPA fallback, such as GitHub Pages. +// +// node scripts/demo-pages.mjs +// +// Copies the build built with SQB_UI_BASE=/demo VITE_SQB_DEMO=1 and the recording, then writes +// the app page as `.html` for every page and recorded item, so an address opened directly +// or reloaded loads the app instead of the site's 404 page. + +import { cpSync, mkdirSync, readFileSync, readdirSync, statSync, writeFileSync } from 'node:fs'; +import { dirname, join, relative } from 'node:path'; +import { gunzipSync } from 'node:zlib'; +import { fileURLToPath } from 'node:url'; + +const [build, recordingPath, destination] = process.argv.slice(2); +if (!build || !recordingPath || !destination) { + console.error('usage: node scripts/demo-pages.mjs '); + process.exit(2); +} + +/** Pages without a parameter, read from the routes, such as `runs` or `admin/audit`. */ +function staticPages(directory, prefix = '') { + return readdirSync(directory).flatMap((name) => { + const path = join(directory, name); + if (!statSync(path).isDirectory() || name.startsWith('[')) return []; + const page = prefix ? `${prefix}/${name}` : name; + const here = readdirSync(path).includes('+page.svelte') ? [page] : []; + return [...here, ...staticPages(path, page)]; + }); +} + +/** Item pages, from the lists the recording answers: every job, run and trigger the demo links to. */ +const ITEM_LISTS = [ + [/\/targets\/[^/]+\/jobs$/, 'jobs', (item) => item.job_key], + [/\/targets\/[^/]+\/runs\?/, 'runs', (item) => item.run_id], + [/\/targets\/[^/]+\/triggers$/, 'triggers', (item) => item.trigger_id], +]; +/** Catalog and check pages only for the items the recording holds, since there are hundreds. */ +const RECORDED_NODE = /\/targets\/[^/]+\/nodes\/([^/?]+)\/sql$/; +const RECORDED_CHECK = /\/targets\/[^/]+\/checks\/([^/?]+)$/; + +const recording = JSON.parse(gunzipSync(readFileSync(recordingPath)).toString('utf8')); +const items = new Set(); +for (const { url, text } of recording.responses) { + const node = url.match(RECORDED_NODE); + if (node) items.add(`catalog/${decodeURIComponent(node[1])}`); + const check = url.match(RECORDED_CHECK); + if (check) items.add(`checks/${decodeURIComponent(check[1])}`); + for (const [pattern, page, key] of ITEM_LISTS) { + if (!pattern.test(url)) continue; + const list = JSON.parse(text); + for (const item of Array.isArray(list) ? list : []) if (key(item)) items.add(`${page}/${key(item)}`); + } +} + +const routes = join(dirname(fileURLToPath(import.meta.url)), '../src/routes'); +mkdirSync(destination, { recursive: true }); +cpSync(build, destination, { recursive: true }); +cpSync(recordingPath, join(destination, 'demo-recording.json.gz')); +const app = readFileSync(join(build, 'index.html')); +const pages = [...staticPages(routes).filter((page) => page !== 'login'), ...items]; +// Hosts differ on whether `/runs` reads `runs.html` or `runs/index.html`, so both are written. +for (const page of pages) { + for (const file of [join(destination, `${page}.html`), join(destination, page, 'index.html')]) { + mkdirSync(dirname(file), { recursive: true }); + writeFileSync(file, app); + } +} +console.log(`Wrote the demo to ${relative(process.cwd(), destination) || '.'}: ${pages.length} pages.`); diff --git a/ui/scripts/record-demo.mjs b/ui/scripts/record-demo.mjs new file mode 100644 index 0000000000..3c5c8b2f15 --- /dev/null +++ b/ui/scripts/record-demo.mjs @@ -0,0 +1,88 @@ +// Records the static demo from a click-through of the UI on `sqb dev`. +// +// node scripts/record-demo.mjs --server [--replace FROM=TO]... ... +// +// The browser's HARs name the requests the pages make; each API read is asked again of the +// `sqb dev` server at --server, because a HAR often drops response bodies, and so are reads sent +// as POST, such as plan previews. `--replace` rewrites text everywhere, for example the project path. + +import { readFileSync, writeFileSync } from 'node:fs'; +import { gzipSync } from 'node:zlib'; + +const READ_POSTS = [/\/plans$/, /\/runs\/adhoc\/check$/]; +// The location's change feed is a live stream; a trigger's change history is an ordinary read. +const SKIPPED = [/^\/api\/auth\//, /\/targets\/[^/]+\/changes$/, /^\/api\/openapi\.json$/]; +const AUDIT = '/api/control/audit'; +const USAGE = 'usage: node scripts/record-demo.mjs --server [--replace FROM=TO]... ...'; + +const [outPath, ...options] = process.argv.slice(2); +let server = null; +const replacements = []; +const hars = []; +for (let i = 0; i < options.length; i++) { + const value = options[i + 1]; + if (options[i] === '--server' && value) server = options[++i].replace(/\/$/, ''); + else if (options[i] === '--replace' && value?.includes('=')) replacements.push([value.slice(0, value.indexOf('=')), options[++i].slice(value.indexOf('=') + 1)]); + else if (!options[i].startsWith('--')) hars.push(options[i]); + else server = undefined; +} +if (!outPath || !server || !hars.length) { + console.error(USAGE); + process.exit(2); +} +function keep(entry) { + kept.set(JSON.stringify([entry.method, entry.url, entry.body ?? null]), entry); +} +const scrub = (text) => replacements.reduce((out, [from, to]) => out.split(from).join(to), text); + +const kept = new Map(); +let auditLimit = null; +for (const { request, response } of hars.flatMap((har) => JSON.parse(readFileSync(har, 'utf8')).log.entries)) { + const url = new URL(request.url); + const path = url.pathname + url.search; + if (!url.pathname.startsWith('/api/') || SKIPPED.some((pattern) => pattern.test(url.pathname))) continue; + if (url.pathname === AUDIT) { + auditLimit = url.searchParams.get('limit'); + continue; + } + const body = request.postData?.text ? JSON.parse(request.postData.text) : undefined; + let answer; + if (request.method === 'GET') { + const fresh = await fetch(server + path); + answer = { status: fresh.status, contentType: fresh.headers.get('content-type') ?? 'application/json', text: await fresh.text() }; + } else if (READ_POSTS.some((pattern) => pattern.test(url.pathname))) { + // A read sent as POST is asked again too; a plan request already taken keeps the HAR's answer. + const fresh = await fetch(server + path, { method: 'POST', headers: { 'content-type': 'application/json' }, body: request.postData?.text }); + if (fresh.ok) answer = { status: fresh.status, contentType: fresh.headers.get('content-type') ?? 'application/json', text: await fresh.text() }; + else if (response.content?.text) answer = { status: response.status, contentType: response.content.mimeType || 'application/json', text: response.content.text }; + else continue; + } else continue; + if (answer.status >= 500) throw new Error(`${server}${path} answered ${answer.status}`); + const entry = { + method: request.method, + url: scrub(path), + ...(body === undefined ? {} : { body: JSON.parse(scrub(JSON.stringify(body))) }), + ...answer, + text: scrub(answer.text) + }; + keep(entry); +} + +// The plan previews asked again above add audit entries, so the browser's later audit pages start +// at the wrong entry. The audit log is read last, following its pages from the newest entry. +if (auditLimit) { + for (let before = Number.MAX_SAFE_INTEGER; before; ) { + const path = `${AUDIT}?before=${before}&limit=${auditLimit}`; + const fresh = await fetch(server + path); + const text = await fresh.text(); + if (!fresh.ok) throw new Error(`${server}${path} answered ${fresh.status}`); + keep({ method: 'GET', url: path, status: fresh.status, contentType: fresh.headers.get('content-type') ?? 'application/json', text: scrub(text) }); + const page = JSON.parse(text); + before = page.length === Number(auditLimit) ? page[page.length - 1].storage_id : 0; + } +} + +const responses = [...kept.values()].sort((a, b) => a.url.localeCompare(b.url) || a.method.localeCompare(b.method)); +// Run output and statements make the recording several megabytes of text, so it is stored packed. +writeFileSync(outPath, gzipSync(`${JSON.stringify({ recordedAt: new Date().toISOString(), responses })}\n`, { level: 9 })); +console.log(`Recorded ${responses.length} responses to ${outPath}.`); diff --git a/ui/src/lib/api/client.ts b/ui/src/lib/api/client.ts index 9a87552387..4bc5c5caeb 100644 --- a/ui/src/lib/api/client.ts +++ b/ui/src/lib/api/client.ts @@ -28,6 +28,7 @@ import type { RunRowPage, RunRowQuery, RunStatement, + Snapshot, RunTabCounts, Scope, SnapshotChange, @@ -134,6 +135,8 @@ export interface ApiClient extends PlatformApi { listPlatformEvents(query: { activeOnly: boolean }): Promise; + /** The newest version deployed to the location; null before its first deploy. */ + getLatestSnapshot(scope: Scope): Promise; /** Null when the location has reported no recent sync. */ getWarehouseSync(scope: Scope): Promise; /** Asks the location to read SQLBuild's state back from the warehouse now. `connection.test` */ diff --git a/ui/src/lib/api/http-client.ts b/ui/src/lib/api/http-client.ts index e4ddf6b8ec..d0e54d123e 100644 --- a/ui/src/lib/api/http-client.ts +++ b/ui/src/lib/api/http-client.ts @@ -7,7 +7,7 @@ import { checkDetailOf, toCheck, toCheckEvidence, toCheckSql } from './http-chec import { findNode, nodeDetailOf, toCatalogNodes, nodeJobsOf, toMicrobatchCoverage, toNodeRuns, toColumnLineage } from './http-catalog'; import { auditMatches, toAuditEntry } from './http-audit'; import { toSnapshotChanges } from './http-changes'; -import { manualConnectionTest, toLocationDetail, toLocationSummary, toWarehouseConnection, warehouseSyncOf } from './http-locations'; +import { manualConnectionTest, snapshotOf, toLocationDetail, toLocationSummary, toWarehouseConnection, warehouseSyncOf } from './http-locations'; import { toAccount, toRoleAssignments, toServerSettings } from './http-people'; import { toMaintenanceHistory } from './http-maintenance'; import { overviewOf } from './http-overview'; @@ -17,7 +17,7 @@ import { jobDetailOf, runWindowOf, toJob, toTick, toTrigger, toTriggerStateEvent import { timelineOf } from './http-timeline'; import { RUN_WINDOW, groupRunNodes, parseRunCursor, planResultOf, queueEntriesOf, toAdhocCheck, runDetailOf, runRowPageOver, runsMatching, runTabCountsOf, toRunNode, toRuns, toRunEvent, toRunOutput, toRunStatements } from './http-runs'; import type { ChangeSubscription, ChangedChannel, ChangedSignal, ConnectionState } from './signals'; -import type { AuthMode, Bootstrap, Capability, Location, NavCounts, PlatformEvent, Scope, WarehouseSync } from './types'; +import type { AuthMode, Bootstrap, Capability, Location, NavCounts, PlatformEvent, Scope, Snapshot, WarehouseSync } from './types'; import type { Account, AuditEntry, AuditQuery, ConnectionTest, LocationDetail, ServerSettings, WarehouseConnection, LocationSummary, PlatformEventQuery, PlatformEventRecord, PlatformHealth, MaintenanceHistory, RoleAssignment, RoleAssignmentRequest, RoleDefinition, RoleDetail } from './types/platform'; import type { Acknowledgement, CatalogNode, CatalogNodeDetail, Check, CheckDetail, CheckEvidence, CheckSql, Overview, SnapshotChange, ColumnLineage } from './types/build'; import type { AdhocRunCheck, AdhocRunRequest, BulkRunResult, PlanResult, QueueEntry, Timeline, Job, JobDetail, JobLaunchOptions, RerunMode, SnapshotNode, Run, RunDetail, RunListQuery, RunRowPage, RunRowQuery, RunTabCounts, Trigger, TriggerDetail, TriggerState, RunEventPage, RunNodeError, RunOutputLine, RunStatement, StatementSql } from './types/operate'; @@ -79,6 +79,7 @@ const SUPPORTED: ReadonlySet = new Set([ 'platformHealth', 'listRoles', 'getRole', + 'getLatestSnapshot', 'getWarehouseSync', 'requestWarehouseSync', 'maintenanceHistory', @@ -251,14 +252,18 @@ export type HttpClientOptions = { baseUrl?: string; /** Called when the session is missing or rejected, before the request fails. */ onUnauthorized: () => void; + /** Answers the requests instead of the network; the static demo replays a recording through it. */ + fetch?: (request: Request) => Promise; + /** The change feed; by default the server's, followed with `EventSource`. */ + changes?: ChangeSubscription; }; /** * The server-backed client. Methods without an endpoint throw, and `supports` * reports them so the shell never shows a page it cannot load. */ -export function createHttpClient({ baseUrl = '', onUnauthorized }: HttpClientOptions): ApiClient { - const http = createFetch({ baseUrl, credentials: 'same-origin' }); +export function createHttpClient({ baseUrl = '', onUnauthorized, fetch: answer, changes }: HttpClientOptions): ApiClient { + const http = createFetch({ baseUrl, credentials: 'same-origin', fetch: answer }); // Locations whose bootstrap actions include view.sql, which may hold on only some nodes, and the // nodes there whose SQL was refused; each refused read is audited, so neither is asked again until reload. let sqlLocations = new Set(); @@ -303,7 +308,7 @@ export function createHttpClient({ baseUrl = '', onUnauthorized }: HttpClientOpt } const implemented = { supports: (method: keyof ApiClient) => SUPPORTED.has(method), - changes: new HttpChangeFeed(baseUrl, (scope) => feedAllowed(baseUrl, scope, onUnauthorized)), + changes: changes ?? new HttpChangeFeed(baseUrl, (scope) => feedAllowed(baseUrl, scope, onUnauthorized)), async bootstrap(): Promise { const { data, error, response } = await http.GET('/api/control/bootstrap'); if (response.status === 401) onUnauthorized(); @@ -543,6 +548,13 @@ export function createHttpClient({ baseUrl = '', onUnauthorized }: HttpClientOpt } return found.slice(0, limit); }, + async getLatestSnapshot(scope: Scope): Promise { + const { locationId, target } = parseLocationKey(scope.locationId); + const { data, error, response } = await http.GET('/api/control/locations/{location_id}/targets/{target}/snapshots', { params: { path: { location_id: locationId, target }, query: { limit: 1 } } }); + if (response.status === 401) onUnauthorized(); + if (!data) throw new ApiError(errorText(error, response), response.status); + return snapshotOf(data[0] ?? null); + }, async getWarehouseSync(scope: Scope): Promise { const { locationId, target } = parseLocationKey(scope.locationId); const { data, error, response } = await http.GET('/api/control/locations/{location_id}/targets/{target}/health', { params: { path: { location_id: locationId, target } } }); diff --git a/ui/src/lib/api/http-locations.ts b/ui/src/lib/api/http-locations.ts index 436cb4d6ad..483ca4b8ba 100644 --- a/ui/src/lib/api/http-locations.ts +++ b/ui/src/lib/api/http-locations.ts @@ -53,7 +53,7 @@ export function manualConnectionTest(record: Wire['LocationWorkRecord'] | null): }; } -function snapshotOf(published: Wire['PublishedSnapshot'] | null): Snapshot | null { +export function snapshotOf(published: Wire['PublishedSnapshot'] | null): Snapshot | null { return published ? { id: published.snapshot_id, diff --git a/ui/src/lib/api/index.ts b/ui/src/lib/api/index.ts index bc585191b3..8b6c78282c 100644 --- a/ui/src/lib/api/index.ts +++ b/ui/src/lib/api/index.ts @@ -1,6 +1,9 @@ import type { ApiClient } from './client'; import { createHttpClient } from './http-client'; import { MockApiClient, type MockOptions } from './mock/mock-client'; +import { replayFetch, type Recording } from './replay'; +import { LocalChangeBus } from './signals'; +import { to } from '$lib/paths'; import type { BuiltInRole } from './types'; export type { ApiClient } from './client'; @@ -13,12 +16,26 @@ const ROLES: readonly BuiltInRole[] = ['viewer', 'operator', 'developer', 'admin const EVENTS: readonly NonNullable[] = ['default', 'none', 'all']; const LIVE: readonly NonNullable[] = ['live', 'reconnecting', 'offline']; +/** The static demo (`VITE_SQB_DEMO=1`): the server client replaying a recording served beside the app. */ +export const DEMO = import.meta.env.VITE_SQB_DEMO === '1'; + +async function loadRecording(): Promise { + const response = await fetch(to('/demo-recording.json.gz')); + if (!response.ok) throw new Error(`The demo recording is missing (${response.status})`); + const bytes = new Uint8Array(await response.arrayBuffer()); + // A host that serves the file with Content-Encoding has already unpacked it. + const packed = bytes[0] === 0x1f && bytes[1] === 0x8b; + const body = packed ? new Response(new Blob([bytes]).stream().pipeThrough(new DecompressionStream('gzip'))) : new Response(bytes); + return body.json(); +} + /** * The client the app uses: the server, or the mock with `?mock` or `VITE_SQB_MOCK=1`. * With the mock, `?as=`, `?events=none|all` and `?live=reconnecting|offline` * adjust it for demos. */ export function createClient(search: URLSearchParams, onUnauthorized: () => void = () => {}): ApiClient { + if (DEMO) return createHttpClient({ onUnauthorized, fetch: replayFetch(loadRecording), changes: new LocalChangeBus() }); if (!search.has('mock') && import.meta.env.VITE_SQB_MOCK !== '1') return createHttpClient({ onUnauthorized }); const role = search.get('as'); const events = search.get('events'); diff --git a/ui/src/lib/api/mock/mock-client.ts b/ui/src/lib/api/mock/mock-client.ts index 5e3d7f4561..97c330805e 100644 --- a/ui/src/lib/api/mock/mock-client.ts +++ b/ui/src/lib/api/mock/mock-client.ts @@ -24,6 +24,7 @@ import type { RunOutputLine, RunStatement, Scope, + Snapshot, SnapshotNode, Timeline, Trigger, @@ -329,6 +330,10 @@ export class MockApiClient implements ApiClient { return this.#reply(query.activeOnly ? events.filter((e) => e.active) : events); } + async getLatestSnapshot(scope: Scope): Promise { + return this.#reply(this.#location(scope).snapshot ?? null); + } + async getWarehouseSync(scope: Scope): Promise { this.#location(scope); return this.#reply({ ...this.#sync(scope) }); diff --git a/ui/src/lib/api/replay.test.ts b/ui/src/lib/api/replay.test.ts new file mode 100644 index 0000000000..07b8f7a934 --- /dev/null +++ b/ui/src/lib/api/replay.test.ts @@ -0,0 +1,55 @@ +import { describe, expect, it } from 'vitest'; +import { READ_ONLY_MESSAGE, replayFetch, type Recording } from './replay'; + +const ok = (method: string, url: string, body: unknown, extra: object = {}) => ({ method, url, status: 200, contentType: 'application/json', text: JSON.stringify(body), ...extra }); + +const recording: Recording = { + recordedAt: '2026-11-13T12:00:00.000Z', + responses: [ + ok('GET', '/api/runs?limit=50', [{ id: 'newest', queued_at: '2026-11-13T11:00:00.000Z' }]), + ok('GET', '/api/runs?before_queued_at=2026-11-13T11:00:00.000Z&limit=50', [{ id: 'older', queued_at: '2026-11-12T09:30:00.000Z' }]), + ok('GET', '/api/stats?after_observed_at=2026-11-06T12:00:00.000Z&name=orders', { name: 'orders' }), + ok('POST', '/api/l/plans', { accepted: true }, { body: { request_id: 'recorded-plan', command: 'sqb build --select orders' } }), + ok('GET', '/api/l/plans/recorded-plan', { request_id: 'recorded-plan', models: 3 }) + ] +}; + +// Seventeen days after the recording, so recorded times move forward by that much. +const now = Date.parse('2026-11-30T12:00:00.000Z'); +const replay = () => replayFetch(async () => recording, () => now); +const get = (fetch: ReturnType, url: string) => fetch(new Request(`http://localhost${url}`)); +const post = (fetch: ReturnType, url: string, body: unknown) => + fetch(new Request(`http://localhost${url}`, { method: 'POST', body: JSON.stringify(body), headers: { 'content-type': 'application/json' } })); + +describe('demo replay', () => { + it('answers with the recorded body, its times moved so the recording ends now', async () => { + expect(await (await get(replay(), '/api/runs?limit=50')).json()).toEqual([{ id: 'newest', queued_at: '2026-11-30T11:00:00.000Z' }]); + }); + + it('pages with the moved times it handed out', async () => { + const response = await get(replay(), '/api/runs?limit=50&before_queued_at=2026-11-30T11:00:00.000Z'); + expect(await response.json()).toEqual([{ id: 'older', queued_at: '2026-11-29T09:30:00.000Z' }]); + }); + + it('ignores times worked out from the clock and page sizes, but not other query values', async () => { + const fetch = replay(); + expect((await get(fetch, '/api/stats?name=orders&after_observed_at=2026-11-23T13:05:00.000Z')).status).toBe(200); + expect(await (await get(fetch, '/api/runs?limit=1')).json()).toEqual([{ id: 'newest', queued_at: '2026-11-30T11:00:00.000Z' }]); + // Another node's statistics are not this node's. + expect((await get(fetch, '/api/stats?name=customers&after_observed_at=2026-11-23T13:05:00.000Z')).status).toBe(404); + expect(await (await get(fetch, '/api/nothing-recorded')).json()).toEqual({ message: 'This demo has no recording of this page.' }); + }); + + it('refuses every write with the read-only message', async () => { + const response = await post(replay(), '/api/control/runs', { run_id: 'x', job_key: 'nightly' }); + expect(response.status).toBe(403); + expect(await response.json()).toEqual({ message: READ_ONLY_MESSAGE }); + }); + + it('previews a recorded plan under the id the page chose', async () => { + const fetch = replay(); + expect((await post(fetch, '/api/l/plans', { request_id: 'fresh-id', command: 'sqb build --select customers' })).status).toBe(404); + expect((await post(fetch, '/api/l/plans', { request_id: 'fresh-id', command: 'sqb build --select orders' })).status).toBe(200); + expect(await (await get(fetch, '/api/l/plans/fresh-id')).json()).toEqual({ request_id: 'recorded-plan', models: 3 }); + }); +}); diff --git a/ui/src/lib/api/replay.ts b/ui/src/lib/api/replay.ts new file mode 100644 index 0000000000..845343f521 --- /dev/null +++ b/ui/src/lib/api/replay.ts @@ -0,0 +1,85 @@ +// The static demo's network: answers the server client's requests from a recording of a real +// server, moved forward in time so the newest recorded moment reads as now. Nothing is sent. + +/** One answered request, as `ui/scripts/record-demo.mjs` writes it. */ +export type RecordedResponse = { + method: string; + /** Path and query as requested, e.g. `/api/control/audit?limit=100`. */ + url: string; + /** The request body, for the few reads sent as POST. */ + body?: unknown; + status: number; + contentType: string; + text: string; +}; + +export type Recording = { recordedAt: string; responses: RecordedResponse[] }; + +/** POSTs that only read, such as a plan preview; every other write is refused. */ +const READ_POSTS = [/\/plans$/, /\/runs\/adhoc\/check$/]; + +export const READ_ONLY_MESSAGE = 'This demo is read-only. Run sqb dev on your own project to start runs and change settings.'; + +const ISO = /\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?Z/g; +const isTime = (value: string) => /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?Z$/.test(value); + +const shiftTimes = (text: string, by: number) => text.replace(ISO, (iso) => new Date(Date.parse(iso) + by).toISOString()); + +/** A body without its caller-chosen ids, so a replayed POST matches the recorded one. */ +function withoutIds(body: unknown): string { + if (!body || typeof body !== 'object') return JSON.stringify(body ?? null); + return JSON.stringify(Object.fromEntries(Object.entries(body).filter(([key]) => !key.endsWith('_id')).sort(([a], [b]) => a.localeCompare(b)))); +} + +const sortedQuery = (url: URL) => [...url.searchParams].sort(([a, b], [c, d]) => a.localeCompare(c) || b.localeCompare(d)); + +const json = (status: number, body: unknown) => new Response(JSON.stringify(body), { status, headers: { 'content-type': 'application/json' } }); + +/** + * A `fetch` for the server client that replays `load()`'s recording. A GET matches its path and + * query; failing that, the same non-time query values; failing that, the same values apart from + * page size. Anything else, such as a node the recording doesn't hold, has no recording. + * A read sent as POST matches its path and body apart from ids, and later reads of the ids it + * chose are answered for the recorded ones. + */ +export function replayFetch(load: () => Promise, now: () => number = Date.now): (request: Request) => Promise { + let recording: Promise<{ shift: number; responses: (RecordedResponse & { parsed: URL })[] }> | null = null; + const ids = new Map(); + return async (request) => { + recording ??= load().then((r) => ({ + shift: now() - Date.parse(r.recordedAt), + responses: r.responses.map((response) => ({ ...response, parsed: new URL(response.url, 'http://demo') })) + })); + const { shift, responses } = await recording; + const url = new URL(request.url, 'http://demo'); + let path = url.pathname; + for (const [asked, recorded] of ids) path = path.replace(asked, recorded); + const answer = (found: RecordedResponse) => + new Response(found.status === 204 ? null : shiftTimes(found.text, shift), { status: found.status, headers: { 'content-type': found.contentType } }); + + if (request.method !== 'GET') { + if (!READ_POSTS.some((pattern) => pattern.test(path))) return json(403, { message: READ_ONLY_MESSAGE }); + const body: unknown = await request.json().catch(() => null); + const found = responses.find((r) => r.method === request.method && r.parsed.pathname === path && withoutIds(r.body) === withoutIds(body)); + if (!found) return json(404, { message: 'This demo has no recording of that request; try one of the suggested commands.' }); + for (const [key, value] of Object.entries(body ?? {})) { + const recorded = (found.body as Record | undefined)?.[key]; + if (key.endsWith('_id') && typeof value === 'string' && typeof recorded === 'string') ids.set(value, recorded); + } + return answer(found); + } + + // Query times were moved forward with the data, so move them back before comparing. + const query = sortedQuery(url).map(([key, value]): [string, string] => [key, isTime(value) ? shiftTimes(value, -shift) : value]); + // Page sizes may differ from the recording's, but every other value names what is asked for: + // a different node_key is a different item and must not borrow another item's answer. + const timeless = (q: [string, string][]) => JSON.stringify(q.filter(([, value]) => !isTime(value))); + const sizeless = (q: [string, string][]) => JSON.stringify(q.filter(([key, value]) => key !== 'limit' && !isTime(value))); + const gets = responses.filter((r) => r.method === 'GET' && r.parsed.pathname === path); + const found = + gets.find((r) => JSON.stringify(sortedQuery(r.parsed)) === JSON.stringify(query)) ?? + gets.find((r) => timeless(sortedQuery(r.parsed)) === timeless(query)) ?? + gets.find((r) => sizeless(sortedQuery(r.parsed)) === sizeless(query)); + return found ? answer(found) : json(404, { message: 'This demo has no recording of this page.' }); + }; +} diff --git a/ui/src/lib/components/admin/role-cell.svelte b/ui/src/lib/components/admin/role-cell.svelte index 6ecd7de8c4..7d622b4406 100644 --- a/ui/src/lib/components/admin/role-cell.svelte +++ b/ui/src/lib/components/admin/role-cell.svelte @@ -31,7 +31,7 @@
    {#each shown as a (a.id)}
  • - e.stopPropagation()}>{a.role} + {a.role} {scopeWords(a.scope.locationId, locations)} {#if a.stale}gives nothing{/if} diff --git a/ui/src/lib/components/app-sidebar.svelte b/ui/src/lib/components/app-sidebar.svelte index eab51d4eba..14313ed73f 100644 --- a/ui/src/lib/components/app-sidebar.svelte +++ b/ui/src/lib/components/app-sidebar.svelte @@ -39,7 +39,7 @@ } -