@bigmistqke/whiteboard 0.6.1 → 0.8.0

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 (101) hide show
  1. package/README.md +23 -216
  2. package/bin/whiteboard.ts +82 -239
  3. package/dist/bin/whiteboard.js +70 -200
  4. package/dist/client/runtime.js +297 -61
  5. package/dist/plugins/badge/README.md +3 -1
  6. package/dist/plugins/badge/public/style.css +17 -0
  7. package/dist/plugins/chat/public/client.js +2 -2
  8. package/dist/plugins/chat/public/style.css +19 -6
  9. package/dist/plugins/code/README.md +10 -0
  10. package/dist/plugins/code/index.js +31 -22
  11. package/dist/plugins/code/public/client.js +1 -0
  12. package/dist/plugins/code/public/style.css +33 -2
  13. package/dist/plugins/comments/public/client.js +7 -2
  14. package/dist/plugins/comments/public/style.css +4 -0
  15. package/dist/plugins/form/public/style.css +2 -0
  16. package/dist/plugins/grid/public/style.css +0 -5
  17. package/dist/plugins/iframe/index.js +2 -2
  18. package/dist/plugins/index.js +5 -1
  19. package/dist/plugins/lightbox/public/client.js +1 -1
  20. package/dist/plugins/terminal/index.js +7 -9
  21. package/dist/plugins/terminal/public/client.js +1 -1
  22. package/dist/plugins/terminal/public/style.css +3 -3
  23. package/dist/plugins/timeline/README.md +40 -0
  24. package/dist/plugins/timeline/index.js +82 -0
  25. package/dist/plugins/timeline/public/style.css +118 -0
  26. package/dist/plugins/toc/README.md +13 -13
  27. package/dist/plugins/toc/index.js +6 -4
  28. package/dist/plugins/toc/public/client.js +1 -1
  29. package/dist/plugins/toc/public/style.css +19 -2
  30. package/dist/plugins/window/index.js +4 -4
  31. package/dist/solid/index.js +2 -1
  32. package/dist/src/auth.js +7 -3
  33. package/dist/src/build.js +159 -0
  34. package/dist/src/client/page/base.css +74 -35
  35. package/dist/src/client/page/main.js +16 -5
  36. package/dist/src/document.js +93 -0
  37. package/dist/src/events.js +7 -15
  38. package/dist/src/hub.js +90 -53
  39. package/dist/src/listen.js +100 -0
  40. package/dist/src/listeners.js +67 -0
  41. package/dist/src/pages/index.js +1 -1
  42. package/dist/src/pages/markdown.js +23 -11
  43. package/dist/src/pages/render.js +13 -8
  44. package/dist/src/serve.js +2 -0
  45. package/dist/src/server.js +204 -514
  46. package/dist/src/site.js +173 -0
  47. package/dist/src/status.js +63 -1
  48. package/dist/src/ws.js +128 -0
  49. package/package.json +1 -1
  50. package/plugins/README.md +258 -0
  51. package/plugins/badge/README.md +3 -1
  52. package/plugins/badge/public/style.css +17 -0
  53. package/plugins/chat/client.tsx +34 -6
  54. package/plugins/chat/public/style.css +19 -6
  55. package/plugins/code/README.md +10 -0
  56. package/plugins/code/client.ts +73 -0
  57. package/plugins/code/index.ts +38 -26
  58. package/plugins/code/public/style.css +33 -2
  59. package/plugins/comments/client.ts +56 -6
  60. package/plugins/comments/public/style.css +4 -0
  61. package/plugins/form/public/style.css +2 -0
  62. package/plugins/grid/public/style.css +0 -5
  63. package/plugins/iframe/index.ts +2 -2
  64. package/plugins/index.ts +6 -1
  65. package/plugins/lightbox/client.ts +13 -3
  66. package/plugins/terminal/client.ts +10 -2
  67. package/plugins/terminal/index.ts +8 -6
  68. package/plugins/terminal/public/style.css +3 -3
  69. package/plugins/timeline/README.md +40 -0
  70. package/plugins/timeline/index.ts +92 -0
  71. package/plugins/timeline/public/style.css +118 -0
  72. package/plugins/toc/README.md +13 -13
  73. package/plugins/toc/client.ts +58 -32
  74. package/plugins/toc/index.ts +13 -13
  75. package/plugins/toc/public/style.css +19 -2
  76. package/plugins/window/index.ts +4 -4
  77. package/skills/whiteboard/SKILL.md +3 -1
  78. package/solid/index.tsx +3 -0
  79. package/src/auth.ts +7 -2
  80. package/src/build.ts +213 -0
  81. package/src/client/forms.ts +102 -5
  82. package/src/client/page/base.css +74 -35
  83. package/src/client/page/elsewhere.ts +14 -4
  84. package/src/client/page/waiting.ts +10 -5
  85. package/src/client/read.ts +16 -3
  86. package/src/client/runtime.ts +219 -52
  87. package/src/client/toast.ts +36 -6
  88. package/src/define.ts +44 -18
  89. package/src/document.ts +191 -0
  90. package/src/events.ts +18 -17
  91. package/src/hub.ts +102 -56
  92. package/src/listen.ts +125 -0
  93. package/src/listeners.ts +95 -0
  94. package/src/pages/index.ts +1 -1
  95. package/src/pages/markdown.ts +29 -18
  96. package/src/pages/render.ts +13 -8
  97. package/src/serve.ts +2 -0
  98. package/src/server.ts +223 -611
  99. package/src/site.ts +165 -0
  100. package/src/status.ts +100 -0
  101. package/src/ws.ts +155 -0
@@ -1,13 +1,14 @@
1
1
  #!/usr/bin/env node
2
2
  import { configFileIn, findProjectDir, pageId, project as project$1 } from "../src/config.js";
3
- import { CLAIM, appendEvent, editEvent, follow, forAgent, handOut, matches, pick, pickedUp, prune, read, readEvents } from "../src/events.js";
3
+ import { appendEvent, editEvent, follow, forAgent, handOut, matches, pick, prune, read, readEvents } from "../src/events.js";
4
4
  import { BUILTIN_COMMANDS, createConfigLoader, pluginDir } from "../src/loader.js";
5
5
  import { parseCommandArgs, pluginCommands, usage } from "../src/plugins.js";
6
6
  import { projectToken } from "../src/auth.js";
7
7
  import { PACKAGE_DIR, VERSION, compareVersions, packagePath } from "../src/package.js";
8
8
  import { recordedSharedPort, runningServer, sharedServer } from "../src/running.js";
9
9
  import { machineDir, machineServerFile, matchesEntries, readMachineConfig, serverPaths, stateDir } from "../src/entries.js";
10
- import { statusLines, takenByOther, whose } from "../src/status.js";
10
+ import { CLAIM, pickedUp, statusLines, whose } from "../src/status.js";
11
+ import { fallback, listen } from "../src/listen.js";
11
12
  import fs from "node:fs";
12
13
  import os from "node:os";
13
14
  import path from "node:path";
@@ -249,99 +250,6 @@ async function askInProcess(route) {
249
250
  listener.close();
250
251
  }
251
252
  }
252
- /**
253
- * Tell the project's server that an agent is waiting, for as long as this command runs: pages show
254
- * what is waited for. Holds one connection open, the listener; reconnects when the server restarts.
255
- * Reading the log does not depend on it, so answers are never missed when the server is away.
256
- */
257
- /**
258
- * Tell the server who is listening, for as long as the command runs (again after a restart). The
259
- * fallback is refused when another one runs: it exits, saying so. Resolves once the server took it.
260
- */
261
- function listen(f, command, fallback = false) {
262
- return new Promise((registered) => {
263
- const connect = async () => {
264
- let base;
265
- try {
266
- base = await needServer();
267
- } catch {
268
- return setTimeout(connect, 2e3).unref();
269
- }
270
- const q = new URLSearchParams({
271
- command,
272
- ...f.page ? { page: f.page } : {},
273
- ...f.type ? { type: f.type } : {},
274
- ...fallback ? { fallback: "" } : {}
275
- });
276
- let again = false;
277
- const retry = (ms) => {
278
- if (!again) {
279
- again = true;
280
- setTimeout(connect, ms).unref();
281
- }
282
- };
283
- const req = http.get(`${base}/api/listen?${q}`, (res) => {
284
- if (res.statusCode === 409) {
285
- console.error("whiteboard: another agent already takes what no other listener does (a wait or watch without --page; one per project). Add --page to wait on a page of your own.");
286
- process.exit(1);
287
- }
288
- res.resume();
289
- registered();
290
- res.on("close", () => retry(1e3));
291
- });
292
- req.on("error", () => retry(2e3));
293
- req.on("socket", (socket) => socket.unref());
294
- };
295
- connect();
296
- });
297
- }
298
- /**
299
- * Who else listens, kept current: the fallback leaves to them what they would take. Calls
300
- * `changed` after each change (the server's live channel says when).
301
- */
302
- async function otherListeners(changed, server = needServer) {
303
- let list = [];
304
- const load = async () => {
305
- try {
306
- list = await (await fetch(`${await server()}/api/listeners`)).json();
307
- } catch {}
308
- changed();
309
- };
310
- const follow = async () => {
311
- let again = false;
312
- const retry = () => {
313
- if (!again) {
314
- again = true;
315
- setTimeout(follow, 1e3).unref();
316
- }
317
- };
318
- let base;
319
- try {
320
- base = await server();
321
- } catch {
322
- return retry();
323
- }
324
- const req = http.get(`${base}/api/watch?dir`, (res) => {
325
- let buf = "";
326
- res.setEncoding("utf8");
327
- res.on("data", (chunk) => {
328
- buf += chunk;
329
- if (buf.includes("event: listeners")) {
330
- buf = "";
331
- load();
332
- }
333
- if (buf.length > 4096) buf = buf.slice(-64);
334
- });
335
- res.on("close", retry);
336
- load();
337
- });
338
- req.on("error", retry);
339
- req.on("socket", (socket) => socket.unref());
340
- };
341
- await load();
342
- follow();
343
- return () => list;
344
- }
345
253
  function init(name) {
346
254
  const dir = process.cwd();
347
255
  const wb = path.join(dir, ".whiteboard");
@@ -560,7 +468,7 @@ switch (cmd) {
560
468
  })
561
469
  })).json();
562
470
  if (!tabs) {
563
- console.error(`whiteboard: no browser has ${id} open; \`whiteboard show ${target}\` prints its URL`);
471
+ console.error(`whiteboard: no tab of ${id} is connected now; one that is open scrolls there once it is back (a background tab connects again when it is shown). \`whiteboard show ${target}\` prints its URL`);
564
472
  process.exit(1);
565
473
  }
566
474
  console.log(`scrolled ${tabs} tab${tabs > 1 ? "s" : ""} of ${id} to ${JSON.stringify(to)}`);
@@ -638,39 +546,54 @@ switch (cmd) {
638
546
  break;
639
547
  }
640
548
  const { handedOut } = await projectConfig(proj);
641
- const fallback = !f.page;
642
- let recheck = () => {};
643
- const others = fallback ? (await listen(f, cmd, true), await otherListeners(() => recheck())) : null;
644
- const held = /* @__PURE__ */ new Set();
645
- const skip = (e) => {
646
- if (!held.has(e.id)) {
647
- held.add(e.id);
648
- setTimeout(() => recheck(), 500).unref();
649
- return true;
650
- }
651
- return takenByOther(others(), e);
549
+ const query = {
550
+ command: cmd,
551
+ ...f.page ? { page: f.page } : {},
552
+ ...f.type ? { type: f.type } : {}
553
+ };
554
+ const refused = (reason) => {
555
+ console.error(`whiteboard: ${reason}: another agent already takes what no other listener does (a wait or watch without --page; one per project). Add --page to wait on a page of your own.`);
556
+ process.exit(1);
652
557
  };
653
- handOut(proj.eventsFile, f, (e) => console.log(JSON.stringify(e)), {
558
+ const at = () => needServer().catch(() => null);
559
+ const options = {
654
560
  count: cmd === "wait" ? Number(opts.count ?? 1) : Infinity,
655
- forAgent: forAgent(handedOut),
656
- ...others && {
657
- skip,
658
- recheck: (again) => {
659
- recheck = again;
660
- }
661
- }
662
- }).then(async () => {
663
- if (cmd === "wait" && !fallback) try {
664
- const pages = await api(`/api/status?page=${encodeURIComponent(f.page)}`);
665
- for (const p of pages) if (p.open.length) console.error(`whiteboard: still unanswered on ${p.page}: ${p.open.join(", ")} → run \`whiteboard wait --page ${relFile(p.file)}\` again, in the background, once you've acted on this`);
666
- } catch {}
667
- process.exit(0);
668
- });
669
- if (!fallback) listen(f, cmd);
561
+ forAgent: forAgent(handedOut)
562
+ };
670
563
  if (opts.timeout && cmd === "wait") setTimeout(() => {
671
564
  console.error("timeout");
672
565
  process.exit(2);
673
566
  }, Number(opts.timeout) * 1e3);
567
+ if (!f.page) {
568
+ const update = fallback((_, handing) => {
569
+ handOut(proj.eventsFile, f, (e) => console.log(JSON.stringify(e)), {
570
+ ...options,
571
+ ...handing
572
+ }).then(() => process.exit(0));
573
+ });
574
+ await listen({
575
+ at,
576
+ query: {
577
+ ...query,
578
+ fallback: ""
579
+ },
580
+ onSnapshot: update,
581
+ onRefused: refused
582
+ });
583
+ } else {
584
+ await listen({
585
+ at,
586
+ query,
587
+ onRefused: refused
588
+ }, { orNone: true });
589
+ handOut(proj.eventsFile, f, (e) => console.log(JSON.stringify(e)), options).then(async () => {
590
+ if (cmd === "wait") try {
591
+ const pages = await api(`/api/status?page=${encodeURIComponent(f.page)}`);
592
+ for (const p of pages) if (p.open.length) console.error(`whiteboard: still unanswered on ${p.page}: ${p.open.join(", ")} → run \`whiteboard wait --page ${relFile(p.file)}\` again, in the background, once you've acted on this`);
593
+ } catch {}
594
+ process.exit(0);
595
+ });
596
+ }
674
597
  break;
675
598
  }
676
599
  case "append": {
@@ -791,67 +714,12 @@ async function entryFallback(entry, f, command) {
791
714
  console.error("timeout");
792
715
  process.exit(2);
793
716
  }, Number(opts.timeout) * 1e3);
794
- await new Promise((registered) => {
795
- const connect = async () => {
796
- const shared = await sharedServer(file);
797
- if (!shared) return setTimeout(connect, 2e3).unref();
798
- const q = new URLSearchParams({
799
- entry,
800
- command,
801
- ...f.type ? { type: f.type } : {}
802
- });
803
- let again = false;
804
- const retry = (ms) => {
805
- if (!again) {
806
- again = true;
807
- setTimeout(connect, ms).unref();
808
- }
809
- };
810
- const req = http.get(`${shared.origin}/api/listen?${q}`, (res) => {
811
- if (res.statusCode === 409) {
812
- let body = "";
813
- res.on("data", (d) => body += d);
814
- res.on("end", () => {
815
- console.error(`whiteboard: ${JSON.parse(body || "{}").error ?? "another agent is the fallback"}. Add --page to wait on a page of your own.`);
816
- process.exit(1);
817
- });
818
- return;
819
- }
820
- res.resume();
821
- registered();
822
- res.on("close", () => retry(1e3));
823
- });
824
- req.on("error", () => retry(2e3));
825
- req.on("socket", (socket) => socket.unref());
826
- };
827
- connect();
828
- });
829
- const following = /* @__PURE__ */ new Map();
830
- async function start(p, origin) {
717
+ let origin = "";
718
+ const page = (id) => id.split("/").map(encodeURIComponent).join("/");
719
+ const update = fallback((p, handing) => {
831
720
  const proj = project$1(p.dir);
832
721
  if (!proj) return;
833
- const stop = new AbortController();
834
- following.set(p.dir, stop);
835
- const { handedOut } = await projectConfig(proj);
836
- let recheck = () => {};
837
- const at = async () => {
838
- const shared = await sharedServer(file);
839
- const now = shared?.projects.find((x) => x.dir === p.dir);
840
- if (!shared || !now) throw new Error("not served");
841
- return `${shared.origin}${now.base}`;
842
- };
843
- const others = await otherListeners(() => recheck(), at);
844
- const held = /* @__PURE__ */ new Set();
845
- const skip = (e) => {
846
- if (!held.has(e.id)) {
847
- held.add(e.id);
848
- setTimeout(() => recheck(), 500).unref();
849
- return true;
850
- }
851
- return takenByOther(others(), e);
852
- };
853
- const page = (id) => id.split("/").map(encodeURIComponent).join("/");
854
- await handOut(proj.eventsFile, { type: f.type }, (e) => {
722
+ projectConfig(proj).then(({ handedOut }) => handOut(proj.eventsFile, { type: f.type }, (e) => {
855
723
  console.log(JSON.stringify({
856
724
  ...e,
857
725
  project: p.dir,
@@ -860,25 +728,27 @@ async function entryFallback(entry, f, command) {
860
728
  if (++total >= count) done.abort();
861
729
  }, {
862
730
  forAgent: forAgent(handedOut),
863
- signal: AbortSignal.any([stop.signal, done.signal]),
864
- skip,
865
- recheck: (again) => {
866
- recheck = again;
867
- }
868
- });
869
- }
870
- const sync = async () => {
871
- const shared = await sharedServer(file);
872
- if (!shared) return;
873
- const mine = shared.projects.filter((p) => p.entry === entry);
874
- for (const p of mine) if (!following.has(p.dir)) start(p, shared.origin);
875
- for (const [dir, stop] of following) if (!mine.some((p) => p.dir === dir)) {
876
- stop.abort();
877
- following.delete(dir);
731
+ ...handing,
732
+ signal: AbortSignal.any([handing.signal, done.signal])
733
+ }));
734
+ });
735
+ await listen({
736
+ at: async () => {
737
+ origin = (await sharedServer(file))?.origin ?? "";
738
+ return origin || null;
739
+ },
740
+ query: {
741
+ entry,
742
+ command,
743
+ ...f.type ? { type: f.type } : {}
744
+ },
745
+ onSnapshot: update,
746
+ onRefused: (reason) => {
747
+ console.error(`whiteboard: ${reason}. Add --page to wait on a page of your own.`);
748
+ process.exit(1);
878
749
  }
879
- };
880
- await sync();
881
- setInterval(sync, 2e3);
750
+ });
751
+ setInterval(() => {}, 1 << 30);
882
752
  }
883
753
  /** The commands the project's plugins add: `whiteboard <plugin> [sub]`. */
884
754
  async function projectCommands(proj) {