dsh-seal-scripts 0.4.10 → 0.4.12
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.en.md +22 -9
- package/README.md +26 -13
- package/lib/client.js +14 -26
- package/lib/core/catalog-snapshot.js +5 -8
- package/lib/core/host-limits.js +219 -0
- package/lib/core/index.js +17 -0
- package/lib/core/schema-runner.js +230 -128
- package/lib/core/schema-tools.js +105 -36
- package/lib/core/schema.js +59 -48
- package/lib/core/tool-transfer.js +174 -85
- package/lib/core/zip.js +328 -8
- package/lib/host/http-util.js +67 -24
- package/lib/host/routes/tools.js +69 -32
- package/package.json +1 -1
|
@@ -15,17 +15,25 @@
|
|
|
15
15
|
import fs from 'node:fs'
|
|
16
16
|
import fsp from 'node:fs/promises'
|
|
17
17
|
import path from 'node:path'
|
|
18
|
+
import { Readable } from 'node:stream'
|
|
19
|
+
import { pipeline } from 'node:stream/promises'
|
|
18
20
|
import {
|
|
19
21
|
BUNDLE_ID_RE, INDEX_FILE, RUNS_DIR, TOOL_SCRIPT_FILE,
|
|
20
22
|
assertToolScriptId, readIndex, runsDir, sha256OfFile, toolScriptDir, toolSequenceRoot, writeIndex
|
|
21
23
|
} from './schema.js'
|
|
22
|
-
import {
|
|
24
|
+
import {
|
|
25
|
+
readZip, safeEntryName, writeZipFile, zipUncompressedTotal, freeBytesOf, DISK_RESERVE_BYTES,
|
|
26
|
+
readZipCentralFromFile, forEachZipEntry
|
|
27
|
+
} from './zip.js'
|
|
28
|
+
import { DSH_DEFAULTS } from './host-limits.js'
|
|
23
29
|
|
|
24
30
|
/** 导出包的名字(用户要的就是这个名字)。 */
|
|
25
31
|
export const EXPORT_NAME = 'toolSequence.zip'
|
|
26
32
|
|
|
27
|
-
|
|
28
|
-
|
|
33
|
+
/*
|
|
34
|
+
* 0.4.11:删掉 24 MB 的导出上限。工具组就是用户的数据,多大数据就给多大——
|
|
35
|
+
* 而且不再先拼进内存再判大小,改成边读边写进目标文件(writeZipFile)。
|
|
36
|
+
*/
|
|
29
37
|
|
|
30
38
|
/** 打包时跳过、导入时也不收的路径片段。 */
|
|
31
39
|
function isTransientName(name) {
|
|
@@ -54,7 +62,8 @@ async function collect(dir, prefix, out) {
|
|
|
54
62
|
if (!item.isFile()) continue // 软链之类一律不收
|
|
55
63
|
const stat = await fsp.stat(abs).catch(() => null)
|
|
56
64
|
if (stat === null) continue
|
|
57
|
-
|
|
65
|
+
// 0.4.11:只记路径,不把内容读进内存;真正写包时按块流式拷贝。
|
|
66
|
+
out.push({ name, source: abs, mtime: stat.mtime })
|
|
58
67
|
}
|
|
59
68
|
}
|
|
60
69
|
|
|
@@ -80,18 +89,20 @@ export async function exportToolSequence({ dshHome, dir }) {
|
|
|
80
89
|
const root = toolSequenceRoot(dshHome)
|
|
81
90
|
const entries = []
|
|
82
91
|
await collect(root, '', entries)
|
|
83
|
-
const buffer = buildZip(entries)
|
|
84
92
|
const files = entries.filter((e) => !String(e.name).endsWith('/'))
|
|
85
|
-
// 先判大小再动磁盘:超限了连临时文件都不留。
|
|
86
|
-
if (buffer.length > MAX_EXPORT_BYTES) {
|
|
87
|
-
throw new Error('工具组目录太大(' + buffer.length + ' 字节),超过导出上限 ' + MAX_EXPORT_BYTES + ' 字节')
|
|
88
|
-
}
|
|
89
93
|
const file = path.join(target, EXPORT_NAME)
|
|
90
94
|
const overwritten = fs.existsSync(file)
|
|
91
95
|
const tmp = file + '.tmp-' + process.pid + '-' + Math.random().toString(36).slice(2, 8)
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
96
|
+
let written
|
|
97
|
+
try {
|
|
98
|
+
// 0.4.11:流式写盘——内存占用与工具组大小无关,也不再有任何上限。
|
|
99
|
+
written = await writeZipFile(tmp, entries)
|
|
100
|
+
await fsp.rename(tmp, file)
|
|
101
|
+
} catch (err) {
|
|
102
|
+
await fsp.rm(tmp, { force: true }).catch(() => {})
|
|
103
|
+
throw err
|
|
104
|
+
}
|
|
105
|
+
return { name: EXPORT_NAME, file, dir: target, size: written.bytes, files: files.length, dirs: entries.length - files.length, overwritten }
|
|
95
106
|
}
|
|
96
107
|
|
|
97
108
|
/**
|
|
@@ -118,7 +129,7 @@ function indexEntryFromDisk(dir, files) {
|
|
|
118
129
|
params: Array.isArray(manifest.params) ? manifest.params.map(String) : [],
|
|
119
130
|
steps: Array.isArray(manifest.steps) ? manifest.steps.map(String) : [],
|
|
120
131
|
dir: String(manifest.id || ''),
|
|
121
|
-
timeoutMs: Number.isFinite(Number(manifest.timeoutMs)) ? Number(manifest.timeoutMs) :
|
|
132
|
+
timeoutMs: Number.isFinite(Number(manifest.timeoutMs)) ? Number(manifest.timeoutMs) : DSH_DEFAULTS.timeoutMs,
|
|
122
133
|
updatedAt: new Date().toISOString(),
|
|
123
134
|
hashes
|
|
124
135
|
}
|
|
@@ -128,104 +139,185 @@ function indexEntryFromDisk(dir, files) {
|
|
|
128
139
|
* 手工把 toolSequence 文件夹压成包时,包里面会多一层 toolSequence/。
|
|
129
140
|
* 只有「所有条目都在这一个顶层目录下、而且它就叫 toolSequence」时才剥——
|
|
130
141
|
* 别的单层目录不猜,免得把某个 id 名字当壳剥掉。
|
|
131
|
-
*
|
|
132
|
-
*
|
|
142
|
+
*
|
|
143
|
+
* 0.4.11:改成只算出一个前缀。缓冲路径和流式路径共用它,
|
|
144
|
+
* 流式那边只看中央目录里的名字就能定前缀。
|
|
145
|
+
* @param {string[]} names
|
|
146
|
+
* @returns {string} 要剥掉的前缀(含结尾 /),没有就是空串
|
|
133
147
|
*/
|
|
134
|
-
function
|
|
148
|
+
function wrapperPrefix(names) {
|
|
135
149
|
const tops = new Set()
|
|
136
|
-
for (const
|
|
137
|
-
const parts = String(
|
|
150
|
+
for (const raw of names) {
|
|
151
|
+
const parts = String(raw).split('/').filter((x) => x !== '')
|
|
138
152
|
if (parts.length === 0) continue
|
|
139
153
|
tops.add(parts[0])
|
|
140
|
-
if (tops.size > 1) return
|
|
154
|
+
if (tops.size > 1) return ''
|
|
141
155
|
}
|
|
142
|
-
if (tops.size !== 1) return
|
|
156
|
+
if (tops.size !== 1) return ''
|
|
143
157
|
const only = [...tops][0]
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
158
|
+
return only.toLowerCase() === 'toolsequence' ? only + '/' : ''
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* 把一个条目名变成「落到哪个工具脚本、哪个相对路径」,或者该跳过。
|
|
163
|
+
* @param {string} rawName 包内原始名(已剥壳之后)
|
|
164
|
+
* @returns {{id:string, rel:string}|{skip:string}}
|
|
165
|
+
*/
|
|
166
|
+
function planEntry(rawName) {
|
|
167
|
+
let safe
|
|
168
|
+
try {
|
|
169
|
+
safe = safeEntryName(rawName)
|
|
170
|
+
} catch (err) {
|
|
171
|
+
return { skip: String(err.message || err) }
|
|
151
172
|
}
|
|
152
|
-
|
|
173
|
+
const parts = safe.split('/')
|
|
174
|
+
const top = parts[0]
|
|
175
|
+
if (parts.length === 1 && top === INDEX_FILE) {
|
|
176
|
+
return { skip: '本机索引按落地后的文件重建,包里的 _index.json 不采信' }
|
|
177
|
+
}
|
|
178
|
+
if (parts.length < 2) return { skip: '不在任何工具脚本目录里' }
|
|
179
|
+
if (top === RUNS_DIR) return { skip: '运行溢写目录,不收' }
|
|
180
|
+
if (!BUNDLE_ID_RE.test(top)) return { skip: '目录名不是合法的工具脚本 id' }
|
|
181
|
+
if (isTransientName(parts[1])) return { skip: '备份/临时目录,不收' }
|
|
182
|
+
return { id: top, rel: parts.slice(1).join('/') }
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* 把一串 Buffer 块写进文件(流式导入用)。
|
|
187
|
+
*
|
|
188
|
+
* 用 pipeline 而不是手写 write/drain:手写版本每块都挂一次 error 监听,
|
|
189
|
+
* 大包会把 EventEmitter 的监听器上限撑爆(实测 11 个就报警)。
|
|
190
|
+
* @param {string} target
|
|
191
|
+
* @param {AsyncIterable<Buffer>} chunks
|
|
192
|
+
*/
|
|
193
|
+
async function writeChunks(target, chunks) {
|
|
194
|
+
await pipeline(Readable.from(chunks), fs.createWriteStream(target))
|
|
153
195
|
}
|
|
154
196
|
|
|
155
197
|
/**
|
|
198
|
+
* 导入一个工具组包。
|
|
199
|
+
*
|
|
200
|
+
* 两种入口(0.4.11):
|
|
201
|
+
* buffer —— 老路径:整个包已经在内存里(设置页的 base64 JSON 请求走它);
|
|
202
|
+
* file —— 新路径:包落在磁盘上的临时文件里,逐条流式解出来直接写进 staging 目录,
|
|
203
|
+
* 内存里只留一块,包多大都不吃内存。
|
|
204
|
+
*
|
|
205
|
+
* 两条路径的落地语义完全一致:每个顶层 <id>/ 先写临时目录,全写完再整体换上去;
|
|
206
|
+
* 包里的 _index.json 不采信,按落地后的文件重建本机索引项。
|
|
156
207
|
*
|
|
157
208
|
* dryRun:只解析,不写盘——设置页先用它把「会装哪些、会盖哪些」念给用户听,点了确认才真装。
|
|
158
209
|
*
|
|
159
|
-
* @param {{dshHome:string, buffer
|
|
210
|
+
* @param {{dshHome:string, buffer?:Buffer, file?:string, dryRun?:boolean}} p
|
|
160
211
|
* @returns {Promise<{dryRun:boolean, imported:Array<{id:string,files:string[]}>, overwritten:string[], skipped:Array<{name:string,reason:string}>}>}
|
|
161
212
|
*/
|
|
162
|
-
export async function importToolSequence({ dshHome, buffer, dryRun = false }) {
|
|
213
|
+
export async function importToolSequence({ dshHome, buffer, file, dryRun = false }) {
|
|
163
214
|
const root = toolSequenceRoot(dshHome)
|
|
164
|
-
const
|
|
165
|
-
const stripped = stripWrapperDir(entries)
|
|
215
|
+
const fromFile = typeof file === 'string' && file !== ''
|
|
166
216
|
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
continue
|
|
179
|
-
}
|
|
180
|
-
if (parts.length < 2) { skipped.push({ name: entry.name, reason: '不在任何工具脚本目录里' }); continue }
|
|
181
|
-
if (top === RUNS_DIR) { skipped.push({ name: entry.name, reason: '运行溢写目录,不收' }); continue }
|
|
182
|
-
if (!BUNDLE_ID_RE.test(top)) { skipped.push({ name: entry.name, reason: '目录名不是合法的工具脚本 id' }); continue }
|
|
183
|
-
if (isTransientName(parts[1])) { skipped.push({ name: entry.name, reason: '备份/临时目录,不收' }); continue }
|
|
184
|
-
const list = byId.get(top) || []
|
|
185
|
-
list.push({ rel: parts.slice(1).join('/'), data: entry.data })
|
|
186
|
-
byId.set(top, list)
|
|
217
|
+
// 0.4.11:解压炸弹按「磁盘装不装得下」判,不再拍 128 MB 的死数。
|
|
218
|
+
// 先读中央目录算解压后总字节(不解压、不占内存),再和可用空间比。
|
|
219
|
+
let need
|
|
220
|
+
let names
|
|
221
|
+
if (fromFile) {
|
|
222
|
+
const central = await readZipCentralFromFile(file)
|
|
223
|
+
need = central.reduce((sum, e) => sum + (e.isDir ? 0 : e.uncompSize), 0)
|
|
224
|
+
names = central.map((e) => e.name)
|
|
225
|
+
} else {
|
|
226
|
+
need = zipUncompressedTotal(buffer).bytes
|
|
227
|
+
names = readZip(buffer).map((e) => e.name)
|
|
187
228
|
}
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
throw new Error('
|
|
229
|
+
const probeDir = fs.existsSync(root) ? root : dshHome
|
|
230
|
+
const free = await freeBytesOf(probeDir)
|
|
231
|
+
if (free !== null && need + DISK_RESERVE_BYTES > free) {
|
|
232
|
+
throw new Error('装不下:解压后要 ' + need + ' 字节,磁盘只剩 ' + free + ' 字节(留余量 ' +
|
|
233
|
+
DISK_RESERVE_BYTES + ' 字节);先腾空间再导入')
|
|
192
234
|
}
|
|
193
235
|
|
|
236
|
+
const cut = wrapperPrefix(names)
|
|
194
237
|
const index = await readIndex(dshHome)
|
|
238
|
+
/** @type {Map<string,string>} id → staging 目录 */
|
|
239
|
+
const stages = new Map()
|
|
240
|
+
/** @type {Map<string,string[]>} id → 已写入的相对路径 */
|
|
241
|
+
const written = new Map()
|
|
195
242
|
const imported = []
|
|
196
243
|
const overwritten = []
|
|
244
|
+
const skipped = []
|
|
197
245
|
|
|
198
246
|
if (dryRun !== true) await fsp.mkdir(root, { recursive: true })
|
|
199
247
|
|
|
200
|
-
|
|
201
|
-
const
|
|
202
|
-
|
|
203
|
-
const exists = fs.existsSync(dest)
|
|
204
|
-
if (exists) overwritten.push(id)
|
|
205
|
-
imported.push({ id, files: files.map((f) => f.rel) })
|
|
206
|
-
if (dryRun === true) continue
|
|
207
|
-
|
|
248
|
+
const ensureStage = async (id) => {
|
|
249
|
+
const hit = stages.get(id)
|
|
250
|
+
if (hit !== undefined) return hit
|
|
208
251
|
const stage = path.join(root, id + '.tmp-import-' + process.pid + '-' + Math.random().toString(36).slice(2, 8))
|
|
209
252
|
await fsp.rm(stage, { recursive: true, force: true })
|
|
210
253
|
await fsp.mkdir(stage, { recursive: true })
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
254
|
+
stages.set(id, stage)
|
|
255
|
+
written.set(id, [])
|
|
256
|
+
imported.push({ id, files: written.get(id) })
|
|
257
|
+
return stage
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* 一个条目落地(或只记账)。
|
|
262
|
+
* @param {string} rawName 已经剥过壳的名字
|
|
263
|
+
* @param {(target:string) => Promise<void>} writeBody
|
|
264
|
+
*/
|
|
265
|
+
const takeEntry = async (rawName, writeBody) => {
|
|
266
|
+
const plan = planEntry(rawName)
|
|
267
|
+
if (plan.skip !== undefined) {
|
|
268
|
+
skipped.push({ name: rawName, reason: plan.skip })
|
|
269
|
+
return
|
|
224
270
|
}
|
|
271
|
+
const id = assertToolScriptId(plan.id)
|
|
272
|
+
if (!overwritten.includes(id) && fs.existsSync(toolScriptDir(dshHome, id))) overwritten.push(id)
|
|
273
|
+
if (dryRun === true) {
|
|
274
|
+
const hit = imported.find((x) => x.id === id)
|
|
275
|
+
if (hit === undefined) imported.push({ id, files: [plan.rel] })
|
|
276
|
+
else hit.files.push(plan.rel)
|
|
277
|
+
return
|
|
278
|
+
}
|
|
279
|
+
const stage = await ensureStage(id)
|
|
280
|
+
const target = path.join(stage, plan.rel)
|
|
281
|
+
const rel = path.relative(stage, target)
|
|
282
|
+
if (rel === '' || rel === '..' || rel.startsWith('..' + path.sep) || path.isAbsolute(rel)) {
|
|
283
|
+
throw new Error('越界路径:' + plan.rel)
|
|
284
|
+
}
|
|
285
|
+
await fsp.mkdir(path.dirname(target), { recursive: true })
|
|
286
|
+
await writeBody(target)
|
|
287
|
+
written.get(id).push(plan.rel)
|
|
288
|
+
}
|
|
225
289
|
|
|
290
|
+
if (fromFile) {
|
|
291
|
+
await forEachZipEntry(file, async (entry) => {
|
|
292
|
+
const name = cut !== '' && entry.name.startsWith(cut) ? entry.name.slice(cut.length) : entry.name
|
|
293
|
+
if (entry.isDir) return
|
|
294
|
+
if (cut !== '' && (entry.name === cut || entry.name === cut.slice(0, -1))) return
|
|
295
|
+
await takeEntry(name, (target) => new Promise((yes, no) => {
|
|
296
|
+
writeChunks(target, entry.read()).then(yes, no)
|
|
297
|
+
}))
|
|
298
|
+
})
|
|
299
|
+
} else {
|
|
300
|
+
for (const entry of readZip(buffer)) {
|
|
301
|
+
if (entry.isDir) continue
|
|
302
|
+
const name = cut !== '' && entry.name.startsWith(cut) ? entry.name.slice(cut.length) : entry.name
|
|
303
|
+
if (cut !== '' && (entry.name === cut || entry.name === cut.slice(0, -1))) continue
|
|
304
|
+
await takeEntry(name, (target) => fsp.writeFile(target, entry.data))
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
if (imported.length === 0) {
|
|
309
|
+
const why = skipped.length > 0 ? ('(' + skipped.slice(0, 3).map((s) => s.name + ':' + s.reason).join(';') + ')') : ''
|
|
310
|
+
for (const stage of stages.values()) await fsp.rm(stage, { recursive: true, force: true }).catch(() => {})
|
|
311
|
+
throw new Error('这个包里没有工具脚本目录' + why)
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
if (dryRun === true) return { dryRun: true, imported, overwritten, skipped }
|
|
315
|
+
|
|
316
|
+
for (const [id, stage] of stages) {
|
|
317
|
+
const dest = toolScriptDir(dshHome, id)
|
|
226
318
|
let backup = ''
|
|
227
319
|
try {
|
|
228
|
-
if (
|
|
320
|
+
if (fs.existsSync(dest)) {
|
|
229
321
|
backup = dest + '.bak-' + Date.now()
|
|
230
322
|
await fsp.rename(dest, backup)
|
|
231
323
|
}
|
|
@@ -235,15 +327,12 @@ export async function importToolSequence({ dshHome, buffer, dryRun = false }) {
|
|
|
235
327
|
if (backup !== '') await fsp.rename(backup, dest).catch(() => {})
|
|
236
328
|
throw err
|
|
237
329
|
}
|
|
238
|
-
|
|
239
|
-
const meta = indexEntryFromDisk(dest, files.map((f) => ({ name: f.rel, size: f.data.length })))
|
|
330
|
+
const meta = indexEntryFromDisk(dest, written.get(id).map((name) => ({ name })))
|
|
240
331
|
if (meta !== null) index.toolScripts[id] = meta
|
|
241
332
|
}
|
|
242
333
|
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
await fsp.mkdir(runsDir(dshHome), { recursive: true }).catch(() => {})
|
|
246
|
-
}
|
|
334
|
+
await writeIndex(dshHome, index)
|
|
335
|
+
await fsp.mkdir(runsDir(dshHome), { recursive: true }).catch(() => {})
|
|
247
336
|
|
|
248
|
-
return { dryRun:
|
|
337
|
+
return { dryRun: false, imported, overwritten, skipped }
|
|
249
338
|
}
|