@sigil-dev/grimoire 0.9.4 → 0.9.5
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 +37 -37
- package/package.json +3 -3
- package/preload.js +3 -3
- package/server.ts +19 -19
- package/src/client/deps.ts +36 -36
- package/src/client/enhance.ts +102 -102
- package/src/client/head.ts +30 -30
- package/src/client/index.ts +5 -5
- package/src/client/router.ts +42 -20
- package/src/client/scope.ts +22 -22
- package/src/dev/compile-module.ts +215 -163
- package/src/dev/effect-registry.ts +23 -23
- package/src/dev/graph.ts +109 -114
- package/src/dev/hmr-client.ts +202 -185
- package/src/dev/hmr-server.ts +261 -187
- package/src/dev/loader.ts +120 -47
- package/src/dev/paths.ts +14 -14
- package/src/dev/resolve.ts +70 -0
- package/src/dev/runtime-bundle.ts +52 -52
- package/src/dev/scan.ts +78 -0
- package/src/dev/watcher.ts +69 -47
- 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 +52 -47
- package/src/rendering/hydrate.ts +16 -3
- package/src/rendering/index.ts +10 -6
- package/src/rendering/ssrPlugin.ts +78 -69
- package/src/routing/manifest-gen.ts +55 -55
- package/src/routing/router.ts +135 -135
- package/src/routing/scanner.ts +196 -187
- 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 +250 -255
- package/src/server/context.ts +10 -10
- package/src/server/cookie-utils.ts +66 -66
- package/src/server/coordinator.ts +364 -364
- package/src/server/hooks.ts +114 -114
- package/src/server/index.ts +711 -677
- package/src/server/interactive.ts +56 -56
- package/src/server/plugins.ts +121 -121
- package/src/server/worker.ts +58 -58
- package/src/typegen/index.ts +389 -389
- package/src/types.ts +286 -286
- package/test/client-router.test.ts +295 -298
- package/test/context.test.ts +52 -52
- package/test/env.test.ts +90 -90
- package/test/exports.test.ts +37 -37
- package/test/fail.test.ts +46 -46
- package/test/headers.test.ts +100 -100
- package/test/hmr.test.ts +426 -0
- 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 +719 -728
- package/test/routing.test.ts +197 -194
- package/test/scanning.test.ts +314 -254
- package/test/scope.test.ts +195 -195
- package/test/server.test.ts +360 -354
- package/test/streaming.test.ts +242 -242
- package/test/transform-routes.test.ts +87 -87
- package/test/typegen.test.ts +697 -697
- package/tsconfig.json +10 -10
package/test/context.test.ts
CHANGED
|
@@ -1,52 +1,52 @@
|
|
|
1
|
-
// packages/grimoire/src/context.test.ts
|
|
2
|
-
import { expect, test } from "bun:test";
|
|
3
|
-
import { createContext, getContext, setContext } from "@sigil-dev/runtime";
|
|
4
|
-
import { runWithContext } from "../src/server/context";
|
|
5
|
-
|
|
6
|
-
const UserKey = createContext<string>();
|
|
7
|
-
const _ThemeKey = createContext<string>();
|
|
8
|
-
|
|
9
|
-
// 1. Basic: does it work at all
|
|
10
|
-
test("getContext returns value set in same context", async () => {
|
|
11
|
-
await runWithContext(async () => {
|
|
12
|
-
setContext(UserKey, "cane");
|
|
13
|
-
expect(getContext(UserKey)).toBe("cane");
|
|
14
|
-
});
|
|
15
|
-
});
|
|
16
|
-
|
|
17
|
-
// 2. Isolation: the whole point of AsyncLocalStorage
|
|
18
|
-
test("concurrent requests do not bleed context", async () => {
|
|
19
|
-
let resolveA!: () => void;
|
|
20
|
-
let resolveB!: () => void;
|
|
21
|
-
|
|
22
|
-
const barrier = {
|
|
23
|
-
a: new Promise<void>((r) => (resolveA = r)),
|
|
24
|
-
b: new Promise<void>((r) => (resolveB = r)),
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
const requestA = runWithContext(async () => {
|
|
28
|
-
setContext(UserKey, "request-A");
|
|
29
|
-
await barrier.a; // suspend, let B run
|
|
30
|
-
// if context bleeds, this returns "request-B"
|
|
31
|
-
return getContext(UserKey);
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
const requestB = runWithContext(async () => {
|
|
35
|
-
setContext(UserKey, "request-B");
|
|
36
|
-
resolveA(); // wake A up
|
|
37
|
-
await barrier.b;
|
|
38
|
-
return getContext(UserKey);
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
resolveB();
|
|
42
|
-
const [a, b] = await Promise.all([requestA, requestB]);
|
|
43
|
-
|
|
44
|
-
expect(a).toBe("request-A"); // fails if bleed
|
|
45
|
-
expect(b).toBe("request-B");
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
// 3. Outside context: should not throw, returns undefined
|
|
49
|
-
test("getContext outside runWithContext returns undefined gracefully", () => {
|
|
50
|
-
const val = getContext(UserKey);
|
|
51
|
-
expect(val).toBeUndefined();
|
|
52
|
-
});
|
|
1
|
+
// packages/grimoire/src/context.test.ts
|
|
2
|
+
import { expect, test } from "bun:test";
|
|
3
|
+
import { createContext, getContext, setContext } from "@sigil-dev/runtime";
|
|
4
|
+
import { runWithContext } from "../src/server/context";
|
|
5
|
+
|
|
6
|
+
const UserKey = createContext<string>();
|
|
7
|
+
const _ThemeKey = createContext<string>();
|
|
8
|
+
|
|
9
|
+
// 1. Basic: does it work at all
|
|
10
|
+
test("getContext returns value set in same context", async () => {
|
|
11
|
+
await runWithContext(async () => {
|
|
12
|
+
setContext(UserKey, "cane");
|
|
13
|
+
expect(getContext(UserKey)).toBe("cane");
|
|
14
|
+
});
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
// 2. Isolation: the whole point of AsyncLocalStorage
|
|
18
|
+
test("concurrent requests do not bleed context", async () => {
|
|
19
|
+
let resolveA!: () => void;
|
|
20
|
+
let resolveB!: () => void;
|
|
21
|
+
|
|
22
|
+
const barrier = {
|
|
23
|
+
a: new Promise<void>((r) => (resolveA = r)),
|
|
24
|
+
b: new Promise<void>((r) => (resolveB = r)),
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const requestA = runWithContext(async () => {
|
|
28
|
+
setContext(UserKey, "request-A");
|
|
29
|
+
await barrier.a; // suspend, let B run
|
|
30
|
+
// if context bleeds, this returns "request-B"
|
|
31
|
+
return getContext(UserKey);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
const requestB = runWithContext(async () => {
|
|
35
|
+
setContext(UserKey, "request-B");
|
|
36
|
+
resolveA(); // wake A up
|
|
37
|
+
await barrier.b;
|
|
38
|
+
return getContext(UserKey);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
resolveB();
|
|
42
|
+
const [a, b] = await Promise.all([requestA, requestB]);
|
|
43
|
+
|
|
44
|
+
expect(a).toBe("request-A"); // fails if bleed
|
|
45
|
+
expect(b).toBe("request-B");
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
// 3. Outside context: should not throw, returns undefined
|
|
49
|
+
test("getContext outside runWithContext returns undefined gracefully", () => {
|
|
50
|
+
const val = getContext(UserKey);
|
|
51
|
+
expect(val).toBeUndefined();
|
|
52
|
+
});
|
package/test/env.test.ts
CHANGED
|
@@ -1,90 +1,90 @@
|
|
|
1
|
-
// packages/grimoire/src/env/env.test.ts
|
|
2
|
-
import { afterEach, beforeEach, describe, expect, test } from "bun:test";
|
|
3
|
-
|
|
4
|
-
describe("$env/dynamic/public", () => {
|
|
5
|
-
let orig: Record<string, string | undefined> = {};
|
|
6
|
-
|
|
7
|
-
beforeEach(() => {
|
|
8
|
-
orig = {
|
|
9
|
-
PUBLIC_APP_NAME: process.env.PUBLIC_APP_NAME,
|
|
10
|
-
PUBLIC_API_URL: process.env.PUBLIC_API_URL,
|
|
11
|
-
SECRET_KEY: process.env.SECRET_KEY,
|
|
12
|
-
};
|
|
13
|
-
});
|
|
14
|
-
|
|
15
|
-
afterEach(() => {
|
|
16
|
-
for (const [k, v] of Object.entries(orig)) {
|
|
17
|
-
if (v === undefined) delete process.env[k];
|
|
18
|
-
else process.env[k] = v;
|
|
19
|
-
}
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
test("reads PUBLIC_ vars at call time", async () => {
|
|
23
|
-
process.env.PUBLIC_APP_NAME = "grimoire";
|
|
24
|
-
const { env } = await import("../src/env/dynamic-public.ts");
|
|
25
|
-
expect(env.PUBLIC_APP_NAME).toBe("grimoire");
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
test("reflects changes after import", async () => {
|
|
29
|
-
const { env } = await import("../src/env/dynamic-public.ts");
|
|
30
|
-
process.env.PUBLIC_APP_NAME = "changed";
|
|
31
|
-
expect(env.PUBLIC_APP_NAME).toBe("changed");
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
test("does not expose non-PUBLIC_ vars", async () => {
|
|
35
|
-
process.env.SECRET_KEY = "supersecret";
|
|
36
|
-
const { env } = await import("../src/env/dynamic-public.ts");
|
|
37
|
-
expect(env.SECRET_KEY).toBeUndefined();
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
test("returns empty string for missing PUBLIC_ key", async () => {
|
|
41
|
-
delete process.env.PUBLIC_API_URL;
|
|
42
|
-
const { env } = await import("../src/env/dynamic-public.ts");
|
|
43
|
-
expect(env.PUBLIC_API_URL).toBe("");
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
test("ownKeys only includes PUBLIC_ vars", async () => {
|
|
47
|
-
process.env.PUBLIC_APP_NAME = "grimoire";
|
|
48
|
-
process.env.SECRET_KEY = "secret";
|
|
49
|
-
const { env } = await import("../src/env/dynamic-public.ts");
|
|
50
|
-
const keys = Object.keys(env);
|
|
51
|
-
expect(keys.every((k) => k.startsWith("PUBLIC_"))).toBe(true);
|
|
52
|
-
});
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
describe("$env/dynamic/private", () => {
|
|
56
|
-
let orig: string | undefined;
|
|
57
|
-
|
|
58
|
-
beforeEach(() => {
|
|
59
|
-
orig = process.env.DATABASE_URL;
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
afterEach(() => {
|
|
63
|
-
if (orig === undefined) delete process.env.DATABASE_URL;
|
|
64
|
-
else process.env.DATABASE_URL = orig;
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
test("reads any env var at call time", async () => {
|
|
68
|
-
process.env.DATABASE_URL = "postgres://localhost/test";
|
|
69
|
-
const { env } = await import("../src/env/dynamic-private.ts");
|
|
70
|
-
expect(env.DATABASE_URL).toBe("postgres://localhost/test");
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
test("reflects changes after import", async () => {
|
|
74
|
-
const { env } = await import("../src/env/dynamic-private.ts");
|
|
75
|
-
process.env.DATABASE_URL = "postgres://localhost/changed";
|
|
76
|
-
expect(env.DATABASE_URL).toBe("postgres://localhost/changed");
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
test("returns empty string for missing key", async () => {
|
|
80
|
-
delete process.env.DATABASE_URL;
|
|
81
|
-
const { env } = await import("../src/env/dynamic-private.ts");
|
|
82
|
-
expect(env.DATABASE_URL).toBe("");
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
test("exposes PUBLIC_ vars too", async () => {
|
|
86
|
-
process.env.PUBLIC_APP_NAME = "grimoire";
|
|
87
|
-
const { env } = await import("../src/env/dynamic-private.ts");
|
|
88
|
-
expect(env.PUBLIC_APP_NAME).toBe("grimoire");
|
|
89
|
-
});
|
|
90
|
-
});
|
|
1
|
+
// packages/grimoire/src/env/env.test.ts
|
|
2
|
+
import { afterEach, beforeEach, describe, expect, test } from "bun:test";
|
|
3
|
+
|
|
4
|
+
describe("$env/dynamic/public", () => {
|
|
5
|
+
let orig: Record<string, string | undefined> = {};
|
|
6
|
+
|
|
7
|
+
beforeEach(() => {
|
|
8
|
+
orig = {
|
|
9
|
+
PUBLIC_APP_NAME: process.env.PUBLIC_APP_NAME,
|
|
10
|
+
PUBLIC_API_URL: process.env.PUBLIC_API_URL,
|
|
11
|
+
SECRET_KEY: process.env.SECRET_KEY,
|
|
12
|
+
};
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
afterEach(() => {
|
|
16
|
+
for (const [k, v] of Object.entries(orig)) {
|
|
17
|
+
if (v === undefined) delete process.env[k];
|
|
18
|
+
else process.env[k] = v;
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
test("reads PUBLIC_ vars at call time", async () => {
|
|
23
|
+
process.env.PUBLIC_APP_NAME = "grimoire";
|
|
24
|
+
const { env } = await import("../src/env/dynamic-public.ts");
|
|
25
|
+
expect(env.PUBLIC_APP_NAME).toBe("grimoire");
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
test("reflects changes after import", async () => {
|
|
29
|
+
const { env } = await import("../src/env/dynamic-public.ts");
|
|
30
|
+
process.env.PUBLIC_APP_NAME = "changed";
|
|
31
|
+
expect(env.PUBLIC_APP_NAME).toBe("changed");
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
test("does not expose non-PUBLIC_ vars", async () => {
|
|
35
|
+
process.env.SECRET_KEY = "supersecret";
|
|
36
|
+
const { env } = await import("../src/env/dynamic-public.ts");
|
|
37
|
+
expect(env.SECRET_KEY).toBeUndefined();
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
test("returns empty string for missing PUBLIC_ key", async () => {
|
|
41
|
+
delete process.env.PUBLIC_API_URL;
|
|
42
|
+
const { env } = await import("../src/env/dynamic-public.ts");
|
|
43
|
+
expect(env.PUBLIC_API_URL).toBe("");
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
test("ownKeys only includes PUBLIC_ vars", async () => {
|
|
47
|
+
process.env.PUBLIC_APP_NAME = "grimoire";
|
|
48
|
+
process.env.SECRET_KEY = "secret";
|
|
49
|
+
const { env } = await import("../src/env/dynamic-public.ts");
|
|
50
|
+
const keys = Object.keys(env);
|
|
51
|
+
expect(keys.every((k) => k.startsWith("PUBLIC_"))).toBe(true);
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
describe("$env/dynamic/private", () => {
|
|
56
|
+
let orig: string | undefined;
|
|
57
|
+
|
|
58
|
+
beforeEach(() => {
|
|
59
|
+
orig = process.env.DATABASE_URL;
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
afterEach(() => {
|
|
63
|
+
if (orig === undefined) delete process.env.DATABASE_URL;
|
|
64
|
+
else process.env.DATABASE_URL = orig;
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
test("reads any env var at call time", async () => {
|
|
68
|
+
process.env.DATABASE_URL = "postgres://localhost/test";
|
|
69
|
+
const { env } = await import("../src/env/dynamic-private.ts");
|
|
70
|
+
expect(env.DATABASE_URL).toBe("postgres://localhost/test");
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
test("reflects changes after import", async () => {
|
|
74
|
+
const { env } = await import("../src/env/dynamic-private.ts");
|
|
75
|
+
process.env.DATABASE_URL = "postgres://localhost/changed";
|
|
76
|
+
expect(env.DATABASE_URL).toBe("postgres://localhost/changed");
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
test("returns empty string for missing key", async () => {
|
|
80
|
+
delete process.env.DATABASE_URL;
|
|
81
|
+
const { env } = await import("../src/env/dynamic-private.ts");
|
|
82
|
+
expect(env.DATABASE_URL).toBe("");
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
test("exposes PUBLIC_ vars too", async () => {
|
|
86
|
+
process.env.PUBLIC_APP_NAME = "grimoire";
|
|
87
|
+
const { env } = await import("../src/env/dynamic-private.ts");
|
|
88
|
+
expect(env.PUBLIC_APP_NAME).toBe("grimoire");
|
|
89
|
+
});
|
|
90
|
+
});
|
package/test/exports.test.ts
CHANGED
|
@@ -1,37 +1,37 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Verifies that every symbol mentioned in the README is actually exported
|
|
3
|
-
* from the correct import path. If any of these break, the README is lying.
|
|
4
|
-
*/
|
|
5
|
-
import { describe, expect, test } from "bun:test";
|
|
6
|
-
|
|
7
|
-
describe("README public API exports", () => {
|
|
8
|
-
test("main package exports: error, fail, redirect, sequence, defineConfig, Head, createServer, TypedLoadContext", async () => {
|
|
9
|
-
const mod = await import("@sigil-dev/grimoire");
|
|
10
|
-
expect(typeof mod.error).toBe("function");
|
|
11
|
-
expect(typeof mod.fail).toBe("function");
|
|
12
|
-
expect(typeof mod.redirect).toBe("function");
|
|
13
|
-
expect(typeof mod.sequence).toBe("function");
|
|
14
|
-
expect(typeof mod.defineConfig).toBe("function");
|
|
15
|
-
expect(typeof mod.Head).toBe("function");
|
|
16
|
-
// isXxx helpers used internally
|
|
17
|
-
expect(typeof mod.isErrorResult).toBe("function");
|
|
18
|
-
expect(typeof mod.isFailResult).toBe("function");
|
|
19
|
-
expect(typeof mod.isRedirectResult).toBe("function");
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
test("@sigil-dev/grimoire/client exports enhance", async () => {
|
|
23
|
-
const mod = await import("@sigil-dev/grimoire/client");
|
|
24
|
-
expect(typeof mod.enhance).toBe("function");
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
test("@sigil-dev/grimoire/hooks exports sequence and createHooks", async () => {
|
|
28
|
-
const mod = await import("@sigil-dev/grimoire/hooks");
|
|
29
|
-
expect(typeof mod.sequence).toBe("function");
|
|
30
|
-
expect(typeof mod.createHooks).toBe("function");
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
test("@sigil-dev/grimoire/headers exports securityHeaders", async () => {
|
|
34
|
-
const mod = await import("@sigil-dev/grimoire/headers");
|
|
35
|
-
expect(typeof mod.securityHeaders).toBe("function");
|
|
36
|
-
});
|
|
37
|
-
});
|
|
1
|
+
/**
|
|
2
|
+
* Verifies that every symbol mentioned in the README is actually exported
|
|
3
|
+
* from the correct import path. If any of these break, the README is lying.
|
|
4
|
+
*/
|
|
5
|
+
import { describe, expect, test } from "bun:test";
|
|
6
|
+
|
|
7
|
+
describe("README public API exports", () => {
|
|
8
|
+
test("main package exports: error, fail, redirect, sequence, defineConfig, Head, createServer, TypedLoadContext", async () => {
|
|
9
|
+
const mod = await import("@sigil-dev/grimoire");
|
|
10
|
+
expect(typeof mod.error).toBe("function");
|
|
11
|
+
expect(typeof mod.fail).toBe("function");
|
|
12
|
+
expect(typeof mod.redirect).toBe("function");
|
|
13
|
+
expect(typeof mod.sequence).toBe("function");
|
|
14
|
+
expect(typeof mod.defineConfig).toBe("function");
|
|
15
|
+
expect(typeof mod.Head).toBe("function");
|
|
16
|
+
// isXxx helpers used internally
|
|
17
|
+
expect(typeof mod.isErrorResult).toBe("function");
|
|
18
|
+
expect(typeof mod.isFailResult).toBe("function");
|
|
19
|
+
expect(typeof mod.isRedirectResult).toBe("function");
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
test("@sigil-dev/grimoire/client exports enhance", async () => {
|
|
23
|
+
const mod = await import("@sigil-dev/grimoire/client");
|
|
24
|
+
expect(typeof mod.enhance).toBe("function");
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
test("@sigil-dev/grimoire/hooks exports sequence and createHooks", async () => {
|
|
28
|
+
const mod = await import("@sigil-dev/grimoire/hooks");
|
|
29
|
+
expect(typeof mod.sequence).toBe("function");
|
|
30
|
+
expect(typeof mod.createHooks).toBe("function");
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
test("@sigil-dev/grimoire/headers exports securityHeaders", async () => {
|
|
34
|
+
const mod = await import("@sigil-dev/grimoire/headers");
|
|
35
|
+
expect(typeof mod.securityHeaders).toBe("function");
|
|
36
|
+
});
|
|
37
|
+
});
|
package/test/fail.test.ts
CHANGED
|
@@ -1,46 +1,46 @@
|
|
|
1
|
-
import { describe, expect, test } from "bun:test";
|
|
2
|
-
import { fail, isFailResult } from "../src/sentinels/fail.ts";
|
|
3
|
-
|
|
4
|
-
describe("fail()", () => {
|
|
5
|
-
test("returns a FailResult with status and data", () => {
|
|
6
|
-
const result = fail(400, { name: "required" });
|
|
7
|
-
expect(result).toEqual({
|
|
8
|
-
__fail: true,
|
|
9
|
-
status: 400,
|
|
10
|
-
data: { name: "required" },
|
|
11
|
-
});
|
|
12
|
-
});
|
|
13
|
-
|
|
14
|
-
test("isFailResult returns true for fail()", () => {
|
|
15
|
-
const result = fail(422, { errors: { email: "invalid" } });
|
|
16
|
-
expect(isFailResult(result)).toBe(true);
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
test("isFailResult returns false for plain objects", () => {
|
|
20
|
-
expect(isFailResult({ redirect: "/foo" })).toBe(false);
|
|
21
|
-
expect(isFailResult(null)).toBe(false);
|
|
22
|
-
expect(isFailResult(undefined)).toBe(false);
|
|
23
|
-
expect(isFailResult("string")).toBe(false);
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
test("preserves complex data structures", () => {
|
|
27
|
-
const data = {
|
|
28
|
-
formData: { email: "test@example.com", name: "" },
|
|
29
|
-
errors: {
|
|
30
|
-
name: "Name is required",
|
|
31
|
-
email: null,
|
|
32
|
-
},
|
|
33
|
-
timestamp: Date.now(),
|
|
34
|
-
};
|
|
35
|
-
const result = fail(400, data);
|
|
36
|
-
expect(result.data).toEqual(data);
|
|
37
|
-
expect(result.status).toBe(400);
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
test("works with different status codes", () => {
|
|
41
|
-
expect(fail(400, {}).status).toBe(400);
|
|
42
|
-
expect(fail(403, {}).status).toBe(403);
|
|
43
|
-
expect(fail(422, {}).status).toBe(422);
|
|
44
|
-
expect(fail(500, {}).status).toBe(500);
|
|
45
|
-
});
|
|
46
|
-
});
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import { fail, isFailResult } from "../src/sentinels/fail.ts";
|
|
3
|
+
|
|
4
|
+
describe("fail()", () => {
|
|
5
|
+
test("returns a FailResult with status and data", () => {
|
|
6
|
+
const result = fail(400, { name: "required" });
|
|
7
|
+
expect(result).toEqual({
|
|
8
|
+
__fail: true,
|
|
9
|
+
status: 400,
|
|
10
|
+
data: { name: "required" },
|
|
11
|
+
});
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
test("isFailResult returns true for fail()", () => {
|
|
15
|
+
const result = fail(422, { errors: { email: "invalid" } });
|
|
16
|
+
expect(isFailResult(result)).toBe(true);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
test("isFailResult returns false for plain objects", () => {
|
|
20
|
+
expect(isFailResult({ redirect: "/foo" })).toBe(false);
|
|
21
|
+
expect(isFailResult(null)).toBe(false);
|
|
22
|
+
expect(isFailResult(undefined)).toBe(false);
|
|
23
|
+
expect(isFailResult("string")).toBe(false);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
test("preserves complex data structures", () => {
|
|
27
|
+
const data = {
|
|
28
|
+
formData: { email: "test@example.com", name: "" },
|
|
29
|
+
errors: {
|
|
30
|
+
name: "Name is required",
|
|
31
|
+
email: null,
|
|
32
|
+
},
|
|
33
|
+
timestamp: Date.now(),
|
|
34
|
+
};
|
|
35
|
+
const result = fail(400, data);
|
|
36
|
+
expect(result.data).toEqual(data);
|
|
37
|
+
expect(result.status).toBe(400);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
test("works with different status codes", () => {
|
|
41
|
+
expect(fail(400, {}).status).toBe(400);
|
|
42
|
+
expect(fail(403, {}).status).toBe(403);
|
|
43
|
+
expect(fail(422, {}).status).toBe(422);
|
|
44
|
+
expect(fail(500, {}).status).toBe(500);
|
|
45
|
+
});
|
|
46
|
+
});
|
package/test/headers.test.ts
CHANGED
|
@@ -1,100 +1,100 @@
|
|
|
1
|
-
import { describe, expect, test } from "bun:test";
|
|
2
|
-
import { securityHeaders } from "../src/headers";
|
|
3
|
-
|
|
4
|
-
function fakeRequest(path = "/") {
|
|
5
|
-
return new Request(`http://localhost${path}`);
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
async function runPlugin(
|
|
9
|
-
config: Parameters<typeof securityHeaders>[0],
|
|
10
|
-
req?: Request,
|
|
11
|
-
): Promise<Headers> {
|
|
12
|
-
const plugin = securityHeaders(config);
|
|
13
|
-
const res = new Response("<html></html>", {
|
|
14
|
-
headers: { "Content-Type": "text/html" },
|
|
15
|
-
});
|
|
16
|
-
const result = await plugin.onRequest!(req ?? fakeRequest(), async () => res);
|
|
17
|
-
return result.headers;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
describe("securityHeaders()", () => {
|
|
21
|
-
test("applies default headers", async () => {
|
|
22
|
-
const headers = await runPlugin({});
|
|
23
|
-
expect(headers.get("X-Content-Type-Options")).toBe("nosniff");
|
|
24
|
-
expect(headers.get("X-Frame-Options")).toBe("DENY");
|
|
25
|
-
expect(headers.get("Referrer-Policy")).toBe(
|
|
26
|
-
"strict-origin-when-cross-origin",
|
|
27
|
-
);
|
|
28
|
-
expect(headers.get("Permissions-Policy")).toBe(
|
|
29
|
-
"camera=(), microphone=(), geolocation=()",
|
|
30
|
-
);
|
|
31
|
-
expect(headers.get("X-XSS-Protection")).toBe("0");
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
test("applies CSP by default", async () => {
|
|
35
|
-
const headers = await runPlugin({});
|
|
36
|
-
expect(headers.get("Content-Security-Policy")).toContain(
|
|
37
|
-
"default-src 'self'",
|
|
38
|
-
);
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
test("allows overriding individual headers", async () => {
|
|
42
|
-
const headers = await runPlugin({
|
|
43
|
-
frameOptions: "SAMEORIGIN",
|
|
44
|
-
contentTypeOptions: false,
|
|
45
|
-
});
|
|
46
|
-
expect(headers.get("X-Frame-Options")).toBe("SAMEORIGIN");
|
|
47
|
-
expect(headers.has("X-Content-Type-Options")).toBe(false);
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
test("disables headers when set to false", async () => {
|
|
51
|
-
const headers = await runPlugin({
|
|
52
|
-
contentSecurityPolicy: false,
|
|
53
|
-
strictTransportSecurity: false,
|
|
54
|
-
permissionsPolicy: false,
|
|
55
|
-
});
|
|
56
|
-
expect(headers.has("Content-Security-Policy")).toBe(false);
|
|
57
|
-
expect(headers.has("Strict-Transport-Security")).toBe(false);
|
|
58
|
-
expect(headers.has("Permissions-Policy")).toBe(false);
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
test("applies route overrides", async () => {
|
|
62
|
-
const headers = await runPlugin(
|
|
63
|
-
{
|
|
64
|
-
frameOptions: "DENY",
|
|
65
|
-
routes: {
|
|
66
|
-
"/admin": { frameOptions: "SAMEORIGIN" },
|
|
67
|
-
},
|
|
68
|
-
},
|
|
69
|
-
fakeRequest("/admin/settings"),
|
|
70
|
-
);
|
|
71
|
-
expect(headers.get("X-Frame-Options")).toBe("SAMEORIGIN");
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
test("route override only applies to matching prefix", async () => {
|
|
75
|
-
const headers = await runPlugin(
|
|
76
|
-
{
|
|
77
|
-
frameOptions: "DENY",
|
|
78
|
-
routes: {
|
|
79
|
-
"/admin": { frameOptions: "SAMEORIGIN" },
|
|
80
|
-
},
|
|
81
|
-
},
|
|
82
|
-
fakeRequest("/dashboard"),
|
|
83
|
-
);
|
|
84
|
-
expect(headers.get("X-Frame-Options")).toBe("DENY");
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
test("preserves existing response headers", async () => {
|
|
88
|
-
const plugin = securityHeaders({});
|
|
89
|
-
const res = new Response("<html></html>", {
|
|
90
|
-
headers: {
|
|
91
|
-
"Content-Type": "text/html",
|
|
92
|
-
"Set-Cookie": "session=abc",
|
|
93
|
-
},
|
|
94
|
-
});
|
|
95
|
-
const result = await plugin.onRequest!(fakeRequest(), async () => res);
|
|
96
|
-
expect(result.headers.get("Content-Type")).toBe("text/html");
|
|
97
|
-
expect(result.headers.get("Set-Cookie")).toBe("session=abc");
|
|
98
|
-
expect(result.headers.get("X-Frame-Options")).toBe("DENY");
|
|
99
|
-
});
|
|
100
|
-
});
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import { securityHeaders } from "../src/headers";
|
|
3
|
+
|
|
4
|
+
function fakeRequest(path = "/") {
|
|
5
|
+
return new Request(`http://localhost${path}`);
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
async function runPlugin(
|
|
9
|
+
config: Parameters<typeof securityHeaders>[0],
|
|
10
|
+
req?: Request,
|
|
11
|
+
): Promise<Headers> {
|
|
12
|
+
const plugin = securityHeaders(config);
|
|
13
|
+
const res = new Response("<html></html>", {
|
|
14
|
+
headers: { "Content-Type": "text/html" },
|
|
15
|
+
});
|
|
16
|
+
const result = await plugin.onRequest!(req ?? fakeRequest(), async () => res);
|
|
17
|
+
return result.headers;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
describe("securityHeaders()", () => {
|
|
21
|
+
test("applies default headers", async () => {
|
|
22
|
+
const headers = await runPlugin({});
|
|
23
|
+
expect(headers.get("X-Content-Type-Options")).toBe("nosniff");
|
|
24
|
+
expect(headers.get("X-Frame-Options")).toBe("DENY");
|
|
25
|
+
expect(headers.get("Referrer-Policy")).toBe(
|
|
26
|
+
"strict-origin-when-cross-origin",
|
|
27
|
+
);
|
|
28
|
+
expect(headers.get("Permissions-Policy")).toBe(
|
|
29
|
+
"camera=(), microphone=(), geolocation=()",
|
|
30
|
+
);
|
|
31
|
+
expect(headers.get("X-XSS-Protection")).toBe("0");
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
test("applies CSP by default", async () => {
|
|
35
|
+
const headers = await runPlugin({});
|
|
36
|
+
expect(headers.get("Content-Security-Policy")).toContain(
|
|
37
|
+
"default-src 'self'",
|
|
38
|
+
);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
test("allows overriding individual headers", async () => {
|
|
42
|
+
const headers = await runPlugin({
|
|
43
|
+
frameOptions: "SAMEORIGIN",
|
|
44
|
+
contentTypeOptions: false,
|
|
45
|
+
});
|
|
46
|
+
expect(headers.get("X-Frame-Options")).toBe("SAMEORIGIN");
|
|
47
|
+
expect(headers.has("X-Content-Type-Options")).toBe(false);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
test("disables headers when set to false", async () => {
|
|
51
|
+
const headers = await runPlugin({
|
|
52
|
+
contentSecurityPolicy: false,
|
|
53
|
+
strictTransportSecurity: false,
|
|
54
|
+
permissionsPolicy: false,
|
|
55
|
+
});
|
|
56
|
+
expect(headers.has("Content-Security-Policy")).toBe(false);
|
|
57
|
+
expect(headers.has("Strict-Transport-Security")).toBe(false);
|
|
58
|
+
expect(headers.has("Permissions-Policy")).toBe(false);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
test("applies route overrides", async () => {
|
|
62
|
+
const headers = await runPlugin(
|
|
63
|
+
{
|
|
64
|
+
frameOptions: "DENY",
|
|
65
|
+
routes: {
|
|
66
|
+
"/admin": { frameOptions: "SAMEORIGIN" },
|
|
67
|
+
},
|
|
68
|
+
},
|
|
69
|
+
fakeRequest("/admin/settings"),
|
|
70
|
+
);
|
|
71
|
+
expect(headers.get("X-Frame-Options")).toBe("SAMEORIGIN");
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
test("route override only applies to matching prefix", async () => {
|
|
75
|
+
const headers = await runPlugin(
|
|
76
|
+
{
|
|
77
|
+
frameOptions: "DENY",
|
|
78
|
+
routes: {
|
|
79
|
+
"/admin": { frameOptions: "SAMEORIGIN" },
|
|
80
|
+
},
|
|
81
|
+
},
|
|
82
|
+
fakeRequest("/dashboard"),
|
|
83
|
+
);
|
|
84
|
+
expect(headers.get("X-Frame-Options")).toBe("DENY");
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
test("preserves existing response headers", async () => {
|
|
88
|
+
const plugin = securityHeaders({});
|
|
89
|
+
const res = new Response("<html></html>", {
|
|
90
|
+
headers: {
|
|
91
|
+
"Content-Type": "text/html",
|
|
92
|
+
"Set-Cookie": "session=abc",
|
|
93
|
+
},
|
|
94
|
+
});
|
|
95
|
+
const result = await plugin.onRequest!(fakeRequest(), async () => res);
|
|
96
|
+
expect(result.headers.get("Content-Type")).toBe("text/html");
|
|
97
|
+
expect(result.headers.get("Set-Cookie")).toBe("session=abc");
|
|
98
|
+
expect(result.headers.get("X-Frame-Options")).toBe("DENY");
|
|
99
|
+
});
|
|
100
|
+
});
|