@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
|
@@ -1,97 +1,97 @@
|
|
|
1
|
-
import { createRequire } from "node:module";
|
|
2
|
-
import { RenderShieldConfig } from "../types.js";
|
|
3
|
-
|
|
4
|
-
const require = createRequire(import.meta.url);
|
|
5
|
-
let VERSION = "0.0.0";
|
|
6
|
-
try {
|
|
7
|
-
const pkg = require("../../package.json") as { version?: string };
|
|
8
|
-
VERSION = pkg.version ?? "0.0.0";
|
|
9
|
-
} catch {
|
|
10
|
-
VERSION = "unknown";
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
function jsStringArray(arr: string[]): string {
|
|
14
|
-
return `[${arr.map((s) => JSON.stringify(s)).join(", ")}]`;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export function generateWorkerJs(cfg: RenderShieldConfig): string {
|
|
18
|
-
const patterns = cfg.worker.botUserAgentPatterns.map((p) => p.toLowerCase());
|
|
19
|
-
const rewriteBases = cfg.worker.rewriteRouteBases;
|
|
20
|
-
|
|
21
|
-
return `/**
|
|
22
|
-
* RenderShield Worker (generated) v${VERSION}
|
|
23
|
-
* Serves prerendered /index.html to bots on selected route bases.
|
|
24
|
-
*/
|
|
25
|
-
|
|
26
|
-
const BOT_SUBSTRINGS = ${jsStringArray(patterns)};
|
|
27
|
-
const REWRITE_BASES = ${jsStringArray(rewriteBases)};
|
|
28
|
-
|
|
29
|
-
function isBot(ua) {
|
|
30
|
-
const s = (ua || "").toLowerCase();
|
|
31
|
-
return BOT_SUBSTRINGS.some((sub) => s.includes(sub));
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
function shouldRewrite(pathname) {
|
|
35
|
-
return REWRITE_BASES.some((base) => pathname.startsWith(base));
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
function toIndexHtml(pathname) {
|
|
39
|
-
let p = pathname;
|
|
40
|
-
if (!p.endsWith("/")) p += "/";
|
|
41
|
-
return p + "index.html";
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
export default {
|
|
45
|
-
async fetch(request, env, ctx) {
|
|
46
|
-
const url = new URL(request.url);
|
|
47
|
-
const ua = request.headers.get("User-Agent") || "";
|
|
48
|
-
const bot = isBot(ua);
|
|
49
|
-
|
|
50
|
-
const isGetLike = request.method === "GET" || request.method === "HEAD";
|
|
51
|
-
const rewrite = bot && isGetLike && shouldRewrite(url.pathname);
|
|
52
|
-
|
|
53
|
-
try {
|
|
54
|
-
if (!rewrite) {
|
|
55
|
-
const passResp = await fetch(request);
|
|
56
|
-
const out = new Response(passResp.body, passResp);
|
|
57
|
-
out.headers.set("x-rendershield", "pass-through");
|
|
58
|
-
return out;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
const origin = ${JSON.stringify(cfg.worker.lovableOrigin)};
|
|
62
|
-
const finalPath = toIndexHtml(url.pathname);
|
|
63
|
-
const originUrl = origin + finalPath + url.search;
|
|
64
|
-
|
|
65
|
-
const headers = new Headers();
|
|
66
|
-
headers.set("Accept", "text/html");
|
|
67
|
-
headers.set("User-Agent", ua);
|
|
68
|
-
|
|
69
|
-
const resp = await fetch(originUrl, { method: "GET", headers });
|
|
70
|
-
|
|
71
|
-
if (!resp.ok) {
|
|
72
|
-
const fallbackUrl = origin + url.pathname + url.search;
|
|
73
|
-
const fb = await fetch(fallbackUrl, { method: "GET", headers });
|
|
74
|
-
const out = new Response(fb.body, fb);
|
|
75
|
-
out.headers.set("x-rendershield", "bot-fallback");
|
|
76
|
-
${cfg.worker.debugHeaders ? `out.headers.set("X-Bot-Detected", "true");
|
|
77
|
-
out.headers.set("X-Prerender-Fallback", "true");
|
|
78
|
-
out.headers.set("X-Requested-Path", url.pathname);` : ""}
|
|
79
|
-
return out;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
const out = new Response(resp.body, resp);
|
|
83
|
-
out.headers.set("x-rendershield", "bot-hit");
|
|
84
|
-
${cfg.worker.debugHeaders ? `out.headers.set("X-Bot-Detected", "true");
|
|
85
|
-
out.headers.set("X-Prerender", "true");
|
|
86
|
-
out.headers.set("X-Final-Path", finalPath);` : ""}
|
|
87
|
-
return out;
|
|
88
|
-
} catch (err) {
|
|
89
|
-
return new Response("Service temporarily unavailable.", {
|
|
90
|
-
status: 500,
|
|
91
|
-
headers: { "Content-Type": "text/plain; charset=utf-8" }
|
|
92
|
-
});
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
};
|
|
96
|
-
`;
|
|
97
|
-
}
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
import { RenderShieldConfig } from "../types.js";
|
|
3
|
+
|
|
4
|
+
const require = createRequire(import.meta.url);
|
|
5
|
+
let VERSION = "0.0.0";
|
|
6
|
+
try {
|
|
7
|
+
const pkg = require("../../package.json") as { version?: string };
|
|
8
|
+
VERSION = pkg.version ?? "0.0.0";
|
|
9
|
+
} catch {
|
|
10
|
+
VERSION = "unknown";
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function jsStringArray(arr: string[]): string {
|
|
14
|
+
return `[${arr.map((s) => JSON.stringify(s)).join(", ")}]`;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function generateWorkerJs(cfg: RenderShieldConfig): string {
|
|
18
|
+
const patterns = cfg.worker.botUserAgentPatterns.map((p) => p.toLowerCase());
|
|
19
|
+
const rewriteBases = cfg.worker.rewriteRouteBases;
|
|
20
|
+
|
|
21
|
+
return `/**
|
|
22
|
+
* RenderShield Worker (generated) v${VERSION}
|
|
23
|
+
* Serves prerendered /index.html to bots on selected route bases.
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
const BOT_SUBSTRINGS = ${jsStringArray(patterns)};
|
|
27
|
+
const REWRITE_BASES = ${jsStringArray(rewriteBases)};
|
|
28
|
+
|
|
29
|
+
function isBot(ua) {
|
|
30
|
+
const s = (ua || "").toLowerCase();
|
|
31
|
+
return BOT_SUBSTRINGS.some((sub) => s.includes(sub));
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function shouldRewrite(pathname) {
|
|
35
|
+
return REWRITE_BASES.some((base) => pathname.startsWith(base));
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function toIndexHtml(pathname) {
|
|
39
|
+
let p = pathname;
|
|
40
|
+
if (!p.endsWith("/")) p += "/";
|
|
41
|
+
return p + "index.html";
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export default {
|
|
45
|
+
async fetch(request, env, ctx) {
|
|
46
|
+
const url = new URL(request.url);
|
|
47
|
+
const ua = request.headers.get("User-Agent") || "";
|
|
48
|
+
const bot = isBot(ua);
|
|
49
|
+
|
|
50
|
+
const isGetLike = request.method === "GET" || request.method === "HEAD";
|
|
51
|
+
const rewrite = bot && isGetLike && shouldRewrite(url.pathname);
|
|
52
|
+
|
|
53
|
+
try {
|
|
54
|
+
if (!rewrite) {
|
|
55
|
+
const passResp = await fetch(request);
|
|
56
|
+
const out = new Response(passResp.body, passResp);
|
|
57
|
+
out.headers.set("x-rendershield", "pass-through");
|
|
58
|
+
return out;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const origin = ${JSON.stringify(cfg.worker.lovableOrigin)};
|
|
62
|
+
const finalPath = toIndexHtml(url.pathname);
|
|
63
|
+
const originUrl = origin + finalPath + url.search;
|
|
64
|
+
|
|
65
|
+
const headers = new Headers();
|
|
66
|
+
headers.set("Accept", "text/html");
|
|
67
|
+
headers.set("User-Agent", ua);
|
|
68
|
+
|
|
69
|
+
const resp = await fetch(originUrl, { method: "GET", headers });
|
|
70
|
+
|
|
71
|
+
if (!resp.ok) {
|
|
72
|
+
const fallbackUrl = origin + url.pathname + url.search;
|
|
73
|
+
const fb = await fetch(fallbackUrl, { method: "GET", headers });
|
|
74
|
+
const out = new Response(fb.body, fb);
|
|
75
|
+
out.headers.set("x-rendershield", "bot-fallback");
|
|
76
|
+
${cfg.worker.debugHeaders ? `out.headers.set("X-Bot-Detected", "true");
|
|
77
|
+
out.headers.set("X-Prerender-Fallback", "true");
|
|
78
|
+
out.headers.set("X-Requested-Path", url.pathname);` : ""}
|
|
79
|
+
return out;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const out = new Response(resp.body, resp);
|
|
83
|
+
out.headers.set("x-rendershield", "bot-hit");
|
|
84
|
+
${cfg.worker.debugHeaders ? `out.headers.set("X-Bot-Detected", "true");
|
|
85
|
+
out.headers.set("X-Prerender", "true");
|
|
86
|
+
out.headers.set("X-Final-Path", finalPath);` : ""}
|
|
87
|
+
return out;
|
|
88
|
+
} catch (err) {
|
|
89
|
+
return new Response("Service temporarily unavailable.", {
|
|
90
|
+
status: 500,
|
|
91
|
+
headers: { "Content-Type": "text/plain; charset=utf-8" }
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
`;
|
|
97
|
+
}
|
package/src/core/loadConfig.ts
CHANGED
|
@@ -1,173 +1,261 @@
|
|
|
1
|
-
import fs from "fs-extra";
|
|
2
|
-
import path from "node:path";
|
|
3
|
-
import { RenderShieldConfig } from "../types.js";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
const
|
|
9
|
-
|
|
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
|
-
|
|
42
|
-
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
`worker.lovableOrigin
|
|
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
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
if (
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
if (
|
|
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
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
1
|
+
import fs from "fs-extra";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { RenderShieldConfig, SCHEMA_TYPES, type SchemaType } from "../types.js";
|
|
4
|
+
import { renderShieldError } from "../errors.js";
|
|
5
|
+
|
|
6
|
+
const CONFIG_NAME = "rendershield.config.json";
|
|
7
|
+
|
|
8
|
+
const DEFAULT_SITEMAP_PATH = "/sitemap.xml";
|
|
9
|
+
const DEFAULT_ROBOTS_PATH = "/robots.txt";
|
|
10
|
+
|
|
11
|
+
type BoolFlag = { enabled: boolean };
|
|
12
|
+
|
|
13
|
+
/** Shape of config as read from JSON (before normalization). Used for validation only. */
|
|
14
|
+
interface ParsedInput {
|
|
15
|
+
version?: unknown;
|
|
16
|
+
site?: { canonicalBase?: unknown; siteName?: unknown; defaultOgImage?: unknown; authorName?: unknown };
|
|
17
|
+
content?: { markdown?: { baseDir?: unknown; collections?: unknown[] } };
|
|
18
|
+
output?: { outDir?: unknown };
|
|
19
|
+
sitemap?: Record<string, unknown>;
|
|
20
|
+
robots?: Record<string, unknown>;
|
|
21
|
+
worker?: Record<string, unknown>;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function isObject(v: unknown): v is Record<string, unknown> {
|
|
25
|
+
return typeof v === "object" && v !== null && !Array.isArray(v);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function coerceBoolFlag(
|
|
29
|
+
parsed: Record<string, unknown>,
|
|
30
|
+
key: "sitemap" | "robots" | "worker",
|
|
31
|
+
defaultEnabled: boolean
|
|
32
|
+
): BoolFlag {
|
|
33
|
+
// If missing, provide defaults (keeps older configs from exploding)
|
|
34
|
+
if (parsed?.[key] == null) return { enabled: defaultEnabled };
|
|
35
|
+
|
|
36
|
+
// If present, validate shape
|
|
37
|
+
const v = parsed[key];
|
|
38
|
+
if (!isObject(v)) {
|
|
39
|
+
throw renderShieldError(
|
|
40
|
+
"CONFIG_INVALID",
|
|
41
|
+
`${key} must be an object like { "enabled": true }`
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
const enabled = (v as Record<string, unknown>).enabled;
|
|
45
|
+
if (typeof enabled !== "boolean") {
|
|
46
|
+
throw renderShieldError(
|
|
47
|
+
"CONFIG_INVALID",
|
|
48
|
+
`${key}.enabled must be a boolean`
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
return { enabled };
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Worker config policy: strict mode.
|
|
56
|
+
* When worker.enabled is true, all of lovableOrigin, rewriteRouteBases, and
|
|
57
|
+
* botUserAgentPatterns are required and must be non-empty. No defaults are
|
|
58
|
+
* supplied — build fails with a crisp error.
|
|
59
|
+
* lovableOrigin: must be http: or https: only (no file:, javascript:, protocol-relative).
|
|
60
|
+
* botUserAgentPatterns: substring match only (not regex); empty strings rejected to avoid overmatching.
|
|
61
|
+
*/
|
|
62
|
+
function validateWorkerWhenEnabled(parsed: Record<string, unknown>): void {
|
|
63
|
+
const worker = parsed.worker as Record<string, unknown> | undefined;
|
|
64
|
+
if (!worker?.lovableOrigin || typeof worker.lovableOrigin !== "string") {
|
|
65
|
+
throw renderShieldError(
|
|
66
|
+
"CONFIG_INVALID",
|
|
67
|
+
`worker.lovableOrigin is required when worker.enabled is true. Example: "https://your-site.lovable.app"`
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
let url: URL;
|
|
71
|
+
try {
|
|
72
|
+
url = new URL(worker.lovableOrigin);
|
|
73
|
+
} catch {
|
|
74
|
+
throw renderShieldError(
|
|
75
|
+
"CONFIG_INVALID",
|
|
76
|
+
`worker.lovableOrigin must be a valid URL. Got: "${worker.lovableOrigin}"`
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
const scheme = url.protocol.toLowerCase();
|
|
80
|
+
if (scheme !== "http:" && scheme !== "https:") {
|
|
81
|
+
throw renderShieldError(
|
|
82
|
+
"CONFIG_INVALID",
|
|
83
|
+
`worker.lovableOrigin must use http or https. Got: "${url.protocol}"`
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
if (
|
|
87
|
+
!Array.isArray(worker.rewriteRouteBases) ||
|
|
88
|
+
worker.rewriteRouteBases.length === 0
|
|
89
|
+
) {
|
|
90
|
+
throw renderShieldError(
|
|
91
|
+
"CONFIG_INVALID",
|
|
92
|
+
`worker.rewriteRouteBases must be a non-empty array when worker.enabled is true. Example: ["/blog/"]`
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
if (
|
|
96
|
+
!Array.isArray(worker.botUserAgentPatterns) ||
|
|
97
|
+
worker.botUserAgentPatterns.length === 0
|
|
98
|
+
) {
|
|
99
|
+
throw renderShieldError(
|
|
100
|
+
"CONFIG_INVALID",
|
|
101
|
+
`worker.botUserAgentPatterns must be a non-empty array when worker.enabled is true. Example: ["googlebot", "bingbot"]`
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
// Substring match only; empty string would match every User-Agent
|
|
105
|
+
for (let i = 0; i < worker.botUserAgentPatterns.length; i++) {
|
|
106
|
+
const p = worker.botUserAgentPatterns[i];
|
|
107
|
+
if (typeof p !== "string" || p.trim() === "") {
|
|
108
|
+
throw renderShieldError(
|
|
109
|
+
"CONFIG_INVALID",
|
|
110
|
+
`worker.botUserAgentPatterns[${i}] must be a non-empty string (substring match). Empty or invalid entry would overmatch.`
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export async function loadConfig(
|
|
117
|
+
cwd = process.cwd()
|
|
118
|
+
): Promise<RenderShieldConfig> {
|
|
119
|
+
const p = path.join(cwd, CONFIG_NAME);
|
|
120
|
+
const exists = await fs.pathExists(p);
|
|
121
|
+
if (!exists) {
|
|
122
|
+
throw renderShieldError(
|
|
123
|
+
"CONFIG_MISSING",
|
|
124
|
+
`Missing ${CONFIG_NAME}. Run: rendershield init`
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const raw = await fs.readFile(p, "utf8");
|
|
129
|
+
let parsed: ParsedInput & Record<string, unknown>;
|
|
130
|
+
try {
|
|
131
|
+
parsed = JSON.parse(raw) as ParsedInput & Record<string, unknown>;
|
|
132
|
+
} catch {
|
|
133
|
+
throw renderShieldError("CONFIG_INVALID", `${CONFIG_NAME} is not valid JSON`);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// Minimal validation (v0)
|
|
137
|
+
if (parsed?.version !== 1) {
|
|
138
|
+
throw renderShieldError("CONFIG_INVALID", "Config version must be 1");
|
|
139
|
+
}
|
|
140
|
+
if (!parsed?.site?.canonicalBase) {
|
|
141
|
+
throw renderShieldError("CONFIG_INVALID", "site.canonicalBase is required");
|
|
142
|
+
}
|
|
143
|
+
if (!parsed?.site?.siteName) {
|
|
144
|
+
throw renderShieldError("CONFIG_INVALID", "site.siteName is required");
|
|
145
|
+
}
|
|
146
|
+
if (!parsed?.site?.defaultOgImage) {
|
|
147
|
+
throw renderShieldError("CONFIG_INVALID", "site.defaultOgImage is required");
|
|
148
|
+
}
|
|
149
|
+
if (!parsed?.site?.authorName) {
|
|
150
|
+
throw renderShieldError("CONFIG_INVALID", "site.authorName is required");
|
|
151
|
+
}
|
|
152
|
+
if (!parsed?.content?.markdown?.baseDir) {
|
|
153
|
+
throw renderShieldError("CONFIG_INVALID", "content.markdown.baseDir is required");
|
|
154
|
+
}
|
|
155
|
+
if (
|
|
156
|
+
!Array.isArray(parsed?.content?.markdown?.collections) ||
|
|
157
|
+
parsed.content.markdown.collections.length === 0
|
|
158
|
+
) {
|
|
159
|
+
throw renderShieldError(
|
|
160
|
+
"CONFIG_INVALID",
|
|
161
|
+
"content.markdown.collections must be a non-empty array"
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
if (!parsed?.output?.outDir) {
|
|
165
|
+
throw renderShieldError("CONFIG_INVALID", "output.outDir is required");
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
parsed.content.markdown.collections = normalizeCollections(
|
|
169
|
+
parsed.content.markdown.collections
|
|
170
|
+
);
|
|
171
|
+
|
|
172
|
+
// Validate + default these optional sections (preserve path so it is not lost)
|
|
173
|
+
const sitemapFlag = coerceBoolFlag(parsed, "sitemap", true);
|
|
174
|
+
const sitemapObj = parsed.sitemap as Record<string, unknown> | undefined;
|
|
175
|
+
const sitemapPathVal = sitemapObj?.path;
|
|
176
|
+
const sitemapPath =
|
|
177
|
+
typeof sitemapPathVal === "string" && sitemapPathVal.trim().startsWith("/")
|
|
178
|
+
? sitemapPathVal.trim()
|
|
179
|
+
: DEFAULT_SITEMAP_PATH;
|
|
180
|
+
parsed.sitemap = { enabled: sitemapFlag.enabled, path: sitemapPath };
|
|
181
|
+
|
|
182
|
+
const robotsFlag = coerceBoolFlag(parsed, "robots", true);
|
|
183
|
+
const robotsObj = parsed.robots as Record<string, unknown> | undefined;
|
|
184
|
+
const robotsPathVal = robotsObj?.path;
|
|
185
|
+
const robotsPath =
|
|
186
|
+
typeof robotsPathVal === "string" && robotsPathVal.trim().startsWith("/")
|
|
187
|
+
? robotsPathVal.trim()
|
|
188
|
+
: DEFAULT_ROBOTS_PATH;
|
|
189
|
+
parsed.robots = { enabled: robotsFlag.enabled, path: robotsPath };
|
|
190
|
+
|
|
191
|
+
const workerFlag = coerceBoolFlag(parsed, "worker", true);
|
|
192
|
+
if (workerFlag.enabled) {
|
|
193
|
+
validateWorkerWhenEnabled(parsed);
|
|
194
|
+
// Keep parsed.worker as the full object from JSON (already validated)
|
|
195
|
+
} else {
|
|
196
|
+
parsed.worker = {
|
|
197
|
+
enabled: false,
|
|
198
|
+
lovableOrigin: "",
|
|
199
|
+
rewriteRouteBases: [],
|
|
200
|
+
botUserAgentPatterns: [],
|
|
201
|
+
debugHeaders: false,
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
return parsed as RenderShieldConfig;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function normalizeCollections(collections: unknown[]): RenderShieldConfig["content"]["markdown"]["collections"] {
|
|
209
|
+
return collections.map((raw, index) => {
|
|
210
|
+
if (!isObject(raw)) {
|
|
211
|
+
throw renderShieldError(
|
|
212
|
+
"CONFIG_INVALID",
|
|
213
|
+
`content.markdown.collections[${index}] must be an object`
|
|
214
|
+
);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
const name = raw.name;
|
|
218
|
+
const pattern = raw.pattern;
|
|
219
|
+
const routeBase = raw.routeBase;
|
|
220
|
+
const schemaTypeRaw = raw.schemaType;
|
|
221
|
+
|
|
222
|
+
if (typeof name !== "string" || name.trim() === "") {
|
|
223
|
+
throw renderShieldError(
|
|
224
|
+
"CONFIG_INVALID",
|
|
225
|
+
`content.markdown.collections[${index}].name must be a non-empty string`
|
|
226
|
+
);
|
|
227
|
+
}
|
|
228
|
+
if (typeof pattern !== "string" || pattern.trim() === "") {
|
|
229
|
+
throw renderShieldError(
|
|
230
|
+
"CONFIG_INVALID",
|
|
231
|
+
`content.markdown.collections[${index}].pattern must be a non-empty string`
|
|
232
|
+
);
|
|
233
|
+
}
|
|
234
|
+
if (typeof routeBase !== "string" || routeBase.trim() === "") {
|
|
235
|
+
throw renderShieldError(
|
|
236
|
+
"CONFIG_INVALID",
|
|
237
|
+
`content.markdown.collections[${index}].routeBase must be a non-empty string`
|
|
238
|
+
);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
const schemaType: SchemaType =
|
|
242
|
+
schemaTypeRaw === undefined ? "Article" : parseSchemaType(schemaTypeRaw, index);
|
|
243
|
+
|
|
244
|
+
return {
|
|
245
|
+
name: name.trim(),
|
|
246
|
+
pattern: pattern.trim(),
|
|
247
|
+
routeBase: routeBase.trim(),
|
|
248
|
+
schemaType,
|
|
249
|
+
};
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
function parseSchemaType(value: unknown, index: number): SchemaType {
|
|
254
|
+
if (typeof value !== "string" || !SCHEMA_TYPES.includes(value as SchemaType)) {
|
|
255
|
+
throw renderShieldError(
|
|
256
|
+
"CONFIG_INVALID",
|
|
257
|
+
`content.markdown.collections[${index}].schemaType must be one of: ${SCHEMA_TYPES.join(", ")}`
|
|
258
|
+
);
|
|
259
|
+
}
|
|
260
|
+
return value as SchemaType;
|
|
261
|
+
}
|