dsh-powertools 0.0.0-stage → 0.2.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 +98 -2
- package/client.js +3213 -0
- package/cordis.patch.yml +3 -0
- package/icon.svg +9 -0
- package/index.js +90 -0
- package/lib/credentials.js +173 -0
- package/lib/enhance.js +159 -0
- package/lib/errors.js +11 -0
- package/lib/instructions.js +298 -0
- package/lib/mcp.js +124 -0
- package/lib/platform.js +70 -0
- package/lib/sessions.js +516 -0
- package/lib/skill-repositories.js +152 -0
- package/lib/skills.js +280 -0
- package/lib/tasks.js +244 -0
- package/licenses/lucide.txt +43 -0
- package/licenses/magi-code.txt +203 -0
- package/locale/en.json +1 -0
- package/locale/zh.json +1 -0
- package/node_modules/yaml/LICENSE +13 -0
- package/node_modules/yaml/README.md +172 -0
- package/node_modules/yaml/bin.mjs +11 -0
- package/node_modules/yaml/browser/dist/compose/compose-collection.js +88 -0
- package/node_modules/yaml/browser/dist/compose/compose-doc.js +43 -0
- package/node_modules/yaml/browser/dist/compose/compose-node.js +109 -0
- package/node_modules/yaml/browser/dist/compose/compose-scalar.js +86 -0
- package/node_modules/yaml/browser/dist/compose/composer.js +219 -0
- package/node_modules/yaml/browser/dist/compose/resolve-block-map.js +115 -0
- package/node_modules/yaml/browser/dist/compose/resolve-block-scalar.js +198 -0
- package/node_modules/yaml/browser/dist/compose/resolve-block-seq.js +49 -0
- package/node_modules/yaml/browser/dist/compose/resolve-end.js +37 -0
- package/node_modules/yaml/browser/dist/compose/resolve-flow-collection.js +207 -0
- package/node_modules/yaml/browser/dist/compose/resolve-flow-scalar.js +226 -0
- package/node_modules/yaml/browser/dist/compose/resolve-props.js +146 -0
- package/node_modules/yaml/browser/dist/compose/util-contains-newline.js +34 -0
- package/node_modules/yaml/browser/dist/compose/util-empty-scalar-position.js +26 -0
- package/node_modules/yaml/browser/dist/compose/util-flow-indent-check.js +15 -0
- package/node_modules/yaml/browser/dist/compose/util-map-includes.js +13 -0
- package/node_modules/yaml/browser/dist/doc/Document.js +335 -0
- package/node_modules/yaml/browser/dist/doc/anchors.js +71 -0
- package/node_modules/yaml/browser/dist/doc/applyReviver.js +55 -0
- package/node_modules/yaml/browser/dist/doc/createNode.js +88 -0
- package/node_modules/yaml/browser/dist/doc/directives.js +176 -0
- package/node_modules/yaml/browser/dist/errors.js +57 -0
- package/node_modules/yaml/browser/dist/index.js +17 -0
- package/node_modules/yaml/browser/dist/log.js +11 -0
- package/node_modules/yaml/browser/dist/nodes/Alias.js +118 -0
- package/node_modules/yaml/browser/dist/nodes/Collection.js +147 -0
- package/node_modules/yaml/browser/dist/nodes/Node.js +38 -0
- package/node_modules/yaml/browser/dist/nodes/Pair.js +36 -0
- package/node_modules/yaml/browser/dist/nodes/Scalar.js +24 -0
- package/node_modules/yaml/browser/dist/nodes/YAMLMap.js +144 -0
- package/node_modules/yaml/browser/dist/nodes/YAMLSeq.js +113 -0
- package/node_modules/yaml/browser/dist/nodes/addPairToJSMap.js +63 -0
- package/node_modules/yaml/browser/dist/nodes/identity.js +36 -0
- package/node_modules/yaml/browser/dist/nodes/toJS.js +37 -0
- package/node_modules/yaml/browser/dist/parse/cst-scalar.js +214 -0
- package/node_modules/yaml/browser/dist/parse/cst-stringify.js +61 -0
- package/node_modules/yaml/browser/dist/parse/cst-visit.js +97 -0
- package/node_modules/yaml/browser/dist/parse/cst.js +98 -0
- package/node_modules/yaml/browser/dist/parse/lexer.js +721 -0
- package/node_modules/yaml/browser/dist/parse/line-counter.js +39 -0
- package/node_modules/yaml/browser/dist/parse/parser.js +975 -0
- package/node_modules/yaml/browser/dist/public-api.js +102 -0
- package/node_modules/yaml/browser/dist/schema/Schema.js +37 -0
- package/node_modules/yaml/browser/dist/schema/common/map.js +17 -0
- package/node_modules/yaml/browser/dist/schema/common/null.js +15 -0
- package/node_modules/yaml/browser/dist/schema/common/seq.js +17 -0
- package/node_modules/yaml/browser/dist/schema/common/string.js +14 -0
- package/node_modules/yaml/browser/dist/schema/core/bool.js +19 -0
- package/node_modules/yaml/browser/dist/schema/core/float.js +43 -0
- package/node_modules/yaml/browser/dist/schema/core/int.js +38 -0
- package/node_modules/yaml/browser/dist/schema/core/schema.js +23 -0
- package/node_modules/yaml/browser/dist/schema/json/schema.js +62 -0
- package/node_modules/yaml/browser/dist/schema/tags.js +96 -0
- package/node_modules/yaml/browser/dist/schema/yaml-1.1/binary.js +58 -0
- package/node_modules/yaml/browser/dist/schema/yaml-1.1/bool.js +26 -0
- package/node_modules/yaml/browser/dist/schema/yaml-1.1/float.js +46 -0
- package/node_modules/yaml/browser/dist/schema/yaml-1.1/int.js +71 -0
- package/node_modules/yaml/browser/dist/schema/yaml-1.1/merge.js +67 -0
- package/node_modules/yaml/browser/dist/schema/yaml-1.1/omap.js +74 -0
- package/node_modules/yaml/browser/dist/schema/yaml-1.1/pairs.js +78 -0
- package/node_modules/yaml/browser/dist/schema/yaml-1.1/schema.js +39 -0
- package/node_modules/yaml/browser/dist/schema/yaml-1.1/set.js +93 -0
- package/node_modules/yaml/browser/dist/schema/yaml-1.1/timestamp.js +101 -0
- package/node_modules/yaml/browser/dist/stringify/foldFlowLines.js +146 -0
- package/node_modules/yaml/browser/dist/stringify/stringify.js +129 -0
- package/node_modules/yaml/browser/dist/stringify/stringifyCollection.js +153 -0
- package/node_modules/yaml/browser/dist/stringify/stringifyComment.js +20 -0
- package/node_modules/yaml/browser/dist/stringify/stringifyDocument.js +85 -0
- package/node_modules/yaml/browser/dist/stringify/stringifyNumber.js +25 -0
- package/node_modules/yaml/browser/dist/stringify/stringifyPair.js +150 -0
- package/node_modules/yaml/browser/dist/stringify/stringifyString.js +336 -0
- package/node_modules/yaml/browser/dist/util.js +11 -0
- package/node_modules/yaml/browser/dist/visit.js +233 -0
- package/node_modules/yaml/browser/index.js +5 -0
- package/node_modules/yaml/browser/package.json +3 -0
- package/node_modules/yaml/dist/cli.d.ts +8 -0
- package/node_modules/yaml/dist/cli.mjs +201 -0
- package/node_modules/yaml/dist/compose/compose-collection.d.ts +11 -0
- package/node_modules/yaml/dist/compose/compose-collection.js +90 -0
- package/node_modules/yaml/dist/compose/compose-doc.d.ts +7 -0
- package/node_modules/yaml/dist/compose/compose-doc.js +45 -0
- package/node_modules/yaml/dist/compose/compose-node.d.ts +29 -0
- package/node_modules/yaml/dist/compose/compose-node.js +112 -0
- package/node_modules/yaml/dist/compose/compose-scalar.d.ts +5 -0
- package/node_modules/yaml/dist/compose/compose-scalar.js +88 -0
- package/node_modules/yaml/dist/compose/composer.d.ts +63 -0
- package/node_modules/yaml/dist/compose/composer.js +224 -0
- package/node_modules/yaml/dist/compose/resolve-block-map.d.ts +6 -0
- package/node_modules/yaml/dist/compose/resolve-block-map.js +117 -0
- package/node_modules/yaml/dist/compose/resolve-block-scalar.d.ts +11 -0
- package/node_modules/yaml/dist/compose/resolve-block-scalar.js +200 -0
- package/node_modules/yaml/dist/compose/resolve-block-seq.d.ts +6 -0
- package/node_modules/yaml/dist/compose/resolve-block-seq.js +51 -0
- package/node_modules/yaml/dist/compose/resolve-end.d.ts +6 -0
- package/node_modules/yaml/dist/compose/resolve-end.js +39 -0
- package/node_modules/yaml/dist/compose/resolve-flow-collection.d.ts +7 -0
- package/node_modules/yaml/dist/compose/resolve-flow-collection.js +209 -0
- package/node_modules/yaml/dist/compose/resolve-flow-scalar.d.ts +10 -0
- package/node_modules/yaml/dist/compose/resolve-flow-scalar.js +228 -0
- package/node_modules/yaml/dist/compose/resolve-props.d.ts +23 -0
- package/node_modules/yaml/dist/compose/resolve-props.js +148 -0
- package/node_modules/yaml/dist/compose/util-contains-newline.d.ts +2 -0
- package/node_modules/yaml/dist/compose/util-contains-newline.js +36 -0
- package/node_modules/yaml/dist/compose/util-empty-scalar-position.d.ts +2 -0
- package/node_modules/yaml/dist/compose/util-empty-scalar-position.js +28 -0
- package/node_modules/yaml/dist/compose/util-flow-indent-check.d.ts +3 -0
- package/node_modules/yaml/dist/compose/util-flow-indent-check.js +17 -0
- package/node_modules/yaml/dist/compose/util-map-includes.d.ts +4 -0
- package/node_modules/yaml/dist/compose/util-map-includes.js +15 -0
- package/node_modules/yaml/dist/doc/Document.d.ts +141 -0
- package/node_modules/yaml/dist/doc/Document.js +337 -0
- package/node_modules/yaml/dist/doc/anchors.d.ts +24 -0
- package/node_modules/yaml/dist/doc/anchors.js +76 -0
- package/node_modules/yaml/dist/doc/applyReviver.d.ts +9 -0
- package/node_modules/yaml/dist/doc/applyReviver.js +57 -0
- package/node_modules/yaml/dist/doc/createNode.d.ts +17 -0
- package/node_modules/yaml/dist/doc/createNode.js +90 -0
- package/node_modules/yaml/dist/doc/directives.d.ts +49 -0
- package/node_modules/yaml/dist/doc/directives.js +178 -0
- package/node_modules/yaml/dist/errors.d.ts +21 -0
- package/node_modules/yaml/dist/errors.js +62 -0
- package/node_modules/yaml/dist/index.d.ts +25 -0
- package/node_modules/yaml/dist/index.js +50 -0
- package/node_modules/yaml/dist/log.d.ts +3 -0
- package/node_modules/yaml/dist/log.js +19 -0
- package/node_modules/yaml/dist/nodes/Alias.d.ts +29 -0
- package/node_modules/yaml/dist/nodes/Alias.js +120 -0
- package/node_modules/yaml/dist/nodes/Collection.d.ts +73 -0
- package/node_modules/yaml/dist/nodes/Collection.js +151 -0
- package/node_modules/yaml/dist/nodes/Node.d.ts +53 -0
- package/node_modules/yaml/dist/nodes/Node.js +40 -0
- package/node_modules/yaml/dist/nodes/Pair.d.ts +22 -0
- package/node_modules/yaml/dist/nodes/Pair.js +39 -0
- package/node_modules/yaml/dist/nodes/Scalar.d.ts +47 -0
- package/node_modules/yaml/dist/nodes/Scalar.js +27 -0
- package/node_modules/yaml/dist/nodes/YAMLMap.d.ts +53 -0
- package/node_modules/yaml/dist/nodes/YAMLMap.js +147 -0
- package/node_modules/yaml/dist/nodes/YAMLSeq.d.ts +60 -0
- package/node_modules/yaml/dist/nodes/YAMLSeq.js +115 -0
- package/node_modules/yaml/dist/nodes/addPairToJSMap.d.ts +4 -0
- package/node_modules/yaml/dist/nodes/addPairToJSMap.js +65 -0
- package/node_modules/yaml/dist/nodes/identity.d.ts +23 -0
- package/node_modules/yaml/dist/nodes/identity.js +53 -0
- package/node_modules/yaml/dist/nodes/toJS.d.ts +29 -0
- package/node_modules/yaml/dist/nodes/toJS.js +39 -0
- package/node_modules/yaml/dist/options.d.ts +350 -0
- package/node_modules/yaml/dist/parse/cst-scalar.d.ts +64 -0
- package/node_modules/yaml/dist/parse/cst-scalar.js +218 -0
- package/node_modules/yaml/dist/parse/cst-stringify.d.ts +8 -0
- package/node_modules/yaml/dist/parse/cst-stringify.js +63 -0
- package/node_modules/yaml/dist/parse/cst-visit.d.ts +39 -0
- package/node_modules/yaml/dist/parse/cst-visit.js +99 -0
- package/node_modules/yaml/dist/parse/cst.d.ts +109 -0
- package/node_modules/yaml/dist/parse/cst.js +112 -0
- package/node_modules/yaml/dist/parse/lexer.d.ts +87 -0
- package/node_modules/yaml/dist/parse/lexer.js +723 -0
- package/node_modules/yaml/dist/parse/line-counter.d.ts +22 -0
- package/node_modules/yaml/dist/parse/line-counter.js +41 -0
- package/node_modules/yaml/dist/parse/parser.d.ts +84 -0
- package/node_modules/yaml/dist/parse/parser.js +980 -0
- package/node_modules/yaml/dist/public-api.d.ts +44 -0
- package/node_modules/yaml/dist/public-api.js +107 -0
- package/node_modules/yaml/dist/schema/Schema.d.ts +17 -0
- package/node_modules/yaml/dist/schema/Schema.js +39 -0
- package/node_modules/yaml/dist/schema/common/map.d.ts +2 -0
- package/node_modules/yaml/dist/schema/common/map.js +19 -0
- package/node_modules/yaml/dist/schema/common/null.d.ts +4 -0
- package/node_modules/yaml/dist/schema/common/null.js +17 -0
- package/node_modules/yaml/dist/schema/common/seq.d.ts +2 -0
- package/node_modules/yaml/dist/schema/common/seq.js +19 -0
- package/node_modules/yaml/dist/schema/common/string.d.ts +2 -0
- package/node_modules/yaml/dist/schema/common/string.js +16 -0
- package/node_modules/yaml/dist/schema/core/bool.d.ts +4 -0
- package/node_modules/yaml/dist/schema/core/bool.js +21 -0
- package/node_modules/yaml/dist/schema/core/float.d.ts +4 -0
- package/node_modules/yaml/dist/schema/core/float.js +47 -0
- package/node_modules/yaml/dist/schema/core/int.d.ts +4 -0
- package/node_modules/yaml/dist/schema/core/int.js +42 -0
- package/node_modules/yaml/dist/schema/core/schema.d.ts +1 -0
- package/node_modules/yaml/dist/schema/core/schema.js +25 -0
- package/node_modules/yaml/dist/schema/json/schema.d.ts +2 -0
- package/node_modules/yaml/dist/schema/json/schema.js +64 -0
- package/node_modules/yaml/dist/schema/json-schema.d.ts +69 -0
- package/node_modules/yaml/dist/schema/tags.d.ts +48 -0
- package/node_modules/yaml/dist/schema/tags.js +99 -0
- package/node_modules/yaml/dist/schema/types.d.ts +92 -0
- package/node_modules/yaml/dist/schema/yaml-1.1/binary.d.ts +2 -0
- package/node_modules/yaml/dist/schema/yaml-1.1/binary.js +70 -0
- package/node_modules/yaml/dist/schema/yaml-1.1/bool.d.ts +7 -0
- package/node_modules/yaml/dist/schema/yaml-1.1/bool.js +29 -0
- package/node_modules/yaml/dist/schema/yaml-1.1/float.d.ts +4 -0
- package/node_modules/yaml/dist/schema/yaml-1.1/float.js +50 -0
- package/node_modules/yaml/dist/schema/yaml-1.1/int.d.ts +5 -0
- package/node_modules/yaml/dist/schema/yaml-1.1/int.js +76 -0
- package/node_modules/yaml/dist/schema/yaml-1.1/merge.d.ts +9 -0
- package/node_modules/yaml/dist/schema/yaml-1.1/merge.js +71 -0
- package/node_modules/yaml/dist/schema/yaml-1.1/omap.d.ts +22 -0
- package/node_modules/yaml/dist/schema/yaml-1.1/omap.js +77 -0
- package/node_modules/yaml/dist/schema/yaml-1.1/pairs.d.ts +10 -0
- package/node_modules/yaml/dist/schema/yaml-1.1/pairs.js +82 -0
- package/node_modules/yaml/dist/schema/yaml-1.1/schema.d.ts +1 -0
- package/node_modules/yaml/dist/schema/yaml-1.1/schema.js +41 -0
- package/node_modules/yaml/dist/schema/yaml-1.1/set.d.ts +28 -0
- package/node_modules/yaml/dist/schema/yaml-1.1/set.js +96 -0
- package/node_modules/yaml/dist/schema/yaml-1.1/timestamp.d.ts +6 -0
- package/node_modules/yaml/dist/schema/yaml-1.1/timestamp.js +105 -0
- package/node_modules/yaml/dist/stringify/foldFlowLines.d.ts +34 -0
- package/node_modules/yaml/dist/stringify/foldFlowLines.js +151 -0
- package/node_modules/yaml/dist/stringify/stringify.d.ts +21 -0
- package/node_modules/yaml/dist/stringify/stringify.js +132 -0
- package/node_modules/yaml/dist/stringify/stringifyCollection.d.ts +17 -0
- package/node_modules/yaml/dist/stringify/stringifyCollection.js +155 -0
- package/node_modules/yaml/dist/stringify/stringifyComment.d.ts +10 -0
- package/node_modules/yaml/dist/stringify/stringifyComment.js +24 -0
- package/node_modules/yaml/dist/stringify/stringifyDocument.d.ts +4 -0
- package/node_modules/yaml/dist/stringify/stringifyDocument.js +87 -0
- package/node_modules/yaml/dist/stringify/stringifyNumber.d.ts +2 -0
- package/node_modules/yaml/dist/stringify/stringifyNumber.js +27 -0
- package/node_modules/yaml/dist/stringify/stringifyPair.d.ts +3 -0
- package/node_modules/yaml/dist/stringify/stringifyPair.js +152 -0
- package/node_modules/yaml/dist/stringify/stringifyString.d.ts +9 -0
- package/node_modules/yaml/dist/stringify/stringifyString.js +338 -0
- package/node_modules/yaml/dist/test-events.d.ts +4 -0
- package/node_modules/yaml/dist/test-events.js +134 -0
- package/node_modules/yaml/dist/util.d.ts +16 -0
- package/node_modules/yaml/dist/util.js +28 -0
- package/node_modules/yaml/dist/visit.d.ts +102 -0
- package/node_modules/yaml/dist/visit.js +236 -0
- package/node_modules/yaml/package.json +97 -0
- package/node_modules/yaml/util.js +2 -0
- package/package.json +74 -4
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
import { constants } from 'node:fs'
|
|
2
|
+
import { access, lstat, mkdir, mkdtemp, open, rm, writeFile } from 'node:fs/promises'
|
|
3
|
+
import path from 'node:path'
|
|
4
|
+
import { createHash, randomUUID } from 'node:crypto'
|
|
5
|
+
import { RequestError, requireValue } from './errors.js'
|
|
6
|
+
import { confinedPath, homeRoot, moveLocal, readOpenFlags, statOrMissing } from './platform.js'
|
|
7
|
+
|
|
8
|
+
const maxInstructionBytes = 512 * 1024
|
|
9
|
+
const baseCandidates = ['AGENTS.md', 'CLAUDE.md']
|
|
10
|
+
const localCandidates = ['AGENTS.local.md', 'CLAUDE.local.md']
|
|
11
|
+
const projectCandidates = [...baseCandidates, ...localCandidates]
|
|
12
|
+
const digest = text => createHash('sha256').update(text).digest('hex')
|
|
13
|
+
const idHash = value => createHash('sha256').update(value).digest('hex').slice(0, 16)
|
|
14
|
+
|
|
15
|
+
async function findProjectRoot(cwd) {
|
|
16
|
+
for (let current = path.resolve(cwd); ; current = path.dirname(current)) {
|
|
17
|
+
if (await statOrMissing(path.join(current, '.git'))) return current
|
|
18
|
+
if (path.dirname(current) === current) return path.resolve(cwd)
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function directoryChain(projectRoot, cwd) {
|
|
23
|
+
const chain = []
|
|
24
|
+
const resolvedRoot = path.resolve(projectRoot)
|
|
25
|
+
let current = path.resolve(cwd)
|
|
26
|
+
while (true) {
|
|
27
|
+
chain.push(current)
|
|
28
|
+
if (current === resolvedRoot) break
|
|
29
|
+
const parent = path.dirname(current)
|
|
30
|
+
if (parent === current) break
|
|
31
|
+
current = parent
|
|
32
|
+
}
|
|
33
|
+
return chain.reverse()
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
async function readInstructionFile(file) {
|
|
37
|
+
const info = await lstat(file)
|
|
38
|
+
requireValue(info.isFile() && info.nlink === 1 && info.size <= maxInstructionBytes, '指令文件须为普通文件,且不超过 512 KiB。', 409)
|
|
39
|
+
const handle = await open(file, readOpenFlags())
|
|
40
|
+
try {
|
|
41
|
+
const opened = await handle.stat()
|
|
42
|
+
requireValue(
|
|
43
|
+
opened.isFile() && opened.ino === info.ino && opened.dev === info.dev && opened.nlink === 1 && opened.size <= maxInstructionBytes,
|
|
44
|
+
'指令文件已变化,请刷新。',
|
|
45
|
+
409,
|
|
46
|
+
)
|
|
47
|
+
const buffer = Buffer.alloc(maxInstructionBytes + 1)
|
|
48
|
+
const { bytesRead } = await handle.read({ buffer, length: buffer.length })
|
|
49
|
+
requireValue(bytesRead <= maxInstructionBytes, '指令文件超过 512 KiB。', 409)
|
|
50
|
+
return { text: buffer.subarray(0, bytesRead).toString('utf8'), mode: opened.mode & 0o777, size: bytesRead, mtimeMs: opened.mtimeMs }
|
|
51
|
+
} finally {
|
|
52
|
+
await handle.close()
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
async function inspectCandidate(candidate) {
|
|
57
|
+
const info = await statOrMissing(candidate.file)
|
|
58
|
+
const dirInfo = await statOrMissing(candidate.dir)
|
|
59
|
+
if (!info) {
|
|
60
|
+
let creatable = false
|
|
61
|
+
if (dirInfo) {
|
|
62
|
+
if (dirInfo.isDirectory() && !dirInfo.isSymbolicLink()) {
|
|
63
|
+
try {
|
|
64
|
+
await access(candidate.dir, constants.W_OK)
|
|
65
|
+
creatable = true
|
|
66
|
+
} catch { creatable = false }
|
|
67
|
+
}
|
|
68
|
+
} else if (candidate.scope === 'user-dsh' || candidate.scope === 'user-agents') {
|
|
69
|
+
creatable = true
|
|
70
|
+
}
|
|
71
|
+
return {
|
|
72
|
+
...candidate,
|
|
73
|
+
exists: false,
|
|
74
|
+
editable: creatable,
|
|
75
|
+
bytes: 0,
|
|
76
|
+
mtimeMs: 0,
|
|
77
|
+
symlink: false,
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
if (info.isSymbolicLink() || !info.isFile()) {
|
|
81
|
+
return {
|
|
82
|
+
...candidate,
|
|
83
|
+
exists: true,
|
|
84
|
+
editable: false,
|
|
85
|
+
bytes: info.size ?? 0,
|
|
86
|
+
mtimeMs: info.mtimeMs ?? 0,
|
|
87
|
+
symlink: info.isSymbolicLink(),
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
let editable = false
|
|
91
|
+
try {
|
|
92
|
+
if (info.nlink === 1 && dirInfo?.isDirectory() && !dirInfo.isSymbolicLink() && await confinedPath(candidate.dir, candidate.file)) {
|
|
93
|
+
await access(candidate.file, constants.W_OK)
|
|
94
|
+
await access(candidate.dir, constants.W_OK)
|
|
95
|
+
editable = true
|
|
96
|
+
}
|
|
97
|
+
} catch {
|
|
98
|
+
editable = false
|
|
99
|
+
}
|
|
100
|
+
return {
|
|
101
|
+
...candidate,
|
|
102
|
+
exists: true,
|
|
103
|
+
editable,
|
|
104
|
+
bytes: info.size,
|
|
105
|
+
mtimeMs: info.mtimeMs,
|
|
106
|
+
symlink: false,
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export async function discoverInstructionCandidates(home, agentsHome, cwd) {
|
|
111
|
+
const candidates = []
|
|
112
|
+
const seen = new Set()
|
|
113
|
+
const add = entry => {
|
|
114
|
+
const resolved = path.resolve(entry.file)
|
|
115
|
+
if (seen.has(resolved)) return
|
|
116
|
+
seen.add(resolved)
|
|
117
|
+
candidates.push({
|
|
118
|
+
id: `${entry.scope}:${idHash(resolved)}`,
|
|
119
|
+
scope: entry.scope,
|
|
120
|
+
scopeLabel: entry.scopeLabel,
|
|
121
|
+
name: path.basename(resolved),
|
|
122
|
+
displayPath: entry.displayPath,
|
|
123
|
+
dir: path.dirname(resolved),
|
|
124
|
+
file: resolved,
|
|
125
|
+
primary: Boolean(entry.primary),
|
|
126
|
+
localOverlay: Boolean(entry.localOverlay),
|
|
127
|
+
})
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
add({
|
|
131
|
+
scope: 'user-dsh',
|
|
132
|
+
scopeLabel: '个人 · DSH',
|
|
133
|
+
displayPath: '~/.dsh/AGENTS.md',
|
|
134
|
+
file: path.join(home, 'AGENTS.md'),
|
|
135
|
+
primary: true,
|
|
136
|
+
})
|
|
137
|
+
add({
|
|
138
|
+
scope: 'user-agents',
|
|
139
|
+
scopeLabel: '个人 · Agents',
|
|
140
|
+
displayPath: '~/.agents/AGENTS.md',
|
|
141
|
+
file: path.join(agentsHome, 'AGENTS.md'),
|
|
142
|
+
primary: false,
|
|
143
|
+
})
|
|
144
|
+
|
|
145
|
+
if (cwd) {
|
|
146
|
+
const resolvedCwd = path.resolve(cwd)
|
|
147
|
+
const projectRoot = await findProjectRoot(resolvedCwd)
|
|
148
|
+
const dirs = directoryChain(projectRoot, resolvedCwd)
|
|
149
|
+
for (const dir of dirs) {
|
|
150
|
+
const relDir = path.relative(projectRoot, dir).split(path.sep).join('/')
|
|
151
|
+
const isRoot = dir === projectRoot
|
|
152
|
+
for (const fileName of projectCandidates) {
|
|
153
|
+
const displayPath = relDir ? `${relDir}/${fileName}` : fileName
|
|
154
|
+
add({
|
|
155
|
+
scope: isRoot ? 'project-root' : 'project-dir',
|
|
156
|
+
scopeLabel: isRoot ? '项目根目录' : `项目子目录 · ${relDir}`,
|
|
157
|
+
displayPath,
|
|
158
|
+
file: path.join(dir, fileName),
|
|
159
|
+
primary: dir === resolvedCwd && fileName === 'AGENTS.md',
|
|
160
|
+
localOverlay: localCandidates.includes(fileName),
|
|
161
|
+
})
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
return Promise.all(candidates.map(inspectCandidate))
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
export function instructionOperations(ctx) {
|
|
170
|
+
let tail = Promise.resolve()
|
|
171
|
+
|
|
172
|
+
async function resolveCandidates(body) {
|
|
173
|
+
const workspace = body.workspaceId ? ctx.workspaceRegistry.get(body.workspaceId) : undefined
|
|
174
|
+
requireValue(!body.workspaceId || workspace, '工作区已移除,请刷新。', 404)
|
|
175
|
+
const home = ctx.profileContext?.home ?? homeRoot(process.env.DSH_HOME, '.dsh')
|
|
176
|
+
const agentsHome = homeRoot(process.env.DSH_AGENTS_HOME, '.agents')
|
|
177
|
+
return discoverInstructionCandidates(home, agentsHome, workspace?.path)
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
function publicRow(entry) {
|
|
181
|
+
return {
|
|
182
|
+
id: entry.id,
|
|
183
|
+
scope: entry.scope,
|
|
184
|
+
scopeLabel: entry.scopeLabel,
|
|
185
|
+
name: entry.name,
|
|
186
|
+
displayPath: entry.displayPath,
|
|
187
|
+
path: entry.file,
|
|
188
|
+
exists: entry.exists,
|
|
189
|
+
editable: entry.editable,
|
|
190
|
+
bytes: entry.bytes,
|
|
191
|
+
mtimeMs: entry.mtimeMs,
|
|
192
|
+
symlink: entry.symlink,
|
|
193
|
+
primary: entry.primary,
|
|
194
|
+
localOverlay: entry.localOverlay,
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
async function runAction(body, signal) {
|
|
199
|
+
signal?.throwIfAborted()
|
|
200
|
+
const candidates = await resolveCandidates(body)
|
|
201
|
+
|
|
202
|
+
if (body.action === 'list') {
|
|
203
|
+
// Return all existing instruction files + primary creatable anchors (user-dsh AGENTS.md & workspace AGENTS.md)
|
|
204
|
+
const rows = candidates
|
|
205
|
+
.filter(item => item.exists || item.primary)
|
|
206
|
+
.map(publicRow)
|
|
207
|
+
return { instructions: rows }
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
const id = typeof body.id === 'string' ? body.id.trim() : ''
|
|
211
|
+
requireValue(id, '请指定指令文件。')
|
|
212
|
+
const target = candidates.find(item => item.id === id)
|
|
213
|
+
requireValue(target, '指令文件不在当前作用域的合法范围内。', 404)
|
|
214
|
+
|
|
215
|
+
if (body.action === 'preview') {
|
|
216
|
+
if (!target.exists) {
|
|
217
|
+
return {
|
|
218
|
+
...publicRow(target),
|
|
219
|
+
content: '',
|
|
220
|
+
revision: null,
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
requireValue(!target.symlink, '此指令文件为符号链接,暂不支持在工具箱内直接读取与写入。', 409)
|
|
224
|
+
const { text, size } = await readInstructionFile(target.file)
|
|
225
|
+
return {
|
|
226
|
+
...publicRow(target),
|
|
227
|
+
bytes: size,
|
|
228
|
+
content: text,
|
|
229
|
+
revision: digest(text),
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
if (body.action === 'save') {
|
|
234
|
+
requireValue(target.editable, '此指令文件当前不可写。', 409)
|
|
235
|
+
requireValue(typeof body.content === 'string', '请输入有效的 Markdown 内容。')
|
|
236
|
+
requireValue(Buffer.byteLength(body.content) <= maxInstructionBytes, '指令文件内容不能超过 512 KiB。')
|
|
237
|
+
|
|
238
|
+
if (!target.exists) {
|
|
239
|
+
requireValue(body.revision === null || body.revision === undefined || body.revision === '', '指令文件已存在或状态已变化,请刷新。', 409)
|
|
240
|
+
await mkdir(target.dir, { recursive: true })
|
|
241
|
+
const dirInfo = await lstat(target.dir)
|
|
242
|
+
requireValue(dirInfo.isDirectory() && !dirInfo.isSymbolicLink(), '指令文件所在目录不是本地普通目录。', 409)
|
|
243
|
+
const temporary = path.join(target.dir, `.powertools-${randomUUID()}.tmp`)
|
|
244
|
+
try {
|
|
245
|
+
await writeFile(temporary, body.content, { flag: 'wx', mode: 0o644 })
|
|
246
|
+
signal?.throwIfAborted()
|
|
247
|
+
requireValue(!await statOrMissing(target.file), '指令文件已被其他进程创建,请刷新。', 409)
|
|
248
|
+
await moveLocal(temporary, target.file)
|
|
249
|
+
} finally {
|
|
250
|
+
await rm(temporary, { force: true }).catch(() => {})
|
|
251
|
+
}
|
|
252
|
+
return { revision: digest(body.content), bytes: Buffer.byteLength(body.content) }
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
const { text, mode } = await readInstructionFile(target.file)
|
|
256
|
+
requireValue(body.revision === digest(text), '指令文件已被外部修改,请重新打开后再保存。', 409)
|
|
257
|
+
const temporary = path.join(target.dir, `.powertools-${randomUUID()}.tmp`)
|
|
258
|
+
try {
|
|
259
|
+
await writeFile(temporary, body.content, { flag: 'wx', mode })
|
|
260
|
+
signal?.throwIfAborted()
|
|
261
|
+
requireValue((await readInstructionFile(target.file)).text === text, '指令文件已被外部修改,请重新打开。', 409)
|
|
262
|
+
await moveLocal(temporary, target.file)
|
|
263
|
+
} finally {
|
|
264
|
+
await rm(temporary, { force: true }).catch(() => {})
|
|
265
|
+
}
|
|
266
|
+
return { revision: digest(body.content), bytes: Buffer.byteLength(body.content) }
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
if (body.action === 'remove') {
|
|
270
|
+
requireValue(target.exists && target.editable, '此指令文件不存在或不可移除。', 409)
|
|
271
|
+
requireValue(body.confirm === target.id, '请确认要移除的指令文件。', 409)
|
|
272
|
+
const { text } = await readInstructionFile(target.file)
|
|
273
|
+
requireValue(body.revision === digest(text), '指令文件已被修改,请刷新后重试。', 409)
|
|
274
|
+
const profileDir = ctx.profileContext?.dir
|
|
275
|
+
requireValue(typeof profileDir === 'string' && path.isAbsolute(profileDir), '无法定位可恢复目录。', 503)
|
|
276
|
+
const recoveryRoot = path.join(profileDir, 'powertools-instruction-recovery')
|
|
277
|
+
await mkdir(recoveryRoot, { recursive: true, mode: 0o700 })
|
|
278
|
+
const recovery = await mkdtemp(path.join(recoveryRoot, 'removed-'))
|
|
279
|
+
try {
|
|
280
|
+
signal?.throwIfAborted()
|
|
281
|
+
await moveLocal(target.file, path.join(recovery, target.name))
|
|
282
|
+
} catch (error) {
|
|
283
|
+
await rm(recovery, { recursive: true, force: true }).catch(() => {})
|
|
284
|
+
throw error
|
|
285
|
+
}
|
|
286
|
+
return { recovery }
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
throw new RequestError('指令文件操作无效。')
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
return (body, signal) => {
|
|
293
|
+
if (body.action === 'list' || body.action === 'preview') return runAction(body, signal)
|
|
294
|
+
const pending = tail.then(() => runAction(body, signal))
|
|
295
|
+
tail = pending.catch(() => {})
|
|
296
|
+
return pending
|
|
297
|
+
}
|
|
298
|
+
}
|
package/lib/mcp.js
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { RequestError, requireValue } from './errors.js'
|
|
2
|
+
import { stdioFields } from './platform.js'
|
|
3
|
+
|
|
4
|
+
const namespace = 'mcp-manager'
|
|
5
|
+
const fields = ['command', 'args', 'cwd', 'env', 'url', 'headers']
|
|
6
|
+
const empty = { command: '', args: [], cwd: '', env: {}, url: '', headers: {} }
|
|
7
|
+
const transports = ['stdio', 'streamable-http']
|
|
8
|
+
|
|
9
|
+
function servers(section) { return section?.servers ?? {} }
|
|
10
|
+
|
|
11
|
+
function descriptor(settings) {
|
|
12
|
+
return settings.describe({ redactSecrets: true }).find(row => row.ns === namespace)
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function mcpList(manager, settings) {
|
|
16
|
+
const doc = descriptor(settings)
|
|
17
|
+
const effective = servers(doc?.value)
|
|
18
|
+
const writable = Boolean(doc && settings.writable && typeof settings.mutate === 'function')
|
|
19
|
+
return {
|
|
20
|
+
revision: doc?.revision,
|
|
21
|
+
writable,
|
|
22
|
+
servers: manager.list().map(({ name, status, configured }) => {
|
|
23
|
+
const entry = Object.hasOwn(effective, name) ? effective[name] : undefined
|
|
24
|
+
const source = !configured ? 'retired' : !entry ? 'plugin' : Object.hasOwn(servers(doc.base), name) ? 'inherited' : 'user'
|
|
25
|
+
return {
|
|
26
|
+
name, source, transport: entry?.transport ?? null,
|
|
27
|
+
enabled: configured && (entry?.enabled ?? true),
|
|
28
|
+
status: status.state, toolCount: status.state === 'connected' ? status.toolCount : undefined,
|
|
29
|
+
editable: source === 'user' && writable && Object.hasOwn(servers(doc.user), name),
|
|
30
|
+
testable: configured && typeof manager.probe === 'function',
|
|
31
|
+
reconnectable: configured && (entry?.enabled ?? true) && typeof manager.restart === 'function',
|
|
32
|
+
}
|
|
33
|
+
}),
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function credentialTemplates(input, allowHeaderPrefix) {
|
|
38
|
+
requireValue(input && typeof input === 'object' && !Array.isArray(input), '凭据映射须为 JSON 对象。')
|
|
39
|
+
const keyPattern = allowHeaderPrefix ? /^[!#$%&'*+.^_\x60|~\w-]+$/ : /^[A-Za-z_][A-Za-z0-9_]*$/
|
|
40
|
+
for (const [key, value] of Object.entries(input)) {
|
|
41
|
+
requireValue(keyPattern.test(key) && !['__proto__', 'prototype', 'constructor'].includes(key), '凭据映射键无效。')
|
|
42
|
+
const template = /^(?:Bearer )?\$\{credential:[^\s{}]+\}$/
|
|
43
|
+
requireValue(typeof value === 'string' && template.test(value), '请使用 DSH 凭据引用,例如 ${credential:mcp.example.token}。')
|
|
44
|
+
}
|
|
45
|
+
return input
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function httpUrl(value) {
|
|
49
|
+
try {
|
|
50
|
+
const url = new URL(value)
|
|
51
|
+
if (['http:', 'https:'].includes(url.protocol) && !url.username && !url.password && !url.search && !url.hash) return url.href
|
|
52
|
+
} catch { /* Report a field-level validation error without echoing the URL. */ }
|
|
53
|
+
throw new RequestError('请输入 HTTP(S) MCP 地址。认证请使用 DSH 凭据引用。')
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function configEdits(input, previous) {
|
|
57
|
+
const transport = input.transport ?? previous?.transport
|
|
58
|
+
requireValue(transports.includes(transport), '请选择 MCP 传输方式。')
|
|
59
|
+
const switching = previous && transport !== previous.transport
|
|
60
|
+
requireValue(!switching || !previous.oauth, '请先在 DSH 中清除这个服务器的 OAuth 配置,再切换传输方式。')
|
|
61
|
+
const entry = { ...previous, ...switching && empty, transport }
|
|
62
|
+
const edited = { ...switching && empty, transport }
|
|
63
|
+
const allowed = transport === 'stdio' ? ['command', 'args', 'cwd', 'env'] : ['url', 'headers']
|
|
64
|
+
for (const field of allowed) {
|
|
65
|
+
if (!Object.hasOwn(input, field)) continue
|
|
66
|
+
const value = input[field]
|
|
67
|
+
edited[field] = field === 'env' || field === 'headers' ? credentialTemplates(value, field === 'headers') : value
|
|
68
|
+
}
|
|
69
|
+
if (transport === 'stdio') {
|
|
70
|
+
const next = { ...entry, ...edited }
|
|
71
|
+
const validated = stdioFields(next.command, next.args ?? [], next.cwd ?? '')
|
|
72
|
+
if (Object.hasOwn(edited, 'command')) edited.command = validated.command
|
|
73
|
+
} else if (Object.hasOwn(edited, 'url') || !previous || switching) {
|
|
74
|
+
edited.url = httpUrl(edited.url)
|
|
75
|
+
}
|
|
76
|
+
return edited
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export async function mcpAction(manager, settings, body, signal) {
|
|
80
|
+
const { action, name, revision } = body
|
|
81
|
+
const view = mcpList(manager, settings)
|
|
82
|
+
const row = view.servers.find(server => server.name === name)
|
|
83
|
+
if (action === 'probe') {
|
|
84
|
+
requireValue(row?.testable, '此服务器无法探测。', 409)
|
|
85
|
+
const result = await manager.probe(name, { signal, maxToolNames: 128 })
|
|
86
|
+
return { ok: result.ok, stage: result.ok ? undefined : result.stage, durationMs: result.durationMs,
|
|
87
|
+
toolCount: result.ok ? result.toolNames.length : undefined,
|
|
88
|
+
toolNames: result.ok ? result.toolNames : undefined,
|
|
89
|
+
truncated: result.ok ? result.truncated : undefined,
|
|
90
|
+
closed: result.closeConfirmed && !result.cleanupError }
|
|
91
|
+
}
|
|
92
|
+
if (action === 'restart') {
|
|
93
|
+
requireValue(row?.reconnectable, '请先启用服务器。', 409)
|
|
94
|
+
await manager.restart(name)
|
|
95
|
+
return {}
|
|
96
|
+
}
|
|
97
|
+
requireValue(view.writable && revision === view.revision, 'MCP 配置已变化或不可写,请刷新后重试。', 409)
|
|
98
|
+
requireValue(['create', 'edit', 'enable', 'disable', 'remove'].includes(action), 'MCP 操作无效。')
|
|
99
|
+
requireValue(typeof name === 'string' && /^[\w-]{1,64}$/.test(name) && !['__proto__', 'prototype', 'constructor'].includes(name), '服务器名称须为 1–64 个字母、数字、下划线或连字符。')
|
|
100
|
+
const key = ['servers', name]
|
|
101
|
+
let ops
|
|
102
|
+
if (action === 'create') {
|
|
103
|
+
requireValue(!row, '服务器名称已存在。', 409)
|
|
104
|
+
ops = [{ op: 'set', path: key, value: { ...configEdits(body, undefined), enabled: false } }]
|
|
105
|
+
} else {
|
|
106
|
+
requireValue(row?.editable, '只有用户配置的服务器可以修改。', 409)
|
|
107
|
+
if (action === 'edit') {
|
|
108
|
+
const previous = servers(descriptor(settings)?.value)[name]
|
|
109
|
+
const updated = configEdits(body, previous)
|
|
110
|
+
ops = fields.concat('transport').flatMap(field => {
|
|
111
|
+
return Object.hasOwn(updated, field) ? [{ op: 'set', path: [...key, field], value: updated[field] }] : []
|
|
112
|
+
})
|
|
113
|
+
} else if (action === 'remove') ops = [{ op: 'unset', path: key }]
|
|
114
|
+
else ops = [{ op: 'set', path: [...key, 'enabled'], value: action === 'enable' }]
|
|
115
|
+
}
|
|
116
|
+
try {
|
|
117
|
+
await settings.mutate(namespace, ops, revision)
|
|
118
|
+
} catch (error) {
|
|
119
|
+
if (error.code === 'SETTINGS_CONFLICT') throw new RequestError('MCP 配置已变化,请刷新后重试。', 409)
|
|
120
|
+
// Lower-layer diagnostics can contain configured endpoint data or arguments.
|
|
121
|
+
throw new RequestError('DSH 拒绝写入 MCP 配置。请检查服务器名称、传输配置及凭据引用。', 409)
|
|
122
|
+
}
|
|
123
|
+
return {}
|
|
124
|
+
}
|
package/lib/platform.js
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { constants } from 'node:fs'
|
|
2
|
+
import { cp, lstat, realpath, rename, rm } from 'node:fs/promises'
|
|
3
|
+
import { homedir } from 'node:os'
|
|
4
|
+
import path, { win32 } from 'node:path'
|
|
5
|
+
import { setTimeout } from 'node:timers/promises'
|
|
6
|
+
import { requireValue } from './errors.js'
|
|
7
|
+
|
|
8
|
+
export function stdioFields(command, args, cwd, platform = process.platform) {
|
|
9
|
+
requireValue(typeof command === 'string' && command.trim() && !command.includes('\0'), '请输入单独的可执行文件名或绝对路径。')
|
|
10
|
+
requireValue(Array.isArray(args) && args.length <= 100 && args.every(arg => typeof arg === 'string' && arg.length <= 8192 && !arg.includes('\0')), '进程参数须为 JSON 字符串数组。')
|
|
11
|
+
const paths = platform === 'win32' ? win32 : path.posix
|
|
12
|
+
requireValue(typeof cwd === 'string' && (!cwd || paths.isAbsolute(cwd) && !cwd.includes('\0')), '启动目录须为绝对路径。')
|
|
13
|
+
if (platform === 'win32') {
|
|
14
|
+
requireValue(!/\.(cmd|bat|ps1)$/i.test(command.trim()), 'Windows 请指定 .exe,例如 node.exe,并将脚本路径作为独立参数。')
|
|
15
|
+
}
|
|
16
|
+
return { command: command.trim(), args, cwd }
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function homeRoot(value, fallback) {
|
|
20
|
+
const configured = value?.trim()
|
|
21
|
+
if (!configured) return path.join(homedir(), fallback)
|
|
22
|
+
if (/^~[/\\]/.test(configured)) return path.resolve(homedir(), configured.slice(2))
|
|
23
|
+
return path.resolve(configured)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function inside(root, candidate) {
|
|
27
|
+
const relative = path.relative(root, candidate)
|
|
28
|
+
return relative !== '' && relative !== '..' && !relative.startsWith(`..${path.sep}`) && !path.isAbsolute(relative)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export async function statOrMissing(file) {
|
|
32
|
+
try { return await lstat(file) } catch (error) {
|
|
33
|
+
if (error.code === 'ENOENT' || error.code === 'ENOTDIR') return undefined
|
|
34
|
+
throw error
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export async function confinedPath(root, candidate) {
|
|
39
|
+
if (!inside(root, candidate)) return false
|
|
40
|
+
const realRoot = await realpath(root)
|
|
41
|
+
const realCandidate = await realpath(candidate)
|
|
42
|
+
return inside(realRoot, realCandidate)
|
|
43
|
+
&& path.relative(realRoot, realCandidate) === path.relative(root, candidate)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function readOpenFlags(platform = process.platform) {
|
|
47
|
+
if (platform === 'win32') return constants.O_RDONLY
|
|
48
|
+
return constants.O_RDONLY | (constants.O_NOFOLLOW ?? 0) | (constants.O_NONBLOCK ?? 0)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Node uses rename(2) on macOS and MoveFileExW on Windows. Transient Windows
|
|
52
|
+
// antivirus or indexer locks may fail with EPERM/EBUSY/EACCES; retry briefly
|
|
53
|
+
// and otherwise leave the original in place.
|
|
54
|
+
export async function moveLocal(source, destination, platform = process.platform) {
|
|
55
|
+
const retryable = new Set(['EPERM', 'EBUSY', 'EACCES'])
|
|
56
|
+
for (let attempt = 0; ; attempt++) {
|
|
57
|
+
try {
|
|
58
|
+
await rename(source, destination)
|
|
59
|
+
return
|
|
60
|
+
} catch (error) {
|
|
61
|
+
if (error.code === 'EXDEV') {
|
|
62
|
+
await cp(source, destination, { recursive: true, errorOnExist: true, force: false })
|
|
63
|
+
await rm(source, { recursive: true, force: true })
|
|
64
|
+
return
|
|
65
|
+
}
|
|
66
|
+
if (platform !== 'win32' || !retryable.has(error.code) || attempt >= 3) throw error
|
|
67
|
+
await setTimeout(50 * (attempt + 1))
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|