@brickkit/cli 0.9.1 → 0.11.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.
- package/README.md +1 -1
- package/dashboard-dist/CollapsiblePanel-BX79_W-b.js +2 -0
- package/dashboard-dist/CompositeRoot-xzybXjnx.js +1 -0
- package/dashboard-dist/PopoverPopup-CvlmC7EL.js +1 -0
- package/dashboard-dist/{api-reference-r893F8zi.js → api-reference-BcAYVYwJ.js} +8 -8
- package/dashboard-dist/app.css +1 -1
- package/dashboard-dist/app.js +13 -13
- package/dashboard-dist/core-BGO9JZ1r.js +1 -0
- package/dashboard-dist/docs-DHikPD2v.js +4 -0
- package/dashboard-dist/kit.js +1 -0
- package/dashboard-dist/popover-DG498XHH.js +1 -0
- package/dashboard-dist/react-dom-client.js +3 -0
- package/dashboard-dist/react-dom.js +3 -0
- package/dashboard-dist/react-jsx-runtime.js +1 -1
- package/dashboard-dist/react.js +3 -3
- package/dashboard-dist/theme.css +123 -0
- package/dist/bin.d.ts.map +1 -1
- package/dist/bin.js +440 -44
- package/dist/bin.js.map +1 -1
- package/dist/client-contract-file2.js +1 -1
- package/dist/client-contract2.js +34 -8
- package/dist/client-contract2.js.map +1 -1
- package/dist/config.js +252 -0
- package/dist/config.js.map +1 -0
- package/dist/dashboard-extension.d.ts +27 -0
- package/dist/dashboard-extension.d.ts.map +1 -0
- package/dist/dashboard.d.ts +3127 -0
- package/dist/dev-telemetry-store.js +1407 -952
- package/dist/dev-telemetry-store.js.map +1 -1
- package/dist/dev-worker.js +2 -2
- package/dist/index.d.ts +95 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -4
- package/dist/lint/eslint.d.ts +2 -0
- package/dist/lint/eslint.js +1 -0
- package/dist/lint/oxlint.d.ts +19 -0
- package/dist/lint/oxlint.d.ts.map +1 -0
- package/dist/lint/oxlint.js +19 -0
- package/dist/lint/oxlint.js.map +1 -0
- package/dist/plugin.d.ts +76 -0
- package/dist/plugin.d.ts.map +1 -0
- package/dist/plugin.js +66 -0
- package/dist/plugin.js.map +1 -0
- package/dist/presets.js +1070 -0
- package/dist/presets.js.map +1 -0
- package/dist/runner.js +112 -26
- package/dist/runner.js.map +1 -1
- package/dist/vite.d.ts +6 -3
- package/dist/vite.d.ts.map +1 -1
- package/dist/vite.js +9 -3
- package/dist/vite.js.map +1 -1
- package/dist/workflows2.js +1 -1
- package/inspector-dist/client/assets/app-CvoGgWcV.css +1 -0
- package/inspector-dist/client/assets/{manifest-4baa7c6f.js → manifest-139c9319.js} +1 -1
- package/inspector-dist/client/assets/root-BMHd_XHz.css +1 -0
- package/inspector-dist/server/assets/{app-C-ZVJtic.js → app-DUP5RybL.js} +11 -11
- package/inspector-dist/server/assets/{highlighted-body-B3W2YXNL-C8sq8F_D.js → highlighted-body-B3W2YXNL-D2g4hL2-.js} +1 -1
- package/inspector-dist/server/assets/{index-CYlmUaKx.js → index-D71yP6AL.js} +2 -2
- package/inspector-dist/server/assets/{index-CWJr34WJ.js → index-wGSWRns3.js} +1 -1
- package/inspector-dist/server/assets/{mermaid-3ZIDBTTL-Dv5rjfMN.js → mermaid-3ZIDBTTL-DsvuKoEV.js} +1 -1
- package/inspector-dist/server/assets/{token-DnLp3wBO.js → token-ClUZZwXl.js} +1 -1
- package/inspector-dist/server/assets/{token-util-Bpfp-B0w.js → token-util-DSxMZ6nI.js} +1 -1
- package/inspector-dist/server/assets/{wrapper-D5LIcZ_u.js → wrapper-B3vstpCO.js} +2 -2
- package/inspector-dist/server/assets/{zstd-browser-decoder-BM8GpVB8.js → zstd-browser-decoder-Dn82m2BU.js} +1 -1
- package/inspector-dist/server/index.js +1 -1
- package/package.json +30 -6
- package/dashboard-dist/CollapsiblePanel-a54TwMKb.js +0 -2
- package/dashboard-dist/PopoverPopup-BLzjTqsi.js +0 -1
- package/dashboard-dist/core-BWAwhSFf.js +0 -1
- package/dashboard-dist/docs-CkMy3Y5i.js +0 -4
- package/dashboard-dist/preload-helper-CjQDFR0M.js +0 -1
- package/dist/build.js +0 -88
- package/dist/build.js.map +0 -1
- package/inspector-dist/client/assets/app-CY7CQBRo.css +0 -1
- package/inspector-dist/client/assets/root-CfCr265_.css +0 -1
- /package/inspector-dist/client/assets/{root-Bmn5n3jF.js → root-cbi6iYwg.js} +0 -0
|
@@ -1,11 +1,12 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
1
2
|
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
2
|
-
import {
|
|
3
|
-
import { readFile, readdir, stat } from "node:fs/promises";
|
|
3
|
+
import { mkdir, readFile, readdir, stat, writeFile } from "node:fs/promises";
|
|
4
4
|
import { createServer } from "node:http";
|
|
5
5
|
import { basename, dirname, extname, join, posix, relative, resolve, sep } from "node:path";
|
|
6
6
|
import { Readable } from "node:stream";
|
|
7
7
|
import { pipeline } from "node:stream/promises";
|
|
8
8
|
import { createHash, randomBytes, timingSafeEqual } from "node:crypto";
|
|
9
|
+
import { existsSync, mkdirSync, statSync, watch, writeFileSync } from "node:fs";
|
|
9
10
|
//#region src/dev-inspector.ts
|
|
10
11
|
/** Where the dashboard serves the inspector. */
|
|
11
12
|
const INSPECTOR_PATH = "/workflows";
|
|
@@ -493,10 +494,18 @@ function parseHistoryQuery(params, now = Date.now()) {
|
|
|
493
494
|
/**
|
|
494
495
|
* The page the dashboard serves at `/`. The React app and its styles load from
|
|
495
496
|
* `/assets/`; the inline script applies the saved theme before first paint. The
|
|
496
|
-
* import map
|
|
497
|
-
* dashboard,
|
|
497
|
+
* import map hands the project's own modules (its docs components and its
|
|
498
|
+
* dashboard extension), bundled apart from the dashboard, the dashboard's copy
|
|
499
|
+
* of React and its UI kit, so there is one of each on the page.
|
|
500
|
+
*
|
|
501
|
+
* `extension` describes the project's extension: `revision` changes with every
|
|
502
|
+
* rebuild (the page reloads when it does), `js` and `css` say what it built.
|
|
503
|
+
* `theme` is whether `/assets/brick-theme.css` has token values to apply.
|
|
498
504
|
*/
|
|
499
|
-
|
|
505
|
+
function dashboardShell(options = {}) {
|
|
506
|
+
const { extension, theme } = options;
|
|
507
|
+
const version = extension ? `?v=${extension.revision}` : "";
|
|
508
|
+
return `<!doctype html>
|
|
500
509
|
<html lang="en">
|
|
501
510
|
<head>
|
|
502
511
|
<meta charset="utf-8">
|
|
@@ -513,8 +522,8 @@ const DEV_DASHBOARD_SHELL = `<!doctype html>
|
|
|
513
522
|
})();
|
|
514
523
|
<\/script>
|
|
515
524
|
<link rel="stylesheet" href="/assets/app.css">
|
|
516
|
-
<script type="importmap">
|
|
517
|
-
{"imports":{"react":"/assets/react.js","react/jsx-runtime":"/assets/react-jsx-runtime.js","react/jsx-dev-runtime":"/assets/react-jsx-runtime.js"}}
|
|
525
|
+
${extension?.css ? `<link rel="stylesheet" href="/assets/extension.css${version}">\n` : ""}${theme ? `<link rel="stylesheet" href="/assets/brick-theme.css" data-brick-theme>\n` : ""}${extension ? `<meta name="brick-extension" data-revision="${extension.revision}"${extension.js ? ` data-src="/assets/extension.js${version}"` : ""}>\n` : ""}<script type="importmap">
|
|
526
|
+
{"imports":{"react":"/assets/react.js","react/jsx-runtime":"/assets/react-jsx-runtime.js","react/jsx-dev-runtime":"/assets/react-jsx-runtime.js","react-dom":"/assets/react-dom.js","react-dom/client":"/assets/react-dom-client.js","@brickkit/cli/dashboard":"/assets/kit.js"}}
|
|
518
527
|
<\/script>
|
|
519
528
|
</head>
|
|
520
529
|
<body>
|
|
@@ -523,6 +532,7 @@ const DEV_DASHBOARD_SHELL = `<!doctype html>
|
|
|
523
532
|
</body>
|
|
524
533
|
</html>
|
|
525
534
|
`;
|
|
535
|
+
}
|
|
526
536
|
//#endregion
|
|
527
537
|
//#region src/dashboard-access.ts
|
|
528
538
|
/** How long the app's `authorize` may take before the request is refused. */
|
|
@@ -641,456 +651,657 @@ function createAccess(auth, onError = () => {}, timeoutMs = AUTHORIZE_TIMEOUT_MS
|
|
|
641
651
|
};
|
|
642
652
|
}
|
|
643
653
|
//#endregion
|
|
644
|
-
//#region src/dev-
|
|
645
|
-
const
|
|
646
|
-
const
|
|
647
|
-
const
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
"
|
|
651
|
-
"
|
|
652
|
-
"
|
|
653
|
-
"
|
|
654
|
-
"
|
|
655
|
-
"
|
|
656
|
-
"trailer",
|
|
657
|
-
"upgrade",
|
|
658
|
-
"content-length",
|
|
659
|
-
"accept-encoding",
|
|
660
|
-
"origin",
|
|
661
|
-
"referer",
|
|
662
|
-
"cookie"
|
|
663
|
-
]);
|
|
664
|
-
const NOT_RETURNED = /* @__PURE__ */ new Set([
|
|
665
|
-
"connection",
|
|
666
|
-
"keep-alive",
|
|
667
|
-
"transfer-encoding",
|
|
668
|
-
"content-length",
|
|
669
|
-
"content-encoding",
|
|
670
|
-
"set-cookie"
|
|
671
|
-
]);
|
|
672
|
-
/**
|
|
673
|
-
* What a production dashboard may show: the shell and its assets, the live
|
|
674
|
-
* state and its event stream, and stored history. Nothing that changes the app
|
|
675
|
-
* or reaches into it (restart, the API proxy, ingest) and nothing from the
|
|
676
|
-
* project's source (documentation, OpenAPI documents).
|
|
677
|
-
*/
|
|
678
|
-
const isMonitoringRoute = (method, pathname) => method === "GET" && (pathname === "/" || pathname.startsWith("/assets/") || pathname === "/api/state" || pathname === "/api/events" || pathname.startsWith("/api/history"));
|
|
679
|
-
const PRODUCTION_HEADERS = {
|
|
680
|
-
"x-content-type-options": "nosniff",
|
|
681
|
-
"x-frame-options": "DENY",
|
|
682
|
-
"referrer-policy": "no-referrer",
|
|
683
|
-
"cross-origin-opener-policy": "same-origin",
|
|
684
|
-
"cross-origin-resource-policy": "same-origin",
|
|
685
|
-
"cache-control": "no-store",
|
|
686
|
-
"content-security-policy": "default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; img-src 'self' data: blob:; font-src 'self' data:; connect-src 'self'; object-src 'none'; base-uri 'none'; form-action 'none'; frame-ancestors 'none'"
|
|
654
|
+
//#region src/dev-docs.ts
|
|
655
|
+
const PACKAGES = ["fumadocs-core", "@mdx-js/mdx"];
|
|
656
|
+
const PAGE_EXTENSIONS = /* @__PURE__ */ new Set([".md", ".mdx"]);
|
|
657
|
+
const IMAGE_TYPES = {
|
|
658
|
+
".png": "image/png",
|
|
659
|
+
".jpg": "image/jpeg",
|
|
660
|
+
".jpeg": "image/jpeg",
|
|
661
|
+
".gif": "image/gif",
|
|
662
|
+
".webp": "image/webp",
|
|
663
|
+
".avif": "image/avif",
|
|
664
|
+
".svg": "image/svg+xml",
|
|
665
|
+
".ico": "image/x-icon"
|
|
687
666
|
};
|
|
688
|
-
|
|
689
|
-
|
|
667
|
+
const REFRESH_DELAY_MS = 100;
|
|
668
|
+
async function loadFumadocs() {
|
|
669
|
+
try {
|
|
670
|
+
const [source, matter, plugins, search, mdx] = await Promise.all([
|
|
671
|
+
import("fumadocs-core/source"),
|
|
672
|
+
import("fumadocs-core/content/md/frontmatter"),
|
|
673
|
+
import("fumadocs-core/mdx-plugins"),
|
|
674
|
+
import("fumadocs-core/search/server"),
|
|
675
|
+
import("@mdx-js/mdx")
|
|
676
|
+
]);
|
|
677
|
+
return {
|
|
678
|
+
loader: source.loader,
|
|
679
|
+
frontmatter: matter.frontmatter,
|
|
680
|
+
plugins,
|
|
681
|
+
createSearchAPI: search.createSearchAPI,
|
|
682
|
+
compile: mdx.compile
|
|
683
|
+
};
|
|
684
|
+
} catch (error) {
|
|
685
|
+
if (error.code === "ERR_MODULE_NOT_FOUND" || /Cannot find (module|package)/.test(String(error?.message))) return [...PACKAGES];
|
|
686
|
+
throw error;
|
|
687
|
+
}
|
|
688
|
+
}
|
|
689
|
+
const humanize = (name) => {
|
|
690
|
+
const spaced = name.replace(/[-_]+/g, " ").trim();
|
|
691
|
+
return spaced.charAt(0).toUpperCase() + spaced.slice(1);
|
|
692
|
+
};
|
|
693
|
+
const H1 = /^#\s+(.+?)\s*#*\s*$/;
|
|
690
694
|
/**
|
|
691
|
-
*
|
|
692
|
-
*
|
|
693
|
-
*
|
|
694
|
-
* required and decides who gets in. Uses node:http, so it runs under Bun and
|
|
695
|
-
* under Node (Vite's dev server runs under Node).
|
|
695
|
+
* The first `# Heading` outside code fences: its text, and the content without
|
|
696
|
+
* it. The page shows its title above the content, so a heading that only
|
|
697
|
+
* repeats the title would appear twice.
|
|
696
698
|
*/
|
|
697
|
-
function
|
|
698
|
-
const
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
send(response, 403, "Forbidden");
|
|
715
|
-
return;
|
|
716
|
-
}
|
|
717
|
-
const url = new URL(request.url ?? "/", "http://127.0.0.1");
|
|
718
|
-
const { pathname } = url;
|
|
719
|
-
if (pathname === "/_health" && (request.method === "GET" || request.method === "HEAD")) {
|
|
720
|
-
await health(response);
|
|
721
|
-
return;
|
|
722
|
-
}
|
|
723
|
-
if (access) {
|
|
724
|
-
const decision = await access(request);
|
|
725
|
-
if (!decision.allowed) {
|
|
726
|
-
send(response, decision.status, decision.body, decision.headers);
|
|
727
|
-
return;
|
|
728
|
-
}
|
|
729
|
-
}
|
|
730
|
-
if (production && !isMonitoringRoute(request.method, pathname)) {
|
|
731
|
-
send(response, 404, "Not found");
|
|
732
|
-
return;
|
|
699
|
+
function firstHeading(content) {
|
|
700
|
+
const lines = content.split("\n");
|
|
701
|
+
let fenced = false;
|
|
702
|
+
let seenText = false;
|
|
703
|
+
for (const [index, line] of lines.entries()) if (/^\s*(```|~~~)/.test(line)) {
|
|
704
|
+
fenced = !fenced;
|
|
705
|
+
seenText = true;
|
|
706
|
+
} else if (!fenced) {
|
|
707
|
+
const match = H1.exec(line);
|
|
708
|
+
if (match) {
|
|
709
|
+
const rest = [...lines.slice(0, index), ...lines.slice(index + 1)];
|
|
710
|
+
if (rest[index]?.trim() === "") rest.splice(index, 1);
|
|
711
|
+
return {
|
|
712
|
+
text: match[1],
|
|
713
|
+
rest: rest.join("\n"),
|
|
714
|
+
leading: !seenText
|
|
715
|
+
};
|
|
733
716
|
}
|
|
734
|
-
|
|
717
|
+
if (line.trim() !== "") seenText = true;
|
|
735
718
|
}
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
719
|
+
}
|
|
720
|
+
async function scan(root, fumadocs, warn) {
|
|
721
|
+
const pages = [];
|
|
722
|
+
const metas = [];
|
|
723
|
+
async function visit(directory) {
|
|
724
|
+
for (const entry of await readdir(directory, { withFileTypes: true })) {
|
|
725
|
+
if (entry.name.startsWith(".") || entry.name === "node_modules") continue;
|
|
726
|
+
const absolute = join(directory, entry.name);
|
|
727
|
+
const path = relative(root, absolute).split(sep).join("/");
|
|
728
|
+
if (entry.isDirectory()) await visit(absolute);
|
|
729
|
+
else if (entry.name === "meta.json") try {
|
|
730
|
+
const data = JSON.parse(await readFile(absolute, "utf8"));
|
|
731
|
+
if (data && typeof data === "object") metas.push({
|
|
732
|
+
path,
|
|
733
|
+
data
|
|
734
|
+
});
|
|
735
|
+
} catch (error) {
|
|
736
|
+
warn(`[Brick-TS] Skipping docs ${path}: ${errorMessage(error)}`);
|
|
737
|
+
}
|
|
738
|
+
else if (PAGE_EXTENSIONS.has(extname(entry.name))) {
|
|
739
|
+
const text = await readFile(absolute, "utf8");
|
|
740
|
+
let data = {};
|
|
741
|
+
let content = text;
|
|
742
|
+
try {
|
|
743
|
+
const parsed = fumadocs.frontmatter(text);
|
|
744
|
+
data = parsed.data;
|
|
745
|
+
content = parsed.content;
|
|
746
|
+
} catch (error) {
|
|
747
|
+
warn(`[Brick-TS] Ignoring the frontmatter of docs ${path}: ${errorMessage(error)}`);
|
|
748
|
+
}
|
|
749
|
+
const name = basename(entry.name, extname(entry.name));
|
|
750
|
+
const heading = firstHeading(content);
|
|
751
|
+
const given = typeof data.title === "string" && data.title ? data.title : void 0;
|
|
752
|
+
const title = given ?? heading?.text ?? humanize(name === "index" ? basename(dirname(path)) || "Home" : name);
|
|
753
|
+
if (heading && (!given || heading.leading && heading.text === given)) content = heading.rest;
|
|
754
|
+
pages.push({
|
|
755
|
+
path,
|
|
756
|
+
absolute,
|
|
757
|
+
title,
|
|
758
|
+
description: typeof data.description === "string" ? data.description : void 0,
|
|
759
|
+
content,
|
|
760
|
+
data
|
|
761
|
+
});
|
|
762
|
+
}
|
|
746
763
|
}
|
|
747
|
-
send(response, up ? 200 : 503, JSON.stringify({
|
|
748
|
-
status: up ? "ok" : "degraded",
|
|
749
|
-
...probe ? { app: up ? "up" : "down" } : {}
|
|
750
|
-
}), {
|
|
751
|
-
"content-type": "application/json",
|
|
752
|
-
"cache-control": "no-store"
|
|
753
|
-
});
|
|
754
764
|
}
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
765
|
+
await visit(root);
|
|
766
|
+
return {
|
|
767
|
+
pages,
|
|
768
|
+
metas
|
|
769
|
+
};
|
|
770
|
+
}
|
|
771
|
+
function errorMessage(error) {
|
|
772
|
+
return error instanceof Error ? error.message : String(error);
|
|
773
|
+
}
|
|
774
|
+
function walk(node, visit) {
|
|
775
|
+
visit(node);
|
|
776
|
+
for (const child of node.children ?? []) walk(child, visit);
|
|
777
|
+
}
|
|
778
|
+
/** Ways a link inside a page can point at another page or a local image. */
|
|
779
|
+
function resolveInside(from, href) {
|
|
780
|
+
if (/^[a-z][a-z\d+.-]*:/i.test(href) || href.startsWith("#")) return void 0;
|
|
781
|
+
if (href.startsWith("/")) return void 0;
|
|
782
|
+
const [target] = href.split(/[?#]/);
|
|
783
|
+
if (!target) return void 0;
|
|
784
|
+
const resolved = posix.normalize(posix.join(posix.dirname(from), target));
|
|
785
|
+
return resolved.startsWith("../") ? void 0 : resolved;
|
|
786
|
+
}
|
|
787
|
+
/**
|
|
788
|
+
* The page tree and compiled pages of one docs folder. It watches the folder,
|
|
789
|
+
* so edits show in the dashboard as they are saved.
|
|
790
|
+
*/
|
|
791
|
+
var DevDocs = class {
|
|
792
|
+
#root;
|
|
793
|
+
#documentation;
|
|
794
|
+
/** Relative paths in `documentation` start here: the app entry's folder. */
|
|
795
|
+
#base;
|
|
796
|
+
#fumadocs;
|
|
797
|
+
#warn;
|
|
798
|
+
#listeners = /* @__PURE__ */ new Set();
|
|
799
|
+
#watchers = [];
|
|
800
|
+
#loader;
|
|
801
|
+
#pages = /* @__PURE__ */ new Map();
|
|
802
|
+
#byPath = /* @__PURE__ */ new Map();
|
|
803
|
+
#compiled = /* @__PURE__ */ new Map();
|
|
804
|
+
#search;
|
|
805
|
+
#timer;
|
|
806
|
+
#refreshing = Promise.resolve();
|
|
807
|
+
#closed = false;
|
|
808
|
+
#title = "Docs";
|
|
809
|
+
revision = 0;
|
|
810
|
+
constructor(base, documentation, fumadocs, warn) {
|
|
811
|
+
this.#base = base;
|
|
812
|
+
this.#documentation = documentation;
|
|
813
|
+
this.#root = resolve(base, documentation.dir);
|
|
814
|
+
this.#fumadocs = fumadocs;
|
|
815
|
+
this.#warn = warn;
|
|
816
|
+
}
|
|
817
|
+
/** Read the folder again and tell listeners. */
|
|
818
|
+
async refresh() {
|
|
819
|
+
this.#refreshing = this.#refreshing.then(() => this.#scan());
|
|
820
|
+
await this.#refreshing;
|
|
821
|
+
}
|
|
822
|
+
async #scan() {
|
|
823
|
+
const { pages, metas } = await scan(this.#root, this.#fumadocs, this.#warn);
|
|
824
|
+
if (this.#closed) return;
|
|
825
|
+
const source = this.#fumadocs.loader({
|
|
826
|
+
baseUrl: "/",
|
|
827
|
+
source: { files: [...pages.map((page) => ({
|
|
828
|
+
type: "page",
|
|
829
|
+
path: page.path,
|
|
830
|
+
data: {
|
|
831
|
+
...page.data,
|
|
832
|
+
title: page.title,
|
|
833
|
+
description: page.description
|
|
834
|
+
}
|
|
835
|
+
})), ...metas.map((meta) => ({
|
|
836
|
+
type: "meta",
|
|
837
|
+
path: meta.path,
|
|
838
|
+
data: meta.data
|
|
839
|
+
}))] }
|
|
840
|
+
});
|
|
841
|
+
this.#loader = source;
|
|
842
|
+
this.#pages = /* @__PURE__ */ new Map();
|
|
843
|
+
this.#byPath = /* @__PURE__ */ new Map();
|
|
844
|
+
for (const page of this.#loader.getPages()) {
|
|
845
|
+
const slug = page.slugs.join("/");
|
|
846
|
+
const file = pages.find((candidate) => candidate.path === page.path);
|
|
847
|
+
if (!file) continue;
|
|
848
|
+
this.#pages.set(slug, file);
|
|
849
|
+
this.#byPath.set(file.path, slug);
|
|
804
850
|
}
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
851
|
+
this.#compiled.clear();
|
|
852
|
+
this.#search = void 0;
|
|
853
|
+
const tree = this.#loader.getPageTree();
|
|
854
|
+
this.#title = typeof tree.name === "string" ? tree.name : "Docs";
|
|
855
|
+
this.revision++;
|
|
856
|
+
for (const listener of this.#listeners) listener();
|
|
857
|
+
}
|
|
858
|
+
/** Start watching the folder, and the project's components module. */
|
|
859
|
+
watch() {
|
|
860
|
+
const changed = () => {
|
|
861
|
+
clearTimeout(this.#timer);
|
|
862
|
+
this.#timer = setTimeout(() => {
|
|
863
|
+
this.refresh().catch((error) => this.#warn(`[Brick-TS] Docs: ${errorMessage(error)}`));
|
|
864
|
+
}, REFRESH_DELAY_MS);
|
|
865
|
+
};
|
|
866
|
+
const folders = [this.#root];
|
|
867
|
+
if (this.#documentation.components) folders.push(dirname(this.#componentsPath()));
|
|
868
|
+
for (const [index, folder] of folders.entries()) try {
|
|
869
|
+
this.#watchers.push(watch(folder, { recursive: index === 0 }, () => changed()));
|
|
870
|
+
} catch {
|
|
871
|
+
try {
|
|
872
|
+
this.#watchers.push(watch(folder, () => changed()));
|
|
873
|
+
} catch {}
|
|
813
874
|
}
|
|
814
|
-
send(response, 404, "Not found");
|
|
815
875
|
}
|
|
816
|
-
|
|
876
|
+
onChange(listener) {
|
|
877
|
+
this.#listeners.add(listener);
|
|
878
|
+
return () => {
|
|
879
|
+
this.#listeners.delete(listener);
|
|
880
|
+
};
|
|
881
|
+
}
|
|
882
|
+
snapshot() {
|
|
817
883
|
return {
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
884
|
+
revision: this.revision,
|
|
885
|
+
status: "ready",
|
|
886
|
+
title: this.#title,
|
|
887
|
+
components: Boolean(this.#documentation.components)
|
|
888
|
+
};
|
|
889
|
+
}
|
|
890
|
+
info() {
|
|
891
|
+
return {
|
|
892
|
+
tree: simplify(this.#loader?.getPageTree()),
|
|
893
|
+
components: Boolean(this.#documentation.components),
|
|
894
|
+
revision: this.revision
|
|
895
|
+
};
|
|
896
|
+
}
|
|
897
|
+
/** Compile one page; undefined when there is no such page. */
|
|
898
|
+
async page(slugs) {
|
|
899
|
+
const key = slugs.join("/");
|
|
900
|
+
const file = this.#pages.get(key);
|
|
901
|
+
if (!file) return void 0;
|
|
902
|
+
const cached = this.#compiled.get(key);
|
|
903
|
+
if (cached) return cached;
|
|
904
|
+
const page = await this.#compile(file, slugs);
|
|
905
|
+
if (this.#pages.get(key) === file) this.#compiled.set(key, page);
|
|
906
|
+
return page;
|
|
907
|
+
}
|
|
908
|
+
async #compile(file, slugs) {
|
|
909
|
+
const { plugins, compile } = this.#fumadocs;
|
|
910
|
+
const base = {
|
|
911
|
+
title: file.title,
|
|
912
|
+
description: file.description,
|
|
913
|
+
path: file.path,
|
|
914
|
+
slugs
|
|
822
915
|
};
|
|
916
|
+
try {
|
|
917
|
+
const result = await compile({
|
|
918
|
+
path: file.absolute,
|
|
919
|
+
value: file.content
|
|
920
|
+
}, {
|
|
921
|
+
outputFormat: "function-body",
|
|
922
|
+
development: false,
|
|
923
|
+
remarkPlugins: [
|
|
924
|
+
plugins.remarkGfm,
|
|
925
|
+
plugins.remarkHeading,
|
|
926
|
+
plugins.remarkCodeTab,
|
|
927
|
+
plugins.remarkNpm,
|
|
928
|
+
plugins.remarkAdmonition,
|
|
929
|
+
() => (tree) => this.#rewriteLinks(tree, file.path)
|
|
930
|
+
],
|
|
931
|
+
rehypePlugins: [plugins.rehypeCode]
|
|
932
|
+
});
|
|
933
|
+
const toc = result.data.toc ?? [];
|
|
934
|
+
return {
|
|
935
|
+
...base,
|
|
936
|
+
toc: toc.map((item) => ({
|
|
937
|
+
title: plainText(item.title),
|
|
938
|
+
url: item.url,
|
|
939
|
+
depth: item.depth
|
|
940
|
+
})),
|
|
941
|
+
code: String(result)
|
|
942
|
+
};
|
|
943
|
+
} catch (error) {
|
|
944
|
+
return {
|
|
945
|
+
...base,
|
|
946
|
+
toc: [],
|
|
947
|
+
code: "",
|
|
948
|
+
error: errorMessage(error)
|
|
949
|
+
};
|
|
950
|
+
}
|
|
823
951
|
}
|
|
824
952
|
/**
|
|
825
|
-
*
|
|
826
|
-
* the
|
|
827
|
-
* the
|
|
953
|
+
* Links to other pages become dashboard routes, and local images are served
|
|
954
|
+
* from the docs folder, so a folder reads the same here as on a docs site.
|
|
955
|
+
* This covers markdown links and the `href` of components such as `<Card>`.
|
|
828
956
|
*/
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
}
|
|
845
|
-
});
|
|
846
|
-
request.on("end", () => {
|
|
847
|
-
if (rejected) return;
|
|
848
|
-
try {
|
|
849
|
-
const payload = JSON.parse(body);
|
|
850
|
-
if (Array.isArray(payload.records)) options.store.ingest(payload.records, options.store.latestGeneration);
|
|
851
|
-
if (typeof payload.worker === "string") options.store.startGeneration(payload.worker);
|
|
852
|
-
send(response, 204, "");
|
|
853
|
-
} catch {
|
|
854
|
-
send(response, 400, "Bad request");
|
|
957
|
+
#rewriteLinks(tree, from) {
|
|
958
|
+
const file = (resolved) => `/api/docs/files/${resolved.split("/").map(encodeURIComponent).join("/")}`;
|
|
959
|
+
walk(tree, (node) => {
|
|
960
|
+
if (node.type === "link" && node.url !== void 0) node.url = this.#route(from, node.url) ?? node.url;
|
|
961
|
+
else if (node.type === "image" && node.url !== void 0) {
|
|
962
|
+
const resolved = resolveInside(from, node.url);
|
|
963
|
+
if (resolved !== void 0) node.url = file(resolved);
|
|
964
|
+
} else if (node.type === "mdxJsxFlowElement" || node.type === "mdxJsxTextElement") for (const attribute of node.attributes ?? []) {
|
|
965
|
+
if (attribute.type !== "mdxJsxAttribute") continue;
|
|
966
|
+
if (typeof attribute.value !== "string") continue;
|
|
967
|
+
if (attribute.name === "href") attribute.value = this.#route(from, attribute.value) ?? attribute.value;
|
|
968
|
+
else if (attribute.name === "src" && node.name === "img") {
|
|
969
|
+
const resolved = resolveInside(from, attribute.value);
|
|
970
|
+
if (resolved !== void 0) attribute.value = file(resolved);
|
|
971
|
+
}
|
|
855
972
|
}
|
|
856
973
|
});
|
|
857
974
|
}
|
|
858
|
-
/** The Docs tab's data: tree, compiled pages, search, images, components. */
|
|
859
|
-
async function docsRoute(response, pathname, params) {
|
|
860
|
-
const docs = options.docs?.();
|
|
861
|
-
if (!docs) {
|
|
862
|
-
send(response, 404, "The app has no documentation.");
|
|
863
|
-
return;
|
|
864
|
-
}
|
|
865
|
-
const json = (body) => send(response, 200, JSON.stringify(body), {
|
|
866
|
-
"content-type": "application/json",
|
|
867
|
-
"cache-control": "no-store"
|
|
868
|
-
});
|
|
869
|
-
try {
|
|
870
|
-
if (pathname === "/api/docs") return json(docs.info());
|
|
871
|
-
if (pathname === "/api/docs/search") return json(await docs.search(params.get("q") ?? ""));
|
|
872
|
-
if (pathname === "/api/docs/components.js") {
|
|
873
|
-
const code = await docs.components();
|
|
874
|
-
if (code === void 0) return send(response, 404, "No components.");
|
|
875
|
-
return send(response, 200, code, {
|
|
876
|
-
"content-type": "text/javascript; charset=utf-8",
|
|
877
|
-
"cache-control": "no-store"
|
|
878
|
-
});
|
|
879
|
-
}
|
|
880
|
-
const pagePrefix = "/api/docs/page";
|
|
881
|
-
if (pathname === pagePrefix || pathname.startsWith(`${pagePrefix}/`)) {
|
|
882
|
-
const slugs = pathname.slice(14).split("/").filter(Boolean).map(decodeURIComponent);
|
|
883
|
-
const page = await docs.page(slugs);
|
|
884
|
-
return page ? json(page) : send(response, 404, "No such page.");
|
|
885
|
-
}
|
|
886
|
-
if (pathname.startsWith("/api/docs/files/")) {
|
|
887
|
-
const file = await docs.file(pathname.slice(16).split("/").map(decodeURIComponent).join("/"));
|
|
888
|
-
if (!file) return send(response, 404, "No such file.");
|
|
889
|
-
return send(response, 200, file.body, {
|
|
890
|
-
"content-type": file.type,
|
|
891
|
-
"cache-control": "no-store",
|
|
892
|
-
"content-security-policy": "sandbox"
|
|
893
|
-
});
|
|
894
|
-
}
|
|
895
|
-
send(response, 404, "Not found");
|
|
896
|
-
} catch (error) {
|
|
897
|
-
send(response, error instanceof URIError ? 400 : 500, error instanceof Error ? error.message : String(error));
|
|
898
|
-
}
|
|
899
|
-
}
|
|
900
|
-
/**
|
|
901
|
-
* Send a request on to the app for the APIs view, which cannot call the app
|
|
902
|
-
* directly: it is another origin. Only the dashboard's own page may use this,
|
|
903
|
-
* and only to reach the app being shown. The address to call is `?url=`; a
|
|
904
|
-
* `?cookie=` is the cookie parameters the reader typed into Try it, and the
|
|
905
|
-
* only cookie the app is sent (the dashboard's own are never forwarded).
|
|
906
|
-
*/
|
|
907
975
|
/**
|
|
908
|
-
*
|
|
909
|
-
*
|
|
910
|
-
*
|
|
911
|
-
*
|
|
912
|
-
* hooks, and a page on another site could otherwise post to it.
|
|
976
|
+
* The dashboard route for a link to a page of these docs, or undefined for
|
|
977
|
+
* anything else (other sites, in-page anchors, files). A link to a page that
|
|
978
|
+
* does not exist still becomes a route, so it says "No such page" here
|
|
979
|
+
* instead of leaving the dashboard.
|
|
913
980
|
*/
|
|
914
|
-
|
|
915
|
-
const
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
if (!bodyless && (site !== void 0 && site !== "same-origin" || origin !== void 0 && origin !== `http://${request.headers.host}`)) {
|
|
924
|
-
send(response, 403, "Forbidden");
|
|
925
|
-
return;
|
|
926
|
-
}
|
|
927
|
-
const headers = new Headers();
|
|
928
|
-
for (const [name, value] of Object.entries(request.headers)) {
|
|
929
|
-
if (value === void 0 || NOT_FORWARDED.has(name)) continue;
|
|
930
|
-
if (name.startsWith("sec-")) continue;
|
|
931
|
-
headers.set(name, Array.isArray(value) ? value.join(", ") : value);
|
|
932
|
-
}
|
|
933
|
-
headers.set(INSPECTOR_TOKEN_HEADER, target.token);
|
|
934
|
-
const abort = new AbortController();
|
|
935
|
-
response.on("close", () => abort.abort());
|
|
936
|
-
try {
|
|
937
|
-
const upstream = await fetch(`http://127.0.0.1:${target.port}${url.pathname}${url.search}`, {
|
|
938
|
-
method: request.method,
|
|
939
|
-
headers,
|
|
940
|
-
redirect: "manual",
|
|
941
|
-
signal: abort.signal,
|
|
942
|
-
...bodyless ? {} : {
|
|
943
|
-
body: Readable.toWeb(request),
|
|
944
|
-
duplex: "half"
|
|
945
|
-
}
|
|
946
|
-
});
|
|
947
|
-
const returned = {};
|
|
948
|
-
upstream.headers.forEach((value, name) => {
|
|
949
|
-
if (!NOT_RETURNED.has(name)) returned[name] = value;
|
|
950
|
-
});
|
|
951
|
-
response.writeHead(upstream.status, returned);
|
|
952
|
-
if (!upstream.body || request.method === "HEAD") {
|
|
953
|
-
response.end();
|
|
954
|
-
return;
|
|
955
|
-
}
|
|
956
|
-
await pipeline(Readable.fromWeb(upstream.body), response);
|
|
957
|
-
} catch {
|
|
958
|
-
if (response.headersSent) response.destroy();
|
|
959
|
-
else send(response, 502, "Could not reach the workflow inspector.");
|
|
981
|
+
#route(from, href) {
|
|
982
|
+
const hashAt = href.indexOf("#");
|
|
983
|
+
const hash = hashAt >= 0 ? href.slice(hashAt) : "";
|
|
984
|
+
const target = (hashAt >= 0 ? href.slice(0, hashAt) : href).split("?")[0] ?? "";
|
|
985
|
+
const route = (slug) => `#/docs${slug ? `/${slug}` : ""}${hash}`;
|
|
986
|
+
if (/^[a-z][a-z\d+.-]*:/i.test(href) || href.startsWith("#") || !target) return void 0;
|
|
987
|
+
if (target.startsWith("/")) {
|
|
988
|
+
const slug = target.replace(/^\/+|\/+$/g, "");
|
|
989
|
+
return this.#pages.has(slug) ? route(slug) : void 0;
|
|
960
990
|
}
|
|
991
|
+
const resolved = resolveInside(from, target);
|
|
992
|
+
if (resolved === void 0) return void 0;
|
|
993
|
+
const known = this.#byPath.get(resolved);
|
|
994
|
+
if (known !== void 0) return route(known);
|
|
995
|
+
if (!PAGE_EXTENSIONS.has(extname(resolved))) return void 0;
|
|
996
|
+
return route(resolved.slice(0, -extname(resolved).length).replace(/(^|\/)index$/, ""));
|
|
961
997
|
}
|
|
962
|
-
async
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
}
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
if (name.startsWith("sec-")) continue;
|
|
987
|
-
headers.set(name, Array.isArray(value) ? value.join(", ") : value);
|
|
988
|
-
}
|
|
989
|
-
const cookie = url.searchParams.get("cookie");
|
|
990
|
-
if (cookie) headers.set("cookie", cookie);
|
|
991
|
-
const bodyless = request.method === "GET" || request.method === "HEAD";
|
|
998
|
+
async search(query) {
|
|
999
|
+
if (!query.trim()) return [];
|
|
1000
|
+
this.#search ??= this.#buildSearch();
|
|
1001
|
+
return (await this.#search).search(query);
|
|
1002
|
+
}
|
|
1003
|
+
async #buildSearch() {
|
|
1004
|
+
const { plugins, createSearchAPI } = this.#fumadocs;
|
|
1005
|
+
const api = createSearchAPI("advanced", { indexes: [...this.#pages.entries()].map(([slug, file]) => ({
|
|
1006
|
+
id: file.path,
|
|
1007
|
+
title: file.title,
|
|
1008
|
+
description: file.description,
|
|
1009
|
+
url: `/${slug}`,
|
|
1010
|
+
structuredData: plugins.structure(file.content)
|
|
1011
|
+
})) });
|
|
1012
|
+
return { async search(query) {
|
|
1013
|
+
return await api.search(query);
|
|
1014
|
+
} };
|
|
1015
|
+
}
|
|
1016
|
+
/** A file from the docs folder that a page can show, such as an image. */
|
|
1017
|
+
async file(relativePath) {
|
|
1018
|
+
const type = IMAGE_TYPES[extname(relativePath).toLowerCase()];
|
|
1019
|
+
if (!type) return void 0;
|
|
1020
|
+
const absolute = resolve(this.#root, relativePath);
|
|
1021
|
+
if (absolute !== this.#root && !absolute.startsWith(this.#root + sep)) return void 0;
|
|
992
1022
|
try {
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
});
|
|
999
|
-
const returned = {};
|
|
1000
|
-
upstream.headers.forEach((value, name) => {
|
|
1001
|
-
if (!NOT_RETURNED.has(name)) returned[name] = value;
|
|
1002
|
-
});
|
|
1003
|
-
send(response, upstream.status, new Uint8Array(await upstream.arrayBuffer()), returned);
|
|
1023
|
+
if (!(await stat(absolute)).isFile()) return void 0;
|
|
1024
|
+
return {
|
|
1025
|
+
body: new Uint8Array(await readFile(absolute)),
|
|
1026
|
+
type
|
|
1027
|
+
};
|
|
1004
1028
|
} catch {
|
|
1005
|
-
|
|
1029
|
+
return;
|
|
1006
1030
|
}
|
|
1007
1031
|
}
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
"cache-control": "no-cache",
|
|
1012
|
-
connection: "keep-alive",
|
|
1013
|
-
"x-accel-buffering": "no"
|
|
1014
|
-
});
|
|
1015
|
-
const write = (chunk) => {
|
|
1016
|
-
if (response.writableEnded || response.destroyed) return;
|
|
1017
|
-
response.write(chunk);
|
|
1018
|
-
};
|
|
1019
|
-
write(`event: snapshot\ndata: ${JSON.stringify(snapshot())}\n\n`);
|
|
1020
|
-
const unsubscribe = options.store.subscribe((event) => write(`event: ${event.type}\ndata: ${JSON.stringify(event)}\n\n`));
|
|
1021
|
-
const keepalive = setInterval(() => write(": keepalive\n\n"), KEEPALIVE_MS);
|
|
1022
|
-
request.on("close", () => {
|
|
1023
|
-
clearInterval(keepalive);
|
|
1024
|
-
unsubscribe();
|
|
1025
|
-
});
|
|
1032
|
+
#componentsPath() {
|
|
1033
|
+
const { components } = this.#documentation;
|
|
1034
|
+
return components ? resolve(this.#base, components) : void 0;
|
|
1026
1035
|
}
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1036
|
+
/** The project's components as one browser module; undefined when it has none. */
|
|
1037
|
+
async components() {
|
|
1038
|
+
const entry = this.#componentsPath();
|
|
1039
|
+
return entry ? bundleComponents(entry) : void 0;
|
|
1040
|
+
}
|
|
1041
|
+
close() {
|
|
1042
|
+
this.#closed = true;
|
|
1043
|
+
clearTimeout(this.#timer);
|
|
1044
|
+
for (const watcher of this.#watchers) watcher.close();
|
|
1045
|
+
this.#watchers.length = 0;
|
|
1046
|
+
this.#listeners.clear();
|
|
1047
|
+
}
|
|
1048
|
+
};
|
|
1049
|
+
const SHARED = [
|
|
1050
|
+
"react",
|
|
1051
|
+
"react/jsx-runtime",
|
|
1052
|
+
"react/jsx-dev-runtime",
|
|
1053
|
+
"react-dom",
|
|
1054
|
+
"react-dom/client",
|
|
1055
|
+
"@brickkit/cli/dashboard"
|
|
1056
|
+
];
|
|
1057
|
+
/** Bundle a module of React components for the dashboard to import. */
|
|
1058
|
+
async function bundleComponents(entry) {
|
|
1059
|
+
if (typeof Bun !== "undefined") {
|
|
1060
|
+
const result = await Bun.build({
|
|
1061
|
+
entrypoints: [entry],
|
|
1062
|
+
target: "browser",
|
|
1063
|
+
format: "esm",
|
|
1064
|
+
external: SHARED,
|
|
1065
|
+
minify: false,
|
|
1066
|
+
throw: false
|
|
1046
1067
|
});
|
|
1068
|
+
if (!result.success) throw new Error(result.logs.map((log) => log.message).join("\n"));
|
|
1069
|
+
return (result.outputs.find((item) => item.kind === "entry-point") ?? result.outputs[0]).text();
|
|
1070
|
+
}
|
|
1071
|
+
const { build } = await import("vite");
|
|
1072
|
+
const output = await build({
|
|
1073
|
+
configFile: false,
|
|
1074
|
+
logLevel: "silent",
|
|
1075
|
+
root: dirname(entry),
|
|
1076
|
+
oxc: { jsx: { runtime: "automatic" } },
|
|
1077
|
+
esbuild: { jsx: "automatic" },
|
|
1078
|
+
define: { "process.env.NODE_ENV": "\"development\"" },
|
|
1079
|
+
build: {
|
|
1080
|
+
write: false,
|
|
1081
|
+
minify: false,
|
|
1082
|
+
lib: {
|
|
1083
|
+
entry,
|
|
1084
|
+
formats: ["es"],
|
|
1085
|
+
fileName: "components"
|
|
1086
|
+
},
|
|
1087
|
+
rollupOptions: { external: SHARED }
|
|
1088
|
+
}
|
|
1047
1089
|
});
|
|
1090
|
+
const bundles = Array.isArray(output) ? output : [output];
|
|
1091
|
+
for (const bundle of bundles) {
|
|
1092
|
+
const chunk = ("output" in bundle ? bundle.output : []).find((item) => item.type === "chunk" && item.isEntry);
|
|
1093
|
+
if (chunk && "code" in chunk) return chunk.code;
|
|
1094
|
+
}
|
|
1095
|
+
throw new Error("Brick: the docs components module produced no output.");
|
|
1048
1096
|
}
|
|
1049
|
-
/**
|
|
1050
|
-
function
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
return;
|
|
1060
|
-
}
|
|
1061
|
-
chunks.push(chunk);
|
|
1062
|
-
});
|
|
1063
|
-
request.on("end", () => resolve(new Uint8Array(Buffer.concat(chunks))));
|
|
1064
|
-
request.on("error", () => resolve(void 0));
|
|
1065
|
-
});
|
|
1097
|
+
/** Flatten what MDX gives for a heading to text. */
|
|
1098
|
+
function plainText(value) {
|
|
1099
|
+
if (typeof value === "string") return value;
|
|
1100
|
+
if (Array.isArray(value)) return value.map(plainText).join("");
|
|
1101
|
+
if (value && typeof value === "object") {
|
|
1102
|
+
const node = value;
|
|
1103
|
+
if (typeof node.value === "string") return node.value;
|
|
1104
|
+
return plainText(node.props?.children ?? node.children);
|
|
1105
|
+
}
|
|
1106
|
+
return "";
|
|
1066
1107
|
}
|
|
1067
|
-
|
|
1068
|
-
|
|
1108
|
+
/** The page tree as plain data for the dashboard; icons and links are left out. */
|
|
1109
|
+
function simplify(tree) {
|
|
1110
|
+
const text = (value) => typeof value === "string" ? value : plainText(value);
|
|
1111
|
+
const convert = (node) => {
|
|
1112
|
+
if (node.type === "page" && node.url !== void 0) return {
|
|
1113
|
+
type: "page",
|
|
1114
|
+
name: text(node.name),
|
|
1115
|
+
url: node.url,
|
|
1116
|
+
...typeof node.description === "string" ? { description: node.description } : {}
|
|
1117
|
+
};
|
|
1118
|
+
if (node.type === "separator") return {
|
|
1119
|
+
type: "separator",
|
|
1120
|
+
...node.name ? { name: text(node.name) } : {}
|
|
1121
|
+
};
|
|
1122
|
+
if (node.type === "folder") return {
|
|
1123
|
+
type: "folder",
|
|
1124
|
+
name: text(node.name),
|
|
1125
|
+
...node.index?.url !== void 0 ? { index: {
|
|
1126
|
+
name: text(node.index.name),
|
|
1127
|
+
url: node.index.url
|
|
1128
|
+
} } : {},
|
|
1129
|
+
children: (node.children ?? []).flatMap((child) => convert(child) ?? []),
|
|
1130
|
+
...node.defaultOpen ? { defaultOpen: true } : {}
|
|
1131
|
+
};
|
|
1132
|
+
};
|
|
1133
|
+
return {
|
|
1134
|
+
name: text(tree?.name) || "Docs",
|
|
1135
|
+
children: (tree?.children ?? []).flatMap((child) => convert(child) ?? [])
|
|
1136
|
+
};
|
|
1137
|
+
}
|
|
1138
|
+
/** Open a docs folder; says what is missing when it cannot be shown. */
|
|
1139
|
+
async function openDocs(options) {
|
|
1140
|
+
const fumadocs = await loadFumadocs();
|
|
1141
|
+
if (Array.isArray(fumadocs)) return {
|
|
1142
|
+
status: "missing",
|
|
1143
|
+
packages: fumadocs
|
|
1144
|
+
};
|
|
1145
|
+
const docs = new DevDocs(options.base, options.documentation, fumadocs, options.warn);
|
|
1069
1146
|
try {
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
"
|
|
1077
|
-
|
|
1078
|
-
|
|
1147
|
+
await docs.refresh();
|
|
1148
|
+
} catch (error) {
|
|
1149
|
+
docs.close();
|
|
1150
|
+
const root = resolve(options.base, options.documentation.dir);
|
|
1151
|
+
return {
|
|
1152
|
+
status: "error",
|
|
1153
|
+
message: error.code === "ENOENT" ? `The docs folder ${root} does not exist.` : errorMessage(error)
|
|
1154
|
+
};
|
|
1155
|
+
}
|
|
1156
|
+
docs.watch();
|
|
1157
|
+
return {
|
|
1158
|
+
status: "ready",
|
|
1159
|
+
docs
|
|
1160
|
+
};
|
|
1161
|
+
}
|
|
1162
|
+
/**
|
|
1163
|
+
* The docs of whichever app is running: opened when the app says where they
|
|
1164
|
+
* are, replaced when that changes, and closed when it stops asking for them.
|
|
1165
|
+
*/
|
|
1166
|
+
var DocsHost = class {
|
|
1167
|
+
#store;
|
|
1168
|
+
#warn;
|
|
1169
|
+
#docs;
|
|
1170
|
+
#stopListening;
|
|
1171
|
+
#key;
|
|
1172
|
+
#pending = Promise.resolve();
|
|
1173
|
+
constructor(store, warn) {
|
|
1174
|
+
this.#store = store;
|
|
1175
|
+
this.#warn = warn;
|
|
1176
|
+
}
|
|
1177
|
+
get current() {
|
|
1178
|
+
return this.#docs;
|
|
1179
|
+
}
|
|
1180
|
+
/**
|
|
1181
|
+
* Follow the app's `documentation`. Relative paths in it start at `base`, the
|
|
1182
|
+
* folder of the app entry, as they read in the file that sets them.
|
|
1183
|
+
*/
|
|
1184
|
+
configure(base, documentation) {
|
|
1185
|
+
this.#pending = this.#pending.then(() => this.#configure(base, documentation));
|
|
1186
|
+
return this.#pending;
|
|
1187
|
+
}
|
|
1188
|
+
async #configure(base, documentation) {
|
|
1189
|
+
const key = JSON.stringify([base, documentation ?? null]);
|
|
1190
|
+
if (key === this.#key) return;
|
|
1191
|
+
this.#key = key;
|
|
1192
|
+
this.#release();
|
|
1193
|
+
if (!documentation) {
|
|
1194
|
+
this.#store.setDocs(null);
|
|
1195
|
+
return;
|
|
1196
|
+
}
|
|
1197
|
+
const opened = await openDocs({
|
|
1198
|
+
base,
|
|
1199
|
+
documentation,
|
|
1200
|
+
warn: this.#warn
|
|
1079
1201
|
});
|
|
1080
|
-
if (
|
|
1081
|
-
const
|
|
1082
|
-
|
|
1202
|
+
if (opened.status !== "ready") {
|
|
1203
|
+
const message = opened.status === "missing" ? `The docs need ${opened.packages.join(" and ")}. Install with: bun add -d ${opened.packages.join(" ")}` : opened.message;
|
|
1204
|
+
this.#warn(`[Brick-TS] Docs are off. ${message}`);
|
|
1205
|
+
this.#store.setDocs({
|
|
1206
|
+
revision: 1,
|
|
1207
|
+
status: opened.status,
|
|
1208
|
+
title: "Docs",
|
|
1209
|
+
components: false,
|
|
1210
|
+
message,
|
|
1211
|
+
...opened.status === "missing" ? { packages: opened.packages } : {}
|
|
1212
|
+
});
|
|
1213
|
+
return;
|
|
1083
1214
|
}
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1215
|
+
this.#docs = opened.docs;
|
|
1216
|
+
this.#stopListening = opened.docs.onChange(() => this.#store.setDocs(opened.docs.snapshot()));
|
|
1217
|
+
this.#store.setDocs(opened.docs.snapshot());
|
|
1218
|
+
}
|
|
1219
|
+
#release() {
|
|
1220
|
+
this.#stopListening?.();
|
|
1221
|
+
this.#stopListening = void 0;
|
|
1222
|
+
this.#docs?.close();
|
|
1223
|
+
this.#docs = void 0;
|
|
1224
|
+
}
|
|
1225
|
+
close() {
|
|
1226
|
+
this.#key = void 0;
|
|
1227
|
+
this.#release();
|
|
1087
1228
|
}
|
|
1088
|
-
}
|
|
1089
|
-
const ASSET_TYPES = {
|
|
1090
|
-
".js": "text/javascript; charset=utf-8",
|
|
1091
|
-
".css": "text/css; charset=utf-8",
|
|
1092
|
-
".woff2": "font/woff2"
|
|
1093
1229
|
};
|
|
1230
|
+
//#endregion
|
|
1231
|
+
//#region src/dashboard-extension.ts
|
|
1232
|
+
/** The colour tokens of `dashboard/theme.css`; a test keeps the two in step. */
|
|
1233
|
+
const THEME_TOKENS = [
|
|
1234
|
+
"background",
|
|
1235
|
+
"foreground",
|
|
1236
|
+
"card",
|
|
1237
|
+
"card-foreground",
|
|
1238
|
+
"popover",
|
|
1239
|
+
"popover-foreground",
|
|
1240
|
+
"primary",
|
|
1241
|
+
"primary-foreground",
|
|
1242
|
+
"secondary",
|
|
1243
|
+
"secondary-foreground",
|
|
1244
|
+
"muted",
|
|
1245
|
+
"muted-foreground",
|
|
1246
|
+
"accent",
|
|
1247
|
+
"accent-foreground",
|
|
1248
|
+
"destructive",
|
|
1249
|
+
"destructive-foreground",
|
|
1250
|
+
"positive",
|
|
1251
|
+
"border",
|
|
1252
|
+
"input",
|
|
1253
|
+
"ring",
|
|
1254
|
+
"chart-1",
|
|
1255
|
+
"chart-2",
|
|
1256
|
+
"chart-3",
|
|
1257
|
+
"chart-4",
|
|
1258
|
+
"chart-5",
|
|
1259
|
+
"span-request",
|
|
1260
|
+
"span-action",
|
|
1261
|
+
"span-rpc",
|
|
1262
|
+
"span-sql",
|
|
1263
|
+
"span-event",
|
|
1264
|
+
"span-other"
|
|
1265
|
+
];
|
|
1266
|
+
const THEME_KEYS = /* @__PURE__ */ new Set([
|
|
1267
|
+
"light",
|
|
1268
|
+
"dark",
|
|
1269
|
+
"radius",
|
|
1270
|
+
"fonts"
|
|
1271
|
+
]);
|
|
1272
|
+
const FONT_KEYS = /* @__PURE__ */ new Set(["sans", "mono"]);
|
|
1273
|
+
const TOKENS = new Set(THEME_TOKENS);
|
|
1274
|
+
/** A value that cannot close the rule it sits in. */
|
|
1275
|
+
function cssValue(where, value) {
|
|
1276
|
+
if (typeof value !== "string" || !value.trim() || /[;{}<>\\]/.test(value)) throw new Error(`Brick: dashboard.theme.${where} must be a CSS value without ; { } < > or \\.`);
|
|
1277
|
+
return value.trim();
|
|
1278
|
+
}
|
|
1279
|
+
/**
|
|
1280
|
+
* The stylesheet for a theme, loaded after the dashboard's own; empty when the
|
|
1281
|
+
* theme sets nothing. Throws on a token name theme.css does not have.
|
|
1282
|
+
*/
|
|
1283
|
+
function themeCss(theme) {
|
|
1284
|
+
if (!theme) return "";
|
|
1285
|
+
for (const key of Object.keys(theme)) if (!THEME_KEYS.has(key)) throw new Error(`Brick: dashboard.theme has no "${key}". It takes light, dark, radius and fonts.`);
|
|
1286
|
+
const rules = [];
|
|
1287
|
+
const root = [];
|
|
1288
|
+
if (theme.radius !== void 0) root.push(`--radius: ${cssValue("radius", theme.radius)};`);
|
|
1289
|
+
for (const [name, value] of Object.entries(theme.fonts ?? {})) {
|
|
1290
|
+
if (!FONT_KEYS.has(name)) throw new Error(`Brick: dashboard.theme.fonts has no "${name}". It takes sans and mono.`);
|
|
1291
|
+
root.push(`--font-${name}: ${cssValue(`fonts.${name}`, value)};`);
|
|
1292
|
+
}
|
|
1293
|
+
if (root.length) rules.push(`:root {\n ${root.join("\n ")}\n}`);
|
|
1294
|
+
for (const [mode, selector] of [["light", ":root:not(.dark)"], ["dark", ":root.dark"]]) {
|
|
1295
|
+
const tokens = theme[mode];
|
|
1296
|
+
if (!tokens) continue;
|
|
1297
|
+
const lines = Object.entries(tokens).map(([name, value]) => {
|
|
1298
|
+
if (!TOKENS.has(name)) throw new Error(`Brick: dashboard.theme.${mode} has no token "${name}". Tokens: ${THEME_TOKENS.join(", ")}.`);
|
|
1299
|
+
return `--${name}: ${cssValue(`${mode}.${name}`, value)};`;
|
|
1300
|
+
});
|
|
1301
|
+
if (lines.length) rules.push(`${selector} {\n ${lines.join("\n ")}\n}`);
|
|
1302
|
+
}
|
|
1303
|
+
return rules.length ? `${rules.join("\n")}\n` : "";
|
|
1304
|
+
}
|
|
1094
1305
|
/** The built React app, next to `src/` in the repo and next to `dist/` when published. */
|
|
1095
1306
|
function assetsDirectory() {
|
|
1096
1307
|
for (const relative of ["../dashboard-dist/", "../../dashboard-dist/"]) {
|
|
@@ -1098,247 +1309,152 @@ function assetsDirectory() {
|
|
|
1098
1309
|
if (existsSync(join(directory, "app.js"))) return directory;
|
|
1099
1310
|
}
|
|
1100
1311
|
}
|
|
1101
|
-
|
|
1102
|
-
if (!/^[\w.-]+$/.test(name) || name.startsWith(".")) {
|
|
1103
|
-
send(response, 404, "Not found");
|
|
1104
|
-
return;
|
|
1105
|
-
}
|
|
1106
|
-
const directory = assetsDirectory();
|
|
1107
|
-
if (!directory) {
|
|
1108
|
-
send(response, 503, "Dashboard assets are missing. Build them with: bun run --filter @brickkit/cli build");
|
|
1109
|
-
return;
|
|
1110
|
-
}
|
|
1111
|
-
readFile(join(directory, name)).then((body) => send(response, 200, body, {
|
|
1112
|
-
"content-type": ASSET_TYPES[extname(name)] ?? "application/octet-stream",
|
|
1113
|
-
"cache-control": "no-store"
|
|
1114
|
-
}), () => send(response, 404, "Not found"));
|
|
1115
|
-
}
|
|
1116
|
-
function send(response, status, body, headers = { "content-type": "text/plain; charset=utf-8" }) {
|
|
1117
|
-
response.writeHead(status, headers);
|
|
1118
|
-
response.end(body);
|
|
1119
|
-
}
|
|
1120
|
-
//#endregion
|
|
1121
|
-
//#region src/dev-docs.ts
|
|
1122
|
-
const PACKAGES = ["fumadocs-core", "@mdx-js/mdx"];
|
|
1123
|
-
const PAGE_EXTENSIONS = /* @__PURE__ */ new Set([".md", ".mdx"]);
|
|
1124
|
-
const IMAGE_TYPES = {
|
|
1125
|
-
".png": "image/png",
|
|
1126
|
-
".jpg": "image/jpeg",
|
|
1127
|
-
".jpeg": "image/jpeg",
|
|
1128
|
-
".gif": "image/gif",
|
|
1129
|
-
".webp": "image/webp",
|
|
1130
|
-
".avif": "image/avif",
|
|
1131
|
-
".svg": "image/svg+xml",
|
|
1132
|
-
".ico": "image/x-icon"
|
|
1133
|
-
};
|
|
1134
|
-
const REFRESH_DELAY_MS = 100;
|
|
1135
|
-
async function loadFumadocs() {
|
|
1136
|
-
try {
|
|
1137
|
-
const [source, matter, plugins, search, mdx] = await Promise.all([
|
|
1138
|
-
import("fumadocs-core/source"),
|
|
1139
|
-
import("fumadocs-core/content/md/frontmatter"),
|
|
1140
|
-
import("fumadocs-core/mdx-plugins"),
|
|
1141
|
-
import("fumadocs-core/search/server"),
|
|
1142
|
-
import("@mdx-js/mdx")
|
|
1143
|
-
]);
|
|
1144
|
-
return {
|
|
1145
|
-
loader: source.loader,
|
|
1146
|
-
frontmatter: matter.frontmatter,
|
|
1147
|
-
plugins,
|
|
1148
|
-
createSearchAPI: search.createSearchAPI,
|
|
1149
|
-
compile: mdx.compile
|
|
1150
|
-
};
|
|
1151
|
-
} catch (error) {
|
|
1152
|
-
if (error.code === "ERR_MODULE_NOT_FOUND" || /Cannot find (module|package)/.test(String(error?.message))) return [...PACKAGES];
|
|
1153
|
-
throw error;
|
|
1154
|
-
}
|
|
1155
|
-
}
|
|
1156
|
-
const humanize = (name) => {
|
|
1157
|
-
const spaced = name.replace(/[-_]+/g, " ").trim();
|
|
1158
|
-
return spaced.charAt(0).toUpperCase() + spaced.slice(1);
|
|
1159
|
-
};
|
|
1160
|
-
const H1 = /^#\s+(.+?)\s*#*\s*$/;
|
|
1312
|
+
let warnedTailwind = false;
|
|
1161
1313
|
/**
|
|
1162
|
-
*
|
|
1163
|
-
*
|
|
1164
|
-
*
|
|
1314
|
+
* Tailwind for the classes in the extension's bundle, compiled against the
|
|
1315
|
+
* dashboard's theme.css so `bg-primary` and the rest match. Only utilities are
|
|
1316
|
+
* emitted: the dashboard's stylesheet already has the base styles and tokens.
|
|
1317
|
+
* Tailwind is an optional peer, found from the project first.
|
|
1165
1318
|
*/
|
|
1166
|
-
function
|
|
1167
|
-
|
|
1168
|
-
let
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1319
|
+
async function extensionCss(entry, js, warn) {
|
|
1320
|
+
let tailwind;
|
|
1321
|
+
let main;
|
|
1322
|
+
for (const from of [entry, fileURLToPath(import.meta.url)]) try {
|
|
1323
|
+
const require = createRequire(from);
|
|
1324
|
+
tailwind = require.resolve("tailwindcss/package.json");
|
|
1325
|
+
main = require.resolve("tailwindcss");
|
|
1326
|
+
break;
|
|
1327
|
+
} catch {}
|
|
1328
|
+
const assets = assetsDirectory();
|
|
1329
|
+
const themeFile = assets ? join(assets, "theme.css") : fileURLToPath(new URL("../dashboard/theme.css", import.meta.url));
|
|
1330
|
+
if (!tailwind || !main) {
|
|
1331
|
+
if (!warnedTailwind) warn("[Brick-TS] tailwindcss is not installed, so the dashboard extension's own Tailwind classes are not compiled. The UI kit is styled either way.");
|
|
1332
|
+
warnedTailwind = true;
|
|
1333
|
+
return "";
|
|
1334
|
+
}
|
|
1335
|
+
const root = dirname(tailwind);
|
|
1336
|
+
const { compile } = await import(pathToFileURL(main).href);
|
|
1337
|
+
const compiler = await compile(`@layer theme, base, components, utilities;
|
|
1338
|
+
@import "tailwindcss/theme.css" theme(reference inline);
|
|
1339
|
+
@import "tailwindcss/utilities.css" layer(utilities);
|
|
1340
|
+
@reference "brick/theme.css";
|
|
1341
|
+
`, {
|
|
1342
|
+
base: root,
|
|
1343
|
+
async loadStylesheet(id) {
|
|
1344
|
+
const path = id === "brick/theme.css" ? themeFile : join(root, id.replace(/^tailwindcss\//, ""));
|
|
1178
1345
|
return {
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1346
|
+
path,
|
|
1347
|
+
base: dirname(path),
|
|
1348
|
+
content: await readFile(path, "utf8")
|
|
1182
1349
|
};
|
|
1183
1350
|
}
|
|
1184
|
-
|
|
1185
|
-
|
|
1351
|
+
});
|
|
1352
|
+
const candidates = new Set(js.split(/[\s"'`]+/).filter(Boolean));
|
|
1353
|
+
const css = compiler.build([...candidates]);
|
|
1354
|
+
return /\{/.test(css) ? css : "";
|
|
1186
1355
|
}
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
const
|
|
1190
|
-
async function visit(directory) {
|
|
1191
|
-
for (const entry of await readdir(directory, { withFileTypes: true })) {
|
|
1192
|
-
if (entry.name.startsWith(".") || entry.name === "node_modules") continue;
|
|
1193
|
-
const absolute = join(directory, entry.name);
|
|
1194
|
-
const path = relative(root, absolute).split(sep).join("/");
|
|
1195
|
-
if (entry.isDirectory()) await visit(absolute);
|
|
1196
|
-
else if (entry.name === "meta.json") try {
|
|
1197
|
-
const data = JSON.parse(await readFile(absolute, "utf8"));
|
|
1198
|
-
if (data && typeof data === "object") metas.push({
|
|
1199
|
-
path,
|
|
1200
|
-
data
|
|
1201
|
-
});
|
|
1202
|
-
} catch (error) {
|
|
1203
|
-
warn(`[Brick-TS] Skipping docs ${path}: ${errorMessage(error)}`);
|
|
1204
|
-
}
|
|
1205
|
-
else if (PAGE_EXTENSIONS.has(extname(entry.name))) {
|
|
1206
|
-
const text = await readFile(absolute, "utf8");
|
|
1207
|
-
let data = {};
|
|
1208
|
-
let content = text;
|
|
1209
|
-
try {
|
|
1210
|
-
const parsed = fumadocs.frontmatter(text);
|
|
1211
|
-
data = parsed.data;
|
|
1212
|
-
content = parsed.content;
|
|
1213
|
-
} catch (error) {
|
|
1214
|
-
warn(`[Brick-TS] Ignoring the frontmatter of docs ${path}: ${errorMessage(error)}`);
|
|
1215
|
-
}
|
|
1216
|
-
const name = basename(entry.name, extname(entry.name));
|
|
1217
|
-
const heading = firstHeading(content);
|
|
1218
|
-
const given = typeof data.title === "string" && data.title ? data.title : void 0;
|
|
1219
|
-
const title = given ?? heading?.text ?? humanize(name === "index" ? basename(dirname(path)) || "Home" : name);
|
|
1220
|
-
if (heading && (!given || heading.leading && heading.text === given)) content = heading.rest;
|
|
1221
|
-
pages.push({
|
|
1222
|
-
path,
|
|
1223
|
-
absolute,
|
|
1224
|
-
title,
|
|
1225
|
-
description: typeof data.description === "string" ? data.description : void 0,
|
|
1226
|
-
content,
|
|
1227
|
-
data
|
|
1228
|
-
});
|
|
1229
|
-
}
|
|
1230
|
-
}
|
|
1231
|
-
}
|
|
1232
|
-
await visit(root);
|
|
1356
|
+
/** Bundle the extension and its styles for the dashboard page. */
|
|
1357
|
+
async function buildDashboardExtension(entry, options = {}) {
|
|
1358
|
+
const js = await bundleComponents(entry);
|
|
1233
1359
|
return {
|
|
1234
|
-
|
|
1235
|
-
|
|
1360
|
+
js,
|
|
1361
|
+
css: await extensionCss(entry, js, options.warn ?? ((message) => console.warn(message)))
|
|
1236
1362
|
};
|
|
1237
1363
|
}
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1364
|
+
/**
|
|
1365
|
+
* Write the app's extension and its plugins' into `outdir`, where `brick start`
|
|
1366
|
+
* can serve them: the app's alone as it is, or with plugins each part apart
|
|
1367
|
+
* and `extension.js` loading them, as `brick dev` serves them. Returns false
|
|
1368
|
+
* when there is none to write. A plugin's that fails to build throws.
|
|
1369
|
+
*/
|
|
1370
|
+
async function writeDashboardExtension(entry, plugins, outdir) {
|
|
1371
|
+
const parts = [...entry ? [{
|
|
1372
|
+
name: "app",
|
|
1373
|
+
path: entry,
|
|
1374
|
+
plugin: false
|
|
1375
|
+
}] : [], ...[...pluginDashboards(plugins)].map(([path, name]) => ({
|
|
1376
|
+
name,
|
|
1377
|
+
path,
|
|
1378
|
+
plugin: true
|
|
1379
|
+
}))];
|
|
1380
|
+
if (!parts.length) return false;
|
|
1381
|
+
const built = await Promise.all(parts.map((part) => buildDashboardExtension(part.path).catch((error) => {
|
|
1382
|
+
if (!part.plugin) throw error;
|
|
1383
|
+
throw new Error(`Brick compiler: the dashboard extension of plugin ${part.name} failed to build: ${error instanceof Error ? error.message : String(error)}`, { cause: error });
|
|
1384
|
+
})));
|
|
1385
|
+
await mkdir(outdir, { recursive: true });
|
|
1386
|
+
if (parts.length === 1 && entry) {
|
|
1387
|
+
await writeFile(join(outdir, "extension.js"), built[0].js);
|
|
1388
|
+
await writeFile(join(outdir, "extension.css"), built[0].css);
|
|
1389
|
+
return true;
|
|
1390
|
+
}
|
|
1391
|
+
for (const [index, part] of built.entries()) await writeFile(join(outdir, `extension-${index}.js`), part.js);
|
|
1392
|
+
await writeFile(join(outdir, "extension.js"), extensionLoader(parts.map((part, index) => ({
|
|
1393
|
+
name: part.name,
|
|
1394
|
+
src: `./extension-${index}.js`,
|
|
1395
|
+
plugin: part.plugin
|
|
1396
|
+
}))));
|
|
1397
|
+
await writeFile(join(outdir, "extension.css"), built.map((part) => part.css).join(""));
|
|
1398
|
+
return true;
|
|
1253
1399
|
}
|
|
1254
1400
|
/**
|
|
1255
|
-
* The
|
|
1256
|
-
*
|
|
1401
|
+
* The extension as the dashboard serves it. From source it is built here, and
|
|
1402
|
+
* rebuilt as its folder changes when watching; from a `brick build` folder it
|
|
1403
|
+
* is read as it is. A failed build keeps the error to show on the page.
|
|
1257
1404
|
*/
|
|
1258
|
-
var
|
|
1259
|
-
#
|
|
1260
|
-
#documentation;
|
|
1261
|
-
/** Relative paths in `documentation` start here: the app entry's folder. */
|
|
1262
|
-
#base;
|
|
1263
|
-
#fumadocs;
|
|
1405
|
+
var DashboardExtensionHost = class {
|
|
1406
|
+
#path;
|
|
1264
1407
|
#warn;
|
|
1265
1408
|
#listeners = /* @__PURE__ */ new Set();
|
|
1266
|
-
#
|
|
1267
|
-
#loader;
|
|
1268
|
-
#pages = /* @__PURE__ */ new Map();
|
|
1269
|
-
#byPath = /* @__PURE__ */ new Map();
|
|
1270
|
-
#compiled = /* @__PURE__ */ new Map();
|
|
1271
|
-
#search;
|
|
1409
|
+
#watcher;
|
|
1272
1410
|
#timer;
|
|
1273
|
-
#
|
|
1274
|
-
#closed = false;
|
|
1275
|
-
#title = "Docs";
|
|
1411
|
+
#pending = Promise.resolve();
|
|
1276
1412
|
revision = 0;
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
this.#
|
|
1281
|
-
this.#fumadocs = fumadocs;
|
|
1413
|
+
built;
|
|
1414
|
+
error;
|
|
1415
|
+
constructor(path, warn) {
|
|
1416
|
+
this.#path = resolve(path);
|
|
1282
1417
|
this.#warn = warn;
|
|
1283
1418
|
}
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1419
|
+
load() {
|
|
1420
|
+
this.#pending = this.#pending.then(async () => {
|
|
1421
|
+
const before = JSON.stringify([this.built, this.error]);
|
|
1422
|
+
try {
|
|
1423
|
+
const prebuilt = join(this.#path, "extension.js");
|
|
1424
|
+
if (existsSync(prebuilt)) {
|
|
1425
|
+
const css = join(this.#path, "extension.css");
|
|
1426
|
+
const parts = {};
|
|
1427
|
+
for (const name of await readdir(this.#path)) if (/^extension-\d+\.js$/.test(name)) parts[name] = await readFile(join(this.#path, name), "utf8");
|
|
1428
|
+
this.built = {
|
|
1429
|
+
js: await readFile(prebuilt, "utf8"),
|
|
1430
|
+
css: existsSync(css) ? await readFile(css, "utf8") : "",
|
|
1431
|
+
parts
|
|
1432
|
+
};
|
|
1433
|
+
} else {
|
|
1434
|
+
if (!existsSync(this.#path)) throw new Error(`Brick: the dashboard extension ${this.#path} does not exist.`);
|
|
1435
|
+
this.built = await buildDashboardExtension(this.#path, { warn: this.#warn });
|
|
1301
1436
|
}
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1437
|
+
this.error = void 0;
|
|
1438
|
+
} catch (error) {
|
|
1439
|
+
this.built = void 0;
|
|
1440
|
+
this.error = error instanceof Error ? error.message : String(error);
|
|
1441
|
+
this.#warn(`[Brick-TS] Dashboard extension: ${this.error}`);
|
|
1442
|
+
}
|
|
1443
|
+
if (JSON.stringify([this.built, this.error]) === before) return;
|
|
1444
|
+
this.revision++;
|
|
1445
|
+
for (const listener of this.#listeners) listener();
|
|
1307
1446
|
});
|
|
1308
|
-
this.#
|
|
1309
|
-
this.#pages = /* @__PURE__ */ new Map();
|
|
1310
|
-
this.#byPath = /* @__PURE__ */ new Map();
|
|
1311
|
-
for (const page of this.#loader.getPages()) {
|
|
1312
|
-
const slug = page.slugs.join("/");
|
|
1313
|
-
const file = pages.find((candidate) => candidate.path === page.path);
|
|
1314
|
-
if (!file) continue;
|
|
1315
|
-
this.#pages.set(slug, file);
|
|
1316
|
-
this.#byPath.set(file.path, slug);
|
|
1317
|
-
}
|
|
1318
|
-
this.#compiled.clear();
|
|
1319
|
-
this.#search = void 0;
|
|
1320
|
-
const tree = this.#loader.getPageTree();
|
|
1321
|
-
this.#title = typeof tree.name === "string" ? tree.name : "Docs";
|
|
1322
|
-
this.revision++;
|
|
1323
|
-
for (const listener of this.#listeners) listener();
|
|
1447
|
+
return this.#pending;
|
|
1324
1448
|
}
|
|
1325
|
-
/**
|
|
1449
|
+
/** Rebuild when a file in the extension's folder changes. */
|
|
1326
1450
|
watch() {
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
if (this.#documentation.components) folders.push(dirname(this.#componentsPath()));
|
|
1335
|
-
for (const [index, folder] of folders.entries()) try {
|
|
1336
|
-
this.#watchers.push(watch(folder, { recursive: index === 0 }, () => changed()));
|
|
1337
|
-
} catch {
|
|
1338
|
-
try {
|
|
1339
|
-
this.#watchers.push(watch(folder, () => changed()));
|
|
1340
|
-
} catch {}
|
|
1341
|
-
}
|
|
1451
|
+
try {
|
|
1452
|
+
this.#watcher = watch(dirname(this.#path), { recursive: true }, (_event, file) => {
|
|
1453
|
+
if (file && /(^|[\\/])(node_modules|\.)/.test(file)) return;
|
|
1454
|
+
clearTimeout(this.#timer);
|
|
1455
|
+
this.#timer = setTimeout(() => void this.load(), 100);
|
|
1456
|
+
});
|
|
1457
|
+
} catch {}
|
|
1342
1458
|
}
|
|
1343
1459
|
onChange(listener) {
|
|
1344
1460
|
this.#listeners.add(listener);
|
|
@@ -1346,350 +1462,688 @@ var DevDocs = class {
|
|
|
1346
1462
|
this.#listeners.delete(listener);
|
|
1347
1463
|
};
|
|
1348
1464
|
}
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
title: this.#title,
|
|
1354
|
-
components: Boolean(this.#documentation.components)
|
|
1355
|
-
};
|
|
1465
|
+
close() {
|
|
1466
|
+
clearTimeout(this.#timer);
|
|
1467
|
+
this.#watcher?.close();
|
|
1468
|
+
this.#listeners.clear();
|
|
1356
1469
|
}
|
|
1357
|
-
|
|
1470
|
+
};
|
|
1471
|
+
/** The plugins' extensions by path, each once, in install order. */
|
|
1472
|
+
function pluginDashboards(plugins) {
|
|
1473
|
+
const wanted = /* @__PURE__ */ new Map();
|
|
1474
|
+
for (const plugin of plugins) if (plugin.dashboard && !wanted.has(resolve(plugin.dashboard))) wanted.set(resolve(plugin.dashboard), plugin.name);
|
|
1475
|
+
return wanted;
|
|
1476
|
+
}
|
|
1477
|
+
/**
|
|
1478
|
+
* The module the page loads when several extensions take part: it imports
|
|
1479
|
+
* each one apart, so one that throws leaves the others, and merges them. The
|
|
1480
|
+
* app's own comes first and keeps its slots; pages whose id is taken are
|
|
1481
|
+
* skipped; roots nest in order; a plugin's pages without a place go under a
|
|
1482
|
+
* section named after it.
|
|
1483
|
+
*/
|
|
1484
|
+
function extensionLoader(parts) {
|
|
1485
|
+
return `const parts = ${JSON.stringify(parts)};
|
|
1486
|
+
const loaded = await Promise.allSettled(parts.map((part) => import(part.src)));
|
|
1487
|
+
const merged = { pages: [], sidebar: [], slots: {} };
|
|
1488
|
+
const roots = [];
|
|
1489
|
+
loaded.forEach((result, index) => {
|
|
1490
|
+
const { name, plugin } = parts[index];
|
|
1491
|
+
const extension = result.status === "fulfilled" ? result.value.default : undefined;
|
|
1492
|
+
if (!extension || typeof extension !== "object") {
|
|
1493
|
+
console.error("[Brick dashboard] Extension " + name + " failed to load:", result.status === "rejected" ? result.reason : "it must default-export defineDashboard({ ... })");
|
|
1494
|
+
return;
|
|
1495
|
+
}
|
|
1496
|
+
for (const page of extension.pages ?? []) {
|
|
1497
|
+
if (merged.pages.some((item) => item.id === page.id)) {
|
|
1498
|
+
console.error("[Brick dashboard] Extension " + name + ": page id " + page.id + " is taken; skipped.");
|
|
1499
|
+
continue;
|
|
1500
|
+
}
|
|
1501
|
+
merged.pages.push(plugin && !page.after && !page.section ? { ...page, section: name } : page);
|
|
1502
|
+
}
|
|
1503
|
+
merged.sidebar.push(...(extension.sidebar ?? []));
|
|
1504
|
+
for (const [slot, value] of Object.entries(extension.slots ?? {})) {
|
|
1505
|
+
if (slot === "request.drawer.tabs") merged.slots[slot] = [...(merged.slots[slot] ?? []), ...value];
|
|
1506
|
+
else if (!(slot in merged.slots)) merged.slots[slot] = value;
|
|
1507
|
+
}
|
|
1508
|
+
if (extension.root) roots.push(extension.root);
|
|
1509
|
+
});
|
|
1510
|
+
if (roots.length) {
|
|
1511
|
+
const { createElement } = await import("react");
|
|
1512
|
+
merged.root = ({ children }) => roots.reduceRight((inner, Root) => createElement(Root, null, inner), children);
|
|
1513
|
+
}
|
|
1514
|
+
export default merged;
|
|
1515
|
+
`;
|
|
1516
|
+
}
|
|
1517
|
+
/**
|
|
1518
|
+
* The app's extension and those of its plugins, served as one. With only the
|
|
1519
|
+
* app's, it is served as it is; with plugins, each is built apart (a failed
|
|
1520
|
+
* build leaves the others) and `extension.js` loads them all.
|
|
1521
|
+
*/
|
|
1522
|
+
var DashboardExtensions = class {
|
|
1523
|
+
#warn;
|
|
1524
|
+
#listeners = /* @__PURE__ */ new Set();
|
|
1525
|
+
#parts = [];
|
|
1526
|
+
#own;
|
|
1527
|
+
revision = 0;
|
|
1528
|
+
constructor(path, warn) {
|
|
1529
|
+
this.#warn = warn;
|
|
1530
|
+
this.#own = path !== void 0;
|
|
1531
|
+
if (path) this.#add("app", path);
|
|
1532
|
+
}
|
|
1533
|
+
#add(name, path) {
|
|
1534
|
+
const host = new DashboardExtensionHost(path, this.#warn);
|
|
1535
|
+
host.onChange(() => this.#changed());
|
|
1536
|
+
this.#parts.push({
|
|
1537
|
+
name,
|
|
1538
|
+
path: resolve(path),
|
|
1539
|
+
host
|
|
1540
|
+
});
|
|
1541
|
+
return host;
|
|
1542
|
+
}
|
|
1543
|
+
#changed() {
|
|
1544
|
+
this.revision++;
|
|
1545
|
+
for (const listener of this.#listeners) listener();
|
|
1546
|
+
}
|
|
1547
|
+
/** Whether any extension takes part. */
|
|
1548
|
+
get active() {
|
|
1549
|
+
return this.#parts.length > 0;
|
|
1550
|
+
}
|
|
1551
|
+
get built() {
|
|
1552
|
+
const built = this.#parts.filter((part) => part.host.built);
|
|
1553
|
+
if (!built.length) return void 0;
|
|
1554
|
+
if (this.#own && this.#parts.length === 1) return built[0].host.built;
|
|
1358
1555
|
return {
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1556
|
+
js: extensionLoader(this.#parts.flatMap((part, index) => part.host.built ? [{
|
|
1557
|
+
name: part.name,
|
|
1558
|
+
src: `./extension-${index}.js?v=${this.revision}`,
|
|
1559
|
+
plugin: !(this.#own && index === 0)
|
|
1560
|
+
}] : [])),
|
|
1561
|
+
css: built.map((part) => part.host.built.css).join("")
|
|
1362
1562
|
};
|
|
1363
1563
|
}
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
if (
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
return
|
|
1564
|
+
get error() {
|
|
1565
|
+
const errors = this.#parts.flatMap((part) => part.host.error ? [this.#own && this.#parts.length === 1 ? part.host.error : `${part.name}: ${part.host.error}`] : []);
|
|
1566
|
+
return errors.length ? errors.join("\n") : void 0;
|
|
1567
|
+
}
|
|
1568
|
+
/** A file under `/assets/` this serves, or undefined. */
|
|
1569
|
+
asset(name) {
|
|
1570
|
+
if (name === "extension.js") return this.built?.js;
|
|
1571
|
+
if (name === "extension.css") return this.built?.css;
|
|
1572
|
+
const match = /^extension-(\d+)\.js$/.exec(name);
|
|
1573
|
+
if (!match) return void 0;
|
|
1574
|
+
if (this.#own && this.#parts.length === 1) return this.#parts[0].host.built?.parts?.[name];
|
|
1575
|
+
return this.#parts[Number(match[1])]?.host.built?.js;
|
|
1576
|
+
}
|
|
1577
|
+
/** Follow the app's plugins: build the extensions they ship, drop the rest. */
|
|
1578
|
+
async setPlugins(plugins) {
|
|
1579
|
+
if (this.#own && existsSync(join(this.#parts[0].path, "extension.js"))) return;
|
|
1580
|
+
const wanted = pluginDashboards(plugins);
|
|
1581
|
+
const start = this.#own ? 1 : 0;
|
|
1582
|
+
const current = this.#parts.slice(start);
|
|
1583
|
+
if (current.length === wanted.size && current.every((part) => wanted.get(part.path) === part.name)) return;
|
|
1584
|
+
for (const part of current) part.host.close();
|
|
1585
|
+
this.#parts.splice(start);
|
|
1586
|
+
const loads = [...wanted].map(([path, name]) => this.#add(name, path).load());
|
|
1587
|
+
await Promise.all(loads);
|
|
1588
|
+
this.#changed();
|
|
1589
|
+
}
|
|
1590
|
+
load() {
|
|
1591
|
+
return Promise.all(this.#parts.map((part) => part.host.load())).then(() => void 0);
|
|
1592
|
+
}
|
|
1593
|
+
/** Rebuild the app's own as its folder changes; plugins' are packages. */
|
|
1594
|
+
watch() {
|
|
1595
|
+
if (this.#own) this.#parts[0].host.watch();
|
|
1374
1596
|
}
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
description: file.description,
|
|
1380
|
-
path: file.path,
|
|
1381
|
-
slugs
|
|
1597
|
+
onChange(listener) {
|
|
1598
|
+
this.#listeners.add(listener);
|
|
1599
|
+
return () => {
|
|
1600
|
+
this.#listeners.delete(listener);
|
|
1382
1601
|
};
|
|
1383
|
-
try {
|
|
1384
|
-
const result = await compile({
|
|
1385
|
-
path: file.absolute,
|
|
1386
|
-
value: file.content
|
|
1387
|
-
}, {
|
|
1388
|
-
outputFormat: "function-body",
|
|
1389
|
-
development: false,
|
|
1390
|
-
remarkPlugins: [
|
|
1391
|
-
plugins.remarkGfm,
|
|
1392
|
-
plugins.remarkHeading,
|
|
1393
|
-
plugins.remarkCodeTab,
|
|
1394
|
-
plugins.remarkNpm,
|
|
1395
|
-
plugins.remarkAdmonition,
|
|
1396
|
-
() => (tree) => this.#rewriteLinks(tree, file.path)
|
|
1397
|
-
],
|
|
1398
|
-
rehypePlugins: [plugins.rehypeCode]
|
|
1399
|
-
});
|
|
1400
|
-
const toc = result.data.toc ?? [];
|
|
1401
|
-
return {
|
|
1402
|
-
...base,
|
|
1403
|
-
toc: toc.map((item) => ({
|
|
1404
|
-
title: plainText(item.title),
|
|
1405
|
-
url: item.url,
|
|
1406
|
-
depth: item.depth
|
|
1407
|
-
})),
|
|
1408
|
-
code: String(result)
|
|
1409
|
-
};
|
|
1410
|
-
} catch (error) {
|
|
1411
|
-
return {
|
|
1412
|
-
...base,
|
|
1413
|
-
toc: [],
|
|
1414
|
-
code: "",
|
|
1415
|
-
error: errorMessage(error)
|
|
1416
|
-
};
|
|
1417
|
-
}
|
|
1418
|
-
}
|
|
1419
|
-
/**
|
|
1420
|
-
* Links to other pages become dashboard routes, and local images are served
|
|
1421
|
-
* from the docs folder, so a folder reads the same here as on a docs site.
|
|
1422
|
-
* This covers markdown links and the `href` of components such as `<Card>`.
|
|
1423
|
-
*/
|
|
1424
|
-
#rewriteLinks(tree, from) {
|
|
1425
|
-
const file = (resolved) => `/api/docs/files/${resolved.split("/").map(encodeURIComponent).join("/")}`;
|
|
1426
|
-
walk(tree, (node) => {
|
|
1427
|
-
if (node.type === "link" && node.url !== void 0) node.url = this.#route(from, node.url) ?? node.url;
|
|
1428
|
-
else if (node.type === "image" && node.url !== void 0) {
|
|
1429
|
-
const resolved = resolveInside(from, node.url);
|
|
1430
|
-
if (resolved !== void 0) node.url = file(resolved);
|
|
1431
|
-
} else if (node.type === "mdxJsxFlowElement" || node.type === "mdxJsxTextElement") for (const attribute of node.attributes ?? []) {
|
|
1432
|
-
if (attribute.type !== "mdxJsxAttribute") continue;
|
|
1433
|
-
if (typeof attribute.value !== "string") continue;
|
|
1434
|
-
if (attribute.name === "href") attribute.value = this.#route(from, attribute.value) ?? attribute.value;
|
|
1435
|
-
else if (attribute.name === "src" && node.name === "img") {
|
|
1436
|
-
const resolved = resolveInside(from, attribute.value);
|
|
1437
|
-
if (resolved !== void 0) attribute.value = file(resolved);
|
|
1438
|
-
}
|
|
1439
|
-
}
|
|
1440
|
-
});
|
|
1441
|
-
}
|
|
1442
|
-
/**
|
|
1443
|
-
* The dashboard route for a link to a page of these docs, or undefined for
|
|
1444
|
-
* anything else (other sites, in-page anchors, files). A link to a page that
|
|
1445
|
-
* does not exist still becomes a route, so it says "No such page" here
|
|
1446
|
-
* instead of leaving the dashboard.
|
|
1447
|
-
*/
|
|
1448
|
-
#route(from, href) {
|
|
1449
|
-
const hashAt = href.indexOf("#");
|
|
1450
|
-
const hash = hashAt >= 0 ? href.slice(hashAt) : "";
|
|
1451
|
-
const target = (hashAt >= 0 ? href.slice(0, hashAt) : href).split("?")[0] ?? "";
|
|
1452
|
-
const route = (slug) => `#/docs${slug ? `/${slug}` : ""}${hash}`;
|
|
1453
|
-
if (/^[a-z][a-z\d+.-]*:/i.test(href) || href.startsWith("#") || !target) return void 0;
|
|
1454
|
-
if (target.startsWith("/")) {
|
|
1455
|
-
const slug = target.replace(/^\/+|\/+$/g, "");
|
|
1456
|
-
return this.#pages.has(slug) ? route(slug) : void 0;
|
|
1457
|
-
}
|
|
1458
|
-
const resolved = resolveInside(from, target);
|
|
1459
|
-
if (resolved === void 0) return void 0;
|
|
1460
|
-
const known = this.#byPath.get(resolved);
|
|
1461
|
-
if (known !== void 0) return route(known);
|
|
1462
|
-
if (!PAGE_EXTENSIONS.has(extname(resolved))) return void 0;
|
|
1463
|
-
return route(resolved.slice(0, -extname(resolved).length).replace(/(^|\/)index$/, ""));
|
|
1464
|
-
}
|
|
1465
|
-
async search(query) {
|
|
1466
|
-
if (!query.trim()) return [];
|
|
1467
|
-
this.#search ??= this.#buildSearch();
|
|
1468
|
-
return (await this.#search).search(query);
|
|
1469
|
-
}
|
|
1470
|
-
async #buildSearch() {
|
|
1471
|
-
const { plugins, createSearchAPI } = this.#fumadocs;
|
|
1472
|
-
const api = createSearchAPI("advanced", { indexes: [...this.#pages.entries()].map(([slug, file]) => ({
|
|
1473
|
-
id: file.path,
|
|
1474
|
-
title: file.title,
|
|
1475
|
-
description: file.description,
|
|
1476
|
-
url: `/${slug}`,
|
|
1477
|
-
structuredData: plugins.structure(file.content)
|
|
1478
|
-
})) });
|
|
1479
|
-
return { async search(query) {
|
|
1480
|
-
return await api.search(query);
|
|
1481
|
-
} };
|
|
1482
|
-
}
|
|
1483
|
-
/** A file from the docs folder that a page can show, such as an image. */
|
|
1484
|
-
async file(relativePath) {
|
|
1485
|
-
const type = IMAGE_TYPES[extname(relativePath).toLowerCase()];
|
|
1486
|
-
if (!type) return void 0;
|
|
1487
|
-
const absolute = resolve(this.#root, relativePath);
|
|
1488
|
-
if (absolute !== this.#root && !absolute.startsWith(this.#root + sep)) return void 0;
|
|
1489
|
-
try {
|
|
1490
|
-
if (!(await stat(absolute)).isFile()) return void 0;
|
|
1491
|
-
return {
|
|
1492
|
-
body: new Uint8Array(await readFile(absolute)),
|
|
1493
|
-
type
|
|
1494
|
-
};
|
|
1495
|
-
} catch {
|
|
1496
|
-
return;
|
|
1497
|
-
}
|
|
1498
|
-
}
|
|
1499
|
-
#componentsPath() {
|
|
1500
|
-
const { components } = this.#documentation;
|
|
1501
|
-
return components ? resolve(this.#base, components) : void 0;
|
|
1502
|
-
}
|
|
1503
|
-
/** The project's components as one browser module; undefined when it has none. */
|
|
1504
|
-
async components() {
|
|
1505
|
-
const entry = this.#componentsPath();
|
|
1506
|
-
return entry ? bundleComponents(entry) : void 0;
|
|
1507
1602
|
}
|
|
1508
1603
|
close() {
|
|
1509
|
-
this.#
|
|
1510
|
-
clearTimeout(this.#timer);
|
|
1511
|
-
for (const watcher of this.#watchers) watcher.close();
|
|
1512
|
-
this.#watchers.length = 0;
|
|
1604
|
+
for (const part of this.#parts) part.host.close();
|
|
1513
1605
|
this.#listeners.clear();
|
|
1514
1606
|
}
|
|
1515
1607
|
};
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
|
|
1608
|
+
//#endregion
|
|
1609
|
+
//#region src/dev-dashboard.ts
|
|
1610
|
+
const DEFAULT_DASHBOARD_PORT = 9400;
|
|
1611
|
+
const KEEPALIVE_MS = 15e3;
|
|
1612
|
+
const MAX_INGEST_BYTES = 5242880;
|
|
1613
|
+
const MAX_PROXY_BYTES = 10485760;
|
|
1614
|
+
const NOT_FORWARDED = /* @__PURE__ */ new Set([
|
|
1615
|
+
"host",
|
|
1616
|
+
"connection",
|
|
1617
|
+
"keep-alive",
|
|
1618
|
+
"proxy-connection",
|
|
1619
|
+
"transfer-encoding",
|
|
1620
|
+
"te",
|
|
1621
|
+
"trailer",
|
|
1622
|
+
"upgrade",
|
|
1623
|
+
"content-length",
|
|
1624
|
+
"accept-encoding",
|
|
1625
|
+
"origin",
|
|
1626
|
+
"referer",
|
|
1627
|
+
"cookie"
|
|
1628
|
+
]);
|
|
1629
|
+
const NOT_RETURNED = /* @__PURE__ */ new Set([
|
|
1630
|
+
"connection",
|
|
1631
|
+
"keep-alive",
|
|
1632
|
+
"transfer-encoding",
|
|
1633
|
+
"content-length",
|
|
1634
|
+
"content-encoding",
|
|
1635
|
+
"set-cookie"
|
|
1636
|
+
]);
|
|
1637
|
+
/**
|
|
1638
|
+
* What a production dashboard may show: the shell and its assets, the live
|
|
1639
|
+
* state and its event stream, and stored history. Nothing that changes the app
|
|
1640
|
+
* or reaches into it (restart, the API proxy, ingest) and nothing from the
|
|
1641
|
+
* project's source (documentation, OpenAPI documents). With an extension, the
|
|
1642
|
+
* `/app` proxy its pages read the app through.
|
|
1643
|
+
*/
|
|
1644
|
+
const isMonitoringRoute = (method, pathname, extension) => extension && isAppRoute(pathname) || method === "GET" && (pathname === "/" || pathname.startsWith("/assets/") || pathname === "/api/state" || pathname === "/api/events" || pathname.startsWith("/api/history"));
|
|
1645
|
+
/** `/app/*` reaches the app itself, for the extension's `useAppFetch`. */
|
|
1646
|
+
const APP_PATH = "/app";
|
|
1647
|
+
const isAppRoute = (pathname) => pathname === APP_PATH || pathname.startsWith(`${APP_PATH}/`);
|
|
1648
|
+
const PRODUCTION_HEADERS = {
|
|
1649
|
+
"x-content-type-options": "nosniff",
|
|
1650
|
+
"x-frame-options": "DENY",
|
|
1651
|
+
"referrer-policy": "no-referrer",
|
|
1652
|
+
"cross-origin-opener-policy": "same-origin",
|
|
1653
|
+
"cross-origin-resource-policy": "same-origin",
|
|
1654
|
+
"cache-control": "no-store",
|
|
1655
|
+
"content-security-policy": "default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; img-src 'self' data: blob:; font-src 'self' data:; connect-src 'self'; object-src 'none'; base-uri 'none'; form-action 'none'; frame-ancestors 'none'"
|
|
1656
|
+
};
|
|
1657
|
+
/** How long `/_health` waits for the app before calling it down. */
|
|
1658
|
+
const PROBE_TIMEOUT_MS = 3e3;
|
|
1659
|
+
/**
|
|
1660
|
+
* Serve the dashboard, on loopback unless told otherwise. On loopback requests
|
|
1661
|
+
* must name a local host (blocks DNS rebinding), and restart requires a header
|
|
1662
|
+
* that cross-origin pages cannot send without a preflight. Elsewhere `auth` is
|
|
1663
|
+
* required and decides who gets in. Uses node:http, so it runs under Bun and
|
|
1664
|
+
* under Node (Vite's dev server runs under Node).
|
|
1665
|
+
*/
|
|
1666
|
+
function startDevDashboard(options) {
|
|
1667
|
+
const host = options.host ?? "127.0.0.1";
|
|
1668
|
+
const loopback = isLoopbackHost(host);
|
|
1669
|
+
const production = options.mode === "production";
|
|
1670
|
+
if (!loopback && !options.auth) return Promise.reject(/* @__PURE__ */ new Error(`Brick: the dashboard would listen on ${host}, where others can reach it, so it needs access control. Set dashboard.auth with a token or an authorize function.`));
|
|
1671
|
+
let theme;
|
|
1672
|
+
try {
|
|
1673
|
+
theme = themeCss(options.dashboard?.theme);
|
|
1674
|
+
} catch (error) {
|
|
1675
|
+
return Promise.reject(error);
|
|
1676
|
+
}
|
|
1677
|
+
const extension = options.dashboard?.extension || !production || options.plugins?.some((plugin) => plugin.dashboard) ? new DashboardExtensions(options.dashboard?.extension, (message) => console.warn(message)) : void 0;
|
|
1678
|
+
const access = options.auth ? createAccess(options.auth, (error) => console.error("[Brick-TS] The dashboard sign-in check failed:", error)) : void 0;
|
|
1679
|
+
let allowedHosts = /* @__PURE__ */ new Set();
|
|
1680
|
+
const server = createServer((request, response) => {
|
|
1681
|
+
respond(request, response).catch((error) => {
|
|
1682
|
+
console.error("[Brick-TS] The dashboard failed to answer:", error);
|
|
1683
|
+
if (response.headersSent) response.end();
|
|
1684
|
+
else send(response, 500, "Server error");
|
|
1530
1685
|
});
|
|
1531
|
-
|
|
1532
|
-
|
|
1686
|
+
});
|
|
1687
|
+
async function respond(request, response) {
|
|
1688
|
+
if (production) for (const [name, value] of Object.entries(PRODUCTION_HEADERS)) response.setHeader(name, value);
|
|
1689
|
+
if (loopback && !allowedHosts.has(request.headers.host ?? "")) {
|
|
1690
|
+
send(response, 403, "Forbidden");
|
|
1691
|
+
return;
|
|
1692
|
+
}
|
|
1693
|
+
const url = new URL(request.url ?? "/", "http://127.0.0.1");
|
|
1694
|
+
const { pathname } = url;
|
|
1695
|
+
if (pathname === "/_health" && (request.method === "GET" || request.method === "HEAD")) {
|
|
1696
|
+
await health(response);
|
|
1697
|
+
return;
|
|
1698
|
+
}
|
|
1699
|
+
if (access) {
|
|
1700
|
+
const decision = await access(request);
|
|
1701
|
+
if (!decision.allowed) {
|
|
1702
|
+
send(response, decision.status, decision.body, decision.headers);
|
|
1703
|
+
return;
|
|
1704
|
+
}
|
|
1705
|
+
}
|
|
1706
|
+
if (production && !isMonitoringRoute(request.method, pathname, Boolean(extension?.active))) {
|
|
1707
|
+
send(response, 404, "Not found");
|
|
1708
|
+
return;
|
|
1709
|
+
}
|
|
1710
|
+
handle(request, response, url);
|
|
1533
1711
|
}
|
|
1534
|
-
|
|
1535
|
-
|
|
1536
|
-
|
|
1537
|
-
|
|
1538
|
-
|
|
1539
|
-
|
|
1540
|
-
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
|
|
1544
|
-
minify: false,
|
|
1545
|
-
lib: {
|
|
1546
|
-
entry,
|
|
1547
|
-
formats: ["es"],
|
|
1548
|
-
fileName: "components"
|
|
1549
|
-
},
|
|
1550
|
-
rollupOptions: { external: SHARED }
|
|
1712
|
+
/** 200 while the app answers its own health check, 503 when it does not. */
|
|
1713
|
+
async function health(response) {
|
|
1714
|
+
const probe = options.probe;
|
|
1715
|
+
let up = true;
|
|
1716
|
+
if (probe) {
|
|
1717
|
+
let timer;
|
|
1718
|
+
up = await Promise.race([probe().catch(() => false), new Promise((resolve) => {
|
|
1719
|
+
timer = setTimeout(() => resolve(false), PROBE_TIMEOUT_MS);
|
|
1720
|
+
})]);
|
|
1721
|
+
clearTimeout(timer);
|
|
1551
1722
|
}
|
|
1552
|
-
|
|
1553
|
-
|
|
1554
|
-
|
|
1555
|
-
|
|
1556
|
-
|
|
1723
|
+
send(response, up ? 200 : 503, JSON.stringify({
|
|
1724
|
+
status: up ? "ok" : "degraded",
|
|
1725
|
+
...probe ? { app: up ? "up" : "down" } : {}
|
|
1726
|
+
}), {
|
|
1727
|
+
"content-type": "application/json",
|
|
1728
|
+
"cache-control": "no-store"
|
|
1729
|
+
});
|
|
1557
1730
|
}
|
|
1558
|
-
|
|
1559
|
-
}
|
|
1560
|
-
|
|
1561
|
-
|
|
1562
|
-
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
|
|
1731
|
+
function handle(request, response, url) {
|
|
1732
|
+
const { pathname } = url;
|
|
1733
|
+
if (request.method === "GET" && pathname === "/") {
|
|
1734
|
+
send(response, 200, dashboardShell({
|
|
1735
|
+
extension: extension?.active ? {
|
|
1736
|
+
revision: extension.revision,
|
|
1737
|
+
js: Boolean(extension.built),
|
|
1738
|
+
css: Boolean(extension.built?.css)
|
|
1739
|
+
} : void 0,
|
|
1740
|
+
theme: theme !== ""
|
|
1741
|
+
}), {
|
|
1742
|
+
"content-type": "text/html; charset=utf-8",
|
|
1743
|
+
"cache-control": "no-store"
|
|
1744
|
+
});
|
|
1745
|
+
return;
|
|
1746
|
+
}
|
|
1747
|
+
if (request.method === "GET" && pathname.startsWith("/assets/")) {
|
|
1748
|
+
const name = pathname.slice(8);
|
|
1749
|
+
const own = name === "brick-theme.css" ? theme : extension?.asset(name);
|
|
1750
|
+
if (own !== void 0) send(response, 200, own, {
|
|
1751
|
+
"content-type": ASSET_TYPES[extname(name)],
|
|
1752
|
+
"cache-control": "no-store"
|
|
1753
|
+
});
|
|
1754
|
+
else serveAsset(response, name);
|
|
1755
|
+
return;
|
|
1756
|
+
}
|
|
1757
|
+
if (isAppRoute(pathname)) {
|
|
1758
|
+
app(request, response, url);
|
|
1759
|
+
return;
|
|
1760
|
+
}
|
|
1761
|
+
if (request.method === "GET" && pathname === "/api/state") {
|
|
1762
|
+
send(response, 200, JSON.stringify(snapshot()), { "content-type": "application/json" });
|
|
1763
|
+
return;
|
|
1764
|
+
}
|
|
1765
|
+
if (request.method === "GET" && pathname === "/api/events") {
|
|
1766
|
+
eventStream(request, response);
|
|
1767
|
+
return;
|
|
1768
|
+
}
|
|
1769
|
+
if (request.method === "GET" && pathname.startsWith("/api/history") && options.history) {
|
|
1770
|
+
history(options.history, pathname, url.searchParams, response);
|
|
1771
|
+
return;
|
|
1772
|
+
}
|
|
1773
|
+
if (request.method === "GET" && pathname.startsWith("/api/docs")) {
|
|
1774
|
+
docsRoute(response, pathname, url.searchParams);
|
|
1775
|
+
return;
|
|
1776
|
+
}
|
|
1777
|
+
if (request.method === "GET" && pathname === "/api/openapi") {
|
|
1778
|
+
send(response, 200, JSON.stringify(options.store.apiDocuments()), {
|
|
1779
|
+
"content-type": "application/json",
|
|
1780
|
+
"cache-control": "no-store"
|
|
1781
|
+
});
|
|
1782
|
+
return;
|
|
1783
|
+
}
|
|
1784
|
+
if (pathname === "/api/proxy") {
|
|
1785
|
+
proxy(request, response, url);
|
|
1786
|
+
return;
|
|
1787
|
+
}
|
|
1788
|
+
if (pathname === "/workflows" || pathname.startsWith(`/workflows/`)) {
|
|
1789
|
+
response.setHeader("content-security-policy", "frame-ancestors 'self'");
|
|
1790
|
+
response.setHeader("x-frame-options", "SAMEORIGIN");
|
|
1791
|
+
inspector(request, response, url);
|
|
1792
|
+
return;
|
|
1793
|
+
}
|
|
1794
|
+
if (request.method === "POST" && pathname === "/api/ingest") {
|
|
1795
|
+
ingest(request, response);
|
|
1796
|
+
return;
|
|
1797
|
+
}
|
|
1798
|
+
if (request.method === "POST" && pathname === "/api/restart" && options.restart) {
|
|
1799
|
+
if (request.headers["x-brick-dashboard"] !== "1") {
|
|
1800
|
+
send(response, 403, "Forbidden");
|
|
1801
|
+
return;
|
|
1802
|
+
}
|
|
1803
|
+
options.restart();
|
|
1804
|
+
send(response, 202, "");
|
|
1805
|
+
return;
|
|
1806
|
+
}
|
|
1807
|
+
send(response, 404, "Not found");
|
|
1568
1808
|
}
|
|
1569
|
-
|
|
1570
|
-
}
|
|
1571
|
-
/** The page tree as plain data for the dashboard; icons and links are left out. */
|
|
1572
|
-
function simplify(tree) {
|
|
1573
|
-
const text = (value) => typeof value === "string" ? value : plainText(value);
|
|
1574
|
-
const convert = (node) => {
|
|
1575
|
-
if (node.type === "page" && node.url !== void 0) return {
|
|
1576
|
-
type: "page",
|
|
1577
|
-
name: text(node.name),
|
|
1578
|
-
url: node.url,
|
|
1579
|
-
...typeof node.description === "string" ? { description: node.description } : {}
|
|
1580
|
-
};
|
|
1581
|
-
if (node.type === "separator") return {
|
|
1582
|
-
type: "separator",
|
|
1583
|
-
...node.name ? { name: text(node.name) } : {}
|
|
1584
|
-
};
|
|
1585
|
-
if (node.type === "folder") return {
|
|
1586
|
-
type: "folder",
|
|
1587
|
-
name: text(node.name),
|
|
1588
|
-
...node.index?.url !== void 0 ? { index: {
|
|
1589
|
-
name: text(node.index.name),
|
|
1590
|
-
url: node.index.url
|
|
1591
|
-
} } : {},
|
|
1592
|
-
children: (node.children ?? []).flatMap((child) => convert(child) ?? []),
|
|
1593
|
-
...node.defaultOpen ? { defaultOpen: true } : {}
|
|
1594
|
-
};
|
|
1595
|
-
};
|
|
1596
|
-
return {
|
|
1597
|
-
name: text(tree?.name) || "Docs",
|
|
1598
|
-
children: (tree?.children ?? []).flatMap((child) => convert(child) ?? [])
|
|
1599
|
-
};
|
|
1600
|
-
}
|
|
1601
|
-
/** Open a docs folder; says what is missing when it cannot be shown. */
|
|
1602
|
-
async function openDocs(options) {
|
|
1603
|
-
const fumadocs = await loadFumadocs();
|
|
1604
|
-
if (Array.isArray(fumadocs)) return {
|
|
1605
|
-
status: "missing",
|
|
1606
|
-
packages: fumadocs
|
|
1607
|
-
};
|
|
1608
|
-
const docs = new DevDocs(options.base, options.documentation, fumadocs, options.warn);
|
|
1609
|
-
try {
|
|
1610
|
-
await docs.refresh();
|
|
1611
|
-
} catch (error) {
|
|
1612
|
-
docs.close();
|
|
1613
|
-
const root = resolve(options.base, options.documentation.dir);
|
|
1809
|
+
function snapshot() {
|
|
1614
1810
|
return {
|
|
1615
|
-
|
|
1616
|
-
|
|
1811
|
+
...options.store.snapshot(),
|
|
1812
|
+
canRestart: Boolean(options.restart),
|
|
1813
|
+
historyDays: options.history?.days ?? 0,
|
|
1814
|
+
mode: production ? "production" : "dev",
|
|
1815
|
+
extension: extension?.active ? {
|
|
1816
|
+
revision: extension.revision,
|
|
1817
|
+
error: extension.error
|
|
1818
|
+
} : null
|
|
1617
1819
|
};
|
|
1618
1820
|
}
|
|
1619
|
-
|
|
1620
|
-
|
|
1621
|
-
|
|
1622
|
-
|
|
1623
|
-
|
|
1624
|
-
|
|
1625
|
-
|
|
1626
|
-
|
|
1627
|
-
|
|
1628
|
-
|
|
1629
|
-
|
|
1630
|
-
|
|
1631
|
-
|
|
1632
|
-
|
|
1633
|
-
|
|
1634
|
-
|
|
1635
|
-
|
|
1636
|
-
|
|
1637
|
-
|
|
1638
|
-
|
|
1821
|
+
/**
|
|
1822
|
+
* Accept telemetry from an application process that cannot share memory with
|
|
1823
|
+
* the dashboard, such as a dev server worker: span batches, and a notice when
|
|
1824
|
+
* the app loads.
|
|
1825
|
+
*/
|
|
1826
|
+
function ingest(request, response) {
|
|
1827
|
+
if (request.headers["x-brick-dashboard"] !== "1") {
|
|
1828
|
+
send(response, 403, "Forbidden");
|
|
1829
|
+
return;
|
|
1830
|
+
}
|
|
1831
|
+
let body = "";
|
|
1832
|
+
let rejected = false;
|
|
1833
|
+
request.setEncoding("utf8");
|
|
1834
|
+
request.on("data", (chunk) => {
|
|
1835
|
+
if (rejected) return;
|
|
1836
|
+
body += chunk;
|
|
1837
|
+
if (body.length > MAX_INGEST_BYTES) {
|
|
1838
|
+
rejected = true;
|
|
1839
|
+
send(response, 413, "Payload too large");
|
|
1840
|
+
request.destroy();
|
|
1841
|
+
}
|
|
1842
|
+
});
|
|
1843
|
+
request.on("end", () => {
|
|
1844
|
+
if (rejected) return;
|
|
1845
|
+
try {
|
|
1846
|
+
const payload = JSON.parse(body);
|
|
1847
|
+
if (Array.isArray(payload.records)) options.store.ingest(payload.records, options.store.latestGeneration);
|
|
1848
|
+
if (typeof payload.worker === "string") options.store.startGeneration(payload.worker);
|
|
1849
|
+
send(response, 204, "");
|
|
1850
|
+
} catch {
|
|
1851
|
+
send(response, 400, "Bad request");
|
|
1852
|
+
}
|
|
1853
|
+
});
|
|
1639
1854
|
}
|
|
1640
|
-
|
|
1641
|
-
|
|
1855
|
+
/** The Docs tab's data: tree, compiled pages, search, images, components. */
|
|
1856
|
+
async function docsRoute(response, pathname, params) {
|
|
1857
|
+
const docs = options.docs?.();
|
|
1858
|
+
if (!docs) {
|
|
1859
|
+
send(response, 404, "The app has no documentation.");
|
|
1860
|
+
return;
|
|
1861
|
+
}
|
|
1862
|
+
const json = (body) => send(response, 200, JSON.stringify(body), {
|
|
1863
|
+
"content-type": "application/json",
|
|
1864
|
+
"cache-control": "no-store"
|
|
1865
|
+
});
|
|
1866
|
+
try {
|
|
1867
|
+
if (pathname === "/api/docs") return json(docs.info());
|
|
1868
|
+
if (pathname === "/api/docs/search") return json(await docs.search(params.get("q") ?? ""));
|
|
1869
|
+
if (pathname === "/api/docs/components.js") {
|
|
1870
|
+
const code = await docs.components();
|
|
1871
|
+
if (code === void 0) return send(response, 404, "No components.");
|
|
1872
|
+
return send(response, 200, code, {
|
|
1873
|
+
"content-type": "text/javascript; charset=utf-8",
|
|
1874
|
+
"cache-control": "no-store"
|
|
1875
|
+
});
|
|
1876
|
+
}
|
|
1877
|
+
const pagePrefix = "/api/docs/page";
|
|
1878
|
+
if (pathname === pagePrefix || pathname.startsWith(`${pagePrefix}/`)) {
|
|
1879
|
+
const slugs = pathname.slice(14).split("/").filter(Boolean).map(decodeURIComponent);
|
|
1880
|
+
const page = await docs.page(slugs);
|
|
1881
|
+
return page ? json(page) : send(response, 404, "No such page.");
|
|
1882
|
+
}
|
|
1883
|
+
if (pathname.startsWith("/api/docs/files/")) {
|
|
1884
|
+
const file = await docs.file(pathname.slice(16).split("/").map(decodeURIComponent).join("/"));
|
|
1885
|
+
if (!file) return send(response, 404, "No such file.");
|
|
1886
|
+
return send(response, 200, file.body, {
|
|
1887
|
+
"content-type": file.type,
|
|
1888
|
+
"cache-control": "no-store",
|
|
1889
|
+
"content-security-policy": "sandbox"
|
|
1890
|
+
});
|
|
1891
|
+
}
|
|
1892
|
+
send(response, 404, "Not found");
|
|
1893
|
+
} catch (error) {
|
|
1894
|
+
send(response, error instanceof URIError ? 400 : 500, error instanceof Error ? error.message : String(error));
|
|
1895
|
+
}
|
|
1642
1896
|
}
|
|
1643
1897
|
/**
|
|
1644
|
-
*
|
|
1645
|
-
*
|
|
1898
|
+
* Send a request on to the app for the APIs view, which cannot call the app
|
|
1899
|
+
* directly: it is another origin. Only the dashboard's own page may use this,
|
|
1900
|
+
* and only to reach the app being shown. The address to call is `?url=`; a
|
|
1901
|
+
* `?cookie=` is the cookie parameters the reader typed into Try it, and the
|
|
1902
|
+
* only cookie the app is sent (the dashboard's own are never forwarded).
|
|
1646
1903
|
*/
|
|
1647
|
-
|
|
1648
|
-
|
|
1649
|
-
|
|
1904
|
+
/**
|
|
1905
|
+
* Pass a request to the workflow inspector, which the app serves on a port of
|
|
1906
|
+
* its own, and stream its answer back (it follows a run's output as it
|
|
1907
|
+
* grows). Anyone who can open the dashboard may look, but a change must come
|
|
1908
|
+
* from the dashboard's own page: the inspector can cancel runs and resume
|
|
1909
|
+
* hooks, and a page on another site could otherwise post to it.
|
|
1910
|
+
*/
|
|
1911
|
+
async function inspector(request, response, url) {
|
|
1912
|
+
const target = options.store.inspectorTarget();
|
|
1913
|
+
if (!target) {
|
|
1914
|
+
send(response, 503, "The workflow inspector is not running. The app may be restarting, or it may run no workflows.", { "content-type": "text/plain; charset=utf-8" });
|
|
1915
|
+
return;
|
|
1916
|
+
}
|
|
1917
|
+
const bodyless = request.method === "GET" || request.method === "HEAD";
|
|
1918
|
+
const site = request.headers["sec-fetch-site"];
|
|
1919
|
+
const origin = request.headers.origin;
|
|
1920
|
+
if (!bodyless && (site !== void 0 && site !== "same-origin" || origin !== void 0 && origin !== `http://${request.headers.host}`)) {
|
|
1921
|
+
send(response, 403, "Forbidden");
|
|
1922
|
+
return;
|
|
1923
|
+
}
|
|
1924
|
+
await forward(request, response, `http://127.0.0.1:${target.port}${url.pathname}${url.search}`, (headers) => headers.set(INSPECTOR_TOKEN_HEADER, target.token), "Could not reach the workflow inspector.");
|
|
1650
1925
|
}
|
|
1651
|
-
|
|
1652
|
-
|
|
1653
|
-
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
1926
|
+
/**
|
|
1927
|
+
* Pass `/app/*` to the app, so the extension's pages read it from the
|
|
1928
|
+
* dashboard's own origin. Only the dashboard's page may use it, as with the
|
|
1929
|
+
* API proxy. A credential for the dashboard itself is not passed on.
|
|
1930
|
+
*/
|
|
1931
|
+
async function app(request, response, url) {
|
|
1932
|
+
const site = request.headers["sec-fetch-site"];
|
|
1933
|
+
const origin = request.headers.origin;
|
|
1934
|
+
if (site !== void 0 && site !== "same-origin" || origin !== void 0 && origin !== `http://${request.headers.host}`) {
|
|
1935
|
+
send(response, 403, "Forbidden");
|
|
1658
1936
|
return;
|
|
1659
1937
|
}
|
|
1660
|
-
const
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
|
|
1664
|
-
}
|
|
1665
|
-
|
|
1666
|
-
|
|
1667
|
-
|
|
1668
|
-
|
|
1669
|
-
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
|
|
1673
|
-
|
|
1674
|
-
|
|
1938
|
+
const appUrl = options.appUrl ?? options.store.apiSnapshot()?.appUrl;
|
|
1939
|
+
if (!appUrl) {
|
|
1940
|
+
send(response, 503, "The app is not running yet.");
|
|
1941
|
+
return;
|
|
1942
|
+
}
|
|
1943
|
+
response.setHeader("content-security-policy", "frame-ancestors 'self'");
|
|
1944
|
+
response.setHeader("x-frame-options", "SAMEORIGIN");
|
|
1945
|
+
await forward(request, response, `${new URL(appUrl).origin}${url.pathname.slice(4) || "/"}${url.search}`, (headers) => {
|
|
1946
|
+
if (access) headers.delete("authorization");
|
|
1947
|
+
}, "Could not reach the app.");
|
|
1948
|
+
}
|
|
1949
|
+
/** Send the request on to `target` and stream its answer back. */
|
|
1950
|
+
async function forward(request, response, target, adjust, unreachable) {
|
|
1951
|
+
const bodyless = request.method === "GET" || request.method === "HEAD";
|
|
1952
|
+
const headers = new Headers();
|
|
1953
|
+
for (const [name, value] of Object.entries(request.headers)) {
|
|
1954
|
+
if (value === void 0 || NOT_FORWARDED.has(name)) continue;
|
|
1955
|
+
if (name.startsWith("sec-")) continue;
|
|
1956
|
+
headers.set(name, Array.isArray(value) ? value.join(", ") : value);
|
|
1957
|
+
}
|
|
1958
|
+
adjust(headers);
|
|
1959
|
+
const abort = new AbortController();
|
|
1960
|
+
response.on("close", () => abort.abort());
|
|
1961
|
+
try {
|
|
1962
|
+
const upstream = await fetch(target, {
|
|
1963
|
+
method: request.method,
|
|
1964
|
+
headers,
|
|
1965
|
+
redirect: "manual",
|
|
1966
|
+
signal: abort.signal,
|
|
1967
|
+
...bodyless ? {} : {
|
|
1968
|
+
body: Readable.toWeb(request),
|
|
1969
|
+
duplex: "half"
|
|
1970
|
+
}
|
|
1971
|
+
});
|
|
1972
|
+
const returned = {};
|
|
1973
|
+
upstream.headers.forEach((value, name) => {
|
|
1974
|
+
if (!NOT_RETURNED.has(name)) returned[name] = value;
|
|
1675
1975
|
});
|
|
1976
|
+
response.writeHead(upstream.status, returned);
|
|
1977
|
+
if (!upstream.body || request.method === "HEAD") {
|
|
1978
|
+
response.end();
|
|
1979
|
+
return;
|
|
1980
|
+
}
|
|
1981
|
+
await pipeline(Readable.fromWeb(upstream.body), response);
|
|
1982
|
+
} catch {
|
|
1983
|
+
if (response.headersSent) response.destroy();
|
|
1984
|
+
else send(response, 502, unreachable);
|
|
1985
|
+
}
|
|
1986
|
+
}
|
|
1987
|
+
async function proxy(request, response, url) {
|
|
1988
|
+
const site = request.headers["sec-fetch-site"];
|
|
1989
|
+
const origin = request.headers.origin;
|
|
1990
|
+
if (site !== void 0 && site !== "same-origin" || origin !== void 0 && origin !== `http://${request.headers.host}`) {
|
|
1991
|
+
send(response, 403, "Forbidden");
|
|
1676
1992
|
return;
|
|
1677
1993
|
}
|
|
1678
|
-
|
|
1679
|
-
|
|
1680
|
-
|
|
1994
|
+
let target;
|
|
1995
|
+
try {
|
|
1996
|
+
target = new URL(url.searchParams.get("url") ?? "");
|
|
1997
|
+
} catch {}
|
|
1998
|
+
const appUrl = options.store.apiSnapshot()?.appUrl;
|
|
1999
|
+
if (!target || !appUrl || target.origin !== new URL(appUrl).origin) {
|
|
2000
|
+
send(response, 403, "Only the app shown in the dashboard can be reached.");
|
|
2001
|
+
return;
|
|
2002
|
+
}
|
|
2003
|
+
const body = await readBytes(request, MAX_PROXY_BYTES);
|
|
2004
|
+
if (!body) {
|
|
2005
|
+
send(response, 413, "Payload too large");
|
|
2006
|
+
return;
|
|
2007
|
+
}
|
|
2008
|
+
const headers = new Headers();
|
|
2009
|
+
for (const [name, value] of Object.entries(request.headers)) {
|
|
2010
|
+
if (value === void 0 || NOT_FORWARDED.has(name)) continue;
|
|
2011
|
+
if (name.startsWith("sec-")) continue;
|
|
2012
|
+
headers.set(name, Array.isArray(value) ? value.join(", ") : value);
|
|
2013
|
+
}
|
|
2014
|
+
const cookie = url.searchParams.get("cookie");
|
|
2015
|
+
if (cookie) headers.set("cookie", cookie);
|
|
2016
|
+
const bodyless = request.method === "GET" || request.method === "HEAD";
|
|
2017
|
+
try {
|
|
2018
|
+
const upstream = await fetch(target, {
|
|
2019
|
+
method: request.method,
|
|
2020
|
+
headers,
|
|
2021
|
+
body: bodyless ? void 0 : body,
|
|
2022
|
+
redirect: "manual"
|
|
2023
|
+
});
|
|
2024
|
+
const returned = {};
|
|
2025
|
+
upstream.headers.forEach((value, name) => {
|
|
2026
|
+
if (!NOT_RETURNED.has(name)) returned[name] = value;
|
|
2027
|
+
});
|
|
2028
|
+
send(response, upstream.status, new Uint8Array(await upstream.arrayBuffer()), returned);
|
|
2029
|
+
} catch {
|
|
2030
|
+
send(response, 502, "Could not reach the app.");
|
|
2031
|
+
}
|
|
1681
2032
|
}
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
|
|
2033
|
+
function eventStream(request, response) {
|
|
2034
|
+
response.writeHead(200, {
|
|
2035
|
+
"content-type": "text/event-stream",
|
|
2036
|
+
"cache-control": "no-cache",
|
|
2037
|
+
connection: "keep-alive",
|
|
2038
|
+
"x-accel-buffering": "no"
|
|
2039
|
+
});
|
|
2040
|
+
const write = (chunk) => {
|
|
2041
|
+
if (response.writableEnded || response.destroyed) return;
|
|
2042
|
+
response.write(chunk);
|
|
2043
|
+
};
|
|
2044
|
+
write(`event: snapshot\ndata: ${JSON.stringify(snapshot())}\n\n`);
|
|
2045
|
+
const unsubscribe = options.store.subscribe((event) => write(`event: ${event.type}\ndata: ${JSON.stringify(event)}\n\n`));
|
|
2046
|
+
const unwatch = extension?.onChange(() => write(`event: extension\ndata: ${JSON.stringify(snapshot().extension)}\n\n`));
|
|
2047
|
+
const keepalive = setInterval(() => write(": keepalive\n\n"), KEEPALIVE_MS);
|
|
2048
|
+
request.on("close", () => {
|
|
2049
|
+
clearInterval(keepalive);
|
|
2050
|
+
unsubscribe();
|
|
2051
|
+
unwatch?.();
|
|
2052
|
+
});
|
|
1687
2053
|
}
|
|
1688
|
-
|
|
1689
|
-
|
|
1690
|
-
|
|
2054
|
+
return (extension ? Promise.all([extension.load(), options.plugins && extension.setPlugins(options.plugins)]) : Promise.resolve()).then(() => listen());
|
|
2055
|
+
function listen() {
|
|
2056
|
+
return new Promise((resolve, reject) => {
|
|
2057
|
+
server.once("error", (error) => {
|
|
2058
|
+
extension?.close();
|
|
2059
|
+
reject(new Error(production ? `Brick: dashboard port ${options.port} is unavailable. Set dashboard.port in brick() to another.` : `Brick: dashboard port ${options.port} is unavailable. Use --dashboard-port <number> or --no-dashboard.`, { cause: error }));
|
|
2060
|
+
});
|
|
2061
|
+
server.listen(options.port, host, () => {
|
|
2062
|
+
const address = server.address();
|
|
2063
|
+
const port = typeof address === "object" && address !== null ? address.port : options.port;
|
|
2064
|
+
allowedHosts = /* @__PURE__ */ new Set([
|
|
2065
|
+
`127.0.0.1:${port}`,
|
|
2066
|
+
`localhost:${port}`,
|
|
2067
|
+
`[::1]:${port}`
|
|
2068
|
+
]);
|
|
2069
|
+
if (!production) extension?.watch();
|
|
2070
|
+
resolve({
|
|
2071
|
+
port,
|
|
2072
|
+
setPluginExtensions: (plugins) => extension?.setPlugins(plugins) ?? Promise.resolve(),
|
|
2073
|
+
stop() {
|
|
2074
|
+
extension?.close();
|
|
2075
|
+
server.close();
|
|
2076
|
+
server.closeAllConnections();
|
|
2077
|
+
}
|
|
2078
|
+
});
|
|
2079
|
+
});
|
|
2080
|
+
});
|
|
1691
2081
|
}
|
|
2082
|
+
}
|
|
2083
|
+
/** The whole request body, or undefined when it is larger than `limit`. */
|
|
2084
|
+
function readBytes(request, limit) {
|
|
2085
|
+
return new Promise((resolve) => {
|
|
2086
|
+
const chunks = [];
|
|
2087
|
+
let size = 0;
|
|
2088
|
+
request.on("data", (chunk) => {
|
|
2089
|
+
size += chunk.length;
|
|
2090
|
+
if (size > limit) {
|
|
2091
|
+
resolve(void 0);
|
|
2092
|
+
request.destroy();
|
|
2093
|
+
return;
|
|
2094
|
+
}
|
|
2095
|
+
chunks.push(chunk);
|
|
2096
|
+
});
|
|
2097
|
+
request.on("end", () => resolve(new Uint8Array(Buffer.concat(chunks))));
|
|
2098
|
+
request.on("error", () => resolve(void 0));
|
|
2099
|
+
});
|
|
2100
|
+
}
|
|
2101
|
+
function history(store, pathname, params, response) {
|
|
2102
|
+
const json = (body) => send(response, 200, JSON.stringify(body), { "content-type": "application/json" });
|
|
2103
|
+
try {
|
|
2104
|
+
if (pathname === "/api/history") return json(store.info());
|
|
2105
|
+
if (pathname === "/api/history/summary") return json(store.summary(parseHistoryQuery(params)));
|
|
2106
|
+
if (pathname === "/api/history/traces") return json(store.traces(parseHistoryQuery(params)));
|
|
2107
|
+
if (pathname === "/api/history/facets") return json(store.facets(parseHistoryQuery(params)));
|
|
2108
|
+
if (pathname === "/api/history/volume") return json(store.volume(parseHistoryQuery(params)));
|
|
2109
|
+
if (pathname === "/api/history/export") return send(response, 200, store.exportCsv(parseHistoryQuery(params)), {
|
|
2110
|
+
"content-type": "text/csv; charset=utf-8",
|
|
2111
|
+
"content-disposition": "attachment; filename=\"brick-requests.csv\"",
|
|
2112
|
+
"cache-control": "no-store"
|
|
2113
|
+
});
|
|
2114
|
+
if (pathname.startsWith("/api/history/traces/")) {
|
|
2115
|
+
const trace = store.trace(decodeURIComponent(pathname.slice(20)));
|
|
2116
|
+
if (trace) return json(trace);
|
|
2117
|
+
}
|
|
2118
|
+
send(response, 404, "Not found");
|
|
2119
|
+
} catch {
|
|
2120
|
+
send(response, 500, "History query failed");
|
|
2121
|
+
}
|
|
2122
|
+
}
|
|
2123
|
+
const ASSET_TYPES = {
|
|
2124
|
+
".js": "text/javascript; charset=utf-8",
|
|
2125
|
+
".css": "text/css; charset=utf-8",
|
|
2126
|
+
".woff2": "font/woff2"
|
|
1692
2127
|
};
|
|
2128
|
+
function serveAsset(response, name) {
|
|
2129
|
+
if (!/^[\w.-]+$/.test(name) || name.startsWith(".")) {
|
|
2130
|
+
send(response, 404, "Not found");
|
|
2131
|
+
return;
|
|
2132
|
+
}
|
|
2133
|
+
const directory = assetsDirectory();
|
|
2134
|
+
if (!directory) {
|
|
2135
|
+
send(response, 503, "Dashboard assets are missing. Build them with: bun run --filter @brickkit/cli build");
|
|
2136
|
+
return;
|
|
2137
|
+
}
|
|
2138
|
+
readFile(join(directory, name)).then((body) => send(response, 200, body, {
|
|
2139
|
+
"content-type": ASSET_TYPES[extname(name)] ?? "application/octet-stream",
|
|
2140
|
+
"cache-control": "no-store"
|
|
2141
|
+
}), () => send(response, 404, "Not found"));
|
|
2142
|
+
}
|
|
2143
|
+
function send(response, status, body, headers = { "content-type": "text/plain; charset=utf-8" }) {
|
|
2144
|
+
response.writeHead(status, headers);
|
|
2145
|
+
response.end(body);
|
|
2146
|
+
}
|
|
1693
2147
|
//#endregion
|
|
1694
2148
|
//#region src/dev-telemetry-store.ts
|
|
1695
2149
|
const LIMITS = {
|
|
@@ -2015,6 +2469,7 @@ function summarize(entry) {
|
|
|
2015
2469
|
service: stringAttribute(actionSpan?.attributes, "brick.service"),
|
|
2016
2470
|
action: stringAttribute(actionSpan?.attributes, "brick.action"),
|
|
2017
2471
|
message: entry.message,
|
|
2472
|
+
...entry.spans.get(entry.rootSpanId ?? "")?.attributes["brick.legacy_route"] === true ? { legacyRoute: true } : {},
|
|
2018
2473
|
spans
|
|
2019
2474
|
};
|
|
2020
2475
|
}
|
|
@@ -2035,6 +2490,6 @@ function numberAttribute(attributes, key) {
|
|
|
2035
2490
|
return typeof value === "number" ? value : void 0;
|
|
2036
2491
|
}
|
|
2037
2492
|
//#endregion
|
|
2038
|
-
export {
|
|
2493
|
+
export { writeDashboardExtension as a, openHistory as c, themeCss as i, startInspector as l, DEFAULT_DASHBOARD_PORT as n, DocsHost as o, startDevDashboard as r, isLoopbackHost as s, DevTelemetryStore as t };
|
|
2039
2494
|
|
|
2040
2495
|
//# sourceMappingURL=dev-telemetry-store.js.map
|