@mebular/mcp 0.1.2 → 0.2.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/bin/mebular.mjs +504 -3
- package/package.json +8 -3
- package/src/admin.mjs +26 -4
- package/src/asset-roots.mjs +22 -0
- package/src/cloud-switch.mjs +21 -2
- package/src/runtime-current.mjs +240 -0
- package/src/runtime-installer.mjs +1040 -0
- package/src/runtime-layout.mjs +44 -0
- package/src/runtime-providers.mjs +378 -0
- package/src/runtime-service.mjs +812 -0
- package/src/runtime-supervisor.mjs +1673 -0
- package/src/runtime-update.mjs +1074 -0
- package/src/runtime-upgrade.mjs +887 -0
- package/src/serve.mjs +60 -12
- package/src/service-control.mjs +22 -3
- package/src/service-loader.mjs +84 -0
- package/src/version.mjs +1 -1
package/src/cloud-switch.mjs
CHANGED
|
@@ -18,6 +18,7 @@ import {
|
|
|
18
18
|
readRegistry,
|
|
19
19
|
setCurrentProfile,
|
|
20
20
|
} from './profiles.mjs';
|
|
21
|
+
import { loadServiceModule } from './service-loader.mjs';
|
|
21
22
|
|
|
22
23
|
export const SWITCH_INTENT_FILE = 'switch.intent.json';
|
|
23
24
|
|
|
@@ -218,13 +219,31 @@ export function profileServiceDescriptor({
|
|
|
218
219
|
heartbeatDir: home,
|
|
219
220
|
workingDir: home,
|
|
220
221
|
execPath,
|
|
221
|
-
env: {
|
|
222
|
+
env: {
|
|
223
|
+
MEBULAR_HOME: home,
|
|
224
|
+
MEBULAR_CLOUD_ROOT: root,
|
|
225
|
+
MEBULAR_RUNTIME_PROVIDER: 'cli',
|
|
226
|
+
MEBULAR_RUNTIME_OWNER: 'cli',
|
|
227
|
+
MEBULAR_RUNTIME_PATH: cliBin,
|
|
228
|
+
},
|
|
229
|
+
runtime: {
|
|
230
|
+
version: null,
|
|
231
|
+
runtimePath: null,
|
|
232
|
+
cliPath: cliBin,
|
|
233
|
+
provider: 'cli',
|
|
234
|
+
owner: 'cli',
|
|
235
|
+
execPath,
|
|
236
|
+
electron: false,
|
|
237
|
+
cloudRoot: root,
|
|
238
|
+
home,
|
|
239
|
+
},
|
|
222
240
|
};
|
|
223
241
|
}
|
|
224
242
|
|
|
225
243
|
async function serviceLib(deps) {
|
|
244
|
+
// App 仍可注入自己的 service adapter;默认共享 core/CLI 走 loader(bare → bundled dist)。
|
|
226
245
|
if (deps) return deps;
|
|
227
|
-
return
|
|
246
|
+
return loadServiceModule();
|
|
228
247
|
}
|
|
229
248
|
|
|
230
249
|
export async function profileServiceStatus({ descriptor, serviceHome = homedir(), deps } = {}) {
|
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
// 共享 Runtime current 指针:`<cloudRoot>/runtime/current.json`。
|
|
2
|
+
//
|
|
3
|
+
// 只记录**非敏感**的安装事实:version / runtimePath / cliPath / provider / owner /
|
|
4
|
+
// installedAt。读取时同时做结构校验与安装完整性校验(包名、版本、CLI 文件),因此
|
|
5
|
+
// provider 发现不会选中半成品目录;损坏/缺失/无效一律回退为 empty 并带 warning,
|
|
6
|
+
// 不阻塞 attach/service/status。
|
|
7
|
+
//
|
|
8
|
+
// 写入是 tmp → rename 的原子替换;调用方(installer/upgrade/plugin bootstrap)负责在
|
|
9
|
+
// 安装锁/启动锁内调用,保证同一时刻只有一个写者。
|
|
10
|
+
|
|
11
|
+
import { existsSync, readFileSync, statSync } from 'node:fs';
|
|
12
|
+
import { chmod, mkdir, readFile, rename, rm, writeFile } from 'node:fs/promises';
|
|
13
|
+
import { homedir } from 'node:os';
|
|
14
|
+
import { isAbsolute, join, resolve } from 'node:path';
|
|
15
|
+
import { cloudError } from './profiles.mjs';
|
|
16
|
+
import {
|
|
17
|
+
RUNTIME_PACKAGE_NAME,
|
|
18
|
+
RUNTIME_PACKAGE_RELATIVE,
|
|
19
|
+
assertRuntimeVersion,
|
|
20
|
+
} from './runtime-layout.mjs';
|
|
21
|
+
|
|
22
|
+
export const RUNTIME_CURRENT_FILE = 'current.json';
|
|
23
|
+
export const RUNTIME_CURRENT_VERSION = 1;
|
|
24
|
+
export const RUNTIME_CURRENT_PROVIDERS = Object.freeze(['shared']);
|
|
25
|
+
export const RUNTIME_CURRENT_OWNERS = Object.freeze(['shared', 'app', 'cli']);
|
|
26
|
+
|
|
27
|
+
const OWNER_RE = /^[a-z0-9][a-z0-9_.:-]{0,63}$/i;
|
|
28
|
+
|
|
29
|
+
export function runtimeCurrentPath(root) {
|
|
30
|
+
return join(root, 'runtime', RUNTIME_CURRENT_FILE);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function isIsoTime(value) {
|
|
34
|
+
return typeof value === 'string' && Number.isFinite(Date.parse(value));
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function assertAbsolutePath(value, field) {
|
|
38
|
+
if (typeof value !== 'string' || value.trim().length === 0 || !isAbsolute(value.trim())) {
|
|
39
|
+
throw cloudError('RUNTIME_CURRENT_INVALID', `current.${field} 必须是绝对路径`);
|
|
40
|
+
}
|
|
41
|
+
return resolve(value.trim());
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function normalizeOwner(value) {
|
|
45
|
+
if (value === undefined || value === null || value === '') return 'shared';
|
|
46
|
+
if (typeof value !== 'string' || !OWNER_RE.test(value.trim())) {
|
|
47
|
+
throw cloudError('RUNTIME_CURRENT_INVALID', 'current.owner 非法');
|
|
48
|
+
}
|
|
49
|
+
const owner = value.trim();
|
|
50
|
+
if (!RUNTIME_CURRENT_OWNERS.includes(owner)) {
|
|
51
|
+
throw cloudError('RUNTIME_CURRENT_INVALID', `current.owner 必须是 ${RUNTIME_CURRENT_OWNERS.join(' / ')}`);
|
|
52
|
+
}
|
|
53
|
+
return owner;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function normalizeProvider(value) {
|
|
57
|
+
if (value === undefined || value === null || value === '') return 'shared';
|
|
58
|
+
if (typeof value !== 'string' || !RUNTIME_CURRENT_PROVIDERS.includes(value.trim())) {
|
|
59
|
+
throw cloudError('RUNTIME_CURRENT_INVALID', `current.provider 必须是 ${RUNTIME_CURRENT_PROVIDERS.join(' / ')}`);
|
|
60
|
+
}
|
|
61
|
+
return value.trim();
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* 结构规范化(写入严格;读取时异常会转成 invalid 状态)。
|
|
66
|
+
* 只保留白名单字段,避免 current.json 变成秘密/任意字段仓库。
|
|
67
|
+
*/
|
|
68
|
+
export function normalizeRuntimeCurrent(input, { now = new Date().toISOString() } = {}) {
|
|
69
|
+
if (!input || typeof input !== 'object' || Array.isArray(input)) {
|
|
70
|
+
throw cloudError('RUNTIME_CURRENT_INVALID', 'current 必须是对象');
|
|
71
|
+
}
|
|
72
|
+
if (input.v !== undefined && input.v !== RUNTIME_CURRENT_VERSION) {
|
|
73
|
+
throw cloudError('RUNTIME_CURRENT_INVALID', `current 版本必须是 v${RUNTIME_CURRENT_VERSION}`);
|
|
74
|
+
}
|
|
75
|
+
const version = assertRuntimeVersion(input.version);
|
|
76
|
+
const runtimePath = assertAbsolutePath(input.runtimePath, 'runtimePath');
|
|
77
|
+
const cliPath = assertAbsolutePath(input.cliPath, 'cliPath');
|
|
78
|
+
const provider = normalizeProvider(input.provider);
|
|
79
|
+
const owner = normalizeOwner(input.owner);
|
|
80
|
+
const installedAt = isIsoTime(input.installedAt) ? input.installedAt : now;
|
|
81
|
+
return {
|
|
82
|
+
v: RUNTIME_CURRENT_VERSION,
|
|
83
|
+
version,
|
|
84
|
+
runtimePath,
|
|
85
|
+
cliPath,
|
|
86
|
+
provider,
|
|
87
|
+
owner,
|
|
88
|
+
installedAt,
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function readJsonFileSync(path) {
|
|
93
|
+
try {
|
|
94
|
+
return JSON.parse(readFileSync(path, 'utf-8'));
|
|
95
|
+
} catch {
|
|
96
|
+
return null;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function inspectInstalledLayoutSync(current) {
|
|
101
|
+
try {
|
|
102
|
+
if (!statSync(current.runtimePath).isDirectory()) {
|
|
103
|
+
return { ok: false, reason: 'runtime_missing', detail: 'runtime_path_not_directory' };
|
|
104
|
+
}
|
|
105
|
+
} catch {
|
|
106
|
+
return { ok: false, reason: 'runtime_missing', detail: 'runtime_path_missing' };
|
|
107
|
+
}
|
|
108
|
+
if (!existsSync(current.cliPath)) {
|
|
109
|
+
return { ok: false, reason: 'cli_missing', detail: 'cli_path_missing' };
|
|
110
|
+
}
|
|
111
|
+
try {
|
|
112
|
+
const info = statSync(current.cliPath);
|
|
113
|
+
if (!info.isFile()) {
|
|
114
|
+
return { ok: false, reason: 'cli_missing', detail: 'cli_not_file' };
|
|
115
|
+
}
|
|
116
|
+
} catch {
|
|
117
|
+
return { ok: false, reason: 'cli_missing', detail: 'cli_unreadable' };
|
|
118
|
+
}
|
|
119
|
+
const packagePath = join(current.runtimePath, RUNTIME_PACKAGE_RELATIVE);
|
|
120
|
+
const manifest = readJsonFileSync(packagePath);
|
|
121
|
+
if (!manifest || typeof manifest !== 'object') {
|
|
122
|
+
return { ok: false, reason: 'package_missing', detail: 'package_json_missing' };
|
|
123
|
+
}
|
|
124
|
+
if (manifest.name !== undefined && manifest.name !== RUNTIME_PACKAGE_NAME) {
|
|
125
|
+
return { ok: false, reason: 'package_mismatch', detail: 'package_name_mismatch' };
|
|
126
|
+
}
|
|
127
|
+
if (manifest.version !== current.version) {
|
|
128
|
+
return { ok: false, reason: 'package_mismatch', detail: 'package_version_mismatch' };
|
|
129
|
+
}
|
|
130
|
+
return { ok: true };
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
async function readJsonFile(path) {
|
|
134
|
+
try {
|
|
135
|
+
return { ok: true, value: JSON.parse(await readFile(path, 'utf-8')) };
|
|
136
|
+
} catch (error) {
|
|
137
|
+
return { ok: false, missing: error?.code === 'ENOENT', error };
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* 读取 current(永不抛错)。
|
|
143
|
+
*
|
|
144
|
+
* `source`:
|
|
145
|
+
* - `main`:文件有效且指向完整安装;
|
|
146
|
+
* - `missing`:没有 current(合法);
|
|
147
|
+
* - `invalid`:文件损坏/字段非法/指向半成品;调用方必须回退到旧 provider 规则并 warning。
|
|
148
|
+
*/
|
|
149
|
+
export async function readRuntimeCurrent(root, { userHome = homedir() } = {}) {
|
|
150
|
+
const base = { current: null, source: 'missing', usable: false, warnings: [] };
|
|
151
|
+
if (typeof root !== 'string' || root.length === 0) {
|
|
152
|
+
return { ...base, source: 'unavailable', warnings: ['cloudRoot 不可用'] };
|
|
153
|
+
}
|
|
154
|
+
const path = runtimeCurrentPath(root);
|
|
155
|
+
const raw = await readJsonFile(path);
|
|
156
|
+
if (!raw.ok) {
|
|
157
|
+
if (raw.missing) return base;
|
|
158
|
+
return {
|
|
159
|
+
...base,
|
|
160
|
+
source: 'invalid',
|
|
161
|
+
warnings: [`runtime current 无法解析:${displayCurrentPath(path, userHome)}`],
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
let current;
|
|
166
|
+
try {
|
|
167
|
+
current = normalizeRuntimeCurrent(raw.value);
|
|
168
|
+
} catch (error) {
|
|
169
|
+
return {
|
|
170
|
+
...base,
|
|
171
|
+
source: 'invalid',
|
|
172
|
+
warnings: [`runtime current 无效:${error?.message ?? error}`],
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
const inspected = inspectInstalledLayoutSync(current);
|
|
177
|
+
if (!inspected.ok) {
|
|
178
|
+
return {
|
|
179
|
+
current,
|
|
180
|
+
source: 'invalid',
|
|
181
|
+
usable: false,
|
|
182
|
+
warnings: [`runtime current 指向不可用的 runtime(${inspected.reason}/${inspected.detail}):${displayCurrentPath(path, userHome)}`],
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
return { current, source: 'main', usable: true, warnings: [] };
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
function displayCurrentPath(path, userHome) {
|
|
189
|
+
const resolved = resolve(path);
|
|
190
|
+
const home = typeof userHome === 'string' && userHome.length > 0 ? resolve(userHome) : null;
|
|
191
|
+
if (home !== null && (resolved === home || resolved.startsWith(`${home}/`))) {
|
|
192
|
+
return `~${resolved.slice(home.length)}`;
|
|
193
|
+
}
|
|
194
|
+
return resolved;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* 原子写 current:tmp-<pid>(0600) → rename(0600)。
|
|
199
|
+
* 输入先严格校验;`installedAt` 缺省用 now。
|
|
200
|
+
*/
|
|
201
|
+
export async function writeRuntimeCurrent(root, input, { now = new Date().toISOString() } = {}) {
|
|
202
|
+
if (typeof root !== 'string' || root.length === 0) {
|
|
203
|
+
throw cloudError('CLOUD_UNAVAILABLE', '缺少 cloudRoot,无法写 runtime current');
|
|
204
|
+
}
|
|
205
|
+
const current = normalizeRuntimeCurrent({ ...input, v: input?.v ?? RUNTIME_CURRENT_VERSION }, { now });
|
|
206
|
+
const dir = join(root, 'runtime');
|
|
207
|
+
await mkdir(dir, { recursive: true });
|
|
208
|
+
const path = runtimeCurrentPath(root);
|
|
209
|
+
const tmp = `${path}.tmp-${process.pid}`;
|
|
210
|
+
try {
|
|
211
|
+
await writeFile(tmp, `${JSON.stringify(current, null, 2)}\n`, { mode: 0o600 });
|
|
212
|
+
await chmod(tmp, 0o600).catch(() => undefined);
|
|
213
|
+
await rename(tmp, path);
|
|
214
|
+
await chmod(path, 0o600).catch(() => undefined);
|
|
215
|
+
} catch (error) {
|
|
216
|
+
await rm(tmp, { force: true }).catch(() => undefined);
|
|
217
|
+
throw error;
|
|
218
|
+
}
|
|
219
|
+
return current;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/** 清除 current(只用于回滚/测试;不删除 runtime 目录本身)。 */
|
|
223
|
+
export async function clearRuntimeCurrent(root) {
|
|
224
|
+
if (typeof root !== 'string' || root.length === 0) return false;
|
|
225
|
+
await rm(runtimeCurrentPath(root), { force: true });
|
|
226
|
+
return true;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/** 返回给 CLI/status 的非敏感摘要(不含任何秘密)。 */
|
|
230
|
+
export function publicRuntimeCurrent(current) {
|
|
231
|
+
if (!current || typeof current !== 'object') return null;
|
|
232
|
+
return {
|
|
233
|
+
version: current.version ?? null,
|
|
234
|
+
runtimePath: current.runtimePath ?? null,
|
|
235
|
+
cliPath: current.cliPath ?? null,
|
|
236
|
+
provider: current.provider ?? null,
|
|
237
|
+
owner: current.owner ?? null,
|
|
238
|
+
installedAt: current.installedAt ?? null,
|
|
239
|
+
};
|
|
240
|
+
}
|