@dooc-dev/dooc 0.0.7 → 0.0.9
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/LICENSE +21 -0
- package/README.md +16 -18
- package/dist/client.global.js +1 -0
- package/dist/code-worker.global.js +11 -11
- package/dist/code.global.js +75 -75
- package/dist/edit.global.js +234 -231
- package/dist/{compiler → engine/compiler}/bundle-urls.js +1 -4
- package/dist/{compiler → engine/compiler}/check-prose.d.ts +1 -1
- package/dist/{compiler → engine/compiler}/check-prose.js +1 -1
- package/dist/{compiler → engine/compiler}/check.js +15 -27
- package/dist/{compiler → engine/compiler}/code-blocks.js +3 -1
- package/dist/{compiler → engine/compiler}/code-languages.js +1 -2
- package/dist/{compiler → engine/compiler}/compile.d.ts +5 -4
- package/dist/{compiler → engine/compiler}/compile.js +91 -32
- package/dist/{compiler → engine/compiler}/config.js +1 -3
- package/dist/{compiler → engine/compiler}/dialect.d.ts +1 -3
- package/dist/{compiler → engine/compiler}/dialect.js +8 -52
- package/dist/{compiler → engine/compiler}/dxf.js +1 -1
- package/dist/{compiler → engine/compiler}/export.js +15 -8
- package/dist/{compiler → engine/compiler}/figures.js +26 -22
- package/dist/{compiler → engine/compiler}/file-cache.d.ts +1 -2
- package/dist/{compiler → engine/compiler}/file-cache.js +8 -15
- package/dist/{compiler → engine/compiler}/files.js +49 -38
- package/dist/{compiler → engine/compiler}/fonts.js +1 -3
- package/dist/{compiler → engine/compiler}/from-mdast.js +1 -1
- package/dist/engine/compiler/host.d.ts +10 -0
- package/dist/engine/compiler/host.js +16 -0
- package/dist/{compiler → engine/compiler}/icons.d.ts +3 -0
- package/dist/{compiler → engine/compiler}/icons.js +28 -10
- package/dist/{compiler → engine/compiler}/ignored-files.d.ts +2 -0
- package/dist/{compiler → engine/compiler}/ignored-files.js +12 -3
- package/dist/{compiler → engine/compiler}/locales.js +2 -8
- package/dist/{compiler → engine/compiler}/manifest.d.ts +3 -2
- package/dist/engine/compiler/manifest.js +46 -0
- package/dist/{compiler → engine/compiler}/ops.d.ts +4 -1
- package/dist/{compiler → engine/compiler}/ops.js +36 -21
- package/dist/engine/compiler/schema-file-references.d.ts +2 -0
- package/dist/engine/compiler/schema-file-references.js +82 -0
- package/dist/engine/compiler/serialize.d.ts +2 -0
- package/dist/engine/compiler/serialize.js +5 -0
- package/dist/{compiler → engine/compiler}/subformat-schema.js +1 -3
- package/dist/{compiler → engine/compiler}/subformats.d.ts +10 -0
- package/dist/{compiler → engine/compiler}/subformats.js +122 -18
- package/dist/{compiler → engine/compiler}/textfile-remote.js +1 -2
- package/dist/{compiler → engine/compiler}/textfile.js +4 -6
- package/dist/{compiler → engine/compiler}/theme-variants.js +1 -1
- package/dist/{compiler → engine/compiler}/zip.js +1 -1
- package/dist/{core → engine/core}/blocks.d.ts +6 -4
- package/dist/{core → engine/core}/content-metadata.d.ts +6 -1
- package/dist/{core → engine/core}/content-metadata.js +84 -18
- package/dist/{core → engine/core}/content-types.d.ts +4 -2
- package/dist/{core → engine/core}/html.d.ts +7 -0
- package/dist/{core → engine/core}/html.js +44 -3
- package/dist/{core → engine/core}/icons.d.ts +3 -0
- package/dist/engine/core/icons.js +20 -0
- package/dist/{core → engine/core}/routing.d.ts +2 -0
- package/dist/{core → engine/core}/routing.js +9 -0
- package/dist/{core → engine/core}/schema-refs.d.ts +1 -1
- package/dist/{core → engine/core}/schema-refs.js +3 -1
- package/dist/engine/core/subformat-references.d.ts +5 -0
- package/dist/engine/core/subformat-references.js +33 -0
- package/dist/{core → engine/core}/subformats.d.ts +7 -0
- package/dist/{core → engine/core}/subformats.js +5 -1
- package/dist/{core → engine/core}/taxonomy.d.ts +2 -3
- package/dist/{core → engine/core}/taxonomy.js +1 -5
- package/dist/flow.global.js +306 -306
- package/dist/frontend/application-request.d.ts +14 -0
- package/dist/frontend/application-request.js +36 -0
- package/dist/frontend/document-application.d.ts +200 -0
- package/dist/{viewer/serve.js → frontend/document-application.js} +1611 -1670
- package/dist/frontend/figure-preview.d.ts +4 -0
- package/dist/{viewer → frontend}/figure-preview.js +4 -4
- package/dist/{viewer → frontend}/file-references.d.ts +1 -1
- package/dist/{viewer → frontend}/file-references.js +3 -3
- package/dist/{viewer → frontend}/font-picker.d.ts +1 -1
- package/dist/{viewer → frontend}/font-settings.d.ts +1 -1
- package/dist/{viewer → frontend}/language-picker.js +112 -18
- package/dist/{viewer → frontend}/locales/eng.json +16 -2
- package/dist/{viewer → frontend}/locales/jpn.json +16 -2
- package/dist/{viewer → frontend}/locales/zho.json +16 -2
- package/dist/{viewer → frontend}/nav-focus.js +2 -0
- package/dist/{viewer → frontend}/plugin-blocks.js +2 -2
- package/dist/{viewer → frontend}/resize.js +14 -2
- package/dist/frontend/resource-download.d.ts +2 -0
- package/dist/frontend/resource-download.js +43 -0
- package/dist/{viewer → frontend}/resource-library.d.ts +9 -2
- package/dist/{viewer → frontend}/resource-library.js +133 -37
- package/dist/{core → frontend}/schema-graph-flow.d.ts +1 -1
- package/dist/{core → frontend}/schema-graph-flow.js +1 -1
- package/dist/{viewer → frontend}/search.d.ts +2 -2
- package/dist/{viewer → frontend}/search.js +26 -8
- package/dist/graph/schema-graph-global.js +17 -17
- package/dist/install.sh +68 -0
- package/dist/plugins.global.js +62 -62
- package/dist/{viewer → server}/assets.d.ts +2 -0
- package/dist/server/assets.js +7 -0
- package/dist/{viewer → server}/build-plugins.js +4 -4
- package/dist/{viewer → server}/build.d.ts +2 -1
- package/dist/{viewer → server}/build.js +31 -15
- package/dist/{cli.js → server/cli.js} +199 -66
- package/dist/server/editor-tools.d.ts +1 -0
- package/dist/server/editor-tools.js +1 -0
- package/dist/{viewer → server}/extension-pack.js +2 -2
- package/dist/{viewer → server}/extensions.d.ts +1 -1
- package/dist/{viewer → server}/extensions.js +3 -3
- package/dist/server/git-diff.d.ts +10 -0
- package/dist/{viewer → server}/git-diff.js +45 -10
- package/dist/server/git-origin.d.ts +4 -0
- package/dist/server/git-origin.js +103 -0
- package/dist/server/git.d.ts +9 -0
- package/dist/{viewer → server}/git.js +50 -6
- package/dist/server/history.d.ts +14 -0
- package/dist/server/history.js +205 -0
- package/dist/{viewer → server}/hmr.d.ts +0 -22
- package/dist/server/hmr.js +168 -0
- package/dist/server/index.d.ts +17 -0
- package/dist/{index.js → server/index.js} +16 -16
- package/dist/{viewer → server}/ja-lint.d.ts +1 -1
- package/dist/{viewer → server}/languages.d.ts +1 -1
- package/dist/{viewer → server}/languages.js +3 -3
- package/dist/{mcp.d.ts → server/mcp.d.ts} +2 -2
- package/dist/{mcp.js → server/mcp.js} +60 -21
- package/dist/{viewer → server}/plugin-config.d.ts +2 -2
- package/dist/{viewer → server}/plugin-config.js +2 -2
- package/dist/{plugins → server/plugins}/local-storage.d.ts +1 -1
- package/dist/{plugins → server/plugins}/local-storage.js +1 -1
- package/dist/{plugins → server/plugins}/runtime.d.ts +4 -4
- package/dist/{plugins → server/plugins}/runtime.js +4 -4
- package/dist/{viewer → server}/plugins.d.ts +3 -3
- package/dist/{viewer → server}/plugins.js +14 -13
- package/dist/server/resource-library.d.ts +1 -0
- package/dist/server/resource-library.js +1 -0
- package/dist/server/self.d.ts +5 -0
- package/dist/server/self.js +98 -0
- package/dist/server/serve.d.ts +61 -0
- package/dist/server/serve.js +503 -0
- package/dist/server/static-client.d.ts +4 -0
- package/dist/server/static-client.js +117 -0
- package/dist/server/workspace-server.d.ts +16 -0
- package/dist/server/workspace-server.js +266 -0
- package/dist/{viewer → server}/workspace.d.ts +6 -1
- package/dist/{viewer → server}/workspace.js +46 -0
- package/dist/subformats.global.js +92 -6
- package/dist/ui.global.js +85 -72
- package/dist/workspace/client.d.ts +13 -0
- package/dist/workspace/client.js +114 -0
- package/dist/{viewer → workspace}/code-file.d.ts +1 -1
- package/dist/{viewer → workspace}/code-file.js +7 -7
- package/dist/workspace/connection.d.ts +27 -0
- package/dist/workspace/connection.js +38 -0
- package/dist/{viewer → workspace}/editor-tools.d.ts +7 -1
- package/dist/{viewer → workspace}/editor-tools.js +12 -9
- package/dist/{viewer → workspace}/git-types.d.ts +7 -0
- package/dist/workspace/github.d.ts +4 -0
- package/dist/workspace/github.js +99 -0
- package/dist/workspace/history.d.ts +46 -0
- package/dist/workspace/history.js +86 -0
- package/dist/workspace/protocol.d.ts +38 -0
- package/dist/workspace/protocol.js +20 -0
- package/dist/workspace/resource-download.d.ts +1 -0
- package/dist/workspace/resource-download.js +8 -0
- package/dist/workspace/server.d.ts +9 -0
- package/dist/workspace/server.js +48 -0
- package/dist/{viewer → workspace}/site-icon.js +9 -10
- package/dist/{viewer → workspace}/site-title.js +6 -7
- package/dist/{viewer → workspace}/textfile.d.ts +1 -1
- package/dist/{viewer → workspace}/textfile.js +5 -5
- package/dist/workspace-worker.global.js +2859 -0
- package/package.json +35 -20
- package/dist/compiler/manifest.js +0 -37
- package/dist/compiler/serialize.d.ts +0 -3
- package/dist/compiler/serialize.js +0 -16
- package/dist/core/icons.js +0 -5
- package/dist/index.d.ts +0 -17
- package/dist/viewer/assets.js +0 -3
- package/dist/viewer/figure-preview.d.ts +0 -4
- package/dist/viewer/git-diff.d.ts +0 -6
- package/dist/viewer/git.d.ts +0 -5
- package/dist/viewer/hmr.js +0 -341
- package/dist/viewer/serve.d.ts +0 -216
- /package/dist/{compiler → engine/compiler}/asset-references.d.ts +0 -0
- /package/dist/{compiler → engine/compiler}/asset-references.js +0 -0
- /package/dist/{compiler → engine/compiler}/bundle-urls.d.ts +0 -0
- /package/dist/{compiler → engine/compiler}/check-types.d.ts +0 -0
- /package/dist/{compiler → engine/compiler}/check-types.js +0 -0
- /package/dist/{compiler → engine/compiler}/check.d.ts +0 -0
- /package/dist/{compiler → engine/compiler}/code-blocks.d.ts +0 -0
- /package/dist/{compiler → engine/compiler}/code-languages.d.ts +0 -0
- /package/dist/{compiler → engine/compiler}/config.d.ts +0 -0
- /package/dist/{compiler → engine/compiler}/drawio.d.ts +0 -0
- /package/dist/{compiler → engine/compiler}/drawio.js +0 -0
- /package/dist/{compiler → engine/compiler}/dxf.d.ts +0 -0
- /package/dist/{compiler → engine/compiler}/export.d.ts +0 -0
- /package/dist/{compiler → engine/compiler}/figures.d.ts +0 -0
- /package/dist/{compiler → engine/compiler}/files.d.ts +0 -0
- /package/dist/{compiler → engine/compiler}/fonts.d.ts +0 -0
- /package/dist/{compiler → engine/compiler}/format.d.ts +0 -0
- /package/dist/{compiler → engine/compiler}/format.js +0 -0
- /package/dist/{compiler → engine/compiler}/from-mdast.d.ts +0 -0
- /package/dist/{compiler → engine/compiler}/language-codes.d.ts +0 -0
- /package/dist/{compiler → engine/compiler}/language-codes.js +0 -0
- /package/dist/{compiler → engine/compiler}/locales.d.ts +0 -0
- /package/dist/{compiler → engine/compiler}/public-file.d.ts +0 -0
- /package/dist/{compiler → engine/compiler}/public-file.js +0 -0
- /package/dist/{compiler → engine/compiler}/subformat-schema.d.ts +0 -0
- /package/dist/{compiler → engine/compiler}/terms.d.ts +0 -0
- /package/dist/{compiler → engine/compiler}/terms.js +0 -0
- /package/dist/{compiler → engine/compiler}/textfile-remote.d.ts +0 -0
- /package/dist/{compiler → engine/compiler}/textfile.d.ts +0 -0
- /package/dist/{compiler → engine/compiler}/theme-variants.d.ts +0 -0
- /package/dist/{compiler → engine/compiler}/zip.d.ts +0 -0
- /package/dist/{core → engine/core}/blocks.js +0 -0
- /package/dist/{core → engine/core}/code-file.d.ts +0 -0
- /package/dist/{core → engine/core}/code-file.js +0 -0
- /package/dist/{core → engine/core}/code-grammars.d.ts +0 -0
- /package/dist/{core → engine/core}/code-grammars.js +0 -0
- /package/dist/{core → engine/core}/content-types.js +0 -0
- /package/dist/{core → engine/core}/escape-html.d.ts +0 -0
- /package/dist/{core → engine/core}/escape-html.js +0 -0
- /package/dist/{core → engine/core}/extension-package.d.ts +0 -0
- /package/dist/{core → engine/core}/extension-package.js +0 -0
- /package/dist/{core → engine/core}/figure-frame.d.ts +0 -0
- /package/dist/{core → engine/core}/figure-frame.js +0 -0
- /package/dist/{core → engine/core}/file-embed.d.ts +0 -0
- /package/dist/{core → engine/core}/file-embed.js +0 -0
- /package/dist/{core → engine/core}/files.d.ts +0 -0
- /package/dist/{core → engine/core}/files.js +0 -0
- /package/dist/{core → engine/core}/graph-common.d.ts +0 -0
- /package/dist/{core → engine/core}/graph-common.js +0 -0
- /package/dist/{core → engine/core}/graph.d.ts +0 -0
- /package/dist/{core → engine/core}/graph.js +0 -0
- /package/dist/{core → engine/core}/inline.d.ts +0 -0
- /package/dist/{core → engine/core}/inline.js +0 -0
- /package/dist/{core → engine/core}/json-tools.d.ts +0 -0
- /package/dist/{core → engine/core}/json-tools.js +0 -0
- /package/dist/{core → engine/core}/language.d.ts +0 -0
- /package/dist/{core → engine/core}/language.js +0 -0
- /package/dist/{core → engine/core}/languages/ja.d.ts +0 -0
- /package/dist/{core → engine/core}/languages/ja.js +0 -0
- /package/dist/{core → engine/core}/markdown.d.ts +0 -0
- /package/dist/{core → engine/core}/markdown.js +0 -0
- /package/dist/{core → engine/core}/previewers.d.ts +0 -0
- /package/dist/{core → engine/core}/previewers.js +0 -0
- /package/dist/{core → engine/core}/schema-comments.d.ts +0 -0
- /package/dist/{core → engine/core}/schema-comments.js +0 -0
- /package/dist/{core → engine/core}/schema-fields.d.ts +0 -0
- /package/dist/{core → engine/core}/schema-fields.js +0 -0
- /package/dist/{core → engine/core}/schema-graph.d.ts +0 -0
- /package/dist/{core → engine/core}/schema-graph.js +0 -0
- /package/dist/{core → engine/core}/subformat-record.d.ts +0 -0
- /package/dist/{core → engine/core}/subformat-record.js +0 -0
- /package/dist/{core → engine/core}/term-reference.d.ts +0 -0
- /package/dist/{core → engine/core}/term-reference.js +0 -0
- /package/dist/{core → engine/core}/textfile.d.ts +0 -0
- /package/dist/{core → engine/core}/textfile.js +0 -0
- /package/dist/{core → engine/core}/theme-variants.d.ts +0 -0
- /package/dist/{core → engine/core}/theme-variants.js +0 -0
- /package/dist/{core → engine/core}/workspace.d.ts +0 -0
- /package/dist/{core → engine/core}/workspace.js +0 -0
- /package/dist/{viewer → frontend}/bundle-network.d.ts +0 -0
- /package/dist/{viewer → frontend}/bundle-network.js +0 -0
- /package/dist/{core → frontend}/flow-base-css.generated.d.ts +0 -0
- /package/dist/{core → frontend}/flow-base-css.generated.js +0 -0
- /package/dist/{core → frontend}/flow-css.d.ts +0 -0
- /package/dist/{core → frontend}/flow-css.js +0 -0
- /package/dist/{viewer → frontend}/font-picker.js +0 -0
- /package/dist/{viewer → frontend}/font-settings.js +0 -0
- /package/dist/{viewer → frontend}/i18n.d.ts +0 -0
- /package/dist/{viewer → frontend}/i18n.js +0 -0
- /package/dist/{viewer → frontend}/language-picker.d.ts +0 -0
- /package/dist/{viewer → frontend}/lazy-preview.d.ts +0 -0
- /package/dist/{viewer → frontend}/lazy-preview.js +0 -0
- /package/dist/{viewer → frontend}/nav-focus.d.ts +0 -0
- /package/dist/{viewer → frontend}/nav-update.d.ts +0 -0
- /package/dist/{viewer → frontend}/nav-update.js +0 -0
- /package/dist/{viewer → frontend}/plugin-blocks.d.ts +0 -0
- /package/dist/{viewer → frontend}/plugin-style.d.ts +0 -0
- /package/dist/{viewer → frontend}/plugin-style.js +0 -0
- /package/dist/{viewer → frontend}/plugin-view.d.ts +0 -0
- /package/dist/{viewer → frontend}/plugin-view.js +0 -0
- /package/dist/{viewer → frontend}/resize.d.ts +0 -0
- /package/dist/{core → frontend}/schema-graph-mount.d.ts +0 -0
- /package/dist/{core → frontend}/schema-graph-mount.js +0 -0
- /package/dist/{core → frontend}/table-ui.d.ts +0 -0
- /package/dist/{core → frontend}/table-ui.js +0 -0
- /package/dist/{core → frontend}/terms-ui.d.ts +0 -0
- /package/dist/{core → frontend}/terms-ui.js +0 -0
- /package/dist/{viewer → frontend}/theme-picker.d.ts +0 -0
- /package/dist/{viewer → frontend}/theme-picker.js +0 -0
- /package/dist/{core → frontend}/toc-ui.d.ts +0 -0
- /package/dist/{core → frontend}/toc-ui.js +0 -0
- /package/dist/{core → frontend}/video-ui.d.ts +0 -0
- /package/dist/{core → frontend}/video-ui.js +0 -0
- /package/dist/{viewer → server}/build-plugins.d.ts +0 -0
- /package/dist/{cli.d.ts → server/cli.d.ts} +0 -0
- /package/dist/{viewer → server}/extension-pack.d.ts +0 -0
- /package/dist/{viewer → server}/fonts/LICENSE.txt +0 -0
- /package/dist/{viewer → server}/fonts/udev-gothic-regular.woff2 +0 -0
- /package/dist/{viewer → server}/fonts.d.ts +0 -0
- /package/dist/{viewer → server}/fonts.js +0 -0
- /package/dist/{viewer → server}/ja-lint.js +0 -0
- /package/dist/{viewer → workspace}/git-types.js +0 -0
- /package/dist/{viewer → workspace}/site-icon.d.ts +0 -0
- /package/dist/{viewer → workspace}/site-title.d.ts +0 -0
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { execFileSync } from "node:child_process";
|
|
2
|
+
import { existsSync, mkdirSync, mkdtempSync, realpathSync, rmSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import { dirname, join } from "node:path";
|
|
5
|
+
import { fileURLToPath } from "node:url";
|
|
6
|
+
import { unzipSync } from "fflate";
|
|
7
|
+
import { prebuilt, readInstaller, standalone } from "./assets.js";
|
|
8
|
+
export const selfUpdate = () => {
|
|
9
|
+
if (!standalone) {
|
|
10
|
+
const hint = "Update with your package manager (npm: npm install -g @dooc-dev/dooc@latest).";
|
|
11
|
+
if (!prebuilt)
|
|
12
|
+
throw new Error(`Cannot self-update a source checkout. ${hint}`);
|
|
13
|
+
const globalRoot = execFileSync("npm", ["root", "--global"], { encoding: "utf8" }).trim();
|
|
14
|
+
const installed = join(globalRoot, "@dooc-dev/dooc");
|
|
15
|
+
if (!existsSync(installed) ||
|
|
16
|
+
realpathSync(fileURLToPath(new URL("../..", import.meta.url))) !==
|
|
17
|
+
realpathSync(installed))
|
|
18
|
+
throw new Error(`This is not a global npm installation. ${hint}`);
|
|
19
|
+
execFileSync("npm", ["install", "--global", "@dooc-dev/dooc@latest"], { stdio: "inherit" });
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
const executable = realpathSync(process.execPath);
|
|
23
|
+
if (executable.includes("/Cellar/"))
|
|
24
|
+
throw new Error("This installation is managed by Homebrew. Run: brew upgrade dooc-dev/dooc/dooc");
|
|
25
|
+
if (executable.includes("/mise/installs/"))
|
|
26
|
+
throw new Error("This installation is managed by mise. Run: mise upgrade github:dooc-dev/dooc");
|
|
27
|
+
execFileSync("sh", ["-s"], {
|
|
28
|
+
input: readInstaller(),
|
|
29
|
+
stdio: ["pipe", "inherit", "inherit"],
|
|
30
|
+
env: { ...process.env, DOOC_VERSION: "latest", DOOC_INSTALL_PATH: executable },
|
|
31
|
+
});
|
|
32
|
+
};
|
|
33
|
+
export const prepareSelfDocs = async () => {
|
|
34
|
+
let root;
|
|
35
|
+
const dispose = () => {
|
|
36
|
+
if (root)
|
|
37
|
+
rmSync(root, { recursive: true, force: true });
|
|
38
|
+
};
|
|
39
|
+
try {
|
|
40
|
+
const response = await fetch("https://codeload.github.com/dooc-dev/dooc/zip/refs/heads/main", {
|
|
41
|
+
signal: AbortSignal.timeout(60_000),
|
|
42
|
+
});
|
|
43
|
+
if (!response.ok)
|
|
44
|
+
throw new Error(`HTTP ${response.status} ${response.statusText}`);
|
|
45
|
+
if (!response.body)
|
|
46
|
+
throw new Error("Empty archive response");
|
|
47
|
+
const chunks = [];
|
|
48
|
+
let downloaded = 0;
|
|
49
|
+
await response.body.pipeTo(new WritableStream({
|
|
50
|
+
write(chunk) {
|
|
51
|
+
downloaded += chunk.length;
|
|
52
|
+
if (downloaded > 128 * 1024 * 1024)
|
|
53
|
+
throw new Error("Documentation archive exceeds 128 MiB");
|
|
54
|
+
chunks.push(chunk);
|
|
55
|
+
},
|
|
56
|
+
}));
|
|
57
|
+
const prefix = "dooc-main/dooc-docs/";
|
|
58
|
+
const paths = new Set();
|
|
59
|
+
let extracted = 0;
|
|
60
|
+
const files = unzipSync(Buffer.concat(chunks), {
|
|
61
|
+
filter: ({ name, size, originalSize, compression }) => {
|
|
62
|
+
if (!name.startsWith(prefix) || name.endsWith("/"))
|
|
63
|
+
return false;
|
|
64
|
+
if (compression === 0 && size !== originalSize)
|
|
65
|
+
throw new Error("Invalid documentation file size");
|
|
66
|
+
const path = name.slice(prefix.length);
|
|
67
|
+
const parts = path.split("/");
|
|
68
|
+
if (/[\\:]/.test(path) ||
|
|
69
|
+
[...path].some((char) => char.charCodeAt(0) < 32 || char.charCodeAt(0) === 127) ||
|
|
70
|
+
parts.some((part) => !part || part === "." || part === ".." || /[. ]$/.test(part)))
|
|
71
|
+
throw new Error("Invalid documentation path");
|
|
72
|
+
if (parts.some((part) => part.startsWith(".")))
|
|
73
|
+
return false;
|
|
74
|
+
const key = path.normalize("NFC").toLowerCase();
|
|
75
|
+
if (paths.has(key))
|
|
76
|
+
throw new Error("Duplicate documentation path");
|
|
77
|
+
paths.add(key);
|
|
78
|
+
extracted += originalSize;
|
|
79
|
+
if (paths.size > 4096 || extracted > 256 * 1024 * 1024)
|
|
80
|
+
throw new Error("Documentation exceeds 4096 files or 256 MiB");
|
|
81
|
+
return true;
|
|
82
|
+
},
|
|
83
|
+
});
|
|
84
|
+
if (!files[`${prefix}dooc.yaml`]?.length)
|
|
85
|
+
throw new Error("GitHub main does not contain dooc-docs/dooc.yaml");
|
|
86
|
+
root = mkdtempSync(join(tmpdir(), "dooc-docs-"));
|
|
87
|
+
for (const [name, data] of Object.entries(files)) {
|
|
88
|
+
const path = join(root, name.slice(prefix.length));
|
|
89
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
90
|
+
writeFileSync(path, data, { flag: "wx" });
|
|
91
|
+
}
|
|
92
|
+
return { root, dispose };
|
|
93
|
+
}
|
|
94
|
+
catch (error) {
|
|
95
|
+
dispose();
|
|
96
|
+
throw new Error(`Failed to download documentation from GitHub: ${error instanceof Error ? error.message : String(error)}`, { cause: error });
|
|
97
|
+
}
|
|
98
|
+
};
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { assembleDoc } from "../engine/compiler/ops.js";
|
|
2
|
+
import { type ServeMode } from "../frontend/document-application.js";
|
|
3
|
+
import { type ViewerRuntime } from "./plugin-config.js";
|
|
4
|
+
export type { EditorBoot, Entry, ServeMode, Site, } from "../frontend/document-application.js";
|
|
5
|
+
export { assembleDoc };
|
|
6
|
+
export declare const siteFrom: ({ title, icon, iconVariants, docs, terms, figures, sources, siteErrors, defaultLang, previews, contentLanguages, themes, subformats, }: import("../engine/compiler/compile.js").Compiled, languages?: readonly import("./index.js").LanguageAdapter[], code?: Pick<import("./languages.js").SiteLanguages, "codeLanguages" | "codeGrammars">) => import("../frontend/document-application.js").Site, filePreviewResults: (entry: import("../frontend/document-application.js").Entry, site: import("../frontend/document-application.js").Site) => import("../frontend/file-references.js").FilePreviewResults, renderEntryHtml: (site: import("../frontend/document-application.js").Site, entry: import("../frontend/document-application.js").Entry) => {
|
|
7
|
+
html: string;
|
|
8
|
+
errors: string[];
|
|
9
|
+
}, renderDocFile: (site: import("../frontend/document-application.js").Site, file: string, raw: string) => {
|
|
10
|
+
gitBase: string | null;
|
|
11
|
+
raw: string;
|
|
12
|
+
frontmatter: import("./index.js").Frontmatter | undefined;
|
|
13
|
+
body: string;
|
|
14
|
+
html: string;
|
|
15
|
+
errors: string[];
|
|
16
|
+
toc: import("./index.js").TocEntry[];
|
|
17
|
+
blockLines: number[];
|
|
18
|
+
filePreviews: import("../frontend/file-references.js").FilePreviewResults;
|
|
19
|
+
previews?: import("../engine/core/previewers.js").PreviewSettings;
|
|
20
|
+
sourceFile?: string;
|
|
21
|
+
}, docQueryResponse: (site: import("../frontend/document-application.js").Site, file: unknown) => {
|
|
22
|
+
status: number;
|
|
23
|
+
body: unknown;
|
|
24
|
+
}, sanitizeFrontmatter: (v: unknown) => import("./index.js").Frontmatter, isExternallyModified: (baseRaw: unknown, currentRaw: string) => boolean, resolveContentFile: (rel: unknown) => {
|
|
25
|
+
id: string;
|
|
26
|
+
name: string;
|
|
27
|
+
abs: string;
|
|
28
|
+
} | undefined, navGroupsHtml: (mode: ServeMode, site: import("../frontend/document-application.js").Site, currentId?: string, currentRes?: string) => string, textfilePreviewHtml: (site: import("../frontend/document-application.js").Site, file: string, source: string) => string, textfilePreviewErrors: (site: import("../frontend/document-application.js").Site, file: string, source: string) => string[], resourcesMain: (site: import("../frontend/document-application.js").Site, rest: string) => {
|
|
29
|
+
title: string;
|
|
30
|
+
main: string;
|
|
31
|
+
sourceFile?: string;
|
|
32
|
+
} | undefined, publishHtml: (site: import("../frontend/document-application.js").Site, graphJs: string, title?: string, codeJs?: string, mode?: "static" | "network" | "live", pluginJs?: string, subformatJs?: string) => string, languageFieldHtml: (defaultLang: string, language?: string, translatedLanguages?: readonly string[], declaredLanguages?: readonly string[]) => string, editorBoot: (entry: import("../frontend/document-application.js").Entry, site: import("../frontend/document-application.js").Site) => import("../frontend/document-application.js").EditorBoot, pageDataResponse: (site: import("../frontend/document-application.js").Site, path: unknown) => {
|
|
33
|
+
status: number;
|
|
34
|
+
body: unknown;
|
|
35
|
+
};
|
|
36
|
+
/** Cache of entry name → { js, etag }. clear() is called on HMR "client" notifications
|
|
37
|
+
* (changes to edit-client.ts / the schema graph interaction layer) (= invalidation is
|
|
38
|
+
* startServer's job; this is just the container). */
|
|
39
|
+
export interface BundleCache {
|
|
40
|
+
get: (entry: string, build: () => Promise<string>) => Promise<{
|
|
41
|
+
js: string;
|
|
42
|
+
etag: string;
|
|
43
|
+
}>;
|
|
44
|
+
clear: () => void;
|
|
45
|
+
}
|
|
46
|
+
export declare const createBundleCache: () => BundleCache;
|
|
47
|
+
/** Minified client bundle shared by local viewing and published HTML. */
|
|
48
|
+
export declare const bundleClient: (entry: string) => Promise<string>;
|
|
49
|
+
export declare const startServer: (mode: ServeMode, port?: number, pluginModule?: string, startup?: {
|
|
50
|
+
open?: boolean;
|
|
51
|
+
file?: string;
|
|
52
|
+
data?: string;
|
|
53
|
+
login?: string;
|
|
54
|
+
/** PEM file paths; with them the viewer is served over HTTPS (and the socket over WSS). */
|
|
55
|
+
tls?: {
|
|
56
|
+
cert: string;
|
|
57
|
+
key: string;
|
|
58
|
+
};
|
|
59
|
+
/** Branch that a shared workspace records its snapshots on. */
|
|
60
|
+
shared?: string;
|
|
61
|
+
}) => Promise<ViewerRuntime>;
|
|
@@ -0,0 +1,503 @@
|
|
|
1
|
+
import { completeFilePaths, filesSnapshot, updateFiles, uploadFile, } from "../engine/compiler/files.js";
|
|
2
|
+
import { MAX_FILE_UPLOAD_BYTES } from "../engine/core/files.js";
|
|
3
|
+
// Combined Node viewer: serves frontend assets and the shared document application.
|
|
4
|
+
// Workspace requests and change notifications travel over a same-origin WebSocket.
|
|
5
|
+
import { spawn } from "node:child_process";
|
|
6
|
+
import { createHash } from "node:crypto";
|
|
7
|
+
import { readFileSync } from "node:fs";
|
|
8
|
+
import { createServer } from "node:http";
|
|
9
|
+
import { createServer as createSecureServer } from "node:https";
|
|
10
|
+
import { basename, dirname, join, resolve } from "node:path";
|
|
11
|
+
import { fileURLToPath } from "node:url";
|
|
12
|
+
import { WebSocketServer } from "ws";
|
|
13
|
+
import { manifestFile } from "../engine/compiler/manifest.js";
|
|
14
|
+
import { assembleDoc } from "../engine/compiler/ops.js";
|
|
15
|
+
import { MAX_EXTENSION_BYTES } from "../engine/core/extension-package.js";
|
|
16
|
+
import { parsePreviewSettings } from "../engine/core/previewers.js";
|
|
17
|
+
import { parseWorkspaceSource } from "../engine/core/workspace.js";
|
|
18
|
+
import { createDocumentApplication, } from "../frontend/document-application.js";
|
|
19
|
+
import { createHistoryOp, parsePerson } from "../workspace/history.js";
|
|
20
|
+
import { WORKSPACE_PATH } from "../workspace/protocol.js";
|
|
21
|
+
import { createWorkspaceServer } from "../workspace/server.js";
|
|
22
|
+
import { readViewerAsset, prebuilt as usePrebuiltAssets } from "./assets.js";
|
|
23
|
+
import { isLocalHost, isLocalToolRequest, runCodeTool, validateCodeTools, validateTextfileCode, } from "./editor-tools.js";
|
|
24
|
+
import { createExtensionManager, EXTENSIONS_DIR } from "./extensions.js";
|
|
25
|
+
import { fontCss, readFont } from "./fonts.js";
|
|
26
|
+
import { createGitHandler, validateGitOrigin } from "./git.js";
|
|
27
|
+
import { gitBaseBody, gitBaseText, setSubformatChanges } from "./git-diff.js";
|
|
28
|
+
import { workspaceHistory } from "./history.js";
|
|
29
|
+
import { watchProject } from "./hmr.js";
|
|
30
|
+
import { loadSiteLanguages } from "./languages.js";
|
|
31
|
+
import { loadViewerPlugins } from "./plugin-config.js";
|
|
32
|
+
import { createLocalPluginStorage } from "./plugins/local-storage.js";
|
|
33
|
+
import { createPluginHandler } from "./plugins.js";
|
|
34
|
+
import { readWorkspaceSource, sourceFileBase } from "./workspace.js";
|
|
35
|
+
import { nodeWorkspaceOperations } from "./workspace-server.js";
|
|
36
|
+
export { assembleDoc };
|
|
37
|
+
// pkgRoot = the dooc package itself; client bundles are built from its frontend sources.
|
|
38
|
+
// docsRoot = the docs folder that follows the layout convention (content/ figures/).
|
|
39
|
+
const pkgRoot = join(dirname(fileURLToPath(import.meta.url)), usePrebuiltAssets ? "../.." : "../../..");
|
|
40
|
+
const frontendRoot = join(pkgRoot, "packages/frontend/src");
|
|
41
|
+
const root = resolve(process.env.DOCS_ROOT ?? process.cwd());
|
|
42
|
+
const application = createDocumentApplication(root, {
|
|
43
|
+
fontCss,
|
|
44
|
+
readWorkspaceSource,
|
|
45
|
+
sourceFileBase,
|
|
46
|
+
gitBaseBody,
|
|
47
|
+
gitBaseText,
|
|
48
|
+
setSubformatChanges,
|
|
49
|
+
runCodeTool,
|
|
50
|
+
validateCodeTools,
|
|
51
|
+
validateTextfileCode,
|
|
52
|
+
});
|
|
53
|
+
export const { siteFrom, filePreviewResults, renderEntryHtml, renderDocFile, docQueryResponse, sanitizeFrontmatter, isExternallyModified, resolveContentFile, navGroupsHtml, textfilePreviewHtml, textfilePreviewErrors, resourcesMain, publishHtml, languageFieldHtml, editorBoot, pageDataResponse, } = application;
|
|
54
|
+
const { readJson, sendJson, notFound } = application;
|
|
55
|
+
const SHARED_IDLE_MS = 5 * 60_000;
|
|
56
|
+
const SHARED_LEAVE_MS = 30_000;
|
|
57
|
+
const HEARTBEAT_MS = 30_000;
|
|
58
|
+
let workspaceSource;
|
|
59
|
+
let sharedBranch;
|
|
60
|
+
export const createBundleCache = () => {
|
|
61
|
+
const store = new Map();
|
|
62
|
+
return {
|
|
63
|
+
get: async (entry, build) => {
|
|
64
|
+
const hit = store.get(entry);
|
|
65
|
+
if (hit) {
|
|
66
|
+
return hit;
|
|
67
|
+
}
|
|
68
|
+
const js = await build();
|
|
69
|
+
const etag = `"${createHash("sha256").update(js).digest("hex")}"`;
|
|
70
|
+
const value = { js, etag };
|
|
71
|
+
store.set(entry, value);
|
|
72
|
+
return value;
|
|
73
|
+
},
|
|
74
|
+
clear: () => store.clear(),
|
|
75
|
+
};
|
|
76
|
+
};
|
|
77
|
+
// The npm package (running as dist/*.js) has no src/, so read the prebuilt bundles.
|
|
78
|
+
const PREBUILT = {
|
|
79
|
+
"schema-graph-global.tsx": "flow.global.js",
|
|
80
|
+
"edit-client.ts": "edit.global.js",
|
|
81
|
+
"client-entry.ts": "client.global.js",
|
|
82
|
+
"workspace-worker.ts": "workspace-worker.global.js",
|
|
83
|
+
"ui-client.ts": "ui.global.js",
|
|
84
|
+
"code-highlight.ts": "code.global.js",
|
|
85
|
+
"code-worker.ts": "code-worker.global.js",
|
|
86
|
+
"published-plugins.ts": "plugins.global.js",
|
|
87
|
+
"published-subformats.ts": "subformats.global.js",
|
|
88
|
+
};
|
|
89
|
+
/** Minified client bundle shared by local viewing and published HTML. */
|
|
90
|
+
export const bundleClient = async (entry) => {
|
|
91
|
+
const prebuilt = PREBUILT[entry];
|
|
92
|
+
if (usePrebuiltAssets && prebuilt) {
|
|
93
|
+
return readViewerAsset(prebuilt).toString("utf8");
|
|
94
|
+
}
|
|
95
|
+
if (entry === "workspace-worker.ts") {
|
|
96
|
+
const script = new URL("../../../scripts/build/client.mjs", import.meta.url).href;
|
|
97
|
+
const { buildBrowserWorkspace } = await import(script);
|
|
98
|
+
const result = await buildBrowserWorkspace({ write: false });
|
|
99
|
+
return result.outputFiles[0].text;
|
|
100
|
+
}
|
|
101
|
+
const esbuild = await import("esbuild");
|
|
102
|
+
const out = await esbuild.build({
|
|
103
|
+
entryPoints: [join(frontendRoot, entry)],
|
|
104
|
+
bundle: true,
|
|
105
|
+
format: "iife",
|
|
106
|
+
target: "es2022",
|
|
107
|
+
write: false,
|
|
108
|
+
minify: true,
|
|
109
|
+
});
|
|
110
|
+
return out.outputFiles?.[0]?.text ?? "";
|
|
111
|
+
};
|
|
112
|
+
export const startServer = async (mode, port = 5179, pluginModule, startup) => {
|
|
113
|
+
// Language adapters are read once; the manifest is re-read by the compiler.
|
|
114
|
+
const siteLanguages = await loadSiteLanguages(root);
|
|
115
|
+
const viewerPlugins = await loadViewerPlugins(pluginModule);
|
|
116
|
+
workspaceSource = viewerPlugins.workspace;
|
|
117
|
+
let gitOrigin = viewerPlugins.gitOrigin;
|
|
118
|
+
const handleGit = createGitHandler(root, () => gitOrigin);
|
|
119
|
+
// File and Git control travel over the workspace socket. `local` marks a connection whose
|
|
120
|
+
// Host is this exact listener, so Git and its credentials stay off forwarded ports.
|
|
121
|
+
// A shared workspace: snapshots go onto one branch and credit everyone who edited, edits
|
|
122
|
+
// left alone are snapshotted after a quiet period, and branch-level Git is not offered.
|
|
123
|
+
sharedBranch = startup?.shared;
|
|
124
|
+
const history = createHistoryOp(() => workspaceHistory(root, sharedBranch), sharedBranch === undefined ? {} : { idleMs: SHARED_IDLE_MS, leaveMs: SHARED_LEAVE_MS });
|
|
125
|
+
const editOps = {
|
|
126
|
+
history: history.op,
|
|
127
|
+
identify: (input, session) => {
|
|
128
|
+
session.user = parsePerson(input);
|
|
129
|
+
return session.user;
|
|
130
|
+
},
|
|
131
|
+
files: (input) => input.action === "complete"
|
|
132
|
+
? completeFilePaths(root, input.prefix)
|
|
133
|
+
: input.action === "read"
|
|
134
|
+
? filesSnapshot(root)
|
|
135
|
+
: input.action === "upload"
|
|
136
|
+
? uploadFile(root, input)
|
|
137
|
+
: updateFiles(root, input),
|
|
138
|
+
git: async (input, session) => {
|
|
139
|
+
if (!session.local)
|
|
140
|
+
throw new Error("Git requires a same-origin local editor request");
|
|
141
|
+
if ((workspaceSource ?? readWorkspaceSource(root)).protocol !== "git")
|
|
142
|
+
throw new Error("Git workspace is not available");
|
|
143
|
+
const result = await handleGit(input);
|
|
144
|
+
if (workspaceSource?.protocol === "git" &&
|
|
145
|
+
(input.action === "switch" || input.action === "branch")) {
|
|
146
|
+
const local = readWorkspaceSource(root);
|
|
147
|
+
if (local.protocol === "git")
|
|
148
|
+
workspaceSource = { ...workspaceSource, branch: local.branch };
|
|
149
|
+
}
|
|
150
|
+
return result;
|
|
151
|
+
},
|
|
152
|
+
};
|
|
153
|
+
let isolated;
|
|
154
|
+
if (mode === "edit" && sharedBranch === undefined) {
|
|
155
|
+
const original = { ...editOps };
|
|
156
|
+
for (const name of ["workspaces", "http", "files", "git", "history"]) {
|
|
157
|
+
editOps[name] = async (input, session) => {
|
|
158
|
+
if (input.workspace === undefined && original[name])
|
|
159
|
+
return original[name](input, session);
|
|
160
|
+
if (!session.local)
|
|
161
|
+
throw new Error("Workspace branches require a local editor connection");
|
|
162
|
+
isolated ??= nodeWorkspaceOperations(join(root, manifestFile(root)), pluginModule, (event, data) => workspaceServer.broadcast(event, data));
|
|
163
|
+
const manager = await isolated;
|
|
164
|
+
const operation = manager.ops[name];
|
|
165
|
+
if (!operation)
|
|
166
|
+
throw new Error("Workspace operation is unavailable");
|
|
167
|
+
return operation(input, session);
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
// A shared workspace records snapshots instead of offering branch-level Git.
|
|
172
|
+
if (sharedBranch !== undefined)
|
|
173
|
+
delete editOps.git;
|
|
174
|
+
// The boot id changes on every process start; it is how an open page notices a restart.
|
|
175
|
+
// A preview only listens for changes, so it gets no operations.
|
|
176
|
+
const workspaceServer = createWorkspaceServer(mode === "edit" ? editOps : {}, {
|
|
177
|
+
boot: `${process.pid}-${Date.now()}`,
|
|
178
|
+
...(sharedBranch === undefined ? {} : { shared: true }),
|
|
179
|
+
});
|
|
180
|
+
const personal = createLocalPluginStorage();
|
|
181
|
+
const handlePlugins = createPluginHandler(viewerPlugins.plugins, viewerPlugins, personal.credentials);
|
|
182
|
+
const handleExtensions = createExtensionManager(root, handlePlugins, viewerPlugins, personal);
|
|
183
|
+
let loginPlugins = startup?.login ? [startup.login] : [];
|
|
184
|
+
process.once("exit", handleExtensions.dispose);
|
|
185
|
+
// Client bundles (bundleClient) = edit's editing UI and the schema graph interaction layer
|
|
186
|
+
// shared by preview/edit. React Flow's stylesheet isn't mixed into the bundle — the mount
|
|
187
|
+
// layer (flow-css.ts) injects it into a <style> at runtime, so the page needs no CSS delivery
|
|
188
|
+
// path (same conditions as the published output).
|
|
189
|
+
// Cache the bundle result + etag per entry (graph.js / edit.js) — this path isn't hit by the
|
|
190
|
+
// per-keystroke /api/render / /api/save, but tsx watch doesn't restart for these, so
|
|
191
|
+
// invalidate explicitly on the watcher's "client" notification (changes to edit-client.ts /
|
|
192
|
+
// the schema graph interaction layer's src/*.tsx).
|
|
193
|
+
const bundleCache = createBundleCache();
|
|
194
|
+
let graphChunks;
|
|
195
|
+
const graphAsset = async (name) => {
|
|
196
|
+
if (usePrebuiltAssets)
|
|
197
|
+
return readViewerAsset(`graph/${name}`).toString("utf8");
|
|
198
|
+
graphChunks ??= import("esbuild")
|
|
199
|
+
.then(async (esbuild) => {
|
|
200
|
+
const output = await esbuild.build({
|
|
201
|
+
entryPoints: [join(frontendRoot, "schema-graph-global.tsx")],
|
|
202
|
+
bundle: true,
|
|
203
|
+
splitting: true,
|
|
204
|
+
format: "esm",
|
|
205
|
+
target: "es2022",
|
|
206
|
+
outdir: join(pkgRoot, "dist/graph"),
|
|
207
|
+
write: false,
|
|
208
|
+
minify: true,
|
|
209
|
+
});
|
|
210
|
+
return new Map(output.outputFiles.map((file) => [basename(file.path), file.text]));
|
|
211
|
+
})
|
|
212
|
+
.catch((error) => {
|
|
213
|
+
graphChunks = undefined;
|
|
214
|
+
throw error;
|
|
215
|
+
});
|
|
216
|
+
const js = (await graphChunks).get(name);
|
|
217
|
+
if (js === undefined)
|
|
218
|
+
throw new Error("Diagram asset not found");
|
|
219
|
+
return js;
|
|
220
|
+
};
|
|
221
|
+
const viewer = {
|
|
222
|
+
register: handlePlugins.register,
|
|
223
|
+
unregister: handlePlugins.unregister,
|
|
224
|
+
setWorkspace(workspace) {
|
|
225
|
+
workspaceSource = workspace === undefined ? undefined : parseWorkspaceSource(workspace);
|
|
226
|
+
application.configure({
|
|
227
|
+
languages: siteLanguages,
|
|
228
|
+
workspace: workspaceSource,
|
|
229
|
+
shared: sharedBranch,
|
|
230
|
+
downloads: mode === "edit",
|
|
231
|
+
});
|
|
232
|
+
workspaceServer.broadcast("change", { kind: "content", files: [] });
|
|
233
|
+
},
|
|
234
|
+
setGitOrigin(origin) {
|
|
235
|
+
gitOrigin = origin === undefined ? undefined : validateGitOrigin(origin);
|
|
236
|
+
},
|
|
237
|
+
setAllowedDomains(domains) {
|
|
238
|
+
if (!Array.isArray(domains))
|
|
239
|
+
throw new Error("Allowed domains must be a list");
|
|
240
|
+
const previews = parsePreviewSettings({ domains });
|
|
241
|
+
application.configure({
|
|
242
|
+
languages: siteLanguages,
|
|
243
|
+
workspace: workspaceSource,
|
|
244
|
+
shared: sharedBranch,
|
|
245
|
+
downloads: mode === "edit",
|
|
246
|
+
previews,
|
|
247
|
+
});
|
|
248
|
+
workspaceServer.broadcast("change", { kind: "content", files: [] });
|
|
249
|
+
},
|
|
250
|
+
};
|
|
251
|
+
viewer.setAllowedDomains(viewerPlugins.allowedDomains ?? []);
|
|
252
|
+
await viewerPlugins.setup?.(viewer);
|
|
253
|
+
const extensionErrors = await handleExtensions
|
|
254
|
+
.restore()
|
|
255
|
+
.catch((error) => [error instanceof Error ? error.message : String(error)]);
|
|
256
|
+
for (const error of extensionErrors)
|
|
257
|
+
console.error(`Plugin: ${error}`);
|
|
258
|
+
if (startup?.file && startup.data) {
|
|
259
|
+
const result = (await handleExtensions({
|
|
260
|
+
action: "install",
|
|
261
|
+
file: startup.file,
|
|
262
|
+
data: startup.data,
|
|
263
|
+
base: null,
|
|
264
|
+
}));
|
|
265
|
+
loginPlugins = result.items.find((item) => item.file === startup.file)?.plugins ?? [];
|
|
266
|
+
console.log(`Installed ${startup.file} in ${join(root, EXTENSIONS_DIR)}`);
|
|
267
|
+
}
|
|
268
|
+
// DOCS_HMR=0 = no hot reload (nothing is watched = changes need a manual reload).
|
|
269
|
+
if (process.env.DOCS_HMR !== "0") {
|
|
270
|
+
const watcher = watchProject(root, pkgRoot, (kind, files) => {
|
|
271
|
+
console.log(`hmr: ${kind} (${files.join(", ")})`);
|
|
272
|
+
if (kind === "client") {
|
|
273
|
+
bundleCache.clear();
|
|
274
|
+
graphChunks = undefined;
|
|
275
|
+
}
|
|
276
|
+
workspaceServer.broadcast("change", { kind, files });
|
|
277
|
+
});
|
|
278
|
+
application.configure({
|
|
279
|
+
languages: siteLanguages,
|
|
280
|
+
workspace: workspaceSource,
|
|
281
|
+
shared: sharedBranch,
|
|
282
|
+
downloads: mode === "edit",
|
|
283
|
+
watch: watcher.watchTextfiles,
|
|
284
|
+
});
|
|
285
|
+
process.once("exit", watcher.close);
|
|
286
|
+
}
|
|
287
|
+
const handle = async (req, res) => {
|
|
288
|
+
if (await handlePlugins(req, res))
|
|
289
|
+
return;
|
|
290
|
+
// The editor exposes workspace content and writes; refuse rebound hosts for every route.
|
|
291
|
+
if (mode === "edit" && !isLocalHost(req.headers.host ?? "")) {
|
|
292
|
+
sendJson(res, 403, { error: "The editor requires a local host" });
|
|
293
|
+
return;
|
|
294
|
+
}
|
|
295
|
+
const path = new URL(req.url ?? "/", "http://localhost").pathname;
|
|
296
|
+
if (req.method === "GET" && path.startsWith("/assets/fonts/")) {
|
|
297
|
+
const font = readFont(path.slice("/assets/fonts/".length));
|
|
298
|
+
if (!font) {
|
|
299
|
+
notFound(res, path);
|
|
300
|
+
return;
|
|
301
|
+
}
|
|
302
|
+
res.writeHead(200, {
|
|
303
|
+
"content-type": "font/woff2",
|
|
304
|
+
"cache-control": "public, max-age=86400",
|
|
305
|
+
});
|
|
306
|
+
res.end(font);
|
|
307
|
+
return;
|
|
308
|
+
}
|
|
309
|
+
// No favicon (this only silences the 404 console error from the browser's auto-request).
|
|
310
|
+
if (path === "/favicon.ico") {
|
|
311
|
+
res.writeHead(204);
|
|
312
|
+
res.end();
|
|
313
|
+
return;
|
|
314
|
+
}
|
|
315
|
+
// Client bundles — graph.js (the schema graph interaction layer) is shared by
|
|
316
|
+
// preview / edit, edit.js is for the editing UI only.
|
|
317
|
+
const clientEntry = path === "/assets/client.js"
|
|
318
|
+
? "client-entry.ts"
|
|
319
|
+
: path === "/assets/workspace-worker.js"
|
|
320
|
+
? "workspace-worker.ts"
|
|
321
|
+
: path === "/assets/code.js"
|
|
322
|
+
? "code-highlight.ts"
|
|
323
|
+
: path === "/assets/code-worker.js"
|
|
324
|
+
? "code-worker.ts"
|
|
325
|
+
: mode === "edit" && path === "/assets/edit.js"
|
|
326
|
+
? "edit-client.ts"
|
|
327
|
+
: path === "/assets/ui.js"
|
|
328
|
+
? "ui-client.ts"
|
|
329
|
+
: undefined;
|
|
330
|
+
const graphName = path === "/assets/graph.js"
|
|
331
|
+
? "schema-graph-global.js"
|
|
332
|
+
: /^\/assets\/graph\/([\w-]+\.js)$/.exec(path)?.[1];
|
|
333
|
+
if (req.method === "GET" && (clientEntry != null || graphName)) {
|
|
334
|
+
const { js, etag } = await bundleCache.get(graphName ? path : (clientEntry ?? ""), () => graphName
|
|
335
|
+
? path === "/assets/graph.js"
|
|
336
|
+
? Promise.resolve('import "/assets/graph/schema-graph-global.js";')
|
|
337
|
+
: graphAsset(graphName)
|
|
338
|
+
: bundleClient(clientEntry ?? ""));
|
|
339
|
+
if (req.headers["if-none-match"] === etag) {
|
|
340
|
+
res.writeHead(304, { etag, "cache-control": "no-cache" });
|
|
341
|
+
res.end();
|
|
342
|
+
return;
|
|
343
|
+
}
|
|
344
|
+
res.writeHead(200, {
|
|
345
|
+
"content-type": "application/javascript; charset=utf-8",
|
|
346
|
+
etag,
|
|
347
|
+
"cache-control": "no-cache",
|
|
348
|
+
});
|
|
349
|
+
res.end(js);
|
|
350
|
+
return;
|
|
351
|
+
}
|
|
352
|
+
if (mode === "edit" && req.method === "POST") {
|
|
353
|
+
// Every write route: a cross-site form or fetch cannot send this origin + JSON type.
|
|
354
|
+
if (!isLocalToolRequest(req.headers.host ?? "", req.headers.origin, req.headers["content-type"])) {
|
|
355
|
+
sendJson(res, 403, {
|
|
356
|
+
error: "Editing requires a same-origin local editor request",
|
|
357
|
+
});
|
|
358
|
+
return;
|
|
359
|
+
}
|
|
360
|
+
if (path === "/api/extensions") {
|
|
361
|
+
res.setHeader("cache-control", "no-store");
|
|
362
|
+
// Plugins run code, so they also stay off forwarded ports.
|
|
363
|
+
if (![
|
|
364
|
+
`localhost:${req.socket.localPort}`,
|
|
365
|
+
`127.0.0.1:${req.socket.localPort}`,
|
|
366
|
+
`[::1]:${req.socket.localPort}`,
|
|
367
|
+
].includes(req.headers.host ?? "")) {
|
|
368
|
+
sendJson(res, 403, {
|
|
369
|
+
error: "Plugins require a same-origin local editor request",
|
|
370
|
+
});
|
|
371
|
+
return;
|
|
372
|
+
}
|
|
373
|
+
try {
|
|
374
|
+
const input = await readJson(req, MAX_EXTENSION_BYTES * 6 + 4096);
|
|
375
|
+
if (!input)
|
|
376
|
+
throw new Error("Expected a plugin request");
|
|
377
|
+
sendJson(res, 200, await handleExtensions(input));
|
|
378
|
+
}
|
|
379
|
+
catch (error) {
|
|
380
|
+
sendJson(res, 409, {
|
|
381
|
+
error: error instanceof Error ? error.message : "Plugin operation failed",
|
|
382
|
+
});
|
|
383
|
+
}
|
|
384
|
+
return;
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
await application.handle(mode, req, res);
|
|
388
|
+
};
|
|
389
|
+
const listener = (req, res) => {
|
|
390
|
+
handle(req, res).catch((e) => {
|
|
391
|
+
if (!res.headersSent) {
|
|
392
|
+
res.writeHead(500, { "content-type": "text/plain; charset=utf-8" });
|
|
393
|
+
}
|
|
394
|
+
res.end(`error: ${e instanceof Error ? e.message : e}`);
|
|
395
|
+
});
|
|
396
|
+
};
|
|
397
|
+
const tls = startup?.tls;
|
|
398
|
+
const server = tls
|
|
399
|
+
? createSecureServer({ cert: readFileSync(tls.cert), key: readFileSync(tls.key) }, listener)
|
|
400
|
+
: createServer(listener);
|
|
401
|
+
const scheme = tls ? "https" : "http";
|
|
402
|
+
server.on("close", () => {
|
|
403
|
+
void isolated?.then((manager) => manager.close());
|
|
404
|
+
});
|
|
405
|
+
{
|
|
406
|
+
const sockets = new WebSocketServer({
|
|
407
|
+
noServer: true,
|
|
408
|
+
maxPayload: Math.ceil((MAX_FILE_UPLOAD_BYTES * 4) / 3) + 16_384,
|
|
409
|
+
});
|
|
410
|
+
// A connection that vanished without closing (sleep, lost network) never answers a ping;
|
|
411
|
+
// ending it here is what tells the history that its editor has left.
|
|
412
|
+
const alive = new WeakSet();
|
|
413
|
+
setInterval(() => {
|
|
414
|
+
for (const ws of sockets.clients) {
|
|
415
|
+
if (!alive.has(ws)) {
|
|
416
|
+
ws.terminate();
|
|
417
|
+
continue;
|
|
418
|
+
}
|
|
419
|
+
alive.delete(ws);
|
|
420
|
+
ws.ping();
|
|
421
|
+
}
|
|
422
|
+
}, HEARTBEAT_MS).unref();
|
|
423
|
+
server.on("upgrade", (req, socket, head) => {
|
|
424
|
+
const host = req.headers.host ?? "";
|
|
425
|
+
// Browsers do not apply CORS to WebSockets, so this origin check is the whole defence.
|
|
426
|
+
// The editor also refuses rebound hosts; a preview may sit behind a proxy.
|
|
427
|
+
if (new URL(req.url ?? "/", "http://localhost").pathname !== WORKSPACE_PATH ||
|
|
428
|
+
(mode === "edit" && !isLocalHost(host)) ||
|
|
429
|
+
![`http://${host}`, `https://${host}`].includes(req.headers.origin ?? "")) {
|
|
430
|
+
socket.destroy();
|
|
431
|
+
return;
|
|
432
|
+
}
|
|
433
|
+
sockets.handleUpgrade(req, socket, head, (ws) => {
|
|
434
|
+
const session = { local: host.endsWith(`:${req.socket.localPort}`) };
|
|
435
|
+
const detach = workspaceServer.connect({
|
|
436
|
+
send: (message) => ws.send(JSON.stringify(message)),
|
|
437
|
+
receive: (handler) => {
|
|
438
|
+
ws.on("message", (data) => {
|
|
439
|
+
let message;
|
|
440
|
+
try {
|
|
441
|
+
message = JSON.parse(data.toString());
|
|
442
|
+
}
|
|
443
|
+
catch {
|
|
444
|
+
return;
|
|
445
|
+
}
|
|
446
|
+
handler(message);
|
|
447
|
+
});
|
|
448
|
+
},
|
|
449
|
+
}, session);
|
|
450
|
+
alive.add(ws);
|
|
451
|
+
ws.on("pong", () => alive.add(ws));
|
|
452
|
+
const leave = () => {
|
|
453
|
+
detach();
|
|
454
|
+
history.left(session);
|
|
455
|
+
};
|
|
456
|
+
ws.on("close", leave);
|
|
457
|
+
ws.on("error", leave);
|
|
458
|
+
});
|
|
459
|
+
});
|
|
460
|
+
}
|
|
461
|
+
// If the port is in use (e.g. a previous instance left running), automatically try +1, up to
|
|
462
|
+
// 10 ports.
|
|
463
|
+
// The URL is printed once, on "listening" (no callbacks left over from failed listens).
|
|
464
|
+
server.once("listening", () => {
|
|
465
|
+
const addr = server.address();
|
|
466
|
+
const p = typeof addr === "object" && addr ? addr.port : port;
|
|
467
|
+
console.log(`dooc ${mode} (${root}): ${scheme}://localhost:${p}/`);
|
|
468
|
+
if (startup?.open) {
|
|
469
|
+
const url = `${scheme}://localhost:${p}/`;
|
|
470
|
+
const command = process.platform === "darwin" ? "open" : "xdg-open";
|
|
471
|
+
const browser = spawn(command, [url], { stdio: "ignore", detached: true });
|
|
472
|
+
browser.on("error", () => console.error(`Open ${url} in your browser.`));
|
|
473
|
+
browser.on("exit", (code) => {
|
|
474
|
+
if (code)
|
|
475
|
+
console.error(`Open ${url} in your browser.`);
|
|
476
|
+
});
|
|
477
|
+
browser.unref();
|
|
478
|
+
}
|
|
479
|
+
for (const plugin of loginPlugins) {
|
|
480
|
+
try {
|
|
481
|
+
console.log(`Log in to ${plugin}: ${handlePlugins.loginLink(plugin, `${scheme}://localhost:${p}`)}`);
|
|
482
|
+
}
|
|
483
|
+
catch (error) {
|
|
484
|
+
console.error(error instanceof Error ? error.message : String(error));
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
if (loginPlugins.length)
|
|
488
|
+
console.log("Keep this command running while signing in. Login links expire in 10 minutes.");
|
|
489
|
+
});
|
|
490
|
+
const listen = (p, left) => {
|
|
491
|
+
server.once("error", (err) => {
|
|
492
|
+
if (err.code === "EADDRINUSE" && left > 0) {
|
|
493
|
+
console.log(`port ${p} in use → trying ${p + 1}`);
|
|
494
|
+
listen(p + 1, left - 1);
|
|
495
|
+
return;
|
|
496
|
+
}
|
|
497
|
+
throw err;
|
|
498
|
+
});
|
|
499
|
+
server.listen(p, "127.0.0.1");
|
|
500
|
+
};
|
|
501
|
+
listen(port, 10);
|
|
502
|
+
return viewer;
|
|
503
|
+
};
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export declare const validateServerUrl: (value: string) => string;
|
|
2
|
+
export declare const clientAssets: (server?: string) => Promise<Map<string, Buffer>>;
|
|
3
|
+
export declare const buildStaticClient: (directory: string, server?: string) => Promise<void>;
|
|
4
|
+
export declare const startStaticClient: (port?: number, socket?: string) => Promise<import("http").Server<typeof import("http").IncomingMessage, typeof import("http").ServerResponse>>;
|