@lownoise-studio/rendershield 0.3.0 → 1.0.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.
Files changed (70) hide show
  1. package/CHANGELOG.md +58 -0
  2. package/CONTRIBUTING.md +41 -0
  3. package/README.md +209 -138
  4. package/SECURITY.md +25 -0
  5. package/dist/cli.d.ts +3 -0
  6. package/dist/cli.d.ts.map +1 -0
  7. package/dist/cli.js +43 -21
  8. package/dist/cli.js.map +1 -1
  9. package/dist/commands/build.d.ts +2 -0
  10. package/dist/commands/build.d.ts.map +1 -0
  11. package/dist/commands/build.js +10 -9
  12. package/dist/commands/build.js.map +1 -1
  13. package/dist/commands/init.d.ts +2 -0
  14. package/dist/commands/init.d.ts.map +1 -0
  15. package/dist/commands/init.js +7 -7
  16. package/dist/commands/init.js.map +1 -1
  17. package/dist/commands/verify.d.ts +19 -0
  18. package/dist/commands/verify.d.ts.map +1 -0
  19. package/dist/commands/verify.js +60 -64
  20. package/dist/commands/verify.js.map +1 -1
  21. package/dist/core/generateRobots.d.ts +3 -0
  22. package/dist/core/generateRobots.d.ts.map +1 -0
  23. package/dist/core/generateSitemap.d.ts +3 -0
  24. package/dist/core/generateSitemap.d.ts.map +1 -0
  25. package/dist/core/generateWorker.d.ts +3 -0
  26. package/dist/core/generateWorker.d.ts.map +1 -0
  27. package/dist/core/generateWorker.js +85 -75
  28. package/dist/core/generateWorker.js.map +1 -1
  29. package/dist/core/loadConfig.d.ts +3 -0
  30. package/dist/core/loadConfig.d.ts.map +1 -0
  31. package/dist/core/loadConfig.js +99 -40
  32. package/dist/core/loadConfig.js.map +1 -1
  33. package/dist/core/loadMarkdown.d.ts +3 -0
  34. package/dist/core/loadMarkdown.d.ts.map +1 -0
  35. package/dist/core/loadMarkdown.js +4 -3
  36. package/dist/core/loadMarkdown.js.map +1 -1
  37. package/dist/core/renderHtml.d.ts +3 -0
  38. package/dist/core/renderHtml.d.ts.map +1 -0
  39. package/dist/core/renderHtml.js +30 -11
  40. package/dist/core/renderHtml.js.map +1 -1
  41. package/dist/core/validateOutput.d.ts +28 -0
  42. package/dist/core/validateOutput.d.ts.map +1 -0
  43. package/dist/core/validateOutput.js +9 -3
  44. package/dist/core/validateOutput.js.map +1 -1
  45. package/dist/errors.d.ts +14 -0
  46. package/dist/errors.d.ts.map +1 -0
  47. package/dist/errors.js +24 -0
  48. package/dist/errors.js.map +1 -0
  49. package/dist/index.d.ts +20 -0
  50. package/dist/index.d.ts.map +1 -0
  51. package/dist/index.js +19 -0
  52. package/dist/index.js.map +1 -0
  53. package/dist/types.d.ts +53 -0
  54. package/dist/types.d.ts.map +1 -0
  55. package/dist/types.js +1 -1
  56. package/dist/types.js.map +1 -1
  57. package/docs/deploy-cloudflare.md +40 -14
  58. package/package.json +30 -4
  59. package/src/cli.ts +86 -60
  60. package/src/commands/build.ts +199 -185
  61. package/src/commands/init.ts +7 -7
  62. package/src/commands/verify.ts +266 -233
  63. package/src/core/generateWorker.ts +97 -87
  64. package/src/core/loadConfig.ts +261 -142
  65. package/src/core/loadMarkdown.ts +11 -5
  66. package/src/core/renderHtml.ts +41 -13
  67. package/src/core/validateOutput.ts +335 -325
  68. package/src/errors.ts +48 -0
  69. package/src/index.ts +40 -0
  70. package/src/types.ts +4 -1
@@ -1,185 +1,199 @@
1
- import fs from "fs-extra";
2
- import path from "node:path";
3
- import { loadConfig } from "../core/loadConfig.js";
4
- import { loadAllMarkdownDocs } from "../core/loadMarkdown.js";
5
- import { renderPageHtml } from "../core/renderHtml.js";
6
- import { generateSitemapXml } from "../core/generateSitemap.js";
7
- import { generateRobotsTxt } from "../core/generateRobots.js";
8
- import { generateWorkerJs } from "../core/generateWorker.js";
9
- import { validatePrerenderHtml } from "../core/validateOutput.js";
10
-
11
- function routeToOutDir(outDirAbs: string, routePath: string): string {
12
- // /blog/slug -> outDir/blog/slug/index.html
13
- const clean = routePath.replace(/^\//, "");
14
- return path.join(outDirAbs, clean);
15
- }
16
-
17
- /**
18
- * Validates output path before any destructive operation (fs.remove).
19
- * Pass: outDir is a subdirectory inside project root; no symlink escape.
20
- * Fail: "/", "C:\", "..", "../", or outDir (or any of its existing parents) resolving outside project.
21
- * Policy: strict build hard-fails before any delete attempt.
22
- * Segment-safe: uses path.relative(root, out) only; no prefix startsWith.
23
- */
24
- async function validateOutputPath(outDir: string, cwd: string): Promise<void> {
25
- const cwdAbs = path.resolve(cwd);
26
- let cwdReal: string;
27
- try {
28
- cwdReal = await fs.realpath(cwdAbs);
29
- } catch (err: unknown) {
30
- const msg = err instanceof Error ? err.message : String(err);
31
- throw new Error(`Cannot resolve project root: ${cwdAbs}. ${msg}`);
32
- }
33
-
34
- const outDirAbs = path.resolve(cwdAbs, outDir);
35
- // Order: realpath → normalize → then case-fold for comparisons
36
- const normalizedCwd = path.normalize(cwdReal);
37
- const normalizedOut = path.normalize(outDirAbs);
38
-
39
- // Segment-safe: use path.relative only (no prefix startsWith on full path). Cross-platform: relative()
40
- // rarely returns absolute on Windows; startsWith("..") and path.isAbsolute(rel) cover escapes.
41
- const relative = path.relative(normalizedCwd, normalizedOut);
42
- if (relative.startsWith("..") || relative === ".." || path.isAbsolute(relative)) {
43
- throw new Error(
44
- `Output directory "${outDir}" resolves outside project root. Use a relative path within the project.`
45
- );
46
- }
47
-
48
- // Reject: root filesystem (/, C:\, C:/) — early reject; real safety is "inside root" above
49
- const rootPaths = ["/", "c:\\", "c:/"];
50
- const outLower = normalizedOut.toLowerCase();
51
- if (rootPaths.includes(outLower)) {
52
- throw new Error(
53
- `Output directory "${outDir}" resolves to root filesystem. This is not allowed for safety.`
54
- );
55
- }
56
-
57
- // Reject: output dir equals project root (would delete entire project). Case-fold after normalize.
58
- const cwdLower = normalizedCwd.toLowerCase();
59
- if (outLower === cwdLower) {
60
- throw new Error(
61
- `Output directory "${outDir}" cannot be the project root. Use a subdirectory (e.g. dist-prerender).`
62
- );
63
- }
64
-
65
- if (await fs.pathExists(outDirAbs)) {
66
- // Path exists: resolve symlinks and ensure real path is inside root
67
- let outDirReal: string;
68
- try {
69
- outDirReal = await fs.realpath(outDirAbs);
70
- } catch {
71
- outDirReal = outDirAbs;
72
- }
73
- const outDirRealNorm = path.normalize(outDirReal);
74
- const relativeReal = path.relative(normalizedCwd, outDirRealNorm);
75
- if (relativeReal.startsWith("..") || relativeReal === ".." || path.isAbsolute(relativeReal)) {
76
- throw new Error(
77
- `Output directory "${outDir}" resolves (via symlink) outside project root. Use a path that does not escape the project.`
78
- );
79
- }
80
- } else {
81
- // Path does not exist: walk up to nearest existing parent, realpath it, ensure it stays inside root.
82
- // Guards e.g. outDir "dist-link/prerender-new" where dist-link is a symlink to /.
83
- // If we never find an existing parent (or the only one is root), we must reject — do not treat as "fine."
84
- let current = normalizedOut;
85
- const rootDir = path.normalize(path.parse(normalizedCwd).root);
86
- let foundParentInsideRoot = false;
87
- while (current) {
88
- if (await fs.pathExists(current)) {
89
- let parentReal: string;
90
- try {
91
- parentReal = await fs.realpath(current);
92
- } catch {
93
- parentReal = current;
94
- }
95
- const parentRealNorm = path.normalize(parentReal);
96
- // Nearest existing parent is filesystem root → outside project, reject
97
- const parentLower = parentRealNorm.toLowerCase();
98
- if (rootPaths.includes(parentLower)) {
99
- throw new Error(
100
- `Output directory "${outDir}" has a parent that resolves to filesystem root. Use a path inside the project.`
101
- );
102
- }
103
- const relParent = path.relative(normalizedCwd, parentRealNorm);
104
- if (relParent.startsWith("..") || relParent === ".." || path.isAbsolute(relParent)) {
105
- throw new Error(
106
- `Output directory "${outDir}" has a parent that resolves (via symlink) outside project root. Use a path that does not escape the project.`
107
- );
108
- }
109
- foundParentInsideRoot = true;
110
- break;
111
- }
112
- const parent = path.dirname(current);
113
- if (parent === current) break;
114
- current = parent;
115
- }
116
- if (!foundParentInsideRoot) {
117
- // No existing parent found before hitting dirname loop stop (shouldn't happen on a normal FS)
118
- throw new Error(
119
- `Output directory "${outDir}" could not be validated: no existing parent path found. Use a path inside the project.`
120
- );
121
- }
122
- }
123
- }
124
-
125
- export async function cmdBuild(cwd = process.cwd()) {
126
- const cfg = await loadConfig(cwd);
127
-
128
- // Validate output path before any destructive operations
129
- await validateOutputPath(cfg.output.outDir, cwd);
130
-
131
- const outDirAbs = path.join(cwd, cfg.output.outDir);
132
-
133
- // Clean output (boring + deterministic)
134
- await fs.remove(outDirAbs);
135
- await fs.ensureDir(outDirAbs);
136
-
137
- const docs = await loadAllMarkdownDocs(cfg, cwd);
138
-
139
- if (docs.length === 0) {
140
- throw new Error("No markdown documents found. Check content paths/patterns.");
141
- }
142
-
143
- // Generate pages (validate BEFORE writing)
144
- for (const doc of docs) {
145
- const pageDir = routeToOutDir(outDirAbs, doc.routePath);
146
- await fs.ensureDir(pageDir);
147
-
148
- const outFile = path.join(pageDir, "index.html");
149
- const html = renderPageHtml(cfg, doc);
150
-
151
- validatePrerenderHtml({
152
- html,
153
- outFile,
154
- routePath: doc.routePath,
155
- sourcePath: doc.sourcePath,
156
- });
157
-
158
- await fs.writeFile(outFile, html, "utf8");
159
- }
160
-
161
- // sitemap.xml
162
- if (cfg.sitemap.enabled) {
163
- const sitemapXml = generateSitemapXml(cfg, docs);
164
- const sitemapPath = path.join(outDirAbs, cfg.sitemap.path.replace(/^\//, ""));
165
- await fs.writeFile(sitemapPath, sitemapXml, "utf8");
166
- }
167
-
168
- // robots.txt
169
- if (cfg.robots.enabled) {
170
- const robotsTxt = generateRobotsTxt(cfg);
171
- const robotsPath = path.join(outDirAbs, cfg.robots.path.replace(/^\//, ""));
172
- await fs.writeFile(robotsPath, robotsTxt, "utf8");
173
- }
174
-
175
- // worker.js
176
- if (cfg.worker.enabled) {
177
- const workerJs = generateWorkerJs(cfg);
178
- await fs.writeFile(path.join(outDirAbs, "worker.js"), workerJs, "utf8");
179
- }
180
-
181
- console.log(`Built ${docs.length} pages into ${cfg.output.outDir}/`);
182
- console.log(
183
- `Output includes: ${cfg.sitemap.enabled ? "sitemap.xml " : ""}${cfg.robots.enabled ? "robots.txt " : ""}${cfg.worker.enabled ? "worker.js" : ""}`
184
- );
185
- }
1
+ import fs from "fs-extra";
2
+ import path from "node:path";
3
+ import { loadConfig } from "../core/loadConfig.js";
4
+ import { loadAllMarkdownDocs } from "../core/loadMarkdown.js";
5
+ import { renderPageHtml } from "../core/renderHtml.js";
6
+ import { generateSitemapXml } from "../core/generateSitemap.js";
7
+ import { generateRobotsTxt } from "../core/generateRobots.js";
8
+ import { generateWorkerJs } from "../core/generateWorker.js";
9
+ import { validatePrerenderHtml } from "../core/validateOutput.js";
10
+ import { renderShieldError } from "../errors.js";
11
+
12
+ function routeToOutDir(outDirAbs: string, routePath: string): string {
13
+ // /blog/slug -> outDir/blog/slug/index.html
14
+ const clean = routePath.replace(/^\//, "");
15
+ return path.join(outDirAbs, clean);
16
+ }
17
+
18
+ /**
19
+ * Validates output path before any destructive operation (fs.remove).
20
+ * Pass: outDir is a subdirectory inside project root; no symlink escape.
21
+ * Fail: "/", "C:\", "..", "../", or outDir (or any of its existing parents) resolving outside project.
22
+ * Policy: strict build hard-fails before any delete attempt.
23
+ * Segment-safe: uses path.relative(root, out) only; no prefix startsWith.
24
+ */
25
+ async function validateOutputPath(outDir: string, cwd: string): Promise<void> {
26
+ const cwdAbs = path.resolve(cwd);
27
+ let cwdReal: string;
28
+ try {
29
+ cwdReal = await fs.realpath(cwdAbs);
30
+ } catch (err: unknown) {
31
+ const msg = err instanceof Error ? err.message : String(err);
32
+ throw renderShieldError(
33
+ "OUTPUT_PATH_UNSAFE",
34
+ `Cannot resolve project root: ${cwdAbs}. ${msg}`
35
+ );
36
+ }
37
+
38
+ const outDirAbs = path.resolve(cwdAbs, outDir);
39
+ // Order: realpath normalize then case-fold for comparisons
40
+ const normalizedCwd = path.normalize(cwdReal);
41
+ const normalizedOut = path.normalize(outDirAbs);
42
+
43
+ // Segment-safe: use path.relative only (no prefix startsWith on full path). Cross-platform: relative()
44
+ // rarely returns absolute on Windows; startsWith("..") and path.isAbsolute(rel) cover escapes.
45
+ const relative = path.relative(normalizedCwd, normalizedOut);
46
+ if (relative.startsWith("..") || relative === ".." || path.isAbsolute(relative)) {
47
+ throw renderShieldError(
48
+ "OUTPUT_PATH_UNSAFE",
49
+ `Output directory "${outDir}" resolves outside project root. Use a relative path within the project.`
50
+ );
51
+ }
52
+
53
+ // Reject: root filesystem (/, C:\, C:/) early reject; real safety is "inside root" above
54
+ const rootPaths = ["/", "c:\\", "c:/"];
55
+ const outLower = normalizedOut.toLowerCase();
56
+ if (rootPaths.includes(outLower)) {
57
+ throw renderShieldError(
58
+ "OUTPUT_PATH_UNSAFE",
59
+ `Output directory "${outDir}" resolves to root filesystem. This is not allowed for safety.`
60
+ );
61
+ }
62
+
63
+ // Reject: output dir equals project root (would delete entire project). Case-fold after normalize.
64
+ const cwdLower = normalizedCwd.toLowerCase();
65
+ if (outLower === cwdLower) {
66
+ throw renderShieldError(
67
+ "OUTPUT_PATH_UNSAFE",
68
+ `Output directory "${outDir}" cannot be the project root. Use a subdirectory (e.g. dist-prerender).`
69
+ );
70
+ }
71
+
72
+ if (await fs.pathExists(outDirAbs)) {
73
+ // Path exists: resolve symlinks and ensure real path is inside root
74
+ let outDirReal: string;
75
+ try {
76
+ outDirReal = await fs.realpath(outDirAbs);
77
+ } catch {
78
+ outDirReal = outDirAbs;
79
+ }
80
+ const outDirRealNorm = path.normalize(outDirReal);
81
+ const relativeReal = path.relative(normalizedCwd, outDirRealNorm);
82
+ if (relativeReal.startsWith("..") || relativeReal === ".." || path.isAbsolute(relativeReal)) {
83
+ throw renderShieldError(
84
+ "OUTPUT_PATH_UNSAFE",
85
+ `Output directory "${outDir}" resolves (via symlink) outside project root. Use a path that does not escape the project.`
86
+ );
87
+ }
88
+ } else {
89
+ // Path does not exist: walk up to nearest existing parent, realpath it, ensure it stays inside root.
90
+ // Guards e.g. outDir "dist-link/prerender-new" where dist-link is a symlink to /.
91
+ // If we never find an existing parent (or the only one is root), we must reject — do not treat as "fine."
92
+ let current = normalizedOut;
93
+ const rootDir = path.normalize(path.parse(normalizedCwd).root);
94
+ let foundParentInsideRoot = false;
95
+ while (current) {
96
+ if (await fs.pathExists(current)) {
97
+ let parentReal: string;
98
+ try {
99
+ parentReal = await fs.realpath(current);
100
+ } catch {
101
+ parentReal = current;
102
+ }
103
+ const parentRealNorm = path.normalize(parentReal);
104
+ // Nearest existing parent is filesystem root outside project, reject
105
+ const parentLower = parentRealNorm.toLowerCase();
106
+ if (rootPaths.includes(parentLower)) {
107
+ throw renderShieldError(
108
+ "OUTPUT_PATH_UNSAFE",
109
+ `Output directory "${outDir}" has a parent that resolves to filesystem root. Use a path inside the project.`
110
+ );
111
+ }
112
+ const relParent = path.relative(normalizedCwd, parentRealNorm);
113
+ if (relParent.startsWith("..") || relParent === ".." || path.isAbsolute(relParent)) {
114
+ throw renderShieldError(
115
+ "OUTPUT_PATH_UNSAFE",
116
+ `Output directory "${outDir}" has a parent that resolves (via symlink) outside project root. Use a path that does not escape the project.`
117
+ );
118
+ }
119
+ foundParentInsideRoot = true;
120
+ break;
121
+ }
122
+ const parent = path.dirname(current);
123
+ if (parent === current) break;
124
+ current = parent;
125
+ }
126
+ if (!foundParentInsideRoot) {
127
+ // No existing parent found before hitting dirname loop stop (shouldn't happen on a normal FS)
128
+ throw renderShieldError(
129
+ "OUTPUT_PATH_UNSAFE",
130
+ `Output directory "${outDir}" could not be validated: no existing parent path found. Use a path inside the project.`
131
+ );
132
+ }
133
+ }
134
+ }
135
+
136
+ export async function cmdBuild(cwd = process.cwd()) {
137
+ const cfg = await loadConfig(cwd);
138
+
139
+ // Validate output path before any destructive operations
140
+ await validateOutputPath(cfg.output.outDir, cwd);
141
+
142
+ const outDirAbs = path.join(cwd, cfg.output.outDir);
143
+
144
+ // Clean output (boring + deterministic)
145
+ await fs.remove(outDirAbs);
146
+ await fs.ensureDir(outDirAbs);
147
+
148
+ const docs = await loadAllMarkdownDocs(cfg, cwd);
149
+
150
+ if (docs.length === 0) {
151
+ throw renderShieldError(
152
+ "BUILD_FAILED",
153
+ "No markdown documents found. Check content paths/patterns."
154
+ );
155
+ }
156
+
157
+ // Generate pages (validate BEFORE writing)
158
+ for (const doc of docs) {
159
+ const pageDir = routeToOutDir(outDirAbs, doc.routePath);
160
+ await fs.ensureDir(pageDir);
161
+
162
+ const outFile = path.join(pageDir, "index.html");
163
+ const html = renderPageHtml(cfg, doc);
164
+
165
+ validatePrerenderHtml({
166
+ html,
167
+ outFile,
168
+ routePath: doc.routePath,
169
+ sourcePath: doc.sourcePath,
170
+ });
171
+
172
+ await fs.writeFile(outFile, html, "utf8");
173
+ }
174
+
175
+ // sitemap.xml
176
+ if (cfg.sitemap.enabled) {
177
+ const sitemapXml = generateSitemapXml(cfg, docs);
178
+ const sitemapPath = path.join(outDirAbs, cfg.sitemap.path.replace(/^\//, ""));
179
+ await fs.writeFile(sitemapPath, sitemapXml, "utf8");
180
+ }
181
+
182
+ // robots.txt
183
+ if (cfg.robots.enabled) {
184
+ const robotsTxt = generateRobotsTxt(cfg);
185
+ const robotsPath = path.join(outDirAbs, cfg.robots.path.replace(/^\//, ""));
186
+ await fs.writeFile(robotsPath, robotsTxt, "utf8");
187
+ }
188
+
189
+ // worker.js
190
+ if (cfg.worker.enabled) {
191
+ const workerJs = generateWorkerJs(cfg);
192
+ await fs.writeFile(path.join(outDirAbs, "worker.js"), workerJs, "utf8");
193
+ }
194
+
195
+ console.log(`Built ${docs.length} pages into ${cfg.output.outDir}/`);
196
+ console.log(
197
+ `Output includes: ${cfg.sitemap.enabled ? "sitemap.xml " : ""}${cfg.robots.enabled ? "robots.txt " : ""}${cfg.worker.enabled ? "worker.js" : ""}`
198
+ );
199
+ }
@@ -16,9 +16,9 @@ const DEFAULT_CONFIG = {
16
16
  baseDir: "content",
17
17
  collections: [
18
18
  {
19
- name: "pages",
20
- pattern: "pages/**/*.md",
21
- routeBase: "/pages",
19
+ name: "blog",
20
+ pattern: "blog/**/*.md",
21
+ routeBase: "/blog",
22
22
  schemaType: "Article"
23
23
  }
24
24
  ]
@@ -39,7 +39,7 @@ const DEFAULT_CONFIG = {
39
39
  worker: {
40
40
  enabled: true,
41
41
  lovableOrigin: "https://YOUR_SITE.lovable.app",
42
- rewriteRouteBases: ["/pages/"],
42
+ rewriteRouteBases: ["/blog/"],
43
43
  botUserAgentPatterns: [
44
44
  "googlebot",
45
45
  "bingbot",
@@ -78,7 +78,7 @@ That's the whole trick.
78
78
 
79
79
  export async function cmdInit(cwd = process.cwd()) {
80
80
  const configPath = path.join(cwd, CONFIG_NAME);
81
- const contentDir = path.join(cwd, "content", "pages");
81
+ const contentDir = path.join(cwd, "content", "blog");
82
82
  const samplePath = path.join(contentDir, "hello-world.md");
83
83
 
84
84
  const configExists = await fs.pathExists(configPath);
@@ -98,7 +98,7 @@ export async function cmdInit(cwd = process.cwd()) {
98
98
  const sampleExists = await fs.pathExists(samplePath);
99
99
  if (!sampleExists) {
100
100
  await fs.writeFile(samplePath, SAMPLE_POST, "utf8");
101
- console.log(`Created sample content: content/pages/hello-world.md`);
101
+ console.log(`Created sample content: content/blog/hello-world.md`);
102
102
  } else {
103
103
  console.log(`Sample content already exists (leaving it alone)`);
104
104
  }
@@ -106,7 +106,7 @@ export async function cmdInit(cwd = process.cwd()) {
106
106
  console.log(`
107
107
  Next:
108
108
  1) Edit rendershield.config.json
109
- 2) Add content under content/pages/
109
+ 2) Add content under content/blog/
110
110
  3) Run: rendershield build
111
111
  `);
112
112
  }