@sigil-dev/grimoire 0.9.29 → 0.9.30

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.
Files changed (105) hide show
  1. package/bin/sigil.ts +4 -0
  2. package/index.ts +16 -10
  3. package/package.json +14 -6
  4. package/server.ts +2 -2
  5. package/src/adapters/bun.ts +17 -17
  6. package/src/adapters/static.ts +41 -41
  7. package/src/adapters/types.ts +36 -36
  8. package/src/base.ts +4 -1
  9. package/src/client/csrf.ts +47 -47
  10. package/src/client/deferred.ts +1 -4
  11. package/src/client/enhance.ts +160 -160
  12. package/src/client/router.ts +927 -926
  13. package/src/config-file.ts +84 -0
  14. package/src/dev/compile-module.ts +199 -193
  15. package/src/dev/hmr-client.ts +229 -229
  16. package/src/dev/hmr-server.ts +274 -266
  17. package/src/dev/loader.ts +144 -144
  18. package/src/dev/runtime-bundle.ts +5 -3
  19. package/src/duration.ts +46 -0
  20. package/src/env/modules.ts +62 -62
  21. package/src/env/required.ts +42 -42
  22. package/src/env/typegen.ts +61 -61
  23. package/src/headers.ts +105 -105
  24. package/src/logger/transports.ts +117 -117
  25. package/src/plugin-order.ts +88 -0
  26. package/src/rendering/head.ts +49 -49
  27. package/src/rendering/hydrate.ts +162 -162
  28. package/src/rendering/images.ts +43 -43
  29. package/src/rendering/preload.ts +22 -8
  30. package/src/rendering/ssrPlugin.ts +107 -107
  31. package/src/rendering/timing.ts +40 -40
  32. package/src/routing/route-ids.ts +65 -0
  33. package/src/routing/route.ts +65 -0
  34. package/src/routing/router.ts +84 -32
  35. package/src/routing/scanner.ts +18 -5
  36. package/src/routing/transform-routes.ts +102 -102
  37. package/src/server/after-response.ts +206 -206
  38. package/src/server/asset-cache.ts +57 -57
  39. package/src/server/build.ts +304 -295
  40. package/src/server/cached.ts +114 -114
  41. package/src/server/client-address.ts +82 -82
  42. package/src/server/client-dir.ts +63 -63
  43. package/src/server/client-entry.ts +44 -44
  44. package/src/server/context.ts +18 -10
  45. package/src/server/cookie-utils.ts +80 -80
  46. package/src/server/coordinator.ts +476 -476
  47. package/src/server/hooks.ts +122 -122
  48. package/src/server/index.ts +8 -0
  49. package/src/server/plugins.ts +172 -172
  50. package/src/server/prerender.ts +473 -473
  51. package/src/server/reserved.ts +17 -17
  52. package/src/server/resolver.ts +149 -123
  53. package/src/server/rewrite.ts +28 -28
  54. package/src/server/warm.ts +9 -3
  55. package/src/server/with-headers.ts +27 -27
  56. package/src/server/worker.ts +2 -6
  57. package/src/server-only.ts +78 -0
  58. package/src/testing/dom.ts +73 -73
  59. package/src/testing/index.ts +169 -169
  60. package/src/testing/server.ts +1 -0
  61. package/src/typegen/index.ts +97 -2
  62. package/src/types.ts +140 -19
  63. package/test/after-response.test.ts +132 -132
  64. package/test/asset-cache-policy.test.ts +6 -2
  65. package/test/base.test.ts +20 -6
  66. package/test/cached.test.ts +126 -126
  67. package/test/client-address.test.ts +78 -78
  68. package/test/client-csrf.test.ts +61 -61
  69. package/test/client-dir.test.ts +101 -101
  70. package/test/client-router.test.ts +37 -5
  71. package/test/config-file.test.ts +84 -0
  72. package/test/coordinator.test.ts +9 -2
  73. package/test/deferred.test.ts +4 -2
  74. package/test/dev-loader.test.ts +30 -30
  75. package/test/env.test.ts +171 -171
  76. package/test/force-production.test.ts +7 -2
  77. package/test/images.test.ts +20 -8
  78. package/test/locals-header.test.ts +61 -61
  79. package/test/middleware.test.ts +243 -243
  80. package/test/parser-plugins.test.ts +98 -92
  81. package/test/plugin-handle.test.ts +62 -62
  82. package/test/plugin-order.test.ts +98 -0
  83. package/test/plugin-routes.test.ts +47 -42
  84. package/test/plugin-services-context.test.ts +7 -2
  85. package/test/preload.test.ts +4 -1
  86. package/test/prerender.test.ts +3 -1
  87. package/test/rendering.test.ts +3 -2
  88. package/test/required-env.test.ts +32 -32
  89. package/test/resolver-windows.test.ts +22 -23
  90. package/test/rewrite-html.test.ts +90 -90
  91. package/test/route-group-layouts.test.ts +38 -36
  92. package/test/route.test.ts +103 -0
  93. package/test/routing.test.ts +225 -197
  94. package/test/scanning.test.ts +53 -33
  95. package/test/server-context.test.ts +22 -0
  96. package/test/server-only.test.ts +110 -0
  97. package/test/server.test.ts +640 -640
  98. package/test/services.test.ts +3 -3
  99. package/test/streaming.test.ts +11 -6
  100. package/test/testing-helpers.test.ts +166 -166
  101. package/test/typegen.test.ts +209 -112
  102. package/test/warm.test.ts +96 -96
  103. package/test/worker-context.test.ts +59 -59
  104. package/test/workspace-files.test.ts +26 -10
  105. package/test/workspace-versions.test.ts +146 -131
@@ -1,6 +1,6 @@
1
1
  import { describe, expect, test } from "bun:test";
2
2
  import { execFileSync } from "node:child_process";
3
- import { existsSync, readFileSync, readdirSync } from "node:fs";
3
+ import { existsSync, readdirSync, readFileSync } from "node:fs";
4
4
  import { dirname, join, normalize, relative } from "node:path";
5
5
  import { fileURLToPath } from "node:url";
6
6
 
@@ -69,7 +69,13 @@ function resolveImport(from: string, spec: string): string | null {
69
69
  const base = normalize(join(dirname(from), spec));
70
70
  // TypeScript's `./x.js` for `./x.ts`.
71
71
  const ts = base.replace(/\.js$/, ".ts");
72
- for (const candidate of [base, ts, `${base}.ts`, `${base}.tsx`, join(base, "index.ts")]) {
72
+ for (const candidate of [
73
+ base,
74
+ ts,
75
+ `${base}.ts`,
76
+ `${base}.tsx`,
77
+ join(base, "index.ts"),
78
+ ]) {
73
79
  if (existsSync(candidate) && SOURCE.test(candidate)) return candidate;
74
80
  }
75
81
  return null;
@@ -78,7 +84,9 @@ function resolveImport(from: string, spec: string): string | null {
78
84
  /** The files the entry points reach through relative imports. */
79
85
  function reachable(dir: string, entries: string[]): Set<string> {
80
86
  const seen = new Set<string>();
81
- const queue = entries.map((e) => normalize(join(dir, e))).filter((f) => SOURCE.test(f));
87
+ const queue = entries
88
+ .map((e) => normalize(join(dir, e)))
89
+ .filter((f) => SOURCE.test(f));
82
90
  while (queue.length) {
83
91
  const file = queue.pop()!;
84
92
  if (seen.has(file) || !existsSync(file)) continue;
@@ -96,11 +104,15 @@ function reachable(dir: string, entries: string[]): Set<string> {
96
104
  }
97
105
 
98
106
  function packedFiles(dir: string): Set<string> {
99
- const out = execFileSync("npm", ["pack", "--dry-run", "--json", "--ignore-scripts"], {
100
- cwd: dir,
101
- encoding: "utf8",
102
- stdio: ["ignore", "pipe", "ignore"],
103
- });
107
+ const out = execFileSync(
108
+ "npm",
109
+ ["pack", "--dry-run", "--json", "--ignore-scripts"],
110
+ {
111
+ cwd: dir,
112
+ encoding: "utf8",
113
+ stdio: ["ignore", "pipe", "ignore"],
114
+ },
115
+ );
104
116
  const [report] = JSON.parse(out) as Array<{ files: Array<{ path: string }> }>;
105
117
  return new Set(report!.files.map((f) => normalize(join(dir, f.path))));
106
118
  }
@@ -118,7 +130,9 @@ describe("workspace files", () => {
118
130
  const packed = packedFiles(dir);
119
131
  for (const file of reachable(dir, entries)) {
120
132
  if (!packed.has(file)) {
121
- missing.push(`${manifest.name}: ${relative(dir, file)} is imported but not packed`);
133
+ missing.push(
134
+ `${manifest.name}: ${relative(dir, file)} is imported but not packed`,
135
+ );
122
136
  }
123
137
  }
124
138
  }
@@ -139,7 +153,9 @@ describe("workspace files", () => {
139
153
  const spec = m[1] ?? m[2] ?? m[3];
140
154
  // `./$types` is generated per route, not a file here.
141
155
  if (spec && !spec.includes("$") && !resolveImport(file, spec)) {
142
- broken.push(`${manifest.name}: ${relative(dir, file)} imports ${spec}`);
156
+ broken.push(
157
+ `${manifest.name}: ${relative(dir, file)} imports ${spec}`,
158
+ );
143
159
  }
144
160
  }
145
161
  }
@@ -1,131 +1,146 @@
1
- import { describe, expect, test } from "bun:test";
2
- import { readFileSync, readdirSync } from "node:fs";
3
- import { join } from "node:path";
4
-
5
- /**
6
- * A packed tarball must not carry a version its manifest does not have.
7
- *
8
- * `bun pm pack` rewrites `workspace:*` to a concrete version by reading the
9
- * *lockfile*, not the manifest. So editing a package.json to bump a version
10
- * and then packing, without installing first, produces a tarball that
11
- * silently depends on the old version of a sibling.
12
- *
13
- * Not hypothetical. Bumping `packages/compiler` to 0.9.12 without running
14
- * `bun install` left `bun.lock` at 0.9.10, so every grimoire tarball packed
15
- * afterwards pinned `@sigil-dev/compiler: 0.9.10`. The app installed it and
16
- * failed at boot with
17
- *
18
- * Export named 'clientStyleSnippet' not found in
19
- * .../@sigil-dev/compiler/src/babel/util/css.ts
20
- *
21
- * which reads like a compiler bug and is really a packaging one. It cost a
22
- * long time to find, because nothing in the failure mentioned a version.
23
- */
24
-
25
- const ROOT = join(import.meta.dir, "../../..");
26
- const lock = readFileSync(join(ROOT, "bun.lock"), "utf8");
27
-
28
- interface Manifest {
29
- name?: string;
30
- version?: string;
31
- private?: boolean;
32
- }
33
-
34
- /** Every workspace under `packages/*`, whether or not it carries a version. */
35
- function allWorkspaces(): { dir: string; manifest: Manifest }[] {
36
- return readdirSync(join(ROOT, "packages"), { withFileTypes: true })
37
- .filter((e) => e.isDirectory())
38
- .flatMap((e) => {
39
- try {
40
- const manifest = JSON.parse(
41
- readFileSync(join(ROOT, "packages", e.name, "package.json"), "utf8"),
42
- ) as Manifest;
43
- return [{ dir: e.name, manifest }];
44
- } catch {
45
- return [];
46
- }
47
- })
48
- .filter(({ manifest }) => manifest.name?.startsWith("@sigil-dev/"));
49
- }
50
-
51
- /**
52
- * The ones an app can install. `site` and the two demos carry no version and
53
- * are never packed, so demanding one of them would be this test failing for a
54
- * reason that has nothing to do with the bug it exists to catch.
55
- */
56
- function shipped(): { dir: string; manifest: Manifest & { name: string; version: string } }[] {
57
- return allWorkspaces().filter(
58
- (w): w is { dir: string; manifest: Manifest & { name: string; version: string } } =>
59
- typeof w.manifest.name === "string" &&
60
- typeof w.manifest.version === "string" &&
61
- !w.manifest.private,
62
- );
63
- }
64
-
65
- describe("workspace versions", () => {
66
- test("the lockfile agrees with every shipped manifest", () => {
67
- // bun.lock records each workspace as
68
- // "packages/compiler": { "name": "@sigil-dev/compiler", "version": "0.9.12",
69
- // so a stale entry is a correct name next to a wrong version.
70
- const mismatches: string[] = [];
71
- for (const { dir, manifest } of shipped()) {
72
- const entry = new RegExp(
73
- `"packages/${dir}": \\{\\s*"name": "${manifest.name}",\\s*"version": "([^"]+)"`,
74
- ).exec(lock);
75
- if (!entry) {
76
- mismatches.push(`${manifest.name}: absent from bun.lock`);
77
- continue;
78
- }
79
- if (entry[1] !== manifest.version) {
80
- mismatches.push(
81
- `${manifest.name}: manifest says ${manifest.version} but bun.lock says ` +
82
- `${entry[1]} — run \`bun install\` before \`bun pm pack\`, or the ` +
83
- `tarball pins the old version of a sibling`,
84
- );
85
- }
86
- }
87
- expect(mismatches).toEqual([]);
88
- });
89
-
90
- test("every shipped package is at the same version", () => {
91
- // Two packages at different versions means a `workspace:*` between
92
- // them resolves to whichever one the lockfile saw, which is how a
93
- // packed dependency ends up pointing at a version nothing claims.
94
- const distinct = [...new Set(shipped().map(({ manifest }) => manifest.version))];
95
- expect(
96
- distinct.length === 1
97
- ? []
98
- : [`packages sit at ${distinct.join(", ")} — a release moves them together`],
99
- ).toEqual([]);
100
- });
101
-
102
- test("the release version is not already published with other content", async () => {
103
- // The same trap wearing a different hat. `@sigil-dev/grimoire@0.9.12`
104
- // was on npm, published before plugin services existed, while this
105
- // tree kept calling itself 0.9.12. An app pinning that exact version
106
- // installed the registry copy *beside* the local tarball: two copies,
107
- // both reporting 0.9.12, one with no `collectServices` at all.
108
- //
109
- // So a version is only safe to depend on once it is published *with*
110
- // this content. Until then a consumer resolves to the older tarball.
111
- const version = shipped()[0]?.manifest.version;
112
- if (!version) return;
113
-
114
- // Registry access is best-effort: a suite that needs the network to
115
- // pass is a suite that gets skipped in CI without saying so.
116
- const res = await fetch("https://registry.npmjs.org/@sigil-dev%2Fgrimoire", {
117
- signal: AbortSignal.timeout(4000),
118
- }).catch(() => null);
119
- if (!res?.ok) return;
120
- const body = (await res.json()) as { versions?: Record<string, unknown> };
121
- const alreadyThere = Boolean(body.versions?.[version]);
122
- expect(
123
- alreadyThere
124
- ? []
125
- : [
126
- `${version} is not on npm yet, so it cannot be pinned by a consumer`,
127
- `without pulling a different tarball at the same version number`,
128
- ],
129
- ).toEqual([]);
130
- });
131
- });
1
+ import { describe, expect, test } from "bun:test";
2
+ import { readdirSync, readFileSync } from "node:fs";
3
+ import { join } from "node:path";
4
+
5
+ /**
6
+ * A packed tarball must not carry a version its manifest does not have.
7
+ *
8
+ * `bun pm pack` rewrites `workspace:*` to a concrete version by reading the
9
+ * *lockfile*, not the manifest. So editing a package.json to bump a version
10
+ * and then packing, without installing first, produces a tarball that
11
+ * silently depends on the old version of a sibling.
12
+ *
13
+ * Not hypothetical. Bumping `packages/compiler` to 0.9.12 without running
14
+ * `bun install` left `bun.lock` at 0.9.10, so every grimoire tarball packed
15
+ * afterwards pinned `@sigil-dev/compiler: 0.9.10`. The app installed it and
16
+ * failed at boot with
17
+ *
18
+ * Export named 'clientStyleSnippet' not found in
19
+ * .../@sigil-dev/compiler/src/babel/util/css.ts
20
+ *
21
+ * which reads like a compiler bug and is really a packaging one. It cost a
22
+ * long time to find, because nothing in the failure mentioned a version.
23
+ */
24
+
25
+ const ROOT = join(import.meta.dir, "../../..");
26
+ const lock = readFileSync(join(ROOT, "bun.lock"), "utf8");
27
+
28
+ interface Manifest {
29
+ name?: string;
30
+ version?: string;
31
+ private?: boolean;
32
+ }
33
+
34
+ /** Every workspace under `packages/*`, whether or not it carries a version. */
35
+ function allWorkspaces(): { dir: string; manifest: Manifest }[] {
36
+ return readdirSync(join(ROOT, "packages"), { withFileTypes: true })
37
+ .filter((e) => e.isDirectory())
38
+ .flatMap((e) => {
39
+ try {
40
+ const manifest = JSON.parse(
41
+ readFileSync(join(ROOT, "packages", e.name, "package.json"), "utf8"),
42
+ ) as Manifest;
43
+ return [{ dir: e.name, manifest }];
44
+ } catch {
45
+ return [];
46
+ }
47
+ })
48
+ .filter(({ manifest }) => manifest.name?.startsWith("@sigil-dev/"));
49
+ }
50
+
51
+ /**
52
+ * The ones an app can install. `site` and the two demos carry no version and
53
+ * are never packed, so demanding one of them would be this test failing for a
54
+ * reason that has nothing to do with the bug it exists to catch.
55
+ */
56
+ function shipped(): {
57
+ dir: string;
58
+ manifest: Manifest & { name: string; version: string };
59
+ }[] {
60
+ return allWorkspaces().filter(
61
+ (
62
+ w,
63
+ ): w is {
64
+ dir: string;
65
+ manifest: Manifest & { name: string; version: string };
66
+ } =>
67
+ typeof w.manifest.name === "string" &&
68
+ typeof w.manifest.version === "string" &&
69
+ !w.manifest.private,
70
+ );
71
+ }
72
+
73
+ describe("workspace versions", () => {
74
+ test("the lockfile agrees with every shipped manifest", () => {
75
+ // bun.lock records each workspace as
76
+ // "packages/compiler": { "name": "@sigil-dev/compiler", "version": "0.9.12",
77
+ // so a stale entry is a correct name next to a wrong version.
78
+ const mismatches: string[] = [];
79
+ for (const { dir, manifest } of shipped()) {
80
+ const entry = new RegExp(
81
+ `"packages/${dir}": \\{\\s*"name": "${manifest.name}",\\s*"version": "([^"]+)"`,
82
+ ).exec(lock);
83
+ if (!entry) {
84
+ mismatches.push(`${manifest.name}: absent from bun.lock`);
85
+ continue;
86
+ }
87
+ if (entry[1] !== manifest.version) {
88
+ mismatches.push(
89
+ `${manifest.name}: manifest says ${manifest.version} but bun.lock says ` +
90
+ `${entry[1]} — run \`bun install\` before \`bun pm pack\`, or the ` +
91
+ `tarball pins the old version of a sibling`,
92
+ );
93
+ }
94
+ }
95
+ expect(mismatches).toEqual([]);
96
+ });
97
+
98
+ test("every shipped package is at the same version", () => {
99
+ // Two packages at different versions means a `workspace:*` between
100
+ // them resolves to whichever one the lockfile saw, which is how a
101
+ // packed dependency ends up pointing at a version nothing claims.
102
+ const distinct = [
103
+ ...new Set(shipped().map(({ manifest }) => manifest.version)),
104
+ ];
105
+ expect(
106
+ distinct.length === 1
107
+ ? []
108
+ : [
109
+ `packages sit at ${distinct.join(", ")} — a release moves them together`,
110
+ ],
111
+ ).toEqual([]);
112
+ });
113
+
114
+ test("the release version is not already published with other content", async () => {
115
+ // The same trap wearing a different hat. `@sigil-dev/grimoire@0.9.12`
116
+ // was on npm, published before plugin services existed, while this
117
+ // tree kept calling itself 0.9.12. An app pinning that exact version
118
+ // installed the registry copy *beside* the local tarball: two copies,
119
+ // both reporting 0.9.12, one with no `collectServices` at all.
120
+ //
121
+ // So a version is only safe to depend on once it is published *with*
122
+ // this content. Until then a consumer resolves to the older tarball.
123
+ const version = shipped()[0]?.manifest.version;
124
+ if (!version) return;
125
+
126
+ // Registry access is best-effort: a suite that needs the network to
127
+ // pass is a suite that gets skipped in CI without saying so.
128
+ const res = await fetch(
129
+ "https://registry.npmjs.org/@sigil-dev%2Fgrimoire",
130
+ {
131
+ signal: AbortSignal.timeout(4000),
132
+ },
133
+ ).catch(() => null);
134
+ if (!res?.ok) return;
135
+ const body = (await res.json()) as { versions?: Record<string, unknown> };
136
+ const alreadyThere = Boolean(body.versions?.[version]);
137
+ expect(
138
+ alreadyThere
139
+ ? []
140
+ : [
141
+ `${version} is not on npm yet, so it cannot be pinned by a consumer`,
142
+ `without pulling a different tarball at the same version number`,
143
+ ],
144
+ ).toEqual([]);
145
+ });
146
+ });