@michengai/dsh-archive-manager 1.3.1 → 1.3.2
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/CHANGELOG.md +8 -0
- package/CHANGELOG.zh-CN.md +8 -0
- package/README.de.md +4 -10
- package/README.en.md +3 -9
- package/README.es.md +4 -10
- package/README.fr.md +4 -10
- package/README.ja.md +3 -9
- package/README.md +3 -9
- package/README.pt.md +4 -10
- package/README.ru.md +3 -9
- package/README.zh-CN.md +3 -9
- package/lib/client.js +1 -1
- package/lib/index.js +65 -63
- package/lib/plugin-settings.js +2 -22
- package/package.json +27 -26
- package/lib/plugin-updater.js +0 -446
- package/lib/release-age.js +0 -164
package/lib/plugin-updater.js
DELETED
|
@@ -1,446 +0,0 @@
|
|
|
1
|
-
import { symbols } from "@deepseek-ai/cordis";
|
|
2
|
-
import { record } from "./contracts.js";
|
|
3
|
-
import { guardReleaseAgeExclude, mergeMinimumReleaseAgeExclude, recordReleaseAgeExclude } from "./release-age.js";
|
|
4
|
-
import { spawn } from "node:child_process";
|
|
5
|
-
import { existsSync, readFileSync } from "node:fs";
|
|
6
|
-
import { readFile } from "node:fs/promises";
|
|
7
|
-
import { homedir } from "node:os";
|
|
8
|
-
import { basename, delimiter, dirname, isAbsolute, resolve } from "node:path";
|
|
9
|
-
import { fileURLToPath } from "node:url";
|
|
10
|
-
const PLUGIN_UPDATE_HEADER = "x-michengai-plugin-update";
|
|
11
|
-
const PLUGIN_UPDATE_IPC = "apply-plugin-updates";
|
|
12
|
-
function header(request, name) {
|
|
13
|
-
const value = request.headers?.[name];
|
|
14
|
-
return Array.isArray(value) ? value[0] : value;
|
|
15
|
-
}
|
|
16
|
-
function isLoopbackAddress(value) {
|
|
17
|
-
const address = value?.toLowerCase().replace(/^\[|\]$/g, "");
|
|
18
|
-
return address === "localhost" || address === "localhost." || address === "::1" || address?.startsWith("127.") === true || address?.startsWith("::ffff:127.") === true;
|
|
19
|
-
}
|
|
20
|
-
function isTrustedUpdateRequest(request) {
|
|
21
|
-
if (header(request, PLUGIN_UPDATE_HEADER) !== "1") return false;
|
|
22
|
-
if (!isLoopbackAddress(request.socket?.remoteAddress)) return false;
|
|
23
|
-
const site = header(request, "sec-fetch-site");
|
|
24
|
-
if (site !== void 0 && site !== "same-origin") return false;
|
|
25
|
-
const host = header(request, "host");
|
|
26
|
-
if (host === void 0 || host === "") return false;
|
|
27
|
-
const origin = header(request, "origin");
|
|
28
|
-
if (origin === void 0 || origin === "") {
|
|
29
|
-
try {
|
|
30
|
-
return isLoopbackAddress(new URL(`http://${host}`).hostname);
|
|
31
|
-
} catch {
|
|
32
|
-
return false;
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
try {
|
|
36
|
-
const url = new URL(origin);
|
|
37
|
-
return (url.protocol === "http:" || url.protocol === "https:") && isLoopbackAddress(url.hostname) && url.host === host;
|
|
38
|
-
} catch {
|
|
39
|
-
return false;
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
function validProfileName(value) {
|
|
43
|
-
return typeof value === "string" && value !== "" && value !== "." && value !== ".." && !value.includes("/") && !value.includes("\\") && !/[\0-\x1f\x7f]/.test(value);
|
|
44
|
-
}
|
|
45
|
-
function profileNameFromArgv(argv) {
|
|
46
|
-
for (let index = 2; index < argv.length; index += 1) {
|
|
47
|
-
if (argv[index] === "--profile") return argv[index + 1];
|
|
48
|
-
if (argv[index]?.startsWith("--profile=")) return argv[index].slice("--profile=".length);
|
|
49
|
-
}
|
|
50
|
-
return argv[2] === "web" ? "web" : void 0;
|
|
51
|
-
}
|
|
52
|
-
function isDshCliEntry(entry, manifest, packageRoot) {
|
|
53
|
-
if (typeof manifest !== "object" || manifest === null) return false;
|
|
54
|
-
if (("name" in manifest ? manifest.name : void 0) !== "@deepseek-ai/dsh") return false;
|
|
55
|
-
const item = manifest;
|
|
56
|
-
const bin = typeof item.bin === "string" ? item.bin : typeof item.bin === "object" && item.bin !== null ? "dsh" in item.bin ? item.bin.dsh : void 0 : void 0;
|
|
57
|
-
return typeof bin === "string" && bin !== "" && !isAbsolute(bin) && resolve(packageRoot, bin) === resolve(entry);
|
|
58
|
-
}
|
|
59
|
-
function cliEntry(argv = process.argv, cwd = process.cwd(), exists = existsSync) {
|
|
60
|
-
const value = argv[1];
|
|
61
|
-
if (value === void 0 || value === "") return void 0;
|
|
62
|
-
const entry = value.startsWith("file:") ? fileURLToPath(value) : resolve(cwd, value);
|
|
63
|
-
if (!exists(entry)) return void 0;
|
|
64
|
-
for (let directory = dirname(entry); ; ) {
|
|
65
|
-
const manifestPath = resolve(directory, "package.json");
|
|
66
|
-
if (exists(manifestPath)) {
|
|
67
|
-
try {
|
|
68
|
-
if (isDshCliEntry(entry, JSON.parse(readFileSync(manifestPath, "utf8")), directory)) return entry;
|
|
69
|
-
} catch {
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
const parent = dirname(directory);
|
|
73
|
-
if (parent === directory) return void 0;
|
|
74
|
-
directory = parent;
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
function optionalService(ctx, name) {
|
|
78
|
-
try {
|
|
79
|
-
return typeof ctx.get === "function" ? ctx.get(name) : void 0;
|
|
80
|
-
} catch {
|
|
81
|
-
return void 0;
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
function isOfficialDesktopHostEntry(entry) {
|
|
85
|
-
if (entry === void 0 || entry === "") return false;
|
|
86
|
-
return entry.replaceAll("\\", "/").includes("/dsh-desktop-host/");
|
|
87
|
-
}
|
|
88
|
-
function stringEnvironment(value) {
|
|
89
|
-
if (value === null || typeof value !== "object") return void 0;
|
|
90
|
-
const env = {};
|
|
91
|
-
for (const [key, item] of Object.entries(value)) {
|
|
92
|
-
if (typeof item === "string") env[key] = item;
|
|
93
|
-
}
|
|
94
|
-
return Object.keys(env).length === 0 ? void 0 : env;
|
|
95
|
-
}
|
|
96
|
-
function packageManagerFrom(value) {
|
|
97
|
-
if (value === null || typeof value !== "object") return void 0;
|
|
98
|
-
const item = value;
|
|
99
|
-
if (typeof item.command !== "string" || item.command === "") return void 0;
|
|
100
|
-
if (!Array.isArray(item.args) || !item.args.every((arg) => typeof arg === "string")) return void 0;
|
|
101
|
-
const env = stringEnvironment(item.env);
|
|
102
|
-
return { command: item.command, args: item.args, ...env === void 0 ? {} : { env } };
|
|
103
|
-
}
|
|
104
|
-
function packageManagerFromArgv(argv, exists, execPath, pathEnv) {
|
|
105
|
-
if (!isOfficialDesktopHostEntry(argv[1])) return void 0;
|
|
106
|
-
const pnpm = argv[5];
|
|
107
|
-
if (typeof pnpm !== "string" || !isAbsolute(pnpm) || !exists(pnpm)) return void 0;
|
|
108
|
-
const bin = argv[6];
|
|
109
|
-
const path = typeof bin === "string" && bin !== "" ? `${bin}${delimiter}${pathEnv ?? ""}` : pathEnv;
|
|
110
|
-
return {
|
|
111
|
-
command: execPath,
|
|
112
|
-
args: ["--expose-internals", pnpm],
|
|
113
|
-
env: { ELECTRON_RUN_AS_NODE: "1", ...typeof path === "string" && path !== "" ? { PATH: path } : {} }
|
|
114
|
-
};
|
|
115
|
-
}
|
|
116
|
-
function pluginManagerFrom(value) {
|
|
117
|
-
if (value === null || typeof value !== "object" || typeof value.installBundle !== "function") return void 0;
|
|
118
|
-
return value;
|
|
119
|
-
}
|
|
120
|
-
function resolveUpdateRuntime(ctx, options = {}) {
|
|
121
|
-
const argv = options.argv ?? process.argv;
|
|
122
|
-
const env = options.env ?? process.env;
|
|
123
|
-
const cwd = options.cwd ?? process.cwd();
|
|
124
|
-
const home = options.homeDir ?? homedir();
|
|
125
|
-
const exists = options.exists ?? existsSync;
|
|
126
|
-
const execPath = options.execPath ?? process.execPath;
|
|
127
|
-
const profiles = optionalService(ctx, "desktopProfiles");
|
|
128
|
-
const desktopPnpm = optionalService(ctx, "desktopPnpm");
|
|
129
|
-
if (profiles?.current !== void 0) {
|
|
130
|
-
const current = profiles.current;
|
|
131
|
-
if (!validProfileName(current?.name) || typeof current.dir !== "string" || !isAbsolute(current.dir)) {
|
|
132
|
-
throw new Error("\u5F53\u524D Desktop Profile \u4FE1\u606F\u65E0\u6548\uFF0C\u8BF7\u91CD\u542F\u540E\u91CD\u8BD5\u3002");
|
|
133
|
-
}
|
|
134
|
-
const runnable = typeof desktopPnpm?.runPlugin === "function";
|
|
135
|
-
return {
|
|
136
|
-
profileName: current.name,
|
|
137
|
-
profileDir: resolve(current.dir),
|
|
138
|
-
officialDesktop: false,
|
|
139
|
-
canAutoUpdate: runnable,
|
|
140
|
-
...runnable ? { desktopPnpm } : {}
|
|
141
|
-
};
|
|
142
|
-
}
|
|
143
|
-
const launched = optionalService(ctx, "profileContext");
|
|
144
|
-
const official = isOfficialDesktopHostEntry(argv[1]) || launched?.name === "desktop";
|
|
145
|
-
const launchedDir = typeof launched?.dir === "string" && isAbsolute(launched.dir) ? resolve(launched.dir) : void 0;
|
|
146
|
-
const projectDir = official && typeof argv[3] === "string" && isAbsolute(argv[3]) ? resolve(argv[3]) : void 0;
|
|
147
|
-
const cwdProfile = official && exists(resolve(cwd, "package.json")) ? resolve(cwd) : void 0;
|
|
148
|
-
const profileDir = launchedDir ?? projectDir ?? cwdProfile ?? resolve(env.DSH_PROFILE_DIR ?? resolve(home, ".dsh", "profiles", "web"));
|
|
149
|
-
const selected = profileNameFromArgv(argv);
|
|
150
|
-
const profileName = validProfileName(launched?.name) ? launched.name : official ? "desktop" : validProfileName(selected) ? selected : validProfileName(basename(profileDir)) ? basename(profileDir) : "web";
|
|
151
|
-
const packageManager = official ? packageManagerFrom(launched?.packageManager) ?? packageManagerFromArgv(argv, exists, execPath, env.PATH) : void 0;
|
|
152
|
-
const pluginManager = official && packageManager === void 0 ? pluginManagerFrom(optionalService(ctx, "pluginManager")) : void 0;
|
|
153
|
-
const entry = official ? void 0 : cliEntry(argv, cwd, exists);
|
|
154
|
-
return {
|
|
155
|
-
profileName,
|
|
156
|
-
profileDir,
|
|
157
|
-
officialDesktop: official,
|
|
158
|
-
canAutoUpdate: packageManager !== void 0 || pluginManager !== void 0 || entry !== void 0,
|
|
159
|
-
...packageManager === void 0 ? {} : { packageManager },
|
|
160
|
-
...pluginManager === void 0 ? {} : { pluginManager },
|
|
161
|
-
...entry === void 0 ? {} : { cliEntry: entry }
|
|
162
|
-
};
|
|
163
|
-
}
|
|
164
|
-
function shouldNotifyParent(target, send = process.send) {
|
|
165
|
-
return target.officialDesktop !== true && target.desktopPnpm === void 0 && target.packageManager === void 0 && typeof send === "function";
|
|
166
|
-
}
|
|
167
|
-
function runtime(ctx) {
|
|
168
|
-
return resolveUpdateRuntime(ctx);
|
|
169
|
-
}
|
|
170
|
-
function parseSemver(value) {
|
|
171
|
-
const match = /^v?(\d+)\.(\d+)\.(\d+)(?:-([0-9A-Za-z.-]+))?(?:\+[0-9A-Za-z.-]+)?$/.exec(value);
|
|
172
|
-
if (match === null) return void 0;
|
|
173
|
-
return { core: [Number(match[1]), Number(match[2]), Number(match[3])], prerelease: match[4]?.split(".") ?? [] };
|
|
174
|
-
}
|
|
175
|
-
function isNewerVersion(currentValue, candidateValue) {
|
|
176
|
-
const current = parseSemver(currentValue);
|
|
177
|
-
const candidate = parseSemver(candidateValue);
|
|
178
|
-
if (current === void 0 || candidate === void 0) return false;
|
|
179
|
-
for (let index = 0; index < 3; index += 1) {
|
|
180
|
-
if (candidate.core[index] !== current.core[index]) return candidate.core[index] > current.core[index];
|
|
181
|
-
}
|
|
182
|
-
return comparePrerelease(candidate.prerelease, current.prerelease) > 0;
|
|
183
|
-
}
|
|
184
|
-
function comparePrerelease(left, right) {
|
|
185
|
-
if (left.length === 0 || right.length === 0) return left.length === right.length ? 0 : left.length === 0 ? 1 : -1;
|
|
186
|
-
const length = Math.max(left.length, right.length);
|
|
187
|
-
for (let index = 0; index < length; index += 1) {
|
|
188
|
-
const a = left[index];
|
|
189
|
-
const b = right[index];
|
|
190
|
-
if (a === void 0 || b === void 0) return a === b ? 0 : a === void 0 ? -1 : 1;
|
|
191
|
-
if (a === b) continue;
|
|
192
|
-
const aNumeric = /^\d+$/.test(a);
|
|
193
|
-
const bNumeric = /^\d+$/.test(b);
|
|
194
|
-
if (aNumeric && bNumeric) {
|
|
195
|
-
const aNumber = BigInt(a);
|
|
196
|
-
const bNumber = BigInt(b);
|
|
197
|
-
if (aNumber !== bNumber) return aNumber > bNumber ? 1 : -1;
|
|
198
|
-
continue;
|
|
199
|
-
}
|
|
200
|
-
if (aNumeric !== bNumeric) return aNumeric ? -1 : 1;
|
|
201
|
-
return a > b ? 1 : -1;
|
|
202
|
-
}
|
|
203
|
-
return 0;
|
|
204
|
-
}
|
|
205
|
-
let latestCache;
|
|
206
|
-
async function latestVersion(packageName) {
|
|
207
|
-
if (latestCache?.packageName === packageName && Date.now() < latestCache.expiresAt) return latestCache.version;
|
|
208
|
-
try {
|
|
209
|
-
const response = await fetch(`https://registry.npmjs.org/${encodeURIComponent(packageName)}/latest`, { signal: AbortSignal.timeout(8e3) });
|
|
210
|
-
if (!response.ok) return void 0;
|
|
211
|
-
const value = record(await response.json());
|
|
212
|
-
if (typeof value.version !== "string" || value.version === "") return void 0;
|
|
213
|
-
latestCache = { packageName, version: value.version, expiresAt: Date.now() + 5 * 6e4 };
|
|
214
|
-
return value.version;
|
|
215
|
-
} catch {
|
|
216
|
-
return void 0;
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
async function currentVersion(manifestUrl) {
|
|
220
|
-
const value = record(JSON.parse(await readFile(manifestUrl, "utf8")));
|
|
221
|
-
if (typeof value.version !== "string" || value.version === "") throw new Error("\u65E0\u6CD5\u8BFB\u53D6\u5F53\u524D\u63D2\u4EF6\u7248\u672C\u3002");
|
|
222
|
-
return value.version;
|
|
223
|
-
}
|
|
224
|
-
async function status(options, target) {
|
|
225
|
-
const current = await currentVersion(options.manifestUrl);
|
|
226
|
-
const latest = await latestVersion(options.packageName);
|
|
227
|
-
return {
|
|
228
|
-
packageName: options.packageName,
|
|
229
|
-
currentVersion: current,
|
|
230
|
-
...latest === void 0 ? {} : { latestVersion: latest },
|
|
231
|
-
latestCheckFailed: latest === void 0,
|
|
232
|
-
updateAvailable: latest !== void 0 && isNewerVersion(current, latest),
|
|
233
|
-
profileName: target.profileName,
|
|
234
|
-
canAutoUpdate: target.canAutoUpdate
|
|
235
|
-
};
|
|
236
|
-
}
|
|
237
|
-
async function runCliInstall(target, packageSpec) {
|
|
238
|
-
if (target.cliEntry === void 0) throw new Error("\u5F53\u524D\u73AF\u5883\u4E0D\u652F\u6301\u81EA\u52A8\u66F4\u65B0\uFF0C\u8BF7\u4F7F\u7528\u624B\u5DE5\u66F4\u65B0\u547D\u4EE4\u3002");
|
|
239
|
-
const entry = target.cliEntry;
|
|
240
|
-
await new Promise((resolvePromise, reject) => {
|
|
241
|
-
const child = spawn(process.execPath, [entry, "plugin", "--profile", target.profileName, "add", "--config.minimumReleaseAge=0", packageSpec, "--registry=https://registry.npmjs.org/"], {
|
|
242
|
-
cwd: target.profileDir,
|
|
243
|
-
windowsHide: true,
|
|
244
|
-
stdio: ["ignore", "pipe", "pipe"],
|
|
245
|
-
env: { ...process.env, NO_COLOR: "1" }
|
|
246
|
-
});
|
|
247
|
-
let detail = "";
|
|
248
|
-
child.stdout?.on("data", (chunk) => {
|
|
249
|
-
detail = (detail + String(chunk)).slice(-4e3);
|
|
250
|
-
});
|
|
251
|
-
child.stderr?.on("data", (chunk) => {
|
|
252
|
-
detail = (detail + String(chunk)).slice(-4e3);
|
|
253
|
-
});
|
|
254
|
-
const timer = setTimeout(() => {
|
|
255
|
-
child.kill();
|
|
256
|
-
reject(new Error("\u66F4\u65B0\u8D85\u65F6\uFF0C\u8BF7\u6539\u7528\u624B\u5DE5\u66F4\u65B0\u3002"));
|
|
257
|
-
}, 10 * 6e4);
|
|
258
|
-
child.once("error", (error) => {
|
|
259
|
-
clearTimeout(timer);
|
|
260
|
-
reject(error);
|
|
261
|
-
});
|
|
262
|
-
child.once("exit", (code) => {
|
|
263
|
-
clearTimeout(timer);
|
|
264
|
-
if (code === 0) resolvePromise();
|
|
265
|
-
else reject(new Error(detail.trim() || `\u66F4\u65B0\u8FDB\u7A0B\u9000\u51FA\u7801 ${String(code)}`));
|
|
266
|
-
});
|
|
267
|
-
});
|
|
268
|
-
}
|
|
269
|
-
async function runPackageManagerInstall(target, packageSpec) {
|
|
270
|
-
const manager = target.packageManager;
|
|
271
|
-
if (manager === void 0) throw new Error("\u5F53\u524D\u73AF\u5883\u4E0D\u652F\u6301\u81EA\u52A8\u66F4\u65B0\uFF0C\u8BF7\u4F7F\u7528\u624B\u5DE5\u66F4\u65B0\u547D\u4EE4\u3002");
|
|
272
|
-
await new Promise((resolvePromise, reject) => {
|
|
273
|
-
const child = spawn(manager.command, [...manager.args, "add", "--config.minimumReleaseAge=0", packageSpec, "--registry=https://registry.npmjs.org/"], {
|
|
274
|
-
cwd: target.profileDir,
|
|
275
|
-
windowsHide: true,
|
|
276
|
-
stdio: ["ignore", "pipe", "pipe"],
|
|
277
|
-
env: { ...process.env, ...manager.env, NO_COLOR: "1" }
|
|
278
|
-
});
|
|
279
|
-
let detail = "";
|
|
280
|
-
child.stdout?.on("data", (chunk) => {
|
|
281
|
-
detail = (detail + String(chunk)).slice(-4e3);
|
|
282
|
-
});
|
|
283
|
-
child.stderr?.on("data", (chunk) => {
|
|
284
|
-
detail = (detail + String(chunk)).slice(-4e3);
|
|
285
|
-
});
|
|
286
|
-
const timer = setTimeout(() => {
|
|
287
|
-
child.kill();
|
|
288
|
-
reject(new Error("\u66F4\u65B0\u8D85\u65F6\uFF0C\u8BF7\u6539\u7528\u624B\u5DE5\u66F4\u65B0\u3002"));
|
|
289
|
-
}, 10 * 6e4);
|
|
290
|
-
child.once("error", (error) => {
|
|
291
|
-
clearTimeout(timer);
|
|
292
|
-
reject(error);
|
|
293
|
-
});
|
|
294
|
-
child.once("exit", (code) => {
|
|
295
|
-
clearTimeout(timer);
|
|
296
|
-
if (code === 0) resolvePromise();
|
|
297
|
-
else reject(new Error(detail.trim() || `\u66F4\u65B0\u8FDB\u7A0B\u9000\u51FA\u7801 ${String(code)}`));
|
|
298
|
-
});
|
|
299
|
-
});
|
|
300
|
-
}
|
|
301
|
-
async function runPluginManagerInstall(target, packageSpec) {
|
|
302
|
-
const manager = target.pluginManager;
|
|
303
|
-
if (manager === void 0) throw new Error("\u5F53\u524D\u73AF\u5883\u4E0D\u652F\u6301\u81EA\u52A8\u66F4\u65B0\uFF0C\u8BF7\u4F7F\u7528\u624B\u5DE5\u66F4\u65B0\u547D\u4EE4\u3002");
|
|
304
|
-
const result = await manager.installBundle(packageSpec);
|
|
305
|
-
if (result.application === "applied" || result.application === "restart-required" || result.application === "overridden") return;
|
|
306
|
-
throw new Error(result.error?.message || result.packageResult?.output || "\u66F4\u65B0\u5931\u8D25\uFF0C\u8BF7\u67E5\u770B\u670D\u52A1\u7AEF\u65E5\u5FD7\u3002");
|
|
307
|
-
}
|
|
308
|
-
async function install(target, packageSpec) {
|
|
309
|
-
await guardReleaseAgeExclude(target.profileDir, packageSpec);
|
|
310
|
-
if (target.desktopPnpm !== void 0) {
|
|
311
|
-
const handle = target.desktopPnpm.runPlugin(["add", "--config.minimumReleaseAge=0", packageSpec, "--registry=https://registry.npmjs.org/"], target.profileDir);
|
|
312
|
-
const result = await handle.done;
|
|
313
|
-
if (result.exitCode !== 0) throw new Error(`\u66F4\u65B0\u8FDB\u7A0B\u9000\u51FA\u7801 ${String(result.exitCode)}\u3002`);
|
|
314
|
-
await recordReleaseAgeExclude(target.profileDir, packageSpec);
|
|
315
|
-
return;
|
|
316
|
-
}
|
|
317
|
-
if (target.packageManager !== void 0) {
|
|
318
|
-
await runPackageManagerInstall(target, packageSpec);
|
|
319
|
-
await recordReleaseAgeExclude(target.profileDir, packageSpec);
|
|
320
|
-
return;
|
|
321
|
-
}
|
|
322
|
-
if (target.pluginManager !== void 0) {
|
|
323
|
-
await runPluginManagerInstall(target, packageSpec);
|
|
324
|
-
await recordReleaseAgeExclude(target.profileDir, packageSpec);
|
|
325
|
-
return;
|
|
326
|
-
}
|
|
327
|
-
await runCliInstall(target, packageSpec);
|
|
328
|
-
await recordReleaseAgeExclude(target.profileDir, packageSpec);
|
|
329
|
-
}
|
|
330
|
-
function json(response, statusCode, value) {
|
|
331
|
-
response.writeHead(statusCode, { "content-type": "application/json; charset=utf-8", "cache-control": "no-store" });
|
|
332
|
-
response.end(JSON.stringify(value));
|
|
333
|
-
}
|
|
334
|
-
function publicError(error) {
|
|
335
|
-
const message = error instanceof Error ? error.message : "\u66F4\u65B0\u6682\u4E0D\u53EF\u7528\u3002";
|
|
336
|
-
return /[A-Za-z]:[\\/]|\/(?:home|root|Users|var|tmp)\//.test(message) ? "\u66F4\u65B0\u5931\u8D25\uFF0C\u8BF7\u67E5\u770B\u670D\u52A1\u7AEF\u65E5\u5FD7\u3002" : message;
|
|
337
|
-
}
|
|
338
|
-
const updateRoutes = Symbol.for("dsh-archive-manager.update-routes");
|
|
339
|
-
function updateRouteBag(server) {
|
|
340
|
-
const owner = server;
|
|
341
|
-
return owner[updateRoutes] ?? (owner[updateRoutes] = /* @__PURE__ */ new Map());
|
|
342
|
-
}
|
|
343
|
-
function rawService(service) {
|
|
344
|
-
const original = Reflect.get(service, symbols.original);
|
|
345
|
-
return original !== null && typeof original === "object" ? original : service;
|
|
346
|
-
}
|
|
347
|
-
function releaseUpdateRoute(server, path) {
|
|
348
|
-
const raw = rawService(server);
|
|
349
|
-
const bag = updateRouteBag(raw);
|
|
350
|
-
const owned = bag.get(path);
|
|
351
|
-
if (owned !== void 0) {
|
|
352
|
-
bag.delete(path);
|
|
353
|
-
owned();
|
|
354
|
-
}
|
|
355
|
-
for (const key of ["exact", "prefixes"]) {
|
|
356
|
-
const table = Reflect.get(raw, key);
|
|
357
|
-
if (table instanceof Map && table.has(path)) table.delete(path);
|
|
358
|
-
}
|
|
359
|
-
}
|
|
360
|
-
function registerPluginUpdater(ctx, options) {
|
|
361
|
-
const host = ctx;
|
|
362
|
-
let installing = false;
|
|
363
|
-
const server = rawService(host.webServer);
|
|
364
|
-
releaseUpdateRoute(server, options.endpoint);
|
|
365
|
-
const dispose = host.webServer.register({
|
|
366
|
-
kind: "exact",
|
|
367
|
-
path: options.endpoint,
|
|
368
|
-
handler: async (request, response) => {
|
|
369
|
-
try {
|
|
370
|
-
if (options.isEnabled) {
|
|
371
|
-
const allowed = await options.isEnabled();
|
|
372
|
-
if (!allowed) {
|
|
373
|
-
json(response, 403, { ok: false, error: "\u5728\u7EBF\u66F4\u65B0\u5DF2\u88AB\u7981\u7528\u3002" });
|
|
374
|
-
return;
|
|
375
|
-
}
|
|
376
|
-
}
|
|
377
|
-
const target = runtime(host);
|
|
378
|
-
if (request.method === "GET" || request.method === "HEAD") {
|
|
379
|
-
const payload = await status(options, target);
|
|
380
|
-
response.writeHead(200, { "content-type": "application/json; charset=utf-8", "cache-control": "no-store" });
|
|
381
|
-
response.end(request.method === "HEAD" ? void 0 : JSON.stringify(payload));
|
|
382
|
-
return;
|
|
383
|
-
}
|
|
384
|
-
if (request.method !== "POST") {
|
|
385
|
-
response.writeHead(405, { allow: "GET, HEAD, POST" });
|
|
386
|
-
response.end();
|
|
387
|
-
return;
|
|
388
|
-
}
|
|
389
|
-
if (!isTrustedUpdateRequest(request)) {
|
|
390
|
-
json(response, 403, { error: "\u5DF2\u62D2\u7EDD\u975E\u672C\u673A\u540C\u6E90\u66F4\u65B0\u8BF7\u6C42\u3002" });
|
|
391
|
-
return;
|
|
392
|
-
}
|
|
393
|
-
if (installing) {
|
|
394
|
-
json(response, 409, { error: "\u5F53\u524D\u63D2\u4EF6\u6B63\u5728\u66F4\u65B0\uFF0C\u8BF7\u7A0D\u5019\u3002" });
|
|
395
|
-
return;
|
|
396
|
-
}
|
|
397
|
-
const before = await status(options, target);
|
|
398
|
-
if (before.latestVersion === void 0) {
|
|
399
|
-
json(response, 503, { error: "\u6682\u65F6\u65E0\u6CD5\u83B7\u53D6\u6700\u65B0\u7248\u672C\u3002" });
|
|
400
|
-
return;
|
|
401
|
-
}
|
|
402
|
-
if (!before.updateAvailable) {
|
|
403
|
-
json(response, 200, before);
|
|
404
|
-
return;
|
|
405
|
-
}
|
|
406
|
-
installing = true;
|
|
407
|
-
try {
|
|
408
|
-
await install(target, `${options.packageName}@${before.latestVersion}`);
|
|
409
|
-
} finally {
|
|
410
|
-
installing = false;
|
|
411
|
-
}
|
|
412
|
-
const notifyParent = shouldNotifyParent(target);
|
|
413
|
-
const autoReload = target.desktopPnpm !== void 0 || notifyParent;
|
|
414
|
-
json(response, 200, { ...before, updatedVersion: before.latestVersion, restartRequired: true, autoReload, ...target.officialDesktop ? { restartDesktop: true } : {} });
|
|
415
|
-
if (notifyParent) setTimeout(() => {
|
|
416
|
-
process.send?.(PLUGIN_UPDATE_IPC);
|
|
417
|
-
}, 150).unref?.();
|
|
418
|
-
} catch (error) {
|
|
419
|
-
ctx.logger.warn(`plugin updater failed: ${String(error)}`);
|
|
420
|
-
json(response, 503, { error: publicError(error) });
|
|
421
|
-
}
|
|
422
|
-
}
|
|
423
|
-
});
|
|
424
|
-
const wrapped = () => {
|
|
425
|
-
const bag = updateRouteBag(server);
|
|
426
|
-
if (bag.get(options.endpoint) !== wrapped) return;
|
|
427
|
-
bag.delete(options.endpoint);
|
|
428
|
-
dispose();
|
|
429
|
-
};
|
|
430
|
-
updateRouteBag(server).set(options.endpoint, wrapped);
|
|
431
|
-
return wrapped;
|
|
432
|
-
}
|
|
433
|
-
export {
|
|
434
|
-
PLUGIN_UPDATE_HEADER,
|
|
435
|
-
PLUGIN_UPDATE_IPC,
|
|
436
|
-
isDshCliEntry,
|
|
437
|
-
isNewerVersion,
|
|
438
|
-
isTrustedUpdateRequest,
|
|
439
|
-
mergeMinimumReleaseAgeExclude,
|
|
440
|
-
rawService,
|
|
441
|
-
recordReleaseAgeExclude,
|
|
442
|
-
registerPluginUpdater,
|
|
443
|
-
releaseUpdateRoute,
|
|
444
|
-
resolveUpdateRuntime,
|
|
445
|
-
shouldNotifyParent
|
|
446
|
-
};
|
package/lib/release-age.js
DELETED
|
@@ -1,164 +0,0 @@
|
|
|
1
|
-
import { existsSync } from "node:fs";
|
|
2
|
-
import { readFile, rename, unlink, writeFile } from "node:fs/promises";
|
|
3
|
-
import { resolve } from "node:path";
|
|
4
|
-
function releaseAgeSpec(packageSpec) {
|
|
5
|
-
const at = packageSpec.lastIndexOf("@");
|
|
6
|
-
if (at <= 0) return void 0;
|
|
7
|
-
const name = packageSpec.slice(0, at);
|
|
8
|
-
const version = packageSpec.slice(at + 1);
|
|
9
|
-
if (!isPackageName(name) || !isReleaseVersion(version)) return void 0;
|
|
10
|
-
return { name, version };
|
|
11
|
-
}
|
|
12
|
-
function isPackageName(name) {
|
|
13
|
-
return /^@[^/\s]+\/[^/\s]+$/.test(name) || /^[^@/\s]+$/.test(name);
|
|
14
|
-
}
|
|
15
|
-
function isReleaseVersion(version) {
|
|
16
|
-
return /^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?$/.test(version);
|
|
17
|
-
}
|
|
18
|
-
function unreadableReleaseAgeExclude() {
|
|
19
|
-
return new Error("\u53D1\u5E03\u5E74\u9F84\u767D\u540D\u5355\u65E0\u6CD5\u5B89\u5168\u5408\u5E76\uFF0C\u5DF2\u505C\u6B62\u66F4\u65B0\uFF0C\u907F\u514D\u8FD9\u4E2A profile \u4E4B\u540E\u65E0\u6CD5\u5B89\u88C5\u63D2\u4EF6\u3002");
|
|
20
|
-
}
|
|
21
|
-
function excludeEntry(indent, name, versions, comment = "") {
|
|
22
|
-
const entry = `${indent}- '${name}@${versions.join(" || ")}'`;
|
|
23
|
-
return comment === "" ? entry : `${entry} ${comment}`;
|
|
24
|
-
}
|
|
25
|
-
function parseExcludeLine(line) {
|
|
26
|
-
const dash = line.indexOf("-");
|
|
27
|
-
if (!/^\s+-/.test(line) || dash < 0) return void 0;
|
|
28
|
-
const indent = line.slice(0, dash);
|
|
29
|
-
const raw = line.slice(dash + 1).trim();
|
|
30
|
-
let body = raw;
|
|
31
|
-
let comment = "";
|
|
32
|
-
if (raw.startsWith("'") || raw.startsWith('"')) {
|
|
33
|
-
const quote = raw[0] ?? "'";
|
|
34
|
-
const end = raw.lastIndexOf(quote);
|
|
35
|
-
if (end <= 0) return void 0;
|
|
36
|
-
const rest = raw.slice(end + 1).trim();
|
|
37
|
-
if (rest !== "" && !rest.startsWith("#")) return void 0;
|
|
38
|
-
if (rest.startsWith("#")) comment = rest;
|
|
39
|
-
body = raw.slice(1, end);
|
|
40
|
-
} else if (raw.includes("#")) return void 0;
|
|
41
|
-
if (body.includes("#")) return void 0;
|
|
42
|
-
const versionAt = body.lastIndexOf("@");
|
|
43
|
-
if (versionAt > 0) {
|
|
44
|
-
const name = body.slice(0, versionAt);
|
|
45
|
-
const versions = body.slice(versionAt + 1).split(" || ");
|
|
46
|
-
if (isPackageName(name) && versions.every(isReleaseVersion)) return { indent, name, versions, comment };
|
|
47
|
-
return void 0;
|
|
48
|
-
}
|
|
49
|
-
if (isPackageName(body) || /^@[^/\s]+\/\*$/.test(body)) return { indent, name: body, versions: void 0, comment };
|
|
50
|
-
return void 0;
|
|
51
|
-
}
|
|
52
|
-
function mergeMinimumReleaseAgeExclude(yaml, packageSpec) {
|
|
53
|
-
const spec = releaseAgeSpec(packageSpec);
|
|
54
|
-
if (spec === void 0) throw new Error("\u66F4\u65B0\u7248\u672C\u65E0\u6CD5\u5199\u5165\u53D1\u5E03\u5E74\u9F84\u767D\u540D\u5355\uFF0C\u5DF2\u505C\u6B62\u66F4\u65B0\u3002");
|
|
55
|
-
const eol = yaml.includes("\r\n") ? "\r\n" : "\n";
|
|
56
|
-
const text = yaml.replaceAll("\r\n", "\n");
|
|
57
|
-
const next = mergeReleaseAgeText(text, spec.name, spec.version);
|
|
58
|
-
return eol === "\r\n" ? next.replaceAll("\n", "\r\n") : next;
|
|
59
|
-
}
|
|
60
|
-
function mergeReleaseAgeText(yaml, name, version) {
|
|
61
|
-
if (/minimumReleaseAgeExclude:[^\n\r]/.test(yaml)) throw unreadableReleaseAgeExclude();
|
|
62
|
-
const marker = /(?:^|\n)minimumReleaseAgeExclude:[ \t]*(?:\n|$)/.exec(yaml);
|
|
63
|
-
if (marker === null) {
|
|
64
|
-
const base = yaml.endsWith("\n") || yaml.length === 0 ? yaml : `${yaml}
|
|
65
|
-
`;
|
|
66
|
-
const gap = base.length === 0 || base.endsWith("\n\n") ? "" : "\n";
|
|
67
|
-
return `${base}${gap}minimumReleaseAgeExclude:
|
|
68
|
-
- '${name}@${version}'
|
|
69
|
-
`;
|
|
70
|
-
}
|
|
71
|
-
const keyAt = (marker.index ?? 0) + (marker[0].startsWith("\n") ? 1 : 0);
|
|
72
|
-
let cursor = keyAt + "minimumReleaseAgeExclude:".length;
|
|
73
|
-
while (cursor < yaml.length && (yaml[cursor] === " " || yaml[cursor] === " ")) cursor += 1;
|
|
74
|
-
if (yaml[cursor] !== "\n") throw unreadableReleaseAgeExclude();
|
|
75
|
-
const bodyStart = cursor + 1;
|
|
76
|
-
const lines = [];
|
|
77
|
-
let bodyEnd = bodyStart;
|
|
78
|
-
while (bodyEnd <= yaml.length) {
|
|
79
|
-
const nextNl = yaml.indexOf("\n", bodyEnd);
|
|
80
|
-
const lineEnd = nextNl === -1 ? yaml.length : nextNl;
|
|
81
|
-
const line = yaml.slice(bodyEnd, lineEnd);
|
|
82
|
-
const ended = line.trim() !== "" && !/^[ \t]*#/.test(line) && !/^[ \t]/.test(line);
|
|
83
|
-
if (ended) break;
|
|
84
|
-
if (line.trim() !== "" && !/^[ \t]*#/.test(line) && !/^[ \t]+-/.test(line)) throw unreadableReleaseAgeExclude();
|
|
85
|
-
lines.push(line);
|
|
86
|
-
if (nextNl === -1) {
|
|
87
|
-
bodyEnd = yaml.length;
|
|
88
|
-
break;
|
|
89
|
-
}
|
|
90
|
-
bodyEnd = nextNl + 1;
|
|
91
|
-
if (bodyEnd === yaml.length) break;
|
|
92
|
-
}
|
|
93
|
-
let ours;
|
|
94
|
-
const rewritten = lines.map((line, index) => {
|
|
95
|
-
if (line.trim() === "" || /^[ \t]*#/.test(line)) return line;
|
|
96
|
-
const parsed = parseExcludeLine(line);
|
|
97
|
-
if (parsed === void 0) throw unreadableReleaseAgeExclude();
|
|
98
|
-
if (parsed.name !== name) return line;
|
|
99
|
-
if (ours === void 0) {
|
|
100
|
-
ours = { index, indent: parsed.indent, versions: parsed.versions, comment: parsed.comment };
|
|
101
|
-
return line;
|
|
102
|
-
}
|
|
103
|
-
if (ours.versions !== void 0 && parsed.versions !== void 0) ours.versions = [...ours.versions, ...parsed.versions.filter((item) => !ours?.versions?.includes(item))];
|
|
104
|
-
return "";
|
|
105
|
-
});
|
|
106
|
-
if (ours?.versions === void 0 && ours !== void 0) return yaml;
|
|
107
|
-
const indent = ours?.indent ?? lines.find((line) => /^[ \t]+-/.test(line))?.match(/^([ \t]+)/)?.[1] ?? " ";
|
|
108
|
-
const versions = ours?.versions === void 0 ? [version] : ours.versions.includes(version) ? ours.versions : [...ours.versions, version];
|
|
109
|
-
const entry = excludeEntry(indent, name, versions, ours?.comment ?? "");
|
|
110
|
-
if (ours === void 0) rewritten.push(entry);
|
|
111
|
-
else rewritten[ours.index] = entry;
|
|
112
|
-
const body = rewritten.filter((line) => line !== "").join("\n");
|
|
113
|
-
const prefix = yaml.slice(0, bodyStart);
|
|
114
|
-
const suffix = yaml.slice(bodyEnd);
|
|
115
|
-
const joined = `${prefix}${body}${body.length === 0 || body.endsWith("\n") ? "" : "\n"}${suffix}`;
|
|
116
|
-
return joined.endsWith("\n") ? joined : `${joined}
|
|
117
|
-
`;
|
|
118
|
-
}
|
|
119
|
-
async function guardReleaseAgeExclude(profileDir, packageSpec) {
|
|
120
|
-
const file = resolve(profileDir, "pnpm-workspace.yaml");
|
|
121
|
-
const current = existsSync(file) ? await readFile(file, "utf8") : "packages:\n - .\n";
|
|
122
|
-
mergeMinimumReleaseAgeExclude(current, packageSpec);
|
|
123
|
-
}
|
|
124
|
-
async function replaceTextFile(file, next) {
|
|
125
|
-
const tmp = `${file}.${process.pid}.tmp`;
|
|
126
|
-
const backup = `${file}.${process.pid}.bak`;
|
|
127
|
-
await writeFile(tmp, next, "utf8");
|
|
128
|
-
try {
|
|
129
|
-
await rename(tmp, file);
|
|
130
|
-
return;
|
|
131
|
-
} catch (error) {
|
|
132
|
-
const code = error && typeof error === "object" && "code" in error ? String(error.code) : "";
|
|
133
|
-
if (code !== "EEXIST" && code !== "EPERM" && code !== "EACCES") {
|
|
134
|
-
await unlink(tmp).catch(() => {
|
|
135
|
-
});
|
|
136
|
-
throw error;
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
await unlink(backup).catch(() => {
|
|
140
|
-
});
|
|
141
|
-
await rename(file, backup);
|
|
142
|
-
try {
|
|
143
|
-
await rename(tmp, file);
|
|
144
|
-
} catch (error) {
|
|
145
|
-
await rename(backup, file).catch(() => {
|
|
146
|
-
});
|
|
147
|
-
await unlink(tmp).catch(() => {
|
|
148
|
-
});
|
|
149
|
-
throw error;
|
|
150
|
-
}
|
|
151
|
-
await unlink(backup).catch(() => {
|
|
152
|
-
});
|
|
153
|
-
}
|
|
154
|
-
async function recordReleaseAgeExclude(profileDir, packageSpec) {
|
|
155
|
-
const file = resolve(profileDir, "pnpm-workspace.yaml");
|
|
156
|
-
const current = existsSync(file) ? await readFile(file, "utf8") : "packages:\n - .\n";
|
|
157
|
-
const next = mergeMinimumReleaseAgeExclude(current, packageSpec);
|
|
158
|
-
if (next !== current) await replaceTextFile(file, next);
|
|
159
|
-
}
|
|
160
|
-
export {
|
|
161
|
-
guardReleaseAgeExclude,
|
|
162
|
-
mergeMinimumReleaseAgeExclude,
|
|
163
|
-
recordReleaseAgeExclude
|
|
164
|
-
};
|