@sigil-dev/grimoire 0.9.11 → 0.9.13
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 +5 -1
- package/package.json +3 -3
- package/server.ts +1 -0
- package/src/client/deferred.ts +179 -0
- package/src/rendering/ssrPlugin.ts +16 -1
- package/src/server/after-response.ts +194 -0
- package/src/server/asset-cache.ts +51 -0
- package/src/server/coordinator.ts +26 -6
- package/src/server/index.ts +593 -468
- package/src/server/plugins.ts +35 -0
- package/src/server/worker.ts +13 -1
- package/src/types.ts +118 -1
- package/test/after-response.test.ts +113 -0
- package/test/asset-cache-policy.test.ts +83 -0
- package/test/coordinator.test.ts +9 -0
- package/test/deferred.test.ts +256 -0
- package/test/parser-plugins.test.ts +92 -0
- package/test/plugin-services-context.test.ts +97 -0
- package/test/services.test.ts +90 -0
- package/test/streaming.test.ts +56 -0
- package/test/worker-context.test.ts +59 -0
- package/test/workspace-files.test.ts +146 -0
- package/test/workspace-versions.test.ts +131 -0
- package/.grimoire/_routes.ts +0 -4
- package/public/__grimoire__/client.js +0 -55
- package/public/__grimoire__/hydrate.js +0 -63
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import { transformSync } from "@babel/core";
|
|
3
|
+
import type { ParserPlugin } from "@babel/core";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The parser configuration the SSR plugin uses, pinned here so a change to
|
|
7
|
+
* it has to be made in two places deliberately.
|
|
8
|
+
*
|
|
9
|
+
* Kept in sync by hand rather than imported: the plugin builds its own
|
|
10
|
+
* parserOpts inline, and a test that imported that would be testing the
|
|
11
|
+
* import rather than the behaviour. The failing case is named below, and it
|
|
12
|
+
* was found by a real adapter rather than by reading the config.
|
|
13
|
+
*/
|
|
14
|
+
const parserOptsFor = (path: string): { plugins: ParserPlugin[] } =>
|
|
15
|
+
path.endsWith(".tsx")
|
|
16
|
+
? { plugins: ["typescript", "jsx"] }
|
|
17
|
+
: { plugins: ["typescript"] };
|
|
18
|
+
|
|
19
|
+
const parse = (source: string, filename: string) =>
|
|
20
|
+
transformSync(source, {
|
|
21
|
+
configFile: false,
|
|
22
|
+
babelrc: false,
|
|
23
|
+
parserOpts: parserOptsFor(filename),
|
|
24
|
+
filename,
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
describe("the jsx parser plugin is scoped to .tsx", () => {
|
|
28
|
+
test("a generic arrow function compiles in a .ts file", () => {
|
|
29
|
+
// This is the regression. With ["typescript", "jsx"] on every file,
|
|
30
|
+
// Babel reads `<R>(x: R) => x` as the start of a JSX element and
|
|
31
|
+
// fails with "Unexpected token `>`". Generic arrows are ordinary
|
|
32
|
+
// TypeScript, and this is the exact line that lets a Drizzle adapter
|
|
33
|
+
// drop an `as any`:
|
|
34
|
+
//
|
|
35
|
+
// transaction: <R>(db: DrizzleDb, fn: (tx: DrizzleTx) => Promise<R>) =>
|
|
36
|
+
// db.transaction(fn)
|
|
37
|
+
expect(() =>
|
|
38
|
+
parse(
|
|
39
|
+
"const o = { f: <R>(x: R): R => x };\nexport const out = o;",
|
|
40
|
+
"adapter.ts",
|
|
41
|
+
),
|
|
42
|
+
).not.toThrow();
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
test("a generic arrow in an object literal, the failing shape", () => {
|
|
46
|
+
// The minimal repro. Inside a call argument the parser is in a
|
|
47
|
+
// different state, which is why the one above passes and this failed.
|
|
48
|
+
expect(() =>
|
|
49
|
+
parse("define({ f: <R>(x: R): R => x });", "adapter.ts"),
|
|
50
|
+
).not.toThrow();
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
test("JSX still compiles in a .tsx file", () => {
|
|
54
|
+
// The whole reason the jsx plugin exists must keep working.
|
|
55
|
+
expect(() =>
|
|
56
|
+
parse("const a = <div x={1} />;\nexport default a;", "page.tsx"),
|
|
57
|
+
).not.toThrow();
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
test("a JSX fragment compiles in a .tsx file", () => {
|
|
61
|
+
expect(() =>
|
|
62
|
+
parse("const a = <><b/></>;\nexport default a;", "page.tsx"),
|
|
63
|
+
).not.toThrow();
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
test("a generic arrow in a .tsx file needs the comma, as TypeScript says", () => {
|
|
67
|
+
// Genuinely ambiguous, so the error is correct behaviour rather than a
|
|
68
|
+
// bug — and worth pinning so the fix is not "regressed" later by
|
|
69
|
+
// someone trying to make this pass without the comma.
|
|
70
|
+
expect(() =>
|
|
71
|
+
parse("const o = { f: <R,>(x: R): R => x };\nexport default o;", "a.tsx"),
|
|
72
|
+
).not.toThrow();
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
describe("other TypeScript syntax in a .ts file", () => {
|
|
77
|
+
// Dropping jsx from .ts must not cost anything else, which is the risk a
|
|
78
|
+
// one-line parser change carries.
|
|
79
|
+
for (const [label, source] of [
|
|
80
|
+
["as const", "const x = { a: 1 } as const;\nexport default x;"],
|
|
81
|
+
["satisfies", "const x = { a: 1 } satisfies Record<string, number>;\nexport default x;"],
|
|
82
|
+
["a generic interface", "interface I<T> { v: T }\nexport default I;"],
|
|
83
|
+
["an enum", "enum E { A }\nexport default E;"],
|
|
84
|
+
["optional chaining", "const x = a?.b?.c;\nexport default x;"],
|
|
85
|
+
["a type-only import", 'import type { A } from "./a.ts";\nexport default A;'],
|
|
86
|
+
["decorators-free classes", "class C { x = 1 }\nexport default C;"],
|
|
87
|
+
] as Array<[string, string]>) {
|
|
88
|
+
test(label, () => {
|
|
89
|
+
expect(() => parse(source, "file.ts")).not.toThrow();
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
});
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { describe, expect, mock, test } from "bun:test";
|
|
2
|
+
import { collectServices } from "../src/server/plugins";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Can one plugin use a database another plugin opened?
|
|
6
|
+
*
|
|
7
|
+
* The SQL plugin owns the connection and publishes it as a service. A second
|
|
8
|
+
* plugin — jobs, a rate limiter, anything that needs to persist something —
|
|
9
|
+
* wants that same connection rather than opening its own pool and splitting
|
|
10
|
+
* the budget again.
|
|
11
|
+
*
|
|
12
|
+
* As things stand `services()` is called with no arguments, so a plugin can
|
|
13
|
+
* only ever see what it produced itself. sekai-viewer works around this with
|
|
14
|
+
* an `inject` plugin that re-publishes the client, which means the ordering
|
|
15
|
+
* of that plugin in the array is load-bearing and nothing says so.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
type AnyPlugin = Parameters<typeof collectServices>[0][number];
|
|
19
|
+
|
|
20
|
+
function sqlPlugin(key = "db") {
|
|
21
|
+
return {
|
|
22
|
+
name: "sql",
|
|
23
|
+
config: () => ({}),
|
|
24
|
+
services: () => ({ [key]: { tag: "client" } }),
|
|
25
|
+
} as unknown as AnyPlugin;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
describe("plugin services", () => {
|
|
29
|
+
test("a later plugin can read what an earlier one published", async () => {
|
|
30
|
+
const consumer = {
|
|
31
|
+
name: "consumer",
|
|
32
|
+
// The whole point: use the client instead of opening a second pool.
|
|
33
|
+
services: async (ctx: { services: Record<string, unknown> }) => ({
|
|
34
|
+
seen: Boolean(ctx.services.db),
|
|
35
|
+
}),
|
|
36
|
+
} as unknown as AnyPlugin;
|
|
37
|
+
|
|
38
|
+
const collected = await collectServices([sqlPlugin(), consumer]);
|
|
39
|
+
|
|
40
|
+
expect(collected.seen).toBe(true);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
test("what an earlier plugin published is handed to the consumer", async () => {
|
|
44
|
+
const received: Record<string, unknown> = {};
|
|
45
|
+
const consumer = {
|
|
46
|
+
name: "consumer",
|
|
47
|
+
services: (ctx: { services: Record<string, unknown> }) => {
|
|
48
|
+
Object.assign(received, ctx.services);
|
|
49
|
+
return {};
|
|
50
|
+
},
|
|
51
|
+
} as unknown as AnyPlugin;
|
|
52
|
+
|
|
53
|
+
await collectServices([sqlPlugin("db"), consumer]);
|
|
54
|
+
|
|
55
|
+
expect(received.db).toEqual({ tag: "client" });
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
test("the client a consumer reads is the same object the sql plugin owns", async () => {
|
|
59
|
+
const client = { tag: "client" };
|
|
60
|
+
const owner = { name: "sql", services: () => ({ db: client }) } as unknown as AnyPlugin;
|
|
61
|
+
const consumer = {
|
|
62
|
+
name: "consumer",
|
|
63
|
+
services: (ctx: { services: Record<string, unknown> }) => ({ used: ctx.services.db }),
|
|
64
|
+
} as unknown as AnyPlugin;
|
|
65
|
+
|
|
66
|
+
const collected = await collectServices([owner, consumer]);
|
|
67
|
+
|
|
68
|
+
// Identity, not equality: opening a second pool to compare is the
|
|
69
|
+
// thing this is meant to prevent.
|
|
70
|
+
expect(collected.used).toBe(client);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
test("a consumer placed before the sql plugin sees nothing, rather than throwing", async () => {
|
|
74
|
+
// Order-independence is the point. A plugin must not have to be
|
|
75
|
+
// registered after the plugin it depends on to be correct.
|
|
76
|
+
const consumer = {
|
|
77
|
+
name: "consumer",
|
|
78
|
+
services: (ctx: { services: Record<string, unknown> }) => ({
|
|
79
|
+
seen: Boolean(ctx.services.db),
|
|
80
|
+
}),
|
|
81
|
+
} as unknown as AnyPlugin;
|
|
82
|
+
|
|
83
|
+
const collected = await collectServices([consumer, sqlPlugin()]);
|
|
84
|
+
|
|
85
|
+
expect(collected.seen).toBe(false);
|
|
86
|
+
expect(collected.db).toEqual({ tag: "client" });
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
test("a plugin that ignores the context still works", async () => {
|
|
90
|
+
const solo = {
|
|
91
|
+
name: "solo",
|
|
92
|
+
services: () => ({ a: 1 }),
|
|
93
|
+
} as unknown as AnyPlugin;
|
|
94
|
+
|
|
95
|
+
expect(await collectServices([solo])).toEqual({ a: 1 });
|
|
96
|
+
});
|
|
97
|
+
});
|
|
@@ -0,0 +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
|
+
});
|
package/test/streaming.test.ts
CHANGED
|
@@ -319,4 +319,60 @@ describe("streamed promises (deferred data)", () => {
|
|
|
319
319
|
expect(all).toContain("error:true");
|
|
320
320
|
expect(all).toContain('data["boom"]');
|
|
321
321
|
});
|
|
322
|
+
|
|
323
|
+
// The end-to-end version: a real streamed render, a component that reads
|
|
324
|
+
// the late value through the helper, and the real patch script run
|
|
325
|
+
// against a real event target. The unit tests in deferred.test.ts cover
|
|
326
|
+
// the helper alone; this one proves the two halves fit.
|
|
327
|
+
test("a component reading through deferred() shows the late value", async () => {
|
|
328
|
+
const dir = join(root, "deferred-helper");
|
|
329
|
+
await mkdir(dir, { recursive: true });
|
|
330
|
+
await writeFile(
|
|
331
|
+
join(dir, "+page.server.ts"),
|
|
332
|
+
[
|
|
333
|
+
"export async function load() {",
|
|
334
|
+
" return {",
|
|
335
|
+
' title: "Book",',
|
|
336
|
+
' reviews: new Promise((r) => setTimeout(() => r(["great"]), 25)),',
|
|
337
|
+
" };",
|
|
338
|
+
"}",
|
|
339
|
+
].join("\n"),
|
|
340
|
+
);
|
|
341
|
+
// The page renders the fallback while the value is in flight.
|
|
342
|
+
//
|
|
343
|
+
// Imported by absolute path because the fixture lives in a tmpdir,
|
|
344
|
+
// where neither a bare specifier nor a relative one can reach this
|
|
345
|
+
// package. That is a limitation of the fixture, not of the import a
|
|
346
|
+
// user would write; deferred.test.ts covers the helper itself.
|
|
347
|
+
await writeFile(
|
|
348
|
+
join(dir, "+page.tsx"),
|
|
349
|
+
[
|
|
350
|
+
'import { deferred } from "/opt/sigil/packages/grimoire/src/client/deferred.ts";',
|
|
351
|
+
"export default function P({ data }) {",
|
|
352
|
+
" const reviews = deferred(data.reviews, []);",
|
|
353
|
+
" return (",
|
|
354
|
+
" <div>",
|
|
355
|
+
" <h1>{data.title}</h1>",
|
|
356
|
+
" {reviews.done ? reviews.value.join() : \"[pending]\"}",
|
|
357
|
+
" </div>",
|
|
358
|
+
" );",
|
|
359
|
+
"}",
|
|
360
|
+
].join("\n"),
|
|
361
|
+
);
|
|
362
|
+
const tree = await scanRoutes(dir);
|
|
363
|
+
const matched = matchRoute(tree, new URL("http://localhost/"));
|
|
364
|
+
const chunks = await readChunks(await renderRoute(matched!, new Request("http://localhost/")));
|
|
365
|
+
|
|
366
|
+
// The shell goes out first, with the fallback rendered.
|
|
367
|
+
const shell = chunks.find((c) => c.includes("__grimoire_state__"))!;
|
|
368
|
+
expect(shell).toContain("Book");
|
|
369
|
+
expect(shell).toContain("[pending]");
|
|
370
|
+
expect(shell).not.toContain("great");
|
|
371
|
+
|
|
372
|
+
// The value arrives in a later chunk as the real patch script.
|
|
373
|
+
const late = chunks.find((c) => c.includes("grimoire:deferred"));
|
|
374
|
+
expect(late).toBeDefined();
|
|
375
|
+
expect(late).toContain("great");
|
|
376
|
+
expect(late).toContain('data["reviews"]');
|
|
377
|
+
});
|
|
322
378
|
});
|
|
@@ -0,0 +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
|
+
});
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import { execFileSync } from "node:child_process";
|
|
3
|
+
import { existsSync, readFileSync, readdirSync } from "node:fs";
|
|
4
|
+
import { dirname, join, normalize, relative } from "node:path";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* A packed tarball must contain every file its entry points import.
|
|
8
|
+
*
|
|
9
|
+
* `files` in package.json is a list someone types, and a module added later
|
|
10
|
+
* is not on it. Nothing in the workspace notices, because every import
|
|
11
|
+
* resolves against the source tree; only the installed tarball is missing
|
|
12
|
+
* the file, and it fails at boot in the app.
|
|
13
|
+
*
|
|
14
|
+
* Not hypothetical, twice: plugin-sql shipped without `database.ts` and
|
|
15
|
+
* `fragment.ts`, which `index.ts` imports, and plugin-jobs shipped without
|
|
16
|
+
* `store.ts`, `store-bun.ts` and `store-drizzle.ts`.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
const ROOT = new URL("../../../", import.meta.url).pathname;
|
|
20
|
+
|
|
21
|
+
interface Manifest {
|
|
22
|
+
name?: string;
|
|
23
|
+
version?: string;
|
|
24
|
+
private?: boolean;
|
|
25
|
+
module?: string;
|
|
26
|
+
main?: string;
|
|
27
|
+
exports?: unknown;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function shipped(): { dir: string; manifest: Manifest }[] {
|
|
31
|
+
return readdirSync(join(ROOT, "packages"), { withFileTypes: true })
|
|
32
|
+
.filter((e) => e.isDirectory())
|
|
33
|
+
.flatMap((e) => {
|
|
34
|
+
const file = join(ROOT, "packages", e.name, "package.json");
|
|
35
|
+
if (!existsSync(file)) return [];
|
|
36
|
+
const manifest = JSON.parse(readFileSync(file, "utf8")) as Manifest;
|
|
37
|
+
return [{ dir: join(ROOT, "packages", e.name), manifest }];
|
|
38
|
+
})
|
|
39
|
+
.filter(
|
|
40
|
+
({ manifest }) =>
|
|
41
|
+
manifest.name?.startsWith("@sigil-dev/") &&
|
|
42
|
+
typeof manifest.version === "string" &&
|
|
43
|
+
!manifest.private,
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Every path string reachable in an `exports` map. */
|
|
48
|
+
function exportTargets(exp: unknown): string[] {
|
|
49
|
+
if (typeof exp === "string") return [exp];
|
|
50
|
+
if (exp && typeof exp === "object") {
|
|
51
|
+
return Object.values(exp).flatMap(exportTargets);
|
|
52
|
+
}
|
|
53
|
+
return [];
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const SOURCE = /\.(ts|tsx|js|mjs)$/;
|
|
57
|
+
const IMPORT =
|
|
58
|
+
/(?:^|[\s;])(?:import|export)\s[^'"]*?from\s*["'](\.{1,2}\/[^"']+)["']|import\(\s*["'](\.{1,2}\/[^"']+)["']\s*\)|^import\s+["'](\.{1,2}\/[^"']+)["']/gm;
|
|
59
|
+
|
|
60
|
+
/** Source without comments, so an import shown in a doc example is not one. */
|
|
61
|
+
const code = (file: string): string =>
|
|
62
|
+
readFileSync(file, "utf8")
|
|
63
|
+
.replace(/\/\*[\s\S]*?\*\//g, "")
|
|
64
|
+
.replace(/^\s*\/\/.*$/gm, "");
|
|
65
|
+
|
|
66
|
+
function resolveImport(from: string, spec: string): string | null {
|
|
67
|
+
const base = normalize(join(dirname(from), spec));
|
|
68
|
+
// TypeScript's `./x.js` for `./x.ts`.
|
|
69
|
+
const ts = base.replace(/\.js$/, ".ts");
|
|
70
|
+
for (const candidate of [base, ts, `${base}.ts`, `${base}.tsx`, join(base, "index.ts")]) {
|
|
71
|
+
if (existsSync(candidate) && SOURCE.test(candidate)) return candidate;
|
|
72
|
+
}
|
|
73
|
+
return null;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** The files the entry points reach through relative imports. */
|
|
77
|
+
function reachable(dir: string, entries: string[]): Set<string> {
|
|
78
|
+
const seen = new Set<string>();
|
|
79
|
+
const queue = entries.map((e) => normalize(join(dir, e))).filter((f) => SOURCE.test(f));
|
|
80
|
+
while (queue.length) {
|
|
81
|
+
const file = queue.pop()!;
|
|
82
|
+
if (seen.has(file) || !existsSync(file)) continue;
|
|
83
|
+
seen.add(file);
|
|
84
|
+
for (const m of code(file).matchAll(IMPORT)) {
|
|
85
|
+
const spec = m[1] ?? m[2] ?? m[3];
|
|
86
|
+
if (!spec) continue;
|
|
87
|
+
const target = resolveImport(file, spec);
|
|
88
|
+
// Only files inside the package: a relative import that escapes
|
|
89
|
+
// it is a different bug, and one the pack cannot fix.
|
|
90
|
+
if (target && !relative(dir, target).startsWith("..")) queue.push(target);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return seen;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function packedFiles(dir: string): Set<string> {
|
|
97
|
+
const out = execFileSync("npm", ["pack", "--dry-run", "--json", "--ignore-scripts"], {
|
|
98
|
+
cwd: dir,
|
|
99
|
+
encoding: "utf8",
|
|
100
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
101
|
+
});
|
|
102
|
+
const [report] = JSON.parse(out) as Array<{ files: Array<{ path: string }> }>;
|
|
103
|
+
return new Set(report!.files.map((f) => normalize(join(dir, f.path))));
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
describe("workspace files", () => {
|
|
107
|
+
test("every file an entry point imports is in the tarball", () => {
|
|
108
|
+
const missing: string[] = [];
|
|
109
|
+
for (const { dir, manifest } of shipped()) {
|
|
110
|
+
const entries = [
|
|
111
|
+
...(manifest.module ? [manifest.module] : []),
|
|
112
|
+
...(manifest.main ? [manifest.main] : []),
|
|
113
|
+
...exportTargets(manifest.exports),
|
|
114
|
+
];
|
|
115
|
+
const packed = packedFiles(dir);
|
|
116
|
+
for (const file of reachable(dir, entries)) {
|
|
117
|
+
if (!packed.has(file)) {
|
|
118
|
+
missing.push(`${manifest.name}: ${relative(dir, file)} is imported but not packed`);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
expect(missing).toEqual([]);
|
|
123
|
+
}, 60_000);
|
|
124
|
+
|
|
125
|
+
test("no relative import points at a file that does not exist", () => {
|
|
126
|
+
// A type-only import of a missing file still runs, because Bun erases
|
|
127
|
+
// it, so this is the only thing that notices.
|
|
128
|
+
const broken: string[] = [];
|
|
129
|
+
for (const { dir, manifest } of shipped()) {
|
|
130
|
+
const entries = [
|
|
131
|
+
...(manifest.module ? [manifest.module] : []),
|
|
132
|
+
...exportTargets(manifest.exports),
|
|
133
|
+
];
|
|
134
|
+
for (const file of reachable(dir, entries)) {
|
|
135
|
+
for (const m of code(file).matchAll(IMPORT)) {
|
|
136
|
+
const spec = m[1] ?? m[2] ?? m[3];
|
|
137
|
+
// `./$types` is generated per route, not a file here.
|
|
138
|
+
if (spec && !spec.includes("$") && !resolveImport(file, spec)) {
|
|
139
|
+
broken.push(`${manifest.name}: ${relative(dir, file)} imports ${spec}`);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
expect(broken).toEqual([]);
|
|
145
|
+
});
|
|
146
|
+
});
|
|
@@ -0,0 +1,131 @@
|
|
|
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 = new URL("../../../", import.meta.url).pathname;
|
|
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
|
+
});
|
package/.grimoire/_routes.ts
DELETED