@maplewf/dsh-web-update 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.en.md +65 -0
- package/README.md +64 -2
- package/client.js +779 -0
- package/cordis.patch.yml +27 -0
- package/icon.svg +15 -0
- package/index.js +766 -0
- package/lib/core.js +259 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +53 -4
package/lib/core.js
ADDED
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure helpers behind the version-update plugin.
|
|
3
|
+
*
|
|
4
|
+
* Everything here is deterministic and side-effect free apart from the two
|
|
5
|
+
* filesystem readers, so the release logic (semver ordering, which global
|
|
6
|
+
* package to install, how the install is invoked) can be tested without a
|
|
7
|
+
* running dsh host.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { accessSync, constants, readFileSync, realpathSync } from "node:fs";
|
|
11
|
+
import { dirname, join } from "node:path";
|
|
12
|
+
|
|
13
|
+
/** Strict semver: `1.2.3`, `1.2.3-rc.1`, `1.2.3+build`. */
|
|
14
|
+
const SEMVER_PATTERN = /^(\d+)\.(\d+)\.(\d+)(?:-([0-9A-Za-z.-]+))?(?:\+[0-9A-Za-z.-]+)?$/;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Parse one semver string.
|
|
18
|
+
* @param {unknown} value - candidate version.
|
|
19
|
+
* @returns {{ major: number, minor: number, patch: number, pre: string[] } | null} parts or null.
|
|
20
|
+
*/
|
|
21
|
+
export function parseVersion(value) {
|
|
22
|
+
if (typeof value !== "string") return null;
|
|
23
|
+
const match = SEMVER_PATTERN.exec(value.trim());
|
|
24
|
+
if (match === null) return null;
|
|
25
|
+
return {
|
|
26
|
+
major: Number(match[1]),
|
|
27
|
+
minor: Number(match[2]),
|
|
28
|
+
patch: Number(match[3]),
|
|
29
|
+
pre: match[4] === undefined ? [] : match[4].split("."),
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Compare two prerelease identifiers per the semver precedence rules.
|
|
35
|
+
* @param {string} left - identifier from the left version.
|
|
36
|
+
* @param {string} right - identifier from the right version.
|
|
37
|
+
* @returns {number} negative, zero, or positive.
|
|
38
|
+
*/
|
|
39
|
+
function compareIdentifiers(left, right) {
|
|
40
|
+
const leftNumeric = /^\d+$/.test(left);
|
|
41
|
+
const rightNumeric = /^\d+$/.test(right);
|
|
42
|
+
if (leftNumeric && rightNumeric) return Number(left) - Number(right);
|
|
43
|
+
if (leftNumeric) return -1;
|
|
44
|
+
if (rightNumeric) return 1;
|
|
45
|
+
return left < right ? -1 : left > right ? 1 : 0;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Compare two versions the way `semver.compare` does.
|
|
50
|
+
* @param {unknown} left - version to place on the left.
|
|
51
|
+
* @param {unknown} right - version to place on the right.
|
|
52
|
+
* @returns {number | null} -1/0/1, or null when either side is not semver.
|
|
53
|
+
*/
|
|
54
|
+
export function compareVersions(left, right) {
|
|
55
|
+
const a = parseVersion(left);
|
|
56
|
+
const b = parseVersion(right);
|
|
57
|
+
if (a === null || b === null) return null;
|
|
58
|
+
for (const field of ["major", "minor", "patch"]) {
|
|
59
|
+
if (a[field] !== b[field]) return a[field] < b[field] ? -1 : 1;
|
|
60
|
+
}
|
|
61
|
+
if (a.pre.length === 0 && b.pre.length === 0) return 0;
|
|
62
|
+
// A release outranks any prerelease of the same core version.
|
|
63
|
+
if (a.pre.length === 0) return 1;
|
|
64
|
+
if (b.pre.length === 0) return -1;
|
|
65
|
+
const length = Math.max(a.pre.length, b.pre.length);
|
|
66
|
+
for (let index = 0; index < length; index += 1) {
|
|
67
|
+
const leftPart = a.pre[index];
|
|
68
|
+
const rightPart = b.pre[index];
|
|
69
|
+
if (leftPart === undefined) return -1;
|
|
70
|
+
if (rightPart === undefined) return 1;
|
|
71
|
+
const order = compareIdentifiers(leftPart, rightPart);
|
|
72
|
+
if (order !== 0) return order < 0 ? -1 : 1;
|
|
73
|
+
}
|
|
74
|
+
return 0;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Decide whether a fetched version is worth installing.
|
|
79
|
+
* @param {unknown} current - version running in the host process.
|
|
80
|
+
* @param {unknown} latest - version published on the channel.
|
|
81
|
+
* @returns {{ updateAvailable: boolean, relation: 'newer' | 'same' | 'older' | 'unknown' }} decision.
|
|
82
|
+
*/
|
|
83
|
+
export function evaluateUpdate(current, latest) {
|
|
84
|
+
const order = compareVersions(latest, current);
|
|
85
|
+
if (order === null) return { updateAvailable: false, relation: "unknown" };
|
|
86
|
+
if (order > 0) return { updateAvailable: true, relation: "newer" };
|
|
87
|
+
if (order === 0) return { updateAvailable: false, relation: "same" };
|
|
88
|
+
return { updateAvailable: false, relation: "older" };
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Resolve the package directory that contains `package.json` for a running
|
|
93
|
+
* entry point such as `/usr/bin/dsh` -> `/usr/lib/node_modules/@deepseek-ai/dsh`.
|
|
94
|
+
* @param {string} entryPath - argv[1] or any file inside the package.
|
|
95
|
+
* @returns {string} absolute package root.
|
|
96
|
+
*/
|
|
97
|
+
export function packageRootFromEntry(entryPath) {
|
|
98
|
+
return dirname(dirname(realpathSync(entryPath)));
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Read a package version from a package root.
|
|
103
|
+
* @param {string} root - directory that contains `package.json`.
|
|
104
|
+
* @returns {string} the declared version.
|
|
105
|
+
*/
|
|
106
|
+
export function readPackageVersion(root) {
|
|
107
|
+
const manifest = JSON.parse(readFileSync(join(root, "package.json"), "utf8"));
|
|
108
|
+
const version = manifest?.version;
|
|
109
|
+
if (typeof version !== "string" || version === "") {
|
|
110
|
+
throw new Error(`package.json at ${root} has no version`);
|
|
111
|
+
}
|
|
112
|
+
return version;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Read the version of the harness that hosts this process.
|
|
117
|
+
* @param {string} entryPath - path to the running entry script.
|
|
118
|
+
* @returns {{ root: string, version: string }} package root and version.
|
|
119
|
+
*/
|
|
120
|
+
export function readRuntimeVersion(entryPath) {
|
|
121
|
+
const root = packageRootFromEntry(entryPath);
|
|
122
|
+
return { root, version: readPackageVersion(root) };
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Whether the current user may write into a directory.
|
|
127
|
+
* @param {string} path - directory to probe.
|
|
128
|
+
* @returns {boolean} true when the directory is writable by this process.
|
|
129
|
+
*/
|
|
130
|
+
export function isWritable(path) {
|
|
131
|
+
try {
|
|
132
|
+
accessSync(path, constants.W_OK);
|
|
133
|
+
return true;
|
|
134
|
+
} catch {
|
|
135
|
+
return false;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Parse `npm view <pkg> dist-tags --json` output.
|
|
141
|
+
* `npm view <pkg> dist-tags --json` answers with an array holding one object,
|
|
142
|
+
* while the same data from `npm view <pkg> version --json` is a bare string.
|
|
143
|
+
* @param {string} stdout - command output, a JSON array/object or a bare version string.
|
|
144
|
+
* @returns {Record<string, string>} tag -> version map (a bare string maps to `latest`).
|
|
145
|
+
*/
|
|
146
|
+
export function parseDistTags(stdout) {
|
|
147
|
+
const text = typeof stdout === "string" ? stdout.trim() : "";
|
|
148
|
+
if (text === "") return {};
|
|
149
|
+
const parsed = JSON.parse(text);
|
|
150
|
+
if (typeof parsed === "string") return { latest: parsed.trim() };
|
|
151
|
+
const value = Array.isArray(parsed) ? parsed.find((item) => item !== null && typeof item === "object") : parsed;
|
|
152
|
+
if (value === null || typeof value !== "object") return {};
|
|
153
|
+
const tags = {};
|
|
154
|
+
for (const [tag, version] of Object.entries(value)) {
|
|
155
|
+
if (typeof version === "string" && version !== "") tags[tag] = version;
|
|
156
|
+
}
|
|
157
|
+
return tags;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Pick the version a channel points at.
|
|
162
|
+
* @param {Record<string, string>} tags - dist-tag map.
|
|
163
|
+
* @param {string} channel - preferred tag, normally `latest`.
|
|
164
|
+
* @returns {string | null} version, or null when the tag is absent.
|
|
165
|
+
*/
|
|
166
|
+
export function pickChannelVersion(tags, channel) {
|
|
167
|
+
const preferred = typeof channel === "string" && channel !== "" ? tags[channel] : undefined;
|
|
168
|
+
if (typeof preferred === "string") return preferred;
|
|
169
|
+
return typeof tags.latest === "string" ? tags.latest : null;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Build the argv used to install one global package version.
|
|
174
|
+
* @param {string} packageName - published package name.
|
|
175
|
+
* @param {string} version - exact version to install.
|
|
176
|
+
* @returns {string[]} npm argv.
|
|
177
|
+
*/
|
|
178
|
+
export function installArgs(packageName, version) {
|
|
179
|
+
return ["install", "-g", `${packageName}@${version}`, "--no-audit", "--no-fund"];
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Wrap an executable call in `sudo -n` when the install prefix is not writable.
|
|
184
|
+
* @param {{ npmPath: string, needsSudo: boolean }} options - invocation options.
|
|
185
|
+
* @param {string[]} args - npm argv.
|
|
186
|
+
* @returns {{ command: string, args: string[], sudo: boolean }} process invocation.
|
|
187
|
+
*/
|
|
188
|
+
export function installInvocation(options, args) {
|
|
189
|
+
const npmPath = typeof options.npmPath === "string" && options.npmPath !== ""
|
|
190
|
+
? options.npmPath
|
|
191
|
+
: "npm";
|
|
192
|
+
if (options.needsSudo !== true) return { command: npmPath, args, sudo: false };
|
|
193
|
+
return { command: "sudo", args: ["-n", npmPath, ...args], sudo: true };
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Render one installation as a single shell-like line for logs and the UI.
|
|
198
|
+
* @param {{ command: string, args: string[] }} invocation - process invocation.
|
|
199
|
+
* @returns {string} printable command.
|
|
200
|
+
*/
|
|
201
|
+
export function formatInvocation(invocation) {
|
|
202
|
+
return [invocation.command, ...invocation.args].join(" ");
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Substitute the `{unit}` placeholder in a user-supplied restart command.
|
|
207
|
+
* @param {string} command - command template.
|
|
208
|
+
* @param {string} unit - configured systemd unit name.
|
|
209
|
+
* @returns {string} expanded command.
|
|
210
|
+
*/
|
|
211
|
+
export function expandRestartCommand(command, unit) {
|
|
212
|
+
return String(command).replaceAll("{unit}", unit);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Build the argv the transient timer executes at restart time: the built-in
|
|
217
|
+
* systemctl invocation, or a user-supplied shell command when configured.
|
|
218
|
+
* An empty command keeps the built-in default.
|
|
219
|
+
* @param {{ command: string, systemctlPath: string, unit: string }} options - restart options.
|
|
220
|
+
* @returns {string[]} inner command argv.
|
|
221
|
+
*/
|
|
222
|
+
export function restartInnerCommand(options) {
|
|
223
|
+
const custom = typeof options.command === "string" && options.command.trim() !== "";
|
|
224
|
+
if (custom) {
|
|
225
|
+
return ["/bin/sh", "-c", expandRestartCommand(options.command.trim(), options.unit)];
|
|
226
|
+
}
|
|
227
|
+
return [options.systemctlPath, "--user", "restart", options.unit];
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Build the full systemd-run argv: a transient --user timer fires after
|
|
232
|
+
* `delaySeconds` and runs `innerCommand`. The timer lives in its own cgroup,
|
|
233
|
+
* outside the unit being restarted, so `KillMode=control-group` cannot kill it
|
|
234
|
+
* while the service stops.
|
|
235
|
+
* @param {{ timerUnit: string, delaySeconds: number, innerCommand: string[] }} options - timer options.
|
|
236
|
+
* @returns {string[]} systemd-run argv.
|
|
237
|
+
*/
|
|
238
|
+
export function systemdRunTimerArgs(options) {
|
|
239
|
+
return [
|
|
240
|
+
"--user",
|
|
241
|
+
"--collect",
|
|
242
|
+
"--quiet",
|
|
243
|
+
`--unit=${options.timerUnit}`,
|
|
244
|
+
`--on-active=${options.delaySeconds}`,
|
|
245
|
+
"--timer-property=AccuracySec=100ms",
|
|
246
|
+
...options.innerCommand,
|
|
247
|
+
];
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Trim captured output to a bounded tail so status responses stay small.
|
|
252
|
+
* @param {string} text - accumulated output.
|
|
253
|
+
* @param {number} [limit] - maximum characters to keep.
|
|
254
|
+
* @returns {string} tail of the text.
|
|
255
|
+
*/
|
|
256
|
+
export function tail(text, limit = 4000) {
|
|
257
|
+
const value = typeof text === "string" ? text : "";
|
|
258
|
+
return value.length <= limit ? value : value.slice(value.length - limit);
|
|
259
|
+
}
|
package/locale/en.json
ADDED
package/locale/zh.json
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,55 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@maplewf/dsh-web-update",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"description": "Check and install dsh web runtime updates from a Settings page",
|
|
7
|
+
"keywords": [
|
|
8
|
+
"deepseek-harness",
|
|
9
|
+
"dsh",
|
|
10
|
+
"update",
|
|
11
|
+
"version",
|
|
12
|
+
"npm"
|
|
13
|
+
],
|
|
14
|
+
"license": "Apache-2.0",
|
|
15
|
+
"author": "maplewf",
|
|
16
|
+
"homepage": "https://github.com/maplewf/dsh-web-update#readme",
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "https://github.com/maplewf/dsh-web-update.git"
|
|
20
|
+
},
|
|
21
|
+
"engines": {
|
|
22
|
+
"node": ">=22",
|
|
23
|
+
"dsh": "0.2.0-rc.2 || >=0.2.1-rc.1 <0.3.0-0"
|
|
24
|
+
},
|
|
25
|
+
"exports": {
|
|
26
|
+
".": "./index.js",
|
|
27
|
+
"./client": "./client.js",
|
|
28
|
+
"./locale/*.json": "./locale/*.json",
|
|
29
|
+
"./icon": "./icon.svg"
|
|
30
|
+
},
|
|
31
|
+
"files": [
|
|
32
|
+
"index.js",
|
|
33
|
+
"client.js",
|
|
34
|
+
"lib/core.js",
|
|
35
|
+
"cordis.patch.yml",
|
|
36
|
+
"locale/*.json",
|
|
37
|
+
"icon.svg",
|
|
38
|
+
"README.md",
|
|
39
|
+
"README.en.md"
|
|
40
|
+
],
|
|
41
|
+
"dsh": {
|
|
42
|
+
"bundle": {
|
|
43
|
+
"patch": "./cordis.patch.yml"
|
|
44
|
+
},
|
|
45
|
+
"client": {
|
|
46
|
+
"platform": "web",
|
|
47
|
+
"immediately": true,
|
|
48
|
+
"inject": [
|
|
49
|
+
"@deepseek-ai/dsh-client-connection",
|
|
50
|
+
"@deepseek-ai/dsh-client-locale",
|
|
51
|
+
"@deepseek-ai/dsh-client-ui-settings"
|
|
52
|
+
]
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|