@capyclaw/capyclaw 0.0.0-stage → 0.2.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.md +22 -2
- package/cordis.patch.yml +5 -0
- package/icon.svg +5 -0
- package/index.js +178 -0
- package/lib/environment.mjs +122 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/manifest.js +18 -0
- package/node_modules/@capyclaw/foundation/README.md +51 -0
- package/node_modules/@capyclaw/foundation/abi/assets.mjs +26 -0
- package/node_modules/@capyclaw/foundation/abi/bridge.mjs +88 -0
- package/node_modules/@capyclaw/foundation/abi/errors.mjs +63 -0
- package/node_modules/@capyclaw/foundation/abi/index.mjs +50 -0
- package/node_modules/@capyclaw/foundation/abi/kinds.mjs +271 -0
- package/node_modules/@capyclaw/foundation/abi/loader.mjs +352 -0
- package/node_modules/@capyclaw/foundation/abi/schema.mjs +98 -0
- package/node_modules/@capyclaw/foundation/abi/spec.mjs +258 -0
- package/node_modules/@capyclaw/foundation/abi/validate.mjs +24 -0
- package/node_modules/@capyclaw/foundation/abi/version.mjs +140 -0
- package/node_modules/@capyclaw/foundation/abi/visibility.mjs +90 -0
- package/node_modules/@capyclaw/foundation/cordis.patch.yml +5 -0
- package/node_modules/@capyclaw/foundation/kit/index.mjs +6 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/client-panel.mjs +98 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/host-event.mjs +35 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/http-router.mjs +69 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/lifecycle-hook.mjs +72 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/own-server.mjs +113 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/raw.mjs +69 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/runtime-driver.mjs +120 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/service-provider.mjs +77 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/skill-provider.mjs +56 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/tool-provider.mjs +50 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/ui-panel.mjs +64 -0
- package/node_modules/@capyclaw/foundation/lib/ctx-service.mjs +42 -0
- package/node_modules/@capyclaw/foundation/lib/host/caps.mjs +38 -0
- package/node_modules/@capyclaw/foundation/lib/host/contract.mjs +88 -0
- package/node_modules/@capyclaw/foundation/lib/host/dsh.mjs +54 -0
- package/node_modules/@capyclaw/foundation/lib/host/memory.mjs +187 -0
- package/node_modules/@capyclaw/foundation/lib/host.mjs +23 -0
- package/node_modules/@capyclaw/foundation/lib/kernel.mjs +195 -0
- package/node_modules/@capyclaw/foundation/lib/skill/scan.mjs +115 -0
- package/node_modules/@capyclaw/foundation/lib/subsystems/events.mjs +60 -0
- package/node_modules/@capyclaw/foundation/lib/subsystems/log.mjs +51 -0
- package/node_modules/@capyclaw/foundation/lib/subsystems/net.mjs +198 -0
- package/node_modules/@capyclaw/foundation/package.json +53 -0
- package/node_modules/@capyclaw/foundation/runtime/index.mjs +20 -0
- package/node_modules/@capyclaw/plugin-kit/INTEGRATION.md +100 -0
- package/node_modules/@capyclaw/plugin-kit/README.md +70 -0
- package/node_modules/@capyclaw/plugin-kit/bin/sync.mjs +103 -0
- package/node_modules/@capyclaw/plugin-kit/package.json +71 -0
- package/node_modules/@capyclaw/plugin-kit/src/cli.mjs +56 -0
- package/node_modules/@capyclaw/plugin-kit/src/config.mjs +142 -0
- package/node_modules/@capyclaw/plugin-kit/src/editor/heartbeat-worker.js +132 -0
- package/node_modules/@capyclaw/plugin-kit/src/editor/heartbeat.mjs +26 -0
- package/node_modules/@capyclaw/plugin-kit/src/editor/lease.mjs +60 -0
- package/node_modules/@capyclaw/plugin-kit/src/editor/shadow.mjs +219 -0
- package/node_modules/@capyclaw/plugin-kit/src/editor/transport.js +13 -0
- package/node_modules/@capyclaw/plugin-kit/src/index.mjs +13 -0
- package/node_modules/@capyclaw/plugin-kit/src/pack.mjs +230 -0
- package/node_modules/@capyclaw/plugin-kit/src/runtime/context.mjs +128 -0
- package/node_modules/@capyclaw/plugin-kit/src/self-contained.mjs +307 -0
- package/node_modules/@capyclaw/plugin-kit/src/service/api.mjs +45 -0
- package/node_modules/@capyclaw/plugin-kit/src/service/http.mjs +113 -0
- package/node_modules/@capyclaw/plugin-kit/src/service/mcp-http.mjs +150 -0
- package/node_modules/@capyclaw/plugin-kit/src/service/server.mjs +131 -0
- package/node_modules/@capyclaw/plugin-kit/src/service/static.mjs +79 -0
- package/node_modules/@capyclaw/plugin-kit/src/skills/provider.mjs +82 -0
- package/node_modules/@capyclaw/plugin-kit/src/store/blob-store.mjs +109 -0
- package/node_modules/@capyclaw/plugin-kit/src/store/content-store.mjs +17 -0
- package/node_modules/@capyclaw/plugin-kit/src/store/journal-store.mjs +89 -0
- package/node_modules/@capyclaw/plugin-kit/src/tools/result.mjs +57 -0
- package/node_modules/@capyclaw/plugin-kit/src/tools/validate.mjs +150 -0
- package/node_modules/@capyclaw/plugin-kit/src/tuning.mjs +82 -0
- package/node_modules/@capyclaw/plugin-kit/src/util/fs-lock.mjs +132 -0
- package/node_modules/@capyclaw/plugin-kit/src/util/http.mjs +130 -0
- package/node_modules/@capyclaw/plugin-kit/src/workspace/manager.mjs +664 -0
- package/node_modules/@capyclaw/plugin-kit/src/zip.mjs +181 -0
- package/package.json +71 -4
- package/test/environment.test.mjs +68 -0
- package/test/package.test.mjs +24 -0
- package/test/plugin.test.mjs +101 -0
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* client-panel@1 适配器:页面开机全局变量 + 可选静态面板。
|
|
3
|
+
*
|
|
4
|
+
* - bootGlobals 必须是同步函数;异步准备放在 prepare(start 阶段)里,
|
|
5
|
+
* 结果通过 state 传给 bootGlobals,避免事件发射器无法 await 的竞态。
|
|
6
|
+
* - 静态 root 与 ui-panel 共用 editorHost + serviceHost 共享前缀。
|
|
7
|
+
*/
|
|
8
|
+
import { fileURLToPath } from 'node:url'
|
|
9
|
+
|
|
10
|
+
import { AbiError, ERROR_CODES } from '../../abi/errors.mjs'
|
|
11
|
+
|
|
12
|
+
export const kind = 'client-panel@1'
|
|
13
|
+
|
|
14
|
+
export function createClientPanelAdapter({ host, net, log }) {
|
|
15
|
+
return {
|
|
16
|
+
kind,
|
|
17
|
+
compile(impl, { spec, config, context }) {
|
|
18
|
+
const ctx = context?.ctx
|
|
19
|
+
if (typeof ctx?.on !== 'function') {
|
|
20
|
+
throw new AbiError(ERROR_CODES.E_ADAPTER_THREW,
|
|
21
|
+
'client-panel@1 需要支持 ctx.on 的 Cordis 宿主', { plugin: spec.name })
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
let unmount
|
|
25
|
+
let editor
|
|
26
|
+
let app
|
|
27
|
+
if (impl.root) {
|
|
28
|
+
const serviceHost = host.http()
|
|
29
|
+
const editorHost = host.ui()
|
|
30
|
+
if (!serviceHost?.createApp || !editorHost?.createEditorHost) {
|
|
31
|
+
throw new AbiError(ERROR_CODES.E_CAP_MISSING,
|
|
32
|
+
'ctx.editorHost / ctx.serviceHost 不可用:无法挂静态面板',
|
|
33
|
+
{ plugin: spec.name, capability: 'os.ui' })
|
|
34
|
+
}
|
|
35
|
+
if (!context?.baseUrl) {
|
|
36
|
+
throw new AbiError(ERROR_CODES.E_BASE_URL_MISSING,
|
|
37
|
+
`插件 ${spec.name} 缺少 baseUrl,无法解析 ${impl.root}`, { plugin: spec.name })
|
|
38
|
+
}
|
|
39
|
+
const dir = fileURLToPath(new URL(impl.root, context.baseUrl))
|
|
40
|
+
editor = editorHost.createEditorHost({
|
|
41
|
+
id: spec.name,
|
|
42
|
+
dir,
|
|
43
|
+
basePath: '',
|
|
44
|
+
index: impl.index,
|
|
45
|
+
})
|
|
46
|
+
app = serviceHost.createApp({ id: spec.name, title: spec.name })
|
|
47
|
+
try {
|
|
48
|
+
for (const route of editor.routes()) app.use(route)
|
|
49
|
+
unmount = net.mount(impl.mount, app)
|
|
50
|
+
} catch (error) {
|
|
51
|
+
/* 装配/挂载失败回收已建 app(editor 静态宿主无 close,GC 回收)。 */
|
|
52
|
+
try { app.close?.() } catch { /* 尽力回收 */ }
|
|
53
|
+
throw new AbiError(ERROR_CODES.E_ADAPTER_THREW,
|
|
54
|
+
`挂载客户端面板 ${impl.mount} 失败:${error.message}`,
|
|
55
|
+
{ plugin: spec.name, prefix: impl.mount, cause: error })
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const panelState = { ready: typeof impl.prepare !== 'function', value: undefined }
|
|
60
|
+
const off = ctx.on('webserver/index-inject', (table) => {
|
|
61
|
+
if (!impl.bootGlobals || !panelState.ready || !Array.isArray(table)) return
|
|
62
|
+
let globals
|
|
63
|
+
try {
|
|
64
|
+
globals = impl.bootGlobals({
|
|
65
|
+
spec, config, log, os: context.os, state: panelState.value,
|
|
66
|
+
})
|
|
67
|
+
} catch (error) {
|
|
68
|
+
log?.error?.(`[foundation] ${spec.name} bootGlobals 失败:${error.message}`)
|
|
69
|
+
return
|
|
70
|
+
}
|
|
71
|
+
if (!globals || typeof globals !== 'object') return
|
|
72
|
+
for (const [name, value] of Object.entries(globals)) {
|
|
73
|
+
if (value !== undefined) table.push({ kind: 'global', name, value })
|
|
74
|
+
}
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
return {
|
|
78
|
+
editor,
|
|
79
|
+
async start() {
|
|
80
|
+
if (typeof impl.prepare === 'function') {
|
|
81
|
+
panelState.value = await impl.prepare({
|
|
82
|
+
spec, config, log, os: context.os,
|
|
83
|
+
})
|
|
84
|
+
panelState.ready = true
|
|
85
|
+
}
|
|
86
|
+
if (unmount) await net.ready()
|
|
87
|
+
},
|
|
88
|
+
dispose() {
|
|
89
|
+
const errors = []
|
|
90
|
+
try { if (typeof off === 'function') off() } catch (error) { errors.push(error) }
|
|
91
|
+
try { if (typeof unmount === 'function') unmount() } catch (error) { errors.push(error) }
|
|
92
|
+
try { app?.close?.() } catch (error) { errors.push(error) }
|
|
93
|
+
if (errors.length) throw errors[0]
|
|
94
|
+
},
|
|
95
|
+
}
|
|
96
|
+
},
|
|
97
|
+
}
|
|
98
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* host-event@1 适配器:把宿主事件订阅交给插件自己的 Cordis ctx。
|
|
3
|
+
*
|
|
4
|
+
* 应用插件只声明事件名与 handler,不接触 ctx;注销由 loader 统一回收。
|
|
5
|
+
*/
|
|
6
|
+
import { AbiError, ERROR_CODES } from '../../abi/errors.mjs'
|
|
7
|
+
|
|
8
|
+
export const kind = 'host-event@1'
|
|
9
|
+
|
|
10
|
+
export function createHostEventAdapter({ log }) {
|
|
11
|
+
return {
|
|
12
|
+
kind,
|
|
13
|
+
compile(impl, { spec, context }) {
|
|
14
|
+
const ctx = context?.ctx
|
|
15
|
+
if (!ctx || typeof ctx.on !== 'function') {
|
|
16
|
+
throw new AbiError(ERROR_CODES.E_ADAPTER_THREW,
|
|
17
|
+
'host-event@1 需要支持 ctx.on 的 Cordis 宿主', { plugin: spec.name, event: impl.event })
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
let dispose
|
|
21
|
+
try {
|
|
22
|
+
const off = ctx.on(impl.event, impl.handler)
|
|
23
|
+
dispose = typeof off === 'function'
|
|
24
|
+
? off
|
|
25
|
+
: () => { if (typeof ctx.off === 'function') ctx.off(impl.event, impl.handler) }
|
|
26
|
+
} catch (error) {
|
|
27
|
+
throw new AbiError(ERROR_CODES.E_ADAPTER_THREW,
|
|
28
|
+
`订阅宿主事件 ${impl.event} 失败:${error.message}`, { plugin: spec.name, event: impl.event, cause: error })
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
log?.debug?.(`[foundation] ${spec.name} 订阅宿主事件 ${impl.event}`)
|
|
32
|
+
return { dispose }
|
|
33
|
+
},
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* http-router@1 适配器:routes(app) 往子 app 上挂路由,内核把子 app 挂到共享宿主的前缀下。
|
|
3
|
+
*
|
|
4
|
+
* 前缀冲突由 service-host 的 sharedHost.mount 检测(重复前缀直接抛错),
|
|
5
|
+
* 应用侧不接触 server、不选端口。
|
|
6
|
+
*
|
|
7
|
+
* 增量(STANDARD v2 V2-1/V2-4 配套,纯向后兼容):
|
|
8
|
+
* - routes 第二参新增 service(name):跨隔离探测公共服务(getService),
|
|
9
|
+
* 让路由插件拿到 ctx.workStore 这类驱动提供的服务,而不必私开句柄;
|
|
10
|
+
* - routes() 返回函数时登记为 disposer:dispose 先 unmount 再调用,
|
|
11
|
+
* 路由层起的定时器/句柄有官方回收点。
|
|
12
|
+
*/
|
|
13
|
+
import { AbiError, ERROR_CODES } from '../../abi/errors.mjs'
|
|
14
|
+
import { getService } from '../ctx-service.mjs'
|
|
15
|
+
|
|
16
|
+
export const kind = 'http-router@1'
|
|
17
|
+
|
|
18
|
+
export function createHttpRouterAdapter({ host, net, log }) {
|
|
19
|
+
return {
|
|
20
|
+
kind,
|
|
21
|
+
compile(impl, { spec, config, context }) {
|
|
22
|
+
const serviceHost = host.http()
|
|
23
|
+
if (!serviceHost?.createApp) {
|
|
24
|
+
throw new AbiError(ERROR_CODES.E_CAP_MISSING,
|
|
25
|
+
'ctx.serviceHost 不可用:无法创建子 app', { plugin: spec.name, capability: 'os.net' })
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const app = serviceHost.createApp({ id: spec.name, title: spec.name })
|
|
29
|
+
const service = (name) => getService(context?.ctx, name)
|
|
30
|
+
let cleanup
|
|
31
|
+
try {
|
|
32
|
+
const returned = impl.routes(app, { spec, config, log, os: context?.os, service })
|
|
33
|
+
if (typeof returned === 'function') cleanup = returned
|
|
34
|
+
} catch (error) {
|
|
35
|
+
/* routes 抛错时 app 已创建:此 entry 尚未入 loader entries,编译回滚够不到,
|
|
36
|
+
* 必须就地回收,否则子 app/句柄泄漏。 */
|
|
37
|
+
try { app.close?.() } catch { /* 尽力回收 */ }
|
|
38
|
+
throw new AbiError(ERROR_CODES.E_ADAPTER_THREW,
|
|
39
|
+
`路由注册失败:${error.message}`, { plugin: spec.name, cause: error })
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
let unmount
|
|
43
|
+
try {
|
|
44
|
+
unmount = net.mount(impl.mount, app)
|
|
45
|
+
} catch (error) {
|
|
46
|
+
/* mount 失败同样回收已建的 app 与 routes 返回的 cleanup。 */
|
|
47
|
+
try { if (cleanup) cleanup() } catch { /* 尽力回收 */ }
|
|
48
|
+
try { app.close?.() } catch { /* 尽力回收 */ }
|
|
49
|
+
throw new AbiError(ERROR_CODES.E_ADAPTER_THREW,
|
|
50
|
+
`挂载前缀 ${impl.mount} 失败:${error.message}`, { plugin: spec.name, prefix: impl.mount, cause: error })
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
log?.debug?.(`[foundation] ${spec.name} 路由挂到 ${impl.mount}`)
|
|
54
|
+
return {
|
|
55
|
+
app,
|
|
56
|
+
start: () => net.ready(),
|
|
57
|
+
dispose: () => {
|
|
58
|
+
/* unmount 与 cleanup 都要执行;cleanup 错误不再吞掉(旧实现 log.warn 后
|
|
59
|
+
* 假装成功,loader 会释放 claims 但定时器/句柄实际泄漏)。错误上抛进
|
|
60
|
+
* loader cleanupErrors,保留「失败保留所有权」语义。 */
|
|
61
|
+
const errors = []
|
|
62
|
+
try { if (typeof unmount === 'function') unmount() } catch (error) { errors.push(error) }
|
|
63
|
+
if (cleanup) { try { cleanup() } catch (error) { errors.push(error) } }
|
|
64
|
+
if (errors.length) throw errors[0]
|
|
65
|
+
},
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
}
|
|
69
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* lifecycle-hook@1 的适配器。
|
|
3
|
+
*
|
|
4
|
+
* 运行时只执行 onStart / onStop。onInstall / onUpgrade 属于安装与升级流程:
|
|
5
|
+
* 这里把它们登记进 hook 注册表(os.hooks),保证「声明了就找得到」。
|
|
6
|
+
* 截至本版本仓内尚无自动 fire 它们的安装/升级消费者(见 plugin-abi.md §4.6)——
|
|
7
|
+
* 这是已声明的集成点,不是已落地的自动钩子。安装器可经 os.hooks.run('install'|'upgrade', args)
|
|
8
|
+
* 主动触发;run() 只提供调用 API,是否/何时调用由安装器决定。
|
|
9
|
+
*/
|
|
10
|
+
import { AbiError, ERROR_CODES } from '../../abi/errors.mjs'
|
|
11
|
+
|
|
12
|
+
export const kind = 'lifecycle-hook@1'
|
|
13
|
+
|
|
14
|
+
export function createHookRegistry() {
|
|
15
|
+
return {
|
|
16
|
+
install: [],
|
|
17
|
+
upgrade: [],
|
|
18
|
+
register(phase, entry) {
|
|
19
|
+
if (!this[phase]) throw new AbiError(ERROR_CODES.E_SCHEMA, `未知生命周期阶段 ${phase}`)
|
|
20
|
+
this[phase].push(entry)
|
|
21
|
+
return () => {
|
|
22
|
+
const index = this[phase].indexOf(entry)
|
|
23
|
+
if (index >= 0) this[phase].splice(index, 1)
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
list() {
|
|
27
|
+
return {
|
|
28
|
+
install: this.install.map((entry) => entry.plugin),
|
|
29
|
+
upgrade: this.upgrade.map((entry) => entry.plugin),
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
/**
|
|
33
|
+
* 安装/升级集成点:按登记顺序触发某阶段全部钩子,逐个 await。
|
|
34
|
+
* 仓内暂无自动调用方(见头注);这是给安装器(plugin-sync / plugin-drop / publisher)
|
|
35
|
+
* 预留的调用 API。未知阶段抛稳定错误码 E_SCHEMA。
|
|
36
|
+
*/
|
|
37
|
+
async run(phase, args = {}) {
|
|
38
|
+
if (!this[phase]) throw new AbiError(ERROR_CODES.E_SCHEMA, `未知生命周期阶段 ${phase}`)
|
|
39
|
+
const results = []
|
|
40
|
+
for (const entry of [...this[phase]]) {
|
|
41
|
+
results.push({ plugin: entry.plugin, result: await entry.run(args) })
|
|
42
|
+
}
|
|
43
|
+
return results
|
|
44
|
+
},
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function createLifecycleAdapter({ hooks, log }) {
|
|
49
|
+
return {
|
|
50
|
+
kind,
|
|
51
|
+
compile(impl, { spec, config, context }) {
|
|
52
|
+
const removers = []
|
|
53
|
+
if (typeof impl.onInstall === 'function') {
|
|
54
|
+
removers.push(hooks.register('install', { plugin: spec.name, run: impl.onInstall }))
|
|
55
|
+
}
|
|
56
|
+
if (typeof impl.onUpgrade === 'function') {
|
|
57
|
+
removers.push(hooks.register('upgrade', { plugin: spec.name, run: impl.onUpgrade }))
|
|
58
|
+
}
|
|
59
|
+
let entered = false
|
|
60
|
+
return {
|
|
61
|
+
async start({ signal } = {}) {
|
|
62
|
+
entered = true
|
|
63
|
+
if (typeof impl.onStart === 'function') await impl.onStart({ plugin: spec.name, config, log, os: context?.os, signal })
|
|
64
|
+
},
|
|
65
|
+
dispose() {
|
|
66
|
+
for (const remove of removers) remove()
|
|
67
|
+
if (entered && typeof impl.onStop === 'function') return impl.onStop({ plugin: spec.name, config, log, os: context?.os })
|
|
68
|
+
},
|
|
69
|
+
}
|
|
70
|
+
},
|
|
71
|
+
}
|
|
72
|
+
}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* own-server@1 适配器:托管一个 HTTP 面。
|
|
3
|
+
*
|
|
4
|
+
* 宿主提供 `web()`(dsh 即 `ctx.webServer`)时,这个面挂到宿主网页端口的路径前缀
|
|
5
|
+
* (`impl.mount`,默认 `/plugins/<id>`)下,**不再新起端口**;只有拿不到 web() 的
|
|
6
|
+
* 宿主才退回 service-host 的独立 loopback origin(凭据中转等必须独立 origin 的场景)。
|
|
7
|
+
*
|
|
8
|
+
* 应用只提供 handler(req,res,api) 与可选 start/stop;监听、端口、锁、健康检查、
|
|
9
|
+
* 关闭全部由宿主 service-host 负责。
|
|
10
|
+
*/
|
|
11
|
+
import { AbiError, ERROR_CODES } from '../../abi/errors.mjs'
|
|
12
|
+
|
|
13
|
+
export const kind = 'own-server@1'
|
|
14
|
+
|
|
15
|
+
function resolvePort(impl, spec, config, fallback) {
|
|
16
|
+
const value = typeof impl.port === 'function' ? impl.port({ spec, config }) : impl.port
|
|
17
|
+
if (value === 'auto') return 0
|
|
18
|
+
if (Number.isInteger(value) && value >= 0 && value <= 65535) return value
|
|
19
|
+
throw new AbiError(ERROR_CODES.E_PLUGIN_SHAPE,
|
|
20
|
+
`own-server port 必须是 0-65535 整数或 "auto",收到 ${String(value)}`,
|
|
21
|
+
{ plugin: spec.name, id: impl.id, port: value, fallback })
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function createOwnServerAdapter({ host, log }) {
|
|
25
|
+
return {
|
|
26
|
+
kind,
|
|
27
|
+
compile(impl, { spec, config, context }) {
|
|
28
|
+
const serviceHost = host.http()
|
|
29
|
+
if (!serviceHost?.createApp) {
|
|
30
|
+
throw new AbiError(ERROR_CODES.E_CAP_MISSING,
|
|
31
|
+
'ctx.serviceHost 不可用:无法托管自有 origin',
|
|
32
|
+
{ plugin: spec.name, capability: 'os.net' })
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const app = serviceHost.createApp({ id: impl.id, title: spec.name })
|
|
36
|
+
const web = typeof host.web === 'function' ? host.web() : null
|
|
37
|
+
const canEmbed = !!(web && typeof web.register === 'function')
|
|
38
|
+
const mount = typeof impl.mount === 'string' && impl.mount
|
|
39
|
+
? impl.mount
|
|
40
|
+
: `/plugins/${impl.id}`
|
|
41
|
+
/* 错误响应必须防 write-after-end:handler 已 res.end() 后再失败(流式响应后
|
|
42
|
+
* 异步出错是常见形态)时,再写会抛 ERR_STREAM_WRITE_AFTER_END——在 .catch 回调
|
|
43
|
+
* 里就是未捕获 rejection,可拖垮整个宿主。已结束只记日志;写本身也包 try/catch。 */
|
|
44
|
+
const failResponse = (res, error) => {
|
|
45
|
+
log?.error?.(`[foundation] ${spec.name} own-server handler 失败:${error?.message || error}`)
|
|
46
|
+
try {
|
|
47
|
+
if (res.writableEnded || res.destroyed) return
|
|
48
|
+
if (!res.headersSent) res.writeHead(500, { 'content-type': 'application/json' })
|
|
49
|
+
res.end(JSON.stringify({ error: 'handler failed' }))
|
|
50
|
+
} catch (writeError) {
|
|
51
|
+
log?.error?.(`[foundation] ${spec.name} own-server 错误响应写入失败:${writeError?.message || writeError}`)
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
app.use((req, res, url) => {
|
|
55
|
+
try {
|
|
56
|
+
const result = impl.handler(req, res, {
|
|
57
|
+
spec, config, log, os: context?.os, url, mount, embedded: canEmbed,
|
|
58
|
+
})
|
|
59
|
+
if (result && typeof result.then === 'function') {
|
|
60
|
+
result.catch((error) => failResponse(res, error))
|
|
61
|
+
}
|
|
62
|
+
} catch (error) {
|
|
63
|
+
failResponse(res, error)
|
|
64
|
+
}
|
|
65
|
+
return true
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
let info
|
|
69
|
+
let started = false
|
|
70
|
+
let stopped = false
|
|
71
|
+
return {
|
|
72
|
+
app,
|
|
73
|
+
/** true = 挂在宿主网页端口的前缀下,没有新起端口。 */
|
|
74
|
+
embedded: canEmbed,
|
|
75
|
+
mount,
|
|
76
|
+
async start() {
|
|
77
|
+
started = true
|
|
78
|
+
info = await app.listen(canEmbed
|
|
79
|
+
? { webServer: web, basePath: mount }
|
|
80
|
+
: { port: resolvePort(impl, spec, config), host: impl.host })
|
|
81
|
+
if (typeof impl.start === 'function') {
|
|
82
|
+
await impl.start({ app, info, spec, config, log, os: context?.os, embedded: canEmbed, mount })
|
|
83
|
+
}
|
|
84
|
+
log?.info?.(`[foundation] ${spec.name} own-server ${impl.id} ${canEmbed ? '挂载' : '监听'} ${info.url}`)
|
|
85
|
+
},
|
|
86
|
+
async dispose() {
|
|
87
|
+
if (stopped) return
|
|
88
|
+
stopped = true
|
|
89
|
+
/* 资源闭合必须在 finally:impl.stop 抛错不能跳过 app.close,否则端口/
|
|
90
|
+
* 监听器永久泄漏且 stopped=true 使重试 no-op(HMR 重挂 EADDRINUSE)。
|
|
91
|
+
* 与 service-provider 的 try{stop}finally{withdraw} 同构。错误仍上抛,
|
|
92
|
+
* 让 loader 记 cleanupErrors(保留所有权语义),但资源已经释放。 */
|
|
93
|
+
const errors = []
|
|
94
|
+
try {
|
|
95
|
+
if (typeof impl.stop === 'function') {
|
|
96
|
+
await impl.stop({ app, info, spec, config, log, os: context?.os, embedded: canEmbed, mount })
|
|
97
|
+
}
|
|
98
|
+
} catch (error) {
|
|
99
|
+
errors.push(error)
|
|
100
|
+
} finally {
|
|
101
|
+
if (started) {
|
|
102
|
+
try { await app.close() } catch (error) { errors.push(error) }
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
if (errors.length > 1) {
|
|
106
|
+
log?.error?.(`[foundation] ${spec.name} own-server dispose 多个错误:${errors.map((e) => e?.message || e).join(';')}`)
|
|
107
|
+
}
|
|
108
|
+
if (errors.length) throw errors[0]
|
|
109
|
+
},
|
|
110
|
+
}
|
|
111
|
+
},
|
|
112
|
+
}
|
|
113
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* raw@1 适配器:逃生舱。
|
|
3
|
+
*
|
|
4
|
+
* 描述符只提供 apply;适配器把插件自己的 Cordis ctx 交给它,并统一处理
|
|
5
|
+
* 同步/异步返回值与 dispose。raw 插件必须登记在 meta/foundation.allowlist.json
|
|
6
|
+
* 的 raw 段,准入闸会把它与普通应用插件分开统计。
|
|
7
|
+
*/
|
|
8
|
+
import { AbiError, ERROR_CODES } from '../../abi/errors.mjs'
|
|
9
|
+
|
|
10
|
+
export const kind = 'raw@1'
|
|
11
|
+
|
|
12
|
+
const thenable = (value) => value && typeof value.then === 'function'
|
|
13
|
+
|
|
14
|
+
function normalize(value) {
|
|
15
|
+
if (typeof value === 'function') return { dispose: value }
|
|
16
|
+
if (value && typeof value === 'object') {
|
|
17
|
+
const instance = {}
|
|
18
|
+
if (typeof value.start === 'function') instance.start = value.start.bind(value)
|
|
19
|
+
if (typeof value.dispose === 'function') instance.dispose = value.dispose.bind(value)
|
|
20
|
+
return instance
|
|
21
|
+
}
|
|
22
|
+
return {}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function createRawAdapter({ log }) {
|
|
26
|
+
return {
|
|
27
|
+
kind,
|
|
28
|
+
compile(impl, { spec, config, context }) {
|
|
29
|
+
const ctx = context?.ctx
|
|
30
|
+
if (!ctx) {
|
|
31
|
+
throw new AbiError(ERROR_CODES.E_ADAPTER_THREW,
|
|
32
|
+
'raw@1 需要 Cordis ctx:请通过 createPlugin 挂载', { plugin: spec.name })
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
let result
|
|
36
|
+
try {
|
|
37
|
+
result = impl.apply({
|
|
38
|
+
ctx, spec, config, log, baseUrl: context.baseUrl, os: context.os,
|
|
39
|
+
})
|
|
40
|
+
} catch (error) {
|
|
41
|
+
throw new AbiError(ERROR_CODES.E_ADAPTER_THREW,
|
|
42
|
+
`raw apply 失败:${error.message}`, { plugin: spec.name, cause: error })
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (thenable(result)) {
|
|
46
|
+
let instance = {}
|
|
47
|
+
/* apply 结算只做归一化,绝不在此 eagerly 跑 instance.start()——旧实现
|
|
48
|
+
* 在 compile 返回的 promise resolve 时就启动,绕过 loader 的 start 阶段与
|
|
49
|
+
* stopRequested:loader 正在回滚/已请求卸载时 raw 仍会起进程/监听。
|
|
50
|
+
* start 交给 loader 在 start 阶段显式触发(此时才尊重 stopRequested)。 */
|
|
51
|
+
const ready = Promise.resolve(result).then((value) => { instance = normalize(value) })
|
|
52
|
+
return {
|
|
53
|
+
start: async ({ signal } = {}) => {
|
|
54
|
+
await ready
|
|
55
|
+
/* 转发 loader 的卸载 signal,让 raw 实例的 start 也能感知取消。 */
|
|
56
|
+
return instance.start ? instance.start({ signal }) : undefined
|
|
57
|
+
},
|
|
58
|
+
dispose: async () => {
|
|
59
|
+
try { await ready } catch { /* apply 失败由 loader 记录 */ }
|
|
60
|
+
if (typeof instance.dispose === 'function') return instance.dispose()
|
|
61
|
+
},
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
log?.debug?.(`[foundation] ${spec.name} 使用 raw@1 逃生舱`)
|
|
66
|
+
return normalize(result)
|
|
67
|
+
},
|
|
68
|
+
}
|
|
69
|
+
}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* runtime-driver@1 适配器:把描述符翻译成 broker 的服务规格。
|
|
3
|
+
*
|
|
4
|
+
* 应用侧不 spawn、不管重试、不做健康检查;这些由 service-broker 的 engine 负责。
|
|
5
|
+
* 端口由描述符或内核配置决定,应用不自己选端口。
|
|
6
|
+
*/
|
|
7
|
+
import { fileURLToPath } from 'node:url'
|
|
8
|
+
|
|
9
|
+
import { AbiError, ERROR_CODES } from '../../abi/errors.mjs'
|
|
10
|
+
|
|
11
|
+
export const kind = 'runtime-driver@1'
|
|
12
|
+
|
|
13
|
+
export function createRuntimeDriverAdapter({ host, config, log }) {
|
|
14
|
+
const defaultPort = config.driverPort ?? 'auto'
|
|
15
|
+
|
|
16
|
+
return {
|
|
17
|
+
kind,
|
|
18
|
+
compile(impl, mountContext) {
|
|
19
|
+
let enabled = true
|
|
20
|
+
if (typeof impl.when === 'function') {
|
|
21
|
+
try {
|
|
22
|
+
enabled = impl.when({ spec: mountContext.spec, config: mountContext.config }) !== false
|
|
23
|
+
} catch (error) {
|
|
24
|
+
throw new AbiError(ERROR_CODES.E_ADAPTER_THREW,
|
|
25
|
+
`runtime-driver when() 失败:${error.message}`, { plugin: mountContext.spec.name, id: impl.id, cause: error })
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
if (!enabled) {
|
|
29
|
+
log?.info?.(`[foundation] ${mountContext.spec.name} 跳过 runtime-driver ${impl.id}(when=false)`)
|
|
30
|
+
return { skipped: true, dispose() {} }
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const broker = host.proc()
|
|
34
|
+
if (!broker?.start || !broker?.stop) {
|
|
35
|
+
throw new AbiError(ERROR_CODES.E_CAP_MISSING,
|
|
36
|
+
'ctx.serviceBroker 不可用:无法拉起长驻进程',
|
|
37
|
+
{ plugin: mountContext.spec.name, capability: 'os.proc' })
|
|
38
|
+
}
|
|
39
|
+
if (!mountContext?.context?.baseUrl) {
|
|
40
|
+
throw new AbiError(ERROR_CODES.E_BASE_URL_MISSING,
|
|
41
|
+
`插件 ${mountContext.spec.name} 缺少 baseUrl,无法解析 ${impl.entry}`,
|
|
42
|
+
{ plugin: mountContext.spec.name })
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const entryPath = fileURLToPath(new URL(impl.entry, mountContext.context.baseUrl))
|
|
46
|
+
const resolvedPort = typeof impl.port === 'function'
|
|
47
|
+
? impl.port({ spec: mountContext.spec, config: mountContext.config })
|
|
48
|
+
: impl.port
|
|
49
|
+
if (resolvedPort !== undefined && resolvedPort !== 'auto'
|
|
50
|
+
&& !(Number.isInteger(resolvedPort) && resolvedPort > 0 && resolvedPort <= 65535)) {
|
|
51
|
+
throw new AbiError(ERROR_CODES.E_PLUGIN_SHAPE,
|
|
52
|
+
`runtime-driver port 必须是 1-65535 整数或 "auto",收到 ${String(resolvedPort)}`,
|
|
53
|
+
{ plugin: mountContext.spec.name, id: impl.id, port: resolvedPort })
|
|
54
|
+
}
|
|
55
|
+
const port = resolvedPort ?? defaultPort
|
|
56
|
+
const portArg = impl.portArg || '--port'
|
|
57
|
+
const cmd = [
|
|
58
|
+
process.execPath,
|
|
59
|
+
entryPath,
|
|
60
|
+
portArg,
|
|
61
|
+
port === 'auto' ? '${PORT}' : String(port),
|
|
62
|
+
...(impl.args || []),
|
|
63
|
+
]
|
|
64
|
+
|
|
65
|
+
/* keepAlive 由声明推导:restart.max===0 表示「不要自动重启」,对应 keepAlive:false。
|
|
66
|
+
* 旧实现恒 true,应用声明 restart:{max:0} 仍被 broker 拉起,切换/回收行为与声明不符。 */
|
|
67
|
+
const keepAlive = !(impl.restart && impl.restart.max === 0)
|
|
68
|
+
const serviceSpec = {
|
|
69
|
+
id: impl.id,
|
|
70
|
+
cmd,
|
|
71
|
+
ports: { main: port },
|
|
72
|
+
url: 'http://127.0.0.1:${PORT}/',
|
|
73
|
+
keepAlive,
|
|
74
|
+
}
|
|
75
|
+
if (impl.health?.path) serviceSpec.healthPath = impl.health.path
|
|
76
|
+
/* 声明的健康期望/超时与重启策略必须透传给 broker,不能被静默丢弃。 */
|
|
77
|
+
if (impl.health?.expect && typeof impl.health.expect === 'object') serviceSpec.expect = impl.health.expect
|
|
78
|
+
if (impl.health?.timeoutMs) serviceSpec.waitMs = impl.health.timeoutMs
|
|
79
|
+
if (impl.restart) serviceSpec.restart = impl.restart
|
|
80
|
+
if (impl.dir) {
|
|
81
|
+
serviceSpec.cwd = fileURLToPath(new URL(impl.dir, mountContext.context.baseUrl))
|
|
82
|
+
}
|
|
83
|
+
const env = impl.env?.({
|
|
84
|
+
spec: mountContext.spec,
|
|
85
|
+
config: mountContext.config,
|
|
86
|
+
os: mountContext.context?.os,
|
|
87
|
+
})
|
|
88
|
+
if (env && typeof env === 'object') serviceSpec.env = env
|
|
89
|
+
|
|
90
|
+
let entered = false
|
|
91
|
+
let stopPromise
|
|
92
|
+
return {
|
|
93
|
+
serviceSpec,
|
|
94
|
+
async start({ signal } = {}) {
|
|
95
|
+
entered = true
|
|
96
|
+
const result = await broker.start(serviceSpec, { signal })
|
|
97
|
+
const failure = (result?.errors || []).find((item) => item.id === impl.id)
|
|
98
|
+
if (failure) {
|
|
99
|
+
throw new AbiError(ERROR_CODES.E_ADAPTER_THREW,
|
|
100
|
+
`长驻进程 ${impl.id} 启动失败:${failure.error}`, { plugin: mountContext.spec.name, id: impl.id })
|
|
101
|
+
}
|
|
102
|
+
log?.info?.(`[foundation] ${impl.id} 已由 broker 拉起(port ${port})`)
|
|
103
|
+
},
|
|
104
|
+
dispose() {
|
|
105
|
+
if (!entered) return
|
|
106
|
+
if (!stopPromise) stopPromise = Promise.resolve().then(async () => {
|
|
107
|
+
const result = await broker.stop(impl.id)
|
|
108
|
+
if (result?.stopped === false) {
|
|
109
|
+
throw new AbiError(ERROR_CODES.E_ADAPTER_THREW,
|
|
110
|
+
`长驻进程 ${impl.id} 未停止:${result.reason || 'broker rejected stop'}`,
|
|
111
|
+
{ plugin: mountContext.spec.name, id: impl.id, result })
|
|
112
|
+
}
|
|
113
|
+
return result
|
|
114
|
+
})
|
|
115
|
+
return stopPromise
|
|
116
|
+
},
|
|
117
|
+
}
|
|
118
|
+
},
|
|
119
|
+
}
|
|
120
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* service-provider@1 适配器:向插件自己的 Cordis ctx 提供一个具名服务。
|
|
3
|
+
*
|
|
4
|
+
* 服务工厂必须同步;需要异步初始化的资源放在 start 钩子里。
|
|
5
|
+
* 注销顺序:先 stop(如果提供),再撤销 ctx.provide。
|
|
6
|
+
*/
|
|
7
|
+
import { AbiError, ERROR_CODES } from '../../abi/errors.mjs'
|
|
8
|
+
|
|
9
|
+
export const kind = 'service-provider@1'
|
|
10
|
+
|
|
11
|
+
const thenable = (value) => value && typeof value.then === 'function'
|
|
12
|
+
|
|
13
|
+
export function createServiceProviderAdapter({ log }) {
|
|
14
|
+
return {
|
|
15
|
+
kind,
|
|
16
|
+
compile(impl, { spec, config, context }) {
|
|
17
|
+
if (typeof impl.when === 'function') {
|
|
18
|
+
let enabled
|
|
19
|
+
try {
|
|
20
|
+
enabled = impl.when({ spec, config, log, os: context.os }) !== false
|
|
21
|
+
} catch (error) {
|
|
22
|
+
throw new AbiError(ERROR_CODES.E_ADAPTER_THREW,
|
|
23
|
+
`service-provider when() 失败:${error.message}`, { plugin: spec.name, service: impl.service, cause: error })
|
|
24
|
+
}
|
|
25
|
+
if (!enabled) {
|
|
26
|
+
log?.info?.(`[foundation] ${spec.name} 跳过 service-provider ${impl.service}(when=false)`)
|
|
27
|
+
return { skipped: true, dispose() {} }
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
const ctx = context?.ctx
|
|
31
|
+
if (!ctx || typeof ctx.provide !== 'function') {
|
|
32
|
+
throw new AbiError(ERROR_CODES.E_ADAPTER_THREW,
|
|
33
|
+
'service-provider@1 需要支持 ctx.provide 的 Cordis 宿主', { plugin: spec.name, service: impl.service })
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
let service
|
|
37
|
+
try {
|
|
38
|
+
service = impl.provide({ spec, config, log, os: context.os })
|
|
39
|
+
} catch (error) {
|
|
40
|
+
throw new AbiError(ERROR_CODES.E_ADAPTER_THREW,
|
|
41
|
+
`服务 ${impl.service} 构建失败:${error.message}`, { plugin: spec.name, service: impl.service, cause: error })
|
|
42
|
+
}
|
|
43
|
+
if (thenable(service)) {
|
|
44
|
+
/* 异步 provide 不被允许;但被拒的 promise 必须先接住,否则抛出
|
|
45
|
+
* E_PLUGIN_SHAPE 之外还泄漏一个 unhandledRejection(可崩宿主)。 */
|
|
46
|
+
service.catch(() => {})
|
|
47
|
+
throw new AbiError(ERROR_CODES.E_PLUGIN_SHAPE,
|
|
48
|
+
`服务 ${impl.service} 的 provide() 必须同步返回服务对象`, { plugin: spec.name, service: impl.service })
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
let withdraw
|
|
52
|
+
try {
|
|
53
|
+
withdraw = ctx.provide(impl.service, service)
|
|
54
|
+
} catch (error) {
|
|
55
|
+
throw new AbiError(ERROR_CODES.E_ADAPTER_THREW,
|
|
56
|
+
`注册服务 ${impl.service} 失败:${error.message}`, { plugin: spec.name, service: impl.service, cause: error })
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
let stopped = false
|
|
60
|
+
return {
|
|
61
|
+
service,
|
|
62
|
+
start: typeof impl.start === 'function'
|
|
63
|
+
? () => impl.start({ service, spec, config, log, os: context.os })
|
|
64
|
+
: undefined,
|
|
65
|
+
async dispose() {
|
|
66
|
+
if (stopped) return
|
|
67
|
+
stopped = true
|
|
68
|
+
try {
|
|
69
|
+
if (typeof impl.stop === 'function') await impl.stop({ service, spec, config, log, os: context.os })
|
|
70
|
+
} finally {
|
|
71
|
+
if (typeof withdraw === 'function') withdraw()
|
|
72
|
+
}
|
|
73
|
+
},
|
|
74
|
+
}
|
|
75
|
+
},
|
|
76
|
+
}
|
|
77
|
+
}
|