@tnunamak/dalan 0.8.2 → 0.8.3
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 +2 -0
- package/package.json +1 -1
- package/src/service-update-runtime.mjs +72 -2
- package/src/service-update.mjs +15 -4
package/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,8 @@ This file lists user-visible changes, newest first. Check the section for your t
|
|
|
4
4
|
|
|
5
5
|
## Unreleased
|
|
6
6
|
|
|
7
|
+
## 0.8.3 (2026-12-03)
|
|
8
|
+
|
|
7
9
|
## 0.8.2 (2026-12-03)
|
|
8
10
|
|
|
9
11
|
- **A repository job that asks for a file in your home directory is refused at submission.** `$HOME/` and `${HOME}/` follow the same rule as `~/`. A warning alone used to let these jobs queue even though the worker could not read the file.
|
package/package.json
CHANGED
|
@@ -5,7 +5,7 @@ import path from 'node:path';
|
|
|
5
5
|
import http from 'node:http';
|
|
6
6
|
import https from 'node:https';
|
|
7
7
|
import net from 'node:net';
|
|
8
|
-
import { spawn } from 'node:child_process';
|
|
8
|
+
import { execFileSync, spawn } from 'node:child_process';
|
|
9
9
|
import { createHash, randomBytes } from 'node:crypto';
|
|
10
10
|
import { fileURLToPath } from 'node:url';
|
|
11
11
|
import { defaultPath } from './paths.mjs';
|
|
@@ -19,6 +19,69 @@ const TIMEOUT = 30_000;
|
|
|
19
19
|
const fail = message => { throw new Error(message); };
|
|
20
20
|
const MAX_TARBALL_BYTES = 200 * 1024 * 1024;
|
|
21
21
|
|
|
22
|
+
function splitSystemdWords(value) {
|
|
23
|
+
const words = [];
|
|
24
|
+
let word = '', quote = null;
|
|
25
|
+
for (let i = 0; i < value.length; i++) {
|
|
26
|
+
const char = value[i];
|
|
27
|
+
if (char === '\\' && i + 1 < value.length) { word += char + value[++i]; continue; }
|
|
28
|
+
if (quote) {
|
|
29
|
+
if (char === quote) quote = null;
|
|
30
|
+
else word += char;
|
|
31
|
+
} else if (char === '"' || char === "'") quote = char;
|
|
32
|
+
else if (/\s/.test(char)) { if (word) { words.push(word); word = ''; } }
|
|
33
|
+
else word += char;
|
|
34
|
+
}
|
|
35
|
+
if (quote) fail('systemd returned an invalid service environment');
|
|
36
|
+
if (word) words.push(word);
|
|
37
|
+
return words;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function unescapeSystemdValue(value) {
|
|
41
|
+
return value.replace(/\\(x[0-9a-fA-F]{2}|[\\nrtse])/g, (_, code) => {
|
|
42
|
+
if (code[0] === 'x') return String.fromCharCode(Number.parseInt(code.slice(1), 16));
|
|
43
|
+
return ({ '\\': '\\', n: '\n', r: '\r', t: '\t', s: ' ', e: '\x1b' })[code];
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function effectiveServiceEnvironment(receipt, options = {}) {
|
|
48
|
+
if (receipt.platform === 'darwin' || !receipt.unit) return {};
|
|
49
|
+
const unit = String(receipt.unit).endsWith('.service') ? String(receipt.unit) : `${receipt.unit}.service`;
|
|
50
|
+
if (!/^[A-Za-z0-9_.@-]+\.service$/.test(unit)) fail('invalid service unit name in receipt');
|
|
51
|
+
let output;
|
|
52
|
+
try {
|
|
53
|
+
output = (options.execFileSync ?? execFileSync)('systemctl', ['--user', 'show', '--property=Environment', '--value', unit], {
|
|
54
|
+
encoding: 'utf8', timeout: 10_000, env: { ...process.env, LC_ALL: 'C' },
|
|
55
|
+
});
|
|
56
|
+
} catch { fail('could not read the effective service environment'); }
|
|
57
|
+
const result = {};
|
|
58
|
+
for (const assignment of splitSystemdWords(String(output))) {
|
|
59
|
+
const equals = assignment.indexOf('=');
|
|
60
|
+
if (equals < 1) continue;
|
|
61
|
+
const name = assignment.slice(0, equals);
|
|
62
|
+
if (Object.hasOwn(CATALOG_SETTINGS, name)) result[name] = unescapeSystemdValue(assignment.slice(equals + 1));
|
|
63
|
+
}
|
|
64
|
+
return result;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// Only settings that shape the tool catalog reach the candidate; paths, ports, credentials and every other unit value stay behind.
|
|
68
|
+
const CATALOG_SETTINGS = Object.freeze({
|
|
69
|
+
DALAN_CATALOG_PROFILE: ['stable', 'questions-v1', 'protocol-v0', 'legacy-v46', 'batch-claim-v1'],
|
|
70
|
+
DALAN_GOZAR_SERVICE: ['0', '1'],
|
|
71
|
+
DALAN_ENABLE_T3_TOOLS: ['0', '1'],
|
|
72
|
+
DALAN_ENABLE_WORKSPACE: ['0', '1'],
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
function catalogSettings(serviceEnv) {
|
|
76
|
+
const result = {};
|
|
77
|
+
for (const [key, value] of Object.entries(serviceEnv ?? {})) {
|
|
78
|
+
if (!Object.hasOwn(CATALOG_SETTINGS, key)) continue;
|
|
79
|
+
if (!CATALOG_SETTINGS[key].includes(value)) fail(`service unit sets an unsupported ${key}`);
|
|
80
|
+
result[key] = value;
|
|
81
|
+
}
|
|
82
|
+
return result;
|
|
83
|
+
}
|
|
84
|
+
|
|
22
85
|
// npm 12 refuses a remote tarball URL as a dependency (allow-remote defaults to none), so the updater fetches the tarball itself,
|
|
23
86
|
// checks every byte against the registry's sha512 integrity, and hands npm only a local file. Works the same on npm 10, 11 and 12.
|
|
24
87
|
export async function fetchTarball(url, integrity, { timeout = 120_000, fetchImpl = fetch } = {}) {
|
|
@@ -291,7 +354,14 @@ export async function probeCandidate(home, receipt, repo, options = {}) {
|
|
|
291
354
|
let child;
|
|
292
355
|
try {
|
|
293
356
|
prepareProbeConfig(home, scratch);
|
|
294
|
-
const
|
|
357
|
+
const serviceEnv = await (options.serviceEnvironment?.(receipt) ?? effectiveServiceEnvironment(receipt, options));
|
|
358
|
+
const inherited = catalogSettings(serviceEnv);
|
|
359
|
+
const env = {
|
|
360
|
+
PATH: process.env.PATH ?? '/usr/bin:/bin', NODE_ENV: 'production',
|
|
361
|
+
DALAN_ENABLE_WORKSPACE: receipt.enable_workspace ? '1' : '0', DALAN_ENABLE_T3_TOOLS: receipt.enable_t3 ? '1' : '0',
|
|
362
|
+
...inherited,
|
|
363
|
+
HOME: scratch, DALAN_HOME: scratch, DALAN_AUTH_MODE: 'builtin', DALAN_BIND_HOST: '127.0.0.1', DALAN_PORT: '0',
|
|
364
|
+
};
|
|
295
365
|
const configDir = defaultPath(scratch, '.config/dot-bridge');
|
|
296
366
|
const pinFile = path.join(configDir, 'service-catalog.json');
|
|
297
367
|
const version = JSON.parse(fs.readFileSync(path.join(repo, 'package.json'), 'utf8')).version;
|
package/src/service-update.mjs
CHANGED
|
@@ -195,10 +195,21 @@ function select(home, repo) {
|
|
|
195
195
|
const current = path.join(home, 'current'), tmp = path.join(home, `.current-${randomUUID()}`);
|
|
196
196
|
fs.symlinkSync(path.relative(home, repo), tmp); fs.renameSync(tmp, current);
|
|
197
197
|
}
|
|
198
|
-
function prune(home, active, previous) {
|
|
198
|
+
function prune(home, active, previous, receiptRepo) {
|
|
199
199
|
const root = path.join(home, 'versions'); if (!fs.existsSync(root)) return;
|
|
200
200
|
const dirs = fs.readdirSync(root, { withFileTypes: true }).filter(d => d.isDirectory() && !d.isSymbolicLink()).map(d => d.name).sort((a,b) => b.localeCompare(a, undefined, { numeric: true }));
|
|
201
|
-
const
|
|
201
|
+
const protectedPaths = [];
|
|
202
|
+
for (const target of [path.join(home, 'current'), receiptRepo]) {
|
|
203
|
+
try { if (target && fs.existsSync(target)) protectedPaths.push(fs.realpathSync(target)); } catch { /* an unavailable target cannot identify a version directory */ }
|
|
204
|
+
}
|
|
205
|
+
const keep = new Set([active, previous].filter(name => dirs.includes(name)));
|
|
206
|
+
for (const name of dirs) {
|
|
207
|
+
const directory = fs.realpathSync(path.join(root, name));
|
|
208
|
+
if (protectedPaths.some(target => {
|
|
209
|
+
const relative = path.relative(directory, target);
|
|
210
|
+
return relative === '' || relative !== '..' && !relative.startsWith(`..${path.sep}`) && !path.isAbsolute(relative);
|
|
211
|
+
})) keep.add(name);
|
|
212
|
+
}
|
|
202
213
|
for (const name of dirs) { if (keep.size >= 3) break; keep.add(name); }
|
|
203
214
|
for (const name of dirs) if (!keep.has(name)) fs.rmSync(path.join(root, name), { recursive: true, force: true });
|
|
204
215
|
}
|
|
@@ -416,7 +427,7 @@ export async function updateService({ home, check = false, yes = false, retry =
|
|
|
416
427
|
const changedCatalog = !legacy && !catalogsEqual(base, tested, workers);
|
|
417
428
|
if (changedCatalog && !yes) {
|
|
418
429
|
result = { outcome: 'connector_refresh_required', current_version: oldVersion, candidate_version: r.version, connector_refresh_required: true, message: 'Worker tool catalogs changed. Run dalan service update --yes with the same --home to switch, then refresh hosted connectors.' };
|
|
419
|
-
prune(home, oldVersion, r.version);
|
|
430
|
+
prune(home, oldVersion, r.version, receipt.repo);
|
|
420
431
|
return result;
|
|
421
432
|
}
|
|
422
433
|
// A catalog difference requires explicit consent; exact parity may proceed without it.
|
|
@@ -470,7 +481,7 @@ export async function updateService({ home, check = false, yes = false, retry =
|
|
|
470
481
|
clearFailedReleaseMarker(home, cleanupWarnings);
|
|
471
482
|
try { fs.rmSync(path.join(home, 'update-transaction.json'), { force: true }); } catch { cleanupWarnings.push('transaction marker cleanup failed'); }
|
|
472
483
|
try { fs.rmSync(txDir, { recursive: true, force: true }); } catch { cleanupWarnings.push('backup cleanup failed'); }
|
|
473
|
-
try { pruneVersions(home, r.version, oldVersion); } catch { cleanupWarnings.push('version pruning failed'); }
|
|
484
|
+
try { pruneVersions(home, r.version, oldVersion, receipt.repo); } catch { cleanupWarnings.push('version pruning failed'); }
|
|
474
485
|
if (cleanupWarnings.length) result.cleanup_warning = cleanupWarnings.join('; ');
|
|
475
486
|
} catch (error) {
|
|
476
487
|
if (error.preserveCandidate) {
|