@lownoise-studio/rendershield 0.3.1 → 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.
- package/CHANGELOG.md +58 -32
- package/CONTRIBUTING.md +41 -0
- package/README.md +209 -144
- package/SECURITY.md +25 -0
- package/dist/cli.d.ts +3 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +27 -19
- package/dist/cli.js.map +1 -1
- package/dist/commands/build.d.ts +2 -0
- package/dist/commands/build.d.ts.map +1 -0
- package/dist/commands/build.js +10 -9
- package/dist/commands/build.js.map +1 -1
- package/dist/commands/init.d.ts +2 -0
- package/dist/commands/init.d.ts.map +1 -0
- package/dist/commands/verify.d.ts +19 -0
- package/dist/commands/verify.d.ts.map +1 -0
- package/dist/commands/verify.js +57 -64
- package/dist/commands/verify.js.map +1 -1
- package/dist/core/generateRobots.d.ts +3 -0
- package/dist/core/generateRobots.d.ts.map +1 -0
- package/dist/core/generateSitemap.d.ts +3 -0
- package/dist/core/generateSitemap.d.ts.map +1 -0
- package/dist/core/generateWorker.d.ts +3 -0
- package/dist/core/generateWorker.d.ts.map +1 -0
- package/dist/core/generateWorker.js +75 -75
- package/dist/core/loadConfig.d.ts +3 -0
- package/dist/core/loadConfig.d.ts.map +1 -0
- package/dist/core/loadConfig.js +68 -25
- package/dist/core/loadConfig.js.map +1 -1
- package/dist/core/loadMarkdown.d.ts +3 -0
- package/dist/core/loadMarkdown.d.ts.map +1 -0
- package/dist/core/loadMarkdown.js +4 -3
- package/dist/core/loadMarkdown.js.map +1 -1
- package/dist/core/renderHtml.d.ts +3 -0
- package/dist/core/renderHtml.d.ts.map +1 -0
- package/dist/core/renderHtml.js +25 -10
- package/dist/core/renderHtml.js.map +1 -1
- package/dist/core/validateOutput.d.ts +28 -0
- package/dist/core/validateOutput.d.ts.map +1 -0
- package/dist/core/validateOutput.js +7 -1
- package/dist/core/validateOutput.js.map +1 -1
- package/dist/errors.d.ts +14 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +24 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +19 -0
- package/dist/index.js.map +1 -0
- package/dist/types.d.ts +53 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +1 -1
- package/dist/types.js.map +1 -1
- package/docs/deploy-cloudflare.md +40 -14
- package/package.json +24 -2
- package/src/cli.ts +86 -75
- package/src/commands/build.ts +199 -185
- package/src/commands/verify.ts +266 -236
- package/src/core/generateWorker.ts +97 -97
- package/src/core/loadConfig.ts +261 -173
- package/src/core/loadMarkdown.ts +9 -3
- package/src/core/renderHtml.ts +36 -12
- package/src/core/validateOutput.ts +335 -328
- package/src/errors.ts +48 -0
- package/src/index.ts +40 -0
- package/src/types.ts +4 -1
package/src/commands/build.ts
CHANGED
|
@@ -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
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
//
|
|
40
|
-
|
|
41
|
-
const
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
if (
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
}
|
|
103
|
-
const
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
const
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
const
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
await fs.writeFile(
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
//
|
|
176
|
-
if (cfg.
|
|
177
|
-
const
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
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
|
+
}
|