@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/services.test.ts
CHANGED
|
@@ -1,90 +1,90 @@
|
|
|
1
|
-
import { describe, expect, test } from "bun:test";
|
|
2
|
-
import { collectServices } from "../src/server/plugins.ts";
|
|
3
|
-
import type { GrimoirePlugin } from "../src/types.ts";
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* `services()` is how a plugin publishes a value the app reads as
|
|
7
|
-
* `locals.<key>`, so the config file stays a config file: the app neither
|
|
8
|
-
* imports a plugin instance out of it nor keeps a module alive to reach it.
|
|
9
|
-
*
|
|
10
|
-
* The properties that matter and are easy to get wrong: order decides
|
|
11
|
-
* conflicts, an async plugin is awaited, a plugin that throws must not
|
|
12
|
-
* silently publish half a set, and undefined must not shadow a real value.
|
|
13
|
-
*/
|
|
14
|
-
|
|
15
|
-
const plugin = (
|
|
16
|
-
name: string,
|
|
17
|
-
services?: GrimoirePlugin["services"],
|
|
18
|
-
): GrimoirePlugin => ({ name, services });
|
|
19
|
-
|
|
20
|
-
describe("collectServices", () => {
|
|
21
|
-
test("returns nothing when no plugin declares services", async () => {
|
|
22
|
-
expect(await collectServices([plugin("a"), plugin("b")])).toEqual({});
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
test("merges keys from several plugins", async () => {
|
|
26
|
-
const result = await collectServices([
|
|
27
|
-
plugin("a", () => ({ one: 1 })),
|
|
28
|
-
plugin("b", () => ({ two: 2 })),
|
|
29
|
-
]);
|
|
30
|
-
expect(result).toEqual({ one: 1, two: 2 });
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
test("later plugins win on a key collision", async () => {
|
|
34
|
-
// Same rule as every other merge in the pipeline: plugin order is
|
|
35
|
-
// priority order, and plugins are listed deliberately.
|
|
36
|
-
const result = await collectServices([
|
|
37
|
-
plugin("a", () => ({ shared: "first" })),
|
|
38
|
-
plugin("b", () => ({ shared: "second" })),
|
|
39
|
-
]);
|
|
40
|
-
expect(result.shared).toBe("second");
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
test("awaits an async plugin", async () => {
|
|
44
|
-
const result = await collectServices([
|
|
45
|
-
plugin("a", async () => {
|
|
46
|
-
await new Promise((r) => setTimeout(r, 5));
|
|
47
|
-
return { ready: true };
|
|
48
|
-
}),
|
|
49
|
-
]);
|
|
50
|
-
// Without the await this is a Promise, and `ready` would be missing.
|
|
51
|
-
expect(result).toEqual({ ready: true });
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
test("a plugin with no services hook is skipped", async () => {
|
|
55
|
-
const result = await collectServices([
|
|
56
|
-
plugin("no-hook"),
|
|
57
|
-
plugin("has-hook", () => ({ ok: 1 })),
|
|
58
|
-
]);
|
|
59
|
-
expect(result).toEqual({ ok: 1 });
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
test("undefined does not shadow an earlier value", async () => {
|
|
63
|
-
// A plugin that returns `foo: undefined` has not provided foo. If it
|
|
64
|
-
// were stored, the key would exist with no value and a later
|
|
65
|
-
// `key in locals` check would treat it as already set.
|
|
66
|
-
const result = await collectServices([
|
|
67
|
-
plugin("a", () => ({ db: "real" })),
|
|
68
|
-
plugin("b", () => ({ db: undefined })),
|
|
69
|
-
]);
|
|
70
|
-
expect(result.db).toBe("real");
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
test("a throwing plugin propagates rather than publishing half a set", async () => {
|
|
74
|
-
// Failing loudly is the point: a queue that never initialised must
|
|
75
|
-
// not look like a working one at boot.
|
|
76
|
-
const bad = plugin("bad", () => {
|
|
77
|
-
throw new Error("no database");
|
|
78
|
-
});
|
|
79
|
-
expect(collectServices([plugin("a", () => ({ ok: 1 })), bad])).rejects.toThrow(
|
|
80
|
-
"no database",
|
|
81
|
-
);
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
test("preserves falsy values that are real values", async () => {
|
|
85
|
-
const result = await collectServices([
|
|
86
|
-
plugin("a", () => ({ flag: false, count: 0, name: "" })),
|
|
87
|
-
]);
|
|
88
|
-
expect(result).toEqual({ flag: false, count: 0, name: "" });
|
|
89
|
-
});
|
|
90
|
-
});
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import { collectServices } from "../src/server/plugins.ts";
|
|
3
|
+
import type { GrimoirePlugin } from "../src/types.ts";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* `services()` is how a plugin publishes a value the app reads as
|
|
7
|
+
* `locals.<key>`, so the config file stays a config file: the app neither
|
|
8
|
+
* imports a plugin instance out of it nor keeps a module alive to reach it.
|
|
9
|
+
*
|
|
10
|
+
* The properties that matter and are easy to get wrong: order decides
|
|
11
|
+
* conflicts, an async plugin is awaited, a plugin that throws must not
|
|
12
|
+
* silently publish half a set, and undefined must not shadow a real value.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
const plugin = (
|
|
16
|
+
name: string,
|
|
17
|
+
services?: GrimoirePlugin["services"],
|
|
18
|
+
): GrimoirePlugin => ({ name, services });
|
|
19
|
+
|
|
20
|
+
describe("collectServices", () => {
|
|
21
|
+
test("returns nothing when no plugin declares services", async () => {
|
|
22
|
+
expect(await collectServices([plugin("a"), plugin("b")])).toEqual({});
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
test("merges keys from several plugins", async () => {
|
|
26
|
+
const result = await collectServices([
|
|
27
|
+
plugin("a", () => ({ one: 1 })),
|
|
28
|
+
plugin("b", () => ({ two: 2 })),
|
|
29
|
+
]);
|
|
30
|
+
expect(result).toEqual({ one: 1, two: 2 });
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
test("later plugins win on a key collision", async () => {
|
|
34
|
+
// Same rule as every other merge in the pipeline: plugin order is
|
|
35
|
+
// priority order, and plugins are listed deliberately.
|
|
36
|
+
const result = await collectServices([
|
|
37
|
+
plugin("a", () => ({ shared: "first" })),
|
|
38
|
+
plugin("b", () => ({ shared: "second" })),
|
|
39
|
+
]);
|
|
40
|
+
expect(result.shared).toBe("second");
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
test("awaits an async plugin", async () => {
|
|
44
|
+
const result = await collectServices([
|
|
45
|
+
plugin("a", async () => {
|
|
46
|
+
await new Promise((r) => setTimeout(r, 5));
|
|
47
|
+
return { ready: true };
|
|
48
|
+
}),
|
|
49
|
+
]);
|
|
50
|
+
// Without the await this is a Promise, and `ready` would be missing.
|
|
51
|
+
expect(result).toEqual({ ready: true });
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
test("a plugin with no services hook is skipped", async () => {
|
|
55
|
+
const result = await collectServices([
|
|
56
|
+
plugin("no-hook"),
|
|
57
|
+
plugin("has-hook", () => ({ ok: 1 })),
|
|
58
|
+
]);
|
|
59
|
+
expect(result).toEqual({ ok: 1 });
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
test("undefined does not shadow an earlier value", async () => {
|
|
63
|
+
// A plugin that returns `foo: undefined` has not provided foo. If it
|
|
64
|
+
// were stored, the key would exist with no value and a later
|
|
65
|
+
// `key in locals` check would treat it as already set.
|
|
66
|
+
const result = await collectServices([
|
|
67
|
+
plugin("a", () => ({ db: "real" })),
|
|
68
|
+
plugin("b", () => ({ db: undefined })),
|
|
69
|
+
]);
|
|
70
|
+
expect(result.db).toBe("real");
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
test("a throwing plugin propagates rather than publishing half a set", async () => {
|
|
74
|
+
// Failing loudly is the point: a queue that never initialised must
|
|
75
|
+
// not look like a working one at boot.
|
|
76
|
+
const bad = plugin("bad", () => {
|
|
77
|
+
throw new Error("no database");
|
|
78
|
+
});
|
|
79
|
+
expect(collectServices([plugin("a", () => ({ ok: 1 })), bad])).rejects.toThrow(
|
|
80
|
+
"no database",
|
|
81
|
+
);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
test("preserves falsy values that are real values", async () => {
|
|
85
|
+
const result = await collectServices([
|
|
86
|
+
plugin("a", () => ({ flag: false, count: 0, name: "" })),
|
|
87
|
+
]);
|
|
88
|
+
expect(result).toEqual({ flag: false, count: 0, name: "" });
|
|
89
|
+
});
|
|
90
|
+
});
|