@sigil-dev/grimoire 0.9.21 → 0.9.23
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/README.md +83 -83
- package/index.ts +50 -48
- package/package.json +22 -10
- package/public/__grimoire__/grimoire-client.js +12 -0
- package/public/__grimoire__/runtime.js +1241 -0
- package/server.ts +22 -20
- package/src/adapters/bun.ts +17 -0
- package/src/adapters/static.ts +41 -0
- package/src/adapters/types.ts +36 -0
- package/src/base.ts +91 -0
- package/src/client/csrf.ts +47 -47
- package/src/client/deferred.ts +179 -179
- package/src/client/enhance.ts +160 -160
- package/src/client/router.ts +871 -833
- package/src/dev/compile-module.ts +211 -211
- package/src/dev/loader.ts +141 -141
- package/src/dev/resolve.ts +70 -70
- package/src/dev/runtime-bundle.ts +51 -51
- package/src/env/modules.ts +62 -62
- package/src/env/required.ts +42 -0
- package/src/env/typegen.ts +61 -61
- package/src/headers.ts +105 -105
- package/src/logger/transports.ts +117 -117
- package/src/rendering/head.ts +49 -49
- package/src/rendering/hydrate.ts +161 -161
- package/src/rendering/images.ts +43 -43
- package/src/rendering/index.ts +755 -749
- package/src/rendering/preload.ts +102 -101
- package/src/rendering/ssrPlugin.ts +1 -1
- package/src/rendering/timing.ts +40 -40
- package/src/routing/router.ts +186 -186
- package/src/routing/scanner.ts +225 -196
- package/src/routing/transform-routes.ts +102 -102
- package/src/server/after-response.ts +206 -206
- package/src/server/asset-cache.ts +57 -57
- package/src/server/build.ts +262 -262
- package/src/server/cached.ts +114 -114
- package/src/server/client-address.ts +82 -82
- package/src/server/client-dir.ts +57 -57
- package/src/server/client-entry.ts +44 -44
- package/src/server/cookie-utils.ts +80 -80
- package/src/server/coordinator.ts +476 -471
- package/src/server/hooks.ts +122 -122
- package/src/server/index.ts +211 -80
- package/src/server/plugins.ts +172 -172
- package/src/server/prerender.ts +444 -0
- package/src/server/reserved.ts +17 -0
- package/src/server/resolver.ts +98 -98
- package/src/server/rewrite.ts +28 -0
- package/src/server/warm.ts +91 -91
- package/src/server/with-headers.ts +27 -27
- package/src/server/worker.ts +74 -72
- package/src/testing/dom.ts +73 -0
- package/src/testing/index.ts +169 -0
- package/src/testing/server.ts +162 -0
- package/src/typegen/index.ts +450 -450
- package/src/types.ts +501 -432
- package/test/after-response.test.ts +132 -132
- package/test/asset-cache-policy.test.ts +96 -96
- package/test/base.test.ts +134 -0
- package/test/cached.test.ts +126 -126
- package/test/client-address.test.ts +78 -78
- package/test/client-csrf.test.ts +61 -61
- package/test/client-dir.test.ts +101 -101
- package/test/client-router.test.ts +789 -734
- package/test/coordinator.test.ts +96 -94
- package/test/deferred.test.ts +256 -256
- package/test/dev-loader.test.ts +30 -30
- package/test/env.test.ts +168 -168
- package/test/force-production.test.ts +44 -0
- package/test/images.test.ts +49 -49
- package/test/locals-header.test.ts +61 -0
- package/test/middleware.test.ts +243 -243
- package/test/parser-plugins.test.ts +1 -1
- package/test/plugin-handle.test.ts +62 -62
- package/test/plugin-services-context.test.ts +97 -97
- package/test/preload.test.ts +48 -48
- package/test/prerender.test.ts +353 -0
- package/test/rendering.test.ts +799 -798
- package/test/required-env.test.ts +32 -0
- package/test/rewrite-html.test.ts +90 -0
- package/test/scanning.test.ts +349 -314
- package/test/server.test.ts +640 -619
- package/test/services.test.ts +90 -90
- package/test/streaming.test.ts +381 -378
- package/test/testing-helpers.test.ts +166 -0
- package/test/typegen.test.ts +808 -799
- package/test/warm.test.ts +96 -96
- package/test/worker-context.test.ts +59 -59
- package/test/workspace-files.test.ts +149 -146
- package/test/workspace-versions.test.ts +1 -1
- package/.grimoire/_routes.ts +0 -4
- package/public/__grimoire__/client.js +0 -55
- package/public/__grimoire__/hydrate.js +0 -63
package/test/cached.test.ts
CHANGED
|
@@ -1,126 +1,126 @@
|
|
|
1
|
-
import { describe, expect, test } from "bun:test";
|
|
2
|
-
import { cached, cachedBy } from "../src/server/cached.ts";
|
|
3
|
-
|
|
4
|
-
const tick = (ms: number) => Bun.sleep(ms);
|
|
5
|
-
|
|
6
|
-
describe("cached", () => {
|
|
7
|
-
test("returns the same value while fresh and builds once", async () => {
|
|
8
|
-
let n = 0;
|
|
9
|
-
const get = cached(async () => ++n, { ttl: 1000 });
|
|
10
|
-
expect(await get()).toBe(1);
|
|
11
|
-
expect(await get()).toBe(1);
|
|
12
|
-
expect(n).toBe(1);
|
|
13
|
-
});
|
|
14
|
-
|
|
15
|
-
test("concurrent first calls share one build", async () => {
|
|
16
|
-
let n = 0;
|
|
17
|
-
const get = cached(
|
|
18
|
-
async () => {
|
|
19
|
-
await tick(20);
|
|
20
|
-
return ++n;
|
|
21
|
-
},
|
|
22
|
-
{ ttl: 1000 },
|
|
23
|
-
);
|
|
24
|
-
expect(await Promise.all([get(), get(), get()])).toEqual([1, 1, 1]);
|
|
25
|
-
expect(n).toBe(1);
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
test("an expired value is served at once while one refresh runs", async () => {
|
|
29
|
-
let n = 0;
|
|
30
|
-
const get = cached(
|
|
31
|
-
async () => {
|
|
32
|
-
await tick(30);
|
|
33
|
-
return ++n;
|
|
34
|
-
},
|
|
35
|
-
{ ttl: 20 },
|
|
36
|
-
);
|
|
37
|
-
await get();
|
|
38
|
-
await tick(40);
|
|
39
|
-
const t0 = performance.now();
|
|
40
|
-
expect(await get()).toBe(1); // stale, instantly
|
|
41
|
-
expect(await get()).toBe(1); // refresh still in flight, still one
|
|
42
|
-
expect(performance.now() - t0).toBeLessThan(25);
|
|
43
|
-
await tick(50);
|
|
44
|
-
expect(n).toBe(2);
|
|
45
|
-
expect(await get()).toBe(2);
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
test("past staleFor the caller waits for the fresh value", async () => {
|
|
49
|
-
let n = 0;
|
|
50
|
-
const get = cached(async () => ++n, { ttl: 10, staleFor: 10 });
|
|
51
|
-
await get();
|
|
52
|
-
await tick(40);
|
|
53
|
-
expect(await get()).toBe(2);
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
test("a failed background refresh keeps the stale value and reports it", async () => {
|
|
57
|
-
let n = 0;
|
|
58
|
-
const errors: unknown[] = [];
|
|
59
|
-
const get = cached(
|
|
60
|
-
async () => {
|
|
61
|
-
if (++n > 1) throw new Error("upstream down");
|
|
62
|
-
return "v1";
|
|
63
|
-
},
|
|
64
|
-
{ ttl: 10, onError: (e) => errors.push(e) },
|
|
65
|
-
);
|
|
66
|
-
await get();
|
|
67
|
-
await tick(20);
|
|
68
|
-
expect(await get()).toBe("v1");
|
|
69
|
-
await tick(10);
|
|
70
|
-
expect(errors).toHaveLength(1);
|
|
71
|
-
expect(await get()).toBe("v1");
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
test("a failed first build throws and is not cached", async () => {
|
|
75
|
-
let n = 0;
|
|
76
|
-
const get = cached(
|
|
77
|
-
async () => {
|
|
78
|
-
if (++n === 1) throw new Error("first fails");
|
|
79
|
-
return "ok";
|
|
80
|
-
},
|
|
81
|
-
{ ttl: 1000 },
|
|
82
|
-
);
|
|
83
|
-
await expect(get()).rejects.toThrow("first fails");
|
|
84
|
-
expect(await get()).toBe("ok");
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
test("invalidate forces the next call to rebuild", async () => {
|
|
88
|
-
let n = 0;
|
|
89
|
-
const get = cached(async () => ++n, { ttl: 1000 });
|
|
90
|
-
await get();
|
|
91
|
-
get.invalidate();
|
|
92
|
-
expect(await get()).toBe(2);
|
|
93
|
-
});
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
describe("cachedBy", () => {
|
|
97
|
-
test("caches per argument and evicts the least recently used past max", async () => {
|
|
98
|
-
const calls: string[] = [];
|
|
99
|
-
const get = cachedBy(
|
|
100
|
-
async (id: string) => {
|
|
101
|
-
calls.push(id);
|
|
102
|
-
return id.toUpperCase();
|
|
103
|
-
},
|
|
104
|
-
{ ttl: 1000, max: 2 },
|
|
105
|
-
);
|
|
106
|
-
await get("a");
|
|
107
|
-
await get("b");
|
|
108
|
-
await get("a"); // hit: "b" is now the least recently used
|
|
109
|
-
expect(calls).toEqual(["a", "b"]);
|
|
110
|
-
await get("c"); // pushes out "b", not "a"
|
|
111
|
-
await get("a");
|
|
112
|
-
expect(calls).toEqual(["a", "b", "c"]);
|
|
113
|
-
await get("b");
|
|
114
|
-
expect(calls).toEqual(["a", "b", "c", "b"]);
|
|
115
|
-
});
|
|
116
|
-
|
|
117
|
-
test("invalidate(key) drops only that key", async () => {
|
|
118
|
-
let n = 0;
|
|
119
|
-
const get = cachedBy(async (_id: string) => ++n, { ttl: 1000 });
|
|
120
|
-
await get("x");
|
|
121
|
-
await get("y");
|
|
122
|
-
get.invalidate("x");
|
|
123
|
-
expect(await get("y")).toBe(2);
|
|
124
|
-
expect(await get("x")).toBe(3);
|
|
125
|
-
});
|
|
126
|
-
});
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import { cached, cachedBy } from "../src/server/cached.ts";
|
|
3
|
+
|
|
4
|
+
const tick = (ms: number) => Bun.sleep(ms);
|
|
5
|
+
|
|
6
|
+
describe("cached", () => {
|
|
7
|
+
test("returns the same value while fresh and builds once", async () => {
|
|
8
|
+
let n = 0;
|
|
9
|
+
const get = cached(async () => ++n, { ttl: 1000 });
|
|
10
|
+
expect(await get()).toBe(1);
|
|
11
|
+
expect(await get()).toBe(1);
|
|
12
|
+
expect(n).toBe(1);
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
test("concurrent first calls share one build", async () => {
|
|
16
|
+
let n = 0;
|
|
17
|
+
const get = cached(
|
|
18
|
+
async () => {
|
|
19
|
+
await tick(20);
|
|
20
|
+
return ++n;
|
|
21
|
+
},
|
|
22
|
+
{ ttl: 1000 },
|
|
23
|
+
);
|
|
24
|
+
expect(await Promise.all([get(), get(), get()])).toEqual([1, 1, 1]);
|
|
25
|
+
expect(n).toBe(1);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
test("an expired value is served at once while one refresh runs", async () => {
|
|
29
|
+
let n = 0;
|
|
30
|
+
const get = cached(
|
|
31
|
+
async () => {
|
|
32
|
+
await tick(30);
|
|
33
|
+
return ++n;
|
|
34
|
+
},
|
|
35
|
+
{ ttl: 20 },
|
|
36
|
+
);
|
|
37
|
+
await get();
|
|
38
|
+
await tick(40);
|
|
39
|
+
const t0 = performance.now();
|
|
40
|
+
expect(await get()).toBe(1); // stale, instantly
|
|
41
|
+
expect(await get()).toBe(1); // refresh still in flight, still one
|
|
42
|
+
expect(performance.now() - t0).toBeLessThan(25);
|
|
43
|
+
await tick(50);
|
|
44
|
+
expect(n).toBe(2);
|
|
45
|
+
expect(await get()).toBe(2);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
test("past staleFor the caller waits for the fresh value", async () => {
|
|
49
|
+
let n = 0;
|
|
50
|
+
const get = cached(async () => ++n, { ttl: 10, staleFor: 10 });
|
|
51
|
+
await get();
|
|
52
|
+
await tick(40);
|
|
53
|
+
expect(await get()).toBe(2);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
test("a failed background refresh keeps the stale value and reports it", async () => {
|
|
57
|
+
let n = 0;
|
|
58
|
+
const errors: unknown[] = [];
|
|
59
|
+
const get = cached(
|
|
60
|
+
async () => {
|
|
61
|
+
if (++n > 1) throw new Error("upstream down");
|
|
62
|
+
return "v1";
|
|
63
|
+
},
|
|
64
|
+
{ ttl: 10, onError: (e) => errors.push(e) },
|
|
65
|
+
);
|
|
66
|
+
await get();
|
|
67
|
+
await tick(20);
|
|
68
|
+
expect(await get()).toBe("v1");
|
|
69
|
+
await tick(10);
|
|
70
|
+
expect(errors).toHaveLength(1);
|
|
71
|
+
expect(await get()).toBe("v1");
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
test("a failed first build throws and is not cached", async () => {
|
|
75
|
+
let n = 0;
|
|
76
|
+
const get = cached(
|
|
77
|
+
async () => {
|
|
78
|
+
if (++n === 1) throw new Error("first fails");
|
|
79
|
+
return "ok";
|
|
80
|
+
},
|
|
81
|
+
{ ttl: 1000 },
|
|
82
|
+
);
|
|
83
|
+
await expect(get()).rejects.toThrow("first fails");
|
|
84
|
+
expect(await get()).toBe("ok");
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
test("invalidate forces the next call to rebuild", async () => {
|
|
88
|
+
let n = 0;
|
|
89
|
+
const get = cached(async () => ++n, { ttl: 1000 });
|
|
90
|
+
await get();
|
|
91
|
+
get.invalidate();
|
|
92
|
+
expect(await get()).toBe(2);
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
describe("cachedBy", () => {
|
|
97
|
+
test("caches per argument and evicts the least recently used past max", async () => {
|
|
98
|
+
const calls: string[] = [];
|
|
99
|
+
const get = cachedBy(
|
|
100
|
+
async (id: string) => {
|
|
101
|
+
calls.push(id);
|
|
102
|
+
return id.toUpperCase();
|
|
103
|
+
},
|
|
104
|
+
{ ttl: 1000, max: 2 },
|
|
105
|
+
);
|
|
106
|
+
await get("a");
|
|
107
|
+
await get("b");
|
|
108
|
+
await get("a"); // hit: "b" is now the least recently used
|
|
109
|
+
expect(calls).toEqual(["a", "b"]);
|
|
110
|
+
await get("c"); // pushes out "b", not "a"
|
|
111
|
+
await get("a");
|
|
112
|
+
expect(calls).toEqual(["a", "b", "c"]);
|
|
113
|
+
await get("b");
|
|
114
|
+
expect(calls).toEqual(["a", "b", "c", "b"]);
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
test("invalidate(key) drops only that key", async () => {
|
|
118
|
+
let n = 0;
|
|
119
|
+
const get = cachedBy(async (_id: string) => ++n, { ttl: 1000 });
|
|
120
|
+
await get("x");
|
|
121
|
+
await get("y");
|
|
122
|
+
get.invalidate("x");
|
|
123
|
+
expect(await get("y")).toBe(2);
|
|
124
|
+
expect(await get("x")).toBe(3);
|
|
125
|
+
});
|
|
126
|
+
});
|
|
@@ -1,78 +1,78 @@
|
|
|
1
|
-
import { afterEach, describe, expect, test } from "bun:test";
|
|
2
|
-
import {
|
|
3
|
-
CLIENT_ADDRESS_HEADER,
|
|
4
|
-
clientAddress,
|
|
5
|
-
resolveClientAddress,
|
|
6
|
-
} from "../src/server/client-address.ts";
|
|
7
|
-
|
|
8
|
-
const req = (headers: Record<string, string> = {}) =>
|
|
9
|
-
new Request("http://localhost/", { headers });
|
|
10
|
-
const server = (address: string | null) =>
|
|
11
|
-
({
|
|
12
|
-
requestIP: () => (address ? { address, family: "IPv4", port: 1 } : null),
|
|
13
|
-
}) as never;
|
|
14
|
-
|
|
15
|
-
describe("the client address", () => {
|
|
16
|
-
const secret = process.env.GRIMOIRE_INTERNAL_SECRET;
|
|
17
|
-
afterEach(() => {
|
|
18
|
-
if (secret === undefined) delete process.env.GRIMOIRE_INTERNAL_SECRET;
|
|
19
|
-
else process.env.GRIMOIRE_INTERNAL_SECRET = secret;
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
test("a direct connection is the visitor, whatever it claims", () => {
|
|
23
|
-
expect(
|
|
24
|
-
resolveClientAddress(req({ "x-forwarded-for": "1.1.1.1" }), "8.8.8.8"),
|
|
25
|
-
).toBe("8.8.8.8");
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
test("behind a local or private proxy, the visitor is the last forwarded entry", () => {
|
|
29
|
-
for (const proxy of [
|
|
30
|
-
"127.0.0.1",
|
|
31
|
-
"::1",
|
|
32
|
-
"10.0.0.2",
|
|
33
|
-
"172.18.0.5",
|
|
34
|
-
"192.168.1.9",
|
|
35
|
-
"::ffff:127.0.0.1",
|
|
36
|
-
]) {
|
|
37
|
-
expect(
|
|
38
|
-
resolveClientAddress(
|
|
39
|
-
req({ "x-forwarded-for": "6.6.6.6, 5.5.5.5" }),
|
|
40
|
-
proxy,
|
|
41
|
-
),
|
|
42
|
-
).toBe("5.5.5.5");
|
|
43
|
-
}
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
test("172.32.x is public, not Docker's range", () => {
|
|
47
|
-
expect(
|
|
48
|
-
resolveClientAddress(req({ "x-forwarded-for": "5.5.5.5" }), "172.32.0.1"),
|
|
49
|
-
).toBe("172.32.0.1");
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
test("a local connection with no forwarding header is itself", () => {
|
|
53
|
-
expect(resolveClientAddress(req(), "127.0.0.1")).toBe("127.0.0.1");
|
|
54
|
-
expect(
|
|
55
|
-
resolveClientAddress(req({ "x-real-ip": "4.4.4.4" }), "127.0.0.1"),
|
|
56
|
-
).toBe("4.4.4.4");
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
test("outside a worker the coordinator's header is not believed", () => {
|
|
60
|
-
delete process.env.GRIMOIRE_INTERNAL_SECRET;
|
|
61
|
-
expect(
|
|
62
|
-
clientAddress(
|
|
63
|
-
req({ [CLIENT_ADDRESS_HEADER]: "1.1.1.1" }),
|
|
64
|
-
server("8.8.8.8"),
|
|
65
|
-
),
|
|
66
|
-
).toBe("8.8.8.8");
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
test("inside a worker it is the address the coordinator worked out", () => {
|
|
70
|
-
process.env.GRIMOIRE_INTERNAL_SECRET = "s";
|
|
71
|
-
expect(
|
|
72
|
-
clientAddress(
|
|
73
|
-
req({ [CLIENT_ADDRESS_HEADER]: "5.5.5.5" }),
|
|
74
|
-
server("127.0.0.1"),
|
|
75
|
-
),
|
|
76
|
-
).toBe("5.5.5.5");
|
|
77
|
-
});
|
|
78
|
-
});
|
|
1
|
+
import { afterEach, describe, expect, test } from "bun:test";
|
|
2
|
+
import {
|
|
3
|
+
CLIENT_ADDRESS_HEADER,
|
|
4
|
+
clientAddress,
|
|
5
|
+
resolveClientAddress,
|
|
6
|
+
} from "../src/server/client-address.ts";
|
|
7
|
+
|
|
8
|
+
const req = (headers: Record<string, string> = {}) =>
|
|
9
|
+
new Request("http://localhost/", { headers });
|
|
10
|
+
const server = (address: string | null) =>
|
|
11
|
+
({
|
|
12
|
+
requestIP: () => (address ? { address, family: "IPv4", port: 1 } : null),
|
|
13
|
+
}) as never;
|
|
14
|
+
|
|
15
|
+
describe("the client address", () => {
|
|
16
|
+
const secret = process.env.GRIMOIRE_INTERNAL_SECRET;
|
|
17
|
+
afterEach(() => {
|
|
18
|
+
if (secret === undefined) delete process.env.GRIMOIRE_INTERNAL_SECRET;
|
|
19
|
+
else process.env.GRIMOIRE_INTERNAL_SECRET = secret;
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
test("a direct connection is the visitor, whatever it claims", () => {
|
|
23
|
+
expect(
|
|
24
|
+
resolveClientAddress(req({ "x-forwarded-for": "1.1.1.1" }), "8.8.8.8"),
|
|
25
|
+
).toBe("8.8.8.8");
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
test("behind a local or private proxy, the visitor is the last forwarded entry", () => {
|
|
29
|
+
for (const proxy of [
|
|
30
|
+
"127.0.0.1",
|
|
31
|
+
"::1",
|
|
32
|
+
"10.0.0.2",
|
|
33
|
+
"172.18.0.5",
|
|
34
|
+
"192.168.1.9",
|
|
35
|
+
"::ffff:127.0.0.1",
|
|
36
|
+
]) {
|
|
37
|
+
expect(
|
|
38
|
+
resolveClientAddress(
|
|
39
|
+
req({ "x-forwarded-for": "6.6.6.6, 5.5.5.5" }),
|
|
40
|
+
proxy,
|
|
41
|
+
),
|
|
42
|
+
).toBe("5.5.5.5");
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
test("172.32.x is public, not Docker's range", () => {
|
|
47
|
+
expect(
|
|
48
|
+
resolveClientAddress(req({ "x-forwarded-for": "5.5.5.5" }), "172.32.0.1"),
|
|
49
|
+
).toBe("172.32.0.1");
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
test("a local connection with no forwarding header is itself", () => {
|
|
53
|
+
expect(resolveClientAddress(req(), "127.0.0.1")).toBe("127.0.0.1");
|
|
54
|
+
expect(
|
|
55
|
+
resolveClientAddress(req({ "x-real-ip": "4.4.4.4" }), "127.0.0.1"),
|
|
56
|
+
).toBe("4.4.4.4");
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
test("outside a worker the coordinator's header is not believed", () => {
|
|
60
|
+
delete process.env.GRIMOIRE_INTERNAL_SECRET;
|
|
61
|
+
expect(
|
|
62
|
+
clientAddress(
|
|
63
|
+
req({ [CLIENT_ADDRESS_HEADER]: "1.1.1.1" }),
|
|
64
|
+
server("8.8.8.8"),
|
|
65
|
+
),
|
|
66
|
+
).toBe("8.8.8.8");
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
test("inside a worker it is the address the coordinator worked out", () => {
|
|
70
|
+
process.env.GRIMOIRE_INTERNAL_SECRET = "s";
|
|
71
|
+
expect(
|
|
72
|
+
clientAddress(
|
|
73
|
+
req({ [CLIENT_ADDRESS_HEADER]: "5.5.5.5" }),
|
|
74
|
+
server("127.0.0.1"),
|
|
75
|
+
),
|
|
76
|
+
).toBe("5.5.5.5");
|
|
77
|
+
});
|
|
78
|
+
});
|
package/test/client-csrf.test.ts
CHANGED
|
@@ -1,61 +1,61 @@
|
|
|
1
|
-
import { beforeEach, describe, expect, test } from "bun:test";
|
|
2
|
-
import { ensureCsrf, onSubmitEnsureCsrf } from "../src/client/csrf.ts";
|
|
3
|
-
|
|
4
|
-
// The root test/preload.ts installs happy-dom globals; this package's
|
|
5
|
-
// compiler options have no DOM lib, so reach them untyped.
|
|
6
|
-
const { window, document, Event } = globalThis as any;
|
|
7
|
-
|
|
8
|
-
window.location.href = "http://localhost/";
|
|
9
|
-
|
|
10
|
-
const token = (form: any) =>
|
|
11
|
-
form.querySelector('input[name="_csrf"]')?.value ?? null;
|
|
12
|
-
|
|
13
|
-
beforeEach(() => {
|
|
14
|
-
document.cookie = "_csrf=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/";
|
|
15
|
-
document.body.innerHTML = "";
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
describe("client CSRF", () => {
|
|
19
|
-
test("a client-rendered form gets the cookie's token", () => {
|
|
20
|
-
document.cookie = "_csrf=abc123; path=/";
|
|
21
|
-
const form = document.createElement("form") as any;
|
|
22
|
-
form.setAttribute("method", "post");
|
|
23
|
-
ensureCsrf(form);
|
|
24
|
-
expect(token(form)).toBe("abc123");
|
|
25
|
-
expect(form.querySelectorAll('input[name="_csrf"]').length).toBe(1);
|
|
26
|
-
expect(form.firstElementChild.hasAttribute("data-grimoire-csrf")).toBe(
|
|
27
|
-
true,
|
|
28
|
-
);
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
test("a stale SSR token is replaced, not duplicated", () => {
|
|
32
|
-
document.cookie = "_csrf=fresh; path=/";
|
|
33
|
-
document.body.innerHTML = `<form method="post"><input type="hidden" name="_csrf" value="stale" data-grimoire-csrf></form>`;
|
|
34
|
-
const form = document.querySelector("form") as any;
|
|
35
|
-
ensureCsrf(form);
|
|
36
|
-
expect(token(form)).toBe("fresh");
|
|
37
|
-
expect(form.querySelectorAll('input[name="_csrf"]').length).toBe(1);
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
test("no cookie, no change", () => {
|
|
41
|
-
const form = document.createElement("form") as any;
|
|
42
|
-
ensureCsrf(form);
|
|
43
|
-
expect(token(form)).toBeNull();
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
test("the submit listener only touches POST forms", () => {
|
|
47
|
-
document.cookie = "_csrf=tok; path=/";
|
|
48
|
-
document.body.innerHTML = `<form method="post" id="p"></form><form id="g"></form>`;
|
|
49
|
-
document.addEventListener("submit", onSubmitEnsureCsrf as any, true);
|
|
50
|
-
for (const id of ["p", "g"]) {
|
|
51
|
-
document
|
|
52
|
-
.getElementById(id)!
|
|
53
|
-
.dispatchEvent(
|
|
54
|
-
new Event("submit", { bubbles: true, cancelable: true }),
|
|
55
|
-
);
|
|
56
|
-
}
|
|
57
|
-
document.removeEventListener("submit", onSubmitEnsureCsrf as any, true);
|
|
58
|
-
expect(token(document.getElementById("p"))).toBe("tok");
|
|
59
|
-
expect(token(document.getElementById("g"))).toBeNull();
|
|
60
|
-
});
|
|
61
|
-
});
|
|
1
|
+
import { beforeEach, describe, expect, test } from "bun:test";
|
|
2
|
+
import { ensureCsrf, onSubmitEnsureCsrf } from "../src/client/csrf.ts";
|
|
3
|
+
|
|
4
|
+
// The root test/preload.ts installs happy-dom globals; this package's
|
|
5
|
+
// compiler options have no DOM lib, so reach them untyped.
|
|
6
|
+
const { window, document, Event } = globalThis as any;
|
|
7
|
+
|
|
8
|
+
window.location.href = "http://localhost/";
|
|
9
|
+
|
|
10
|
+
const token = (form: any) =>
|
|
11
|
+
form.querySelector('input[name="_csrf"]')?.value ?? null;
|
|
12
|
+
|
|
13
|
+
beforeEach(() => {
|
|
14
|
+
document.cookie = "_csrf=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/";
|
|
15
|
+
document.body.innerHTML = "";
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
describe("client CSRF", () => {
|
|
19
|
+
test("a client-rendered form gets the cookie's token", () => {
|
|
20
|
+
document.cookie = "_csrf=abc123; path=/";
|
|
21
|
+
const form = document.createElement("form") as any;
|
|
22
|
+
form.setAttribute("method", "post");
|
|
23
|
+
ensureCsrf(form);
|
|
24
|
+
expect(token(form)).toBe("abc123");
|
|
25
|
+
expect(form.querySelectorAll('input[name="_csrf"]').length).toBe(1);
|
|
26
|
+
expect(form.firstElementChild.hasAttribute("data-grimoire-csrf")).toBe(
|
|
27
|
+
true,
|
|
28
|
+
);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
test("a stale SSR token is replaced, not duplicated", () => {
|
|
32
|
+
document.cookie = "_csrf=fresh; path=/";
|
|
33
|
+
document.body.innerHTML = `<form method="post"><input type="hidden" name="_csrf" value="stale" data-grimoire-csrf></form>`;
|
|
34
|
+
const form = document.querySelector("form") as any;
|
|
35
|
+
ensureCsrf(form);
|
|
36
|
+
expect(token(form)).toBe("fresh");
|
|
37
|
+
expect(form.querySelectorAll('input[name="_csrf"]').length).toBe(1);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
test("no cookie, no change", () => {
|
|
41
|
+
const form = document.createElement("form") as any;
|
|
42
|
+
ensureCsrf(form);
|
|
43
|
+
expect(token(form)).toBeNull();
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
test("the submit listener only touches POST forms", () => {
|
|
47
|
+
document.cookie = "_csrf=tok; path=/";
|
|
48
|
+
document.body.innerHTML = `<form method="post" id="p"></form><form id="g"></form>`;
|
|
49
|
+
document.addEventListener("submit", onSubmitEnsureCsrf as any, true);
|
|
50
|
+
for (const id of ["p", "g"]) {
|
|
51
|
+
document
|
|
52
|
+
.getElementById(id)!
|
|
53
|
+
.dispatchEvent(
|
|
54
|
+
new Event("submit", { bubbles: true, cancelable: true }),
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
document.removeEventListener("submit", onSubmitEnsureCsrf as any, true);
|
|
58
|
+
expect(token(document.getElementById("p"))).toBe("tok");
|
|
59
|
+
expect(token(document.getElementById("g"))).toBeNull();
|
|
60
|
+
});
|
|
61
|
+
});
|