@sigil-dev/grimoire 0.9.28 → 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.
- package/bin/sigil.ts +4 -0
- package/index.ts +16 -10
- package/package.json +14 -6
- package/server.ts +2 -2
- package/src/adapters/bun.ts +17 -17
- package/src/adapters/static.ts +41 -41
- package/src/adapters/types.ts +36 -36
- package/src/base.ts +4 -1
- package/src/client/csrf.ts +47 -47
- package/src/client/deferred.ts +1 -4
- package/src/client/enhance.ts +160 -160
- package/src/client/router.ts +927 -926
- package/src/config-file.ts +84 -0
- package/src/dev/compile-module.ts +199 -193
- package/src/dev/hmr-client.ts +229 -229
- package/src/dev/hmr-server.ts +274 -266
- package/src/dev/loader.ts +144 -144
- package/src/dev/runtime-bundle.ts +5 -3
- package/src/duration.ts +46 -0
- package/src/env/modules.ts +62 -62
- package/src/env/required.ts +42 -42
- package/src/env/typegen.ts +61 -61
- package/src/headers.ts +105 -105
- package/src/logger/transports.ts +117 -117
- package/src/plugin-order.ts +88 -0
- package/src/rendering/head.ts +49 -49
- package/src/rendering/hydrate.ts +162 -162
- package/src/rendering/images.ts +43 -43
- package/src/rendering/preload.ts +22 -8
- package/src/rendering/ssrPlugin.ts +107 -107
- package/src/rendering/timing.ts +40 -40
- package/src/routing/route-ids.ts +65 -0
- package/src/routing/route.ts +65 -0
- package/src/routing/router.ts +84 -32
- package/src/routing/scanner.ts +18 -5
- 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 +304 -295
- package/src/server/cached.ts +114 -114
- package/src/server/client-address.ts +82 -82
- package/src/server/client-dir.ts +63 -63
- package/src/server/client-entry.ts +44 -44
- package/src/server/context.ts +18 -10
- package/src/server/cookie-utils.ts +80 -80
- package/src/server/coordinator.ts +476 -476
- package/src/server/hooks.ts +122 -122
- package/src/server/index.ts +8 -0
- package/src/server/plugins.ts +172 -172
- package/src/server/prerender.ts +473 -473
- package/src/server/reserved.ts +17 -17
- package/src/server/resolver.ts +149 -123
- package/src/server/rewrite.ts +28 -28
- package/src/server/warm.ts +9 -3
- package/src/server/with-headers.ts +27 -27
- package/src/server/worker.ts +2 -6
- package/src/server-only.ts +78 -0
- package/src/testing/dom.ts +73 -73
- package/src/testing/index.ts +169 -169
- package/src/testing/server.ts +1 -0
- package/src/typegen/index.ts +97 -2
- package/src/types.ts +140 -19
- package/test/after-response.test.ts +132 -132
- package/test/asset-cache-policy.test.ts +6 -2
- package/test/base.test.ts +20 -6
- 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 +37 -5
- package/test/config-file.test.ts +84 -0
- package/test/coordinator.test.ts +9 -2
- package/test/deferred.test.ts +4 -2
- package/test/dev-loader.test.ts +30 -30
- package/test/env.test.ts +171 -171
- package/test/force-production.test.ts +7 -2
- package/test/images.test.ts +20 -8
- package/test/locals-header.test.ts +61 -61
- package/test/middleware.test.ts +243 -243
- package/test/parser-plugins.test.ts +98 -92
- package/test/plugin-handle.test.ts +62 -62
- package/test/plugin-order.test.ts +98 -0
- package/test/plugin-routes.test.ts +47 -42
- package/test/plugin-services-context.test.ts +7 -2
- package/test/preload.test.ts +4 -1
- package/test/prerender.test.ts +3 -1
- package/test/rendering.test.ts +3 -2
- package/test/required-env.test.ts +32 -32
- package/test/resolver-windows.test.ts +22 -23
- package/test/rewrite-html.test.ts +90 -90
- package/test/route-group-layouts.test.ts +38 -36
- package/test/route.test.ts +103 -0
- package/test/routing.test.ts +225 -197
- package/test/scanning.test.ts +53 -33
- package/test/server-context.test.ts +22 -0
- package/test/server-only.test.ts +110 -0
- package/test/server.test.ts +640 -640
- package/test/services.test.ts +3 -3
- package/test/streaming.test.ts +11 -6
- package/test/testing-helpers.test.ts +166 -166
- package/test/typegen.test.ts +209 -112
- package/test/warm.test.ts +96 -96
- package/test/worker-context.test.ts +59 -59
- package/test/workspace-files.test.ts +26 -10
- package/test/workspace-versions.test.ts +146 -131
package/test/typegen.test.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { afterAll, beforeAll, describe, expect, test } from "bun:test";
|
|
|
2
2
|
import { existsSync } from "node:fs";
|
|
3
3
|
import { mkdir, readFile, rm, writeFile } from "node:fs/promises";
|
|
4
4
|
import { tmpdir } from "node:os";
|
|
5
|
-
import { join, sep } from "node:path";
|
|
5
|
+
import { join, resolve, sep } from "node:path";
|
|
6
6
|
import type { RouteFile } from "../src/routing/scanner";
|
|
7
7
|
import { scanRoutes } from "../src/routing/scanner";
|
|
8
8
|
import {
|
|
@@ -601,7 +601,10 @@ export async function comment() {
|
|
|
601
601
|
);
|
|
602
602
|
|
|
603
603
|
// the scanner rejects a +page.server.ts that has no page beside it
|
|
604
|
-
await writeFile(
|
|
604
|
+
await writeFile(
|
|
605
|
+
join(routeDir, "+page.tsx"),
|
|
606
|
+
"export default () => null;\n",
|
|
607
|
+
);
|
|
605
608
|
|
|
606
609
|
// a +server.ts route: its types must compile without Bun's types installed
|
|
607
610
|
const chatDir = join(tscDir, "src", "routes", "chat");
|
|
@@ -795,123 +798,217 @@ describe("tsconfig paths for aliases", () => {
|
|
|
795
798
|
"utf-8",
|
|
796
799
|
),
|
|
797
800
|
);
|
|
798
|
-
|
|
801
|
+
const { "@sigil-dev/runtime": runtime, ...aliases } =
|
|
802
|
+
config.compilerOptions.paths;
|
|
803
|
+
expect(aliases).toEqual({
|
|
799
804
|
$lib: ["../src/lib"],
|
|
800
805
|
"$lib/*": ["../src/lib/*"],
|
|
801
806
|
"@ui": ["../ui"],
|
|
802
807
|
"@ui/*": ["../ui/*"],
|
|
803
808
|
});
|
|
809
|
+
// The app reads grimoire's runtime, not one of its own.
|
|
810
|
+
expect(runtime).toHaveLength(1);
|
|
811
|
+
expect(
|
|
812
|
+
resolve(join(root, ".grimoire"), runtime[0]).replace(/\\/g, "/"),
|
|
813
|
+
).toBe(
|
|
814
|
+
Bun.resolveSync(
|
|
815
|
+
"@sigil-dev/runtime",
|
|
816
|
+
join(import.meta.dir, "../src/typegen"),
|
|
817
|
+
).replace(/\\/g, "/"),
|
|
818
|
+
);
|
|
819
|
+
} finally {
|
|
820
|
+
await rm(root, { recursive: true, force: true });
|
|
821
|
+
}
|
|
822
|
+
});
|
|
823
|
+
});
|
|
824
|
+
|
|
825
|
+
describe("universal load in +page.tsx / +layout.tsx", () => {
|
|
826
|
+
test("PageData and LayoutData merge it under the server load, and tsc agrees", async () => {
|
|
827
|
+
const root = join(tmpdir(), `grimoire-universal-${Date.now()}`);
|
|
828
|
+
const routeDir = join(root, "src", "routes", "post");
|
|
829
|
+
await mkdir(routeDir, { recursive: true });
|
|
830
|
+
try {
|
|
831
|
+
await writeFile(
|
|
832
|
+
join(routeDir, "+page.tsx"),
|
|
833
|
+
`export async function load() { return { a: "universal", shared: 1, loadUsers: true }; }
|
|
834
|
+
export default () => null;
|
|
835
|
+
`,
|
|
836
|
+
);
|
|
837
|
+
await writeFile(
|
|
838
|
+
join(routeDir, "+page.server.ts"),
|
|
839
|
+
`export async function load() { return { b: 2, shared: "server" }; }`,
|
|
840
|
+
);
|
|
841
|
+
await writeFile(
|
|
842
|
+
join(routeDir, "+layout.tsx"),
|
|
843
|
+
`export async function load() { return { theme: "dark" }; }
|
|
844
|
+
export default (p: { children?: unknown }) => p.children;
|
|
845
|
+
`,
|
|
846
|
+
);
|
|
847
|
+
await writeFile(
|
|
848
|
+
join(routeDir, "check.ts"),
|
|
849
|
+
`import type { LayoutProps, PageProps } from "./$types";
|
|
850
|
+
declare const page: PageProps;
|
|
851
|
+
declare const layout: LayoutProps;
|
|
852
|
+
const _a: string = page.data.a;
|
|
853
|
+
const _b: number = page.data.b;
|
|
854
|
+
// the server load wins on a clashing key
|
|
855
|
+
const _shared: string = page.data.shared;
|
|
856
|
+
const _theme: string = layout.data.theme;
|
|
857
|
+
const _path: string = layout.path;
|
|
858
|
+
// @ts-expect-error not exported by either load
|
|
859
|
+
page.data.nope;
|
|
860
|
+
export {};`,
|
|
861
|
+
);
|
|
862
|
+
await writeFile(
|
|
863
|
+
join(root, "tsconfig.json"),
|
|
864
|
+
JSON.stringify({
|
|
865
|
+
extends: "./.grimoire/tsconfig.generated.json",
|
|
866
|
+
compilerOptions: {
|
|
867
|
+
target: "ESNext",
|
|
868
|
+
module: "ESNext",
|
|
869
|
+
moduleResolution: "bundler",
|
|
870
|
+
jsx: "preserve",
|
|
871
|
+
strict: true,
|
|
872
|
+
skipLibCheck: true,
|
|
873
|
+
noEmit: true,
|
|
874
|
+
},
|
|
875
|
+
include: ["src", ".grimoire/types"],
|
|
876
|
+
}),
|
|
877
|
+
);
|
|
878
|
+
const routesDir = join(root, "src", "routes");
|
|
879
|
+
const tree = await scanRoutes(routesDir, root);
|
|
880
|
+
await generateTypes(tree, {
|
|
881
|
+
projectRoot: root,
|
|
882
|
+
routesDir,
|
|
883
|
+
outDir: join(root, ".grimoire", "types"),
|
|
884
|
+
});
|
|
885
|
+
const proc = Bun.spawnSync(
|
|
886
|
+
[
|
|
887
|
+
process.execPath,
|
|
888
|
+
"x",
|
|
889
|
+
"--bun",
|
|
890
|
+
"@typescript/native-preview",
|
|
891
|
+
"--noEmit",
|
|
892
|
+
"--project",
|
|
893
|
+
"tsconfig.json",
|
|
894
|
+
],
|
|
895
|
+
{ cwd: root },
|
|
896
|
+
);
|
|
897
|
+
expect(proc.stdout.toString() + proc.stderr.toString()).not.toContain(
|
|
898
|
+
"error TS",
|
|
899
|
+
);
|
|
900
|
+
expect(proc.exitCode).toBe(0);
|
|
901
|
+
} finally {
|
|
902
|
+
await rm(root, { recursive: true, force: true });
|
|
903
|
+
}
|
|
904
|
+
});
|
|
905
|
+
|
|
906
|
+
test("a load-less page keeps the plain Record<string, never>", async () => {
|
|
907
|
+
const root = join(tmpdir(), `grimoire-noload-${Date.now()}`);
|
|
908
|
+
const routeDir = join(root, "src", "routes", "x");
|
|
909
|
+
await mkdir(routeDir, { recursive: true });
|
|
910
|
+
try {
|
|
911
|
+
// `loadUsers` is not `load`
|
|
912
|
+
await writeFile(
|
|
913
|
+
join(routeDir, "+page.tsx"),
|
|
914
|
+
`export function loadUsers() { return {}; }\nexport default () => null;\n`,
|
|
915
|
+
);
|
|
916
|
+
const routesDir = join(root, "src", "routes");
|
|
917
|
+
const tree = await scanRoutes(routesDir, root);
|
|
918
|
+
await generateTypes(tree, {
|
|
919
|
+
projectRoot: root,
|
|
920
|
+
routesDir,
|
|
921
|
+
outDir: join(root, ".grimoire", "types"),
|
|
922
|
+
});
|
|
923
|
+
const out = await readFile(
|
|
924
|
+
join(root, ".grimoire", "types", "src", "routes", "x", "$types.d.ts"),
|
|
925
|
+
"utf-8",
|
|
926
|
+
);
|
|
927
|
+
expect(out).toContain("export type PageData = Record<string, never>;");
|
|
928
|
+
} finally {
|
|
929
|
+
await rm(root, { recursive: true, force: true });
|
|
930
|
+
}
|
|
931
|
+
});
|
|
932
|
+
});
|
|
933
|
+
|
|
934
|
+
describe("generateTypes — locals from the plugins in sigil.config.ts", () => {
|
|
935
|
+
async function project(withConfig: boolean) {
|
|
936
|
+
const root = join(tmpdir(), `grimoire-locals-${crypto.randomUUID()}`);
|
|
937
|
+
const routeDir = join(root, "src", "routes");
|
|
938
|
+
await mkdir(routeDir, { recursive: true });
|
|
939
|
+
await writeFile(
|
|
940
|
+
join(routeDir, "+page.tsx"),
|
|
941
|
+
"export default () => null;\n",
|
|
942
|
+
);
|
|
943
|
+
if (withConfig) {
|
|
944
|
+
await writeFile(join(root, "sigil.config.ts"), "export default {};\n");
|
|
945
|
+
}
|
|
946
|
+
const run = async () =>
|
|
947
|
+
generateTypes(await scanRoutes(routeDir, root), {
|
|
948
|
+
projectRoot: root,
|
|
949
|
+
routesDir: routeDir,
|
|
950
|
+
outDir: join(root, ".grimoire", "types"),
|
|
951
|
+
});
|
|
952
|
+
return {
|
|
953
|
+
root,
|
|
954
|
+
run,
|
|
955
|
+
locals: join(root, ".grimoire", "types", "locals.d.ts"),
|
|
956
|
+
};
|
|
957
|
+
}
|
|
958
|
+
|
|
959
|
+
test("writes App.Locals from the config's plugins", async () => {
|
|
960
|
+
const p = await project(true);
|
|
961
|
+
try {
|
|
962
|
+
await p.run();
|
|
963
|
+
const out = await readFile(p.locals, "utf-8");
|
|
964
|
+
expect(out).toContain('import type config from "../../sigil.config.js";');
|
|
965
|
+
expect(out).toContain(
|
|
966
|
+
"interface Locals extends PluginLocals<typeof config> {}",
|
|
967
|
+
);
|
|
968
|
+
} finally {
|
|
969
|
+
await rm(p.root, { recursive: true, force: true });
|
|
970
|
+
}
|
|
971
|
+
});
|
|
972
|
+
|
|
973
|
+
test("no config, no locals.d.ts, and a stale one is removed", async () => {
|
|
974
|
+
const p = await project(true);
|
|
975
|
+
try {
|
|
976
|
+
await p.run();
|
|
977
|
+
expect(existsSync(p.locals)).toBe(true);
|
|
978
|
+
await rm(join(p.root, "sigil.config.ts"));
|
|
979
|
+
await p.run();
|
|
980
|
+
expect(existsSync(p.locals)).toBe(false);
|
|
981
|
+
} finally {
|
|
982
|
+
await rm(p.root, { recursive: true, force: true });
|
|
983
|
+
}
|
|
984
|
+
});
|
|
985
|
+
});
|
|
986
|
+
|
|
987
|
+
describe("tables.d.ts", () => {
|
|
988
|
+
test("hands tome the plugins' tables from the config", async () => {
|
|
989
|
+
const { generateTables } = await import("../src/typegen/index");
|
|
990
|
+
const out = generateTables("/app/.grimoire/types", "/app/sigil.config.ts");
|
|
991
|
+
expect(out).toContain('import type config from "../../sigil.config');
|
|
992
|
+
expect(out).toContain('declare module "@sigil-dev/tome"');
|
|
993
|
+
expect(out).toContain("pluginTables: PluginTables<typeof config>;");
|
|
994
|
+
});
|
|
995
|
+
});
|
|
996
|
+
|
|
997
|
+
describe("tsconfig paths", () => {
|
|
998
|
+
test("an alias inside .grimoire is ./-relative, as TS needs without baseUrl", async () => {
|
|
999
|
+
const root = await import("node:fs").then((fs) => fs.mkdtempSync(join(tmpdir(), "paths-")));
|
|
1000
|
+
try {
|
|
1001
|
+
await generateTypes({ routes: [], layouts: [], servers: [], errors: [], paramMatcherFiles: [] } as never, {
|
|
1002
|
+
projectRoot: root,
|
|
1003
|
+
outDir: join(root, ".grimoire/types"),
|
|
1004
|
+
routesDir: join(root, "src/routes"),
|
|
1005
|
+
aliases: { $db: join(root, ".grimoire/db"), $lib: join(root, "src/lib") },
|
|
1006
|
+
} as never);
|
|
1007
|
+
const tsconfig = JSON.parse(await readFile(join(root, ".grimoire/tsconfig.generated.json"), "utf8"));
|
|
1008
|
+
expect(tsconfig.compilerOptions.paths.$db).toEqual(["./db"]);
|
|
1009
|
+
expect(tsconfig.compilerOptions.paths.$lib).toEqual(["../src/lib"]);
|
|
804
1010
|
} finally {
|
|
805
1011
|
await rm(root, { recursive: true, force: true });
|
|
806
1012
|
}
|
|
807
1013
|
});
|
|
808
1014
|
});
|
|
809
|
-
|
|
810
|
-
describe("universal load in +page.tsx / +layout.tsx", () => {
|
|
811
|
-
test("PageData and LayoutData merge it under the server load, and tsc agrees", async () => {
|
|
812
|
-
const root = join(tmpdir(), `grimoire-universal-${Date.now()}`);
|
|
813
|
-
const routeDir = join(root, "src", "routes", "post");
|
|
814
|
-
await mkdir(routeDir, { recursive: true });
|
|
815
|
-
try {
|
|
816
|
-
await writeFile(
|
|
817
|
-
join(routeDir, "+page.tsx"),
|
|
818
|
-
`export async function load() { return { a: "universal", shared: 1, loadUsers: true }; }
|
|
819
|
-
export default () => null;
|
|
820
|
-
`,
|
|
821
|
-
);
|
|
822
|
-
await writeFile(
|
|
823
|
-
join(routeDir, "+page.server.ts"),
|
|
824
|
-
`export async function load() { return { b: 2, shared: "server" }; }`,
|
|
825
|
-
);
|
|
826
|
-
await writeFile(
|
|
827
|
-
join(routeDir, "+layout.tsx"),
|
|
828
|
-
`export async function load() { return { theme: "dark" }; }
|
|
829
|
-
export default (p: { children?: unknown }) => p.children;
|
|
830
|
-
`,
|
|
831
|
-
);
|
|
832
|
-
await writeFile(
|
|
833
|
-
join(routeDir, "check.ts"),
|
|
834
|
-
`import type { LayoutProps, PageProps } from "./$types";
|
|
835
|
-
declare const page: PageProps;
|
|
836
|
-
declare const layout: LayoutProps;
|
|
837
|
-
const _a: string = page.data.a;
|
|
838
|
-
const _b: number = page.data.b;
|
|
839
|
-
// the server load wins on a clashing key
|
|
840
|
-
const _shared: string = page.data.shared;
|
|
841
|
-
const _theme: string = layout.data.theme;
|
|
842
|
-
const _path: string = layout.path;
|
|
843
|
-
// @ts-expect-error not exported by either load
|
|
844
|
-
page.data.nope;
|
|
845
|
-
export {};`,
|
|
846
|
-
);
|
|
847
|
-
await writeFile(
|
|
848
|
-
join(root, "tsconfig.json"),
|
|
849
|
-
JSON.stringify({
|
|
850
|
-
extends: "./.grimoire/tsconfig.generated.json",
|
|
851
|
-
compilerOptions: {
|
|
852
|
-
target: "ESNext",
|
|
853
|
-
module: "ESNext",
|
|
854
|
-
moduleResolution: "bundler",
|
|
855
|
-
jsx: "preserve",
|
|
856
|
-
strict: true,
|
|
857
|
-
skipLibCheck: true,
|
|
858
|
-
noEmit: true,
|
|
859
|
-
},
|
|
860
|
-
include: ["src", ".grimoire/types"],
|
|
861
|
-
}),
|
|
862
|
-
);
|
|
863
|
-
const routesDir = join(root, "src", "routes");
|
|
864
|
-
const tree = await scanRoutes(routesDir, root);
|
|
865
|
-
await generateTypes(tree, {
|
|
866
|
-
projectRoot: root,
|
|
867
|
-
routesDir,
|
|
868
|
-
outDir: join(root, ".grimoire", "types"),
|
|
869
|
-
});
|
|
870
|
-
const proc = Bun.spawnSync(
|
|
871
|
-
[
|
|
872
|
-
process.execPath,
|
|
873
|
-
"x",
|
|
874
|
-
"--bun",
|
|
875
|
-
"@typescript/native-preview",
|
|
876
|
-
"--noEmit",
|
|
877
|
-
"--project",
|
|
878
|
-
"tsconfig.json",
|
|
879
|
-
],
|
|
880
|
-
{ cwd: root },
|
|
881
|
-
);
|
|
882
|
-
expect(proc.stdout.toString() + proc.stderr.toString()).not.toContain(
|
|
883
|
-
"error TS",
|
|
884
|
-
);
|
|
885
|
-
expect(proc.exitCode).toBe(0);
|
|
886
|
-
} finally {
|
|
887
|
-
await rm(root, { recursive: true, force: true });
|
|
888
|
-
}
|
|
889
|
-
});
|
|
890
|
-
|
|
891
|
-
test("a load-less page keeps the plain Record<string, never>", async () => {
|
|
892
|
-
const root = join(tmpdir(), `grimoire-noload-${Date.now()}`);
|
|
893
|
-
const routeDir = join(root, "src", "routes", "x");
|
|
894
|
-
await mkdir(routeDir, { recursive: true });
|
|
895
|
-
try {
|
|
896
|
-
// `loadUsers` is not `load`
|
|
897
|
-
await writeFile(
|
|
898
|
-
join(routeDir, "+page.tsx"),
|
|
899
|
-
`export function loadUsers() { return {}; }\nexport default () => null;\n`,
|
|
900
|
-
);
|
|
901
|
-
const routesDir = join(root, "src", "routes");
|
|
902
|
-
const tree = await scanRoutes(routesDir, root);
|
|
903
|
-
await generateTypes(tree, {
|
|
904
|
-
projectRoot: root,
|
|
905
|
-
routesDir,
|
|
906
|
-
outDir: join(root, ".grimoire", "types"),
|
|
907
|
-
});
|
|
908
|
-
const out = await readFile(
|
|
909
|
-
join(root, ".grimoire", "types", "src", "routes", "x", "$types.d.ts"),
|
|
910
|
-
"utf-8",
|
|
911
|
-
);
|
|
912
|
-
expect(out).toContain("export type PageData = Record<string, never>;");
|
|
913
|
-
} finally {
|
|
914
|
-
await rm(root, { recursive: true, force: true });
|
|
915
|
-
}
|
|
916
|
-
});
|
|
917
|
-
});
|
package/test/warm.test.ts
CHANGED
|
@@ -1,96 +1,96 @@
|
|
|
1
|
-
import { describe, expect, test } from "bun:test";
|
|
2
|
-
import { preloadModules, startWarming } from "../src/server/warm.ts";
|
|
3
|
-
|
|
4
|
-
describe("preloadModules", () => {
|
|
5
|
-
test("imports each file once and reports failures without throwing", async () => {
|
|
6
|
-
const seen: string[] = [];
|
|
7
|
-
const warnings: string[] = [];
|
|
8
|
-
const loaded = await preloadModules(
|
|
9
|
-
["/a.tsx", "/b.tsx", "/a.tsx", "/bad.tsx"],
|
|
10
|
-
async (f) => {
|
|
11
|
-
seen.push(f);
|
|
12
|
-
if (f === "/bad.tsx") throw new Error("boom");
|
|
13
|
-
},
|
|
14
|
-
(msg) => warnings.push(msg),
|
|
15
|
-
);
|
|
16
|
-
expect(loaded).toBe(2);
|
|
17
|
-
expect(seen.sort()).toEqual(["/a.tsx", "/b.tsx", "/bad.tsx"]);
|
|
18
|
-
expect(warnings).toEqual(["could not preload /bad.tsx:"]);
|
|
19
|
-
});
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
describe("startWarming", () => {
|
|
23
|
-
test("requests paths in order, with the given headers, and drains bodies", async () => {
|
|
24
|
-
const calls: Array<{ path: string; header: string | null }> = [];
|
|
25
|
-
let drained = 0;
|
|
26
|
-
const warmer = startWarming({
|
|
27
|
-
paths: ["/one", "/two"],
|
|
28
|
-
origin: "http://127.0.0.1:1234",
|
|
29
|
-
headers: { "x-grimoire-internal": "s3cret" },
|
|
30
|
-
handle: async (req) => {
|
|
31
|
-
calls.push({
|
|
32
|
-
path: new URL(req.url).pathname,
|
|
33
|
-
header: req.headers.get("x-grimoire-internal"),
|
|
34
|
-
});
|
|
35
|
-
const stream = new ReadableStream({
|
|
36
|
-
pull(c) {
|
|
37
|
-
drained++;
|
|
38
|
-
c.enqueue(new TextEncoder().encode("x"));
|
|
39
|
-
c.close();
|
|
40
|
-
},
|
|
41
|
-
});
|
|
42
|
-
return new Response(stream);
|
|
43
|
-
},
|
|
44
|
-
log: () => {},
|
|
45
|
-
});
|
|
46
|
-
await warmer.firstPass;
|
|
47
|
-
warmer.stop();
|
|
48
|
-
expect(calls).toEqual([
|
|
49
|
-
{ path: "/one", header: "s3cret" },
|
|
50
|
-
{ path: "/two", header: "s3cret" },
|
|
51
|
-
]);
|
|
52
|
-
expect(drained).toBe(2);
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
test("a failing path is logged and does not stop the rest", async () => {
|
|
56
|
-
const logs: string[] = [];
|
|
57
|
-
const paths: string[] = [];
|
|
58
|
-
const warmer = startWarming({
|
|
59
|
-
paths: ["/bad", "/good"],
|
|
60
|
-
origin: "http://x",
|
|
61
|
-
handle: async (req) => {
|
|
62
|
-
const p = new URL(req.url).pathname;
|
|
63
|
-
paths.push(p);
|
|
64
|
-
if (p === "/bad") throw new Error("nope");
|
|
65
|
-
return new Response("ok");
|
|
66
|
-
},
|
|
67
|
-
log: (m) => logs.push(m),
|
|
68
|
-
});
|
|
69
|
-
await warmer.firstPass;
|
|
70
|
-
warmer.stop();
|
|
71
|
-
expect(paths).toEqual(["/bad", "/good"]);
|
|
72
|
-
expect(logs[0]).toContain("/bad failed: nope");
|
|
73
|
-
expect(logs[1]).toContain("warmed /good (200)");
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
test("every repeats the pass until stopped", async () => {
|
|
77
|
-
let n = 0;
|
|
78
|
-
const warmer = startWarming({
|
|
79
|
-
paths: ["/p"],
|
|
80
|
-
every: 20,
|
|
81
|
-
origin: "http://x",
|
|
82
|
-
handle: async () => {
|
|
83
|
-
n++;
|
|
84
|
-
return new Response("ok");
|
|
85
|
-
},
|
|
86
|
-
log: () => {},
|
|
87
|
-
});
|
|
88
|
-
await warmer.firstPass;
|
|
89
|
-
await Bun.sleep(90);
|
|
90
|
-
warmer.stop();
|
|
91
|
-
const seen = n;
|
|
92
|
-
expect(seen).toBeGreaterThanOrEqual(3);
|
|
93
|
-
await Bun.sleep(60);
|
|
94
|
-
expect(n).toBe(seen);
|
|
95
|
-
});
|
|
96
|
-
});
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import { preloadModules, startWarming } from "../src/server/warm.ts";
|
|
3
|
+
|
|
4
|
+
describe("preloadModules", () => {
|
|
5
|
+
test("imports each file once and reports failures without throwing", async () => {
|
|
6
|
+
const seen: string[] = [];
|
|
7
|
+
const warnings: string[] = [];
|
|
8
|
+
const loaded = await preloadModules(
|
|
9
|
+
["/a.tsx", "/b.tsx", "/a.tsx", "/bad.tsx"],
|
|
10
|
+
async (f) => {
|
|
11
|
+
seen.push(f);
|
|
12
|
+
if (f === "/bad.tsx") throw new Error("boom");
|
|
13
|
+
},
|
|
14
|
+
(msg) => warnings.push(msg),
|
|
15
|
+
);
|
|
16
|
+
expect(loaded).toBe(2);
|
|
17
|
+
expect(seen.sort()).toEqual(["/a.tsx", "/b.tsx", "/bad.tsx"]);
|
|
18
|
+
expect(warnings).toEqual(["could not preload /bad.tsx:"]);
|
|
19
|
+
});
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
describe("startWarming", () => {
|
|
23
|
+
test("requests paths in order, with the given headers, and drains bodies", async () => {
|
|
24
|
+
const calls: Array<{ path: string; header: string | null }> = [];
|
|
25
|
+
let drained = 0;
|
|
26
|
+
const warmer = startWarming({
|
|
27
|
+
paths: ["/one", "/two"],
|
|
28
|
+
origin: "http://127.0.0.1:1234",
|
|
29
|
+
headers: { "x-grimoire-internal": "s3cret" },
|
|
30
|
+
handle: async (req) => {
|
|
31
|
+
calls.push({
|
|
32
|
+
path: new URL(req.url).pathname,
|
|
33
|
+
header: req.headers.get("x-grimoire-internal"),
|
|
34
|
+
});
|
|
35
|
+
const stream = new ReadableStream({
|
|
36
|
+
pull(c) {
|
|
37
|
+
drained++;
|
|
38
|
+
c.enqueue(new TextEncoder().encode("x"));
|
|
39
|
+
c.close();
|
|
40
|
+
},
|
|
41
|
+
});
|
|
42
|
+
return new Response(stream);
|
|
43
|
+
},
|
|
44
|
+
log: () => {},
|
|
45
|
+
});
|
|
46
|
+
await warmer.firstPass;
|
|
47
|
+
warmer.stop();
|
|
48
|
+
expect(calls).toEqual([
|
|
49
|
+
{ path: "/one", header: "s3cret" },
|
|
50
|
+
{ path: "/two", header: "s3cret" },
|
|
51
|
+
]);
|
|
52
|
+
expect(drained).toBe(2);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
test("a failing path is logged and does not stop the rest", async () => {
|
|
56
|
+
const logs: string[] = [];
|
|
57
|
+
const paths: string[] = [];
|
|
58
|
+
const warmer = startWarming({
|
|
59
|
+
paths: ["/bad", "/good"],
|
|
60
|
+
origin: "http://x",
|
|
61
|
+
handle: async (req) => {
|
|
62
|
+
const p = new URL(req.url).pathname;
|
|
63
|
+
paths.push(p);
|
|
64
|
+
if (p === "/bad") throw new Error("nope");
|
|
65
|
+
return new Response("ok");
|
|
66
|
+
},
|
|
67
|
+
log: (m) => logs.push(m),
|
|
68
|
+
});
|
|
69
|
+
await warmer.firstPass;
|
|
70
|
+
warmer.stop();
|
|
71
|
+
expect(paths).toEqual(["/bad", "/good"]);
|
|
72
|
+
expect(logs[0]).toContain("/bad failed: nope");
|
|
73
|
+
expect(logs[1]).toContain("warmed /good (200)");
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
test("every repeats the pass until stopped", async () => {
|
|
77
|
+
let n = 0;
|
|
78
|
+
const warmer = startWarming({
|
|
79
|
+
paths: ["/p"],
|
|
80
|
+
every: 20,
|
|
81
|
+
origin: "http://x",
|
|
82
|
+
handle: async () => {
|
|
83
|
+
n++;
|
|
84
|
+
return new Response("ok");
|
|
85
|
+
},
|
|
86
|
+
log: () => {},
|
|
87
|
+
});
|
|
88
|
+
await warmer.firstPass;
|
|
89
|
+
await Bun.sleep(90);
|
|
90
|
+
warmer.stop();
|
|
91
|
+
const seen = n;
|
|
92
|
+
expect(seen).toBeGreaterThanOrEqual(3);
|
|
93
|
+
await Bun.sleep(60);
|
|
94
|
+
expect(n).toBe(seen);
|
|
95
|
+
});
|
|
96
|
+
});
|
|
@@ -1,59 +1,59 @@
|
|
|
1
|
-
import { describe, expect, test } from "bun:test";
|
|
2
|
-
import { workerContext } from "../src/types.ts";
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* `workers` exists so a per-worker resource can divide a global budget.
|
|
6
|
-
* A wrong value here is not a crash, it is a connection pool quietly
|
|
7
|
-
* sized N-times too large — which shows up as a database refusing
|
|
8
|
-
* connections in production and nowhere else.
|
|
9
|
-
*/
|
|
10
|
-
describe("workerContext", () => {
|
|
11
|
-
test("reads index and count from a coordinator spawn", () => {
|
|
12
|
-
expect(
|
|
13
|
-
workerContext({
|
|
14
|
-
GRIMOIRE_WORKER_INDEX: "3",
|
|
15
|
-
GRIMOIRE_WORKER_COUNT: "4",
|
|
16
|
-
GRIMOIRE_MODE: "production",
|
|
17
|
-
}),
|
|
18
|
-
).toEqual({ workerId: 2, workers: 4, dev: false });
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
test("is zero-based", () => {
|
|
22
|
-
// The coordinator writes 1-based indices, so index "1" is the first
|
|
23
|
-
// worker and must read back as 0, not 1.
|
|
24
|
-
expect(workerContext({ GRIMOIRE_WORKER_INDEX: "1" }).workerId).toBe(0);
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
test("dev mode is read from GRIMOIRE_MODE", () => {
|
|
28
|
-
expect(workerContext({ GRIMOIRE_MODE: "development" }).dev).toBe(true);
|
|
29
|
-
expect(workerContext({ GRIMOIRE_MODE: "production" }).dev).toBe(false);
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
test("no coordinator means one worker of one", () => {
|
|
33
|
-
// `sigil dev` and a plain single-process start set no vars. That is a
|
|
34
|
-
// real situation, not a failure, and it has to resolve to something
|
|
35
|
-
// usable.
|
|
36
|
-
expect(workerContext({})).toEqual({
|
|
37
|
-
workerId: 0,
|
|
38
|
-
workers: 1,
|
|
39
|
-
dev: false,
|
|
40
|
-
});
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
test("workers is never zero", () => {
|
|
44
|
-
// The whole point of the number is to divide by it. Zero would make
|
|
45
|
-
// `maxTotal / workers` Infinity.
|
|
46
|
-
expect(workerContext({ GRIMOIRE_WORKER_COUNT: "0" }).workers).toBe(1);
|
|
47
|
-
expect(workerContext({ GRIMOIRE_WORKER_COUNT: "garbage" }).workers).toBe(1);
|
|
48
|
-
expect(workerContext({ GRIMOIRE_WORKER_COUNT: "-4" }).workers).toBe(1);
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
test("a budget divides evenly", () => {
|
|
52
|
-
// The arithmetic the field exists for.
|
|
53
|
-
const ctx = workerContext({
|
|
54
|
-
GRIMOIRE_WORKER_INDEX: "1",
|
|
55
|
-
GRIMOIRE_WORKER_COUNT: "4",
|
|
56
|
-
});
|
|
57
|
-
expect(Math.ceil(20 / ctx.workers)).toBe(5);
|
|
58
|
-
});
|
|
59
|
-
});
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import { workerContext } from "../src/types.ts";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* `workers` exists so a per-worker resource can divide a global budget.
|
|
6
|
+
* A wrong value here is not a crash, it is a connection pool quietly
|
|
7
|
+
* sized N-times too large — which shows up as a database refusing
|
|
8
|
+
* connections in production and nowhere else.
|
|
9
|
+
*/
|
|
10
|
+
describe("workerContext", () => {
|
|
11
|
+
test("reads index and count from a coordinator spawn", () => {
|
|
12
|
+
expect(
|
|
13
|
+
workerContext({
|
|
14
|
+
GRIMOIRE_WORKER_INDEX: "3",
|
|
15
|
+
GRIMOIRE_WORKER_COUNT: "4",
|
|
16
|
+
GRIMOIRE_MODE: "production",
|
|
17
|
+
}),
|
|
18
|
+
).toEqual({ workerId: 2, workers: 4, dev: false });
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
test("is zero-based", () => {
|
|
22
|
+
// The coordinator writes 1-based indices, so index "1" is the first
|
|
23
|
+
// worker and must read back as 0, not 1.
|
|
24
|
+
expect(workerContext({ GRIMOIRE_WORKER_INDEX: "1" }).workerId).toBe(0);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
test("dev mode is read from GRIMOIRE_MODE", () => {
|
|
28
|
+
expect(workerContext({ GRIMOIRE_MODE: "development" }).dev).toBe(true);
|
|
29
|
+
expect(workerContext({ GRIMOIRE_MODE: "production" }).dev).toBe(false);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
test("no coordinator means one worker of one", () => {
|
|
33
|
+
// `sigil dev` and a plain single-process start set no vars. That is a
|
|
34
|
+
// real situation, not a failure, and it has to resolve to something
|
|
35
|
+
// usable.
|
|
36
|
+
expect(workerContext({})).toEqual({
|
|
37
|
+
workerId: 0,
|
|
38
|
+
workers: 1,
|
|
39
|
+
dev: false,
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
test("workers is never zero", () => {
|
|
44
|
+
// The whole point of the number is to divide by it. Zero would make
|
|
45
|
+
// `maxTotal / workers` Infinity.
|
|
46
|
+
expect(workerContext({ GRIMOIRE_WORKER_COUNT: "0" }).workers).toBe(1);
|
|
47
|
+
expect(workerContext({ GRIMOIRE_WORKER_COUNT: "garbage" }).workers).toBe(1);
|
|
48
|
+
expect(workerContext({ GRIMOIRE_WORKER_COUNT: "-4" }).workers).toBe(1);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
test("a budget divides evenly", () => {
|
|
52
|
+
// The arithmetic the field exists for.
|
|
53
|
+
const ctx = workerContext({
|
|
54
|
+
GRIMOIRE_WORKER_INDEX: "1",
|
|
55
|
+
GRIMOIRE_WORKER_COUNT: "4",
|
|
56
|
+
});
|
|
57
|
+
expect(Math.ceil(20 / ctx.workers)).toBe(5);
|
|
58
|
+
});
|
|
59
|
+
});
|