Skip to content

Commit 5fa4a23

Browse files
author
福晋
committed
refactor: harden site integration boundaries
1 parent e26f20f commit 5fa4a23

47 files changed

Lines changed: 1532 additions & 966 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.github/workflows/ci.yml‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
pull_request:
6+
7+
jobs:
8+
verify:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- uses: actions/checkout@v4
12+
- uses: pnpm/action-setup@v4
13+
with:
14+
version: 10.34.5
15+
- uses: actions/setup-node@v4
16+
with:
17+
node-version: 22
18+
cache: pnpm
19+
- run: pnpm install --frozen-lockfile
20+
- run: pnpm run build
21+
- run: pnpm run check:types
22+
- run: pnpm run check:theme
23+
- run: pnpm run test:unit
24+
- run: pnpm run test:integration

‎LICENSE‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2026 AntV
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

‎README.md‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ export default defineConfig({
9999

100100
`theme.tokens` is a single CSS custom-property map applied to `:root`. It keeps product branding and theme customization in the consuming repository without exposing Astro internals or adding a package-owned color-mode switch.
101101

102+
`site.origin` must be a bare HTTP(S) origin without credentials, a path, query, or fragment. Navigation, footer, home-action, and version links accept relative URLs plus HTTP(S), `mailto:`, and `tel:` schemes; unsafe schemes and control characters are rejected during configuration.
103+
102104
Use normal Astro commands:
103105

104106
```json
@@ -112,7 +114,7 @@ Use normal Astro commands:
112114
}
113115
```
114116

115-
The public `defineConfig` facade installs the integration and configures the bundled theme, static output, sitemap, public assets, Demo ESM entries, and Pagefind indexing. It no longer replaces Astro's `srcDir`: consumer pages, components, middleware, and content configuration remain in the standard Astro project tree.
117+
The public `defineConfig` facade installs the integration and configures the bundled theme, sitemap, Demo ESM entries, and Pagefind indexing. `@antv/site` supports Astro's static output only and fails during configuration when non-static output is requested. It does not replace Astro's `srcDir` or `publicDir`: consumer pages, components, middleware, content configuration, and static assets remain in the standard Astro project tree.
116118

117119
Advanced Astro configurations can compose the same integration directly:
118120

@@ -128,7 +130,7 @@ export default defineConfig({
128130
});
129131
```
130132

131-
Use this standard Astro composition whenever the site needs Astro-level options such as `base`, adapters, redirects, or additional integrations. `antvSite()` returns only the package-owned core integration; advanced consumers explicitly install and enable `@astrojs/mdx` and `@astrojs/sitemap` when needed. The package `defineConfig` facade enables both by default. Internal theme links, canonical URLs, Demo iframes, QA navigation, static assets, and search assets all honor Astro's `base` value.
133+
Use this standard Astro composition whenever the site needs Astro-level options such as `base`, redirects, or additional integrations. Server adapters are intentionally unsupported because generated content, Demo runners, QA routes, and Pagefind indexing are owned as a static-site pipeline. `antvSite()` returns only the package-owned core integration; advanced consumers explicitly install and enable `@astrojs/mdx` and `@astrojs/sitemap` when needed. The package `defineConfig` facade enables both by default. Internal theme links, canonical URLs, Demo iframes, QA navigation, static assets, and search assets all honor Astro's `base` value.
132134

133135
When qa is configured, each locale receives a Result route at /{locale}/result/ by default and the shared home page renders the package-owned QA entry. `qa.defaultStack` selects the initial G2, S2, G6, F2, X6, or L7 stack. Change `qa.path` to use another route. The platform owns the shared Sive service endpoints and protocol. The QA entry owns stack selection, authentication, session creation, history, and Result navigation; the Result page owns streaming responses, follow-up questions, Markdown rendering, code highlighting, and optional visualization previews.
134136

@@ -219,6 +221,8 @@ A minimal metadata file is:
219221
}
220222
```
221223

224+
`meta.json` is validated before routes are generated. Demo filenames must reference JavaScript or TypeScript modules inside their own `demo` directory, and duplicate route keys are rejected. Group Markdown frontmatter is parsed strictly; malformed or unclosed frontmatter fails the build with the source path instead of being repaired heuristically.
225+
222226
Relative imports in Demo source resolve from the original source file. Vite emits standard ESM chunks and automatically shares common chunks where beneficial.
223227

224228
## Demo trust boundary
@@ -248,7 +252,7 @@ Unknown keys are rejected. Removed features therefore fail fast instead of being
248252

249253
## Package boundary
250254

251-
Consumers import `defineConfig`, `antvSite`, and configuration types from the package root. The `@antv/site/content` subpath exports the Astro Content Loader and schema. The shared QA entry is available from `@antv/site/qa-entry`; lower-level browser-side QA session and history helpers remain available from `@antv/site/qa`. The package build copies the consumer-compiled Astro theme source into `dist/theme`, and npm publishes only `dist`; `src/theme` is a repository source directory, not a separate package path. Astro remains the peer build engine; Vite, Pagefind, and the sitemap integration are implementation dependencies.
255+
Consumers import `defineConfig`, `antvSite`, and configuration types from the package root. The `@antv/site/content` subpath exports the Astro Content Loader and schema. The shared QA entry is available from `@antv/site/qa-entry`; lower-level browser-side QA session and history helpers are compiled to JavaScript and published from `@antv/site/qa`. The package build emits NodeNext-compatible ESM directly, without a regex-based post-build import rewrite, then copies the consumer-compiled Astro theme source into `dist/theme`. npm publishes only `dist`; `src/theme` is a repository source directory, not a separate package path. Astro remains the peer build engine; Vite, Pagefind, and the sitemap integration are implementation dependencies.
252256

253257
## Repository demo
254258

‎demos/basic-site/site.config.mjs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export const siteConfig = {
1212
navigation: [
1313
{ text: { zh: "文档", en: "Docs" }, href: "/guide/" },
1414
{ text: { zh: "示例", en: "Examples" }, href: "/examples/" },
15+
{ text: { zh: "邮件", en: "Email" }, href: "mailto:team@example.com" },
1516
],
1617
search: {
1718
aliases: { esm: ["module"] },
@@ -29,6 +30,7 @@ export const siteConfig = {
2930
description: { zh: "使用 Astro 构建。", en: "Built with Astro." },
3031
actions: [
3132
{ text: { zh: "查看示例", en: "View examples" }, href: "/examples/" },
33+
{ text: { zh: "电话联系", en: "Call us" }, href: "tel:+861012345678" },
3234
],
3335
},
3436
slots: {

‎package.json‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22
"name": "@antv/site",
33
"version": "0.1.0",
44
"description": "Astro integration for static open-source documentation and examples",
5+
"license": "MIT",
6+
"repository": {
7+
"type": "git",
8+
"url": "https://github.com/antvis/site.git"
9+
},
10+
"homepage": "https://github.com/antvis/site#readme",
11+
"bugs": {
12+
"url": "https://github.com/antvis/site/issues"
13+
},
514
"type": "module",
615
"packageManager": "pnpm@10.34.5",
716
"exports": {
@@ -14,8 +23,8 @@
1423
"import": "./dist/content.js"
1524
},
1625
"./qa": {
17-
"types": "./dist/theme/features/qa/index.ts",
18-
"import": "./dist/theme/features/qa/index.ts"
26+
"types": "./dist/qa-browser.d.ts",
27+
"import": "./dist/qa-browser.js"
1928
},
2029
"./qa-entry": {
2130
"types": "./dist/theme/components/QaEntry.astro",
@@ -31,7 +40,11 @@
3140
},
3241
"scripts": {
3342
"clean": "node -e \"require('node:fs').rmSync('dist',{recursive:true,force:true})\"",
34-
"build": "pnpm run clean && tsc -p tsconfig.build.json && node scripts/rewrite-imports.mjs && node scripts/copy-theme.mjs",
43+
"build": "pnpm run clean && tsc -p tsconfig.build.json && node scripts/copy-theme.mjs",
44+
"check:types": "tsc -p tsconfig.build.json --noEmit",
45+
"check:theme": "astro check --root demos/basic-site",
46+
"test:unit": "node --test test/config.test.mjs test/content.test.mjs test/package-boundary.test.mjs test/qa-adapters.test.mjs test/qa-protocol.test.mjs test/integration-config.test.mjs",
47+
"test:integration": "node --test test/integration-build.test.mjs test/development-search.test.mjs",
3548
"test": "pnpm run build && node --test test/*.test.mjs",
3649
"check": "pnpm run test",
3750
"prepack": "pnpm run build"

‎scripts/rewrite-imports.mjs‎

Lines changed: 0 additions & 48 deletions
This file was deleted.

‎src/compiler/config.ts‎

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
qaServiceEndpoints,
1515
type QaPreviewProduct,
1616
type QaProduct,
17-
} from "../qa";
17+
} from "../qa.js";
1818

1919
export type SiteLocale = "zh" | "en";
2020

@@ -202,13 +202,54 @@ const localizedTextSchema = z.strictObject({
202202
en: z.string(),
203203
});
204204

205+
const httpUrlSchema = z.url().refine(
206+
(value) => {
207+
const protocol = new URL(value).protocol;
208+
return protocol === "http:" || protocol === "https:";
209+
},
210+
"URL must use HTTP or HTTPS.",
211+
);
212+
213+
const siteOriginSchema = httpUrlSchema.refine(
214+
(value) => {
215+
const url = new URL(value);
216+
return (
217+
!url.username &&
218+
!url.password &&
219+
url.pathname === "/" &&
220+
!url.search &&
221+
!url.hash
222+
);
223+
},
224+
"Site origin must not include credentials, a path, a query, or a hash.",
225+
);
226+
227+
const supportedLinkProtocols = new Set(["http:", "https:", "mailto:", "tel:"]);
228+
const linkProtocolPattern = /^[a-z][a-z\d+.-]*:/i;
229+
const safeHrefSchema = z
230+
.string()
231+
.min(1)
232+
.refine((value) => !/\s/.test(value), "Links must not contain whitespace.")
233+
.refine(
234+
(value) => !/[\u0000-\u001f\u007f]/.test(value),
235+
"Links must not contain control characters.",
236+
)
237+
.refine((value) => {
238+
if (!linkProtocolPattern.test(value)) return true;
239+
try {
240+
return supportedLinkProtocols.has(new URL(value).protocol);
241+
} catch {
242+
return false;
243+
}
244+
}, "Links must be relative or use HTTP, HTTPS, mailto, or tel.");
245+
205246
const sidebarItemSchema = localizedTextSchema.extend({
206247
order: z.number().optional(),
207248
});
208249

209250
const linkSchema = z.strictObject({
210251
text: localizedTextSchema,
211-
href: z.string().min(1),
252+
href: safeHrefSchema,
212253
});
213254

214255
const footerGroupSchema = z.strictObject({
@@ -269,8 +310,8 @@ const configSchema = z
269310
.strictObject({
270311
site: z.strictObject({
271312
title: z.string().min(1),
272-
origin: z.url(),
273-
repository: z.url(),
313+
origin: siteOriginSchema,
314+
repository: httpUrlSchema,
274315
description: localizedTextSchema,
275316
defaultLocale: z.enum(["zh", "en"]).default("zh"),
276317
locales: z
@@ -302,7 +343,7 @@ const configSchema = z
302343
sidebar: z.record(z.string(), sidebarItemSchema).default({}),
303344
}),
304345
navigation: z.array(linkSchema).default([]),
305-
versions: z.record(z.string(), z.string()).default({}),
346+
versions: z.record(z.string(), safeHrefSchema).default({}),
306347
search: z
307348
.strictObject({
308349
enabled: z.boolean().default(true),

0 commit comments

Comments
 (0)