@sigil-dev/grimoire 0.9.2 → 0.9.3
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/index.ts +35 -35
- package/package.json +8 -8
- package/preload.js +3 -3
- package/server.ts +19 -19
- package/src/client/deps.ts +13 -7
- package/src/client/enhance.ts +98 -98
- package/src/client/head.ts +30 -30
- package/src/client/index.ts +2 -2
- package/src/client/router.ts +445 -371
- package/src/client/scope.ts +22 -22
- package/src/dev/compile-module.ts +163 -163
- package/src/dev/effect-registry.ts +23 -23
- package/src/dev/graph.ts +114 -114
- package/src/dev/hmr-client.ts +185 -185
- package/src/dev/loader.ts +47 -47
- package/src/dev/paths.ts +14 -14
- package/src/dev/runtime-bundle.ts +52 -52
- package/src/dev/watcher.ts +47 -46
- package/src/env/dynamic-private.ts +19 -19
- package/src/env/dynamic-public.ts +22 -22
- package/src/env/index.ts +25 -25
- package/src/env/plugin.ts +19 -19
- package/src/env/private.ts +5 -5
- package/src/env/public.ts +7 -7
- package/src/env/typegen.ts +61 -61
- package/src/headers.ts +114 -114
- package/src/integrations/vite.ts +73 -73
- package/src/logger/index.ts +4 -4
- package/src/logger/instance.ts +6 -6
- package/src/logger/logger.ts +113 -113
- package/src/logger/request-middleware.ts +24 -24
- package/src/logger/transports.ts +117 -117
- package/src/logger/types.ts +39 -39
- package/src/rendering/head.ts +47 -47
- package/src/rendering/hydrate.ts +98 -79
- package/src/rendering/index.ts +455 -372
- package/src/rendering/ssrPlugin.ts +69 -69
- package/src/routing/manifest-gen.ts +12 -11
- package/src/routing/router.ts +109 -109
- package/src/routing/scanner.ts +141 -141
- package/src/routing/transform-routes.ts +102 -102
- package/src/sentinels/error.ts +52 -52
- package/src/sentinels/fail.ts +41 -41
- package/src/sentinels/redirect.ts +35 -35
- package/src/server/build.ts +243 -219
- package/src/server/context.ts +10 -10
- package/src/server/cookie-utils.ts +66 -66
- package/src/server/coordinator.ts +364 -356
- package/src/server/hooks.ts +114 -114
- package/src/server/index.ts +655 -658
- package/src/server/interactive.ts +56 -56
- package/src/server/plugins.ts +121 -120
- package/src/server/worker.ts +58 -58
- package/src/typegen/index.ts +356 -356
- package/src/types.ts +4 -4
- package/test/client-router.test.ts +37 -31
- package/test/context.test.ts +52 -52
- package/test/env.test.ts +70 -70
- package/test/exports.test.ts +37 -37
- package/test/fail.test.ts +46 -46
- package/test/headers.test.ts +100 -100
- package/test/hydration.test.ts +119 -119
- package/test/middleware.test.ts +223 -223
- package/test/preload.ts +5 -5
- package/test/redirect-error.test.ts +112 -112
- package/test/rendering.test.ts +615 -579
- package/test/routing.test.ts +81 -81
- package/test/scanning.test.ts +200 -200
- package/test/scope.test.ts +195 -195
- package/test/server.test.ts +249 -249
- package/test/streaming.test.ts +125 -125
- package/test/transform-routes.test.ts +84 -84
- package/test/typegen.test.ts +682 -682
- package/tsconfig.json +10 -10
- package/.grimoire/_routes.dom.js +0 -8
- package/.grimoire/_routes.hydrate.js +0 -8
- package/.grimoire/tsconfig.generated.json +0 -11
- package/.grimoire/types/ambient.d.ts +0 -59
- package/.grimoire/types/api/hello/$types.d.ts +0 -50
- package/.grimoire/types/api/items/$types.d.ts +0 -50
- package/.grimoire/types/echo/$types.d.ts +0 -50
- package/.grimoire/types/env-private.d.ts +0 -5
- package/.grimoire/types/env-public.d.ts +0 -5
- package/.grimoire/types/mixed/$types.d.ts +0 -50
- package/.grimoire/types/params/[docId]/$types.d.ts +0 -52
- package/.grimoire/types/reject/$types.d.ts +0 -50
- package/public/__grimoire__/hydrate.js +0 -585
- package/public/__grimoire__/index.js +0 -490
package/src/dev/watcher.ts
CHANGED
|
@@ -1,46 +1,47 @@
|
|
|
1
|
-
import { watch } from "node:fs";
|
|
2
|
-
import { join } from "node:path";
|
|
3
|
-
import { normalizePath } from "./paths";
|
|
4
|
-
|
|
5
|
-
export type ChangeHandler = (filePath: string) => Promise<void>;
|
|
6
|
-
|
|
7
|
-
export function startWatcher(
|
|
8
|
-
srcDir: string,
|
|
9
|
-
onChange: ChangeHandler,
|
|
10
|
-
): () => void {
|
|
11
|
-
const pending = new Set<string>();
|
|
12
|
-
let debounce: Timer | null = null;
|
|
13
|
-
|
|
14
|
-
const flush = async () => {
|
|
15
|
-
const files = [...pending];
|
|
16
|
-
pending.clear();
|
|
17
|
-
for (const f of files) {
|
|
18
|
-
await onChange(f);
|
|
19
|
-
}
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
const watcher = watch(srcDir, { recursive: true }, (_evt, filename) => {
|
|
23
|
-
if (!filename) return;
|
|
24
|
-
const name = filename.toString();
|
|
25
|
-
// FUCKING WEBSTORM
|
|
26
|
-
if (name.endsWith("~") || name.endsWith(".tmp") || name.startsWith("."))
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
debounce
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
.
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
1
|
+
import { watch } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { normalizePath } from "./paths";
|
|
4
|
+
|
|
5
|
+
export type ChangeHandler = (filePath: string) => Promise<void>;
|
|
6
|
+
|
|
7
|
+
export function startWatcher(
|
|
8
|
+
srcDir: string,
|
|
9
|
+
onChange: ChangeHandler,
|
|
10
|
+
): () => void {
|
|
11
|
+
const pending = new Set<string>();
|
|
12
|
+
let debounce: Timer | null = null;
|
|
13
|
+
|
|
14
|
+
const flush = async () => {
|
|
15
|
+
const files = [...pending];
|
|
16
|
+
pending.clear();
|
|
17
|
+
for (const f of files) {
|
|
18
|
+
await onChange(f);
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const watcher = watch(srcDir, { recursive: true }, (_evt, filename) => {
|
|
23
|
+
if (!filename) return;
|
|
24
|
+
const name = filename.toString();
|
|
25
|
+
// FUCKING WEBSTORM
|
|
26
|
+
if (name.endsWith("~") || name.endsWith(".tmp") || name.startsWith("."))
|
|
27
|
+
return;
|
|
28
|
+
const raw = join(srcDir, name);
|
|
29
|
+
pending.add(normalizePath(raw));
|
|
30
|
+
if (debounce) clearTimeout(debounce);
|
|
31
|
+
debounce = setTimeout(flush, 60);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
return () => watcher.close();
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Read file with atomic-write retry — VSCode on Windows writes temp-then-rename */
|
|
38
|
+
export async function safeRead(filePath: string): Promise<string | null> {
|
|
39
|
+
for (let attempt = 0; attempt < 3; attempt++) {
|
|
40
|
+
const text = await Bun.file(filePath)
|
|
41
|
+
.text()
|
|
42
|
+
.catch(() => null);
|
|
43
|
+
if (text && text.length > 0) return text;
|
|
44
|
+
await Bun.sleep(30);
|
|
45
|
+
}
|
|
46
|
+
return null;
|
|
47
|
+
}
|
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
// all env vars — server-only, never bundle for client
|
|
2
|
-
export const env: Record<string, string> = new Proxy(
|
|
3
|
-
{} as Record<string, string>,
|
|
4
|
-
{
|
|
5
|
-
get(_, key: string) {
|
|
6
|
-
return process.env[key as string] ?? "";
|
|
7
|
-
},
|
|
8
|
-
ownKeys() {
|
|
9
|
-
return Object.keys(process.env);
|
|
10
|
-
},
|
|
11
|
-
getOwnPropertyDescriptor(_, key: string) {
|
|
12
|
-
return {
|
|
13
|
-
enumerable: true,
|
|
14
|
-
configurable: true,
|
|
15
|
-
value: process.env[key] ?? "",
|
|
16
|
-
};
|
|
17
|
-
},
|
|
18
|
-
},
|
|
19
|
-
);
|
|
1
|
+
// all env vars — server-only, never bundle for client
|
|
2
|
+
export const env: Record<string, string> = new Proxy(
|
|
3
|
+
{} as Record<string, string>,
|
|
4
|
+
{
|
|
5
|
+
get(_, key: string) {
|
|
6
|
+
return process.env[key as string] ?? "";
|
|
7
|
+
},
|
|
8
|
+
ownKeys() {
|
|
9
|
+
return Object.keys(process.env);
|
|
10
|
+
},
|
|
11
|
+
getOwnPropertyDescriptor(_, key: string) {
|
|
12
|
+
return {
|
|
13
|
+
enumerable: true,
|
|
14
|
+
configurable: true,
|
|
15
|
+
value: process.env[key] ?? "",
|
|
16
|
+
};
|
|
17
|
+
},
|
|
18
|
+
},
|
|
19
|
+
);
|
|
@@ -1,22 +1,22 @@
|
|
|
1
|
-
// PUBLIC_ prefixed env vars — safe to expose to client
|
|
2
|
-
export const env: Record<string, string> = new Proxy(
|
|
3
|
-
{} as Record<string, string>,
|
|
4
|
-
{
|
|
5
|
-
get(_, key: string) {
|
|
6
|
-
const val = process.env[key];
|
|
7
|
-
if (!key.startsWith("PUBLIC_")) return undefined;
|
|
8
|
-
return val ?? "";
|
|
9
|
-
},
|
|
10
|
-
ownKeys() {
|
|
11
|
-
return Object.keys(process.env).filter((k) => k.startsWith("PUBLIC_"));
|
|
12
|
-
},
|
|
13
|
-
getOwnPropertyDescriptor(_, key: string) {
|
|
14
|
-
if (!key.startsWith("PUBLIC_")) return undefined;
|
|
15
|
-
return {
|
|
16
|
-
enumerable: true,
|
|
17
|
-
configurable: true,
|
|
18
|
-
value: process.env[key] ?? "",
|
|
19
|
-
};
|
|
20
|
-
},
|
|
21
|
-
},
|
|
22
|
-
);
|
|
1
|
+
// PUBLIC_ prefixed env vars — safe to expose to client
|
|
2
|
+
export const env: Record<string, string> = new Proxy(
|
|
3
|
+
{} as Record<string, string>,
|
|
4
|
+
{
|
|
5
|
+
get(_, key: string) {
|
|
6
|
+
const val = process.env[key];
|
|
7
|
+
if (!key.startsWith("PUBLIC_")) return undefined;
|
|
8
|
+
return val ?? "";
|
|
9
|
+
},
|
|
10
|
+
ownKeys() {
|
|
11
|
+
return Object.keys(process.env).filter((k) => k.startsWith("PUBLIC_"));
|
|
12
|
+
},
|
|
13
|
+
getOwnPropertyDescriptor(_, key: string) {
|
|
14
|
+
if (!key.startsWith("PUBLIC_")) return undefined;
|
|
15
|
+
return {
|
|
16
|
+
enumerable: true,
|
|
17
|
+
configurable: true,
|
|
18
|
+
value: process.env[key] ?? "",
|
|
19
|
+
};
|
|
20
|
+
},
|
|
21
|
+
},
|
|
22
|
+
);
|
package/src/env/index.ts
CHANGED
|
@@ -1,25 +1,25 @@
|
|
|
1
|
-
export const publicPrefix = "PUBLIC_";
|
|
2
|
-
|
|
3
|
-
export function filterPublic(
|
|
4
|
-
env: Record<string, string | undefined>,
|
|
5
|
-
): Record<string, string> {
|
|
6
|
-
const result: Record<string, string> = {};
|
|
7
|
-
for (const [key, value] of Object.entries(env)) {
|
|
8
|
-
if (key.startsWith(publicPrefix) && value !== undefined) {
|
|
9
|
-
result[key] = value;
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
return result;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export function filterPrivate(
|
|
16
|
-
env: Record<string, string | undefined>,
|
|
17
|
-
): Record<string, string> {
|
|
18
|
-
const result: Record<string, string> = {};
|
|
19
|
-
for (const [key, value] of Object.entries(env)) {
|
|
20
|
-
if (!key.startsWith(publicPrefix) && value !== undefined) {
|
|
21
|
-
result[key] = value;
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
return result;
|
|
25
|
-
}
|
|
1
|
+
export const publicPrefix = "PUBLIC_";
|
|
2
|
+
|
|
3
|
+
export function filterPublic(
|
|
4
|
+
env: Record<string, string | undefined>,
|
|
5
|
+
): Record<string, string> {
|
|
6
|
+
const result: Record<string, string> = {};
|
|
7
|
+
for (const [key, value] of Object.entries(env)) {
|
|
8
|
+
if (key.startsWith(publicPrefix) && value !== undefined) {
|
|
9
|
+
result[key] = value;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
return result;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function filterPrivate(
|
|
16
|
+
env: Record<string, string | undefined>,
|
|
17
|
+
): Record<string, string> {
|
|
18
|
+
const result: Record<string, string> = {};
|
|
19
|
+
for (const [key, value] of Object.entries(env)) {
|
|
20
|
+
if (!key.startsWith(publicPrefix) && value !== undefined) {
|
|
21
|
+
result[key] = value;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return result;
|
|
25
|
+
}
|
package/src/env/plugin.ts
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
import { join } from "node:path";
|
|
2
|
-
|
|
3
|
-
export const envPlugin = () => ({
|
|
4
|
-
name: "grimoire-env",
|
|
5
|
-
setup(build: any) {
|
|
6
|
-
build.onResolve({ filter: /^\$env\/static\/public$/ }, () => ({
|
|
7
|
-
path: join(import.meta.dir, "public.ts"),
|
|
8
|
-
}));
|
|
9
|
-
build.onResolve({ filter: /^\$env\/static\/private$/ }, () => ({
|
|
10
|
-
path: join(import.meta.dir, "private.ts"),
|
|
11
|
-
}));
|
|
12
|
-
build.onResolve({ filter: /^\$env\/dynamic\/public$/ }, () => ({
|
|
13
|
-
path: join(import.meta.dir, "dynamic-public.ts"),
|
|
14
|
-
}));
|
|
15
|
-
build.onResolve({ filter: /^\$env\/dynamic\/private$/ }, () => ({
|
|
16
|
-
path: join(import.meta.dir, "dynamic-private.ts"),
|
|
17
|
-
}));
|
|
18
|
-
},
|
|
19
|
-
});
|
|
1
|
+
import { join } from "node:path";
|
|
2
|
+
|
|
3
|
+
export const envPlugin = () => ({
|
|
4
|
+
name: "grimoire-env",
|
|
5
|
+
setup(build: any) {
|
|
6
|
+
build.onResolve({ filter: /^\$env\/static\/public$/ }, () => ({
|
|
7
|
+
path: join(import.meta.dir, "public.ts"),
|
|
8
|
+
}));
|
|
9
|
+
build.onResolve({ filter: /^\$env\/static\/private$/ }, () => ({
|
|
10
|
+
path: join(import.meta.dir, "private.ts"),
|
|
11
|
+
}));
|
|
12
|
+
build.onResolve({ filter: /^\$env\/dynamic\/public$/ }, () => ({
|
|
13
|
+
path: join(import.meta.dir, "dynamic-public.ts"),
|
|
14
|
+
}));
|
|
15
|
+
build.onResolve({ filter: /^\$env\/dynamic\/private$/ }, () => ({
|
|
16
|
+
path: join(import.meta.dir, "dynamic-private.ts"),
|
|
17
|
+
}));
|
|
18
|
+
},
|
|
19
|
+
});
|
package/src/env/private.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { filterPrivate } from "./index";
|
|
2
|
-
|
|
3
|
-
// Runtime values — reads from live process.env on each access.
|
|
4
|
-
// Never sent to the client; server-only.
|
|
5
|
-
export const env: Record<string, string> = filterPrivate(process.env);
|
|
1
|
+
import { filterPrivate } from "./index";
|
|
2
|
+
|
|
3
|
+
// Runtime values — reads from live process.env on each access.
|
|
4
|
+
// Never sent to the client; server-only.
|
|
5
|
+
export const env: Record<string, string> = filterPrivate(process.env);
|
package/src/env/public.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { filterPublic } from "./index";
|
|
2
|
-
|
|
3
|
-
// Inlined at build time — values captured from process.env during the build.
|
|
4
|
-
// For runtime reads (when not compiled), fall back to live process.env.
|
|
5
|
-
export const env = filterPublic(
|
|
6
|
-
typeof process !== "undefined" ? process.env : {},
|
|
7
|
-
);
|
|
1
|
+
import { filterPublic } from "./index";
|
|
2
|
+
|
|
3
|
+
// Inlined at build time — values captured from process.env during the build.
|
|
4
|
+
// For runtime reads (when not compiled), fall back to live process.env.
|
|
5
|
+
export const env = filterPublic(
|
|
6
|
+
typeof process !== "undefined" ? process.env : {},
|
|
7
|
+
);
|
package/src/env/typegen.ts
CHANGED
|
@@ -1,61 +1,61 @@
|
|
|
1
|
-
import { readFile, writeFile } from "node:fs/promises";
|
|
2
|
-
import { join } from "node:path";
|
|
3
|
-
|
|
4
|
-
export async function generateEnvTypes(
|
|
5
|
-
projectRoot: string,
|
|
6
|
-
outDir: string,
|
|
7
|
-
): Promise<void> {
|
|
8
|
-
const envPath = join(projectRoot, ".env");
|
|
9
|
-
let envContent = "";
|
|
10
|
-
try {
|
|
11
|
-
envContent = await readFile(envPath, "utf-8");
|
|
12
|
-
} catch {
|
|
13
|
-
// no .env file — generate empty types
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
const publicVars: string[] = [];
|
|
17
|
-
const privateVars: string[] = [];
|
|
18
|
-
|
|
19
|
-
for (const line of envContent.split("\n")) {
|
|
20
|
-
const trimmed = line.trim();
|
|
21
|
-
if (!trimmed || trimmed.startsWith("#")) continue;
|
|
22
|
-
const eqIndex = trimmed.indexOf("=");
|
|
23
|
-
if (eqIndex === -1) continue;
|
|
24
|
-
const key = trimmed.slice(0, eqIndex).trim();
|
|
25
|
-
if (!key) continue;
|
|
26
|
-
if (key.startsWith("PUBLIC_")) {
|
|
27
|
-
publicVars.push(key);
|
|
28
|
-
} else {
|
|
29
|
-
privateVars.push(key);
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
const publicTypeContent = `// Auto-generated by Grimoire. Do not edit.
|
|
34
|
-
declare module "$env/static/public" {
|
|
35
|
-
${publicVars.map((v) => `\texport const ${v}: string;`).join("\n")}
|
|
36
|
-
${publicVars.length === 0 ? "\texport {};" : ""}
|
|
37
|
-
}
|
|
38
|
-
`;
|
|
39
|
-
|
|
40
|
-
const privateTypeContent = `// Auto-generated by Grimoire. Do not edit.
|
|
41
|
-
declare module "$env/static/private" {
|
|
42
|
-
${privateVars.map((v) => `\texport const ${v}: string;`).join("\n")}
|
|
43
|
-
${privateVars.length === 0 ? "\texport {};" : ""}
|
|
44
|
-
}
|
|
45
|
-
`;
|
|
46
|
-
|
|
47
|
-
const dynamicTypeContent = `// Auto-generated by Grimoire. Do not edit.
|
|
48
|
-
declare module "$env/dynamic/public" {
|
|
49
|
-
\texport const env: Record<string, string>;
|
|
50
|
-
}
|
|
51
|
-
declare module "$env/dynamic/private" {
|
|
52
|
-
\texport const env: Record<string, string>;
|
|
53
|
-
}
|
|
54
|
-
`;
|
|
55
|
-
|
|
56
|
-
await Promise.all([
|
|
57
|
-
writeFile(join(outDir, "env-public.d.ts"), publicTypeContent, "utf-8"),
|
|
58
|
-
writeFile(join(outDir, "env-private.d.ts"), privateTypeContent, "utf-8"),
|
|
59
|
-
writeFile(join(outDir, "env-dynamic.d.ts"), dynamicTypeContent, "utf-8"),
|
|
60
|
-
]);
|
|
61
|
-
}
|
|
1
|
+
import { readFile, writeFile } from "node:fs/promises";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
|
|
4
|
+
export async function generateEnvTypes(
|
|
5
|
+
projectRoot: string,
|
|
6
|
+
outDir: string,
|
|
7
|
+
): Promise<void> {
|
|
8
|
+
const envPath = join(projectRoot, ".env");
|
|
9
|
+
let envContent = "";
|
|
10
|
+
try {
|
|
11
|
+
envContent = await readFile(envPath, "utf-8");
|
|
12
|
+
} catch {
|
|
13
|
+
// no .env file — generate empty types
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const publicVars: string[] = [];
|
|
17
|
+
const privateVars: string[] = [];
|
|
18
|
+
|
|
19
|
+
for (const line of envContent.split("\n")) {
|
|
20
|
+
const trimmed = line.trim();
|
|
21
|
+
if (!trimmed || trimmed.startsWith("#")) continue;
|
|
22
|
+
const eqIndex = trimmed.indexOf("=");
|
|
23
|
+
if (eqIndex === -1) continue;
|
|
24
|
+
const key = trimmed.slice(0, eqIndex).trim();
|
|
25
|
+
if (!key) continue;
|
|
26
|
+
if (key.startsWith("PUBLIC_")) {
|
|
27
|
+
publicVars.push(key);
|
|
28
|
+
} else {
|
|
29
|
+
privateVars.push(key);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const publicTypeContent = `// Auto-generated by Grimoire. Do not edit.
|
|
34
|
+
declare module "$env/static/public" {
|
|
35
|
+
${publicVars.map((v) => `\texport const ${v}: string;`).join("\n")}
|
|
36
|
+
${publicVars.length === 0 ? "\texport {};" : ""}
|
|
37
|
+
}
|
|
38
|
+
`;
|
|
39
|
+
|
|
40
|
+
const privateTypeContent = `// Auto-generated by Grimoire. Do not edit.
|
|
41
|
+
declare module "$env/static/private" {
|
|
42
|
+
${privateVars.map((v) => `\texport const ${v}: string;`).join("\n")}
|
|
43
|
+
${privateVars.length === 0 ? "\texport {};" : ""}
|
|
44
|
+
}
|
|
45
|
+
`;
|
|
46
|
+
|
|
47
|
+
const dynamicTypeContent = `// Auto-generated by Grimoire. Do not edit.
|
|
48
|
+
declare module "$env/dynamic/public" {
|
|
49
|
+
\texport const env: Record<string, string>;
|
|
50
|
+
}
|
|
51
|
+
declare module "$env/dynamic/private" {
|
|
52
|
+
\texport const env: Record<string, string>;
|
|
53
|
+
}
|
|
54
|
+
`;
|
|
55
|
+
|
|
56
|
+
await Promise.all([
|
|
57
|
+
writeFile(join(outDir, "env-public.d.ts"), publicTypeContent, "utf-8"),
|
|
58
|
+
writeFile(join(outDir, "env-private.d.ts"), privateTypeContent, "utf-8"),
|
|
59
|
+
writeFile(join(outDir, "env-dynamic.d.ts"), dynamicTypeContent, "utf-8"),
|
|
60
|
+
]);
|
|
61
|
+
}
|
package/src/headers.ts
CHANGED
|
@@ -1,114 +1,114 @@
|
|
|
1
|
-
import type { GrimoirePlugin } from "./types";
|
|
2
|
-
|
|
3
|
-
export interface SecurityHeadersConfig {
|
|
4
|
-
/** Content-Security-Policy. Set false to omit. */
|
|
5
|
-
contentSecurityPolicy?: string | false;
|
|
6
|
-
/** X-Content-Type-Options. Default: "nosniff" */
|
|
7
|
-
contentTypeOptions?: string | false;
|
|
8
|
-
/** X-Frame-Options. Default: "DENY" */
|
|
9
|
-
frameOptions?: string | false;
|
|
10
|
-
/** X-XSS-Protection. Default: "0" (modern browsers; "1; mode=block" for legacy) */
|
|
11
|
-
xssProtection?: string | false;
|
|
12
|
-
/** Referrer-Policy. Default: "strict-origin-when-cross-origin" */
|
|
13
|
-
referrerPolicy?: string | false;
|
|
14
|
-
/** Strict-Transport-Security. Set false to omit. */
|
|
15
|
-
strictTransportSecurity?: string | false;
|
|
16
|
-
/** Permissions-Policy. Default: "camera=(), microphone=(), geolocation=()" */
|
|
17
|
-
permissionsPolicy?: string | false;
|
|
18
|
-
/** Per-route overrides: path pattern → partial config */
|
|
19
|
-
routes?: Record<string, Partial<SecurityHeadersConfig>>;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
const DEFAULTS: Required<Omit<SecurityHeadersConfig, "routes">> = {
|
|
23
|
-
contentSecurityPolicy:
|
|
24
|
-
"default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; img-src 'self' data:;",
|
|
25
|
-
contentTypeOptions: "nosniff",
|
|
26
|
-
frameOptions: "DENY",
|
|
27
|
-
xssProtection: "0",
|
|
28
|
-
referrerPolicy: "strict-origin-when-cross-origin",
|
|
29
|
-
strictTransportSecurity: "max-age=31536000; includeSubDomains",
|
|
30
|
-
permissionsPolicy: "camera=(), microphone=(), geolocation=()",
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
function resolveHeaders(
|
|
34
|
-
config: SecurityHeadersConfig,
|
|
35
|
-
pathname: string,
|
|
36
|
-
): Record<string, string> {
|
|
37
|
-
// Start with defaults, overlay user config
|
|
38
|
-
const merged = { ...DEFAULTS, ...config };
|
|
39
|
-
delete (merged as any).routes;
|
|
40
|
-
|
|
41
|
-
// Apply route overrides
|
|
42
|
-
if (config.routes) {
|
|
43
|
-
for (const pattern in config.routes) {
|
|
44
|
-
if (pathname.startsWith(pattern)) {
|
|
45
|
-
Object.assign(merged, config.routes[pattern]);
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
const headers: Record<string, string> = {};
|
|
51
|
-
|
|
52
|
-
if (merged.contentSecurityPolicy !== false)
|
|
53
|
-
headers["Content-Security-Policy"] = merged.contentSecurityPolicy;
|
|
54
|
-
if (merged.contentTypeOptions !== false)
|
|
55
|
-
headers["X-Content-Type-Options"] = merged.contentTypeOptions;
|
|
56
|
-
if (merged.frameOptions !== false)
|
|
57
|
-
headers["X-Frame-Options"] = merged.frameOptions;
|
|
58
|
-
if (merged.xssProtection !== false)
|
|
59
|
-
headers["X-XSS-Protection"] = merged.xssProtection;
|
|
60
|
-
if (merged.referrerPolicy !== false)
|
|
61
|
-
headers["Referrer-Policy"] = merged.referrerPolicy;
|
|
62
|
-
if (merged.strictTransportSecurity !== false)
|
|
63
|
-
headers["Strict-Transport-Security"] = merged.strictTransportSecurity;
|
|
64
|
-
if (merged.permissionsPolicy !== false)
|
|
65
|
-
headers["Permissions-Policy"] = merged.permissionsPolicy;
|
|
66
|
-
|
|
67
|
-
return headers;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Security headers plugin.
|
|
72
|
-
* Adds standard security headers to all responses.
|
|
73
|
-
*
|
|
74
|
-
* Usage:
|
|
75
|
-
*
|
|
76
|
-
* import { securityHeaders } from "@sigil-dev/grimoire/headers";
|
|
77
|
-
*
|
|
78
|
-
* createServer({
|
|
79
|
-
* plugins: [
|
|
80
|
-
* securityHeaders({
|
|
81
|
-
* // override defaults
|
|
82
|
-
* frameOptions: "SAMEORIGIN",
|
|
83
|
-
* // per-route
|
|
84
|
-
* routes: {
|
|
85
|
-
* "/admin": { contentSecurityPolicy: "default-src 'self'" },
|
|
86
|
-
* },
|
|
87
|
-
* }),
|
|
88
|
-
* ],
|
|
89
|
-
* });
|
|
90
|
-
*/
|
|
91
|
-
export function securityHeaders(
|
|
92
|
-
config: SecurityHeadersConfig = {},
|
|
93
|
-
): GrimoirePlugin {
|
|
94
|
-
return {
|
|
95
|
-
name: "grimoire-security-headers",
|
|
96
|
-
async onRequest(req, next) {
|
|
97
|
-
const res = await next();
|
|
98
|
-
const url = new URL(req.url);
|
|
99
|
-
const headers = resolveHeaders(config, url.pathname);
|
|
100
|
-
|
|
101
|
-
// Clone response with security headers
|
|
102
|
-
const newHeaders = new Headers(res.headers);
|
|
103
|
-
for (const [key, value] of Object.entries(headers)) {
|
|
104
|
-
newHeaders.set(key, value);
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
return new Response(res.body, {
|
|
108
|
-
status: res.status,
|
|
109
|
-
statusText: res.statusText,
|
|
110
|
-
headers: newHeaders,
|
|
111
|
-
});
|
|
112
|
-
},
|
|
113
|
-
};
|
|
114
|
-
}
|
|
1
|
+
import type { GrimoirePlugin } from "./types";
|
|
2
|
+
|
|
3
|
+
export interface SecurityHeadersConfig {
|
|
4
|
+
/** Content-Security-Policy. Set false to omit. */
|
|
5
|
+
contentSecurityPolicy?: string | false;
|
|
6
|
+
/** X-Content-Type-Options. Default: "nosniff" */
|
|
7
|
+
contentTypeOptions?: string | false;
|
|
8
|
+
/** X-Frame-Options. Default: "DENY" */
|
|
9
|
+
frameOptions?: string | false;
|
|
10
|
+
/** X-XSS-Protection. Default: "0" (modern browsers; "1; mode=block" for legacy) */
|
|
11
|
+
xssProtection?: string | false;
|
|
12
|
+
/** Referrer-Policy. Default: "strict-origin-when-cross-origin" */
|
|
13
|
+
referrerPolicy?: string | false;
|
|
14
|
+
/** Strict-Transport-Security. Set false to omit. */
|
|
15
|
+
strictTransportSecurity?: string | false;
|
|
16
|
+
/** Permissions-Policy. Default: "camera=(), microphone=(), geolocation=()" */
|
|
17
|
+
permissionsPolicy?: string | false;
|
|
18
|
+
/** Per-route overrides: path pattern → partial config */
|
|
19
|
+
routes?: Record<string, Partial<SecurityHeadersConfig>>;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const DEFAULTS: Required<Omit<SecurityHeadersConfig, "routes">> = {
|
|
23
|
+
contentSecurityPolicy:
|
|
24
|
+
"default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; img-src 'self' data:;",
|
|
25
|
+
contentTypeOptions: "nosniff",
|
|
26
|
+
frameOptions: "DENY",
|
|
27
|
+
xssProtection: "0",
|
|
28
|
+
referrerPolicy: "strict-origin-when-cross-origin",
|
|
29
|
+
strictTransportSecurity: "max-age=31536000; includeSubDomains",
|
|
30
|
+
permissionsPolicy: "camera=(), microphone=(), geolocation=()",
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
function resolveHeaders(
|
|
34
|
+
config: SecurityHeadersConfig,
|
|
35
|
+
pathname: string,
|
|
36
|
+
): Record<string, string> {
|
|
37
|
+
// Start with defaults, overlay user config
|
|
38
|
+
const merged = { ...DEFAULTS, ...config };
|
|
39
|
+
delete (merged as any).routes;
|
|
40
|
+
|
|
41
|
+
// Apply route overrides
|
|
42
|
+
if (config.routes) {
|
|
43
|
+
for (const pattern in config.routes) {
|
|
44
|
+
if (pathname.startsWith(pattern)) {
|
|
45
|
+
Object.assign(merged, config.routes[pattern]);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const headers: Record<string, string> = {};
|
|
51
|
+
|
|
52
|
+
if (merged.contentSecurityPolicy !== false)
|
|
53
|
+
headers["Content-Security-Policy"] = merged.contentSecurityPolicy;
|
|
54
|
+
if (merged.contentTypeOptions !== false)
|
|
55
|
+
headers["X-Content-Type-Options"] = merged.contentTypeOptions;
|
|
56
|
+
if (merged.frameOptions !== false)
|
|
57
|
+
headers["X-Frame-Options"] = merged.frameOptions;
|
|
58
|
+
if (merged.xssProtection !== false)
|
|
59
|
+
headers["X-XSS-Protection"] = merged.xssProtection;
|
|
60
|
+
if (merged.referrerPolicy !== false)
|
|
61
|
+
headers["Referrer-Policy"] = merged.referrerPolicy;
|
|
62
|
+
if (merged.strictTransportSecurity !== false)
|
|
63
|
+
headers["Strict-Transport-Security"] = merged.strictTransportSecurity;
|
|
64
|
+
if (merged.permissionsPolicy !== false)
|
|
65
|
+
headers["Permissions-Policy"] = merged.permissionsPolicy;
|
|
66
|
+
|
|
67
|
+
return headers;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Security headers plugin.
|
|
72
|
+
* Adds standard security headers to all responses.
|
|
73
|
+
*
|
|
74
|
+
* Usage:
|
|
75
|
+
*
|
|
76
|
+
* import { securityHeaders } from "@sigil-dev/grimoire/headers";
|
|
77
|
+
*
|
|
78
|
+
* createServer({
|
|
79
|
+
* plugins: [
|
|
80
|
+
* securityHeaders({
|
|
81
|
+
* // override defaults
|
|
82
|
+
* frameOptions: "SAMEORIGIN",
|
|
83
|
+
* // per-route
|
|
84
|
+
* routes: {
|
|
85
|
+
* "/admin": { contentSecurityPolicy: "default-src 'self'" },
|
|
86
|
+
* },
|
|
87
|
+
* }),
|
|
88
|
+
* ],
|
|
89
|
+
* });
|
|
90
|
+
*/
|
|
91
|
+
export function securityHeaders(
|
|
92
|
+
config: SecurityHeadersConfig = {},
|
|
93
|
+
): GrimoirePlugin {
|
|
94
|
+
return {
|
|
95
|
+
name: "grimoire-security-headers",
|
|
96
|
+
async onRequest(req, next) {
|
|
97
|
+
const res = await next();
|
|
98
|
+
const url = new URL(req.url);
|
|
99
|
+
const headers = resolveHeaders(config, url.pathname);
|
|
100
|
+
|
|
101
|
+
// Clone response with security headers
|
|
102
|
+
const newHeaders = new Headers(res.headers);
|
|
103
|
+
for (const [key, value] of Object.entries(headers)) {
|
|
104
|
+
newHeaders.set(key, value);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
return new Response(res.body, {
|
|
108
|
+
status: res.status,
|
|
109
|
+
statusText: res.statusText,
|
|
110
|
+
headers: newHeaders,
|
|
111
|
+
});
|
|
112
|
+
},
|
|
113
|
+
};
|
|
114
|
+
}
|