@neuralnomads/codenomad 0.7.5 → 0.8.1
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/events/bus.js +0 -2
- package/dist/index.js +40 -16
- package/dist/opencode-config/package.json +1 -1
- package/dist/ui/__tests__/remote-ui.test.js +46 -0
- package/dist/ui/remote-ui.js +462 -0
- package/package.json +3 -1
- package/public/assets/index-CbktiZIE.css +1 -0
- package/public/assets/{loading-A3c3OC91.js → loading-BZSKFGkV.js} +1 -1
- package/public/assets/main-D5c_2IWi.js +184 -0
- package/public/index.html +3 -3
- package/public/loading.html +3 -3
- package/public/ui-version.json +3 -0
- package/public/assets/index-DhjiW0WU.css +0 -1
- package/public/assets/main-C_3DbNp8.js +0 -184
- /package/public/assets/{index-D4j6wgeo.js → index-ipdyZsa8.js} +0 -0
package/dist/events/bus.js
CHANGED
|
@@ -25,7 +25,6 @@ export class EventBus extends EventEmitter {
|
|
|
25
25
|
this.on("instance.dataChanged", handler);
|
|
26
26
|
this.on("instance.event", handler);
|
|
27
27
|
this.on("instance.eventStatus", handler);
|
|
28
|
-
this.on("app.releaseAvailable", handler);
|
|
29
28
|
return () => {
|
|
30
29
|
this.off("workspace.created", handler);
|
|
31
30
|
this.off("workspace.started", handler);
|
|
@@ -37,7 +36,6 @@ export class EventBus extends EventEmitter {
|
|
|
37
36
|
this.off("instance.dataChanged", handler);
|
|
38
37
|
this.off("instance.event", handler);
|
|
39
38
|
this.off("instance.eventStatus", handler);
|
|
40
|
-
this.off("app.releaseAvailable", handler);
|
|
41
39
|
};
|
|
42
40
|
}
|
|
43
41
|
}
|
package/dist/index.js
CHANGED
|
@@ -16,7 +16,7 @@ import { InstanceStore } from "./storage/instance-store";
|
|
|
16
16
|
import { InstanceEventBridge } from "./workspaces/instance-events";
|
|
17
17
|
import { createLogger } from "./logger";
|
|
18
18
|
import { launchInBrowser } from "./launcher";
|
|
19
|
-
import {
|
|
19
|
+
import { resolveUi } from "./ui/remote-ui";
|
|
20
20
|
import { AuthManager, BOOTSTRAP_TOKEN_STDOUT_PREFIX, DEFAULT_AUTH_USERNAME } from "./auth/manager";
|
|
21
21
|
const require = createRequire(import.meta.url);
|
|
22
22
|
const packageJson = require("../package.json");
|
|
@@ -41,6 +41,9 @@ function parseCliOptions(argv) {
|
|
|
41
41
|
.addOption(new Option("--log-destination <path>", "Log destination file (defaults to stdout)").env("CLI_LOG_DESTINATION"))
|
|
42
42
|
.addOption(new Option("--ui-dir <path>", "Directory containing the built UI bundle").env("CLI_UI_DIR").default(DEFAULT_UI_STATIC_DIR))
|
|
43
43
|
.addOption(new Option("--ui-dev-server <url>", "Proxy UI requests to a running dev server").env("CLI_UI_DEV_SERVER"))
|
|
44
|
+
.addOption(new Option("--ui-no-update", "Disable remote UI updates").env("CLI_UI_NO_UPDATE").default(false))
|
|
45
|
+
.addOption(new Option("--ui-auto-update <enabled>", "Enable remote UI updates (true|false)").env("CLI_UI_AUTO_UPDATE").default("true"))
|
|
46
|
+
.addOption(new Option("--ui-manifest-url <url>", "Remote UI manifest URL").env("CLI_UI_MANIFEST_URL"))
|
|
44
47
|
.addOption(new Option("--launch", "Launch the UI in a browser after start").env("CLI_LAUNCH").default(false))
|
|
45
48
|
.addOption(new Option("--username <username>", "Username for server authentication")
|
|
46
49
|
.env("CODENOMAD_SERVER_USERNAME")
|
|
@@ -53,6 +56,8 @@ function parseCliOptions(argv) {
|
|
|
53
56
|
const parsed = program.opts();
|
|
54
57
|
const resolvedRoot = parsed.workspaceRoot ?? parsed.root ?? process.cwd();
|
|
55
58
|
const normalizedHost = resolveHost(parsed.host);
|
|
59
|
+
const autoUpdateString = (parsed.uiAutoUpdate ?? "true").trim().toLowerCase();
|
|
60
|
+
const uiAutoUpdate = autoUpdateString === "1" || autoUpdateString === "true" || autoUpdateString === "yes";
|
|
56
61
|
return {
|
|
57
62
|
port: parsed.port,
|
|
58
63
|
host: normalizedHost,
|
|
@@ -63,6 +68,9 @@ function parseCliOptions(argv) {
|
|
|
63
68
|
logDestination: parsed.logDestination,
|
|
64
69
|
uiStaticDir: parsed.uiDir,
|
|
65
70
|
uiDevServer: parsed.uiDevServer,
|
|
71
|
+
uiAutoUpdate,
|
|
72
|
+
uiNoUpdate: Boolean(parsed.uiNoUpdate),
|
|
73
|
+
uiManifestUrl: parsed.uiManifestUrl,
|
|
66
74
|
launch: Boolean(parsed.launch),
|
|
67
75
|
authUsername: parsed.username,
|
|
68
76
|
authPassword: parsed.password,
|
|
@@ -88,6 +96,9 @@ function resolveHost(input) {
|
|
|
88
96
|
}
|
|
89
97
|
return trimmed;
|
|
90
98
|
}
|
|
99
|
+
function programHasArg(argv, flag) {
|
|
100
|
+
return argv.includes(flag);
|
|
101
|
+
}
|
|
91
102
|
async function main() {
|
|
92
103
|
const options = parseCliOptions(process.argv.slice(2));
|
|
93
104
|
const logger = createLogger({ level: options.logLevel, destination: options.logDestination, component: "app" });
|
|
@@ -140,19 +151,32 @@ async function main() {
|
|
|
140
151
|
eventBus,
|
|
141
152
|
logger: logger.child({ component: "instance-events" }),
|
|
142
153
|
});
|
|
143
|
-
const
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
154
|
+
const uiDirEnvOverride = Boolean(process.env.CLI_UI_DIR);
|
|
155
|
+
const uiDirCliOverride = programHasArg(process.argv.slice(2), "--ui-dir");
|
|
156
|
+
const uiOverrideIsExplicit = uiDirEnvOverride || uiDirCliOverride;
|
|
157
|
+
const uiDirOverride = uiOverrideIsExplicit ? options.uiStaticDir : undefined;
|
|
158
|
+
const autoUpdateEnabled = options.uiAutoUpdate && !options.uiNoUpdate;
|
|
159
|
+
const uiResolution = await resolveUi({
|
|
160
|
+
serverVersion: packageJson.version,
|
|
161
|
+
bundledUiDir: DEFAULT_UI_STATIC_DIR,
|
|
162
|
+
autoUpdate: autoUpdateEnabled,
|
|
163
|
+
overrideUiDir: uiDirOverride,
|
|
164
|
+
uiDevServerUrl: options.uiDevServer,
|
|
165
|
+
manifestUrl: options.uiManifestUrl,
|
|
166
|
+
logger: logger.child({ component: "ui" }),
|
|
155
167
|
});
|
|
168
|
+
serverMeta.serverVersion = packageJson.version;
|
|
169
|
+
serverMeta.ui = {
|
|
170
|
+
version: uiResolution.uiVersion,
|
|
171
|
+
source: uiResolution.source,
|
|
172
|
+
};
|
|
173
|
+
serverMeta.support = {
|
|
174
|
+
supported: uiResolution.supported,
|
|
175
|
+
message: uiResolution.message,
|
|
176
|
+
latestServerVersion: uiResolution.latestServerVersion,
|
|
177
|
+
latestServerUrl: uiResolution.latestServerUrl,
|
|
178
|
+
minServerVersion: uiResolution.minServerVersion,
|
|
179
|
+
};
|
|
156
180
|
const server = createHttpServer({
|
|
157
181
|
host: options.host,
|
|
158
182
|
port: options.port,
|
|
@@ -164,8 +188,8 @@ async function main() {
|
|
|
164
188
|
serverMeta,
|
|
165
189
|
instanceStore,
|
|
166
190
|
authManager,
|
|
167
|
-
uiStaticDir:
|
|
168
|
-
uiDevServerUrl:
|
|
191
|
+
uiStaticDir: uiResolution.uiStaticDir ?? DEFAULT_UI_STATIC_DIR,
|
|
192
|
+
uiDevServerUrl: uiResolution.uiDevServerUrl,
|
|
169
193
|
logger,
|
|
170
194
|
});
|
|
171
195
|
const startInfo = await server.start();
|
|
@@ -197,7 +221,7 @@ async function main() {
|
|
|
197
221
|
catch (error) {
|
|
198
222
|
logger.error({ err: error }, "Workspace manager shutdown failed");
|
|
199
223
|
}
|
|
200
|
-
|
|
224
|
+
// no-op: remote UI manifest replaces GitHub release monitor
|
|
201
225
|
logger.info("Exiting process");
|
|
202
226
|
process.exit(0);
|
|
203
227
|
};
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { mkdir } from "node:fs/promises";
|
|
4
|
+
import os from "node:os";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
import { afterEach, beforeEach, describe, it } from "node:test";
|
|
7
|
+
import { resolveUi } from "../remote-ui";
|
|
8
|
+
const noopLogger = {
|
|
9
|
+
debug: () => { },
|
|
10
|
+
info: () => { },
|
|
11
|
+
warn: () => { },
|
|
12
|
+
error: () => { },
|
|
13
|
+
trace: () => { },
|
|
14
|
+
child: () => noopLogger,
|
|
15
|
+
isLevelEnabled: () => false,
|
|
16
|
+
};
|
|
17
|
+
let tempRoot;
|
|
18
|
+
beforeEach(() => {
|
|
19
|
+
tempRoot = mkdtempSync(path.join(os.tmpdir(), "codenomad-ui-test-"));
|
|
20
|
+
});
|
|
21
|
+
afterEach(() => {
|
|
22
|
+
rmSync(tempRoot, { recursive: true, force: true });
|
|
23
|
+
});
|
|
24
|
+
describe("resolveUi local version preference", () => {
|
|
25
|
+
it("prefers bundled when bundled version is higher", async () => {
|
|
26
|
+
const bundledDir = path.join(tempRoot, "bundled");
|
|
27
|
+
const configDir = path.join(tempRoot, "config");
|
|
28
|
+
const currentDir = path.join(configDir, "ui", "current");
|
|
29
|
+
await mkdir(bundledDir, { recursive: true });
|
|
30
|
+
await mkdir(currentDir, { recursive: true });
|
|
31
|
+
writeFileSync(path.join(bundledDir, "index.html"), "<html>bundled</html>");
|
|
32
|
+
writeFileSync(path.join(bundledDir, "ui-version.json"), JSON.stringify({ uiVersion: "0.8.1" }));
|
|
33
|
+
writeFileSync(path.join(currentDir, "index.html"), "<html>current</html>");
|
|
34
|
+
writeFileSync(path.join(currentDir, "ui-version.json"), JSON.stringify({ uiVersion: "0.8.0" }));
|
|
35
|
+
const result = await resolveUi({
|
|
36
|
+
serverVersion: "0.8.1",
|
|
37
|
+
bundledUiDir: bundledDir,
|
|
38
|
+
autoUpdate: false,
|
|
39
|
+
configDir,
|
|
40
|
+
logger: noopLogger,
|
|
41
|
+
});
|
|
42
|
+
assert.equal(result.source, "bundled");
|
|
43
|
+
assert.equal(result.uiStaticDir, bundledDir);
|
|
44
|
+
assert.equal(result.uiVersion, "0.8.1");
|
|
45
|
+
});
|
|
46
|
+
});
|
|
@@ -0,0 +1,462 @@
|
|
|
1
|
+
import { createHash } from "crypto";
|
|
2
|
+
import fs from "fs";
|
|
3
|
+
import { promises as fsp } from "fs";
|
|
4
|
+
import os from "os";
|
|
5
|
+
import path from "path";
|
|
6
|
+
import { Readable } from "stream";
|
|
7
|
+
import { fetch } from "undici";
|
|
8
|
+
import yauzl from "yauzl";
|
|
9
|
+
const DEFAULT_MANIFEST_URL = "https://ui.codenomad.neuralnomads.ai/version.json";
|
|
10
|
+
const MANIFEST_TIMEOUT_MS = 5000;
|
|
11
|
+
const ZIP_TIMEOUT_MS = 30000;
|
|
12
|
+
export async function resolveUi(options) {
|
|
13
|
+
const manifestUrl = options.manifestUrl ?? DEFAULT_MANIFEST_URL;
|
|
14
|
+
if (options.uiDevServerUrl) {
|
|
15
|
+
return {
|
|
16
|
+
uiDevServerUrl: options.uiDevServerUrl,
|
|
17
|
+
source: "dev-proxy",
|
|
18
|
+
supported: true,
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
if (options.overrideUiDir) {
|
|
22
|
+
const resolved = await resolveStaticUiDir(options.overrideUiDir);
|
|
23
|
+
return {
|
|
24
|
+
uiStaticDir: resolved ?? options.overrideUiDir,
|
|
25
|
+
source: "override",
|
|
26
|
+
uiVersion: await readUiVersion(resolved ?? options.overrideUiDir),
|
|
27
|
+
supported: true,
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
const uiRoot = resolveUiCacheRoot(options.configDir);
|
|
31
|
+
const currentDir = path.join(uiRoot, "current");
|
|
32
|
+
const previousDir = path.join(uiRoot, "previous");
|
|
33
|
+
if (!options.autoUpdate) {
|
|
34
|
+
return await resolveFromCacheOrBundled({
|
|
35
|
+
logger: options.logger,
|
|
36
|
+
bundledUiDir: options.bundledUiDir,
|
|
37
|
+
currentDir,
|
|
38
|
+
previousDir,
|
|
39
|
+
supported: true,
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
let manifest = null;
|
|
43
|
+
try {
|
|
44
|
+
manifest = await fetchManifest(manifestUrl, options.logger);
|
|
45
|
+
}
|
|
46
|
+
catch (error) {
|
|
47
|
+
options.logger.debug({ err: error }, "Remote UI manifest unavailable; using cached/bundled UI");
|
|
48
|
+
}
|
|
49
|
+
if (!manifest) {
|
|
50
|
+
return await resolveFromCacheOrBundled({
|
|
51
|
+
logger: options.logger,
|
|
52
|
+
bundledUiDir: options.bundledUiDir,
|
|
53
|
+
currentDir,
|
|
54
|
+
previousDir,
|
|
55
|
+
supported: true,
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
const supported = compareSemverCore(options.serverVersion, manifest.minServerVersion) >= 0;
|
|
59
|
+
if (!supported) {
|
|
60
|
+
const message = "Upgrade App to use latest features";
|
|
61
|
+
return await resolveFromCacheOrBundled({
|
|
62
|
+
logger: options.logger,
|
|
63
|
+
bundledUiDir: options.bundledUiDir,
|
|
64
|
+
currentDir,
|
|
65
|
+
previousDir,
|
|
66
|
+
supported: false,
|
|
67
|
+
message,
|
|
68
|
+
latestServerVersion: manifest.latestServerVersion,
|
|
69
|
+
latestServerUrl: manifest.latestServerUrl,
|
|
70
|
+
minServerVersion: manifest.minServerVersion,
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
const bestLocal = await pickBestLocalUi({
|
|
74
|
+
logger: options.logger,
|
|
75
|
+
bundledUiDir: options.bundledUiDir,
|
|
76
|
+
currentDir,
|
|
77
|
+
previousDir,
|
|
78
|
+
});
|
|
79
|
+
const remoteIsNewer = !bestLocal ||
|
|
80
|
+
compareSemverMaybe(manifest.latestUIVersion, bestLocal.uiVersion) > 0;
|
|
81
|
+
if (!remoteIsNewer) {
|
|
82
|
+
return await resolveFromCacheOrBundled({
|
|
83
|
+
logger: options.logger,
|
|
84
|
+
bundledUiDir: options.bundledUiDir,
|
|
85
|
+
currentDir,
|
|
86
|
+
previousDir,
|
|
87
|
+
supported: true,
|
|
88
|
+
latestServerVersion: manifest.latestServerVersion,
|
|
89
|
+
latestServerUrl: manifest.latestServerUrl,
|
|
90
|
+
minServerVersion: manifest.minServerVersion,
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
try {
|
|
94
|
+
await installRemoteUi({
|
|
95
|
+
manifest,
|
|
96
|
+
uiRoot,
|
|
97
|
+
currentDir,
|
|
98
|
+
previousDir,
|
|
99
|
+
logger: options.logger,
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
catch (error) {
|
|
103
|
+
options.logger.warn({ err: error }, "Failed to install remote UI; falling back");
|
|
104
|
+
return await resolveFromCacheOrBundled({
|
|
105
|
+
logger: options.logger,
|
|
106
|
+
bundledUiDir: options.bundledUiDir,
|
|
107
|
+
currentDir,
|
|
108
|
+
previousDir,
|
|
109
|
+
supported: true,
|
|
110
|
+
latestServerVersion: manifest.latestServerVersion,
|
|
111
|
+
latestServerUrl: manifest.latestServerUrl,
|
|
112
|
+
minServerVersion: manifest.minServerVersion,
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
const installed = await resolveStaticUiDir(currentDir);
|
|
116
|
+
if (installed) {
|
|
117
|
+
return {
|
|
118
|
+
uiStaticDir: installed,
|
|
119
|
+
source: "downloaded",
|
|
120
|
+
uiVersion: await readUiVersion(installed),
|
|
121
|
+
supported: true,
|
|
122
|
+
latestServerVersion: manifest.latestServerVersion,
|
|
123
|
+
latestServerUrl: manifest.latestServerUrl,
|
|
124
|
+
minServerVersion: manifest.minServerVersion,
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
return await resolveFromCacheOrBundled({
|
|
128
|
+
logger: options.logger,
|
|
129
|
+
bundledUiDir: options.bundledUiDir,
|
|
130
|
+
currentDir,
|
|
131
|
+
previousDir,
|
|
132
|
+
supported: true,
|
|
133
|
+
latestServerVersion: manifest.latestServerVersion,
|
|
134
|
+
latestServerUrl: manifest.latestServerUrl,
|
|
135
|
+
minServerVersion: manifest.minServerVersion,
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
function resolveUiCacheRoot(configDir) {
|
|
139
|
+
if (configDir) {
|
|
140
|
+
return path.join(configDir, "ui");
|
|
141
|
+
}
|
|
142
|
+
return path.join(os.homedir(), ".config", "codenomad", "ui");
|
|
143
|
+
}
|
|
144
|
+
async function resolveFromCacheOrBundled(args) {
|
|
145
|
+
const bestLocal = await pickBestLocalUi({
|
|
146
|
+
logger: args.logger,
|
|
147
|
+
bundledUiDir: args.bundledUiDir,
|
|
148
|
+
currentDir: args.currentDir,
|
|
149
|
+
previousDir: args.previousDir,
|
|
150
|
+
});
|
|
151
|
+
if (bestLocal) {
|
|
152
|
+
return {
|
|
153
|
+
uiStaticDir: bestLocal.uiStaticDir,
|
|
154
|
+
source: bestLocal.source,
|
|
155
|
+
uiVersion: bestLocal.uiVersion,
|
|
156
|
+
supported: args.supported,
|
|
157
|
+
message: args.message,
|
|
158
|
+
latestServerVersion: args.latestServerVersion,
|
|
159
|
+
latestServerUrl: args.latestServerUrl,
|
|
160
|
+
minServerVersion: args.minServerVersion,
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
args.logger.warn({ bundledUiDir: args.bundledUiDir }, "No UI assets found");
|
|
164
|
+
return {
|
|
165
|
+
uiStaticDir: args.bundledUiDir,
|
|
166
|
+
source: "missing",
|
|
167
|
+
supported: args.supported,
|
|
168
|
+
message: args.message,
|
|
169
|
+
latestServerVersion: args.latestServerVersion,
|
|
170
|
+
latestServerUrl: args.latestServerUrl,
|
|
171
|
+
minServerVersion: args.minServerVersion,
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
async function pickBestLocalUi(args) {
|
|
175
|
+
const candidates = [];
|
|
176
|
+
const currentResolved = await resolveStaticUiDir(args.currentDir);
|
|
177
|
+
if (currentResolved) {
|
|
178
|
+
candidates.push({
|
|
179
|
+
uiStaticDir: currentResolved,
|
|
180
|
+
source: "downloaded",
|
|
181
|
+
uiVersion: await readUiVersion(currentResolved),
|
|
182
|
+
priority: 2,
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
const bundledResolved = await resolveStaticUiDir(args.bundledUiDir);
|
|
186
|
+
if (bundledResolved) {
|
|
187
|
+
candidates.push({
|
|
188
|
+
uiStaticDir: bundledResolved,
|
|
189
|
+
source: "bundled",
|
|
190
|
+
uiVersion: await readUiVersion(bundledResolved),
|
|
191
|
+
priority: 1,
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
const previousResolved = await resolveStaticUiDir(args.previousDir);
|
|
195
|
+
if (previousResolved) {
|
|
196
|
+
candidates.push({
|
|
197
|
+
uiStaticDir: previousResolved,
|
|
198
|
+
source: "previous",
|
|
199
|
+
uiVersion: await readUiVersion(previousResolved),
|
|
200
|
+
priority: 0,
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
if (candidates.length === 0) {
|
|
204
|
+
return null;
|
|
205
|
+
}
|
|
206
|
+
candidates.sort((a, b) => {
|
|
207
|
+
const versionCmp = compareSemverMaybe(a.uiVersion, b.uiVersion);
|
|
208
|
+
if (versionCmp !== 0)
|
|
209
|
+
return -versionCmp;
|
|
210
|
+
return b.priority - a.priority;
|
|
211
|
+
});
|
|
212
|
+
const best = candidates[0];
|
|
213
|
+
if (!best)
|
|
214
|
+
return null;
|
|
215
|
+
return { uiStaticDir: best.uiStaticDir, source: best.source, uiVersion: best.uiVersion };
|
|
216
|
+
}
|
|
217
|
+
function compareSemverMaybe(a, b) {
|
|
218
|
+
if (!a && !b)
|
|
219
|
+
return 0;
|
|
220
|
+
if (!a)
|
|
221
|
+
return -1;
|
|
222
|
+
if (!b)
|
|
223
|
+
return 1;
|
|
224
|
+
return compareSemverCore(a, b);
|
|
225
|
+
}
|
|
226
|
+
async function resolveStaticUiDir(uiDir) {
|
|
227
|
+
try {
|
|
228
|
+
const indexPath = path.join(uiDir, "index.html");
|
|
229
|
+
await fsp.access(indexPath, fs.constants.R_OK);
|
|
230
|
+
return uiDir;
|
|
231
|
+
}
|
|
232
|
+
catch {
|
|
233
|
+
return null;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
async function readUiVersion(uiDir) {
|
|
237
|
+
try {
|
|
238
|
+
const content = await fsp.readFile(path.join(uiDir, "ui-version.json"), "utf-8");
|
|
239
|
+
const parsed = JSON.parse(content);
|
|
240
|
+
return parsed.uiVersion ?? parsed.version;
|
|
241
|
+
}
|
|
242
|
+
catch {
|
|
243
|
+
return undefined;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
async function fetchManifest(url, logger) {
|
|
247
|
+
const controller = new AbortController();
|
|
248
|
+
const timeout = setTimeout(() => controller.abort(), MANIFEST_TIMEOUT_MS);
|
|
249
|
+
try {
|
|
250
|
+
const response = await fetch(url, {
|
|
251
|
+
signal: controller.signal,
|
|
252
|
+
headers: {
|
|
253
|
+
Accept: "application/json",
|
|
254
|
+
"User-Agent": "CodeNomad-CLI",
|
|
255
|
+
},
|
|
256
|
+
});
|
|
257
|
+
if (!response.ok) {
|
|
258
|
+
throw new Error(`Manifest responded with ${response.status}`);
|
|
259
|
+
}
|
|
260
|
+
const json = (await response.json());
|
|
261
|
+
validateManifest(json);
|
|
262
|
+
return json;
|
|
263
|
+
}
|
|
264
|
+
catch (error) {
|
|
265
|
+
logger.debug({ err: error, url }, "Failed to fetch remote UI manifest");
|
|
266
|
+
throw error;
|
|
267
|
+
}
|
|
268
|
+
finally {
|
|
269
|
+
clearTimeout(timeout);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
function validateManifest(manifest) {
|
|
273
|
+
const required = ["minServerVersion", "latestUIVersion", "uiPackageURL", "sha256"];
|
|
274
|
+
for (const key of required) {
|
|
275
|
+
const value = manifest[key];
|
|
276
|
+
if (typeof value !== "string" || value.trim().length === 0) {
|
|
277
|
+
throw new Error(`Manifest missing ${key}`);
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
if (!/^https:\/\//i.test(manifest.uiPackageURL)) {
|
|
281
|
+
throw new Error("uiPackageURL must be https");
|
|
282
|
+
}
|
|
283
|
+
if (!/^[a-f0-9]{64}$/i.test(manifest.sha256.trim())) {
|
|
284
|
+
throw new Error("sha256 must be 64 hex chars");
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
async function installRemoteUi(args) {
|
|
288
|
+
await fsp.mkdir(args.uiRoot, { recursive: true });
|
|
289
|
+
const tmpDir = path.join(args.uiRoot, `tmp-${Date.now()}`);
|
|
290
|
+
const zipPath = path.join(args.uiRoot, `ui-${args.manifest.latestUIVersion}.zip`);
|
|
291
|
+
try {
|
|
292
|
+
await downloadFile(args.manifest.uiPackageURL, zipPath, args.logger);
|
|
293
|
+
const digest = await sha256File(zipPath);
|
|
294
|
+
if (digest.toLowerCase() !== args.manifest.sha256.toLowerCase()) {
|
|
295
|
+
throw new Error(`sha256 mismatch for UI zip (expected ${args.manifest.sha256}, got ${digest})`);
|
|
296
|
+
}
|
|
297
|
+
await extractZip(zipPath, tmpDir);
|
|
298
|
+
const indexPath = path.join(tmpDir, "index.html");
|
|
299
|
+
if (!fs.existsSync(indexPath)) {
|
|
300
|
+
throw new Error("Extracted UI missing index.html");
|
|
301
|
+
}
|
|
302
|
+
await rotateDirs({ currentDir: args.currentDir, previousDir: args.previousDir, logger: args.logger });
|
|
303
|
+
fs.rmSync(args.currentDir, { recursive: true, force: true });
|
|
304
|
+
fs.renameSync(tmpDir, args.currentDir);
|
|
305
|
+
}
|
|
306
|
+
finally {
|
|
307
|
+
fs.rmSync(tmpDir, { recursive: true, force: true });
|
|
308
|
+
fs.rmSync(zipPath, { force: true });
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
async function rotateDirs(args) {
|
|
312
|
+
try {
|
|
313
|
+
if (fs.existsSync(args.previousDir)) {
|
|
314
|
+
fs.rmSync(args.previousDir, { recursive: true, force: true });
|
|
315
|
+
}
|
|
316
|
+
if (fs.existsSync(args.currentDir)) {
|
|
317
|
+
fs.renameSync(args.currentDir, args.previousDir);
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
catch (error) {
|
|
321
|
+
args.logger.warn({ err: error }, "Failed to rotate UI cache directories");
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
async function downloadFile(url, targetPath, logger) {
|
|
325
|
+
const controller = new AbortController();
|
|
326
|
+
const timeout = setTimeout(() => controller.abort(), ZIP_TIMEOUT_MS);
|
|
327
|
+
try {
|
|
328
|
+
const response = await fetch(url, {
|
|
329
|
+
signal: controller.signal,
|
|
330
|
+
headers: {
|
|
331
|
+
Accept: "application/octet-stream",
|
|
332
|
+
"User-Agent": "CodeNomad-CLI",
|
|
333
|
+
},
|
|
334
|
+
});
|
|
335
|
+
if (!response.ok || !response.body) {
|
|
336
|
+
throw new Error(`UI zip download failed with ${response.status}`);
|
|
337
|
+
}
|
|
338
|
+
await fsp.mkdir(path.dirname(targetPath), { recursive: true });
|
|
339
|
+
const fileStream = fs.createWriteStream(targetPath);
|
|
340
|
+
const body = response.body;
|
|
341
|
+
if (!body) {
|
|
342
|
+
throw new Error("UI zip response missing body");
|
|
343
|
+
}
|
|
344
|
+
const nodeStream = Readable.fromWeb(body);
|
|
345
|
+
await new Promise((resolve, reject) => {
|
|
346
|
+
nodeStream.pipe(fileStream);
|
|
347
|
+
nodeStream.on("error", reject);
|
|
348
|
+
fileStream.on("error", reject);
|
|
349
|
+
fileStream.on("finish", () => resolve());
|
|
350
|
+
});
|
|
351
|
+
logger.debug({ url, targetPath }, "Downloaded remote UI bundle");
|
|
352
|
+
}
|
|
353
|
+
finally {
|
|
354
|
+
clearTimeout(timeout);
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
async function sha256File(filePath) {
|
|
358
|
+
const hash = createHash("sha256");
|
|
359
|
+
const stream = fs.createReadStream(filePath);
|
|
360
|
+
await new Promise((resolve, reject) => {
|
|
361
|
+
stream.on("data", (chunk) => hash.update(chunk));
|
|
362
|
+
stream.on("error", reject);
|
|
363
|
+
stream.on("end", () => resolve());
|
|
364
|
+
});
|
|
365
|
+
return hash.digest("hex");
|
|
366
|
+
}
|
|
367
|
+
async function extractZip(zipPath, targetDir) {
|
|
368
|
+
await fsp.mkdir(targetDir, { recursive: true });
|
|
369
|
+
await new Promise((resolve, reject) => {
|
|
370
|
+
yauzl.open(zipPath, { lazyEntries: true }, (openErr, zipfile) => {
|
|
371
|
+
if (openErr || !zipfile) {
|
|
372
|
+
reject(openErr ?? new Error("Unable to open zip"));
|
|
373
|
+
return;
|
|
374
|
+
}
|
|
375
|
+
const root = path.resolve(targetDir);
|
|
376
|
+
const closeWithError = (error) => {
|
|
377
|
+
try {
|
|
378
|
+
zipfile.close();
|
|
379
|
+
}
|
|
380
|
+
catch {
|
|
381
|
+
// ignore
|
|
382
|
+
}
|
|
383
|
+
reject(error);
|
|
384
|
+
};
|
|
385
|
+
zipfile.readEntry();
|
|
386
|
+
zipfile.on("entry", (entry) => {
|
|
387
|
+
// Normalize and guard against zip-slip.
|
|
388
|
+
const entryPath = entry.fileName.replace(/\\/g, "/");
|
|
389
|
+
const segments = entryPath.split("/").filter(Boolean);
|
|
390
|
+
if (segments.some((segment) => segment === "..") || path.isAbsolute(entryPath)) {
|
|
391
|
+
closeWithError(new Error(`Invalid zip entry path: ${entry.fileName}`));
|
|
392
|
+
return;
|
|
393
|
+
}
|
|
394
|
+
const destination = path.resolve(targetDir, entryPath);
|
|
395
|
+
if (!destination.startsWith(root + path.sep) && destination !== root) {
|
|
396
|
+
closeWithError(new Error(`Zip entry escapes target dir: ${entry.fileName}`));
|
|
397
|
+
return;
|
|
398
|
+
}
|
|
399
|
+
const isDirectory = entry.fileName.endsWith("/");
|
|
400
|
+
if (isDirectory) {
|
|
401
|
+
fsp
|
|
402
|
+
.mkdir(destination, { recursive: true })
|
|
403
|
+
.then(() => zipfile.readEntry())
|
|
404
|
+
.catch((error) => closeWithError(error));
|
|
405
|
+
return;
|
|
406
|
+
}
|
|
407
|
+
fsp
|
|
408
|
+
.mkdir(path.dirname(destination), { recursive: true })
|
|
409
|
+
.then(() => {
|
|
410
|
+
zipfile.openReadStream(entry, (streamErr, readStream) => {
|
|
411
|
+
if (streamErr || !readStream) {
|
|
412
|
+
closeWithError(streamErr ?? new Error("Unable to read zip entry"));
|
|
413
|
+
return;
|
|
414
|
+
}
|
|
415
|
+
const writeStream = fs.createWriteStream(destination);
|
|
416
|
+
const cleanup = (error) => {
|
|
417
|
+
readStream.destroy();
|
|
418
|
+
writeStream.destroy();
|
|
419
|
+
if (error) {
|
|
420
|
+
closeWithError(error);
|
|
421
|
+
}
|
|
422
|
+
};
|
|
423
|
+
readStream.on("error", cleanup);
|
|
424
|
+
writeStream.on("error", cleanup);
|
|
425
|
+
writeStream.on("finish", () => zipfile.readEntry());
|
|
426
|
+
readStream.pipe(writeStream);
|
|
427
|
+
});
|
|
428
|
+
})
|
|
429
|
+
.catch((error) => closeWithError(error));
|
|
430
|
+
});
|
|
431
|
+
zipfile.on("end", () => {
|
|
432
|
+
zipfile.close();
|
|
433
|
+
resolve();
|
|
434
|
+
});
|
|
435
|
+
zipfile.on("error", (error) => closeWithError(error));
|
|
436
|
+
});
|
|
437
|
+
});
|
|
438
|
+
}
|
|
439
|
+
function compareSemverCore(a, b) {
|
|
440
|
+
const pa = parseSemverCore(a);
|
|
441
|
+
const pb = parseSemverCore(b);
|
|
442
|
+
if (pa.major !== pb.major)
|
|
443
|
+
return pa.major > pb.major ? 1 : -1;
|
|
444
|
+
if (pa.minor !== pb.minor)
|
|
445
|
+
return pa.minor > pb.minor ? 1 : -1;
|
|
446
|
+
if (pa.patch !== pb.patch)
|
|
447
|
+
return pa.patch > pb.patch ? 1 : -1;
|
|
448
|
+
return 0;
|
|
449
|
+
}
|
|
450
|
+
function parseSemverCore(value) {
|
|
451
|
+
const core = value.trim().replace(/^v/i, "").split("-", 1)[0] ?? "0.0.0";
|
|
452
|
+
const parts = core.split(".");
|
|
453
|
+
const parsePart = (input) => {
|
|
454
|
+
const n = Number.parseInt((input ?? "0").replace(/[^0-9]/g, ""), 10);
|
|
455
|
+
return Number.isFinite(n) ? n : 0;
|
|
456
|
+
};
|
|
457
|
+
return {
|
|
458
|
+
major: parsePart(parts[0]),
|
|
459
|
+
minor: parsePart(parts[1]),
|
|
460
|
+
patch: parsePart(parts[2]),
|
|
461
|
+
};
|
|
462
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@neuralnomads/codenomad",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.1",
|
|
4
4
|
"description": "CodeNomad Server",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "Neural Nomads",
|
|
@@ -32,9 +32,11 @@
|
|
|
32
32
|
"fuzzysort": "^2.0.4",
|
|
33
33
|
"pino": "^9.4.0",
|
|
34
34
|
"undici": "^6.19.8",
|
|
35
|
+
"yauzl": "^2.10.0",
|
|
35
36
|
"zod": "^3.23.8"
|
|
36
37
|
},
|
|
37
38
|
"devDependencies": {
|
|
39
|
+
"@types/yauzl": "^2.10.0",
|
|
38
40
|
"cross-env": "^7.0.3",
|
|
39
41
|
"ts-node": "^10.9.2",
|
|
40
42
|
"tsx": "^4.20.6",
|