@capyclaw/port-sidebar 1.0.2 → 1.0.5
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 +12 -6
- package/cordis.patch.yml +1 -1
- package/node_modules/@capyclaw/foundation/README.md +51 -0
- package/node_modules/@capyclaw/foundation/abi/bridge.mjs +86 -0
- package/node_modules/@capyclaw/foundation/abi/errors.mjs +63 -0
- package/node_modules/@capyclaw/foundation/abi/index.mjs +35 -0
- package/node_modules/@capyclaw/foundation/abi/kinds.mjs +271 -0
- package/node_modules/@capyclaw/foundation/abi/loader.mjs +345 -0
- package/node_modules/@capyclaw/foundation/abi/schema.mjs +98 -0
- package/node_modules/@capyclaw/foundation/abi/spec.mjs +255 -0
- package/node_modules/@capyclaw/foundation/abi/validate.mjs +24 -0
- package/node_modules/@capyclaw/foundation/abi/version.mjs +82 -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 +55 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/own-server.mjs +113 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/raw.mjs +68 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/runtime-driver.mjs +113 -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 +34 -0
- package/node_modules/@capyclaw/foundation/lib/host/contract.mjs +85 -0
- package/node_modules/@capyclaw/foundation/lib/host/dsh.mjs +50 -0
- package/node_modules/@capyclaw/foundation/lib/host/memory.mjs +160 -0
- package/node_modules/@capyclaw/foundation/lib/host.mjs +23 -0
- package/node_modules/@capyclaw/foundation/lib/kernel.mjs +185 -0
- package/node_modules/@capyclaw/foundation/lib/skill/scan.mjs +87 -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 +188 -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 +69 -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 +129 -0
- package/node_modules/@capyclaw/plugin-kit/src/editor/lease.mjs +139 -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/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 +656 -0
- package/node_modules/@capyclaw/plugin-kit/src/zip.mjs +181 -0
- package/package.json +65 -62
- package/plugin.mjs +76 -0
- package/lib/host.mjs +0 -71
- package/test/plugin.test.mjs +0 -111
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 极简跨平台 ZIP 读写(store + deflate 读,store 写;无外部依赖)——全仓单一真源。
|
|
3
|
+
*
|
|
4
|
+
* 之前三处实现各持一份(plugin-development 写 / tools-plugin-release 读 /
|
|
5
|
+
* @capyclaw/suite-kit zip-read 读),本模块合并之。旧位置保留兼容 shim。
|
|
6
|
+
*
|
|
7
|
+
* 写入:store 模式(内容不压缩,跨平台稳定,产物可被任何标准读取器读)。
|
|
8
|
+
* 读取:支持 store(0) 与 deflate(8);接受 Buffer 或文件路径——本地文件与
|
|
9
|
+
* 远端下载源共用同一条校验/解包路径。
|
|
10
|
+
*/
|
|
11
|
+
import fs from 'node:fs'
|
|
12
|
+
import path from 'node:path'
|
|
13
|
+
import zlib from 'node:zlib'
|
|
14
|
+
|
|
15
|
+
/* ------------------------------------------------------------------ 写入 */
|
|
16
|
+
|
|
17
|
+
const CRC_TABLE = (() => {
|
|
18
|
+
const table = new Uint32Array(256)
|
|
19
|
+
for (let n = 0; n < 256; n++) {
|
|
20
|
+
let c = n
|
|
21
|
+
for (let k = 0; k < 8; k++) c = (c & 1) ? (0xedb88320 ^ (c >>> 1)) : (c >>> 1)
|
|
22
|
+
table[n] = c >>> 0
|
|
23
|
+
}
|
|
24
|
+
return table
|
|
25
|
+
})()
|
|
26
|
+
|
|
27
|
+
function crc32(buffer) {
|
|
28
|
+
let crc = 0xffffffff
|
|
29
|
+
for (let i = 0; i < buffer.length; i++) crc = CRC_TABLE[(crc ^ buffer[i]) & 0xff] ^ (crc >>> 8)
|
|
30
|
+
return (crc ^ 0xffffffff) >>> 0
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function dosDateTime(date = new Date()) {
|
|
34
|
+
const year = Math.max(1980, date.getFullYear())
|
|
35
|
+
const time = ((date.getHours() & 0x1f) << 11) | ((date.getMinutes() & 0x3f) << 5) | (Math.floor(date.getSeconds() / 2) & 0x1f)
|
|
36
|
+
const day = (((year - 1980) & 0x7f) << 9) | (((date.getMonth() + 1) & 0x0f) << 5) | (date.getDate() & 0x1f)
|
|
37
|
+
return { time, day }
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function walkFiles(dir, base = dir) {
|
|
41
|
+
const out = []
|
|
42
|
+
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
|
|
43
|
+
const full = path.join(dir, entry.name)
|
|
44
|
+
if (entry.isDirectory()) out.push(...walkFiles(full, base))
|
|
45
|
+
else if (entry.isFile()) out.push(path.relative(base, full).replace(/\\/g, '/'))
|
|
46
|
+
}
|
|
47
|
+
return out.sort()
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** 把目录写成 store 模式 ZIP。返回打包的相对路径列表。 */
|
|
51
|
+
export function writeZipFromDir(sourceDir, outFile) {
|
|
52
|
+
const files = walkFiles(sourceDir)
|
|
53
|
+
const now = dosDateTime()
|
|
54
|
+
const localParts = []
|
|
55
|
+
const centralParts = []
|
|
56
|
+
let offset = 0
|
|
57
|
+
|
|
58
|
+
for (const name of files) {
|
|
59
|
+
const data = fs.readFileSync(path.join(sourceDir, name))
|
|
60
|
+
const nameBytes = Buffer.from(name, 'utf8')
|
|
61
|
+
const crc = crc32(data)
|
|
62
|
+
|
|
63
|
+
const local = Buffer.alloc(30)
|
|
64
|
+
local.writeUInt32LE(0x04034b50, 0)
|
|
65
|
+
local.writeUInt16LE(20, 4)
|
|
66
|
+
local.writeUInt16LE(0x0800, 6)
|
|
67
|
+
local.writeUInt16LE(0, 8)
|
|
68
|
+
local.writeUInt16LE(now.time, 10)
|
|
69
|
+
local.writeUInt16LE(now.day, 12)
|
|
70
|
+
local.writeUInt32LE(crc, 14)
|
|
71
|
+
local.writeUInt32LE(data.length, 18)
|
|
72
|
+
local.writeUInt32LE(data.length, 22)
|
|
73
|
+
local.writeUInt16LE(nameBytes.length, 26)
|
|
74
|
+
local.writeUInt16LE(0, 28)
|
|
75
|
+
localParts.push(local, nameBytes, data)
|
|
76
|
+
|
|
77
|
+
const central = Buffer.alloc(46)
|
|
78
|
+
central.writeUInt32LE(0x02014b50, 0)
|
|
79
|
+
central.writeUInt16LE(20, 4)
|
|
80
|
+
central.writeUInt16LE(20, 6)
|
|
81
|
+
central.writeUInt16LE(0x0800, 8)
|
|
82
|
+
central.writeUInt16LE(0, 10)
|
|
83
|
+
central.writeUInt16LE(now.time, 12)
|
|
84
|
+
central.writeUInt16LE(now.day, 14)
|
|
85
|
+
central.writeUInt32LE(crc, 16)
|
|
86
|
+
central.writeUInt32LE(data.length, 20)
|
|
87
|
+
central.writeUInt32LE(data.length, 24)
|
|
88
|
+
central.writeUInt16LE(nameBytes.length, 28)
|
|
89
|
+
central.writeUInt16LE(0, 30)
|
|
90
|
+
central.writeUInt16LE(0, 32)
|
|
91
|
+
central.writeUInt16LE(0, 34)
|
|
92
|
+
central.writeUInt16LE(0, 36)
|
|
93
|
+
central.writeUInt32LE(0, 38)
|
|
94
|
+
central.writeUInt32LE(offset, 42)
|
|
95
|
+
centralParts.push(central, nameBytes)
|
|
96
|
+
|
|
97
|
+
offset += local.length + nameBytes.length + data.length
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const centralBuffer = Buffer.concat(centralParts)
|
|
101
|
+
const eocd = Buffer.alloc(22)
|
|
102
|
+
eocd.writeUInt32LE(0x06054b50, 0)
|
|
103
|
+
eocd.writeUInt16LE(0, 4)
|
|
104
|
+
eocd.writeUInt16LE(0, 6)
|
|
105
|
+
eocd.writeUInt16LE(files.length, 8)
|
|
106
|
+
eocd.writeUInt16LE(files.length, 10)
|
|
107
|
+
eocd.writeUInt32LE(centralBuffer.length, 12)
|
|
108
|
+
eocd.writeUInt32LE(offset, 16)
|
|
109
|
+
eocd.writeUInt16LE(0, 20)
|
|
110
|
+
|
|
111
|
+
fs.mkdirSync(path.dirname(outFile), { recursive: true })
|
|
112
|
+
fs.writeFileSync(outFile, Buffer.concat([...localParts, centralBuffer, eocd]))
|
|
113
|
+
return files
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/* ------------------------------------------------------------------ 读取 */
|
|
117
|
+
|
|
118
|
+
function locateEocd(data, label) {
|
|
119
|
+
for (let i = data.length - 22; i >= 0; i--) {
|
|
120
|
+
if (data.readUInt32LE(i) === 0x06054b50) return i
|
|
121
|
+
}
|
|
122
|
+
throw new Error(`不是有效的 ZIP: ${label}`)
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* 列出 ZIP 全部条目名。
|
|
127
|
+
* @param {Buffer|string} input - ZIP 字节或文件路径
|
|
128
|
+
*/
|
|
129
|
+
export function listZipEntryNames(input, label = 'zip') {
|
|
130
|
+
const data = Buffer.isBuffer(input) ? input : fs.readFileSync(input)
|
|
131
|
+
const eocd = locateEocd(data, label)
|
|
132
|
+
const count = data.readUInt16LE(eocd + 10)
|
|
133
|
+
let offset = data.readUInt32LE(eocd + 16)
|
|
134
|
+
const names = []
|
|
135
|
+
for (let i = 0; i < count; i++) {
|
|
136
|
+
if (data.readUInt32LE(offset) !== 0x02014b50) throw new Error(`ZIP 中央目录损坏: ${label}`)
|
|
137
|
+
const nameLength = data.readUInt16LE(offset + 28)
|
|
138
|
+
const extraLength = data.readUInt16LE(offset + 30)
|
|
139
|
+
const commentLength = data.readUInt16LE(offset + 32)
|
|
140
|
+
names.push(data.subarray(offset + 46, offset + 46 + nameLength).toString('utf8'))
|
|
141
|
+
offset += 46 + nameLength + extraLength + commentLength
|
|
142
|
+
}
|
|
143
|
+
return names
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* 从 ZIP 读单个条目的原始字节;不存在返回 undefined。
|
|
148
|
+
* @param {Buffer|string} input - ZIP 字节或文件路径
|
|
149
|
+
*/
|
|
150
|
+
export function readZipEntryBuffer(input, entryName, label = 'zip') {
|
|
151
|
+
const data = Buffer.isBuffer(input) ? input : fs.readFileSync(input)
|
|
152
|
+
const eocd = locateEocd(data, label)
|
|
153
|
+
const count = data.readUInt16LE(eocd + 10)
|
|
154
|
+
let offset = data.readUInt32LE(eocd + 16)
|
|
155
|
+
for (let i = 0; i < count; i++) {
|
|
156
|
+
if (data.readUInt32LE(offset) !== 0x02014b50) throw new Error(`ZIP 中央目录损坏: ${label}`)
|
|
157
|
+
const method = data.readUInt16LE(offset + 10)
|
|
158
|
+
const compSize = data.readUInt32LE(offset + 20)
|
|
159
|
+
const nameLength = data.readUInt16LE(offset + 28)
|
|
160
|
+
const extraLength = data.readUInt16LE(offset + 30)
|
|
161
|
+
const commentLength = data.readUInt16LE(offset + 32)
|
|
162
|
+
const localOffset = data.readUInt32LE(offset + 42)
|
|
163
|
+
const name = data.subarray(offset + 46, offset + 46 + nameLength).toString('utf8')
|
|
164
|
+
if (name === entryName) {
|
|
165
|
+
const localNameLength = data.readUInt16LE(localOffset + 26)
|
|
166
|
+
const localExtraLength = data.readUInt16LE(localOffset + 28)
|
|
167
|
+
const start = localOffset + 30 + localNameLength + localExtraLength
|
|
168
|
+
const body = data.subarray(start, start + compSize)
|
|
169
|
+
if (method === 0) return body
|
|
170
|
+
if (method === 8) return zlib.inflateRawSync(body)
|
|
171
|
+
throw new Error(`不支持的 ZIP 压缩方式 ${method}: ${label}`)
|
|
172
|
+
}
|
|
173
|
+
offset += 46 + nameLength + extraLength + commentLength
|
|
174
|
+
}
|
|
175
|
+
return undefined
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** 读取 ZIP 中央目录条目名(文件路径版;打包自检/测试用)。 */
|
|
179
|
+
export function listZipEntries(file) {
|
|
180
|
+
return listZipEntryNames(fs.readFileSync(file), file)
|
|
181
|
+
}
|
package/package.json
CHANGED
|
@@ -1,62 +1,65 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@capyclaw/port-sidebar",
|
|
3
|
-
"version": "1.0.
|
|
4
|
-
"description": "在 DSH 右侧边栏稳定展示一个本机端口页面:每个会话的侧边栏都确保存在指向该端口的 Browser tab,切换话题不消失。",
|
|
5
|
-
"type": "module",
|
|
6
|
-
"main": "./
|
|
7
|
-
"exports": {
|
|
8
|
-
".": "./
|
|
9
|
-
"./client": "./client/client.js",
|
|
10
|
-
"./package.json": "./package.json"
|
|
11
|
-
},
|
|
12
|
-
"dependencies": {
|
|
13
|
-
"@capyclaw/plugin-kit": "^0.2.0"
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
"
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
"
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
"
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
"
|
|
33
|
-
"
|
|
34
|
-
"
|
|
35
|
-
"
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
"
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
"
|
|
46
|
-
"
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
"
|
|
51
|
-
"
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
"
|
|
58
|
-
"
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "@capyclaw/port-sidebar",
|
|
3
|
+
"version": "1.0.5",
|
|
4
|
+
"description": "在 DSH 右侧边栏稳定展示一个本机端口页面:每个会话的侧边栏都确保存在指向该端口的 Browser tab,切换话题不消失。",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./plugin.mjs",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./plugin.mjs",
|
|
9
|
+
"./client": "./client/client.js",
|
|
10
|
+
"./package.json": "./package.json"
|
|
11
|
+
},
|
|
12
|
+
"dependencies": {
|
|
13
|
+
"@capyclaw/plugin-kit": "^0.2.0",
|
|
14
|
+
"@capyclaw/foundation": "^0.1.1"
|
|
15
|
+
},
|
|
16
|
+
"dsh": {
|
|
17
|
+
"bundle": {
|
|
18
|
+
"patch": "./cordis.patch.yml"
|
|
19
|
+
},
|
|
20
|
+
"client": {
|
|
21
|
+
"platform": "web",
|
|
22
|
+
"immediately": true
|
|
23
|
+
},
|
|
24
|
+
"category": "ui",
|
|
25
|
+
"platform": "web"
|
|
26
|
+
},
|
|
27
|
+
"meta": {
|
|
28
|
+
"title": "端口侧边栏",
|
|
29
|
+
"description": "右侧边栏常驻展示指定服务(服务解耦标准:config.service 走中间件 discovery,端口不写死)的界面,切换会话/话题不消失。"
|
|
30
|
+
},
|
|
31
|
+
"files": [
|
|
32
|
+
"plugin.mjs",
|
|
33
|
+
"lib/",
|
|
34
|
+
"client/",
|
|
35
|
+
"cordis.patch.yml",
|
|
36
|
+
"README.md"
|
|
37
|
+
],
|
|
38
|
+
"scripts": {},
|
|
39
|
+
"engines": {
|
|
40
|
+
"dsh": "^0.2.0-rc.2",
|
|
41
|
+
"node": ">=18"
|
|
42
|
+
},
|
|
43
|
+
"author": {
|
|
44
|
+
"name": "CapyClaw",
|
|
45
|
+
"url": "https://github.com/capyclaw",
|
|
46
|
+
"email": "support@capyclaw.dev"
|
|
47
|
+
},
|
|
48
|
+
"repository": {
|
|
49
|
+
"type": "git",
|
|
50
|
+
"url": "git+https://github.com/Maple496/capyclaw.git",
|
|
51
|
+
"directory": "B/official/capyclaw-port-sidebar"
|
|
52
|
+
},
|
|
53
|
+
"bugs": {
|
|
54
|
+
"url": "https://github.com/Maple496/capyclaw/issues"
|
|
55
|
+
},
|
|
56
|
+
"homepage": "https://github.com/capyclaw/dsh-plugins",
|
|
57
|
+
"license": "MIT",
|
|
58
|
+
"publishConfig": {
|
|
59
|
+
"access": "public"
|
|
60
|
+
},
|
|
61
|
+
"bundledDependencies": [
|
|
62
|
+
"@capyclaw/foundation",
|
|
63
|
+
"@capyclaw/plugin-kit"
|
|
64
|
+
]
|
|
65
|
+
}
|
package/plugin.mjs
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @capyclaw/port-sidebar:foundation ABI 入口(宿主半)。
|
|
3
|
+
*
|
|
4
|
+
* 启动时解析目标并做 bridge 健康守卫;通过 lifecycleHook.onStart 完成后,
|
|
5
|
+
* 再订阅 webserver/index-inject,把 __DSH_PORT_SIDEBAR__ 注入页面开机表。
|
|
6
|
+
* 浏览器半仍是 client/client.js,由 dsh.client 元数据加载。
|
|
7
|
+
*/
|
|
8
|
+
import { createPlugin, hostEvent, lifecycleHook } from '@capyclaw/foundation/abi'
|
|
9
|
+
import { resolveTarget } from './lib/url.mjs'
|
|
10
|
+
|
|
11
|
+
export const BOOT_GLOBAL = '__DSH_PORT_SIDEBAR__'
|
|
12
|
+
|
|
13
|
+
const HEALTH_TIMEOUT_MS = 2500
|
|
14
|
+
|
|
15
|
+
async function probeOnce(url, expected) {
|
|
16
|
+
const healthUrl = String(url).replace(/\/+$/, '') + '/healthz'
|
|
17
|
+
const controller = new AbortController()
|
|
18
|
+
const timer = setTimeout(() => controller.abort(), HEALTH_TIMEOUT_MS)
|
|
19
|
+
try {
|
|
20
|
+
const res = await fetch(healthUrl, { signal: controller.signal })
|
|
21
|
+
if (!res.ok) return false
|
|
22
|
+
const body = await res.json()
|
|
23
|
+
return !!body && body.bridge === expected
|
|
24
|
+
} catch {
|
|
25
|
+
return false
|
|
26
|
+
} finally {
|
|
27
|
+
clearTimeout(timer)
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/* 启动期 workbench 可能比 web 注入晚就绪:有限重试(~4s)后仍不就绪才拒绝注入。 */
|
|
32
|
+
async function probeBridge(url, expected, attempts = 7, delayMs = 600) {
|
|
33
|
+
if (!expected) return true
|
|
34
|
+
for (let i = 0; i < attempts; i++) {
|
|
35
|
+
if (await probeOnce(url, expected)) return true
|
|
36
|
+
if (i < attempts - 1) await new Promise((resolve) => setTimeout(resolve, delayMs))
|
|
37
|
+
}
|
|
38
|
+
return false
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const state = { target: undefined, ready: false, config: null }
|
|
42
|
+
|
|
43
|
+
function injectionRows(table) {
|
|
44
|
+
if (!state.ready || state.target === undefined) return
|
|
45
|
+
table.push({ kind: 'global', name: BOOT_GLOBAL, value: state.target })
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export const { name, inject, apply } = createPlugin({
|
|
49
|
+
name: 'port-sidebar',
|
|
50
|
+
implements: [
|
|
51
|
+
lifecycleHook({
|
|
52
|
+
async onStart({ config }) {
|
|
53
|
+
state.config = config
|
|
54
|
+
const target = resolveTarget(config)
|
|
55
|
+
if (target === undefined) {
|
|
56
|
+
console.warn('[dsh-port-sidebar] 启动时未能解析目标端口地址,本次注入不生效')
|
|
57
|
+
return
|
|
58
|
+
}
|
|
59
|
+
if (config.requireBridge) {
|
|
60
|
+
const ok = await probeBridge(target.url, String(config.requireBridge))
|
|
61
|
+
if (!ok) {
|
|
62
|
+
console.error(`[dsh-port-sidebar] 目标 ${target.url} 不是 ${config.requireBridge};拒绝注入(避免侧边栏显示错误实例)`)
|
|
63
|
+
return
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
state.target = target
|
|
67
|
+
state.ready = true
|
|
68
|
+
console.log(`[dsh-port-sidebar] 侧边栏目标: ${target.url}(bridge=${config.requireBridge || 'any'},restoreOnBoot: ${target.restoreOnBoot})`)
|
|
69
|
+
},
|
|
70
|
+
}),
|
|
71
|
+
hostEvent({
|
|
72
|
+
event: 'webserver/index-inject',
|
|
73
|
+
handler: injectionRows,
|
|
74
|
+
}),
|
|
75
|
+
],
|
|
76
|
+
}, import.meta.url)
|
package/lib/host.mjs
DELETED
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* dsh-port-sidebar 宿主半(Cordis 入口)。
|
|
3
|
-
*
|
|
4
|
-
* 不启动任何 HTTP 服务。唯一的职责:向 web 页面的开机注入表
|
|
5
|
-
* (webserver/index-inject 事件)推送一行 global 数据
|
|
6
|
-
* `__DSH_PORT_SIDEBAR__ = { url, restoreOnBoot }`,浏览器半据此
|
|
7
|
-
* 在右侧边栏为每个会话确保存在指向该端口的 Browser tab。
|
|
8
|
-
*
|
|
9
|
-
* 端口注入方式(优先级从高到低):
|
|
10
|
-
* 1. config.url / 环境变量 DSH_PORT_SIDEBAR_URL(完整地址)
|
|
11
|
-
* 2. config.service → 中间件 discovery(services.json)里该服务的实时 url
|
|
12
|
-
* (服务解耦标准:端口事实归 discovery,不写死)
|
|
13
|
-
* 3. config.port / 环境变量 DSH_PORT_SIDEBAR_PORT(拼 http://127.0.0.1:<port>/)
|
|
14
|
-
* 4. cordis.patch.yml 里的默认 config
|
|
15
|
-
*/
|
|
16
|
-
import { resolveTarget } from './url.mjs'
|
|
17
|
-
|
|
18
|
-
export const name = '@capyclaw/port-sidebar'
|
|
19
|
-
|
|
20
|
-
/** 注入到页面的全局变量名(client/client.js 读取同名全局)。 */
|
|
21
|
-
export const BOOT_GLOBAL = '__DSH_PORT_SIDEBAR__'
|
|
22
|
-
|
|
23
|
-
/** 目标健康探测:requireBridge 指定时,目标必须是该 bridge(防止显示错实例)。 */
|
|
24
|
-
const HEALTH_TIMEOUT_MS = 2500
|
|
25
|
-
|
|
26
|
-
async function probeOnce(url, expected) {
|
|
27
|
-
const healthUrl = String(url).replace(/\/+$/, '') + '/healthz'
|
|
28
|
-
const controller = new AbortController()
|
|
29
|
-
const timer = setTimeout(() => controller.abort(), HEALTH_TIMEOUT_MS)
|
|
30
|
-
try {
|
|
31
|
-
const res = await fetch(healthUrl, { signal: controller.signal })
|
|
32
|
-
if (!res.ok) return false
|
|
33
|
-
const body = await res.json()
|
|
34
|
-
return !!body && body.bridge === expected
|
|
35
|
-
} catch {
|
|
36
|
-
return false
|
|
37
|
-
} finally {
|
|
38
|
-
clearTimeout(timer)
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/* 启动期 workbench 可能比 web 注入晚就绪:有限重试(~4s)后仍不就绪才拒绝注入。 */
|
|
43
|
-
async function probeBridge(url, expected, attempts = 7, delayMs = 600) {
|
|
44
|
-
if (!expected) return true
|
|
45
|
-
for (let i = 0; i < attempts; i++) {
|
|
46
|
-
if (await probeOnce(url, expected)) return true
|
|
47
|
-
if (i < attempts - 1) await new Promise((resolve) => setTimeout(resolve, delayMs))
|
|
48
|
-
}
|
|
49
|
-
return false
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
export async function apply(ctx, config = {}) {
|
|
53
|
-
const target = resolveTarget(config)
|
|
54
|
-
if (target === undefined) {
|
|
55
|
-
console.warn('[dsh-port-sidebar] 启动时未能解析目标端口地址,本次注入不生效')
|
|
56
|
-
return async () => {}
|
|
57
|
-
}
|
|
58
|
-
if (config.requireBridge) {
|
|
59
|
-
const ok = await probeBridge(target.url, String(config.requireBridge))
|
|
60
|
-
if (!ok) {
|
|
61
|
-
console.error(`[dsh-port-sidebar] 目标 ${target.url} 不是 ${config.requireBridge};拒绝注入(避免侧边栏显示错误实例)`)
|
|
62
|
-
return async () => {}
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
// 每次收集注入行都会重新 emit(读实时配置,无注册过期问题)。
|
|
66
|
-
ctx.on('webserver/index-inject', (table) => {
|
|
67
|
-
table.push({ kind: 'global', name: BOOT_GLOBAL, value: target })
|
|
68
|
-
})
|
|
69
|
-
console.log(`[dsh-port-sidebar] 侧边栏目标: ${target.url}(bridge=${config.requireBridge || 'any'},restoreOnBoot: ${target.restoreOnBoot})`)
|
|
70
|
-
return async () => {}
|
|
71
|
-
}
|
package/test/plugin.test.mjs
DELETED
|
@@ -1,111 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* dsh-port-sidebar 冒烟测试(node:test,无外部依赖)。
|
|
3
|
-
*
|
|
4
|
-
* 覆盖三类契约:
|
|
5
|
-
* 1. 包清单:dsh.client 声明、exports["./client"]、bundle patch、files;
|
|
6
|
-
* 2. 浏览器半:__ModuleLoader__ 装载协议、id 与包名一致、注入面与行为关键路径;
|
|
7
|
-
* 3. 宿主半:地址解析(config/环境变量/协议白名单)与入口形状。
|
|
8
|
-
*/
|
|
9
|
-
import test from 'node:test'
|
|
10
|
-
import assert from 'node:assert/strict'
|
|
11
|
-
import { existsSync, readFileSync } from 'node:fs'
|
|
12
|
-
import { fileURLToPath } from 'node:url'
|
|
13
|
-
import { dirname, join, resolve } from 'node:path'
|
|
14
|
-
|
|
15
|
-
const packageDir = resolve(dirname(fileURLToPath(import.meta.url)), '..')
|
|
16
|
-
const readJson = (p) => JSON.parse(readFileSync(p, 'utf8'))
|
|
17
|
-
|
|
18
|
-
test('package.json 声明满足 client-modules 扫描契约', () => {
|
|
19
|
-
const pkg = readJson(join(packageDir, 'package.json'))
|
|
20
|
-
assert.equal(pkg.name, '@capyclaw/port-sidebar')
|
|
21
|
-
assert.equal(pkg.type, 'module')
|
|
22
|
-
assert.equal(pkg.main, './lib/host.mjs')
|
|
23
|
-
|
|
24
|
-
// dsh.client:web 平台 + 开机立即装载
|
|
25
|
-
assert.equal(pkg.dsh.client.platform, 'web')
|
|
26
|
-
assert.equal(pkg.dsh.client.immediately, true)
|
|
27
|
-
|
|
28
|
-
// exports["./client"] 指向真实存在的浏览器半文件(字符串或 { default } 均可)
|
|
29
|
-
const clientExport = pkg.exports['./client']
|
|
30
|
-
const clientRel = typeof clientExport === 'string' ? clientExport : clientExport.default
|
|
31
|
-
assert.equal(typeof clientRel, 'string')
|
|
32
|
-
assert.ok(existsSync(join(packageDir, clientRel)), `浏览器半文件不存在: ${clientRel}`)
|
|
33
|
-
|
|
34
|
-
// bundle patch 与宿主入口真实存在
|
|
35
|
-
assert.ok(readFileSync(join(packageDir, pkg.dsh.bundle.patch), 'utf8').includes('@capyclaw/port-sidebar'))
|
|
36
|
-
assert.ok(readFileSync(join(packageDir, pkg.main), 'utf8').length > 0)
|
|
37
|
-
})
|
|
38
|
-
|
|
39
|
-
test('client.js 遵循 __ModuleLoader__ 装载协议且插件形状正确', () => {
|
|
40
|
-
const pkg = readJson(join(packageDir, 'package.json'))
|
|
41
|
-
const source = readFileSync(join(packageDir, pkg.exports['./client']), 'utf8')
|
|
42
|
-
assert.ok(source.includes('window.__ModuleLoader__.load'), '缺少装载协议调用')
|
|
43
|
-
assert.ok(source.includes(`id: '${pkg.name}'`), 'factory id 必须与包名一致')
|
|
44
|
-
assert.ok(source.includes("inject: ['slots', 'sidebarRight', 'sidebarRightTabs']"), '必须声明注入 slots/sidebarRight/sidebarRightTabs')
|
|
45
|
-
assert.ok(source.includes('return module.exports;'), 'factory 必须返回 module.exports')
|
|
46
|
-
assert.ok(source.includes('__DSH_PORT_SIDEBAR__'), '必须读取宿主半注入的全局')
|
|
47
|
-
assert.ok(source.includes('sidebarRightTabs.register'), '必须注册自有 tab 类型')
|
|
48
|
-
assert.ok(source.includes("kind: TAB_KIND"), '必须使用自有 kind')
|
|
49
|
-
assert.ok(source.includes('registerCloseHandler'), '必须注册关闭钩子做保活')
|
|
50
|
-
assert.ok(source.includes('checkAndRevive'), '必须带保活检查')
|
|
51
|
-
assert.ok(source.includes('hasOwnTab'), '关闭判定必须以会话级清单为权威')
|
|
52
|
-
assert.ok(source.includes('isExpanded'), '折叠时不得抢布局')
|
|
53
|
-
assert.ok(source.includes('guardCloseClicks'), '必须在 UI 层拦截关闭点击')
|
|
54
|
-
assert.ok(source.includes("addEventListener('click', guardCloseClicks, true)"), '拦截必须在 capture 阶段')
|
|
55
|
-
assert.ok(source.includes('ensured.add'), '必须防止重复首次打开')
|
|
56
|
-
assert.ok(source.includes('keepAlive'), '保活必须可配置')
|
|
57
|
-
assert.ok(!/\bimport\s/.test(source), '浏览器半不允许静态 import')
|
|
58
|
-
assert.ok(!/export\s+(default|const|function|let|var)\b/.test(source), '浏览器半不允许 ESM 导出')
|
|
59
|
-
})
|
|
60
|
-
|
|
61
|
-
test('宿主半入口形状正确', async () => {
|
|
62
|
-
const pkg = readJson(join(packageDir, 'package.json'))
|
|
63
|
-
const host = await import(`file://${join(packageDir, pkg.main).replace(/\\/g, '/')}`)
|
|
64
|
-
assert.equal(host.name, pkg.name)
|
|
65
|
-
assert.equal(typeof host.apply, 'function')
|
|
66
|
-
assert.equal(host.BOOT_GLOBAL, '__DSH_PORT_SIDEBAR__')
|
|
67
|
-
})
|
|
68
|
-
|
|
69
|
-
test('resolveTarget:默认 127.0.0.1:39600', async () => {
|
|
70
|
-
const { resolveTarget } = await import('../lib/url.mjs')
|
|
71
|
-
const target = resolveTarget({}, {})
|
|
72
|
-
assert.equal(target.url, 'http://127.0.0.1:39600/')
|
|
73
|
-
assert.equal(target.restoreOnBoot, true)
|
|
74
|
-
})
|
|
75
|
-
|
|
76
|
-
test('resolveTarget:config 优先于环境变量', async () => {
|
|
77
|
-
const { resolveTarget } = await import('../lib/url.mjs')
|
|
78
|
-
const target = resolveTarget({ port: 5000 }, { DSH_PORT_SIDEBAR_PORT: '6000' })
|
|
79
|
-
assert.equal(target.url, 'http://127.0.0.1:5000/')
|
|
80
|
-
assert.equal(
|
|
81
|
-
resolveTarget({ url: 'http://localhost:7000/app' }, { DSH_PORT_SIDEBAR_URL: 'http://x:1/' }).url,
|
|
82
|
-
'http://localhost:7000/app',
|
|
83
|
-
)
|
|
84
|
-
})
|
|
85
|
-
|
|
86
|
-
test('resolveTarget:环境变量端口生效', async () => {
|
|
87
|
-
const { resolveTarget } = await import('../lib/url.mjs')
|
|
88
|
-
assert.equal(resolveTarget({}, { DSH_PORT_SIDEBAR_PORT: '4173' }).url, 'http://127.0.0.1:4173/')
|
|
89
|
-
})
|
|
90
|
-
|
|
91
|
-
test('resolveTarget:非 http/https 被拒绝', async () => {
|
|
92
|
-
const { resolveTarget } = await import('../lib/url.mjs')
|
|
93
|
-
assert.equal(resolveTarget({ url: 'file:///etc/passwd' }, {}), undefined)
|
|
94
|
-
assert.equal(resolveTarget({ url: 'not a url' }, {}), undefined)
|
|
95
|
-
})
|
|
96
|
-
|
|
97
|
-
test('resolveTarget:restoreOnBoot 可被 config 与环境变量关闭', async () => {
|
|
98
|
-
const { resolveTarget } = await import('../lib/url.mjs')
|
|
99
|
-
assert.equal(resolveTarget({ restoreOnBoot: false }, {}).restoreOnBoot, false)
|
|
100
|
-
assert.equal(resolveTarget({}, { DSH_PORT_SIDEBAR_RESTORE: 'false' }).restoreOnBoot, false)
|
|
101
|
-
assert.equal(resolveTarget({}, { DSH_PORT_SIDEBAR_RESTORE: '0' }).restoreOnBoot, false)
|
|
102
|
-
assert.equal(resolveTarget({ restoreOnBoot: false }, { DSH_PORT_SIDEBAR_RESTORE: 'true' }).restoreOnBoot, true)
|
|
103
|
-
})
|
|
104
|
-
|
|
105
|
-
test('resolveTarget:keepAlive 默认开启,可被 config 与环境变量关闭', async () => {
|
|
106
|
-
const { resolveTarget } = await import('../lib/url.mjs')
|
|
107
|
-
assert.equal(resolveTarget({}, {}).keepAlive, true)
|
|
108
|
-
assert.equal(resolveTarget({ keepAlive: false }, {}).keepAlive, false)
|
|
109
|
-
assert.equal(resolveTarget({}, { DSH_PORT_SIDEBAR_KEEP_ALIVE: 'false' }).keepAlive, false)
|
|
110
|
-
assert.equal(resolveTarget({ keepAlive: false }, { DSH_PORT_SIDEBAR_KEEP_ALIVE: '1' }).keepAlive, true)
|
|
111
|
-
})
|