Files
webstudio/packages/cli/src/filesystem-content-source.test.ts
T
Oleg Isonen d78e770d82 perf: Keep article indexing out of publish API requests (#6427)
Ref #6416

The publish workflow was doing too much work in one short-lived API
request. This moves content indexing into the build runner and keeps
article bodies out of hosted/SSR sync.

## What changed

- The build-bundle API can return the lightweight project bundle without
preparing a full content index.
- The CLI downloads required assets directly from storage and builds the
index locally for SSG. Asset downloads remain bounded to 10 concurrent
requests.
- Hosted/SSR sync skips Markdown and MDX article downloads and does not
compile an article index. It still downloads regular assets needed by
the build.
- Markdown body queries use stored frontmatter and fetch only the
requested article at runtime. MDX still needs source downloads and
compilation for SSG.
- Public MCP and content-engine docs explain the Markdown/MDX tradeoff.

This removes article parsing and article-body transfer from the tRPC
request. SSG still downloads article sources from storage and compiles
MDX in the build runner. Hosted runtime article reads remain on demand.

## Checklist

- [x] Keep the tRPC bundle request limited to project metadata and
necessary records.
- [x] Prepare SSG content indexes in the build runner; keep hosted
article bodies out of sync.
- [x] Cover no-body query preparation, direct asset reads, and
SSG-versus-hosted sync behavior with regression tests.
- [x] Update public CLI, MCP, and content-engine guidance.
- [x] Regenerate and verify fixtures.
- [x] Run workspace tests, typechecks, lint, formatting,
package-boundary, generated API/docs, and fixture-size checks.
- [ ] Verify the deployed publish flow has comfortable margin below the
hosted API timeout.

## Verification

- `pnpm -r --workspace-concurrency=1 test`: passed, including all 3,082
Builder tests and 1,150 CLI tests.
- `pnpm -r typecheck`, `pnpm lint`, `pnpm check:package-boundaries`,
`pnpm check:generated-api`, `pnpm check:generated-docs`, `pnpm
check:fixture-sizes`, and `pnpm exec oxfmt --check`: passed.
- The new hosted article-download regression test was confirmed to fail
with the old behavior and pass after the fix.
- A deployed end-to-end publish timing check is still needed, so this PR
remains draft.
2026-09-26 11:56:37 +02:00

304 lines
9.1 KiB
TypeScript

import { mkdir, mkdtemp, rm, symlink, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import {
createContentCompilationPlan,
createLiteralContentCompilationQuery,
} from "@webstudio-is/content-engine";
import {
compileContentSource,
readBoundedBytes,
} from "@webstudio-is/content-engine/compiler";
import type { Asset, AssetFolders } from "@webstudio-is/sdk";
import { afterEach, describe, expect, test, vi } from "vitest";
import { createFileSystemContentSource } from "./filesystem-content-source";
const temporaryDirectories: string[] = [];
const createTemporaryDirectory = async () => {
const directory = await mkdtemp(join(tmpdir(), "webstudio-content-source-"));
temporaryDirectories.push(directory);
return directory;
};
afterEach(async () => {
await Promise.all(
temporaryDirectories
.splice(0)
.map((directory) => rm(directory, { recursive: true, force: true }))
);
});
const createAsset = (name: string): Asset => ({
id: "post",
projectId: "project",
type: "file",
name,
filename: "hello",
size: 0,
description: "Post description",
folderId: "blog",
createdAt: "2026-07-27T00:00:00.000Z",
format: "md",
meta: {},
});
const folders: AssetFolders = new Map([
[
"blog",
{
id: "blog",
projectId: "project",
name: "Blog",
createdAt: "2026-07-27T00:00:00.000Z",
},
],
]);
const plan = createContentCompilationPlan([
createLiteralContentCompilationQuery({
id: "posts",
query: {
where: {
all: [
{
field: ["properties", "slug"],
operator: "eq",
value: "hello",
},
],
},
sort: [],
limit: 10,
offset: 0,
output: { mode: "all", includeMetadata: true },
content: { mode: "full" },
},
}),
]);
describe("filesystem content source", () => {
test("uses the shared parser and hydrates matching local content", async () => {
const directory = await createTemporaryDirectory();
const name = "hello_hash.md";
const content = "---\ntitle: Hello\nslug: hello\n---\n# Hello\nBody";
await writeFile(join(directory, name), content);
const snapshot = await createFileSystemContentSource({
projectId: "project",
assets: [createAsset(name)],
folders,
assetsDirectory: directory,
}).openSnapshot();
await expect(snapshot.loadEntries(plan)).resolves.toMatchObject([
{
document: {
path: "Blog/hello.md",
description: "Post description",
properties: { title: "Hello", slug: "hello" },
},
content,
},
]);
await expect(snapshot.loadEntries()).resolves.toMatchObject([
{
document: {
properties: { title: "Hello", slug: "hello" },
excerpt: "Hello Body",
},
},
]);
const [documentSource] = (await snapshot.loadDocumentSources?.()) ?? [];
expect(documentSource?.id).toBe("post");
const documentText = new TextDecoder().decode(
await readBoundedBytes(documentSource?.source ?? "", content.length)
);
expect(documentText).toBe(content);
await expect(snapshot.isCurrent()).resolves.toBe(true);
});
test("reuses prepared local frontmatter when compiling deferred article queries", async () => {
const directory = await createTemporaryDirectory();
const name = "hello_hash.md";
await writeFile(join(directory, name), "---\ntitle: Hello\n---\n# Body\n");
const snapshot = await createFileSystemContentSource({
projectId: "project",
assets: [createAsset(name)],
folders,
assetsDirectory: directory,
}).openSnapshot();
const deferredPlan = createContentCompilationPlan([
createLiteralContentCompilationQuery({
id: "post",
query: {
where: { all: [] },
sort: [],
limit: 1,
offset: 0,
output: { mode: "base", includeMetadata: false },
content: { mode: "markdown-body-ref" },
},
}),
]);
const readGraphSource = vi.fn(() => {
throw new Error(
"Prepared frontmatter must not require a second source read"
);
});
const { artifact } = await compileContentSource({
projectId: "project",
plan: deferredPlan,
source: {
async openSnapshot() {
return {
...snapshot,
async loadDocumentSources() {
return [
{
id: "post",
source: { [Symbol.asyncIterator]: readGraphSource },
},
];
},
};
},
},
});
expect(readGraphSource).not.toHaveBeenCalled();
expect(artifact.documents).toHaveLength(1);
expect(artifact.contents).toBeUndefined();
});
test("invalidates a snapshot when a local file is replaced", async () => {
const directory = await createTemporaryDirectory();
const name = "hello_hash.md";
const path = join(directory, name);
await writeFile(path, "first");
const snapshot = await createFileSystemContentSource({
projectId: "project",
assets: [createAsset(name)],
folders,
assetsDirectory: directory,
}).openSnapshot();
await writeFile(path, "replacement");
await expect(snapshot.isCurrent()).resolves.toBe(false);
await expect(snapshot.loadEntries(plan)).rejects.toThrow(
"Content source file changed"
);
});
test("invalidates a snapshot when a local file is deleted", async () => {
const directory = await createTemporaryDirectory();
const name = "hello_hash.md";
const path = join(directory, name);
await writeFile(path, "content");
const snapshot = await createFileSystemContentSource({
projectId: "project",
assets: [createAsset(name)],
folders,
assetsDirectory: directory,
}).openSnapshot();
await rm(path);
await expect(snapshot.isCurrent()).resolves.toBe(false);
});
test("rejects selected content that cannot be embedded as UTF-8", async () => {
const directory = await createTemporaryDirectory();
const name = "invalid_hash.md";
await writeFile(join(directory, name), new Uint8Array([0xff]));
const snapshot = await createFileSystemContentSource({
projectId: "project",
assets: [createAsset(name)],
folders,
assetsDirectory: directory,
}).openSnapshot();
const fullContentPlan = createContentCompilationPlan([
createLiteralContentCompilationQuery({
id: "all-posts",
query: {
where: { all: [] },
sort: [],
limit: 1,
offset: 0,
output: { mode: "base", includeMetadata: true },
content: { mode: "full" },
},
}),
]);
await expect(snapshot.loadEntries(fullContentPlan)).rejects.toThrow(
"Selected content source file is not valid UTF-8"
);
});
test("rejects symlinks outside the assets directory", async () => {
const parent = await createTemporaryDirectory();
const directory = join(parent, "assets");
const outside = join(parent, "outside.md");
const name = "outside_hash.md";
await mkdir(directory);
await writeFile(outside, "private");
await symlink(outside, join(directory, name));
await expect(
createFileSystemContentSource({
projectId: "project",
assets: [createAsset(name)],
folders,
assetsDirectory: directory,
}).openSnapshot()
).rejects.toThrow("outside the assets directory");
});
test("invalidates a snapshot when the assets directory symlink changes", async () => {
const parent = await createTemporaryDirectory();
const firstDirectory = join(parent, "first");
const secondDirectory = join(parent, "second");
const linkedDirectory = join(parent, "assets");
const name = "hello_hash.md";
await mkdir(firstDirectory);
await mkdir(secondDirectory);
await writeFile(join(firstDirectory, name), "first");
await writeFile(join(secondDirectory, name), "second");
await symlink(firstDirectory, linkedDirectory);
const snapshot = await createFileSystemContentSource({
projectId: "project",
assets: [createAsset(name)],
folders,
assetsDirectory: linkedDirectory,
}).openSnapshot();
await rm(linkedDirectory);
await symlink(secondDirectory, linkedDirectory);
await expect(snapshot.isCurrent()).resolves.toBe(false);
});
test("invalidates a snapshot when an asset symlink changes", async () => {
const directory = await createTemporaryDirectory();
const first = join(directory, "first.md");
const second = join(directory, "second.md");
const name = "hello_hash.md";
const linkedFile = join(directory, name);
await writeFile(first, "first");
await writeFile(second, "second");
await symlink(first, linkedFile);
const snapshot = await createFileSystemContentSource({
projectId: "project",
assets: [createAsset(name)],
folders,
assetsDirectory: directory,
}).openSnapshot();
await rm(linkedFile);
await symlink(second, linkedFile);
await expect(snapshot.isCurrent()).resolves.toBe(false);
});
});