@neuralnomads/codenomad 0.14.0 → 0.16.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/dist/cli-upgrade.js +53 -0
- package/dist/cli-upgrade.test.js +31 -0
- package/dist/filesystem/__tests__/browser.test.js +65 -0
- package/dist/filesystem/__tests__/search-cache.test.js +2 -2
- package/dist/filesystem/browser.js +40 -5
- package/dist/index.js +27 -6
- package/dist/opencode-plugin/codenomad-opencode-plugin.tgz +0 -0
- package/dist/opencode-plugin.js +146 -0
- package/dist/opencode-plugin.test.js +28 -0
- package/dist/previews/manager.js +63 -0
- package/dist/server/__tests__/remote-proxy.test.js +204 -0
- package/dist/server/http-server.js +380 -46
- package/dist/server/remote-proxy.js +466 -0
- package/dist/server/routes/filesystem.js +13 -0
- package/dist/server/routes/previews.js +25 -0
- package/dist/server/routes/remote-proxy.js +42 -0
- package/dist/server/routes/settings.js +1 -1
- package/dist/server/routes/workspaces.js +141 -1
- package/dist/workspaces/__tests__/git-clone.test.js +141 -0
- package/dist/workspaces/__tests__/git-worktrees.test.js +43 -0
- package/dist/workspaces/__tests__/spawn.test.js +158 -0
- package/dist/workspaces/git-clone.js +148 -0
- package/dist/workspaces/git-mutations.js +98 -0
- package/dist/workspaces/git-status.js +323 -0
- package/dist/workspaces/git-worktrees.js +18 -1
- package/dist/workspaces/manager.js +17 -9
- package/dist/workspaces/opencode-auth.js +18 -0
- package/dist/workspaces/opencode-auth.test.js +34 -0
- package/dist/workspaces/runtime.js +54 -77
- package/dist/workspaces/spawn.js +245 -0
- package/dist/workspaces/worktree-directory.js +74 -0
- package/package.json +3 -3
- package/public/assets/ChangesTab-CxrJwz2Z.js +2 -0
- package/public/assets/DiffToolbar-CqJttTgx.js +1 -0
- package/public/assets/FilesTab-tBrdrLID.js +2 -0
- package/public/assets/GitChangesTab-DUUJn4Va.js +2 -0
- package/public/assets/KaTeX_AMS-Regular-BQhdFMY1.woff2 +0 -0
- package/public/assets/KaTeX_AMS-Regular-DMm9YOAa.woff +0 -0
- package/public/assets/KaTeX_AMS-Regular-DRggAlZN.ttf +0 -0
- package/public/assets/KaTeX_Caligraphic-Bold-ATXxdsX0.ttf +0 -0
- package/public/assets/KaTeX_Caligraphic-Bold-BEiXGLvX.woff +0 -0
- package/public/assets/KaTeX_Caligraphic-Bold-Dq_IR9rO.woff2 +0 -0
- package/public/assets/KaTeX_Caligraphic-Regular-CTRA-rTL.woff +0 -0
- package/public/assets/KaTeX_Caligraphic-Regular-Di6jR-x-.woff2 +0 -0
- package/public/assets/KaTeX_Caligraphic-Regular-wX97UBjC.ttf +0 -0
- package/public/assets/KaTeX_Fraktur-Bold-BdnERNNW.ttf +0 -0
- package/public/assets/KaTeX_Fraktur-Bold-BsDP51OF.woff +0 -0
- package/public/assets/KaTeX_Fraktur-Bold-CL6g_b3V.woff2 +0 -0
- package/public/assets/KaTeX_Fraktur-Regular-CB_wures.ttf +0 -0
- package/public/assets/KaTeX_Fraktur-Regular-CTYiF6lA.woff2 +0 -0
- package/public/assets/KaTeX_Fraktur-Regular-Dxdc4cR9.woff +0 -0
- package/public/assets/KaTeX_Main-Bold-Cx986IdX.woff2 +0 -0
- package/public/assets/KaTeX_Main-Bold-Jm3AIy58.woff +0 -0
- package/public/assets/KaTeX_Main-Bold-waoOVXN0.ttf +0 -0
- package/public/assets/KaTeX_Main-BoldItalic-DxDJ3AOS.woff2 +0 -0
- package/public/assets/KaTeX_Main-BoldItalic-DzxPMmG6.ttf +0 -0
- package/public/assets/KaTeX_Main-BoldItalic-SpSLRI95.woff +0 -0
- package/public/assets/KaTeX_Main-Italic-3WenGoN9.ttf +0 -0
- package/public/assets/KaTeX_Main-Italic-BMLOBm91.woff +0 -0
- package/public/assets/KaTeX_Main-Italic-NWA7e6Wa.woff2 +0 -0
- package/public/assets/KaTeX_Main-Regular-B22Nviop.woff2 +0 -0
- package/public/assets/KaTeX_Main-Regular-Dr94JaBh.woff +0 -0
- package/public/assets/KaTeX_Main-Regular-ypZvNtVU.ttf +0 -0
- package/public/assets/KaTeX_Math-BoldItalic-B3XSjfu4.ttf +0 -0
- package/public/assets/KaTeX_Math-BoldItalic-CZnvNsCZ.woff2 +0 -0
- package/public/assets/KaTeX_Math-BoldItalic-iY-2wyZ7.woff +0 -0
- package/public/assets/KaTeX_Math-Italic-DA0__PXp.woff +0 -0
- package/public/assets/KaTeX_Math-Italic-flOr_0UB.ttf +0 -0
- package/public/assets/KaTeX_Math-Italic-t53AETM-.woff2 +0 -0
- package/public/assets/KaTeX_SansSerif-Bold-CFMepnvq.ttf +0 -0
- package/public/assets/KaTeX_SansSerif-Bold-D1sUS0GD.woff2 +0 -0
- package/public/assets/KaTeX_SansSerif-Bold-DbIhKOiC.woff +0 -0
- package/public/assets/KaTeX_SansSerif-Italic-C3H0VqGB.woff2 +0 -0
- package/public/assets/KaTeX_SansSerif-Italic-DN2j7dab.woff +0 -0
- package/public/assets/KaTeX_SansSerif-Italic-YYjJ1zSn.ttf +0 -0
- package/public/assets/KaTeX_SansSerif-Regular-BNo7hRIc.ttf +0 -0
- package/public/assets/KaTeX_SansSerif-Regular-CS6fqUqJ.woff +0 -0
- package/public/assets/KaTeX_SansSerif-Regular-DDBCnlJ7.woff2 +0 -0
- package/public/assets/KaTeX_Script-Regular-C5JkGWo-.ttf +0 -0
- package/public/assets/KaTeX_Script-Regular-D3wIWfF6.woff2 +0 -0
- package/public/assets/KaTeX_Script-Regular-D5yQViql.woff +0 -0
- package/public/assets/KaTeX_Size1-Regular-C195tn64.woff +0 -0
- package/public/assets/KaTeX_Size1-Regular-Dbsnue_I.ttf +0 -0
- package/public/assets/KaTeX_Size1-Regular-mCD8mA8B.woff2 +0 -0
- package/public/assets/KaTeX_Size2-Regular-B7gKUWhC.ttf +0 -0
- package/public/assets/KaTeX_Size2-Regular-Dy4dx90m.woff2 +0 -0
- package/public/assets/KaTeX_Size2-Regular-oD1tc_U0.woff +0 -0
- package/public/assets/KaTeX_Size3-Regular-CTq5MqoE.woff +0 -0
- package/public/assets/KaTeX_Size3-Regular-DgpXs0kz.ttf +0 -0
- package/public/assets/KaTeX_Size4-Regular-BF-4gkZK.woff +0 -0
- package/public/assets/KaTeX_Size4-Regular-DWFBv043.ttf +0 -0
- package/public/assets/KaTeX_Size4-Regular-Dl5lxZxV.woff2 +0 -0
- package/public/assets/KaTeX_Typewriter-Regular-C0xS9mPB.woff +0 -0
- package/public/assets/KaTeX_Typewriter-Regular-CO6r4hn1.woff2 +0 -0
- package/public/assets/KaTeX_Typewriter-Regular-D3Ib7_Hf.ttf +0 -0
- package/public/assets/{SplitFilePanel-CupweEA5.js → SplitFilePanel-CiZrHI3o.js} +1 -1
- package/public/assets/StatusTab-DN31GijL.js +1 -0
- package/public/assets/align-justify-CKbOni6U.js +1 -0
- package/public/assets/{bundle-full-B-5u6Iv3.js → bundle-full-B_MbXXHv.js} +1 -1
- package/public/assets/{diff-viewer-_ehhYA-Z.js → diff-viewer-CersJruo.js} +1 -1
- package/public/assets/index-BTenW4sL.js +1 -0
- package/public/assets/index-BsYORY4z.js +1 -0
- package/public/assets/index-BvfQjuig.js +1 -0
- package/public/assets/{index-BqiNjV6R.js → index-CK51N0B7.js} +1 -1
- package/public/assets/index-Chghci_R.js +1 -0
- package/public/assets/{index-CxQr-wOF.js → index-DYa3o0_M.js} +1 -1
- package/public/assets/index-DskBWxXS.js +2 -0
- package/public/assets/index-DslkXxdb.js +1 -0
- package/public/assets/index-UCZ7MxNK.js +1 -0
- package/public/assets/index-i5UooSrU.css +1 -0
- package/public/assets/{loading-Dck2H3XH.js → loading-hkCubi3k.js} +1 -1
- package/public/assets/main-CXJTHRp1.js +62 -0
- package/public/assets/markdown-Cy9tCAXp.js +315 -0
- package/public/assets/monaco-viewer-B3iAKbO0.js +26 -0
- package/public/assets/tool-call-DhWimeRu.js +59 -0
- package/public/assets/{unified-picker-Dw0kTXoT.js → unified-picker-C47f5lFi.js} +1 -1
- package/public/assets/wrap-text-hwXbREuU.js +1 -0
- package/public/index.html +4 -4
- package/public/loading.html +4 -4
- package/public/sw.js +1 -1
- package/public/ui-version.json +1 -1
- package/dist/opencode-config/README.md +0 -32
- package/dist/opencode-config/opencode.jsonc +0 -3
- package/dist/opencode-config/package.json +0 -9
- package/dist/opencode-config/plugin/codenomad.ts +0 -62
- package/dist/opencode-config/plugin/lib/background-process.ts +0 -265
- package/dist/opencode-config/plugin/lib/client.ts +0 -133
- package/dist/opencode-config/plugin/lib/request.ts +0 -214
- package/dist/opencode-config.js +0 -26
- package/public/assets/ChangesTab-CFkk3tAp.js +0 -2
- package/public/assets/DiffToolbar-CQQLR1mp.js +0 -1
- package/public/assets/FilesTab-CXgucPtN.js +0 -2
- package/public/assets/GitChangesTab-BidSb28_.js +0 -2
- package/public/assets/StatusTab-C2Pl_Vce.js +0 -1
- package/public/assets/index-BnfDkr8C.js +0 -1
- package/public/assets/index-BntAaLZs.js +0 -1
- package/public/assets/index-BwCEJ4BE.css +0 -1
- package/public/assets/index-DXaitXVC.js +0 -1
- package/public/assets/index-DzRzoTK8.js +0 -2
- package/public/assets/index-ShHad48G.js +0 -1
- package/public/assets/index-_EaXFUIj.js +0 -1
- package/public/assets/index-mMh5TJvW.js +0 -1
- package/public/assets/main-BsIf5lOY.js +0 -50
- package/public/assets/markdown-CFV9kR80.js +0 -58
- package/public/assets/monaco-viewer-CMM6n0T3.js +0 -25
- package/public/assets/todo-Ghwirj-b.js +0 -1
- package/public/assets/tool-call-BC2Pv3uG.js +0 -60
- package/public/assets/wrap-text-D2MJk2ad.js +0 -1
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { spawn } from "child_process";
|
|
2
|
+
const CODENOMAD_PACKAGE_NAME = "@neuralnomads/codenomad";
|
|
3
|
+
function detectFromText(value) {
|
|
4
|
+
const lower = (value ?? "").toLowerCase();
|
|
5
|
+
if (!lower)
|
|
6
|
+
return null;
|
|
7
|
+
if (lower.includes("pnpm"))
|
|
8
|
+
return "pnpm";
|
|
9
|
+
if (lower.includes("bun"))
|
|
10
|
+
return "bun";
|
|
11
|
+
if (lower.includes("npm"))
|
|
12
|
+
return "npm";
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
15
|
+
export function detectPackageManager(env = process.env) {
|
|
16
|
+
return detectFromText(env.npm_config_user_agent) ?? detectFromText(env.npm_execpath) ?? "npm";
|
|
17
|
+
}
|
|
18
|
+
export function buildUpgradeCommand(version, packageManager = detectPackageManager()) {
|
|
19
|
+
const targetVersion = (version ?? "").trim() || "latest";
|
|
20
|
+
const packageSpec = `${CODENOMAD_PACKAGE_NAME}@${targetVersion}`;
|
|
21
|
+
const args = packageManager === "bun" ? ["add", "-g", packageSpec] : ["install", "-g", packageSpec];
|
|
22
|
+
return {
|
|
23
|
+
command: packageManager,
|
|
24
|
+
args,
|
|
25
|
+
packageSpec,
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
export function formatUpgradeCommand(command) {
|
|
29
|
+
return [command.command, ...command.args].join(" ");
|
|
30
|
+
}
|
|
31
|
+
export function runCliUpgrade(version, env = process.env) {
|
|
32
|
+
const upgrade = buildUpgradeCommand(version, detectPackageManager(env));
|
|
33
|
+
console.log(`Upgrading CodeNomad with: ${formatUpgradeCommand(upgrade)}`);
|
|
34
|
+
return new Promise((resolve) => {
|
|
35
|
+
const child = spawn(upgrade.command, upgrade.args, {
|
|
36
|
+
env,
|
|
37
|
+
shell: process.platform === "win32",
|
|
38
|
+
stdio: "inherit",
|
|
39
|
+
});
|
|
40
|
+
child.on("exit", (code, signal) => {
|
|
41
|
+
if (signal) {
|
|
42
|
+
console.error(`Upgrade command stopped by signal ${signal}`);
|
|
43
|
+
resolve(1);
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
resolve(code ?? 0);
|
|
47
|
+
});
|
|
48
|
+
child.on("error", (error) => {
|
|
49
|
+
console.error("Failed to launch upgrade command", error);
|
|
50
|
+
resolve(1);
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { describe, it } from "node:test";
|
|
3
|
+
import { buildUpgradeCommand, detectPackageManager, formatUpgradeCommand } from "./cli-upgrade";
|
|
4
|
+
describe("cli upgrade", () => {
|
|
5
|
+
it("defaults to npm when no package manager can be detected", () => {
|
|
6
|
+
assert.equal(detectPackageManager({}), "npm");
|
|
7
|
+
});
|
|
8
|
+
it("detects package managers from npm user agent", () => {
|
|
9
|
+
assert.equal(detectPackageManager({ npm_config_user_agent: "pnpm/9.0.0 node/v22" }), "pnpm");
|
|
10
|
+
assert.equal(detectPackageManager({ npm_config_user_agent: "bun/1.0.0" }), "bun");
|
|
11
|
+
assert.equal(detectPackageManager({ npm_config_user_agent: "npm/10.0.0 node/v22" }), "npm");
|
|
12
|
+
});
|
|
13
|
+
it("builds latest upgrade command by default", () => {
|
|
14
|
+
const command = buildUpgradeCommand(undefined, "npm");
|
|
15
|
+
assert.equal(command.packageSpec, "@neuralnomads/codenomad@latest");
|
|
16
|
+
assert.deepEqual(command.args, ["install", "-g", "@neuralnomads/codenomad@latest"]);
|
|
17
|
+
assert.equal(formatUpgradeCommand(command), "npm install -g @neuralnomads/codenomad@latest");
|
|
18
|
+
});
|
|
19
|
+
it("builds a versioned upgrade command", () => {
|
|
20
|
+
const command = buildUpgradeCommand("0.10.5", "pnpm");
|
|
21
|
+
assert.equal(command.packageSpec, "@neuralnomads/codenomad@0.10.5");
|
|
22
|
+
assert.deepEqual(command.args, ["install", "-g", "@neuralnomads/codenomad@0.10.5"]);
|
|
23
|
+
assert.equal(formatUpgradeCommand(command), "pnpm install -g @neuralnomads/codenomad@0.10.5");
|
|
24
|
+
});
|
|
25
|
+
it("uses bun add for Bun installs", () => {
|
|
26
|
+
const command = buildUpgradeCommand("0.10.5", "bun");
|
|
27
|
+
assert.equal(command.packageSpec, "@neuralnomads/codenomad@0.10.5");
|
|
28
|
+
assert.deepEqual(command.args, ["add", "-g", "@neuralnomads/codenomad@0.10.5"]);
|
|
29
|
+
assert.equal(formatUpgradeCommand(command), "bun add -g @neuralnomads/codenomad@0.10.5");
|
|
30
|
+
});
|
|
31
|
+
});
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import fs from "node:fs";
|
|
3
|
+
import os from "node:os";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { afterEach, describe, it } from "node:test";
|
|
6
|
+
import { FileSystemBrowser } from "../browser";
|
|
7
|
+
import { WINDOWS_DRIVES_ROOT } from "../../api-types";
|
|
8
|
+
const tempRoots = [];
|
|
9
|
+
describe("FileSystemBrowser", () => {
|
|
10
|
+
afterEach(() => {
|
|
11
|
+
for (const directory of tempRoots.splice(0)) {
|
|
12
|
+
fs.rmSync(directory, { recursive: true, force: true });
|
|
13
|
+
}
|
|
14
|
+
});
|
|
15
|
+
it("starts unrestricted browsing from the configured root", () => {
|
|
16
|
+
const rootDir = createTempRoot();
|
|
17
|
+
fs.mkdirSync(path.join(rootDir, "project"));
|
|
18
|
+
const browser = new FileSystemBrowser({ rootDir, unrestricted: true });
|
|
19
|
+
const listing = browser.browse();
|
|
20
|
+
assert.equal(listing.metadata.scope, "unrestricted");
|
|
21
|
+
assert.equal(listing.metadata.currentPath, rootDir);
|
|
22
|
+
assert.equal(listing.metadata.rootPath, rootDir);
|
|
23
|
+
assert.equal(listing.metadata.displayPath, rootDir);
|
|
24
|
+
assert.equal(listing.metadata.pathKind, "absolute");
|
|
25
|
+
assert.ok(listing.entries.some((entry) => entry.name === "project" && entry.absolutePath === path.join(rootDir, "project")));
|
|
26
|
+
});
|
|
27
|
+
it("treats dot as the configured unrestricted root", () => {
|
|
28
|
+
const rootDir = createTempRoot();
|
|
29
|
+
const browser = new FileSystemBrowser({ rootDir, unrestricted: true });
|
|
30
|
+
const listing = browser.browse(".");
|
|
31
|
+
assert.equal(listing.metadata.currentPath, rootDir);
|
|
32
|
+
assert.equal(listing.metadata.rootPath, rootDir);
|
|
33
|
+
});
|
|
34
|
+
it("allows unrestricted browsing outside the configured root", () => {
|
|
35
|
+
const rootDir = createTempRoot();
|
|
36
|
+
const parentDir = path.dirname(rootDir);
|
|
37
|
+
const browser = new FileSystemBrowser({ rootDir, unrestricted: true });
|
|
38
|
+
const listing = browser.browse(parentDir);
|
|
39
|
+
assert.equal(listing.metadata.currentPath, parentDir);
|
|
40
|
+
assert.equal(listing.metadata.rootPath, rootDir);
|
|
41
|
+
assert.ok(listing.entries.some((entry) => entry.absolutePath === rootDir));
|
|
42
|
+
});
|
|
43
|
+
it("creates folders under the configured unrestricted root by default", () => {
|
|
44
|
+
const rootDir = createTempRoot();
|
|
45
|
+
const browser = new FileSystemBrowser({ rootDir, unrestricted: true });
|
|
46
|
+
const created = browser.createFolder(undefined, "created-folder");
|
|
47
|
+
assert.equal(created.path, path.join(rootDir, "created-folder"));
|
|
48
|
+
assert.equal(created.absolutePath, path.join(rootDir, "created-folder"));
|
|
49
|
+
assert.equal(fs.statSync(created.absolutePath).isDirectory(), true);
|
|
50
|
+
});
|
|
51
|
+
it("reports the configured root for the Windows drives pseudo-root", () => {
|
|
52
|
+
const rootDir = createTempRoot();
|
|
53
|
+
const browser = new FileSystemBrowser({ rootDir, unrestricted: true, platform: "win32" });
|
|
54
|
+
const listing = browser.browse(WINDOWS_DRIVES_ROOT);
|
|
55
|
+
assert.equal(listing.metadata.scope, "unrestricted");
|
|
56
|
+
assert.equal(listing.metadata.currentPath, WINDOWS_DRIVES_ROOT);
|
|
57
|
+
assert.equal(listing.metadata.rootPath, rootDir);
|
|
58
|
+
assert.equal(listing.metadata.pathKind, "drives");
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
function createTempRoot() {
|
|
62
|
+
const root = fs.mkdtempSync(path.join(os.tmpdir(), "codenomad-browser-"));
|
|
63
|
+
tempRoots.push(root);
|
|
64
|
+
return fs.realpathSync(root);
|
|
65
|
+
}
|
|
@@ -19,11 +19,11 @@ describe("workspace search cache", () => {
|
|
|
19
19
|
it("replaces cached entries when manually refreshed", () => {
|
|
20
20
|
const workspacePath = "/tmp/workspace";
|
|
21
21
|
refreshWorkspaceCandidates(workspacePath, () => [createEntry("file-a")], 5000);
|
|
22
|
-
const initial = getWorkspaceCandidates(workspacePath);
|
|
22
|
+
const initial = getWorkspaceCandidates(workspacePath, 5001);
|
|
23
23
|
assert.ok(initial);
|
|
24
24
|
assert.equal(initial[0].name, "file-a");
|
|
25
25
|
refreshWorkspaceCandidates(workspacePath, () => [createEntry("file-b")], 6000);
|
|
26
|
-
const refreshed = getWorkspaceCandidates(workspacePath);
|
|
26
|
+
const refreshed = getWorkspaceCandidates(workspacePath, 6001);
|
|
27
27
|
assert.ok(refreshed);
|
|
28
28
|
assert.equal(refreshed[0].name, "file-b");
|
|
29
29
|
});
|
|
@@ -3,12 +3,13 @@ import os from "os";
|
|
|
3
3
|
import path from "path";
|
|
4
4
|
import { WINDOWS_DRIVES_ROOT, } from "../api-types";
|
|
5
5
|
const WINDOWS_DRIVE_LETTERS = Array.from({ length: 26 }, (_, i) => String.fromCharCode(65 + i));
|
|
6
|
+
const MAX_READABLE_FILE_BYTES = 5 * 1024 * 1024;
|
|
6
7
|
export class FileSystemBrowser {
|
|
7
8
|
constructor(options) {
|
|
8
9
|
this.root = path.resolve(options.rootDir);
|
|
9
10
|
this.unrestricted = Boolean(options.unrestricted);
|
|
10
11
|
this.homeDir = os.homedir();
|
|
11
|
-
this.isWindows = process.platform === "win32";
|
|
12
|
+
this.isWindows = (options.platform ?? process.platform) === "win32";
|
|
12
13
|
}
|
|
13
14
|
list(relativePath = ".", options = {}) {
|
|
14
15
|
if (this.unrestricted) {
|
|
@@ -64,6 +65,26 @@ export class FileSystemBrowser {
|
|
|
64
65
|
const resolved = this.toRestrictedAbsolute(relativePath);
|
|
65
66
|
return fs.readFileSync(resolved, "utf-8");
|
|
66
67
|
}
|
|
68
|
+
readFileBase64(relativePath) {
|
|
69
|
+
if (this.unrestricted) {
|
|
70
|
+
throw new Error("readFileBase64 is not available in unrestricted mode");
|
|
71
|
+
}
|
|
72
|
+
const resolved = this.toRestrictedAbsolute(relativePath);
|
|
73
|
+
return fs.readFileSync(resolved).toString("base64");
|
|
74
|
+
}
|
|
75
|
+
readFileContent(targetPath, options) {
|
|
76
|
+
const encoding = options?.encoding ?? "utf-8";
|
|
77
|
+
const resolved = this.unrestricted ? this.resolveUnrestrictedPath(targetPath) : this.toRestrictedAbsolute(targetPath);
|
|
78
|
+
const stats = fs.statSync(resolved);
|
|
79
|
+
if (!stats.isFile()) {
|
|
80
|
+
throw new Error("Selected path is not a file");
|
|
81
|
+
}
|
|
82
|
+
if (stats.size > MAX_READABLE_FILE_BYTES) {
|
|
83
|
+
throw new Error("Selected file is too large to attach");
|
|
84
|
+
}
|
|
85
|
+
const contents = encoding === "base64" ? fs.readFileSync(resolved).toString("base64") : fs.readFileSync(resolved, "utf-8");
|
|
86
|
+
return { path: targetPath, contents, encoding };
|
|
87
|
+
}
|
|
67
88
|
listRestrictedWithMetadata(relativePath, includeFiles) {
|
|
68
89
|
const normalizedPath = this.normalizeRelativePath(relativePath);
|
|
69
90
|
const absolutePath = this.toRestrictedAbsolute(normalizedPath);
|
|
@@ -98,7 +119,7 @@ export class FileSystemBrowser {
|
|
|
98
119
|
scope: "unrestricted",
|
|
99
120
|
currentPath: resolvedPath,
|
|
100
121
|
parentPath,
|
|
101
|
-
rootPath: this.
|
|
122
|
+
rootPath: this.root,
|
|
102
123
|
homePath: this.homeDir,
|
|
103
124
|
displayPath: resolvedPath,
|
|
104
125
|
pathKind: "absolute",
|
|
@@ -137,7 +158,7 @@ export class FileSystemBrowser {
|
|
|
137
158
|
scope: "unrestricted",
|
|
138
159
|
currentPath: WINDOWS_DRIVES_ROOT,
|
|
139
160
|
parentPath: undefined,
|
|
140
|
-
rootPath: this.
|
|
161
|
+
rootPath: this.root,
|
|
141
162
|
homePath: this.homeDir,
|
|
142
163
|
displayPath: "Drives",
|
|
143
164
|
pathKind: "drives",
|
|
@@ -206,6 +227,17 @@ export class FileSystemBrowser {
|
|
|
206
227
|
if (!input || input === "." || input === "./" || input === "/") {
|
|
207
228
|
return ".";
|
|
208
229
|
}
|
|
230
|
+
if (path.isAbsolute(input)) {
|
|
231
|
+
const resolved = path.resolve(input);
|
|
232
|
+
const relativeToRoot = path.relative(this.root, resolved);
|
|
233
|
+
if (relativeToRoot === "") {
|
|
234
|
+
return ".";
|
|
235
|
+
}
|
|
236
|
+
if (this.isOutsideRoot(relativeToRoot)) {
|
|
237
|
+
throw new Error("Access outside of root is not allowed");
|
|
238
|
+
}
|
|
239
|
+
return relativeToRoot.replace(/\\+/g, "/");
|
|
240
|
+
}
|
|
209
241
|
let normalized = input.replace(/\\+/g, "/");
|
|
210
242
|
if (normalized.startsWith("./")) {
|
|
211
243
|
normalized = normalized.replace(/^\.\/+/, "");
|
|
@@ -232,14 +264,17 @@ export class FileSystemBrowser {
|
|
|
232
264
|
const normalized = this.normalizeRelativePath(relativePath);
|
|
233
265
|
const target = path.resolve(this.root, normalized);
|
|
234
266
|
const relativeToRoot = path.relative(this.root, target);
|
|
235
|
-
if (
|
|
267
|
+
if (this.isOutsideRoot(relativeToRoot)) {
|
|
236
268
|
throw new Error("Access outside of root is not allowed");
|
|
237
269
|
}
|
|
238
270
|
return target;
|
|
239
271
|
}
|
|
272
|
+
isOutsideRoot(relativeToRoot) {
|
|
273
|
+
return relativeToRoot === ".." || relativeToRoot.startsWith(`..${path.sep}`) || path.isAbsolute(relativeToRoot);
|
|
274
|
+
}
|
|
240
275
|
resolveUnrestrictedPath(input) {
|
|
241
276
|
if (!input || input === "." || input === "./") {
|
|
242
|
-
return this.
|
|
277
|
+
return this.root;
|
|
243
278
|
}
|
|
244
279
|
if (this.isWindows) {
|
|
245
280
|
if (input === WINDOWS_DRIVES_ROOT) {
|
package/dist/index.js
CHANGED
|
@@ -20,13 +20,16 @@ import { launchInBrowser } from "./launcher";
|
|
|
20
20
|
import { resolveUi } from "./ui/remote-ui";
|
|
21
21
|
import { AuthManager, BOOTSTRAP_TOKEN_STDOUT_PREFIX, DEFAULT_AUTH_COOKIE_NAME, DEFAULT_AUTH_USERNAME } from "./auth/manager";
|
|
22
22
|
import { resolveHttpsOptions } from "./server/tls";
|
|
23
|
+
import { RemoteProxySessionManager } from "./server/remote-proxy";
|
|
23
24
|
import { resolveNetworkAddresses, resolveRemoteAddresses } from "./server/network-addresses";
|
|
24
25
|
import { startDevReleaseMonitor } from "./releases/dev-release-monitor";
|
|
25
26
|
import { SpeechService } from "./speech/service";
|
|
26
27
|
import { SideCarManager } from "./sidecars/manager";
|
|
28
|
+
import { PreviewManager } from "./previews/manager";
|
|
27
29
|
import { ClientConnectionManager } from "./clients/connection-manager";
|
|
28
30
|
import { PluginChannelManager } from "./plugins/channel";
|
|
29
31
|
import { VoiceModeManager } from "./plugins/voice-mode";
|
|
32
|
+
import { runCliUpgrade } from "./cli-upgrade";
|
|
30
33
|
const require = createRequire(import.meta.url);
|
|
31
34
|
const packageJson = require("../package.json");
|
|
32
35
|
const __filename = fileURLToPath(import.meta.url);
|
|
@@ -74,9 +77,11 @@ function parseCliOptions(argv) {
|
|
|
74
77
|
.default(false))
|
|
75
78
|
.addOption(new Option("--dangerously-skip-auth", "Disable CodeNomad's internal auth. Use only behind a trusted perimeter (SSO/VPN/etc).")
|
|
76
79
|
.env("CODENOMAD_SKIP_AUTH")
|
|
77
|
-
.default(false))
|
|
80
|
+
.default(false))
|
|
81
|
+
.addOption(new Option("--upgrade [version]", "Upgrade the global CodeNomad CLI server package and exit"));
|
|
78
82
|
program.parse(argv, { from: "user" });
|
|
79
83
|
const parsed = program.opts();
|
|
84
|
+
const upgrade = parsed.upgrade;
|
|
80
85
|
const parseBooleanEnv = (value) => {
|
|
81
86
|
const normalized = (value ?? "").trim().toLowerCase();
|
|
82
87
|
return normalized === "1" || normalized === "true" || normalized === "yes" || normalized === "y" || normalized === "on";
|
|
@@ -87,7 +92,7 @@ function parseCliOptions(argv) {
|
|
|
87
92
|
const uiAutoUpdate = autoUpdateString === "1" || autoUpdateString === "true" || autoUpdateString === "yes";
|
|
88
93
|
const httpsEnabled = parseBooleanEnv(parsed.https);
|
|
89
94
|
const httpEnabled = parseBooleanEnv(parsed.http);
|
|
90
|
-
if (!httpsEnabled && !httpEnabled) {
|
|
95
|
+
if (upgrade === undefined && !httpsEnabled && !httpEnabled) {
|
|
91
96
|
throw new InvalidArgumentError("At least one listener must be enabled (--https or --http)");
|
|
92
97
|
}
|
|
93
98
|
return {
|
|
@@ -116,6 +121,7 @@ function parseCliOptions(argv) {
|
|
|
116
121
|
authCookieName: parsed.authCookieName,
|
|
117
122
|
generateToken: Boolean(parsed.generateToken),
|
|
118
123
|
dangerouslySkipAuth: Boolean(parsed.dangerouslySkipAuth),
|
|
124
|
+
upgrade,
|
|
119
125
|
};
|
|
120
126
|
}
|
|
121
127
|
function parsePort(input) {
|
|
@@ -142,6 +148,11 @@ function programHasArg(argv, flag) {
|
|
|
142
148
|
}
|
|
143
149
|
async function main() {
|
|
144
150
|
const options = parseCliOptions(process.argv.slice(2));
|
|
151
|
+
if (options.upgrade !== undefined) {
|
|
152
|
+
const version = typeof options.upgrade === "string" ? options.upgrade : undefined;
|
|
153
|
+
process.exitCode = await runCliUpgrade(version);
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
145
156
|
const logger = createLogger({ level: options.logLevel, destination: options.logDestination, component: "app" });
|
|
146
157
|
const workspaceLogger = logger.child({ component: "workspace" });
|
|
147
158
|
const configLogger = logger.child({ component: "config" });
|
|
@@ -209,7 +220,10 @@ async function main() {
|
|
|
209
220
|
getServerBaseUrl: () => serverMeta.localUrl,
|
|
210
221
|
nodeExtraCaCertsPath,
|
|
211
222
|
});
|
|
212
|
-
const fileSystemBrowser = new FileSystemBrowser({
|
|
223
|
+
const fileSystemBrowser = new FileSystemBrowser({
|
|
224
|
+
rootDir: options.rootDir,
|
|
225
|
+
unrestricted: options.unrestrictedRoot,
|
|
226
|
+
});
|
|
213
227
|
const instanceStore = new InstanceStore(configLocation.instancesDir);
|
|
214
228
|
const speechService = new SpeechService(settings, logger.child({ component: "speech" }));
|
|
215
229
|
const sidecarManager = new SideCarManager({
|
|
@@ -217,6 +231,7 @@ async function main() {
|
|
|
217
231
|
eventBus,
|
|
218
232
|
logger: logger.child({ component: "sidecars" }),
|
|
219
233
|
});
|
|
234
|
+
const previewManager = new PreviewManager();
|
|
220
235
|
const instanceEventBridge = new InstanceEventBridge({
|
|
221
236
|
workspaceManager,
|
|
222
237
|
eventBus,
|
|
@@ -262,12 +277,14 @@ async function main() {
|
|
|
262
277
|
},
|
|
263
278
|
})
|
|
264
279
|
: null;
|
|
265
|
-
if (uiResolution.uiDevServerUrl && options.https) {
|
|
266
|
-
throw new InvalidArgumentError("UI dev proxy is only supported with --https=false --http=true");
|
|
267
|
-
}
|
|
268
280
|
const remoteAccessEnabled = options.host === "0.0.0.0" || !isLoopbackHost(options.host);
|
|
269
281
|
const clientConnectionManager = new ClientConnectionManager(logger.child({ component: "client-connections" }));
|
|
270
282
|
const pluginChannel = new PluginChannelManager(logger.child({ component: "plugin-channel" }));
|
|
283
|
+
const remoteProxySessionManager = new RemoteProxySessionManager({
|
|
284
|
+
authManager,
|
|
285
|
+
logger: logger.child({ component: "remote-proxy" }),
|
|
286
|
+
httpsOptions: tlsResolution?.httpsOptions,
|
|
287
|
+
});
|
|
271
288
|
const voiceModeManager = new VoiceModeManager({
|
|
272
289
|
connections: clientConnectionManager,
|
|
273
290
|
channel: pluginChannel,
|
|
@@ -298,10 +315,12 @@ async function main() {
|
|
|
298
315
|
instanceStore,
|
|
299
316
|
speechService,
|
|
300
317
|
sidecarManager,
|
|
318
|
+
previewManager,
|
|
301
319
|
authManager,
|
|
302
320
|
clientConnectionManager,
|
|
303
321
|
pluginChannel,
|
|
304
322
|
voiceModeManager,
|
|
323
|
+
remoteProxySessionManager,
|
|
305
324
|
uiStaticDir: uiResolution.uiStaticDir ?? DEFAULT_UI_STATIC_DIR,
|
|
306
325
|
uiDevServerUrl: uiResolution.uiDevServerUrl,
|
|
307
326
|
logger,
|
|
@@ -322,10 +341,12 @@ async function main() {
|
|
|
322
341
|
instanceStore,
|
|
323
342
|
speechService,
|
|
324
343
|
sidecarManager,
|
|
344
|
+
previewManager,
|
|
325
345
|
authManager,
|
|
326
346
|
clientConnectionManager,
|
|
327
347
|
pluginChannel,
|
|
328
348
|
voiceModeManager,
|
|
349
|
+
remoteProxySessionManager,
|
|
329
350
|
uiStaticDir: uiResolution.uiStaticDir ?? DEFAULT_UI_STATIC_DIR,
|
|
330
351
|
uiDevServerUrl: undefined,
|
|
331
352
|
logger,
|
|
Binary file
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { existsSync, readdirSync } from "fs";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import { fileURLToPath, pathToFileURL } from "url";
|
|
4
|
+
import { createLogger } from "./logger";
|
|
5
|
+
const log = createLogger({ component: "opencode-plugin" });
|
|
6
|
+
const pluginPackageName = "@codenomad/codenomad-opencode-plugin";
|
|
7
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
8
|
+
const __dirname = path.dirname(__filename);
|
|
9
|
+
const resourcesPath = process.resourcesPath;
|
|
10
|
+
const devPluginEntry = path.resolve(__dirname, "../../opencode-plugin/plugin/codenomad.ts");
|
|
11
|
+
const prodPluginDirs = [
|
|
12
|
+
resourcesPath ? path.resolve(resourcesPath, "opencode-plugin") : undefined,
|
|
13
|
+
resourcesPath ? path.resolve(resourcesPath, "server/dist/opencode-plugin") : undefined,
|
|
14
|
+
path.resolve(__dirname, "opencode-plugin"),
|
|
15
|
+
].filter((dir) => Boolean(dir));
|
|
16
|
+
const isDevBuild = Boolean(process.env.CODENOMAD_DEV ??
|
|
17
|
+
process.env.CLI_UI_DEV_SERVER ??
|
|
18
|
+
process.env.VITE_DEV_SERVER_URL ??
|
|
19
|
+
process.env.ELECTRON_RENDERER_URL);
|
|
20
|
+
const isSourceRun = path.basename(__dirname) === "src" && existsSync(devPluginEntry);
|
|
21
|
+
export function getCodeNomadPluginUrl() {
|
|
22
|
+
if (isDevBuild || isSourceRun) {
|
|
23
|
+
if (!existsSync(devPluginEntry)) {
|
|
24
|
+
throw new Error(`CodeNomad OpenCode plugin entry missing at ${devPluginEntry}`);
|
|
25
|
+
}
|
|
26
|
+
log.debug({ pluginEntry: devPluginEntry }, "Using OpenCode plugin source directly (dev mode)");
|
|
27
|
+
return pathToFileURL(devPluginEntry).href;
|
|
28
|
+
}
|
|
29
|
+
for (const dir of prodPluginDirs) {
|
|
30
|
+
const tarball = findPluginTarball(dir);
|
|
31
|
+
if (tarball) {
|
|
32
|
+
return toNpmFileSpecifier(tarball);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
throw new Error(`CodeNomad OpenCode plugin package missing in ${prodPluginDirs.join(", ")}`);
|
|
36
|
+
}
|
|
37
|
+
export function buildOpencodeConfigContent(existingContent, pluginUrl) {
|
|
38
|
+
const config = existingContent?.trim() ? parseJsoncObject(existingContent) : {};
|
|
39
|
+
const existingPlugins = normalizePluginEntries(config.plugin);
|
|
40
|
+
if (!existingPlugins.includes(pluginUrl)) {
|
|
41
|
+
existingPlugins.push(pluginUrl);
|
|
42
|
+
}
|
|
43
|
+
return JSON.stringify({
|
|
44
|
+
"$schema": typeof config["$schema"] === "string" ? config["$schema"] : "https://opencode.ai/config.json",
|
|
45
|
+
...config,
|
|
46
|
+
plugin: existingPlugins,
|
|
47
|
+
}, null, 2);
|
|
48
|
+
}
|
|
49
|
+
export function resolveExistingOpencodeConfigContent(userEnvironment) {
|
|
50
|
+
const userValue = normalizeConfigContentValue(userEnvironment.OPENCODE_CONFIG_CONTENT);
|
|
51
|
+
if (userValue) {
|
|
52
|
+
return userValue;
|
|
53
|
+
}
|
|
54
|
+
return normalizeConfigContentValue(process.env.OPENCODE_CONFIG_CONTENT);
|
|
55
|
+
}
|
|
56
|
+
function toNpmFileSpecifier(filePath) {
|
|
57
|
+
return `${pluginPackageName}@file:${filePath.replace(/\\/g, "/")}`;
|
|
58
|
+
}
|
|
59
|
+
function findPluginTarball(dir) {
|
|
60
|
+
if (!existsSync(dir)) {
|
|
61
|
+
return null;
|
|
62
|
+
}
|
|
63
|
+
const tarballs = readdirSync(dir)
|
|
64
|
+
.filter((name) => name.endsWith(".tgz"))
|
|
65
|
+
.sort();
|
|
66
|
+
return tarballs.length > 0 ? path.resolve(dir, tarballs[tarballs.length - 1]) : null;
|
|
67
|
+
}
|
|
68
|
+
function normalizeConfigContentValue(value) {
|
|
69
|
+
return typeof value === "string" && value.trim().length > 0 ? value : undefined;
|
|
70
|
+
}
|
|
71
|
+
function parseJsoncObject(content) {
|
|
72
|
+
try {
|
|
73
|
+
const parsed = JSON.parse(stripJsonc(content));
|
|
74
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
|
|
75
|
+
throw new Error("OPENCODE_CONFIG_CONTENT must be a JSON object");
|
|
76
|
+
}
|
|
77
|
+
return parsed;
|
|
78
|
+
}
|
|
79
|
+
catch (error) {
|
|
80
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
81
|
+
throw new Error(`Failed to parse OPENCODE_CONFIG_CONTENT: ${reason}`);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
function normalizePluginEntries(value) {
|
|
85
|
+
if (value === undefined) {
|
|
86
|
+
return [];
|
|
87
|
+
}
|
|
88
|
+
if (typeof value === "string") {
|
|
89
|
+
return [value];
|
|
90
|
+
}
|
|
91
|
+
if (Array.isArray(value) && value.every((item) => typeof item === "string")) {
|
|
92
|
+
return [...value];
|
|
93
|
+
}
|
|
94
|
+
throw new Error("OPENCODE_CONFIG_CONTENT plugin field must be a string or string array");
|
|
95
|
+
}
|
|
96
|
+
function stripJsonc(input) {
|
|
97
|
+
let output = "";
|
|
98
|
+
let inString = false;
|
|
99
|
+
let escape = false;
|
|
100
|
+
for (let index = 0; index < input.length; index += 1) {
|
|
101
|
+
const char = input[index];
|
|
102
|
+
const next = input[index + 1];
|
|
103
|
+
if (escape) {
|
|
104
|
+
output += char;
|
|
105
|
+
escape = false;
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
if (char === "\\" && inString) {
|
|
109
|
+
output += char;
|
|
110
|
+
escape = true;
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
if (char === '"') {
|
|
114
|
+
output += char;
|
|
115
|
+
inString = !inString;
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
if (!inString && char === "/" && next === "/") {
|
|
119
|
+
while (index < input.length && input[index] !== "\n") {
|
|
120
|
+
index += 1;
|
|
121
|
+
}
|
|
122
|
+
output += "\n";
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
if (!inString && char === "/" && next === "*") {
|
|
126
|
+
index += 2;
|
|
127
|
+
while (index < input.length && !(input[index] === "*" && input[index + 1] === "/")) {
|
|
128
|
+
output += input[index] === "\n" ? "\n" : "";
|
|
129
|
+
index += 1;
|
|
130
|
+
}
|
|
131
|
+
index += 1;
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
if (!inString && char === ",") {
|
|
135
|
+
let lookahead = index + 1;
|
|
136
|
+
while (lookahead < input.length && /\s/.test(input[lookahead])) {
|
|
137
|
+
lookahead += 1;
|
|
138
|
+
}
|
|
139
|
+
if (input[lookahead] === "}" || input[lookahead] === "]") {
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
output += char;
|
|
144
|
+
}
|
|
145
|
+
return output;
|
|
146
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { describe, it } from "node:test";
|
|
3
|
+
import { buildOpencodeConfigContent } from "./opencode-plugin";
|
|
4
|
+
describe("buildOpencodeConfigContent", () => {
|
|
5
|
+
it("creates config content with the CodeNomad plugin", () => {
|
|
6
|
+
const content = buildOpencodeConfigContent(undefined, "file:///plugin.tgz");
|
|
7
|
+
assert.deepEqual(JSON.parse(content), {
|
|
8
|
+
"$schema": "https://opencode.ai/config.json",
|
|
9
|
+
plugin: ["file:///plugin.tgz"],
|
|
10
|
+
});
|
|
11
|
+
});
|
|
12
|
+
it("merges with existing JSONC content", () => {
|
|
13
|
+
const content = buildOpencodeConfigContent(`{
|
|
14
|
+
// user plugin
|
|
15
|
+
"plugin": ["npm:user-plugin",],
|
|
16
|
+
"model": "test-model",
|
|
17
|
+
}`, "file:///plugin.tgz");
|
|
18
|
+
assert.deepEqual(JSON.parse(content), {
|
|
19
|
+
"$schema": "https://opencode.ai/config.json",
|
|
20
|
+
plugin: ["npm:user-plugin", "file:///plugin.tgz"],
|
|
21
|
+
model: "test-model",
|
|
22
|
+
});
|
|
23
|
+
});
|
|
24
|
+
it("does not duplicate the CodeNomad plugin", () => {
|
|
25
|
+
const content = buildOpencodeConfigContent('{"plugin":["file:///plugin.tgz"]}', "file:///plugin.tgz");
|
|
26
|
+
assert.deepEqual(JSON.parse(content).plugin, ["file:///plugin.tgz"]);
|
|
27
|
+
});
|
|
28
|
+
});
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { randomUUID } from "crypto";
|
|
2
|
+
export class PreviewManager {
|
|
3
|
+
constructor() {
|
|
4
|
+
this.previews = new Map();
|
|
5
|
+
}
|
|
6
|
+
create(sessionId, rawUrl) {
|
|
7
|
+
const target = this.normalizeTargetUrl(rawUrl);
|
|
8
|
+
const token = randomUUID();
|
|
9
|
+
const record = {
|
|
10
|
+
token,
|
|
11
|
+
sessionId,
|
|
12
|
+
target,
|
|
13
|
+
createdAt: new Date().toISOString(),
|
|
14
|
+
};
|
|
15
|
+
this.previews.set(token, record);
|
|
16
|
+
return this.toPreviewSession(record);
|
|
17
|
+
}
|
|
18
|
+
get(token) {
|
|
19
|
+
const record = this.previews.get(token);
|
|
20
|
+
return record ? this.toPreviewSession(record) : undefined;
|
|
21
|
+
}
|
|
22
|
+
delete(token) {
|
|
23
|
+
return this.previews.delete(token);
|
|
24
|
+
}
|
|
25
|
+
buildTargetUrl(token, incomingPath, search = "") {
|
|
26
|
+
const record = this.previews.get(token);
|
|
27
|
+
if (!record)
|
|
28
|
+
return undefined;
|
|
29
|
+
const publicBase = this.buildProxyBasePath(token);
|
|
30
|
+
let targetPath = incomingPath.startsWith(publicBase) ? incomingPath.slice(publicBase.length) : incomingPath;
|
|
31
|
+
if (!targetPath || targetPath === "/") {
|
|
32
|
+
targetPath = record.target.pathname || "/";
|
|
33
|
+
}
|
|
34
|
+
else if (!targetPath.startsWith("/")) {
|
|
35
|
+
targetPath = `/${targetPath}`;
|
|
36
|
+
}
|
|
37
|
+
return new URL(`${targetPath}${search}`, record.target.origin);
|
|
38
|
+
}
|
|
39
|
+
buildProxyBasePath(token) {
|
|
40
|
+
return `/previews/${encodeURIComponent(token)}`;
|
|
41
|
+
}
|
|
42
|
+
normalizeTargetUrl(rawUrl) {
|
|
43
|
+
const trimmed = rawUrl.trim();
|
|
44
|
+
const withProtocol = /^[a-z][a-z0-9+.-]*:/i.test(trimmed) ? trimmed : `https://${trimmed}`;
|
|
45
|
+
const target = new URL(withProtocol);
|
|
46
|
+
if (target.protocol !== "http:" && target.protocol !== "https:") {
|
|
47
|
+
throw new Error("Preview URL must use HTTP or HTTPS");
|
|
48
|
+
}
|
|
49
|
+
if (target.username || target.password) {
|
|
50
|
+
throw new Error("Preview URL cannot include credentials");
|
|
51
|
+
}
|
|
52
|
+
return target;
|
|
53
|
+
}
|
|
54
|
+
toPreviewSession(record) {
|
|
55
|
+
return {
|
|
56
|
+
token: record.token,
|
|
57
|
+
sessionId: record.sessionId,
|
|
58
|
+
targetUrl: record.target.toString(),
|
|
59
|
+
proxyUrl: `${this.buildProxyBasePath(record.token)}${record.target.pathname}${record.target.search}${record.target.hash}`,
|
|
60
|
+
createdAt: record.createdAt,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
}
|