@lownoise-studio/rendershield 0.1.4 → 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/dist/cli.js +19 -13
- package/dist/cli.js.map +1 -1
- package/dist/commands/build.js +96 -0
- package/dist/commands/build.js.map +1 -1
- package/dist/commands/verify.js +129 -38
- package/dist/commands/verify.js.map +1 -1
- package/dist/core/generateWorker.js +75 -68
- package/dist/core/generateWorker.js.map +1 -1
- package/dist/core/loadConfig.js +53 -1
- package/dist/core/loadConfig.js.map +1 -1
- package/dist/core/validateOutput.js +154 -29
- package/dist/core/validateOutput.js.map +1 -1
- package/package.json +1 -1
- package/src/cli.ts +60 -54
- package/src/commands/build.ts +185 -72
- package/src/commands/verify.ts +233 -114
- package/src/core/generateWorker.ts +87 -80
- package/src/core/loadConfig.ts +142 -74
- package/src/core/validateOutput.ts +214 -37
|
@@ -1,80 +1,87 @@
|
|
|
1
|
-
import { RenderShieldConfig } from "../types.js";
|
|
2
|
-
|
|
3
|
-
function jsStringArray(arr: string[]): string {
|
|
4
|
-
return `[${arr.map((s) => JSON.stringify(s)).join(", ")}]`;
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
export function generateWorkerJs(cfg: RenderShieldConfig): string {
|
|
8
|
-
const patterns = cfg.worker.botUserAgentPatterns.map((p) => p.toLowerCase());
|
|
9
|
-
const rewriteBases = cfg.worker.rewriteRouteBases;
|
|
10
|
-
|
|
11
|
-
return `/**
|
|
12
|
-
* RenderShield Worker (generated)
|
|
13
|
-
* Serves prerendered /index.html to bots on selected route bases.
|
|
14
|
-
*/
|
|
15
|
-
|
|
16
|
-
const BOT_SUBSTRINGS = ${jsStringArray(patterns)};
|
|
17
|
-
const REWRITE_BASES = ${jsStringArray(rewriteBases)};
|
|
18
|
-
|
|
19
|
-
function isBot(ua) {
|
|
20
|
-
const s = (ua || "").toLowerCase();
|
|
21
|
-
return BOT_SUBSTRINGS.some((sub) => s.includes(sub));
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
function shouldRewrite(pathname) {
|
|
25
|
-
return REWRITE_BASES.some((base) => pathname.startsWith(base));
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
function toIndexHtml(pathname) {
|
|
29
|
-
let p = pathname;
|
|
30
|
-
if (!p.endsWith("/")) p += "/";
|
|
31
|
-
return p + "index.html";
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export default {
|
|
35
|
-
async fetch(request, env, ctx) {
|
|
36
|
-
const url = new URL(request.url);
|
|
37
|
-
const ua = request.headers.get("User-Agent") || "";
|
|
38
|
-
const bot = isBot(ua);
|
|
39
|
-
|
|
40
|
-
const isGetLike = request.method === "GET" || request.method === "HEAD";
|
|
41
|
-
const rewrite = bot && isGetLike && shouldRewrite(url.pathname);
|
|
42
|
-
|
|
43
|
-
try {
|
|
44
|
-
if (!rewrite)
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
|
|
1
|
+
import { RenderShieldConfig } from "../types.js";
|
|
2
|
+
|
|
3
|
+
function jsStringArray(arr: string[]): string {
|
|
4
|
+
return `[${arr.map((s) => JSON.stringify(s)).join(", ")}]`;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export function generateWorkerJs(cfg: RenderShieldConfig): string {
|
|
8
|
+
const patterns = cfg.worker.botUserAgentPatterns.map((p) => p.toLowerCase());
|
|
9
|
+
const rewriteBases = cfg.worker.rewriteRouteBases;
|
|
10
|
+
|
|
11
|
+
return `/**
|
|
12
|
+
* RenderShield Worker (generated)
|
|
13
|
+
* Serves prerendered /index.html to bots on selected route bases.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
const BOT_SUBSTRINGS = ${jsStringArray(patterns)};
|
|
17
|
+
const REWRITE_BASES = ${jsStringArray(rewriteBases)};
|
|
18
|
+
|
|
19
|
+
function isBot(ua) {
|
|
20
|
+
const s = (ua || "").toLowerCase();
|
|
21
|
+
return BOT_SUBSTRINGS.some((sub) => s.includes(sub));
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function shouldRewrite(pathname) {
|
|
25
|
+
return REWRITE_BASES.some((base) => pathname.startsWith(base));
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function toIndexHtml(pathname) {
|
|
29
|
+
let p = pathname;
|
|
30
|
+
if (!p.endsWith("/")) p += "/";
|
|
31
|
+
return p + "index.html";
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export default {
|
|
35
|
+
async fetch(request, env, ctx) {
|
|
36
|
+
const url = new URL(request.url);
|
|
37
|
+
const ua = request.headers.get("User-Agent") || "";
|
|
38
|
+
const bot = isBot(ua);
|
|
39
|
+
|
|
40
|
+
const isGetLike = request.method === "GET" || request.method === "HEAD";
|
|
41
|
+
const rewrite = bot && isGetLike && shouldRewrite(url.pathname);
|
|
42
|
+
|
|
43
|
+
try {
|
|
44
|
+
if (!rewrite) {
|
|
45
|
+
const passResp = await fetch(request);
|
|
46
|
+
const out = new Response(passResp.body, passResp);
|
|
47
|
+
out.headers.set("x-rendershield", "pass-through");
|
|
48
|
+
return out;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const origin = ${JSON.stringify(cfg.worker.lovableOrigin)};
|
|
52
|
+
const finalPath = toIndexHtml(url.pathname);
|
|
53
|
+
const originUrl = origin + finalPath + url.search;
|
|
54
|
+
|
|
55
|
+
const headers = new Headers();
|
|
56
|
+
headers.set("Accept", "text/html");
|
|
57
|
+
headers.set("User-Agent", ua);
|
|
58
|
+
|
|
59
|
+
const resp = await fetch(originUrl, { method: "GET", headers });
|
|
60
|
+
|
|
61
|
+
if (!resp.ok) {
|
|
62
|
+
const fallbackUrl = origin + url.pathname + url.search;
|
|
63
|
+
const fb = await fetch(fallbackUrl, { method: "GET", headers });
|
|
64
|
+
const out = new Response(fb.body, fb);
|
|
65
|
+
out.headers.set("x-rendershield", "bot-fallback");
|
|
66
|
+
${cfg.worker.debugHeaders ? `out.headers.set("X-Bot-Detected", "true");
|
|
67
|
+
out.headers.set("X-Prerender-Fallback", "true");
|
|
68
|
+
out.headers.set("X-Requested-Path", url.pathname);` : ""}
|
|
69
|
+
return out;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const out = new Response(resp.body, resp);
|
|
73
|
+
out.headers.set("x-rendershield", "bot-hit");
|
|
74
|
+
${cfg.worker.debugHeaders ? `out.headers.set("X-Bot-Detected", "true");
|
|
75
|
+
out.headers.set("X-Prerender", "true");
|
|
76
|
+
out.headers.set("X-Final-Path", finalPath);` : ""}
|
|
77
|
+
return out;
|
|
78
|
+
} catch (err) {
|
|
79
|
+
return new Response("Worker error: " + (err && err.message ? err.message : String(err)), {
|
|
80
|
+
status: 500,
|
|
81
|
+
headers: { "Content-Type": "text/plain; charset=utf-8" }
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
`;
|
|
87
|
+
}
|
package/src/core/loadConfig.ts
CHANGED
|
@@ -1,74 +1,142 @@
|
|
|
1
|
-
import fs from "fs-extra";
|
|
2
|
-
import path from "node:path";
|
|
3
|
-
import { RenderShieldConfig } from "../types.js";
|
|
4
|
-
|
|
5
|
-
const CONFIG_NAME = "rendershield.config.json";
|
|
6
|
-
|
|
7
|
-
type BoolFlag = { enabled: boolean };
|
|
8
|
-
|
|
9
|
-
function isObject(v: unknown): v is Record<string, unknown> {
|
|
10
|
-
return typeof v === "object" && v !== null && !Array.isArray(v);
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
function coerceBoolFlag(
|
|
14
|
-
parsed: any,
|
|
15
|
-
key: "sitemap" | "robots" | "worker",
|
|
16
|
-
defaultEnabled: boolean
|
|
17
|
-
): BoolFlag {
|
|
18
|
-
// If missing, provide defaults (keeps older configs from exploding)
|
|
19
|
-
if (parsed?.[key] == null) return { enabled: defaultEnabled };
|
|
20
|
-
|
|
21
|
-
// If present, validate shape
|
|
22
|
-
const v = parsed[key];
|
|
23
|
-
if (!isObject(v)) {
|
|
24
|
-
throw new Error(`${key} must be an object like { "enabled": true }`);
|
|
25
|
-
}
|
|
26
|
-
if (typeof (v as any).enabled !== "boolean") {
|
|
27
|
-
throw new Error(`${key}.enabled must be a boolean`);
|
|
28
|
-
}
|
|
29
|
-
return { enabled: (v as any).enabled };
|
|
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
|
-
if (
|
|
61
|
-
!Array.isArray(parsed?.
|
|
62
|
-
parsed.
|
|
63
|
-
) {
|
|
64
|
-
throw new Error(
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
1
|
+
import fs from "fs-extra";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { RenderShieldConfig } from "../types.js";
|
|
4
|
+
|
|
5
|
+
const CONFIG_NAME = "rendershield.config.json";
|
|
6
|
+
|
|
7
|
+
type BoolFlag = { enabled: boolean };
|
|
8
|
+
|
|
9
|
+
function isObject(v: unknown): v is Record<string, unknown> {
|
|
10
|
+
return typeof v === "object" && v !== null && !Array.isArray(v);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function coerceBoolFlag(
|
|
14
|
+
parsed: any,
|
|
15
|
+
key: "sitemap" | "robots" | "worker",
|
|
16
|
+
defaultEnabled: boolean
|
|
17
|
+
): BoolFlag {
|
|
18
|
+
// If missing, provide defaults (keeps older configs from exploding)
|
|
19
|
+
if (parsed?.[key] == null) return { enabled: defaultEnabled };
|
|
20
|
+
|
|
21
|
+
// If present, validate shape
|
|
22
|
+
const v = parsed[key];
|
|
23
|
+
if (!isObject(v)) {
|
|
24
|
+
throw new Error(`${key} must be an object like { "enabled": true }`);
|
|
25
|
+
}
|
|
26
|
+
if (typeof (v as any).enabled !== "boolean") {
|
|
27
|
+
throw new Error(`${key}.enabled must be a boolean`);
|
|
28
|
+
}
|
|
29
|
+
return { enabled: (v as any).enabled };
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Worker config policy: strict mode.
|
|
34
|
+
* When worker.enabled is true, all of lovableOrigin, rewriteRouteBases, and
|
|
35
|
+
* botUserAgentPatterns are required and must be non-empty. No defaults are
|
|
36
|
+
* supplied — build fails with a crisp error.
|
|
37
|
+
* lovableOrigin: must be http: or https: only (no file:, javascript:, protocol-relative).
|
|
38
|
+
* botUserAgentPatterns: substring match only (not regex); empty strings rejected to avoid overmatching.
|
|
39
|
+
*/
|
|
40
|
+
function validateWorkerWhenEnabled(parsed: any): void {
|
|
41
|
+
if (!parsed.worker?.lovableOrigin || typeof parsed.worker.lovableOrigin !== "string") {
|
|
42
|
+
throw new Error(
|
|
43
|
+
`worker.lovableOrigin is required when worker.enabled is true. Example: "https://your-site.lovable.app"`
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
let url: URL;
|
|
47
|
+
try {
|
|
48
|
+
url = new URL(parsed.worker.lovableOrigin);
|
|
49
|
+
} catch {
|
|
50
|
+
throw new Error(
|
|
51
|
+
`worker.lovableOrigin must be a valid URL. Got: "${parsed.worker.lovableOrigin}"`
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
const scheme = url.protocol.toLowerCase();
|
|
55
|
+
if (scheme !== "http:" && scheme !== "https:") {
|
|
56
|
+
throw new Error(
|
|
57
|
+
`worker.lovableOrigin must use http or https. Got: "${url.protocol}"`
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
if (
|
|
61
|
+
!Array.isArray(parsed.worker?.rewriteRouteBases) ||
|
|
62
|
+
parsed.worker.rewriteRouteBases.length === 0
|
|
63
|
+
) {
|
|
64
|
+
throw new Error(
|
|
65
|
+
`worker.rewriteRouteBases must be a non-empty array when worker.enabled is true. Example: ["/blog/"]`
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
if (
|
|
69
|
+
!Array.isArray(parsed.worker?.botUserAgentPatterns) ||
|
|
70
|
+
parsed.worker.botUserAgentPatterns.length === 0
|
|
71
|
+
) {
|
|
72
|
+
throw new Error(
|
|
73
|
+
`worker.botUserAgentPatterns must be a non-empty array when worker.enabled is true. Example: ["googlebot", "bingbot"]`
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
// Substring match only; empty string would match every User-Agent
|
|
77
|
+
for (let i = 0; i < parsed.worker.botUserAgentPatterns.length; i++) {
|
|
78
|
+
const p = parsed.worker.botUserAgentPatterns[i];
|
|
79
|
+
if (typeof p !== "string" || p.trim() === "") {
|
|
80
|
+
throw new Error(
|
|
81
|
+
`worker.botUserAgentPatterns[${i}] must be a non-empty string (substring match). Empty or invalid entry would overmatch.`
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export async function loadConfig(
|
|
88
|
+
cwd = process.cwd()
|
|
89
|
+
): Promise<RenderShieldConfig> {
|
|
90
|
+
const p = path.join(cwd, CONFIG_NAME);
|
|
91
|
+
const exists = await fs.pathExists(p);
|
|
92
|
+
if (!exists) {
|
|
93
|
+
throw new Error(`Missing ${CONFIG_NAME}. Run: rendershield init`);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const raw = await fs.readFile(p, "utf8");
|
|
97
|
+
let parsed: any;
|
|
98
|
+
try {
|
|
99
|
+
parsed = JSON.parse(raw);
|
|
100
|
+
} catch {
|
|
101
|
+
throw new Error(`${CONFIG_NAME} is not valid JSON`);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// Minimal validation (v0)
|
|
105
|
+
if (parsed?.version !== 1) throw new Error(`Config version must be 1`);
|
|
106
|
+
if (!parsed?.site?.canonicalBase)
|
|
107
|
+
throw new Error(`site.canonicalBase is required`);
|
|
108
|
+
if (!parsed?.site?.siteName) throw new Error(`site.siteName is required`);
|
|
109
|
+
if (!parsed?.site?.defaultOgImage)
|
|
110
|
+
throw new Error(`site.defaultOgImage is required`);
|
|
111
|
+
if (!parsed?.site?.authorName)
|
|
112
|
+
throw new Error(`site.authorName is required`);
|
|
113
|
+
if (!parsed?.content?.markdown?.baseDir)
|
|
114
|
+
throw new Error(`content.markdown.baseDir is required`);
|
|
115
|
+
if (
|
|
116
|
+
!Array.isArray(parsed?.content?.markdown?.collections) ||
|
|
117
|
+
parsed.content.markdown.collections.length === 0
|
|
118
|
+
) {
|
|
119
|
+
throw new Error(`content.markdown.collections must be a non-empty array`);
|
|
120
|
+
}
|
|
121
|
+
if (!parsed?.output?.outDir) throw new Error(`output.outDir is required`);
|
|
122
|
+
|
|
123
|
+
// Validate + default these optional sections to prevent TypeErrors later
|
|
124
|
+
parsed.sitemap = coerceBoolFlag(parsed, "sitemap", true);
|
|
125
|
+
parsed.robots = coerceBoolFlag(parsed, "robots", true);
|
|
126
|
+
const workerFlag = coerceBoolFlag(parsed, "worker", true);
|
|
127
|
+
parsed.worker = workerFlag;
|
|
128
|
+
|
|
129
|
+
if (parsed.worker.enabled) {
|
|
130
|
+
validateWorkerWhenEnabled(parsed);
|
|
131
|
+
} else {
|
|
132
|
+
parsed.worker = {
|
|
133
|
+
enabled: false,
|
|
134
|
+
lovableOrigin: "",
|
|
135
|
+
rewriteRouteBases: [],
|
|
136
|
+
botUserAgentPatterns: [],
|
|
137
|
+
debugHeaders: false,
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
return parsed as RenderShieldConfig;
|
|
142
|
+
}
|