1// ## pages
2test("a page that this machine asks for gets the visualization, and the hash it can ask for again", async () => {
3 const debug = new incremental.Debugger(new incremental.Graph({ db: null }));
4 debug.update(result({ ok: true, value: null }));
5 let acceptEncoding: string | null = "unread";
6 const respond = debug.respond(async (request) => {
7 acceptEncoding = request.headers.get("accept-encoding");
8 return new Response("<p>hello</p>", { headers: { "content-type": "text/html", "content-length": "12" } });
9 });
10 const ask = (headers: Record<string, string>, init: RequestInit & { remoteAddress?: string } = {}) =>
11 respond(new Request("http://localhost:3000/page", { ...init, headers: { host: "localhost:3000", ...headers } }), {
12 node: progress.nullNode,
13 remoteAddress: init.remoteAddress ?? "127.0.0.1",
14 });
15
16 const page = await ask({ "sec-fetch-dest": "document", "accept-encoding": "gzip" });
17 const html = await page.text();
18 const hash = UNWRAP(/visualization\.ts\?page=([\w-]+)/.exec(html), html)[1];
19 assert.ok(html.startsWith("<p>hello</p><script type=module src=/.clover/lib/incremental/visualization.ts?page="));
20 assert.equal(page.headers.get("content-length"), null);
21 // a page is read whole, which a compressed one can't be
22 assert.equal(acceptEncoding, null);
23
24 const check = await ask({ "clover-page": "" });
25 assert.equal(check.status, 204);
26 assert.equal(check.headers.get("clover-page"), hash);
27
28 // what was posted can't be asked for again, so it never reloads
29 const posted = await ask({ "sec-fetch-dest": "document" }, { method: "POST", body: "a=1" });
30 assert.match(await posted.text(), /visualization\.ts><\/script>$/);
31});
32
33test("a page asked for before a build was served shows that build, until it can reload into the page", async () => {
34 const debug = new incremental.Debugger(new incremental.Graph({ db: null }));
35 const respond = debug.respond(async () => new Response("<p>hello</p>", { headers: { "content-type": "text/html" } }));
36 const ask = () =>
37 respond(
38 new Request("http://localhost:3000/page", { headers: { host: "localhost:3000", "sec-fetch-dest": "document" } }),
39 { node: progress.nullNode, remoteAddress: "127.0.0.1" },
40 ).then((response) => response.text());
41
42 assert.match(await ask(), /^<!doctype html>.*visualization\.ts\?page=&graph><\/script>$/);
43 debug.update(result({ ok: true, value: null }));
44 assert.match(
45 await ask(),
46 /^<p>hello<\/p><script type=module src=\/\.clover\/lib\/incremental\/visualization\.ts\?page=[\w-]+>/,
47 );
48});
49
50test("a request through a proxy, from another machine, or for another name is served as it is", async () => {
51 const debug = new incremental.Debugger(new incremental.Graph({ db: null }));
52 debug.update(result({ ok: true, value: null }));
53 const respond = debug.respond(async () => new Response("<p>hello</p>", { headers: { "content-type": "text/html" } }));
54 const page = (headers: Record<string, string>, remoteAddress: string) =>
55 respond(new Request("http://localhost:3000/", { headers: { "sec-fetch-dest": "document", ...headers } }), {
56 node: progress.nullNode,
57 remoteAddress,
58 }).then((response) => response.text());
59
60 assert.notEqual(await page({ host: "localhost:3000" }, "::1"), "<p>hello</p>");
61 assert.notEqual(await page({ host: "127.0.0.1:3000" }, "::ffff:127.0.0.1"), "<p>hello</p>");
62 assert.equal(await page({ host: "localhost:3000" }, "192.168.1.20"), "<p>hello</p>");
63 assert.equal(await page({ host: "localhost:3000", "x-forwarded-for": "1.2.3.4" }, "127.0.0.1"), "<p>hello</p>");
64 assert.equal(await page({ host: "localhost:3000", forwarded: "for=1.2.3.4" }, "127.0.0.1"), "<p>hello</p>");
65 // a name that points here by dns can be any page's
66 assert.equal(await page({ host: "rebound.example:3000" }, "127.0.0.1"), "<p>hello</p>");
67});
68
69test("the library is served as javascript, and only its typescript", async () => {
70 const debug = new incremental.Debugger(new incremental.Graph({ db: null }));
71 const respond = debug.respond(async () => new Response("from the site", { status: 404 }));
72 const get = (pathname: string) =>
73 respond(new Request(`http://localhost:3000${pathname}`, { headers: { host: "localhost:3000" } }), {
74 node: progress.nullNode,
75 remoteAddress: "127.0.0.1",
76 });
77
78 const code = await get("/.clover/lib/incremental/visualization.ts");
79 assert.equal(code.headers.get("content-type"), "text/javascript;charset=utf-8");
80 const text = await code.text();
81 assert.match(text, /^import \* as progress from "\.\.\/progress\.ts";$/m);
82 assert.doesNotMatch(text, /import type/);
83 // safari has no `using`, nor what it disposes with
84 assert.match(text, /^import "\.\.\/safari\.js";$/m);
85 assert.match(await (await get("/.clover/lib/safari.js")).text(), /DisposableStack/);
86 assert.doesNotMatch(await (await get("/.clover/lib/progress.ts")).text(), /^\s*using /m);
87 assert.doesNotMatch(await (await get("/.clover/lib/log.ts")).text(), /using/);
88 for (const pathname of ["/.clover/lib/package.json", "/.clover/lib/missing.ts", "/.clover/lib/..%2Fpackage.json"]) {
89 assert.equal((await get(pathname)).status, 404, pathname);
90 }
91 assert.equal(await (await get("/.clover/lib/%2e%2e/run.js")).text(), "from the site");
92});
93
94test.skipIf(os.platform() === "win32")(
95 "a page opens a file in the editor, which only this server's pages may",
96 async () => {
97 const graph = new incremental.Graph({ db: null });
98 const respond = new incremental.Debugger(graph).respond(async () => new Response("from the site"));
99 const dir = fs.mkdtempSync(path.join(os.tmpdir(), "clover-editor-"));
100 const editor = path.join(dir, "code");
101 fs.writeFileSync(
102 editor,
103 `#!${process.execPath}\nrequire("node:fs").writeFileSync(process.argv[1] + ".argv", JSON.stringify(process.argv.slice(2)));\n`,
104 { mode: 0o755 },
105 );
106 const open = (origin: string) =>
107 respond(
108 new Request("http://localhost:3000/.clover/open", {
109 method: "POST",
110 headers: { host: "localhost:3000", origin },
111 body: "lib/incremental.ts:12:4",
112 }),
113 { node: progress.nullNode, remoteAddress: "127.0.0.1" },
114 );
115 process.env.LAUNCH_EDITOR = editor;
116 try {
117 assert.equal((await open("http://elsewhere.example")).status, 403);
118 assert.equal((await open("http://localhost:3000")).status, 204);
119 while (!fs.existsSync(editor + ".argv")) await new Promise((resolve) => setTimeout(resolve, 10));
120 // a relative path is from the project
121 assert.deepEqual(JSON.parse(fs.readFileSync(editor + ".argv", "utf8")), [
122 "-r",
123 "-g",
124 `${path.join(graph.root, "lib/incremental.ts")}:12:4`,
125 ]);
126 } finally {
127 delete process.env.LAUNCH_EDITOR;
128 fs.rmSync(dir, { recursive: true, force: true });
129 }
130 },
131);
132
133// ## the socket
134test("a page hears of every build, its progress, and its errors", async () => {
135 const debug = new incremental.Debugger(new incremental.Graph({ db: null }));
136 await using server = await http.serve({
137 respond: debug.respond(async () => new Response("")),
138 upgrade: debug.upgrade,
139 port: 0,
140 });
141 // as a build's errors say where they came from
142 const error = Object.defineProperties(new Error("the page did not render"), {
143 steps: { value: ["render"] },
144 fatal: { value: true },
145 cached: { value: false },
146 });
147
148 debug.update(result({ ok: true, value: null }));
149 const page = await connect(server.url, { origin: server.url.slice(0, -1) });
150 const [type, { at, ...status }] = await page.next("status");
151 assert.equal(type, "status");
152 assert.ok(Math.abs(at - Date.now()) < 5000);
153 assert.deepEqual(status, {
154 state: "ok",
155 earlier: false,
156 changed: ["src/page.marko", "src/"],
157 stats: { duration: 12, ran: 1, reused: 2, written: 3, cacheBytes: 4, outputBytes: 5 },
158 statusLine: "Watching 1 file",
159 errors: [],
160 tokens: {},
161 });
162
163 {
164 using node = debug.progress.start("build");
165 assert.deepEqual(await page.next(), ["building"]);
166 const [type, event] = await page.next();
167 assert.equal(type, "progress");
168 assert.ok(JSON.stringify(event).includes("\"build\""));
169 node.end();
170 }
171
172 // errors that a build is still served with are warnings
173 debug.update(result({ ok: false, error: new incremental.BuildError([error], null), value: null }));
174 const warnings = await page.next("status");
175 assert.equal(warnings[1].state, "warnings");
176 const [diagnostic] = warnings[1].errors;
177 assert.deepEqual([diagnostic?.label, diagnostic?.message, diagnostic?.notes.at(-1)?.text], [
178 "error",
179 "the page did not render",
180 "thrown in render",
181 ]);
182 assert.match(UNWRAP(diagnostic?.location?.file), /debugger\.test\.ts$/);
183 // the code is colored before a page gets it
184 const { file, line, first, code } = UNWRAP(diagnostic?.location);
185 const text = UNWRAP(code[UNWRAP(line) - first]);
186 assert.equal(warnings[1].tokens[`${file}:${line}`]?.[text.indexOf(`"the page`)], "s");
187 debug.update(result({ ok: false, error: new incremental.BuildError([error], undefined) }));
188 assert.equal((await page.next("status"))[1].state, "failed");
189 // what a build made can fail to load, which leaves the last build served
190 debug.update(result({ ok: true, value: null }), new Error("the backend did not load"));
191 const [, { state, earlier, errors: [unloaded] }] = await page.next("status");
192 assert.deepEqual([state, earlier, unloaded?.message, unloaded?.notes.at(-1)?.text], [
193 "failed",
194 true,
195 "the backend did not load",
196 "thrown while the handler loaded",
197 ]);
198
199 // a page that leaves is answered, and is not written to again
200 assert.equal(await page.close(), true);
201 debug.update(result({ ok: true, value: null }));
202});
203
204test("a page hears what happens to steps, and reads the whole graph", async () => {
205 const graph = new incremental.Graph({ db: null });
206 const debug = new incremental.Debugger(graph);
207 await using server = await http.serve({
208 respond: debug.respond(async () => new Response("")),
209 upgrade: debug.upgrade,
210 port: 0,
211 });
212 const page = await connect(server.url, {});
213 function greet() {
214 return "hello";
215 }
216 await graph.incrementalBuild({
217 progress: debug.progress,
218 buildAndWriteOutput: (ref) => ref.step(greet).read(),
219 });
220
221 const heard: incremental.StepEvent[] = [];
222 while (!heard.some((event) => event.state === "ran" && event.label.startsWith("greet"))) {
223 heard.push(...(await page.next("steps"))[1]);
224 }
225 const greeting = heard.filter((event) => event.label.startsWith("greet")).map((event) => event.state);
226 assert.deepEqual(greeting, ["checking", "running", "ran"]);
227
228 const snapshot = await debug.respond(async () => new Response(""))(
229 new Request(new URL("/.clover/graph", server.url), { headers: { host: new URL(server.url).host } }),
230 { node: progress.nullNode, remoteAddress: "127.0.0.1" },
231 ).then((response) => response.json() as Promise<incremental.GraphSnapshot>);
232 const step = UNWRAP(snapshot.steps.find((step) => step.label.startsWith("greet")));
233 // a step depends on the code that started it
234 assert.ok(step.steps.some((key) => key.startsWith("closure:") && key.endsWith("/debugger.test.ts")));
235 assert.ok(step.bytes > 0);
236 assert.ok(snapshot.inputs.some((input) => input.url.endsWith("/debugger.test.ts")));
237});
238
239test("only this server's pages can open the socket", async () => {
240 const debug = new incremental.Debugger(new incremental.Graph({ db: null }));
241 await using server = await http.serve({
242 respond: debug.respond(async () => new Response("")),
243 upgrade: debug.upgrade,
244 port: 0,
245 });
246 await assert.rejects(connect(server.url, { origin: "http://elsewhere.example" }));
247 await assert.rejects(connect(server.url, { "x-forwarded-for": "1.2.3.4" }));
248});
249
250test("a page asked for with an upgrade to another protocol is served as a page", async () => {
251 const debug = new incremental.Debugger(new incremental.Graph({ db: null }));
252 await using server = await http.serve({
253 respond: debug.respond(async () => new Response("page")),
254 upgrade: debug.upgrade,
255 shouldUpgrade: debug.shouldUpgrade,
256 port: 0,
257 });
258 // how `curl --http2` asks for a page
259 const client = net.connect(server.port, "127.0.0.1");
260 client.write(
261 "GET / HTTP/1.1\r\nhost: localhost\r\nconnection: Upgrade, HTTP2-Settings, close\r\nupgrade: h2c\r\n"
262 + "http2-settings: AAMAAABkAAQCAAAAAAIAAAAA\r\n\r\n",
263 );
264 const response = await consumers.text(client);
265 assert.match(response, /^HTTP\/1\.1 200 /);
266 assert.match(response, /page/);
267 assert.ok(await (await connect(server.url, {})).close());
268});
269
270// ## serving
271test("a server listens before the first build, and a request waits for what serves it", async () => {
272 await using server = await serve((ref) =>
273 ref.step(function greet() {
274 return "hello";
275 }).read()
276 );
277 const page = fetch(server.url);
278 const builds = server.watch();
279 const first = await builds.next();
280 assert.ok(!first.done && first.value.ok);
281 server.setHandler(() => new Response(first.value.value));
282 assert.equal(await (await page).text(), "hello");
283 await builds.return();
284});
285
286test("a handler given as a promise serves once it loads", async () => {
287 await using server = await serve(async () => "hello");
288 const page = fetch(server.url);
289 const builds = server.watch();
290 const first = await builds.next();
291 assert.ok(!first.done && first.value.ok);
292 server.setHandler(Promise.resolve(() => new Response(first.value.value)));
293 assert.equal(await (await page).text(), "hello");
294 await builds.return();
295});
296
297test("a build that left nothing to serve answers requests with why", async () => {
298 await using server = await serve((ref) =>
299 ref.step(function fail(): string {
300 throw new Error("the page did not render");
301 }).read()
302 );
303 const page = fetch(server.url);
304 const builds = server.watch();
305 const first = await builds.next();
306 assert.ok(!first.done && !first.value.ok);
307 assert.equal((await page).status, 503);
308 await builds.return();
309});
310
311test("a request waits for the handler of a build that returned nothing", async () => {
312 // the build takes long enough for the request to be waiting by its end
313 await using server = await serve(() => new Promise((resolve) => setTimeout(resolve, 100)));
314 const page = fetch(server.url);
315 const builds = server.watch();
316 await builds.next();
317 server.setHandler(() => new Response("hello"));
318 assert.equal(await (await page).text(), "hello");
319 await builds.return();
320});
321
322test("a server that reads its files only once serves pages without a debugger", async () => {
323 await using server = await serve(async () => {});
324 const builds = server.watch();
325 await builds.next();
326 server.setHandler(() => new Response("<p>hello</p>", { headers: { "content-type": "text/html" } }));
327 const page = await fetch(server.url, { headers: { "sec-fetch-dest": "document" } });
328 assert.equal(await page.text(), "<p>hello</p>");
329 await builds.return();
330});
331
332/** a quiet server on a random port, which reads its files once */
333function serve<T>(buildAndWriteOutput: (ref: incremental.Ref) => Promise<T>) {
334 return incremental.serve({
335 graph: new incremental.Graph({ db: null }),
336 buildAndWriteOutput,
337 watchFilesystem: false,
338 port: 0,
339 log: log.headlessScope(() => {}),
340 progress: new progress.Root(),
341 });
342}
343
344/** what a build did, as a watch yields it */
345function result(outcome: { ok: true; value: unknown } | { ok: false; error: incremental.BuildError; value?: unknown }) {
346 return {
347 ...outcome,
348 changed: [
349 url.pathToFileURL(path.resolve("src/page.marko")).href,
350 url.pathToFileURL(path.resolve("src")).href + "/",
351 ],
352 stats: { duration: 12, ran: 1, reused: 2, written: 3, cacheBytes: 4, outputBytes: 5 },
353 watching: { file: 1 },
354 statusLine: "Watching 1 file",
355 };
356}
357
358/** a page's end of the socket, which reads the messages in order */
359async function connect(base: string, headers: Record<string, string>) {
360 // the client of node.js takes headers, which a browser's does not
361 const socket = new WebSocket(new URL("/.clover/socket", base.replace(/^http/, "ws")), { headers } as never);
362 const queue: incremental.DebuggerMessage[] = [];
363 const waiting: Array<() => void> = [];
364 socket.onmessage = (event) => {
365 queue.push(JSON.parse(event.data));
366 waiting.shift()?.();
367 };
368 await new Promise((resolve, reject) => {
369 socket.onopen = resolve;
370 socket.onerror = () => reject(new Error("the socket was refused"));
371 });
372 return {
373 /** the next message, or the next of a type */
374 async next<T extends incremental.DebuggerMessage[0] = incremental.DebuggerMessage[0]>(
375 type?: T,
376 ): Promise<Extract<incremental.DebuggerMessage, [T, ...unknown[]]>> {
377 while (true) {
378 while (queue.length === 0) await new Promise<void>((resolve) => waiting.push(resolve));
379 const message = queue.shift()!;
380 if (!type || message[0] === type) return message as Extract<incremental.DebuggerMessage, [T, ...unknown[]]>;
381 }
382 },
383 /** whether the server answered the close */
384 close: () =>
385 new Promise<boolean>((resolve) => {
386 socket.onclose = (event) => resolve(event.wasClean);
387 socket.close();
388 }),
389 };
390}
391
392import assert from "node:assert/strict";
393import * as fs from "node:fs";
394import * as net from "node:net";
395import * as os from "node:os";
396import * as path from "node:path";
397import * as consumers from "node:stream/consumers";
398import * as url from "node:url";
399import { test } from "vitest";
400import { UNWRAP } from "../assert.ts";
401import * as http from "../http.ts";
402import * as incremental from "../incremental.ts";
403import * as log from "../log.ts";
404import * as progress from "../progress.ts";