@capyclaw/plugin-drop 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +68 -2
- package/archive.js +306 -0
- package/client.js +287 -0
- package/cordis.patch.yml +8 -0
- package/icon.svg +7 -0
- package/index.js +129 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- 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 +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/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 +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 +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 +187 -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 +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 +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 +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/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 +67 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 maple496
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,69 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @capyclaw/plugin-drop
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
把插件压缩包拖到「插件」页,直接安装。
|
|
4
|
+
|
|
5
|
+
## 它做什么
|
|
6
|
+
|
|
7
|
+
装上之后,插件页多出一个 **拖拽安装** 标签。把一个 `.zip` / `.tgz` / `.tar.gz` 插件包拖进去,页面半把字节上传给宿主,宿主在本地解包、校验,再把结果交给插件页原有的安装对话框,最终以**链接包**的方式装进当前 profile。
|
|
8
|
+
|
|
9
|
+
链路一共三段,全在本机完成,不经过网络:
|
|
10
|
+
|
|
11
|
+
```
|
|
12
|
+
拖入文件 → POST /dsh-plugin-drop/prepare → 解包 + 校验 → 返回 { spec, name, version, files }
|
|
13
|
+
↓
|
|
14
|
+
插件页安装对话框(原有流程)
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## 支持的压缩包
|
|
18
|
+
|
|
19
|
+
| 格式 | 判定方式 |
|
|
20
|
+
| --- | --- |
|
|
21
|
+
| `.zip` | 文件头 `PK\x03\x04`,支持 store(method 0)与 deflate(method 8) |
|
|
22
|
+
| `.tgz` / `.tar.gz` | gzip 魔数 `0x1f 0x8b`,解压后按 tar 读 |
|
|
23
|
+
| `.tar` | 后缀 `.tar` |
|
|
24
|
+
|
|
25
|
+
上限:单个包 **32 MiB**、最多 **4096** 个条目。
|
|
26
|
+
|
|
27
|
+
## 校验规则
|
|
28
|
+
|
|
29
|
+
- **路径安全**:拒绝空路径、含 NUL、盘符、绝对路径、含 `..` 的条目 —— 压缩包不能让文件落到目标目录之外;
|
|
30
|
+
- **找插件根**:从最浅层开始找带 `package.json` 的目录(最多下探 3 层),多个候选取最浅的,同层优先带 `dsh.bundle` 的;
|
|
31
|
+
- **必须是 DSH 插件**:`package.json` 必须声明 `dsh.bundle.patch`,且该补丁文件真的在包里;
|
|
32
|
+
- 任何一条不满足都会回到页面半,弹出具体原因(例如「这个 package.json 没有声明 dsh.bundle.patch…」),不会装一半。
|
|
33
|
+
|
|
34
|
+
## 安全边界
|
|
35
|
+
|
|
36
|
+
- **只在 Web 版可用**:宿主半 `inject: ['webServer']`,桌面版没有这条路由,页面半会直接提示不可用;
|
|
37
|
+
- **只接受本机调用**:请求不带 `Origin`(同源 fetch 常见情况)放行;带 `Origin` 时只放行 `localhost` / `127.0.0.1` / `[::1]` / 与 `webServer.getOrigin()` 相同者,其余一律 403;
|
|
38
|
+
- **不覆盖已有目录**:每次安装写到一个以时间戳命名的新目录(`%LOCALAPPDATA%\dsh-drop-plugins\<时间戳>`,Linux/macOS 为 `$XDG_DATA_HOME` 或 `~/.local/share` 下的同名目录下的 `dsh-drop-plugins`),装坏了删目录即可。
|
|
39
|
+
|
|
40
|
+
## 安装
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
dsh plugin add @capyclaw/plugin-drop
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
或在插件管理器里用 `plugin_manager({ action: 'install_bundle', target: '<本包目录或 zip>' })`。
|
|
47
|
+
|
|
48
|
+
卸载:
|
|
49
|
+
|
|
50
|
+
```bash
|
|
51
|
+
dsh plugin remove @capyclaw/plugin-drop
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
卸载只移除插件本身,已经装好的其他插件不受影响。
|
|
55
|
+
|
|
56
|
+
## 已知限制
|
|
57
|
+
|
|
58
|
+
- 一次只能拖一个压缩包,批量安装请逐个拖;
|
|
59
|
+
- 压缩包里的插件仍需满足 DSH 的发布要求(`dsh.bundle.patch` 指向的文件必须存在),否则会被插件管理器拒绝 —— 本插件的校验只是提前把话说清楚,不替代插件管理器的检查。
|
|
60
|
+
|
|
61
|
+
## 开发
|
|
62
|
+
|
|
63
|
+
源码工程在 `B/projects/capyclaw-plugin-drop/`,成品由构建生成,不要手改成品目录:
|
|
64
|
+
|
|
65
|
+
```bash
|
|
66
|
+
cd B/projects/capyclaw-plugin-drop
|
|
67
|
+
npm test # 解包 / 路径安全 / 插件根识别的单元测试,不联网
|
|
68
|
+
npm run build # 生成 B/official/capyclaw-plugin-drop
|
|
69
|
+
```
|
package/archive.js
ADDED
|
@@ -0,0 +1,306 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Archive handling for the "drop to install" bundle.
|
|
3
|
+
*
|
|
4
|
+
* Pure unpacking and package discovery: no HTTP, no Cordis. `unpack` reads the three formats a
|
|
5
|
+
* zip/compressed folder can arrive in, `inspectArchive` finds the DSH plugin root inside the
|
|
6
|
+
* entry list, and `materialize` writes that tree under one persistent local folder whose path
|
|
7
|
+
* the Plugin Manager installer accepts as a `link:` spec.
|
|
8
|
+
*/
|
|
9
|
+
import path from 'node:path'
|
|
10
|
+
import os from 'node:os'
|
|
11
|
+
import { existsSync } from 'node:fs'
|
|
12
|
+
import { mkdir, writeFile, rename, rm } from 'node:fs/promises'
|
|
13
|
+
import { gunzipSync, inflateRawSync } from 'node:zlib'
|
|
14
|
+
import { createHash } from 'node:crypto'
|
|
15
|
+
|
|
16
|
+
const MAX_DEPTH = 3
|
|
17
|
+
// 解压炸弹防护:32MB 的压缩包可能解出 GB 级内容。给解压总量与条目数设硬上限,
|
|
18
|
+
// 超限即抛错(prepare 会归一成 unpack-failed),而不是把宿主内存吃光。
|
|
19
|
+
const MAX_UNPACKED_BYTES = 128 * 1024 * 1024
|
|
20
|
+
const MAX_ENTRIES = 4096
|
|
21
|
+
const tooManyEntries = () => new Error(`压缩包里的文件太多,超出 ${MAX_ENTRIES} 的上限。`)
|
|
22
|
+
const tooLarge = () => new Error(`压缩包解压后超过 ${Math.round(MAX_UNPACKED_BYTES / 1024 / 1024)} MB 上限。`)
|
|
23
|
+
|
|
24
|
+
/** Read a .zip, .tgz, .tar.gz (or bare .tar) buffer into a flat entry list. */
|
|
25
|
+
export function unpack(buffer, filename) {
|
|
26
|
+
if (buffer.length > 2 && buffer[0] === 0x1f && buffer[1] === 0x8b) {
|
|
27
|
+
return untar(gunzipSync(buffer, { maxOutputLength: MAX_UNPACKED_BYTES }))
|
|
28
|
+
}
|
|
29
|
+
if (buffer.length > 4 && buffer[0] === 0x50 && buffer[1] === 0x4b && buffer[2] === 0x03 && buffer[3] === 0x04) {
|
|
30
|
+
return unzip(buffer)
|
|
31
|
+
}
|
|
32
|
+
if (/\.tar$/i.test(filename)) return untar(buffer)
|
|
33
|
+
throw new Error('无法识别的压缩格式,请使用 .zip、.tgz 或 .tar.gz。')
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function untar(buffer) {
|
|
37
|
+
const entries = []
|
|
38
|
+
let offset = 0
|
|
39
|
+
let pendingPath = null
|
|
40
|
+
|
|
41
|
+
while (offset + 512 <= buffer.length) {
|
|
42
|
+
const header = buffer.subarray(offset, offset + 512)
|
|
43
|
+
if (header.every((byte) => byte === 0)) break
|
|
44
|
+
if (entries.length >= MAX_ENTRIES) throw tooManyEntries()
|
|
45
|
+
|
|
46
|
+
const name = readCString(header, 0, 100)
|
|
47
|
+
const prefix = readCString(header, 345, 155)
|
|
48
|
+
const size = readOctal(header, 124, 12)
|
|
49
|
+
const type = String.fromCharCode(header[156] || 0x30)
|
|
50
|
+
const dataStart = offset + 512
|
|
51
|
+
const data = buffer.subarray(dataStart, Math.min(dataStart + size, buffer.length))
|
|
52
|
+
offset = dataStart + Math.ceil(size / 512) * 512
|
|
53
|
+
|
|
54
|
+
if (type === 'x') {
|
|
55
|
+
const pax = parsePax(data)
|
|
56
|
+
if (pax.path) pendingPath = pax.path
|
|
57
|
+
continue
|
|
58
|
+
}
|
|
59
|
+
if (type === 'g') continue
|
|
60
|
+
if (type === 'L') {
|
|
61
|
+
pendingPath = readCString(data, 0, data.length)
|
|
62
|
+
continue
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const entryPath = pendingPath || (prefix ? `${prefix}/${name}` : name)
|
|
66
|
+
pendingPath = null
|
|
67
|
+
if (!entryPath) continue
|
|
68
|
+
|
|
69
|
+
if (type === '5' || entryPath.endsWith('/')) {
|
|
70
|
+
entries.push({ path: entryPath, dir: true })
|
|
71
|
+
continue
|
|
72
|
+
}
|
|
73
|
+
if (type === '0' || type === '7') entries.push({ path: entryPath, data: Buffer.from(data) })
|
|
74
|
+
}
|
|
75
|
+
return entries
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function parsePax(data) {
|
|
79
|
+
const result = {}
|
|
80
|
+
let cursor = 0
|
|
81
|
+
while (cursor < data.length) {
|
|
82
|
+
const space = data.indexOf(0x20, cursor)
|
|
83
|
+
if (space < 0) break
|
|
84
|
+
const length = Number.parseInt(data.subarray(cursor, space).toString('utf8'), 10)
|
|
85
|
+
if (!Number.isFinite(length) || length <= 0) break
|
|
86
|
+
const record = data.subarray(space + 1, cursor + length - 1).toString('utf8')
|
|
87
|
+
const equals = record.indexOf('=')
|
|
88
|
+
if (equals > 0) result[record.slice(0, equals)] = record.slice(equals + 1)
|
|
89
|
+
cursor += length
|
|
90
|
+
}
|
|
91
|
+
return result
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function unzip(buffer) {
|
|
95
|
+
const eocd = findEndOfCentralDirectory(buffer)
|
|
96
|
+
if (eocd < 0) throw new Error('ZIP 结构损坏:找不到目录结尾。')
|
|
97
|
+
|
|
98
|
+
const count = buffer.readUInt16LE(eocd + 10)
|
|
99
|
+
const directoryOffset = buffer.readUInt32LE(eocd + 16)
|
|
100
|
+
const entries = []
|
|
101
|
+
let cursor = directoryOffset
|
|
102
|
+
let total = 0
|
|
103
|
+
|
|
104
|
+
for (let index = 0; index < count; index += 1) {
|
|
105
|
+
if (cursor + 46 > buffer.length || buffer.readUInt32LE(cursor) !== 0x02014b50) break
|
|
106
|
+
if (entries.length >= MAX_ENTRIES) throw tooManyEntries()
|
|
107
|
+
const method = buffer.readUInt16LE(cursor + 10)
|
|
108
|
+
const compressedSize = buffer.readUInt32LE(cursor + 20)
|
|
109
|
+
const nameLength = buffer.readUInt16LE(cursor + 28)
|
|
110
|
+
const extraLength = buffer.readUInt16LE(cursor + 30)
|
|
111
|
+
const commentLength = buffer.readUInt16LE(cursor + 32)
|
|
112
|
+
const localOffset = buffer.readUInt32LE(cursor + 42)
|
|
113
|
+
const name = buffer.subarray(cursor + 46, cursor + 46 + nameLength).toString('utf8')
|
|
114
|
+
cursor += 46 + nameLength + extraLength + commentLength
|
|
115
|
+
|
|
116
|
+
if (!name) continue
|
|
117
|
+
if (name.endsWith('/')) {
|
|
118
|
+
entries.push({ path: name, dir: true })
|
|
119
|
+
continue
|
|
120
|
+
}
|
|
121
|
+
if (buffer.readUInt32LE(localOffset) !== 0x04034b50) throw new Error('ZIP 结构损坏:本地文件头无效。')
|
|
122
|
+
const localNameLength = buffer.readUInt16LE(localOffset + 26)
|
|
123
|
+
const localExtraLength = buffer.readUInt16LE(localOffset + 28)
|
|
124
|
+
const dataStart = localOffset + 30 + localNameLength + localExtraLength
|
|
125
|
+
const raw = buffer.subarray(dataStart, Math.min(dataStart + compressedSize, buffer.length))
|
|
126
|
+
|
|
127
|
+
let data
|
|
128
|
+
if (method === 0) {
|
|
129
|
+
if (total + raw.length > MAX_UNPACKED_BYTES) throw tooLarge()
|
|
130
|
+
data = Buffer.from(raw)
|
|
131
|
+
total += raw.length
|
|
132
|
+
} else if (method === 8) {
|
|
133
|
+
// maxOutputLength 让 zlib 在超出剩余额度时立刻抛错,而不是先解出巨量数据。
|
|
134
|
+
data = inflateRawSync(raw, { maxOutputLength: MAX_UNPACKED_BYTES - total })
|
|
135
|
+
total += data.length
|
|
136
|
+
} else throw new Error(`不支持的 ZIP 压缩方式(method ${method})。`)
|
|
137
|
+
|
|
138
|
+
entries.push({ path: name, data })
|
|
139
|
+
}
|
|
140
|
+
return entries
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function findEndOfCentralDirectory(buffer) {
|
|
144
|
+
const minimum = Math.max(0, buffer.length - 0xffff - 22)
|
|
145
|
+
for (let cursor = buffer.length - 22; cursor >= minimum; cursor -= 1) {
|
|
146
|
+
if (buffer.readUInt32LE(cursor) === 0x06054b50) return cursor
|
|
147
|
+
}
|
|
148
|
+
return -1
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function readCString(buffer, start, length) {
|
|
152
|
+
const slice = buffer.subarray(start, Math.min(start + length, buffer.length))
|
|
153
|
+
const end = slice.indexOf(0)
|
|
154
|
+
return (end < 0 ? slice : slice.subarray(0, end)).toString('utf8').trim()
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function readOctal(buffer, start, length) {
|
|
158
|
+
const value = Number.parseInt(readCString(buffer, start, length), 8)
|
|
159
|
+
return Number.isFinite(value) && value > 0 ? value : 0
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// ---------------------------------------------------------------------------
|
|
163
|
+
// package discovery
|
|
164
|
+
// ---------------------------------------------------------------------------
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Normalize an archive path to a POSIX relative path, or drop it.
|
|
168
|
+
*
|
|
169
|
+
* Absolute entries (`/etc/x`, `\\server\share`, `C:/x`) are dropped rather than flattened: a
|
|
170
|
+
* plugin archive never needs them, and keeping them would let a malformed zip scatter files
|
|
171
|
+
* next to the package folder instead of inside it.
|
|
172
|
+
*/
|
|
173
|
+
export function safeRelative(entryPath) {
|
|
174
|
+
const normalized = entryPath.replace(/\\/g, '/')
|
|
175
|
+
const absolute = normalized.startsWith('/') || /^[a-zA-Z]:/.test(normalized)
|
|
176
|
+
if (!normalized || normalized.includes('\0') || absolute) return undefined
|
|
177
|
+
const parts = normalized.split('/').filter((part) => part.length > 0 && part !== '.')
|
|
178
|
+
if (parts.length === 0 || parts.includes('..')) return undefined
|
|
179
|
+
return parts.join('/')
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function indexEntries(entries) {
|
|
183
|
+
const files = new Map()
|
|
184
|
+
for (const entry of entries) {
|
|
185
|
+
const relative = safeRelative(entry.path)
|
|
186
|
+
if (!relative) continue
|
|
187
|
+
files.set(relative, entry)
|
|
188
|
+
}
|
|
189
|
+
return files
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Find the DSH plugin root: the shallowest directory holding a package.json, preferring one
|
|
194
|
+
* that declares `dsh.bundle`. Throws with an actionable message when there is none.
|
|
195
|
+
*/
|
|
196
|
+
export function inspectArchive(entries) {
|
|
197
|
+
const files = indexEntries(entries)
|
|
198
|
+
const candidates = []
|
|
199
|
+
|
|
200
|
+
for (const [key, entry] of files) {
|
|
201
|
+
if (entry.dir || !key.endsWith('package.json')) continue
|
|
202
|
+
const parts = key.split('/')
|
|
203
|
+
const depth = parts.length - 1
|
|
204
|
+
if (depth > MAX_DEPTH) continue
|
|
205
|
+
let manifest
|
|
206
|
+
try {
|
|
207
|
+
manifest = JSON.parse(entry.data.toString('utf8'))
|
|
208
|
+
} catch {
|
|
209
|
+
continue
|
|
210
|
+
}
|
|
211
|
+
if (!manifest || typeof manifest !== 'object' || typeof manifest.name !== 'string') continue
|
|
212
|
+
candidates.push({ dir: parts.slice(0, -1).join('/'), depth, manifest })
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
if (candidates.length === 0) {
|
|
216
|
+
throw new Error('压缩包里没有找到 package.json,请把插件包本身压缩后再拖进来。')
|
|
217
|
+
}
|
|
218
|
+
candidates.sort((left, right) => left.depth - right.depth)
|
|
219
|
+
const root = candidates.find((candidate) => candidate.manifest.dsh && candidate.manifest.dsh.bundle) || candidates[0]
|
|
220
|
+
const manifest = root.manifest
|
|
221
|
+
|
|
222
|
+
const patch = manifest.dsh && manifest.dsh.bundle ? manifest.dsh.bundle.patch : undefined
|
|
223
|
+
if (!patch) throw new Error('这个 package.json 没有声明 dsh.bundle.patch,不是可安装的 DSH 插件包。')
|
|
224
|
+
const patchRelative = safeRelative(String(patch))
|
|
225
|
+
if (!patchRelative) throw new Error('package.json 里的 dsh.bundle.patch 路径不合法。')
|
|
226
|
+
const patchKey = root.dir ? `${root.dir}/${patchRelative}` : patchRelative
|
|
227
|
+
if (!files.has(patchKey)) throw new Error(`插件包缺少补丁文件 ${patch}。`)
|
|
228
|
+
|
|
229
|
+
return {
|
|
230
|
+
dir: root.dir,
|
|
231
|
+
name: manifest.name,
|
|
232
|
+
version: typeof manifest.version === 'string' ? manifest.version : '0.0.0',
|
|
233
|
+
description: typeof manifest.description === 'string' ? manifest.description : '',
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/** Root folder that holds the unpacked plugin sources users install from. */
|
|
238
|
+
export function storeRoot() {
|
|
239
|
+
const base = process.env.LOCALAPPDATA || process.env.XDG_DATA_HOME || path.join(os.homedir(), '.local', 'share')
|
|
240
|
+
return path.join(base, 'dsh-drop-plugins')
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/** Write the tree under one content-addressed folder and answer the plugin package directory inside it. */
|
|
244
|
+
export async function materialize(entries, packageDir) {
|
|
245
|
+
const normalized = []
|
|
246
|
+
for (const entry of entries) {
|
|
247
|
+
const relative = safeRelative(entry.path)
|
|
248
|
+
if (!relative) continue
|
|
249
|
+
normalized.push({ relative, dir: !!entry.dir, data: entry.data })
|
|
250
|
+
}
|
|
251
|
+
normalized.sort((a, b) => (a.relative < b.relative ? -1 : a.relative > b.relative ? 1 : 0))
|
|
252
|
+
|
|
253
|
+
// 内容地址化:同一份压缩包永远落到同一个目录。修复前每次 prepare 都新建时间戳目录,
|
|
254
|
+
// 只增不减、重复安装不幂等、同毫秒还会碰撞合并树。
|
|
255
|
+
const hash = createHash('sha256')
|
|
256
|
+
for (const e of normalized) {
|
|
257
|
+
hash.update(e.relative)
|
|
258
|
+
hash.update(e.dir ? '\0dir' : '\0file')
|
|
259
|
+
if (!e.dir && e.data) hash.update(e.data)
|
|
260
|
+
}
|
|
261
|
+
const base = path.join(storeRoot(), hash.digest('hex'))
|
|
262
|
+
const target = packageDir ? path.join(base, ...packageDir.split('/')) : base
|
|
263
|
+
|
|
264
|
+
// 幂等复用:内容已落盘就直接返回,不再重复写盘。
|
|
265
|
+
if (existsSync(base)) {
|
|
266
|
+
await mkdir(target, { recursive: true })
|
|
267
|
+
return target
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
// 先写临时目录,成功后原子 rename 成内容地址目录;任何失败都清掉临时目录,不留半截垃圾。
|
|
271
|
+
const tmp = `${base}.tmp-${process.pid}-${Date.now()}`
|
|
272
|
+
try {
|
|
273
|
+
await mkdir(tmp, { recursive: true })
|
|
274
|
+
for (const e of normalized) {
|
|
275
|
+
const dest = path.join(tmp, ...e.relative.split('/'))
|
|
276
|
+
if (!isInside(tmp, dest)) continue
|
|
277
|
+
if (e.dir) {
|
|
278
|
+
await mkdir(dest, { recursive: true })
|
|
279
|
+
continue
|
|
280
|
+
}
|
|
281
|
+
await mkdir(path.dirname(dest), { recursive: true })
|
|
282
|
+
await writeFile(dest, e.data)
|
|
283
|
+
}
|
|
284
|
+
if (packageDir) await mkdir(path.join(tmp, ...packageDir.split('/')), { recursive: true })
|
|
285
|
+
try {
|
|
286
|
+
await rename(tmp, base)
|
|
287
|
+
} catch (err) {
|
|
288
|
+
// 并发:另一进程已抢先落盘同一内容 → 复用它的成果,清掉自己的临时目录。
|
|
289
|
+
if (existsSync(base)) {
|
|
290
|
+
await rm(tmp, { recursive: true, force: true })
|
|
291
|
+
await mkdir(target, { recursive: true })
|
|
292
|
+
return target
|
|
293
|
+
}
|
|
294
|
+
throw err
|
|
295
|
+
}
|
|
296
|
+
return target
|
|
297
|
+
} catch (err) {
|
|
298
|
+
await rm(tmp, { recursive: true, force: true }).catch(() => {})
|
|
299
|
+
throw err
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
function isInside(parent, target) {
|
|
304
|
+
const relative = path.relative(parent, target)
|
|
305
|
+
return relative !== '' && !relative.startsWith('..') && !path.isAbsolute(relative)
|
|
306
|
+
}
|
package/client.js
ADDED
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Browser half of the "drop to install" bundle.
|
|
3
|
+
*
|
|
4
|
+
* Adds one tab to the Plugins page. Dropping an archive on it uploads the bytes to the Host
|
|
5
|
+
* half, which unpacks them into a local plugin folder; the tab then hands that folder to the
|
|
6
|
+
* page's own `install({ spec })`, so the shipped install dialog keeps the confirm step,
|
|
7
|
+
* progress, cancellation and diagnostics.
|
|
8
|
+
*/
|
|
9
|
+
window.__ModuleLoader__.load({
|
|
10
|
+
id: '@capyclaw/plugin-drop',
|
|
11
|
+
factory(require) {
|
|
12
|
+
const React = require('react')
|
|
13
|
+
const h = React.createElement
|
|
14
|
+
const NS = 'pluginDrop'
|
|
15
|
+
|
|
16
|
+
const DICT = {
|
|
17
|
+
en: {
|
|
18
|
+
tab: 'Drop to install',
|
|
19
|
+
heading: 'Drop a plugin package here',
|
|
20
|
+
hint: 'Accepts .zip, .tgz or .tar.gz containing a DSH plugin bundle.',
|
|
21
|
+
choose: 'Choose a file…',
|
|
22
|
+
busy: 'Reading the package…',
|
|
23
|
+
ready: 'Handing it to the installer…',
|
|
24
|
+
unavailable: 'The Host route is not reachable. Drag-and-drop install needs the Web GUI.',
|
|
25
|
+
unsupported: 'Unsupported file type. Use .zip, .tgz or .tar.gz.',
|
|
26
|
+
failed: 'Could not read the package:',
|
|
27
|
+
once: 'One archive at a time.',
|
|
28
|
+
note: 'The archive is unpacked into your local plugin folder and installed as a linked bundle.',
|
|
29
|
+
},
|
|
30
|
+
zh: {
|
|
31
|
+
tab: '拖拽安装',
|
|
32
|
+
heading: '把插件包拖到这里',
|
|
33
|
+
hint: '支持 .zip / .tgz / .tar.gz,压缩包里要有 DSH 插件(声明了 dsh.bundle.patch)。',
|
|
34
|
+
choose: '选择文件…',
|
|
35
|
+
busy: '正在读取插件包…',
|
|
36
|
+
ready: '正在交给安装对话框…',
|
|
37
|
+
unavailable: '没有连上宿主路由,拖拽安装只在 Web 版可用。',
|
|
38
|
+
unsupported: '不支持的文件类型,请用 .zip、.tgz 或 .tar.gz。',
|
|
39
|
+
failed: '读取插件包失败:',
|
|
40
|
+
once: '一次只能拖一个压缩包。',
|
|
41
|
+
note: '压缩包会解到本机插件目录,并以链接包的方式安装。',
|
|
42
|
+
},
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function useLocaleRevision(locale) {
|
|
46
|
+
const [revision, setRevision] = React.useState(() => locale.getSnapshot().revision)
|
|
47
|
+
React.useEffect(() => locale.subscribe(() => setRevision(locale.getSnapshot().revision)), [locale])
|
|
48
|
+
return revision
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function Icon() {
|
|
52
|
+
return h(
|
|
53
|
+
'svg',
|
|
54
|
+
{
|
|
55
|
+
viewBox: '0 0 48 48',
|
|
56
|
+
width: 40,
|
|
57
|
+
height: 40,
|
|
58
|
+
fill: 'none',
|
|
59
|
+
'aria-hidden': true,
|
|
60
|
+
style: { display: 'block', margin: '0 auto 14px' },
|
|
61
|
+
},
|
|
62
|
+
h('path', {
|
|
63
|
+
d: 'M24 6v21m0 0 8-8m-8 8-8-8',
|
|
64
|
+
stroke: 'var(--dsw-alias-brand-primary)',
|
|
65
|
+
strokeWidth: 2.4,
|
|
66
|
+
strokeLinecap: 'round',
|
|
67
|
+
strokeLinejoin: 'round',
|
|
68
|
+
}),
|
|
69
|
+
h('path', {
|
|
70
|
+
d: 'M8 32v6a4 4 0 0 0 4 4h24a4 4 0 0 0 4-4v-6',
|
|
71
|
+
stroke: 'var(--dsw-alias-brand-primary)',
|
|
72
|
+
strokeWidth: 2.4,
|
|
73
|
+
strokeLinecap: 'round',
|
|
74
|
+
}),
|
|
75
|
+
)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function DropTab(props) {
|
|
79
|
+
const { install, locale } = props
|
|
80
|
+
useLocaleRevision(locale)
|
|
81
|
+
const t = locale.bind(NS)
|
|
82
|
+
const [over, setOver] = React.useState(false)
|
|
83
|
+
const [phase, setPhase] = React.useState({ kind: 'idle' })
|
|
84
|
+
const input = React.useRef(null)
|
|
85
|
+
|
|
86
|
+
const take = React.useCallback(
|
|
87
|
+
async (file) => {
|
|
88
|
+
if (!file) return
|
|
89
|
+
if (!/\.(zip|tgz|tar\.gz|tar)$/i.test(file.name)) {
|
|
90
|
+
setPhase({ kind: 'error', message: t('unsupported') })
|
|
91
|
+
return
|
|
92
|
+
}
|
|
93
|
+
setPhase({ kind: 'busy', name: file.name })
|
|
94
|
+
try {
|
|
95
|
+
const response = await fetch('/dsh-plugin-drop/prepare', {
|
|
96
|
+
method: 'POST',
|
|
97
|
+
headers: {
|
|
98
|
+
'content-type': 'application/octet-stream',
|
|
99
|
+
'x-dsh-filename': encodeURIComponent(file.name),
|
|
100
|
+
},
|
|
101
|
+
body: file,
|
|
102
|
+
})
|
|
103
|
+
const payload = await response.json().catch(() => null)
|
|
104
|
+
if (response.status === 404 || response.status === 405) throw new Error(t('unavailable'))
|
|
105
|
+
if (!response.ok || !payload) throw new Error(`HTTP ${response.status}`)
|
|
106
|
+
if (!payload.ok) throw new Error(payload.message || payload.error || `HTTP ${response.status}`)
|
|
107
|
+
setPhase({ kind: 'ready' })
|
|
108
|
+
if (typeof install === 'function') {
|
|
109
|
+
install({ spec: payload.spec, title: payload.name, version: payload.version })
|
|
110
|
+
}
|
|
111
|
+
setPhase({ kind: 'idle' })
|
|
112
|
+
} catch (error) {
|
|
113
|
+
const message = error && error.message ? error.message : String(error)
|
|
114
|
+
setPhase({ kind: 'error', message: message === t('unavailable') ? message : `${t('failed')} ${message}` })
|
|
115
|
+
}
|
|
116
|
+
},
|
|
117
|
+
[install, t],
|
|
118
|
+
)
|
|
119
|
+
|
|
120
|
+
const onDrop = (event) => {
|
|
121
|
+
event.preventDefault()
|
|
122
|
+
event.stopPropagation()
|
|
123
|
+
setOver(false)
|
|
124
|
+
const files = event.dataTransfer && event.dataTransfer.files
|
|
125
|
+
if (!files || files.length === 0) return
|
|
126
|
+
if (files.length > 1) setPhase({ kind: 'error', message: t('once') })
|
|
127
|
+
take(files[0])
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const zoneStyle = {
|
|
131
|
+
border: `1px dashed ${over ? 'var(--dsw-alias-brand-primary)' : 'var(--dsw-alias-border-l2)'}`,
|
|
132
|
+
borderRadius: 12,
|
|
133
|
+
background: over ? 'var(--dsw-alias-bg-layer-2)' : 'var(--dsw-alias-bg-layer-1)',
|
|
134
|
+
padding: '56px 24px',
|
|
135
|
+
textAlign: 'center',
|
|
136
|
+
color: 'var(--dsw-alias-label-primary)',
|
|
137
|
+
transition: 'background 120ms ease, border-color 120ms ease',
|
|
138
|
+
cursor: 'pointer',
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const body =
|
|
142
|
+
phase.kind === 'busy' || phase.kind === 'ready'
|
|
143
|
+
? h(
|
|
144
|
+
'div',
|
|
145
|
+
{ style: { display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 8 } },
|
|
146
|
+
h(Icon),
|
|
147
|
+
h('div', { style: { fontSize: 15, fontWeight: 600 } }, t('busy')),
|
|
148
|
+
h(
|
|
149
|
+
'div',
|
|
150
|
+
{ style: { fontSize: 12.5, color: 'var(--dsw-alias-label-secondary)' } },
|
|
151
|
+
phase.name || '',
|
|
152
|
+
),
|
|
153
|
+
)
|
|
154
|
+
: h(
|
|
155
|
+
'div',
|
|
156
|
+
null,
|
|
157
|
+
h(Icon),
|
|
158
|
+
h('div', { style: { fontSize: 16, fontWeight: 600 } }, t('heading')),
|
|
159
|
+
h(
|
|
160
|
+
'div',
|
|
161
|
+
{
|
|
162
|
+
style: {
|
|
163
|
+
fontSize: 12.5,
|
|
164
|
+
lineHeight: 1.7,
|
|
165
|
+
color: 'var(--dsw-alias-label-secondary)',
|
|
166
|
+
marginTop: 8,
|
|
167
|
+
maxWidth: 420,
|
|
168
|
+
marginLeft: 'auto',
|
|
169
|
+
marginRight: 'auto',
|
|
170
|
+
},
|
|
171
|
+
},
|
|
172
|
+
t('hint'),
|
|
173
|
+
),
|
|
174
|
+
h(
|
|
175
|
+
'button',
|
|
176
|
+
{
|
|
177
|
+
type: 'button',
|
|
178
|
+
onClick: (event) => {
|
|
179
|
+
event.stopPropagation()
|
|
180
|
+
if (input.current) input.current.click()
|
|
181
|
+
},
|
|
182
|
+
onDragOver: (event) => event.stopPropagation(),
|
|
183
|
+
style: {
|
|
184
|
+
marginTop: 18,
|
|
185
|
+
padding: '7px 16px',
|
|
186
|
+
borderRadius: 8,
|
|
187
|
+
border: '1px solid var(--dsw-alias-border-l1)',
|
|
188
|
+
background: 'var(--dsw-alias-bg-overlay)',
|
|
189
|
+
color: 'var(--dsw-alias-label-primary)',
|
|
190
|
+
fontSize: 13,
|
|
191
|
+
cursor: 'pointer',
|
|
192
|
+
},
|
|
193
|
+
},
|
|
194
|
+
t('choose'),
|
|
195
|
+
),
|
|
196
|
+
)
|
|
197
|
+
|
|
198
|
+
return h(
|
|
199
|
+
'div',
|
|
200
|
+
{ style: { margin: '8px 0 32px' } },
|
|
201
|
+
h(
|
|
202
|
+
'div',
|
|
203
|
+
{
|
|
204
|
+
onDragOver: (event) => {
|
|
205
|
+
event.preventDefault()
|
|
206
|
+
if (event.dataTransfer) event.dataTransfer.dropEffect = 'copy'
|
|
207
|
+
setOver(true)
|
|
208
|
+
},
|
|
209
|
+
onDragEnter: (event) => {
|
|
210
|
+
event.preventDefault()
|
|
211
|
+
setOver(true)
|
|
212
|
+
},
|
|
213
|
+
onDragLeave: () => setOver(false),
|
|
214
|
+
onDrop,
|
|
215
|
+
onClick: () => {
|
|
216
|
+
if (phase.kind === 'idle' && input.current) input.current.click()
|
|
217
|
+
},
|
|
218
|
+
style: zoneStyle,
|
|
219
|
+
},
|
|
220
|
+
body,
|
|
221
|
+
),
|
|
222
|
+
h(
|
|
223
|
+
'input',
|
|
224
|
+
{
|
|
225
|
+
type: 'file',
|
|
226
|
+
accept: '.zip,.tgz,.tar.gz,.tar,application/zip',
|
|
227
|
+
ref: input,
|
|
228
|
+
style: { display: 'none' },
|
|
229
|
+
onChange: (event) => {
|
|
230
|
+
const chosen = event.target.files && event.target.files[0]
|
|
231
|
+
take(chosen)
|
|
232
|
+
event.target.value = ''
|
|
233
|
+
},
|
|
234
|
+
},
|
|
235
|
+
),
|
|
236
|
+
phase.kind === 'error'
|
|
237
|
+
? h(
|
|
238
|
+
'div',
|
|
239
|
+
{
|
|
240
|
+
role: 'alert',
|
|
241
|
+
style: {
|
|
242
|
+
marginTop: 12,
|
|
243
|
+
fontSize: 12.5,
|
|
244
|
+
lineHeight: 1.6,
|
|
245
|
+
color: 'var(--dsw-alias-state-error-primary)',
|
|
246
|
+
},
|
|
247
|
+
},
|
|
248
|
+
phase.message,
|
|
249
|
+
)
|
|
250
|
+
: null,
|
|
251
|
+
h(
|
|
252
|
+
'div',
|
|
253
|
+
{
|
|
254
|
+
style: {
|
|
255
|
+
marginTop: 12,
|
|
256
|
+
fontSize: 11.5,
|
|
257
|
+
lineHeight: 1.6,
|
|
258
|
+
color: 'var(--dsw-alias-label-secondary)',
|
|
259
|
+
textAlign: 'center',
|
|
260
|
+
},
|
|
261
|
+
},
|
|
262
|
+
t('note'),
|
|
263
|
+
),
|
|
264
|
+
)
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
return {
|
|
268
|
+
inject: ['slots', 'locale'],
|
|
269
|
+
apply(ctx) {
|
|
270
|
+
ctx.effect(() => ctx.locale.register(NS, 'en', DICT.en), 'plugin-drop: en dictionary')
|
|
271
|
+
ctx.effect(() => ctx.locale.register(NS, 'zh', DICT.zh), 'plugin-drop: zh dictionary')
|
|
272
|
+
const label = ctx.locale.bind(NS)
|
|
273
|
+
ctx.slots.inject('plugins.tab', () =>
|
|
274
|
+
ctx.slots.register(
|
|
275
|
+
{
|
|
276
|
+
name: 'plugins.tab',
|
|
277
|
+
id: 'plugin-drop',
|
|
278
|
+
order: 40,
|
|
279
|
+
label: () => label('tab'),
|
|
280
|
+
},
|
|
281
|
+
(props) => h(DropTab, { install: props.install, locale: ctx.locale }),
|
|
282
|
+
),
|
|
283
|
+
)
|
|
284
|
+
},
|
|
285
|
+
}
|
|
286
|
+
},
|
|
287
|
+
})
|
package/cordis.patch.yml
ADDED