@unbase-app/pages 0.0.0-stage → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/node.mjs ADDED
@@ -0,0 +1,183 @@
1
+ // @ts-check
2
+ // ============================================================================
3
+ // @unbase-app/pages — what the build plugins do on disk: read the app's
4
+ // files for core.mjs, plan the drafts of a production build, and finish its
5
+ // output (page lists cleaned, the leak check).
6
+ // ============================================================================
7
+
8
+ import { existsSync, readdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
9
+ import { join, relative, sep } from "node:path";
10
+ import { brotliDecompressSync, gunzipSync } from "node:zlib";
11
+ import * as core from "./core.mjs";
12
+
13
+ const SKIP_DIRS = new Set(["node_modules", "dist", "build", "out", ".git", ".astro", ".next", ".vercel", ".output", ".netlify", ".turbo", ".cache"]);
14
+ const SOURCE = /\.(astro|tsx|jsx|ts|js|mjs|cjs|mts|cts|vue|svelte|md|mdx|html|json)$/;
15
+ /** What a host serves or runs as text, compressed or not: where a page's words could end up. */
16
+ const SERVED_TEXT = /\.(html?|js|mjs|cjs|css|json|xml|txt|map|webmanifest|svg|md)(\.(gz|br))?$/;
17
+
18
+ const ENTITIES = { amp: "&", lt: "<", gt: ">", quot: '"', apos: "'", nbsp: " " };
19
+
20
+ /**
21
+ * A served file's text as a reader would get it: decompressed (.gz, .br), HTML entities and
22
+ * JavaScript escapes decoded, whitespace as one space. The leak check matches on this, so a
23
+ * sentence a minifier or an HTML writer encoded is still found.
24
+ */
25
+ export function readServed(file) {
26
+ let bytes = readFileSync(file);
27
+ if (file.endsWith(".gz")) bytes = gunzipSync(bytes);
28
+ else if (file.endsWith(".br")) bytes = brotliDecompressSync(bytes);
29
+ return decodeText(bytes.toString("utf8"));
30
+ }
31
+
32
+ export function decodeText(text) {
33
+ return text
34
+ .replace(/\\u\{([0-9a-fA-F]+)\}|\\u([0-9a-fA-F]{4})|\\x([0-9a-fA-F]{2})/g, (_, a, b, c) => String.fromCodePoint(parseInt(a ?? b ?? c, 16)))
35
+ .replace(/\\(["'`/])/g, "$1")
36
+ .replace(/\\[nrt]/g, " ")
37
+ .replace(/&#x([0-9a-fA-F]+);/g, (_, h) => String.fromCodePoint(parseInt(h, 16)))
38
+ .replace(/&#(\d+);/g, (_, d) => String.fromCodePoint(Number(d)))
39
+ .replace(/&([a-z]+);/g, (m, n) => ENTITIES[n] ?? m)
40
+ .replace(/\s+/g, " ");
41
+ }
42
+
43
+ /** The app's source files, relative to `root`, read once when asked. */
44
+ export function appFiles(root) {
45
+ /** @type {string[]} */
46
+ const paths = [];
47
+ const walk = (dir) => {
48
+ for (const e of readdirSync(dir, { withFileTypes: true })) {
49
+ if (e.isDirectory()) {
50
+ if (SKIP_DIRS.has(e.name) || (e.name.startsWith(".") && e.name !== ".schemas")) continue;
51
+ walk(join(dir, e.name));
52
+ } else if (SOURCE.test(e.name)) {
53
+ paths.push(relative(root, join(dir, e.name)).split(sep).join("/"));
54
+ }
55
+ }
56
+ };
57
+ walk(root);
58
+ paths.sort();
59
+ const cache = new Map();
60
+ const text = (p) => {
61
+ if (!cache.has(p)) {
62
+ let t = null;
63
+ try {
64
+ t = readFileSync(join(root, p), "utf8");
65
+ } catch {}
66
+ cache.set(p, t);
67
+ }
68
+ return cache.get(p);
69
+ };
70
+ return { paths, text };
71
+ }
72
+
73
+ const WHY = {
74
+ invalid: (d) => `${core.PAGE_FILE} is not valid: ${d}`,
75
+ framework: () => "this framework has no draft support in the kit yet",
76
+ home: () => "Home cannot be a draft",
77
+ template: (d) => `it is made by a dynamic route (${d}); keep it off the site where its data lives`,
78
+ missing: () => `the code makes no page at this address; remove it from ${core.PAGE_FILE}`,
79
+ nested: () => "its route has child routes",
80
+ unparsed: () => "its route is not one component the kit can trace (element={<Page />})",
81
+ shared: (d) => `its file or its content is also used by ${d}`,
82
+ content_unknown: () => "it reads content the kit cannot attribute to it",
83
+ generated_media: (d) => `the site draws pages into images (${d}), which the kit cannot check`,
84
+ };
85
+
86
+ /** The refusal of a production build, one line per page. */
87
+ export function refusal(failed) {
88
+ const lines = failed.map((f) => ` ${f.path}: ${(WHY[f.reason] ?? (() => f.reason))(f.detail ?? "")}`);
89
+ return `[unbase-pages] This production build would publish draft pages it cannot keep out, so it stops:\n${lines.join("\n")}\nMark them as ready in Unbase, or fix the reason, and build again.`;
90
+ }
91
+
92
+ /**
93
+ * What a build keeps out, or null when nothing is a draft or this is not a production build.
94
+ * Throws the refusal when a draft cannot be kept out (fail closed).
95
+ * @param {{ root: string, framework: string, command: "build" | "dev", log?: (line: string) => void }} input
96
+ */
97
+ export function planBuild({ root, framework, command, log = () => {} }) {
98
+ const ctx = core.deployContext({ env: process.env, command });
99
+ log(`${ctx.context} build (${ctx.source})`);
100
+ if (ctx.context !== "production") return null;
101
+ const files = appFiles(root);
102
+ const pageFile = core.parsePageFile(files.text(core.PAGE_FILE));
103
+ const plan = core.draftPlan({ framework, ...files, pageFile });
104
+ if (plan.failed.length) throw new Error(refusal(plan.failed));
105
+ if (!plan.checks.length) return null;
106
+ /** @type {any[]} */
107
+ const checks = plan.checks;
108
+ const drafts = checks.map((c) => ({
109
+ path: c.path,
110
+ file: join(root, c.page.file),
111
+ router: c.page.router ? join(root, c.page.router.file) : null,
112
+ content: c.content,
113
+ prints: core.leakPrints(c, files),
114
+ }));
115
+ log(`keeping out: ${drafts.map((d) => d.path).join(", ")}`);
116
+ return { root, files, drafts };
117
+ }
118
+
119
+ /** Every file a host would serve as text, under `dir`. */
120
+ function servedFiles(dir) {
121
+ /** @type {string[]} */
122
+ const out = [];
123
+ const walk = (d) => {
124
+ for (const e of readdirSync(d, { withFileTypes: true })) {
125
+ const p = join(d, e.name);
126
+ if (e.isDirectory()) walk(p);
127
+ else if (SERVED_TEXT.test(e.name)) out.push(p);
128
+ }
129
+ };
130
+ if (existsSync(dir)) walk(dir);
131
+ return out.sort();
132
+ }
133
+
134
+ /**
135
+ * After a production build: the drafts' pages out of every list in `dir` (sitemaps,
136
+ * comment-modus/pages.json, the content map), then the leak check over everything served. Throws
137
+ * when a draft's text is still there.
138
+ * @param {string} dir
139
+ * @param {{ drafts: { path: string, content: string[], prints: string[] }[] }} plan
140
+ */
141
+ export function finishOutput(dir, plan, log = (_line) => {}, scan = [dir]) {
142
+ const paths = plan.drafts.map((d) => d.path);
143
+ const owned = plan.drafts.flatMap((d) => d.content);
144
+ for (const f of servedFiles(dir)) {
145
+ const name = relative(dir, f).split(sep).join("/");
146
+ let next = null;
147
+ if (/\.(gz|br)$/.test(name)) continue;
148
+ const text = readFileSync(f, "utf8");
149
+ if (/(^|\/)sitemap[^/]*\.xml$/.test(name)) next = core.withoutInSitemap(text, paths);
150
+ else if (name === "comment-modus/pages.json") next = core.withoutInPageList(text, paths);
151
+ else if (name === "comment-modus/content-map.json") next = core.withoutInContentMap(text, owned);
152
+ if (next !== null && next !== text) {
153
+ writeFileSync(f, next);
154
+ log(`cleaned ${name}`);
155
+ }
156
+ }
157
+ const leaks = [];
158
+ for (const root of scan) {
159
+ for (const f of servedFiles(root)) {
160
+ const text = readServed(f);
161
+ for (const d of plan.drafts) for (const p of core.leaksIn(text, d.prints)) leaks.push(` ${d.path}: "${p.slice(0, 60)}…" in ${relative(root, f)}`);
162
+ }
163
+ }
164
+ if (leaks.length) throw new Error(`[unbase-pages] Draft text is still in the production output, so the build stops:\n${leaks.join("\n")}`);
165
+ }
166
+
167
+ /**
168
+ * A built page's HTML, wherever the build's format put it. Anything else written right under the
169
+ * draft's address (an image made for it, a feed) is output the kit does not know: the build stops.
170
+ */
171
+ export function removePageHtml(dir, path) {
172
+ const gone = [];
173
+ for (const f of [join(dir, path, "index.html"), join(dir, `${path}.html`), join(dir, `${path}.html.gz`), join(dir, `${path}.html.br`), join(dir, path, "index.html.gz"), join(dir, path, "index.html.br")]) {
174
+ if (existsSync(f)) {
175
+ rmSync(f);
176
+ gone.push(f);
177
+ }
178
+ }
179
+ const folder = join(dir, path);
180
+ const left = existsSync(folder) ? readdirSync(folder, { withFileTypes: true }).filter((e) => e.isFile()).map((e) => e.name) : [];
181
+ if (left.length) throw new Error(`[unbase-pages] ${path} is a draft, and the build wrote ${left.join(", ")} under its address, which the kit cannot check, so the build stops.`);
182
+ return gone;
183
+ }
package/package.json CHANGED
@@ -1,6 +1,44 @@
1
1
  {
2
2
  "name": "@unbase-app/pages",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.3.0",
4
+ "description": "Keeps draft pages out of a site's production build: an Astro integration and a Vite plugin, for Unbase.",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/unbase-app/kit.git",
10
+ "directory": "pages"
11
+ },
12
+ "homepage": "https://github.com/unbase-app/kit/tree/main/pages#readme",
13
+ "bugs": "https://github.com/unbase-app/kit/issues",
14
+ "keywords": [
15
+ "astro",
16
+ "astro-integration",
17
+ "vite-plugin",
18
+ "react-router",
19
+ "drafts",
20
+ "unbase"
21
+ ],
22
+ "publishConfig": {
23
+ "access": "public"
24
+ },
25
+ "exports": {
26
+ ".": "./core.mjs",
27
+ "./astro": {
28
+ "types": "./astro.d.mts",
29
+ "default": "./astro.mjs"
30
+ },
31
+ "./vite": {
32
+ "types": "./vite.d.mts",
33
+ "default": "./vite.mjs"
34
+ },
35
+ "./package.json": "./package.json"
36
+ },
37
+ "files": ["core.mjs", "node.mjs", "astro.mjs", "astro.d.mts", "vite.mjs", "vite.d.mts", "README.md", "LICENSE"],
38
+ "engines": {
39
+ "node": ">=18.17"
40
+ },
41
+ "scripts": {
42
+ "test": "node --test test/*.test.mjs"
43
+ }
44
+ }
package/vite.d.mts ADDED
@@ -0,0 +1,8 @@
1
+ import type { Plugin } from "vite";
2
+
3
+ /**
4
+ * The Vite plugin for a React Router app: in a production build every draft in unbase.pages.json
5
+ * is kept out, or the build stops.
6
+ */
7
+ export declare function unbasePages(): Plugin;
8
+ export default unbasePages;
package/vite.mjs ADDED
@@ -0,0 +1,66 @@
1
+ // @ts-check
2
+ // ============================================================================
3
+ // @unbase-app/pages/vite — the Vite plugin for a React Router app (what
4
+ // Lovable makes). In a production build every draft in unbase.pages.json is
5
+ // kept out: its <Route … /> is removed from the router before JSX is compiled,
6
+ // and its page module becomes an empty one, so neither its route nor its text
7
+ // reaches a chunk; its address falls to the app's own "*" (Not Found). Then
8
+ // the output is checked for the page's text. Previews and the dev server show
9
+ // drafts as they are. Fails closed: a draft it cannot keep out stops the build.
10
+ //
11
+ // // vite.config.ts
12
+ // import { unbasePages } from "@unbase-app/pages/vite";
13
+ // plugins: [react(), unbasePages()]
14
+ // ============================================================================
15
+
16
+ import { resolve } from "node:path";
17
+ import * as core from "./core.mjs";
18
+ import { finishOutput, planBuild } from "./node.mjs";
19
+
20
+ const clean = (id) => id.replace(/\?.*$/, "");
21
+
22
+ /** @returns {import("vite").Plugin} */
23
+ export function unbasePages() {
24
+ /** @type {ReturnType<typeof planBuild>} */
25
+ let plan = null;
26
+ let outDir = "";
27
+ const removed = new Set();
28
+ /** @type {(line: string) => void} */
29
+ let log = () => {};
30
+
31
+ return {
32
+ name: "unbase-pages",
33
+ enforce: "pre",
34
+ configResolved(config) {
35
+ log = (line) => config.logger.info(`[unbase-pages] ${line}`);
36
+ outDir = resolve(config.root, config.build.outDir);
37
+ plan = planBuild({ root: config.root, framework: "vite-react", command: config.command === "serve" ? "dev" : "build", log });
38
+ },
39
+ load(id) {
40
+ if (!plan) return null;
41
+ const file = clean(id);
42
+ if (plan.drafts.some((d) => d.file === file)) return "export default function UnbaseDraftPage() {\n return null;\n}\n";
43
+ return null;
44
+ },
45
+ transform(code, id) {
46
+ if (!plan) return null;
47
+ const file = clean(id);
48
+ const drop = plan.drafts.filter((d) => d.router === file).map((d) => d.path);
49
+ if (!drop.length) return null;
50
+ const rel = file.slice(plan.root.length + 1).split("\\").join("/");
51
+ const out = core.removeRoutes({ file: rel, code, paths: plan.files.paths, text: plan.files.text, drop });
52
+ for (const p of out.removed) removed.add(p);
53
+ return { code: out.code, map: null };
54
+ },
55
+ buildEnd(error) {
56
+ if (!plan || error) return;
57
+ const left = plan.drafts.filter((d) => !removed.has(d.path)).map((d) => d.path);
58
+ if (left.length) this.error(`[unbase-pages] The route of ${left.join(", ")} was not found in the router while building, so the build stops.`);
59
+ },
60
+ closeBundle() {
61
+ if (plan) finishOutput(outDir, plan, log);
62
+ },
63
+ };
64
+ }
65
+
66
+ export default unbasePages;